-
Notifications
You must be signed in to change notification settings - Fork 481
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add rtt control block into YAML config #1418
Open
crides
wants to merge
3
commits into
pyocd:main
Choose a base branch
from
crides:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
# pyOCD debugger | ||
# Copyright (c) 2022 crides | ||
# Copyright (c) 2021 mikisama | ||
# Copyright (C) 2021 Ciro Cattuto <[email protected]> | ||
# Copyright (C) 2021 Simon D. Levy <[email protected]> | ||
|
@@ -27,6 +28,7 @@ | |
from pyocd.utility.cmdline import convert_session_options, int_base_0 | ||
from pyocd.utility.kbhit import KBHit | ||
from ctypes import Structure, c_char, c_int32, c_uint32, sizeof | ||
from elftools.elf.elffile import ELFFile | ||
|
||
|
||
LOG = logging.getLogger(__name__) | ||
|
@@ -82,11 +84,13 @@ def get_args(cls) -> List[argparse.ArgumentParser]: | |
|
||
rtt_parser = argparse.ArgumentParser(cls.HELP, add_help=False) | ||
|
||
rtt_options = rtt_parser.add_argument_group("rtt options") | ||
rtt_options.add_argument("-a", "--address", type=int_base_0, default=None, | ||
rtt_search_opts = rtt_parser.add_argument_group("rtt options") | ||
rtt_search_opts.add_argument("-a", "--address", type=int_base_0, default=None, | ||
help="Start address of RTT control block search range.") | ||
rtt_options.add_argument("-s", "--size", type=int_base_0, default=None, | ||
rtt_search_opts.add_argument("-s", "--size", type=int_base_0, default=None, | ||
help="Size of RTT control block search range.") | ||
rtt_parser.add_argument("-e", "--elf", type=str, default=None, | ||
help="ELF file for finding the control block address directly") | ||
|
||
return [cls.CommonOptions.COMMON, cls.CommonOptions.CONNECT, rtt_parser] | ||
|
||
|
@@ -120,27 +124,48 @@ def invoke(self) -> int: | |
memory_map: MemoryMap = target.get_memory_map() | ||
ram_region: MemoryRegion = memory_map.get_default_region_of_type(MemoryType.RAM) | ||
|
||
if self._args.address is None or self._args.size is None: | ||
rtt_range_start = ram_region.start | ||
rtt_range_size = ram_region.length | ||
elif ram_region.start <= self._args.address and self._args.size <= ram_region.length: | ||
rtt_range_start = self._args.address | ||
rtt_range_size = self._args.size | ||
|
||
LOG.info(f"RTT control block search range [{rtt_range_start:#08x}, {rtt_range_size:#08x}]") | ||
|
||
rtt_cb_addr = -1 | ||
data = bytearray(b'0000000000') | ||
chunk_size = 1024 | ||
while rtt_range_size > 0: | ||
read_size = min(chunk_size, rtt_range_size) | ||
data += bytearray(target.read_memory_block8(rtt_range_start, read_size)) | ||
pos = data[-(read_size + 10):].find(b"SEGGER RTT") | ||
if pos != -1: | ||
rtt_cb_addr = rtt_range_start + pos - 10 | ||
break | ||
rtt_range_start += read_size | ||
rtt_range_size -= read_size | ||
|
||
elf_file_arg, elf_file_option = self._args.elf, session.options.get("rtt.elf") | ||
if elf_file_arg != None: | ||
elf_file = elf_file_arg | ||
elif elf_file_option != None: | ||
elf_file = elf_file_option | ||
else: | ||
elf_file = None | ||
|
||
if elf_file != None: | ||
elf = ELFFile(open(self._args.elf, "rb")) | ||
symtab = elf.get_section_by_name(".symtab") | ||
if symtab: | ||
rtt_entry = symtab.get_symbol_by_name("_SEGGER_RTT") | ||
if len(rtt_entry) > 0: | ||
rtt_cb_addr = rtt_entry[0].entry["st_value"] | ||
else: | ||
def valid_range(start, size): | ||
return start != None and size != None \ | ||
and ram_region.start <= start and size + start <= ram_region.start + ram_region.length | ||
config_start, config_size = session.options.get('rtt.start'), session.options.get('rtt.size') | ||
if valid_range(self._args.address, self._args.size): | ||
rtt_range_start, rtt_range_size = self._args.address, self._args.size | ||
elif valid_range(config_start, config_size): | ||
rtt_range_start, rtt_range_size = config_start, config_size | ||
else: | ||
rtt_range_start, rtt_range_size = ram_region.start, ram_region.length | ||
|
||
LOG.info(f"RTT control block search range [{rtt_range_start:#08x}, {rtt_range_size:#08x}]") | ||
|
||
data = bytearray(b'0000000000') | ||
chunk_size = 1024 | ||
while rtt_range_size > 0: | ||
read_size = min(chunk_size, rtt_range_size) | ||
data += bytearray(target.read_memory_block8(rtt_range_start, read_size)) | ||
pos = data[-(read_size + 10):].find(b"SEGGER RTT") | ||
if pos != -1: | ||
rtt_cb_addr = rtt_range_start + pos - 10 | ||
break | ||
rtt_range_start += read_size | ||
rtt_range_size -= read_size | ||
|
||
if rtt_cb_addr == -1: | ||
LOG.error("No RTT control block available") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,6 +63,7 @@ install_requires = | |
pyyaml>=6.0,<7.0 | ||
six>=1.15.0,<2.0 | ||
typing-extensions>=4.0,<5.0 | ||
elftools>=0.28 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This package doesn't exist. The package on PyPI is actually called |
||
|
||
[options.extras_require] | ||
pemicro = | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fyi, pyocd has some built-in support for ELF symbol extraction (and other things) in the
pyocd.debug.elf
sub-package. It's based on pyelftools, so for cases like this, where there is not much advantage, it's fine to directly use pyelftools. So no need to rewrite this.For reference, this is what this code would look like using pyocd's ELF tools: