2 .\" Manual page created with latex2man on Thu Jan 12 06:50:29 PST 2017
3 .\" NOTE: This file is generated, DO NOT EDIT.
13 .TH "LIBUNWIND" "3" "12 January 2017" "Programming Library " "Programming Library "
16 \-\- a (mostly) platform\-independent unwind API
21 #include <libunwind.h>
25 unw_getcontext(unw_context_t *);
28 unw_init_local(unw_cursor_t *,
32 unw_init_remote(unw_cursor_t *,
37 unw_step(unw_cursor_t *);
40 unw_get_reg(unw_cursor_t *,
45 unw_get_fpreg(unw_cursor_t *,
50 unw_set_reg(unw_cursor_t *,
55 unw_set_fpreg(unw_cursor_t *,
60 unw_resume(unw_cursor_t *);
67 unw_create_addr_space(unw_accessors_t,
71 unw_destroy_addr_space(unw_addr_space_t);
74 unw_get_accessors(unw_addr_space_t);
77 unw_flush_cache(unw_addr_space_t,
82 unw_set_caching_policy(unw_addr_space_t,
83 unw_caching_policy_t);
86 unw_set_cache_size(unw_addr_space_t,
91 const char *unw_regname(unw_regnum_t);
94 unw_get_proc_info(unw_cursor_t *,
98 unw_get_save_loc(unw_cursor_t *,
103 unw_is_fpreg(unw_regnum_t);
106 unw_is_signal_frame(unw_cursor_t *);
109 unw_get_proc_name(unw_cursor_t *,
116 _U_dyn_register(unw_dyn_info_t *);
119 _U_dyn_cancel(unw_dyn_info_t *);
126 is very easy to use when unwinding a stack from
127 within a running program. This is called \fIlocal\fP
129 you want to unwind the stack while executing in some function
131 In this function, you would call unw_getcontext()
132 to get a snapshot of the CPU registers (machine\-state). Then you
133 initialize an \fIunwind cursor\fP
134 based on this snapshot. This is
135 done with a call to unw_init_local().
136 The cursor now points
137 to the current frame, that is, the stack frame that corresponds to the
138 current activation of function F().
139 The unwind cursor can then
140 be moved ``up\&'' (towards earlier stack frames) by calling
142 By repeatedly calling this routine, you can
143 uncover the entire call\-chain that led to the activation of function
145 A positive return value from unw_step()
147 that there are more frames in the chain, zero indicates that the end
148 of the chain has been reached, and any negative value indicates that
149 some sort of error has occurred.
151 While it is not possible to directly move the unwind cursor in the
152 ``down\&'' direction (towards newer stack frames), this effect can be
153 achieved by making copies of an unwind cursor. For example, a program
154 that sometimes has to move ``down\&'' by one stack frame could maintain
155 two cursor variables: ``curr\&''
158 would be used as the current cursor and prev
160 as the ``previous frame\&'' cursor by copying the contents of curr
162 right before calling unw_step().
164 approach, the program could move one step ``down\&'' simply by copying
167 whenever that is necessary. In the most
168 extreme case, a program could maintain a separate cursor for each call
169 frame and that way it could move up and down the callframe\-chain at
172 Given an unwind cursor, it is possible to read and write the CPU
173 registers that were preserved for the current stack frame (as
174 identified by the cursor). Libunwind
175 provides several routines
176 for this purpose: unw_get_reg()
177 reads an integer (general)
178 register, unw_get_fpreg()
179 reads a floating\-point register,
181 writes an integer register, and
183 writes a floating\-point register. Note that,
184 by definition, only the \fIpreserved\fP
185 machine state can be accessed
186 during an unwind operation. Normally, this state consists of the
188 (``preserved\&'') registers. However, in some
189 special circumstances (e.g., in a signal handler trampoline), even the
191 (``scratch\&'') registers are preserved in the stack
192 frame and, in those cases, libunwind
193 will grant access to them
194 as well. The exact set of registers that can be accessed via the
195 cursor depends, of course, on the platform. However, there are two
196 registers that can be read on all platforms: the instruction pointer
197 (IP), sometimes also known as the ``program counter\&'', and the stack
198 pointer (SP). In libunwind,
199 these registers are identified by
200 the macros UNW_REG_IP
204 Besides just moving the unwind cursor and reading/writing saved
206 also provides the ability to resume
207 execution at an arbitrary stack frame. As you might guess, this is
208 useful for implementing non\-local gotos and the exception handling
209 needed by some high\-level languages such as Java. Resuming execution
210 with a particular stack frame simply requires calling
212 and passing the cursor identifying the target
213 frame as the only argument.
216 supports both local and remote unwinding
217 (the latter will be explained in the next section). However, if you
218 tell libunwind that your program only needs local unwinding, then a
219 special implementation can be selected which may run much faster than
220 the generic implementation which supports both kinds of unwinding. To
221 select this optimized version, simply define the macro
223 before including the headerfile
225 It is perfectly OK for a single program to
226 employ both local\-only and generic unwinding. That is, whether or not
228 is defined is a choice that each source\-file
229 (compilation\-unit) can make on its own. Independent of the setting(s)
231 you\&'ll always link the same library into
232 the program (normally \fB\-l\fPunwind).
235 that manages unwind\-info for dynamically
236 generated code is not affected by the setting of
239 If we put all of the above together, here is how we could use
241 to write a function ``show_backtrace()\&''
242 which prints a classic stack trace:
245 #define UNW_LOCAL_ONLY
246 #include <libunwind.h>
248 void show_backtrace (void) {
249 unw_cursor_t cursor; unw_context_t uc;
253 unw_init_local(&cursor, &uc);
254 while (unw_step(&cursor) > 0) {
255 unw_get_reg(&cursor, UNW_REG_IP, &ip);
256 unw_get_reg(&cursor, UNW_REG_SP, &sp);
257 printf ("ip = %lx, sp = %lx\\n", (long) ip, (long) sp);
266 can also be used to unwind a stack in a ``remote\&''
267 process. Here, ``remote\&'' may mean another process on the same
268 machine or even a process on a completely different machine from the
269 one that is running libunwind\&.
270 Remote unwinding is typically
271 used by debuggers and instruction\-set simulators, for example.
273 Before you can unwind a remote process, you need to create a new
274 address\-space object for that process. This is achieved with the
275 unw_create_addr_space()
276 routine. The routine takes two
277 arguments: a pointer to a set of \fIaccessor\fP
279 integer that specifies the byte\-order of the target process. The
280 accessor routines provide libunwind
282 communicate with the remote process. In particular, there are
283 callbacks to read and write the process\&'s memory, its registers, and
284 to access unwind information which may be needed by libunwind\&.
286 With the address space created, unwinding can be initiated by a call
287 to unw_init_remote().
288 This routine is very similar to
290 except that it takes an address\-space
291 object and an opaque pointer as arguments. The routine uses these
292 arguments to fetch the initial machine state. Libunwind
294 uses the opaque pointer on its own, but instead just passes it on to
295 the accessor (callback) routines. Typically, this pointer is used to
296 select, e.g., the thread within a process that is to be unwound.
298 Once a cursor has been initialized with unw_init_remote(),
299 unwinding works exactly like in the local case. That is, you can use
301 to move ``up\&'' in the call\-chain, read and write
302 registers, or resume execution at a particular stack frame by calling
305 .SH CROSS\-PLATFORM AND MULTI\-PLATFORM UNWINDING
309 has been designed to enable unwinding across
310 platforms (architectures). Indeed, a single program can use
312 to unwind an arbitrary number of target platforms,
313 all at the same time!
315 We call the machine that is running libunwind
317 and the machine that is running the process being unwound the
319 If the host and the target platform are the same, we
321 unwinding. If they differ, we call it
322 \fIcross\-platform\fP
325 The principle behind supporting native, cross\-platform, and
326 multi\-platform unwinding is very simple: for native unwinding, a
327 program includes <libunwind.h>
328 and uses the linker switch
330 For cross\-platform unwinding, a program
331 includes <libunwind\-PLAT\&.h>
333 switch \fB\-l\fPunwind\-PLAT,
336 of the target platform (e.g., ia64
337 for IA\-64, hppa\-elf
338 for ELF\-based HP PA\-RISC, or x86
339 for 80386). Multi\-platform
340 unwinding works exactly like cross\-platform unwinding, the only
341 limitation is that a single source file (compilation unit) can include
342 at most one libunwind
343 header file. In other words, the
344 platform\-specific support for each supported target needs to be
345 isolated in separate source files\-\-\-a limitation that shouldn\&'t be an
348 Note that, by definition, local unwinding is possible only for the
349 native case. Attempting to call, e.g., unw_local_init()
351 targeting a cross\-platform will result in a link\-time error
352 (unresolved references).
354 .SH THREAD\- AND SIGNAL\-SAFETY
358 routines are thread\-safe. What this means is
359 that multiple threads may use libunwind
361 However, any given cursor may be accessed by only one thread at
364 To ensure thread\-safety, some libunwind
366 use locking. Such routines \fImust not\fP
367 be called from signal
368 handlers (directly or indirectly) and are therefore \fInot\fP
369 signal\-safe. The manual page for each libunwind
371 identifies whether or not it is signal\-safe, but as a general rule,
372 any routine that may be needed for \fIlocal\fP
374 signal\-safe (e.g., unw_step()
375 for local unwinding is
376 signal\-safe). For remote\-unwinding, \fInone\fP
379 routines are guaranteed to be signal\-safe.
381 .SH UNWINDING THROUGH DYNAMICALLY GENERATED CODE
385 provides the routines _U_dyn_register()
388 to register/cancel the information required to
389 unwind through code that has been generated at runtime (e.g., by a
390 just\-in\-time (JIT) compiler). It is important to register the
391 information for \fIall\fP
392 dynamically generated code because
393 otherwise, a debugger may not be able to function properly or
394 high\-level language exception handling may not work as expected.
396 The interface for registering and canceling dynamic unwind info has
397 been designed for maximum efficiency, so as to minimize the
398 performance impact on JIT\-compilers. In particular, both routines are
399 guaranteed to execute in ``constant time\&'' (O(1)) and the
400 data\-structure encapsulating the dynamic unwind info has been designed
401 to facilitate sharing, such that similar procedures can share much of
402 the underlying information.
404 For more information on the libunwind
405 support for dynamically
406 generated code, see libunwind\-dynamic(3)\&.
408 .SH CACHING OF UNWIND INFO
411 To speed up execution, libunwind
412 may aggressively cache the
413 information it needs to perform unwinding. If a process changes
414 during its lifetime, this creates a risk of libunwind
416 stale data. For example, this would happen if libunwind
418 to cache information about a shared library which later on gets
419 unloaded (e.g., via \fIdlclose\fP(3)).
421 To prevent the risk of using stale data, libunwind
423 facilities: first, it is possible to flush the cached information
424 associated with a specific address range in the target process (or the
425 entire address space, if desired). This functionality is provided by
427 The second facility is provided by
428 unw_set_caching_policy(),
430 select the exact caching policy in use for a given address\-space
431 object. In particular, by selecting the policy
433 it is possible to turn off caching
434 completely, therefore eliminating the risk of stale data alltogether
435 (at the cost of slower execution). By default, caching is enabled for
436 local unwinding only. The cache size can be dynamically changed with
437 unw_set_cache_size(),
438 which also fluches the current cache.
445 Headerfile to include for native (same
449 Headerfile to include when
450 the unwind target runs on platform PLAT\&.
451 For example, to unwind
452 an IA\-64 program, the header file libunwind\-ia64.h
457 Linker\-switch to add when building a
458 program that does native (same platform) unwinding.
460 \fB\-l\fPunwind\-PLAT
461 Linker\-switch to add when
462 building a program that unwinds a program on platform PLAT\&.
463 For example, to (cross\-)unwind an IA\-64 program, the linker switch
465 should be added. Note: multiple such switches
466 may need to be specified for programs that can unwind programs on
472 libunwind\-dynamic(3),
474 libunwind\-ptrace(3),
475 libunwind\-setjmp(3),
476 unw_create_addr_space(3),
477 unw_destroy_addr_space(3),
479 unw_get_accessors(3),
481 unw_get_proc_info(3),
482 unw_get_proc_name(3),
488 unw_is_signal_frame(3),
491 unw_set_caching_policy(3),
492 unw_set_cache_size(3),
503 David Mosberger\-Tang
505 Email: \fBdmosberger@gmail.com\fP
507 WWW: \fBhttp://www.nongnu.org/libunwind/\fP\&.
508 .\" NOTE: This file is generated, DO NOT EDIT.