给定一个程序:
[]$ 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;
}
}
如果我编译它并在 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 }
然后每次访问或写入变量时都会跳过结构化绑定声明行。
有什么方法可以让它不跨过这些线,期待第一次,类似于这样:
(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 }
auto& a = pair.first; auto& b = pair.second
并且它可以正常工作,但这意味着根本不使用结构化绑定。commands | next | end
并不完全有效,每个结构化绑定仍然需要手动工作。-Og
)都会优化整个代码(因为它没有任何可观察到的副作用);或者优化一些未使用的变量。skip
命令只能跳过函数,不能跳过行。工具版本:
[]$ 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.
这个问题有答案吗?我也遇到了同样的问题,但无法解决。