[patchv2] Write bpt at the ON_STACK bpt address

Jan Kratochvil
2012-07-26T21:24:36Z

On Thu, 26 Jul 2012 00:39:33 +0200, Joel Brobecker wrote:
> > A valid instruction is not enough. We need a valid instruction
> > that will cause Valgrind to terminate block translation.
> > The breakpoint trap instruction is ok for that.
> > (0xcc for x86 and amd64, 0x0005000d for mips32).
>
> I think it is fine to update GDB to insert the breakpoint instruction
> instead of leaving random bytes at the breakpoint location.

Attached. I no longer find it useful as arch-dependent code, it would do the
same in each arch.

No regressions on {x86_64,x86_64-m32,i686}-fedorarawhide-linux-gnu.

Philippe, do you have an easy enough way to regression test it on mips when
you was asking for the mips fix? mips has many execution modes as I see.

I still have to write a testcase for it using valgrind.

> But it sounds like this is forcing GDB to have insider knowledge of
> valgrind.

As you were asking to put there 'int3' (and not 'hlt') the user convenience
coincidentally matches here with what Philippe asks for valgrind.

I am only concerned a bit about this change arcross all archs for 7.5.
Maybe 7.5 could limit this patch only for i386/x86_64 which is well
understood.

Thanks,
Jan

gdb/
2012-07-26 Jan Kratochvil <jan.kratochvil@redhat.com>

* infcall.c (call_function_by_hand): Move BP_ADDR comment to
AT_ENTRY_POINT.
(call_function_by_hand) <ON_STACK>: Call write_memory with
gdbarch_breakpoint_from_pc, if possible.
(call_function_by_hand) <AT_ENTRY_POINT>: The BP_ADDR comment is moved
here.

gdb/doc/
2012-07-26 Jan Kratochvil <jan.kratochvil@redhat.com>

* gdbint.texinfo (Defining Other Architecture Features): Clarify *pcptr
encoding for gdbarch_breakpoint_from_pc, bp_addr for
gdbarch_push_dummy_call and bp_addr for gdbarch_push_dummy_code.

diff --git a/gdb/doc/gdbint.texinfo b/gdb/doc/gdbint.texinfo
index 5e00f1f..b66f80b 100644
--- a/gdb/doc/gdbint.texinfo
+++ b/gdb/doc/gdbint.texinfo
@@ -4540,8 +4540,10 @@ contents and size of a breakpoint instruction. It returns a pointer to
a static string of bytes that encode a breakpoint instruction, stores the
length of the string to @code{*@var{lenptr}}, and adjusts the program
counter (if necessary) to point to the actual memory location where the
-breakpoint should be inserted. May return @code{NULL} to indicate that
-software breakpoints are not supported.
+breakpoint should be inserted. The program counter (@code{*@var{pcptr}}
+is inferior PC register encoded on the input and it is a plain address on the
+output. Function may return @code{NULL} to indicate that software breakpoints
+are not supported.

Although it is common to use a trap instruction for a breakpoint, it's
not required; for instance, the bit pattern could be an invalid
@@ -4821,7 +4823,7 @@ instead of value.
@anchor{gdbarch_push_dummy_call} Define this to push the dummy frame's call to
the inferior function onto the stack. In addition to pushing @var{nargs}, the
code should push @var{struct_addr} (when @var{struct_return} is non-zero), and
-the return address (@var{bp_addr}).
+the return address (@var{bp_addr}, in inferior PC register encoding).

@var{function} is a pointer to a @code{struct value}; on architectures that use
function descriptors, this contains the function descriptor value.
@@ -4835,12 +4837,14 @@ instruction sequence (including space for a breakpoint) to which the
called function should return.

Set @var{bp_addr} to the address at which the breakpoint instruction
-should be inserted, @var{real_pc} to the resume address when starting
-the call sequence, and return the updated inner-most stack address.
+should be inserted (in inferior PC register encoding), @var{real_pc} to the
+resume address when starting the call sequence, and return the updated
+inner-most stack address.

By default, the stack is grown sufficient to hold a frame-aligned
(@pxref{frame_align}) breakpoint, @var{bp_addr} is set to the address
-reserved for that breakpoint, and @var{real_pc} set to @var{funaddr}.
+reserved for that breakpoint (in inferior PC register encoding), and
+@var{real_pc} set to @var{funaddr}.

This method replaces @w{@code{gdbarch_call_dummy_location (@var{gdbarch})}}.

diff --git a/gdb/infcall.c b/gdb/infcall.c
index 51cd118..6ac6624 100644
--- a/gdb/infcall.c
+++ b/gdb/infcall.c
@@ -618,15 +618,37 @@ call_function_by_hand (struct value *function, int nargs, struct value **args)
not just the breakpoint but also an extra word containing the
size (?) of the structure being passed. */

- /* The actual breakpoint (at BP_ADDR) is inserted separatly so there
- is no need to write that out. */
-
switch (gdbarch_call_dummy_location (gdbarch))
{
case ON_STACK:
- sp = push_dummy_code (gdbarch, sp, funaddr,
- args, nargs, target_values_type,
- &real_pc, &bp_addr, get_current_regcache ());
+ {
+ const gdb_byte *bp_bytes;
+ CORE_ADDR bp_addr_as_address;
+ int bp_size;
+
+ /* Be careful BP_ADDR is in inferior PC encoding while
+ BP_ADDR_AS_ADDRESS is a plain memory address. */
+
+ sp = push_dummy_code (gdbarch, sp, funaddr, args, nargs,
+ target_values_type, &real_pc, &bp_addr,
+ get_current_regcache ());
+
+ /* Write a legitimate instruction at the point where the infcall
+ breakpoint is going to be inserted. While this instruction
+ is never going to be executed, a user investigating the
+ memory from GDB would see this instruction instead of random
+ uninitialized bytes. We chose the breakpoint instruction
+ just because it may look as the most logical one to the user.
+
+ If software breakpoints are unsupported for this target we
+ leave the user visible memory content uninitialized. */
+
+ bp_addr_as_address = bp_addr;
+ bp_bytes = gdbarch_breakpoint_from_pc (gdbarch, &bp_addr_as_address,
+ &bp_size);
+ if (bp_bytes != NULL)
+ write_memory (bp_addr_as_address, bp_bytes, bp_size);
+ }
break;
case AT_ENTRY_POINT:
{
@@ -634,8 +656,12 @@ call_function_by_hand (struct value *function, int nargs, struct value **args)

real_pc = funaddr;
dummy_addr = entry_point_address ();
+
/* A call dummy always consists of just a single breakpoint, so
- its address is the same as the address of the dummy. */
+ its address is the same as the address of the dummy.
+
+ The actual breakpoint is inserted separatly so there is no need to
+ write that out. */
bp_addr = dummy_addr;
break;
}