GDB cannot step or break into template functions compiled with g++ -Og

Iru Cai vimacs.hacks@gmail.com
Tue Nov 1 09:40:22 GMT 2022


Hi,

Tested with GDB 12.1 and GDB in binutils-gdb git 1751ca43fda, I find 
that GDB cannot step or break into C++ template functions compiled with 
g++ -Og (GCC version 12.2.0), for example:

```

template <typename T = int>
int t0(int n)
{
     int sum = 0;
     for (int i = 0; i < n; ++i) {
         sum += i * i;
     }
     return sum;
}

int t1(int n)
{
     int sum = 0;
     for (int i = 0; i < n; ++i) {
         sum += i * i;
     }
     return sum;
}

int main()
{
     int a = t0(5);
     int b = t1(5);
     return a + b;
}

```

compile with: g++ -g -Og -o test test.cpp

Debugging with GDB 12.1, I get:

```

(gdb) b t0
Breakpoint 1 at 0x1151: file test.cpp, line 2.
(gdb) b t1
Breakpoint 2 at 0x1119: file test.cpp, line 14.
(gdb) r
Starting program: /tmp/test

Breakpoint 1, t0<int> (n=n@entry=5) at test.cpp:2
2    int t0(int n)
(gdb) s
main () at test.cpp:23
23        int b = t1(5);
(gdb) c
Continuing.

Breakpoint 2, t1 (n=n@entry=5) at test.cpp:14
14        for (int i = 0; i < n; ++i) {
(gdb) n
15            sum += i * i;

```

With GDB git, it even cannot recognize the template function name unless 
I use the full name:

```

(gdb) b t0
Function "t0" not defined.
Make breakpoint pending on future shared library load? (y or [n]) n
(gdb) b t0<int>(int)
Breakpoint 1 at 0x1151: file test.cpp, line 2.
(gdb) r
Starting program: /tmp/test

Breakpoint 1, t0<int> (n=n@entry=5) at test.cpp:2
2    int t0(int n)
(gdb) s
main () at test.cpp:23
23        int b = t1(5);

```

While LLDB 14.0.6 can step into this function:

```

(lldb) b t0
Breakpoint 1: where = test`int t0<int>(int) at test.cpp:5:11, address = 
0x0000000000001151
(lldb) r
Process 82245 launched: '/tmp/test' (x86_64)
Process 82245 stopped
* thread #1, name = 'test', stop reason = breakpoint 1.1
     frame #0: 0x0000555555555151 test`int t0<int>(n=5) at test.cpp:5:11
    2       int t0(int n)
    3       {
    4           int sum = 0;
-> 5           for (int i = 0; i < n; ++i) {
    6               sum += i * i;
    7           }
    8           return sum;
(lldb) n
Process 82245 stopped
* thread #1, name = 'test', stop reason = step over
     frame #0: 0x0000555555555156 test`int t0<int>(n=5) at test.cpp:4:6
    1       template <typename T = int>
    2       int t0(int n)
    3       {
-> 4           int sum = 0;
    5           for (int i = 0; i < n; ++i) {
    6               sum += i * i;
    7           }

```

Can anyone help to fix this?

Thanks,

Iru



More information about the Gdb mailing list