1 // Copyright 2005 - 2007 Google Inc.
2 // All rights reserved.
6 // Produce stack trace using libgcc
9 #include <stdlib.h> // for NULL
10 #include <unwind.h> // ABI defined unwinder
12 #include "stacktrace.h"
14 _START_GOOGLE_NAMESPACE_
24 // Workaround for the malloc() in _Unwind_Backtrace() issue.
25 static _Unwind_Reason_Code nop_backtrace(struct _Unwind_Context *uc, void *opq) {
26 return _URC_NO_REASON;
30 // This code is not considered ready to run until
31 // static initializers run so that we are guaranteed
32 // that any malloc-related initialization is done.
33 static bool ready_to_run = false;
34 class StackTraceInit {
37 // Extra call to force initialization
38 _Unwind_Backtrace(nop_backtrace, NULL);
43 static StackTraceInit module_initializer; // Force initialization
45 static _Unwind_Reason_Code GetOneFrame(struct _Unwind_Context *uc, void *opq) {
46 trace_arg_t *targ = (trace_arg_t *) opq;
48 if (targ->skip_count > 0) {
51 targ->result[targ->count++] = (void *) _Unwind_GetIP(uc);
54 if (targ->count == targ->max_depth)
55 return _URC_END_OF_STACK;
57 return _URC_NO_REASON;
60 // If you change this function, also change GetStackFrames below.
61 int GetStackTrace(void** result, int max_depth, int skip_count) {
67 skip_count += 1; // Do not include the "GetStackTrace" frame
70 targ.max_depth = max_depth;
71 targ.skip_count = skip_count;
74 _Unwind_Backtrace(GetOneFrame, &targ);
79 _END_GOOGLE_NAMESPACE_