attachp: add default target if not provided (#2819)

* 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.

* Update pwndbg/commands/attachp.py

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
pull/2821/head
Disconnect3d 8 months ago committed by GitHub
parent ff3f1b82d4
commit a170362e9b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -63,8 +63,10 @@ parser.add_argument(
)
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,19 @@ 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 = os.path.basename(bin_path)
try:
resolved_target = int(target)
except ValueError:

Loading…
Cancel
Save