public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Pierre Langlois <pierre.langlois@arm.com>
To: gdb-patches@sourceware.org
Cc: Pierre Langlois <pierre.langlois@arm.com>
Subject: [PATCH 7/8] [testsuite] Add a gdb.trace test for instruction relocation
Date: Mon, 14 Sep 2015 11:32:00 -0000	[thread overview]
Message-ID: <1442230282-20751-8-git-send-email-pierre.langlois@arm.com> (raw)
In-Reply-To: <1442230282-20751-1-git-send-email-pierre.langlois@arm.com>

This test case makes sure that relocating PC relative instructions does
not change their behaviors.  All PC relative AArch64 instructions are
covered.  While call and jump (32 bit relative) instructions are covered
on x86.

The test case creates a static array of function pointers for each
supported architecture.  Each function in this array tests a specific
instruction using inline assembly.  They all need to contain a symbol in
the form of 'set_point\[0-9\]+' and finish by either calling pass or
fail.  The number of 'set_pointN' needs to go from 0 to
(ARRAY_SIZE - 1).

The test will:
- look up the number of function pointers in the static array.
- set fast tracepoints on each 'set_point\[0-9\]+' symbol, one in each
  functions from 0 to (ARRAY_SIZE - 1).
- run the trace experiment and make sure the pass function is called for
  every function.

gdb/testsuite/ChangeLog:

	* gdb.trace/ftrace-insn-reloc.c: New file.
	* gdb.trace/ftrace-insn-reloc.exp: New file.
---
 gdb/testsuite/gdb.trace/ftrace-insn-reloc.c   | 508 ++++++++++++++++++++++++++
 gdb/testsuite/gdb.trace/ftrace-insn-reloc.exp | 114 ++++++
 2 files changed, 622 insertions(+)
 create mode 100644 gdb/testsuite/gdb.trace/ftrace-insn-reloc.c
 create mode 100644 gdb/testsuite/gdb.trace/ftrace-insn-reloc.exp

diff --git a/gdb/testsuite/gdb.trace/ftrace-insn-reloc.c b/gdb/testsuite/gdb.trace/ftrace-insn-reloc.c
new file mode 100644
index 0000000..c7148a2
--- /dev/null
+++ b/gdb/testsuite/gdb.trace/ftrace-insn-reloc.c
@@ -0,0 +1,508 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2015 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#include <stddef.h>
+#include <stdint.h>
+
+typedef void (*testcase_ftype)(void);
+
+/* Each function checks the correctness of the instruction being
+   relocated due to a fast tracepoint.  Call function pass if it is
+   correct, otherwise call function fail.  GDB sets a breakpoints on
+   pass and fail in order to check the correctness.  */
+
+static void
+pass (void)
+{
+}
+
+static void
+fail (void)
+{
+}
+
+#if (defined __x86_64__ || defined __i386__)
+
+#ifdef SYMBOL_PREFIX
+#define SYMBOL(str)     SYMBOL_PREFIX #str
+#else
+#define SYMBOL(str)     #str
+#endif
+
+/* Make sure we can relocate a CALL instruction.  CALL instructions are
+   5 bytes long so we can always set a fast tracepoints on them.
+
+     JMP set_point0
+   f:
+     MOV $1, %[ok]
+     JMP end
+   set_point0:
+     CALL f ; tracepoint here.
+   end:
+
+   */
+
+static void
+can_relocate_call (void)
+{
+  int ok = 0;
+
+  asm ("    .global " SYMBOL (set_point0) "\n"
+       "  jmp " SYMBOL (set_point0) "\n"
+       "0:\n"
+       "  mov $1, %[ok]\n"
+       "  jmp 1f\n"
+       SYMBOL (set_point0) ":\n"
+       "  call 0b\n"
+       "1:\n"
+       : [ok] "=r" (ok));
+
+  if (ok == 1)
+    pass ();
+  else
+    fail ();
+}
+
+/* Make sure we can relocate a JMP instruction.  We need the JMP
+   instruction to be 5 bytes long in order to set a fast tracepoint on
+   it.  To do this, we emit the opcode directly.
+
+     JMP next ; tracepoint here.
+   next:
+     MOV $1, %[ok]
+
+   */
+
+static void
+can_relocate_jump (void)
+{
+  int ok = 0;
+
+  asm ("    .global " SYMBOL (set_point1) "\n"
+       SYMBOL (set_point1) ":\n"
+       ".byte 0xe9\n"  /* jmp  */
+       ".byte 0x00\n"
+       ".byte 0x00\n"
+       ".byte 0x00\n"
+       ".byte 0x00\n"
+       "  mov $1, %[ok]\n"
+       : [ok] "=r" (ok));
+
+  if (ok == 1)
+    pass ();
+  else
+    fail ();
+}
+#elif (defined __aarch64__)
+
+/* Make sure we can relocate a B instruction.
+
+     B set_point0
+   set_ok:
+     MOV %[ok], #1
+     B end
+   set_point0:
+     B set_ok ; tracepoint here.
+     MOV %[ok], #0
+   end
+
+   */
+
+static void
+can_relocate_b (void)
+{
+  int ok = 0;
+
+  asm ("  b set_point0\n"
+       "0:\n"
+       "  mov %[ok], #1\n"
+       "  b 1f\n"
+       "set_point0:\n"
+       "  b 0b\n"
+       "  mov %[ok], #0\n"
+       "1:\n"
+       : [ok] "=r" (ok));
+
+  if (ok == 1)
+    pass ();
+  else
+    fail ();
+}
+
+/* Make sure we can relocate a B.cond instruction.
+
+     MOV x0, #8
+     TST x0, #8 ; Clear the Z flag.
+     B set_point1
+   set_ok:
+     MOV %[ok], #1
+     B end
+   set_point1:
+     B.NE set_ok ; tracepoint here.
+     MOV %[ok], #0
+   end
+
+   */
+
+static void
+can_relocate_bcond (void)
+{
+  int ok = 0;
+
+  asm ("  mov x0, #8\n"
+       "  tst x0, #8\n"
+       "  b set_point1\n"
+       "0:\n"
+       "  mov %[ok], #1\n"
+       "  b 1f\n"
+       "set_point1:\n"
+       "  b.ne 0b\n"
+       "  mov %[ok], #0\n"
+       "1:\n"
+       : [ok] "=r" (ok)
+       :
+       : "0", "cc");
+
+  if (ok == 1)
+    pass ();
+  else
+    fail ();
+}
+
+/* Make sure we can relocate a CBZ instruction.
+
+     MOV x0, #0
+     B set_point2
+   set_ok:
+     MOV %[ok], #1
+     B end
+   set_point2:
+     CBZ x0, set_ok ; tracepoint here.
+     MOV %[ok], #0
+   end
+
+   */
+
+static void
+can_relocate_cbz (void)
+{
+  int ok = 0;
+
+  asm ("  mov x0, #0\n"
+       "  b set_point2\n"
+       "0:\n"
+       "  mov %[ok], #1\n"
+       "  b 1f\n"
+       "set_point2:\n"
+       "  cbz x0, 0b\n"
+       "  mov %[ok], #0\n"
+       "1:\n"
+       : [ok] "=r" (ok)
+       :
+       : "0");
+
+  if (ok == 1)
+    pass ();
+  else
+    fail ();
+}
+
+/* Make sure we can relocate a CBNZ instruction.
+
+     MOV x0, #8
+     B set_point3
+   set_ok:
+     MOV %[ok], #1
+     B end
+   set_point3:
+     CBNZ x0, set_ok ; tracepoint here.
+     MOV %[ok], #0
+   end
+
+   */
+
+static void
+can_relocate_cbnz (void)
+{
+  int ok = 0;
+
+  asm ("  mov x0, #8\n"
+       "  b set_point3\n"
+       "0:\n"
+       "  mov %[ok], #1\n"
+       "  b 1f\n"
+       "set_point3:\n"
+       "  cbnz x0, 0b\n"
+       "  mov %[ok], #0\n"
+       "1:\n"
+       : [ok] "=r" (ok)
+       :
+       : "0");
+
+  if (ok == 1)
+    pass ();
+  else
+    fail ();
+}
+
+/* Make sure we can relocate a TBZ instruction.
+
+     MOV x0, #8
+     MVN x0, x0 ; Clear bit 3.
+     B set_point4
+   set_ok:
+     MOV %[ok], #1
+     B end
+   set_point4:
+     TBZ x0, #3, set_ok ; tracepoint here.
+     MOV %[ok], #0
+   end
+
+   */
+
+static void
+can_relocate_tbz (void)
+{
+  int ok = 0;
+
+  asm ("  mov x0, #8\n"
+       "  mvn x0, x0\n"
+       "  b set_point4\n"
+       "0:\n"
+       "  mov %[ok], #1\n"
+       "  b 1f\n"
+       "set_point4:\n"
+       "  tbz x0, #3, 0b\n"
+       "  mov %[ok], #0\n"
+       "1:\n"
+       : [ok] "=r" (ok)
+       :
+       : "0");
+
+  if (ok == 1)
+    pass ();
+  else
+    fail ();
+}
+
+/* Make sure we can relocate a TBNZ instruction.
+
+     MOV x0, #8 ; Set bit 3.
+     B set_point5
+   set_ok:
+     MOV %[ok], #1
+     B end
+   set_point5:
+     TBNZ x0, #3, set_ok ; tracepoint here.
+     MOV %[ok], #0
+   end
+
+   */
+
+static void
+can_relocate_tbnz (void)
+{
+  int ok = 0;
+
+  asm ("  mov x0, #8\n"
+       "  b set_point5\n"
+       "0:\n"
+       "  mov %[ok], #1\n"
+       "  b 1f\n"
+       "set_point5:\n"
+       "  tbnz x0, #3, 0b\n"
+       "  mov %[ok], #0\n"
+       "1:\n"
+       : [ok] "=r" (ok)
+       :
+       : "0");
+
+  if (ok == 1)
+    pass ();
+  else
+    fail ();
+}
+
+/* Make sure we can relocate an ADR instruction with a positive offset.
+
+   set_point6:
+     ADR x0, target ; tracepoint here.
+     BR x0 ; jump to target
+     MOV %[ok], #0
+     B end
+   target:
+     MOV %[ok], #1
+   end
+
+   */
+
+static void
+can_relocate_adr_forward (void)
+{
+  int ok = 0;
+
+  asm ("set_point6:\n"
+       "  adr x0, 0f\n"
+       "  br x0\n"
+       "  mov %[ok], #0\n"
+       "  b 1f\n"
+       "0:\n"
+       "  mov %[ok], #1\n"
+       "1:\n"
+       : [ok] "=r" (ok)
+       :
+       : "0");
+
+  if (ok == 1)
+    pass ();
+  else
+    fail ();
+}
+
+/* Make sure we can relocate an ADR instruction with a negative offset.
+
+     B set_point7
+   target:
+     MOV %[ok], #1
+     B end
+   set_point7:
+     ADR x0, target ; tracepoint here.
+     BR x0 ; jump to target
+     MOV %[ok], #0
+   end
+
+   */
+
+static void
+can_relocate_adr_backward (void)
+{
+  int ok = 0;
+
+  asm ("b set_point7\n"
+       "0:\n"
+       "  mov %[ok], #1\n"
+       "  b 1f\n"
+       "set_point7:\n"
+       "  adr x0, 0b\n"
+       "  br x0\n"
+       "  mov %[ok], #0\n"
+       "1:\n"
+       : [ok] "=r" (ok)
+       :
+       : "0");
+
+  if (ok == 1)
+    pass ();
+  else
+    fail ();
+}
+
+/* Make sure we can relocate an ADRP instruction.
+
+   set_point8:
+     ADRP %[addr], set_point8 ; tracepoint here.
+     ADR %[pc], set_point8
+
+   ADR computes the address of the given label.  While ADRP gives us its
+   page, on a 4K boundary.  We can check ADRP executed normally by
+   making sure the result of ADR and ADRP are equivalent, except for the
+   12 lowest bits which should be cleared.
+
+   */
+
+static void
+can_relocate_adrp (void)
+{
+  uintptr_t page;
+  uintptr_t pc;
+
+  asm ("set_point8:\n"
+       "  adrp %[page], set_point8\n"
+       "  adr %[pc], set_point8\n"
+       : [page] "=r" (page), [pc] "=r" (pc));
+
+  if (page == (pc & ~0xfff))
+    pass ();
+  else
+    fail ();
+}
+
+/* Make sure we can relocate an LDR instruction, where the memory to
+   read is an offset from the current PC.
+
+     B set_point9
+   data:
+     .word 0x0cabba9e
+   set_point9:
+     LDR %[result], data ; tracepoint here.
+
+   */
+
+static void
+can_relocate_ldr (void)
+{
+  uint32_t result = 0;
+
+  asm ("b set_point9\n"
+       "0:\n"
+       "  .word 0x0cabba9e\n"
+       "set_point9:\n"
+       "  ldr %w[result], 0b\n"
+       : [result] "=r" (result));
+
+  if (result == 0x0cabba9e)
+    pass ();
+  else
+    fail ();
+}
+#endif
+
+/* Functions testing relocations need to be placed here.  GDB will read
+   n_testcases to know how many fast tracepoints to place.  It will look
+   for symbols in the form of 'set_point\[0-9\]+' so each functions
+   needs one, starting at 0.  */
+
+static testcase_ftype testcases[] = {
+#if (defined __x86_64__ || defined __i386__)
+  can_relocate_call,
+  can_relocate_jump
+#elif (defined __aarch64__)
+  can_relocate_b,
+  can_relocate_bcond,
+  can_relocate_cbz,
+  can_relocate_cbnz,
+  can_relocate_tbz,
+  can_relocate_tbnz,
+  can_relocate_adr_forward,
+  can_relocate_adr_backward,
+  can_relocate_adrp,
+  can_relocate_ldr
+#endif
+};
+
+static size_t n_testcases = (sizeof (testcases) / sizeof (testcase_ftype));
+
+int
+main ()
+{
+  int i = 0;
+
+  for (i = 0; i < n_testcases; i++)
+    testcases[i] ();
+
+  return 0;
+}
diff --git a/gdb/testsuite/gdb.trace/ftrace-insn-reloc.exp b/gdb/testsuite/gdb.trace/ftrace-insn-reloc.exp
new file mode 100644
index 0000000..dd33190
--- /dev/null
+++ b/gdb/testsuite/gdb.trace/ftrace-insn-reloc.exp
@@ -0,0 +1,114 @@
+# Copyright 2015 Free Software Foundation, Inc.
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+load_lib "trace-support.exp"
+
+standard_testfile
+set executable $testfile
+set expfile $testfile.exp
+
+# Some targets have leading underscores on assembly symbols.
+set additional_flags [gdb_target_symbol_prefix_flags]
+
+if [prepare_for_testing $expfile $executable $srcfile \
+	[list debug $additional_flags]] {
+    untested "failed to prepare for trace tests"
+    return -1
+}
+
+if ![runto_main] {
+    fail "Can't run to main to check for trace support"
+    return -1
+}
+
+if ![gdb_target_supports_trace] {
+    unsupported "target does not support trace"
+    return -1
+}
+
+set libipa [get_in_proc_agent]
+gdb_load_shlibs $libipa
+
+# Can't use prepare_for_testing, because that splits compiling into
+# building objects and then linking, and we'd fail with "linker input
+# file unused because linking not done" when building the object.
+
+if { [gdb_compile "$srcdir/$subdir/$srcfile" $binfile \
+	  executable [list debug $additional_flags shlib=$libipa] ] != "" } {
+    untested "failed to compile ftrace tests"
+    return -1
+}
+clean_restart ${executable}
+
+if ![runto_main] {
+    fail "Can't run to main for ftrace tests"
+    return 0
+}
+
+gdb_reinitialize_dir $srcdir/$subdir
+
+if { [gdb_test "info sharedlibrary" ".*${libipa}.*" "IPA loaded"] != 0 } {
+    untested "Could not find IPA lib loaded"
+    return 1
+}
+
+# Read function name from testcases[N].
+
+proc read_testcase { n } {
+    global gdb_prompt
+
+    set result -1
+    gdb_test_multiple "print testcases\[${n}\]" "read name of test case ${n}" {
+	-re "\[$\].*= .*<(.*)>.*$gdb_prompt $" {
+	    set result $expect_out(1,string)
+	}
+	-re "$gdb_prompt $" { }
+    }
+
+    return $result
+}
+
+set n_testcases [gdb_readexpr "n_testcases"]
+
+if { ${n_testcases} == 0 } {
+    untested "No instruction relocation to test"
+    return 1
+}
+
+# Set a fast tracepoint on each set_point${i} symbol.  There is one for
+# each testcase.
+for { set i 0 } { ${i} < ${n_testcases} } { incr i } {
+    set testcase [read_testcase $i]
+
+    gdb_test "ftrace *set_point$i" "Fast tracepoint .*" \
+	"fast tracepoint on ${testcase}"
+}
+
+gdb_test "break pass" ".*" ""
+gdb_test "break fail" ".*" ""
+
+gdb_test_no_output "tstart" "start trace experiment"
+
+# Make sure we have hit the pass breakpoint for each testcase.
+for { set i 0 } { ${i} < ${n_testcases} } { incr i } {
+    set testcase [read_testcase $i]
+
+    gdb_test "continue" \
+	".*Breakpoint \[0-9\]+, pass \(\).*" \
+	"relocated instruction at ${testcase}"
+}
+
+gdb_test "tstatus" ".*Collected ${n_testcases} trace frames.*" "check on trace status"
+
+gdb_test "tstop" "" ""
-- 
2.4.6

  parent reply	other threads:[~2015-09-14 11:32 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-09-14 11:31 [PATCH 0/8] [AArch64] Support fast tracepoints Pierre Langlois
2015-09-14 11:31 ` [PATCH 3/8] [AArch64] Make aarch64_decode_adrp handle both ADR and ADRP instructions Pierre Langlois
2015-09-15 10:12   ` Yao Qi
2015-09-15 12:05     ` [PATCH 3/8 v2] " Pierre Langlois
2015-09-14 11:31 ` [PATCH 1/8] [AArch64] Use debug_printf instead of fprintf_unfiltered Pierre Langlois
2015-09-15 10:06   ` Yao Qi
2015-09-15 11:07     ` Pierre Langlois
2015-09-15 11:19       ` Yao Qi
2015-09-15 11:43         ` [PATCH 2/8 v2] " Pierre Langlois
2015-09-15 17:40           ` Pierre Langlois
2015-09-14 11:32 ` [PATCH 5/8] [GDBserver][AArch64] Implement target_emit_ops Pierre Langlois
2015-09-14 11:32 ` Pierre Langlois [this message]
2015-09-15 11:27   ` [PATCH 7/8] [testsuite] Add a gdb.trace test for instruction relocation Yao Qi
2015-09-14 11:32 ` [PATCH 6/8] [testsuite][AArch64] Enable fast tracepoint tests Pierre Langlois
2015-09-15 10:18   ` Yao Qi
2015-09-14 11:33 ` [PATCH 4/8] [GDBserver][AArch64] Add support for fast tracepoints Pierre Langlois
2015-09-15 13:01   ` Yao Qi
2015-09-14 11:38 ` [PATCH 8/8] [testsuite] Add a test case for fast tracepoints' locking mechanism Pierre Langlois
2015-09-14 11:38 ` [PATCH 2/8] [AArch64] Move instruction decoding into new arch/ directory Pierre Langlois
2015-09-15 10:10   ` Yao Qi
2015-09-15 12:02     ` [PATCH 2/8 v2] " Pierre Langlois
2015-09-14 16:45 ` [PATCH] Add NEWS entry for fast tracepoint support on aarch64-linux Pierre Langlois
2015-09-18 12:43 ` [PATCH 0/8 V2] [AArch64] Support fast tracepoints Yao Qi
2015-09-18 12:43   ` [PATCH 1/8] Move instruction decoding into new arch/ directory Yao Qi
2015-09-18 12:43   ` [PATCH 2/8] Make aarch64_decode_adrp handle both ADR and ADRP instructions Yao Qi
2015-09-18 12:43   ` [PATCH 7/8] Add a test case for fast tracepoints' locking mechanism Yao Qi
2015-09-18 12:43   ` [PATCH 4/8] Implement target_emit_ops Yao Qi
2015-09-18 12:57     ` Pierre Langlois
2016-02-05 20:09     ` Antoine Tremblay
2016-02-08 17:30       ` Yao Qi
2016-02-08 17:59         ` Antoine Tremblay
2015-09-18 12:43   ` [PATCH 8/8] Add NEWS entry for fast tracepoint support on aarch64-linux Yao Qi
2015-09-18 14:07     ` Eli Zaretskii
2015-09-18 12:43   ` [PATCH 5/8] Enable fast tracepoint tests Yao Qi
2015-09-18 12:44   ` [PATCH 6/8] Add a gdb.trace test for instruction relocation Yao Qi
2015-09-18 12:44   ` [PATCH 3/8] Add support for fast tracepoints Yao Qi
2015-09-21 14:06   ` [PATCH 0/8 V2] [AArch64] Support " Yao Qi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1442230282-20751-8-git-send-email-pierre.langlois@arm.com \
    --to=pierre.langlois@arm.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).