From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-qv1-xf32.google.com (mail-qv1-xf32.google.com [IPv6:2607:f8b0:4864:20::f32]) by sourceware.org (Postfix) with ESMTPS id C2F723840C3C for ; Wed, 15 Jul 2020 19:49:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org C2F723840C3C Received: by mail-qv1-xf32.google.com with SMTP id a14so1477196qvq.6 for ; Wed, 15 Jul 2020 12:49:56 -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=U8XVhdZs1zHrenZ9ELvBdTB1cTXdYeXbnbC3hkdNAWg=; b=iOo94uUIXrRixt/m3cF9oY+JVnJqVtes/o5riCRKQ+idktKq5AK7mEVw8csgAOJXWf 9zaxAIkbwam3zav3FymP5ogbpFeFbjkShy6ws+HMJlcEGbH6VU6I+npflE1DKPnGjhtA tJfptyv+Xl7Q1+C9pxgEPXvYh4B1L4gw1nkpWmF4hSz6SK2mUVsVzOhnUuXl5DvGH60M 7b4yY3/lzbpHaKrYAINM+IRsktPoR5bntK57vLAhdWXrKB78IDOXpI1vijJo3uxDwFHD WpU6TRoy+7GHU5y+i1Rs4eCe8ZSA6mStjB5qsyGYlOpmLKKNdjw0ljUhNCgbEtPugblA HdZA== X-Gm-Message-State: AOAM531EY+dad0HEPOsqgcCuVbi/RrNKXdOKyx7QeXJQgSI3hwqV6MSg s7+rZppl6w6q7PUsEmVP/LtFsw+VNu60Ew== X-Google-Smtp-Source: ABdhPJwRcnmPakdtzFybVBz1qomnoOpX43m93GV8j0uB78pki2G9zNRuIdjkpBqYn87CmcR93ituvQ== X-Received: by 2002:a0c:a992:: with SMTP id a18mr871451qvb.211.1594842595853; Wed, 15 Jul 2020 12:49:55 -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.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Jul 2020 12:49:55 -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 20/23] Extend "x" and "print" commands to support memory tagging Date: Wed, 15 Jul 2020 16:45:10 -0300 Message-Id: <20200715194513.16641-21-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:58 -0000 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 0x1234: 0x01 0x02 0x00 0x00 0x00 0x00 0x00 0x00 0x123c: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x1244: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x124c: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 (gdb) x/4gxm a 0x1234: 0x0000000000000201 0x0000000000000000 0x1244: 0x0000000000000000 0x0000000000000000 gdb/ChangeLog: YYYY-MM-DD Luis Machado * 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.h (struct format_data) : New field. --- gdb/printcmd.c | 83 +++++++++++++++++++++++++++++++++++++++++++++++++- gdb/valprint.h | 1 + 2 files changed, 83 insertions(+), 1 deletion(-) diff --git a/gdb/printcmd.c b/gdb/printcmd.c index ef1c16031b..ecdca8886f 100644 --- a/gdb/printcmd.c +++ b/gdb/printcmd.c @@ -54,6 +54,7 @@ #include "gdbsupport/byte-vector.h" #include "gdbsupport/gdb_optional.h" #include "gdbsupport/rsp-low.h" +#include "infrun.h" /* For memtag setting. */ /* Chain containing all defined mtag subcommands. */ @@ -210,6 +211,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 == '-') { @@ -232,6 +234,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 @@ -1105,12 +1112,47 @@ 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 && memtag + && 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)) + { + std::string atag = gdbarch_memtag_to_string (gdbarch, v_addr, + tag_allocation); + + if (!atag.empty ()) + { + printf_filtered (_("\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); @@ -1141,6 +1183,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"); } @@ -1213,6 +1260,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 (memtag && target_supports_memory_tagging () + && gdbarch_tagged_address_p (target_gdbarch (), value)) + { + gdb_assert (value && value_type (value)); + + 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 * @@ -1248,7 +1315,21 @@ 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 (should_validate_memtags (val) + && 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).\n"), + ltag.c_str (), atag.c_str ()); + } + print_value (val, print_opts); + } } /* See valprint.h. */ diff --git a/gdb/valprint.h b/gdb/valprint.h index 57bc0339fc..37a7405428 100644 --- a/gdb/valprint.h +++ b/gdb/valprint.h @@ -241,6 +241,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.17.1