2012-01-20 11:15:45 +08:00
#!/usr/bin/python
#----------------------------------------------------------------------
# Be sure to add the python path that points to the LLDB shared library.
2012-01-21 08:37:19 +08:00
#
# To use this in the embedded python interpreter using "lldb":
#
# cd /path/containing/crashlog.py
# lldb
# (lldb) script import crashlog
# "crashlog" command installed, type "crashlog --help" for detailed help
# (lldb) crashlog ~/Library/Logs/DiagnosticReports/a.crash
#
2016-09-07 04:57:50 +08:00
# The benefit of running the crashlog command inside lldb in the
# embedded python interpreter is when the command completes, there
2012-01-21 08:37:19 +08:00
# will be a target with all of the files loaded at the locations
# described in the crash log. Only the files that have stack frames
# in the backtrace will be loaded unless the "--load-all" option
# has been specified. This allows users to explore the program in the
2016-09-07 04:57:50 +08:00
# state it was in right at crash time.
2012-01-21 08:37:19 +08:00
#
2012-01-20 11:15:45 +08:00
# On MacOSX csh, tcsh:
2012-01-21 08:37:19 +08:00
# ( setenv PYTHONPATH /path/to/LLDB.framework/Resources/Python ; ./crashlog.py ~/Library/Logs/DiagnosticReports/a.crash )
#
2012-01-20 11:15:45 +08:00
# On MacOSX sh, bash:
2012-01-21 08:37:19 +08:00
# PYTHONPATH=/path/to/LLDB.framework/Resources/Python ./crashlog.py ~/Library/Logs/DiagnosticReports/a.crash
2012-01-20 11:15:45 +08:00
#----------------------------------------------------------------------
2019-03-07 07:50:36 +08:00
from __future__ import print_function
2012-05-05 04:44:14 +08:00
import cmd
2012-06-28 04:02:04 +08:00
import datetime
2012-05-05 04:44:14 +08:00
import glob
2012-01-20 11:15:45 +08:00
import optparse
import os
2012-06-28 04:02:04 +08:00
import platform
2012-01-20 11:15:45 +08:00
import plistlib
2016-09-07 04:57:50 +08:00
import pprint # pp = pprint.PrettyPrinter(indent=4); pp.pprint(command_args)
2012-01-20 11:15:45 +08:00
import re
2012-01-21 12:26:24 +08:00
import shlex
2012-05-05 04:44:14 +08:00
import string
2019-03-07 06:54:11 +08:00
import subprocess
2012-01-20 11:15:45 +08:00
import sys
import time
2012-01-20 14:12:47 +08:00
import uuid
2012-09-19 09:59:34 +08:00
2019-05-18 09:57:12 +08:00
def read_plist ( s ) :
if sys . version_info . major == 3 :
return plistlib . loads ( s )
else :
return plistlib . readPlistFromString ( s )
2016-09-07 04:57:50 +08:00
try :
2012-09-19 09:59:34 +08:00
# Just try for LLDB in case PYTHONPATH is already correctly setup
import lldb
except ImportError :
lldb_python_dirs = list ( )
# lldb is not in the PYTHONPATH, try some defaults for the current platform
platform_system = platform . system ( )
if platform_system == ' Darwin ' :
# On Darwin, try the currently selected Xcode directory
2019-03-07 06:54:11 +08:00
xcode_dir = subprocess . check_output ( " xcode-select --print-path " , shell = True )
2012-09-19 09:59:34 +08:00
if xcode_dir :
2016-09-07 04:57:50 +08:00
lldb_python_dirs . append (
os . path . realpath (
xcode_dir +
' /../SharedFrameworks/LLDB.framework/Resources/Python ' ) )
lldb_python_dirs . append (
xcode_dir + ' /Library/PrivateFrameworks/LLDB.framework/Resources/Python ' )
lldb_python_dirs . append (
' /System/Library/PrivateFrameworks/LLDB.framework/Resources/Python ' )
2012-09-19 09:59:34 +08:00
success = False
for lldb_python_dir in lldb_python_dirs :
if os . path . exists ( lldb_python_dir ) :
if not ( sys . path . __contains__ ( lldb_python_dir ) ) :
sys . path . append ( lldb_python_dir )
2016-09-07 04:57:50 +08:00
try :
2012-09-19 09:59:34 +08:00
import lldb
except ImportError :
pass
else :
2019-03-07 06:54:11 +08:00
print ( ' imported lldb from: " %s " ' % ( lldb_python_dir ) )
2012-09-19 09:59:34 +08:00
success = True
break
if not success :
2019-03-07 06:54:11 +08:00
print ( " error: couldn ' t locate the ' lldb ' module, please set PYTHONPATH correctly " )
2012-09-19 09:59:34 +08:00
sys . exit ( 1 )
2012-05-05 04:44:14 +08:00
from lldb . utils import symbolication
2012-01-20 11:15:45 +08:00
PARSE_MODE_NORMAL = 0
PARSE_MODE_THREAD = 1
PARSE_MODE_IMAGES = 2
PARSE_MODE_THREGS = 3
PARSE_MODE_SYSTEM = 4
2016-09-07 04:57:50 +08:00
2012-05-05 04:44:14 +08:00
class CrashLog ( symbolication . Symbolicator ) :
2012-01-20 11:15:45 +08:00
""" Class that does parses darwin crash logs """
2016-09-07 04:57:50 +08:00
parent_process_regex = re . compile ( ' ^Parent Process: \ s*(.*) \ [( \ d+) \ ] ' )
2012-01-20 11:15:45 +08:00
thread_state_regex = re . compile ( ' ^Thread ([0-9]+) crashed with ' )
thread_regex = re . compile ( ' ^Thread ([0-9]+)([^:]*):(.*) ' )
2016-09-07 04:57:50 +08:00
app_backtrace_regex = re . compile (
' ^Application Specific Backtrace ([0-9]+)([^:]*):(.*) ' )
2018-12-18 01:26:04 +08:00
frame_regex = re . compile ( ' ^([0-9]+) \ s+(.+?) \ s+(0x[0-9a-fA-F] {7} [0-9a-fA-F]+) +(.*) ' )
2019-06-12 22:46:37 +08:00
null_frame_regex = re . compile ( ' ^([0-9]+) \ s+ \ ? \ ? \ ? \ s+(0 {7} 0+) +(.*) ' )
2016-09-07 04:57:50 +08:00
image_regex_uuid = re . compile (
2018-12-18 01:26:04 +08:00
' (0x[0-9a-fA-F]+)[- \ s]+(0x[0-9a-fA-F]+) \ s+[+]?(.+?) \ s+( \ (.+ \ ))? \ s?(<([-0-9a-fA-F]+)>)? (.*) ' )
2012-01-20 11:15:45 +08:00
empty_line_regex = re . compile ( ' ^$ ' )
2016-09-07 04:57:50 +08:00
2019-03-05 09:34:47 +08:00
class Thread :
2012-01-20 11:15:45 +08:00
""" Class that represents a thread in a darwin crash log """
2016-09-07 04:57:50 +08:00
2015-03-06 06:53:06 +08:00
def __init__ ( self , index , app_specific_backtrace ) :
2012-01-20 11:15:45 +08:00
self . index = index
self . frames = list ( )
2012-07-13 11:19:35 +08:00
self . idents = list ( )
2012-01-20 11:15:45 +08:00
self . registers = dict ( )
self . reason = None
self . queue = None
2015-03-06 06:53:06 +08:00
self . app_specific_backtrace = app_specific_backtrace
2016-09-07 04:57:50 +08:00
2012-01-20 11:15:45 +08:00
def dump ( self , prefix ) :
2015-03-06 06:53:06 +08:00
if self . app_specific_backtrace :
2019-03-07 06:54:11 +08:00
print ( " % Application Specific Backtrace[ %u ] %s " % ( prefix , self . index , self . reason ) )
2015-03-06 06:53:06 +08:00
else :
2019-03-07 06:54:11 +08:00
print ( " %s Thread[ %u ] %s " % ( prefix , self . index , self . reason ) )
2012-01-20 11:15:45 +08:00
if self . frames :
2019-03-07 06:54:11 +08:00
print ( " %s Frames: " % ( prefix ) )
2012-01-20 11:15:45 +08:00
for frame in self . frames :
frame . dump ( prefix + ' ' )
if self . registers :
2019-03-07 06:54:11 +08:00
print ( " %s Registers: " % ( prefix ) )
2019-03-05 09:34:47 +08:00
for reg in self . registers . keys ( ) :
2019-03-07 06:54:11 +08:00
print ( " %s %-5s = %#16.16x " % ( prefix , reg , self . registers [ reg ] ) )
2016-09-07 04:57:50 +08:00
def dump_symbolicated ( self , crash_log , options ) :
2015-03-06 06:53:06 +08:00
this_thread_crashed = self . app_specific_backtrace
if not this_thread_crashed :
this_thread_crashed = self . did_crash ( )
if options . crashed_only and this_thread_crashed == False :
return
2019-03-07 06:54:11 +08:00
print ( " %s " % self )
2015-03-06 06:53:06 +08:00
#prev_frame_index = -1
display_frame_idx = - 1
for frame_idx , frame in enumerate ( self . frames ) :
2016-09-07 04:57:50 +08:00
disassemble = (
this_thread_crashed or options . disassemble_all_threads ) and frame_idx < options . disassemble_depth
2015-03-06 06:53:06 +08:00
if frame_idx == 0 :
2016-09-07 04:57:50 +08:00
symbolicated_frame_addresses = crash_log . symbolicate (
frame . pc & crash_log . addr_mask , options . verbose )
2015-03-06 06:53:06 +08:00
else :
2016-09-07 04:57:50 +08:00
# Any frame above frame zero and we have to subtract one to
# get the previous line entry
symbolicated_frame_addresses = crash_log . symbolicate (
( frame . pc & crash_log . addr_mask ) - 1 , options . verbose )
2015-03-06 06:53:06 +08:00
if symbolicated_frame_addresses :
symbolicated_frame_address_idx = 0
for symbolicated_frame_address in symbolicated_frame_addresses :
display_frame_idx + = 1
2019-03-07 06:54:11 +08:00
print ( ' [ %3u ] %s ' % ( frame_idx , symbolicated_frame_address ) )
2016-09-07 04:57:50 +08:00
if ( options . source_all or self . did_crash (
) ) and display_frame_idx < options . source_frames and options . source_context :
2015-03-06 06:53:06 +08:00
source_context = options . source_context
line_entry = symbolicated_frame_address . get_symbol_context ( ) . line_entry
if line_entry . IsValid ( ) :
strm = lldb . SBStream ( )
if line_entry :
2016-09-07 04:57:50 +08:00
lldb . debugger . GetSourceManager ( ) . DisplaySourceLinesWithLineNumbers (
line_entry . file , line_entry . line , source_context , source_context , " -> " , strm )
2015-03-06 06:53:06 +08:00
source_text = strm . GetData ( )
if source_text :
# Indent the source a bit
indent_str = ' '
join_str = ' \n ' + indent_str
2019-03-07 06:54:11 +08:00
print ( ' %s %s ' % ( indent_str , join_str . join ( source_text . split ( ' \n ' ) ) ) )
2015-03-06 06:53:06 +08:00
if symbolicated_frame_address_idx == 0 :
if disassemble :
instructions = symbolicated_frame_address . get_instructions ( )
if instructions :
2019-03-07 06:54:11 +08:00
print ( )
2016-09-07 04:57:50 +08:00
symbolication . disassemble_instructions (
crash_log . get_target ( ) ,
instructions ,
frame . pc ,
options . disassemble_before ,
options . disassemble_after ,
frame . index > 0 )
2019-03-07 06:54:11 +08:00
print ( )
2015-03-06 06:53:06 +08:00
symbolicated_frame_address_idx + = 1
else :
2019-03-07 06:54:11 +08:00
print ( frame )
2016-09-07 04:57:50 +08:00
2012-07-13 11:19:35 +08:00
def add_ident ( self , ident ) :
2016-09-07 04:57:50 +08:00
if ident not in self . idents :
2012-07-13 11:19:35 +08:00
self . idents . append ( ident )
2016-09-07 04:57:50 +08:00
2012-01-20 11:15:45 +08:00
def did_crash ( self ) :
2016-09-07 04:57:50 +08:00
return self . reason is not None
2012-01-20 11:15:45 +08:00
def __str__ ( self ) :
2015-03-06 06:53:06 +08:00
if self . app_specific_backtrace :
s = " Application Specific Backtrace[ %u ] " % self . index
else :
s = " Thread[ %u ] " % self . index
2012-01-20 11:15:45 +08:00
if self . reason :
s + = ' %s ' % self . reason
return s
2016-09-07 04:57:50 +08:00
2019-03-05 09:34:47 +08:00
class Frame :
2012-01-20 11:15:45 +08:00
""" Class that represents a stack frame in a thread in a darwin crash log """
2016-09-07 04:57:50 +08:00
2012-04-04 05:35:43 +08:00
def __init__ ( self , index , pc , description ) :
2012-01-20 11:15:45 +08:00
self . pc = pc
2012-04-04 05:35:43 +08:00
self . description = description
self . index = index
2016-09-07 04:57:50 +08:00
2012-01-20 11:15:45 +08:00
def __str__ ( self ) :
2012-04-04 05:35:43 +08:00
if self . description :
2016-09-07 04:57:50 +08:00
return " [ %3u ] 0x %16.16x %s " % (
self . index , self . pc , self . description )
2012-04-04 05:35:43 +08:00
else :
2012-05-04 02:46:28 +08:00
return " [ %3u ] 0x %16.16x " % ( self . index , self . pc )
def dump ( self , prefix ) :
2019-03-07 06:54:11 +08:00
print ( " %s %s " % ( prefix , str ( self ) ) )
2016-09-07 04:57:50 +08:00
2012-05-05 04:44:14 +08:00
class DarwinImage ( symbolication . Image ) :
2012-01-20 11:15:45 +08:00
""" Class that represents a binary images in a darwin crash log """
2019-04-18 05:51:55 +08:00
dsymForUUIDBinary = ' /usr/local/bin/dsymForUUID '
2012-01-20 14:12:47 +08:00
if not os . path . exists ( dsymForUUIDBinary ) :
2019-03-07 08:41:51 +08:00
try :
dsymForUUIDBinary = subprocess . check_output ( ' which dsymForUUID ' ,
2019-04-19 05:32:36 +08:00
shell = True ) . rstrip ( ' \n ' )
2019-03-07 08:41:51 +08:00
except :
dsymForUUIDBinary = " "
2016-09-07 04:57:50 +08:00
dwarfdump_uuid_regex = re . compile (
' UUID: ([-0-9a-fA-F]+) \ (([^ \ (]+) \ ) .* ' )
def __init__ (
self ,
text_addr_lo ,
text_addr_hi ,
identifier ,
version ,
uuid ,
path ) :
symbolication . Image . __init__ ( self , path , uuid )
self . add_section (
symbolication . Section (
text_addr_lo ,
text_addr_hi ,
" __TEXT " ) )
2012-04-04 05:35:43 +08:00
self . identifier = identifier
2012-01-20 11:15:45 +08:00
self . version = version
2016-09-07 04:57:50 +08:00
2018-12-18 01:25:57 +08:00
def find_matching_slice ( self ) :
2019-03-07 06:54:11 +08:00
dwarfdump_cmd_output = subprocess . check_output (
2019-05-30 08:35:43 +08:00
' dwarfdump --uuid " %s " ' % self . path , shell = True ) . decode ( " utf-8 " )
2018-12-18 01:25:57 +08:00
self_uuid = self . get_uuid ( )
for line in dwarfdump_cmd_output . splitlines ( ) :
match = self . dwarfdump_uuid_regex . search ( line )
if match :
dwarf_uuid_str = match . group ( 1 )
dwarf_uuid = uuid . UUID ( dwarf_uuid_str )
if self_uuid == dwarf_uuid :
self . resolved_path = self . path
self . arch = match . group ( 2 )
return True
if not self . resolved_path :
self . unavailable = True
2019-03-07 06:54:11 +08:00
print ( ( " error \n error: unable to locate ' %s ' with UUID %s "
% ( self . path , self . get_normalized_uuid_string ( ) ) ) )
2018-12-18 01:25:57 +08:00
return False
2012-04-04 05:35:43 +08:00
def locate_module_and_debug_symbols ( self ) :
2012-06-05 07:22:17 +08:00
# Don't load a module twice...
if self . resolved :
2012-04-21 07:31:27 +08:00
return True
2012-06-05 07:22:17 +08:00
# Mark this as resolved so we don't keep trying
self . resolved = True
2012-05-11 08:30:14 +08:00
uuid_str = self . get_normalized_uuid_string ( )
2019-03-07 06:54:11 +08:00
print ( ' Getting symbols for %s %s ... ' % ( uuid_str , self . path ) , end = ' ' )
2012-01-20 11:32:35 +08:00
if os . path . exists ( self . dsymForUUIDBinary ) :
2016-09-07 04:57:50 +08:00
dsym_for_uuid_command = ' %s %s ' % (
self . dsymForUUIDBinary , uuid_str )
2019-03-07 06:54:11 +08:00
s = subprocess . check_output ( dsym_for_uuid_command , shell = True )
2012-01-20 11:32:35 +08:00
if s :
2017-03-29 07:25:34 +08:00
try :
2019-05-18 09:57:12 +08:00
plist_root = read_plist ( s )
2017-03-29 07:25:34 +08:00
except :
2019-03-21 22:39:55 +08:00
print ( ( " Got exception: " , sys . exc_info ( ) [ 1 ] , " handling dsymForUUID output: \n " , s ) )
2017-03-29 07:25:34 +08:00
raise
2012-01-20 11:32:35 +08:00
if plist_root :
2012-05-11 08:30:14 +08:00
plist = plist_root [ uuid_str ]
2012-01-20 14:12:47 +08:00
if plist :
if ' DBGArchitecture ' in plist :
self . arch = plist [ ' DBGArchitecture ' ]
if ' DBGDSYMPath ' in plist :
2016-09-07 04:57:50 +08:00
self . symfile = os . path . realpath (
plist [ ' DBGDSYMPath ' ] )
2012-01-20 14:12:47 +08:00
if ' DBGSymbolRichExecutable ' in plist :
2016-09-07 04:57:50 +08:00
self . path = os . path . expanduser (
plist [ ' DBGSymbolRichExecutable ' ] )
2014-04-08 07:50:17 +08:00
self . resolved_path = self . path
2012-01-20 14:12:47 +08:00
if not self . resolved_path and os . path . exists ( self . path ) :
2018-12-18 01:25:57 +08:00
if not self . find_matching_slice ( ) :
2012-04-21 07:31:27 +08:00
return False
2018-12-18 01:25:57 +08:00
if not self . resolved_path and not os . path . exists ( self . path ) :
try :
dsym = subprocess . check_output (
[ " /usr/bin/mdfind " ,
" com_apple_xcode_dsym_uuids == %s " % uuid_str ] ) [ : - 1 ]
if dsym and os . path . exists ( dsym ) :
2019-03-07 06:54:11 +08:00
print ( ( ' falling back to binary inside " %s " ' % dsym ) )
2018-12-18 01:25:57 +08:00
self . symfile = dsym
dwarf_dir = os . path . join ( dsym , ' Contents/Resources/DWARF ' )
for filename in os . listdir ( dwarf_dir ) :
self . path = os . path . join ( dwarf_dir , filename )
if not self . find_matching_slice ( ) :
return False
break
except :
pass
2016-09-07 04:57:50 +08:00
if ( self . resolved_path and os . path . exists ( self . resolved_path ) ) or (
self . path and os . path . exists ( self . path ) ) :
2019-03-07 06:54:11 +08:00
print ( ' ok ' )
2012-04-21 07:31:27 +08:00
return True
2012-06-05 07:22:17 +08:00
else :
self . unavailable = True
2012-04-21 07:31:27 +08:00
return False
2016-09-07 04:57:50 +08:00
2012-01-20 11:15:45 +08:00
def __init__ ( self , path ) :
""" CrashLog constructor that take a path to a darwin crash log file """
2016-09-07 04:57:50 +08:00
symbolication . Symbolicator . __init__ ( self )
self . path = os . path . expanduser ( path )
2012-01-20 11:15:45 +08:00
self . info_lines = list ( )
self . system_profile = list ( )
self . threads = list ( )
2016-09-07 04:57:50 +08:00
self . backtraces = list ( ) # For application specific backtraces
self . idents = list ( ) # A list of the required identifiers for doing all stack backtraces
2012-01-20 11:15:45 +08:00
self . crashed_thread_idx = - 1
self . version = - 1
2012-01-21 12:26:24 +08:00
self . error = None
2015-03-06 06:53:06 +08:00
self . target = None
2016-09-07 04:57:50 +08:00
# With possible initial component of ~ or ~user replaced by that user's
# home directory.
2012-01-21 12:26:24 +08:00
try :
f = open ( self . path )
except IOError :
self . error = ' error: cannot open " %s " ' % self . path
return
2012-01-20 11:15:45 +08:00
self . file_lines = f . read ( ) . splitlines ( )
parse_mode = PARSE_MODE_NORMAL
thread = None
2015-03-06 06:53:06 +08:00
app_specific_backtrace = False
2012-01-20 11:15:45 +08:00
for line in self . file_lines :
# print line
line_len = len ( line )
if line_len == 0 :
if thread :
if parse_mode == PARSE_MODE_THREAD :
if thread . index == self . crashed_thread_idx :
thread . reason = ' '
if self . thread_exception :
thread . reason + = self . thread_exception
if self . thread_exception_data :
2015-03-06 06:53:06 +08:00
thread . reason + = " ( %s ) " % self . thread_exception_data
if app_specific_backtrace :
self . backtraces . append ( thread )
else :
self . threads . append ( thread )
2012-01-20 11:15:45 +08:00
thread = None
else :
2016-09-07 04:57:50 +08:00
# only append an extra empty line if the previous line
2012-01-20 11:15:45 +08:00
# in the info_lines wasn't empty
if len ( self . info_lines ) > 0 and len ( self . info_lines [ - 1 ] ) :
self . info_lines . append ( line )
parse_mode = PARSE_MODE_NORMAL
# print 'PARSE_MODE_NORMAL'
elif parse_mode == PARSE_MODE_NORMAL :
2016-09-07 04:57:50 +08:00
if line . startswith ( ' Process: ' ) :
( self . process_name , pid_with_brackets ) = line [
8 : ] . strip ( ) . split ( ' [ ' )
2012-01-20 11:15:45 +08:00
self . process_id = pid_with_brackets . strip ( ' [] ' )
2016-09-07 04:57:50 +08:00
elif line . startswith ( ' Path: ' ) :
2012-01-20 11:15:45 +08:00
self . process_path = line [ 5 : ] . strip ( )
2016-09-07 04:57:50 +08:00
elif line . startswith ( ' Identifier: ' ) :
2012-01-20 11:15:45 +08:00
self . process_identifier = line [ 11 : ] . strip ( )
2016-09-07 04:57:50 +08:00
elif line . startswith ( ' Version: ' ) :
2012-05-11 06:45:54 +08:00
version_string = line [ 8 : ] . strip ( )
matched_pair = re . search ( " (.+) \ ((.+) \ ) " , version_string )
if matched_pair :
self . process_version = matched_pair . group ( 1 )
2016-09-07 04:57:50 +08:00
self . process_compatability_version = matched_pair . group (
2 )
2012-05-11 06:45:54 +08:00
else :
self . process = version_string
self . process_compatability_version = version_string
2013-01-12 10:11:49 +08:00
elif self . parent_process_regex . search ( line ) :
2016-09-07 04:57:50 +08:00
parent_process_match = self . parent_process_regex . search (
line )
2013-01-12 10:11:49 +08:00
self . parent_process_name = parent_process_match . group ( 1 )
self . parent_process_id = parent_process_match . group ( 2 )
2016-09-07 04:57:50 +08:00
elif line . startswith ( ' Exception Type: ' ) :
2012-01-20 11:15:45 +08:00
self . thread_exception = line [ 15 : ] . strip ( )
continue
2016-09-07 04:57:50 +08:00
elif line . startswith ( ' Exception Codes: ' ) :
2012-01-20 11:15:45 +08:00
self . thread_exception_data = line [ 16 : ] . strip ( )
continue
2016-12-08 08:22:45 +08:00
elif line . startswith ( ' Exception Subtype: ' ) : # iOS
self . thread_exception_data = line [ 18 : ] . strip ( )
continue
2016-09-07 04:57:50 +08:00
elif line . startswith ( ' Crashed Thread: ' ) :
2012-01-20 11:15:45 +08:00
self . crashed_thread_idx = int ( line [ 15 : ] . strip ( ) . split ( ) [ 0 ] )
continue
2016-12-08 08:22:45 +08:00
elif line . startswith ( ' Triggered by Thread: ' ) : # iOS
self . crashed_thread_idx = int ( line [ 20 : ] . strip ( ) . split ( ) [ 0 ] )
continue
2016-09-07 04:57:50 +08:00
elif line . startswith ( ' Report Version: ' ) :
2012-01-20 11:15:45 +08:00
self . version = int ( line [ 15 : ] . strip ( ) )
continue
2016-09-07 04:57:50 +08:00
elif line . startswith ( ' System Profile: ' ) :
2012-01-20 11:15:45 +08:00
parse_mode = PARSE_MODE_SYSTEM
continue
2016-09-07 04:57:50 +08:00
elif ( line . startswith ( ' Interval Since Last Report: ' ) or
line . startswith ( ' Crashes Since Last Report: ' ) or
line . startswith ( ' Per-App Interval Since Last Report: ' ) or
line . startswith ( ' Per-App Crashes Since Last Report: ' ) or
line . startswith ( ' Sleep/Wake UUID: ' ) or
line . startswith ( ' Anonymous UUID: ' ) ) :
2012-01-20 11:15:45 +08:00
# ignore these
2016-09-07 04:57:50 +08:00
continue
elif line . startswith ( ' Thread ' ) :
thread_state_match = self . thread_state_regex . search ( line )
2012-01-20 11:15:45 +08:00
if thread_state_match :
2015-03-06 06:53:06 +08:00
app_specific_backtrace = False
2016-09-07 04:57:50 +08:00
thread_state_match = self . thread_regex . search ( line )
2012-01-20 11:15:45 +08:00
thread_idx = int ( thread_state_match . group ( 1 ) )
parse_mode = PARSE_MODE_THREGS
thread = self . threads [ thread_idx ]
else :
2016-09-07 04:57:50 +08:00
thread_match = self . thread_regex . search ( line )
2012-01-20 11:15:45 +08:00
if thread_match :
2015-03-06 06:53:06 +08:00
app_specific_backtrace = False
2012-01-20 11:15:45 +08:00
parse_mode = PARSE_MODE_THREAD
thread_idx = int ( thread_match . group ( 1 ) )
2015-03-06 06:53:06 +08:00
thread = CrashLog . Thread ( thread_idx , False )
2012-01-20 11:15:45 +08:00
continue
2016-09-07 04:57:50 +08:00
elif line . startswith ( ' Binary Images: ' ) :
2012-01-20 11:15:45 +08:00
parse_mode = PARSE_MODE_IMAGES
continue
2016-09-07 04:57:50 +08:00
elif line . startswith ( ' Application Specific Backtrace ' ) :
app_backtrace_match = self . app_backtrace_regex . search ( line )
2015-03-06 06:53:06 +08:00
if app_backtrace_match :
parse_mode = PARSE_MODE_THREAD
app_specific_backtrace = True
idx = int ( app_backtrace_match . group ( 1 ) )
thread = CrashLog . Thread ( idx , True )
2016-12-08 08:22:45 +08:00
elif line . startswith ( ' Last Exception Backtrace: ' ) : # iOS
parse_mode = PARSE_MODE_THREAD
app_specific_backtrace = True
idx = 1
thread = CrashLog . Thread ( idx , True )
2012-01-20 11:15:45 +08:00
self . info_lines . append ( line . strip ( ) )
elif parse_mode == PARSE_MODE_THREAD :
2016-09-07 04:57:50 +08:00
if line . startswith ( ' Thread ' ) :
2012-05-11 08:30:14 +08:00
continue
2019-06-12 22:46:37 +08:00
if self . null_frame_regex . search ( line ) :
print ( ' warning: thread parser ignored null-frame: " %s " ' % line )
continue
2012-01-20 11:15:45 +08:00
frame_match = self . frame_regex . search ( line )
if frame_match :
ident = frame_match . group ( 2 )
2012-07-13 11:19:35 +08:00
thread . add_ident ( ident )
2016-09-07 04:57:50 +08:00
if ident not in self . idents :
2012-01-20 11:15:45 +08:00
self . idents . append ( ident )
2016-09-07 04:57:50 +08:00
thread . frames . append ( CrashLog . Frame ( int ( frame_match . group ( 1 ) ) , int (
frame_match . group ( 3 ) , 0 ) , frame_match . group ( 4 ) ) )
2012-01-20 11:15:45 +08:00
else :
2019-03-07 06:54:11 +08:00
print ( ' error: frame regex failed for line: " %s " ' % line )
2012-01-20 11:15:45 +08:00
elif parse_mode == PARSE_MODE_IMAGES :
2016-09-07 04:57:50 +08:00
image_match = self . image_regex_uuid . search ( line )
2012-01-20 11:15:45 +08:00
if image_match :
2018-12-18 01:26:04 +08:00
( img_lo , img_hi , img_name , img_version ,
_ , img_uuid , img_path ) = image_match . groups ( )
image = CrashLog . DarwinImage ( int ( img_lo , 0 ) , int ( img_hi , 0 ) ,
img_name . strip ( ) ,
img_version . strip ( )
if img_version else " " ,
uuid . UUID ( img_uuid ) , img_path )
2016-09-07 04:57:50 +08:00
self . images . append ( image )
2012-01-20 11:15:45 +08:00
else :
2019-03-07 06:54:11 +08:00
print ( " error: image regex failed for: %s " % line )
2012-01-20 11:15:45 +08:00
elif parse_mode == PARSE_MODE_THREGS :
stripped_line = line . strip ( )
2012-08-29 07:46:12 +08:00
# "r12: 0x00007fff6b5939c8 r13: 0x0000000007000006 r14: 0x0000000000002a03 r15: 0x0000000000000c00"
2016-09-07 04:57:50 +08:00
reg_values = re . findall (
' ([a-zA-Z0-9]+: 0[Xx][0-9a-fA-F]+) * ' , stripped_line )
2012-01-20 11:15:45 +08:00
for reg_value in reg_values :
2016-09-07 04:57:50 +08:00
# print 'reg_value = "%s"' % reg_value
2012-01-20 11:15:45 +08:00
( reg , value ) = reg_value . split ( ' : ' )
2016-09-07 04:57:50 +08:00
# print 'reg = "%s"' % reg
# print 'value = "%s"' % value
2012-01-20 11:15:45 +08:00
thread . registers [ reg . strip ( ) ] = int ( value , 0 )
elif parse_mode == PARSE_MODE_SYSTEM :
self . system_profile . append ( line )
f . close ( )
2016-09-07 04:57:50 +08:00
2012-01-20 11:15:45 +08:00
def dump ( self ) :
2019-03-07 06:54:11 +08:00
print ( " Crash Log File: %s " % ( self . path ) )
2015-03-06 06:53:06 +08:00
if self . backtraces :
2019-03-07 06:54:11 +08:00
print ( " \n Application Specific Backtraces: " )
2015-03-06 06:53:06 +08:00
for thread in self . backtraces :
thread . dump ( ' ' )
2019-03-07 06:54:11 +08:00
print ( " \n Threads: " )
2012-01-20 11:15:45 +08:00
for thread in self . threads :
thread . dump ( ' ' )
2019-03-07 06:54:11 +08:00
print ( " \n Images: " )
2012-01-20 11:15:45 +08:00
for image in self . images :
image . dump ( ' ' )
2016-09-07 04:57:50 +08:00
2012-04-04 05:35:43 +08:00
def find_image_with_identifier ( self , identifier ) :
2012-01-20 11:15:45 +08:00
for image in self . images :
2012-04-04 05:35:43 +08:00
if image . identifier == identifier :
2016-09-07 04:57:50 +08:00
return image
2016-06-11 04:09:33 +08:00
regex_text = ' ^.* \ . %s $ ' % ( re . escape ( identifier ) )
2015-03-06 06:53:06 +08:00
regex = re . compile ( regex_text )
for image in self . images :
if regex . match ( image . identifier ) :
2012-01-20 11:15:45 +08:00
return image
return None
2016-09-07 04:57:50 +08:00
2012-01-21 03:25:32 +08:00
def create_target ( self ) :
2016-09-07 04:57:50 +08:00
# print 'crashlog.create_target()...'
2015-03-06 06:53:06 +08:00
if self . target is None :
self . target = symbolication . Symbolicator . create_target ( self )
if self . target :
return self . target
2016-09-07 04:57:50 +08:00
# We weren't able to open the main executable as, but we can still
# symbolicate
2019-03-07 06:54:11 +08:00
print ( ' crashlog.create_target()...2 ' )
2015-03-06 06:53:06 +08:00
if self . idents :
for ident in self . idents :
2016-09-07 04:57:50 +08:00
image = self . find_image_with_identifier ( ident )
2015-03-06 06:53:06 +08:00
if image :
2016-09-07 04:57:50 +08:00
self . target = image . create_target ( )
2015-03-06 06:53:06 +08:00
if self . target :
2016-09-07 04:57:50 +08:00
return self . target # success
2019-03-07 06:54:11 +08:00
print ( ' crashlog.create_target()...3 ' )
2015-03-06 06:53:06 +08:00
for image in self . images :
2016-09-07 04:57:50 +08:00
self . target = image . create_target ( )
2015-03-06 06:53:06 +08:00
if self . target :
2016-09-07 04:57:50 +08:00
return self . target # success
2019-03-07 06:54:11 +08:00
print ( ' crashlog.create_target()...4 ' )
print ( ' error: unable to locate any executables from the crash log ' )
2015-03-06 06:53:06 +08:00
return self . target
2016-09-07 04:57:50 +08:00
2015-03-06 06:53:06 +08:00
def get_target ( self ) :
return self . target
2012-01-20 11:15:45 +08:00
2016-09-07 04:57:50 +08:00
2012-01-20 11:15:45 +08:00
def usage ( ) :
2019-03-07 06:54:11 +08:00
print ( " Usage: lldb-symbolicate.py [-n name] executable-image " )
2012-01-20 11:15:45 +08:00
sys . exit ( 0 )
2016-09-07 04:57:50 +08:00
2012-05-05 04:44:14 +08:00
class Interactive ( cmd . Cmd ) :
''' Interactive prompt for analyzing one or more Darwin crash logs, type " help " to see a list of supported commands. '''
image_option_parser = None
2016-09-07 04:57:50 +08:00
2012-05-05 04:44:14 +08:00
def __init__ ( self , crash_logs ) :
cmd . Cmd . __init__ ( self )
2012-07-04 05:40:18 +08:00
self . use_rawinput = False
2012-05-05 04:44:14 +08:00
self . intro = ' Interactive crashlogs prompt, type " help " to see a list of supported commands. '
self . crash_logs = crash_logs
self . prompt = ' % '
def default ( self , line ) :
''' Catch all for unknown command, which will exit the interpreter. '''
2019-03-07 06:54:11 +08:00
print ( " uknown command: %s " % line )
2012-05-05 04:44:14 +08:00
return True
def do_q ( self , line ) :
''' Quit command '''
return True
def do_quit ( self , line ) :
''' Quit command '''
return True
2012-06-01 05:21:08 +08:00
def do_symbolicate ( self , line ) :
2016-09-07 04:57:50 +08:00
description = ''' Symbolicate one or more darwin crash log files by index to provide source file and line information,
2012-06-01 05:21:08 +08:00
inlined stack frames back to the concrete functions , and disassemble the location of the crash
for the first frame of the crashed thread . '''
2016-09-07 04:57:50 +08:00
option_parser = CreateSymbolicateCrashLogOptions (
' symbolicate ' , description , False )
2012-06-01 05:21:08 +08:00
command_args = shlex . split ( line )
try :
( options , args ) = option_parser . parse_args ( command_args )
except :
return
2012-07-17 04:40:20 +08:00
if args :
# We have arguments, they must valid be crash log file indexes
for idx_str in args :
idx = int ( idx_str )
if idx < len ( self . crash_logs ) :
2016-09-07 04:57:50 +08:00
SymbolicateCrashLog ( self . crash_logs [ idx ] , options )
2012-07-17 04:40:20 +08:00
else :
2019-03-07 06:54:11 +08:00
print ( ' error: crash log index %u is out of range ' % ( idx ) )
2012-07-17 04:40:20 +08:00
else :
2016-09-07 04:57:50 +08:00
# No arguments, symbolicate all crash logs using the options
# provided
2012-07-17 04:40:20 +08:00
for idx in range ( len ( self . crash_logs ) ) :
2016-09-07 04:57:50 +08:00
SymbolicateCrashLog ( self . crash_logs [ idx ] , options )
2012-05-05 04:44:14 +08:00
def do_list ( self , line = None ) :
''' Dump a list of all crash logs that are currently loaded.
2016-09-07 04:57:50 +08:00
2012-05-05 04:44:14 +08:00
USAGE : list '''
2019-03-07 06:54:11 +08:00
print ( ' %u crash logs are loaded: ' % len ( self . crash_logs ) )
2012-05-05 04:44:14 +08:00
for ( crash_log_idx , crash_log ) in enumerate ( self . crash_logs ) :
2019-03-07 06:54:11 +08:00
print ( ' [ %u ] = %s ' % ( crash_log_idx , crash_log . path ) )
2012-05-05 04:44:14 +08:00
def do_image ( self , line ) :
2012-07-17 04:40:20 +08:00
''' Dump information about one or more binary images in the crash log given an image basename, or all images if no arguments are provided. '''
2012-05-05 04:44:14 +08:00
usage = " usage: % prog [options] <PATH> [PATH ...] "
2016-09-07 04:57:50 +08:00
description = ''' Dump information about one or more images in all crash logs. The <PATH> can be a full path, image basename, or partial path. Searches are done in this order. '''
2012-05-05 04:44:14 +08:00
command_args = shlex . split ( line )
if not self . image_option_parser :
2016-09-07 04:57:50 +08:00
self . image_option_parser = optparse . OptionParser (
description = description , prog = ' image ' , usage = usage )
self . image_option_parser . add_option (
' -a ' ,
' --all ' ,
action = ' store_true ' ,
help = ' show all images ' ,
default = False )
2012-05-05 04:44:14 +08:00
try :
( options , args ) = self . image_option_parser . parse_args ( command_args )
except :
return
2016-09-07 04:57:50 +08:00
2012-05-11 08:30:14 +08:00
if args :
for image_path in args :
fullpath_search = image_path [ 0 ] == ' / '
2012-07-17 04:40:20 +08:00
for ( crash_log_idx , crash_log ) in enumerate ( self . crash_logs ) :
2012-05-11 08:30:14 +08:00
matches_found = 0
for ( image_idx , image ) in enumerate ( crash_log . images ) :
if fullpath_search :
if image . get_resolved_path ( ) == image_path :
matches_found + = 1
2019-03-07 06:54:11 +08:00
print ( ' [ %u ] ' % ( crash_log_idx ) , image )
2012-05-11 08:30:14 +08:00
else :
image_basename = image . get_resolved_path_basename ( )
if image_basename == image_path :
matches_found + = 1
2019-03-07 06:54:11 +08:00
print ( ' [ %u ] ' % ( crash_log_idx ) , image )
2012-05-11 08:30:14 +08:00
if matches_found == 0 :
for ( image_idx , image ) in enumerate ( crash_log . images ) :
2012-05-17 04:49:19 +08:00
resolved_image_path = image . get_resolved_path ( )
2016-09-07 04:57:50 +08:00
if resolved_image_path and string . find (
image . get_resolved_path ( ) , image_path ) > = 0 :
2019-03-07 06:54:11 +08:00
print ( ' [ %u ] ' % ( crash_log_idx ) , image )
2012-05-11 08:30:14 +08:00
else :
2012-05-05 04:44:14 +08:00
for crash_log in self . crash_logs :
for ( image_idx , image ) in enumerate ( crash_log . images ) :
2019-03-07 06:54:11 +08:00
print ( ' [ %u ] %s ' % ( image_idx , image ) )
2012-05-05 04:44:14 +08:00
return False
def interactive_crashlogs ( options , args ) :
crash_log_files = list ( )
for arg in args :
for resolved_path in glob . glob ( arg ) :
crash_log_files . append ( resolved_path )
2016-09-07 04:57:50 +08:00
crash_logs = list ( )
2012-05-05 04:44:14 +08:00
for crash_log_file in crash_log_files :
2016-09-07 04:57:50 +08:00
# print 'crash_log_file = "%s"' % crash_log_file
2012-05-05 04:44:14 +08:00
crash_log = CrashLog ( crash_log_file )
if crash_log . error :
2019-03-07 06:54:11 +08:00
print ( crash_log . error )
2012-05-05 04:44:14 +08:00
continue
2012-06-29 02:10:14 +08:00
if options . debug :
2012-05-05 04:44:14 +08:00
crash_log . dump ( )
if not crash_log . images :
2019-03-07 06:54:11 +08:00
print ( ' error: no images in crash log " %s " ' % ( crash_log ) )
2012-05-05 04:44:14 +08:00
continue
else :
crash_logs . append ( crash_log )
2016-09-07 04:57:50 +08:00
2012-05-05 04:44:14 +08:00
interpreter = Interactive ( crash_logs )
# List all crash logs that were imported
interpreter . do_list ( )
interpreter . cmdloop ( )
2016-09-07 04:57:50 +08:00
2012-06-28 04:02:04 +08:00
2017-10-12 10:21:41 +08:00
def save_crashlog ( debugger , command , exe_ctx , result , dict ) :
2012-06-28 04:02:04 +08:00
usage = " usage: % prog [options] <output-path> "
2016-09-07 04:57:50 +08:00
description = ''' Export the state of current target into a crashlog file '''
parser = optparse . OptionParser (
description = description ,
prog = ' save_crashlog ' ,
usage = usage )
parser . add_option (
' -v ' ,
' --verbose ' ,
action = ' store_true ' ,
dest = ' verbose ' ,
help = ' display verbose debug info ' ,
default = False )
2012-06-28 04:02:04 +08:00
try :
( options , args ) = parser . parse_args ( shlex . split ( command ) )
except :
2016-09-07 04:57:50 +08:00
result . PutCString ( " error: invalid options " )
2012-06-28 04:02:04 +08:00
return
if len ( args ) != 1 :
2016-09-07 04:57:50 +08:00
result . PutCString (
" error: invalid arguments, a single output file is the only valid argument " )
2012-06-28 04:02:04 +08:00
return
out_file = open ( args [ 0 ] , ' w ' )
if not out_file :
2016-09-07 04:57:50 +08:00
result . PutCString (
" error: failed to open file ' %s ' for writing... " ,
args [ 0 ] )
2012-06-28 04:02:04 +08:00
return
2017-10-12 10:21:41 +08:00
target = exe_ctx . target
2015-06-24 04:26:45 +08:00
if target :
identifier = target . executable . basename
2017-10-12 10:21:41 +08:00
process = exe_ctx . process
if process :
pid = process . id
2012-06-28 04:02:04 +08:00
if pid != lldb . LLDB_INVALID_PROCESS_ID :
2016-09-07 04:57:50 +08:00
out_file . write (
' Process: %s [ %u ] \n ' %
( identifier , pid ) )
2015-06-24 04:26:45 +08:00
out_file . write ( ' Path: %s \n ' % ( target . executable . fullpath ) )
2012-06-28 04:02:04 +08:00
out_file . write ( ' Identifier: %s \n ' % ( identifier ) )
2016-09-07 04:57:50 +08:00
out_file . write ( ' \n Date/Time: %s \n ' %
( datetime . datetime . now ( ) . strftime ( " % Y- % m- %d % H: % M: % S " ) ) )
out_file . write (
' OS Version: Mac OS X %s ( %s ) \n ' %
2019-03-07 06:54:11 +08:00
( platform . mac_ver ( ) [ 0 ] , subprocess . check_output ( ' sysctl -n kern.osversion ' , shell = True ) ) )
2012-06-28 04:02:04 +08:00
out_file . write ( ' Report Version: 9 \n ' )
2017-10-12 10:21:41 +08:00
for thread_idx in range ( process . num_threads ) :
thread = process . thread [ thread_idx ]
2012-06-28 04:02:04 +08:00
out_file . write ( ' \n Thread %u : \n ' % ( thread_idx ) )
for ( frame_idx , frame ) in enumerate ( thread . frames ) :
frame_pc = frame . pc
frame_offset = 0
if frame . function :
block = frame . GetFrameBlock ( )
block_range = block . range [ frame . addr ]
if block_range :
block_start_addr = block_range [ 0 ]
frame_offset = frame_pc - block_start_addr . load_addr
else :
frame_offset = frame_pc - frame . function . addr . load_addr
elif frame . symbol :
frame_offset = frame_pc - frame . symbol . addr . load_addr
2016-09-07 04:57:50 +08:00
out_file . write (
' %-3u %-32s 0x %16.16x %s ' %
( frame_idx , frame . module . file . basename , frame_pc , frame . name ) )
if frame_offset > 0 :
2012-06-28 04:02:04 +08:00
out_file . write ( ' + %u ' % ( frame_offset ) )
line_entry = frame . line_entry
if line_entry :
if options . verbose :
# This will output the fullpath + line + column
out_file . write ( ' %s ' % ( line_entry ) )
else :
2016-09-07 04:57:50 +08:00
out_file . write (
' %s : %u ' %
( line_entry . file . basename , line_entry . line ) )
2012-06-28 04:02:04 +08:00
column = line_entry . column
2016-09-07 04:57:50 +08:00
if column :
2012-06-28 04:02:04 +08:00
out_file . write ( ' : %u ' % ( column ) )
out_file . write ( ' \n ' )
2016-09-07 04:57:50 +08:00
2012-06-28 04:02:04 +08:00
out_file . write ( ' \n Binary Images: \n ' )
2015-06-24 04:26:45 +08:00
for module in target . modules :
2012-06-28 04:02:04 +08:00
text_segment = module . section [ ' __TEXT ' ]
if text_segment :
2015-06-24 04:26:45 +08:00
text_segment_load_addr = text_segment . GetLoadAddress ( target )
2012-06-28 04:02:04 +08:00
if text_segment_load_addr != lldb . LLDB_INVALID_ADDRESS :
text_segment_end_load_addr = text_segment_load_addr + text_segment . size
identifier = module . file . basename
module_version = ' ??? '
module_version_array = module . GetVersion ( )
if module_version_array :
2016-09-07 04:57:50 +08:00
module_version = ' . ' . join (
map ( str , module_version_array ) )
out_file . write (
' 0x %16.16x - 0x %16.16x %s ( %s - ???) < %s > %s \n ' %
( text_segment_load_addr ,
text_segment_end_load_addr ,
identifier ,
module_version ,
module . GetUUIDString ( ) ,
module . file . fullpath ) )
2012-06-28 04:02:04 +08:00
out_file . close ( )
else :
2016-09-07 04:57:50 +08:00
result . PutCString ( " error: invalid target " )
2012-01-20 11:15:45 +08:00
def Symbolicate ( debugger , command , result , dict ) :
2012-01-21 12:26:24 +08:00
try :
2016-09-07 04:57:50 +08:00
SymbolicateCrashLogs ( shlex . split ( command ) )
2012-01-21 12:26:24 +08:00
except :
2016-09-07 04:57:50 +08:00
result . PutCString ( " error: python exception %s " % sys . exc_info ( ) [ 0 ] )
2012-06-01 05:21:08 +08:00
def SymbolicateCrashLog ( crash_log , options ) :
if crash_log . error :
2019-03-07 06:54:11 +08:00
print ( crash_log . error )
2012-06-01 05:21:08 +08:00
return
2012-06-29 02:10:14 +08:00
if options . debug :
2012-06-01 05:21:08 +08:00
crash_log . dump ( )
if not crash_log . images :
2019-03-07 06:54:11 +08:00
print ( ' error: no images in crash log ' )
2012-06-01 05:21:08 +08:00
return
<rdar://problem/11757916>
Make breakpoint setting by file and line much more efficient by only looking for inlined breakpoint locations if we are setting a breakpoint in anything but a source implementation file. Implementing this complex for a many reasons. Turns out that parsing compile units lazily had some issues with respect to how we need to do things with DWARF in .o files. So the fixes in the checkin for this makes these changes:
- Add a new setting called "target.inline-breakpoint-strategy" which can be set to "never", "always", or "headers". "never" will never try and set any inlined breakpoints (fastest). "always" always looks for inlined breakpoint locations (slowest, but most accurate). "headers", which is the default setting, will only look for inlined breakpoint locations if the breakpoint is set in what are consudered to be header files, which is realy defined as "not in an implementation source file".
- modify the breakpoint setting by file and line to check the current "target.inline-breakpoint-strategy" setting and act accordingly
- Modify compile units to be able to get their language and other info lazily. This allows us to create compile units from the debug map and not have to fill all of the details in, and then lazily discover this information as we go on debuggging. This is needed to avoid parsing all .o files when setting breakpoints in implementation only files (no inlines). Otherwise we would need to parse the .o file, the object file (mach-o in our case) and the symbol file (DWARF in the object file) just to see what the compile unit was.
- modify the "SymbolFileDWARFDebugMap" to subclass lldb_private::Module so that the virtual "GetObjectFile()" and "GetSymbolVendor()" functions can be intercepted when the .o file contenst are later lazilly needed. Prior to this fix, when we first instantiated the "SymbolFileDWARFDebugMap" class, we would also make modules, object files and symbol files for every .o file in the debug map because we needed to fix up the sections in the .o files with information that is in the executable debug map. Now we lazily do this in the DebugMapModule::GetObjectFile()
Cleaned up header includes a bit as well.
llvm-svn: 162860
2012-08-30 05:13:06 +08:00
if options . dump_image_list :
2019-03-07 06:54:11 +08:00
print ( " Binary Images: " )
<rdar://problem/11757916>
Make breakpoint setting by file and line much more efficient by only looking for inlined breakpoint locations if we are setting a breakpoint in anything but a source implementation file. Implementing this complex for a many reasons. Turns out that parsing compile units lazily had some issues with respect to how we need to do things with DWARF in .o files. So the fixes in the checkin for this makes these changes:
- Add a new setting called "target.inline-breakpoint-strategy" which can be set to "never", "always", or "headers". "never" will never try and set any inlined breakpoints (fastest). "always" always looks for inlined breakpoint locations (slowest, but most accurate). "headers", which is the default setting, will only look for inlined breakpoint locations if the breakpoint is set in what are consudered to be header files, which is realy defined as "not in an implementation source file".
- modify the breakpoint setting by file and line to check the current "target.inline-breakpoint-strategy" setting and act accordingly
- Modify compile units to be able to get their language and other info lazily. This allows us to create compile units from the debug map and not have to fill all of the details in, and then lazily discover this information as we go on debuggging. This is needed to avoid parsing all .o files when setting breakpoints in implementation only files (no inlines). Otherwise we would need to parse the .o file, the object file (mach-o in our case) and the symbol file (DWARF in the object file) just to see what the compile unit was.
- modify the "SymbolFileDWARFDebugMap" to subclass lldb_private::Module so that the virtual "GetObjectFile()" and "GetSymbolVendor()" functions can be intercepted when the .o file contenst are later lazilly needed. Prior to this fix, when we first instantiated the "SymbolFileDWARFDebugMap" class, we would also make modules, object files and symbol files for every .o file in the debug map because we needed to fix up the sections in the .o files with information that is in the executable debug map. Now we lazily do this in the DebugMapModule::GetObjectFile()
Cleaned up header includes a bit as well.
llvm-svn: 162860
2012-08-30 05:13:06 +08:00
for image in crash_log . images :
if options . verbose :
2019-03-07 06:54:11 +08:00
print ( image . debug_dump ( ) )
<rdar://problem/11757916>
Make breakpoint setting by file and line much more efficient by only looking for inlined breakpoint locations if we are setting a breakpoint in anything but a source implementation file. Implementing this complex for a many reasons. Turns out that parsing compile units lazily had some issues with respect to how we need to do things with DWARF in .o files. So the fixes in the checkin for this makes these changes:
- Add a new setting called "target.inline-breakpoint-strategy" which can be set to "never", "always", or "headers". "never" will never try and set any inlined breakpoints (fastest). "always" always looks for inlined breakpoint locations (slowest, but most accurate). "headers", which is the default setting, will only look for inlined breakpoint locations if the breakpoint is set in what are consudered to be header files, which is realy defined as "not in an implementation source file".
- modify the breakpoint setting by file and line to check the current "target.inline-breakpoint-strategy" setting and act accordingly
- Modify compile units to be able to get their language and other info lazily. This allows us to create compile units from the debug map and not have to fill all of the details in, and then lazily discover this information as we go on debuggging. This is needed to avoid parsing all .o files when setting breakpoints in implementation only files (no inlines). Otherwise we would need to parse the .o file, the object file (mach-o in our case) and the symbol file (DWARF in the object file) just to see what the compile unit was.
- modify the "SymbolFileDWARFDebugMap" to subclass lldb_private::Module so that the virtual "GetObjectFile()" and "GetSymbolVendor()" functions can be intercepted when the .o file contenst are later lazilly needed. Prior to this fix, when we first instantiated the "SymbolFileDWARFDebugMap" class, we would also make modules, object files and symbol files for every .o file in the debug map because we needed to fix up the sections in the .o files with information that is in the executable debug map. Now we lazily do this in the DebugMapModule::GetObjectFile()
Cleaned up header includes a bit as well.
llvm-svn: 162860
2012-08-30 05:13:06 +08:00
else :
2019-03-07 06:54:11 +08:00
print ( image )
<rdar://problem/11757916>
Make breakpoint setting by file and line much more efficient by only looking for inlined breakpoint locations if we are setting a breakpoint in anything but a source implementation file. Implementing this complex for a many reasons. Turns out that parsing compile units lazily had some issues with respect to how we need to do things with DWARF in .o files. So the fixes in the checkin for this makes these changes:
- Add a new setting called "target.inline-breakpoint-strategy" which can be set to "never", "always", or "headers". "never" will never try and set any inlined breakpoints (fastest). "always" always looks for inlined breakpoint locations (slowest, but most accurate). "headers", which is the default setting, will only look for inlined breakpoint locations if the breakpoint is set in what are consudered to be header files, which is realy defined as "not in an implementation source file".
- modify the breakpoint setting by file and line to check the current "target.inline-breakpoint-strategy" setting and act accordingly
- Modify compile units to be able to get their language and other info lazily. This allows us to create compile units from the debug map and not have to fill all of the details in, and then lazily discover this information as we go on debuggging. This is needed to avoid parsing all .o files when setting breakpoints in implementation only files (no inlines). Otherwise we would need to parse the .o file, the object file (mach-o in our case) and the symbol file (DWARF in the object file) just to see what the compile unit was.
- modify the "SymbolFileDWARFDebugMap" to subclass lldb_private::Module so that the virtual "GetObjectFile()" and "GetSymbolVendor()" functions can be intercepted when the .o file contenst are later lazilly needed. Prior to this fix, when we first instantiated the "SymbolFileDWARFDebugMap" class, we would also make modules, object files and symbol files for every .o file in the debug map because we needed to fix up the sections in the .o files with information that is in the executable debug map. Now we lazily do this in the DebugMapModule::GetObjectFile()
Cleaned up header includes a bit as well.
llvm-svn: 162860
2012-08-30 05:13:06 +08:00
2016-09-07 04:57:50 +08:00
target = crash_log . create_target ( )
2012-06-01 05:21:08 +08:00
if not target :
return
exe_module = target . GetModuleAtIndex ( 0 )
images_to_load = list ( )
loaded_images = list ( )
if options . load_all_images :
# --load-all option was specified, load everything up
for image in crash_log . images :
images_to_load . append ( image )
else :
# Only load the images found in stack frames for the crashed threads
2012-07-13 11:19:35 +08:00
if options . crashed_only :
for thread in crash_log . threads :
if thread . did_crash ( ) :
for ident in thread . idents :
2016-09-07 04:57:50 +08:00
images = crash_log . find_images_with_identifier ( ident )
2012-07-13 11:19:35 +08:00
if images :
for image in images :
images_to_load . append ( image )
else :
2019-03-07 06:54:11 +08:00
print ( ' error: can \' t find image for identifier " %s " ' % ident )
2012-07-13 11:19:35 +08:00
else :
for ident in crash_log . idents :
2016-09-07 04:57:50 +08:00
images = crash_log . find_images_with_identifier ( ident )
2012-07-13 11:19:35 +08:00
if images :
for image in images :
images_to_load . append ( image )
else :
2019-03-07 06:54:11 +08:00
print ( ' error: can \' t find image for identifier " %s " ' % ident )
2012-06-01 05:21:08 +08:00
for image in images_to_load :
2016-09-07 04:57:50 +08:00
if image not in loaded_images :
err = image . add_module ( target )
2012-06-01 05:21:08 +08:00
if err :
2019-03-07 06:54:11 +08:00
print ( err )
2012-06-01 05:21:08 +08:00
else :
2016-09-07 04:57:50 +08:00
# print 'loaded %s' % image
2012-06-01 05:21:08 +08:00
loaded_images . append ( image )
2015-03-06 06:53:06 +08:00
if crash_log . backtraces :
for thread in crash_log . backtraces :
2016-09-07 04:57:50 +08:00
thread . dump_symbolicated ( crash_log , options )
2019-03-07 06:54:11 +08:00
print ( )
2015-03-06 06:53:06 +08:00
2012-06-01 05:21:08 +08:00
for thread in crash_log . threads :
2016-09-07 04:57:50 +08:00
thread . dump_symbolicated ( crash_log , options )
2019-03-07 06:54:11 +08:00
print ( )
2012-06-01 05:21:08 +08:00
2016-09-07 04:57:50 +08:00
def CreateSymbolicateCrashLogOptions (
command_name ,
description ,
add_interactive_options ) :
2012-01-21 08:37:19 +08:00
usage = " usage: % prog [options] <FILE> [FILE ...] "
2016-09-07 04:57:50 +08:00
option_parser = optparse . OptionParser (
description = description , prog = ' crashlog ' , usage = usage )
option_parser . add_option (
' --verbose ' ,
' -v ' ,
action = ' store_true ' ,
dest = ' verbose ' ,
help = ' display verbose debug info ' ,
default = False )
option_parser . add_option (
' --debug ' ,
' -g ' ,
action = ' store_true ' ,
dest = ' debug ' ,
help = ' display verbose debug logging ' ,
default = False )
option_parser . add_option (
' --load-all ' ,
' -a ' ,
action = ' store_true ' ,
dest = ' load_all_images ' ,
help = ' load all executable images, not just the images found in the crashed stack frames ' ,
default = False )
option_parser . add_option (
' --images ' ,
action = ' store_true ' ,
dest = ' dump_image_list ' ,
help = ' show image list ' ,
default = False )
option_parser . add_option (
' --debug-delay ' ,
type = ' int ' ,
dest = ' debug_delay ' ,
metavar = ' NSEC ' ,
help = ' pause for NSEC seconds for debugger ' ,
default = 0 )
option_parser . add_option (
' --crashed-only ' ,
' -c ' ,
action = ' store_true ' ,
dest = ' crashed_only ' ,
help = ' only symbolicate the crashed thread ' ,
default = False )
option_parser . add_option (
' --disasm-depth ' ,
' -d ' ,
type = ' int ' ,
dest = ' disassemble_depth ' ,
help = ' set the depth in stack frames that should be disassembled (default is 1) ' ,
default = 1 )
option_parser . add_option (
' --disasm-all ' ,
' -D ' ,
action = ' store_true ' ,
dest = ' disassemble_all_threads ' ,
help = ' enabled disassembly of frames on all threads (not just the crashed thread) ' ,
default = False )
option_parser . add_option (
' --disasm-before ' ,
' -B ' ,
type = ' int ' ,
dest = ' disassemble_before ' ,
help = ' the number of instructions to disassemble before the frame PC ' ,
default = 4 )
option_parser . add_option (
' --disasm-after ' ,
' -A ' ,
type = ' int ' ,
dest = ' disassemble_after ' ,
help = ' the number of instructions to disassemble after the frame PC ' ,
default = 4 )
option_parser . add_option (
' --source-context ' ,
' -C ' ,
type = ' int ' ,
metavar = ' NLINES ' ,
dest = ' source_context ' ,
help = ' show NLINES source lines of source context (default = 4) ' ,
default = 4 )
option_parser . add_option (
' --source-frames ' ,
type = ' int ' ,
metavar = ' NFRAMES ' ,
dest = ' source_frames ' ,
help = ' show source for NFRAMES (default = 4) ' ,
default = 4 )
option_parser . add_option (
' --source-all ' ,
action = ' store_true ' ,
dest = ' source_all ' ,
help = ' show source for all threads, not just the crashed thread ' ,
default = False )
2012-06-01 05:21:08 +08:00
if add_interactive_options :
2016-09-07 04:57:50 +08:00
option_parser . add_option (
' -i ' ,
' --interactive ' ,
action = ' store_true ' ,
help = ' parse all crash logs and enter interactive mode ' ,
default = False )
2012-06-01 05:21:08 +08:00
return option_parser
2016-09-07 04:57:50 +08:00
2012-06-01 05:21:08 +08:00
def SymbolicateCrashLogs ( command_args ) :
2016-09-07 04:57:50 +08:00
description = ''' Symbolicate one or more darwin crash log files to provide source file and line information,
2012-01-21 08:37:19 +08:00
inlined stack frames back to the concrete functions , and disassemble the location of the crash
for the first frame of the crashed thread .
If this script is imported into the LLDB command interpreter , a " crashlog " command will be added to the interpreter
for use at the LLDB command line . After a crash log has been parsed and symbolicated , a target will have been
created that has all of the shared libraries loaded at the load addresses found in the crash log file . This allows
2016-09-07 04:57:50 +08:00
you to explore the program as if it were stopped at the locations described in the crash log and functions can
2012-01-21 08:37:19 +08:00
be disassembled and lookups can be performed using the addresses found in the crash log . '''
2016-09-07 04:57:50 +08:00
option_parser = CreateSymbolicateCrashLogOptions (
' crashlog ' , description , True )
2012-01-21 12:26:24 +08:00
try :
2012-06-01 05:21:08 +08:00
( options , args ) = option_parser . parse_args ( command_args )
2012-01-21 12:26:24 +08:00
except :
return
2016-09-07 04:57:50 +08:00
2012-06-29 02:10:14 +08:00
if options . debug :
2019-03-07 06:54:11 +08:00
print ( ' command_args = %s ' % command_args )
print ( ' options ' , options )
print ( ' args ' , args )
2016-09-07 04:57:50 +08:00
2012-01-20 11:15:45 +08:00
if options . debug_delay > 0 :
2019-03-07 06:54:11 +08:00
print ( " Waiting %u seconds for debugger to attach... " % options . debug_delay )
2012-01-20 11:15:45 +08:00
time . sleep ( options . debug_delay )
error = lldb . SBError ( )
2016-09-07 04:57:50 +08:00
2012-01-21 03:25:32 +08:00
if args :
2012-05-05 04:44:14 +08:00
if options . interactive :
interactive_crashlogs ( options , args )
else :
for crash_log_file in args :
2012-06-05 07:22:17 +08:00
crash_log = CrashLog ( crash_log_file )
2016-09-07 04:57:50 +08:00
SymbolicateCrashLog ( crash_log , options )
2012-01-20 11:15:45 +08:00
if __name__ == ' __main__ ' :
2012-01-21 03:25:32 +08:00
# Create a new debugger instance
lldb . debugger = lldb . SBDebugger . Create ( )
2016-09-07 04:57:50 +08:00
SymbolicateCrashLogs ( sys . argv [ 1 : ] )
lldb . SBDebugger . Destroy ( lldb . debugger )
2012-05-04 06:31:30 +08:00
elif getattr ( lldb , ' debugger ' , None ) :
2016-09-07 04:57:50 +08:00
lldb . debugger . HandleCommand (
' command script add -f lldb.macosx.crashlog.Symbolicate crashlog ' )
lldb . debugger . HandleCommand (
' command script add -f lldb.macosx.crashlog.save_crashlog save_crashlog ' )
2019-03-07 06:54:11 +08:00
print ( ' " crashlog " and " save_crashlog " command installed, use the " --help " option for detailed help ' )