-
-
Notifications
You must be signed in to change notification settings - Fork 337
/
DebugReportGenerator.cpp
306 lines (244 loc) · 6.67 KB
/
DebugReportGenerator.cpp
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
* Copyright 2012, Rene Gollent, rene@gollent.com.
* Distributed under the terms of the MIT License.
*/
#include "DebugReportGenerator.h"
#include <sys/utsname.h>
#include <cpu_type.h>
#include <AutoLocker.h>
#include <Entry.h>
#include <File.h>
#include <Path.h>
#include <StringForSize.h>
#include "Architecture.h"
#include "CpuState.h"
#include "Image.h"
#include "MessageCodes.h"
#include "Register.h"
#include "StackFrame.h"
#include "StackTrace.h"
#include "StringUtils.h"
#include "Team.h"
#include "Thread.h"
#include "UiUtils.h"
DebugReportGenerator::DebugReportGenerator(::Team* team)
:
BLooper("DebugReportGenerator"),
fTeam(team),
fArchitecture(team->GetArchitecture()),
fTeamDataSem(-1)
{
fTeam->AddListener(this);
fArchitecture->AcquireReference();
}
DebugReportGenerator::~DebugReportGenerator()
{
fTeam->RemoveListener(this);
fArchitecture->ReleaseReference();
}
status_t
DebugReportGenerator::Init()
{
fTeamDataSem = create_sem(0, "debug_controller_data_wait");
if (fTeamDataSem < B_OK)
return fTeamDataSem;
Run();
return B_OK;
}
DebugReportGenerator*
DebugReportGenerator::Create(::Team* team)
{
DebugReportGenerator* self = new DebugReportGenerator(team);
try {
self->Init();
} catch (...) {
delete self;
throw;
}
return self;
}
status_t
DebugReportGenerator::_GenerateReport(const entry_ref& outputPath)
{
BFile file(&outputPath, B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE);
status_t result = file.InitCheck();
if (result != B_OK)
return result;
BString output;
result = _GenerateReportHeader(output);
if (result != B_OK)
return result;
result = _DumpLoadedImages(output);
if (result != B_OK)
return result;
result = _DumpRunningThreads(output);
if (result != B_OK)
return result;
result = file.Write(output.String(), output.Length());
if (result < 0)
return result;
BPath path(&outputPath);
fTeam->NotifyDebugReportChanged(path.Path());
return B_OK;
}
void
DebugReportGenerator::MessageReceived(BMessage* message)
{
switch (message->what) {
case MSG_GENERATE_DEBUG_REPORT:
{
entry_ref ref;
if (message->FindRef("target", &ref) == B_OK)
_GenerateReport(ref);
break;
}
default:
BLooper::MessageReceived(message);
break;
}
}
void
DebugReportGenerator::ThreadStackTraceChanged(const ::Team::ThreadEvent& event)
{
release_sem(fTeamDataSem);
}
status_t
DebugReportGenerator::_GenerateReportHeader(BString& _output)
{
AutoLocker< ::Team> locker(fTeam);
BString data;
data.SetToFormat("Debug information for team %s (%" B_PRId32 "):\n",
fTeam->Name(), fTeam->ID());
_output << data;
// TODO: this information should probably be requested via the debugger
// interface, since e.g. in the case of a remote team, the report should
// include data about the target, not the debugging host
system_info info;
if (get_system_info(&info) == B_OK) {
data.SetToFormat("CPU(s): %" B_PRId32 "x %s %s\n",
info.cpu_count, get_cpu_vendor_string(info.cpu_type),
get_cpu_model_string(&info));
_output << data;
char maxSize[32];
char usedSize[32];
data.SetToFormat("Memory: %s total, %s used\n",
BPrivate::string_for_size((int64)info.max_pages * B_PAGE_SIZE,
maxSize, sizeof(maxSize)),
BPrivate::string_for_size((int64)info.used_pages * B_PAGE_SIZE,
usedSize, sizeof(usedSize)));
_output << data;
}
utsname name;
uname(&name);
data.SetToFormat("Haiku revision: %s (%s)\n", name.version, name.machine);
_output << data;
return B_OK;
}
status_t
DebugReportGenerator::_DumpLoadedImages(BString& _output)
{
AutoLocker< ::Team> locker(fTeam);
_output << "\nLoaded Images:\n";
BString data;
for (ImageList::ConstIterator it = fTeam->Images().GetIterator();
Image* image = it.Next();) {
const ImageInfo& info = image->Info();
char buffer[32];
try {
target_addr_t textBase = info.TextBase();
target_addr_t dataBase = info.DataBase();
data.SetToFormat("\t%s (%" B_PRId32 ", %s) "
"Text: %#08" B_PRIx64 " - %#08" B_PRIx64 ", Data: %#08"
B_PRIx64 " - %#08" B_PRIx64 "\n", info.Name().String(),
info.ImageID(), UiUtils::ImageTypeToString(info.Type(),
buffer, sizeof(buffer)), textBase,
textBase + info.TextSize(), dataBase,
dataBase + info.DataSize());
_output << data;
} catch (...) {
return B_NO_MEMORY;
}
}
return B_OK;
}
status_t
DebugReportGenerator::_DumpRunningThreads(BString& _output)
{
AutoLocker< ::Team> locker(fTeam);
_output << "\nActive Threads:\n";
BString data;
status_t result = B_OK;
for (ThreadList::ConstIterator it = fTeam->Threads().GetIterator();
::Thread* thread = it.Next();) {
try {
data.SetToFormat("\t%s %s, id: %" B_PRId32", state: %s",
thread->Name(), thread->IsMainThread()
? "(main)" : "", thread->ID(),
UiUtils::ThreadStateToString(thread->State(),
thread->StoppedReason()));
if (thread->State() == THREAD_STATE_STOPPED) {
const BString& stoppedInfo = thread->StoppedReasonInfo();
if (stoppedInfo.Length() != 0)
data << " (" << stoppedInfo << ")";
}
_output << data << "\n";
if (thread->State() == THREAD_STATE_STOPPED) {
// we need to release our lock on the team here
// since we might need to block and wait
// on the stack trace.
BReference< ::Thread> threadRef(thread);
locker.Unlock();
result = _DumpDebuggedThreadInfo(_output, thread);
locker.Lock();
}
if (result != B_OK)
return result;
} catch (...) {
return B_NO_MEMORY;
}
}
return B_OK;
}
status_t
DebugReportGenerator::_DumpDebuggedThreadInfo(BString& _output,
::Thread* thread)
{
AutoLocker< ::Team> locker;
if (thread->State() != THREAD_STATE_STOPPED)
return B_OK;
StackTrace* trace = NULL;
for (;;) {
trace = thread->GetStackTrace();
if (trace != NULL)
break;
locker.Unlock();
status_t result = acquire_sem(fTeamDataSem);
if (result != B_OK)
return result;
locker.Lock();
}
_output << "\t\tFrame\t\tIP\t\t\tFunction Name\n";
_output << "\t\t-----------------------------------------------\n";
BString data;
for (int32 i = 0; StackFrame* frame = trace->FrameAt(i); i++) {
char functionName[512];
data.SetToFormat("\t\t%#08" B_PRIx64 "\t%#08" B_PRIx64 "\t%s\n",
frame->FrameAddress(), frame->InstructionPointer(),
UiUtils::FunctionNameForFrame(frame, functionName,
sizeof(functionName)));
_output << data;
}
_output << "\n\t\tRegisters:\n";
CpuState* state = thread->GetCpuState();
BVariant value;
const Register* reg = NULL;
for (int32 i = 0; i < fArchitecture->CountRegisters(); i++) {
reg = fArchitecture->Registers() + i;
state->GetRegisterValue(reg, value);
char buffer[64];
data.SetToFormat("\t\t\t%5s:\t%s\n", reg->Name(),
UiUtils::VariantToString(value, buffer, sizeof(buffer)));
_output << data;
}
return B_OK;
}