00001 // Copyright (c) 2005, Google Inc. 00002 // All rights reserved. 00003 // 00004 // Redistribution and use in source and binary forms, with or without 00005 // modification, are permitted provided that the following conditions are 00006 // met: 00007 // 00008 // * Redistributions of source code must retain the above copyright 00009 // notice, this list of conditions and the following disclaimer. 00010 // * Redistributions in binary form must reproduce the above 00011 // copyright notice, this list of conditions and the following disclaimer 00012 // in the documentation and/or other materials provided with the 00013 // distribution. 00014 // * Neither the name of Google Inc. nor the names of its 00015 // contributors may be used to endorse or promote products derived from 00016 // this software without specific prior written permission. 00017 // 00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00029 00030 // --- 00031 // Author: Arun Sharma 00032 // 00033 // Produce stack trace using libgcc 00034 00035 extern "C" { 00036 #include <stdlib.h> // for NULL 00037 #include <unwind.h> // ABI defined unwinder 00038 } 00039 #include "google/stacktrace.h" 00040 00041 typedef struct { 00042 void **result; 00043 int max_depth; 00044 int skip_count; 00045 int count; 00046 } trace_arg_t; 00047 00048 00049 // Workaround for the malloc() in _Unwind_Backtrace() issue. 00050 static _Unwind_Reason_Code nop_backtrace(struct _Unwind_Context *uc, void *opq) { 00051 return _URC_NO_REASON; 00052 } 00053 00054 00055 // This code is not considered ready to run until 00056 // static initializers run so that we are guaranteed 00057 // that any malloc-related initialization is done. 00058 static bool ready_to_run = false; 00059 class StackTraceInit { 00060 public: 00061 StackTraceInit() { 00062 // Extra call to force initialization 00063 _Unwind_Backtrace(nop_backtrace, NULL); 00064 ready_to_run = true; 00065 } 00066 }; 00067 00068 static StackTraceInit module_initializer; // Force initialization 00069 00070 static _Unwind_Reason_Code GetOneFrame(struct _Unwind_Context *uc, void *opq) { 00071 trace_arg_t *targ = (trace_arg_t *) opq; 00072 00073 if (targ->skip_count > 0) { 00074 targ->skip_count--; 00075 } else { 00076 targ->result[targ->count++] = (void *) _Unwind_GetIP(uc); 00077 } 00078 00079 if (targ->count == targ->max_depth) 00080 return _URC_END_OF_STACK; 00081 00082 return _URC_NO_REASON; 00083 } 00084 00085 int GetStackTrace(void** result, int max_depth, int skip_count) { 00086 if (!ready_to_run) 00087 return 0; 00088 00089 trace_arg_t targ; 00090 00091 skip_count += 1; // Do not include the "GetStackTrace" frame 00092 00093 targ.result = result; 00094 targ.max_depth = max_depth; 00095 targ.skip_count = skip_count; 00096 targ.count = 0; 00097 00098 _Unwind_Backtrace(GetOneFrame, &targ); 00099 00100 return targ.count; 00101 }