Working on OSX with llvm and lldb, I have the following code:
#include <stdio.h>
class A{
public:
void f() __attribute__((noinline))
{
printf("f()\n");
}
void g() __attribute__((noinline))
{
printf("g()\n");
}
};
int main()
{
A a;
a.g();
return 0;
}
When breaking in main(), and trying to call p a.f() from the debugger I get:
error: Couldn't lookup symbols: __ZN1A1fEv
calling p g.f(), however, works well.
I understand that f() may be optimized away, but is there any way to disable this optimization, so I can use it in the debugger anyway?
Thanks!
While moving the definition out of line may help in some cases, it won't help in all cases, e.g. if it's not an odr c++ member function but rather just a static function or if your entire program is LTO optimized and the function isn't referenced outside of the local translation unit.
In those cases (and probably this one as well) you should instead mark the function as __attribute__((used)) to tell the compiler that the function is used and to make sure and generate code for it in the resulting object file - which also means that debug information will be generated as well.
Your final code for this will end up looking like:
#include <stdio.h>
class A {
public:
void f() __attribute__((noinline, used))
{
printf("f()\n");
}
void g() __attribute__((noinline))
{
printf("g()\n");
}
};
int main()
{
A a;
a.g();
return 0;
}
This is one of the reasons why __attribute__((used)) was designed. From the gcc documentation:
This attribute, attached to a function, means that code must be emitted for the function even if it appears that the function is not referenced. This is useful, for example, when the function is referenced only in inline assembly. When applied to a member function of a C++ class template, the attribute also means that the function is instantiated if the class itself is instantiated.
This means that the function will be guaranteed to be emitted in all cases even if it isn't used.
(As a further addendum I haven't verified that this will actually work for LTO optimization - it probably should, but I'm betting it's a corner case that isn't often thought of, I didn't until just now and I work on LTO :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With