blob: be64886d582a205c698d4fb197025b716d46295b [file] [log] [blame]
mostang.com!davidmb5a54062003-01-17 07:48:52 +00001'\" t
mostang.com!davidm9eb9f312003-04-01 07:19:34 +00002.\" Manual page created with latex2man on Mon Mar 31 14:18:01 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!davidm9eb9f312003-04-01 07:19:34 +000013.TH "LIBUNWIND" "3" "31 March 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
148achieved by making copyies of an unwind cursor. For example, a
149program that sometimes has to move ``down\&'' by one stack frame could
150maintain two cursor variables: ``curr\&''
151and ``prev\&''\&.
152The
153former would be used as the current cursor and prev
154would be
155maintained as the ``previous frame\&'' cursor by copying the contents of
156curr
157to prev
158right before calling unw_step().
159With this approach, the program could move one step ``down\&'' simply by
160copying back prev
161to curr
162whenever that is necessary. In
163the mosts extreme case, a program could maintain a separate cursor for
164each call frame and that way it could move up and down the call frame
165chain at will.
166.PP
167Given an unwind cursor, it is possible to read and write the CPU
168registers that were preserved for the current stack frame identified
169by the cursor. Libunwind
170provides several routines for this
171purpose: unw_get_reg()
172reads an integer (general) register,
173unw_get_fpreg()
174reads 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>\&.
220If is perfectly OK for a single program to
221employ 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).
228.PP
229If we put all of the above together, here is how we could use
230libunwind
231write function show_backtrace()
232which prints
233a classic stack trace:
234.PP
235.Vb
236#define UNW_LOCAL_ONLY
237#include <libunwind.h>
238
239void show_backtrace (void) {
240 unw_cursor_t cursor; unw_context_t uc;
241 unw_word_t ip, sp;
242
243 unw_getcontext(&uc);
244 unw_init_local(&cursor, &uc);
245 while (unw_step(&cursor) > 0) {
246 unw_get_reg(&cursor, UNW_REG_IP, &ip);
247 unw_get_reg(&cursor, UNW_REG_SP, &sp);
248 printf ("ip = 0, sp = 0\\n", (long) ip, (long) sp);
249 }
250}
251.Ve
252.PP
253.SH REMOTE UNWINDING
254
255.PP
256Libunwind
257can also be used to unwind a stack in a ``remote\&''
258process. Here, ``remote\&'' may mean another process on the same
259machine or even a process on a completely different machine from the
260one that is running libunwind\&.
261Remote unwinding is typically
262used by debuggers and instruction\-set simulators, for example.
263.PP
264Before you can unwind a remote process, you need to create a new
265address\-space object for that process. This is achieved with the
266unw_create_addr_space
267routine. The routine takes two
268arguments: a pointer to a set of \fIaccessor\fP
269routines and an
270integer that specifies the byte\-order of the target process. The
271accessor routines provide libunwind
272with the means to
273communicate with the remote process. In particular, there are
274callbacks to read and write the process\&'s memory, its registers, and
275to access unwind information which may be needed by libunwind\&.
276.PP
277With the address space created, unwinding can be initiated by a call
278to unw_init_remote().
279This routine is very similar to
280unw_init_local(),
281except that it takes an address\-space
282object and an opaque pointer as arguments. The routine uses these
283arguments to fetch the initial machine state. Libunwind
284never
285uses the opaque pointer on its own, but instead justs passes it on to
286the accessor (callback) routines. Typically, this pointer is used to
287select, e.g., the thread within a process that is to be unwound.
288.PP
289Once a cursor has been initialized with unw_init_remote(),
290unwinding works exactly like in the local case. That is, you can use
291unw_step
292to move ``up\&'' in the call\-chain, read and write
293registers, or resume execution at a particular stack frame by calling
294unw_resume\&.
295.PP
296.SH CROSS\-PLATFORM AND MULTI\-PLATFORM UNWINDING
297
298.PP
299Libunwind
300has been designed to enable unwinding across
301platforms (architectures). Indeed, a single program can use
302libunwind
303to unwind an arbitrary number of target platforms,
304all at the same time!
305.PP
306We call the machine that is running libunwind
307the \fIhost\fP
308and the machine that is running the process being unwound the
309\fItarget\fP\&.
310If the host and the target platform are the same, we
311call it \fInative\fP
312unwinding. If they differ, we call it
313\fIcross\-platform\fP
314unwinding.
315.PP
316The principle behind supporting native, cross\-platform, and
317multi\-platform unwinding are very simple: for native unwinding, a
318program includes <libunwind.h>
319and uses the linker switch
320\fB\-l\fPunwind\&.
321For cross\-platform unwinding, a program
322includes <libunwind\-PLAT\&.h>
323and uses the linker
324switch \fB\-l\fPunwind\-PLAT,
325where PLAT
326is the name
327of the target platform (e.g., ia64
328for IA\-64, hppa\-elf
329for ELF\-based HP PA\-RISC, or x86
330for 80386). Multi\-platform
331unwinding works exactly like cross\-platform unwinding, the only
332limitation is that a single source file (compilation unit) can include
333at most one libunwind
334header file. In other words, the
335platform\-specific support for each supported target needs to be
336isolated in separate source files\-\-\-a limitation that shouldn\&'t be an
337issue in practice.
338.PP
339Note that, by definition, local unwinding is possible only for the
340native case. Attempting to call, e.g., unw_local_init()
341when
342targeting a cross\-platform will result in a link\-time error
343(unresolved references).
344.PP
345.SH THREAD\- AND SIGNAL\-SAFETY
346
347.PP
348All libunwind
349routines are thread\-safe. What this means is
350that multiple threads may use libunwind
351simulatenously.
352However, any given cursor may be accessed by only one thread at
353any given time.
354.PP
355To ensure thread\-safety, some libunwind
356routines may have to
357use locking. Such routines \fImust not\fP
358be called from signal
359handlers (directly or indirectly) and are therefore \fInot\fP
360signal\-safe. The manual page for each libunwind
361routine
362identifies whether or not it is signal\-safe, but as a general rule,
363any routine that may be needed for \fIlocal\fP
364unwinding is
365signal\-safe (e.g., unw_step()
366for local unwinding is
367signal\-safe). For remote\-unwinding, \fInone\fP
368of the
369libunwind
370routines are guaranteed to be signal\-safe.
371.PP
372.SH UNWINDING THROUGH DYNAMICALLY GENERATED CODE
373
374.PP
375Libunwind
376provides the routines _U_dyn_register()
377and
378_U_dyn_cancel
379to register/cancel the information required to
380unwind through code that has been generated at runtime (e.g., by a
381just\-in\-time (JIT) compiler). It is important to register the
382information for \fIall\fP
383dynamically generated code because
384otherwise, a debugger may not be able to function properly or
385high\-level language exception handling may not work as expected.
386.PP
387The interface for registering and canceling dynamic unwind info has
388been designed for maximum efficiency, so as to minimize the
389performance impact on JIT\-compilers. In particular, both routines are
390guaranteed to execute in ``constant time\&'' (O(1)) and the
391data\-structure encapsulating the dynamic unwind info has been designed
392to facilitate sharing, such that similar procedures can share much of
393the underlying information.
394.PP
395.SH CACHING OF UNWIND INFO
396
397.PP
398To speed up execution, libunwind
399may aggressively cache the
400information it needs to perform unwinding. If a process changes
401during its lifetime, this creates a risk of libunwind
402using
403stale data. For example, this would happen if libunwind
404were
405to cache information about a shared library which later on gets
406unloaded (e.g., via \fIdlclose\fP(3)).
407.PP
408To prevent the risk of using stale data, libunwind
409provides two
410facilities: first, it is possible to flush the cached information
411associated with a specific address range in the target process (or the
412entire address space, if desired). This functionality is provided by
413unw_flush_cache().
414The second facility is provided by
415unw_set_caching_policy(),
416which lets a program
417select the exact caching policy in use for a given address\-space
418object. In particular, by selecting the policy
419UNW_CACHE_NONE,
420it is possible to turn off caching
421completely, therefore eliminating the risk of stale data alltogether
422(at the cost of slower execution). By default, caching is enabled for
423local unwinding only.
424.PP
425.SH FILES
426
427.PP
428.TP
429libunwind.h
430 Headerfile to include for native (same
431platform) unwinding.
432.TP
433libunwind\-PLAT\&.h
434 Headerfile to include when
435unwind target runs on platform PLAT\&.
436For example, to unwind
437an IA\-64 program, the header file libunwind\-ia64.h
438should be
439included.
440.TP
441\fB\-l\fPunwind
442 Linker\-switch to add when building a
443program that does native (same platform) unwinding.
444.TP
445\fB\-l\fPunwind\-PLAT
446 Linker\-switch to add when
447building a program that unwinds a program on platform PLAT\&.
448For example, to (cross\-)unwind an IA\-64 program, the linker switch
449\-lunwind\-ia64
450should be added. Note: multiple such switches
mostang.com!davidma4e77a22003-03-27 04:29:07 +0000451may need to be specified for programs that can unwind programs on
mostang.com!davidmb5a54062003-01-17 07:48:52 +0000452multiple platforms.
453.PP
454.SH SEE ALSO
455
456.PP
mostang.com!davidma8db0b32003-02-08 10:10:59 +0000457libunwind\-ia64(3),
458libunwind\-ptrace(3),
459libunwind\-setjmp(3),
mostang.com!davidm624251b2003-03-20 07:51:37 +0000460unw_create_addr_space(3),
461unw_destroy_addr_space(3),
462unw_flush_cache(3),
463unw_get_accessors(3),
464unw_get_fpreg(3),
465unw_get_proc_info(3),
466unw_get_proc_name(3),
467unw_get_reg(3),
468unw_getcontext(3),
469unw_init_local(3),
470unw_init_remote(3),
471unw_is_fpreg(3),
472unw_is_signal_frame(3),
473unw_regname(3),
474unw_resume(3),
475unw_set_caching_policy(3),
476unw_set_fpreg(3),
477unw_set_reg(3),
478unw_step(3)
mostang.com!davidmb5a54062003-01-17 07:48:52 +0000479.PP
480.SH AUTHOR
481
482.PP
483David Mosberger\-Tang
484.br
485Hewlett\-Packard Labs
486.br
487Palo\-Alto, CA 94304
488.br
489Email: \fBdavidm@hpl.hp.com\fP
490.br
491WWW: \fBhttp://www.hpl.hp.com/research/linux/libunwind/\fP\&.
492.\" NOTE: This file is generated, DO NOT EDIT.