Tracer  v0.0.1
A platform independant stack trace generator
tracer.h
Go to the documentation of this file.
1 /* Copyright (c) 2017, Daniel Mensinger
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  * * Redistributions of source code must retain the above copyright
7  * notice, this list of conditions and the following disclaimer.
8  * * Redistributions in binary form must reproduce the above copyright
9  * notice, this list of conditions and the following disclaimer in the
10  * documentation and/or other materials provided with the distribution.
11  * * Neither the name of the Daniel Mensinger nor the
12  * names of its contributors may be used to endorse or promote products
13  * derived from this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  * DISCLAIMED. IN NO EVENT SHALL Daniel Mensinger BE LIABLE FOR ANY
19  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
27 
28 #pragma once
29 
30 #include <stddef.h>
31 #include <stdint.h>
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
37 #if UINTPTR_MAX == 0xffffffff // 32-bit
38 typedef uint32_t Address;
39 #elif UINTPTR_MAX == 0xffffffffffffffff // 64-bit
40 typedef uint64_t Address;
41 #else // wtf
42 #error "Could not detect address legth"
43 #endif
44 
45 struct tr_TracerHandler;
46 struct tr_Tracer;
47 struct tr_PrinterContainer;
48 
50 enum TR_BOOL { TR_FALSE, TR_TRUE };
51 
53 enum TR_TraceerEngines { TR_E_END = 0, TR_LIBUNWIND, TR_GLIBC, TR_WIN32_TRACER, TR_DUMMY_T };
54 
56 enum TR_DebuggerEngines { TR_D_END = 0, TR_LIBDWFL, TR_LIBBFD, TR_WIN32_INFO, TR_EXTERNAL_FALLBACK, TR_DUMMY_D };
57 
58 typedef enum TR_BOOL TR_BOOL_t;
61 
63 typedef void (*tr_callBackPTR)(void *tracer, void *printer, void *userData);
64 
67  TR_TraceerEngines_t tracer[8];
68  TR_DebuggerEngines_t debuggers[8];
69  TR_BOOL_t autoPrintToStdErr;
70  TR_BOOL_t autoPrintToFile;
71  char logFile[1024];
72  TR_BOOL_t appendToFile;
73 
75  void * callbackData;
76 
78 
79  int signums[64];
80 };
81 
84  char prefix[64];
85  char seper1[64];
86  char seper2[64];
87  char seper3[64];
88  char suffix[64];
89 
90  char colorFrameNum[64];
91  char colorNotFound[64];
92  char colorAddress[64];
93  char colorFuncName[64];
94  char colorLineInfo[64];
95  char colorModule[64];
96 
97  TR_BOOL_t shortenFiles;
98  TR_BOOL_t shortenModules;
99  TR_BOOL_t canonicalizePaths;
100 };
101 
103 struct tr_Frame {
104  Address frameAddr;
105  const char *funcName;
106  const char *moduleName;
107  const char *fileName;
108  int line;
109  int column;
110 };
111 
114  TR_TraceerEngines_t tracer[8];
115  TR_DebuggerEngines_t debuggers[8];
116 };
117 
120 typedef struct tr_Tracer tr_Tracer_t;
123 typedef struct tr_Frame tr_Frame_t;
125 
126 void tr_defaultSetup();
127 
128 /* TracerHandler */
129 
133 
135 TR_BOOL_t tr_TracerHandler__setup(tr_TracerHandler_t *trTH, tr_Printer_t *pContainer);
136 
139 
140 /* Tracer */
141 
143 tr_Tracer_t *tr_getTracerWithParam(TR_TraceerEngines_t tracer, TR_DebuggerEngines_t debugInfo);
144 tr_Tracer_t *tr_getTracerFromVoid(void *tVoid);
145 
146 void tr_Tracer__trace(tr_Tracer_t *tracer);
147 size_t tr_Tracer__getNumFrames(tr_Tracer_t *tracer);
148 tr_Frame_t tr_Tracer__getFrame(tr_Tracer_t *tracer, size_t frameNum);
149 
151 
152 void tr_freeTracer(tr_Tracer_t *tracer);
153 
154 /* PrinterContainer */
155 
156 tr_Printer_t *tr_getPrinter(void *printer);
161 
162 void *tr_PrinterContainer__get(tr_Printer_t *pContainer);
163 
164 void tr_freePrinter(tr_Printer_t *pContainer);
165 
166 /* Printer */
167 
168 /*
169  * ALL Printers are accessed with tr_Printer functions.
170  * If a function is not supported by a printer then castOK is set to TR_FALSE
171  */
172 
173 void tr_Printer__genStringPreFrame(tr_Printer_t *p, size_t frameNum, char **outStr, TR_BOOL_t *castOK);
174 void tr_Printer__genStringForFrame(tr_Printer_t *p, size_t frameNum, char **outStr, TR_BOOL_t *castOK);
175 void tr_Printer__genStringPostFrame(tr_Printer_t *p, size_t frameNum, char **outStr, TR_BOOL_t *castOK);
176 void tr_Printer__generateString(tr_Printer_t *p, char **outStr, TR_BOOL_t *castOK);
177 
178 void tr_Printer__printToFile(tr_Printer_t *p, const char *file, TR_BOOL_t append, TR_BOOL_t *castOK);
179 void tr_Printer__printToStdOut(tr_Printer_t *p, TR_BOOL_t *castOK);
180 void tr_Printer__printToStdErr(tr_Printer_t *p, TR_BOOL_t *castOK);
181 
182 void tr_Printer__enableColor(tr_Printer_t *p, TR_BOOL_t *castOK);
183 void tr_Printer__disableColor(tr_Printer_t *p, TR_BOOL_t *castOK);
184 
185 void tr_Printer__setTrace(tr_Printer_t *p, tr_Tracer_t *t, TR_BOOL_t *castOK);
186 
188 void tr_Printer__setConfig(tr_Printer_t *p, tr_DefaultPrinter_Config_t cfg, TR_BOOL_t *castOK);
189 
190 void tr_Printer__setSignum(tr_Printer_t *p, int signum, TR_BOOL_t *castOK);
191 void tr_Printer__addSystemEntry(tr_Printer_t *p, const char *name, const char *value, TR_BOOL_t *castOK);
192 
193 #ifdef __cplusplus
194 }
195 #endif
void tr_Printer__disableColor(tr_Printer_t *p, TR_BOOL_t *castOK)
Wrapper for tracer::AbstractPrinter::disableColor.
Definition: cPrinters.cpp:161
void(* tr_callBackPTR)(void *tracer, void *printer, void *userData)
C user callback for the signal handler.
Definition: tracer.h:63
void tr_Printer__setSignum(tr_Printer_t *p, int signum, TR_BOOL_t *castOK)
Wrapper for tracer::SystemInfoPrinter::setSignum.
Definition: cPrinters.cpp:240
tr_Printer_t * tr_getPrinter__file()
Wrapper for tracer::PrinterContainer::file.
const char * funcName
The name of the function.
Definition: tracer.h:105
void tr_Printer__setTrace(tr_Printer_t *p, tr_Tracer_t *t, TR_BOOL_t *castOK)
Wrapper for tracer::AbstractPrinter::setTrace.
Definition: cPrinters.cpp:171
int line
The line number in the source file.
Definition: tracer.h:108
const char * fileName
The name / path of the source file.
Definition: tracer.h:107
void tr_TracerHandler__setConfig(tr_TracerHandler_t *trTH, tr_TracerHandler_Config_t cfg)
Wrapper for tracer::TracerHandler::setConfig.
TR_BOOL
Private struct to manage PrinterContainer.
Definition: tracer.h:50
tr_callBackPTR callback
Function pointer to be called in the internal signal handler (MUST return)
Definition: tracer.h:74
void tr_Tracer__trace(tr_Tracer_t *tracer)
Wrapper for tracer::Tracer::trace.
Definition: cTracer.cpp:61
size_t tr_Tracer__getNumFrames(tr_Tracer_t *tracer)
(Indirect) Wrapper for tracer::Tracer::getFrames
Definition: cTracer.cpp:72
Internal C wrapper structure.
TR_BOOL_t tr_TracerHandler__setup(tr_TracerHandler_t *trTH, tr_Printer_t *pContainer)
Wrapper for tracer::TracerHandler::setup.
tr_Tracer_t * tr_getTracerFromVoid(void *tVoid)
Creates the priavet C struct form a void pointer to the C++ Tracer object.
Definition: cTracer.cpp:54
void tr_Printer__printToStdOut(tr_Printer_t *p, TR_BOOL_t *castOK)
Wrapper for tracer::AbstractPrinter::printToStdOut.
Definition: cPrinters.cpp:130
void tr_Printer__enableColor(tr_Printer_t *p, TR_BOOL_t *castOK)
Wrapper for tracer::AbstractPrinter::enableColor.
Definition: cPrinters.cpp:150
TR_TraceerEngines
Wrapper for tracer::TracerEngines.
Definition: tracer.h:53
void tr_Printer__generateString(tr_Printer_t *p, char **outStr, TR_BOOL_t *castOK)
Wrapper for tracer::AbstractPrinter::generateString.
Definition: cPrinters.cpp:104
tr_Printer_t * tr_getPrinter__system()
Wrapper for tracer::PrinterContainer::system.
Internal C wrapper structure.
tr_DefaultPrinter_Config_t tr_Printer__getConfig(tr_Printer_t *p, TR_BOOL_t *castOK)
Wrapper for tracer::DefaultPrinter::getConfig.
Definition: cPrinters.cpp:182
tr_Tracer_t * tr_getTracer()
Wrapper for tracer::Tracer::Tracer.
Definition: cTracer.cpp:40
tr_Tracer_AvailableEngines_t tr_Tracer__getAvailableEngines()
(Indirect) Wrapper for tracer::Tracer::getAvailableEngines and tracer::Tracer::getAvailableDebuggers ...
Definition: cTracer.cpp:118
TR_DebuggerEngines_t debuggers[8]
List of preferred engines; First try them for the Tracer (0 marks the end)
Definition: tracer.h:68
tr_Printer_t * tr_getPrinter(void *printer)
Manages the Printer.
TR_DebuggerEngines
Wrapper for tracer::DebuggerEngines.
Definition: tracer.h:56
void tr_Printer__genStringPostFrame(tr_Printer_t *p, size_t frameNum, char **outStr, TR_BOOL_t *castOK)
Wrapper for tracer::AbstractPrinter::genStringPostFrame.
Definition: cPrinters.cpp:88
void tr_Printer__setConfig(tr_Printer_t *p, tr_DefaultPrinter_Config_t cfg, TR_BOOL_t *castOK)
Wrapper for tracer::DefaultPrinter::setConfig.
Definition: cPrinters.cpp:213
tr_TracerHandler_t * tr_getTracerHandler()
Retunrns the handler as a private c struct.
TR_BOOL_t shortenFiles
The source file path.
Definition: tracer.h:97
tr_Printer_t * tr_getPrinter__plain()
Wrapper for tracer::PrinterContainer::plain.
TR_BOOL_t autoPrintToStdErr
Prints the stack trace to stderr when enabled.
Definition: tracer.h:69
TR_BOOL_t canonicalizePaths
Fixes path names if they contain "/../" or are relative.
Definition: tracer.h:99
void * tr_PrinterContainer__get(tr_Printer_t *pContainer)
Wrapper for tracer::PrinterContainer::get.
TR_BOOL_t tr_TracerHandler__defaultSetup(tr_TracerHandler_t *trTH)
Wrapper for tracer::TracerHandler::defaultSetup.
const char * moduleName
The name / path of the binary file.
Definition: tracer.h:106
Represents a stack frame.
Definition: tracer.h:103
void tr_Printer__addSystemEntry(tr_Printer_t *p, const char *name, const char *value, TR_BOOL_t *castOK)
Wrapper for tracer::SystemInfoPrinter::addSystemEntry.
Definition: cPrinters.cpp:250
Internal C wrapper structure.
Address frameAddr
The instruction pointer of the stack frame.
Definition: tracer.h:104
TR_BOOL_t shortenModules
The executable module (.so/.dll/.exe)
Definition: tracer.h:98
Helper structure for wrapping the small std::vector&#39;s.
Definition: tracer.h:113
enum TR_DebuggerEngines TR_DebuggerEngines_t
Wrapper for tracer::DebuggerEngines.
Definition: tracer.h:60
enum TR_BOOL TR_BOOL_t
Define some our own boolean values.
Definition: tracer.h:58
void tr_Printer__printToStdErr(tr_Printer_t *p, TR_BOOL_t *castOK)
Wrapper for tracer::AbstractPrinter::printToStdErr.
Definition: cPrinters.cpp:140
void tr_Printer__printToFile(tr_Printer_t *p, const char *file, TR_BOOL_t append, TR_BOOL_t *castOK)
Wrapper for tracer::AbstractPrinter::printToFile.
Definition: cPrinters.cpp:120
void tr_Printer__genStringPreFrame(tr_Printer_t *p, size_t frameNum, char **outStr, TR_BOOL_t *castOK)
Wrapper for tracer::AbstractPrinter::genStringPreFrame.
Definition: cPrinters.cpp:56
tr_Printer_t * tr_getPrinter__fancy()
Wrapper for tracer::PrinterContainer::fancy.
tr_Tracer_t * tr_getTracerWithParam(TR_TraceerEngines_t tracer, TR_DebuggerEngines_t debugInfo)
Wrapper for tracer::Tracer::Tracer(TraceerEngines, DebuggerEngines)
Definition: cTracer.cpp:47
void tr_freeTracer(tr_Tracer_t *tracer)
Destroyes the private C object and the Tracer C++ object UNLESS tr_getTracerFromVoid was used...
Definition: cTracer.cpp:142
int column
The column in the source file.
Definition: tracer.h:109
void tr_freeTracerHandler(tr_TracerHandler_t *trTH)
Frees the handler.
TR_BOOL_t appendToFile
Overides file co; Requires autoPrintToFile.
Definition: tracer.h:72
User configuration structure.
Definition: tracer.h:83
tr_Frame_t tr_Tracer__getFrame(tr_Tracer_t *tracer, size_t frameNum)
(Indirect) Wrapper for tracer::Tracer::getFrames
Definition: cTracer.cpp:83
int signums[64]
List of signals to handle (0 marks the end of the list)
Definition: tracer.h:79
void tr_TracerHandler__reset()
Wrapper for tracer::TracerHandler::reset.
enum TR_TraceerEngines TR_TraceerEngines_t
Wrapper for tracer::TracerEngines.
Definition: tracer.h:59
char logFile[1024]
The file to automatically print to; Requires autoPrintToFile.
Definition: tracer.h:71
void tr_freePrinter(tr_Printer_t *pContainer)
Frees the Printer container.
TR_BOOL_t callDefultHandlerWhenDone
The signal handler will call the default signal handler when done.
Definition: tracer.h:77
void tr_Printer__genStringForFrame(tr_Printer_t *p, size_t frameNum, char **outStr, TR_BOOL_t *castOK)
Wrapper for tracer::AbstractPrinter::genStringForFrame.
Definition: cPrinters.cpp:72
Configuration.
Definition: tracer.h:66
TR_BOOL_t autoPrintToFile
Automatically writes the stack trace to a file when enabled.
Definition: tracer.h:70
void * callbackData
User defined data to be send to the callback function.
Definition: tracer.h:75
tr_TracerHandler_Config_t tr_TracerHandler__getConfig(tr_TracerHandler_t *trTH)
Wrapper for tracer::TracerHandler::getConfig.
void tr_defaultSetup()
Runs tracer::TracerHandler::getTracer()->defaultSetup();.