mostang.com!davidm | b5a5406 | 2003-01-17 07:48:52 +0000 | [diff] [blame] | 1 | '\" t |
mostang.com!davidm | 455b0ac | 2003-03-11 01:05:47 +0000 | [diff] [blame] | 2 | .\" Manual page created with latex2man on Fri Mar 7 14:44:48 PST 2003 |
mostang.com!davidm | b5a5406 | 2003-01-17 07:48:52 +0000 | [diff] [blame] | 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 | |
| 11 | .fi |
| 12 | .. |
mostang.com!davidm | 455b0ac | 2003-03-11 01:05:47 +0000 | [diff] [blame] | 13 | .TH "LIBUNWIND" "3" "07 March 2003" "Programming Library " "Programming Library " |
mostang.com!davidm | b5a5406 | 2003-01-17 07:48:52 +0000 | [diff] [blame] | 14 | .SH NAME |
| 15 | |
| 16 | .PP |
| 17 | libunwind \-\- a (mostly) platform\-independent unwind API |
| 18 | .PP |
| 19 | .SH SYNOPSIS |
| 20 | |
| 21 | .PP |
| 22 | #include <libunwind.h> |
| 23 | .br |
| 24 | .PP |
| 25 | int |
| 26 | unw_getcontext(unw_context_t *); |
| 27 | .br |
| 28 | int |
| 29 | unw_init_local(unw_cursor_t *, |
| 30 | unw_context_t *); |
| 31 | .br |
| 32 | int |
| 33 | unw_init_remote(unw_cursor_t *, |
| 34 | unw_addr_space_t, |
| 35 | void *); |
| 36 | .br |
| 37 | int |
| 38 | unw_step(unw_cursor_t *); |
| 39 | .br |
| 40 | int |
| 41 | unw_get_reg(unw_cursor_t *, |
| 42 | int, |
| 43 | unw_word_t *); |
| 44 | .br |
| 45 | int |
| 46 | unw_get_fpreg(unw_cursor_t *, |
| 47 | int, |
| 48 | unw_fpreg_t *); |
| 49 | .br |
| 50 | int |
| 51 | unw_set_reg(unw_cursor_t *, |
| 52 | int, |
| 53 | unw_word_t); |
| 54 | .br |
| 55 | int |
mostang.com!davidm | edc22a3 | 2003-01-18 08:10:40 +0000 | [diff] [blame] | 56 | unw_set_fpreg(unw_cursor_t *, |
mostang.com!davidm | b5a5406 | 2003-01-17 07:48:52 +0000 | [diff] [blame] | 57 | int, |
| 58 | unw_fpreg_t); |
| 59 | .br |
| 60 | int |
| 61 | unw_resume(unw_cursor_t *); |
| 62 | .br |
| 63 | .PP |
| 64 | unw_addr_space_t |
mostang.com!davidm | 455b0ac | 2003-03-11 01:05:47 +0000 | [diff] [blame] | 65 | unw_local_addr_space; |
mostang.com!davidm | b5a5406 | 2003-01-17 07:48:52 +0000 | [diff] [blame] | 66 | .br |
| 67 | unw_addr_space_t |
| 68 | unw_create_addr_space(unw_accessors_t, |
| 69 | int); |
| 70 | .br |
| 71 | void |
| 72 | unw_destroy_addr_space(unw_addr_space_t); |
| 73 | .br |
| 74 | unw_accessors_t |
| 75 | unw_get_accessors(unw_addr_space_t); |
| 76 | .br |
| 77 | void |
| 78 | unw_flush_cache(unw_addr_space_t, |
| 79 | unw_word_t, |
| 80 | unw_word_t); |
| 81 | .br |
| 82 | int |
| 83 | unw_set_caching_policy(unw_addr_space_t, |
| 84 | unw_caching_policy_t); |
| 85 | .br |
| 86 | .PP |
| 87 | const char *unw_regname(int); |
| 88 | .br |
| 89 | int |
| 90 | unw_get_proc_info(unw_cursor_t *, |
| 91 | unw_proc_info_t *); |
| 92 | .br |
| 93 | int |
| 94 | unw_get_save_loc(unw_cursor_t *, |
| 95 | int, |
| 96 | unw_save_loc_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 *, |
mostang.com!davidm | a8db0b3 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 104 | size_t, |
| 105 | unw_word_t *); |
mostang.com!davidm | b5a5406 | 2003-01-17 07:48:52 +0000 | [diff] [blame] | 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 | |
| 117 | .PP |
| 118 | Libunwind |
| 119 | is very easy to use when unwinding a stack from |
| 120 | within a running program. This is called \fIlocal\fP |
| 121 | unwinding. Say |
| 122 | you want to unwind the stack while executing in some function |
| 123 | F(). |
| 124 | In this function, you would call unw_getcontext() |
| 125 | to get a snapshot of the CPU registers (machine\-state). Then you |
| 126 | initialize an \fIunwind cursor\fP |
| 127 | based on this snapshot. This is |
| 128 | done with a call to unw_init_local(). |
| 129 | The cursor now points |
| 130 | to the current frame, that is, the stack frame that corresponds to the |
| 131 | current activation of function F(). |
| 132 | The unwind cursor can then |
| 133 | be moved ``up\&'' (towards earlier stack frames) by calling |
| 134 | unw_step(). |
| 135 | By repeatedly calling this routine, you can |
| 136 | uncover the entire call\-chain that led to the activation of function |
| 137 | F(). |
| 138 | A positive return value from unw_step() |
| 139 | indicates |
| 140 | that there are more frames in the chain, zero indicates that the end |
| 141 | of the chain has been reached, and any negative value indicates that |
| 142 | some sort of error has occurred. |
| 143 | .PP |
| 144 | While it is not possible to directly move the unwind cursor in the |
| 145 | ``down\&'' direction (towards newer stack frames), this effect can be |
| 146 | achieved by making copyies of an unwind cursor. For example, a |
| 147 | program that sometimes has to move ``down\&'' by one stack frame could |
| 148 | maintain two cursor variables: ``curr\&'' |
| 149 | and ``prev\&''\&. |
| 150 | The |
| 151 | former would be used as the current cursor and prev |
| 152 | would be |
| 153 | maintained as the ``previous frame\&'' cursor by copying the contents of |
| 154 | curr |
| 155 | to prev |
| 156 | right before calling unw_step(). |
| 157 | With this approach, the program could move one step ``down\&'' simply by |
| 158 | copying back prev |
| 159 | to curr |
| 160 | whenever that is necessary. In |
| 161 | the mosts extreme case, a program could maintain a separate cursor for |
| 162 | each call frame and that way it could move up and down the call frame |
| 163 | chain at will. |
| 164 | .PP |
| 165 | Given an unwind cursor, it is possible to read and write the CPU |
| 166 | registers that were preserved for the current stack frame identified |
| 167 | by the cursor. Libunwind |
| 168 | provides several routines for this |
| 169 | purpose: unw_get_reg() |
| 170 | reads an integer (general) register, |
| 171 | unw_get_fpreg() |
| 172 | reads a floating\-point register, |
| 173 | unw_set_reg() |
| 174 | writes an integer register, and |
| 175 | unw_set_fpreg() |
| 176 | writes a floating\-point register. Note that, |
| 177 | by definition, only the \fIpreserved\fP |
| 178 | machine state can be accessed |
| 179 | during an unwind operation. Normally, this state consists of the |
| 180 | \fIcallee\-saved\fP |
| 181 | (``preserved\&'') registers. However, in some |
| 182 | special circumstances (e.g., in a signal handler trampoline), even the |
| 183 | \fIcaller\-saved\fP |
| 184 | (``scratch\&'') registers are preserved in the stack |
| 185 | frame and, in those cases, libunwind |
| 186 | will grant access to them |
| 187 | as well. The exact set of registers that can be accessed via the |
| 188 | cursor depends, of course, on the platform. However, there are two |
| 189 | registers that can be read on all platforms: the instruction pointer |
| 190 | (IP), sometimes also known as the ``program counter\&'', and the stack |
| 191 | pointer (SP). In libunwind, |
| 192 | these registers are identified by |
| 193 | the macros UNW_REG_IP |
| 194 | and UNW_REG_SP, |
| 195 | respectively. |
| 196 | .PP |
| 197 | Besides just moving the unwind cursor and reading/writing saved |
| 198 | registers, libunwind |
| 199 | also provides the ability to resume |
| 200 | execution at an arbitrary stack frame. As you might guess, this is |
| 201 | useful for implementing non\-local gotos and the exception handling |
| 202 | needed by some high\-level languages such as Java. Resuming execution |
| 203 | with a particular stack frame simply requires calling |
| 204 | unw_resume() |
| 205 | and passing the cursor identifying the target |
| 206 | frame as the only argument. |
| 207 | .PP |
| 208 | Normally, libunwind |
| 209 | supports both local and remote unwinding |
| 210 | (the latter will be explained in the next section). However, if you |
| 211 | tell libunwind that your program only needs local unwinding, then a |
| 212 | special implementation can be selected which may run much faster than |
| 213 | the generic implementation which supports both kinds of unwinding. To |
| 214 | select this optimized version, simply define the macro |
| 215 | UNW_LOCAL_ONLY |
| 216 | before including the headerfile |
| 217 | <libunwind.h>\&. |
| 218 | If is perfectly OK for a single program to |
| 219 | employ both local\-only and generic unwinding. That is, whether or not |
| 220 | UNW_LOCAL_ONLY |
| 221 | is defined is a choice that each source\-file |
| 222 | (compilation\-unit) can make on its own. Independent of the setting(s) |
| 223 | of UNW_LOCAL_ONLY, |
| 224 | you\&'ll always link the same library into |
| 225 | the program (normally \fB\-l\fPunwind). |
| 226 | .PP |
| 227 | If we put all of the above together, here is how we could use |
| 228 | libunwind |
| 229 | write function show_backtrace() |
| 230 | which prints |
| 231 | a classic stack trace: |
| 232 | .PP |
| 233 | .Vb |
| 234 | #define UNW_LOCAL_ONLY |
| 235 | #include <libunwind.h> |
| 236 | |
| 237 | void show_backtrace (void) { |
| 238 | unw_cursor_t cursor; unw_context_t uc; |
| 239 | unw_word_t ip, sp; |
| 240 | |
| 241 | unw_getcontext(&uc); |
| 242 | unw_init_local(&cursor, &uc); |
| 243 | while (unw_step(&cursor) > 0) { |
| 244 | unw_get_reg(&cursor, UNW_REG_IP, &ip); |
| 245 | unw_get_reg(&cursor, UNW_REG_SP, &sp); |
| 246 | printf ("ip = 0, sp = 0\\n", (long) ip, (long) sp); |
| 247 | } |
| 248 | } |
| 249 | .Ve |
| 250 | .PP |
| 251 | .SH REMOTE UNWINDING |
| 252 | |
| 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 justs 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 | |
| 296 | .PP |
| 297 | Libunwind |
| 298 | has been designed to enable unwinding across |
| 299 | platforms (architectures). Indeed, a single program can use |
| 300 | libunwind |
| 301 | to unwind an arbitrary number of target platforms, |
| 302 | all at the same time! |
| 303 | .PP |
| 304 | We call the machine that is running libunwind |
| 305 | the \fIhost\fP |
| 306 | and the machine that is running the process being unwound the |
| 307 | \fItarget\fP\&. |
| 308 | If the host and the target platform are the same, we |
| 309 | call it \fInative\fP |
| 310 | unwinding. If they differ, we call it |
| 311 | \fIcross\-platform\fP |
| 312 | unwinding. |
| 313 | .PP |
| 314 | The principle behind supporting native, cross\-platform, and |
| 315 | multi\-platform unwinding are very simple: for native unwinding, a |
| 316 | program includes <libunwind.h> |
| 317 | and uses the linker switch |
| 318 | \fB\-l\fPunwind\&. |
| 319 | For cross\-platform unwinding, a program |
| 320 | includes <libunwind\-PLAT\&.h> |
| 321 | and uses the linker |
| 322 | switch \fB\-l\fPunwind\-PLAT, |
| 323 | where PLAT |
| 324 | is the name |
| 325 | of the target platform (e.g., ia64 |
| 326 | for IA\-64, hppa\-elf |
| 327 | for ELF\-based HP PA\-RISC, or x86 |
| 328 | for 80386). Multi\-platform |
| 329 | unwinding works exactly like cross\-platform unwinding, the only |
| 330 | limitation is that a single source file (compilation unit) can include |
| 331 | at most one libunwind |
| 332 | header file. In other words, the |
| 333 | platform\-specific support for each supported target needs to be |
| 334 | isolated in separate source files\-\-\-a limitation that shouldn\&'t be an |
| 335 | issue in practice. |
| 336 | .PP |
| 337 | Note that, by definition, local unwinding is possible only for the |
| 338 | native case. Attempting to call, e.g., unw_local_init() |
| 339 | when |
| 340 | targeting a cross\-platform will result in a link\-time error |
| 341 | (unresolved references). |
| 342 | .PP |
| 343 | .SH THREAD\- AND SIGNAL\-SAFETY |
| 344 | |
| 345 | .PP |
| 346 | All libunwind |
| 347 | routines are thread\-safe. What this means is |
| 348 | that multiple threads may use libunwind |
| 349 | simulatenously. |
| 350 | However, any given cursor may be accessed by only one thread at |
| 351 | any given time. |
| 352 | .PP |
| 353 | To ensure thread\-safety, some libunwind |
| 354 | routines may have to |
| 355 | use locking. Such routines \fImust not\fP |
| 356 | be called from signal |
| 357 | handlers (directly or indirectly) and are therefore \fInot\fP |
| 358 | signal\-safe. The manual page for each libunwind |
| 359 | routine |
| 360 | identifies whether or not it is signal\-safe, but as a general rule, |
| 361 | any routine that may be needed for \fIlocal\fP |
| 362 | unwinding is |
| 363 | signal\-safe (e.g., unw_step() |
| 364 | for local unwinding is |
| 365 | signal\-safe). For remote\-unwinding, \fInone\fP |
| 366 | of the |
| 367 | libunwind |
| 368 | routines are guaranteed to be signal\-safe. |
| 369 | .PP |
| 370 | .SH UNWINDING THROUGH DYNAMICALLY GENERATED CODE |
| 371 | |
| 372 | .PP |
| 373 | Libunwind |
| 374 | provides the routines _U_dyn_register() |
| 375 | and |
| 376 | _U_dyn_cancel |
| 377 | to register/cancel the information required to |
| 378 | unwind through code that has been generated at runtime (e.g., by a |
| 379 | just\-in\-time (JIT) compiler). It is important to register the |
| 380 | information for \fIall\fP |
| 381 | dynamically generated code because |
| 382 | otherwise, a debugger may not be able to function properly or |
| 383 | high\-level language exception handling may not work as expected. |
| 384 | .PP |
| 385 | The interface for registering and canceling dynamic unwind info has |
| 386 | been designed for maximum efficiency, so as to minimize the |
| 387 | performance impact on JIT\-compilers. In particular, both routines are |
| 388 | guaranteed to execute in ``constant time\&'' (O(1)) and the |
| 389 | data\-structure encapsulating the dynamic unwind info has been designed |
| 390 | to facilitate sharing, such that similar procedures can share much of |
| 391 | the underlying information. |
| 392 | .PP |
| 393 | .SH CACHING OF UNWIND INFO |
| 394 | |
| 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 | |
| 425 | .PP |
| 426 | .TP |
| 427 | libunwind.h |
| 428 | Headerfile to include for native (same |
| 429 | platform) unwinding. |
| 430 | .TP |
| 431 | libunwind\-PLAT\&.h |
| 432 | Headerfile to include when |
| 433 | unwind target runs on platform PLAT\&. |
| 434 | For example, to unwind |
| 435 | an IA\-64 program, the header file libunwind\-ia64.h |
| 436 | should be |
| 437 | included. |
| 438 | .TP |
| 439 | \fB\-l\fPunwind |
| 440 | Linker\-switch to add when building a |
| 441 | program that does native (same platform) unwinding. |
| 442 | .TP |
| 443 | \fB\-l\fPunwind\-PLAT |
| 444 | Linker\-switch to add when |
| 445 | building a program that unwinds a program on platform PLAT\&. |
| 446 | For example, to (cross\-)unwind an IA\-64 program, the linker switch |
| 447 | \-lunwind\-ia64 |
| 448 | should be added. Note: multiple such switches |
| 449 | may need to be specified for programs that can unwind programss on |
| 450 | multiple platforms. |
| 451 | .PP |
| 452 | .SH SEE ALSO |
| 453 | |
| 454 | .PP |
mostang.com!davidm | a8db0b3 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 455 | libunwind\-ia64(3), |
| 456 | libunwind\-ptrace(3), |
| 457 | libunwind\-setjmp(3), |
| 458 | unw_resume(3) |
mostang.com!davidm | b5a5406 | 2003-01-17 07:48:52 +0000 | [diff] [blame] | 459 | .PP |
| 460 | .SH AUTHOR |
| 461 | |
| 462 | .PP |
| 463 | David Mosberger\-Tang |
| 464 | .br |
| 465 | Hewlett\-Packard Labs |
| 466 | .br |
| 467 | Palo\-Alto, CA 94304 |
| 468 | .br |
| 469 | Email: \fBdavidm@hpl.hp.com\fP |
| 470 | .br |
| 471 | WWW: \fBhttp://www.hpl.hp.com/research/linux/libunwind/\fP\&. |
| 472 | .\" NOTE: This file is generated, DO NOT EDIT. |