Crash with serial driver

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
branilson
Posts: 7
Joined: Thu Aug 06, 2015 10:15 pm

Crash with serial driver

Post by branilson »

Hi,

I am having trouble on a function that prints plain text via serial port. All the other similar functions are working fine but this one crashes the entire system when called. The function is named plainOutput and is inside the log.c. It prints until the first or second character and crashes everything. I have tried a lot of things but I cannot see what is wrong. The function printHelp in serial_forwarding.c calls the function plainOutput in log.c to send the strings. serial_forwarding.c is a colection of routines to forward messages coming from other serial ports and redirect to the log serial port. log.c has log routines and the corresponding thead.

log.c

Code: Select all

#include "log.h"
#include <string.h>
#include "strutils.h"

static output_queue_t *out = NULL;
static semaphore_t sem;
static logHook hook = NULL;

void logInit(output_queue_t *q)
{
    chSysLock();
    if (out)
    {
        chSysUnlock();
        return;
    }
    out = q;
    chSemObjectInit(&sem, 1);
    chSysUnlock();
}

void plainOutput(const char *msg){
    char buffer[LOG_MAX_MSGLEN+1];
    /*Check if logging system has been initialized*/
    if (!out)
   return;

    buffer[0] = '\0';
    strncat(buffer, msg, LOG_MAX_MSGLEN - strlen(buffer) - 2);
   
    chSemWait(&sem);
    chOQWriteTimeout(out, (uint8_t *)buffer, strlen(buffer), TIME_INFINITE);
    if (hook) hook(buffer);
    chSemSignal(&sem);
}

void plainNumOutput(const char *msg, uint8_t length){
    char buffer[LOG_MAX_MSGLEN+1];
    /*Check if logging system has been initialized*/
    if (!out)
   return;
   
   
    buffer[0] = '\0';
    strncat(buffer, msg, LOG_MAX_MSGLEN - length - 2);
   
    chSemWait(&sem);
    chOQWriteTimeout(out, (uint8_t *)buffer, length, TIME_INFINITE);
    if (hook) hook(buffer);
    chSemSignal(&sem);
}

void logOutput(LogLevel lvl, const char *msg)
{
    char buffer[LOG_MAX_MSGLEN+1];
    char tmp[3]; /* Capable to hold a two digit number and a trailing zero*/
    systime_t ts = chVTGetSystemTime();

    /*Check if logging system has been initialized*/
    if (!out)
        return;

    /*Prepare buffer*/
    buffer[0] = '\0';
    strcat(buffer, "<S>");
    strcat(buffer, "[");
    itoa(tmp, (ts / CH_CFG_ST_FREQUENCY / 3600) % 100);
    strcat(buffer, tmp);
    strcat(buffer, "-");
    itoa(tmp, (ts / CH_CFG_ST_FREQUENCY / 60) % 60);
    strcat(buffer, tmp);
    strcat(buffer, ":");
    itoa(tmp, (ts / CH_CFG_ST_FREQUENCY) % 60);
    strcat(buffer, tmp);
    strcat(buffer, "]");
    switch (lvl)
    {
        case LOG_ERROR:
            strcat(buffer, "[ERROR] ");
            break;
        case LOG_CRITICAL:
            strcat(buffer, "[CRITICAL] ");
            break;
        case LOG_WARNING:
            strcat(buffer, "[WARNING] ");
            break;
        default:
            strcat(buffer, "[INFO] ");
            break;
    }
    strncat(buffer, msg, LOG_MAX_MSGLEN - strlen(buffer) - 2); /*Note: we need space for \n\0*/
    strcat(buffer, "\n");

    /*Print buffer to output but protect it from inter-thread interference*/
    chSemWait(&sem);
    chOQWriteTimeout(out, (uint8_t *)buffer, strlen(buffer), TIME_INFINITE);
    if (hook) hook(buffer);
    chSemSignal(&sem);
}

void logSetHook(logHook h)
{
    chSysLock();
    hook = h;
    chSysUnlock();
}


serial_forwarding.c

Code: Select all

#include "log.h"
#include <string.h>
#include "strutils.h"

static output_queue_t *out = NULL;
static semaphore_t sem;
static logHook hook = NULL;

void logInit(output_queue_t *q)
{
    chSysLock();
    if (out)
    {
        chSysUnlock();
        return;
    }
    out = q;
    chSemObjectInit(&sem, 1);
    chSysUnlock();
}

void plainOutput(const char *msg){
    char buffer[LOG_MAX_MSGLEN+1];
    /*Check if logging system has been initialized*/
    if (!out)
   return;

    buffer[0] = '\0';
    strncat(buffer, msg, LOG_MAX_MSGLEN - strlen(buffer) - 2);
    strcat(buffer, "\n"); // Teste BL
   
    chSemWait(&sem);
    chOQWriteTimeout(out, (uint8_t *)buffer, strlen(buffer), TIME_INFINITE);
    if (hook) hook(buffer);
    chSemSignal(&sem);
}

void plainNumOutput(const char *msg, uint8_t length){
    char buffer[LOG_MAX_MSGLEN+1];
    /*Check if logging system has been initialized*/
    if (!out)
   return;
   
   
    buffer[0] = '\0';
    strncat(buffer, msg, LOG_MAX_MSGLEN - length - 2);
    strcat(buffer, "\n"); // Teste BL
   
    chSemWait(&sem);
    chOQWriteTimeout(out, (uint8_t *)buffer, length, TIME_INFINITE);
    if (hook) hook(buffer);
    chSemSignal(&sem);
}


void logOutput(LogLevel lvl, const char *msg)
{
    char buffer[LOG_MAX_MSGLEN+1];
    char tmp[3]; /* Capable to hold a two digit number and a trailing zero*/
    systime_t ts = chVTGetSystemTime();

    /*Check if logging system has been initialized*/
    if (!out)
        return;

    /*Prepare buffer*/
    buffer[0] = '\0';
    strcat(buffer, "<S>");
    strcat(buffer, "[");
    itoa(tmp, (ts / CH_CFG_ST_FREQUENCY / 3600) % 100);
    strcat(buffer, tmp);
    strcat(buffer, "-");
    itoa(tmp, (ts / CH_CFG_ST_FREQUENCY / 60) % 60);
    strcat(buffer, tmp);
    strcat(buffer, ":");
    itoa(tmp, (ts / CH_CFG_ST_FREQUENCY) % 60);
    strcat(buffer, tmp);
    strcat(buffer, "]");
    switch (lvl)
    {
        case LOG_ERROR:
            strcat(buffer, "[ERROR] ");
            break;
        case LOG_CRITICAL:
            strcat(buffer, "[CRITICAL] ");
            break;
        case LOG_WARNING:
            strcat(buffer, "[WARNING] ");
            break;
        default:
            strcat(buffer, "[INFO] ");
            break;
    }
    strncat(buffer, msg, LOG_MAX_MSGLEN - strlen(buffer) - 2); /*Note: we need space for \n\0*/
    strcat(buffer, "\n");

    /*Print buffer to output but protect it from inter-thread interference*/
    chSemWait(&sem);
    chOQWriteTimeout(out, (uint8_t *)buffer, strlen(buffer), TIME_INFINITE);
    if (hook) hook(buffer);
    chSemSignal(&sem);
}

void logSetHook(logHook h)
{
    chSysLock();
    hook = h;
    chSysUnlock();
}


The image below shows the cutecom terminal when the system crashes. Notice that before I send the ? (that uses plainOutput function) I sent the string <S>=lsr,A1 that activates a laser and prints the message "<S>[0-1:24] [INFO] Laser A on", using the function logOutput without problems. But when I sent ? character, that calls the function plainOutput (the problematic one) the system crashed at the second character printed. I have a 1Hz LED blinking routine in the main thread and it stops as well. Notice that I am using a semaphore before send via serial in both functions, but the crash problem only happens on the plainOutput function.

Please help me if you see something I am doing wrong. Thank you.

Terminal crash image example
[img]
https://drive.google.com/open?id=0B_ZfW ... ndRRjhaVEk
[/img]
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Crash with serial driver

Post by Giovanni »

Hi,

Are debug options enabled in chconf.h? assertions, checks, state checker, stack overflow check.

After the crash/stop examine the stack trace and the debug message (if in halt).

Giovanni
branilson
Posts: 7
Joined: Thu Aug 06, 2015 10:15 pm

Re: Crash with serial driver

Post by branilson »

Thank you for the reply.

Sorry, I did not used debug options because I am not so familiar to debug in Eclipse. Now I activated the debug options on chconf.h as you said. I also needed to make some changes on CH_CFG_ST_TIMEDELTA, changed from 2 to 0, and to the Makefile that I changed the USER_OPT from -O2 to O0.

With those changes the system is crashing at the start. Now I am getting the error: chSysHalt (reason=0x8016ad0 <__func__.7958> "can_lld_start") at /home/bl/ARMToolchains/ChibiOS_16.1.2/os/rt/src/chsys.c:179" on the debug console, that as I see is a infinite loop of the halt condition.

Besides that, I am having some trouble with the debug and OpenOCD on eclipse. Some errors are occurring, listed below.My board is based on the STM32F407IGT6 chip and I am using a STM32 discovery as JTAG. My OpenOCD configuration is using stm32f4discovery.cfg file as argument, I don't know if it is causing the problem.

Code: Select all

Open On-Chip Debugger 0.9.0 (2016-01-26-16:17)
Licensed under GNU GPL v2
For bug reports, read
   http://openocd.org/doc/doxygen/bugs.html
Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD
adapter speed: 2000 kHz
adapter_nsrst_delay: 100
none separate
srst_only separate srst_nogate srst_open_drain connect_deassert_srst
Info : Unable to match requested speed 2000 kHz, using 1800 kHz
Info : Unable to match requested speed 2000 kHz, using 1800 kHz
Info : clock speed 1800 kHz
Info : STLINK v2 JTAG v24 API v2 SWIM v0 VID 0x0483 PID 0x3748
Info : using stlink api v2
Info : Target voltage: 2.909307
Info : stm32f4x.cpu: hardware has 6 breakpoints, 4 watchpoints
Info : accepting 'gdb' connection on tcp/3333
Info : device id = 0x10036413
Info : flash size = 1024kbytes
undefined debug reason 7 - target needs reset
requesting target halt and executing a soft reset
Error: Target stm32f4x.cpu does not support soft_reset_halt
requesting target halt and executing a soft reset
Error: Target stm32f4x.cpu does not support soft_reset_halt
Error: timed out while waiting for target halted

background polling: on
TAP: stm32f4x.cpu (enabled)
target state: running
Error: Target not halted
stm32x mass erase failed

Error: Target not halted
Error: error writing to flash at address 0x08000000 at offset 0x00000000

requesting target halt and executing a soft reset
Error: Target stm32f4x.cpu does not support soft_reset_halt
Warn : WARNING! The target is already running. All changes GDB did to registers will be discarded! Waiting for target to halt.
Info : dropped 'gdb' connection
Info : accepting 'gdb' connection on tcp/3333
requesting target halt and executing a soft reset
Error: Target stm32f4x.cpu does not support soft_reset_halt
requesting target halt and executing a soft reset
Error: Target stm32f4x.cpu does not support soft_reset_halt
background polling: on
TAP: stm32f4x.cpu (enabled)
target state: halted
target halted due to debug-request, current mode: Thread
xPSR: 0x61000000 pc: 0x08001a30 psp: 0x20000a88
stm32x mass erase complete
target state: halted
target halted due to breakpoint, current mode: Thread
xPSR: 0x61000000 pc: 0x20000042 psp: 0x20000a88
wrote 96732 bytes from file build/ch.bin to flash bank 0 at offset 0x00000000 in 1.091466s (86.549 KiB/s)
requesting target halt and executing a soft reset
Error: Target stm32f4x.cpu does not support soft_reset_halt
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Crash with serial driver

Post by Giovanni »

Hi,

If the system halts then an assertion has been triggered in that function, look at the assertion condition in that function and you will know the reason of the error.

If you are using the Eclipse debugger you just need to click on the stack trace and go up into the stack up to the assertion point.

Giovanni
branilson
Posts: 7
Joined: Thu Aug 06, 2015 10:15 pm

Re: Crash with serial driver

Post by branilson »

I am not using Chibistudio. I use Linux, so I don't have the chibiOS debug plugin installed. Is there a way to install the ChibiOS plugins mannualy?
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Crash with serial driver

Post by Giovanni »

Hi,

It is not a plugin feature, it is an Eclipse view. the plugin can be installed from its update site: http://www.chibios.org/eclipse/chibistudio

Giovanni
rew
Posts: 383
Joined: Sat Jul 19, 2014 12:59 pm
Has thanked: 2 times
Been thanked: 14 times

Re: Crash with serial driver

Post by rew »

but then again, no plugin is required. Just run the program in the debugger and when it has stopped, hit control-C and type "where". It will tell you what caused the problem.

If that doesn't work set a breakpoint in chSysHalt, and gdb will show you where things went wrong immediately when the system halts.
User avatar
tfAteba
Posts: 551
Joined: Fri Oct 16, 2015 11:03 pm
Has thanked: 91 times
Been thanked: 50 times

Re: Crash with serial driver

Post by tfAteba »

Hi,

Good to know, I'm also using linux. Very helpful astuce. Thanks,

Theo.
branilson
Posts: 7
Joined: Thu Aug 06, 2015 10:15 pm

Re: Crash with serial driver

Post by branilson »

Hi everybody,


The crash problem was caused by stack overflow, so I increased the size of the serial thread stack and problem is solved. Additionally the debugger made me find another problem I was not seeing with the CAN thread.

Thank you all.
User avatar
tfAteba
Posts: 551
Joined: Fri Oct 16, 2015 11:03 pm
Has thanked: 91 times
Been thanked: 50 times

Re: Crash with serial driver

Post by tfAteba »

Hi all,

Is there a way to prevent a stack overflow or not? And how can we determin a trhread stack size in advance to avoid that kind of trouble?

cheers

Theo
Post Reply