0

Probably I'm doing something wrong. I'm writing an Arduino sketch for ESP32. Among the others I have this function:

#define HDR_MAX_LENGHT 4
#define CMD_MAX_LENGHT 5
#define ARG_MAX_LENGHT 5
#define ARG_MAX_NUMBER 8

void ProcessLine(HardwareSerial *serial, char *line)
{
  int ret;
  int argc;
  char header[HDR_MAX_LENGHT + 1];
  char cmd[CMD_MAX_LENGHT + 1];
  char argv[ARG_MAX_NUMBER][ARG_MAX_LENGHT + 1];

  return; // for debugging
}

Usage example:

void loop() 
{
    static char inBuf[64];

    if (Serial.available())
    {
      size_t size = Serial.readBytes(inBuf, 64);
      inBuf[size] = '\0';
      ProcessLine(&Serial, inBuf);
    }  
}

In the last days all worked fine. After adding other code, I noticed that sometimes my program "hangs": nothing runs. Perhaps it goes in an exception trap.

Commenting out the different sections of the code, I discovered that if I remove the declarations of the variables in ProcessLine() no more hangs happen. The current RAM consumption is negligible: 0.5%.

During the tests NO serial data was received! So the function ProcessLine() was never called. Hence, only the declaration of the variables lead to the "hang".

I wonder if there are some mistakes in the code above. Otherwise, how I can further debug my code to understand what I've done wrong?

7
  • 1
    if size is 64 then you write \0 out of the array
    – Juraj
    Jun 4 at 7:15
  • @Juraj good catch, thanks. But as said I'm not sending any data right now.
    – Mark
    Jun 4 at 7:16
  • Can you write a testable, minimal example that reproduces the issue? Jun 4 at 8:01
  • @EdgarBonet unfortunately not because with a minimal example all works as expected (as said, in the beginning of the development there were no issues). For this reason I'm asking how to debug in addition to commenting out some part of the code.
    – Mark
    Jun 4 at 8:33
  • 2
    There is nothing wrong with these declarations. The fact that the program works when you comment out some lines of code does not mean that these lines are causing the problem. It looks like you may have triggered undefined behavior somewhere. When this happens, the behavior of the program becomes unpredictable, and can depend on changes to parts of the code that are completely unrelated to the actual problem. A typical example is memory corruption. Jun 4 at 10:37

1 Answer 1

4

As I said in a comment, there is nothing wrong with these declarations. Now, here are some debugging strategies you may try:

  1. You can rebuild your program with all compiler warnings enabled. Look carefully at the compiler messages and investigate any warning you see.

  2. You can continue with your current strategy: put back those declarations, then try to remove something else. You may find that there are multiple pieces of code that, when removed, make your program work again. Chances are the problem resides in one of those pieces. But then, it may be that the problem is in the interaction between multiple pieces rather than in one specific piece.

  3. You may just read the code carefully and see if you spot something suspicious. This can work sometimes: see how @Juraj spotted a buffer overflow in your code snippet just by reading it.

  4. You can add debug messages across your program, to check what branches are executed, the values of variables...

  5. I don't know the ESP32 dev environment, but you can check whether the compiler supports adding “sanitizers”. These are pieces of code added by the compiler that do run-time checks on your program, and report problems as they happen. You may want to enable at least an address sanitizer and an undefined behavior sanitizer.

  6. If the environment supports it, you can try to run the program in a debugger. No success guaranteed: if the program corrupts its memory, it may crash in some place unrelated to the actual problem. The debugger will tell you where it crashed, which may not be very helpful in this case.

  7. If the ESP dev environment supports neither 5 nor 6, you may try to run your program on your PC. There are a couple of Arduino-compatible cores that you can download for running on a PC.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.