EmbDev.net

Forum: ARM programming with GCC/GNU tools watch #define constant expression in debug


Author: Paul (Guest)
Posted on:

Rate this post
0 useful
not useful
Hello guys,
i came about a something strange to me while programming and Debugging 
some c-Code with AVR-Studio 7. I was trying to watch the value of an 
Expression while Debugging, but i get the message "unknown identifier". 
The Expression is defined as follows:

#define THRESH1 (uint16_t) ((uint32_t)1024*(500 + 1*800)/5000)

does anyone has an idea, why i can't watch the value of this constant? 
By the way, i've tried declaring it localy or global, but i still get 
the same message. Moreover, i tried with the following Expression:

const uint16_t thresh1_3 = (uint16_t) ((uint32_t)1024*(500 + 
3*800)/5000);

but the watch variable only works when it is a local constant, not 
global. Does anyone has a hint there too? I also deactivated 
optimizations too but it doesn't seem to help. Thanks and Cheers
Paul

Author: Rolf Magnus (Guest)
Posted on:

Rate this post
0 useful
not useful
Paul wrote:
> Hello guys,
> i came about a something strange to me while programming and Debugging
> some c-Code with AVR-Studio 7. I was trying to watch the value of an
> Expression while Debugging, but i get the message "unknown identifier".
> The Expression is defined as follows:
>
> #define THRESH1 (uint16_t) ((uint32_t)1024*(500 + 1*800)/5000)
>
> does anyone has an idea, why i can't watch the value of this constant?

Because it is a preprocessor macro. At runtime, it doesn't exist in that 
form anymore. A macro is similar to your editors search/replace 
functions, just with the difference that the text to be replaced is 
specified within the processed file itself.

> By the way, i've tried declaring it localy or global, but i still get
> the same message.

There is no such thing as "locally" or "gobally" with respect to macros.

> Moreover, i tried with the following Expression:
>
> const uint16_t thresh1_3 = (uint16_t) ((uint32_t)1024*(500 +
> 3*800)/5000);
>
> but the watch variable only works when it is a local constant, not
> global.

It might get optimized out by the compiler. Does the symbol thresh1_3 
exist in the generated executable?

Author: Paul Mbak (dapaul)
Posted on:

Rate this post
0 useful
not useful
> Because it is a preprocessor macro. At runtime, it doesn't exist in that
> form anymore. A macro is similar to your editors search/replace
> functions, just with the difference that the text to be replaced is
> specified within the processed file itself.

Thank you, that is what i tought too, anyways I want to use the 
calculated constant to test for a certain condition. So I expected I 
could actually see what value was calculated:
#define THRESH1_1 (uint16_t) ((uint32_t)1024*(500 + 1*800)/5000)
#define THRESH1_2 (uint16_t) ((uint32_t)1024*(500 + 2*800)/5000)
#define THRESH1_3 (uint16_t) ((uint32_t)1024*(500 + 3*800)/5000)
#define THRESH1_4 (uint16_t) ((uint32_t)1024*(500 + 4*800)/5000)
if(u16_adValue0 <= THRESH1_1)
{
... something;
}  
else if(u16_adValue0 <= THRESH1_2) 
{
... some other thing;
}
and so on...

> It might get optimized out by the compiler. Does the symbol thresh1_3
> exist in the generated executable?
I did deactivate optimizations, but it didn't Change anything. I don't 
really understand what you mean with generated executable because i'm 
flashing the Code directly in a microcontroller (ATmega32u4)

Author: Bernd (Guest)
Posted on:

Rate this post
0 useful
not useful
If your macro is expected to represent just a numerical expression 
(macros could actually represent anything) then you could directly 
assign it to a variable for debugging purposes. If you make that 
variable volatile then it won't be optimized away and you can watch it 
in the debugger to confirm that your macro expands and evaluates to the 
correct value.
#define FOO_BAR  (uint16_t) ((uint32_t)1024*(500 + 1*800)/5000)

// fixme: remove this after you know its evaluated correctly
volatile uint16_t debug_foo_bar = FOO_BAR;

Reply

Entering an e-mail address is optional. If you want to receive reply notifications by e-mail, please log in.

Rules — please read before posting

  • Post long source code as attachment, not in the text
  • Posting advertisements is forbidden.

Formatting options

  • [c]C code[/c]
  • [avrasm]AVR assembler code[/avrasm]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]




Bild automatisch verkleinern, falls nötig