libunwind.tex 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. \documentclass{article}
  2. \usepackage[fancyhdr,pdf]{latex2man}
  3. \input{common.tex}
  4. \begin{document}
  5. \begin{Name}{3}{libunwind}{David Mosberger-Tang}{Programming Library}{Introduction to libunwind}libunwind -- a (mostly) platform-independent unwind API
  6. \end{Name}
  7. \section{Synopsis}
  8. \File{\#include $<$libunwind.h$>$}\\
  9. \noindent
  10. \Type{int} \Func{unw\_getcontext}(\Type{unw\_context\_t~*});\\
  11. \noindent
  12. \Type{int} \Func{unw\_init\_local}(\Type{unw\_cursor\_t~*}, \Type{unw\_context\_t~*});\\
  13. \noindent
  14. \Type{int} \Func{unw\_init\_remote}(\Type{unw\_cursor\_t~*}, \Type{unw\_addr\_space\_t}, \Type{void~*});\\
  15. \noindent
  16. \Type{int} \Func{unw\_step}(\Type{unw\_cursor\_t~*});\\
  17. \noindent
  18. \Type{int} \Func{unw\_get\_reg}(\Type{unw\_cursor\_t~*}, \Type{unw\_regnum\_t}, \Type{unw\_word\_t~*});\\
  19. \noindent
  20. \Type{int} \Func{unw\_get\_fpreg}(\Type{unw\_cursor\_t~*}, \Type{unw\_regnum\_t}, \Type{unw\_fpreg\_t~*});\\
  21. \noindent
  22. \Type{int} \Func{unw\_set\_reg}(\Type{unw\_cursor\_t~*}, \Type{unw\_regnum\_t}, \Type{unw\_word\_t});\\
  23. \noindent
  24. \Type{int} \Func{unw\_set\_fpreg}(\Type{unw\_cursor\_t~*}, \Type{unw\_regnum\_t}, \Type{unw\_fpreg\_t});\\
  25. \noindent
  26. \Type{int} \Func{unw\_resume}(\Type{unw\_cursor\_t~*});\\
  27. \noindent
  28. \Type{unw\_addr\_space\_t} \Var{unw\_local\_addr\_space};\\
  29. \noindent
  30. \Type{unw\_addr\_space\_t} \Func{unw\_create\_addr\_space}(\Type{unw\_accessors\_t}, \Type{int});\\
  31. \noindent
  32. \Type{void} \Func{unw\_destroy\_addr\_space}(\Type{unw\_addr\_space\_t});\\
  33. \noindent
  34. \Type{unw\_accessors\_t} \Func{unw\_get\_accessors}(\Type{unw\_addr\_space\_t});\\
  35. \noindent
  36. \Type{void} \Func{unw\_flush\_cache}(\Type{unw\_addr\_space\_t}, \Type{unw\_word\_t}, \Type{unw\_word\_t});\\
  37. \noindent
  38. \Type{int} \Func{unw\_set\_caching\_policy}(\Type{unw\_addr\_space\_t}, \Type{unw\_caching\_policy\_t});\\
  39. \noindent
  40. \Type{const char *}\Func{unw\_regname}(\Type{unw\_regnum\_t});\\
  41. \noindent
  42. \Type{int} \Func{unw\_get\_proc\_info}(\Type{unw\_cursor\_t~*}, \Type{unw\_proc\_info\_t~*});\\
  43. \noindent
  44. \Type{int} \Func{unw\_get\_save\_loc}(\Type{unw\_cursor\_t~*}, \Type{int}, \Type{unw\_save\_loc\_t~*});\\
  45. \noindent
  46. \Type{int} \Func{unw\_is\_fpreg}(\Type{unw\_regnum\_t});\\
  47. \Type{int} \Func{unw\_is\_signal\_frame}(\Type{unw\_cursor\_t~*});\\
  48. \noindent
  49. \Type{int} \Func{unw\_get\_proc\_name}(\Type{unw\_cursor\_t~*}, \Type{char~*}, \Type{size\_t}, \Type{unw\_word\_t~*});\\
  50. \noindent
  51. \Type{void} \Func{\_U\_dyn\_register}(\Type{unw\_dyn\_info\_t~*});\\
  52. \noindent
  53. \Type{void} \Func{\_U\_dyn\_cancel}(\Type{unw\_dyn\_info\_t~*});\\
  54. \section{Local Unwinding}
  55. \Prog{Libunwind} is very easy to use when unwinding a stack from
  56. within a running program. This is called \emph{local} unwinding. Say
  57. you want to unwind the stack while executing in some function
  58. \Func{F}(). In this function, you would call \Func{unw\_getcontext}()
  59. to get a snapshot of the CPU registers (machine-state). Then you
  60. initialize an \emph{unwind~cursor} based on this snapshot. This is
  61. done with a call to \Func{unw\_init\_local}(). The cursor now points
  62. to the current frame, that is, the stack frame that corresponds to the
  63. current activation of function \Func{F}(). The unwind cursor can then
  64. be moved ``up'' (towards earlier stack frames) by calling
  65. \Func{unw\_step}(). By repeatedly calling this routine, you can
  66. uncover the entire call-chain that led to the activation of function
  67. \Func{F}(). A positive return value from \Func{unw\_step}() indicates
  68. that there are more frames in the chain, zero indicates that the end
  69. of the chain has been reached, and any negative value indicates that
  70. some sort of error has occurred.
  71. While it is not possible to directly move the unwind cursor in the
  72. ``down'' direction (towards newer stack frames), this effect can be
  73. achieved by making copies of an unwind cursor. For example, a program
  74. that sometimes has to move ``down'' by one stack frame could maintain
  75. two cursor variables: ``\Var{curr}'' and ``\Var{prev}''. The former
  76. would be used as the current cursor and \Var{prev} would be maintained
  77. as the ``previous frame'' cursor by copying the contents of \Var{curr}
  78. to \Var{prev} right before calling \Func{unw\_step}(). With this
  79. approach, the program could move one step ``down'' simply by copying
  80. back \Var{prev} to \Var{curr} whenever that is necessary. In the most
  81. extreme case, a program could maintain a separate cursor for each call
  82. frame and that way it could move up and down the callframe-chain at
  83. will.
  84. Given an unwind cursor, it is possible to read and write the CPU
  85. registers that were preserved for the current stack frame (as
  86. identified by the cursor). \Prog{Libunwind} provides several routines
  87. for this purpose: \Func{unw\_get\_reg}() reads an integer (general)
  88. register, \Func{unw\_get\_fpreg}() reads a floating-point register,
  89. \Func{unw\_set\_reg}() writes an integer register, and
  90. \Func{unw\_set\_fpreg}() writes a floating-point register. Note that,
  91. by definition, only the \emph{preserved} machine state can be accessed
  92. during an unwind operation. Normally, this state consists of the
  93. \emph{callee-saved} (``preserved'') registers. However, in some
  94. special circumstances (e.g., in a signal handler trampoline), even the
  95. \emph{caller-saved} (``scratch'') registers are preserved in the stack
  96. frame and, in those cases, \Prog{libunwind} will grant access to them
  97. as well. The exact set of registers that can be accessed via the
  98. cursor depends, of course, on the platform. However, there are two
  99. registers that can be read on all platforms: the instruction pointer
  100. (IP), sometimes also known as the ``program counter'', and the stack
  101. pointer (SP). In \Prog{libunwind}, these registers are identified by
  102. the macros \Const{UNW\_REG\_IP} and \Const{UNW\_REG\_SP},
  103. respectively.
  104. Besides just moving the unwind cursor and reading/writing saved
  105. registers, \Prog{libunwind} also provides the ability to resume
  106. execution at an arbitrary stack frame. As you might guess, this is
  107. useful for implementing non-local gotos and the exception handling
  108. needed by some high-level languages such as Java. Resuming execution
  109. with a particular stack frame simply requires calling
  110. \Func{unw\_resume}() and passing the cursor identifying the target
  111. frame as the only argument.
  112. Normally, \Prog{libunwind} supports both local and remote unwinding
  113. (the latter will be explained in the next section). However, if you
  114. tell libunwind that your program only needs local unwinding, then a
  115. special implementation can be selected which may run much faster than
  116. the generic implementation which supports both kinds of unwinding. To
  117. select this optimized version, simply define the macro
  118. \Const{UNW\_LOCAL\_ONLY} before including the headerfile
  119. \File{$<$libunwind.h$>$}. It is perfectly OK for a single program to
  120. employ both local-only and generic unwinding. That is, whether or not
  121. \Const{UNW\_LOCAL\_ONLY} is defined is a choice that each source-file
  122. (compilation-unit) can make on its own. Independent of the setting(s)
  123. of \Const{UNW\_LOCAL\_ONLY}, you'll always link the same library into
  124. the program (normally \Opt{-l}\File{unwind}). Furthermore, the
  125. portion of \Prog{libunwind} that manages unwind-info for dynamically
  126. generated code is not affected by the setting of
  127. \Const{UNW\_LOCAL\_ONLY}.
  128. If we put all of the above together, here is how we could use
  129. \Prog{libunwind} to write a function ``\Func{show\_backtrace}()''
  130. which prints a classic stack trace:
  131. \begin{verbatim}
  132. #define UNW_LOCAL_ONLY
  133. #include <libunwind.h>
  134. void show_backtrace (void) {
  135. unw_cursor_t cursor; unw_context_t uc;
  136. unw_word_t ip, sp;
  137. unw_getcontext(&uc);
  138. unw_init_local(&cursor, &uc);
  139. while (unw_step(&cursor) > 0) {
  140. unw_get_reg(&cursor, UNW_REG_IP, &ip);
  141. unw_get_reg(&cursor, UNW_REG_SP, &sp);
  142. printf ("ip = %lx, sp = %lx\n", (long) ip, (long) sp);
  143. }
  144. }
  145. \end{verbatim}
  146. \section{Remote Unwinding}
  147. \Prog{Libunwind} can also be used to unwind a stack in a ``remote''
  148. process. Here, ``remote'' may mean another process on the same
  149. machine or even a process on a completely different machine from the
  150. one that is running \Prog{libunwind}. Remote unwinding is typically
  151. used by debuggers and instruction-set simulators, for example.
  152. Before you can unwind a remote process, you need to create a new
  153. address-space object for that process. This is achieved with the
  154. \Func{unw\_create\_addr\_space}() routine. The routine takes two
  155. arguments: a pointer to a set of \emph{accessor} routines and an
  156. integer that specifies the byte-order of the target process. The
  157. accessor routines provide \Func{libunwind} with the means to
  158. communicate with the remote process. In particular, there are
  159. callbacks to read and write the process's memory, its registers, and
  160. to access unwind information which may be needed by \Func{libunwind}.
  161. With the address space created, unwinding can be initiated by a call
  162. to \Func{unw\_init\_remote}(). This routine is very similar to
  163. \Func{unw\_init\_local}(), except that it takes an address-space
  164. object and an opaque pointer as arguments. The routine uses these
  165. arguments to fetch the initial machine state. \Prog{Libunwind} never
  166. uses the opaque pointer on its own, but instead just passes it on to
  167. the accessor (callback) routines. Typically, this pointer is used to
  168. select, e.g., the thread within a process that is to be unwound.
  169. Once a cursor has been initialized with \Func{unw\_init\_remote}(),
  170. unwinding works exactly like in the local case. That is, you can use
  171. \Func{unw\_step}() to move ``up'' in the call-chain, read and write
  172. registers, or resume execution at a particular stack frame by calling
  173. \Func{unw\_resume}.
  174. \section{Cross-platform and Multi-platform Unwinding}
  175. \Prog{Libunwind} has been designed to enable unwinding across
  176. platforms (architectures). Indeed, a single program can use
  177. \Prog{libunwind} to unwind an arbitrary number of target platforms,
  178. all at the same time!
  179. We call the machine that is running \Prog{libunwind} the \emph{host}
  180. and the machine that is running the process being unwound the
  181. \emph{target}. If the host and the target platform are the same, we
  182. call it \emph{native} unwinding. If they differ, we call it
  183. \emph{cross-platform} unwinding.
  184. The principle behind supporting native, cross-platform, and
  185. multi-platform unwinding is very simple: for native unwinding, a
  186. program includes \File{$<$libunwind.h$>$} and uses the linker switch
  187. \Opt{-l}\File{unwind}. For cross-platform unwinding, a program
  188. includes \File{$<$libunwind-}\Var{PLAT}\File{.h$>$} and uses the linker
  189. switch \Opt{-l}\File{unwind-}\Var{PLAT}, where \Var{PLAT} is the name
  190. of the target platform (e.g., \File{ia64} for IA-64, \File{hppa-elf}
  191. for ELF-based HP PA-RISC, or \File{x86} for 80386). Multi-platform
  192. unwinding works exactly like cross-platform unwinding, the only
  193. limitation is that a single source file (compilation unit) can include
  194. at most one \Prog{libunwind} header file. In other words, the
  195. platform-specific support for each supported target needs to be
  196. isolated in separate source files---a limitation that shouldn't be an
  197. issue in practice.
  198. Note that, by definition, local unwinding is possible only for the
  199. native case. Attempting to call, e.g., \Func{unw\_local\_init}() when
  200. targeting a cross-platform will result in a link-time error
  201. (unresolved references).
  202. \section{Thread- and Signal-Safety}
  203. All \Prog{libunwind} routines are thread-safe. What this means is
  204. that multiple threads may use \Prog{libunwind} simulatenously.
  205. However, any given cursor may be accessed by only one thread at
  206. any given time.
  207. To ensure thread-safety, some \Prog{libunwind} routines may have to
  208. use locking. Such routines \emph{must~not} be called from signal
  209. handlers (directly or indirectly) and are therefore \emph{not}
  210. signal-safe. The manual page for each \Prog{libunwind} routine
  211. identifies whether or not it is signal-safe, but as a general rule,
  212. any routine that may be needed for \emph{local} unwinding is
  213. signal-safe (e.g., \Func{unw\_step}() for local unwinding is
  214. signal-safe). For remote-unwinding, \emph{none} of the
  215. \Prog{libunwind} routines are guaranteed to be signal-safe.
  216. \section{Unwinding Through Dynamically Generated Code}
  217. \Func{Libunwind} provides the routines \Func{\_U\_dyn\_register}() and
  218. \Func{\_U\_dyn\_cancel}() to register/cancel the information required to
  219. unwind through code that has been generated at runtime (e.g., by a
  220. just-in-time (JIT) compiler). It is important to register the
  221. information for \emph{all} dynamically generated code because
  222. otherwise, a debugger may not be able to function properly or
  223. high-level language exception handling may not work as expected.
  224. The interface for registering and canceling dynamic unwind info has
  225. been designed for maximum efficiency, so as to minimize the
  226. performance impact on JIT-compilers. In particular, both routines are
  227. guaranteed to execute in ``constant time'' (O(1)) and the
  228. data-structure encapsulating the dynamic unwind info has been designed
  229. to facilitate sharing, such that similar procedures can share much of
  230. the underlying information.
  231. For more information on the \Prog{libunwind} support for dynamically
  232. generated code, see \SeeAlso{libunwind-dynamic(3)}.
  233. \section{Caching of Unwind Info}
  234. To speed up execution, \Prog{libunwind} may aggressively cache the
  235. information it needs to perform unwinding. If a process changes
  236. during its lifetime, this creates a risk of \Prog{libunwind} using
  237. stale data. For example, this would happen if \Prog{libunwind} were
  238. to cache information about a shared library which later on gets
  239. unloaded (e.g., via \Cmd{dlclose}{3}).
  240. To prevent the risk of using stale data, \Prog{libunwind} provides two
  241. facilities: first, it is possible to flush the cached information
  242. associated with a specific address range in the target process (or the
  243. entire address space, if desired). This functionality is provided by
  244. \Func{unw\_flush\_cache}(). The second facility is provided by
  245. \Func{unw\_set\_caching\_policy}(), which lets a program
  246. select the exact caching policy in use for a given address-space
  247. object. In particular, by selecting the policy
  248. \Const{UNW\_CACHE\_NONE}, it is possible to turn off caching
  249. completely, therefore eliminating the risk of stale data alltogether
  250. (at the cost of slower execution). By default, caching is enabled for
  251. local unwinding only.
  252. \section{Files}
  253. \begin{Description}
  254. \item[\File{libunwind.h}] Headerfile to include for native (same
  255. platform) unwinding.
  256. \item[\File{libunwind-}\Var{PLAT}\File{.h}] Headerfile to include when
  257. the unwind target runs on platform \Var{PLAT}. For example, to unwind
  258. an IA-64 program, the header file \File{libunwind-ia64.h} should be
  259. included.
  260. \item[\Opt{-l}\File{unwind}] Linker-switch to add when building a
  261. program that does native (same platform) unwinding.
  262. \item[\Opt{-l}\File{unwind-}\Var{PLAT}] Linker-switch to add when
  263. building a program that unwinds a program on platform \Var{PLAT}.
  264. For example, to (cross-)unwind an IA-64 program, the linker switch
  265. \File{-lunwind-ia64} should be added. Note: multiple such switches
  266. may need to be specified for programs that can unwind programs on
  267. multiple platforms.
  268. \end{Description}
  269. \section{See Also}
  270. \SeeAlso{libunwind-dynamic(3)},
  271. \SeeAlso{libunwind-ia64(3)},
  272. \SeeAlso{libunwind-ptrace(3)},
  273. \SeeAlso{libunwind-setjmp(3)},
  274. \SeeAlso{unw\_create\_addr\_space(3)},
  275. \SeeAlso{unw\_destroy\_addr\_space(3)},
  276. \SeeAlso{unw\_flush\_cache(3)},
  277. \SeeAlso{unw\_get\_accessors(3)},
  278. \SeeAlso{unw\_get\_fpreg(3)},
  279. \SeeAlso{unw\_get\_proc\_info(3)},
  280. \SeeAlso{unw\_get\_proc\_name(3)},
  281. \SeeAlso{unw\_get\_reg(3)},
  282. \SeeAlso{unw\_getcontext(3)},
  283. \SeeAlso{unw\_init\_local(3)},
  284. \SeeAlso{unw\_init\_remote(3)},
  285. \SeeAlso{unw\_is\_fpreg(3)},
  286. \SeeAlso{unw\_is\_signal\_frame(3)},
  287. \SeeAlso{unw\_regname(3)},
  288. \SeeAlso{unw\_resume(3)},
  289. \SeeAlso{unw\_set\_caching\_policy(3)},
  290. \SeeAlso{unw\_set\_fpreg(3)},
  291. \SeeAlso{unw\_set\_reg(3)},
  292. \SeeAlso{unw\_step(3)},
  293. \SeeAlso{unw\_strerror(3)},
  294. \SeeAlso{\_U\_dyn\_register(3)},
  295. \SeeAlso{\_U\_dyn\_cancel(3)}
  296. \section{Author}
  297. \noindent
  298. David Mosberger-Tang\\
  299. Email: \Email{dmosberger@gmail.com}\\
  300. WWW: \URL{http://www.nongnu.org/libunwind/}.
  301. \LatexManEnd
  302. \end{document}