HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_StackTrace.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_StackTrace.h ( UT Library, C++)
7  *
8  * COMMENTS:
9  * Debugging utility to print stack tracebacks from anywhere in
10  * the code. Example usage:
11  *
12  * #include <UT/UT_StackTrace.h>
13  * UT_StackTrace tracer(stderr);
14  * tracer.doTraceback("in load method");
15  *
16  * From inside a signal handler use as follows:
17  *
18  * void sig_handler( UTsignalHandlerArg sig_arg)
19  * {
20  * UT_StackTrace tracer;
21  *
22  * tracer.doTraceback(sig_arg, "inside sig_handler");
23  * ...
24  * }
25  *
26  */
27 
28 #ifndef __UT_StackTrace__
29 #define __UT_StackTrace__
30 
31 #include "UT_API.h"
32 #include "UT_Signal.h"
33 #include "UT_Debug.h"
34 
35 #include <SYS/SYS_Types.h>
36 #include <stdio.h>
37 
38 class UT_WorkBuffer;
39 
40 #define TRACE_STACK() { UT_StackTrace tr; tr.doTraceback(); }
41 #define TRACE_STACK_LIMIT(lm) { UT_StackTrace tr; tr.setMaxStackLevel(lm); \
42  tr.doTraceback(); }
43 #define TRACE_STACK_FILE(fp) { UT_StackTrace tr(fp); tr.doTraceback(); }
44 #define TRACE_STACK_COND(cond){ UT_StackTrace tr; tr.doCondTraceback(cond); }
45 
46 /// Helper class to output stack traces from the current execution point.
48 {
49 public:
50  explicit UT_StackTrace(FILE *output = stderr);
51 
52  void doTraceback(UTsignalHandlerArg sig_arg,
53  const char *label = 0,
54  bool add_markers = false);
55 
56  void doTraceback(const char *label = 0,
57  bool add_markers = false);
58 
59  void doCondTraceback(const char *symbol,
60  const char *label = 0,
61  bool add_markers = false);
62 
63  void doTracebackIntoBuffer(UT_WorkBuffer &result,
64  UTsignalHandlerArg sig_arg,
65  const char *label = 0,
66  bool add_markers = false);
67  void doTracebackIntoBuffer(UT_WorkBuffer &result,
68  const char *label = 0,
69  bool add_markers = false);
70 
71  bool getSymbolNameAtAddress(const void *address,
72  UT_WorkBuffer &result);
73 
74 
75  void setShowPC(bool state)
76  {
77  myShowPC = state;
78  }
79  void setVerbose(bool state)
80  {
81  myVerbose = state;
82  }
83  void setTraceAllThreads(bool state) // defaults to OFF
84  {
85  myTraceAllThreads = state;
86  }
87  // setting the Max stack level is useful if you only want to
88  // print out a set number of levels of the stack
89  // (i.e. you are trying to find out who is directly calling a
90  // certain method)
92  {
93  myMaxStackLevel = level;
94  }
95  //
96  // Allow global control of the stack tracebacks so that we can
97  // selectively enable/disable them in chosen code blocks.
98  //
99  static void enableTraceBacks(bool state)
100  {
101  ourEnableFlag = state;
102  }
103 
104  static void enableCondTrace(const char *symbol, bool enable);
105 
106 
107 private:
108  void doTracebackIntoBuffer(UT_WorkBuffer &result,
109  UTsignalHandlerArg *sig_arg,
110  const char *label,
111  bool add_markers);
112 
113  FILE *myFile;
114  int myMaxStackLevel;
115  bool myVerbose:1,
116  myShowPC:1,
117  myTraceAllThreads:1;
118 
119  static bool ourEnableFlag;
120 };
121 
122 
123 // Simply returns the usage of the current stack of the current thread.
125 
126 // Simply returns the max size of the current stack of the current thread.
128 
129 #ifdef _WIN32
130 /// WINDOWS ONLY: Get the stack trace of an (opened) process handle with
131 /// sufficient permissions.
132 UT_API extern bool UTgetProcessStackTrace(
133  UT_WorkBuffer &result,
134  void *process,
135  bool add_markers = false);
136 #endif
137 
138 #endif
void setMaxStackLevel(int level)
Definition: UT_StackTrace.h:91
GLuint GLsizei const GLchar * label
Definition: glcorearb.h:2544
GLint level
Definition: glcorearb.h:107
#define UT_API
Definition: UT_API.h:13
int64 exint
Definition: SYS_Types.h:116
Helper class to output stack traces from the current execution point.
Definition: UT_StackTrace.h:47
void setTraceAllThreads(bool state)
Definition: UT_StackTrace.h:83
void setShowPC(bool state)
Definition: UT_StackTrace.h:75
UT_API exint UTgetCurrentStackLimit()
UT_API exint UTgetCurrentStackUsage()
void setVerbose(bool state)
Definition: UT_StackTrace.h:79
static void enableTraceBacks(bool state)
Definition: UT_StackTrace.h:99