This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

How to debug with stdlib code around? Was: Code fails in snprintf, how to debug?

I am using an nrf51822 DK with a (so far) very simple code along:

main()
{
  //... init I/O
  run();
}

Inside run:

run()
{
    uint32_t retval = NRF_SUCCESS;

  // nrf_gpio_* calls to set up some GPIOs...
  // Set up UART...

  // VALUE_SIZE >2
    snprintf(value, VALUE_SIZE, "%s", "TI");


    // Initial states.
    foo = WAIT_NOTHING;
    return retval;
}

Now if I run the code I never get to 'foo = WAIT_NOTHING'. But if I comment the snprintf line I can get to the last statement.

If I step-debug after snprintf gets called the cursor goes back to the nrf_gpio_* calls.

EDIT: after some trial and error I figured out that if I use snprintf in a debug build (-O0 -g3) code will do what's described above. If I set a release build (-O3) the code works, but I can no longer debug the rest of the code... So, what am I missing here?

Notes: I am using jlink 6.12.5, SDK v10.0.0,

Parents
  • With GCC and newlib you can try using sniprintf(). This function does not support formatting floating point numbers - but should not crash due to missing heap.

    Also, you code example does not compile due to missing declaration of value.

    Try to set up an Hardfault_Handler() in your application. This way you will know that something bad happended.

  • With GCC and newlib you can try using sniprintf(). This function does not support formatting floating point numbers - but should not crash due to missing heap.

    Good point! Sadly even using it I'm still facing the issue.

    Also, you code example does not compile due to missing declaration of value.

    Right, it was just a snippet.

    Try to set up an Hardfault_Handler() in your application. This way you will know that something bad happended.

    That sounds like a very good advice, thanks!

Reply
  • With GCC and newlib you can try using sniprintf(). This function does not support formatting floating point numbers - but should not crash due to missing heap.

    Good point! Sadly even using it I'm still facing the issue.

    Also, you code example does not compile due to missing declaration of value.

    Right, it was just a snippet.

    Try to set up an Hardfault_Handler() in your application. This way you will know that something bad happended.

    That sounds like a very good advice, thanks!

Children
No Data
Related