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