How to do a GDB backtrace showing only function names and nothing else?

I've been doing a bunch of analysis of complex code, and to explain how things work I often want to give backtraces to some point of interest containing only function names.

However, when I do just:

bt

it adds a lot of extra information such as addresses and arguments which I have to remove manually:

#0  f2 (i=0) at main.c:1
#1  0x0000555555555155 in f1 (i=1) at main.c:6
#2  0x0000555555555177 in main (argc=1, argv=0x7fffffffc178) at main.c:10

Is there a way to print just the function names and nothing else as in:

f2
f1
main

?

Test program:

main.c

int f2(int i) {
    return i + 1;
}

int f1(int i) {
    return f2(i) + 1;
}

int main(int argc, char *argv[]) {
    return f1(argc);
}

Compile and run:

g++ -ggdb3 -O0 -std=c++11 -Wall -Wextra -pedantic -o main.out main.c
gdb -nh -batch -q -ex 'b f2' -ex r -ex bt main.out

For this use case, I'm mostly interested in a single usage option, but if there are any set configs that get the job done I'm also interested in knowing about them.

Tested in Ubuntu 19.10, GDB 8.3.

Upvotes: 2

Views: 1377

Answers (3)

Yang Song
Yang Song

Reputation: 11

try:

set print address off

(gdb) help set print

Generic command for setting how things print.

List of set print subcommands:

set print address -- Set printing of addresses
set print array -- Set prettyprinting of arrays
set print array-indexes -- Set printing of array indexes
set print asm-demangle -- Set demangling of C++/ObjC names in disassembly listings
set print demangle -- Set demangling of encoded C++/ObjC names when displaying symbols
set print elements -- Set limit on string chars or array elements to print
...

Upvotes: 0

Abhishek
Abhishek

Reputation: 49

(gdb) set print frame-info short-location

(gdb) bt 5

 #0  pthread_cond_wait@@GLIBC_2.3.2 ()
 #1  log4cxx::helpers::Condition::await(log4cxx::helpers::Mutex&) ()
 #2  log4cxx::AsyncAppender::dispatch(apr_thread_t*, void*) ()
 #3  launcher ()
 #4  start_thread ()

Upvotes: 3

Since I couldn't find an option easily, I ended up hacking a GDB Python script for it:

class BtFuncOnly(gdb.Command):
    """Backtrace with function names only
"""
    def __init__(self):
        super(self.__class__, self).__init__('btf', gdb.COMMAND_FILES)
    def invoke(self, argument, from_tty):
        frame = gdb.selected_frame()
        while frame is not None:
            gdb.write('{}\n'.format(frame.name()))
            frame = frame.older()
BtFuncOnly()

GitHub upstream.

Now I can to:

btf

and that gives me the desired output.

Upvotes: 3

Related Questions