omGetBackTrace.c
Go to the documentation of this file.
1 /*******************************************************************
2  * File: omGetBackTrace.c
3  * Purpose: routines for getting Backtraces of stack
4  * Author: obachman (Olaf Bachmann)
5  * Created: 11/99
6  *******************************************************************/
7 #ifndef OM_NDEBUG
8 
9 #if __GNUC__ > 1
10 
11 static void* om_this_main_frame_addr = 0;
12 
13 void omInitGetBackTrace()
14 {
15  if (__builtin_frame_address(0) != 0 &&
16  __builtin_frame_address(1) > __builtin_frame_address(0))
17  om_this_main_frame_addr = __builtin_frame_address(1);
18 }
19 
20 #define OM_GET_BACK_TRACE(j) \
21 case j: \
22 { \
23  f_addr = __builtin_frame_address(j); \
24  if (f_addr > this_frame && f_addr < om_this_main_frame_addr) \
25  { \
26  r_addr = __builtin_return_address(j); \
27  if (r_addr) \
28  { \
29  bt[i] = r_addr; \
30  i++; \
31  if (i >= max) break; \
32  } \
33  else break; \
34  } \
35  else break; \
36 }
37 
38 int omGetBackTrace(void** bt, int start, int max)
39 {
40  int i = 0;
41  void* this_frame = __builtin_frame_address(0);
42  void* f_addr;
43  void* r_addr;
44 
45  start++;
46 
47  switch(start)
48  {
49  OM_GET_BACK_TRACE(1)
50  OM_GET_BACK_TRACE(2)
51 /* the following fails on Mac OsX, but the debugging
52  * support it provides is too useful to disable it
53  */
54 #ifdef __linux
55 #if defined(__x86_64) || defined(__i386)
56  OM_GET_BACK_TRACE(3)
57  OM_GET_BACK_TRACE(4)
58  OM_GET_BACK_TRACE(5)
59  OM_GET_BACK_TRACE(6)
60  OM_GET_BACK_TRACE(7)
61  OM_GET_BACK_TRACE(8)
62  OM_GET_BACK_TRACE(9)
63  OM_GET_BACK_TRACE(10)
64  OM_GET_BACK_TRACE(11)
65  OM_GET_BACK_TRACE(12)
66  OM_GET_BACK_TRACE(13)
67  OM_GET_BACK_TRACE(14)
68  OM_GET_BACK_TRACE(15)
69  OM_GET_BACK_TRACE(16)
70  OM_GET_BACK_TRACE(17)
71 #endif
72 #endif
73  }
74  if (i < max) bt[i] = 0;
75  return i;
76 }
77 
78 #endif /* __GNUC__ > 1 */
79 
80 #endif /* ! OM_NDEBUG */
#define omInitGetBackTrace()
static int max(int a, int b)
Definition: fast_mult.cc:264
#define omGetBackTrace(bt, s, max)
int i
Definition: cfEzgcd.cc:123