Page 1 of 1

Crash on USB Disconnect when using Shell over USB (Only with optimisation on)

Posted: Sun May 02, 2021 5:45 am
by jmaredev
Hi,

I have been working with the shell module which is very useful, but I am having a problem with handling the usb disconnection and then spawning a new shell. I am using a STM32F407 Discovery, my full code is here: https://github.com/JMare/chibios_shell_demo
The relevent code causing the problem is here: https://github.com/JMare/chibios_shell_ ... /esh.c#L61
This code is closely adapted from this example here: https://github.com/ChibiOS/ChibiOS/blob ... ain.c#L161

To reproduce the problem:
1. Load code and reset board
2. plug usb otg port into computer
3. open serial port and interact with shell (using screen on linux)
4. disconnect usb cable

When the USB is disconnected, all code on the board appears to stop running, evidenced by the led blinker thread stopping.
Running in GDB reveals:

Code: Select all

(gdb) run
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /home/james/git/chibios_shell_demo/build/ch.elf
Program received signal SIGTRAP, Trace/breakpoint trap.
VectorFC () at ./chibios/os/common/startup/ARMCMx/compilers/GCC/vectors.S:1021
1021           bl          _unhandled_exception
(gdb) backtrace
#0  VectorFC () at ./chibios/os/common/startup/ARMCMx/compilers/GCC/vectors.S:1021
#1  <signal handler called>
#2  0x08003f9c in ch_sch_prio_insert (qp=0xb, tp=0x200009f4 <ch+48>) at ./chibios/os/rt/include/chschd.h:508
#3  chMtxLockS (mp=0xb) at ./chibios/os/rt/src/chmtx.c:214
#4  0x08004e94 in chMtxLock (mp=0xb) at ./chibios/os/rt/src/chmtx.c:126
#5  chHeapFree (p=0x2001feb8) at ./chibios/os/oslib/src/chmemheaps.c:310
#6  0x0800e512 in chThdRelease (tp=<optimized out>) at esh.c:77
#7  eshService () at esh.c:76
#8  main () at main.c:64


However, this issue only occurs with optimisation set at the default -O2 in the makefile.
When I reduce this to -O0, the code runs just fine, and I can disconnect and reconnect the USB port as often as I want.
Also worth noting, this problem doesnt occur if I dont open the serial port on the linux side before disconnecting the usb.
But once opened, even closing screen on the linux side before pulling the cable causes the problem.

Since the error happens on call to chThdRelease, is there something I need to call before this?

Thanks,
James

Re: Crash on USB Disconnect when using Shell over USB (Only with optimisation on)

Posted: Sun May 02, 2021 6:09 am
by Giovanni
Hi,

Do you have debug options enabled in chconf.h? stack overflow, assertions and state checker.

Giovanni

Re: Crash on USB Disconnect when using Shell over USB (Only with optimisation on)

Posted: Sun May 02, 2021 7:14 am
by jmaredev
Giovanni wrote:Hi,

Do you have debug options enabled in chconf.h? stack overflow, assertions and state checker.

Giovanni


Thank you for the quick reply!
I went and enabled CH_DBG_SYSTEM_STATE_CHECK, CH_DBG_ENABLE_ASSERTS, CH_DBG_ENABLE STACK_CHECK.

It quickly became evident based on GDB output that the issue was a stack overflow on my shell thread. I now see that the demo I based my code on had a far greater thread working area.

Thanks again for your help.

Re: Crash on USB Disconnect when using Shell over USB (Only with optimisation on)

Posted: Sun May 02, 2021 7:51 am
by Giovanni
Be careful because used stack varies with compiler options, better optimize stacks at the very end.

Giovanni