libunwind.man 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. '\" t
  2. .\" Manual page created with latex2man on Thu Aug 16 09:44:43 MDT 2007
  3. .\" NOTE: This file is generated, DO NOT EDIT.
  4. .de Vb
  5. .ft CW
  6. .nf
  7. ..
  8. .de Ve
  9. .ft R
  10. .fi
  11. ..
  12. .TH "LIBUNWIND" "3" "16 August 2007" "Programming Library " "Programming Library "
  13. .SH NAME
  14. libunwind
  15. \-\- a (mostly) platform\-independent unwind API
  16. .PP
  17. .SH SYNOPSIS
  18. .PP
  19. #include <libunwind.h>
  20. .br
  21. .PP
  22. int
  23. unw_getcontext(unw_context_t *);
  24. .br
  25. int
  26. unw_init_local(unw_cursor_t *,
  27. unw_context_t *);
  28. .br
  29. int
  30. unw_init_remote(unw_cursor_t *,
  31. unw_addr_space_t,
  32. void *);
  33. .br
  34. int
  35. unw_step(unw_cursor_t *);
  36. .br
  37. int
  38. unw_get_reg(unw_cursor_t *,
  39. unw_regnum_t,
  40. unw_word_t *);
  41. .br
  42. int
  43. unw_get_fpreg(unw_cursor_t *,
  44. unw_regnum_t,
  45. unw_fpreg_t *);
  46. .br
  47. int
  48. unw_set_reg(unw_cursor_t *,
  49. unw_regnum_t,
  50. unw_word_t);
  51. .br
  52. int
  53. unw_set_fpreg(unw_cursor_t *,
  54. unw_regnum_t,
  55. unw_fpreg_t);
  56. .br
  57. int
  58. unw_resume(unw_cursor_t *);
  59. .br
  60. .PP
  61. unw_addr_space_t
  62. unw_local_addr_space;
  63. .br
  64. unw_addr_space_t
  65. unw_create_addr_space(unw_accessors_t,
  66. int);
  67. .br
  68. void
  69. unw_destroy_addr_space(unw_addr_space_t);
  70. .br
  71. unw_accessors_t
  72. unw_get_accessors(unw_addr_space_t);
  73. .br
  74. void
  75. unw_flush_cache(unw_addr_space_t,
  76. unw_word_t,
  77. unw_word_t);
  78. .br
  79. int
  80. unw_set_caching_policy(unw_addr_space_t,
  81. unw_caching_policy_t);
  82. .br
  83. .PP
  84. const char *unw_regname(unw_regnum_t);
  85. .br
  86. int
  87. unw_get_proc_info(unw_cursor_t *,
  88. unw_proc_info_t *);
  89. .br
  90. int
  91. unw_get_save_loc(unw_cursor_t *,
  92. int,
  93. unw_save_loc_t *);
  94. .br
  95. int
  96. unw_is_fpreg(unw_regnum_t);
  97. .br
  98. int
  99. unw_is_signal_frame(unw_cursor_t *);
  100. .br
  101. int
  102. unw_get_proc_name(unw_cursor_t *,
  103. char *,
  104. size_t,
  105. unw_word_t *);
  106. .br
  107. .PP
  108. void
  109. _U_dyn_register(unw_dyn_info_t *);
  110. .br
  111. void
  112. _U_dyn_cancel(unw_dyn_info_t *);
  113. .br
  114. .PP
  115. .SH LOCAL UNWINDING
  116. .PP
  117. Libunwind
  118. is very easy to use when unwinding a stack from
  119. within a running program. This is called \fIlocal\fP
  120. unwinding. Say
  121. you want to unwind the stack while executing in some function
  122. F().
  123. In this function, you would call unw_getcontext()
  124. to get a snapshot of the CPU registers (machine\-state). Then you
  125. initialize an \fIunwind cursor\fP
  126. based on this snapshot. This is
  127. done with a call to unw_init_local().
  128. The cursor now points
  129. to the current frame, that is, the stack frame that corresponds to the
  130. current activation of function F().
  131. The unwind cursor can then
  132. be moved ``up\&'' (towards earlier stack frames) by calling
  133. unw_step().
  134. By repeatedly calling this routine, you can
  135. uncover the entire call\-chain that led to the activation of function
  136. F().
  137. A positive return value from unw_step()
  138. indicates
  139. that there are more frames in the chain, zero indicates that the end
  140. of the chain has been reached, and any negative value indicates that
  141. some sort of error has occurred.
  142. .PP
  143. While it is not possible to directly move the unwind cursor in the
  144. ``down\&'' direction (towards newer stack frames), this effect can be
  145. achieved by making copies of an unwind cursor. For example, a program
  146. that sometimes has to move ``down\&'' by one stack frame could maintain
  147. two cursor variables: ``curr\&''
  148. and ``prev\&''\&.
  149. The former
  150. would be used as the current cursor and prev
  151. would be maintained
  152. as the ``previous frame\&'' cursor by copying the contents of curr
  153. to prev
  154. right before calling unw_step().
  155. With this
  156. approach, the program could move one step ``down\&'' simply by copying
  157. back prev
  158. to curr
  159. whenever that is necessary. In the most
  160. extreme case, a program could maintain a separate cursor for each call
  161. frame and that way it could move up and down the callframe\-chain at
  162. will.
  163. .PP
  164. Given an unwind cursor, it is possible to read and write the CPU
  165. registers that were preserved for the current stack frame (as
  166. identified by the cursor). Libunwind
  167. provides several routines
  168. for this purpose: unw_get_reg()
  169. reads an integer (general)
  170. register, unw_get_fpreg()
  171. reads a floating\-point register,
  172. unw_set_reg()
  173. writes an integer register, and
  174. unw_set_fpreg()
  175. writes a floating\-point register. Note that,
  176. by definition, only the \fIpreserved\fP
  177. machine state can be accessed
  178. during an unwind operation. Normally, this state consists of the
  179. \fIcallee\-saved\fP
  180. (``preserved\&'') registers. However, in some
  181. special circumstances (e.g., in a signal handler trampoline), even the
  182. \fIcaller\-saved\fP
  183. (``scratch\&'') registers are preserved in the stack
  184. frame and, in those cases, libunwind
  185. will grant access to them
  186. as well. The exact set of registers that can be accessed via the
  187. cursor depends, of course, on the platform. However, there are two
  188. registers that can be read on all platforms: the instruction pointer
  189. (IP), sometimes also known as the ``program counter\&'', and the stack
  190. pointer (SP). In libunwind,
  191. these registers are identified by
  192. the macros UNW_REG_IP
  193. and UNW_REG_SP,
  194. respectively.
  195. .PP
  196. Besides just moving the unwind cursor and reading/writing saved
  197. registers, libunwind
  198. also provides the ability to resume
  199. execution at an arbitrary stack frame. As you might guess, this is
  200. useful for implementing non\-local gotos and the exception handling
  201. needed by some high\-level languages such as Java. Resuming execution
  202. with a particular stack frame simply requires calling
  203. unw_resume()
  204. and passing the cursor identifying the target
  205. frame as the only argument.
  206. .PP
  207. Normally, libunwind
  208. supports both local and remote unwinding
  209. (the latter will be explained in the next section). However, if you
  210. tell libunwind that your program only needs local unwinding, then a
  211. special implementation can be selected which may run much faster than
  212. the generic implementation which supports both kinds of unwinding. To
  213. select this optimized version, simply define the macro
  214. UNW_LOCAL_ONLY
  215. before including the headerfile
  216. <libunwind.h>\&.
  217. It is perfectly OK for a single program to
  218. employ both local\-only and generic unwinding. That is, whether or not
  219. UNW_LOCAL_ONLY
  220. is defined is a choice that each source\-file
  221. (compilation\-unit) can make on its own. Independent of the setting(s)
  222. of UNW_LOCAL_ONLY,
  223. you\&'ll always link the same library into
  224. the program (normally \fB\-l\fPunwind).
  225. Furthermore, the
  226. portion of libunwind
  227. that manages unwind\-info for dynamically
  228. generated code is not affected by the setting of
  229. UNW_LOCAL_ONLY\&.
  230. .PP
  231. If we put all of the above together, here is how we could use
  232. libunwind
  233. to write a function ``show_backtrace()\&''
  234. which prints a classic stack trace:
  235. .PP
  236. .Vb
  237. #define UNW_LOCAL_ONLY
  238. #include <libunwind.h>
  239. void show_backtrace (void) {
  240. unw_cursor_t cursor; unw_context_t uc;
  241. unw_word_t ip, sp;
  242. unw_getcontext(&uc);
  243. unw_init_local(&cursor, &uc);
  244. while (unw_step(&cursor) > 0) {
  245. unw_get_reg(&cursor, UNW_REG_IP, &ip);
  246. unw_get_reg(&cursor, UNW_REG_SP, &sp);
  247. printf ("ip = %lx, sp = %lx\\n", (long) ip, (long) sp);
  248. }
  249. }
  250. .Ve
  251. .PP
  252. .SH REMOTE UNWINDING
  253. .PP
  254. Libunwind
  255. can also be used to unwind a stack in a ``remote\&''
  256. process. Here, ``remote\&'' may mean another process on the same
  257. machine or even a process on a completely different machine from the
  258. one that is running libunwind\&.
  259. Remote unwinding is typically
  260. used by debuggers and instruction\-set simulators, for example.
  261. .PP
  262. Before you can unwind a remote process, you need to create a new
  263. address\-space object for that process. This is achieved with the
  264. unw_create_addr_space()
  265. routine. The routine takes two
  266. arguments: a pointer to a set of \fIaccessor\fP
  267. routines and an
  268. integer that specifies the byte\-order of the target process. The
  269. accessor routines provide libunwind
  270. with the means to
  271. communicate with the remote process. In particular, there are
  272. callbacks to read and write the process\&'s memory, its registers, and
  273. to access unwind information which may be needed by libunwind\&.
  274. .PP
  275. With the address space created, unwinding can be initiated by a call
  276. to unw_init_remote().
  277. This routine is very similar to
  278. unw_init_local(),
  279. except that it takes an address\-space
  280. object and an opaque pointer as arguments. The routine uses these
  281. arguments to fetch the initial machine state. Libunwind
  282. never
  283. uses the opaque pointer on its own, but instead just passes it on to
  284. the accessor (callback) routines. Typically, this pointer is used to
  285. select, e.g., the thread within a process that is to be unwound.
  286. .PP
  287. Once a cursor has been initialized with unw_init_remote(),
  288. unwinding works exactly like in the local case. That is, you can use
  289. unw_step()
  290. to move ``up\&'' in the call\-chain, read and write
  291. registers, or resume execution at a particular stack frame by calling
  292. unw_resume\&.
  293. .PP
  294. .SH CROSS\-PLATFORM AND MULTI\-PLATFORM UNWINDING
  295. .PP
  296. Libunwind
  297. has been designed to enable unwinding across
  298. platforms (architectures). Indeed, a single program can use
  299. libunwind
  300. to unwind an arbitrary number of target platforms,
  301. all at the same time!
  302. .PP
  303. We call the machine that is running libunwind
  304. the \fIhost\fP
  305. and the machine that is running the process being unwound the
  306. \fItarget\fP\&.
  307. If the host and the target platform are the same, we
  308. call it \fInative\fP
  309. unwinding. If they differ, we call it
  310. \fIcross\-platform\fP
  311. unwinding.
  312. .PP
  313. The principle behind supporting native, cross\-platform, and
  314. multi\-platform unwinding is very simple: for native unwinding, a
  315. program includes <libunwind.h>
  316. and uses the linker switch
  317. \fB\-l\fPunwind\&.
  318. For cross\-platform unwinding, a program
  319. includes <libunwind\-PLAT\&.h>
  320. and uses the linker
  321. switch \fB\-l\fPunwind\-PLAT,
  322. where PLAT
  323. is the name
  324. of the target platform (e.g., ia64
  325. for IA\-64, hppa\-elf
  326. for ELF\-based HP PA\-RISC, or x86
  327. for 80386). Multi\-platform
  328. unwinding works exactly like cross\-platform unwinding, the only
  329. limitation is that a single source file (compilation unit) can include
  330. at most one libunwind
  331. header file. In other words, the
  332. platform\-specific support for each supported target needs to be
  333. isolated in separate source files\-\-\-a limitation that shouldn\&'t be an
  334. issue in practice.
  335. .PP
  336. Note that, by definition, local unwinding is possible only for the
  337. native case. Attempting to call, e.g., unw_local_init()
  338. when
  339. targeting a cross\-platform will result in a link\-time error
  340. (unresolved references).
  341. .PP
  342. .SH THREAD\- AND SIGNAL\-SAFETY
  343. .PP
  344. All libunwind
  345. routines are thread\-safe. What this means is
  346. that multiple threads may use libunwind
  347. simulatenously.
  348. However, any given cursor may be accessed by only one thread at
  349. any given time.
  350. .PP
  351. To ensure thread\-safety, some libunwind
  352. routines may have to
  353. use locking. Such routines \fImust not\fP
  354. be called from signal
  355. handlers (directly or indirectly) and are therefore \fInot\fP
  356. signal\-safe. The manual page for each libunwind
  357. routine
  358. identifies whether or not it is signal\-safe, but as a general rule,
  359. any routine that may be needed for \fIlocal\fP
  360. unwinding is
  361. signal\-safe (e.g., unw_step()
  362. for local unwinding is
  363. signal\-safe). For remote\-unwinding, \fInone\fP
  364. of the
  365. libunwind
  366. routines are guaranteed to be signal\-safe.
  367. .PP
  368. .SH UNWINDING THROUGH DYNAMICALLY GENERATED CODE
  369. .PP
  370. Libunwind
  371. provides the routines _U_dyn_register()
  372. and
  373. _U_dyn_cancel()
  374. to register/cancel the information required to
  375. unwind through code that has been generated at runtime (e.g., by a
  376. just\-in\-time (JIT) compiler). It is important to register the
  377. information for \fIall\fP
  378. dynamically generated code because
  379. otherwise, a debugger may not be able to function properly or
  380. high\-level language exception handling may not work as expected.
  381. .PP
  382. The interface for registering and canceling dynamic unwind info has
  383. been designed for maximum efficiency, so as to minimize the
  384. performance impact on JIT\-compilers. In particular, both routines are
  385. guaranteed to execute in ``constant time\&'' (O(1)) and the
  386. data\-structure encapsulating the dynamic unwind info has been designed
  387. to facilitate sharing, such that similar procedures can share much of
  388. the underlying information.
  389. .PP
  390. For more information on the libunwind
  391. support for dynamically
  392. generated code, see libunwind\-dynamic(3)\&.
  393. .PP
  394. .SH CACHING OF UNWIND INFO
  395. .PP
  396. To speed up execution, libunwind
  397. may aggressively cache the
  398. information it needs to perform unwinding. If a process changes
  399. during its lifetime, this creates a risk of libunwind
  400. using
  401. stale data. For example, this would happen if libunwind
  402. were
  403. to cache information about a shared library which later on gets
  404. unloaded (e.g., via \fIdlclose\fP(3)).
  405. .PP
  406. To prevent the risk of using stale data, libunwind
  407. provides two
  408. facilities: first, it is possible to flush the cached information
  409. associated with a specific address range in the target process (or the
  410. entire address space, if desired). This functionality is provided by
  411. unw_flush_cache().
  412. The second facility is provided by
  413. unw_set_caching_policy(),
  414. which lets a program
  415. select the exact caching policy in use for a given address\-space
  416. object. In particular, by selecting the policy
  417. UNW_CACHE_NONE,
  418. it is possible to turn off caching
  419. completely, therefore eliminating the risk of stale data alltogether
  420. (at the cost of slower execution). By default, caching is enabled for
  421. local unwinding only.
  422. .PP
  423. .SH FILES
  424. .PP
  425. .TP
  426. libunwind.h
  427. Headerfile to include for native (same
  428. platform) unwinding.
  429. .TP
  430. libunwind\-PLAT\&.h
  431. Headerfile to include when
  432. the unwind target runs on platform PLAT\&.
  433. For example, to unwind
  434. an IA\-64 program, the header file libunwind\-ia64.h
  435. should be
  436. included.
  437. .TP
  438. \fB\-l\fPunwind
  439. Linker\-switch to add when building a
  440. program that does native (same platform) unwinding.
  441. .TP
  442. \fB\-l\fPunwind\-PLAT
  443. Linker\-switch to add when
  444. building a program that unwinds a program on platform PLAT\&.
  445. For example, to (cross\-)unwind an IA\-64 program, the linker switch
  446. \-lunwind\-ia64
  447. should be added. Note: multiple such switches
  448. may need to be specified for programs that can unwind programs on
  449. multiple platforms.
  450. .PP
  451. .SH SEE ALSO
  452. .PP
  453. libunwind\-dynamic(3),
  454. libunwind\-ia64(3),
  455. libunwind\-ptrace(3),
  456. libunwind\-setjmp(3),
  457. unw_create_addr_space(3),
  458. unw_destroy_addr_space(3),
  459. unw_flush_cache(3),
  460. unw_get_accessors(3),
  461. unw_get_fpreg(3),
  462. unw_get_proc_info(3),
  463. unw_get_proc_name(3),
  464. unw_get_reg(3),
  465. unw_getcontext(3),
  466. unw_init_local(3),
  467. unw_init_remote(3),
  468. unw_is_fpreg(3),
  469. unw_is_signal_frame(3),
  470. unw_regname(3),
  471. unw_resume(3),
  472. unw_set_caching_policy(3),
  473. unw_set_fpreg(3),
  474. unw_set_reg(3),
  475. unw_step(3),
  476. unw_strerror(3),
  477. _U_dyn_register(3),
  478. _U_dyn_cancel(3)
  479. .PP
  480. .SH AUTHOR
  481. .PP
  482. David Mosberger\-Tang
  483. .br
  484. Email: \fBdmosberger@gmail.com\fP
  485. .br
  486. WWW: \fBhttp://www.nongnu.org/libunwind/\fP\&.
  487. .\" NOTE: This file is generated, DO NOT EDIT.