util: Lookup symbol names from addresses.
[mesa.git] / src / gallium / auxiliary / util / u_debug_stack.c
1 /**************************************************************************
2 *
3 * Copyright 2009 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /**
29 * @file
30 * Stack backtracing.
31 *
32 * @author Jose Fonseca <jfonseca@vmware.com>
33 */
34
35 #include "u_debug.h"
36 #include "u_debug_symbol.h"
37 #include "u_debug_stack.h"
38
39
40 void
41 debug_backtrace_capture(struct debug_stack_frame *backtrace,
42 unsigned start_frame,
43 unsigned nr_frames)
44 {
45 const void **frame_pointer = NULL;
46 unsigned i = 0;
47
48 if(!nr_frames)
49 return;
50
51 #if defined(PIPE_CC_GCC)
52 frame_pointer = ((const void **)__builtin_frame_address(1));
53 #elif defined(PIPE_CC_MSVC) && defined(PIPE_ARCH_X86)
54 __asm {
55 mov frame_pointer, ebp
56 }
57 frame_pointer = (const void **)frame_pointer[0];
58 #else
59 frame_pointer = NULL;
60 #endif
61
62
63 #ifdef PIPE_ARCH_X86
64 while(nr_frames) {
65 if(!frame_pointer)
66 break;
67
68 if(start_frame)
69 --start_frame;
70 else {
71 backtrace[i++].function = frame_pointer[1];
72 --nr_frames;
73 }
74
75 frame_pointer = (const void **)frame_pointer[0];
76 }
77 #endif
78
79 while(nr_frames) {
80 backtrace[i++].function = NULL;
81 --nr_frames;
82 }
83 }
84
85
86 void
87 debug_backtrace_dump(const struct debug_stack_frame *backtrace,
88 unsigned nr_frames)
89 {
90 unsigned i;
91
92 for(i = 0; i < nr_frames; ++i) {
93 if(!backtrace[i].function)
94 break;
95 debug_symbol_print(backtrace[i].function);
96 }
97 }
98