readelf.py: minor enhancements for debugging (#294)
authorPierre-Marie de Rodat <pmderodat@kawie.fr>
Tue, 10 Mar 2020 12:46:46 +0000 (13:46 +0100)
committerGitHub <noreply@github.com>
Tue, 10 Mar 2020 12:46:46 +0000 (05:46 -0700)
* readelf.py: add an option to show traceback on error

* readelf.py: flush stdout before printing to sys.stderr

This is necessary to make error messages appear after any display that
was emitted before the error actually happened.

scripts/readelf.py

index 3a075fd640c8b028cce164664b39f5039a433663..2ff229a7c193c97dbf035d02687dcc288177e0e4 100755 (executable)
@@ -10,6 +10,7 @@
 import argparse
 import os, sys
 import string
+import traceback
 import itertools
 # Note: zip has different behaviour between Python 2.x and 3.x.
 # - Using izip ensures compatibility.
@@ -1396,6 +1397,10 @@ def main(stream=None):
             help=(
                 'Display the contents of DWARF debug sections. <what> can ' +
                 'one of {info,decodedline,frames,frames-interp}'))
+    argparser.add_argument('--traceback',
+                           action='store_true', dest='show_traceback',
+                           help='Dump the Python traceback on ELFError'
+                                ' exceptions from elftools')
 
     args = argparser.parse_args()
 
@@ -1440,7 +1445,10 @@ def main(stream=None):
             if args.debug_dump_what:
                 readelf.display_debug_dump(args.debug_dump_what)
         except ELFError as ex:
+            sys.stdout.flush()
             sys.stderr.write('ELF error: %s\n' % ex)
+            if args.show_traceback:
+                traceback.print_exc()
             sys.exit(1)