esp-idf: Override ESP panic print handler (IDFGH-5996)
Is your feature request related to a problem? Please describe.
We want to be able to capture all the errors/panics that occur on our ESP32 based product. Is there a way to override the panic handler so that we can redirect output to our own console/logs and also keep a count of the number of panics etc.?
Describe the solution you’d like
The panic handler provides a user hook where it passes the formatted output, which can then be printed to a console or logged somewhere or sent over a network.
Describe alternatives you’ve considered
We’ve been trying to use the linker wrap option -Wl,--wrap=panic_print_char but this seems like a major hack.
Additional context
Add any other context or screenshots about the feature request here.
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 15 (2 by maintainers)
Hello @shreyasbharath ,
The simplest way I see to override or extend the panic handler is to wrap the panic handler function, and not
panic_print_char, thanks to the linker.I wouldn’t consider wrapping a function through the linker as a hack. After all, it is a static behavior, defined as compilation time, it is thus by definition safer than a dynamic behavior. Anyway, here is what I came up with:
In your project C file, create a wrapper for
esp_panic_handler:Remark, this function is called after enabling the cache, so it can be placed in flash.
In the
CMakeLists.txtof your project, add the following line betweeninclude($ENV{IDF_PATH}/tools/cmake/project.cmake)andproject(my_super_project):In the case of the hello-world example, it’ll look like:
Feel free to ask if you have any other question
Hi @mmoskal , I understand why one would need to do this, but the fact is this function is not meant to be wrapped. Defining it in another file would also make it non-inlinable.
One solution I can think of is force the inclusion of a header file when compiling that lib in order to make
panic_print_char()weak. Then, in your main component you will be able to define a strongpanic_print_char()function. All the references topanic_print_char()would then result to your function instead of the weak one.To do so, I will take the
get_started/hello_worldexample. In itsCMakeLists.txtfile, add the following line at the end:Near
CMakeLists.txtfile, defineweakprint.h, as:In
main/hello_world_main.c, you can now define:This is not equivalent to wrapping because you do have access to the weak function, but at least now it will replace all the occurences of
panic_print_char, even inesp_systemcomponent (wherepanic.cis part of)@shreyasbharath Glad it worked for you!
Do you mean documentation for wrapping any function from ESP-IDF or wrapping this panic function in particular?
Thank you for responding so quickly @o-marshmallow 😁
I’ll try this out and report back.