Repository: pwndbg/pwndbg
Pull request: attachp: add default target if not provided
Fixes #2815. The `attachp` command with no arguments will now attempt to attach to a process with the same name as the name of the binary loaded into the debugger.

<!-- Please make sure to read the testing and linting instructions at https://github.com/pwndbg/pwndbg/blob/dev/DEVELOPING.md before creating a PR -->


Problem statement:
attachp without a target could just look for the loaded binary file
When we do `gdb ./somebinary`, the GDB knows that we have the `somebinary` loaded.

```
pwndbg> attachp
usage: attachp [-h] [--no-truncate] [--retry] [--user USER] [-a] target
attachp: error: the following arguments are required: target
```

Then we could have `attachp` without a `target` argument assume that the target we want to attach is the binary name we loaded.

That's just a minor convenience/enhancement we could add here :).

Hints from issue discussion:

Commit message: attachp: add default target if not provided

Fixes #2815. The `attachp` command with no arguments will now attempt to attach to a process with the same name as the
name of the binary loaded into the debugger.

Patch to review:
diff --git a/pwndbg/commands/attachp.py b/pwndbg/commands/attachp.py
index a6b3b301e17..0f4ece6aca2 100644
--- a/pwndbg/commands/attachp.py
+++ b/pwndbg/commands/attachp.py
@@ -63,8 +63,10 @@
 )
 parser.add_argument(
     "target",
+    nargs="?",
+    default=None,
     type=str,
-    help="pid, process name, part of cmdline to be matched or device file to attach to",
+    help="pid, process name, part of cmdline to be matched or device file to attach to (uses current loaded file name if not provided)",
 )
 
 
@@ -119,6 +121,20 @@ def find_pids(target, user, all):
 
 @pwndbg.commands.ArgparsedCommand(parser, category=CommandCategory.START)
 def attachp(target, no_truncate, retry, all, user=None) -> None:
+    # As a default, the user may want to attach to a binary name taken from currently loaded file name
+    if target is None:
+        bin_path = pwndbg.dbg.selected_inferior().main_module_name()
+        if bin_path is None:
+            print(
+                message.error(
+                    "No target name/pid/cmdline provided and no binary loaded in the debugger"
+                )
+            )
+            print(message.error("(could not find the process name to attach to)"))
+            return
+
+        target = bin_path.rsplit("/")[-1]
+
     try:
         resolved_target = int(target)
     except ValueError:


Code review comments:
diff_hunk: @@ -119,6 +121,20 @@ def find_pids(target, user, all):
 
 @pwndbg.commands.ArgparsedCommand(parser, category=CommandCategory.START)
 def attachp(target, no_truncate, retry, all, user=None) -> None:
+    # As a default, the user may want to attach to a binary name taken from currently loaded file name
+    if target is None:
+        bin_path = pwndbg.dbg.selected_inferior().main_module_name()
+        if bin_path is None:
+            print(
+                message.error(
+                    "No target name/pid/cmdline provided and no binary loaded in the debugger"
+                )
+            )
+            print(message.error("(could not find the process name to attach to)"))
+            return
+
+        target = bin_path.rsplit("/")[-1]
+
path: pwndbg/commands/attachp.py
text: @user1:
[nitpick] Using bin_path.rsplit('/')[-1] may not be cross-platform safe. Consider using os.path.basename(bin_path) instead for improved portability.
```suggestion
        target = os.path.basename(bin_path)
```