From be75e359a242935d8890333130be118d8a3d2043 Mon Sep 17 00:00:00 2001 From: grdddj Date: Thu, 4 May 2023 14:28:57 +0200 Subject: [PATCH] feat(core): add support for gdb scripts to emulator --- core/.gitignore | 1 + core/emu.py | 15 ++++++++++++--- core/tools/gdb_scripts/layout_size.gdb | 18 ++++++++++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 core/tools/gdb_scripts/layout_size.gdb diff --git a/core/.gitignore b/core/.gitignore index 9a9ab8549..25c2107f2 100644 --- a/core/.gitignore +++ b/core/.gitignore @@ -10,3 +10,4 @@ htmlcov/ mypy_report /CMakeLists.txt /cmake-build-debug/ +tools/gdb_scripts/*.log diff --git a/core/emu.py b/core/emu.py index e070d948c..dd183fe9c 100755 --- a/core/emu.py +++ b/core/emu.py @@ -67,7 +67,7 @@ def watch_emulator(emulator: CoreEmulator) -> int: return 0 -def run_debugger(emulator: CoreEmulator) -> None: +def run_debugger(emulator: CoreEmulator, gdb_script_file: str | Path | None) -> None: os.chdir(emulator.workdir) env = emulator.make_env() if platform.system() == "Darwin": @@ -78,8 +78,15 @@ def run_debugger(emulator: CoreEmulator) -> None: env, ) else: + # Optionally run a gdb script from a file + if gdb_script_file is None: + gdb = ["gdb"] + else: + gdb = ["gdb", "-x", str(HERE / gdb_script_file)] os.execvpe( - "gdb", ["gdb", "--args", str(emulator.executable)] + emulator.make_args(), env + "gdb", + gdb + ["--args", str(emulator.executable)] + emulator.make_args(), + env, ) @@ -110,6 +117,7 @@ def _from_env(name: str) -> bool: @click.option("-q", "--quiet", is_flag=True, help="Silence emulator output") @click.option("-r", "--record-dir", help="Directory where to record screen changes") @click.option("-s", "--slip0014", is_flag=True, help="Initialize device with SLIP-14 seed (all all all...)") +@click.option("-S", "--script-gdb-file", type=click.Path(exists=True, dir_okay=False), help="Run gdb with an init file") @click.option("-t", "--temporary-profile", is_flag=True, help="Create an empty temporary profile") @click.option("-w", "--watch", is_flag=True, help="Restart emulator if sources change") @click.option("-X", "--extra-arg", "extra_args", multiple=True, help="Extra argument to pass to micropython") @@ -135,6 +143,7 @@ def cli( quiet: bool, record_dir: Optional[str], slip0014: bool, + script_gdb_file: str | Path | None, temporary_profile: bool, watch: bool, extra_args: list[str], @@ -253,7 +262,7 @@ def cli( os.environ["TREZOR_MEMPERF"] = "1" if debugger: - run_debugger(emulator) + run_debugger(emulator, script_gdb_file) raise RuntimeError("run_debugger should not return") emulator.start() diff --git a/core/tools/gdb_scripts/layout_size.gdb b/core/tools/gdb_scripts/layout_size.gdb new file mode 100644 index 000000000..339ebcf99 --- /dev/null +++ b/core/tools/gdb_scripts/layout_size.gdb @@ -0,0 +1,18 @@ +set pagination off +set print array on +set print pretty on +# pwd is core/src +set logging file ../tools/gdb_scripts/layout_size.log +set logging on + +break src/ui/layout/obj.rs:122 +commands 1 + print "Size of current root layout" + print sizeof(root) + print root + + # not stopping the debugger + continue +end + +run