blob: fec879993d9658a2fa9ce88303f324374c511ff8 [file] [log] [blame]
Greg Clayton6b152ff2012-09-10 20:55:08 +00001#!/usr/bin/python
2
3import lldb
4import commands
5import optparse
6import shlex
7
Kate Stoneb9c1b512016-09-06 20:57:50 +00008
Greg Clayton6b152ff2012-09-10 20:55:08 +00009def stack_frames(debugger, command, result, dict):
10 command_args = shlex.split(command)
11 usage = "usage: %prog [options] <PATH> [PATH ...]"
Kate Stoneb9c1b512016-09-06 20:57:50 +000012 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 Clayton6b152ff2012-09-10 20:55:08 +000022 try:
23 (options, args) = parser.parse_args(command_args)
24 except:
25 return
Kate Stoneb9c1b512016-09-06 20:57:50 +000026
Greg Claytonbca7db72014-07-11 22:41:30 +000027 target = debugger.GetSelectedTarget()
28 process = target.GetProcess()
Kate Stoneb9c1b512016-09-06 20:57:50 +000029
Greg Clayton6b152ff2012-09-10 20:55:08 +000030 frame_info = {}
Greg Claytonbca7db72014-07-11 22:41:30 +000031 for thread in process:
Greg Clayton6b152ff2012-09-10 20:55:08 +000032 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 Stoneb9c1b512016-09-06 20:57:50 +000039 # No frame one the first frame (might be right at the
40 # entry point)
Greg Clayton6b152ff2012-09-10 20:55:08 +000041 first_frame_size = 0
42 frame_size = frame.fp - frame.sp
43 else:
44 # First frame that has a valid size
Kate Stoneb9c1b512016-09-06 20:57:50 +000045 first_frame_size = last_frame.fp - last_frame.sp
Greg Claytona4475f22012-09-11 02:25:20 +000046 print "<%#7x> %s" % (first_frame_size, last_frame)
Greg Clayton6b152ff2012-09-10 20:55:08 +000047 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 Stoneb9c1b512016-09-06 20:57:50 +000055 frame_size = frame.fp - last_frame.fp
Greg Claytona4475f22012-09-11 02:25:20 +000056 print "<%#7x> %s" % (frame_size, frame)
Greg Clayton6b152ff2012-09-10 20:55:08 +000057 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 Clayton6b152ff2012-09-10 20:55:08 +000065
Kate Stoneb9c1b512016-09-06 20:57:50 +000066
67lldb.debugger.HandleCommand(
68 "command script add -f stacks.stack_frames stack_frames")
69print "A new command called 'stack_frames' was added, type 'stack_frames --help' for more information."