public inbox for gdb@sourceware.org
 help / color / mirror / Atom feed
* software breakpoint in gdb
@ 2012-01-29 16:48 Xin Tong
  2012-01-29 16:53 ` Jan Kratochvil
  0 siblings, 1 reply; 7+ messages in thread
From: Xin Tong @ 2012-01-29 16:48 UTC (permalink / raw)
  To: gdb

Hello

I have 2 questions on how software breakpoint is done in gdb.

One of the most commonly used feature is breakpoint. Breakpoint can be
implemented in two ways, hardware breakpoint and software breakpoint.

Hardware breakpoint requires hardware support. Typically, x86
processors come with some debugging register and when a breakpoint is
required, the pc of the instruction is stored into the debug register
and checked whenever an instruction executes.

Software breakpoint, on x86 if no hardware breakpoint register is
supported or the machine runs out of hardware registers. It will fall
back to software breakpoint. Software breakpoint involves replacing
the breakpointed instruction with an interrupt instruction.

What if the interrupt instruction is bigger than breakpointed
instruction ? Where is the breakpointed instruction kept after it is
overwritten by an interrupt instruction in gdb ?


Thanks

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: software breakpoint in gdb
  2012-01-29 16:48 software breakpoint in gdb Xin Tong
@ 2012-01-29 16:53 ` Jan Kratochvil
  2012-01-30  0:54   ` Robert Dewar
  0 siblings, 1 reply; 7+ messages in thread
From: Jan Kratochvil @ 2012-01-29 16:53 UTC (permalink / raw)
  To: Xin Tong; +Cc: gdb

On Sun, 29 Jan 2012 17:48:30 +0100, Xin Tong wrote:
> What if the interrupt instruction is bigger than breakpointed
> instruction ?

I do not know about such architecture, it probably does not exist.


> Where is the breakpointed instruction kept after it is
> overwritten by an interrupt instruction in gdb ?

struct breakpoint->loc->target_info->shadow_contents


Regards,
Jan

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: software breakpoint in gdb
  2012-01-29 16:53 ` Jan Kratochvil
@ 2012-01-30  0:54   ` Robert Dewar
  2012-01-30  5:54     ` Kevin Pouget
  0 siblings, 1 reply; 7+ messages in thread
From: Robert Dewar @ 2012-01-30  0:54 UTC (permalink / raw)
  To: Jan Kratochvil; +Cc: Xin Tong, gdb

On 1/29/2012 11:53 AM, Jan Kratochvil wrote:
> On Sun, 29 Jan 2012 17:48:30 +0100, Xin Tong wrote:
>> What if the interrupt instruction is bigger than breakpointed
>> instruction ?
>
> I do not know about such architecture, it probably does not exist.

It sure would be a disastrous mistake to make in any architectural
design. the x86 of course has a one byte interrupt instruction
precisely for this purpose.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: software breakpoint in gdb
  2012-01-30  0:54   ` Robert Dewar
@ 2012-01-30  5:54     ` Kevin Pouget
  2012-01-30  6:17       ` Yao Qi
  2012-01-30 13:19       ` Robert Dewar
  0 siblings, 2 replies; 7+ messages in thread
From: Kevin Pouget @ 2012-01-30  5:54 UTC (permalink / raw)
  To: Robert Dewar; +Cc: Jan Kratochvil, Xin Tong, gdb

On Mon, Jan 30, 2012 at 1:53 AM, Robert Dewar <dewar@adacore.com> wrote:
> On 1/29/2012 11:53 AM, Jan Kratochvil wrote:
>>
>> On Sun, 29 Jan 2012 17:48:30 +0100, Xin Tong wrote:
>>>
>>> What if the interrupt instruction is bigger than breakpointed
>>> instruction ?
>>
>>
>> I do not know about such architecture, it probably does not exist.
>
>
> It sure would be a disastrous mistake to make in any architectural
> design. the x86 of course has a one byte interrupt instruction
> precisely for this purpose.

Hello,

I would be curious about why it would be "a disastrous mistake".
Please correct me if I'm wrong in my understanding:
- gdb sets a breakpoint at a given PC, save corresponding instruction,
instructionS or part of instruction,
- when the OS hits the breakpoint, GDB receives a SIGTRAP,
- GDB rewrites the original memory content and moves back the PC to
the BP address
- GDB continues the inferior step-by-step, *until the PCs steps out of
the breakpoint area* -- one step only if not(interrupt instruction is
bigger than breakpointed instruction)
- GDB re-set the breakpoint inst at the BP address, and continues the execution


so, is it doing 2 steps to jump over the breakpoint which is complicated?

Thnaks,

Kevin

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: software breakpoint in gdb
  2012-01-30  5:54     ` Kevin Pouget
@ 2012-01-30  6:17       ` Yao Qi
  2012-01-30 15:51         ` Marc Khouzam
  2012-01-30 13:19       ` Robert Dewar
  1 sibling, 1 reply; 7+ messages in thread
From: Yao Qi @ 2012-01-30  6:17 UTC (permalink / raw)
  To: Kevin Pouget; +Cc: Robert Dewar, Jan Kratochvil, Xin Tong, gdb

On 01/30/2012 01:54 PM, Kevin Pouget wrote:
> I would be curious about why it would be "a disastrous mistake".
> Please correct me if I'm wrong in my understanding:

The real disaster is interrupt instruction may corrupt the following
instruction of breakpointed instruction, if interrupt instruction is
longer than breakpointed instruction.  For example,

   JMP L1:
   ...
   INSN1 (1-byte)
L1:INSN2 (2-byte)

Supposing the length of interrupt instruction is 2-byte, when setting
breakpoint on INSN1, the INSN2 is corrupted.  If the program runs JMP
instruction to L1 directly, something wrong will be executed.

-- 
Yao (齐尧)

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: software breakpoint in gdb
  2012-01-30  5:54     ` Kevin Pouget
  2012-01-30  6:17       ` Yao Qi
@ 2012-01-30 13:19       ` Robert Dewar
  1 sibling, 0 replies; 7+ messages in thread
From: Robert Dewar @ 2012-01-30 13:19 UTC (permalink / raw)
  To: Kevin Pouget; +Cc: Jan Kratochvil, Xin Tong, gdb

On 1/30/2012 12:54 AM, Kevin Pouget wrote:

> I would be curious about why it would be "a disastrous mistake".

If the breakpoint instruction took more than one insturction, what would
happenb to a jump that jumnped to the second instruction?
>
> Thnaks,
>
> Kevin

^ permalink raw reply	[flat|nested] 7+ messages in thread

* RE: software breakpoint in gdb
  2012-01-30  6:17       ` Yao Qi
@ 2012-01-30 15:51         ` Marc Khouzam
  0 siblings, 0 replies; 7+ messages in thread
From: Marc Khouzam @ 2012-01-30 15:51 UTC (permalink / raw)
  To: 'Yao Qi', 'Kevin Pouget'
  Cc: 'Robert Dewar', 'Jan Kratochvil',
	'Xin Tong', 'gdb@sourceware.org'

> -----Original Message-----
> From: gdb-owner@sourceware.org 
> [mailto:gdb-owner@sourceware.org] On Behalf Of Yao Qi
> Sent: Monday, January 30, 2012 1:17 AM
> To: Kevin Pouget
> Cc: Robert Dewar; Jan Kratochvil; Xin Tong; gdb@sourceware.org
> Subject: Re: software breakpoint in gdb
> 
> On 01/30/2012 01:54 PM, Kevin Pouget wrote:
> > I would be curious about why it would be "a disastrous mistake".
> > Please correct me if I'm wrong in my understanding:
> 
> The real disaster is interrupt instruction may corrupt the following
> instruction of breakpointed instruction, if interrupt instruction is
> longer than breakpointed instruction.  For example,
> 
>    JMP L1:
>    ...
>    INSN1 (1-byte)
> L1:INSN2 (2-byte)
> 
> Supposing the length of interrupt instruction is 2-byte, when setting
> breakpoint on INSN1, the INSN2 is corrupted.  If the program runs JMP
> instruction to L1 directly, something wrong will be executed.

In fact, I believe fast tracepoints in GDB had to deal with this very
limitation. Fast tracepoints don't use an interrupt instruction (too slow)
but instead use a jump instruction; that jump takes the execution to another
location to execute the tracepoint directly in the program.  Because we 
cannot replace more than one instruction, fast tracepoints are only allowed
on 5-byte instructions (actually, 4-byte instructions starting with GDB 7.4).

Marc

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2012-01-30 15:51 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-01-29 16:48 software breakpoint in gdb Xin Tong
2012-01-29 16:53 ` Jan Kratochvil
2012-01-30  0:54   ` Robert Dewar
2012-01-30  5:54     ` Kevin Pouget
2012-01-30  6:17       ` Yao Qi
2012-01-30 15:51         ` Marc Khouzam
2012-01-30 13:19       ` Robert Dewar

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).