<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Back trace from hard fault handler with s110 flashed?</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/420/back-trace-from-hard-fault-handler-with-s110-flashed</link><description>I&amp;#39;m using Nordic NRF51822 and the Segger JLink with the s110 Bluetooth stack active (correction: flashed on the chip, see below). When my code hard faults (say due to a dereferencing an invalid pointer), I see only the following stack trace: 
 (gdb)</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 10 Feb 2014 20:30:54 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/420/back-trace-from-hard-fault-handler-with-s110-flashed" /><item><title>RE: Back trace from hard fault handler with s110 flashed?</title><link>https://devzone.nordicsemi.com/thread/2173?ContentTypeID=1</link><pubDate>Mon, 10 Feb 2014 20:30:54 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:af4e3dd6-a0a1-4015-8a53-b31d8d565248</guid><dc:creator>Ole Morten</dc:creator><description>&lt;p&gt;Finding the correct link register value on the stack is actually a very neat trick that I didn&amp;#39;t think about at the time. Thanks for posting!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Back trace from hard fault handler with s110 flashed?</title><link>https://devzone.nordicsemi.com/thread/2172?ContentTypeID=1</link><pubDate>Mon, 10 Feb 2014 18:18:23 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0b725c4a-8d45-4d92-b310-eac98214cad7</guid><dc:creator>Andre Pang</dc:creator><description>&lt;p&gt;Hi Chris,&lt;/p&gt;
&lt;p&gt;Just letting you know that if you&amp;#39;re still using SoftDevice 5.x, you can fake a proper backtrace in gdb by replacing the link register yourself. I had a look at your HardFault_Handler code that you posted, and since you call a C-based HardFault_HandlerC function, you can correct the backtrace before hitting your C function. Add something like&lt;/p&gt;
&lt;p&gt;// Load MSP into R0 (replace with PSP if appropriate)
mrs r0, msp
// Replace LR (which SoftDevice stomped on) with the stacked LR
ldr r2, [r0, #24]
mov lr, r2&lt;/p&gt;
&lt;p&gt;to your asm HardFault_Handler. That&amp;#39;ll correct the LR before HardFault_HandlerC gets hit, so gdb can print out a nice happy backtrace. Works For Me&lt;span class="emoticon" data-url="https://devzone.nordicsemi.com/cfs-file/__key/system/emoji/2122.svg" title="Tm"&gt;&amp;#x2122;&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;And thanks a ton for your HardFault_Handler code! I&amp;#39;m completely new to firmware programming as of a few months ago, and am very happy that I have a handler that&amp;#39;ll log all our application crashes. (Not that our app ever crashes, of course.) Much appreciated.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Back trace from hard fault handler with s110 flashed?</title><link>https://devzone.nordicsemi.com/thread/2171?ContentTypeID=1</link><pubDate>Thu, 19 Sep 2013 12:52:52 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:05def350-a39b-4e99-9f45-af3bde819f7f</guid><dc:creator>Christopher Mason</dc:creator><description>&lt;p&gt;Y&amp;#39;all are completely awesome.  :)  Thanks so much!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Back trace from hard fault handler with s110 flashed?</title><link>https://devzone.nordicsemi.com/thread/2170?ContentTypeID=1</link><pubDate>Thu, 19 Sep 2013 09:10:37 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:033def79-995c-4fdc-9180-1fc035f19ac6</guid><dc:creator>Ole Morten</dc:creator><description>&lt;p&gt;I&amp;#39;m sorry, it turned out that I was confused by Windows choosing a different GCC/GDB version than I expected. When I make sure to use 4.7q2, I see the same behavior you do.&lt;/p&gt;
&lt;p&gt;A softdevice developer had a look on this, and it seems that this is caused by the softdevice&amp;#39;s HardFault handler doing a BLX instead of a BX when jumping to the application&amp;#39;s handler. Apparently, this causes GDB and other debuggers to get lost.&lt;/p&gt;
&lt;p&gt;A fix for this should hopefully get into the final version 6.0.0 of the S110. Thanks for the help with reproducing this problem!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Back trace from hard fault handler with s110 flashed?</title><link>https://devzone.nordicsemi.com/thread/2169?ContentTypeID=1</link><pubDate>Wed, 18 Sep 2013 03:22:44 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:dd8cc04a-edcb-4a02-bd43-1e4e3793a651</guid><dc:creator>Christopher Mason</dc:creator><description>&lt;p&gt;Ole, I really appreciate you investigating, but I&amp;#39;m afraid I&amp;#39;m not seeing the same thing you are.&lt;/p&gt;
&lt;p&gt;I just reconfirmed that I am indeed able to see backtraces in gdb from HardFault handlers when the soft device is not flashed on the NRF51822.&lt;/p&gt;
&lt;p&gt;If I run this program:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;
#include &amp;lt;stdint.h&amp;gt;

void cause_hardfault(void)
{
    uint32_t * cause_hardfault = (uint32_t *) 0x1;

    uint32_t value = *cause_hardfault;
    (void) value;
}

int do_something(int unused) {
    cause_hardfault();
    return 1;
}

int main() {
    do_something(0);
    return 0;
}


&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And I compile, using &lt;a href="https://launchpad.net/gcc-arm-embedded"&gt;gcc-arm-none-eabi-4_7-2013q2&lt;/a&gt; like such:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;
arm-none-eabi-gcc -Wall -g3 -O0 -mcpu=cortex-m0 -mthumb -c main_crash.c
arm-none-eabi-gcc -Wall -g3 -O0 -mcpu=cortex-m0 -mthumb -c nRF51822.c
arm-none-eabi-gcc -Wall -g3 -O0 -mcpu=cortex-m0 -mthumb -c error.c
arm-none-eabi-gcc -O0  -g3 -mcpu=cortex-m0 -mthumb -TnRF51822-bare.ld -o main_crash.elf main_crash.o  nRF51822.o error.o -lg_s -lc_s -lrdimon

&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I then flash this on the chip with no soft device present.&lt;/p&gt;
&lt;p&gt;I get this stack trace:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;
Breakpoint 1, main () at main_crash.c:17
17	    do_something(0);
(gdb) c

Program received signal SIGTRAP, Trace/breakpoint trap.
HardFault_HandlerC (hardfault_args=0x20003fa8) at error.c:58
58	    __asm(&amp;quot;BKPT #0&amp;quot;  ) ; // Break into the debugger
(gdb) bt
#0  HardFault_HandlerC (hardfault_args=0x20003fa8) at error.c:58
#1  &amp;lt;signal handler called&amp;gt;
#2  0x000004e0 in cause_hardfault () at main_crash.c:7
#3  0x000004f8 in do_something (unused=0) at main_crash.c:12
#4  0x0000050e in main () at main_crash.c:17

&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I&amp;#39;ve also tried with -O2 and -Os.  All of these work when I do not have the s110 stack flashed on the chip.&lt;/p&gt;
&lt;p&gt;However, if I use the soft device ld file:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;
arm-none-eabi-gcc -O0  -g3 -mcpu=cortex-m0 -mthumb -TnRF51822-softdevice.ld -o main_crash.elf main_crash.o  nRF51822.o error.o -lg_s -lc_s -lrdimon

&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And flash it onto a chip where the soft device has also been loaded, I get this stack trace:&lt;/p&gt;
&lt;p&gt;Breakpoint 1, main () at main_crash.c:17
17	    do_something(0);
(gdb) c&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;
Program received signal SIGTRAP, Trace/breakpoint trap.
HardFault_HandlerC (hardfault_args=0x20003fa8) at error.c:58
58	    __asm(&amp;quot;BKPT #0&amp;quot;  ) ; // Break into the debugger
(gdb) bt
#0  HardFault_HandlerC (hardfault_args=0x20003fa8) at error.c:58
#1  0x00011290 in ?? ()
#2  0x00011290 in ?? ()
Backtrace stopped: previous frame identical to this frame (corrupt stack?)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It&amp;#39;s not necessary for the s110 to be active, but if it is flashed on the chip, then I get ?? backtraces like the ones shown in the above.&lt;/p&gt;
&lt;p&gt;I&amp;#39;ve attached a zip of a &lt;a href="http://hg.cmason.com/nrf-minimal"&gt;project with Makefile&lt;/a&gt; that demonstrates this.  Use:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;
make NRF51_USE_SOFTDEVICE=0 all
make clean
make NRF51_USE_SOFTDEVICE=1 all

&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;to test.  I use the JLink to clear the flash between runs so none of the soft device is left in flash.&lt;/p&gt;
&lt;p&gt;I have to conclude that something about the presence of the s110 soft device causes gdb to not be able to report stack traces from hard faults.&lt;/p&gt;
&lt;p&gt;Again, would it be possible for you to share the contents of the s110 HardFault handler function?&lt;/p&gt;
&lt;p&gt;Thank you so much for your kind attention!&lt;/p&gt;
&lt;p&gt;-c&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Back trace from hard fault handler with s110 flashed?</title><link>https://devzone.nordicsemi.com/thread/2168?ContentTypeID=1</link><pubDate>Mon, 16 Sep 2013 11:13:29 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:600c9f44-dca6-4472-ae86-60364151fd28</guid><dc:creator>Ole Morten</dc:creator><description>&lt;p&gt;Unfortunately, as far as I can see, there is something about a HardFault occuring that causes the GDB backtracing to be confused, no matter if the softdevice is currently on the chip or not.&lt;/p&gt;
&lt;p&gt;When testing this, I&amp;#39;ve been using the following code to cause a hardfault in a simple program:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;
void cause_hardfault(void)
{
	uint32_t * cause_hardfault = (uint32_t *) 0x1;
	
	uint32_t value = *cause_hardfault;
	(void) value;
}

&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;When running this, even with no softdevice on the chip, I&amp;#39;m not able to get any reasonable backtrace from any hardfault handlers. When inspecting memory manually, it does however seem that the stack isn&amp;#39;t corrupted, but no matter what I try to set the current frame to, GDB won&amp;#39;t give me any useful backtrace.&lt;/p&gt;
&lt;p&gt;I have to admit that I&amp;#39;m on thin ice regarding my own knowledge here, so there could very well be some (potentially easy) trick that can be done to &amp;quot;explain&amp;quot; GDB how to read the stack. It would have been very interesting if either someone here, or someone on Stack Overflow could come up with something!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Back trace from hard fault handler with s110 flashed?</title><link>https://devzone.nordicsemi.com/thread/2167?ContentTypeID=1</link><pubDate>Fri, 13 Sep 2013 14:37:32 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:724cd5c1-0c3a-4f6a-aed7-b03a40f0d65e</guid><dc:creator>Christopher Mason</dc:creator><description>&lt;p&gt;I&amp;#39;ve attached the code, which comes directly from the link in my original post. Would it be possible to post the code from s110&amp;#39;s hard fault handler? Does it have &lt;strong&gt;attribute&lt;/strong&gt;((naked))? Does it just directly forward to the handler in my interrupt vector? I&amp;#39;m confused, based on reading docs [1], why both the Nordic and my fault handlers are not simply pushed onto the existing stack...&lt;/p&gt;
&lt;p&gt;[1] &lt;a target="_blank" href="http://infocenter.arm.com/help/topic/com.arm.doc.dui0497a/Babefdjc.html#BABDBGGA" rel="nofollow"&gt;http://infocenter.arm.com/help/topic/com.arm.doc.dui0497a/Babefdjc.html#BABDBGGA&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Back trace from hard fault handler with s110 flashed?</title><link>https://devzone.nordicsemi.com/thread/2166?ContentTypeID=1</link><pubDate>Fri, 13 Sep 2013 09:52:39 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:aa148e90-d3fb-4ffe-a8a4-6654175dc9bf</guid><dc:creator>Ole Morten</dc:creator><description>&lt;p&gt;I&amp;#39;m sorry for the delay, but I&amp;#39;ve been digging around a little today, experimenting with different handlers and inspecting the stack manually, and as far as I can see, this isn&amp;#39;t really possible. However, to be a little more certain, it would have been useful to have a look at your HardFault handlers. Could you upload a code file with them here?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Back trace from hard fault handler with s110 flashed?</title><link>https://devzone.nordicsemi.com/thread/2165?ContentTypeID=1</link><pubDate>Tue, 10 Sep 2013 19:43:54 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2d2693d9-6273-48b3-9f5d-4bea6bdfc0ec</guid><dc:creator>Christopher Mason</dc:creator><description>&lt;p&gt;Does anyone have an ideas here?  Can I modify the fault handler somehow to give me access to the frame pointer?  See also the same question at Stack Overflow: &lt;a target="_blank" href="http://stackoverflow.com/questions/18640673/gdb-backtrace-from-hard-fault-handler-on-arm-cortex-m0-nrf51822" rel="nofollow"&gt;http://stackoverflow.com/questions/18640673/gdb-backtrace-from-hard-fault-handler-on-arm-cortex-m0-nrf51822&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>