You can create a breakpoint at an offset from the current stopped position with gdb breakpoint +<offset>.
You can also create a breakpoint on a specific line number using either gdb break <linenumber> (for the current source file) or gdb break <filename>:<linenumber> (for a file other than the current file).
More details in the docs.
Answer from lsowen on Stack OverflowYou can create a breakpoint at an offset from the current stopped position with gdb breakpoint +<offset>.
You can also create a breakpoint on a specific line number using either gdb break <linenumber> (for the current source file) or gdb break <filename>:<linenumber> (for a file other than the current file).
More details in the docs.
There isn't a way to set a breakpoint relative to the start of a function such that it will retain its relative position if the source file is modified. This would sometimes be useful; but it is just a feature that nobody has added to gdb.
It could maybe be emulated from Python, though it couldn't work exactly the way that ordinary breakpoints work, because Python doesn't have access to the breakpoint resetting mechanism inside gdb.
A one-shot solution can be done either as shown in the other answer or from Python.
When I have needed this sort of functionality -- a breakpoint mid-function that is reasonably robust against source changes -- I have used "SDT" static probe points. These let you name such spots in your source.
Videos
In my CMake project I have executable with Google tests.
I compile my project with flag "-g". When I try to set breakpoint in GDB on specific line of file with tests
b 392
I get error "No line 392 in the current file.". This line really exists in the target file.
What can be the cause of this problem?
Is it possible to add breakpoint in an asm file using line number in GDB, like we can do with C files using b main.c:30 ? I know that we can add breakpoint using instruction bkpt or using the instruction address like b *0x08000000 , are there any other way to add breakpoint in asm code?
help running provides some hints:
There are step and next instuctions (and also nexti and stepi).
(gdb) help next
Step program, proceeding through subroutine calls.
Usage: next [N]
Unlike "step", if the current source line calls a subroutine,
this command does not enter the subroutine, but instead steps over
the call, in effect treating it as a single source line.
So we can see that step steps into subroutines, but next will step over subroutines.
The step and stepi (and the next and nexti) are distinguishing by "line" or "instruction" increments.
step -- Step program until it reaches a different source line
stepi -- Step one instruction exactly
Related is finish:
(gdb) help finish
Execute until selected stack frame returns.
Usage: finish
Upon return, the value returned is printed and put in the value history.
A lot more useful information is at https://sourceware.org/gdb/onlinedocs/gdb/Continuing-and-Stepping.html
Use command 'finish'; this sometimes does the same thing as 'step-out'. It'll finish what the stack is doing (a function, usually), and go to the next line after that. Look up the command for more info.