pc_from_ucontext.m4 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. # We want to access the "PC" (Program Counter) register from a struct
  2. # ucontext. Every system has its own way of doing that. We try all the
  3. # possibilities we know about. Note REG_PC should come first (REG_RIP
  4. # is also defined on solaris, but does the wrong thing).
  5. # OpenBSD doesn't have ucontext.h, but we can get PC from ucontext_t
  6. # by using signal.h.
  7. # The first argument of AC_PC_FROM_UCONTEXT will be invoked when we
  8. # cannot find a way to obtain PC from ucontext.
  9. AC_DEFUN([AC_PC_FROM_UCONTEXT],
  10. [AC_CHECK_HEADERS(ucontext.h)
  11. # Redhat 7 has <sys/ucontext.h>, but it barfs if we #include it directly
  12. # (this was fixed in later redhats). <ucontext.h> works fine, so use that.
  13. if grep "Red Hat Linux release 7" /etc/redhat-release >/dev/null 2>&1; then
  14. AC_DEFINE(HAVE_SYS_UCONTEXT_H, 0, [<sys/ucontext.h> is broken on redhat 7])
  15. ac_cv_header_sys_ucontext_h=no
  16. else
  17. AC_CHECK_HEADERS(sys/ucontext.h) # ucontext on OS X 10.6 (at least)
  18. fi
  19. AC_CHECK_HEADERS(cygwin/signal.h) # ucontext on cywgin
  20. AC_CHECK_HEADERS(asm/ptrace.h) # get ptrace macros, e.g. PT_NIP
  21. AC_MSG_CHECKING([how to access the program counter from a struct ucontext])
  22. pc_fields=" uc_mcontext.gregs[[REG_PC]]" # Solaris x86 (32 + 64 bit)
  23. pc_fields="$pc_fields uc_mcontext.gregs[[REG_EIP]]" # Linux (i386)
  24. pc_fields="$pc_fields uc_mcontext.gregs[[REG_RIP]]" # Linux (x86_64)
  25. pc_fields="$pc_fields uc_mcontext.sc_ip" # Linux (ia64)
  26. pc_fields="$pc_fields uc_mcontext.pc" # Linux (mips)
  27. pc_fields="$pc_fields uc_mcontext.uc_regs->gregs[[PT_NIP]]" # Linux (ppc)
  28. pc_fields="$pc_fields uc_mcontext.psw.addr" # Linux (s390)
  29. pc_fields="$pc_fields uc_mcontext.gregs[[R15]]" # Linux (arm old [untested])
  30. pc_fields="$pc_fields uc_mcontext.arm_pc" # Linux (arm arch 5)
  31. pc_fields="$pc_fields uc_mcontext.gp_regs[[PT_NIP]]" # Suse SLES 11 (ppc64)
  32. pc_fields="$pc_fields uc_mcontext.mc_eip" # FreeBSD (i386)
  33. pc_fields="$pc_fields uc_mcontext.mc_srr0" # FreeBSD (powerpc, powerpc64)
  34. pc_fields="$pc_fields uc_mcontext.mc_rip" # FreeBSD (x86_64 [untested])
  35. pc_fields="$pc_fields uc_mcontext.__gregs[[_REG_EIP]]" # NetBSD (i386)
  36. pc_fields="$pc_fields uc_mcontext.__gregs[[_REG_RIP]]" # NetBSD (x86_64)
  37. pc_fields="$pc_fields uc_mcontext->ss.eip" # OS X (i386, <=10.4)
  38. pc_fields="$pc_fields uc_mcontext->__ss.__eip" # OS X (i386, >=10.5)
  39. pc_fields="$pc_fields uc_mcontext->ss.rip" # OS X (x86_64)
  40. pc_fields="$pc_fields uc_mcontext->__ss.__rip" # OS X (>=10.5 [untested])
  41. pc_fields="$pc_fields uc_mcontext->ss.srr0" # OS X (ppc, ppc64 [untested])
  42. pc_fields="$pc_fields uc_mcontext->__ss.__srr0" # OS X (>=10.5 [untested])
  43. pc_field_found=false
  44. for pc_field in $pc_fields; do
  45. if ! $pc_field_found; then
  46. # Prefer sys/ucontext.h to ucontext.h, for OS X's sake.
  47. if test "x$ac_cv_header_cygwin_signal_h" = xyes; then
  48. AC_TRY_COMPILE([#define _GNU_SOURCE 1
  49. #include <cygwin/signal.h>],
  50. [ucontext_t u; return u.$pc_field == 0;],
  51. AC_DEFINE_UNQUOTED(PC_FROM_UCONTEXT, $pc_field,
  52. How to access the PC from a struct ucontext)
  53. AC_MSG_RESULT([$pc_field])
  54. pc_field_found=true)
  55. elif test "x$ac_cv_header_asm_ptrace_h" = xyes -a "x$ac_cv_header_sys_ucontext_h" = xyes; then
  56. AC_TRY_COMPILE([#define _GNU_SOURCE 1
  57. #include <asm/ptrace.h>
  58. #include <sys/ucontext.h>],
  59. [ucontext_t u; return u.$pc_field == 0;],
  60. AC_DEFINE_UNQUOTED(PC_FROM_UCONTEXT, $pc_field,
  61. How to access the PC from a struct ucontext)
  62. AC_MSG_RESULT([$pc_field])
  63. pc_field_found=true)
  64. elif test "x$ac_cv_header_sys_ucontext_h" = xyes; then
  65. AC_TRY_COMPILE([#define _GNU_SOURCE 1
  66. #include <sys/ucontext.h>],
  67. [ucontext_t u; return u.$pc_field == 0;],
  68. AC_DEFINE_UNQUOTED(PC_FROM_UCONTEXT, $pc_field,
  69. How to access the PC from a struct ucontext)
  70. AC_MSG_RESULT([$pc_field])
  71. pc_field_found=true)
  72. elif test "x$ac_cv_header_ucontext_h" = xyes; then
  73. AC_TRY_COMPILE([#define _GNU_SOURCE 1
  74. #include <ucontext.h>],
  75. [ucontext_t u; return u.$pc_field == 0;],
  76. AC_DEFINE_UNQUOTED(PC_FROM_UCONTEXT, $pc_field,
  77. How to access the PC from a struct ucontext)
  78. AC_MSG_RESULT([$pc_field])
  79. pc_field_found=true)
  80. else # hope some standard header gives it to us
  81. AC_TRY_COMPILE([],
  82. [ucontext_t u; return u.$pc_field == 0;],
  83. AC_DEFINE_UNQUOTED(PC_FROM_UCONTEXT, $pc_field,
  84. How to access the PC from a struct ucontext)
  85. AC_MSG_RESULT([$pc_field])
  86. pc_field_found=true)
  87. fi
  88. fi
  89. done
  90. if ! $pc_field_found; then
  91. pc_fields=" sc_eip" # OpenBSD (i386)
  92. pc_fields="$pc_fields sc_rip" # OpenBSD (x86_64)
  93. for pc_field in $pc_fields; do
  94. if ! $pc_field_found; then
  95. AC_TRY_COMPILE([#include <signal.h>],
  96. [ucontext_t u; return u.$pc_field == 0;],
  97. AC_DEFINE_UNQUOTED(PC_FROM_UCONTEXT, $pc_field,
  98. How to access the PC from a struct ucontext)
  99. AC_MSG_RESULT([$pc_field])
  100. pc_field_found=true)
  101. fi
  102. done
  103. fi
  104. if ! $pc_field_found; then
  105. [$1]
  106. fi])