NOTES 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. The central data structure of the unwind API is the unwind cursor.
  2. This structure tracks the register contents. The unwind API defines a
  3. handful of well-known frame "registers":
  4. - ip: the instruction pointer (pc)
  5. - rp: the return pointer (rp, aka "return address" or "return link")
  6. - sp: the stack pointer (memory stack pointer, in the case of ia64)
  7. - fp: the frame pointer
  8. - first_ip: the starting address of the current "procedure"
  9. - handler: a pointer to an architecture & language-specific
  10. "personality" routine
  11. - lsda: a pointer to an architecture & language-specific
  12. data-area
  13. The API defines no well-known preserved registers. Each architecture
  14. can define additional registers as needed. Of course, a portable
  15. application may only rely on well-known registers. The names for
  16. preserved registers are defined in the architecture-specific header
  17. file <unwind-ARCH.h>. For example, to get the IA-64-specific register
  18. names, an application would do:
  19. #include <unwind-ia64.h>
  20. The API is designed to handle two primary cases: unwinding within the
  21. current (local) process and unwinding of another ("remote") process
  22. (e.g., through ptrace()). In the local case, the initial machine
  23. state is captured by an unwind context (currently the same as
  24. ucontext_t). In the remote case, the initial machine state is
  25. captured by an unwind accessor structure, which provides callback
  26. routines for reading/writing memory and registers and for obtaining
  27. unwind information.
  28. Once a cursor has been initialized, you can step through the call
  29. chain with the unw_step() routine. The frame registers and the
  30. preserved state can then be accessed with unw_get_reg() or modified
  31. with unw_set_reg(). For floating-point registers, there are separate
  32. unw_get_fpreg() and unw_set_fpreg() routines (on some arches, e.g.,
  33. Alpha, these could be just aliases for unw_{g,s}et_reg()). The
  34. unw_resume() routine can be used to resume execution at an arbitrary
  35. point in the call-chain (as identified by an unwind cursor). This is
  36. intended for exception handling and, at least for now, the intention
  37. is to support this routine only for the local case. Kevin, if you
  38. feel gdb could benefit from such a routine, I'd be interested to hear
  39. about it.
  40. Note that it is perfectly legal to make copies of the unwind cursor.
  41. This makes it possible, e.g., to obtain an unwind context, modify the
  42. state in an earlier call frame, and then resume execution at the point
  43. at which the unwind context was captured.
  44. Here is a quick example of how to use the unwind API to do a simple
  45. stack trace:
  46. unw_cursor_t cursor;
  47. unw_word_t ip, sp;
  48. unw_context_t uc;
  49. unw_getcontext(&uc);
  50. unw_init_local(&cursor, &uc);
  51. do
  52. {
  53. unw_get_reg(&cursor, UNW_REG_IP, &ip);
  54. unw_get_reg(&cursor, UNW_REG_SP, &sp);
  55. printf ("ip=%016lx sp=%016lx\n", ip, sp);
  56. }
  57. while (unw_step (&cursor) > 0);
  58. Note that this particular example should work on pretty much any
  59. architecture, as it doesn't rely on any arch-specific registers.
  60. * Multiarchitecture support
  61. If libunwind is configured for a target other than the local (native)
  62. host, the library is installed as libunwind-$ARCH, where $ARCH is
  63. the target architecture name (e.g., ia32, ia64, or alpha). Similarly,
  64. the header file is installed as libunwind-$ARCH.
  65. With this setup, an application should:
  66. - include <libunwind.h>, and
  67. - link against -lunwind
  68. if the application needs to use the unwinder of the host. An
  69. application wanting to use the unwinder for a different target (e.g.,
  70. a cross-debugger) should:
  71. - include <libunwind-$ARCH.h>, and
  72. - link against -lunwind-$ARCH
  73. The global symbols exported by -lunwind-$ARCH are unique such that the
  74. same application can be linked against the separate unwind libraries
  75. of multiple targets. However, a single compilation unit can include
  76. the header file for only one target. For example, foo.c might include
  77. <libunwind-ia64.h> and bar.c might include <libunwind.h> and the
  78. entire application would have to be linked against both -lunwind and
  79. -lunwind-ia64.
  80. Note: the unwind header files of all targets have a common dependency
  81. on libunwind-common.h. To avoid version conflicts, it is necessary to
  82. ensure that the unwind libraries for all targets were derived from the
  83. same release of libunwind. That is, if the unwind library for one
  84. target is upgraded to a newer version, the libraries for all other
  85. targets also need to be upgraded.
  86. Note 2: The assumption is that a cross-unwinder can handle all
  87. interesting flavors of a target. For example, the unwinder for the
  88. ia64 target is expected to be able to handle both Linux and HP-UX.
  89. * IA-64 Specific Information
  90. Apart from the normal frame-registers, the IA-64 implementation of
  91. libunwind provides the means to access the current value of the
  92. register backing store pointer (bsp). One quirk with this
  93. frame-register is that it corresponds to the address that would be in
  94. register ar.bsp after flushing the current register stack to the
  95. backing store (i.e., as if a "flushrs" instruction had been executed).
  96. Of course, given this value and the contents of the current frame
  97. marker (CFM), it's easy to calculate the original value of ar.bsp:
  98. unw_word_t cfm, bsp, bsp_after_flushrs, sof;
  99. unw_get_reg (&cursor, UNW_IA64_BSP, &bsp_after_flushrs);
  100. unw_get_reg (&cursor, UNW_IA64_CFM, &cfm);
  101. bsp = ia64_rse_skip_regs (bsp_after_flushrs, -(cfm & 0x7f));
  102. ** Dynamic Unwind Info