1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
/*
* Copyright (C) 2006 Thomas Sondergaard All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "gltrace_support.h"
#include <assert.h>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <execinfo.h>
#include <cxxabi.h>
#include <sys/time.h>
namespace {
const char *
demangle (const char * mangled) throw()
{
static char buf[4096];
int status;
unsigned int length = sizeof(buf)-1;
memset (buf, 0, sizeof(buf));
if (!mangled)
return 0;
char * demangled = __cxxabiv1::__cxa_demangle(mangled,
buf,
&length,
&status);
if (demangled && !status)
return demangled;
else
return mangled;
}
void
printStackTrace (void **stackframes,
int stackframe_size,
std::ostream & out )
{
char **strings = 0;
std::stringstream ss;
// this might actually fail if memory is tight or we are in a
// signal handler
strings = backtrace_symbols (stackframes, stackframe_size);
ss << "Backtrace :";
if (stackframe_size == gltrace::MAX_STACKFRAMES)
ss << "(possibly incomplete maximal number of frames exceeded):" << std::endl;
else
ss << std::endl;
out << ss.str();
// the first frame is the constructor of the exception
// the last frame always seem to be bogus?
for (int i = 0; strings && i < stackframe_size-1; ++i) {
char libname[257], funcname[2049];
unsigned int address=0, funcoffset = 0x0;
memset (libname,0,sizeof(libname));
memset (funcname,0,sizeof(funcname));
strcpy (funcname,"??");
strcpy (libname, "??");
int scanned = sscanf (strings[i], "%256[^(] ( %2048[^+] + %x ) [ %x ]",
libname,
funcname,
&funcoffset,
&address);
/* ok, so no function was mentioned in the backtrace */
if (scanned < 4) {
scanned = sscanf (strings[i], "%256[^([] [ %x ]",
libname,
&address);
}
if (funcname[0] == '_') {
const char * demangled;
if ((demangled = demangle(funcname) ) != funcname) {
strncpy (funcname, demangled, sizeof(funcname)-1);
}
}
else
strcat (funcname," ()");
out << "\t#" << i << std::hex << " 0x" << address << " in " << funcname
<< " at 0x" << funcoffset << " (from " << libname << ")" << std::endl;
}
free (strings);
}
} // anon namespace
namespace gltrace {
std::string getStackTrace(int count, int first) {
++first;
std::stringstream ss;
const int BA_MAX = 1000;
assert(count + first <= BA_MAX);
void *ba[BA_MAX];
int n = backtrace(ba, count+first);
printStackTrace( &ba[first], n-first, ss);
return ss.str();
}
std::ostream &timeNow(std::ostream &os) {
struct timeval now;
struct tm t;
static char *months[12] =
{
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
gettimeofday (&now, 0);
localtime_r ((time_t*) &now.tv_sec, &t);
os
<< months[t.tm_mon] << " "
<< std::setw(2) << t.tm_mday << " "
<< std::setw(2) << t.tm_hour << ":"
<< std::setw(2) << t.tm_min << ":"
<< std::setw(2) << t.tm_sec << "."
<< std::setw(3) << now.tv_usec/1000;
return os;
}
logstream::logstream(const char *filename) {
if (!filename)
init(std::cerr.rdbuf());
else {
file_os.reset(new std::ofstream(filename));
if (file_os->good())
init(file_os->rdbuf());
else {
std::cerr << "ERROR: gltrace: Failed to open '" << filename
<< "' for writing. Falling back to stderr." << std::endl;
init(std::cerr.rdbuf());
}
}
*this << std::setfill('0'); // setw used in timeNow
}
Config::Config() :
logCalls(true),
checkErrors(true),
logTime(true),
log(getenv("GLTRACE_LOGFILE")) {
if (const char *v = getenv("GLTRACE_LOG_CALLS"))
logCalls = strncmp("1", v, 1) == 0;
if (const char *v = getenv("GLTRACE_CHECK_ERRORS"))
checkErrors = strncmp("1", v, 1) == 0;
if (const char *v = getenv("GLTRACE_LOG_TIME"))
logTime = strncmp("1", v, 1) == 0;
}
// *The* config
Config config;
} // namespace gltrace
|