[lldb-dev] Evaluate expression for template class

Jim Ingham via lldb-dev lldb-dev at lists.llvm.org
Tue Jul 23 17:35:45 PDT 2019


lldb can't currently create new template instantiations.  It can only access ones that were generated in the binary you were debugging.  The debug information doesn't have any code, so we can't create new instantiations from there.  Having the debugger try to include headers into the expression context in the same way the compiler did originally is also tricky since we don't know the header search paths or defines that the compiler used when it built the template instantiations...  Raphael has been working on doing this for the case where the C++ code is build into a clang module, but I don't think that work is done yet, and then you have to build your code with modules to use it...

The <int> expression is failing because clang is being smart and sees that though you mention foo<int> in reference to "test" you never actually use "test", so it doesn't actually have to make that instantiation. In the case where this works with gdb, did you build with clang or did you build with gcc?  If the latter, this might be working for gdb because gcc emits code for the int template instantiation when you mention it, rather than only when you use it?

Does it also work for gdb when you try:

(gdb) p sizeof(foo<double>)

or any other type you haven't mentioned in your code?

Not that that really matters for what lldb would have to do, but it would be interesting to know...

Jim



> On Jul 23, 2019, at 4:22 PM, Scott Funkenhauser via lldb-dev <lldb-dev at lists.llvm.org> wrote:
> 
> Hey,
> 
> I've noticed that evaluating expressions involving templated classes seems to have some unexpected behavior.
> 
> I've created the following sample code to reproduce the issue: 
> 
> template <typename T> class foo
> { 
>   uint32_t data;
> };
> 
> foo<int> test;
> 
> int main() {
>   foo<void> test2;
>   return 0;
> }
> 
> I've set a breakpoint on main and evaluated the following expressions:
> (lldb) p sizeof(foo<void>)
> (unsigned long) $0 = 4
> (lldb) p sizeof(foo<int>)
> error: implicit instantiation of undefined template 'foo<int>'
> template is declared here
> 
> It seems like expression evaluation can only find the specialized templated classes that were used in the current scope. Running the same example using GDB works as expected (both expressions are evaluated correctly).
> 
> Is this a known issue?
> Does anybody have any insight, or familiarity with expression evaluation that would be able to point me in the right direction?
> 
> Thanks,
> Scott
> _______________________________________________
> lldb-dev mailing list
> lldb-dev at lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-dev



More information about the lldb-dev mailing list