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