Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automate gdb: show backtrace at every call to function puts

Tags:

gdb

I want to debug some program. I need backtraces from all calls to some function, e.g. puts.

Now I use such gdb script:

set width 0
set height 0
set verbose off
break puts
commands 1
backtrace
continue
end

But starting it with

gdb --batch --command=script --args ./some_program arguments

Gives a error:

Function "puts" not defined.
Make breakpoint pending on future shared library load? (y or [n]) [answered N; input not from terminal]
/root/script:5: Error in sourced command file:
No breakpoint number 1.

How can I set breakpoint in script for library call?

like image 477
osgx Avatar asked Mar 05 '10 17:03

osgx


People also ask

How do I check bt of all threads?

To display the backtrace for several or all of the threads, use the command thread apply (see thread apply). For example, if you type thread apply all backtrace , gdb will display the backtrace for all the threads; this is handy when you debug a core dump of a multi-threaded program.

How do I print backtrace?

To print a backtrace of the entire stack, use the backtrace command, or its alias bt . This command will print one line per frame for frames in the stack. By default, all stack frames are printed. You can stop the backtrace at any time by typing the system interrupt character, normally Ctrl-c .

How do I list breakpoints in gdb?

You can see these breakpoints with the GDB maintenance command `maint info breakpoints' . Using the same format as `info breakpoints' , display both the breakpoints you've set explicitly, and those GDB is using for internal purposes. Internal breakpoints are shown with negative breakpoint numbers.

What does backtrace mean in gdb?

A backtrace is a summary of how your program got where it is. It shows one line per frame, for many frames, starting with the currently executing frame (frame zero), followed by its caller (frame one), and on up the stack.


1 Answers

Try this instead:

set width 0
set height 0
set verbose off
start  # runs to main, so shared libraries are loaded
       # after you reach main, GDB should have libc symbols, "puts" among them
break puts
commands 1
backtrace
continue
end

If this doesn't work, please state operating system version.

EDIT: as osgx correctly points out, the other alternative is to add

set breakpoint pending on

before break puts

like image 157
Employed Russian Avatar answered Sep 25 '22 09:09

Employed Russian