How to Set Breakpoints on Future Shared Libraries with a Command Flag

How to set breakpoints on future shared libraries with a command flag

Replying to myself, I'd like to give the answer that someone gave me on IRC:


(gdb) apropos pending
actions -- Specify the actions to be taken at a tracepoint
set breakpoint -- Breakpoint specific settings
set breakpoint pending -- Set debugger's behavior regarding pending breakpoints
show breakpoint -- Breakpoint specific settings
show breakpoint pending -- Show debugger's behavior regarding pending breakpoints

And so set breakpoint pending on does the trick; it is used in cmds.gdb like e.g.

set breakpoint pending on
break <source file name>:<line number>

Make breakpoint pending on future shared library load? (y or [n])

You can't (easily) debug a stripped executable. Because the GDB debugger needs the DWARF debug information in it.

So link your code with just:

 g++ -std=c++11 -g   -O3 -Wall -Wextra ./opr.o ./main.o  -o torun

You may find easier to debug programs with less compiler optimizations (e.g. -O0 or -Og or -O1 at most, instead of -O3).

PS. There is some way to put the debug information in a different file, but that is a different question (and Linux specific).

GDB - breakpoint pending on future shared library load

The GDB start command sets a breakpoint at the start of main, and then runs your program. Presumably your program doesn't have a main symbol.

Read the error message carefully, it says Function "main" not defined. The breakpoint at _start is already set; if that was the problem you'd have seen the prompt after that command, and wouldn't have seen a numeric address. (Try it with b xyz or b printf.)


You're looking for the run command, which just runs the program without looking for any symbols in it.

Use help start and help run in GDB to see what they do.

Or the online docs: https://sourceware.org/gdb/onlinedocs/gdb/Starting.html

  • run: just start the program, no new breakpoints added.
  • start: set a one-time breakpoint in main before run. Useful for compiled C/C++, or hand-written asm that defines a main and uses the standard CRT startup files. This is what you tried to use.
  • starti: stop at the first asm instruction of the process. Especially useful for a PIE executable without symbols, where the entry-point numeric address isn't known until after ASLR chooses it, if you run it with ASLR enabled. (The GDB default behaviour is to disable ASLR).

starti is a relatively recent feature; before that one common hack was to use b *0, which leads to an error right after the process starts, before any instructions execute. Stopping at the first machine code instruction in GDB

Can I make .gdbinit YES to Make breakpoint pending on future shared library load?

Add set breakpoint pending on in .gdbinit anywhere before break my_function.
See gdb documentation:

set breakpoint pending on

This indicates that an unrecognized breakpoint location should automatically result in a pending breakpoint being created.

Breakpoint on the .init Section of a Shared Library

Does anybody know the reason for the error in the raw address case?

The reason: this address isn't mapped yet (the library hasn't been loaded yet).

It works for break _init case because GDB can check whether any newly-loaded shared library defines that symbol. But it's not smart enough to check whether address 0x7ffff6ebd9d0 becomes breakpoint-able.

You can work around this by using (gdb) set stop-on-solib-events 1. GDB will then stop every time new shared libraries are loaded, before running their initializers.

Once libinkscape_base.so shows up, you will be able to use the address breakpoint as desired.

How to set breakpoint with gdb on function from stripped shared library?

Is it possible to set breakpoint on function fun using gdb?

Yes: GDB can set a breakpoint on arbitrary address:

(gdb) break *0x12345678

How to define the address of fun at runtime?

Since GDB by default disables ASLR, the address of fun will not change from run to run (assuming you run the program under GDB from the start).

Therefore, you only need to find the address of fun once.

Let's assume that your lib.so is linked at 0 (most non-prelinked shared libraries are).

Further let's assume that you are on Linux.

Then info proc map will tell you where the lib.so is loaded (you want the first start address belonging to it). Add that start address to the value of fun you found by disassembling, and set a breakpoint there.

How to confirm action (answer Y) in gdb script?

(gdb) set breakpoint pending on

This will make gdb skip asking for confirmation, quote from the docs:

This indicates that an unrecognized breakpoint location should
automatically result in a pending breakpoint being created.

How do I set a break point on the exact line number in a shared library?

For Debugging shared libraries, you need to use

set breakpoint pending -- Set debugger's behavior regarding pending breakpoints.

It's quite common to have a breakpoint inside a shared library. Shared libraries can be loaded and unloaded explicitly, and possibly repeatedly, as the program is executed. To support this use case, gdb updates breakpoint locations whenever any shared library is loaded or unloaded. Typically, you would set a breakpoint in a shared library at the beginning of your debugging session, when the library is not loaded, and when the symbols from the library are not available. When you try to set breakpoint, gdb will ask you if you want to set a so called pending breakpoint—breakpoint whose address is not yet resolved.

gdb provides some additional commands for controlling what happens when the `break' command cannot resolve breakpoint address specification to an address:

set breakpoint pending auto
This is the default behavior. When gdb cannot find the breakpoint location, it queries you whether a pending breakpoint should be created.

set breakpoint pending on
This indicates that an unrecognized breakpoint location should automatically result in a pending breakpoint being created.

set breakpoint pending off
This indicates that pending breakpoints are not to be created. Any unrecognized breakpoint location results in an error. This setting does not affect any pending breakpoints previously created.

show breakpoint pending
Show the current behavior setting for creating pending breakpoints.

Coming to your question. i.e Skipping a line

use jump +1 when your code reaches before that shared library line(skip_me()).

References

http://wiki.documentfoundation.org/Development/How_to_debug

gdb: how to set breakpoints on future shared libraries with a --command flag

http://bhushanverma.blogspot.in/2009/08/how-to-debug-shared-library-using-gdb.html

http://www.toptip.ca/2010/06/gdb-skip-instructions-or-lines-while.html

Can I use gdb to skip a line?



Related Topics



Leave a reply



Submit