StackTrace.cc

Go to the documentation of this file.
00001 /*
00002  *    Copyright 2005-2006 Intel Corporation
00003  * 
00004  *    Licensed under the Apache License, Version 2.0 (the "License");
00005  *    you may not use this file except in compliance with the License.
00006  *    You may obtain a copy of the License at
00007  * 
00008  *        http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  *    Unless required by applicable law or agreed to in writing, software
00011  *    distributed under the License is distributed on an "AS IS" BASIS,
00012  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  *    See the License for the specific language governing permissions and
00014  *    limitations under the License.
00015  */
00016 
00017 
00018 #include <config.h>
00019 
00020 #include "StackTrace.h"
00021 #include <dlfcn.h>
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 #include <string.h>
00025 #include <unistd.h>
00026 
00027 namespace oasys {
00028 
00029 void
00030 StackTrace::print_current_trace(bool in_sighandler)
00031 {
00032     void *stack[MAX_STACK_DEPTH];
00033     memset(stack, 0, sizeof(stack));
00034     size_t count = get_trace(stack, MAX_STACK_DEPTH, in_sighandler ? 3 : 0);
00035     if (count > 0) {
00036         print_trace(stack + 2, count - 2); // skip this fn
00037     } else {
00038         char buf[1024];
00039         strncpy(buf, "NO STACK TRACE AVAILABLE ON THIS ARCHITECTURE\n",
00040                 sizeof(buf));
00041         write(2, buf, strlen(buf));
00042     }
00043 }
00044 
00045 void
00046 StackTrace::print_trace(void *stack[], size_t count)
00047 {
00048     char buf[1024];
00049     void* addr;
00050     
00051     strncpy(buf, "STACK TRACE: ", sizeof(buf));
00052     write(2, buf, strlen(buf));
00053 
00054     for (size_t i = 0; i < count; ++i) {
00055         addr = stack[i];
00056 
00057 #if HAVE_DLADDR
00058         Dl_info info;
00059         if (dladdr(addr, &info)) {
00060             int dll_offset = (int)((char*)addr - (char*)info.dli_fbase);
00061             sprintf(buf, "0x%08x:%s@0x%08x+0x%08x ",
00062                     (u_int)addr, info.dli_fname,
00063                     (u_int)info.dli_fbase, dll_offset);
00064         } else
00065 #endif
00066         {
00067             sprintf(buf, "%p ", addr);
00068         }
00069         
00070         write(2, buf, strlen(buf));
00071     }
00072     write(2, "\n", 1);
00073 }
00074 
00075 #if defined(__i386__)
00076 #include "StackTrace-x86.cc"
00077 
00078 #elif defined(__POWERPC__) || defined(PPC)
00079 #include "StackTrace-ppc.cc"
00080 
00081 #elif HAVE_EXECINFO_H
00082 
00083 // Stack trace function using the glibc builtin
00084 #include <execinfo.h>
00085 size_t
00086 StackTrace::get_trace(void* stack[], size_t size, u_int sighandler_frame)
00087 {
00088     (void)sighandler_frame;
00089     return backtrace(stack, size);
00090 }
00091 
00092 #else 
00093 
00094 // Last resort -- just an no-op
00095 size_t
00096 StackTrace::get_trace(void* stack[], size_t size, u_int sighandler_frame)
00097 {
00098     (void)sighandler_frame;
00099     memset(stack, 0, size);
00100     return 0;
00101 }
00102 
00103 #endif
00104 
00105 } // namespace oasys

Generated on Thu Jun 7 16:56:52 2007 for DTN Reference Implementation by  doxygen 1.5.1