StackTrace-x86.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 #if defined(__i386__)
00019 
00020 #include "FatalSignals.h"
00021 
00022 #if defined(__linux__)
00023 #include <asm/sigcontext.h>
00024 struct sigframe
00025 {
00026         char *pretcode;
00027         int sig;
00028         struct sigcontext sc;
00029         struct _fpstate fpstate;
00030 };
00031 #endif
00032 
00033 // Generally, this function just follows the frame pointer and looks
00034 // at the return address (i.e. the next one on the stack).
00035 //
00036 // Things get a little funky in the frame for the signal handler
00037 // (identified by the parameter sighandler_frame), where we need to
00038 // look into the place where the kernel stored the faulting address.
00039 size_t
00040 StackTrace::get_trace(void* stack[], size_t size, u_int sighandler_frame)
00041 {
00042     void **fp;
00043 
00044     asm volatile("movl %%ebp,%0" : "=r" (fp));
00045 
00046     stack[0] = 0; // fake frame for this this fn, just use 0
00047     size_t frame = 1;
00048     while (frame < size) {
00049         if (*(fp + 1) == 0 || *fp == 0)
00050             break;
00051         
00052         if (sighandler_frame != 0 && frame == sighandler_frame) {
00053 #if defined(__linux__) 
00054             struct sigframe* sf = (struct sigframe*)(fp+1);
00055             struct sigcontext* scxt = &(sf->sc);
00056             stack[frame] = (void*) scxt->eip;
00057 #else
00058             stack[frame] = *(fp + 1);
00059 #endif 
00060         } else {
00061             stack[frame] = *(fp + 1);
00062         }
00063         
00064         fp = (void **)(*fp);
00065         ++frame;
00066     }
00067 
00068     return frame;
00069 }
00070 
00071 #endif /* __i386__ */

Generated on Sat Sep 8 08:43:34 2007 for DTN Reference Implementation by  doxygen 1.5.3