Skip to content

Conversation

sjanc
Copy link
Contributor

@sjanc sjanc commented Oct 7, 2025

If Zephyr process was terminated due to segmentation fault print note and backtrace for debugging purposes.

*** Booting Zephyr OS build v4.2.0-5260-g9a778a6beff7 ***
[00:00:00.000,000] <dbg> bttester: tester_init: Initializing tester
[00:00:00.000,000] <err> bttester_main: SEGMENTATION FAULT (address (nil))
zephyr/build/zephyr/zephyr.exe() [0x8049987]
linux-gate.so.1(__kernel_rt_sigreturn+0x0) [0xf7f695b0]
zephyr/build/zephyr/zephyr.exe() [0x804a8dc]
zephyr/build/zephyr/zephyr.exe() [0x8049fc5]
zephyr/build/zephyr/zephyr.exe() [0x80499fb]
zephyr/build/zephyr/zephyr.exe() [0x80b2c24]
zephyr/build/zephyr/zephyr.exe() [0x8060166]
zephyr/build/zephyr/zephyr.exe() [0x80658ea]
zephyr/build/zephyr/zephyr.exe() [0x80b9a45]
/lib/libc.so.6(+0x72b20) [0xf7cd2b20]
/lib/libc.so.6(+0xf8c58) [0xf7d58c58]

@sjanc
Copy link
Contributor Author

sjanc commented Oct 7, 2025

when figured out on how to pass -rdynamic to linker we should also get nice functions names in trace too

@zephyrbot zephyrbot added area: Bluetooth Qualification Bluetooth Qualification -related issues and pull requests area: Bluetooth area: Tests Issues related to a particular existing or missing test labels Oct 7, 2025
@sjanc sjanc force-pushed the tester_backtrace branch 2 times, most recently from 61abb6e to bff0cbc Compare October 7, 2025 16:21
Thalley
Thalley previously approved these changes Oct 8, 2025
}
LOG_INF("==================================");

free(strings);
Copy link
Contributor

Choose a reason for hiding this comment

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

Given that we do a free here, I guess backtrace_symbols is doing a malloc?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, however I could use backtrace_symbols_fd() instead which doesn't use heap

int nptrs;

LOG_ERR("SEGMENTATION FAULT (address %p)", si->si_addr);
nptrs = backtrace(buffer, BACKTRACE_BUF_SIZE);
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider adding a check if nptrs == BACKTRACE_BUF_SIZE which may indicate that the output was truncated, and we could add a log entry or something to let us know that BACKTRACE_BUF_SIZE may need to be increased

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah, I'll add this, however BACKTRACE_BUF_SIZE is 100 :)


LOG_ERR("SEGMENTATION FAULT (address %p)", si->si_addr);
nptrs = backtrace(buffer, BACKTRACE_BUF_SIZE);
backtrace_symbols_fd(buffer, nptrs, 2);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
backtrace_symbols_fd(buffer, nptrs, 2);
backtrace_symbols_fd(buffer, nptrs, STDERR_FILENO);

Copy link
Contributor

Choose a reason for hiding this comment

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

May need to inlcude unistd.h for this

sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_SIGINFO;

(void)sigaction(SIGSEGV, &sa, NULL);
Copy link
Contributor

Choose a reason for hiding this comment

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

sigaction may return an error; suggest to actually verify this and LOG if it happens

We may also want to return that error to main and immediately stop the program if it fails to register?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ERRORS
       EFAULT act or oldact points to memory which is not a valid part of the process address space.

       EINVAL An invalid signal was specified.  This will also be generated if an attempt is made to change the action for SIGKILL or SIGSTOP, which cannot be caught or ignored.

so sigaction() won't fail here, and even if it would fail I don't think we should abort (it is just debug code after all)

Copy link
Contributor

Choose a reason for hiding this comment

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

Might be worth having an assert for it. Our coding guidelines require us to test all error codes (not sure if the coding guidelines actually apply to tests/test applications as well)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll add warning log for this

@sjanc sjanc force-pushed the tester_backtrace branch from ffa5d45 to 6c2bc0b Compare October 9, 2025 10:38
If Zephyr process was terminated due to segmentation fault print
note and backtrace for debugging purposes.

*** Booting Zephyr OS build v4.2.0-5260-g9a778a6beff7 ***
[00:00:00.000,000] <dbg> bttester: tester_init: Initializing tester
[00:00:00.000,000] <err> bttester_main: SEGMENTATION FAULT (address (nil))
zephyr/build/zephyr/zephyr.exe() [0x8049987]
linux-gate.so.1(__kernel_rt_sigreturn+0x0) [0xf7f695b0]
zephyr/build/zephyr/zephyr.exe() [0x804a8dc]
zephyr/build/zephyr/zephyr.exe() [0x8049fc5]
zephyr/build/zephyr/zephyr.exe() [0x80499fb]
zephyr/build/zephyr/zephyr.exe() [0x80b2c24]
zephyr/build/zephyr/zephyr.exe() [0x8060166]
zephyr/build/zephyr/zephyr.exe() [0x80658ea]
zephyr/build/zephyr/zephyr.exe() [0x80b9a45]
/lib/libc.so.6(+0x72b20) [0xf7cd2b20]
/lib/libc.so.6(+0xf8c58) [0xf7d58c58]

Signed-off-by: Szymon Janc <[email protected]>
@sjanc sjanc force-pushed the tester_backtrace branch from 6c2bc0b to 9f21fc4 Compare October 9, 2025 10:48
Copy link

sonarqubecloud bot commented Oct 9, 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: Bluetooth Qualification Bluetooth Qualification -related issues and pull requests area: Bluetooth area: Tests Issues related to a particular existing or missing test

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants