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
192
193
194
195
196
197
198
199
200
201
|
/**************************************************************************
*
* Copyright 2007-2011 VMware, Inc.
* 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 THE
* AUTHORS OR COPYRIGHT HOLDERS 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 <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "os.hpp"
#include "os_thread.hpp"
#include "os_string.hpp"
#include "trace_file.hpp"
#include "trace_writer_local.hpp"
#include "trace_format.hpp"
namespace trace {
static const char *memcpy_args[3] = {"dest", "src", "n"};
const FunctionSig memcpy_sig = {0, "memcpy", 3, memcpy_args};
static const char *malloc_args[1] = {"size"};
const FunctionSig malloc_sig = {1, "malloc", 1, malloc_args};
static const char *free_args[1] = {"ptr"};
const FunctionSig free_sig = {2, "free", 1, free_args};
static const char *realloc_args[2] = {"ptr", "size"};
const FunctionSig realloc_sig = {3, "realloc", 2, realloc_args};
static void exceptionCallback(void)
{
localWriter.flush();
}
LocalWriter::LocalWriter() :
acquired(0)
{
// Install the signal handlers as early as possible, to prevent
// interfering with the application's signal handling.
os::setExceptionCallback(exceptionCallback);
}
LocalWriter::~LocalWriter()
{
os::resetExceptionCallback();
}
void
LocalWriter::open(void) {
os::String szFileName;
const char *lpFileName;
lpFileName = getenv("TRACE_FILE");
if (!lpFileName) {
static unsigned dwCounter = 0;
os::String process = os::getProcessName();
#ifdef _WIN32
process.trimExtension();
#endif
process.trimDirectory();
#ifdef ANDROID
os::String prefix = "/data";
#else
os::String prefix = os::getCurrentDir();
#endif
prefix.join(process);
for (;;) {
FILE *file;
if (dwCounter)
szFileName = os::String::format("%s.%u.trace", prefix.str(), dwCounter);
else
szFileName = os::String::format("%s.trace", prefix.str());
lpFileName = szFileName;
file = fopen(lpFileName, "rb");
if (file == NULL)
break;
fclose(file);
++dwCounter;
}
}
os::log("apitrace: tracing to %s\n", lpFileName);
if (!Writer::open(lpFileName)) {
os::log("apitrace: error: failed to open %s\n", lpFileName);
os::abort();
}
#if 0
// For debugging the exception handler
*((int *)0) = 0;
#endif
}
static uintptr_t next_thread_num = 1;
static OS_THREAD_SPECIFIC_PTR(void)
thread_num;
unsigned LocalWriter::beginEnter(const FunctionSig *sig) {
mutex.lock();
++acquired;
if (!m_file->isOpened()) {
open();
}
// Although thread_num is a void *, we actually use it as a uintptr_t
uintptr_t this_thread_num =
reinterpret_cast<uintptr_t>(static_cast<void *>(thread_num));
if (!this_thread_num) {
this_thread_num = next_thread_num++;
thread_num = reinterpret_cast<void *>(this_thread_num);
}
assert(this_thread_num);
unsigned thread_id = this_thread_num - 1;
return Writer::beginEnter(sig, thread_id);
}
void LocalWriter::endEnter(void) {
Writer::endEnter();
--acquired;
mutex.unlock();
}
void LocalWriter::beginLeave(unsigned call) {
mutex.lock();
++acquired;
Writer::beginLeave(call);
}
void LocalWriter::endLeave(void) {
Writer::endLeave();
--acquired;
mutex.unlock();
}
void LocalWriter::flush(void) {
/*
* Do nothing if the mutex is already acquired (e.g., if a segfault happen
* while writing the file) as state could be inconsistent, therefore yield
* inconsistent trace files and/or repeated segfaults till infinity.
*/
mutex.lock();
if (acquired) {
os::log("apitrace: ignoring exception while tracing\n");
} else {
++acquired;
if (m_file->isOpened()) {
os::log("apitrace: flushing trace due to an exception\n");
m_file->flush();
}
--acquired;
}
mutex.unlock();
}
LocalWriter localWriter;
} /* namespace trace */
|