1#!/usr/bin/python
2
3#----------------------------------------------------------------------
4# Be sure to add the python path that points to the LLDB shared library.
5#
6# To use this in the embedded python interpreter using "lldb":
7#
8#   cd /path/containing/crashlog.py
9#   lldb
10#   (lldb) script import crashlog
11#   "crashlog" command installed, type "crashlog --help" for detailed help
12#   (lldb) crashlog ~/Library/Logs/DiagnosticReports/a.crash
13#
14# The benefit of running the crashlog command inside lldb in the
15# embedded python interpreter is when the command completes, there
16# will be a target with all of the files loaded at the locations
17# described in the crash log. Only the files that have stack frames
18# in the backtrace will be loaded unless the "--load-all" option
19# has been specified. This allows users to explore the program in the
20# state it was in right at crash time.
21#
22# On MacOSX csh, tcsh:
23#   ( setenv PYTHONPATH /path/to/LLDB.framework/Resources/Python ; ./crashlog.py ~/Library/Logs/DiagnosticReports/a.crash )
24#
25# On MacOSX sh, bash:
26#   PYTHONPATH=/path/to/LLDB.framework/Resources/Python ./crashlog.py ~/Library/Logs/DiagnosticReports/a.crash
27#----------------------------------------------------------------------
28
29import lldb
30import commands
31import optparse
32import os
33import plistlib
34import re
35import shlex
36import sys
37import time
38import uuid
39
40class Address:
41    """Class that represents an address that will be symbolicated"""
42    def __init__(self, target, load_addr):
43        self.target = target
44        self.load_addr = load_addr # The load address that this object represents
45        self.so_addr = None # the resolved lldb.SBAddress (if any), named so_addr for section/offset address
46        self.sym_ctx = None # The cached symbol context for this address
47        self.description = None # Any original textual description of this address to be used as a backup in case symbolication fails
48        self.symbolication = None # The cached symbolicated string that describes this address
49        self.inlined = False
50    def __str__(self):
51        s = "%#16.16x" % (self.load_addr)
52        if self.symbolication:
53            s += " %s" % (self.symbolication)
54        elif self.description:
55            s += " %s" % (self.description)
56        elif self.so_addr:
57            s += " %s" % (self.so_addr)
58        return s
59
60    def resolve_addr(self):
61        if self.so_addr == None:
62            self.so_addr = self.target.ResolveLoadAddress (self.load_addr)
63        return self.so_addr
64
65    def is_inlined(self):
66        return self.inlined
67
68    def get_symbol_context(self):
69        if self.sym_ctx == None:
70            sb_addr = self.resolve_addr()
71            if sb_addr:
72                self.sym_ctx = self.target.ResolveSymbolContextForAddress (sb_addr, lldb.eSymbolContextEverything)
73            else:
74                self.sym_ctx = lldb.SBSymbolContext()
75        return self.sym_ctx
76
77    def get_instructions(self):
78        sym_ctx = self.get_symbol_context()
79        if sym_ctx:
80            function = sym_ctx.GetFunction()
81            if function:
82                return function.GetInstructions(self.target)
83            return sym_ctx.GetSymbol().GetInstructions(self.target)
84        return None
85
86    def symbolicate(self):
87        if self.symbolication == None:
88            self.symbolication = ''
89            self.inlined = False
90            sym_ctx = self.get_symbol_context()
91            if sym_ctx:
92                module = sym_ctx.GetModule()
93                if module:
94                    self.symbolication += module.GetFileSpec().GetFilename() + '`'
95                    function_start_load_addr = -1
96                    function = sym_ctx.GetFunction()
97                    block = sym_ctx.GetBlock()
98                    line_entry = sym_ctx.GetLineEntry()
99                    symbol = sym_ctx.GetSymbol()
100                    inlined_block = block.GetContainingInlinedBlock();
101                    if function:
102                        self.symbolication += function.GetName()
103
104                        if inlined_block:
105                            self.inlined = True
106                            self.symbolication += ' [inlined] ' + inlined_block.GetInlinedName();
107                            block_range_idx = inlined_block.GetRangeIndexForBlockAddress (self.so_addr)
108                            if block_range_idx < lldb.UINT32_MAX:
109                                block_range_start_addr = inlined_block.GetRangeStartAddress (block_range_idx)
110                                function_start_load_addr = block_range_start_addr.GetLoadAddress (self.target)
111                        if function_start_load_addr == -1:
112                            function_start_load_addr = function.GetStartAddress().GetLoadAddress (self.target)
113                    elif symbol:
114                        self.symbolication += symbol.GetName()
115                        function_start_load_addr = symbol.GetStartAddress().GetLoadAddress (self.target)
116                    else:
117                        self.symbolication = ''
118                        return False
119
120                    # Dump the offset from the current function or symbol if it is non zero
121                    function_offset = self.load_addr - function_start_load_addr
122                    if function_offset > 0:
123                        self.symbolication += " + %u" % (function_offset)
124                    elif function_offset < 0:
125                        self.symbolication += " %i (invalid negative offset, file a bug) " % function_offset
126
127                    # Print out any line information if any is available
128                    if line_entry.GetFileSpec():
129                            self.symbolication += ' at %s' % line_entry.GetFileSpec().GetFilename()
130                            self.symbolication += ':%u' % line_entry.GetLine ()
131                            column = line_entry.GetColumn()
132                            if column > 0:
133                                self.symbolication += ':%u' % column
134                    return True
135        return False
136
137class Section:
138    """Class that represents an load address range"""
139    sect_info_regex = re.compile('(?P<name>[^=]+)=(?P<range>.*)')
140    addr_regex = re.compile('^\s*(?P<start>0x[0-9A-Fa-f]+)\s*$')
141    range_regex = re.compile('^\s*(?P<start>0x[0-9A-Fa-f]+)\s*(?P<op>[-+])\s*(?P<end>0x[0-9A-Fa-f]+)\s*$')
142
143    def __init__(self, start_addr = None, end_addr = None, name = None):
144        self.start_addr = start_addr
145        self.end_addr = end_addr
146        self.name = name
147
148    def contains(self, addr):
149        return self.start_addr <= addr and addr < self.end_addr;
150
151    def set_from_string(self, s):
152        match = self.sect_info_regex.match (s)
153        if match:
154            self.name = match.group('name')
155            range_str = match.group('range')
156            addr_match = self.addr_regex.match(range_str)
157            if addr_match:
158                self.start_addr = int(addr_match.group('start'), 16)
159                self.end_addr = None
160                return True
161
162            range_match = self.range_regex.match(range_str)
163            if range_match:
164                self.start_addr = int(range_match.group('start'), 16)
165                self.end_addr = int(range_match.group('end'), 16)
166                op = range_match.group('op')
167                if op == '+':
168                    self.end_addr += self.start_addr
169                return True
170        print 'error: invalid section info string "%s"' % s
171        print 'Valid section info formats are:'
172        print 'Format                Example                    Description'
173        print '--------------------- -----------------------------------------------'
174        print '<name>=<base>        __TEXT=0x123000             Section from base address only'
175        print '<name>=<base>-<end>  __TEXT=0x123000-0x124000    Section from base address and end address'
176        print '<name>=<base>+<size> __TEXT=0x123000+0x1000      Section from base address and size'
177        return False
178
179    def __str__(self):
180        if self.name:
181            if self.end_addr != None:
182                if self.start_addr != None:
183                    return "%s=[0x%16.16x - 0x%16.16x)" % (self.name, self.start_addr, self.end_addr)
184            else:
185                if self.start_addr != None:
186                    return "%s=0x%16.16x" % (self.name, self.start_addr)
187            return self.name
188        return "<invalid>"
189
190class Image:
191    """A class that represents an executable image and any associated data"""
192
193    def __init__(self, path, uuid = None):
194        self.path = path
195        self.resolved_path = None
196        self.resolved = False
197        self.unavailable = False
198        self.uuid = uuid
199        self.section_infos = list()
200        self.identifier = None
201        self.version = None
202        self.arch = None
203        self.module = None
204        self.symfile = None
205        self.slide = None
206
207    def dump(self, prefix):
208        print "%s%s" % (prefix, self)
209
210    def __str__(self):
211        s = "%s %s %s" % (self.get_uuid(), self.version, self.get_resolved_path())
212        for section_info in self.section_infos:
213            s += ", %s" % (section_info)
214        if self.slide != None:
215            s += ', slide = 0x%16.16x' % self.slide
216        return s
217
218    def add_section(self, section):
219        #print "added '%s' to '%s'" % (section, self.path)
220        self.section_infos.append (section)
221
222    def get_section_containing_load_addr (self, load_addr):
223        for section_info in self.section_infos:
224            if section_info.contains(load_addr):
225                return section_info
226        return None
227
228    def get_resolved_path(self):
229        if self.resolved_path:
230            return self.resolved_path
231        elif self.path:
232            return self.path
233        return None
234
235    def get_resolved_path_basename(self):
236        path = self.get_resolved_path()
237        if path:
238            return os.path.basename(path)
239        return None
240
241    def symfile_basename(self):
242        if self.symfile:
243            return os.path.basename(self.symfile)
244        return None
245
246    def has_section_load_info(self):
247        return self.section_infos or self.slide != None
248
249    def load_module(self, target):
250        if self.unavailable:
251            return None # We already warned that we couldn't find this module, so don't return an error string
252        # Load this module into "target" using the section infos to
253        # set the section load addresses
254        if self.has_section_load_info():
255            if target:
256                if self.module:
257                    if self.section_infos:
258                        num_sections_loaded = 0
259                        for section_info in self.section_infos:
260                            if section_info.name:
261                                section = self.module.FindSection (section_info.name)
262                                if section:
263                                    error = target.SetSectionLoadAddress (section, section_info.start_addr)
264                                    if error.Success():
265                                        num_sections_loaded += 1
266                                    else:
267                                        return 'error: %s' % error.GetCString()
268                                else:
269                                    return 'error: unable to find the section named "%s"' % section_info.name
270                            else:
271                                return 'error: unable to find "%s" section in "%s"' % (range.name, self.get_resolved_path())
272                        if num_sections_loaded == 0:
273                            return 'error: no sections were successfully loaded'
274                    else:
275                        err = target.SetModuleLoadAddress(self.module, self.slide)
276                        if err.Fail():
277                            return err.GetCString()
278                    return None
279                else:
280                    return 'error: invalid module'
281            else:
282                return 'error: invalid target'
283        else:
284            return 'error: no section infos'
285
286    def add_module(self, target):
287        '''Add the Image described in this object to "target" and load the sections if "load" is True.'''
288        if target:
289            # Try and find using UUID only first so that paths need not match up
290            uuid_str = self.get_normalized_uuid_string()
291            if uuid_str:
292                self.module = target.AddModule (None, None, uuid_str)
293            if not self.module:
294                self.locate_module_and_debug_symbols ()
295                if self.unavailable:
296                    return None
297                resolved_path = self.get_resolved_path()
298                self.module = target.AddModule (resolved_path, self.arch, uuid_str, self.symfile)
299            if not self.module:
300                return 'error: unable to get module for (%s) "%s"' % (self.arch, self.get_resolved_path())
301            if self.has_section_load_info():
302                return self.load_module(target)
303            else:
304                return None # No sections, the module was added to the target, so success
305        else:
306            return 'error: invalid target'
307
308    def locate_module_and_debug_symbols (self):
309        # By default, just use the paths that were supplied in:
310        # self.path
311        # self.resolved_path
312        # self.module
313        # self.symfile
314        # Subclasses can inherit from this class and override this function
315        self.resolved = True
316        return True
317
318    def get_uuid(self):
319        if not self.uuid and self.module:
320            self.uuid = uuid.UUID(self.module.GetUUIDString())
321        return self.uuid
322
323    def get_normalized_uuid_string(self):
324        if self.uuid:
325            return str(self.uuid).upper()
326        return None
327
328    def create_target(self):
329        '''Create a target using the information in this Image object.'''
330        if self.unavailable:
331            return None
332
333        if self.locate_module_and_debug_symbols ():
334            resolved_path = self.get_resolved_path();
335            path_spec = lldb.SBFileSpec (resolved_path)
336            #result.PutCString ('plist[%s] = %s' % (uuid, self.plist))
337            error = lldb.SBError()
338            target = lldb.debugger.CreateTarget (resolved_path, self.arch, None, False, error);
339            if target:
340                self.module = target.FindModule(path_spec)
341                if self.has_section_load_info():
342                    err = self.load_module(target)
343                    if err:
344                        print 'ERROR: ', err
345                return target
346            else:
347                print 'error: unable to create a valid target for (%s) "%s"' % (self.arch, self.path)
348        else:
349            print 'error: unable to locate main executable (%s) "%s"' % (self.arch, self.path)
350        return None
351
352class Symbolicator:
353
354    def __init__(self):
355        """A class the represents the information needed to symbolicate addresses in a program"""
356        self.target = None
357        self.images = list() # a list of images to be used when symbolicating
358
359
360    def __str__(self):
361        s = "Symbolicator:\n"
362        if self.target:
363            s += "Target = '%s'\n" % (self.target)
364            s += "Target modules:'\n"
365            for m in self.target.modules:
366                print m
367        s += "Images:\n"
368        for image in self.images:
369            s += '    %s\n' % (image)
370        return s
371
372    def find_images_with_identifier(self, identifier):
373        images = list()
374        for image in self.images:
375            if image.identifier == identifier:
376                images.append(image)
377        return images
378
379    def find_image_containing_load_addr(self, load_addr):
380        for image in self.images:
381            if image.get_section_containing_load_addr (load_addr):
382                return image
383        return None
384
385    def create_target(self):
386        if self.target:
387            return self.target
388
389        if self.images:
390            for image in self.images:
391                self.target = image.create_target ()
392                if self.target:
393                    return self.target
394        return None
395
396    def symbolicate(self, load_addr):
397        if not self.target:
398            self.create_target()
399        if self.target:
400            image = self.find_image_containing_load_addr (load_addr)
401            if image:
402                image.add_module (self.target)
403                symbolicated_address = Address(self.target, load_addr)
404                if symbolicated_address.symbolicate ():
405
406                    if symbolicated_address.so_addr:
407                        symbolicated_addresses = list()
408                        symbolicated_addresses.append(symbolicated_address)
409                        # See if we were able to reconstruct anything?
410                        while 1:
411                            inlined_parent_so_addr = lldb.SBAddress()
412                            inlined_parent_sym_ctx = symbolicated_address.sym_ctx.GetParentOfInlinedScope (symbolicated_address.so_addr, inlined_parent_so_addr)
413                            if not inlined_parent_sym_ctx:
414                                break
415                            if not inlined_parent_so_addr:
416                                break
417
418                            symbolicated_address = Address(self.target, inlined_parent_so_addr.GetLoadAddress(self.target))
419                            symbolicated_address.sym_ctx = inlined_parent_sym_ctx
420                            symbolicated_address.so_addr = inlined_parent_so_addr
421                            symbolicated_address.symbolicate ()
422
423                            # push the new frame onto the new frame stack
424                            symbolicated_addresses.append (symbolicated_address)
425
426                        if symbolicated_addresses:
427                            return symbolicated_addresses
428        else:
429            print 'error: no target in Symbolicator'
430        return None
431
432
433def disassemble_instructions (target, instructions, pc, insts_before_pc, insts_after_pc, non_zeroeth_frame):
434    lines = list()
435    pc_index = -1
436    comment_column = 50
437    for inst_idx, inst in enumerate(instructions):
438        inst_pc = inst.GetAddress().GetLoadAddress(target);
439        if pc == inst_pc:
440            pc_index = inst_idx
441        mnemonic = inst.GetMnemonic (target)
442        operands =  inst.GetOperands (target)
443        comment =  inst.GetComment (target)
444        #data = inst.GetData (target)
445        lines.append ("%#16.16x: %8s %s" % (inst_pc, mnemonic, operands))
446        if comment:
447            line_len = len(lines[-1])
448            if line_len < comment_column:
449                lines[-1] += ' ' * (comment_column - line_len)
450                lines[-1] += "; %s" % comment
451
452    if pc_index >= 0:
453        # If we are disassembling the non-zeroeth frame, we need to backup the PC by 1
454        if non_zeroeth_frame and pc_index > 0:
455            pc_index = pc_index - 1
456        if insts_before_pc == -1:
457            start_idx = 0
458        else:
459            start_idx = pc_index - insts_before_pc
460        if start_idx < 0:
461            start_idx = 0
462        if insts_before_pc == -1:
463            end_idx = inst_idx
464        else:
465            end_idx = pc_index + insts_after_pc
466        if end_idx > inst_idx:
467            end_idx = inst_idx
468        for i in range(start_idx, end_idx+1):
469            if i == pc_index:
470                print ' -> ', lines[i]
471            else:
472                print '    ', lines[i]
473
474def print_module_section_data (section):
475    print section
476    section_data = section.GetSectionData()
477    if section_data:
478        ostream = lldb.SBStream()
479        section_data.GetDescription (ostream, section.GetFileAddress())
480        print ostream.GetData()
481
482def print_module_section (section, depth):
483    print section
484    if depth > 0:
485        num_sub_sections = section.GetNumSubSections()
486        for sect_idx in range(num_sub_sections):
487            print_module_section (section.GetSubSectionAtIndex(sect_idx), depth - 1)
488
489def print_module_sections (module, depth):
490    for sect in module.section_iter():
491        print_module_section (sect, depth)
492
493def print_module_symbols (module):
494    for sym in module:
495        print sym
496
497def Symbolicate(command_args):
498
499    usage = "usage: %prog [options] <addr1> [addr2 ...]"
500    description='''Symbolicate one or more addresses using LLDB's python scripting API..'''
501    parser = optparse.OptionParser(description=description, prog='crashlog.py',usage=usage)
502    parser.add_option('-v', '--verbose', action='store_true', dest='verbose', help='display verbose debug info', default=False)
503    parser.add_option('-p', '--platform', type='string', metavar='platform', dest='platform', help='specify one platform by name')
504    parser.add_option('-f', '--file', type='string', metavar='file', dest='file', help='Specify a file to use when symbolicating')
505    parser.add_option('-a', '--arch', type='string', metavar='arch', dest='arch', help='Specify a architecture to use when symbolicating')
506    parser.add_option('-s', '--slide', type='int', metavar='slide', dest='slide', help='Specify the slide to use on the file specified with the --file option', default=None)
507    parser.add_option('--section', type='string', action='append', dest='section_strings', help='specify <sect-name>=<start-addr> or <sect-name>=<start-addr>-<end-addr>')
508    try:
509        (options, args) = parser.parse_args(command_args)
510    except:
511        return
512    symbolicator = Symbolicator()
513    images = list();
514    if options.file:
515        image = Image(options.file);
516        image.arch = options.arch
517        # Add any sections that were specified with one or more --section options
518        if options.section_strings:
519            for section_str in options.section_strings:
520                section = Section()
521                if section.set_from_string (section_str):
522                    image.add_section (section)
523                else:
524                    sys.exit(1)
525        if options.slide != None:
526            image.slide = options.slide
527        symbolicator.images.append(image)
528
529    target = symbolicator.create_target()
530    if options.verbose:
531        print symbolicator
532    if target:
533        for addr_str in args:
534            addr = int(addr_str, 0)
535            symbolicated_addrs = symbolicator.symbolicate(addr)
536            for symbolicated_addr in symbolicated_addrs:
537                print symbolicated_addr
538            print
539    else:
540        print 'error: no target for %s' % (symbolicator)
541
542if __name__ == '__main__':
543    # Create a new debugger instance
544    lldb.debugger = lldb.SBDebugger.Create()
545    Symbolicate (sys.argv[1:])
546