2

In genral I know about usage of break point, but would like to know how exactly does it work? how it can interrupt the executing code? how it provides user interface(break point) on executable code and why it normally allows only 6 break points.

Thanks!

VRU
  • 109
  • 9
  • Where is that *6* coming from? – meskobalazs Sep 09 '15 at 14:37
  • @tom Why would that matter in the slightest? It doesn't even matter which debugger they are using. All CPUs work pretty much in the same way when it comes to breakpoints. Some OS might have some fluff API in between the debugger and the CPU, but that's rather irrelevant. – Lundin Sep 09 '15 at 14:50
  • 1
    [How does a debugger work?](http://stackoverflow.com/q/216819/995714) – phuclv Sep 09 '15 at 14:53
  • Hi Geeks! .. yeah I guess I can elaborate my question in much better way, putting User interface in one part and concern about the debugging process and breakpoint operation separated but it should not be wrong as all are related. I will try to update my question. – VRU Sep 10 '15 at 09:52

2 Answers2

14

There are usually two different kinds of breakpoint that a debugger can set: software breakpoints and hardware breakpoints.

A software breakpoint consists of replacing the instruction at the target address with a "break" instruction (e.g. int 3 on x86) and waiting for the CPU to execute it. When that instruction is hit, the CPU raises an exception and transfers control to the debugger. The upside is that you can define as many breakpoints as you want, but the downside is that this requires modifying the program in memory (which may not be possible for programs in read-only memory, or may cause the program to behave differently if it reads its own program memory).

The other kind, a hardware breakpoint, consists of setting a special debug register in the CPU to ask it to break when it hits a specified address. The CPU will automatically raise an exception when the program counter reaches that address. The upside is that no software modification is needed, but the downside is that this relies on a limited resource (debug registers) of which there may not be many. For example, x86 processors typically only have 4 debug address registers, so you can only set 4 hardware breakpoints at a time.

Debuggers typically pick a strategy depending on available resources (e.g. hardware breakpoints for the first 4 breakpoints and software breakpoints thereafter), although many can also be configured to force one particular type of breakpoint. For example, the popular debugger GDB has the hbreak command to explicitly create hardware breakpoints.

nneonneo
  • 171,345
  • 36
  • 312
  • 383
  • Hi nneonneo, thanks for your response. I am actually having a lot of doubts on debugging and breakpoint is one among that, my main concern is on software breakpoint... what exactly happens when program counter reaches breakpoint how the process handling happens. Can you elaborate more about on this? – VRU Sep 10 '15 at 09:48
1

It depends upon the processor and the operating system. On Linux, a debugger is using the ptrace(2) system call, which the kernel executes with the help of some hardware features of the processors.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547