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