-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.cpp
475 lines (407 loc) · 15.1 KB
/
extension.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/**
* 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 <memory>
#include <string>
#include "extension.h"
#include "debugger.h"
#include "console-helpers.h"
#include <amtl/am-platform.h>
#include <amtl/os/am-shared-library.h>
#ifdef KE_POSIX
#include <ctype.h>
#endif
using namespace ke;
/**
* @file extension.cpp
* @brief Implement extension code here.
*/
ConsoleDebugger g_Debugger; /**< Global singleton for extension's main interface */
SMEXT_LINK(&g_Debugger);
void OnDebugBreak(IPluginContext *ctx, sp_debug_break_info_t& dbginfo, const SourcePawn::IErrorReport *report);
#if defined PLATFORM_X86
# define SOURCEPAWN_DLL "sourcepawn.jit.x86"
#else
# define SOURCEPAWN_DLL "sourcepawn.vm"
#endif
bool
ConsoleDebugger::SDK_OnLoad(char *error, size_t maxlength, bool late)
{
if (smutils->GetScriptingEngine()->GetEngineAPIVersion() < 5)
{
ke::SafeStrcpy(error, maxlength, "This SourcePawn VM doesn't support line debugging. Please update SourceMod.");
return false;
}
if (!debugger_map_.init())
{
ke::SafeStrcpy(error, maxlength, "Failed to setup plugin -> debugger instance map.");
return false;
}
if (!late)
{
// Try to enable line debugging support in the VM until https://github.com/alliedmodders/sourcemod/pull/2240 is merged.
// This is a hack to get the SourcePawn VM factory function.
// https://github.com/Garey27/sm-debugger/blob/3c01bf6cede2bb7ddeffb24d0f35d87c4538e039/src/extension.cpp#L127
char file[PLATFORM_MAX_PATH];
smutils->BuildPath(Path_SM, file, sizeof(file), "bin/" PLATFORM_ARCH_FOLDER SOURCEPAWN_DLL ".%s", PLATFORM_LIB_EXT);
auto module = ke::SharedLib::Open(file);
if (module != nullptr)
{
GetSourcePawnFactoryFn factoryFn = module->get<decltype(factoryFn)>("GetSourcePawnFactory");
if (factoryFn)
{
const int LOWEST_SOURCEPAWN_API_VERSION = 0x0207;
ISourcePawnFactory *factory = factoryFn(LOWEST_SOURCEPAWN_API_VERSION);
if (factory)
{
ISourcePawnEnvironment *current_env = factory->CurrentEnvironment();
if (current_env)
{
current_env->EnableDebugBreak();
}
}
}
}
}
if (smutils->GetScriptingEngine()->SetDebugBreakHandler(OnDebugBreak) != SP_ERROR_NONE)
{
ke::SafeStrcpy(error, maxlength, "Failed to install debugger in the SourcePawn VM. Enable line debugging support in SourceMod's core.cfg. The extension can't be late loaded after any plugins were already loaded.");
return false;
}
plsys->AddPluginsListener(this);
rootconsole->AddRootConsoleCommand3("debug", "Debug Plugins", this);
return true;
}
void
ConsoleDebugger::SDK_OnUnload()
{
plsys->RemovePluginsListener(this);
rootconsole->RemoveRootConsoleCommand("debug", this);
IPluginIterator *pliter = plsys->GetPluginIterator();
while (pliter->MorePlugins())
{
IPlugin *pl = pliter->GetPlugin();
if (pl->GetStatus() == PluginStatus::Plugin_Running)
OnPluginUnloaded(pl);
pliter->NextPlugin();
}
delete pliter;
}
void
ConsoleDebugger::OnPluginLoaded(IPlugin *plugin)
{
// Create a debugger object for this plugin.
Debugger *debugger = new Debugger(plugin->GetBaseContext());
if (!debugger->Initialize()) {
smutils->LogError(myself, "Failed to initialize debugger instance for plugin %s.", plugin->GetFilename());
delete debugger;
return;
}
DebuggerMap::Insert i = debugger_map_.findForAdd(plugin->GetBaseContext());
debugger_map_.add(i, plugin->GetBaseContext(), debugger);
// Start debugging this plugin right away.
if (debug_next_plugin_) {
debug_next_plugin_ = false;
debugger->Activate();
debugger->SetRunmode(STEPPING);
}
}
void
ConsoleDebugger::OnPluginUnloaded(IPlugin *plugin)
{
// Cleanup after the plugin.
DebuggerMap::Result r = debugger_map_.find(plugin->GetBaseContext());
if (!r.found())
return;
delete r->value;
debugger_map_.remove(r);
}
void
ConsoleDebugger::OnRootConsoleCommand(const char *cmdname, const ICommandArgs *args)
{
int argcount = args->ArgC();
if (argcount < 3) {
// Draw the main menu
rootconsole->ConsolePrint("SourceMod Debug Menu:");
rootconsole->DrawGenericOption("start", "Start debugging a plugin");
rootconsole->DrawGenericOption("next", "Start debugging the plugin which is loaded next");
rootconsole->DrawGenericOption("bp", "Handle breakpoints in a plugin");
return;
}
const char *cmd = args->Arg(2);
if (!strcmp(cmd, "start")) {
if (argcount < 4) {
rootconsole->ConsolePrint("[SM] Usage: sm debug start <#|file>");
return;
}
const char *plugin = args->Arg(3);
IPlugin *pl = FindPluginByConsoleArg(plugin);
if (!pl) {
rootconsole->ConsolePrint("[SM] Plugin %s is not loaded.", plugin);
return;
}
char name[PLATFORM_MAX_PATH];
const sm_plugininfo_t *info = pl->GetPublicInfo();
if (pl->GetStatus() <= Plugin_Paused)
SafeStrcpy(name, sizeof(name), (info->name[0] != '\0') ? info->name : pl->GetFilename());
else
SafeStrcpy(name, sizeof(name), pl->GetFilename());
// Break on the next instruction.
if (StartPluginDebugging(pl->GetBaseContext()))
rootconsole->ConsolePrint("[SM] Pausing plugin %s for debugging. Will halt on next instruction.", name);
else
rootconsole->ConsolePrint("[SM] Failed to pause plugin %s for debugging.", name);
}
else if (!strcmp(cmd, "next")) {
debug_next_plugin_ = true;
rootconsole->ConsolePrint("[SM] Will halt on the first instruction of the next loaded plugin.");
}
else if (!strcmp(cmd, "bp")) {
if (argcount < 5) {
// Draw the sub menu
rootconsole->ConsolePrint("[SM] Usage: sm debug bp <#|file> <option>");
rootconsole->DrawGenericOption("list", "List breakpoints");
rootconsole->DrawGenericOption("add", "Add a breakpoint");
rootconsole->DrawGenericOption("remove", "Remove a breakpoint");
return;
}
const char *plugin = args->Arg(3);
IPlugin *pl = FindPluginByConsoleArg(plugin);
if (!pl) {
rootconsole->ConsolePrint("[SM] Plugin %s is not loaded.", plugin);
return;
}
// Get a nice printable name.
char name[PLATFORM_MAX_PATH];
const sm_plugininfo_t *info = pl->GetPublicInfo();
if (pl->GetStatus() <= Plugin_Paused)
SafeStrcpy(name, sizeof(name), (info->name[0] != '\0') ? info->name : pl->GetFilename());
else
SafeStrcpy(name, sizeof(name), pl->GetFilename());
// Get the debugger instance.
Debugger *debugger = GetPluginDebugger(pl->GetBaseContext());
if (!debugger || !debugger->active()) {
rootconsole->ConsolePrint("[SM] Debugger is not active on plugin %s.", name);
return;
}
BreakpointManager& breakpoints = debugger->breakpoints();
// Check the sub command.
const char *arg = args->Arg(4);
if (!strcmp(arg, "list")) {
rootconsole->ConsolePrint("[SM] Listing %zu breakpoint(s) for plugin %s:", breakpoints.GetBreakpointCount(), name);
breakpoints.ListBreakpoints();
}
else if (!strcmp(arg, "add")) {
if (argcount < 6) {
rootconsole->ConsolePrint("[SM] Usage: sm debug bp <#|file> add <file:line | file:function>");
return;
}
std::string bpline = args->Arg(5);
// check if a filename precedes the breakpoint location
std::string filename;
bpline = breakpoints.ParseBreakpointLine(bpline, &filename);
// User didn't specify a filename.
// Use the main source file by default (last one in the list).
if (filename.empty()) {
IPluginDebugInfo *debuginfo = pl->GetRuntime()->GetDebugInfo();
if (debuginfo->NumFiles() <= 0)
return;
filename = debuginfo->GetFileName(debuginfo->NumFiles() - 1);
}
Breakpoint *bp = nullptr;
// User specified a line number
if (isdigit(bpline[0]))
bp = breakpoints.AddBreakpoint(filename, strtol(bpline.c_str(), NULL, 10) - 1, false);
// User specified a function name
else
bp = breakpoints.AddBreakpoint(filename, bpline, false);
if (!bp)
rootconsole->ConsolePrint("[SM] Invalid breakpoint address specification.");
else
rootconsole->ConsolePrint("[SM] Added breakpoint in file %s on line %d", bp->filename(), bp->line());
}
// Remove a breakpoint for a plugin.
else if (!strcmp(arg, "remove")) {
if (argcount < 6) {
rootconsole->ConsolePrint("[SM] Usage: sm debug bp <#|file> remove <#>");
return;
}
const char *bpstr = args->Arg(5);
int bpnum = strtoul(bpstr, NULL, 10);
if (breakpoints.ClearBreakpoint(bpnum))
rootconsole->ConsolePrint("[SM] Breakpoint removed.");
else
rootconsole->ConsolePrint("[SM] Failed to remove breakpoint.");
}
}
}
IPlugin *
ConsoleDebugger::FindPluginByConsoleArg(const char *arg)
{
char *end;
int id = strtol(arg, &end, 10);
std::unique_ptr<IPluginIterator> iter(plsys->GetPluginIterator());
if (*end == '\0')
{
// Get plugin by order in the list.
if (id < 1 || id >(int)plsys->GetPluginCount())
return nullptr;
for (int i = 1; iter->MorePlugins() && i < id; iter->NextPlugin(), i++) {
// Empty loop.
}
return iter->GetPlugin();
}
else
{
// Check for a plugin with that name.
char pluginfile[256];
const char *ext = libsys->GetFileExtension(arg) ? "" : ".smx";
libsys->PathFormat(pluginfile, sizeof(pluginfile), "%s%s", arg, ext);
for (; iter->MorePlugins(); iter->NextPlugin()) {
IPlugin *pl = iter->GetPlugin();
if (!strcmp(pl->GetFilename(), pluginfile))
return pl;
}
return nullptr;
}
}
bool
ConsoleDebugger::StartPluginDebugging(IPluginContext *ctx)
{
Debugger *debugger = GetPluginDebugger(ctx);
if (!debugger)
return false;
if (!ctx->IsDebugging())
return false;
if (ctx->GetRuntime()->IsPaused())
return false;
debugger->Activate();
debugger->SetRunmode(STEPPING);
return true;
}
Debugger *
ConsoleDebugger::GetPluginDebugger(IPluginContext *ctx)
{
DebuggerMap::Result r = debugger_map_.find(ctx);
if (!r.found())
return nullptr;
return r->value;
}
void
OnDebugBreak(IPluginContext *ctx, sp_debug_break_info_t& dbginfo, const SourcePawn::IErrorReport *report)
{
// Make sure we know how to interpret the data.
if (dbginfo.version > DEBUG_BREAK_INFO_VERSION) {
smutils->LogError(myself, "VM is too new. Debug context version is %x (only support up to %x).", dbginfo.version, DEBUG_BREAK_INFO_VERSION);
smutils->GetScriptingEngine()->SetDebugBreakHandler(nullptr);
return;
}
// Try to get the debugger instance for this plugin.
Debugger *debugger = g_Debugger.GetPluginDebugger(ctx);
if (!debugger)
return;
// Continue normal execution, if this plugin isn't being debugged.
if (!debugger->active())
return;
IPluginDebugInfo *debuginfo = ctx->GetRuntime()->GetDebugInfo();
bool isBreakpoint = false;
// Was there an exception instead of a dbreak instruction?
if (report) {
if (report->IsFatal())
printf("STOP on FATAL exception: %s\n", report->Message());
else
printf("STOP on exception: %s\n", report->Message());
}
else {
// When running until the function returns,
// check the current frame address against
// the saved one from the function.
// If the current one is higher than the saved one,
// we're in a caller, so start stepping again!
if (debugger->runmode() == Runmode::STEPOUT &&
dbginfo.frm > debugger->lastframe())
{
debugger->SetRunmode(Runmode::STEPPING);
}
// See if there is a breakpoint set at the current cip.
// We don't need to check for breakpoints,
// if we're halting on each line anyways.
if (debugger->runmode() != Runmode::STEPPING &&
debugger->runmode() != Runmode::STEPOVER)
{
// Check breakpoint address
isBreakpoint = debugger->breakpoints().CheckBreakpoint(dbginfo.cip);
// Continue execution normally.
if (!isBreakpoint)
return;
// There is a breakpoint! Start stepping through the plugin.
debugger->SetRunmode(Runmode::STEPPING);
}
// If we want to skip calls, check whether
// we are stepping through a sub-function.
// The lastframe is set after changing to a STEPOVER or STEPOUT runmode.
if (debugger->runmode() == Runmode::STEPOVER) {
assert(debugger->lastframe() != 0);
// If we're in a lower frame, just execute the code.
if (dbginfo.frm < debugger->lastframe())
return;
}
}
// Remember on which line we halt.
uint32_t line = 0;
debuginfo->LookupLine(dbginfo.cip, &line);
debugger->SetCurrentLine(line);
// Remember which file we're in.
const char *filename = nullptr;
debuginfo->LookupFile(dbginfo.cip, &filename);
debugger->SetCurrentFile(filename);
// Remember which function we're in.
const char *function = nullptr;
debuginfo->LookupFunction(dbginfo.cip, &function);
debugger->SetCurrentFunction(function);
// Echo input back and enable basic control.
// This helps to have a shell-like typing experience.
// Features depend on the operating system.
unsigned int old_flags = EnableTerminalEcho();
// Disable the game's watchdog timer while we're in the debug shell.
unsigned int oldtimeout = DisableEngineWatchdog();
// Start the debugger shell and wait for commands.
debugger->HandleInput(dbginfo.cip, dbginfo.frm, isBreakpoint);
// Enable the watchdog timer again if it was enabled before.
ResetEngineWatchdog(oldtimeout);
// Reset the console input mode back to the normal flags.
ResetTerminalEcho(old_flags);
// step OVER functions (so save the stack frame)
if (debugger->runmode() == Runmode::STEPOVER ||
debugger->runmode() == Runmode::STEPOUT)
{
debugger->SetLastFrame(dbginfo.frm);
}
}