Tracer  v0.0.1
A platform independant stack trace generator
cTracerHandler.cpp
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 #include "defines.hpp"
29 #include "tracer.h"
30 #include "tracerInternal.hpp"
31 #include <string.h>
32 
33 using namespace tracer;
34 using namespace std;
35 
36 extern "C" {
37 
40 
44 
45  if (!trTH)
46  return nullptr;
47 
49  trTH->cfg = trTH->handler->getConfig();
50 
51  return trTH;
52 }
53 
59  if (trTH)
60  delete trTH;
61 }
62 
65  if (!trTH)
66  return TR_FALSE;
67 
68  return trTH->handler->defaultSetup() == true ? TR_TRUE : TR_FALSE;
69 }
70 
73  if (!pContainer || !trTH)
74  return TR_FALSE;
75 
76  return trTH->handler->setup(move(pContainer->obj)) == true ? TR_TRUE : TR_FALSE;
77 }
78 
81 
82 
86  memset(&cfg, 0, sizeof(cfg));
87 
88  if (!trTH)
89  return cfg;
90 
91  trTH->cfg = trTH->handler->getConfig();
92 
93  for (size_t i = 0; i < 8 && i < trTH->cfg.preferredTracerEngines.size(); ++i) {
94  cfg.tracer[i] = static_cast<TR_TraceerEngines_t>(trTH->cfg.preferredTracerEngines[i]);
95  }
96 
97  for (size_t i = 0; i < 8 && i < trTH->cfg.preferredDebuggerEngines.size(); ++i) {
98  cfg.debuggers[i] = static_cast<TR_DebuggerEngines_t>(trTH->cfg.preferredDebuggerEngines[i]);
99  }
100 
101  cfg.autoPrintToStdErr = trTH->cfg.autoPrintToStdErr ? TR_TRUE : TR_FALSE;
102  cfg.autoPrintToFile = trTH->cfg.autoPrintToFile ? TR_TRUE : TR_FALSE;
103  cfg.appendToFile = trTH->cfg.appendToFile ? TR_TRUE : TR_FALSE;
104  cfg.callDefultHandlerWhenDone = trTH->cfg.callDefultHandlerWhenDone ? TR_TRUE : TR_FALSE;
105 
106  strncpy(cfg.logFile, trTH->cfg.logFile.c_str(), 1024);
107 
108  cfg.callback = reinterpret_cast<tr_callBackPTR>(trTH->cfg.callback);
109  cfg.callbackData = trTH->cfg.callbackData;
110 
111 
112  for (size_t i = 0; i < 64 && i < trTH->cfg.signums.size(); ++i) {
113  cfg.signums[i] = trTH->cfg.signums[i];
114  }
115 
116  return cfg;
117 }
118 
121  if (!trTH)
122  return;
123 
124  trTH->cfg.preferredTracerEngines.clear();
125  for (size_t i = 0; i < 8; ++i) {
126  if (cfg.tracer[i] == 0)
127  break;
128 
129  trTH->cfg.preferredTracerEngines.push_back(static_cast<TraceerEngines>(cfg.tracer[i]));
130  }
131 
132  trTH->cfg.preferredDebuggerEngines.clear();
133  for (size_t i = 0; i < 8; ++i) {
134  if (cfg.tracer[i] == 0)
135  break;
136 
137  trTH->cfg.preferredDebuggerEngines.push_back(static_cast<DebuggerEngines>(cfg.tracer[i]));
138  }
139 
140  trTH->cfg.autoPrintToStdErr = cfg.autoPrintToStdErr == TR_TRUE;
141  trTH->cfg.autoPrintToFile = cfg.autoPrintToFile == TR_TRUE;
142  trTH->cfg.appendToFile = cfg.appendToFile == TR_TRUE;
144 
145  trTH->cfg.logFile = cfg.logFile;
146 
147  trTH->cfg.callback = reinterpret_cast<TracerHandler::callBackPTR>(cfg.callback);
148  trTH->cfg.callbackData = cfg.callbackData;
149 
150  trTH->cfg.signums.clear();
151  for (size_t i = 0; i < 64; ++i) {
152  if (cfg.signums[i] == 0)
153  break;
154 
155  trTH->cfg.signums.push_back(cfg.signums[i]);
156  }
157 
158  trTH->handler->setConfig(trTH->cfg);
159 }
160 }
void tr_freeTracerHandler(tr_TracerHandler_t *trTH)
Frees the handler.
void(* tr_callBackPTR)(void *tracer, void *printer, void *userData)
C user callback for the signal handler.
Definition: tracer.h:63
std::vector< TraceerEngines > preferredTracerEngines
List of preferred engines; First try them for the Tracer.
void setConfig(Config c)
Sets the new configuration.
void tr_TracerHandler__reset()
Wrapper for tracer::TracerHandler::reset.
tr_callBackPTR callback
Function pointer to be called in the internal signal handler (MUST return)
Definition: tracer.h:74
void(* callBackPTR)(Tracer *tracer, AbstractPrinter *printer, void *userData)
The typedef callback for the user.
Internal C wrapper structure.
std::vector< int > signums
List of signals to handle.
bool appendToFile
Overides file co; Requires autoPrintToFile.
std::string logFile
The file to automatically print to; Requires autoPrintToFile.
tr_TracerHandler_t * tr_getTracerHandler()
Retunrns the handler as a private c struct.
bool autoPrintToFile
Automatically writes the stack trace to a file when enabled.
STL namespace.
callBackPTR callback
Function pointer to be called in the internal signal handler (MUST return)
TR_BOOL_t tr_TracerHandler__defaultSetup(tr_TracerHandler_t *trTH)
Wrapper for tracer::TracerHandler::defaultSetup.
Internal C wrapper structure.
bool defaultSetup()
Basic setup, sufficient for most use cases.
std::vector< DebuggerEngines > preferredDebuggerEngines
List of preferred engines; First try them for the Tracer.
TR_DebuggerEngines_t debuggers[8]
List of preferred engines; First try them for the Tracer (0 marks the end)
Definition: tracer.h:68
bool setup(PrinterContainer printerToUse)
Sets everything up with a custom printer.
tracer::PrinterContainer obj
PrinterContainer object.
void tr_TracerHandler__setConfig(tr_TracerHandler_t *trTH, tr_TracerHandler_Config_t cfg)
Wrapper for tracer::TracerHandler::setConfig.
TR_BOOL_t autoPrintToStdErr
Prints the stack trace to stderr when enabled.
Definition: tracer.h:69
tracer::TracerHandler::Config cfg
TracerHandler config.
static TracerHandler * getTracer()
Returns a pointer to the TracerHandler object.
tracer::TracerHandler * handler
TracerHandler pointer.
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
TR_BOOL_t appendToFile
Overides file co; Requires autoPrintToFile.
Definition: tracer.h:72
TR_TraceerEngines_t tracer[8]
List of preferred engines; First try them for the Tracer (0 marks the end)
Definition: tracer.h:67
void tr_defaultSetup()
Runs tracer::TracerHandler::getTracer()->defaultSetup();.
Config getConfig() const
Returns the current configuration.
int signums[64]
List of signals to handle (0 marks the end of the list)
Definition: tracer.h:79
enum TR_TraceerEngines TR_TraceerEngines_t
Wrapper for tracer::TracerEngines.
Definition: tracer.h:59
TR_BOOL_t tr_TracerHandler__setup(tr_TracerHandler_t *trTH, tr_Printer_t *pContainer)
Wrapper for tracer::TracerHandler::setup.
char logFile[1024]
The file to automatically print to; Requires autoPrintToFile.
Definition: tracer.h:71
bool autoPrintToStdErr
Prints the stack trace to stderr when enabled.
bool callDefultHandlerWhenDone
The signal handler will call the default signal handler when done.
TR_BOOL_t callDefultHandlerWhenDone
The signal handler will call the default signal handler when done.
Definition: tracer.h:77
tr_TracerHandler_Config_t tr_TracerHandler__getConfig(tr_TracerHandler_t *trTH)
Wrapper for tracer::TracerHandler::getConfig.
Configuration.
Definition: tracer.h:66
TR_BOOL_t autoPrintToFile
Automatically writes the stack trace to a file when enabled.
Definition: tracer.h:70
struct tr_TracerHandler tr_TracerHandler_t
Manages TracerHandler.
Definition: tracer.h:118
static void reset()
Resets the singleton.
void * callbackData
User defined data to be send to the callback function.
Definition: tracer.h:75
void * callbackData
User defined data to be send to the callback function.