blob: e963ecdfe5d693fc84415085eefc3d8b691c8eb9 [file] [log] [blame]
Johnny Chen1605cf62010-09-08 22:54:46 +00001"""
Johnny Chenb51d87d2010-10-07 21:38:28 +00002This LLDB module contains miscellaneous utilities.
Johnny Chen30e48502011-05-13 21:55:30 +00003Some of the test suite takes advantage of the utility functions defined here.
4They can also be useful for general purpose lldb scripting.
Johnny Chen1605cf62010-09-08 22:54:46 +00005"""
6
7import lldb
Johnny Chen0bfa8592011-03-23 20:28:59 +00008import os, sys
Johnny Chened5f04e2010-10-15 23:33:18 +00009import StringIO
Johnny Chen1605cf62010-09-08 22:54:46 +000010
Johnny Chen8a3b54e2011-04-26 23:07:40 +000011# ===================================================
12# Utilities for locating/checking executable programs
13# ===================================================
Johnny Chen979cb5d2011-04-26 22:53:38 +000014
Johnny Chen0bfa8592011-03-23 20:28:59 +000015def is_exe(fpath):
Johnny Chenefdc26a2011-04-26 23:10:15 +000016 """Returns True if fpath is an executable."""
Johnny Chen0bfa8592011-03-23 20:28:59 +000017 return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
18
Johnny Chen0bfa8592011-03-23 20:28:59 +000019def which(program):
Johnny Chenefdc26a2011-04-26 23:10:15 +000020 """Returns the full path to a program; None otherwise."""
Johnny Chen0bfa8592011-03-23 20:28:59 +000021 fpath, fname = os.path.split(program)
22 if fpath:
23 if is_exe(program):
24 return program
25 else:
26 for path in os.environ["PATH"].split(os.pathsep):
27 exe_file = os.path.join(path, program)
28 if is_exe(exe_file):
29 return exe_file
30 return None
31
Johnny Chen51ed1b62011-03-03 19:14:00 +000032# ===================================================
33# Disassembly for an SBFunction or an SBSymbol object
34# ===================================================
35
36def disassemble(target, function_or_symbol):
37 """Disassemble the function or symbol given a target.
38
39 It returns the disassembly content in a string object.
40 """
41 buf = StringIO.StringIO()
42 insts = function_or_symbol.GetInstructions(target)
Johnny Chend643c082011-04-28 22:57:01 +000043 for i in insts:
Johnny Chen51ed1b62011-03-03 19:14:00 +000044 print >> buf, i
45 return buf.getvalue()
46
47
Johnny Chen4c70f282011-03-02 01:36:45 +000048# ==========================================================
49# Integer (byte size 1, 2, 4, and 8) to bytearray conversion
50# ==========================================================
51
52def int_to_bytearray(val, bytesize):
53 """Utility function to convert an integer into a bytearray.
54
Johnny Chend2765fc2011-03-02 20:54:22 +000055 It returns the bytearray in the little endian format. It is easy to get the
56 big endian format, just do ba.reverse() on the returned object.
Johnny Chen4c70f282011-03-02 01:36:45 +000057 """
Johnny Chenf4c0d1d2011-03-30 17:54:35 +000058 import struct
Johnny Chen4c70f282011-03-02 01:36:45 +000059
60 if bytesize == 1:
61 return bytearray([val])
62
63 # Little endian followed by a format character.
64 template = "<%c"
65 if bytesize == 2:
66 fmt = template % 'h'
67 elif bytesize == 4:
68 fmt = template % 'i'
69 elif bytesize == 4:
70 fmt = template % 'q'
71 else:
72 return None
73
Johnny Chenf4c0d1d2011-03-30 17:54:35 +000074 packed = struct.pack(fmt, val)
Johnny Chen4c70f282011-03-02 01:36:45 +000075 return bytearray(map(ord, packed))
76
77def bytearray_to_int(bytes, bytesize):
78 """Utility function to convert a bytearray into an integer.
79
Johnny Chend2765fc2011-03-02 20:54:22 +000080 It interprets the bytearray in the little endian format. For a big endian
81 bytearray, just do ba.reverse() on the object before passing it in.
Johnny Chen4c70f282011-03-02 01:36:45 +000082 """
Johnny Chenf4c0d1d2011-03-30 17:54:35 +000083 import struct
Johnny Chen4c70f282011-03-02 01:36:45 +000084
85 if bytesize == 1:
86 return ba[0]
87
88 # Little endian followed by a format character.
89 template = "<%c"
90 if bytesize == 2:
91 fmt = template % 'h'
92 elif bytesize == 4:
93 fmt = template % 'i'
94 elif bytesize == 4:
95 fmt = template % 'q'
96 else:
97 return None
98
Johnny Chenf4c0d1d2011-03-30 17:54:35 +000099 unpacked = struct.unpack(fmt, str(bytes))
Johnny Chen4c70f282011-03-02 01:36:45 +0000100 return unpacked[0]
101
102
Johnny Chenbc1a93e2011-04-23 00:13:34 +0000103# ==============================================================
104# Get the description of an lldb object or None if not available
105# ==============================================================
Johnny Chenbdc36bd2011-04-25 20:23:05 +0000106def get_description(obj, option=None):
107 """Calls lldb_obj.GetDescription() and returns a string, or None.
108
109 For SBTarget and SBBreakpointLocation lldb objects, an extra option can be
110 passed in to describe the detailed level of description desired:
111 o lldb.eDescriptionLevelBrief
112 o lldb.eDescriptionLevelFull
113 o lldb.eDescriptionLevelVerbose
114 """
115 method = getattr(obj, 'GetDescription')
Johnny Chenbc1a93e2011-04-23 00:13:34 +0000116 if not method:
117 return None
Johnny Chenbdc36bd2011-04-25 20:23:05 +0000118 if isinstance(obj, lldb.SBTarget) or isinstance(obj, lldb.SBBreakpointLocation):
119 if option is None:
120 option = lldb.eDescriptionLevelBrief
121
Johnny Chenbc1a93e2011-04-23 00:13:34 +0000122 stream = lldb.SBStream()
123 if option is None:
124 success = method(stream)
125 else:
126 success = method(stream, option)
127 if not success:
128 return None
129 return stream.GetData()
130
131
Johnny Chen168a61a2010-10-22 21:31:03 +0000132# =================================================
133# Convert some enum value to its string counterpart
134# =================================================
Johnny Chenbe683bc2010-10-07 22:15:58 +0000135
Johnny Chen47342d52011-04-27 17:43:07 +0000136def state_type_to_str(enum):
Johnny Chenbe683bc2010-10-07 22:15:58 +0000137 """Returns the stateType string given an enum."""
138 if enum == lldb.eStateInvalid:
Johnny Chen59b84772010-10-18 15:46:54 +0000139 return "invalid"
Johnny Chenbe683bc2010-10-07 22:15:58 +0000140 elif enum == lldb.eStateUnloaded:
Johnny Chen59b84772010-10-18 15:46:54 +0000141 return "unloaded"
Johnny Chen42da4da2011-03-05 01:20:11 +0000142 elif enum == lldb.eStateConnected:
143 return "connected"
Johnny Chenbe683bc2010-10-07 22:15:58 +0000144 elif enum == lldb.eStateAttaching:
Johnny Chen59b84772010-10-18 15:46:54 +0000145 return "attaching"
Johnny Chenbe683bc2010-10-07 22:15:58 +0000146 elif enum == lldb.eStateLaunching:
Johnny Chen59b84772010-10-18 15:46:54 +0000147 return "launching"
Johnny Chenbe683bc2010-10-07 22:15:58 +0000148 elif enum == lldb.eStateStopped:
Johnny Chen59b84772010-10-18 15:46:54 +0000149 return "stopped"
Johnny Chenbe683bc2010-10-07 22:15:58 +0000150 elif enum == lldb.eStateRunning:
Johnny Chen59b84772010-10-18 15:46:54 +0000151 return "running"
Johnny Chenbe683bc2010-10-07 22:15:58 +0000152 elif enum == lldb.eStateStepping:
Johnny Chen59b84772010-10-18 15:46:54 +0000153 return "stepping"
Johnny Chenbe683bc2010-10-07 22:15:58 +0000154 elif enum == lldb.eStateCrashed:
Johnny Chen59b84772010-10-18 15:46:54 +0000155 return "crashed"
Johnny Chenbe683bc2010-10-07 22:15:58 +0000156 elif enum == lldb.eStateDetached:
Johnny Chen59b84772010-10-18 15:46:54 +0000157 return "detached"
Johnny Chenbe683bc2010-10-07 22:15:58 +0000158 elif enum == lldb.eStateExited:
Johnny Chen59b84772010-10-18 15:46:54 +0000159 return "exited"
Johnny Chenbe683bc2010-10-07 22:15:58 +0000160 elif enum == lldb.eStateSuspended:
Johnny Chen59b84772010-10-18 15:46:54 +0000161 return "suspended"
Johnny Chenbe683bc2010-10-07 22:15:58 +0000162 else:
Johnny Chen42da4da2011-03-05 01:20:11 +0000163 raise Exception("Unknown StateType enum")
Johnny Chenbe683bc2010-10-07 22:15:58 +0000164
Johnny Chen47342d52011-04-27 17:43:07 +0000165def stop_reason_to_str(enum):
Johnny Chenbe683bc2010-10-07 22:15:58 +0000166 """Returns the stopReason string given an enum."""
167 if enum == lldb.eStopReasonInvalid:
Johnny Chen59b84772010-10-18 15:46:54 +0000168 return "invalid"
Johnny Chenbe683bc2010-10-07 22:15:58 +0000169 elif enum == lldb.eStopReasonNone:
Johnny Chen59b84772010-10-18 15:46:54 +0000170 return "none"
Johnny Chenbe683bc2010-10-07 22:15:58 +0000171 elif enum == lldb.eStopReasonTrace:
Johnny Chen59b84772010-10-18 15:46:54 +0000172 return "trace"
Johnny Chenbe683bc2010-10-07 22:15:58 +0000173 elif enum == lldb.eStopReasonBreakpoint:
Johnny Chen59b84772010-10-18 15:46:54 +0000174 return "breakpoint"
Johnny Chenbe683bc2010-10-07 22:15:58 +0000175 elif enum == lldb.eStopReasonWatchpoint:
Johnny Chen59b84772010-10-18 15:46:54 +0000176 return "watchpoint"
Johnny Chenbe683bc2010-10-07 22:15:58 +0000177 elif enum == lldb.eStopReasonSignal:
Johnny Chen59b84772010-10-18 15:46:54 +0000178 return "signal"
Johnny Chenbe683bc2010-10-07 22:15:58 +0000179 elif enum == lldb.eStopReasonException:
Johnny Chen59b84772010-10-18 15:46:54 +0000180 return "exception"
Johnny Chenbe683bc2010-10-07 22:15:58 +0000181 elif enum == lldb.eStopReasonPlanComplete:
Johnny Chen59b84772010-10-18 15:46:54 +0000182 return "plancomplete"
Johnny Chenbe683bc2010-10-07 22:15:58 +0000183 else:
Johnny Chen42da4da2011-03-05 01:20:11 +0000184 raise Exception("Unknown StopReason enum")
Johnny Chenbe683bc2010-10-07 22:15:58 +0000185
Johnny Chen47342d52011-04-27 17:43:07 +0000186def value_type_to_str(enum):
Johnny Chen2c8d1592010-11-03 21:37:58 +0000187 """Returns the valueType string given an enum."""
188 if enum == lldb.eValueTypeInvalid:
189 return "invalid"
190 elif enum == lldb.eValueTypeVariableGlobal:
191 return "global_variable"
192 elif enum == lldb.eValueTypeVariableStatic:
193 return "static_variable"
194 elif enum == lldb.eValueTypeVariableArgument:
195 return "argument_variable"
196 elif enum == lldb.eValueTypeVariableLocal:
197 return "local_variable"
198 elif enum == lldb.eValueTypeRegister:
199 return "register"
200 elif enum == lldb.eValueTypeRegisterSet:
201 return "register_set"
202 elif enum == lldb.eValueTypeConstResult:
203 return "constant_result"
204 else:
Johnny Chen42da4da2011-03-05 01:20:11 +0000205 raise Exception("Unknown ValueType enum")
Johnny Chen2c8d1592010-11-03 21:37:58 +0000206
Johnny Chenbe683bc2010-10-07 22:15:58 +0000207
Johnny Chen168a61a2010-10-22 21:31:03 +0000208# ==================================================
209# Utility functions related to Threads and Processes
210# ==================================================
Johnny Chenbe683bc2010-10-07 22:15:58 +0000211
Johnny Chene428d332011-04-25 22:04:05 +0000212def get_stopped_threads(process, reason):
Johnny Chen5aba3f52011-05-26 21:53:05 +0000213 """Returns the thread(s) with the specified stop reason in a list.
214
215 The list can be empty if no such thread exists.
216 """
Johnny Chene428d332011-04-25 22:04:05 +0000217 threads = []
Johnny Chend643c082011-04-28 22:57:01 +0000218 for t in process:
Johnny Chene428d332011-04-25 22:04:05 +0000219 if t.GetStopReason() == reason:
220 threads.append(t)
221 return threads
222
223def get_stopped_thread(process, reason):
224 """A convenience function which returns the first thread with the given stop
225 reason or None.
226
227 Example usages:
228
229 1. Get the stopped thread due to a breakpoint condition
230
231 ...
232 from lldbutil import get_stopped_thread
Johnny Chen3d8ae462011-06-15 22:14:12 +0000233 thread = get_stopped_thread(process, lldb.eStopReasonPlanComplete)
Johnny Chene428d332011-04-25 22:04:05 +0000234 self.assertTrue(thread != None, "There should be a thread stopped due to breakpoint condition")
235 ...
236
237 2. Get the thread stopped due to a breakpoint
238
239 ...
240 from lldbutil import get_stopped_thread
Johnny Chen3d8ae462011-06-15 22:14:12 +0000241 thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint)
Johnny Chene428d332011-04-25 22:04:05 +0000242 self.assertTrue(thread != None, "There should be a thread stopped due to breakpoint")
243 ...
244
245 """
246 threads = get_stopped_threads(process, reason)
247 if len(threads) == 0:
248 return None
249 return threads[0]
250
Johnny Chen318aaa02011-04-25 23:38:13 +0000251def get_threads_stopped_at_breakpoint (process, bkpt):
252 """ For a stopped process returns the thread stopped at the breakpoint passed in bkpt"""
253 stopped_threads = []
254 threads = []
255
256 stopped_threads = get_stopped_threads (process, lldb.eStopReasonBreakpoint)
257
258 if len(stopped_threads) == 0:
259 return threads
260
261 for thread in stopped_threads:
262 # Make sure we've hit our breakpoint...
263 break_id = thread.GetStopReasonDataAtIndex (0)
264 if break_id == bkpt.GetID():
265 threads.append(thread)
266
267 return threads
268
269def continue_to_breakpoint (process, bkpt):
270 """ Continues the process, if it stops, returns the threads stopped at bkpt; otherwise, returns None"""
271 process.Continue()
272 if process.GetState() != lldb.eStateStopped:
273 return None
274 else:
275 return get_threads_stopped_at_breakpoint (process, bkpt)
276
Johnny Chen69af39d2011-03-09 23:45:56 +0000277def get_caller_symbol(thread):
278 """
279 Returns the symbol name for the call site of the leaf function.
280 """
281 depth = thread.GetNumFrames()
282 if depth <= 1:
283 return None
284 caller = thread.GetFrameAtIndex(1).GetSymbol()
285 if caller:
286 return caller.GetName()
287 else:
288 return None
289
290
Johnny Chen318aaa02011-04-25 23:38:13 +0000291def get_function_names(thread):
Johnny Chen1605cf62010-09-08 22:54:46 +0000292 """
293 Returns a sequence of function names from the stack frames of this thread.
294 """
295 def GetFuncName(i):
296 return thread.GetFrameAtIndex(i).GetFunction().GetName()
297
298 return map(GetFuncName, range(thread.GetNumFrames()))
299
300
Johnny Chen318aaa02011-04-25 23:38:13 +0000301def get_symbol_names(thread):
Johnny Chenb51d87d2010-10-07 21:38:28 +0000302 """
303 Returns a sequence of symbols for this thread.
304 """
305 def GetSymbol(i):
306 return thread.GetFrameAtIndex(i).GetSymbol().GetName()
307
308 return map(GetSymbol, range(thread.GetNumFrames()))
309
310
Johnny Chen318aaa02011-04-25 23:38:13 +0000311def get_pc_addresses(thread):
Johnny Chenb51d87d2010-10-07 21:38:28 +0000312 """
313 Returns a sequence of pc addresses for this thread.
314 """
315 def GetPCAddress(i):
316 return thread.GetFrameAtIndex(i).GetPCAddress()
317
318 return map(GetPCAddress, range(thread.GetNumFrames()))
319
320
Johnny Chen318aaa02011-04-25 23:38:13 +0000321def get_filenames(thread):
Johnny Chen1605cf62010-09-08 22:54:46 +0000322 """
323 Returns a sequence of file names from the stack frames of this thread.
324 """
325 def GetFilename(i):
326 return thread.GetFrameAtIndex(i).GetLineEntry().GetFileSpec().GetFilename()
327
328 return map(GetFilename, range(thread.GetNumFrames()))
329
330
Johnny Chen318aaa02011-04-25 23:38:13 +0000331def get_line_numbers(thread):
Johnny Chen1605cf62010-09-08 22:54:46 +0000332 """
333 Returns a sequence of line numbers from the stack frames of this thread.
334 """
335 def GetLineNumber(i):
336 return thread.GetFrameAtIndex(i).GetLineEntry().GetLine()
337
338 return map(GetLineNumber, range(thread.GetNumFrames()))
339
340
Johnny Chen318aaa02011-04-25 23:38:13 +0000341def get_module_names(thread):
Johnny Chen1605cf62010-09-08 22:54:46 +0000342 """
343 Returns a sequence of module names from the stack frames of this thread.
344 """
345 def GetModuleName(i):
346 return thread.GetFrameAtIndex(i).GetModule().GetFileSpec().GetFilename()
347
348 return map(GetModuleName, range(thread.GetNumFrames()))
349
350
Johnny Chen318aaa02011-04-25 23:38:13 +0000351def get_stack_frames(thread):
Johnny Chen88866ac2010-09-09 00:55:07 +0000352 """
353 Returns a sequence of stack frames for this thread.
354 """
355 def GetStackFrame(i):
356 return thread.GetFrameAtIndex(i)
357
358 return map(GetStackFrame, range(thread.GetNumFrames()))
359
360
Johnny Chen318aaa02011-04-25 23:38:13 +0000361def print_stacktrace(thread, string_buffer = False):
Johnny Chen1605cf62010-09-08 22:54:46 +0000362 """Prints a simple stack trace of this thread."""
Johnny Chen30425e92010-10-07 18:52:48 +0000363
Johnny Chened5f04e2010-10-15 23:33:18 +0000364 output = StringIO.StringIO() if string_buffer else sys.stdout
Johnny Chenb51d87d2010-10-07 21:38:28 +0000365 target = thread.GetProcess().GetTarget()
366
Johnny Chen1605cf62010-09-08 22:54:46 +0000367 depth = thread.GetNumFrames()
368
Johnny Chen318aaa02011-04-25 23:38:13 +0000369 mods = get_module_names(thread)
370 funcs = get_function_names(thread)
371 symbols = get_symbol_names(thread)
372 files = get_filenames(thread)
373 lines = get_line_numbers(thread)
374 addrs = get_pc_addresses(thread)
Johnny Chen30425e92010-10-07 18:52:48 +0000375
Johnny Chenad5fd402010-10-25 19:13:52 +0000376 if thread.GetStopReason() != lldb.eStopReasonInvalid:
Johnny Chen47342d52011-04-27 17:43:07 +0000377 desc = "stop reason=" + stop_reason_to_str(thread.GetStopReason())
Johnny Chenad5fd402010-10-25 19:13:52 +0000378 else:
379 desc = ""
380 print >> output, "Stack trace for thread id={0:#x} name={1} queue={2} ".format(
381 thread.GetThreadID(), thread.GetName(), thread.GetQueueName()) + desc
Johnny Chen1605cf62010-09-08 22:54:46 +0000382
Johnny Chenb51d87d2010-10-07 21:38:28 +0000383 for i in range(depth):
384 frame = thread.GetFrameAtIndex(i)
385 function = frame.GetFunction()
Johnny Chen1605cf62010-09-08 22:54:46 +0000386
Johnny Chenb51d87d2010-10-07 21:38:28 +0000387 load_addr = addrs[i].GetLoadAddress(target)
Johnny Chen960ce122011-05-25 19:06:18 +0000388 if not function:
Johnny Chenb51d87d2010-10-07 21:38:28 +0000389 file_addr = addrs[i].GetFileAddress()
Johnny Chen49f3d812011-06-16 22:07:48 +0000390 start_addr = frame.GetSymbol().GetStartAddress().GetFileAddress()
391 symbol_offset = file_addr - start_addr
392 print >> output, " frame #{num}: {addr:#016x} {mod}`{symbol} + {offset}".format(
393 num=i, addr=load_addr, mod=mods[i], symbol=symbols[i], offset=symbol_offset)
Johnny Chenb51d87d2010-10-07 21:38:28 +0000394 else:
Johnny Chen49f3d812011-06-16 22:07:48 +0000395 print >> output, " frame #{num}: {addr:#016x} {mod}`{func} at {file}:{line} {args}".format(
396 num=i, addr=load_addr, mod=mods[i], func=funcs[i], file=files[i], line=lines[i],
397 args=get_args_as_string(frame, showFuncName=False))
Johnny Chenb51d87d2010-10-07 21:38:28 +0000398
399 if string_buffer:
Johnny Chened5f04e2010-10-15 23:33:18 +0000400 return output.getvalue()
Johnny Chenb51d87d2010-10-07 21:38:28 +0000401
402
Johnny Chen318aaa02011-04-25 23:38:13 +0000403def print_stacktraces(process, string_buffer = False):
Johnny Chenb51d87d2010-10-07 21:38:28 +0000404 """Prints the stack traces of all the threads."""
405
Johnny Chened5f04e2010-10-15 23:33:18 +0000406 output = StringIO.StringIO() if string_buffer else sys.stdout
Johnny Chenb51d87d2010-10-07 21:38:28 +0000407
408 print >> output, "Stack traces for " + repr(process)
409
Johnny Chen311b1d62011-05-05 18:50:56 +0000410 for thread in process:
411 print >> output, print_stacktrace(thread, string_buffer=True)
Johnny Chen30425e92010-10-07 18:52:48 +0000412
413 if string_buffer:
Johnny Chened5f04e2010-10-15 23:33:18 +0000414 return output.getvalue()
Johnny Chen185e2c12011-05-08 17:25:27 +0000415
416# ===================================
417# Utility functions related to Frames
418# ===================================
419
Johnny Chenabb3b2d2011-05-12 00:32:41 +0000420def get_parent_frame(frame):
421 """
422 Returns the parent frame of the input frame object; None if not available.
423 """
424 thread = frame.GetThread()
425 parent_found = False
426 for f in thread:
427 if parent_found:
428 return f
429 if f.GetFrameID() == frame.GetFrameID():
430 parent_found = True
431
432 # If we reach here, no parent has been found, return None.
433 return None
434
Johnny Chen49f3d812011-06-16 22:07:48 +0000435def get_args_as_string(frame, showFuncName=True):
Johnny Chenabb3b2d2011-05-12 00:32:41 +0000436 """
437 Returns the args of the input frame object as a string.
438 """
439 # arguments => True
440 # locals => False
441 # statics => False
442 # in_scope_only => True
443 vars = frame.GetVariables(True, False, False, True) # type of SBValueList
444 args = [] # list of strings
445 for var in vars:
446 args.append("(%s)%s=%s" % (var.GetTypeName(),
447 var.GetName(),
448 var.GetValue(frame)))
Johnny Chen960ce122011-05-25 19:06:18 +0000449 if frame.GetFunction():
Johnny Chenbbc18b62011-05-13 00:44:49 +0000450 name = frame.GetFunction().GetName()
Johnny Chen960ce122011-05-25 19:06:18 +0000451 elif frame.GetSymbol():
Johnny Chenbbc18b62011-05-13 00:44:49 +0000452 name = frame.GetSymbol().GetName()
453 else:
454 name = ""
Johnny Chen49f3d812011-06-16 22:07:48 +0000455 if showFuncName:
456 return "%s(%s)" % (name, ", ".join(args))
457 else:
458 return "(%s)" % (", ".join(args))
459
Johnny Chen185e2c12011-05-08 17:25:27 +0000460def print_registers(frame, string_buffer = False):
Johnny Chenb2998772011-05-08 18:55:37 +0000461 """Prints all the register sets of the frame."""
Johnny Chen185e2c12011-05-08 17:25:27 +0000462
463 output = StringIO.StringIO() if string_buffer else sys.stdout
464
465 print >> output, "Register sets for " + repr(frame)
466
Johnny Chen728255b2011-05-10 19:21:13 +0000467 registerSet = frame.GetRegisters() # Return type of SBValueList.
468 print >> output, "Frame registers (size of register set = %d):" % registerSet.GetSize()
469 for value in registerSet:
Johnny Chen185e2c12011-05-08 17:25:27 +0000470 #print >> output, value
471 print >> output, "%s (number of children = %d):" % (value.GetName(), value.GetNumChildren())
472 for child in value:
473 print >> output, "Name: %s, Value: %s" % (child.GetName(), child.GetValue(frame))
474
475 if string_buffer:
476 return output.getvalue()
Johnny Chen728255b2011-05-10 19:21:13 +0000477
478def get_registers(frame, kind):
479 """Returns the registers given the frame and the kind of registers desired.
480
481 Returns None if there's no such kind.
482 """
483 registerSet = frame.GetRegisters() # Return type of SBValueList.
484 for value in registerSet:
485 if kind.lower() in value.GetName().lower():
486 return value
487
488 return None
489
490def get_GPRs(frame):
491 """Returns the general purpose registers of the frame as an SBValue.
492
Johnny Chenfd1175c2011-05-10 23:01:44 +0000493 The returned SBValue object is iterable. An example:
494 ...
495 from lldbutil import get_GPRs
496 regs = get_GPRs(frame)
497 for reg in regs:
498 print "%s => %s" % (reg.GetName(), reg.GetValue())
499 ...
Johnny Chen728255b2011-05-10 19:21:13 +0000500 """
501 return get_registers(frame, "general purpose")
502
503def get_FPRs(frame):
504 """Returns the floating point registers of the frame as an SBValue.
505
Johnny Chenfd1175c2011-05-10 23:01:44 +0000506 The returned SBValue object is iterable. An example:
507 ...
508 from lldbutil import get_FPRs
509 regs = get_FPRs(frame)
510 for reg in regs:
511 print "%s => %s" % (reg.GetName(), reg.GetValue())
512 ...
Johnny Chen728255b2011-05-10 19:21:13 +0000513 """
514 return get_registers(frame, "floating point")
515
516def get_ESRs(frame):
517 """Returns the exception state registers of the frame as an SBValue.
518
Johnny Chenfd1175c2011-05-10 23:01:44 +0000519 The returned SBValue object is iterable. An example:
520 ...
521 from lldbutil import get_ESRs
522 regs = get_ESRs(frame)
523 for reg in regs:
524 print "%s => %s" % (reg.GetName(), reg.GetValue())
525 ...
Johnny Chen728255b2011-05-10 19:21:13 +0000526 """
527 return get_registers(frame, "exception state")