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 
84  // WARNING: myTraceAllThreads is only implemented for Windows
85  void setTraceAllThreads(bool state) // defaults to OFF
86  {
87  myTraceAllThreads = state;
88  }
89 
90  // setting the Max stack level is useful if you only want to
91  // print out a set number of levels of the stack
92  // (i.e. you are trying to find out who is directly calling a
93  // certain method)
95  {
96  myMaxStackLevel = level;
97  }
98  //
99  // Allow global control of the stack tracebacks so that we can
100  // selectively enable/disable them in chosen code blocks.
101  //
102  static void enableTraceBacks(bool state)
103  {
104  ourEnableFlag = state;
105  }
106 
107  static void enableCondTrace(const char *symbol, bool enable);
108 
109 
110 private:
111  void doTracebackIntoBuffer(UT_WorkBuffer &result,
112  UTsignalHandlerArg *sig_arg,
113  const char *label,
114  bool add_markers);
115 
116  FILE *myFile;
117  int myMaxStackLevel;
118  bool myVerbose:1,
119  myShowPC:1,
120  myTraceAllThreads:1;
121 
122  static bool ourEnableFlag;
123 };
124 
125 
126 // Simply returns the usage of the current stack of the current thread.
128 
129 // Simply returns the max size of the current stack of the current thread.
131 
132 /// WINDOWS ONLY: Find bad modules and report an error message if found
134 
135 #ifdef _WIN32
136 /// WINDOWS ONLY: Get the stack trace of an (opened) process handle with
137 /// sufficient permissions.
138 UT_API extern bool UTgetProcessStackTrace(
140  void *process,
141  bool add_markers = false);
142 #endif
143 
144 #endif
void setMaxStackLevel(int level)
Definition: UT_StackTrace.h:94
GLuint GLsizei const GLchar * message
Definition: glcorearb.h:2543
GLuint GLsizei const GLchar * label
Definition: glcorearb.h:2545
int64 exint
Definition: SYS_Types.h:125
GLint level
Definition: glcorearb.h:108
#define UT_API
Definition: UT_API.h:14
**But if you need a result
Definition: thread.h:613
Helper class to output stack traces from the current execution point.
Definition: UT_StackTrace.h:47
void setTraceAllThreads(bool state)
Definition: UT_StackTrace.h:85
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
UT_API bool UTdetectBadModules(UT_WorkBuffer &message)
WINDOWS ONLY: Find bad modules and report an error message if found.
static void enableTraceBacks(bool state)