Author Topic: Tracing Arduino .ino program with breakpoint  (Read 1059 times)

0 Members and 5 Guests are viewing this topic.

Offline Wilson__Topic starter

  • Regular Contributor
  • *
  • Posts: 92
  • Country: gb
Tracing Arduino .ino program with breakpoint
« on: June 20, 2024, 04:20:25 am »
Is there a software platform (seem PlatformIO or others??) allowing breakpoint and variable value inspection for .ino file on Artduino ATMEGA 2560 hardware? 

We need to trace one piece of .ino code to understand how it works

Many thanks
 

Offline Whales

  • Super Contributor
  • ***
  • Posts: 1983
  • Country: au
    • Halestrom
Re: Tracing Arduino .ino program with breakpoint
« Reply #1 on: June 20, 2024, 05:23:11 am »
Arduino MEGAs don't have debug hardware on them.  You either need to buy an external debugging tool to plug into the board or a different board that has one built in.

There is a list of supported boards here: https://docs.arduino.cc/software/ide-v2/tutorials/ide-v2-debugger/

Further detail: The USB connector on an Arduino Mega goes to a chip that converts USB signals to UART ("serial") signals.  These UART signals go to the ATMEGA2560 chip.  This chip has a program on it loaded at the factory called a bootloader, this program listens to the UART for new programs to write to the flash.  No part of this chain supports debugging commands, to debug an AVR chip you need to use a different interface (different wires) than the serial UART.

I generally use printf() style debugging in these situations instead (unlike breakpoints and memory watches you don't need any special hardware or software).
« Last Edit: June 20, 2024, 05:27:16 am by Whales »
 
The following users thanked this post: Wilson__

Offline SteveThackery

  • Regular Contributor
  • *
  • Posts: 109
  • Country: gb
Re: Tracing Arduino .ino program with breakpoint
« Reply #2 on: June 20, 2024, 11:08:14 am »
Is there a software platform (seem PlatformIO or others??) allowing breakpoint and variable value inspection for .ino file on Artduino ATMEGA 2560 hardware? 

This might do the trick:

https://visualmicro.com

It's an add-in for Microsoft Visual Studio and provides an improved development environment including the debugging features you are asking for.  It is 100% back-and-forth compatible with the Arduino code editor.

Importantly, unlike Arduino editor V2, it will debug any Arduino, not just those that support hardware debugging.

I use it almost exclusively for all my Arduino work and I think it's great.
 
The following users thanked this post: Wilson__

Offline Whales

  • Super Contributor
  • ***
  • Posts: 1983
  • Country: au
    • Halestrom
Re: Tracing Arduino .ino program with breakpoint
« Reply #3 on: June 20, 2024, 12:46:35 pm »
Importantly, unlike Arduino editor V2, it will debug any Arduino, not just those that support hardware debugging.

Huh?  How?

Don't you need extra hardware?  Or does it upload its own supervisor code?
 
The following users thanked this post: Wilson__

Offline Wilson__Topic starter

  • Regular Contributor
  • *
  • Posts: 92
  • Country: gb
Re: Tracing Arduino .ino program with breakpoint
« Reply #4 on: June 20, 2024, 01:43:58 pm »
Many thanks. 

While hadware debug are built-in with ARM chip, PIC and others, there were time when MPU first come out and used software debugger.

The debug software linked in a 'remote monitoring' code with the user program.

When human user requests a breakpoint, it put (overwtite) an assembly language JUMP instruction at that point in RAM.   
 
When user program hit the breakpoint, control jump into the 'remote monitoring' code that can communicate with the user interface (over uart serial) to read/modify variables that it knows where they are and its structure from the linker MAP file.

When user hit resume key, it restore the original machine code at RAM and execute a JUMP to the point.

Many thanks
 
The following users thanked this post: Whales

Offline Peabody

  • Super Contributor
  • ***
  • Posts: 2085
  • Country: us
Re: Tracing Arduino .ino program with breakpoint
« Reply #5 on: June 20, 2024, 01:58:28 pm »
Michael Kohn's Naken Assembler includes a single-step simulator for AVR code.  It's pretty basic, and of course works at the compiled assembly language level, so it doesn't keep track of variables, only the registers.  But it's free.
 
The following users thanked this post: Wilson__

Offline SteveThackery

  • Regular Contributor
  • *
  • Posts: 109
  • Country: gb
Re: Tracing Arduino .ino program with breakpoint
« Reply #6 on: June 20, 2024, 03:29:57 pm »
Importantly, unlike Arduino editor V2, it will debug any Arduino, not just those that support hardware debugging.

Huh?  How?

Don't you need extra hardware?  Or does it upload its own supervisor code?

As explained by @Wilson__, it's a software debugger.  That's why you have to recompile and upload when adding or removing a breakpoint or watch window.  But don't worry about that - despite it sounding like a kludge it is actually really easy to use and works brilliantly.  There are situations where hardware debugging is better, but for most Arduinos you don't get a choice.  Software debugging techniques have been refined over decades and are very reliable.

One last small point - VisualMicro will make use of a hardware debugger if it is supported by the target, so it provides a very clean and well integrated environment.
« Last Edit: June 20, 2024, 03:32:32 pm by SteveThackery »
 
The following users thanked this post: Wilson__

Offline Wilson__Topic starter

  • Regular Contributor
  • *
  • Posts: 92
  • Country: gb
Re: Tracing Arduino .ino program with breakpoint
« Reply #7 on: June 20, 2024, 08:01:11 pm »
Many thanks, Steve.

Does visualmicro has typical functions of hardware debugger, break point, single step, sep into, step out,...   

Watch/inspect/edit variable decoded as data type (char, int...) and c typedef structure,  array, etc.?
 

Offline tooki

  • Super Contributor
  • ***
  • Posts: 12041
  • Country: ch
Re: Tracing Arduino .ino program with breakpoint
« Reply #8 on: June 20, 2024, 08:23:03 pm »
« Last Edit: June 21, 2024, 10:17:45 am by tooki »
 
The following users thanked this post: Wilson__

Offline thm_w

  • Super Contributor
  • ***
  • Posts: 6713
  • Country: ca
  • Non-expert
Re: Tracing Arduino .ino program with breakpoint
« Reply #9 on: June 20, 2024, 11:28:13 pm »
Many thanks, Steve.

Does visualmicro has typical functions of hardware debugger, break point, single step, sep into, step out,...   

Watch/inspect/edit variable decoded as data type (char, int...) and c typedef structure,  array, etc.?

Yes but 30 day trial: https://www.visualmicro.com/page/User-Guide.aspx?doc=Debugging-Explained.html
Visualgdb is also 30 day trial: https://visualgdb.com/tutorials/arduino/

If you are dead set on arduino, its worth paying for one of these.
Profile -> Modify profile -> Look and Layout ->  Don't show users' signatures
 
The following users thanked this post: Wilson__

Offline Doctorandus_P

  • Super Contributor
  • ***
  • Posts: 3566
  • Country: nl
Re: Tracing Arduino .ino program with breakpoint
« Reply #10 on: June 21, 2024, 02:25:52 am »
I never used a debugger on my microcontroller projects, and at some times it may have been useful, I do not miss it very much.

More complex algorithm (for example verifying of CRC algorithms) I first debug on my PC and only put it in a microcontroller when the code is correct.

When interacting with microcontroller hardware, classical debugging (break points stepping though code) is often also of limited use because the debugger completely upsets timing.

Sometimes I push out some data through an unused peripheral (Uart, SPI) or even some spare generic I/O pins and catch that with a logic analyzer. This way you can follow the code execution path in the microcontroller with very little changes in timing. It's also easy to combine this with a few #define macro's so your debug code can either be defined into oblivion for the normal program flow, or defined into debug statements for the debugging. For an AVR processor, starting with an EUR10 "Saleaea" clone logic analyzer, and using it with Sigrok / Pulseview is a good start. This should be in anyone's toolbox when programming / debugging microcontroller stuff.
 
The following users thanked this post: Wilson__

Offline tooki

  • Super Contributor
  • ***
  • Posts: 12041
  • Country: ch
Re: Tracing Arduino .ino program with breakpoint
« Reply #11 on: June 21, 2024, 10:18:40 am »
Many thanks, Steve.

Does visualmicro has typical functions of hardware debugger, break point, single step, sep into, step out,...   

Watch/inspect/edit variable decoded as data type (char, int...) and c typedef structure,  array, etc.?

Yes but 30 day trial: https://www.visualmicro.com/page/User-Guide.aspx?doc=Debugging-Explained.html
Visualgdb is also 30 day trial: https://visualgdb.com/tutorials/arduino/

If you are dead set on arduino, its worth paying for one of these.
Why, when it’s entirely free to do with PlatformIO?

I just tested it in PlatformIO, works fine.
 
The following users thanked this post: Wilson__

Offline tooki

  • Super Contributor
  • ***
  • Posts: 12041
  • Country: ch
Re: Tracing Arduino .ino program with breakpoint
« Reply #12 on: June 21, 2024, 10:25:18 am »
I never used a debugger on my microcontroller projects, and at some times it may have been useful, I do not miss it very much.

More complex algorithm (for example verifying of CRC algorithms) I first debug on my PC and only put it in a microcontroller when the code is correct.

When interacting with microcontroller hardware, classical debugging (break points stepping though code) is often also of limited use because the debugger completely upsets timing.

Sometimes I push out some data through an unused peripheral (Uart, SPI) or even some spare generic I/O pins and catch that with a logic analyzer. This way you can follow the code execution path in the microcontroller with very little changes in timing. It's also easy to combine this with a few #define macro's so your debug code can either be defined into oblivion for the normal program flow, or defined into debug statements for the debugging. For an AVR processor, starting with an EUR10 "Saleaea" clone logic analyzer, and using it with Sigrok / Pulseview is a good start. This should be in anyone's toolbox when programming / debugging microcontroller stuff.
While I don’t disagree with the other approaches you mention — I also debug algorithms on a computer (actually, I usually just use onlineGDB), and a logic analyzer is definitely helpful. But your post makes it sound as though interactive debugging is practically pointless on an MCU, and I definitely disagree with that — and clearly, so do many others, since there’s a huge industry around MCU debugging.

Given that for many MCUs, the barrier to entry to hardware debugging is so low,* it seems silly to me to not include it in your toolbox. It won’t solve every problem, but it is decidedly useful.

*The official ESP32 debugger is $10. STM32 has a few options, the cheapest being around $30 IIRC — or get a Nucleo board for $20 that has it built in! For PIC and AVR, a picKIT is under $100. The software debugger for AVR in Arduino is free. If it were a multi-hundred-dollar investment I wouldn’t bother, either, but for these peanuts, why not?
 
The following users thanked this post: Wilson__

Offline SteveThackery

  • Regular Contributor
  • *
  • Posts: 109
  • Country: gb
Re: Tracing Arduino .ino program with breakpoint
« Reply #13 on: June 21, 2024, 12:21:47 pm »
Many thanks, Steve.

Does visualmicro has typical functions of hardware debugger, break point, single step, sep into, step out,...   

Watch/inspect/edit variable decoded as data type (char, int...) and c typedef structure,  array, etc.?

Sorry for the late reply.  Yes to all of those, except I need to check the exact situation with arrays, structures, etc, because my memory is unreliable. You have probably already checked it for yourself by now - the VisualMicro documentation is excellent.

I'm afraid I can't comment on PlatformIO, as I've never used it, but VisualMicro have made a comparison chart between VisualMicro, PlatformIO and the Arduino IDE.  Obviously they are biased, but it is pretty clear that paying the extra for VisualMicro gets you some useful features over PlatformIO.  Only you can decide which is best for you.

It is true that software debugging has limitations around timing, etc, but honestly that's mostly the nature of debugging microcontrollers. Most of them operate effectively as real-time devices. Hardware debugging has similar limitations. In any case, for the great majority of Arduinos it's software debugging or nothing (apart from the age-old trick of sticking print statements all over the place).  :palm:
 
The following users thanked this post: thm_w, Wilson__

Offline tooki

  • Super Contributor
  • ***
  • Posts: 12041
  • Country: ch
Re: Tracing Arduino .ino program with breakpoint
« Reply #14 on: June 21, 2024, 04:12:15 pm »
I'm afraid I can't comment on PlatformIO, as I've never used it, but VisualMicro have made a comparison chart between VisualMicro, PlatformIO and the Arduino IDE.  Obviously they are biased, but it is pretty clear that paying the extra for VisualMicro gets you some useful features over PlatformIO.
Wow, that comparison table is disingenuous.

“Arduino IDE Compatible   
Yes - Visual Micro is fully compatible with the Arduino IDE, allowing backwards compatibility for your projects.   
Yes - All Arduino IDE Sketches will work in the Arduino IDE   
No - PlatformIO requires converting your INO to CPP, which makes it incompatible with the Arduino IDE” — outright false. Though it is true that PlatformIO is optimized to use .cpp files, I know for a fact that you can open .ino files in PlatformIO and they will compile and run. It knows that if it encounters a .ino file, to do what Arduino IDE (and VisualMicro) know, which is to silently insert the Arduino.h header and tweak some compiler flags or something.



“Supports Single INO   
Yes - Visual Micro supports INO Sketches fully   
Yes - Arduino IDE supports INO Sketches fully   
No - PlatformIO requires conversion from INO to CPP and additional headers adding [sic].”
Nope. It doesn’t require it. And isn’t this just the same thing as above worded differently?

If you do choose to convert, converting to .cpp requires one single change: inserting the “#include <arduino.h>” line. AFAIK, adding that to a .ino file won’t hurt it even in Arduino IDE. Explicitly adding it to a .ino file in PlatformIO gives the IntelliSense engine the header to provide code autocompletion and gets rid of squiggly underlines.

“Supports Multiple INO   
Yes - Visual Micro allows multiple INO files in line with the Arduino Specification   
Yes - Arduino IDE supports multiple INO files in tabs   
No - PlatformIO requires conversion from INO to CPP and #include statements using [sic]”
Could be. I never tried not using #include statements in Arduino IDE…

“Sharing of variables and methods within multiple .ino files   
Yes - Visual Micro allows sharing of variables and functions between your INO files   
Yes - Arduino IDE allows sharing of variables and functions between your INO files   
No - PlatformIO requires conversion from INO to CPP and #include statements using [sic]”
Isn’t this just the same thing worded differently?

“Use Arduino Examples (without code change)
Yes - All examples can be used in Visual Micro with no alterations   
Yes - All examples can be used in Arduino IDE with no alterations    
No - Examples have to be modified to work in PlatformIO”
And
“Use any historic Arduino project from the web (without code change)
Yes - All historic Arduino compatible projects are supported by Visual Micro without modification   
Yes - All historic project are supported by the Arduino IDE without modification   
No - PlatformIO requires modification to INO files as a minimum to use Arduino projects from the web”
The same point two more times, reworded again, and still mostly untrue.

“Use same hardware configuration as the Arduino IDE   
Yes - All Arduino board options and board packages are fully supported in Visual Micro   
Yes - All board options and board packages are supported by the Arduino IDE   
No - Configuration is performed via an INI file with different options to standard Arduino pacakges”
True, but this is a good thing, in that it’s way more rational.

“Use same library configuration as the Arduino IDE   
Yes - All Arduino Library specification options are supported in Visual Micro for seamless integration   
Yes - All Arduino Library specification options are supported in the Arduino IDE   
No - Libraries may differ for PlatformIO configurations and often require additional files adding to the package”
Library management in PlatformIO is one of its biggest strengths! Arduino IDE’s global-only library management, with no concern for libraries using the same file names, is absolutely bonkers bad, and one of the strongest arguments for switching to PlatformIO. Visual Micro continuing that tradition is nothing to brag about.


“Cross-compatible code editing with Arduino IDE   
Yes - You can edit code in Visual Micro and the Arduino IDE without any changes   
Yes - You can edit code in the Arduino IDE without modification from Visual Micro   
No - Code changes are required to use the Arduino IDE again after PlatformIO”
Utter nonsense as far as I can tell.

If anything, code breaks in Arduino IDE even when coming directly from Arduino IDE because of missing or conflicting libraries, and Arduino IDE has no way to fix that. PlatformIO, thanks to knowing exactly which libraries you use, will go and grab them, and one project’s libraries won’t conflict with another’s.

“Requires additional config outside of normal Arduino usage   
No - All Arduino configuration is supported in Visual Micro without changes   
No - All Arduino configuration is supported in the Arduino IDE   
Yes - Additional INI file configuration is required which is not available for Arduino projects”
A tiny, tiny bit true. But mostly it’s just that the configuration is stored differently than in Arduino IDE. There’s still a config file, it’s just not normally edited as text by the user.

“Automatic Prototype Generation   
Yes - Function Prototypes are generated automatically, and can be disabled if required (Optional)   
Yes - Function Prototypes are generated automatically   
No - Function Prototypes must be written with the code for correct compilation”
When you use PlatformIO with .ino files, it behaves like Arduino IDE and generates prototypes automatically.

“Supports Shared Projects   
Yes - Shared Projects can be used to modularize code and use it in multiple cross platform projects   
No - Only libraries are available in the Arduino IDE   
No - Only libraries are avaiable in PlatformIO”
Or a user can learn to use #include…

Intellisense Code Suggestions   
Yes - Code Suggestions are generated in Visual Micro as you type   
Yes - Code Suggestions are available in the Arduino IDE 2.0   
Yes - Code Suggerstions are shown in PlatformIO as you type”
Not mentioned: that the code suggestions in Arduino IDE are SLOOOOOW. I don’t know what’s happening behind the scenes, but it sometimes makes me think it’s actually compiling the code constantly, and since it can’t compile as fast as I can type, it gets behind.

No such trouble in PlatformIO or Visual Studio.


“Provides a Serial/USB debugger (No code changes, all boards)
Yes - Visual Micro's unique Serial Debugger is simple to setup, and requires no code changes to your project, with many features available'   
No - Only Serial.println() can be used in the Arduino IDE, which requires modification to your sketch   
No - Only Serial.println() can be used in PlatformIO, which requires modification to your sketch”
Nice little “no code changes, all boards” qualifier, since both Arduino IDE and PlatformIO support debugging with no code changes on some boards, and support it with tiny code changes on other boards.

“Supports Hardware Debug Probes   
Yes - Visual Micro supports a wide variety of debug probes in a few clicks, as well as custom configuration available to suit any scenario    
Yes - Arduino IDE 2 supports some debug probes with some configuration required for some boards   
Yes - PlatformIO supports a range of debug probes using the INI configuration system”
Maybe it has an advantage here. I’d have to test it. Debugging in PlatformIO is very easy to configure for some boards (like SMT32), fickle for others (like ESP32).



“On-PC Unit Testing (without code change)   
Yes - PC based Unit Testing Projects are supported in Visual Micro to allow cross platform compatible code to be tested without the hardware being required   
No - No PC based Unit Testing features are currently available in the Arduino IDE   
No - No PC based Unit Testing features are currently available in PlatformIO”
And
“On Chip Unit Testing (with Code Changes)   
No - Currently no On-Chip Unit Testing is available in Visual Micro   
No - Currently no On-Chip Unit Testing is available in the Arduino IDE   
Yes - PlatformIO supports On-Chip unit testing via Serial using a Library which requires code changes”
I have zero experience with unit testing so I can’t comment on these.


But basically, that table is designed to make PlatformIO appear WAY inferior to both Visual Micro and Arduino IDE (!), when that certainly is not the case overall.

It certainly does seem as though they designed Visual Micro to be absolutely round-trip compatible with Arduino IDE with zero configuration changes needed. But migrating to (or from) PlatformIO is not the nightmare they try to portray it as.

What is a nightmare is Arduino IDE’s library management, if you can call it “management”. It’s more like a collection of toddlers thrown into one sandbox. PlatformIO actually manages libraries, including versioning and dependencies, and does it per-project, so that projects don’t trample on each other, and so that projects will always be able to compile later. I’ve seen too many Arduino projects that no longer compile because the author never documented which libraries they used, and the names of the header files are often not sufficient to identify the correct library. (For example, there are numerous DS3231 RTC libraries that all call themselves ds3231.h, but are incompatible with each other. Never mind that each one could have incompatible versions of itself!)
 
The following users thanked this post: Wilson__

Offline thm_w

  • Super Contributor
  • ***
  • Posts: 6713
  • Country: ca
  • Non-expert
Re: Tracing Arduino .ino program with breakpoint
« Reply #15 on: June 21, 2024, 10:44:01 pm »
Why, when it’s entirely free to do with PlatformIO?

I just tested it in PlatformIO, works fine.

If its hobby purposes, absolutely go for it. If its for work, and spending $100 on a license saves an hours time, its worth it to me.
I've spent many many hours screwing around with platformIO launch.json settings (not for arduino, other projects). I'm not a programmer so I lack experience with these things and easily get hung up on them.

Good point about the library versioning.
Profile -> Modify profile -> Look and Layout ->  Don't show users' signatures
 
The following users thanked this post: Wilson__

Offline Wilson__Topic starter

  • Regular Contributor
  • *
  • Posts: 92
  • Country: gb
Re: Tracing Arduino .ino program with breakpoint
« Reply #16 on: June 22, 2024, 06:45:52 am »
Many thanks for testing it out.

I have platformio installed time ago for a brief trail.  Not really using it. 

Please advises pointer to information on where I need to continnue to get "Arduino ATMEGA2560" doing breakpoint

Either hardware or software breakpoit is fine.  I need to trace and follow the flow of a piece of old code that is working fine.  No need for speed.  Just steping through the code.   Many thanks
 

Offline tooki

  • Super Contributor
  • ***
  • Posts: 12041
  • Country: ch
Re: Tracing Arduino .ino program with breakpoint
« Reply #17 on: June 22, 2024, 07:59:08 am »
If its hobby purposes, absolutely go for it. If its for work, and spending $100 on a license saves an hours time, its worth it to me.
I've spent many many hours screwing around with platformIO launch.json settings (not for arduino, other projects). I'm not a programmer so I lack experience with these things and easily get hung up on them.

Good point about the library versioning.
FWIW, I’m basically a non-programmer, too. But I found PlatformIO to be quite easy to wrap my head around.

I absolutely understand about the value of saving time.

But yeah, that Visual Micro uses the Arduino way of “managing” libraries is lunacy IMHO. That alone makes it worth switching to PlatformIO if you ask me. Even just among dinky hobby projects, I’ve got old Arduino projects I can’t compile anymore because I can’t find the library I used, and none of the ones in library manager work. (And switching them around risks breaking other projects!) That problem is greatly reduced in PlatformIO, where you can even specify a particular version of a library for a particular project. And it is more intuitive to me how to add manually modified libraries to a project, since VS Code simply shows you your folder structure.

With that said, depending on how professional a project needs to be, I might not use Arduino at all. Overall I like that platform, but one thing that bugs me about it is how slow all of the available IDEs are to compile. Arduino compiles fastest for AVR, it seems, but when you compare the IDEs from the MCU vendors, the compile-upload-debug cycles are waaaay shorter, and debugging seems more reliable. (At least among the ones I’ve used so far, namely MPLAB X for PIC, and Keil for STM32.) The last MCU-based projects I did were with PIC (because my boss told me to) and after a short learning curve, I was coding just as (in)effectively as I would in PlatformIO or Arduino IDE. :p
 
The following users thanked this post: thm_w, Wilson__

Offline tooki

  • Super Contributor
  • ***
  • Posts: 12041
  • Country: ch
Re: Tracing Arduino .ino program with breakpoint
« Reply #18 on: June 22, 2024, 08:34:39 am »
Many thanks for testing it out.

I have platformio installed time ago for a brief trail.  Not really using it. 

Please advises pointer to information on where I need to continnue to get "Arduino ATMEGA2560" doing breakpoint

Either hardware or software breakpoit is fine.  I need to trace and follow the flow of a piece of old code that is working fine.  No need for speed.  Just steping through the code.   Many thanks
I already gave you that, for PlatformIO, in a post above. What exactly is still unclear now?
 
The following users thanked this post: Wilson__


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf