<div dir="ltr"><div dir="ltr"><div>A small further update here to support the discussion further. It turns out that there is an undocumented function that, in contrast to _Unwind_Backtrace, allows passing a given stack:</div><div></div><div><p><i> In my practice standard _Unwind_Backtrace failed to switch to pre-signal stack. I've managed to get some before-signal stacks by calling internal
libgcc __gnu_Unwind_Backtrace - it has an extra argument being "current
registry values" - so it operates on given stack, not on current stack.</i></p></div><div></div><div><a href="https://stackoverflow.com/a/30515756/598057">https://stackoverflow.com/a/30515756/598057</a></div><div><br></div><div>The challenge with this is how to access the crashed stack frame, i.e., the right context has to be passed to __gnu_Unwind_Backtrace.</div><div><pre class="gmail-default gmail-s-code-block"><code class="gmail-hljs gmail-language-cpp"> _Unwind_Reason_Code
__gnu_Unwind_Backtrace(_Unwind_Trace_Fn trace, <span class="gmail-hljs-type">void</span> * trace_argument, phase2_vrs * entry_vrs);</code><code class="gmail-hljs gmail-language-cpp"> <br> ...<br> pre_signal_state.core = *<span class="gmail-hljs-built_in">reinterpret_cast</span><<span class="gmail-hljs-type">const</span> core_regs*>(&(signal_context->uc_mcontext.arm_r0));
__gnu_Unwind_Backtrace(tracer, &state, &pre_signal_state);</code></pre></div><div><br></div><div>In this SO example, they are using the signal_context while what we have in the RTEMS, for example in bsp_fatal_extension() is:</div><div><br></div><div> <span style="font-family:monospace">const rtems_exception_frame *exception_frame = (const rtems_exception_frame *) code;</span></div><div><br></div></div><div>Could anyone advise on how we access the original crashed stack trace from the <font size="2"><span style="font-family:monospace">code</span></font> variable? We confirm that _Unwind_Backtrace can work on both Zynq UltraScale+ and TMS570. It would be great to get it or <span style="font-family:monospace">__gnu_Unwind_Backtrace</span> to work with the real user frames.<br></div><div><br></div><div>Thanks,</div><div>Stanislav Pankevich<br></div><div><br></div><div><div><div><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Jun 26, 2024 at 4:14 PM Stanislav Pankevich <<a href="mailto:s.pankevich@gmail.com">s.pankevich@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>Hi Sebastian,</div><div><br></div><div>We had to add -funwind-tables to have this trace() called, and we can now see the function names as follows. The challenge is that the functions of the exception handler are printed, not the code that led to the exception. Now we need to figure out how to switch this code to print our function names instead.<br></div><div><br></div><div>FUNC NAME: bsp_fatal_extension<br>FUNC NAME: _User_extensions_Iterate<br>FUNC NAME: _Terminate<br>FUNC NAME: _ARM_Exception_default<br><br></div><div>static _Unwind_Reason_Code trace(_Unwind_Context *ctx, void *arg)<br>{<br> (void)arg;<br><br> const uint32_t func_entry = _Unwind_GetRegionStart(ctx);<br><br> char *func_name = "??";<br> uint8_t *canary = (uint8_t *)(func_entry - 0x4);<br> const uint32_t len = (*((uint32_t *)(func_entry - 0x4))) & 0x00FFFFFF;<br> if (*canary == 0xFF)<br> {<br> func_name = (char *)((uint32_t)canary - len);<br> *canary = '\0';<br> }<br> printk("FUNC NAME: %s\n", func_name);<br><br> return _URC_NO_REASON;<br>}</div><div><br></div><div>Thanks,</div><div>Stanislav Pankevich<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Jun 26, 2024 at 11:25 AM Sebastian Huber <<a href="mailto:sebastian.huber@embedded-brains.de" target="_blank">sebastian.huber@embedded-brains.de</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hello Stanislav,<br>
<br>
On 26.06.24 11:15, Stanislav Pankevich wrote:<br>
> Dear RTEMS community,<br>
> <br>
> My colleague George cannot register to post to this forum, getting 403 <br>
> when subscribing at <a href="https://lists.rtems.org/mailman/listinfo/users" rel="noreferrer" target="_blank">https://lists.rtems.org/mailman/listinfo/users</a> <br>
> <<a href="https://lists.rtems.org/mailman/listinfo/users" rel="noreferrer" target="_blank">https://lists.rtems.org/mailman/listinfo/users</a>>. I am posting this <br>
> question on his behalf.<br>
> <br>
> ---<br>
> <br>
> We are currently seeing an RTEMS_FATAL_SOURCE_EXCEPTION right at the <br>
> start of execution and can debug the program once the exception handler <br>
> has been reached. From here we can inspect the call stack however we <br>
> only see the trace that led to the final function call (void <br>
> bsp_reset(void) while loop) that handles the exception, not the trace <br>
> that caused the exception. We would like to implement an exception <br>
> handler that prints the trace that caused the exception to be triggered. <br>
> Is this something that is already implemented in RTEMS? We have <br>
> developed an RTEMS port to the Zynq UltraScale+ MPSoC Cortex R5 RPU <br>
> running in lockstep. Code execution is from the shared DDR4 memory with <br>
> a 256MB region allocated.<br>
<br>
I have some improvements for this BSP in my patch queue. It adds support <br>
for the split mode for example.<br>
<br>
> <br>
> What we want is to have the actual stack trace to be printed to the console.<br>
> <br>
> Here is an example of what we have now in GDB:<br>
> <br>
> bsp_reset@0x0017b062 <br>
> (.../rtems/bsps/arm/xilinx-zynqmp-rpu/start/bspreset.c:40)<br>
> _User_extensions_Iterate@0x00186854 <br>
> (.../rtems/cpukit/score/src/userextiterate.c:194)<br>
> _User_extensions_Fatal@0x00183108 <br>
> (.../rtems/cpukit/include/rtems/score/userextimpl.h:467)<br>
> _Terminate@0x00183108 (.../rtems/cpukit/score/src/interr.c:55)<br>
> rtems_fatal@0x0018adc2 (.../rtems/cpukit/include/rtems/fatal.h:160)<br>
> _ARM_Exception_default@0x0018adc2 <br>
> (.../rtems/cpukit/score/cpu/arm/arm-exception-default.c:37)<br>
> save_more_context@0x00186cfc <br>
> (.../rtems/cpukit/score/cpu/arm/armv4-exception-default.S:176)<br>
> <br>
> This is currently what is printed on the serial port:<br>
> <br>
> *** FATAL ***<br>
> fatal source: 9 (RTEMS_FATAL_SOURCE_EXCEPTION)<br>
> <br>
> R0 = 0x00000056 R8 = 0x00ac8aec<br>
> R1 = 0xf000e3a0 R9 = 0x00ac8a5c<br>
> R2 = 0x00000020 R10 = 0x00000000<br>
> R3 = 0x00000020 R11 = 0x00000000<br>
> R4 = 0x00000056 R12 = 0x010e6630<br>
> R5 = 0x010e67c0 SP = 0x010e65d4<br>
> R6 = 0x010e6698 LR = 0x001854ae<br>
> R7 = 0x001867f5 PC = 0x001854ae<br>
> CPSR = 0x200f01f7 VEC = 0x00000004<br>
> RTEMS version: 6.0.0.62f15c07482dd953663143554e78b4bf287ccb83<br>
> RTEMS tools: 12.2.1 20230224 (RTEMS 6, RSB <br>
> 4c73a76c802588d3864c64ee4dd48a84b953201a, Newlib 17ac400)<br>
<br>
Getting stack traces on arm is not that easy since you need a <br>
description of the stack frames. You can try to use the unwinder from <br>
libgcc:<br>
<br>
static _Unwind_Reason_Code trace(_Unwind_Context *ctx, void *arg)<br>
{<br>
(void)arg;<br>
printk("%08" PRIuPTR "\n", (uintptr_t)_Unwind_GetIP(ctx));<br>
return _URC_NO_REASON;<br>
}<br>
<br>
static void f(int i);<br>
<br>
static void h(int i)<br>
{<br>
f(i + 1);<br>
}<br>
<br>
static void f(int i)<br>
{<br>
if (i > 10) {<br>
_Unwind_Backtrace(trace, NULL);<br>
} else {<br>
h(i + 1);<br>
}<br>
}<br>
<br>
-- <br>
embedded brains GmbH & Co. KG<br>
Herr Sebastian HUBER<br>
Dornierstr. 4<br>
82178 Puchheim<br>
Germany<br>
email: <a href="mailto:sebastian.huber@embedded-brains.de" target="_blank">sebastian.huber@embedded-brains.de</a><br>
phone: +49-89-18 94 741 - 16<br>
fax: +49-89-18 94 741 - 08<br>
<br>
Registergericht: Amtsgericht München<br>
Registernummer: HRB 157899<br>
Vertretungsberechtigte Geschäftsführer: Peter Rasmussen, Thomas Dörfler<br>
Unsere Datenschutzerklärung finden Sie hier:<br>
<a href="https://embedded-brains.de/datenschutzerklaerung/" rel="noreferrer" target="_blank">https://embedded-brains.de/datenschutzerklaerung/</a><br>
</blockquote></div>
</blockquote></div></div></div></div></div>