--- hatari/src/debug/debugcpu.c 2019/04/09 08:48:37 1.1 +++ hatari/src/debug/debugcpu.c 2019/04/09 08:53:03 1.1.1.5 @@ -1,8 +1,8 @@ /* Hatari - debugcpu.c - This file is distributed under the GNU Public License, version 2 or at - your option any later version. Read the file gpl.txt for details. + This file is distributed under the GNU General Public License, version 2 + or at your option any later version. Read the file gpl.txt for details. debugcpu.c - function needed for the CPU debugging tasks like memory and register dumps. @@ -21,12 +21,18 @@ const char DebugCpu_fileid[] = "Hatari d #include "debugcpu.h" #include "evaluate.h" #include "hatari-glue.h" +#include "history.h" #include "log.h" #include "m68000.h" #include "memorySnapShot.h" +#include "profile.h" #include "stMemory.h" #include "str.h" #include "symbols.h" +#include "68kDisass.h" +#include "console.h" +#include "options.h" + #define MEMDUMP_COLS 16 /* memdump, number of bytes per row */ #define NON_PRINT_CHAR '.' /* character to display for non-printables */ @@ -34,6 +40,7 @@ const char DebugCpu_fileid[] = "Hatari d static Uint32 disasm_addr; /* disasm address */ static Uint32 memdump_addr; /* memdump address */ +static bool bCpuProfiling; /* Whether CPU profiling is activated */ static int nCpuActiveCBs = 0; /* Amount of active conditional breakpoints */ static int nCpuSteps = 0; /* Amount of steps for CPU single-stepping */ @@ -130,10 +137,10 @@ static int DebugCpu_SaveBin(int nArgc, c /** - * Check whether given address matches any CPU symbol, if yes, - * show the symbol information. + * Check whether given address matches any CPU symbol and whether + * there's profiling information available for it. If yes, show it. */ -static void DebugCpu_ShowMatchedSymbol(Uint32 addr) +static void DebugCpu_ShowAddressInfo(Uint32 addr) { const char *symbol = Symbols_GetByCpuAddress(addr); if (symbol) @@ -151,7 +158,7 @@ int DebugCpu_DisAsm(int nArgc, char *psA if (nArgc > 1) { - switch (Eval_Range(psArgs[1], &disasm_addr, &disasm_upper)) + switch (Eval_Range(psArgs[1], &disasm_addr, &disasm_upper, false)) { case -1: /* invalid value(s) */ @@ -187,8 +194,8 @@ int DebugCpu_DisAsm(int nArgc, char *psA /* output a range */ for (insts = 0; insts < max_insts && disasm_addr < disasm_upper; insts++) { - DebugCpu_ShowMatchedSymbol(disasm_addr); - m68k_disasm(debugOutput, (uaecptr)disasm_addr, &nextpc, 1); + DebugCpu_ShowAddressInfo(disasm_addr); + Disasm(debugOutput, (uaecptr)disasm_addr, &nextpc, 1); disasm_addr = nextpc; } fflush(debugOutput); @@ -229,7 +236,7 @@ static char *DebugCpu_MatchRegister(cons /** - * Set address of the named register to given argument. + * Set address of the named 32-bit register to given argument. * Return register size in bits or zero for uknown register name. * Handles D0-7 data and A0-7 address registers, but not PC & SR * registers as they need to be accessed using UAE accessors. @@ -340,7 +347,7 @@ error_msg: /** - * CPU wrapper for BreakAddr_Command/BreakPointCount. + * CPU wrapper for BreakAddr_Command(). */ static int DebugCpu_BreakAddr(int nArgc, char *psArgs[]) { @@ -349,7 +356,7 @@ static int DebugCpu_BreakAddr(int nArgc, } /** - * CPU wrapper for BreakCond_Command/BreakPointCount. + * CPU wrapper for BreakCond_Command(). */ static int DebugCpu_BreakCond(int nArgc, char *psArgs[]) { @@ -357,6 +364,14 @@ static int DebugCpu_BreakCond(int nArgc, return DEBUGGER_CMDDONE; } +/** + * CPU wrapper for Profile_Command(). + */ +static int DebugCpu_Profile(int nArgc, char *psArgs[]) +{ + return Profile_Command(nArgc, psArgs, false); +} + /** * Do a memory dump, args = starting address. @@ -369,7 +384,7 @@ int DebugCpu_MemDump(int nArgc, char *ps if (nArgc > 1) { - switch (Eval_Range(psArgs[1], &memdump_addr, &memdump_upper)) + switch (Eval_Range(psArgs[1], &memdump_addr, &memdump_upper, false)) { case -1: /* invalid value(s) */ @@ -479,26 +494,70 @@ static int DebugCpu_Continue(int nArgc, return DEBUGGER_END; } +/** + * Command: Single-step CPU + */ +static int DebugCpu_Step(int nArgc, char *psArgv[]) +{ + nCpuSteps = 1; + return DEBUGGER_END; +} + +/** + * Command: Step CPU, but proceed through subroutines + * Does this by temporary conditional breakpoint + */ +static int DebugCpu_Next(int nArgc, char *psArgv[]) +{ + char command[32]; + Uint32 nextpc = Disasm_GetNextPC(M68000_GetPC()); + sprintf(command, "pc=$%x :once :quiet\n", nextpc); + if (BreakCond_Command(command, false)) { + nCpuSteps = 0; /* using breakpoint, not steps */ + return DEBUGGER_END; + } + return DEBUGGER_CMDDONE; +} + /** * This function is called after each CPU instruction when debugging is enabled. */ void DebugCpu_Check(void) { - if (LOG_TRACE_LEVEL(TRACE_CPU_DISASM)) + if (bCpuProfiling) { - DebugCpu_ShowMatchedSymbol(M68000_GetPC()); + Profile_CpuUpdate(); + } + if (LOG_TRACE_LEVEL((TRACE_CPU_DISASM|TRACE_CPU_SYMBOLS))) + { + DebugCpu_ShowAddressInfo(M68000_GetPC()); } if (nCpuActiveCBs) { if (BreakCond_MatchCpu()) - DebugUI(); + { + DebugUI(REASON_CPU_BREAKPOINT); + /* make sure we don't decrease step count + * below, before even even getting out of here + */ + if (nCpuSteps) + nCpuSteps++; + } } if (nCpuSteps) { - nCpuSteps -= 1; + nCpuSteps--; if (nCpuSteps == 0) - DebugUI(); + DebugUI(REASON_CPU_STEPS); + } + if (History_TrackCpu()) + { + History_AddCpu(); + } + if (ConOutDevice != CONOUT_DEVICE_NONE) + { + Console_Check(); } } @@ -509,8 +568,12 @@ void DebugCpu_Check(void) */ void DebugCpu_SetDebugging(void) { + bCpuProfiling = Profile_CpuStart(); nCpuActiveCBs = BreakCond_BreakPointCount(false); - if (nCpuActiveCBs || nCpuSteps) + + if (nCpuActiveCBs || nCpuSteps || bCpuProfiling || History_TrackCpu() + || LOG_TRACE_LEVEL((TRACE_CPU_DISASM|TRACE_CPU_SYMBOLS)) + || ConOutDevice != CONOUT_DEVICE_NONE) M68000_SetSpecial(SPCFLAG_DEBUGGER); else M68000_UnsetSpecial(SPCFLAG_DEBUGGER); @@ -538,6 +601,11 @@ static const dbgcommand_t cpucommands[] "\tIf no address is given, this command disassembles from the last\n" "\tposition or from current PC if no last position is available.", false }, + { DebugCpu_Profile, Profile_Match, + "profile", "", + "profile CPU code", + Profile_Description, + false }, { DebugCpu_Register, DebugCpu_MatchRegister, "cpureg", "r", "dump register values or set register to value", @@ -556,7 +624,7 @@ static const dbgcommand_t cpucommands[] "write bytes to memory", "address byte1 [byte2 ...]\n" "\tWrite bytes to a memory address, bytes are space separated\n" - "\thexadecimals.", + "\tvalues in current number base.", false }, { DebugCpu_LoadBin, NULL, "loadbin", "l", @@ -565,7 +633,7 @@ static const dbgcommand_t cpucommands[] "\tLoad the file into memory starting at
.", false }, { DebugCpu_SaveBin, NULL, - "savebin", "s", + "savebin", "", "save memory to a file", "filename address length\n" "\tSave the memory block at
with given to\n" @@ -576,6 +644,19 @@ static const dbgcommand_t cpucommands[] "load CPU symbols & their addresses", Symbols_Description, false }, + { DebugCpu_Step, NULL, + "step", "s", + "single-step CPU", + "\n" + "\tExecute next CPU instruction (equals 'c 1')", + false }, + { DebugCpu_Next, NULL, + "next", "n", + "step CPU, proceeding through subroutine calls", + "\n" + "\tLike the 'step' command as long as subroutine calls do not\n" + "\thappen. When they do, the call is treated as one instruction.", + false }, { DebugCpu_Continue, NULL, "cont", "c", "continue emulation / CPU single-stepping", @@ -612,4 +693,5 @@ int DebugCpu_Init(const dbgcommand_t **t void DebugCpu_InitSession(void) { disasm_addr = M68000_GetPC(); + Profile_CpuStop(); }