all repos — mgba @ 5bcf243139275654fe879b05c3d931a337c0fe90

mGBA Game Boy Advance Emulator

src/debugger/cli-debugger.c (view raw)

   1/* Copyright (c) 2013-2014 Jeffrey Pfau
   2 *
   3 * This Source Code Form is subject to the terms of the Mozilla Public
   4 * License, v. 2.0. If a copy of the MPL was not distributed with this
   5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
   6#include <mgba/internal/debugger/cli-debugger.h>
   7
   8#include <mgba/internal/debugger/symbols.h>
   9
  10#include <mgba/core/core.h>
  11#include <mgba/core/version.h>
  12#include <mgba/internal/debugger/parser.h>
  13#include <mgba-util/string.h>
  14#include <mgba-util/vfs.h>
  15
  16#ifdef ENABLE_SCRIPTING
  17#include <mgba/core/scripting.h>
  18#endif
  19
  20#if !defined(NDEBUG) && !defined(_WIN32)
  21#include <signal.h>
  22#endif
  23
  24#ifdef USE_PTHREADS
  25#include <pthread.h>
  26#endif
  27
  28const char* ERROR_MISSING_ARGS = "Arguments missing"; // TODO: share
  29const char* ERROR_OVERFLOW = "Arguments overflow";
  30const char* ERROR_INVALID_ARGS = "Invalid arguments";
  31const char* INFO_BREAKPOINT_ADDED = "Added breakpoint #%" PRIz "i\n";
  32const char* INFO_WATCHPOINT_ADDED = "Added watchpoint #%" PRIz "i\n";
  33
  34static struct ParseTree* _parseTree(const char** string);
  35static bool _doTrace(struct CLIDebugger* debugger);
  36
  37#if !defined(NDEBUG) && !defined(_WIN32)
  38static void _breakInto(struct CLIDebugger*, struct CLIDebugVector*);
  39#endif
  40static void _continue(struct CLIDebugger*, struct CLIDebugVector*);
  41static void _disassemble(struct CLIDebugger*, struct CLIDebugVector*);
  42static void _next(struct CLIDebugger*, struct CLIDebugVector*);
  43static void _print(struct CLIDebugger*, struct CLIDebugVector*);
  44static void _printBin(struct CLIDebugger*, struct CLIDebugVector*);
  45static void _printHex(struct CLIDebugger*, struct CLIDebugVector*);
  46static void _printStatus(struct CLIDebugger*, struct CLIDebugVector*);
  47static void _printHelp(struct CLIDebugger*, struct CLIDebugVector*);
  48static void _quit(struct CLIDebugger*, struct CLIDebugVector*);
  49static void _readByte(struct CLIDebugger*, struct CLIDebugVector*);
  50static void _reset(struct CLIDebugger*, struct CLIDebugVector*);
  51static void _readHalfword(struct CLIDebugger*, struct CLIDebugVector*);
  52static void _readWord(struct CLIDebugger*, struct CLIDebugVector*);
  53static void _setBreakpoint(struct CLIDebugger*, struct CLIDebugVector*);
  54static void _clearBreakpoint(struct CLIDebugger*, struct CLIDebugVector*);
  55static void _listBreakpoints(struct CLIDebugger*, struct CLIDebugVector*);
  56static void _setReadWriteWatchpoint(struct CLIDebugger*, struct CLIDebugVector*);
  57static void _setReadWatchpoint(struct CLIDebugger*, struct CLIDebugVector*);
  58static void _setWriteWatchpoint(struct CLIDebugger*, struct CLIDebugVector*);
  59static void _setWriteChangedWatchpoint(struct CLIDebugger*, struct CLIDebugVector*);
  60static void _listWatchpoints(struct CLIDebugger*, struct CLIDebugVector*);
  61static void _trace(struct CLIDebugger*, struct CLIDebugVector*);
  62static void _writeByte(struct CLIDebugger*, struct CLIDebugVector*);
  63static void _writeHalfword(struct CLIDebugger*, struct CLIDebugVector*);
  64static void _writeRegister(struct CLIDebugger*, struct CLIDebugVector*);
  65static void _writeWord(struct CLIDebugger*, struct CLIDebugVector*);
  66static void _dumpByte(struct CLIDebugger*, struct CLIDebugVector*);
  67static void _dumpHalfword(struct CLIDebugger*, struct CLIDebugVector*);
  68static void _dumpWord(struct CLIDebugger*, struct CLIDebugVector*);
  69#ifdef ENABLE_SCRIPTING
  70static void _source(struct CLIDebugger*, struct CLIDebugVector*);
  71#endif
  72static void _backtrace(struct CLIDebugger*, struct CLIDebugVector*);
  73static void _finish(struct CLIDebugger*, struct CLIDebugVector*);
  74static void _setStackTraceMode(struct CLIDebugger*, struct CLIDebugVector*);
  75static void _setSymbol(struct CLIDebugger*, struct CLIDebugVector*);
  76static void _findSymbol(struct CLIDebugger*, struct CLIDebugVector*);
  77
  78static struct CLIDebuggerCommandSummary _debuggerCommands[] = {
  79	{ "backtrace", _backtrace, "i", "Print backtrace of all or specified frames" },
  80	{ "break", _setBreakpoint, "Is", "Set a breakpoint" },
  81	{ "continue", _continue, "", "Continue execution" },
  82	{ "delete", _clearBreakpoint, "I", "Delete a breakpoint or watchpoint" },
  83	{ "disassemble", _disassemble, "Ii", "Disassemble instructions" },
  84	{ "finish", _finish, "", "Execute until current stack frame returns" },
  85	{ "help", _printHelp, "S", "Print help" },
  86	{ "listb", _listBreakpoints, "", "List breakpoints" },
  87	{ "listw", _listWatchpoints, "", "List watchpoints" },
  88	{ "next", _next, "", "Execute next instruction" },
  89	{ "print", _print, "S+", "Print a value" },
  90	{ "print/t", _printBin, "S+", "Print a value as binary" },
  91	{ "print/x", _printHex, "S+", "Print a value as hexadecimal" },
  92	{ "quit", _quit, "", "Quit the emulator" },
  93	{ "reset", _reset, "", "Reset the emulation" },
  94	{ "r/1", _readByte, "I", "Read a byte from a specified offset" },
  95	{ "r/2", _readHalfword, "I", "Read a halfword from a specified offset" },
  96	{ "r/4", _readWord, "I", "Read a word from a specified offset" },
  97	{ "set", _setSymbol, "SI", "Assign a symbol to an address" },
  98	{ "stack", _setStackTraceMode, "S", "Change the stack tracing mode" },
  99	{ "status", _printStatus, "", "Print the current status" },
 100	{ "symbol", _findSymbol, "I", "Find the symbol name for an address" },
 101	{ "trace", _trace, "Is", "Trace a number of instructions" },
 102	{ "w/1", _writeByte, "II", "Write a byte at a specified offset" },
 103	{ "w/2", _writeHalfword, "II", "Write a halfword at a specified offset" },
 104	{ "w/r", _writeRegister, "SI", "Write a register" },
 105	{ "w/4", _writeWord, "II", "Write a word at a specified offset" },
 106	{ "watch", _setReadWriteWatchpoint, "Is", "Set a watchpoint" },
 107	{ "watch/c", _setWriteChangedWatchpoint, "Is", "Set a change watchpoint" },
 108	{ "watch/r", _setReadWatchpoint, "Is", "Set a read watchpoint" },
 109	{ "watch/w", _setWriteWatchpoint, "Is", "Set a write watchpoint" },
 110	{ "x/1", _dumpByte, "Ii", "Examine bytes at a specified offset" },
 111	{ "x/2", _dumpHalfword, "Ii", "Examine halfwords at a specified offset" },
 112	{ "x/4", _dumpWord, "Ii", "Examine words at a specified offset" },
 113#ifdef ENABLE_SCRIPTING
 114	{ "source", _source, "S", "Load a script" },
 115#endif
 116#if !defined(NDEBUG) && !defined(_WIN32)
 117	{ "!", _breakInto, "", "Break into attached debugger (for developers)" },
 118#endif
 119	{ 0, 0, 0, 0 }
 120};
 121
 122static struct CLIDebuggerCommandAlias _debuggerCommandAliases[] = {
 123	{ "b", "break" },
 124	{ "bt", "backtrace" },
 125	{ "c", "continue" },
 126	{ "d", "delete" },
 127	{ "dis", "disassemble" },
 128	{ "disasm", "disassemble" },
 129	{ "h", "help" },
 130	{ "i", "status" },
 131	{ "info", "status" },
 132	{ "lb", "listb" },
 133	{ "lw", "listw" },
 134	{ "n", "next" },
 135	{ "p", "print" },
 136	{ "p/t", "print/t" },
 137	{ "p/x", "print/x" },
 138	{ "q", "quit" },
 139	{ "w", "watch" },
 140	{ ".", "source" },
 141	{ 0, 0 }
 142};
 143
 144#if !defined(NDEBUG) && !defined(_WIN32)
 145static void _handleDeath(int sig) {
 146	UNUSED(sig);
 147	printf("No debugger attached!\n");
 148}
 149
 150static void _breakInto(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
 151	UNUSED(debugger);
 152	UNUSED(dv);
 153	struct sigaction sa, osa;
 154	sa.sa_handler = _handleDeath;
 155	sigemptyset(&sa.sa_mask);
 156	sigaddset(&sa.sa_mask, SIGTRAP);
 157	sa.sa_flags = SA_RESTART;
 158	sigaction(SIGTRAP, &sa, &osa);
 159#ifdef USE_PTHREADS
 160	pthread_kill(pthread_self(), SIGTRAP);
 161#else
 162	kill(getpid(), SIGTRAP);
 163#endif
 164	sigaction(SIGTRAP, &osa, 0);
 165}
 166#endif
 167
 168static bool CLIDebuggerCheckTraceMode(struct CLIDebugger* debugger, bool requireEnabled) {
 169	struct mDebuggerPlatform* platform = debugger->d.platform;
 170	if (!platform->getStackTraceMode) {
 171		debugger->backend->printf(debugger->backend, "Stack tracing is not supported by this platform.\n");
 172		return false;
 173	} else if (requireEnabled && platform->getStackTraceMode(platform) == STACK_TRACE_DISABLED) {
 174		debugger->backend->printf(debugger->backend, "Stack tracing is not enabled.\n");
 175		return false;
 176	}
 177	return true;
 178}
 179
 180static void _continue(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
 181	UNUSED(dv);
 182	debugger->d.state = debugger->traceRemaining != 0 ? DEBUGGER_CALLBACK : DEBUGGER_RUNNING;
 183}
 184
 185static void _next(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
 186	UNUSED(dv);
 187	struct mDebuggerPlatform* platform = debugger->d.platform;
 188	debugger->d.core->step(debugger->d.core);
 189	if (platform->getStackTraceMode && platform->getStackTraceMode(platform) != STACK_TRACE_DISABLED) {
 190		platform->updateStackTrace(platform);
 191	}
 192	_printStatus(debugger, 0);
 193}
 194
 195static void _disassemble(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
 196	debugger->system->disassemble(debugger->system, dv);
 197}
 198
 199static bool _parseExpression(struct mDebugger* debugger, struct CLIDebugVector* dv, int32_t* intValue, int* segmentValue) {
 200	size_t args = 0;
 201	struct CLIDebugVector* accum;
 202	for (accum = dv; accum; accum = accum->next) {
 203		++args;
 204	}
 205	const char** arglist = calloc(args + 1, sizeof(const char*));
 206	args = 0;
 207	for (accum = dv; accum; accum = accum->next) {
 208		arglist[args] = accum->charValue;
 209		++args;
 210	}
 211	arglist[args] = NULL;
 212	struct ParseTree* tree = _parseTree(arglist);
 213	free(arglist);
 214
 215	if (!tree) {
 216		return false;
 217	}
 218	if (!mDebuggerEvaluateParseTree(debugger, tree, intValue, segmentValue)) {
 219		parseFree(tree);
 220		free(tree);
 221		return false;
 222	}
 223	parseFree(tree);
 224	free(tree);
 225	return true;
 226}
 227
 228static void _print(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
 229	int32_t intValue = 0;
 230	int segmentValue = -1;
 231	if (!_parseExpression(&debugger->d, dv, &intValue, &segmentValue)) {
 232		debugger->backend->printf(debugger->backend, "Parse error\n");
 233		return;
 234	}
 235	if (segmentValue >= 0) {
 236		debugger->backend->printf(debugger->backend, " $%02X:%04X\n", segmentValue, intValue);
 237	} else {
 238		debugger->backend->printf(debugger->backend, " %u\n", intValue);
 239	}
 240}
 241
 242static void _printBin(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
 243	int32_t intValue = 0;
 244	int segmentValue = -1;
 245	if (!_parseExpression(&debugger->d, dv, &intValue, &segmentValue)) {
 246		debugger->backend->printf(debugger->backend, "Parse error\n");
 247		return;
 248	}
 249	debugger->backend->printf(debugger->backend, " 0b");
 250	int i = 32;
 251	while (i--) {
 252		debugger->backend->printf(debugger->backend, "%u", (intValue >> i) & 1);
 253	}
 254	debugger->backend->printf(debugger->backend, "\n");
 255}
 256
 257static void _printHex(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
 258	int32_t intValue = 0;
 259	int segmentValue = -1;
 260	if (!_parseExpression(&debugger->d, dv, &intValue, &segmentValue)) {
 261		debugger->backend->printf(debugger->backend, "Parse error\n");
 262		return;
 263	}
 264	debugger->backend->printf(debugger->backend, " 0x%08X\n", intValue);
 265}
 266
 267static void _printCommands(struct CLIDebugger* debugger, struct CLIDebuggerCommandSummary* commands, struct CLIDebuggerCommandAlias* aliases) {
 268	int i;
 269	for (i = 0; commands[i].name; ++i) {
 270		debugger->backend->printf(debugger->backend, "%-15s  %s\n", commands[i].name, commands[i].summary);
 271		if (aliases) {
 272			bool printedAlias = false;
 273			int j;
 274			for (j = 0; aliases[j].name; ++j) {
 275				if (strcmp(aliases[j].original, commands[i].name) == 0) {
 276					if (!printedAlias) {
 277						debugger->backend->printf(debugger->backend, "                 Aliases:");
 278						printedAlias = true;
 279					}
 280					debugger->backend->printf(debugger->backend, " %s", aliases[j].name);
 281				}
 282			}
 283			if (printedAlias) {
 284				debugger->backend->printf(debugger->backend, "\n");
 285			}
 286		}
 287	}
 288}
 289
 290static void _printCommandSummary(struct CLIDebugger* debugger, const char* name, struct CLIDebuggerCommandSummary* commands, struct CLIDebuggerCommandAlias* aliases) {
 291	int i;
 292	for (i = 0; commands[i].name; ++i) {
 293		if (strcmp(commands[i].name, name) == 0) {
 294			debugger->backend->printf(debugger->backend, " %s\n", commands[i].summary);
 295			if (aliases) {
 296				bool printedAlias = false;
 297				int j;
 298				for (j = 0; aliases[j].name; ++j) {
 299					if (strcmp(aliases[j].original, commands[i].name) == 0) {
 300						if (!printedAlias) {
 301							debugger->backend->printf(debugger->backend, " Aliases:");
 302							printedAlias = true;
 303						}
 304						debugger->backend->printf(debugger->backend, " %s", aliases[j].name);
 305					}
 306				}
 307				if (printedAlias) {
 308					debugger->backend->printf(debugger->backend, "\n");
 309				}
 310			}
 311			return;
 312		}
 313	}
 314}
 315
 316static void _printHelp(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
 317	UNUSED(dv);
 318	if (!dv) {
 319		debugger->backend->printf(debugger->backend, "Generic commands:\n");
 320		_printCommands(debugger, _debuggerCommands, _debuggerCommandAliases);
 321		if (debugger->system) {
 322			debugger->backend->printf(debugger->backend, "\n%s commands:\n", debugger->system->platformName);
 323			_printCommands(debugger, debugger->system->platformCommands, debugger->system->platformCommandAliases);
 324			debugger->backend->printf(debugger->backend, "\n%s commands:\n", debugger->system->name);
 325			_printCommands(debugger, debugger->system->commands, debugger->system->commandAliases);
 326		}
 327	} else {
 328		_printCommandSummary(debugger, dv->charValue, _debuggerCommands, _debuggerCommandAliases);
 329		if (debugger->system) {
 330			_printCommandSummary(debugger, dv->charValue, debugger->system->platformCommands, debugger->system->platformCommandAliases);
 331			_printCommandSummary(debugger, dv->charValue, debugger->system->commands, debugger->system->commandAliases);
 332		}
 333	}
 334}
 335
 336static void _quit(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
 337	UNUSED(dv);
 338	debugger->d.state = DEBUGGER_SHUTDOWN;
 339}
 340
 341static void _readByte(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
 342	if (!dv || dv->type != CLIDV_INT_TYPE) {
 343		debugger->backend->printf(debugger->backend, "%s\n", ERROR_MISSING_ARGS);
 344		return;
 345	}
 346	uint32_t address = dv->intValue;
 347	uint8_t value;
 348	if (dv->segmentValue >= 0) {
 349		value = debugger->d.core->rawRead8(debugger->d.core, address, dv->segmentValue);
 350	} else {
 351		value = debugger->d.core->busRead8(debugger->d.core, address);
 352	}
 353	debugger->backend->printf(debugger->backend, " 0x%02X\n", value);
 354}
 355
 356static void _reset(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
 357	UNUSED(dv);
 358	mStackTraceClear(&debugger->d.stackTrace);
 359	debugger->d.core->reset(debugger->d.core);
 360	_printStatus(debugger, 0);
 361}
 362
 363static void _readHalfword(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
 364	if (!dv || dv->type != CLIDV_INT_TYPE) {
 365		debugger->backend->printf(debugger->backend, "%s\n", ERROR_MISSING_ARGS);
 366		return;
 367	}
 368	uint32_t address = dv->intValue;
 369	uint16_t value;
 370	if (dv->segmentValue >= 0) {
 371		value = debugger->d.core->rawRead16(debugger->d.core, address & -1, dv->segmentValue);
 372	} else {
 373		value = debugger->d.core->busRead16(debugger->d.core, address & ~1);
 374	}
 375	debugger->backend->printf(debugger->backend, " 0x%04X\n", value);
 376}
 377
 378static void _readWord(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
 379	if (!dv || dv->type != CLIDV_INT_TYPE) {
 380		debugger->backend->printf(debugger->backend, "%s\n", ERROR_MISSING_ARGS);
 381		return;
 382	}
 383	uint32_t address = dv->intValue;
 384	uint32_t value;
 385	if (dv->segmentValue >= 0) {
 386		value = debugger->d.core->rawRead32(debugger->d.core, address & -3, dv->segmentValue);
 387	} else {
 388		value = debugger->d.core->busRead32(debugger->d.core, address & ~3);
 389	}
 390	debugger->backend->printf(debugger->backend, " 0x%08X\n", value);
 391}
 392
 393static void _writeByte(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
 394	if (!dv || !dv->next) {
 395		debugger->backend->printf(debugger->backend, "%s\n", ERROR_MISSING_ARGS);
 396		return;
 397	}
 398	if (dv->type != CLIDV_INT_TYPE || dv->next->type != CLIDV_INT_TYPE) {
 399		debugger->backend->printf(debugger->backend, "%s\n", ERROR_INVALID_ARGS);
 400		return;
 401	}
 402	uint32_t address = dv->intValue;
 403	uint32_t value = dv->next->intValue;
 404	if (value > 0xFF) {
 405		debugger->backend->printf(debugger->backend, "%s\n", ERROR_OVERFLOW);
 406		return;
 407	}
 408	if (dv->segmentValue >= 0) {
 409		debugger->d.core->rawWrite8(debugger->d.core, address, value, dv->segmentValue);
 410	} else {
 411		debugger->d.core->busWrite8(debugger->d.core, address, value);
 412	}
 413}
 414
 415static void _writeHalfword(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
 416	if (!dv || !dv->next) {
 417		debugger->backend->printf(debugger->backend, "%s\n", ERROR_MISSING_ARGS);
 418		return;
 419	}
 420	if (dv->type != CLIDV_INT_TYPE || dv->next->type != CLIDV_INT_TYPE) {
 421		debugger->backend->printf(debugger->backend, "%s\n", ERROR_INVALID_ARGS);
 422		return;
 423	}
 424	uint32_t address = dv->intValue;
 425	uint32_t value = dv->next->intValue;
 426	if (value > 0xFFFF) {
 427		debugger->backend->printf(debugger->backend, "%s\n", ERROR_OVERFLOW);
 428		return;
 429	}
 430	if (dv->segmentValue >= 0) {
 431		debugger->d.core->rawWrite16(debugger->d.core, address, value, dv->segmentValue);
 432	} else {
 433		debugger->d.core->busWrite16(debugger->d.core, address, value);
 434	}
 435}
 436
 437static void _writeRegister(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
 438	if (!dv || !dv->next) {
 439		debugger->backend->printf(debugger->backend, "%s\n", ERROR_MISSING_ARGS);
 440		return;
 441	}
 442	if (dv->type != CLIDV_CHAR_TYPE || dv->next->type != CLIDV_INT_TYPE) {
 443		debugger->backend->printf(debugger->backend, "%s\n", ERROR_INVALID_ARGS);
 444		return;
 445	}
 446	if (!debugger->d.platform->setRegister(debugger->d.platform, dv->charValue, dv->next->intValue)) {
 447		debugger->backend->printf(debugger->backend, "%s\n", ERROR_INVALID_ARGS);
 448	}
 449}
 450
 451static void _writeWord(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
 452	if (!dv || !dv->next) {
 453		debugger->backend->printf(debugger->backend, "%s\n", ERROR_MISSING_ARGS);
 454		return;
 455	}
 456	if (dv->type != CLIDV_INT_TYPE || dv->next->type != CLIDV_INT_TYPE) {
 457		debugger->backend->printf(debugger->backend, "%s\n", ERROR_INVALID_ARGS);
 458		return;
 459	}
 460	uint32_t address = dv->intValue;
 461	uint32_t value = dv->next->intValue;
 462	if (dv->segmentValue >= 0) {
 463		debugger->d.core->rawWrite32(debugger->d.core, address, value, dv->segmentValue);
 464	} else {
 465		debugger->d.core->busWrite32(debugger->d.core, address, value);
 466	}
 467}
 468
 469static void _dumpByte(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
 470	if (!dv || dv->type != CLIDV_INT_TYPE) {
 471		debugger->backend->printf(debugger->backend, "%s\n", ERROR_MISSING_ARGS);
 472		return;
 473	}
 474	uint32_t address = dv->intValue;
 475	uint32_t words = 16;
 476	if (dv->next && dv->next->type == CLIDV_INT_TYPE) {
 477		words = dv->next->intValue;
 478	}
 479	while (words) {
 480		uint32_t line = 16;
 481		if (line > words) {
 482			line = words;
 483		}
 484		debugger->backend->printf(debugger->backend, "0x%08X:", address);
 485		for (; line > 0; --line, ++address, --words) {
 486			uint32_t value;
 487			if (dv->segmentValue >= 0) {
 488				value = debugger->d.core->rawRead8(debugger->d.core, address, dv->segmentValue);
 489			} else {
 490				value = debugger->d.core->busRead8(debugger->d.core, address);
 491			}
 492			debugger->backend->printf(debugger->backend, " %02X", value);
 493		}
 494		debugger->backend->printf(debugger->backend, "\n");
 495	}
 496}
 497
 498static void _dumpHalfword(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
 499	if (!dv || dv->type != CLIDV_INT_TYPE) {
 500		debugger->backend->printf(debugger->backend, "%s\n", ERROR_MISSING_ARGS);
 501		return;
 502	}
 503	uint32_t address = dv->intValue;
 504	uint32_t words = 8;
 505	if (dv->next && dv->next->type == CLIDV_INT_TYPE) {
 506		words = dv->next->intValue;
 507	}
 508	while (words) {
 509		uint32_t line = 8;
 510		if (line > words) {
 511			line = words;
 512		}
 513		debugger->backend->printf(debugger->backend, "0x%08X:", address);
 514		for (; line > 0; --line, address += 2, --words) {
 515			uint32_t value;
 516			if (dv->segmentValue >= 0) {
 517				value = debugger->d.core->rawRead16(debugger->d.core, address, dv->segmentValue);
 518			} else {
 519				value = debugger->d.core->busRead16(debugger->d.core, address);
 520			}
 521			debugger->backend->printf(debugger->backend, " %04X", value);
 522		}
 523		debugger->backend->printf(debugger->backend, "\n");
 524	}
 525}
 526
 527static void _dumpWord(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
 528	if (!dv || dv->type != CLIDV_INT_TYPE) {
 529		debugger->backend->printf(debugger->backend, "%s\n", ERROR_MISSING_ARGS);
 530		return;
 531	}
 532	uint32_t address = dv->intValue;
 533	uint32_t words = 4;
 534	if (dv->next && dv->next->type == CLIDV_INT_TYPE) {
 535		words = dv->next->intValue;
 536	}
 537	while (words) {
 538		uint32_t line = 4;
 539		if (line > words) {
 540			line = words;
 541		}
 542		debugger->backend->printf(debugger->backend, "0x%08X:", address);
 543		for (; line > 0; --line, address += 4, --words) {
 544			uint32_t value;
 545			if (dv->segmentValue >= 0) {
 546				value = debugger->d.core->rawRead32(debugger->d.core, address, dv->segmentValue);
 547			} else {
 548				value = debugger->d.core->busRead32(debugger->d.core, address);
 549			}
 550			debugger->backend->printf(debugger->backend, " %08X", value);
 551		}
 552		debugger->backend->printf(debugger->backend, "\n");
 553	}
 554}
 555
 556#ifdef ENABLE_SCRIPTING
 557static void _source(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
 558	if (!dv) {
 559		debugger->backend->printf(debugger->backend, "Needs a filename\n");
 560		return;
 561	}
 562	if (debugger->d.bridge && mScriptBridgeLoadScript(debugger->d.bridge, dv->charValue)) {
 563		mScriptBridgeRun(debugger->d.bridge);
 564	} else {
 565		debugger->backend->printf(debugger->backend, "Failed to load script\n");
 566	}
 567}
 568#endif
 569
 570static struct ParseTree* _parseTree(const char** string) {
 571	struct LexVector lv;
 572	bool error = false;
 573	LexVectorInit(&lv, 0);
 574	size_t i;
 575	for (i = 0; string[i]; ++i) {
 576		size_t length = strlen(string[i]);
 577		size_t adjusted = lexExpression(&lv, string[i], length, NULL);
 578		if (!adjusted || adjusted > length) {
 579			error = true;
 580		}
 581	}
 582	struct ParseTree* tree = NULL;
 583	if (!error) {
 584		tree = malloc(sizeof(*tree));
 585		parseLexedExpression(tree, &lv);
 586	}
 587	lexFree(&lv);
 588	LexVectorClear(&lv);
 589	LexVectorDeinit(&lv);
 590	if (error) {
 591		if (tree) {
 592			parseFree(tree);
 593			free(tree);
 594		}
 595		return NULL;
 596	} else {
 597		return tree;
 598	}
 599}
 600
 601static void _setBreakpoint(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
 602	if (!dv || dv->type != CLIDV_INT_TYPE) {
 603		debugger->backend->printf(debugger->backend, "%s\n", ERROR_MISSING_ARGS);
 604		return;
 605	}
 606	struct mBreakpoint breakpoint = {
 607		.address = dv->intValue,
 608		.segment = dv->segmentValue,
 609		.type = BREAKPOINT_HARDWARE
 610	};
 611	if (dv->next && dv->next->type == CLIDV_CHAR_TYPE) {
 612		struct ParseTree* tree = _parseTree((const char*[]) { dv->next->charValue, NULL });
 613		if (tree) {
 614			breakpoint.condition = tree;
 615		} else {
 616			debugger->backend->printf(debugger->backend, "%s\n", ERROR_INVALID_ARGS);
 617			return;
 618		}
 619	}
 620	ssize_t id = debugger->d.platform->setBreakpoint(debugger->d.platform, &breakpoint);
 621	if (id > 0) {
 622		debugger->backend->printf(debugger->backend, INFO_BREAKPOINT_ADDED, id);
 623	}
 624}
 625
 626static void _setWatchpoint(struct CLIDebugger* debugger, struct CLIDebugVector* dv, enum mWatchpointType type) {
 627	if (!dv || dv->type != CLIDV_INT_TYPE) {
 628		debugger->backend->printf(debugger->backend, "%s\n", ERROR_MISSING_ARGS);
 629		return;
 630	}
 631	if (!debugger->d.platform->setWatchpoint) {
 632		debugger->backend->printf(debugger->backend, "Watchpoints are not supported by this platform.\n");
 633		return;
 634	}
 635	struct mWatchpoint watchpoint = {
 636		.address = dv->intValue,
 637		.segment = dv->segmentValue,
 638		.type = type
 639	};
 640	if (dv->next && dv->next->type == CLIDV_CHAR_TYPE) {
 641		struct ParseTree* tree = _parseTree((const char*[]) { dv->next->charValue, NULL });
 642		if (tree) {
 643			watchpoint.condition = tree;
 644		} else {
 645			debugger->backend->printf(debugger->backend, "%s\n", ERROR_INVALID_ARGS);
 646			return;
 647		}
 648	}
 649	ssize_t id = debugger->d.platform->setWatchpoint(debugger->d.platform, &watchpoint);
 650	if (id > 0) {
 651		debugger->backend->printf(debugger->backend, INFO_WATCHPOINT_ADDED, id);
 652	}
 653}
 654
 655static void _setReadWriteWatchpoint(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
 656	_setWatchpoint(debugger, dv, WATCHPOINT_RW);
 657}
 658
 659static void _setReadWatchpoint(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
 660	_setWatchpoint(debugger, dv, WATCHPOINT_READ);
 661}
 662
 663static void _setWriteWatchpoint(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
 664	_setWatchpoint(debugger, dv, WATCHPOINT_WRITE);
 665}
 666
 667static void _setWriteChangedWatchpoint(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
 668	_setWatchpoint(debugger, dv, WATCHPOINT_WRITE_CHANGE);
 669}
 670
 671static void _clearBreakpoint(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
 672	if (!dv || dv->type != CLIDV_INT_TYPE) {
 673		debugger->backend->printf(debugger->backend, "%s\n", ERROR_MISSING_ARGS);
 674		return;
 675	}
 676	uint64_t id = dv->intValue;
 677	debugger->d.platform->clearBreakpoint(debugger->d.platform, id);
 678}
 679
 680static void _listBreakpoints(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
 681	UNUSED(dv);
 682	struct mBreakpointList breakpoints;
 683	mBreakpointListInit(&breakpoints, 0);
 684	debugger->d.platform->listBreakpoints(debugger->d.platform, &breakpoints);
 685	size_t i;
 686	for (i = 0; i < mBreakpointListSize(&breakpoints); ++i) {
 687		struct mBreakpoint* breakpoint = mBreakpointListGetPointer(&breakpoints, i);
 688		if (breakpoint->segment >= 0) {
 689			debugger->backend->printf(debugger->backend, "%" PRIz "i: %02X:%X\n", breakpoint->id, breakpoint->segment, breakpoint->address);
 690		} else {
 691			debugger->backend->printf(debugger->backend, "%" PRIz "i: 0x%X\n", breakpoint->id, breakpoint->address);
 692		}
 693	}
 694	mBreakpointListDeinit(&breakpoints);
 695}
 696
 697static void _listWatchpoints(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
 698	UNUSED(dv);
 699	struct mWatchpointList watchpoints;
 700	mWatchpointListInit(&watchpoints, 0);
 701	debugger->d.platform->listWatchpoints(debugger->d.platform, &watchpoints);
 702	size_t i;
 703	for (i = 0; i < mWatchpointListSize(&watchpoints); ++i) {
 704		struct mWatchpoint* watchpoint = mWatchpointListGetPointer(&watchpoints, i);
 705		if (watchpoint->segment >= 0) {
 706			debugger->backend->printf(debugger->backend, "%" PRIz "i: %02X:%X\n", watchpoint->id, watchpoint->segment, watchpoint->address);
 707		} else {
 708			debugger->backend->printf(debugger->backend, "%" PRIz "i: 0x%X\n", watchpoint->id, watchpoint->address);
 709		}
 710	}
 711	mWatchpointListDeinit(&watchpoints);
 712}
 713
 714static void _trace(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
 715	if (!dv || dv->type != CLIDV_INT_TYPE) {
 716		debugger->backend->printf(debugger->backend, "%s\n", ERROR_MISSING_ARGS);
 717		return;
 718	}
 719
 720	debugger->traceRemaining = dv->intValue;
 721	if (debugger->traceVf) {
 722		debugger->traceVf->close(debugger->traceVf);
 723		debugger->traceVf = NULL;
 724	}
 725	if (debugger->traceRemaining == 0) {
 726		return;
 727	}
 728	if (dv->next && dv->next->charValue) {
 729		debugger->traceVf = VFileOpen(dv->next->charValue, O_CREAT | O_WRONLY | O_APPEND);
 730	}
 731	if (_doTrace(debugger)) {
 732		debugger->d.state = DEBUGGER_CALLBACK;
 733	} else {
 734		debugger->system->printStatus(debugger->system);
 735	}
 736}
 737
 738static bool _doTrace(struct CLIDebugger* debugger) {
 739	char trace[1024];
 740	trace[sizeof(trace) - 1] = '\0';
 741	size_t traceSize = sizeof(trace) - 2;
 742	debugger->d.platform->trace(debugger->d.platform, trace, &traceSize);
 743	if (traceSize + 1 <= sizeof(trace)) {
 744		trace[traceSize] = '\n';
 745		trace[traceSize + 1] = '\0';
 746	}
 747	if (debugger->traceVf) {
 748		debugger->traceVf->write(debugger->traceVf, trace, traceSize + 1);
 749	} else {
 750		debugger->backend->printf(debugger->backend, "%s", trace);
 751	}
 752	if (debugger->traceRemaining > 0) {
 753		--debugger->traceRemaining;
 754	}
 755	if (!debugger->traceRemaining) {
 756		debugger->traceVf->close(debugger->traceVf);
 757		debugger->traceVf = NULL;
 758		return false;
 759	}
 760	return true;
 761}
 762
 763static void _printStatus(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
 764	UNUSED(dv);
 765	debugger->system->printStatus(debugger->system);
 766}
 767
 768struct CLIDebugVector* CLIDVParse(struct CLIDebugger* debugger, const char* string, size_t length) {
 769	if (!string || length < 1) {
 770		return 0;
 771	}
 772
 773	struct CLIDebugVector dvTemp = { .type = CLIDV_INT_TYPE, .segmentValue = -1 };
 774
 775	struct LexVector lv;
 776	LexVectorInit(&lv, 0);
 777	size_t adjusted = lexExpression(&lv, string, length, " ");
 778	if (adjusted > length) {
 779		dvTemp.type = CLIDV_ERROR_TYPE;
 780	}
 781
 782	struct ParseTree tree;
 783	parseLexedExpression(&tree, &lv);
 784	if (tree.token.type == TOKEN_ERROR_TYPE) {
 785		dvTemp.type = CLIDV_ERROR_TYPE;
 786	} else {
 787		if (!mDebuggerEvaluateParseTree(&debugger->d, &tree, &dvTemp.intValue, &dvTemp.segmentValue)) {
 788			dvTemp.type = CLIDV_ERROR_TYPE;
 789		}
 790	}
 791
 792	parseFree(&tree);
 793
 794	lexFree(&lv);
 795	LexVectorDeinit(&lv);
 796
 797	struct CLIDebugVector* dv = malloc(sizeof(struct CLIDebugVector));
 798	if (dvTemp.type == CLIDV_ERROR_TYPE) {
 799		dv->type = CLIDV_ERROR_TYPE;
 800		dv->next = 0;
 801	} else {
 802		*dv = dvTemp;
 803	}
 804	return dv;
 805}
 806
 807struct CLIDebugVector* CLIDVStringParse(struct CLIDebugger* debugger, const char* string, size_t length) {
 808	UNUSED(debugger);
 809	if (!string || length < 1) {
 810		return 0;
 811	}
 812
 813	struct CLIDebugVector dvTemp = { .type = CLIDV_CHAR_TYPE };
 814
 815	dvTemp.charValue = strndup(string, length);
 816
 817	struct CLIDebugVector* dv = malloc(sizeof(struct CLIDebugVector));
 818	*dv = dvTemp;
 819	return dv;
 820}
 821
 822static void _DVFree(struct CLIDebugVector* dv) {
 823	struct CLIDebugVector* next;
 824	while (dv) {
 825		next = dv->next;
 826		if (dv->type == CLIDV_CHAR_TYPE) {
 827			free(dv->charValue);
 828		}
 829		free(dv);
 830		dv = next;
 831	}
 832}
 833
 834static struct CLIDebugVector* _parseArg(struct CLIDebugger* debugger, const char* args, size_t argsLen, char type) {
 835	struct CLIDebugVector* dv = NULL;
 836	switch (type) {
 837	case 'I':
 838	case 'i':
 839		return CLIDVParse(debugger, args, argsLen);
 840	case 'S':
 841	case 's':
 842		return CLIDVStringParse(debugger, args, argsLen);
 843	case '*':
 844		dv = _parseArg(debugger, args, argsLen, 'I');
 845		if (!dv) {
 846			dv = _parseArg(debugger, args, argsLen, 'S');
 847		}
 848		break;
 849	}
 850	return dv;
 851}
 852
 853static int _tryCommands(struct CLIDebugger* debugger, struct CLIDebuggerCommandSummary* commands, struct CLIDebuggerCommandAlias* aliases, const char* command, size_t commandLen, const char* args, size_t argsLen) {
 854	struct CLIDebugVector* dv = NULL;
 855	struct CLIDebugVector* dvLast = NULL;
 856	int i;
 857	const char* name;
 858	if (aliases) {
 859		for (i = 0; (name = aliases[i].name); ++i) {
 860			if (strlen(name) != commandLen) {
 861				continue;
 862			}
 863			if (strncasecmp(name, command, commandLen) == 0) {
 864				command = aliases[i].original;
 865				commandLen = strlen(aliases[i].original);
 866			}
 867		}
 868	}
 869	for (i = 0; (name = commands[i].name); ++i) {
 870		if (strlen(name) != commandLen) {
 871			continue;
 872		}
 873		if (strncasecmp(name, command, commandLen) == 0) {
 874			if (commands[i].format && args) {
 875				char lastArg = '\0';
 876				int arg;
 877				for (arg = 0; commands[i].format[arg] && argsLen; ++arg) {
 878					while (isspace(args[0]) && argsLen) {
 879						++args;
 880						--argsLen;
 881					}
 882					if (!args[0] || !argsLen) {
 883						debugger->backend->printf(debugger->backend, "Wrong number of arguments\n");
 884						_DVFree(dv);
 885						return 0;
 886					}
 887
 888					size_t adjusted;
 889					const char* next = strchr(args, ' ');
 890					if (next) {
 891						adjusted = next - args;
 892					} else {
 893						adjusted = argsLen;
 894					}
 895
 896					struct CLIDebugVector* dvNext = NULL;
 897					bool nextArgMandatory = false;
 898
 899					if (commands[i].format[arg] == '+') {
 900						dvNext = _parseArg(debugger, args, adjusted, lastArg);
 901						--arg;
 902					} else {
 903						nextArgMandatory = isupper(commands[i].format[arg]) || (commands[i].format[arg] == '*');
 904						dvNext = _parseArg(debugger, args, adjusted, commands[i].format[arg]);
 905						lastArg = commands[i].format[arg];
 906					}
 907
 908					args += adjusted;
 909					argsLen -= adjusted;
 910
 911					if (!dvNext) {
 912						if (!nextArgMandatory) {
 913							args = NULL;
 914						}
 915						break;
 916					}
 917					if (dvNext->type == CLIDV_ERROR_TYPE) {
 918						debugger->backend->printf(debugger->backend, "Parse error\n");
 919						_DVFree(dv);
 920						_DVFree(dvNext);
 921						return 0;
 922					}
 923
 924					if (dvLast) {
 925						dvLast->next = dvNext;
 926						dvLast = dvNext;
 927					} else {
 928						dv = dvNext;
 929						dvLast = dv;
 930					}
 931				}
 932			}
 933
 934			if (args) {
 935				while (isspace(args[0]) && argsLen) {
 936					++args;
 937					--argsLen;
 938				}
 939			}
 940			if (args && argsLen) {
 941				debugger->backend->printf(debugger->backend, "Wrong number of arguments\n");
 942				_DVFree(dv);
 943				return 0;
 944			}
 945			commands[i].command(debugger, dv);
 946			_DVFree(dv);
 947			return 1;
 948		}
 949	}
 950	return -1;
 951}
 952
 953bool CLIDebuggerRunCommand(struct CLIDebugger* debugger, const char* line, size_t count) {
 954	const char* firstSpace = strchr(line, ' ');
 955	size_t cmdLength;
 956	if (firstSpace) {
 957		cmdLength = firstSpace - line;
 958	} else {
 959		cmdLength = count;
 960	}
 961
 962	const char* args = 0;
 963	if (firstSpace) {
 964		args = firstSpace + 1;
 965	}
 966	int result = _tryCommands(debugger, _debuggerCommands, _debuggerCommandAliases, line, cmdLength, args, count - cmdLength - 1);
 967	if (result < 0 && debugger->system) {
 968		result = _tryCommands(debugger, debugger->system->commands, debugger->system->commandAliases, line, cmdLength, args, count - cmdLength - 1);
 969		if (result < 0) {
 970			result = _tryCommands(debugger, debugger->system->platformCommands, debugger->system->platformCommandAliases, line, cmdLength, args, count - cmdLength - 1);
 971		}
 972	}
 973	if (result < 0) {
 974		debugger->backend->printf(debugger->backend, "Command not found\n");
 975	}
 976	return false;
 977}
 978
 979static void _commandLine(struct mDebugger* debugger) {
 980	struct CLIDebugger* cliDebugger = (struct CLIDebugger*) debugger;
 981	const char* line;
 982		size_t len;
 983	_printStatus(cliDebugger, 0);
 984	while (debugger->state == DEBUGGER_PAUSED) {
 985		line = cliDebugger->backend->readline(cliDebugger->backend, &len);
 986		if (!line || len == 0) {
 987			debugger->state = DEBUGGER_SHUTDOWN;
 988			return;
 989		}
 990		if (line[0] == '\n') {
 991			line = cliDebugger->backend->historyLast(cliDebugger->backend, &len);
 992			if (line && len) {
 993				CLIDebuggerRunCommand(cliDebugger, line, len);
 994			}
 995		} else {
 996			CLIDebuggerRunCommand(cliDebugger, line, len);
 997			cliDebugger->backend->historyAppend(cliDebugger->backend, line);
 998		}
 999	}
1000}
1001
1002static void _reportEntry(struct mDebugger* debugger, enum mDebuggerEntryReason reason, struct mDebuggerEntryInfo* info) {
1003	struct CLIDebugger* cliDebugger = (struct CLIDebugger*) debugger;
1004	if (cliDebugger->traceRemaining > 0) {
1005		cliDebugger->traceRemaining = 0;
1006	}
1007	switch (reason) {
1008	case DEBUGGER_ENTER_MANUAL:
1009	case DEBUGGER_ENTER_ATTACHED:
1010		break;
1011	case DEBUGGER_ENTER_BREAKPOINT:
1012		if (info) {
1013			if (info->pointId > 0) {
1014				cliDebugger->backend->printf(cliDebugger->backend, "Hit breakpoint %" PRIz "i at 0x%08X\n", info->pointId, info->address);
1015			} else {
1016				cliDebugger->backend->printf(cliDebugger->backend, "Hit unknown breakpoint at 0x%08X\n", info->address);
1017			}
1018		} else {
1019			cliDebugger->backend->printf(cliDebugger->backend, "Hit breakpoint\n");
1020		}
1021		break;
1022	case DEBUGGER_ENTER_WATCHPOINT:
1023		if (info) {
1024			if (info->type.wp.accessType & WATCHPOINT_WRITE) {
1025				cliDebugger->backend->printf(cliDebugger->backend, "Hit watchpoint %" PRIz "i at 0x%08X: (new value = 0x%08X, old value = 0x%08X)\n", info->pointId, info->address, info->type.wp.newValue, info->type.wp.oldValue);
1026			} else {
1027				cliDebugger->backend->printf(cliDebugger->backend, "Hit watchpoint %" PRIz "i at 0x%08X: (value = 0x%08X)\n", info->pointId, info->address, info->type.wp.oldValue);
1028			}
1029		} else {
1030			cliDebugger->backend->printf(cliDebugger->backend, "Hit watchpoint\n");
1031		}
1032		break;
1033	case DEBUGGER_ENTER_ILLEGAL_OP:
1034		if (info) {
1035			cliDebugger->backend->printf(cliDebugger->backend, "Hit illegal opcode at 0x%08X: 0x%08X\n", info->address, info->type.bp.opcode);
1036		} else {
1037			cliDebugger->backend->printf(cliDebugger->backend, "Hit illegal opcode\n");
1038		}
1039		break;
1040	case DEBUGGER_ENTER_STACK:
1041		if (info) {
1042			if (info->type.st.traceType == STACK_TRACE_BREAK_ON_CALL) {
1043				struct mStackTrace* stack = &cliDebugger->d.stackTrace;
1044				struct mStackFrame* frame = mStackTraceGetFrame(stack, 0);
1045				if (frame->interrupt) {
1046					cliDebugger->backend->printf(cliDebugger->backend, "Hit interrupt at at 0x%08X\n", info->address);
1047				} else {
1048					cliDebugger->backend->printf(cliDebugger->backend, "Hit function call at at 0x%08X\n", info->address);
1049				}
1050			} else {
1051				cliDebugger->backend->printf(cliDebugger->backend, "Hit function return at at 0x%08X\n", info->address);
1052			}
1053		} else {
1054			cliDebugger->backend->printf(cliDebugger->backend, "Hit function call or return\n");
1055		}
1056		_backtrace(cliDebugger, NULL);
1057		break;
1058	}
1059}
1060
1061static void _cliDebuggerInit(struct mDebugger* debugger) {
1062	struct CLIDebugger* cliDebugger = (struct CLIDebugger*) debugger;
1063	cliDebugger->traceRemaining = 0;
1064	cliDebugger->traceVf = NULL;
1065	cliDebugger->backend->init(cliDebugger->backend);
1066}
1067
1068static void _cliDebuggerDeinit(struct mDebugger* debugger) {
1069	struct CLIDebugger* cliDebugger = (struct CLIDebugger*) debugger;
1070	if (cliDebugger->traceVf) {
1071		cliDebugger->traceVf->close(cliDebugger->traceVf);
1072		cliDebugger->traceVf = NULL;
1073	}
1074
1075	if (cliDebugger->system) {
1076		if (cliDebugger->system->deinit) {
1077			cliDebugger->system->deinit(cliDebugger->system);
1078		}
1079		free(cliDebugger->system);
1080		cliDebugger->system = NULL;
1081	}
1082	if (cliDebugger->backend && cliDebugger->backend->deinit) {
1083		cliDebugger->backend->deinit(cliDebugger->backend);
1084		cliDebugger->backend = NULL;
1085	}
1086}
1087
1088static void _cliDebuggerCustom(struct mDebugger* debugger) {
1089	struct CLIDebugger* cliDebugger = (struct CLIDebugger*) debugger;
1090	bool retain = true;
1091	enum mDebuggerState next = DEBUGGER_RUNNING;
1092	if (cliDebugger->traceRemaining) {
1093		retain = _doTrace(cliDebugger) && retain;
1094		next = DEBUGGER_PAUSED;
1095	}
1096	if (cliDebugger->system) {
1097		retain = cliDebugger->system->custom(cliDebugger->system) && retain;
1098	}
1099	if (!retain && debugger->state == DEBUGGER_CALLBACK) {
1100		debugger->state = next;
1101	}
1102}
1103
1104void CLIDebuggerCreate(struct CLIDebugger* debugger) {
1105	debugger->d.init = _cliDebuggerInit;
1106	debugger->d.deinit = _cliDebuggerDeinit;
1107	debugger->d.custom = _cliDebuggerCustom;
1108	debugger->d.paused = _commandLine;
1109	debugger->d.entered = _reportEntry;
1110	debugger->d.type = DEBUGGER_CLI;
1111
1112	debugger->system = NULL;
1113	debugger->backend = NULL;
1114}
1115
1116void CLIDebuggerAttachSystem(struct CLIDebugger* debugger, struct CLIDebuggerSystem* system) {
1117	if (debugger->system) {
1118		if (debugger->system->deinit) {
1119			debugger->system->deinit(debugger->system);
1120		}
1121		free(debugger->system);
1122	}
1123
1124	debugger->system = system;
1125	system->p = debugger;
1126}
1127
1128void CLIDebuggerAttachBackend(struct CLIDebugger* debugger, struct CLIDebuggerBackend* backend) {
1129	if (debugger->backend == backend) {
1130		return;
1131	}
1132	if (debugger->backend && debugger->backend->deinit) {
1133		debugger->backend->deinit(debugger->backend);
1134	}
1135
1136	debugger->backend = backend;
1137	backend->p = debugger;
1138}
1139
1140bool CLIDebuggerTabComplete(struct CLIDebugger* debugger, const char* token, bool initial, size_t tokenLen) {
1141	size_t cmd = 0;
1142	size_t len;
1143	const char* name = 0;
1144	for (len = 1; len <= tokenLen; ++len) {
1145		for (; (name = _debuggerCommands[cmd].name); ++cmd) {
1146			int cmp = strncasecmp(name, token, len);
1147			if (cmp > 0) {
1148				return false;
1149			}
1150			if (cmp == 0) {
1151				break;
1152			}
1153		}
1154	}
1155	if (!name) {
1156		return false;
1157	}
1158	if (_debuggerCommands[cmd + 1].name && strlen(_debuggerCommands[cmd + 1].name) >= len && name[len - 1] == _debuggerCommands[cmd + 1].name[len - 1]) {
1159		--len;
1160		const char* next = 0;
1161		int i;
1162		for (i = cmd + 1; _debuggerCommands[i].name; ++i) {
1163			if (strncasecmp(name, _debuggerCommands[i].name, len)) {
1164				break;
1165			}
1166			next = _debuggerCommands[i].name;
1167		}
1168		if (!next) {
1169			return false;
1170		}
1171
1172		for (; name[len]; ++len) {
1173			if (name[len] != next[len]) {
1174				break;
1175			}
1176			char out[2] = { name[len], '\0' };
1177			debugger->backend->lineAppend(debugger->backend, out);
1178		}
1179		return true;
1180	}
1181	name += len - 1;
1182	debugger->backend->lineAppend(debugger->backend, name);
1183	debugger->backend->lineAppend(debugger->backend, " ");
1184	return true;
1185}
1186
1187static void _backtrace(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
1188	if (!CLIDebuggerCheckTraceMode(debugger, true)) {
1189		return;
1190	}
1191	struct mStackTrace* stack = &debugger->d.stackTrace;
1192	ssize_t frames = mStackTraceGetDepth(stack);
1193	if (dv && dv->type == CLIDV_INT_TYPE && dv->intValue < frames) {
1194		frames = dv->intValue;
1195	}
1196	ssize_t i;
1197	struct mDebuggerSymbols* symbolTable = debugger->d.core->symbolTable;
1198	for (i = 0; i < frames; ++i) {
1199		char trace[1024];
1200		size_t traceSize = sizeof(trace) - 2;
1201		mStackTraceFormatFrame(stack, symbolTable, i, trace, &traceSize);
1202		debugger->backend->printf(debugger->backend, "%s", trace);
1203	}
1204}
1205
1206static void _finish(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
1207	UNUSED(dv);
1208	if (!CLIDebuggerCheckTraceMode(debugger, true)) {
1209		return;
1210	}
1211	struct mStackTrace* stack = &debugger->d.stackTrace;
1212	struct mStackFrame* frame = mStackTraceGetFrame(stack, 0);
1213	if (!frame) {
1214		debugger->backend->printf(debugger->backend, "No current stack frame.\n");
1215		return;
1216	}
1217	frame->breakWhenFinished = true;
1218	_continue(debugger, dv);
1219}
1220
1221static void _setStackTraceMode(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
1222	if (!CLIDebuggerCheckTraceMode(debugger, false)) {
1223		return;
1224	}
1225	if (!dv) {
1226		debugger->backend->printf(debugger->backend, "off           disable stack tracing (default)\n");
1227		debugger->backend->printf(debugger->backend, "trace-only    enable stack tracing\n");
1228		debugger->backend->printf(debugger->backend, "break-call    break on function calls\n");
1229		debugger->backend->printf(debugger->backend, "break-return  break on function returns\n");
1230		debugger->backend->printf(debugger->backend, "break-all     break on function calls and returns\n");
1231		return;
1232	}
1233	if (dv->type != CLIDV_CHAR_TYPE) {
1234		debugger->backend->printf(debugger->backend, "%s\n", ERROR_INVALID_ARGS);
1235		return;
1236	}
1237	struct mDebuggerPlatform* platform = debugger->d.platform;
1238	if (strcmp(dv->charValue, "off") == 0) {
1239		platform->setStackTraceMode(platform, STACK_TRACE_DISABLED);
1240	} else if (strcmp(dv->charValue, "trace-only") == 0) {
1241		platform->setStackTraceMode(platform, STACK_TRACE_ENABLED);
1242	} else if (strcmp(dv->charValue, "break-call") == 0) {
1243		platform->setStackTraceMode(platform, STACK_TRACE_BREAK_ON_CALL);
1244	} else if (strcmp(dv->charValue, "break-return") == 0) {
1245		platform->setStackTraceMode(platform, STACK_TRACE_BREAK_ON_RETURN);
1246	} else if (strcmp(dv->charValue, "break-all") == 0) {
1247		platform->setStackTraceMode(platform, STACK_TRACE_BREAK_ON_BOTH);
1248	} else {
1249		debugger->backend->printf(debugger->backend, "%s\n", ERROR_INVALID_ARGS);
1250	}
1251}
1252
1253static void _setSymbol(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
1254	struct mDebuggerSymbols* symbolTable = debugger->d.core->symbolTable;
1255	if (!symbolTable) {
1256		debugger->backend->printf(debugger->backend, "No symbol table available.\n");
1257		return;
1258	}
1259	if (!dv || !dv->next) {
1260		debugger->backend->printf(debugger->backend, "%s\n", ERROR_MISSING_ARGS);
1261		return;
1262	}
1263	if (dv->type != CLIDV_CHAR_TYPE || dv->next->type != CLIDV_INT_TYPE) {
1264		debugger->backend->printf(debugger->backend, "%s\n", ERROR_INVALID_ARGS);
1265		return;
1266	}
1267	mDebuggerSymbolAdd(symbolTable, dv->charValue, dv->next->intValue, dv->next->segmentValue);
1268}
1269
1270static void _findSymbol(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
1271	struct mDebuggerSymbols* symbolTable = debugger->d.core->symbolTable;
1272	if (!symbolTable) {
1273		debugger->backend->printf(debugger->backend, "No symbol table available.\n");
1274		return;
1275	}
1276	if (!dv) {
1277		debugger->backend->printf(debugger->backend, "%s\n", ERROR_MISSING_ARGS);
1278		return;
1279	}
1280	if (dv->type != CLIDV_INT_TYPE) {
1281		debugger->backend->printf(debugger->backend, "%s\n", ERROR_INVALID_ARGS);
1282		return;
1283	}
1284	const char* name = mDebuggerSymbolReverseLookup(symbolTable, dv->intValue, dv->segmentValue);
1285	if (name) {
1286		if (dv->segmentValue >= 0) {
1287			debugger->backend->printf(debugger->backend, " 0x%02X:%08X = %s\n", dv->segmentValue, dv->intValue, name);
1288		} else {
1289			debugger->backend->printf(debugger->backend, " 0x%08X = %s\n", dv->intValue, name);
1290		}
1291	} else {
1292		debugger->backend->printf(debugger->backend, "Not found.\n");
1293	}
1294}