From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id iOjdM16f7F8ACwAAWB0awg (envelope-from ) for ; Wed, 30 Dec 2020 10:40:14 -0500 Received: by simark.ca (Postfix, from userid 112) id D19F51F0BE; Wed, 30 Dec 2020 10:40:14 -0500 (EST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on simark.ca X-Spam-Level: X-Spam-Status: No, score=0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,MAILING_LIST_MULTI,RDNS_NONE,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.2 Received: from sourceware.org (unknown [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id E83141F075 for ; Wed, 30 Dec 2020 10:40:13 -0500 (EST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 741F4388E823; Wed, 30 Dec 2020 15:40:13 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 741F4388E823 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1609342813; bh=CIsyPD3mh0ncAIBpIq1w4O2PtoOj9Uum5rTMYQypBbM=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=E03OGkw36rBBDGkazZ4/zq9FKYbbpw3CnJ4kn48db5FsVQHR1orm/is8wuZWjKR2n h3iWXLrJddRjKNdFZ6jq2J+8qj13HsafGq75AZxaKnND83la8qduDjgF49iNV3LFZO Mny9HWcoYUXcZ0GyXciryN9obLidgqAjwapbuoSk= Received: from mail-qt1-x833.google.com (mail-qt1-x833.google.com [IPv6:2607:f8b0:4864:20::833]) by sourceware.org (Postfix) with ESMTPS id 51CD9388E819 for ; Wed, 30 Dec 2020 15:40:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 51CD9388E819 Received: by mail-qt1-x833.google.com with SMTP id c14so11168981qtn.0 for ; Wed, 30 Dec 2020 07:40:09 -0800 (PST) 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:mime-version:content-transfer-encoding; bh=CIsyPD3mh0ncAIBpIq1w4O2PtoOj9Uum5rTMYQypBbM=; b=tiYpLuT1877Dj8nGUM2uoJB6eYRu6aPkGlZVXKaPtLs/e7TDA9TfnuKX9CqwVoga4C EWlImmTjwnwGw7W7FbNxQYztXrrSel+1rI4nl611v399x8v4K0OUUhJI52uTXAWjAlcD 3/MkkrcgBbS9110FNKcDcrOvbfY9u87Vt5fqkKphDaaSkcDLSjjzWotw8GCQL7m74G4+ 7sXs2NGJbSppBdiXcmciMoErJvzeznDWVvPMXws7Bx2fmd92O5GH/Qa34sHbxf/mhjyM NXkITK96CLHDSphKIKqYPh/xBdvfFToUW/1lw3eXvvAN4JKE5D+n2gnkYezzHmw1bCPE b4Zw== X-Gm-Message-State: AOAM531JeMZqtGFGXuol4wJ9eHrcXAN5ZSeeTunHpnaGg6y0+ozGLfT+ fycjvYE/hVXdvVfOst3kqoIC2VIOvcr2rA== X-Google-Smtp-Source: ABdhPJxqco4J0odV0flsny7wvd6g5pGQb2OLCsnLHw157WBj+qpRjp79NQASDO/LJd6nnwODYzFhgw== X-Received: by 2002:ac8:7306:: with SMTP id x6mr52565016qto.371.1609342808641; Wed, 30 Dec 2020 07:40:08 -0800 (PST) Received: from localhost.localdomain ([2804:7f0:8284:370e:c9ba:c4ec:737b:57da]) by smtp.gmail.com with ESMTPSA id f6sm28589336qkh.2.2020.12.30.07.40.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Dec 2020 07:40:08 -0800 (PST) To: gdb-patches@sourceware.org Subject: [PATCH v4 22/25] Extend "x" and "print" commands to support memory tagging Date: Wed, 30 Dec 2020 12:39:13 -0300 Message-Id: <20201230153916.1586725-23-luis.machado@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201230153916.1586725-1-luis.machado@linaro.org> References: <20201230153916.1586725-1-luis.machado@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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: , From: Luis Machado via Gdb-patches Reply-To: Luis Machado Cc: david.spickett@linaro.org Errors-To: gdb-patches-bounces@sourceware.org Sender: "Gdb-patches" 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 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 | 82 +++++++++++++++++++++++++++++++++++++++++++++++++- gdb/valprint.h | 1 + 2 files changed, 82 insertions(+), 1 deletion(-) diff --git a/gdb/printcmd.c b/gdb/printcmd.c index 3736b35147..5ef32c3399 100644 --- a/gdb/printcmd.c +++ b/gdb/printcmd.c @@ -211,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 == '-') { @@ -233,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 @@ -1118,12 +1124,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 && memory_tagging + && 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); @@ -1154,6 +1195,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"); } @@ -1226,6 +1272,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 (memory_tagging && 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 * @@ -1261,7 +1327,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_matches_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); + } } /* Called from command completion function to skip over /FMT diff --git a/gdb/valprint.h b/gdb/valprint.h index 47f6ed009b..87b62bdaa1 100644 --- a/gdb/valprint.h +++ b/gdb/valprint.h @@ -252,6 +252,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