From 26bb0aa3400f5e98c94153dac821c44b8fb1fa49 Mon Sep 17 00:00:00 2001
From: pompolic <pompolic@special-circumstanc.es>
Date: Wed, 9 Nov 2022 21:30:25 +0100
Subject: [PATCH] (WIP) Profile hammer-parse-apply command

---
 gdb-port/commands.py        | 30 +++++++++++++++++++++---------
 gdb-port/top-level-parse.py |  2 ++
 2 files changed, 23 insertions(+), 9 deletions(-)

diff --git a/gdb-port/commands.py b/gdb-port/commands.py
index 84ab837..d65a2e3 100644
--- a/gdb-port/commands.py
+++ b/gdb-port/commands.py
@@ -1,3 +1,5 @@
+import cProfile, pstats, io # DEBUG
+
 print(": Registering basic commands")
 
 class ExtendedParseStepInfo(gdb.Parameter):
@@ -220,14 +222,24 @@ class HammerParseApply(FlowControlWithPrint):
 		print(":: hammer-parse-apply")
 
 	def invoke(self, arg, from_tty):
-		top_level_parse.setup_ast_stack_index(0) #TODO: is it a problem if this command overwrites it? would it better to use a convenience variable, as with parse-step?
-
-		# HDoParseRetBreakpoint sets hammer_step_counter to 1 after capturing the AST subtree, so this will stop at the first h_do_parse invocation afterwards
-		if gdb.selected_inferior().pid > 0:
-			gdb.execute("continue")
-
-		top_level_parse.clear_ast_stack_index()
-
-		self.conditionally_print_backtrace()
+		#profiler = cProfile.Profile() # DEBUG
+		#profiler.enable() # DEBUG
+		with cProfile.Profile() as profiler: # DEBUG
+			top_level_parse.setup_ast_stack_index(0) #TODO: is it a problem if this command overwrites it? would it better to use a convenience variable, as with parse-step?
+
+			# HDoParseRetBreakpoint sets hammer_step_counter to 1 after capturing the AST subtree, so this will stop at the first h_do_parse invocation afterwards
+			if gdb.selected_inferior().pid > 0:
+				gdb.execute("continue")
+
+			top_level_parse.clear_ast_stack_index()
+
+			self.conditionally_print_backtrace()
+		profiler.print_stats() # DEBUG
+		#profiler.disable() # DEBUG
+		#s = io.StringIO() # DEBUG
+		#sortby = SortKey.CUMULATIVE # DEBUG
+		#ps = pstats.Stats(pr, stream=s).sort_stats(sortby) # DEBUG
+		#ps.print_stats() # DEBUG
+		#print(s.getvalue()) # DEBUG
 
 HammerParseApply()
diff --git a/gdb-port/top-level-parse.py b/gdb-port/top-level-parse.py
index 96db1d3..1134504 100644
--- a/gdb-port/top-level-parse.py
+++ b/gdb-port/top-level-parse.py
@@ -1,3 +1,5 @@
+#import cProfile #DEBUG
+
 class TopLevelParse:
 	def __init__(self):
 		self.parser_stacks = []
-- 
GitLab