Tracer  v0.0.1
A platform independant stack trace generator
cTracer.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 <limits>
32 #include <string.h>
33 
34 using namespace tracer;
35 using namespace std;
36 
37 extern "C" {
38 
41  tr_Tracer_t *t = new tr_Tracer_t;
42  t->tPTR = &t->tracer;
43  return t;
44 }
45 
48  tr_Tracer_t *t = new tr_Tracer_t(static_cast<TraceerEngines>(tracer), static_cast<DebuggerEngines>(debugInfo));
49  t->tPTR = &t->tracer;
50  return t;
51 }
52 
55  tr_Tracer_t *t = new tr_Tracer_t;
56  t->tPTR = reinterpret_cast<Tracer *>(tVoid);
57  return t;
58 }
59 
62  if (!tracer)
63  return;
64 
65  tracer->tPTR->trace();
66 }
67 
73  if (!tracer)
74  return numeric_limits<size_t>::max();
75 
76  return tracer->tPTR->getFrames()->size();
77 }
78 
84  tr_Frame_t frame;
85  memset(&frame, 0, sizeof(frame));
86 
87  if (!tracer)
88  return frame;
89 
90  auto *frames = tracer->tPTR->getFrames();
91 
92  if (frameNum >= frames->size())
93  return frame;
94 
95  auto &f = frames->at(frameNum);
96  frame.frameAddr = f.frameAddr;
97  frame.funcName = f.funcName.c_str();
98  frame.moduleName = f.moduleName.c_str();
99  frame.fileName = f.fileName.c_str();
100  frame.line = f.line;
101  frame.column = f.column;
102 
103  if ((f.flags & FrameFlags::HAS_FUNC_NAME) != FrameFlags::HAS_FUNC_NAME)
104  frame.funcName = nullptr;
105 
106  if ((f.flags & FrameFlags::HAS_MODULE_INFO) != FrameFlags::HAS_MODULE_INFO)
107  frame.moduleName = nullptr;
108 
109  if ((f.flags & FrameFlags::HAS_LINE_INFO) != FrameFlags::HAS_LINE_INFO)
110  frame.fileName = nullptr;
111 
112  return frame;
113 }
114 
120  memset(&result, 0, sizeof(result));
121 
122  int c = 0;
123  for (auto i : tracer::Tracer::getAvailableEngines()) {
124  result.tracer[c++] = static_cast<TR_TraceerEngines_t>(i);
125 
126  if (c >= 8)
127  break;
128  }
129 
130  c = 0;
131  for (auto i : tracer::Tracer::getAvailableDebuggers()) {
132  result.debuggers[c++] = static_cast<TR_DebuggerEngines_t>(i);
133 
134  if (c >= 8)
135  break;
136  }
137 
138  return result;
139 }
140 
143  if (tracer)
144  delete tracer;
145 }
146 }
const char * funcName
The name of the function.
Definition: tracer.h:105
TR_DebuggerEngines_t debuggers[8]
List of available engines; First try them for the Tracer (0 marks the end)
Definition: tracer.h:115
int line
The line number in the source file.
Definition: tracer.h:108
tr_Tracer_AvailableEngines_t tr_Tracer__getAvailableEngines()
(Indirect) Wrapper for tracer::Tracer::getAvailableEngines and tracer::Tracer::getAvailableDebuggers ...
Definition: cTracer.cpp:118
const char * fileName
The name / path of the source file.
Definition: tracer.h:107
tracer::Tracer tracer
Tracer object.
STL namespace.
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
tr_Frame_t tr_Tracer__getFrame(tr_Tracer_t *tracer, size_t frameNum)
(Indirect) Wrapper for tracer::Tracer::getFrames
Definition: cTracer.cpp:83
TR_TraceerEngines_t tracer[8]
List of available engines; First try them for the Tracer (0 marks the end)
Definition: tracer.h:114
tr_Tracer_t * tr_getTracerWithParam(TR_TraceerEngines_t tracer, TR_DebuggerEngines_t debugInfo)
Wrapper for tracer::Tracer::Tracer(TraceerEngines, DebuggerEngines)
Definition: cTracer.cpp:47
tr_Tracer_t * tr_getTracer()
Wrapper for tracer::Tracer::Tracer.
Definition: cTracer.cpp:40
static std::vector< TraceerEngines > getAvailableEngines()
Returns a list of available tracer engines.
Definition: Tracer.cpp:165
const char * moduleName
The name / path of the binary file.
Definition: tracer.h:106
Represents a stack frame.
Definition: tracer.h:103
Internal C wrapper structure.
Address frameAddr
The instruction pointer of the stack frame.
Definition: tracer.h:104
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
static std::vector< DebuggerEngines > getAvailableDebuggers()
Returns a list of available debug info engines.
Definition: Tracer.cpp:184
std::vector< Frame > * getFrames()
Returns a pointer to the stack frames.
Definition: Tracer.cpp:157
size_t tr_Tracer__getNumFrames(tr_Tracer_t *tracer)
(Indirect) Wrapper for tracer::Tracer::getFrames
Definition: cTracer.cpp:72
void tr_Tracer__trace(tr_Tracer_t *tracer)
Wrapper for tracer::Tracer::trace.
Definition: cTracer.cpp:61
int column
The column in the source file.
Definition: tracer.h:109
struct tr_Tracer tr_Tracer_t
Private struct to manage Tracer.
Definition: tracer.h:120
enum TR_TraceerEngines TR_TraceerEngines_t
Wrapper for tracer::TracerEngines.
Definition: tracer.h:59
std::vector< Frame > * trace()
Generates the stack trace.
Definition: Tracer.cpp:145
tracer::Tracer * tPTR
Pointer to the tracer object to use.
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
Backtrace generator.
Definition: Tracer.hpp:48