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