1# This implements the "diagnose-unwind" command, usually installed 2# in the debug session like 3# command script import lldb.diagnose 4# it is used when lldb's backtrace fails -- it collects and prints 5# information about the stack frames, and tries an alternate unwind 6# algorithm, that will help to understand why lldb's unwind algorithm 7# did not succeed. 8 9import optparse 10import lldb 11import re 12import shlex 13 14# Print the frame number, pc, frame pointer, module UUID and function name 15def backtrace_print_frame (target, frame_num, addr, fp): 16 process = target.GetProcess() 17 addr_for_printing = addr 18 addr_width = process.GetAddressByteSize() * 2 19 if frame_num > 0: 20 addr = addr - 1 21 22 sbaddr = lldb.SBAddress() 23 try: 24 sbaddr.SetLoadAddress(addr, target) 25 module_description = "" 26 if sbaddr.GetModule(): 27 module_filename = "" 28 module_uuid_str = sbaddr.GetModule().GetUUIDString() 29 if module_uuid_str == None: 30 module_uuid_str = "" 31 if sbaddr.GetModule().GetFileSpec(): 32 module_filename = sbaddr.GetModule().GetFileSpec().GetFilename() 33 if module_filename == None: 34 module_filename = "" 35 if module_uuid_str != "" or module_filename != "": 36 module_description = '%s %s' % (module_filename, module_uuid_str) 37 except Exception: 38 print '%2d: pc==0x%-*x fp==0x%-*x' % (frame_num, addr_width, addr_for_printing, addr_width, fp) 39 return 40 41 sym_ctx = target.ResolveSymbolContextForAddress(sbaddr, lldb.eSymbolContextEverything) 42 if sym_ctx.IsValid() and sym_ctx.GetSymbol().IsValid(): 43 function_start = sym_ctx.GetSymbol().GetStartAddress().GetLoadAddress(target) 44 offset = addr - function_start 45 print '%2d: pc==0x%-*x fp==0x%-*x %s %s + %d' % (frame_num, addr_width, addr_for_printing, addr_width, fp, module_description, sym_ctx.GetSymbol().GetName(), offset) 46 else: 47 print '%2d: pc==0x%-*x fp==0x%-*x %s' % (frame_num, addr_width, addr_for_printing, addr_width, fp, module_description) 48 49# A simple stack walk algorithm that follows the frame chain after the first two frames. 50def simple_backtrace(debugger): 51 target = debugger.GetSelectedTarget() 52 process = target.GetProcess() 53 cur_thread = process.GetSelectedThread() 54 55 backtrace_print_frame (target, 0, cur_thread.GetFrameAtIndex(0).GetPC(), cur_thread.GetFrameAtIndex(0).GetFP()) 56 if cur_thread.GetNumFrames() < 2: 57 return 58 59 cur_fp = cur_thread.GetFrameAtIndex(1).GetFP() 60 cur_pc = cur_thread.GetFrameAtIndex(1).GetPC() 61 62 # If the pseudoreg "fp" isn't recognized, on arm hardcode to r7 which is correct for Darwin programs. 63 if cur_fp == lldb.LLDB_INVALID_ADDRESS and target.triple[0:3] == "arm": 64 for reggroup in cur_thread.GetFrameAtIndex(1).registers: 65 if reggroup.GetName() == "General Purpose Registers": 66 for reg in reggroup: 67 if reg.GetName() == "r7": 68 cur_fp = int (reg.GetValue(), 16) 69 70 frame_num = 1 71 72 while cur_pc != 0 and cur_fp != 0 and cur_pc != lldb.LLDB_INVALID_ADDRESS and cur_fp != lldb.LLDB_INVALID_ADDRESS: 73 backtrace_print_frame (target, frame_num, cur_pc, cur_fp) 74 frame_num = frame_num + 1 75 next_pc = 0 76 next_fp = 0 77 if target.triple[0:6] == "x86_64" or target.triple[0:4] == "i386" or target.triple[0:3] == "arm": 78 error = lldb.SBError() 79 next_pc = process.ReadPointerFromMemory(cur_fp + process.GetAddressByteSize(), error) 80 if not error.Success(): 81 next_pc = 0 82 next_fp = process.ReadPointerFromMemory(cur_fp, error) 83 if not error.Success(): 84 next_fp = 0 85 # Clear the 0th bit for arm frames - this indicates it is a thumb frame 86 if target.triple[0:3] == "arm" and (next_pc & 1) == 1: 87 next_pc = next_pc & ~1 88 cur_pc = next_pc 89 cur_fp = next_fp 90 backtrace_print_frame (target, frame_num, cur_pc, cur_fp) 91 92def diagnose_unwind(debugger, command, result, dict): 93 """ 94Gather diagnostic information to help debug incorrect unwind (backtrace) 95behavior in lldb. When there is a backtrace that doesn't look 96correct, run this command with the correct thread selected and a 97large amount of diagnostic information will be printed, it is likely 98to be helpful when reporting the problem. 99 """ 100 101 command_args = shlex.split(command) 102 parser = create_diagnose_unwind_options() 103 try: 104 (options, args) = parser.parse_args(command_args) 105 except: 106 return 107 target = debugger.GetSelectedTarget() 108 if target: 109 process = target.GetProcess() 110 if process: 111 thread = process.GetSelectedThread() 112 if thread: 113 lldb_versions_match = re.search(r'[lL][lL][dD][bB]-(\d+)([.](\d+))?([.](\d+))?', debugger.GetVersionString()) 114 lldb_version = 0 115 lldb_minor = 0 116 if len(lldb_versions_match.groups()) >= 1 and lldb_versions_match.groups()[0]: 117 lldb_major = int(lldb_versions_match.groups()[0]) 118 if len(lldb_versions_match.groups()) >= 5 and lldb_versions_match.groups()[4]: 119 lldb_minor = int(lldb_versions_match.groups()[4]) 120 121 print 'LLDB version %s' % debugger.GetVersionString() 122 print 'Unwind diagnostics for thread %d' % thread.GetIndexID() 123 print "" 124 print "lldb's unwind algorithm:" 125 print "" 126 frame_num = 0 127 for frame in thread.frames: 128 if not frame.IsInlined(): 129 backtrace_print_frame (target, frame_num, frame.GetPC(), frame.GetFP()) 130 frame_num = frame_num + 1 131 print "" 132 print "=============================================================================================" 133 print "" 134 print "Simple stack walk algorithm:" 135 print "" 136 simple_backtrace(debugger) 137 print "" 138 print "=============================================================================================" 139 print "" 140 for frame in thread.frames: 141 if not frame.IsInlined(): 142 print "--------------------------------------------------------------------------------------" 143 print "" 144 print "Disassembly of %s, frame %d" % (frame.GetFunctionName(), frame.GetFrameID()) 145 print "" 146 if lldb_major > 300 or (lldb_major == 300 and lldb_minor >= 18): 147 if target.triple[0:6] == "x86_64" or target.triple[0:4] == "i386": 148 debugger.HandleCommand('disassemble -F att -a 0x%x' % frame.GetPC()) 149 else: 150 debugger.HandleCommand('disassemble -a 0x%x' % frame.GetPC()) 151 else: 152 debugger.HandleCommand('disassemble -n "%s"' % frame.GetFunctionName()) 153 print "" 154 print "=============================================================================================" 155 print "" 156 for frame in thread.frames: 157 if not frame.IsInlined(): 158 print "--------------------------------------------------------------------------------------" 159 print "" 160 print "Unwind instructions for %s, frame %d" % (frame.GetFunctionName(), frame.GetFrameID()) 161 print "" 162 if lldb_major > 300 or (lldb_major == 300 and lldb_minor >= 20): 163 debugger.HandleCommand('image show-unwind -a "0x%x"' % frame.GetPC()) 164 else: 165 debugger.HandleCommand('image show-unwind -n "%s"' % frame.GetFunctionName()) 166 167def create_diagnose_unwind_options(): 168 usage = "usage: %prog" 169 description='''Print diagnostic information about a thread backtrace which will help to debug unwind problems''' 170 parser = optparse.OptionParser(description=description, prog='diagnose_unwind',usage=usage) 171 return parser 172 173lldb.debugger.HandleCommand('command script add -f %s.diagnose_unwind diagnose-unwind' % __name__) 174print 'The "diagnose-unwind" command has been installed, type "help diagnose-unwind" for detailed help.' 175