chprintf / DEBUG Macro

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

Moderators: RoccoMarco, lbednarz, tfAteba

Tabulous
Posts: 518
Joined: Fri May 03, 2013 12:02 pm
Has thanked: 7 times
Been thanked: 18 times

chprintf / DEBUG Macro

Post by Tabulous »

I'm try to create my own DEBUG routines by using chprintf.

chprintf works fine when called like this
chprintf((BaseSequentialStream*)&SD6, "my string");


But i want to add some conditions around this so i tried this

in my debug.h file i have a macro
#define EDC_DEBUG( x, y, ... ) chprintf(&SD6, y)

now if i call macro EDC_DEBUG like this
EDC_DEBUG( DBG_MASK_ATC, "my string" );


It outputs rubbish, am i doing something wrong here, sure i'm missing something.
utzig
Posts: 359
Joined: Sat Jan 07, 2012 6:22 pm
Has thanked: 1 time
Been thanked: 20 times

Re: chprintf / DEBUG Macro

Post by utzig »

The snippets of code you copied look perfectly fine to me. There must be something else causing the corruption.
Tabulous
Posts: 518
Joined: Fri May 03, 2013 12:02 pm
Has thanked: 7 times
Been thanked: 18 times

Re: chprintf / DEBUG Macro

Post by Tabulous »

its something more sinister than than, like i said the chprintf((BaseSequentialStream*)&SD6, "my string"); works fine.

its is something to do with the macro not been defined correctly, this can be tricky where variadic is concerned.
utzig
Posts: 359
Joined: Sat Jan 07, 2012 6:22 pm
Has thanked: 1 time
Been thanked: 20 times

Re: chprintf / DEBUG Macro

Post by utzig »

So, if you remove the variadic parameter it all works?
utzig
Posts: 359
Joined: Sat Jan 07, 2012 6:22 pm
Has thanked: 1 time
Been thanked: 20 times

Re: chprintf / DEBUG Macro

Post by utzig »

Btw, just did a little test here and your macro definition should probably look like this:

Code: Select all

#define EDC_DEBUG( x, y, ... ) chprintf(&SD6, y, ## __VA_ARGS__)


Some code I used to test (not on ChibiOS):

Code: Select all

#include <stdio.h>

#define DEBUG(x, y, ...) printf(y, ## __VA_ARGS__)

int main(void) {
        DEBUG(0, "test\n");
        DEBUG(1, "test value=%d\n", 1);
        DEBUG(2, "test values=(%d, %d)\n", 1, 2);
        return 0;
}


The output looks correct:

Code: Select all

test
test value=1
test values=(1, 2)


More info: https://gcc.gnu.org/onlinedocs/cpp/Variadic-Macros.html
Tabulous
Posts: 518
Joined: Fri May 03, 2013 12:02 pm
Has thanked: 7 times
Been thanked: 18 times

Re: chprintf / DEBUG Macro

Post by Tabulous »

Thanks utzig :-)
User avatar
Chudik
Posts: 152
Joined: Fri Jan 16, 2015 7:51 am
Has thanked: 7 times
Been thanked: 1 time

Re: chprintf / DEBUG Macro

Post by Chudik »

Based on this thread and Beginners - Command Shell I created a base version for my Debug messages processing.
With my shell I can set 3 levels of debug messages (could be any, but I think 3 will be enough). Level set in global variable console_debug.

Here are the fragments of my code:

console.h

Code: Select all

...
#define M3_DEBUG( level, ... ) if(console_debug && level <= console_debug) chprintf(console_cfg.sc_channel, ## __VA_ARGS__)
...
extern uint8_t console_debug;
...



console.c

Code: Select all

...
uint8_t console_debug = 0;

void cmd_debug(BaseSequentialStream *chp, int argc, char *argv[])
{
  uint8_t dlevel;


  if (argc != 1)
  {
    chprintf(chp, "Usage: debug [0..3]\r\n");
    chprintf(chp, "\t0 - no debug info\r\n");
    chprintf(chp, "\t1 - debug level 1\r\n");
    chprintf(chp, "\t2 - debug level 2\r\n");
    chprintf(chp, "\t3 - debug level 3\r\n");
    chprintf(chp, "Debug level %d\r\n", console_debug);
    return;
  }

    dlevel = argv[0][0];

    if (isdigit(dlevel))
    {
      switch(dlevel)
      {
        case '0': console_debug = 0;
                  chprintf(chp, "Debug off\r\n");
                  return;
        case '1': console_debug = 1; break;
        case '2': console_debug = 2; break;
        case '3': console_debug = 3; break;
        default:  chprintf(chp, "No debug level change\r\n"); return;
      }

      chprintf(chp, "Debug level %c\r\n", dlevel);
    }
    else
    {
        chprintf(chp, "No debug level change\r\n");
    }
}
...

I know it could be written more effective :-)



And in the main (of course it is Blinky, what else to use for beginning :-))
main.c

Code: Select all

....

 static msg_t Blinky(void *arg)
 {
     (void)arg;

     chRegSetThreadName("Blinky");

     while(1)
     {
        palSetPad(GPIOA, GPIOA_LED_GREEN);
        chThdSleepMilliseconds(500);
        M3_DEBUG(2,"LED ON\r\n");

        palClearPad(GPIOA, GPIOA_LED_GREEN);
        chThdSleepMilliseconds(500);
        M3_DEBUG(3,"LED OFF\r\n");
     }

         return 0;
 }
...

When I set debug level 0 (off) or 1, there are no messages on terminal. When I set 2, only "LED ON" messages appear, and when I set 3, both messages shown in terminal. So, it works as expected.
However, I'm not sure that I correctly accessing the console_debug variable. What is the correct way of accessing the global variable in multitasking environment? May I use such global variable or I should use some other way?
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: chprintf / DEBUG Macro

Post by Giovanni »

Hi,

Global variables are perfectly normal variables unless accessed by more than one thread, if this is the case then you should evaluate implementing a mutual exclusion on the variable, it depends how it is used, usually if there are read-modify-write operations on the variable then you could need mutual exclusion.

Giovanni
User avatar
Chudik
Posts: 152
Joined: Fri Jan 16, 2015 7:51 am
Has thanked: 7 times
Been thanked: 1 time

Re: chprintf / DEBUG Macro

Post by Chudik »

Yeah, that what I was expecting... Definitely I will need to use it from several threads. Need to read about the mutexes.

Is it OK to use chprintf with one channel from several threads? Does this function resolves conflicts internally?
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: chprintf / DEBUG Macro

Post by Giovanni »

Hi,

chprintf() writes to a stream, in itself it can be used in multiple threads but written data can get corrupted because concurrent writes to the same stream.
For example writing concurrently "ciao" and "mamma" could result in something like "cimamaoma".

For mutual exclusion over a simple variable you don't need mutexes (heavy mechanism), you may use chSysLock() and chSysUnlock() for a lightweight critical zone.

Giovanni
Post Reply