Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do breakpoints work in C++ code?

How do breakpoints work in C++ code? Are they special instructions inserted in between some assembler instructions when the code is compiled? Or is there something else in place? Also, how are stepping-through-the-code implemented? The same way as breakpoints...?

like image 587
gablin Avatar asked Oct 12 '10 14:10

gablin


People also ask

What does a breakpoint do in C?

A breakpoint stops the execution of a program and returns control to the debugger, where its variables and memory can be examined before continuing.

How do you use breakpoints?

Add breakpoints to your projectAdd a breakpoint by clicking the gutter next to the line number you want to pause at. A dot will appear next to the line number, and the line will be highlighted.

How do you set breakpoints in code?

To set a breakpoint in source code, click in the far left margin next to a line of code. You can also select the line and press F9, select Debug > Toggle Breakpoint, or right-click and select Breakpoint > Insert breakpoint. The breakpoint appears as a red dot in the left margin.


2 Answers

This is heavly depend on the CPU and debugger.

For example, one of the possible solution on x86 CPU:

  • Insert one-byte INT3 instruction on the required place
  • Wait until breakpoint exception hits
  • Compare exception address to the list of breakpoint to determine which one
  • Do breakpoint actions
  • Replace INT3 with original byte and switch the debugged process into trace mode (step-by-step execution of CPU instructions)
  • Continue debugged process
  • Immediately you catch trace exception - the instruction was executed
  • Put INT3 back

Watchpoints can be implemented in the similar way, but instead of INT3 you put the memory page where watched variable is into read only, or into no access mode, and wait for segmentation exception.

Stepping through assembly can also be done by using trace mode. Stepping through source lines can also be done by placing breakpoints onto next instructions, based on debug data.

Also some CPU has hardware breakpoint support, when you just load address into some register.

like image 155
Xeor Avatar answered Sep 27 '22 23:09

Xeor


According to this blog entry on technochakra.com you are correct:

Software breakpoints work by inserting a special instruction in the program being debugged. This special instruction on the Intel platform is “int 3″. When executed it calls the debugger’s exception handler.

I'm not sure how stepping into or over the next instruction is implemented though. However, the article goes on to add:

For practical reasons, it is unwise to ask for a recompilation whenever a breakpoint is added or deleted. Debuggers change the loaded image of the executable in memory and insert the “int 3″ instruction at runtime.

However, this would only be used for the "run to current line option".

like image 28
ChrisF Avatar answered Sep 28 '22 01:09

ChrisF