2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / sysdep / dwarf2-backtrace.cc
blob550aa5bdd35a17e3851e0c6a6c26e42ec42f45e1
1 /* dwarf2-backtrac.cc - backtrace implementation driven by the dwarf2
2 exception unwinder. */
4 /* Copyright (C) 2003 Free Software Foundation
6 This file is part of libgcj.
8 This software is copyrighted work licensed under the terms of the
9 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
10 details. */
12 /* Written by David Daney <ddaney@avtrex.com> */
15 Although this in theory could be 'C' instead of C++, saying that it
16 is C++ and including jvm.h makes it easier to insure that the proper
17 compiler options are used. There must be unwind tables for
18 backtrace because it is on the stack when _Unwind_Backtrace is
19 called. Compiling as C++ insures this.
23 #include <config.h>
25 #include <unwind.h>
27 #include <jvm.h>
30 extern "C"
32 int backtrace (void **, int);
35 struct backtrace_state
37 int skip_count;
38 int current_level;
39 int max_level;
40 void **locations;
43 static _Unwind_Reason_Code
44 my_trace_fn (struct _Unwind_Context *uc, void *arg)
47 struct backtrace_state *bs = (struct backtrace_state *) arg;
49 if (bs->skip_count)
51 bs->skip_count--;
52 return _URC_NO_REASON;
55 _Unwind_Ptr loc = _Unwind_GetIP (uc);
57 if (bs->current_level < bs->max_level)
58 bs->locations[bs->current_level++] = (void *) loc;
60 if (bs->current_level >= bs->max_level)
61 return _URC_END_OF_STACK;
62 else
63 return _URC_NO_REASON;
67 * backtrace is defined in (some versions of) libc. This definition
68 * must match so that it can replace the libc version at link time.
70 * Fill the locations array with at most len back trace locations.
72 * Returns the number of locations actually filled in.
75 int
76 backtrace (void **locations, int len)
78 struct backtrace_state bs;
79 bs.skip_count = 1; /* Don't log the call to backtrace itself. */
80 bs.current_level = 0;
81 bs.max_level = len;
82 bs.locations = locations;
84 _Unwind_Backtrace (my_trace_fn, &bs);
85 return bs.current_level;