# GDB call stack unwinding issue with FreeRTOS

**URL:** <https://forums.freertos.org/t/gdb-call-stack-unwinding-issue-with-freertos/19441>\
**Category:** Infineon\
**Created:** [March 5, 2024, 6:02am UTC](https://forums.freertos.org/t/gdb-call-stack-unwinding-issue-with-freertos/19441 "2024-03-05T06:02:22Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![vikasshah1991](https://avatars.discourse-cdn.com/v4/letter/v/e79b87/32.png) [@vikasshah1991](https://forums.freertos.org/u/vikasshah1991)\
**Post date:** [March 5, 2024, 6:02am UTC](https://forums.freertos.org/t/gdb-call-stack-unwinding-issue-with-freertos/19441/1 "2024-03-05T06:02:22Z")

</div>

Seeing a call stack unwinding issue with ARM compiler using FreeRTOS. Based on my understanding, It seems that GDB when unwinding the call stack not finding the end of frame in the stack and still looking further which causes GDB to go into infinite to check for the end of frame in stack.

One of suggestion was to set the link register to 0 by setting configTASK\_RETURN\_ADDRESS to NULL. But even after this, issue is seen with ARMC6 compiler.

Appreciate any help on this…

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [March 5, 2024, 2:37pm UTC](https://forums.freertos.org/t/gdb-call-stack-unwinding-issue-with-freertos/19441/2 "2024-03-05T14:37:19Z")

</div>

This seems more like a question for GDB maintainers. What does the call stack look like when you put a breakpoint somewhere in main before the FreeRTOS scheduler is started? Whatever is that last function in that callstack, can you try setting `configTASK_RETURN_ADDRESS` to the same?

---

<div class="post-metadata">

**Author:** ![vikasshah1991](https://avatars.discourse-cdn.com/v4/letter/v/e79b87/32.png) [@vikasshah1991](https://forums.freertos.org/u/vikasshah1991)\
**Post date:** [March 5, 2024, 6:05pm UTC](https://forums.freertos.org/t/gdb-call-stack-unwinding-issue-with-freertos/19441/3 "2024-03-05T18:05:42Z")

</div>

Thanks Gaurav for your response.  
I tried setting configTASK\_RETURN\_ADDRESS to 0 but the timer task created during scheduler start goes into infinite loop of having same call stack. Hence, after sometime it gives strange call stack during debugging

---

<div class="post-metadata">

**Author:** ![RAc](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rac/32/1389_2.png) [@RAc](https://forums.freertos.org/u/RAc)\
**Post date:** [March 5, 2024, 8:58pm UTC](https://forums.freertos.org/t/gdb-call-stack-unwinding-issue-with-freertos/19441/4 "2024-03-05T20:58:55Z")

</div>

It may not be a debugger issue after all. The ARM architecture provides so many obfuscating optimization possibilities that there may no discernible relationship between source and assembly code. The same phenomenon holds true on other debuggers as well. There is inlining, unfolding, tail recursion elimination, mode transitions… One thing you may want to try is build your code with optimizations disabled (-O0) and compare your stack frames. You are in for a number of surprises…

---

<div class="post-metadata">

**Author:** ![hs2](https://avatars.discourse-cdn.com/v4/letter/h/a4c791/32.png) [@hs2](https://forums.freertos.org/u/hs2)\
**Post date:** [March 5, 2024, 9:24pm UTC](https://forums.freertos.org/t/gdb-call-stack-unwinding-issue-with-freertos/19441/5 "2024-03-05T21:24:50Z")

</div>

Maybe a silly question, but is the ARM toolchain compatible with your gdb ?  
In other words is the gdb you’re using provided by ARM along with their compiler ?

---

<div class="post-metadata">

**Author:** ![vikasshah1991](https://avatars.discourse-cdn.com/v4/letter/v/e79b87/32.png) [@vikasshah1991](https://forums.freertos.org/u/vikasshah1991)\
**Post date:** [March 6, 2024, 3:38am UTC](https://forums.freertos.org/t/gdb-call-stack-unwinding-issue-with-freertos/19441/6 "2024-03-06T03:38:39Z")

</div>

I was using -O1 optimization level for the debug build but setting it to -O0 I dont see an issue. But is it hiding some issue or for the call unwinding to work optimization level must be set to -O0?

---

<div class="post-metadata">

**Author:** ![vikasshah1991](https://avatars.discourse-cdn.com/v4/letter/v/e79b87/32.png) [@vikasshah1991](https://forums.freertos.org/u/vikasshah1991)\
**Post date:** [March 6, 2024, 3:40am UTC](https://forums.freertos.org/t/gdb-call-stack-unwinding-issue-with-freertos/19441/7 "2024-03-06T03:40:07Z")

</div>

Yes. GDB is provided by ARM along with the compiler.

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [March 6, 2024, 5:21am UTC](https://forums.freertos.org/t/gdb-call-stack-unwinding-issue-with-freertos/19441/8 "2024-03-06T05:21:45Z")

</div>

> [@vikasshah1991](#):
>
> I was using -O1 optimization level for the debug build but setting it to -O0 I dont see an issue.

Then it is exactly the issue mentioned by @RAc.

> [@vikasshah1991](#):
>
> But is it hiding some issue or for the call unwinding to work optimization level must be set to -O0?

It is common to build code with no optimization for debugging as it ensures better relationship between source and the generated assembly. Unless, you trying to debug an issue that only happens with optimization enabled, it is okay to keep optimization turned off for debugging.

---

<div class="post-metadata">

**Author:** ![vikasshah1991](https://avatars.discourse-cdn.com/v4/letter/v/e79b87/32.png) [@vikasshah1991](https://forums.freertos.org/u/vikasshah1991)\
**Post date:** [March 6, 2024, 10:05am UTC](https://forums.freertos.org/t/gdb-call-stack-unwinding-issue-with-freertos/19441/9 "2024-03-06T10:05:42Z")

</div>

okay. The reason of not going with complete optimization disabled is it consumes more code space and has an issue of fitting into the hardware. Hence, we used -01. But I still have a question does call stack unwinding have relation with the optimization level?

---

<div class="post-metadata">

**Author:** ![RAc](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rac/32/1389_2.png) [@RAc](https://forums.freertos.org/u/RAc)\
**Post date:** [March 6, 2024, 10:26am UTC](https://forums.freertos.org/t/gdb-call-stack-unwinding-issue-with-freertos/19441/10 "2024-03-06T10:26:18Z")

</div>

yes, it does. Under different optimizations, the call stack may be very very different. “Unwinding” as in “a debugger trying to determine the call sequence and matching it against source code” becomes a very challenging and error prone issue for a debugger.

---

<div class="post-metadata">

**Author:** ![hs2](https://avatars.discourse-cdn.com/v4/letter/h/a4c791/32.png) [@hs2](https://forums.freertos.org/u/hs2)\
**Post date:** [March 6, 2024, 12:08pm UTC](https://forums.freertos.org/t/gdb-call-stack-unwinding-issue-with-freertos/19441/11 "2024-03-06T12:08:30Z")

</div>

`-Og` could be a good compromise supported by GCC. If the image still fits into your target.
