trace-boot 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/usr/bin/env python3
  2. import imp
  3. import os
  4. import subprocess
  5. import re
  6. import common
  7. run = imp.load_source('run', os.path.join(common.root_dir, 'run'))
  8. qemu_trace2txt = imp.load_source('qemu_trace2txt', os.path.join(common.root_dir, 'qemu-trace2txt'))
  9. parser = common.get_argparse(argparse_args={
  10. 'description': '''Trace the PIC addresses executed on a Linux kernel boot.
  11. More information at: https://github.com/cirosantilli/linux-kernel-module-cheat#tracing
  12. '''
  13. })
  14. parser.add_argument(
  15. 'extra_emulator_args', nargs='*',
  16. help='Extra options to append at the end of the emulator command line'
  17. )
  18. args = common.setup(parser)
  19. extra_args = {
  20. 'extra_emulator_args': args.extra_emulator_args,
  21. }
  22. if args.gem5:
  23. extra_args.update({
  24. 'eval': 'm5 exit',
  25. 'trace': 'Exec,-ExecSymbol,-ExecMicro',
  26. })
  27. run.main(args, extra_args)
  28. else:
  29. extra_args.update({
  30. 'kernel_cli_extra': 'init=/poweroff.out',
  31. 'trace': 'exec_tb',
  32. })
  33. run.main(args, extra_args)
  34. qemu_trace2txt.main()
  35. # Instruction count.
  36. # We could put this on a separate script, but it just adds more arch boilerplate to a new script.
  37. # So let's just leave it here for now since it did not add a significant processing time.
  38. kernel_entry_addr = hex(common.get_elf_entry(common.vmlinux))
  39. nlines = 0
  40. nlines_firmware = 0
  41. with open(common.qemu_trace_txt_file, 'r') as trace_file:
  42. in_firmware = True
  43. for line in trace_file:
  44. line = line.rstrip()
  45. nlines += 1
  46. pc = line.split('=')[-1]
  47. if pc == kernel_entry_addr:
  48. in_firmware = False
  49. if in_firmware:
  50. nlines_firmware += 1
  51. print('''\
  52. instructions {}
  53. entry_address {}
  54. instructions_firmware {}\
  55. '''.format(
  56. nlines,
  57. kernel_entry_addr,
  58. nlines_firmware
  59. ))