1

When compiling this

#include <vector>
#include <stdio.h>
int main()
{
    std::vector<int> foo;
    foo.push_back( 1 );
    printf( "%zu\n", foo.size() );
}

with clang++ foo.cpp -stdlib=libc++ -g, when running a.out in gdb and trying to show the result of foo.size(), gdb says "Cannot evaluate function -- may be inlined".

Is there a way to avoid the compiler's inlining in debug mode? I could use libstdc++, but it is quite painful when it is needed to go inside the templates (many many subcalls plus indentation is sometimes space-based and sometimes tab-based).

I am running with Debian 9 (stretch) using libc++-dev v3.5 with clang 3.8 (tried with clang 5.0 too, same result) and gdb 7.12.

2
  • Possible duplicate stackoverflow.com/q/22163730/817643 Commented Mar 6, 2018 at 10:55
  • I must admit I didn't noticed this one, but here there is at least 2 differences: it works with libstdc++, and the inlining is not under my control. Commented Mar 6, 2018 at 11:15

1 Answer 1

3

libstdc++ implements so called Python xmethods, see documentation:

Xmethods are additional methods or replacements for existing methods of a C++ class. This feature is useful for those cases where a method defined in C++ source code could be inlined or optimized out by the compiler, making it unavailable to GDB. For such cases, one can define an xmethod to serve as a replacement for the method defined in the C++ source code. GDB will then invoke the xmethod, instead of the C++ method, to evaluate expressions. One can also use xmethods when debugging with core files. Moreover, when debugging live programs, invoking an xmethod need not involve running the inferior (which can potentially perturb its state). Hence, even if the C++ method is available, it is better to use its replacement xmethod if one is defined.

That's why you can call mock foo.size() even if the real foo.size() was inlined by compiler when using libstdc++. As far as I know there is no alike xmethod implementation for libc++.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.