StackTrace.cc

Go to the documentation of this file.
00001 /*
00002  * IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. By
00003  * downloading, copying, installing or using the software you agree to
00004  * this license. If you do not agree to this license, do not download,
00005  * install, copy or use the software.
00006  * 
00007  * Intel Open Source License 
00008  * 
00009  * Copyright (c) 2005 Intel Corporation. All rights reserved. 
00010  * 
00011  * Redistribution and use in source and binary forms, with or without
00012  * modification, are permitted provided that the following conditions are
00013  * met:
00014  * 
00015  *   Redistributions of source code must retain the above copyright
00016  *   notice, this list of conditions and the following disclaimer.
00017  * 
00018  *   Redistributions in binary form must reproduce the above copyright
00019  *   notice, this list of conditions and the following disclaimer in the
00020  *   documentation and/or other materials provided with the distribution.
00021  * 
00022  *   Neither the name of the Intel Corporation nor the names of its
00023  *   contributors may be used to endorse or promote products derived from
00024  *   this software without specific prior written permission.
00025  *  
00026  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00027  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00028  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00029  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR
00030  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00031  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00032  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00033  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00034  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00035  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00036  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037  */
00038 
00039 #include <config.h>
00040 
00041 #include "StackTrace.h"
00042 #include <dlfcn.h>
00043 #include <stdio.h>
00044 #include <stdlib.h>
00045 #include <string.h>
00046 #include <unistd.h>
00047 
00048 namespace oasys {
00049 
00050 void
00051 StackTrace::print_current_trace(bool in_sighandler)
00052 {
00053     void *stack[MAX_STACK_DEPTH];
00054     memset(stack, 0, sizeof(stack));
00055     size_t count = get_trace(stack, MAX_STACK_DEPTH, in_sighandler ? 3 : 0);
00056     if (count > 0) {
00057         print_trace(stack + 2, count - 2); // skip this fn
00058     } else {
00059         char buf[1024];
00060         strncpy(buf, "NO STACK TRACE AVAILABLE ON THIS ARCHITECTURE\n",
00061                 sizeof(buf));
00062         write(2, buf, strlen(buf));
00063     }
00064 }
00065 
00066 void
00067 StackTrace::print_trace(void *stack[], size_t count)
00068 {
00069     char buf[1024];
00070     void* addr;
00071     
00072     strncpy(buf, "STACK TRACE: ", sizeof(buf));
00073     write(2, buf, strlen(buf));
00074 
00075     for (size_t i = 0; i < count; ++i) {
00076         addr = stack[i];
00077 
00078 #if HAVE_DLADDR
00079         Dl_info info;
00080         if (dladdr(addr, &info)) {
00081             int dll_offset = (int)((char*)addr - (char*)info.dli_fbase);
00082             sprintf(buf, "0x%08x:%s@0x%08x+0x%08x ",
00083                     (u_int)addr, info.dli_fname,
00084                     (u_int)info.dli_fbase, dll_offset);
00085         } else
00086 #endif
00087         {
00088             sprintf(buf, "%p ", addr);
00089         }
00090         
00091         write(2, buf, strlen(buf));
00092     }
00093     write(2, "\n", 1);
00094 }
00095 
00096 #if defined(__i386__)
00097 #include "StackTrace-x86.cc"
00098 
00099 #elif defined(__POWERPC__) || defined(PPC)
00100 #include "StackTrace-ppc.cc"
00101 
00102 #elif HAVE_EXECINFO_H
00103 
00104 // Stack trace function using the glibc builtin
00105 #include <execinfo.h>
00106 size_t
00107 StackTrace::get_trace(void* stack[], size_t size, u_int sighandler_frame)
00108 {
00109     (void)sighandler_frame;
00110     return backtrace(stack, size);
00111 }
00112 
00113 #else 
00114 
00115 // Last resort -- just an no-op
00116 size_t
00117 StackTrace::get_trace(void* stack[], size_t size, u_int sighandler_frame)
00118 {
00119     (void)sighandler_frame;
00120     memset(stack, 0, size);
00121     return 0;
00122 }
00123 
00124 #endif
00125 
00126 } // namespace oasys

Generated on Fri Dec 22 14:48:00 2006 for DTN Reference Implementation by  doxygen 1.5.1