-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebugger.cpp
373 lines (321 loc) · 10.2 KB
/
debugger.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/**
* vim: set ts=4 :
* =============================================================================
* SourceMod Console Debugger Extension
* Copyright (C) 2018-2021 Peace-Maker All rights reserved.
* =============================================================================
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 3.0, as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, AlliedModders LLC gives you permission to link the
* code of this program (as well as its derivative works) to "Half-Life 2," the
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
* by the Valve Corporation. You must obey the GNU General Public License in
* all respects for all other code used. Additionally, AlliedModders LLC grants
* this exception to all derivative works. AlliedModders LLC defines further
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
* or <http://www.sourcemod.net/license.php>.
*
* Version: $Id$
*/
#include "debugger.h"
#include "commands.h"
#include "breakpoints.h"
#include "symbols.h"
#include <amtl/am-string.h>
#include <ctype.h>
#include <iterator>
#include <iostream>
#include <sstream>
using namespace SourcePawn;
Debugger::Debugger(IPluginContext *context)
: context_(context),
runmode_(RUNNING),
lastfrm_(0),
lastline_(-1),
currentfile_(nullptr),
currentfunction_(nullptr),
is_breakpoint_(false),
active_(false),
breakpoints_(this),
symbols_(this),
cip_(0),
frm_(0),
frame_count_(0),
selected_frame_(0),
selected_context_(context)
{
commands_.push_back(std::make_shared<BacktraceCommand>(this));
commands_.push_back(std::make_shared<BreakpointCommand>(this));
commands_.push_back(std::make_shared<ClearBreakpointCommand>(this));
commands_.push_back(std::make_shared<ClearWatchVariableCommand>(this));
commands_.push_back(std::make_shared<ContinueCommand>(this));
commands_.push_back(std::make_shared<FilesCommand>(this));
commands_.push_back(std::make_shared<FrameCommand>(this));
commands_.push_back(std::make_shared<FunctionsCommand>(this));
commands_.push_back(std::make_shared<NextCommand>(this));
commands_.push_back(std::make_shared<PositionCommand>(this));
commands_.push_back(std::make_shared<PrintVariableCommand>(this));
commands_.push_back(std::make_shared<QuitCommand>(this));
commands_.push_back(std::make_shared<SetVariableCommand>(this));
commands_.push_back(std::make_shared<StepCommand>(this));
commands_.push_back(std::make_shared<ExamineMemoryCommand>(this));
commands_.push_back(std::make_shared<WatchVariableCommand>(this));
}
bool
Debugger::Initialize()
{
if (!breakpoints_.Initialize())
return false;
if (!symbols_.Initialize())
return false;
return true;
}
void
Debugger::Activate()
{
active_ = true;
}
void
Debugger::Deactivate()
{
active_ = false;
breakpoints_.ClearAllBreakpoints();
symbols_.ClearAllWatches();
SetRunmode(RUNNING);
}
SourcePawn::IPluginDebugInfo*
Debugger::GetDebugInfo() const
{
return selected_context_->GetRuntime()->GetDebugInfo();
}
void
Debugger::HandleInput(cell_t cip, cell_t frm, bool isBp)
{
// Remember which command was executed last,
// so you don't have to type it again if you
// want to repeat it.
static std::string lastcommand = "";
// Reset the state.
IFrameIterator *frames = context_->CreateFrameIterator();
frame_count_ = 0;
selected_frame_ = 0;
cip_ = cip;
frm_ = frm;
selected_context_ = context_;
is_breakpoint_ = isBp;
// Count the frames
// Select first scripted frame, if it's not frame 0
bool selected_first_scripted = false;
for (; !frames->Done(); frames->Next(), frame_count_++) {
if (frames->IsInternalFrame())
continue;
if (!selected_first_scripted && frames->IsScriptedFrame()) {
selected_frame_ = frame_count_;
selected_first_scripted = true;
}
}
context_->DestroyFrameIterator(frames);
// Show where we've stopped.
PrintCurrentPosition();
// Print all watched variables now.
symbols_.ListWatches();
std::string line, command, params;
for (;;) {
// Show a debugger prompt.
std::cout << "dbg> ";
// Read debugger command.
if (!std::getline(std::cin, line)) {
// Ctrl+C? Not sure what's the best behavior here.
SetRunmode(RUNNING);
std::cout << std::endl;
break;
}
line = trimString(line);
// Repeat the last command, if no new command was given.
if (line.empty()) {
line = lastcommand;
}
lastcommand.clear();
// Split the line into "<command> <params...>"
size_t pos = line.find_first_of(" ");
if (pos == std::string::npos) {
command = line;
params = "";
}
else {
// Extract the first word from the string.
command = line.substr(0, pos);
// Optional params start after the command.
params = line.substr(pos + 1);
params = trimString(params);
}
if (command.empty()) {
ListCommands("");
continue;
}
// Handle inbuilt help command.
if (!stricmp(command.c_str(), "?") || !stricmp(command.c_str(), "help")) {
ListCommands(params);
continue;
}
std::shared_ptr<DebuggerCommand> matched_cmd = ResolveCommandString(command);
if (!matched_cmd) {
continue;
}
lastcommand = line;
if (matched_cmd->Accept(command, params) == CR_LeaveCommandLoop) {
return;
}
/*
// Change display format of symbol
else if (!stricmp(command, "type")) {
HandleDisplayFormatChangeCmd(params);
}*/
}
}
std::shared_ptr<DebuggerCommand>
Debugger::ResolveCommandString(const std::string command)
{
if (command.empty()) {
return nullptr;
}
std::vector<std::shared_ptr<DebuggerCommand>> matched_cmds;
std::string match;
for (auto& cmd : commands_) {
match = cmd->GetMatch(command);
if (match.empty())
continue;
// Early exit for complete matches.
if (match.size() == command.size())
return cmd;
matched_cmds.push_back(cmd);
}
if (matched_cmds.empty()) {
std::cout << "\tInvalid command \"" << command << "\", use \"?\" to view all commands\n";
return nullptr;
}
if (matched_cmds.size() > 1) {
std::cout << "\tAmbiguous command \"" << command << "\", need more characters\n\t";
for (auto& cmd : matched_cmds) {
std::cout << "\"" << cmd->GetMatch(command) << "\", ";
}
std::cout << "\n";
return nullptr;
}
return matched_cmds[0];
}
void
Debugger::ListCommands(const std::string command)
{
if (command.empty() || command == "?" || !stricmp(command.c_str(), "help")) {
std::cout << "At the prompt, you can type debug commands. For example, the word \"step\" is a\n"
"command to execute a single line in the source code. The commands that you will\n"
"use most frequently may be abbreviated to a single letter: instead of the full\n"
"word \"step\", you can also type the letter \"s\" followed by the enter key.\n\n"
"Available commands:\n";
// Print a short description for every available command.
for (auto& cmd : commands_) {
cmd->ShortHelp();
}
std::cout << "\n\tUse \"? <command name>\" to view more information on a command\n";
return;
}
std::cout << "Options for command \"" << command << "\":\n";
std::shared_ptr<DebuggerCommand> matched_cmd = ResolveCommandString(command);
// Ambiguous or invalid command. ResolveCommandString printed an error.
if (!matched_cmd)
return;
if (!matched_cmd->LongHelp(command)) {
std::cout << "\tno additional information\n";
}
/*if (!stricmp(command, "type")) {
printf("\tTYPE var STRING\t\tdisplay \"var\" as string\n"
"\tTYPE var STD\t\tset default display format (decimal integer)\n"
"\tTYPE var HEX\t\tset hexadecimal integer format\n"
"\tTYPE var FLOAT\t\tset floating point format\n");
}
else {
printf(
"\tTYPE\t\tset the \"display type\" of a symbol\n"
"\n\tUse \"? <command name>\" to view more information on a command\n");
}*/
}
void
Debugger::PrintCurrentPosition()
{
if (!is_breakpoint_)
printf("STOP");
else
printf("BREAK");
// Print file, function, line and selected frame.
printf(" at line %d", lastline_);
if (currentfile_)
printf(" in %s", SkipPath(currentfile_));
if (currentfunction_)
printf(" in %s", currentfunction_);
if (selected_frame_ > 0)
printf("\tframe: %d", selected_frame_);
fputs("\n", stdout);
}
void
Debugger::DumpStack()
{
IFrameIterator *frames = context_->CreateFrameIterator();
uint32_t index = 0;
for (; !frames->Done(); frames->Next(), index++) {
if (frames->IsInternalFrame())
continue;
if (index == selected_frame_) {
fputs("->", stdout);
}
else {
fputs(" ", stdout);
}
const char *name = frames->FunctionName();
if (!name) {
name = "<unknown function>";
}
if (frames->IsNativeFrame()) {
fprintf(stdout, "[%d] %s\n", index, name);
continue;
}
if (frames->IsScriptedFrame()) {
const char *file = frames->FilePath();
if (!file)
file = "<unknown>";
fprintf(stdout, "[%d] Line %d, %s::%s\n", index, frames->LineNumber(), SkipPath(file), name);
}
}
context_->DestroyFrameIterator(frames);
}
const char*
Debugger::FindFileByPartialName(const std::string partialname)
{
// the user may have given a partial filename (e.g. without a path), so
// walk through all files to find a match.
IPluginDebugInfo *debuginfo = context_->GetRuntime()->GetDebugInfo();
size_t len = partialname.size();
size_t offs;
const char *filename;
for (uint32_t i = 0; i < debuginfo->NumFiles(); i++) {
filename = debuginfo->GetFileName(i);
offs = strlen(filename) - len;
if (offs >= 0 &&
!strncmp(filename + offs, partialname.c_str(), len))
{
return filename;
}
}
return nullptr;
}