Greg Clayton | 6b152ff | 2012-09-10 20:55:08 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | import lldb |
| 4 | import commands |
| 5 | import optparse |
| 6 | import shlex |
| 7 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8 | |
Greg Clayton | 6b152ff | 2012-09-10 20:55:08 +0000 | [diff] [blame] | 9 | def stack_frames(debugger, command, result, dict): |
| 10 | command_args = shlex.split(command) |
| 11 | usage = "usage: %prog [options] <PATH> [PATH ...]" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 12 | description = '''This command will enumerate all stack frames, print the stack size for each, and print an aggregation of which functions have the largest stack frame sizes at the end.''' |
| 13 | parser = optparse.OptionParser( |
| 14 | description=description, prog='ls', usage=usage) |
| 15 | parser.add_option( |
| 16 | '-v', |
| 17 | '--verbose', |
| 18 | action='store_true', |
| 19 | dest='verbose', |
| 20 | help='display verbose debug info', |
| 21 | default=False) |
Greg Clayton | 6b152ff | 2012-09-10 20:55:08 +0000 | [diff] [blame] | 22 | try: |
| 23 | (options, args) = parser.parse_args(command_args) |
| 24 | except: |
| 25 | return |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 26 | |
Greg Clayton | bca7db7 | 2014-07-11 22:41:30 +0000 | [diff] [blame] | 27 | target = debugger.GetSelectedTarget() |
| 28 | process = target.GetProcess() |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 29 | |
Greg Clayton | 6b152ff | 2012-09-10 20:55:08 +0000 | [diff] [blame] | 30 | frame_info = {} |
Greg Clayton | bca7db7 | 2014-07-11 22:41:30 +0000 | [diff] [blame] | 31 | for thread in process: |
Greg Clayton | 6b152ff | 2012-09-10 20:55:08 +0000 | [diff] [blame] | 32 | last_frame = None |
| 33 | print "thread %u" % (thread.id) |
| 34 | for frame in thread.frames: |
| 35 | if last_frame: |
| 36 | frame_size = 0 |
| 37 | if frame.idx == 1: |
| 38 | if frame.fp == last_frame.fp: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 39 | # No frame one the first frame (might be right at the |
| 40 | # entry point) |
Greg Clayton | 6b152ff | 2012-09-10 20:55:08 +0000 | [diff] [blame] | 41 | first_frame_size = 0 |
| 42 | frame_size = frame.fp - frame.sp |
| 43 | else: |
| 44 | # First frame that has a valid size |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 45 | first_frame_size = last_frame.fp - last_frame.sp |
Greg Clayton | a4475f2 | 2012-09-11 02:25:20 +0000 | [diff] [blame] | 46 | print "<%#7x> %s" % (first_frame_size, last_frame) |
Greg Clayton | 6b152ff | 2012-09-10 20:55:08 +0000 | [diff] [blame] | 47 | if first_frame_size: |
| 48 | name = last_frame.name |
| 49 | if name not in frame_info: |
| 50 | frame_info[name] = first_frame_size |
| 51 | else: |
| 52 | frame_info[name] += first_frame_size |
| 53 | else: |
| 54 | # Second or higher frame |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 55 | frame_size = frame.fp - last_frame.fp |
Greg Clayton | a4475f2 | 2012-09-11 02:25:20 +0000 | [diff] [blame] | 56 | print "<%#7x> %s" % (frame_size, frame) |
Greg Clayton | 6b152ff | 2012-09-10 20:55:08 +0000 | [diff] [blame] | 57 | if frame_size > 0: |
| 58 | name = frame.name |
| 59 | if name not in frame_info: |
| 60 | frame_info[name] = frame_size |
| 61 | else: |
| 62 | frame_info[name] += frame_size |
| 63 | last_frame = frame |
| 64 | print frame_info |
Greg Clayton | 6b152ff | 2012-09-10 20:55:08 +0000 | [diff] [blame] | 65 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 66 | |
| 67 | lldb.debugger.HandleCommand( |
| 68 | "command script add -f stacks.stack_frames stack_frames") |
| 69 | print "A new command called 'stack_frames' was added, type 'stack_frames --help' for more information." |