From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-qt1-x82e.google.com (mail-qt1-x82e.google.com [IPv6:2607:f8b0:4864:20::82e]) by sourceware.org (Postfix) with ESMTPS id 233A4382E05C for ; Wed, 15 Jul 2020 19:49:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 233A4382E05C Received: by mail-qt1-x82e.google.com with SMTP id d27so2750530qtg.4 for ; Wed, 15 Jul 2020 12:49:33 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=fCovSADJz61NiOFKvJq/2j1SyL9YxcX6hdITZ12wyog=; b=RMBPsjLYcaFI0S6o0Xlj6FWrLvUTdarW870ljJTLYxsRJ5JJ58u6RDPbNmniXVssOR 1a5jpb+Lla1PSvTBhWEpz/xcXW7zopH/1UKfaQ4/Q9tGq2/9RcF2znML2lXccaZFylWQ 6TkTlBs9QAyYW7+Y4RbqVedAR0mLDrT12MyYnx7G33ZhRw+GiCOYXNndF1j/A5SR23yn Zi/Bbp7pruJaRUZoqxvJ9EhC0RTKUk/R7+SquJop4h453+yv/0wA/mu54f+Vd0VRKJKu i0cJdb+n+b/KA/1EyOZVzDRgPJezlHCDcO6rBicUgPpXPJhh87LQMntSD2eRn8FUl15p nsGQ== X-Gm-Message-State: AOAM531Bq1r0JvJ0sJIyN4Lh3N3G4RX60gM0OEE3nSwcPwRSfYiPpW2G /fywhE7K4Vu5DW8MhzDnINdffCgc3/mu3g== X-Google-Smtp-Source: ABdhPJxa5+Uyt6JiUPWsp5kQ9ZPghwsViWqUAnyQjF+v7Tbm7N5snA2Yz3Fj+1IjIYSGEHXHA3WjHw== X-Received: by 2002:ac8:408c:: with SMTP id p12mr1493063qtl.332.1594842571612; Wed, 15 Jul 2020 12:49:31 -0700 (PDT) Received: from localhost.localdomain ([2804:7f0:8283:82c3:9daa:7611:ebe6:931]) by smtp.gmail.com with ESMTPSA id z18sm4340537qta.51.2020.07.15.12.49.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Jul 2020 12:49:31 -0700 (PDT) From: Luis Machado To: gdb-patches@sourceware.org, Alan.Hayward@arm.com Cc: omair.javaid@linaro.org, catalin.marinas@arm.com, david.spickett@linaro.org, jose.marchesi@oracle.com Subject: [PATCH 18/23] New mtag commands Date: Wed, 15 Jul 2020 16:45:08 -0300 Message-Id: <20200715194513.16641-19-luis.machado@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200715194513.16641-1-luis.machado@linaro.org> References: <20200715194513.16641-1-luis.machado@linaro.org> X-Spam-Status: No, score=-10.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 15 Jul 2020 19:49:35 -0000 Add new commands under the "mtag" prefix to allow users to inspect, modify and check memory tags in different ways. The available subcommands are the following: - mtag showltag : Shows the logical tag for a particular address. - mtag setltag : Prints the address tagged with the logical tag . - mtag showatag : Shows the allocation tag for a particular address. - mtag setatag : Sets one or more allocation tags to the specified tags. - mtag check : Check if the logical tag in
matches its allocation tag. These commands make use of the memory tagging gdbarch methods, and are still available, but disabled, when memory tagging is not supported by the architecture. I've pondered about a way to make these commands invisible when memory tagging is not available, but given the check is at runtime (and support may come and go based on a process' configuration), that is a bit too late in the process to either not include the commands or get rid of them. Ideas are welcome. gdb/ChangeLog: YYYY-MM-DD Luis Machado * printcmd.c: Include gdbsupport/rsp-low.h. (mtaglist): New static global. (process_print_command_args): Factored out of print_command_1. (print_command_1): Use process_print_command_args. (show_addr_not_tagged, show_memtag_unsupported, mtag_command) (mtag_showtag_command, mtag_showltag_command, mtag_showatag_command) (parse_setltag_input, mtag_setltag_command, parse_setatag_input) (mtag_setatag_command, mtag_check_command): New functions. (_initialize_printcmd): Add "mtag" prefix and subcommands. gdbsupport/ChangeLog: YYYY-MM-DD Luis Machado * rsp-low.cc (fromhex): Change error message text to not be RSP-specific. --- gdb/printcmd.c | 359 ++++++++++++++++++++++++++++++++++++++++-- gdbsupport/rsp-low.cc | 2 +- 2 files changed, 348 insertions(+), 13 deletions(-) diff --git a/gdb/printcmd.c b/gdb/printcmd.c index d0261a021a..ef1c16031b 100644 --- a/gdb/printcmd.c +++ b/gdb/printcmd.c @@ -53,6 +53,11 @@ #include "source.h" #include "gdbsupport/byte-vector.h" #include "gdbsupport/gdb_optional.h" +#include "gdbsupport/rsp-low.h" + +/* Chain containing all defined mtag subcommands. */ + +struct cmd_list_element *mtaglist; /* Last specified output format. */ @@ -1208,31 +1213,38 @@ print_value (value *val, const value_print_options &opts) annotate_value_history_end (); } -/* Implementation of the "print" and "call" commands. */ +/* Helper for parsing arguments for print_command_1. */ -static void -print_command_1 (const char *args, int voidprint) +static struct value * +process_print_command_args (const char *args, value_print_options *print_opts) { - struct value *val; - value_print_options print_opts; - - get_user_print_options (&print_opts); + get_user_print_options (print_opts); /* Override global settings with explicit options, if any. */ - auto group = make_value_print_options_def_group (&print_opts); + auto group = make_value_print_options_def_group (print_opts); gdb::option::process_options (&args, gdb::option::PROCESS_OPTIONS_REQUIRE_DELIMITER, group); - print_command_parse_format (&args, "print", &print_opts); + print_command_parse_format (&args, "print", print_opts); const char *exp = args; if (exp != nullptr && *exp) { expression_up expr = parse_expression (exp); - val = evaluate_expression (expr.get ()); + return evaluate_expression (expr.get ()); } - else - val = access_value_history (0); + + return access_value_history (0); +} + +/* Implementation of the "print" and "call" commands. */ + +static void +print_command_1 (const char *args, int voidprint) +{ + value_print_options print_opts; + + struct value *val = process_print_command_args (args, &print_opts); if (voidprint || (val && value_type (val) && value_type (val)->code () != TYPE_CODE_VOID)) @@ -2700,6 +2712,273 @@ eval_command (const char *arg, int from_tty) execute_command (expanded.c_str (), from_tty); } +/* Convenience function for error checking in mtag commands. */ + +static void +show_addr_not_tagged (CORE_ADDR address) +{ + error (_("Address %s not in a region mapped with a memory tagging flag."), + paddress (target_gdbarch (), address)); +} + +/* Convenience function for error checking in mtag commands. */ + +static void +show_memtag_unsupported (void) +{ + error (_("Memory tagging not supported or disabled by the current" + " architecture.")); +} + +/* Implement the "mtag" prefix command. */ + +static void +mtag_command (const char *arg, int from_tty) +{ + help_list (mtaglist, "mtag ", all_commands, gdb_stdout); +} + +/* Helper for showltag and showatag. */ + +static void +mtag_showtag_command (const char *args, enum memtag_type tag_type) +{ + if (args == nullptr) + error_no_arg (_("address or pointer")); + + /* Parse args into a value. If the value is a pointer or an address, + then fetch the logical or allocation tag. */ + value_print_options print_opts; + + struct value *val = process_print_command_args (args, &print_opts); + + /* If the address is not in a region memory mapped with a memory tagging + flag, it is no use trying to access/manipulate its allocation tag. + + It is OK to manipulate the logical tag though. */ + if (tag_type == tag_allocation + && !gdbarch_tagged_address_p (target_gdbarch (), val)) + show_addr_not_tagged (value_as_address (val)); + + std::string tag = gdbarch_memtag_to_string (target_gdbarch (), + val, tag_type); + if (tag.empty ()) + printf_filtered (_("%s tag unavailable.\n"), + tag_type == tag_logical? "Logical" : "Allocation"); + + struct value *v_tag = process_print_command_args (tag.c_str (), + &print_opts); + print_opts.output_format = 'x'; + print_value (v_tag, print_opts); +} + +/* Implement the "mtag showltag" command. */ + +static void +mtag_showltag_command (const char *args, int from_tty) +{ + if (!memtag || !target_supports_memory_tagging ()) + show_memtag_unsupported (); + + mtag_showtag_command (args, tag_logical); +} + +/* Implement the "mtag showatag" command. */ + +static void +mtag_showatag_command (const char *args, int from_tty) +{ + if (!memtag || !target_supports_memory_tagging ()) + show_memtag_unsupported (); + + mtag_showtag_command (args, tag_allocation); +} + +/* Parse ARGS and extract ADDR and TAG. + ARGS should have format . */ + +static void +parse_setltag_input (const char *args, struct value **val, + gdb::byte_vector &tags, value_print_options *print_opts) +{ + /* Given can be reasonably complex, we parse things backwards + so we can isolate the portion. */ + + /* Fetch the address. */ + std::string s_address = extract_string_maybe_quoted (&args); + + /* Parse the address into a value. */ + *val = process_print_command_args (s_address.c_str (), print_opts); + + /* Fetch the tag bytes. */ + std::string s_tag = extract_string_maybe_quoted (&args); + + /* Validate the input. */ + if (s_address.empty () || s_tag.empty ()) + error (_("Missing arguments.")); + + if (s_tag.length () % 2) + error (_("Error parsing tags argument. The tag should be 2 digits.")); + + tags = hex2bin (s_tag.c_str ()); +} + +/* Implement the "mtag setltag" command. */ + +static void +mtag_setltag_command (const char *args, int from_tty) +{ + if (!memtag || !target_supports_memory_tagging ()) + show_memtag_unsupported (); + + if (args == nullptr) + error_no_arg (_("
")); + + gdb::byte_vector tags; + struct value *val; + value_print_options print_opts; + + /* Parse the input. */ + parse_setltag_input (args, &val, tags, &print_opts); + + /* Setting the logical tag is just a local operation that does not touch + any memory from the target. Given an input value, we modify the value + to include the appropriate tag. + + For this reason we need to cast the argument value to a + (void *) pointer. This is so we have the right the for the gdbarch + hook to manipulate the value and insert the tag. + + Otherwise, this would fail if, for example, GDB parsed the argument value + into an int-sized value and the pointer value has a type of greater + length. */ + + /* Cast to (void *). */ + val = value_cast (builtin_type (target_gdbarch ())->builtin_data_ptr, + val); + + if (gdbarch_set_memtags (target_gdbarch (), val, 0, tags, + tag_logical) != 0) + printf_filtered (_("Could not update the logical tag data.\n")); + else + { + /* Always print it in hex format. */ + print_opts.output_format = 'x'; + print_value (val, print_opts); + } +} + +/* Parse ARGS and extract ADDR, LENGTH and TAGS. */ + +static void +parse_setatag_input (const char *args, struct value **val, size_t *length, + gdb::byte_vector &tags) +{ + /* Fetch the address. */ + std::string s_address = extract_string_maybe_quoted (&args); + + /* Parse the address into a value. */ + value_print_options print_opts; + *val = process_print_command_args (s_address.c_str (), &print_opts); + + /* Fetch the length. */ + std::string s_length = extract_string_maybe_quoted (&args); + + /* Fetch the tag bytes. */ + std::string s_tags = extract_string_maybe_quoted (&args); + + /* Validate the input. */ + if (s_address.empty () || s_length.empty () || s_tags.empty ()) + error (_("Missing arguments.")); + + errno = 0; + *length = strtoulst (s_length.c_str (), NULL, 10); + if (errno != 0) + error (_("Error parsing length argument.")); + + if (s_tags.length () % 2) + error (_("Error parsing tags argument. Tags should be 2 digits per byte.")); + + tags = hex2bin (s_tags.c_str ()); + + /* If the address is not in a region memory mapped with a memory tagging + flag, it is no use trying to access/manipulate its allocation tag. */ + if (!gdbarch_tagged_address_p (target_gdbarch (), *val)) + show_addr_not_tagged (value_as_address (*val)); +} + +/* Implement the "mtag setatag" command. + ARGS should be in the format
. */ + +static void +mtag_setatag_command (const char *args, int from_tty) +{ + if (!memtag || !target_supports_memory_tagging ()) + show_memtag_unsupported (); + + if (args == nullptr) + error_no_arg (_(" ")); + + gdb::byte_vector tags; + size_t length = 0; + struct value *val; + + /* Parse the input. */ + parse_setatag_input (args, &val, &length, tags); + + if (gdbarch_set_memtags (target_gdbarch (), val, length, tags, + tag_allocation) != 0) + printf_filtered (_("Could not update the allocation tag(s).\n")); + else + printf_filtered (_("Allocation tag(s) updated successfully.\n")); +} + +/* Implement the "mtag check" command. */ + +static void +mtag_check_command (const char *args, int from_tty) +{ + if (!memtag || !target_supports_memory_tagging ()) + show_memtag_unsupported (); + + if (args == nullptr) + error (_("Argument required (address or pointer)")); + + /* Parse the expression into a value. If the value is an address or + pointer, then check its logical tag against the allocation tag. */ + value_print_options print_opts; + + struct value *val = process_print_command_args (args, &print_opts); + + /* If the address is not in a region memory mapped with a memory tagging + flag, it is no use trying to access/manipulate its allocation tag. */ + if (!gdbarch_tagged_address_p (target_gdbarch (), val)) + show_addr_not_tagged (value_as_address (val)); + + CORE_ADDR addr = value_as_address (val); + + /* If memory tagging validation is on, check if the tag is valid. */ + if (gdbarch_memtag_mismatch_p (target_gdbarch (), val)) + { + std::string ltag = gdbarch_memtag_to_string (target_gdbarch (), + val, tag_logical); + std::string atag = gdbarch_memtag_to_string (target_gdbarch (), + val, tag_allocation); + + printf_filtered (_("Logical tag (%s) does not match" + " the allocation tag (%s) for address %s.\n"), + ltag.c_str (), atag.c_str (), + paddress (target_gdbarch (), addr)); + } + else + { + std::string ltag = gdbarch_memtag_to_string (target_gdbarch (), + val, tag_logical); + printf_filtered (_("Memory tags for address %s match (%s).\n"), + paddress (target_gdbarch (), addr), ltag.c_str ()); + } +} + void _initialize_printcmd (); void _initialize_printcmd () @@ -2910,4 +3189,60 @@ certain operations have illegal tags."), NULL, show_memtag, &setlist, &showlist); + + /* Memory tagging commands. */ + add_prefix_cmd ("mtag", class_vars, mtag_command, _("\ +Generic command for showing and manipulating memory tag properties."), + &mtaglist, "mtag ", 0, &cmdlist); + add_cmd ("showltag", class_vars, mtag_showltag_command, + ("Show the logical tag for an address.\n\ +Usage: mtag showltag
.\n\ +
is an expression that evaluates to a pointer or memory address.\n\ +GDB will show the logical tag associated with
. The tag\n\ +interpretation is architecture-specific."), + &mtaglist); + add_cmd ("showatag", class_vars, mtag_showatag_command, + _("Show the allocation tag for an address.\n\ +Usage: mtag showatag
.\n\ +
is an expression that evaluates to a pointer or memory address.\n\ +GDB will show the allocation tag associated with
. The tag\n\ +interpretation is architecture-specific."), + &mtaglist); + add_cmd ("setltag", class_vars, mtag_setltag_command, + _("Set the logical tag for an address.\n\ +Usage: mtag setltag
\n\ +
is an expression that evaluates to a pointer or memory address.\n\ + is a sequence of hex bytes that will be interpreted by the\n\ +architecture as a single memory tag.\n\ +GDB will set the logical tag for
to , and will show the\n\ +resulting address with the updated tag."), + &mtaglist); + add_cmd ("setatag", class_vars, mtag_setatag_command, + _("Set the allocation tag for an address.\n\ +Usage: mtag setatag
\n\ +
is an expression that evaluates to a pointer or memory address\n\ + is the number of bytes that will get added to
to calculate\n\ +the memory range.\n\ + is a sequence of hex bytes that will be interpreted by the\n\ +architecture as one or more memory tags.\n\ +Sets the tags of the memory range [
,
+ )\n\ +to the specified tag bytes.\n\ +\n\ +If the number of tags is greater than or equal to the number of tag granules\n\ +in the [
,
+ ,
+ ] is stored to."), + &mtaglist); + add_cmd ("check", class_vars, mtag_check_command, + _("Validate the logical tag against the allocation tag.\n\ +Usage: mtag check
\n\ +
is an expression that evaluates to a pointer or memory address\n\ +GDB will fetch the logical and allocation tags for
and will\n\ +compare them for equality. If the tags do not match, GDB will show\n\ +additional information about the mismatch."), + &mtaglist); } diff --git a/gdbsupport/rsp-low.cc b/gdbsupport/rsp-low.cc index 9bb16605dd..90b51dbb18 100644 --- a/gdbsupport/rsp-low.cc +++ b/gdbsupport/rsp-low.cc @@ -32,7 +32,7 @@ fromhex (int a) else if (a >= 'A' && a <= 'F') return a - 'A' + 10; else - error (_("Reply contains invalid hex digit %d"), a); + error (_("Invalid hex digit %d"), a); } /* See rsp-low.h. */ -- 2.17.1