test-gdb 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/usr/bin/env python3
  2. import threading
  3. import os
  4. import common
  5. import lkmc.import_path
  6. import path_properties
  7. class Main(common.TestCliFunction):
  8. def __init__(self):
  9. super().__init__(
  10. description='''\
  11. https://cirosantilli.com/linux-kernel-module-cheat#gdb-tests
  12. ''',
  13. defaults={
  14. 'mode': 'userland',
  15. }
  16. )
  17. self.add_argument(
  18. 'tests',
  19. nargs='*',
  20. help='''\
  21. If given, run only the given tests. Otherwise, run all tests,
  22. found by searching for the Python test files.
  23. '''
  24. )
  25. def setup_one(self):
  26. self.env['tests'] = self.resolve_targets(
  27. [
  28. self.env['baremetal_source_dir'],
  29. self.env['userland_source_dir']
  30. ],
  31. self.env['tests']
  32. )
  33. def timed_main(self):
  34. if self.env['mode'] == 'userland':
  35. exts = self.env['build_in_exts']
  36. elif self.env['mode'] == 'baremetal':
  37. exts = self.env['baremetal_build_in_exts']
  38. rootdir_abs_len = len(self.env['root_dir'])
  39. for test in self.env['tests']:
  40. for path, in_dirnames, in_filenames in self.sh.walk(test):
  41. path_abs = os.path.abspath(path)
  42. dirpath_relative_root = path_abs[rootdir_abs_len + 1:]
  43. for in_filename in in_filenames:
  44. in_file_abs = os.path.join(path_abs, in_filename)
  45. path_relative_root = os.path.join(dirpath_relative_root, in_filename)
  46. path_relative_root_base, ext = os.path.splitext(path_relative_root)
  47. if ext in exts and os.path.exists(path_relative_root_base + '.py'):
  48. my_path_properties = path_properties.get(path_relative_root)
  49. if my_path_properties.should_be_tested(
  50. self.env,
  51. ):
  52. run = lkmc.import_path.import_path_main('run')
  53. run_gdb = lkmc.import_path.import_path_main('run-gdb')
  54. common_args = self.get_common_args()
  55. common_args[self.env['mode']] = path_relative_root
  56. run_args = common_args.copy()
  57. run_args['gdb_wait'] = True
  58. run_args.update(self.base_run_args)
  59. test_id_string = self.test_setup(
  60. run_args,
  61. '{} {}'.format(self.env['mode'], path_relative_root)
  62. )
  63. run_thread = threading.Thread(target=lambda: run(**run_args))
  64. run_thread.start()
  65. gdb_args = common_args.copy()
  66. gdb_args['test'] = True
  67. exit_status = run_gdb(**gdb_args)
  68. run_thread.join()
  69. self.test_teardown(run, exit_status, test_id_string)
  70. if __name__ == '__main__':
  71. Main().cli()