|
| 1 | +# This script will profile the qemu.bin build and produce a calltrace log |
| 2 | +# It is meant to be loaded from within gdb via: |
| 3 | +# arm-none-eabi-gdb-py |
| 4 | +# source gdb |
| 5 | +# profile |
| 6 | +# An STM32 QEMU is needed (either docker or natively compiled): https://github.com/beckus/qemu_stm32 |
| 7 | +# QEMU is started before running gdb via: |
| 8 | +# qemu-system-arm -S -s -M stm32-p103 -kernel qemu.bin |
| 9 | + |
| 10 | +import gdb |
| 11 | + |
| 12 | +compress = True |
| 13 | + |
| 14 | +class func: |
| 15 | + def __init__(self, parent, pc, lr, name): |
| 16 | + self.exclusive = 0 |
| 17 | + self.parent = parent |
| 18 | + self.pc = pc |
| 19 | + self.lr = lr |
| 20 | + self.children = [] |
| 21 | + self.name = name |
| 22 | + def newchild(self, pc, lr, name): |
| 23 | + if compress: |
| 24 | + for child in self.children: |
| 25 | + if child.pc == pc: |
| 26 | + child.lr = lr |
| 27 | + return child |
| 28 | + newfunc = func(self, pc, lr, name) |
| 29 | + self.children.append(newfunc) |
| 30 | + return newfunc |
| 31 | + |
| 32 | +def printstack(trace, depth=""): |
| 33 | + inclusive = trace.exclusive |
| 34 | + childstr = "" |
| 35 | + for child in trace.children: |
| 36 | + _str, incl = printstack(child, depth + " ") |
| 37 | + childstr += _str |
| 38 | + inclusive += incl |
| 39 | + childstr = "{}{} 0x{:02x} (ex:{} inc:{})\n".format(depth, trace.name, trace.pc, trace.exclusive, inclusive) + childstr |
| 40 | + if depth == "": |
| 41 | + print childstr |
| 42 | + else: |
| 43 | + return childstr, inclusive |
| 44 | + |
| 45 | +topfunc = func(None, 0, 0, None) |
| 46 | + |
| 47 | +class Profile(gdb.Command): |
| 48 | + def __init__(self): |
| 49 | + # This registers our class as "simple_command" |
| 50 | + super(Profile, self).__init__("profile", gdb.COMMAND_DATA) |
| 51 | + gdb.execute("file qemu.elf") |
| 52 | + gdb.execute("target remote localhost:1234") |
| 53 | + gdb.execute("set pagination off") |
| 54 | + self.trace = topfunc |
| 55 | + |
| 56 | + def invoke(self, arg, from_tty): |
| 57 | + # When we call "simple_command" from gdb, this is the method |
| 58 | + # that will be called. |
| 59 | + #gdb.execute("set logging file /dev/null") |
| 60 | + #gdb.execute("set logging redirect on") |
| 61 | + #gdb.execute("set logging on") |
| 62 | + gdb.execute("b run_profile") |
| 63 | + gdb.execute("c") |
| 64 | + gdb.execute("disable") |
| 65 | + stop_pc = gdb.newest_frame().older().pc() |
| 66 | + |
| 67 | + last_lr = int(gdb.parse_and_eval('$lr'))-1 |
| 68 | + while True: |
| 69 | + frame = gdb.newest_frame() |
| 70 | + pc = frame.pc() |
| 71 | + lr = int(gdb.parse_and_eval('$lr'))-1 |
| 72 | + if pc == self.trace.lr: |
| 73 | + print "Returned from {:02x}".format(self.trace.pc) |
| 74 | + self.trace = self.trace.parent |
| 75 | + #return |
| 76 | + elif lr != last_lr: |
| 77 | + self.trace = self.trace.newchild(pc, lr, frame.name()) |
| 78 | + print "Called {}{:02x} (return: {:02x})".format(frame.name(), pc, lr) |
| 79 | + #return |
| 80 | + if pc == 0 or pc == stop_pc: |
| 81 | + break; |
| 82 | + self.trace.exclusive += 1 |
| 83 | + last_lr = lr |
| 84 | + gdb.execute("si") |
| 85 | + #gdb.execute("set logging off") |
| 86 | + #gdb.execute("display") |
| 87 | + printstack(topfunc) |
| 88 | + |
| 89 | +Profile() |
0 commit comments