28

I'm debugging C++ code with GDB and when it enters a constructor of some object containing standard library objects, it shows me the constructor of these objects (like std::map) and everything that's underneath.

I know about the next operator, but I'd prefer to basically black list any standard library code, which is never the source of the error I'm investigating. The wished behavior is that a simple skip would send me to the next "user-land" code.

Warren Seine
  • 2,311
  • 2
  • 25
  • 38
  • I don't know if [this answer](http://stackoverflow.com/questions/1448426/how-to-avoid-entering-librarys-source-files-while-debugging-in-qt-creator-with-g/1475262#1475262) helps. – jonsca Apr 15 '11 at 11:55
  • A little bit, but the example is quite simple. When there's multiple constructors with overloading, inheritance, it's much more tricky. – Warren Seine Apr 15 '11 at 12:32
  • 1
    I meant more the `set auto-solib-add off` example than the one with all of the code. – jonsca Apr 15 '11 at 12:37
  • The code I'm talking about is not in a shared object, it's part of the binary because it's template-generated. – Warren Seine Apr 15 '11 at 15:11
  • This is a long time enhancement request for gdb. someone sent a patch for this today, so hopefully it will be answerable soon. http://sourceware.org/bugzilla/show_bug.cgi?id=8287 – matt Apr 26 '11 at 03:27
  • @matt I've looked at this bug a few days ago. According to the tracker, nothing happened since last year (what do you mean by "today"?). I wish I had more time / more experience to have a look at it and perhaps help. – Warren Seine Apr 27 '11 at 14:31
  • @WarrenSeine it was sent to the mailing list http://sourceware.org/ml/gdb-patches/2011-04/msg00455.html – matt Apr 28 '11 at 04:58
  • @matt: We're att the end of 2012 and this feature is still not part of gdb... – nimrodm Nov 12 '12 at 11:45
  • 1
    @nimrodm: actually the patch adding this feature was committed a year ago. – matt Nov 14 '12 at 15:08
  • @matt: Correct. My mistake. Very useful feature. – nimrodm Nov 15 '12 at 08:52
  • https://stackoverflow.com/questions/1133365/preventing-gdb-from-stepping-into-a-function-or-file – Ciro Santilli OurBigBook.com Sep 11 '17 at 15:27

4 Answers4

33

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
Dhiraj Reddy
  • 542
  • 1
  • 4
  • 11
  • 1
    In case your STL file location varies, you can, eg, `skip -gfile bits/*.h`. – John H. Jun 02 '17 at 15:32
  • 1
    It is also useful to `skip -function "operator new"`. – John H. Jun 02 '17 at 15:33
  • Supposed to work but not for me :( `skip -gfi /usr/include/c++/6/bits/*.h` `run` `s` `std::vector >::~vector (this=0x7fffffffaf10, __in_chrg=) at /usr/include/c++/6/bits/stl_vector.h:426` – Jeff Trull Jun 21 '17 at 15:18
  • Can concur: file glob matching appears to be dysfunctional in GDB 10, however, function regexp matching works as documented. – Pavel Kirienko Jan 07 '22 at 20:22
11

* Changes in GDB 7.4

  • GDB now allows you to skip uninteresting functions and files when stepping with the "skip function" and "skip file" commands.
matt
  • 5,364
  • 1
  • 25
  • 25
  • how to add these in the gdbinit file without gdb asking "Ignore file pending future shared library load? (y or [n])" – viktorzeid Dec 10 '14 at 20:26
  • 1
    This lets me skip all code in the current file (`skip file`). Can I not preconfigure gdb to skip everything in, for example, `/usr/include`? – dhardy May 11 '15 at 11:55
  • 1
    @dhardy AFAIK there is no way at the moment to do that beyond calling 'skip file /usr/include/foo.h' for everything in /usr/include – matt May 12 '15 at 18:33
  • @dhardy, wanted to do the same and found a way by using python (see at the bottom [here](http://xaizek.github.io/2016-05-26/skipping-standard-library-in-gdb/)), it just calls `skip file` for whole standard library when you start debugging a C++ program. The obvious requirement is that gdb should be compiled with python support (or rewrite that code in Guile if you have that instead). – xaizek May 26 '16 at 12:15
2

Step instructions and skip all files without source

This will be too slow for most applications, but it is fun!

Based on: Displaying each assembly instruction executed in gdb

class ContinueUntilSource(gdb.Command):
    def __init__(self):
        super().__init__(
            'cus',
            gdb.COMMAND_BREAKPOINTS,
            gdb.COMPLETE_NONE,
            False
        )
    def invoke(self, argument, from_tty):
        argv = gdb.string_to_argv(argument)
        if argv:
            gdb.write('Does not take any arguments.\n')
        else:
            done = False
            thread = gdb.inferiors()[0].threads()[0]
            while True:
                message = gdb.execute('si', to_string=True)
                if not thread.is_valid():
                    break
                try:
                    path = gdb.selected_frame().find_sal().symtab.fullname()
                except:
                    pass
                else:
                    if os.path.exists(path):
                        break
ContinueUntilSource()

Tested in Ubuntu 16.04, GDB 7.11. GitHub upstream.

std::function case

How to step debug into std::function user code from C++ functional with GDB?

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
0

Modified from Ciro Santilli's answer command ss steps inside specific source. You may specify source file name or the current one will be stepped. Very handy for stepping through bison/yacc sources or other meta-sources that generate С code and insert #line directives.

import os.path

class StepSource(gdb.Command):
    def __init__(self):
        super().__init__(
            'ss',
            gdb.COMMAND_BREAKPOINTS,
            gdb.COMPLETE_NONE,
            False
        )
    def invoke(self, argument, from_tty):
        argv = gdb.string_to_argv(argument)
        if argv:
            if len(argv) > 1:
                gdb.write('Usage:\nns [source-name]]\n')
                return
            source = argv[0]
            full_path = False if os.path.basename(source) == source else True
        else:
            source = gdb.selected_frame().find_sal().symtab.fullname()
            full_path = True
        thread = gdb.inferiors()[0].threads()[0]
        while True:
            message = gdb.execute('next', to_string=True)
            if not thread.is_valid():
                break
            try:
                cur_source = gdb.selected_frame().find_sal().symtab.fullname()
                if not full_path:
                    cur_source = os.path.basename(cur_source)
            except:
                break
            else:
                if source == cur_source:
                    break
StepSource()

Known bugs

  1. it doesn't interrupt debugger on SIGINT while running;
  2. changed pass to break on exception as not sure whether it is right.
midenok
  • 930
  • 10
  • 14
  • The idea is good and the code a very good start, but it isn't actually an answer to this question "how to skip standard files". Instead it is an answer to the later posted question https://stackoverflow.com/questions/59059741/how-can-we-limit-gdb-to-debug-only-a-specific-source-for-example-yacc-lex-for, where I'll post an answer based on this with the issues solved. ... I'd really like to upvote this, but the "(good) answer to the wrong question" means it is "neutral" in the end. Still: big thanks for posting htis.! – Simon Sobisch Jan 31 '23 at 09:19