8000 ps: fall back to a listing without icons when icon loading fails by tubibibo · Pull Request #211 · frida/frida-tools · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions frida_tools/ps.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,11 @@ def _list_processes(self) -> None:

try:
assert self._device is not None
processes = self._device.enumerate_processes(scope=scope)
processes = _enumerate_with_icon_fallback(
self._device.enumerate_processes,
scope,
lambda e: self._log("warning", f"Unable to load app icons ({e}), listing without them"),
)
except Exception as e:
self._update_status(f"Failed to enumerate processes: {e}")
self._exit(1)
Expand Down Expand Up @@ -128,7 +132,11 @@ def _list_applications(self) -> None:

try:
assert self._device is not None
applications = self._device.enumerate_applications(scope=scope)
applications = _enumerate_with_icon_fallback(
self._device.enumerate_applications,
scope,
lambda e: self._log("warning", f"Unable to load app icons ({e}), listing without them"),
)
except Exception as e:
self._update_status(f"Failed to enumerate applications: {e}")
self._exit(1)
Expand Down Expand Up @@ -287,6 +295,18 @@ def compute_icon_width(item: Union[_frida.Application, _frida.Process]) -> int:
app.run()


def _enumerate_with_icon_fallback(enumerate_fn, scope, warn):
if scope != "full":
return enumerate_fn(scope=scope)

try:
return enumerate_fn(scope="full")
except Exception as e:
result = enumerate_fn(scope="minimal")
warn(e)
return result


if __name__ == "__main__":
try:
main()
Expand Down
48 changes: 48 additions & 0 deletions tests/test_ps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import unittest

from frida_tools.ps import _enumerate_with_icon_fallback


class TestPSEnumerationFallback(unittest.TestCase):
def test_full_scope_failure_is_retried_without_icons(self):
device = FakeDevice(fail_scopes={"full"})
warnings = []
processes = _enumerate_with_icon_fallback(device.enumerate_processes, "full", warnings.append)
self.assertEqual(processes, ["init"])
self.assertEqual(device.calls, ["full", "minimal"])
self.assertEqual(len(warnings), 1)
self.assertIn("could not load icons", str(warnings[0]))

def test_full_scope_success_is_returned_as_is(self):
device = FakeDevice(fail_scopes=set())
warnings = []
processes = _enumerate_with_icon_fallback(device.enumerate_processes, "full", warnings.append)
self.assertEqual(processes, ["init"])
self.assertEqual(device.calls, ["full"])
self.assertEqual(warnings, [])

def test_minimal_scope_failure_is_propagated(self):
device = FakeDevice(fail_scopes={"minimal"})
with self.assertRaises(RuntimeError):
_enumerate_with_icon_fallback(device.enumerate_processes, "minimal", lambda e: None)
self.assertEqual(device.calls, ["minimal"])

def test_failure_that_is_not_about_icons_is_not_reported_as_one(self):
device = FakeDevice(fail_scopes={"full", "minimal"})
warnings = []
with self.assertRaises(RuntimeError):
_enumerate_with_icon_fallback(device.enumerate_processes, "full", warnings.append)
self.assertEqual(device.calls, ["full", "minimal"])
self.assertEqual(warnings, [])


class FakeDevice:
def __init__(self, fail_scopes):
self.fail_scopes = fail_scopes
self.calls = []

def enumerate_processes(self, scope="minimal"):
self.calls.append(scope)
if scope in self.fail_scopes:
raise RuntimeError(f"could not load icons for {scope}")
return ["init"]
Loading
0