unused code hangs program

chromebin
Posts: 56
Joined: Wed Feb 07, 2018 3:53 pm

unused code hangs program

Postby chromebin » Thu Sep 13, 2018 5:07 pm

Update: my test seemed to indicate a suggested solution (see below), but when applied to my app, the code continues to hang. So I do not have a solution :(

I had have this weird situation.

Starting point: some code working normally.
What I did: included a trivial 4 line function with settimeofday() in it. I only added the code of the function, no calls!
Result: compiled normally (as expected), but hangs right after:

I (304) cpu_start: Pro cpu start user code"

Instead of the regular:

I (304) cpu_start: Pro cpu start user code
I (97) cpu_start: Starting scheduler on PRO CPU.

In other words before the code could output something it hang [edit: this is due to hanging in app_main, before the scheduler is started].

The Solution (or so I thought), include:

#include "time.h"

Note I had only:

#include "sys/time.h"

Which was sufficient to compile.
I presume there is an ugly bug lurking somewhere (tested on ESP32-WROOM).
Last edited by chromebin on Thu Sep 13, 2018 7:29 pm, edited 4 times in total.

chromebin
Posts: 56
Joined: Wed Feb 07, 2018 3:53 pm

Re: unused code hangs program?

Postby chromebin » Thu Sep 13, 2018 5:18 pm

The function is:

Code: Select all

#include "time.h"
#include "sys/time.h"

//...

void esp32_rtc_init(struct tm *rtc_time)
{
    struct timeval t;
    t.tv_sec = mktime(rtc_time);
    t.tv_usec = 0;
    settimeofday(&t, 0);            // merely linking it stops MCU after boot
}
As indicated, when this function is included in the source (never called), my code no longer runs.
When I comment out the call to settimeofday() the code compiles and runs as intended.
Last edited by chromebin on Thu Sep 13, 2018 9:34 pm, edited 1 time in total.

chromebin
Posts: 56
Joined: Wed Feb 07, 2018 3:53 pm

Re: unused code hangs program?

Postby chromebin » Thu Sep 13, 2018 7:25 pm

The problem isn't exclusive to settimeofday(). I now have code, involving access to GPIO pins, that, when I comment it out (no other changes), makes the code run.
Last edited by chromebin on Thu Sep 13, 2018 9:34 pm, edited 1 time in total.

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

Re: unused code hangs program

Postby chegewara » Thu Sep 13, 2018 8:39 pm

There is not much information to propose solution, but. Maybe you have task which quits without vTaskDelete()?

chromebin
Posts: 56
Joined: Wed Feb 07, 2018 3:53 pm

Re: unused code hangs program

Postby chromebin » Thu Sep 13, 2018 9:50 pm

It's always good to get a response, maybe there is an angle to this, thanks Chegewara.

What baffles me is that the code is in an unused function.

I demoed it to a collegue, otherwise I'd doubt my own sanity right now ;) I simply commented out an unused function and code went from hanging to working.

I thought maybe mentioning settimeofday() (in the code) pulled in some lib as a side-effect.

I guess I have to continue with baby-steps to get hints of how things break.

ESP_Angus
Posts: 1318
Joined: Sun May 08, 2016 4:11 am

Re: unused code hangs program

Postby ESP_Angus » Fri Sep 14, 2018 12:05 am

That's a bit odd!

There are some situations where changing the source code input to the compiler can move things around in the linker output, and trigger bugs like memory corruption which were harmless before.

Can you please post some more info:

- What IDF version?
- If you turn up the default log level to Debug, do you get any clues?
- What is the full log output (at Debug level), both from a boot which "hangs" and a boot which succeeds?
- Is there any C++ code in your app? Any static objects with constructors?

chromebin
Posts: 56
Joined: Wed Feb 07, 2018 3:53 pm

Re: unused code hangs program

Postby chromebin » Fri Sep 14, 2018 4:46 pm

ESP_Angus wrote:That's a bit odd!

There are some situations where changing the source code input to the compiler can move things around in the linker output, and trigger bugs like memory corruption which were harmless before.

Can you please post some more info:

- What IDF version?
- If you turn up the default log level to Debug, do you get any clues?
- What is the full log output (at Debug level), both from a boot which "hangs" and a boot which succeeds?
- Is there any C++ code in your app? Any static objects with constructors?
Hi Angus, thanks for responding.

I have moved on, taking baby-steps, but have nothing conclusive yet. When the problem reappears I'll report, using your tips.

There is no C++ code. No constructors. The app uses Wifi but doesn't get to its initialization. I was testing new code in app_main, to avoid running the rest of the app (and the printf noise that generates).

But I did notice there was a loop in my testcode that could be endless under some condition. I presume it might have hung there. But I do not recall the specifics of the condition, maybe there was a strange dependence on code alignment or data at some location. I could not see output from printf from before that code.

The weirdest problems often have the very simple causes.

As said, if the problem recurs I'll report back in. Thanks again!

chromebin
Posts: 56
Joined: Wed Feb 07, 2018 3:53 pm

Re: unused code hangs program

Postby chromebin » Mon Sep 17, 2018 1:55 pm

The situation is different now, as the code links. But the weird behaviour again involves settimeofday(). The program, including the code below, links:

Code: Select all

void esp32_rtc_init(struct tm *rtc_time)
{
    struct timeval t;
    t.tv_sec = mktime(rtc_time);
    t.tv_usec = 0;
    printf("SETTIMEOFDAY");
    settimeofday(&t, 0);   
}
But generates the following output (and nothing more):

Code: Select all

rst:0x1 (POWERON_RESET),boot:0x17 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:4924
load:0x40078000,len:8920
load:0x40080400,len:5824
entry 0x400806f4
I (269) cpu_start: Pro cpu up.
I (269) cpu_start: Single core mode
I (269) heap_init: Initializing. RAM available for dynamic allocation:
I (273) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (279) heap_init: At 3FFC3478 len 0001CB88 (114 KiB): DRAM
I (285) heap_init: At 3FFE0440 len 00003BC0 (14 KiB): D/IRAM
I (291) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (298) heap_init: At 40090264 len 0000FD9C (63 KiB): IRAM
I (304) cpu_start: Pro cpu start user code
Yes, prints nothing at all from user code.

If I comment out the call to settimeofday():

Code: Select all

void esp32_rtc_init(struct tm *rtc_time)
{
    struct timeval t;
    t.tv_sec = mktime(rtc_time);
    t.tv_usec = 0;
    printf("SETTIMEOFDAY");
//    settimeofday(&t, 0);
}
then the program works normally and results in the expected output:

Code: Select all

rst:0x1 (POWERON_RESET),boot:0x17 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:4924
load:0x40078000,len:8920
load:0x40080400,len:5824
entry 0x400806f4
I (269) cpu_start: Pro cpu up.
I (269) cpu_start: Single core mode
I (269) heap_init: Initializing. RAM available for dynamic allocation:
I (273) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (279) heap_init: At 3FFC3478 len 0001CB88 (114 KiB): DRAM
I (285) heap_init: At 3FFE0440 len 00003BC0 (14 KiB): D/IRAM
I (291) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (298) heap_init: At 40090264 len 0000FD9C (63 KiB): IRAM
I (304) cpu_start: Pro cpu start user code
I (97) cpu_start: Starting scheduler on PRO CPU.
APP-HELLO
PININIT
RTCINIT
SETTIMEOFDAY
etc
Like before, when the program doesn't work, there is NO output from user code. This is a bad place to be in, regardless of the error.

I think this isn't a uniquely uncommon behaviour though: in my experience when messing with peripheral setup, things can go dramatically wrong in the same way. But here the only difference is a seemingly innocent call to settimeofday(), which should be a (very?) simple function. What is happening??

ESP_Angus
Posts: 1318
Joined: Sun May 08, 2016 4:11 am

Re: unused code hangs program

Postby ESP_Angus » Mon Sep 17, 2018 11:20 pm

chromebin wrote:What is happening??
I don't know, but my guess would be silent memory corruption somewhere else which is sometimes breaking everything when you call settimeofday().

Are you able to post a full project that (at least sometimes) exhibits this problem, somewhere?

Angus

ESP_igrr
Posts: 1260
Joined: Tue Dec 01, 2015 8:37 am

Re: unused code hangs program

Postby ESP_igrr » Mon Sep 17, 2018 11:41 pm

Also, try setting log level to verbose, and see if there is any more useful output from the startup code. This might indicate where things go wrong.

Who is online

Users browsing this forum: Baidu [Spider] and 21 guests