76068a65091744f747c083b81d291dfdd2e6a0f9
[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_stack.h"
37
38
39 void
40 debug_backtrace_capture(struct debug_stack_frame *backtrace,
41 unsigned start_frame,
42 unsigned nr_frames)
43 {
44 const void **frame_pointer = NULL;
45 unsigned i = 0;
46
47 if(!nr_frames)
48 return;
49
50 #if defined(PIPE_CC_GCC)
51 frame_pointer = ((const void **)__builtin_frame_address(1));
52 #elif defined(PIPE_CC_MSVC)
53 __asm {
54 mov frame_pointer, ebp
55 }
56 frame_pointer = (const void **)frame_pointer[0];
57 #else
58 frame_pointer = NULL;
59 #endif
60
61
62 #ifdef PIPE_ARCH_X86
63 while(nr_frames) {
64 if(!frame_pointer)
65 break;
66
67 if(start_frame)
68 --start_frame;
69 else {
70 backtrace[i++].function = frame_pointer[1];
71 --nr_frames;
72 }
73
74 frame_pointer = (const void **)frame_pointer[0];
75 }
76 #endif
77
78 while(nr_frames) {
79 backtrace[i++].function = NULL;
80 --nr_frames;
81 }
82 }
83
84
85 void
86 debug_backtrace_dump(const struct debug_stack_frame *backtrace,
87 unsigned nr_frames)
88 {
89 unsigned i;
90
91 for(i = 0; i < nr_frames; ++i) {
92 if(!backtrace[i].function)
93 break;
94 debug_printf("\t%p\n", backtrace[i].function);
95 }
96 }
97