ps.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals, print_function
  3. def main():
  4. from frida.application import ConsoleApplication
  5. class PSApplication(ConsoleApplication):
  6. def _add_options(self, parser):
  7. parser.add_option("-a", "--applications", help="list only applications",
  8. action='store_true', dest="list_only_applications", default=False)
  9. parser.add_option("-i", "--installed", help="include all installed applications",
  10. action='store_true', dest="include_all_applications", default=False)
  11. def _initialize(self, parser, options, args):
  12. if options.include_all_applications and not options.list_only_applications:
  13. parser.error("-i cannot be used without -a")
  14. self._list_only_applications = options.list_only_applications
  15. self._include_all_applications = options.include_all_applications
  16. def _usage(self):
  17. return "usage: %prog [options]"
  18. def _start(self):
  19. if self._list_only_applications:
  20. try:
  21. applications = self._device.enumerate_applications()
  22. except Exception as e:
  23. self._update_status("Failed to enumerate applications: %s" % e)
  24. self._exit(1)
  25. return
  26. if not self._include_all_applications:
  27. applications = list(filter(lambda app: app.pid != 0, applications))
  28. if len(applications) > 0:
  29. pid_column_width = max(map(lambda app: len("%d" % app.pid), applications))
  30. name_column_width = max(map(lambda app: len(app.name), applications))
  31. identifier_column_width = max(map(lambda app: len(app.identifier), applications))
  32. else:
  33. pid_column_width = 0
  34. name_column_width = 0
  35. identifier_column_width = 0
  36. header_format = "%" + str(pid_column_width) + "s " + \
  37. "%-" + str(name_column_width) + "s " + \
  38. "%-" + str(identifier_column_width) + "s"
  39. self._print(header_format % ("PID", "Name", "Identifier"))
  40. self._print("%s %s %s" % (pid_column_width * "-", name_column_width * "-", identifier_column_width * "-"))
  41. line_format = "%" + str(pid_column_width) + "s " + \
  42. "%-" + str(name_column_width) + "s " + \
  43. "%-" + str(identifier_column_width) + "s"
  44. for app in sorted(applications, key=cmp_to_key(compare_applications)):
  45. if app.pid == 0:
  46. self._print(line_format % ("-", app.name, app.identifier))
  47. else:
  48. self._print(line_format % (app.pid, app.name, app.identifier))
  49. else:
  50. try:
  51. processes = self._device.enumerate_processes()
  52. except Exception as e:
  53. self._update_status("Failed to enumerate processes: %s" % e)
  54. self._exit(1)
  55. return
  56. pid_column_width = max(map(lambda p: len("%d" % p.pid), processes))
  57. name_column_width = max(map(lambda p: len(p.name), processes))
  58. header_format = "%" + str(pid_column_width) + "s %s"
  59. self._print(header_format % ("PID", "Name"))
  60. self._print("%s %s" % (pid_column_width * "-", name_column_width * "-"))
  61. line_format = "%" + str(pid_column_width) + "d %s"
  62. for process in sorted(processes, key=cmp_to_key(compare_processes)):
  63. self._print(line_format % (process.pid, process.name))
  64. self._exit(0)
  65. def compare_applications(a, b):
  66. a_is_running = a.pid != 0
  67. b_is_running = b.pid != 0
  68. if a_is_running == b_is_running:
  69. if a.name > b.name:
  70. return 1
  71. elif a.name < b.name:
  72. return -1
  73. else:
  74. return 0
  75. elif a_is_running:
  76. return -1
  77. else:
  78. return 1
  79. def compare_processes(a, b):
  80. a_has_icon = a.get_small_icon() is not None
  81. b_has_icon = b.get_small_icon() is not None
  82. if a_has_icon == b_has_icon:
  83. if a.name > b.name:
  84. return 1
  85. elif a.name < b.name:
  86. return -1
  87. else:
  88. return 0
  89. elif a_has_icon:
  90. return -1
  91. else:
  92. return 1
  93. def cmp_to_key(mycmp):
  94. "Convert a cmp= function into a key= function"
  95. class K:
  96. def __init__(self, obj, *args):
  97. self.obj = obj
  98. def __lt__(self, other):
  99. return mycmp(self.obj, other.obj) < 0
  100. def __gt__(self, other):
  101. return mycmp(self.obj, other.obj) > 0
  102. def __eq__(self, other):
  103. return mycmp(self.obj, other.obj) == 0
  104. def __le__(self, other):
  105. return mycmp(self.obj, other.obj) <= 0
  106. def __ge__(self, other):
  107. return mycmp(self.obj, other.obj) >= 0
  108. def __ne__(self, other):
  109. return mycmp(self.obj, other.obj) != 0
  110. return K
  111. app = PSApplication()
  112. app.run()
  113. if __name__ == '__main__':
  114. main()