Code Monkey home page Code Monkey logo

arduinotrace's Introduction

A quote from Brian Kernighan

ArduinoTrace

arduino-library-badge Continuous Integration

A dead-simple tracing library to debug your Arduino programs.

Example

All you need to do is call TRACE() or DUMP(variable).

#include <ArduinoTrace.h>

int value = 0;

void setup() {
  Serial.begin(9600);
  TRACE();
}

void loop() {
  value++;
  DUMP(value);
  BREAK();
}

The program above would print:

MyProgram.ino:7: void setup()
MyProgram.ino:12: value = 1
MyProgram.ino:13: BREAK! (press [enter] to continue)
MyProgram.ino:12: value = 2
MyProgram.ino:13: BREAK! (press [enter] to continue)
MyProgram.ino:12: value = 3
MyProgram.ino:13: BREAK! (press [enter] to continue)
...

Features

  • TRACE() prints:
    • filename
    • line number
    • function's name
    • function's parameters
    • template parameters (if any)
  • DUMP(variable) prints:
    • filename
    • line number
    • variable's name
    • variable's value
  • BREAK() pauses the program until you send a line-break to the Serial
  • TRACE() and DUMP(variable) work at global scope, provided that you call ARDUINOTRACE_INIT() to initialize the Serial port.
  • Flushes the Serial port to make sure that each line is complete
  • Uses Flash memory when possible
  • Deduplicates strings a much as reasonable feasible
  • Header-only
  • Roughly 200 lines of code

A simple recipe to find where the code crashes

  1. sprinkle your code with TRACE() and DUMP(variable)
  2. run the program
  3. view all traces in the Serial monitor
  4. repeat the process until you find the line that causes the crash

Configuration

Setting Default Description
ARDUINOTRACE_ENABLE 1 Determines whether the library is active. If set to 1, ArduinoTrace is active, and prints traces to the serial port. Set this value to 0 to disable all the traces at once.
ARDUINOTRACE_ENABLE_PROGMEM 1 Determines whether the strings are stored in Flash or RAM. If defined to 1 (it's the default), ArduinoTrace places the string in the Flash memory to reduce the memory consumption. Only set this value to 0 if you have a compilation issue.
ARDUINOTRACE_ENABLE_FULLPATH 0 Determines how the filename is written. If set to 1, ArduinoTrace prints the full path of the file. If set to 0, ArduinoTrace only prints the filename.
ARDUINOTRACE_SERIAL Serial Define the serial port to use. Change this value to use an alternative serial port, for example, SerialUSB.

To change one of the settings above, you must define the symbol before including the library. For example:

#define ARDUINOTRACE_ENABLE 0  // Disable all traces
#include <ArduinoTrace.h>

FAQ

Is there a performance impact?

Of course, there is! Your program will become fat and slow, so it's essential to use this library only when debugging.

You should never use it in production.

You should never commit a program with traces.

Does this library replace my logging library?

Absolutely not! Tracing and logging are different things.

Logging is recording (possibly in an SD card) the important things that happen in a program so that we can do a post-mortem analysis, in case something goes wrong.

Tracing is recording every little step to narrow down the area of analysis when you're fixing a bug. It's a technique that you use for short periods of time, during a debugging session. Again, you should not commit code that contains traces.

In short: logging is something you do in production, tracing is something you do while debugging.

Why not use a debugger instead?

Sometimes, you cannot use a debugger. In this case, you can always go back to the good-old tracing technique.

And what about EspExceptionDecoder?

EspExceptionDecoder is an awesome tool, but unfortunately, the results are not always accurate; you often see unrelated function names in the stack. I don't know why that happens (I guess it's due to compiler optimizations), but whatever the reason, it's still good to have a fallback option.

Tutorials

Want to see how this library works?

Youtube video: How to debug any Arduino program with tracing

arduinotrace's People

Contributors

bblanchon avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

arduinotrace's Issues

Adding options to configure the output

Adding options to configure the output would be a nice feature.

For example in most cases you don't need the filename, because it is mostly the "main" *.ino file

Request: Feature to remove traces without requiring re-fixing

Your video 12:57 shows you retyping the fix after tracing it. you just did your bug-fix TWICE. Too much work, and what if you forget the fix? What if the fix is many small changes? Please add feature to remove dumps and traces automatically, instead of programmer having to revert and then retype their fix. thx!

Request to add break command

It would be useful to have a BREAK() command to "stop" execution and wait for a CR input to continue.

It would stop your trace output to allow you to view it, then continue with a CR instead of trying to catch it by stopping scrolling.

I prototyped it and it works great!
void BREAK(){
Serial.print("BREAK hit enter");
while (!(Serial.available() > 0)){} //wait for input

int incomingByte = Serial.read();
}

[question/suggestion] can print stack backtraces?

hi.... could be a way to print something like a stack backtrace? ... i mean, a backward chain of function calls in a given point,,,, imagine something like

void setup(){
  Serial.begin(9600);
  subSetup1();
}

void loop(){
  subLoop1();
}

void subSetup(){
  STACK();
}

void subLoop1(){
  subLoop2();
}

void subLoop2()
  STACK("running");
  BREAK();
}

... that prints something like...

mySketch.ino:11: stack[0] "" = void subSetup()
mySketch.ino:3: stack[1] "" = void setup()
mySketch.ino:19: stack[0] "running" = void subLoop2()
mySketch.ino:15: stack[1] "running" = void subLoop1()
mySketch.ino:7: stack[2] "running" = void loop()

that will look like a collection of TRACE()'s... that would be very useful to follow the program flow and tracking back possible errors or understand the program better.... but i don't imagine how this could be achieved, i am not skilled in c/c++

i got the idea from java, where you can specifically print a backtrace anytime at will, and that backtrace contains all function calls from current point, passing by all involved functions, back to main() ... https://en.wikipedia.org/wiki/Stack_trace

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.