Add option to show/hide debug info

Signed-off-by: Juraj Oravec <jurajoravec@mailo.com>
This commit is contained in:
Juraj Oravec 2024-05-12 21:53:15 +02:00
parent 9bd0cf4980
commit bd7cbef0c0
Signed by: SGOrava
GPG Key ID: 13660A3F1D9F093B

20
main.py
View File

@ -8,16 +8,23 @@ from dwex import formats
from elftools.dwarf.locationlists import LocationParser, LocationExpr from elftools.dwarf.locationlists import LocationParser, LocationExpr
from elftools.dwarf.dwarf_expr import DWARFExprParser, DWARFExprOp, DW_OP_opcode2name from elftools.dwarf.dwarf_expr import DWARFExprParser, DWARFExprOp, DW_OP_opcode2name
from dwex.dwarfone import DWARFExprParserV1 from dwex.dwarfone import DWARFExprParserV1
from pprint import pprint
SCRIPT_VERSION = '0.1.0' SCRIPT_VERSION = '0.1.0'
configuration = { configuration = {
'include_file_name': False, 'include_file_name': False,
'print_debug_info': False,
} }
def eprint(*args, **kwargs): def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs) if configuration['print_debug_info']:
print(*args, file=sys.stderr, **kwargs)
def epprint(*args, **kwargs):
if configuration['print_debug_info']:
pprint(*args, stream=sys.stderr, **kwargs)
class DWARFParseError(Exception): class DWARFParseError(Exception):
@ -171,6 +178,10 @@ class Bear():
for child_die in die_type._children: for child_die in die_type._children:
if 'DW_AT_type' in child_die.attributes: if 'DW_AT_type' in child_die.attributes:
typ_die = child_die.get_DIE_from_attribute('DW_AT_type') typ_die = child_die.get_DIE_from_attribute('DW_AT_type')
else:
eprint('Child DIE with no type information')
epprint(child_die)
continue
child_entry = dict() child_entry = dict()
child_entry['name'] = safe_DIE_name(child_die, '?') child_entry['name'] = safe_DIE_name(child_die, '?')
@ -201,6 +212,10 @@ class Bear():
for child_die in die_type._children: for child_die in die_type._children:
if 'DW_AT_type' in child_die.attributes: if 'DW_AT_type' in child_die.attributes:
typ_die = child_die.get_DIE_from_attribute('DW_AT_type') typ_die = child_die.get_DIE_from_attribute('DW_AT_type')
else:
eprint('Child DIE with no type information')
epprint(child_die)
continue
child_entry = dict() child_entry = dict()
child_entry['name'] = safe_DIE_name(child_die, '?') child_entry['name'] = safe_DIE_name(child_die, '?')
@ -310,12 +325,15 @@ def main():
help='ELF file to try to extract symbols') help='ELF file to try to extract symbols')
parser.add_argument('--include-file-name', dest='include_file_name', action='store_true', parser.add_argument('--include-file-name', dest='include_file_name', action='store_true',
help='Display filename at the beginning') help='Display filename at the beginning')
parser.add_argument('--print-debug-info', dest='print_debug_info', action='store_true',
help='Print debug infor when parsing DWARF')
parser.add_argument('--version', action='version', parser.add_argument('--version', action='version',
version='%(prog)s {version}'.format(version=SCRIPT_VERSION)) version='%(prog)s {version}'.format(version=SCRIPT_VERSION))
args = parser.parse_args() args = parser.parse_args()
configuration['include_file_name'] = args.include_file_name configuration['include_file_name'] = args.include_file_name
configuration['print_debug_info'] = args.print_debug_info
if not args.elf_file: if not args.elf_file:
parser.print_help() parser.print_help()