开发者

Tell gdb to skip standard files

I'm debugging C++ code with GDB and when it enters a constructor of some object containing standard library obje开发者_运维问答cts, 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.


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


* 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.


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?


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.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜