Skip to content

Commit

Permalink
testing/esp: simplify ocd and gdb classes
Browse files Browse the repository at this point in the history
  • Loading branch information
sobuch authored and erhankur committed Dec 12, 2024
1 parent c42a793 commit cb9d8ea
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 86 deletions.
47 changes: 14 additions & 33 deletions testing/esp/py_debug_backend/debug_backend/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,6 @@ class NoMatchingClassError(RuntimeError):
pass


def _look_for_subclasses(scope, chip_name, cls_obj):
if scope is None:
scope = globals()
for a in scope:
attr = scope[a]
if type(attr) is type and issubclass(attr, cls_obj) and chip_name == getattr(attr, 'chip_name'):
return attr
return None

def _parse_target_triple(target_triple):
# <arch><sub>-<vendor>-<sys>-<abi>
arch = vendor = sys = abi = 'unknown'
Expand All @@ -31,6 +22,14 @@ def _parse_target_triple(target_triple):
pass
return arch,vendor,sys,abi

def _get_num_cores(chip_name):
multi_core_chips = {
'esp32' : 2,
'esp32s3' : 2,
'esp32p4' : 2,
}
return 1 if chip_name not in multi_core_chips.keys() else multi_core_chips[chip_name]


def create_gdb(chip_name=None,
target_triple=None,
Expand Down Expand Up @@ -69,30 +68,19 @@ def create_gdb(chip_name=None,
gdb_init_args['log_stream_handler'] = log_stream_handler
if log_file_handler is not None:
gdb_init_args['log_file_handler'] = log_file_handler
# At first, for backward compatibility try to get subclass for `chip_name`
gdb_cls = _look_for_subclasses(scope, chip_name, Gdb)
if gdb_cls:
return gdb_cls(**gdb_init_args)
if target_triple:
# interpret `target_triple` as normal target triple like `xtensa-esp32s2-elf`
arch,vendor,sys,_ = _parse_target_triple(target_triple)
if sys == 'elf':
if arch == 'xtensa':
if vendor.startswith('esp'):
obj = GdbEspXtensa(**gdb_init_args)
obj.chip_name = chip_name
return obj
return GdbEspXtensa(**gdb_init_args)
elif arch.startswith('riscv32'):
if vendor.startswith('esp'):
obj = GdbEspRiscv32(**gdb_init_args)
obj.chip_name = chip_name
return obj
return GdbEspRiscv32(**gdb_init_args)
elif arch.startswith('riscv'):
if vendor.startswith('esp'):
obj = GdbEspRiscv(**gdb_init_args)
obj.chip_name = chip_name
return obj

return GdbEspRiscv(**gdb_init_args)
raise NoMatchingClassError("GDB class was not found for chip '%s' and target tripple '%s'" % (chip_name, target_triple))


Expand Down Expand Up @@ -143,22 +131,15 @@ def create_oocd(chip_name=None,
oocd_init_args['log_stream_handler'] = log_stream_handler
if log_file_handler is not None:
oocd_init_args['log_file_handler'] = log_file_handler
# At first, for backward compatibility try to get subclass for `chip_name`
oocd_cls = _look_for_subclasses(scope, chip_name, Oocd)
if oocd_cls:
return oocd_cls(**oocd_init_args)
if target_triple:
# interpret `target_triple` as normal target triple like `xtensa-esp32s2-elf`
arch,vendor,sys,_ = _parse_target_triple(target_triple)
cores_num = _get_num_cores(chip_name)
if sys == 'elf':
if arch == 'xtensa':
if vendor.startswith('esp'):
obj = OocdEspXtensa(**oocd_init_args)
obj.chip_name = chip_name
return obj
return OocdEspXtensa(**oocd_init_args, cores_num=cores_num)
elif arch.startswith('riscv'):
if vendor.startswith('esp'):
obj = OocdEspRiscv(**oocd_init_args)
obj.chip_name = chip_name
return obj
return OocdEspRiscv(**oocd_init_args, cores_num=cores_num)
raise NoMatchingClassError("OOCD class was not found for chip '%s' and target tripple '%s'" % (chip_name, target_triple))
45 changes: 0 additions & 45 deletions testing/esp/py_debug_backend/debug_backend/hw_specific/esp.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,51 +240,6 @@ def get_thread_info(self, thread_id=None):
self.halt()
return super(GdbEspXtensa, self).get_thread_info(thread_id)

class OocdEsp32(OocdEspXtensa):
"""
Class to communicate to OpenOCD supporting ESP32 specific features
"""
chip_name = 'esp32'

def __init__(self, oocd_exec=None, oocd_scripts=None, oocd_cfg_files=[], oocd_cfg_cmds=[], oocd_debug=2,
oocd_args=[], host='127.0.0.1', log_level=None, log_stream_handler=None, log_file_handler=None):
super(OocdEsp32, self).__init__(cores_num=2, oocd_exec=oocd_exec, oocd_scripts=oocd_scripts,
oocd_cfg_files=oocd_cfg_files, oocd_cfg_cmds=oocd_cfg_cmds,
oocd_debug=oocd_debug,
oocd_args=oocd_args, host=host, log_level=log_level,
log_stream_handler=log_stream_handler,
log_file_handler=log_file_handler)

class OocdEsp32s3(OocdEspXtensa):
"""
Class to communicate to OpenOCD supporting ESP32-S3 specific features
"""
chip_name = 'esp32s3'

def __init__(self, oocd_exec=None, oocd_scripts=None, oocd_cfg_files=[], oocd_cfg_cmds=[], oocd_debug=2,
oocd_args=[], host='127.0.0.1', log_level=None, log_stream_handler=None, log_file_handler=None):
super(OocdEsp32s3, self).__init__(cores_num=2, oocd_exec=oocd_exec, oocd_scripts=oocd_scripts,
oocd_cfg_files=oocd_cfg_files, oocd_cfg_cmds=oocd_cfg_cmds,
oocd_debug=oocd_debug,
oocd_args=oocd_args, host=host, log_level=log_level,
log_stream_handler=log_stream_handler,
log_file_handler=log_file_handler)

class OocdEsp32p4(OocdEspRiscv):
"""
Class to communicate to OpenOCD supporting ESP32-P4 specific features
"""
chip_name = 'esp32p4'

def __init__(self, oocd_exec=None, oocd_scripts=None, oocd_cfg_files=[], oocd_cfg_cmds=[], oocd_debug=2,
oocd_args=[], host='127.0.0.1', log_level=None, log_stream_handler=None, log_file_handler=None):
super(OocdEsp32p4, self).__init__(cores_num=2, oocd_exec=oocd_exec, oocd_scripts=oocd_scripts,
oocd_cfg_files=oocd_cfg_files, oocd_cfg_cmds=oocd_cfg_cmds,
oocd_debug=oocd_debug,
oocd_args=oocd_args, host=host, log_level=log_level,
log_stream_handler=log_stream_handler,
log_file_handler=log_file_handler)

class GdbEspRiscv(GdbEspImpl, GdbRiscv):
"""
Class to communicate to GDB supporting ESP RISCV-specific features
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ class OocdRiscv(Oocd):
"""
Class to communicate to OpenOCD supporting RISCV-specific features
"""
chip_name = 'riscv'

def __init__(self, oocd_exec=None, oocd_scripts=None, oocd_cfg_files=[], oocd_cfg_cmds=[],
oocd_debug=2, oocd_args=[], host='localhost', log_level=None, log_stream_handler=None, log_file_handler=None):
super(OocdRiscv, self).__init__(oocd_exec=oocd_exec, oocd_scripts=oocd_scripts,
Expand All @@ -21,8 +19,6 @@ class GdbRiscv(Gdb):
"""
Class to communicate to GDB supporting RISCV-specific features
"""
chip_name = 'riscv'

def __init__(self, gdb_path='gdb', remote_target=None, extended_remote_mode=False, gdb_log_folder=None,
log_level=None, log_stream_handler=None, log_file_handler=None):
super(GdbRiscv, self).__init__(gdb_path=gdb_path, remote_target=remote_target, extended_remote_mode=extended_remote_mode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ class OocdXtensa(Oocd):
"""
Class to communicate to OpenOCD supporting Xtensa-specific features
"""
chip_name = 'xtensa'

def __init__(self, oocd_exec=None, oocd_scripts=None, oocd_cfg_files=[], oocd_cfg_cmds=[],
oocd_debug=2, oocd_args=[], host='localhost', log_level=None, log_stream_handler=None, log_file_handler=None):
super(OocdXtensa, self).__init__(oocd_exec=oocd_exec, oocd_scripts=oocd_scripts,
Expand Down Expand Up @@ -63,8 +61,6 @@ class GdbXtensa(Gdb):
"""
Class to communicate to GDB supporting Xtensa-specific features
"""
chip_name = 'xtensa'

def __init__(self, gdb_path='gdb', remote_target=None, extended_remote_mode=False, gdb_log_folder=None,
log_level=None, log_stream_handler=None, log_file_handler=None):
super(GdbXtensa, self).__init__(gdb_path=gdb_path, remote_target=remote_target, extended_remote_mode=extended_remote_mode,
Expand Down

0 comments on commit cb9d8ea

Please sign in to comment.