What is the address of this line?
Ingo Krabbe
ikrabbe.ask@web.de
Wed Jun 21 07:26:00 GMT 2006
Am Dienstag, 20. Juni 2006 11:01 schrieb Andrew Haley:
> Brian Dessent writes:
> > John Carter wrote:
> > > So we using gcc on an embedded platform. ie. Very constrained flash &
> > > ram.
> > >
> > > We also using asserts.
> > >
> > > Storing __LINE__ and __FILE__ is taking too much flash.
> > >
> > > Question: I would like to create an assert macro ....
> > >
> > > #define Assert( expression) do {\
> > > if(!(expression)) { \
> > > assert_occurred_at_program_counter = THE_ADDRESS_OF_THIS_LINE();
> > > \ }
> > > } while(0)
> > >
> > > So how do I write that magical function / macro
> > > THE_ADDRESS_OF_THIS_LINE(); That returns the address / PC at that
> > > line? Preferable in a CPU neutral fashion, otherwise for a Sparc CPU.
> >
> > How about something like: (see also section 5.2 of the manual)
> >
> > #define Assert(expression) ({ \
> > __label__ here; \
> > if (!(expression)) { \
> > here: assert_occurred_at_program_counter = &&here; \
> > } \
> > })
>
> I also thought of
>
> void *foo ()
> {
> return __builtin_return_address(0);
> }
>
> Andrew.
Anyway, do you really think you need this ? On an embedded platform you won't
want the program to step out at assertions, do you ? In the normal case, you
only have the assertions in while you develop, on your development platform.
Hopefully you are able to run in a simulator on your development plaftorm.
Once you compile the product for the embedded machine, you will use -DNDEBUG
to disable the asserts. This is the meaning and the idea behind the assert
macro.
__LINE__ and __FILE__ aren't used to get a runtime PC of the code, but are
debugging informations to throw out sourcecode lines at illegal behaviour.
It seems that |__builtin_return_adress(0)| is a valid way to read PC, though.
bye ingo
More information about the Gcc-help
mailing list