trace-boot 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #!/usr/bin/env python3
  2. import common
  3. from shell_helpers import LF
  4. class Main(common.LkmcCliFunction):
  5. def __init__(self):
  6. super().__init__(
  7. description='''Trace the PIC addresses executed on a Linux kernel boot.
  8. More information at: https://github.com/cirosantilli/linux-kernel-module-cheat#tracing
  9. '''
  10. )
  11. def timed_main(self):
  12. args = self.get_common_args()
  13. run = self.import_path_main('run')
  14. if self.env['emulator'] == 'gem5':
  15. args['trace'] = 'Exec,-ExecSymbol,-ExecMicro'
  16. run.main(**args)
  17. elif self.env['emulator'] == 'qemu':
  18. run_args = args.copy()
  19. run_args['trace'] = 'exec_tb'
  20. run_args['quit_after_boot'] = True
  21. run.main(**run_args)
  22. qemu_trace2txt = self.import_path_main('qemu-trace2txt')
  23. qemu_trace2txt.main(**args)
  24. # Instruction count.
  25. # We could put this on a separate script, but it just adds more arch boilerplate to a new script.
  26. # So let's just leave it here for now since it did not add a significant processing time.
  27. kernel_entry_addr = hex(self.get_elf_entry(self.env['vmlinux']))
  28. nlines = 0
  29. nlines_firmware = 0
  30. with open(self.env['qemu_trace_txt_file'], 'r') as trace_file:
  31. in_firmware = True
  32. for line in trace_file:
  33. line = line.rstrip()
  34. nlines += 1
  35. pc = line.split('=')[-1]
  36. if pc == kernel_entry_addr:
  37. in_firmware = False
  38. if in_firmware:
  39. nlines_firmware += 1
  40. print('''\
  41. instructions {}
  42. entry_address {}
  43. instructions_firmware {}
  44. '''.format(
  45. nlines,
  46. kernel_entry_addr,
  47. nlines_firmware
  48. ),
  49. end=''
  50. )
  51. if __name__ == '__main__':
  52. Main().cli()