How to use __attribute__((used))?

Marc Glisse marc.glisse@inria.fr
Tue Sep 17 15:38:00 GMT 2013


On Tue, 17 Sep 2013, Paul Smith wrote:

> On Tue, 2013-09-17 at 08:08 -0700, Ian Lance Taylor wrote:
>> In your example code you are using the attribute on a local variable.
>> The used attribute is only meaningful on a function or global
>> variable.
>
> Aha.  OK thanks.  It would be very helpful if the manual made this more
> clear: it simply says "variable" and I see no obvious way to infer that
> it applies only to global variables or functions.
>
>> It's intended to be used to force the function or global variable to
>> be emitted even if it is not used.
>>
>> It sounds like you are looking for the ability to disable optimization
>> for a local variable.  It doesn't make sense to say that you want to
>> force a local variable to exist even at high levels of optimization.
>> At high levels of optimization local variables more or less disappear,
>> though debug info is able to track some cases.  In any case, GCC has
>> no such ability.
>
> IMO it is not nonsensical to ask the compiler to preserve a variable on
> the stack even at high levels of optimization, so that its value can be
> queried from a coredump or even live debugging.
>
> The variables I'm referring to are typically of the type in my example
> (although their value is not so simply reproduced obviously): they are
> local variables which are assigned but never read.  They exist solely to
> keep track of a value which may be interesting in a debugging session.
> Ideally we'd not be debugging highly optimized code but real life is not
> always ideal :-).
>
> However, I understand that this capability is not currently available.

volatile or inline asm (or ...) can get you roughly what you want.

-- 
Marc Glisse



More information about the Gcc-help mailing list