Skip to content

Commit

Permalink
Added argparsing support and unit test for processlisting (#659)
Browse files Browse the repository at this point in the history
  • Loading branch information
hugsy committed Jun 12, 2021
1 parent d6fd038 commit bbe84db
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
23 changes: 10 additions & 13 deletions gef.py
Original file line number Diff line number Diff line change
Expand Up @@ -2615,7 +2615,7 @@ def wrapper(*args, **kwargs):

if argname.startswith("-"):
# optional args
if isinstance(argtype, bool):
if argtype == bool:
parser.add_argument(argname, action="store_true" if argvalue else "store_false")
else:
parser.add_argument(argname, type=argtype, required=True, default=argvalue)
Expand Down Expand Up @@ -7383,26 +7383,23 @@ class ProcessListingCommand(GenericCommand):
by this pattern."""

_cmdline_ = "process-search"
_syntax_ = "{:s} [PATTERN]".format(_cmdline_)
_syntax_ = "{:s} [REGEX_PATTERN]".format(_cmdline_)
_aliases_ = ["ps",]
_example_ = "{:s} gdb".format(_cmdline_)
_example_ = "{:s} gdb.*".format(_cmdline_)

def __init__(self):
super().__init__(complete=gdb.COMPLETE_LOCATION)
ps = which("ps")
self.add_setting("ps_command", "{:s} auxww".format(ps), "`ps` command to get process information")
return

def do_invoke(self, argv):
do_attach = False
smart_scan = False

opts, args = getopt.getopt(argv, "as")
for o, _ in opts:
if o == "-a": do_attach = True
if o == "-s": smart_scan = True

pattern = re.compile("^.*$") if not args else re.compile(args[0])
@parse_arguments({"pattern": ""}, {"--attach": True, "--smart-scan": True})
def do_invoke(self, argv, *args, **kwargs):
args = kwargs["arguments"]
do_attach = args.attach
smart_scan = args.smart_scan
pattern = args.pattern
pattern = re.compile("^.*$") if not args else re.compile(pattern)

for process in self.get_processes():
pid = int(process["pid"])
Expand Down
14 changes: 14 additions & 0 deletions tests/runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,20 @@ def test_cmd_process_status(self):
self.assertIn("No open connections", res)
return

def test_cmd_process_search(self):
res = gdb_start_silent_cmd("process-search", target="/tmp/pattern.out", before=["set args w00tw00t", ])
self.assertNoException(res)
self.assertIn("/tmp/pattern.out", res)

res = gdb_start_silent_cmd("process-search gdb.*fakefake", target="/tmp/pattern.out", before=["set args w00tw00t", ])
self.assertNoException(res)
self.assertIn("gdb", res)

res = gdb_start_silent_cmd("process-search --smart-scan gdb.*fakefake", target="/tmp/pattern.out", before=["set args w00tw00t", ])
self.assertNoException(res)
self.assertNotIn("gdb", res)
return

def test_cmd_registers(self):
self.assertFailIfInactiveSession(gdb_run_cmd("registers"))
res = gdb_start_silent_cmd("registers")
Expand Down

0 comments on commit bbe84db

Please sign in to comment.