From 58cd81ea46fa25608a0111199a7e2c9af5b509b4 Mon Sep 17 00:00:00 2001 From: xuxingliang Date: Sat, 31 Aug 2024 18:14:15 +0800 Subject: [PATCH] tools/gdb: register NuttX commands when loading the first elf file Make sure elf exists before registering our commands. If no elf at the moment, register event to GDB to get notified when user adds the first object file. Signed-off-by: xuxingliang --- tools/gdb/__init__.py | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/tools/gdb/__init__.py b/tools/gdb/__init__.py index 599c1402bad..ecc716b0f30 100644 --- a/tools/gdb/__init__.py +++ b/tools/gdb/__init__.py @@ -34,13 +34,27 @@ sys.path.insert(1, python_dir) py_files = glob.glob(f"{python_dir}/*.py") py_files.remove(os.path.abspath(__file__)) -gdb.execute("set pagination off") -gdb.write("set pagination off\n") -gdb.execute("set python print-stack full") -gdb.write("set python print-stack full\n") -for py_file in py_files: - gdb.execute(f"source {py_file}") - gdb.write(f"source {py_file}\n") -gdb.execute('handle SIGUSR1 "nostop" "pass" "noprint"') -gdb.write('"handle SIGUSR1 "nostop" "pass" "noprint"\n') +def register_commands(event): + if getattr(register_commands, "registered", False): + return + + register_commands.registered = True + + gdb.write("Registering NuttX GDB commands...\n") + gdb.execute("set pagination off") + gdb.write("set pagination off\n") + gdb.execute("set python print-stack full") + gdb.write("set python print-stack full\n") + for py_file in py_files: + gdb.execute(f"source {py_file}") + gdb.write(f"source {py_file}\n") + + gdb.execute('handle SIGUSR1 "nostop" "pass" "noprint"') + gdb.write('"handle SIGUSR1 "nostop" "pass" "noprint"\n') + + +if len(gdb.objfiles()) == 0: + gdb.events.new_objfile.connect(register_commands) +else: + register_commands(None)