How to step through C++ code with structured binding in GDB without jumping to the declaration line?

Viewed 221

Given a program:

[]$ cat a.cpp
#include <iostream>

int main(){
    auto [a, b] = std::make_pair(1, 2);
    for(int x = 0; x < 2; ++x) {
        a += b;
        b += x;
    }
}

If I compile it and step through it line by line in GDB:

[]$ g++ -std=c++17 -g a.cpp
[]$ gdb -q a.out
Reading symbols from a.out...
(gdb) break main
Breakpoint 1 at 0x114d: file a.cpp, line 4.
(gdb) run
Starting program: /tmp/a.out 

Breakpoint 1, main () at a.cpp:4
4       auto [a, b] = std::make_pair(1, 2);
(gdb) next
5       for(int x = 0; x < 2; ++x) {
(gdb) 
4       auto [a, b] = std::make_pair(1, 2);
(gdb) 
6           a += b;
(gdb) 
4       auto [a, b] = std::make_pair(1, 2);
(gdb) 
7           b += x;
(gdb) 
5       for(int x = 0; x < 2; ++x) {
(gdb) 
4       auto [a, b] = std::make_pair(1, 2);
(gdb) 
6           a += b;
(gdb) 
4       auto [a, b] = std::make_pair(1, 2);
(gdb) 
7           b += x;
(gdb) 
5       for(int x = 0; x < 2; ++x) {
(gdb) 
9   }

Then the structured binding declaration line is stepped over every time the variable is accessed or written to.

Is there any way so that it does not step through those lines, expect for the first time, similar to this:

(gdb) run
Starting program: /tmp/a.out 

Breakpoint 1, main () at a.cpp:4
4       auto [a, b] = std::make_pair(1, 2);
(gdb) next
5       for(int x = 0; x < 2; ++x) {
(gdb) 
6           a += b;
(gdb) 
7           b += x;
(gdb) 
5       for(int x = 0; x < 2; ++x) {
(gdb) 
6           a += b;
(gdb) 
7           b += x;
(gdb) 
5       for(int x = 0; x < 2; ++x) {
(gdb) 
9   }

  • It's possible to change the structured binding to auto& a = pair.first; auto& b = pair.second and it works correctly, but it means not using structured binding at all.
  • Setting a breakpoint at the line and enter command commands | next | end does not completely work, and still require manual work for each structured binding.
  • Adding any optimization flag (even -Og in this case) optimize the whole code away (because it doesn't have any observable side effect); or optimizes some unused variables away.
    When there are observable side effect however, it does work.
  • Searching online for "gdb debug structured binding", or even "gdb skip through particular line while stepping" does not give any useful result. The skip command can only skip through functions, not lines.

Tools version:

[]$ g++ --version
g++ (Arch Linux 9.2.1+20200130-2) 9.2.1 20200130
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

[]$ gdb --version
GNU gdb (GDB) 9.1
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
0 Answers
Related