1

I'm developing in c++ for my STM32F3 Discovery board and using std::deque as queue. After trying to debug my code (directly on device with ST-link or in simulator), the code eventually stops at breakpoint before even entering my code in main(). However, SystemInit() configures board just fine..

I've traced this behavior down to using push_back() (and push_front) as commenting it out from code solves the issue. Through disassmebly I found that after using it, the execution stops at breakpoint instruction BKPT and won't move further after resuming execution. This instruction is part of _sysopen() call, with call path:

__main -> __scatterload -> __scatterload_null -> __rt_entry -> __rt_lib_init -> __rt_lib_init_atexit_1 -> _initio -> freopen -> _sysopen

What intrigues me is call to _initio, which is missing if push_back isn't used, because there is no __rt_lib_init_atexit_1. Introducing push_back also makes the code size go from 10 kB to 34 kB.

Might this be a result of some bad configuration or should I try another IDE? I'm out of ideas.

4
  • Can you show something of the actual C++ code? The fact that removing one line prevents the failure doesn't mean necessarily that the error is in that line.
    – Antonio
    Jul 9, 2013 at 12:46
  • @Antonio Sure, have a look: gist.github.com/BetaRavener/5957167 . Uncommenting any of push_back lines causes this.
    – Raven
    Jul 9, 2013 at 13:06
  • What about the declaration of scale_buffer?
    – Antonio
    Jul 9, 2013 at 13:10
  • Yeah sorry, updated the repo so there's now header also.
    – Raven
    Jul 9, 2013 at 13:19

2 Answers 2

3

I had the same problem. I learnt it has something to do with so called 'semihosting' and that I should build with my project file 'retarget.c' that contains definitions of the functions like '_sys_xxxx()' that are target specific driver level functions (many versions of 'retarget.c' are part of the Keil-MDK and also ca be found on web). So I did but then linker thrown errors similar to this:

Error: L6200E: Symbol _sys_open multiply defined (by arm_xxx_lib.o and retarget.o)
Error: L6200E: Symbol _sys_close multiply defined (by arm_xxx_lib.o and retarget.o)
...

I solved this by editing original 'retarget.c' so that functions defined in it will override the ones in Keil-MDK libraries. The new 'retarged.c' is here:

#include <stdio.h>
#include <rt_misc.h>

#pragma import(__use_no_semihosting_swi)

#include <rt_sys.h>

extern void $Super$$_sys_open(void);

FILEHANDLE $Sub$$_sys_open(const char *name, int openmode)
{
 return 1; /* everything goes to the same output */
}

extern void $Super$$_sys_close(void);
int $Sub$$_sys_close(FILEHANDLE fh)
{
 return 0;
}

extern void $Super$$_sys_write(void);
int $Sub$$_sys_write(FILEHANDLE fh, const unsigned char *buf,
              unsigned len, int mode)
{
 //your_device_write(buf, len);
 return 0;
}

extern void $Super$$_sys_read(void);
int $Sub$$_sys_read(FILEHANDLE fh, unsigned char *buf,
             unsigned len, int mode)
{
 return -1; /* not supported */
}

extern void $Super$$_ttywrch(void);
void $Sub$$_ttywrch(int ch)
{
 char c = ch;
 //your_device_write(&c, 1);
}

extern void $Super$$_sys_istty(void);
int $Sub$$_sys_istty(FILEHANDLE fh)
{
 return 0; /* buffered output */
}

extern void $Super$$_sys_seek(void);
int $Sub$$_sys_seek(FILEHANDLE fh, long pos)
{
 return -1; /* not supported */
}

extern void $Super$$_sys_flen(void);
long $Sub$$_sys_flen(FILEHANDLE fh)
{
 return -1; /* not supported */
}

extern void $Super$$_sys_exit(void);
long $Sub$$_sys_exit(FILEHANDLE fh)
{
 return -1; /* not supported */
}

With this version of 'retarget.c' linker was satisfied and my program run w/o problem. Maybe this will help you as well.

2
  • This is a solution when continuing to use Keil, however because of this problem I later switched to Eclipse and building with an ARM specific toolchain: launchpad.net/gcc-arm-embedded .
    – Raven
    Dec 31, 2013 at 12:54
  • @francek Thanks. Helped me.
    – Flip
    Jan 30, 2018 at 14:56
0

Try by checking if scale_buffer contains any element (scale_buffer.empty()) before calls to .back() and .front(): you are probably reading and writing some garbage, which makes the deque invalid, preparing the ground for a crash when you call push_back()

7
  • The problem is I don't get to call back/front. And if I would (push_back commented out) I end up with HardFault interrupt, so this is not solution
    – Raven
    Jul 9, 2013 at 15:11
  • @Raven What is the return value of scale_buffer.empty()?
    – Antonio
    Jul 9, 2013 at 15:34
  • logically if I don't push anything, then true (practically too). Man I'm not a beginner.
    – Raven
    Jul 9, 2013 at 15:45
  • @Raven What I see is (for example) scale_buffer.back().second > 0: if the buffer is empty how does that check make sense?
    – Antonio
    Jul 9, 2013 at 15:54
  • you're analyzing my concept rather than solving this issue. That buffer is supposed to have at least one item in it at each time (pushed in constructor, never popped, doesn't mean it will be the same item forever). However if push isn't working, that code is going to have lots of problems like this, but it's not what I want to solve.
    – Raven
    Jul 9, 2013 at 16:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.