-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofile.py
77 lines (64 loc) · 2.21 KB
/
profile.py
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import gdb
#********************************************************************************
class EndPoint(gdb.FinishBreakpoint):
def __init__(self, breakpoint, *a, **kw):
super().__init__(*a, **kw)
self.silent = True
self.breakpoint = breakpoint
def stop(self):
# normal finish
end = int(gdb.parse_and_eval('$mcycle'))
start, out = self.breakpoint.stack.pop()
diff = end - start
print(out.strip())
print(f"\tCost in mcycle: {diff}")
return False
class StartPoint(gdb.Breakpoint):
def __init__(self, *a, **kw):
super().__init__(*a, **kw)
self.silent = True
self.stack = []
def stop(self):
start = int(gdb.parse_and_eval('$mcycle'))
# start, end, diff
frame = gdb.selected_frame() # == gdb.newest_frame()
sym_and_line = frame.find_sal()
func = frame.function().name
filename = sym_and_line.symtab.filename
line = sym_and_line.line
block = frame.block()
# older (upper) frame
of = frame.older()
sym_and_line = of.find_sal()
of_func = of.function().name
of_filename = sym_and_line.symtab.filename
of_line = sym_and_line.line
args = []
for s in block:
if not s.is_argument:
continue
name = s.name
typ = s.type
val = s.value(frame)
args.append(f"{name}: {val} [{typ}]")
# format
out = ""
out += f"{func} @ {filename}:{line}\n"
out += f"\t\t> Called from {of_func} @ {of_filename}:{of_line}\n"
for a in args:
out += f"\t{a}\n"
# append current status to a breakpoint stack
self.stack.append((start, out))
EndPoint(self, internal=True) # Pass StartPoint-object as self to EndPoint(...)
return False
class Profile(gdb.Command):
'''Simple profiling.
usage:
prof function/method name'''
def __init__(self):
super().__init__("prof", gdb.COMMAND_USER)
@classmethod
def invoke(cls, args, tty):
StartPoint(args)
Profile()
#********************************************************************************