Tell Gdb to Skip Standard Files

Tell gdb to skip standard files

gdb 7.12 supports file globbing to specify the files to skip in the debugger. The documentation for the same is as below:

https://sourceware.org/gdb/onlinedocs/gdb/Skipping-Over-Functions-and-Files.html

To skip stepping into all library headers in the directory /usr/include/c++/5/bits, add the below lines to ~/.gdbinit

# To skip all .h files in /usr/include/c++/5/bits
skip -gfi /usr/include/c++/5/bits/*.h

Instead to skip a specific file, say stl_vector.h, add the below lines to ~/.gdbinit

# To skip the file /usr/include/c++/5/bits/stl_vector.h
skip file /usr/include/c++/5/bits/stl_vector.h

Doing the above with gdb 7.11 and below version leads to the below error:

Ignore function pending future shared library load? (y or [n]) [answered N; input not from terminal]

However, gdb 7.12 seems to have solved the above issue.

This blog addresses the same problem for gdb version 7.11 or below.

Note - You can use the below command from the gdb command prompt to list all the files marked for skipping

info skip

Tell gdb to skip _only_ std when stepping

This answer suggests using python scripting ability for gdb. So I managed to construct this a bit messy script that works for me most of the time (although sometimes it requires a step or two to step out of ASAN assembly):

import gdb
import re

def stop_handler(event):
frame_name = gdb.selected_frame().name()
if re.search("(^std::.*)|(^boost::.*)|(^__gnu_cxx::.*)|(^__sanitizer::.*)|(^__ubsan::.*)", frame_name) != None:
gdb.execute("step")
return

symtab = gdb.selected_frame().find_sal().symtab
fullname = symtab.fullname()

matches = ["libsanitizer", "sysdeps", "/usr/include/", "ubsan"]
if any(x in fullname for x in matches):
gdb.execute("step")
return

objfile = symtab.objfile.filename

if "libubsan.so" in objfile:
gdb.execute("step")
return

gdb.events.stop.connect(stop_handler)

I'm using Clion and this script makes it blink really fast while stepping through files, so I guess be aware of a blinking hazard

Preventing GDB from stepping into a function (or file)

Starting with GDB 7.4, skip can be used.

Run info skip, or check out the manual for details: https://sourceware.org/gdb/onlinedocs/gdb/Skipping-Over-Functions-and-Files.html

GDB C++ How to Stop Stepping Inside Standard Libraries

If you have GDB 7.12.1 or above you can add the following to your ~/.gdbinit file:

skip -gfi /usr/include/c++/*/*/*
skip -gfi /usr/include/c++/*/*
skip -gfi /usr/include/c++/*

Double check that these are in fact the right places for those libs. This answer comes from here.

Some options without editing gdb init

If you want to "step over" a line (like std::vector<int> a;) without stepping into it, you can use next.

If you have a situation like int b = is_negative(a[0]) and you want to stop in is_negative you have a few options:

  1. Step into the a[0] function then use finish to step out again.
  2. Use tbreak is_negative to create a temporary breakpoint on the is_negative function then use next to step which will break on the breakpoint.

2 is faster in the case where you have something with many sub calls in a function like:

is_negative(a[b.at(2)] * c[3]);

Can I use gdb to skip a line without having to type line numbers?

jump +1

jumps to the next line line i.e. skipping the current line. You may also want to combine it with tbreak +1 to set a temporary breakpoint at the jump target.

See http://sourceware.org/gdb/current/onlinedocs/gdb/Specify-Location.html for more ways of expressing locations with gdb.

Note that without a breakpoint gdb is likely to continue execution normally instead of jumping. So if jumping doesn't seem to work, make sure you set a breakpoint at the destination.



Related Topics



Leave a reply



Submit