From 48c406942f25c46e29b3f097e94e210c93cc2297 Mon Sep 17 00:00:00 2001
From: pompolic <pompolic@special-circumstanc.es>
Date: Fri, 11 Jun 2021 23:08:32 +0200
Subject: [PATCH] Fix hammer-parse-step always stopping after one step

---
 gdb-port/parser-name-instrumentation-gdb.py | 24 ++++++++++++++++++---
 gdb-port/utility-commands.py                |  1 +
 2 files changed, 22 insertions(+), 3 deletions(-)

diff --git a/gdb-port/parser-name-instrumentation-gdb.py b/gdb-port/parser-name-instrumentation-gdb.py
index 9fb5961..782b4da 100644
--- a/gdb-port/parser-name-instrumentation-gdb.py
+++ b/gdb-port/parser-name-instrumentation-gdb.py
@@ -249,10 +249,13 @@ class HDoParseBreakpoint(gdb.Breakpoint):
 
 		# Check if we need to stop after a number of steps
 		step_counter = gdb.convenience_variable("hammer_step_counter")
-		# TODO: check first or decrement first? step_counter needs to be checked for NULL anyway
 		if step_counter is not None and step_counter > 0:
 			step_counter -= 1
-			retval = True
+			if step_counter == 0:
+				gdb.set_convenience_variable("hammer_step_counter", None) # unset step counter
+				retval = True
+			else:
+				gdb.set_convenience_variable("hammer_step_counter", step_counter)
 		#else:
 			# retval remains False
 
@@ -326,13 +329,28 @@ class HammerParserBacktrace(gdb.Command):
 
 	def invoke(self, arg, from_tty):
 		parserstack = top_level_parse.peek_parserstack().p_stack
-		depth = min(len(parserstack), 10) # TODO: configurable max stack depth
+		args = gdb.string_to_argv(arg)
+		# TODO: remove default 10, len(parserstack) should be default
+		if len(args) < 1:
+			maxsize = len(parserstack)
+		else:
+			try:
+				maxsize = int(args[0])
+				if maxsize < 1:
+					raise ValueError
+			except ValueError:
+				maxsize = len(parserstacK)
+				print("Argument must be a positive integer")
+
+		depth = min(len(parserstack), maxsize)
 		if depth > 0: # if stack not empty
 			# unsure what the idiomatic python is for handling negative indices starting with -1,
 			# but this addition is to avoid off-by-one errors
 			index = -(depth+1)
 			for p in parserstack[-1:index:-1]:
 				print("[" + str(hex(p.address)) + "] " + p.name)
+			if depth < len(parserstack):
+				print("[...]")
 
 HammerParserBacktrace()
 
diff --git a/gdb-port/utility-commands.py b/gdb-port/utility-commands.py
index df25c10..b94e379 100644
--- a/gdb-port/utility-commands.py
+++ b/gdb-port/utility-commands.py
@@ -28,6 +28,7 @@ class HammerParseStep(gdb.Command):
 		else:
 			steps = 1
 
+		print("Steps: " + str(int(steps)))
 		gdb.set_convenience_variable("hammer_step_counter", int(steps))
 		# Counting the stops is the responsibility of HDoParseBreakpoint
 		# On each breakpoint hit, something like this is executed in HDoParseBreakpoint.stop():
-- 
GitLab