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