attachp: add --exact for exact command name match (#2821)

* attachp: add --exact for exact command name match

Fixes #2816. This can now be done e.g. with `attach --retry --exact ls`

To test it, run `ls -lah /*` so ls is running long enough for GDB to attach to it

* Update pwndbg/commands/attachp.py

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

* Update pwndbg/commands/attachp.py

---------

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

@ -55,11 +55,17 @@ Original GDB attach command help:
parser.add_argument("--no-truncate", action="store_true", help="dont truncate command args")
parser.add_argument("--retry", action="store_true", help="retry until a target is found")
parser.add_argument("--user", type=str, default=None, help="username or uid to filter by")
parser.add_argument(
"-e",
"--exact",
action="store_true",
help="get the pid only for an exact command name match",
)
parser.add_argument(
"-a",
"--all",
action="store_true",
help="get pids for all matches (exact and partial cmdline etc)",
help="get pids also for partial cmdline matches etc",
)
parser.add_argument(
"target",
@ -70,7 +76,7 @@ parser.add_argument(
)
def find_pids(target, user, all):
def find_pids(target, user, exact, all):
# Note: we can't use `ps -C <target>` because this does not accept process names with spaces
# so target='a b' would actually match process names 'a' and 'b' here
# so instead, we will filter by process name or full cmdline later on
@ -113,14 +119,18 @@ def find_pids(target, user, all):
elif target in args:
pids_partial_match_args.append(pid)
if all:
if exact and all:
return pids_exact_match_cmd + pids_partial_match_cmd + pids_partial_match_args
return pids_exact_match_cmd or pids_partial_match_cmd or pids_partial_match_args
elif exact:
return pids_exact_match_cmd
elif all:
return pids_exact_match_cmd + pids_partial_match_cmd + pids_partial_match_args
else:
return pids_exact_match_cmd or pids_partial_match_cmd or pids_partial_match_args
@pwndbg.commands.ArgparsedCommand(parser, category=CommandCategory.START)
def attachp(target, no_truncate, retry, all, user=None) -> None:
def attachp(target, no_truncate, retry, exact, 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()
@ -153,7 +163,7 @@ def attachp(target, no_truncate, retry, all, user=None) -> None:
resolved_target = target
else:
pids = find_pids(target, user, all)
pids = find_pids(target, user, exact, all)
if not pids and retry:
user_filter = "" if not user else f" and user={user}"
print(
@ -162,7 +172,7 @@ def attachp(target, no_truncate, retry, all, user=None) -> None:
)
)
while not pids:
pids = find_pids(target, user, all)
pids = find_pids(target, user, exact, all)
if not pids:
print(message.error(f"Process {target} not found"))

Loading…
Cancel
Save