如何在跳过所有内部实现的同时跳转到 GDB 中 std::function 中的函数?

How can I jump into a function held inside std::function in GDB while skipping all the internal implementation?

本文关键字:GDB 函数 std function 实现 内部      更新时间:2023-10-16

取以下代码:

#include <iostream>
#include <memory>
#include <functional>
std::function<int()> getint = []
{
return 5;
};
void foo(int i)
{
std::cout<<i<<std::endl;
}
int main()
{
foo(getint());
}

我停在第 17 行的断点处。我想进入getint功能。默认情况下使用 gdb 的step可以带我浏览一堆我不感兴趣的std::function内部废话。如果我继续单步执行,我最终会打通 lambda,但必须为每个std::function调用执行此操作非常烦人。

我尝试使用skip命令:

skip -rfu ^std::.*

但这导致step直接跳入foo,完全跳过std::function内部的λ。

是否可以以某种方式配置 gdb,其中第 17 行的step会将我直接带到第 7 行的 lambda?

好的,我设法使用一个简单的python脚本解决了这个问题:

import gdb
import re
def stop_handler(event):
frame_name = gdb.selected_frame().name();
if re.search("(^std::.*)|(^boost::.*)", frame_name) != None:
gdb.execute("step")
gdb.events.stop.connect(stop_handler)