pc_from_ucontext.m4 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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.__gregs[[REG_PC]]" # Linux (riscv64)
  29. pc_fields="$pc_fields uc_mcontext.psw.addr" # Linux (s390)
  30. pc_fields="$pc_fields uc_mcontext.gregs[[R15]]" # Linux (arm old [untested])
  31. pc_fields="$pc_fields uc_mcontext.arm_pc" # Linux (arm arch 5)
  32. pc_fields="$pc_fields uc_mcontext.cr0_hi" # Linux (e2k)
  33. pc_fields="$pc_fields uc_mcontext.gp_regs[[PT_NIP]]" # Suse SLES 11 (ppc64)
  34. pc_fields="$pc_fields uc_mcontext.mc_eip" # FreeBSD (i386)
  35. pc_fields="$pc_fields uc_mcontext.mc_srr0" # FreeBSD (powerpc, powerpc64)
  36. pc_fields="$pc_fields uc_mcontext.mc_rip" # FreeBSD (x86_64 [untested])
  37. pc_fields="$pc_fields uc_mcontext.__gregs[[_REG_EIP]]" # NetBSD (i386)
  38. pc_fields="$pc_fields uc_mcontext.__gregs[[_REG_RIP]]" # NetBSD (x86_64)
  39. pc_fields="$pc_fields uc_mcontext->ss.eip" # OS X (i386, <=10.4)
  40. pc_fields="$pc_fields uc_mcontext->__ss.__eip" # OS X (i386, >=10.5)
  41. pc_fields="$pc_fields uc_mcontext->ss.rip" # OS X (x86_64)
  42. pc_fields="$pc_fields uc_mcontext->__ss.__rip" # OS X (>=10.5 [untested])
  43. pc_fields="$pc_fields uc_mcontext->ss.srr0" # OS X (ppc, ppc64 [untested])
  44. pc_fields="$pc_fields uc_mcontext->__ss.__srr0" # OS X (>=10.5 [untested])
  45. pc_fields="$pc_fields uc_mcontext->__ss.__pc" # OS X (arm64)
  46. pc_field_found=false
  47. for pc_field in $pc_fields; do
  48. if ! $pc_field_found; then
  49. # Prefer sys/ucontext.h to ucontext.h, for OS X's sake.
  50. if test "x$ac_cv_header_cygwin_signal_h" = xyes; then
  51. AC_TRY_COMPILE([#define _GNU_SOURCE 1
  52. #include <cygwin/signal.h>],
  53. [ucontext_t u; return u.$pc_field == 0;],
  54. AC_DEFINE_UNQUOTED(PC_FROM_UCONTEXT, $pc_field,
  55. How to access the PC from a struct ucontext)
  56. AC_MSG_RESULT([$pc_field])
  57. pc_field_found=true)
  58. elif test "x$ac_cv_header_asm_ptrace_h" = xyes -a "x$ac_cv_header_sys_ucontext_h" = xyes; then
  59. AC_TRY_COMPILE([#define _GNU_SOURCE 1
  60. #include <asm/ptrace.h>
  61. #include <sys/ucontext.h>],
  62. [ucontext_t u; return u.$pc_field == 0;],
  63. AC_DEFINE_UNQUOTED(PC_FROM_UCONTEXT, $pc_field,
  64. How to access the PC from a struct ucontext)
  65. AC_MSG_RESULT([$pc_field])
  66. pc_field_found=true)
  67. elif test "x$ac_cv_header_sys_ucontext_h" = xyes; then
  68. AC_TRY_COMPILE([#define _GNU_SOURCE 1
  69. #include <sys/ucontext.h>],
  70. [ucontext_t u; return u.$pc_field == 0;],
  71. AC_DEFINE_UNQUOTED(PC_FROM_UCONTEXT, $pc_field,
  72. How to access the PC from a struct ucontext)
  73. AC_MSG_RESULT([$pc_field])
  74. pc_field_found=true)
  75. elif test "x$ac_cv_header_ucontext_h" = xyes; then
  76. AC_TRY_COMPILE([#define _GNU_SOURCE 1
  77. #include <ucontext.h>],
  78. [ucontext_t u; return u.$pc_field == 0;],
  79. AC_DEFINE_UNQUOTED(PC_FROM_UCONTEXT, $pc_field,
  80. How to access the PC from a struct ucontext)
  81. AC_MSG_RESULT([$pc_field])
  82. pc_field_found=true)
  83. else # hope some standard header gives it to us
  84. AC_TRY_COMPILE([],
  85. [ucontext_t u; return u.$pc_field == 0;],
  86. AC_DEFINE_UNQUOTED(PC_FROM_UCONTEXT, $pc_field,
  87. How to access the PC from a struct ucontext)
  88. AC_MSG_RESULT([$pc_field])
  89. pc_field_found=true)
  90. fi
  91. fi
  92. done
  93. if ! $pc_field_found; then
  94. pc_fields=" sc_eip" # OpenBSD (i386)
  95. pc_fields="$pc_fields sc_rip" # OpenBSD (x86_64)
  96. for pc_field in $pc_fields; do
  97. if ! $pc_field_found; then
  98. AC_TRY_COMPILE([#include <signal.h>],
  99. [ucontext_t u; return u.$pc_field == 0;],
  100. AC_DEFINE_UNQUOTED(PC_FROM_UCONTEXT, $pc_field,
  101. How to access the PC from a struct ucontext)
  102. AC_MSG_RESULT([$pc_field])
  103. pc_field_found=true)
  104. fi
  105. done
  106. fi
  107. if ! $pc_field_found; then
  108. [$1]
  109. fi])