How to debug my sketch code step-by-step?

I have a sketch the hangs at a certain place in the code, depending on the outcome of a prior section of code. I've exhasuted all ideas using printf debugging to the Serial Monitor. I'm running on a Teensy 4.0 board, using the Teensyduino IDE.

I'm somewhat shocked that I can't find a way to single-step through my code, watch variables, etc. Looking for help on how to do some basic debugging. Thanks!!

Hello
Post your sketch.

Perhaps if you posted your code someone could suggest an approach. One technique that I have been using along with printing messages indicating the path being taken through the code and the value of significant variables is to add while loops at various points in the code that are controlled by Serial input to allow the code to continue.

This is painful and in no way replaces a proper debugger with the ability to set breakpoints, single step and examine or change the value of variables, but it is all I have

Note: If the sketch crashes and resets the processor, the characters in the output buffer will never get to Serial Monitor. Put Serial.flush(); after each debug message so the output completes before going on to code that might crash. That will get you better debug output and allow you to localize the problem.

That is because your C code is first converted to assembly, and then compiled into executable instructions for your particular processor. A programing language that uses an INTERPRETER can do what you suggest.
Paul

I have never used a debugger to single step through code in anger in my life. I find serial print will give me all I need.

I doubt it.

This is why it really helps to test as you go. It sounds like you're engaging in a bit of big-bang testing, which rarely goes well. You may find it helpful to remove (or comment out) sections of your code until you get to a point where you have something working and then start adding it back a bit at a time.

You should post code by using code-tags
There is an automatic function for doing this in the Arduino-IDE
just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy for forum"
  3. paste clipboard into write-window of a posting

If other users take a look into your code I'm pretty sure that they will at least give new ideas how you could debug.

best regards Stefan

People that write embedded code for a living in industry use real development environments and hardware in-circuit debuggers that support the requested functionality even for compiled code.

No, not everyone in industry.
The problem with a debug build as it is called is that it runs slower than the code normally would. It is not unknown for a debug build to work fine, only to fail when all that debug support is removed.

The clue here is real time. The solution to this is to use a logic analyser injected to the data bus and address bus and decode the actual code and turn back into assembly code. That will only work if these are exposed, which they tend not to be in a lot of small embedded systems.

Most of these sort of problems turn out to be with the operating system, which is why you should avoid them if possible.

Yes, I saw such a box at the Unisys plant in Plymouth, MI. I was writing communication software for a PC to communicate with their check reader/sorter and the communication was dying. An engineer used such a device to track the

Forum seems to be dying while I respond.

Divide and conquer; repeat as necessary.

1 Like

I'm somewhat shocked that I can't find a way to single-step through my code, watch variables, etc. Looking for help on how to do some basic debugging.

Yep. No debugging in the Arduino IDE.

teensy 4 will support a gdb stub so that it can be debugged over the usb connection without needing an SWD/JTAG probe and etc.
See GitHub - ftrias/TeensyDebug: GDB proxy and debugging support to Teensy 3/4 or How to debug an Teensy Arduino project with GDBStub (note that Visual Micro is not free. But it's pretty reasonable.)

The 2.0 version of Arduino is supposed to support debugging on "advanced" platforms. I don't know whether that includes third party boards without probes...

Hi, @jepporob
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".

This will help with advice on how to present your code and problems.

Thanks.. Tom... :grinning: :+1: :australia:

Well you can let some users here take a look into your code to get new ideas how to debug it. If your code is not top secret and you did'nt signed a NDA (non disclosure agreement) post your code with this method:

You should post code by using code-tags
There is an automatic function for doing this in the Arduino-IDE
just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy for forum"
  3. paste clipboard into write-window of a posting

best regards Stefan

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.