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

Take this code:

#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());
}

I'm stopped at a breakpoint at line 17. I want to step into the getint function. Using gdb's step by default takes me through a bunch of std::function's internal crap that I'm not interested in. If I continue stepping, I will eventually get through to the lambda, but having to do this for every std::function call is extremely annoying.

I tried using the skip command:

skip -rfu ^std::.*

but this causes step to jump straight into foo, completely skipping the lambda inside std::function.

Is it possible to configure gdb in a way, where step at line 17 would take me straight to the lambda at line 7?

1 answer

  • answered 2018-09-21 13:44 user697683

    Ok, I managed to solve this using a simple python script:

    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)