27
27
# ----------------------------------------------------------------------
28
28
29
29
import abc
30
+ import argparse
30
31
import concurrent .futures
31
32
import contextlib
32
33
import datetime
33
34
import json
34
- import optparse
35
35
import os
36
36
import platform
37
37
import plistlib
@@ -1234,12 +1234,20 @@ def usage():
1234
1234
1235
1235
1236
1236
def save_crashlog (debugger , command , exe_ctx , result , dict ):
1237
- usage = "usage: %prog [options] <output-path>"
1237
+ usage = "save_crashlog [options] <output-path>"
1238
1238
description = """Export the state of current target into a crashlog file"""
1239
- parser = optparse .OptionParser (
1240
- description = description , prog = "save_crashlog" , usage = usage
1239
+ parser = argparse .ArgumentParser (
1240
+ description = description ,
1241
+ prog = "save_crashlog" ,
1242
+ formatter_class = argparse .ArgumentDefaultsHelpFormatter ,
1241
1243
)
1242
- parser .add_option (
1244
+ parser .add_argument (
1245
+ "output" ,
1246
+ metavar = "output-file" ,
1247
+ type = argparse .FileType ("w" , encoding = "utf-8" ),
1248
+ nargs = 1 ,
1249
+ )
1250
+ parser .add_argument (
1243
1251
"-v" ,
1244
1252
"--verbose" ,
1245
1253
action = "store_true" ,
@@ -1248,21 +1256,13 @@ def save_crashlog(debugger, command, exe_ctx, result, dict):
1248
1256
default = False ,
1249
1257
)
1250
1258
try :
1251
- (options , args ) = parser .parse_args (shlex .split (command ))
1252
- except :
1253
- result .PutCString ("error: invalid options" )
1254
- return
1255
- if len (args ) != 1 :
1256
- result .PutCString (
1257
- "error: invalid arguments, a single output file is the only valid argument"
1258
- )
1259
- return
1260
- out_file = open (args [0 ], "w" , encoding = "utf-8" )
1261
- if not out_file :
1262
- result .PutCString ("error: failed to open file '%s' for writing..." , args [0 ])
1259
+ options = parser .parse_args (shlex .split (command ))
1260
+ except Exception as e :
1261
+ result .SetError (str (e ))
1263
1262
return
1264
1263
target = exe_ctx .target
1265
1264
if target :
1265
+ out_file = options .output
1266
1266
identifier = target .executable .basename
1267
1267
process = exe_ctx .process
1268
1268
if process :
@@ -1352,7 +1352,7 @@ def save_crashlog(debugger, command, exe_ctx, result, dict):
1352
1352
)
1353
1353
out_file .close ()
1354
1354
else :
1355
- result .PutCString ( "error: invalid target" )
1355
+ result .SetError ( " invalid target" )
1356
1356
1357
1357
1358
1358
class Symbolicate :
@@ -1366,8 +1366,8 @@ def get_short_help(self):
1366
1366
return "Symbolicate one or more darwin crash log files."
1367
1367
1368
1368
def get_long_help (self ):
1369
- option_parser = CrashLogOptionParser ()
1370
- return option_parser .format_help ()
1369
+ arg_parser = CrashLogOptionParser ()
1370
+ return arg_parser .format_help ()
1371
1371
1372
1372
1373
1373
def SymbolicateCrashLog (crash_log , options ):
@@ -1523,35 +1523,46 @@ def synchronous(debugger):
1523
1523
def CreateSymbolicateCrashLogOptions (
1524
1524
command_name , description , add_interactive_options
1525
1525
):
1526
- usage = "usage: %prog [options] <FILE> [FILE ...]"
1527
- option_parser = optparse .OptionParser (
1528
- description = description , prog = "crashlog" , usage = usage
1526
+ usage = "crashlog [options] <FILE> [FILE ...]"
1527
+ arg_parser = argparse .ArgumentParser (
1528
+ description = description ,
1529
+ prog = "crashlog" ,
1530
+ usage = usage ,
1531
+ formatter_class = argparse .ArgumentDefaultsHelpFormatter ,
1532
+ )
1533
+ arg_parser .add_argument (
1534
+ "reports" ,
1535
+ metavar = "FILE" ,
1536
+ type = str ,
1537
+ nargs = "*" ,
1538
+ help = "crash report(s) to symbolicate" ,
1529
1539
)
1530
- option_parser .add_option (
1540
+
1541
+ arg_parser .add_argument (
1531
1542
"--version" ,
1532
1543
"-V" ,
1533
1544
dest = "version" ,
1534
1545
action = "store_true" ,
1535
1546
help = "Show crashlog version" ,
1536
1547
default = False ,
1537
1548
)
1538
- option_parser . add_option (
1549
+ arg_parser . add_argument (
1539
1550
"--verbose" ,
1540
1551
"-v" ,
1541
1552
action = "store_true" ,
1542
1553
dest = "verbose" ,
1543
1554
help = "display verbose debug info" ,
1544
1555
default = False ,
1545
1556
)
1546
- option_parser . add_option (
1557
+ arg_parser . add_argument (
1547
1558
"--debug" ,
1548
1559
"-g" ,
1549
1560
action = "store_true" ,
1550
1561
dest = "debug" ,
1551
1562
help = "display verbose debug logging" ,
1552
1563
default = False ,
1553
1564
)
1554
- option_parser . add_option (
1565
+ arg_parser . add_argument (
1555
1566
"--load-all" ,
1556
1567
"-a" ,
1557
1568
action = "store_true" ,
@@ -1561,116 +1572,116 @@ def CreateSymbolicateCrashLogOptions(
1561
1572
"interactive mode." ,
1562
1573
default = False ,
1563
1574
)
1564
- option_parser . add_option (
1575
+ arg_parser . add_argument (
1565
1576
"--images" ,
1566
1577
action = "store_true" ,
1567
1578
dest = "dump_image_list" ,
1568
1579
help = "show image list" ,
1569
1580
default = False ,
1570
1581
)
1571
- option_parser . add_option (
1582
+ arg_parser . add_argument (
1572
1583
"--debug-delay" ,
1573
- type = " int" ,
1584
+ type = int ,
1574
1585
dest = "debug_delay" ,
1575
1586
metavar = "NSEC" ,
1576
1587
help = "pause for NSEC seconds for debugger" ,
1577
1588
default = 0 ,
1578
1589
)
1579
- option_parser . add_option (
1590
+ arg_parser . add_argument (
1580
1591
"--crashed-only" ,
1581
1592
"-c" ,
1582
1593
action = "store_true" ,
1583
1594
dest = "crashed_only" ,
1584
1595
help = "only symbolicate the crashed thread" ,
1585
1596
default = False ,
1586
1597
)
1587
- option_parser . add_option (
1598
+ arg_parser . add_argument (
1588
1599
"--disasm-depth" ,
1589
1600
"-d" ,
1590
- type = " int" ,
1601
+ type = int ,
1591
1602
dest = "disassemble_depth" ,
1592
- help = "set the depth in stack frames that should be disassembled (default is 1) " ,
1603
+ help = "set the depth in stack frames that should be disassembled" ,
1593
1604
default = 1 ,
1594
1605
)
1595
- option_parser . add_option (
1606
+ arg_parser . add_argument (
1596
1607
"--disasm-all" ,
1597
1608
"-D" ,
1598
1609
action = "store_true" ,
1599
1610
dest = "disassemble_all_threads" ,
1600
1611
help = "enabled disassembly of frames on all threads (not just the crashed thread)" ,
1601
1612
default = False ,
1602
1613
)
1603
- option_parser . add_option (
1614
+ arg_parser . add_argument (
1604
1615
"--disasm-before" ,
1605
1616
"-B" ,
1606
- type = " int" ,
1617
+ type = int ,
1607
1618
dest = "disassemble_before" ,
1608
1619
help = "the number of instructions to disassemble before the frame PC" ,
1609
1620
default = 4 ,
1610
1621
)
1611
- option_parser . add_option (
1622
+ arg_parser . add_argument (
1612
1623
"--disasm-after" ,
1613
1624
"-A" ,
1614
- type = " int" ,
1625
+ type = int ,
1615
1626
dest = "disassemble_after" ,
1616
1627
help = "the number of instructions to disassemble after the frame PC" ,
1617
1628
default = 4 ,
1618
1629
)
1619
- option_parser . add_option (
1630
+ arg_parser . add_argument (
1620
1631
"--source-context" ,
1621
1632
"-C" ,
1622
- type = " int" ,
1633
+ type = int ,
1623
1634
metavar = "NLINES" ,
1624
1635
dest = "source_context" ,
1625
- help = "show NLINES source lines of source context (default = 4) " ,
1636
+ help = "show NLINES source lines of source context" ,
1626
1637
default = 4 ,
1627
1638
)
1628
- option_parser . add_option (
1639
+ arg_parser . add_argument (
1629
1640
"--source-frames" ,
1630
- type = " int" ,
1641
+ type = int ,
1631
1642
metavar = "NFRAMES" ,
1632
1643
dest = "source_frames" ,
1633
- help = "show source for NFRAMES (default = 4) " ,
1644
+ help = "show source for NFRAMES" ,
1634
1645
default = 4 ,
1635
1646
)
1636
- option_parser . add_option (
1647
+ arg_parser . add_argument (
1637
1648
"--source-all" ,
1638
1649
action = "store_true" ,
1639
1650
dest = "source_all" ,
1640
1651
help = "show source for all threads, not just the crashed thread" ,
1641
1652
default = False ,
1642
1653
)
1643
1654
if add_interactive_options :
1644
- option_parser . add_option (
1655
+ arg_parser . add_argument (
1645
1656
"-i" ,
1646
1657
"--interactive" ,
1647
1658
action = "store_true" ,
1648
1659
help = "parse a crash log and load it in a ScriptedProcess" ,
1649
1660
default = False ,
1650
1661
)
1651
- option_parser . add_option (
1662
+ arg_parser . add_argument (
1652
1663
"-b" ,
1653
1664
"--batch" ,
1654
1665
action = "store_true" ,
1655
1666
help = "dump symbolicated stackframes without creating a debug session" ,
1656
1667
default = True ,
1657
1668
)
1658
- option_parser . add_option (
1669
+ arg_parser . add_argument (
1659
1670
"--target" ,
1660
1671
"-t" ,
1661
1672
dest = "target_path" ,
1662
1673
help = "the target binary path that should be used for interactive crashlog (optional)" ,
1663
1674
default = None ,
1664
1675
)
1665
- option_parser . add_option (
1676
+ arg_parser . add_argument (
1666
1677
"--skip-status" ,
1667
1678
"-s" ,
1668
1679
dest = "skip_status" ,
1669
1680
action = "store_true" ,
1670
1681
help = "prevent the interactive crashlog to dump the process status and thread backtrace at launch" ,
1671
1682
default = False ,
1672
1683
)
1673
- return option_parser
1684
+ return arg_parser
1674
1685
1675
1686
1676
1687
def CrashLogOptionParser ():
@@ -1686,15 +1697,16 @@ def CrashLogOptionParser():
1686
1697
1687
1698
1688
1699
def SymbolicateCrashLogs (debugger , command_args , result , is_command ):
1689
- option_parser = CrashLogOptionParser ()
1700
+ arg_parser = CrashLogOptionParser ()
1690
1701
1691
1702
if not len (command_args ):
1692
- option_parser .print_help ()
1703
+ arg_parser .print_help ()
1693
1704
return
1694
1705
1695
1706
try :
1696
- (options , args ) = option_parser .parse_args (command_args )
1697
- except :
1707
+ options = arg_parser .parse_args (command_args )
1708
+ except Exception as e :
1709
+ result .SetError (str (e ))
1698
1710
return
1699
1711
1700
1712
# Interactive mode requires running the crashlog command from inside lldb.
@@ -1724,7 +1736,7 @@ def SymbolicateCrashLogs(debugger, command_args, result, is_command):
1724
1736
if options .debug :
1725
1737
print ("command_args = %s" % command_args )
1726
1738
print ("options" , options )
1727
- print ("args" , args )
1739
+ print ("args" , options . reports )
1728
1740
1729
1741
if options .debug_delay > 0 :
1730
1742
print ("Waiting %u seconds for debugger to attach..." % options .debug_delay )
@@ -1743,8 +1755,8 @@ def should_run_in_interactive_mode(options, ci):
1743
1755
1744
1756
ci = debugger .GetCommandInterpreter ()
1745
1757
1746
- if args :
1747
- for crashlog_file in args :
1758
+ if options . reports :
1759
+ for crashlog_file in options . reports :
1748
1760
crashlog_path = os .path .expanduser (crashlog_file )
1749
1761
if not os .path .exists (crashlog_path ):
1750
1762
raise FileNotFoundError (
0 commit comments