Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print source location for panics when using -monitor #3673

Merged
merged 2 commits into from
Apr 26, 2023

Conversation

aykevl
Copy link
Member

@aykevl aykevl commented Apr 24, 2023

This PR adds the ability to print source locations for runtime panics. Here is an example:

$ tinygo flash -target=circuitplay-bluefruit -monitor ./examples/heartrate
Connected to /dev/ttyACM0. Press Ctrl-C to exit.
tick 00:00.810
tick 00:01.587
tick 00:02.387
tick 00:03.244
panic: runtime error at 0x00027c4d: alloc in interrupt
[tinygo: panic at /home/ayke/src/tinygo/bluetooth/adapter_sd.go:74:4]

To be clear, this path isn't stored on the microcontroller. It's stored as part of the build, and -monitor just looks up the path from the panic message.

Possible enhancements:

  • Print such an address for regular panics as well. I'm not sure that's so useful, as it's usually a lot easier to look up panics just by their message.
  • Use runtimePanicAt (instead of runtimePanic) in other locations, if that proves to be beneficial.
  • Print the TinyGo-generated output in some other color, to distinguish it from the regular console output.
  • Print more details when panicking (registers, stack values), and print an actual backtrace.

@deadprogram
Copy link
Member

@aykevl looks like something failing the various CI tests, maybe a file that was not added?

@aykevl aykevl force-pushed the print-panic-address branch 2 times, most recently from 7cfceb5 to 02eed71 Compare April 25, 2023 13:09
@aykevl
Copy link
Member Author

aykevl commented Apr 25, 2023

Fixed, but I can't really test the change because of #3674. At least not on my Circuit Playground Bluefruit.

@deadprogram
Copy link
Member

deadprogram commented Apr 25, 2023

Looks like CI still failing.

@aykevl
Copy link
Member Author

aykevl commented Apr 25, 2023

Fixed, MachO is problematic again and I'm not really interested in fixing that. It's ELF that's important (used on Linux and baremetal).

@aykevl
Copy link
Member Author

aykevl commented Apr 25, 2023

Breaks on Windows too, so I'll limit testing on Linux only. Which is all we care about, really (we care about the ELF format which isn't used on MacOS or Windows).

@deadprogram
Copy link
Member

@aykevl can you please resolve merge conflict?

This is not very useful in itself, but makes it possible to detect this
address in the output. See the next commit.

This adds around 50 bytes to each binary (except for AVR and wasm). This
is unfortunate, but I think this feature is quite useful still.
A future enhancement might be to create a build tag for extended panic
information that's not set by default.
The previous commit started printing the instruction address for runtime
panics. This commit starts using this address to print the source
location.

Here is an example where this feature is very useful. There is a heap
allocation in the Bluetooth package, but we don't know where exactly.
Printing the instruction address of the panic is already useful, but
what is even more useful is looking up this address in the DWARF debug
information that's part of the binary:

    $ tinygo flash -target=circuitplay-bluefruit -monitor ./examples/heartrate
    Connected to /dev/ttyACM0. Press Ctrl-C to exit.
    tick 00:00.810
    tick 00:01.587
    tick 00:02.387
    tick 00:03.244
    panic: runtime error at 0x00027c4d: alloc in interrupt
    [tinygo: panic at /home/ayke/src/tinygo/bluetooth/adapter_sd.go:74:4]

To be clear, this path isn't stored on the microcontroller. It's stored
as part of the build, and `-monitor` just looks up the path from the
panic message.

Possible enhancements:

  - Print such an address for regular panics as well. I'm not sure
    that's so useful, as it's usually a lot easier to look up panics
    just by their message.
  - Use runtimePanicAt (instead of runtimePanic) in other locations, if
    that proves to be beneficial.
  - Print the TinyGo-generated output in some other color, to
    distinguish it from the regular console output.
  - Print more details when panicking (registers, stack values), and
    print an actual backtrace.
@aykevl
Copy link
Member Author

aykevl commented Apr 26, 2023

Rebased.

@deadprogram
Copy link
Member

$ tinygo flash -target=circuitplay-bluefruit -monitor ./examples/heartrate                                                                        
Connected to /dev/ttyACM0. Press Ctrl-C to exit.                                                                                                  
tick 00:01.588                                                                                                                                    
tick 00:02.388                                                                                                                                    
tick 00:03.245                                                                                                                                    
tick 00:03.976
...
tick 00:53.848
tick 00:54.771
tick 00:55.512
tick 00:56.323
tick 00:57.123
tick 00:57.902
tick 00:58.811

It never seems to fail?

@aykevl
Copy link
Member Author

aykevl commented Apr 26, 2023

IIRC it only fails when you actually connect to it. You can try connecting using examples/heartrate-monitor for example.

@deadprogram
Copy link
Member

it only fails when you actually connect to it.

Thanks. It works (fails?) as expected.

Copy link
Member

@deadprogram deadprogram left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well worth the small increase in size to be able to figure out what is going on!

@deadprogram
Copy link
Member

Now merging, thanks @aykevl

@deadprogram deadprogram merged commit ae381e7 into dev Apr 26, 2023
@deadprogram deadprogram deleted the print-panic-address branch April 26, 2023 16:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants