Skip to content
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

dist/tools/openocd: start debug-server in background and wait #19737

Merged

Conversation

fabian18
Copy link
Contributor

Contribution description

When the target debug-server is called from a remote it is not so convenient to exit it properly, because Ctrl+C is cached by make itself. This terminates the script but the server keeps running, consequently a second invocation of the debug-server fails. A fix for this would be to start the server in the background, save the PID in a file and wait for the openocd process. This PR makes the openocd server shutdown when Ctrl+D is pressed or anything else can read the PID file and kill the server.

Testing procedure

BOARD=same54-xpro make flash debug-server with set -x

### Starting GDB Server ###
+ test_config
+ '[' '!' -f /home/fabian.huessler@ml-pa.loc/RIOT/boards/same54-xpro/dist/openocd.cfg ']'
++ mktemp -t openocd_pid.XXXXXXXXXX
+ OCD_PIDFILE=/tmp/openocd_pid.GX9T0mZT2N
+ trap 'cleanup /tmp/openocd_pid.GX9T0mZT2N' EXIT
+ read -r line
+ sh -c 'openocd             -c '\''source [find interface/cmsis-dap.cfg]'\'' -c '\''transport select swd'\''             -f '\''/home/fabian.huessler@ml-pa.loc/RIOT/boards/same54-xpro/dist/openocd.cfg'\''                          -c '\''bindto localhost'\''             -c '\''tcl_port 6333'\''             -c '\''telnet_port 4444'\''             -c '\''gdb_port 3333'\''             -c '\''init'\''                          -c '\''targets'\''             -c '\''halt'\'' &             echo $! > /tmp/openocd_pid.GX9T0mZT2N ;             wait $(cat /tmp/openocd_pid.GX9T0mZT2N)'
Open On-Chip Debugger 0.12.0-rc3-00999-gfcb40f49b (2023-05-12-10:54)
Licensed under GNU GPL v2
For bug reports, read
        http://openocd.org/doc/doxygen/bugs.html
swd
Info : CMSIS-DAP: SWD supported
Info : CMSIS-DAP: FW Version = 03.25.01B6
Info : CMSIS-DAP: Serial# = ATML2748101800010083
Info : CMSIS-DAP: Interface Initialised (SWD)
Info : SWCLK/TCK = 1 SWDIO/TMS = 1 TDI = 1 TDO = 1 nTRST = 0 nRESET = 1
Info : CMSIS-DAP: Interface ready
Info : clock speed 2000 kHz
Info : SWD DPIDR 0x2ba01477
Info : [atsame5.cpu] Cortex-M4 r0p1 processor detected
Info : [atsame5.cpu] target has 6 breakpoints, 4 watchpoints
Info : starting gdb server for atsame5.cpu on 3333
Info : Listening on port 3333 for gdb connections
    TargetName         Type       Endian TapName            State       
--  ------------------ ---------- ------ ------------------ ------------
 0* atsame5.cpu        cortex_m   little atsame5.cpu        running

[atsame5.cpu] halted due to debug-request, current mode: Handler PendSV
xPSR: 0x8100000e pc: 0x0000972e msp: 0x20000190
Info : Listening on port 6333 for tcl connections
Info : Listening on port 4444 for telnet connections

+ echo 'Exit with Ctrl+D'
Exit with Ctrl+D
+ read -r line

+ echo 'Exit with Ctrl+D'
Exit with Ctrl+D
+ read -r line
+ cleanup /tmp/openocd_pid.GX9T0mZT2N
++ cat /tmp/openocd_pid.GX9T0mZT2N
+ OCD_PID=154902
+ kill 154902
+ rm -f /tmp/openocd_pid.GX9T0mZT2N
shutdown command invoked
+ exit 0
Terminated

Issues/PRs references

@github-actions github-actions bot added the Area: tools Area: Supplementary tools label Jun 15, 2023
@benpicco benpicco added CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR CI: skip compile test If set, CI server will run only non-compile jobs, but no compile jobs or their dependent jobs labels Jun 16, 2023
@riot-ci
Copy link

riot-ci commented Jun 16, 2023

Murdock results

✔️ PASSED

1b93025 dist/tools/openocd: start debug-server in background and wait

Success Failures Total Runtime
1 0 1 56s

Artifacts

-c 'halt'"
-c 'halt' & \
echo \$! > $OCD_PIDFILE ; \
wait \$(cat $OCD_PIDFILE)" &
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the background wait needed?
Does it keep the script (and trap) alive for the cleanup?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just tested it without the ampersand in L437 and I think what happens then is that the shell waits for the openocd process to finish, while it is running obviously. So the script does not get to the while read until something on the remote side kills openocd

@benpicco
Copy link
Contributor

bors merge

@bors
Copy link
Contributor

bors bot commented Jun 20, 2023

Build succeeded!

The publicly hosted instance of bors-ng is deprecated and will go away soon.

If you want to self-host your own instance, instructions are here.
For more help, visit the forum.

If you want to switch to GitHub's built-in merge queue, visit their help page.

@bors bors bot merged commit 561e193 into RIOT-OS:master Jun 20, 2023
@benpicco benpicco added this to the Release 2023.07 milestone Aug 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: tools Area: Supplementary tools CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR CI: skip compile test If set, CI server will run only non-compile jobs, but no compile jobs or their dependent jobs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants