public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Luis Machado <luis.machado@linaro.org>
To: gdb-patches@sourceware.org
Subject: [PATCH v6 22/25] Extend "x" and "print" commands to support memory tagging
Date: Mon, 22 Mar 2021 10:21:16 -0300	[thread overview]
Message-ID: <20210322132120.1202230-23-luis.machado@linaro.org> (raw)
In-Reply-To: <20210322132120.1202230-1-luis.machado@linaro.org>

Updates on v6:

- Update uses of memtag_type.
- Update uses of gdbarch_memtag_to_string.
- Update command documentation.
- Record previously-used tag-printing format option so repetition works for
  the x command.

Updates on v5:

- Add -memory-tag-violations print option to control whether GDB should print
a warning when a logical tag from a pointer doesn't match the allocation tag
contained in memory.
- Remove checks for a removed memory_tagging global variable.
- Update tests listing print options.

Updates on v4:

- Updated function names.
- Removed useless include of infrun.h

--

Extend the "x" and "print" commands to make use of memory tagging
functionality, if supported by the architecture.

The "print" command will point out any possible tag mismatches it finds
when dealing with pointers, in case such a pointer is tagged.  No additional
modifiers are needed.

Suppose we have a pointer "p" with value 0x1234 (logical tag 0x0) and that we
have an allocation tag of 0x1 for that particular area of memory. This is the
expected output:

(gdb) p/x p
Logical tag (0x0) does not match the allocation tag (0x1).
$1 = 0x1234

The "x" command has a new 'm' modifier that will enable displaying of
allocation tags alongside the data dump.  It will display one allocation
tag per line.

AArch64 has a tag granule of 16 bytes, which means we can have one tag for
every 16 bytes of memory. In this case, this is what the "x" command will
display with the new 'm' modifier:

(gdb) x/32bxm p
<Allocation Tag 0x1 for range [0x1230,0x1240)>
0x1234:	0x01	0x02	0x00	0x00	0x00	0x00	0x00	0x00
0x123c:	0x00	0x00	0x00	0x00	0x00	0x00	0x00	0x00
<Allocation Tag 0x1 for range [0x1240,0x1250)>
0x1244:	0x00	0x00	0x00	0x00	0x00	0x00	0x00	0x00
0x124c:	0x00	0x00	0x00	0x00	0x00	0x00	0x00	0x00

(gdb) x/4gxm a
<Allocation Tag 0x1 for range [0x1230,0x1240)>
0x1234:	0x0000000000000201	0x0000000000000000
<Allocation Tag 0x1 for range [0x1240,0x1250)>
0x1244:	0x0000000000000000	0x0000000000000000

gdb/ChangeLog:

YYYY-MM-DD  Luis Machado  <luis.machado@linaro.org>

	* printcmd.c (decode_format): Handle the 'm' modifier.
	(do_examine): Display allocation tags when required/supported.
	(should_validate_memtags): New function.
	(print_command_1): Display memory tag mismatches.
	* valprint.c (show_memory_tag_violations): New function.
	(value_print_option_defs): Add new option "memory-tag-violations".
	(user_print_options) <memory_tag_violations>: Initialize to 1.
	* valprint.h (struct format_data) <print_tags>: New field.
	(value_print_options) <memory_tag_violations>: New field.

gdb/testsuite/ChangeLog:

YYYY-MM-DD  Luis Machado  <luis.machado@linaro.org>

	* gdb.base/options.exp: Adjust for new print options.
	* gdb.base/with.exp: Likewise.
---
 gdb/printcmd.c                     | 102 ++++++++++++++++++++++++++++-
 gdb/testsuite/gdb.base/options.exp |   1 +
 gdb/testsuite/gdb.base/with.exp    |   2 +-
 gdb/valprint.c                     |  23 +++++++
 gdb/valprint.h                     |   4 ++
 5 files changed, 130 insertions(+), 2 deletions(-)

diff --git a/gdb/printcmd.c b/gdb/printcmd.c
index 9200e66db39..c82f709136f 100644
--- a/gdb/printcmd.c
+++ b/gdb/printcmd.c
@@ -72,6 +72,10 @@ static char last_size = 'w';
 
 static int last_count;
 
+/* Last specified tag-printing option.  */
+
+static bool last_print_tags = false;
+
 /* Default address to examine next, and associated architecture.  */
 
 static struct gdbarch *next_gdbarch;
@@ -193,6 +197,7 @@ decode_format (const char **string_ptr, int oformat, int osize)
   val.size = '?';
   val.count = 1;
   val.raw = 0;
+  val.print_tags = false;
 
   if (*p == '-')
     {
@@ -215,6 +220,11 @@ decode_format (const char **string_ptr, int oformat, int osize)
 	  val.raw = 1;
 	  p++;
 	}
+      else if (*p == 'm')
+	{
+	  val.print_tags = true;
+	  p++;
+	}
       else if (*p >= 'a' && *p <= 'z')
 	val.format = *p++;
       else
@@ -1100,12 +1110,50 @@ do_examine (struct format_data fmt, struct gdbarch *gdbarch, CORE_ADDR addr)
       need_to_update_next_address = 1;
     }
 
+  /* Whether we need to print the memory tag information for the current
+     address range.  */
+  bool print_range_tag = true;
+  uint32_t gsize = gdbarch_memtag_granule_size (gdbarch);
+
   /* Print as many objects as specified in COUNT, at most maxelts per line,
      with the address of the next one at the start of each line.  */
 
   while (count > 0)
     {
       QUIT;
+
+      CORE_ADDR tag_laddr = 0, tag_haddr = 0;
+
+      /* Print the memory tag information if requested.  */
+      if (fmt.print_tags && print_range_tag
+	  && target_supports_memory_tagging ())
+	{
+	  tag_laddr = align_down (next_address, gsize);
+	  tag_haddr = align_down (next_address + gsize, gsize);
+
+	  struct value *v_addr
+	    = value_from_ulongest (builtin_type (gdbarch)->builtin_data_ptr,
+				   tag_laddr);
+
+	  if (gdbarch_tagged_address_p (target_gdbarch (), v_addr))
+	    {
+	      /* Fetch the allocation tag.  */
+	      struct value *tag
+		= gdbarch_get_memtag (gdbarch, v_addr, memtag_type::allocation);
+	      std::string atag
+		= gdbarch_memtag_to_string (gdbarch, tag);
+
+	      if (!atag.empty ())
+		{
+		  printf_filtered (_("<Allocation Tag %s for range [%s,%s)>\n"),
+				   atag.c_str (),
+				   paddress (gdbarch, tag_laddr),
+				   paddress (gdbarch, tag_haddr));
+		}
+	    }
+	  print_range_tag = false;
+	}
+
       if (format == 'i')
 	fputs_filtered (pc_prefix (next_address), gdb_stdout);
       print_address (next_gdbarch, next_address, gdb_stdout);
@@ -1136,6 +1184,11 @@ do_examine (struct format_data fmt, struct gdbarch *gdbarch, CORE_ADDR addr)
 	  /* Display any branch delay slots following the final insn.  */
 	  if (format == 'i' && count == 1)
 	    count += branch_delay_insns;
+
+	  /* Update the tag range based on the current address being
+	     processed.  */
+	  if (tag_haddr <= next_address)
+	      print_range_tag = true;
 	}
       printf_filtered ("\n");
     }
@@ -1208,6 +1261,26 @@ print_value (value *val, const value_print_options &opts)
   annotate_value_history_end ();
 }
 
+/* Returns true if memory tags should be validated.  False otherwise.  */
+
+static bool
+should_validate_memtags (struct value *value)
+{
+  if (target_supports_memory_tagging ()
+      && gdbarch_tagged_address_p (target_gdbarch (), value))
+    {
+      gdb_assert (value != nullptr && value_type (value) != nullptr);
+
+      enum type_code code = value_type (value)->code ();
+
+      return (code == TYPE_CODE_PTR
+	      || code == TYPE_CODE_REF
+	      || code == TYPE_CODE_METHODPTR
+	      || code == TYPE_CODE_MEMBERPTR);
+    }
+  return false;
+}
+
 /* Helper for parsing arguments for print_command_1.  */
 
 static struct value *
@@ -1246,7 +1319,30 @@ print_command_1 (const char *args, int voidprint)
 
   if (voidprint || (val && value_type (val) &&
 		    value_type (val)->code () != TYPE_CODE_VOID))
-    print_value (val, print_opts);
+    {
+      /* If memory tagging validation is on, check if the tag is valid.  */
+      if (print_opts.memory_tag_violations && should_validate_memtags (val)
+	  && !gdbarch_memtag_matches_p (target_gdbarch (), val))
+	{
+	  /* Fetch the logical tag.  */
+	  struct value *tag
+	    = gdbarch_get_memtag (target_gdbarch (), val,
+				  memtag_type::logical);
+	  std::string ltag
+	    = gdbarch_memtag_to_string (target_gdbarch (), tag);
+
+	  /* Fetch the allocation tag.  */
+	  tag = gdbarch_get_memtag (target_gdbarch (), val,
+				    memtag_type::allocation);
+	  std::string atag
+	    = gdbarch_memtag_to_string (target_gdbarch (), tag);
+
+	  printf_filtered (_("Logical tag (%s) does not match the "
+			     "allocation tag (%s).\n"),
+			   ltag.c_str (), atag.c_str ());
+	}
+      print_value (val, print_opts);
+    }
 }
 
 /* Called from command completion function to skip over /FMT
@@ -1741,6 +1837,7 @@ x_command (const char *exp, int from_tty)
   struct value *val;
 
   fmt.format = last_format ? last_format : 'x';
+  fmt.print_tags = last_print_tags;
   fmt.size = last_size;
   fmt.count = 1;
   fmt.raw = 0;
@@ -1797,6 +1894,9 @@ x_command (const char *exp, int from_tty)
     last_size = fmt.size;
   last_format = fmt.format;
 
+  /* Remember tag-printing setting.  */
+  last_print_tags = fmt.print_tags;
+
   /* Set a couple of internal variables if appropriate.  */
   if (last_examine_value != nullptr)
     {
diff --git a/gdb/testsuite/gdb.base/options.exp b/gdb/testsuite/gdb.base/options.exp
index 44c773c3fa3..78ec54502c0 100644
--- a/gdb/testsuite/gdb.base/options.exp
+++ b/gdb/testsuite/gdb.base/options.exp
@@ -165,6 +165,7 @@ proc_with_prefix test-print {{prefix ""}} {
 	"-array-indexes"
 	"-elements"
 	"-max-depth"
+	"-memory-tag-violations"
 	"-null-stop"
 	"-object"
 	"-pretty"
diff --git a/gdb/testsuite/gdb.base/with.exp b/gdb/testsuite/gdb.base/with.exp
index 0e83ad4898e..0d53dbebba3 100644
--- a/gdb/testsuite/gdb.base/with.exp
+++ b/gdb/testsuite/gdb.base/with.exp
@@ -238,7 +238,7 @@ with_test_prefix "errors" {
     gdb_test "with w" \
 	"Ambiguous set command \"w\": watchdog, width, write\\."
     gdb_test "with print m" \
-	"Ambiguous set print command \"m\": max-depth, max-symbolic-offset\\."
+	"Ambiguous set print command \"m\": max-depth, max-symbolic-offset, memory-tag-violations\\."
 
     gdb_test "with variable xxx=1" \
 	"Cannot use this setting with the \"with\" command"
diff --git a/gdb/valprint.c b/gdb/valprint.c
index 340a329f9d0..c089ee27a23 100644
--- a/gdb/valprint.c
+++ b/gdb/valprint.c
@@ -110,6 +110,7 @@ struct value_print_options user_print_options =
   10,				/* repeat_count_threshold */
   0,				/* output_format */
   0,				/* format */
+  1,				/* memory_tag_violations */
   0,				/* stop_print_at_null */
   0,				/* print_array_indexes */
   0,				/* deref_ref */
@@ -203,6 +204,17 @@ show_repeat_count_threshold (struct ui_file *file, int from_tty,
 		    value);
 }
 
+/* If nonzero, prints memory tag violations for pointers.  */
+
+static void
+show_memory_tag_violations (struct ui_file *file, int from_tty,
+			    struct cmd_list_element *c, const char *value)
+{
+  fprintf_filtered (file,
+		    _("Printing of memory tag violations is %s.\n"),
+		    value);
+}
+
 /* If nonzero, stops printing of char arrays at first null.  */
 
 static void
@@ -3023,6 +3035,17 @@ will be replaced with either '{...}' or '(...)' depending on the language.\n\
 Use \"unlimited\" to print the complete structure.")
   },
 
+  boolean_option_def {
+    "memory-tag-violations",
+    [] (value_print_options *opt) { return &opt->memory_tag_violations; },
+    show_memory_tag_violations, /* show_cmd_cb */
+    N_("Set printing of memory tag violations for pointers."),
+    N_("Show printing of memory tag violations for pointers."),
+    N_("Issue a warning when the printed value is a pointer\n\
+whose logical tag doesn't match the allocation tag of the memory\n\
+location it points to."),
+  },
+
   boolean_option_def {
     "null-stop",
     [] (value_print_options *opt) { return &opt->stop_print_at_null; },
diff --git a/gdb/valprint.h b/gdb/valprint.h
index ca8ad6aa715..e1dae2cc8fc 100644
--- a/gdb/valprint.h
+++ b/gdb/valprint.h
@@ -65,6 +65,9 @@ struct value_print_options
      e.g. when the user passes a format to "print".  */
   int format;
 
+  /* Print memory tag violations for pointers.  */
+  bool memory_tag_violations;
+
   /* Stop printing at null character?  */
   bool stop_print_at_null;
 
@@ -252,6 +255,7 @@ struct format_data
     int count;
     char format;
     char size;
+    bool print_tags;
 
     /* True if the value should be printed raw -- that is, bypassing
        python-based formatters.  */
-- 
2.25.1


  parent reply	other threads:[~2021-03-22 13:22 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-22 13:20 [PATCH v6 00/25] Memory Tagging Support + AArch64 Linux implementation Luis Machado
2021-03-22 13:20 ` [PATCH v6 01/25] New target methods for memory tagging support Luis Machado
2021-03-23 21:22   ` Simon Marchi
2021-03-22 13:20 ` [PATCH v6 02/25] New gdbarch memory tagging hooks Luis Machado
2021-03-22 13:20 ` [PATCH v6 03/25] Add GDB-side remote target support for memory tagging Luis Machado
2021-03-22 13:20 ` [PATCH v6 04/25] Unit testing for GDB-side remote memory tagging handling Luis Machado
2021-03-22 13:20 ` [PATCH v6 05/25] GDBserver remote packet support for memory tagging Luis Machado
2021-03-22 13:21 ` [PATCH v6 06/25] Unit tests for gdbserver memory tagging remote packets Luis Machado
2021-03-22 13:21 ` [PATCH v6 07/25] Documentation for " Luis Machado
2021-03-22 17:47   ` Eli Zaretskii
2021-03-22 13:21 ` [PATCH v6 08/25] AArch64: Add MTE CPU feature check support Luis Machado
2021-03-22 13:21 ` [PATCH v6 09/25] AArch64: Add target description/feature for MTE registers Luis Machado
2021-03-22 13:21 ` [PATCH v6 10/25] AArch64: Add MTE register set support for GDB and gdbserver Luis Machado
2021-03-22 13:21 ` [PATCH v6 11/25] AArch64: Add MTE ptrace requests Luis Machado
2021-03-22 13:21 ` [PATCH v6 12/25] AArch64: Implement memory tagging target methods for AArch64 Luis Machado
2021-03-22 13:21 ` [PATCH v6 13/25] Convert char array to std::string in linux_find_memory_regions_full Luis Machado
2021-03-22 13:21 ` [PATCH v6 14/25] Refactor parsing of /proc/<pid>/smaps Luis Machado
2021-03-23 21:28   ` Simon Marchi
2021-03-22 13:21 ` [PATCH v6 15/25] AArch64: Implement the memory tagging gdbarch hooks Luis Machado
2021-03-23 21:32   ` Simon Marchi
2021-03-22 13:21 ` [PATCH v6 16/25] AArch64: Add unit testing for logical tag set/get operations Luis Machado
2022-01-31 14:34   ` Simon Marchi
2021-03-22 13:21 ` [PATCH v6 17/25] AArch64: Report tag violation error information Luis Machado
2021-03-22 13:21 ` [PATCH v6 18/25] AArch64: Add gdbserver MTE support Luis Machado
2021-03-22 13:21 ` [PATCH v6 19/25] AArch64: Add MTE register set support for core files Luis Machado
2021-03-22 13:21 ` [PATCH v6 20/25] New memory-tag commands Luis Machado
2021-03-22 13:21 ` [PATCH v6 21/25] Documentation for the new mtag commands Luis Machado
2021-03-22 17:49   ` Eli Zaretskii
2021-03-22 13:21 ` Luis Machado [this message]
2021-03-22 13:21 ` [PATCH v6 23/25] Document new "x" and "print" memory tagging extensions Luis Machado
2021-03-22 17:51   ` Eli Zaretskii
2021-03-22 13:21 ` [PATCH v6 24/25] Add NEWS entry Luis Machado
2021-03-22 17:51   ` Eli Zaretskii
2021-03-22 17:52     ` Luis Machado
2021-03-22 13:21 ` [PATCH v6 25/25] Add memory tagging testcases Luis Machado
2021-03-23 22:50 ` [PATCH v6 00/25] Memory Tagging Support + AArch64 Linux implementation Simon Marchi
2021-03-24 18:18   ` Luis Machado

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=20210322132120.1202230-23-luis.machado@linaro.org \
    --to=luis.machado@linaro.org \
    --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).