Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how GDB knows it has to break at specified break point?

Tags:

c++

c

gdb

A basic question & I am very new to C/C++ and GDB.

We use GDB to debug a process. We attach GDB to a process and then specify filename.c along with line number to put break point.

My question is "How would GDB or OS OR possibly anything else know that it has to break at specified line number (in filename.c) after we connect GDB to running process?"

What is coming into picture that, say, the current process is run in debug mode and a breakpoint is applied and the process execution has to break (wait for user input) at that point?

like image 230
Gana Avatar asked Jun 07 '13 15:06

Gana


People also ask

How does GDB set a breakpoint?

Setting breakpoints A breakpoint is like a stop sign in your code -- whenever gdb gets to a breakpoint it halts execution of your program and allows you to examine it. To set breakpoints, type "break [filename]:[linenumber]". For example, if you wanted to set a breakpoint at line 55 of main.

Why is GDB not stopping at breakpoint?

GDB normally ignores breakpoints when it resumes execution, until at least one instruction has been executed. If it did not do this, you would be unable to proceed past a breakpoint without first disabling the breakpoint. This rule applies whether or not the breakpoint already existed when your program stopped.

What does breakpoint do in GDB?

A breakpoint makes your program stop whenever a certain point in the program is reached. For each breakpoint, you can add conditions to control in finer detail whether your program stops.


2 Answers

The same way that if your program stops or crashes at a particular point, the debugger can tell you where in the program that point is.

For both of these to work the program binary must contain additional debugging information that associates addresses in the program image with locations in the source code (source file and line number.)

To add a breakpoint at a particular line the debugger finds the program address closest to that line, modifies the copy of the executable in memory to insert a special "break" instruction at that location which will cause the program's execution to be interrupted, then "traces" the program's execution and waits for it to reach the breakpoint and stop.

For more details see http://eli.thegreenplace.net/2011/01/23/how-debuggers-work-part-1/ and http://www.howzatt.demon.co.uk/articles/SimplePTrace.html

like image 111
Jonathan Wakely Avatar answered Sep 30 '22 16:09

Jonathan Wakely


I can't comment for the latest version of gdb - but many debuggers actually swap the assembly instruction at the desired breakpoint location (in memory) with an interrupt instruction. This "wakes up" the debugger which takes control at this point.

Using a substituted interrupt instruction means that the CPU can execute your program at full speed and "trip up" at the desired location.

Modern processors are very complex, however, and probably have far superior debugging features.

like image 40
PP. Avatar answered Sep 30 '22 17:09

PP.