[PATCH 3/4] PC offset config option for CPU & DSP disasm default address

[ Thread Index | Date Index | More lists.tuxfamily.org/hatari-devel Archives ]


Because trying to disassemble instructions from "random" address will
often output incorrect disassembly, this scans execution history for
an address in suitable range before program counter.

New config option specifies the size of this address range as offset
from PC (in bytes), so that disassemby start address does not veer off
too much from PC.

I.e. to use this new feature, user needs both to enable history (for
CPU, DSP or both) and set suitable max PC-offset config value.
---
 doc/debugger.html            | 29 +++++++++++++-------
 src/configuration.c          | 14 +++++-----
 src/debug/debugcpu.c         |  2 +-
 src/debug/debugdsp.c         |  2 +-
 src/debug/history.c          | 51 ++++++++++++++++++++++++++++++++++++
 src/debug/history.h          |  3 ++-
 src/includes/configuration.h |  7 ++---
 7 files changed, 86 insertions(+), 22 deletions(-)

diff --git a/doc/debugger.html b/doc/debugger.html
index 74b4c5b5..33598480 100644
--- a/doc/debugger.html
+++ b/doc/debugger.html
@@ -112,10 +112,11 @@ These are their defaults:</p>
 <pre>
 [Debugger]
 nNumberBase = 10
-nSymbolLines = -1
-nMemdumpLines = -1
-nDisasmLines = -1
 nBacktraceLines = 0
+nDisasmLines = -1
+nHistoryDisasmOffset = 0
+nMemdumpLines = -1
+nSymbolLines = -1
 nExceptionDebugMask = 515
 nDisasmOptions = 15
 bDisasmUAE = FALSE
@@ -128,14 +129,22 @@ bMatchAllSymbols = FALSE
 <dt>nNumberBase</dt>
 <dd>Debugger number base.
     Set with the debugger "setopt [bin|dec|hex]" command</dd>
-<dt>nSymbolLines</dt>
-<dd>Number of lines to show when listing debug symbols</dd>
-<dt>nMemdumpLines</dt>
-<dd>Number of memory dump lines to show</dd>
-<dt>nDisasmLines</dt>
-<dd>Number of disassembly lines to show</dd>
 <dt>nBacktraceLines</dt>
-<dd>Number of items to show in stack/bactraces</dd>
+<dd>Number of items to show in stack/bactraces (0 = all)</dd>
+<dt>nDisasmLines</dt>
+<dd>Number of disassembly lines to show (-1 = use console size)</dd>
+<dt>nHistoryDisasmOffset</dt>
+<dd>Whenever debugger is (re-)invoked, disassembly command output
+    defaults to current PC address.  When history offset value is
+    given, an address from execution history is used instead, if
+    history has an address that is within given offset from PC
+    (execution history is required to have valid instruction address
+    for disassembly). If history is disabled, offset value is ignored</dd>
+<dt>nMemdumpLines</dt>
+<dd>Number of memory dump lines to show (-1 = use console size)</dd>
+<dt>nSymbolLines</dt>
+<dd>Number of lines to show when listing debug symbols
+    (-1 = use console size)</dd>
 <dt>nExceptionDebugMask</dt>
 <dd>Mask of exceptions which invoke debugger when exceptions catching
     is enabled (-D).  Set with the "--debug-except" option</dd>
diff --git a/src/configuration.c b/src/configuration.c
index 15daca88..30193994 100644
--- a/src/configuration.c
+++ b/src/configuration.c
@@ -59,10 +59,11 @@ static const struct Config_Tag configs_Log[] =
 static const struct Config_Tag configs_Debugger[] =
 {
 	{ "nNumberBase", Int_Tag, &ConfigureParams.Debugger.nNumberBase },
-	{ "nSymbolLines", Int_Tag, &ConfigureParams.Debugger.nSymbolLines },
-	{ "nMemdumpLines", Int_Tag, &ConfigureParams.Debugger.nMemdumpLines },
-	{ "nDisasmLines", Int_Tag, &ConfigureParams.Debugger.nDisasmLines },
 	{ "nBacktraceLines", Int_Tag, &ConfigureParams.Debugger.nBacktraceLines },
+	{ "nDisasmLines", Int_Tag, &ConfigureParams.Debugger.nDisasmLines },
+	{ "nHistoryDisasmOffset", Int_Tag, &ConfigureParams.Debugger.nHistoryDisasmOffset },
+	{ "nMemdumpLines", Int_Tag, &ConfigureParams.Debugger.nMemdumpLines },
+	{ "nSymbolLines", Int_Tag, &ConfigureParams.Debugger.nSymbolLines },
 	{ "nExceptionDebugMask", Int_Tag, &ConfigureParams.Debugger.nExceptionDebugMask },
 	{ "nDisasmOptions", Int_Tag, &ConfigureParams.Debugger.nDisasmOptions },
 	{ "bDisasmUAE", Bool_Tag, &ConfigureParams.Debugger.bDisasmUAE },
@@ -524,10 +525,11 @@ void Configuration_SetDefault(void)
 
 	/* Set defaults for debugger */
 	ConfigureParams.Debugger.nNumberBase = 10;
-	ConfigureParams.Debugger.nSymbolLines = -1; /* <0: use terminal size */
-	ConfigureParams.Debugger.nMemdumpLines = -1; /* <0: use terminal size */
-	ConfigureParams.Debugger.nDisasmLines = -1; /* <0: use terminal size */
 	ConfigureParams.Debugger.nBacktraceLines = 0; /* <=0: show all */
+	ConfigureParams.Debugger.nDisasmLines = -1; /* <0: use terminal size */
+	ConfigureParams.Debugger.nHistoryDisasmOffset = 0; /* <=0: no offset */
+	ConfigureParams.Debugger.nMemdumpLines = -1; /* <0: use terminal size */
+	ConfigureParams.Debugger.nSymbolLines = -1; /* <0: use terminal size */
 	ConfigureParams.Debugger.nExceptionDebugMask = DEFAULT_EXCEPTIONS;
 	/* external one has nicer output, but isn't as complete as UAE one */
 	ConfigureParams.Debugger.bDisasmUAE = true;
diff --git a/src/debug/debugcpu.c b/src/debug/debugcpu.c
index eeb35529..e6e95774 100644
--- a/src/debug/debugcpu.c
+++ b/src/debug/debugcpu.c
@@ -1073,6 +1073,6 @@ int DebugCpu_Init(const dbgcommand_t **table)
  */
 void DebugCpu_InitSession(void)
 {
-	disasm_addr = M68000_GetPC();
+	disasm_addr = History_DisasmAddr(M68000_GetPC(), false);
 	Profile_CpuStop();
 }
diff --git a/src/debug/debugdsp.c b/src/debug/debugdsp.c
index 18cc1e6e..ab4ffe0d 100644
--- a/src/debug/debugdsp.c
+++ b/src/debug/debugdsp.c
@@ -669,6 +669,6 @@ int DebugDsp_Init(const dbgcommand_t **table)
  */
 void DebugDsp_InitSession(void)
 {
-	dsp_disasm_addr = DSP_GetPC();
+	dsp_disasm_addr = (Uint16)History_DisasmAddr(DSP_GetPC(), true);
 	Profile_DspStop();
 }
diff --git a/src/debug/history.c b/src/debug/history.c
index bc170dd8..23791a64 100644
--- a/src/debug/history.c
+++ b/src/debug/history.c
@@ -13,6 +13,7 @@ const char History_fileid[] = "Hatari history.c";
 #include <assert.h>
 #include <errno.h>
 #include "main.h"
+#include "configuration.h"
 #include "debugui.h"
 #include "debug_priv.h"
 #include "dsp.h"
@@ -157,6 +158,56 @@ void History_Mark(debug_reason_t reason)
 	}
 }
 
+/**
+ * Find lowest address in history that is within range:
+ *   (pc-offset) - pc
+ * where 'offset' is the history disasm offset limit.
+ *
+ * If history has no such address, return given pc value.
+ */
+Uint32 History_DisasmAddr(Uint32 pc, bool for_dsp)
+{
+	unsigned int i, count;
+	Uint32 limit, first;
+	int offset, track;
+
+	offset = ConfigureParams.Debugger.nHistoryDisasmOffset;
+	if (!offset) {
+		return pc;
+	}
+	track = for_dsp ? HISTORY_TRACK_DSP : HISTORY_TRACK_CPU;
+	if (!(track & HistoryTracking)) {
+		return pc;
+	}
+	count = History.count;
+	if (count > History.limit) {
+		count = History.limit;
+	}
+	if (count <= 0) {
+		return pc;
+	}
+	first = pc;
+	limit = pc - abs(offset);
+	i = History.idx;
+	while (count-- > 0) {
+		i++;
+		i %= History.limit;
+		assert(History.item[i].valid);
+		if (History.item[i].for_dsp != for_dsp) {
+			continue;
+		}
+		if (for_dsp) {
+			pc = History.item[i].pc.dsp;
+		} else {
+			pc = History.item[i].pc.cpu;
+		}
+		if (pc >= limit && pc < first) {
+			first = pc;
+		}
+	}
+	return first;
+}
+
 /**
  * Output collected CPU/DSP debugger/breakpoint history
  */
diff --git a/src/debug/history.h b/src/debug/history.h
index e3da1477..bcff1f0a 100644
--- a/src/debug/history.h
+++ b/src/debug/history.h
@@ -13,7 +13,7 @@ typedef enum {
 	HISTORY_TRACK_NONE = 0,
 	HISTORY_TRACK_CPU = 1,
 	HISTORY_TRACK_DSP = 2,
-	HISTORY_TRACK_ALL = 3
+	HISTORY_TRACK_ALL = (HISTORY_TRACK_CPU|HISTORY_TRACK_DSP)
 } history_type_t;
 
 extern history_type_t HistoryTracking;
@@ -30,6 +30,7 @@ static inline bool History_TrackDsp(void)
 /* for debugcpu/dsp.c */
 extern void History_AddCpu(void);
 extern void History_AddDsp(void);
+extern Uint32 History_DisasmAddr(Uint32 pc, bool for_dsp);
 
 /* for debugInfo.c */
 extern void History_Show(FILE *fp, Uint32 count);
diff --git a/src/includes/configuration.h b/src/includes/configuration.h
index a400e2c8..df101699 100644
--- a/src/includes/configuration.h
+++ b/src/includes/configuration.h
@@ -28,10 +28,11 @@ typedef struct
 typedef struct
 {
   int nNumberBase;
-  int nSymbolLines;
-  int nMemdumpLines;
-  int nDisasmLines;
   int nBacktraceLines;
+  int nDisasmLines;
+  int nHistoryDisasmOffset;
+  int nMemdumpLines;
+  int nSymbolLines;
   int nExceptionDebugMask;
   int nDisasmOptions;
   bool bDisasmUAE;
-- 
2.20.1


--------------4023024045B6B32B00F42206
Content-Type: text/x-patch; charset=UTF-8;
 name="0004-Add-PConSymbol-debugger-variable.patch"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
 filename="0004-Add-PConSymbol-debugger-variable.patch"



Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/