Wrap around ESP function

Stipa88
Posts: 20
Joined: Mon May 09, 2022 12:26 pm

Wrap around ESP function

Postby Stipa88 » Thu Oct 10, 2024 7:28 am

Hello everyone.

I want to build a wrapper around some ESP function. Inside the function wrapper, I will execute my own code and call the original function. For example i want to wrap esp_panic_handler function.

Inside the main CMakeLists.txt I have the following commands:
cmake_minimum_required(VERSION 3.16)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
set(application_name "PROJECT")
project(${application_name})
target_link_libraries(${project_elf} PRIVATE "-Wl,--wrap=esp_panic_handler")


Inside the project, for example in the folder MyPanic.c, I have a function defined

Code: Select all

void __real_esp_panic_handler(panic_info_t *info);

void __wrap_esp_panic_handler(panic_info_t *info)
{
	// My code ...
	__real_esp_panic_handler(info);
}
The compilation result is:

system.a(panic_handler.c.obj): in function `panic_handler':
C:/Espressif/frameworks/esp-idf-v5.0/components/esp_system/port/panic_handler.c:189: undefined reference to `__wrap_esp_panic_handler'
collect2.exe: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.

How to solve the problem, so that the linker "sees" the Wrap.. function.

Tny

nopnop2002
Posts: 112
Joined: Thu Oct 03, 2019 10:52 pm
Contact:

Re: Wrap around ESP function

Postby nopnop2002 » Thu Oct 10, 2024 2:43 pm

Your problem is very similar to this.
https://github.com/espressif/esp-idf/issues/7681

Stipa88
Posts: 20
Joined: Mon May 09, 2022 12:26 pm

Re: Wrap around ESP function

Postby Stipa88 » Thu Oct 10, 2024 7:17 pm

It's exactly what I need. Linking is a problem.

Error: C:/Espressif/frameworks/esp-idf-v5.0/components/esp_system/port/panic_handler.c:189: undefined reference to `__wrap_esp_panic_handler'

Please can someone check with esp-idf v5.0.7? Tnx

chegewara
Posts: 2378
Joined: Wed Jun 14, 2017 9:00 pm

Re: Wrap around ESP function

Postby chegewara » Fri Oct 11, 2024 5:13 pm

Please try this definition

Code: Select all

// void __real_esp_panic_handler(panic_info_t *info);
// void __wrap_esp_panic_handler(panic_info_t *info)
// {
// 	// My code ...
// 	__real_esp_panic_handler(info);
// }

void __real_esp_panic_handler(void *info);
void __wrap_esp_panic_handler(void *info)
{
    esp_rom_printf("\t\tpanic\n");
	// My code ...
	__real_esp_panic_handler(info);
}

and

Code: Select all

idf_build_set_property(LINK_OPTIONS "-Wl,--wrap=esp_panic_handler" APPEND)

Stipa88
Posts: 20
Joined: Mon May 09, 2022 12:26 pm

Re: Wrap around ESP function

Postby Stipa88 » Sat Oct 12, 2024 8:35 pm

When an abort occurs, what function prints
"0x4200e011: HelpBtn_EventCb at E:/Program/Screen/TestScreen.c:294"

I need to override that function, because I want to save that data in NVS in the call point of the abort() function.

This is the result of printing log messages when abort() occurs:

abort() was called at PC 0x4200e011 on core 0
0x4200e011: HelpBtn_EventCb at E:/Program/Screen/TestScreen.c:294

Core 0 register dump:
MEPC : 0x40380948 RA : 0x403873f8 SP : 0x3fcbff30 GP : 0x3fc90400
0x40380948: panic_abort at C:/Espressif/frameworks/esp-idf-v5.0/components/esp_system/panic.c:446

0x403873f8: __ubsan_include at C:/Espressif/frameworks/esp-idf-v5.0/components/esp_system/ubsan.c:313

TP : 0x3fc25b90 T0 : 0x37363534 T1 : 0x7271706f T2 : 0x33323130
S0/FP : 0x00000004 S1 : 0x3fcbff94 A0 : 0x3fcbff5c A1 : 0x3fcbff92
A2 : 0x00000000 A3 : 0x3fcbff89 A4 : 0x00000001 A5 : 0x3fcb9000
A6 : 0x7a797877 A7 : 0x76757473 S2 : 0x3fcbffe4 S3 : 0x00000001
....
ITD
...

Who is online

Users browsing this forum: Marcosss and 120 guests