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