From 8e118766b486c2508040d5861b6d5a3bba8d3c43 Mon Sep 17 00:00:00 2001 From: Dreg Date: Sun, 13 Aug 2023 20:55:11 +0200 Subject: [PATCH 1/4] add debugging instructions --- docs/debugging.md | 79 +++++++++++++++++++++++++++++++++++++++++++++++ mkdocs.yml | 1 + 2 files changed, 80 insertions(+) create mode 100644 docs/debugging.md diff --git a/docs/debugging.md b/docs/debugging.md new file mode 100644 index 000000000..cd134b9af --- /dev/null +++ b/docs/debugging.md @@ -0,0 +1,79 @@ +## Debugging + +Debugging GEF has a trick, let's see some examples + +## Debugging a command execution with pdb + +Open gef.py + +Search for **class NopCommand(GenericCommand)**, go to do_invoke method and insert: + +```python +import pdb; pdb.set_trace() +``` + +Open a gdb session -> start -> nop + +Done! +```bash +gef➤ nop +> /home/dreg/.gef-7c170cf6be3d84b2672a22e43b9128a23fe53c3b.py(6075)do_invoke() +-> args : argparse.Namespace = kwargs["arguments"] +(Pdb) ll +6070 @only_if_gdb_running +6071 @parse_arguments({"address": "$pc"}, {"--i": 1, "--b": True, "--f": True, "--n": True}) +6072 def do_invoke(self, _: List[str], **kwargs: Any) -> None: +6073 import pdb; pdb.set_trace() +6074 +6075 -> args : argparse.Namespace = kwargs["arguments"] +6076 address = parse_address(args.address) +``` + +Learn more about [pdb](https://docs.python.org/3/library/pdb.html) + +## Debugging a command execution with pycharm + +Install [pycharm](https://www.jetbrains.com/help/pycharm/installation-guide.html) + +Create a new project: + +![pycharm1](https://github.com/hugsy/gef/assets/9882181/600a9522-208a-4f2e-89b2-707136ba020a) + +![pycharm2](https://github.com/hugsy/gef/assets/9882181/4cf51b17-6aa0-463f-b538-200dd9e9b5e6) + +Go to menu -> Run -> Edit configurations...: + +![pycharm3](https://github.com/hugsy/gef/assets/9882181/6fdacda8-c4cc-44e0-8fc1-3b18cf118fbe) + +Create a Python Debug Server: + +![pycharm4](https://github.com/hugsy/gef/assets/9882181/09f99b28-5716-48be-8a0c-8ed69920c4a0) + +![pycharm5](https://github.com/hugsy/gef/assets/9882181/814fe019-c390-4ca3-8605-e3842be04df1) + +Debug your new Unnamed: + +![pycharm6](https://github.com/hugsy/gef/assets/9882181/f0f1eee9-fcaa-4919-8985-8d7d09907ebd) + +![pycharm7](https://github.com/hugsy/gef/assets/9882181/039e8749-b949-49e8-917f-b592f9cf6dac) + +Copy the info from output Window to gef.py: + +![pycharm8](https://github.com/hugsy/gef/assets/9882181/be24ee23-3101-4b71-b62f-70883c9135ad) + +First, add to gef.py: +```python +import pydevd_pycharm +``` + +Second, search for **class NopCommand(GenericCommand)**, go to do_invoke method and insert: + +```python +pydevd_pycharm.settrace('localhost', port=35747, stdoutToServer=True, stderrToServer=True) +``` + +Open a gdb session -> start -> nop + +Done! + +![pycharm9](https://github.com/hugsy/gef/assets/9882181/b22ec431-57e7-442a-835e-5817bdac7687) diff --git a/mkdocs.yml b/mkdocs.yml index 5e209372f..aa7175379 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -21,6 +21,7 @@ nav: - Testing: testing.md - Write extensions: api.md - API: api/gef.md + - Debugging: debugging.md - Coverage: https://hugsy.github.io/gef/coverage/ - Commands: - aliases: commands/aliases.md From 20e8f5030db797b8231f7e560ba59a8a3cb5c789 Mon Sep 17 00:00:00 2001 From: Dreg Date: Mon, 14 Aug 2023 07:15:35 +0200 Subject: [PATCH 2/4] Update docs/debugging.md Co-authored-by: crazy hugsy --- docs/debugging.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/debugging.md b/docs/debugging.md index cd134b9af..fc5b1de8e 100644 --- a/docs/debugging.md +++ b/docs/debugging.md @@ -62,6 +62,7 @@ Copy the info from output Window to gef.py: ![pycharm8](https://github.com/hugsy/gef/assets/9882181/be24ee23-3101-4b71-b62f-70883c9135ad) First, add to gef.py: + ```python import pydevd_pycharm ``` From 0476aff20c56b94ed275ea506227891df56a7439 Mon Sep 17 00:00:00 2001 From: Dreg Date: Mon, 14 Aug 2023 07:15:47 +0200 Subject: [PATCH 3/4] Update docs/debugging.md Co-authored-by: crazy hugsy --- docs/debugging.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/debugging.md b/docs/debugging.md index fc5b1de8e..203d9ca3b 100644 --- a/docs/debugging.md +++ b/docs/debugging.md @@ -15,6 +15,7 @@ import pdb; pdb.set_trace() Open a gdb session -> start -> nop Done! + ```bash gef➤ nop > /home/dreg/.gef-7c170cf6be3d84b2672a22e43b9128a23fe53c3b.py(6075)do_invoke() From 09597f9b899bd0e1570dd3425ced4be1a26d16ae Mon Sep 17 00:00:00 2001 From: Dreg Date: Tue, 15 Aug 2023 20:18:27 +0200 Subject: [PATCH 4/4] revert to order register logic --- gef.py | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/gef.py b/gef.py index 023881259..c8fd7314c 100644 --- a/gef.py +++ b/gef.py @@ -6440,7 +6440,7 @@ def do_invoke(self, argv: List[str]) -> None: if "all" in argv: tids = [t.num for t in threads] else: - tids = self.check_thread_ids([int(a) for a in argv]) + tids = self.check_thread_ids(argv) else: tids = [current_thread.num] @@ -6523,9 +6523,21 @@ def find_tcache() -> int: @staticmethod def check_thread_ids(tids: List[int]) -> List[int]: - """Return the subset of tids that are currently valid.""" - existing_tids = set(t.num for t in gdb.selected_inferior().threads()) - return list(set(tids) & existing_tids) + """Check the validity, dedup, and return all valid tids.""" + existing_tids = [t.num for t in gdb.selected_inferior().threads()] + valid_tids = set() + for tid in tids: + try: + tid = int(tid) + except ValueError: + err(f"Invalid thread id {tid:d}") + continue + if tid in existing_tids: + valid_tids.add(tid) + else: + err(f"Unknown thread {tid}") + + return list(valid_tids) @staticmethod def tcachebin(tcache_base: int, i: int) -> Tuple[Optional[GlibcTcacheChunk], int]: @@ -6772,11 +6784,11 @@ def do_invoke(self, _: List[str], **kwargs: Any) -> None: args : argparse.Namespace = kwargs["arguments"] if args.registers and args.registers[0]: - requested_regs = set(args.registers) - valid_regs = set(gef.arch.all_registers) & requested_regs + required_regs = set(args.registers) + valid_regs = [reg for reg in gef.arch.all_registers if reg in required_regs] if valid_regs: regs = valid_regs - invalid_regs = requested_regs - valid_regs + invalid_regs = [reg for reg in required_regs if reg not in valid_regs] if invalid_regs: err(f"invalid registers for architecture: {', '.join(invalid_regs)}") @@ -7358,7 +7370,7 @@ def context_regs(self) -> None: if self["show_registers_raw"] is False: regs = set(gef.arch.all_registers) - printable_registers = " ".join(regs - ignored_registers) + printable_registers = " ".join(list(regs - ignored_registers)) gdb.execute(f"registers {printable_registers}") return