Skip to content

Commit c3db83b

Browse files
nandojvecfriedt
authored andcommitted
scripts: runners: openocd: Enable thread awareness
Zephyr thread awareness is available for openocd but boards don't have debuggers configuration. This configure OpenOCD runner automatically to complete configuration. User still require enable CONFIG_DEBUG_THREAD_INFO=y to visualize thread debug information. Signed-off-by: Gerson Fernando Budke <[email protected]>
1 parent 0c2dabb commit c3db83b

File tree

6 files changed

+69
-19
lines changed

6 files changed

+69
-19
lines changed

boards/common/openocd-nrf5.board.cmake

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ set(pre_init_cmds
2727
"source [find interface/${OPENOCD_NRF5_INTERFACE}.cfg]"
2828
"transport select swd"
2929
"source [find target/${OPENOCD_NRF5_SUBFAMILY}.cfg]"
30-
"$_TARGETNAME configure -rtos auto"
3130
)
3231

3332
foreach(cmd ${pre_init_cmds})

cmake/flash/CMakeLists.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,10 @@ function(runners_yaml_append_config)
5353
endif()
5454
if(OPENOCD)
5555
runners_yaml_append(" openocd: ${OPENOCD}")
56-
endif()
57-
if(OPENOCD_DEFAULT_PATH)
58-
runners_yaml_append(" openocd_search: ${OPENOCD_DEFAULT_PATH}")
56+
runners_yaml_append(" openocd_search:")
57+
if(OPENOCD_DEFAULT_PATH)
58+
runners_yaml_append(" - ${OPENOCD_DEFAULT_PATH}")
59+
endif()
5960
endif()
6061
runners_yaml_append("")
6162
endfunction()

scripts/west_commands/run_common.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -356,8 +356,8 @@ def output_file(filetype):
356356

357357
return None
358358

359-
def config(attr):
360-
return getattr(args, attr, None) or yaml_config.get(attr)
359+
def config(attr, default=None):
360+
return getattr(args, attr, None) or yaml_config.get(attr, default)
361361

362362
return RunnerConfig(build_dir,
363363
yaml_config['board_dir'],
@@ -366,7 +366,7 @@ def config(attr):
366366
output_file('bin'),
367367
config('gdb'),
368368
config('openocd'),
369-
config('openocd_search'))
369+
config('openocd_search', []))
370370

371371
def dump_traceback():
372372
# Save the current exception to a file and return its path.

scripts/west_commands/runners/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ class RunnerConfig(NamedTuple):
260260
bin_file: Optional[str] # zephyr.bin path, or None
261261
gdb: Optional[str] = None # path to a usable gdb
262262
openocd: Optional[str] = None # path to a usable openocd
263-
openocd_search: Optional[str] = None # add this to openocd search path
263+
openocd_search: List[str] = [] # add these paths to the openocd search path
264264

265265

266266
_YN_CHOICES = ['Y', 'y', 'N', 'n', 'yes', 'no', 'YES', 'NO']

scripts/west_commands/runners/openocd.py

Lines changed: 60 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
'''Runner for openocd.'''
66

7+
import subprocess
8+
import re
9+
710
from os import path
811
from pathlib import Path
912

@@ -38,13 +41,14 @@ def __init__(self, cfg, pre_init=None, pre_load=None,
3841
self.openocd_config = config
3942

4043
search_args = []
41-
for i in self.openocd_config:
42-
if path.exists(i):
43-
search_args.append('-s')
44-
search_args.append(path.dirname(i))
45-
46-
if cfg.openocd_search is not None:
47-
search_args.extend(['-s', cfg.openocd_search])
44+
if self.openocd_config is not None:
45+
for i in self.openocd_config:
46+
if path.exists(i):
47+
search_args.append('-s')
48+
search_args.append(path.dirname(i))
49+
50+
for p in cfg.openocd_search:
51+
search_args.extend(['-s', p])
4852
self.openocd_cmd = [cfg.openocd] + search_args
4953
# openocd doesn't cope with Windows path names, so convert
5054
# them to POSIX style just to be sure.
@@ -117,16 +121,56 @@ def do_create(cls, cfg, args):
117121
tcl_port=args.tcl_port, telnet_port=args.telnet_port,
118122
gdb_port=args.gdb_port)
119123

124+
def print_gdbserver_message(self):
125+
if not self.thread_info_enabled:
126+
thread_msg = '; no thread info available'
127+
elif self.supports_thread_info():
128+
thread_msg = '; thread info enabled'
129+
else:
130+
thread_msg = '; update OpenOCD software for thread info'
131+
self.logger.info('OpenOCD GDB server running on port '
132+
f'{self.gdb_port}{thread_msg}')
133+
134+
# pylint: disable=R0201
135+
def to_num(self, number):
136+
dev_match = re.search(r"^\d*\+dev", number)
137+
dev_version = not dev_match is None
138+
139+
num_match = re.search(r"^\d*", number)
140+
num = int(num_match.group(0))
141+
142+
if dev_version:
143+
num += 1
144+
145+
return num
146+
147+
def read_version(self):
148+
self.require(self.openocd_cmd[0])
149+
150+
# OpenOCD prints in stderr, need redirect to get output
151+
out = self.check_output([self.openocd_cmd[0], '--version'],
152+
stderr=subprocess.STDOUT).decode()
153+
154+
return out.split('\n')[0]
155+
156+
def supports_thread_info(self):
157+
# Zephyr rtos was introduced after 0.11.0
158+
version_str = self.read_version().split(' ')[3]
159+
version = version_str.split('.')
160+
(major, minor, rev) = [self.to_num(i) for i in version]
161+
return (major, minor, rev) > (0, 11, 0)
162+
120163
def do_run(self, command, **kwargs):
121164
self.require(self.openocd_cmd[0])
122165
if ELFFile is None:
123166
raise RuntimeError(
124167
'elftools missing; please "pip3 install elftools"')
125168

126169
self.cfg_cmd = []
127-
for i in self.openocd_config:
128-
self.cfg_cmd.append('-f')
129-
self.cfg_cmd.append(i)
170+
if self.openocd_config is not None:
171+
for i in self.openocd_config:
172+
self.cfg_cmd.append('-f')
173+
self.cfg_cmd.append(i)
130174

131175
if command == 'flash' and self.use_elf:
132176
self.do_flash_elf(**kwargs)
@@ -213,6 +257,10 @@ def do_attach_debug(self, command, **kwargs):
213257
pre_init_cmd.append("-c")
214258
pre_init_cmd.append(i)
215259

260+
if self.thread_info_enabled and self.supports_thread_info():
261+
pre_init_cmd.append("-c")
262+
pre_init_cmd.append("$_TARGETNAME configure -rtos Zephyr")
263+
216264
server_cmd = (self.openocd_cmd + self.serial + self.cfg_cmd +
217265
['-c', 'tcl_port {}'.format(self.tcl_port),
218266
'-c', 'telnet_port {}'.format(self.telnet_port),
@@ -225,6 +273,7 @@ def do_attach_debug(self, command, **kwargs):
225273
if command == 'debug':
226274
gdb_cmd.extend(['-ex', 'load'])
227275
self.require(gdb_cmd[0])
276+
self.print_gdbserver_message()
228277
self.run_server_and_client(server_cmd, gdb_cmd)
229278

230279
def do_debugserver(self, **kwargs):
@@ -240,4 +289,5 @@ def do_debugserver(self, **kwargs):
240289
pre_init_cmd + ['-c', 'init',
241290
'-c', 'targets',
242291
'-c', 'reset halt'])
292+
self.print_gdbserver_message()
243293
self.check_call(cmd)

scripts/west_commands/tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
RC_KERNEL_BIN = 'test-zephyr.bin'
1616
RC_GDB = 'test-none-gdb'
1717
RC_OPENOCD = 'test-openocd'
18-
RC_OPENOCD_SEARCH = '/test/openocd/search'
18+
RC_OPENOCD_SEARCH = ['/test/openocd/search']
1919

2020

2121
@pytest.fixture

0 commit comments

Comments
 (0)