Debugging templates with GDB

Asked
Active3 hr before
Viewed126 times

7 Answers

templatesdebugging
90%

I was having a hard time trying to get gdb debugging to work with templates code using gdb 6.8 with code compiled with gcc version 4.4.1, Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers ,Making statements based on opinion; back them up with references or personal experience.,After ripping my hair for a while, I found that it magically started working perfectly when I add the -fPIC switch to the g++ compile arguments.

ex: main.cpp

#include <iostream>

template <typename T>
void coin(T v)
{
    std::cout << v << std::endl;
}

template<typename T>
class Foo
{
public:

    T bar(T c)
    {
        return c * 2;
    }
};

int main(int argc, char** argv)
{
    Foo<int> f;
    coin(f.bar(21));
}
load more v
88%

Setting breakpoints at class or function template instantiations (see stop inclass classname Command, stop infunction name Command, and stop in function Command), Setting breakpoints at class or function template instantiations (see stop inclass classname Command, stop infunction name Command, and stop in function Command) , Printing a list of all class and function template instantiations (see whereis name Command), Printing a list of all class and function template instantiations (see whereis name Command)


 1        template<class C> void square(C num, C *result)
 2        {
 3             *result = num * num;
 4        }
 5    
 6        template<class T> class Array
 7        {
 8        public:
 9              int getlength(void)
10             {
11                    return length;
12              }
13    
14              T & operator[](int i)
15              {
16                    return array[i];
17             }
18    
19              Array(int l)
20              {
21                    length = l;
22                    array = new T[length];
23              }
24    
25              ~Array(void)
26              {
27                    delete [] array;
28              }
29    
30        private:
31              int length;
32              T *array;
33        };
34    
35        int main(void)
36        {
37              int i, j = 3;
38             square(j, &i);
39    
40             double d, e = 4.1;
41              square(e, &d);
42    
43              Array<int> iarray(5);
44              for (i = 0; i < iarray.getlength(); ++i)
45              {
46                   iarray[i] = i;
47             }
48    
49              Array<double> darray(5);
50              for (i = 0; i < darray.getlength(); ++i)
51             {
52                    darray[i] = i * 2.1;
53              }
54    
55              return 0;
56        }
load more v
72%

My gdb is GNU gdb Red Hat Linux (6.3.0.0-1.162.el4rh) and I can't debug templates. How can I debug templates with this debugger?,Edit: To debug libc related files (in a Ubuntu distro) you would need to:

ex: main.cpp

#include <iostream>

template <typename T>
void coin(T v)
{
    std::cout << v << std::endl;
}

template<typename T>
class Foo
{
public:

    T bar(T c)
    {
        return c * 2;
    }
};

int main(int argc, char** argv)
{
    Foo<int> f;
    coin(f.bar(21));
}

compile with g++ -O0 -g main.cpp

gdb ./a.out
(gdb) b Foo<int>::bar(int)
   Breakpoint 2 at 0x804871d: file main.cpp, line 16.
   (gdb) b void coin<int>(int)
      Breakpoint 1 at 0x804872a: file main.cpp, line 6.
      (gdb) r
      ... debugging start

otherwise you could just use

(gdb) b main.cpp: 16
load more v
65%

toolchain version: GNU Arm Embedded Toolchain 9-2020-q2-update,mbed-os version: 6.5.0

Example of project that has such issue: https://github.com/vznncv/mbed-os-issue-gcc-debug

This example contains method SimpleSequenceWrapper::next in the main.cpp with this issue:

SimpleSequenceWrapper::next

Example of project that has such issue: https://github.com/vznncv/mbed-os-issue-gcc-debug

This example contains method SimpleSequenceWrapper::next in the main.cpp with this issue:

main.cpp
load more v
75%

This file describes GDB, the GNU symbolic debugger. ,This file documents the GNU debugger GDB. ,If you call a function interactively using print or call, any watchpoints you have set will be inactive until GDB reaches another kind of breakpoint or the call completes. , For a C++ template function, a given line in the function can correspond to any number of instantiations.

load more v
40%

All your existing debug configurations (of type cppdbg) will continue to work as expected.,The new template of type cppgdb will be used by default whenever you add a new Linux or WSL debug configuration.,Note: In Visual Studio 2019 version 16.6 Preview 2 you will need to manually set the configuration type to cppgdb. This bug has been fixed in Preview 3.,Note: Old Linux/WSL configurations of type cppdbg depend on the “environment” syntax. This alternative syntax is defined in our documentation.

We heard your feedback that the old debug configurations were too verbose, too confusing, and not well documented. The new cppgdb configuration has been simplified and looks like this:

{
   "type": "cppgdb",
   "name": "My custom debug configuration",
   "project": "CMakeLists.txt",
   "projectTarget": "DemoApp.exe",
   "comment": "Learn how to configure remote debugging. See here for more info http://aka.ms/vslinuxdebug",
   "debuggerConfiguration": "gdb",
   "args": [],
   "env": {}
}
load more v
22%

Is there any effective method to debug C++ template code? Just like debug program through gdb? Here, I want to show and inspect the instantiation of template,You can declare a template class without definition like this,Unit test is a good choice. But it's suitable for a built program. If a template program could't be built, I need other tools to help me to work around the instantiation of the template.,I wrote my own little library that produces compile time strings for every template type that I am working with so that at least I can verify that the instantiated types are correct at any given point in the code.

You can declare a template class without definition like this

template <typename... Ts>
   struct show_type;

Other "templates-debugging" queries related to "Debugging templates with GDB"