Strange stacktrace triggered by removing a log

mrsaiba
Posts: 2
Joined: Mon Jan 22, 2024 7:59 pm

Strange stacktrace triggered by removing a log

Postby mrsaiba » Mon Jan 22, 2024 8:19 pm

Hello!
So while coding a simple application via ESP-IDF on my ESP32-WROOM I encountered a problem with a stacktrace. The problem only reproduces when I remove a log from a specific area of code (but with at least one log everything it works). For the logging I use the dedicated macros ESP_LOGx (with a fflush(stdout) before and printfs the stacktrace still appears).
I can't figure it out why this happens. The message that is printed on the tty is the following one:

Code: Select all

Guru Meditation Error: Core  0 panic'ed (LoadProhibited). Exception was unhandled.

Core  0 register dump:
PC      : 0x400d70d5  PS      : 0x00060930  A0      : 0x800d6aba  A1      : 0x3ffb9c90  
A2      : 0x00000000  A3      : 0x00000001  A4      : 0x00000021  A5      : 0x00000022  
A6      : 0x00000001  A7      : 0x00000021  A8      : 0x00fb9fb0  A9      : 0x3ffb9c40  
A10     : 0x0000001f  A11     : 0x3ffb9c90  A12     : 0x00000200  A13     : 0x3ffb9f58  
A14     : 0x00ff0000  A15     : 0x0000000c  SAR     : 0x00000004  EXCCAUSE: 0x0000001c  
EXCVADDR: 0x00fb9fb4  LBEG    : 0x4000c46c  LEND    : 0x4000c477  LCOUNT  : 0x00000000  


Backtrace: 0x400d70d2:0x3ffb9c90 0x400d6ab7:0x3ffb9fb0 0x40155cd7:0x3ffba0a0 0x40089c99:0x3ffba0d0

I read about the EXCVADDR register, and I understood that a random value the following thing:
If this address is something else (garbage value, not in 0x3fxxxxxx - 0x6xxxxxxx range), it likely means that the pointer used to access the data is either not initialized or has been corrupted.
Source https://docs.espressif.com/projects/esp ... prohibited.

The code:

Code: Select all

char buffer[BUFFER_SIZE];
	esp_err_t result = ESP_FAIL;
	int read_memory;
	while ((read_memory = read(config->fd, buffer, 512)) > 0) { // needs to be greater then 0, because 0 means EOF
		int current_char = 0;
		int key_index = 0;
		int value_index = 0;
		char temp_key[KEY_MAX_LENGTH];
		char temp_value[VALUE_MAX_LENGTH];
		int compare_key_or_value = 0;
		while (buffer[current_char] != WD_EOL && current_char < read_memory) { // we assume that the user will never add more than 19 chars in the key
			if (compare_key_or_value == 0 && buffer[current_char] != WD_EQUAL_SIGN) {
				ESP_LOGE(TAG, "HELLO1");
				temp_key[key_index] = buffer[current_char]; 
				current_char++; // it is more visible if done here, than in the array
				key_index++;
				continue;
			}
			if (buffer[current_char] == WD_EQUAL_SIGN) {

				ESP_LOGE(TAG, "HELLO2");
				temp_key[current_char] = '\0'; // nobody should write more then 19 char for the name of the key
				if (strcmp(temp_key, key)) {
					ESP_LOGE(TAG, "Here");
					while (buffer[current_char++] != '\n' && current_char < read_memory);// consume until EOL, including EOL
					memset(temp_key, 0, KEY_MAX_LENGTH);
					memset(temp_value, 0, VALUE_MAX_LENGTH);
					key_index = 0;
					value_index = 0;

				}	else {

					compare_key_or_value = 1;
					current_char++;
				}
				continue;
			}
			if (compare_key_or_value == 1) {
				ESP_LOGE(TAG, "HELLO3");
				if (buffer[current_char + 1] == '\n') {
					temp_value[value_index] = '\0';
				ESP_LOGE(TAG, "HELLO4");
					strncpy(value, temp_value, value_index);
					result = ESP_OK;
					memset(temp_key, 0, KEY_MAX_LENGTH);
					memset(temp_value, 0, VALUE_MAX_LENGTH);
					key_index = 0;
					value_index = 0;
				}
				/* *************************************** 
				In this if I remove the logs the stacktrace occurs 
				 **************************************** */
				temp_value[value_index] = buffer[current_char];
				//ESP_LOGE(TAG, "1.Cur char: %i, value index: %i", current_char, value_index);
				current_char++;

				//ESP_LOGE(TAG, "2.Cur char: %i, value index: %i", current_char, value_index);
				value_index++;

				//ESP_LOGE(TAG, "3.Cur char: %i, value index: %i", current_char, value_index);
				/* *************************************** 
				End of section
				 **************************************** */
			}
		}
	}
I think that when the while loop reiterates the problme occurs, because when using printfs, all messages are printed.

Thank you.
Last edited by mrsaiba on Tue Jan 23, 2024 8:11 am, edited 1 time in total.

ESP_Sprite
Posts: 9727
Joined: Thu Nov 26, 2015 4:08 am

Re: Strange stacktrace triggered by removing by a log

Postby ESP_Sprite » Tue Jan 23, 2024 2:37 am

Can you decode that stacktrace? If you're using ESP-IDF, the easiest way is to use the built-in serial monitor ('idf.py monitor') as it automatically does that.

mrsaiba
Posts: 2
Joined: Mon Jan 22, 2024 7:59 pm

Re: Strange stacktrace triggered by removing by a log

Postby mrsaiba » Tue Jan 23, 2024 9:17 am

ESP_Sprite wrote:
Tue Jan 23, 2024 2:37 am
Can you decode that stacktrace? If you're using ESP-IDF, the easiest way is to use the built-in serial monitor ('idf.py monitor') as it automatically does that.
I'll do that when I arrive home. Thanks for your idea. :D

Who is online

Users browsing this forum: No registered users and 90 guests