1
votes

I am learning some mechanism of breakpoint and I learned that 'In x86, there exist a instruction called int3 for debugger to interrupt the CPU. And then CPU will interrupt the running program by signal'.

For example:

8048e20:   55                      push   %ebp
8048e21:   89 e5                   mov    %esp,%ebp

When the user input

b *0x8048e21

The instruction will be replaced by int3(opcode 0xcc) and become this:

8048e20:   55                      push   %ebp
8048e21:   cc e5                   mov    %esp,%ebp

And it will stop at the right place.

Then comes the question:

What would happen if I set the breakpoint not at the beginning of a instruction? ie, if I input:

b *0x8048e22

will debugger still replace the e5 with cc? So I write a simple example and run it with gdb. enter image description here

As you can see above, I set two break points and the second is at the middle of a break points. I Input r and stop at the first breakpoint and input c and run to the end.

So it seems that the gdb ignore the second breakpoint. (For if it really repalce it with a int3 the program would be totally wrong).

Question: What happen to the second breakpoint, more specifically, what does gdb deal with it( or what I learn is wrong?)

Edit: @dbrank already give a great example about altering the data field of a instruction, I will try to make it more comprehensive with a similar example (it seems the register). enter image description here(Any reference about mechanism of breakpoint is appreciated!)

1

1 Answers

1
votes

Inserting breakpoint in the middle of instruction will alter the instruction.

See this example of a program, where inserting a breakpoint overwrites original value assigned to variable (42 (0x2a)) with breakpoint instruction (0xcc (204)).

gdb debug inserting breakpoint in middle of instruction

You can find more about how breakpoints work here.

You can also look into GDB sources (breakpoint.c & infrun.c mostly).