From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ed1-x52e.google.com (mail-ed1-x52e.google.com [IPv6:2a00:1450:4864:20::52e]) by sourceware.org (Postfix) with ESMTPS id 281D1384803C for ; Mon, 26 Jul 2021 13:17:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 281D1384803C Received: by mail-ed1-x52e.google.com with SMTP id h8so10540119ede.4 for ; Mon, 26 Jul 2021 06:17:22 -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:mime-version:content-transfer-encoding; bh=sv5dJ6i8FMTv2AAhBVnGbNBPstuvte7c13p6huyJo54=; b=X2J43oFN2naX+qcgDCQEgHlAujLRbKdsGU1YCs3kx4mlHZN8LBgprmJVuSbgZ78yWq I2usO1UT9hSoF8rmPIRjvD51t0ytHafieZdaMNG/up2CYyXxW42hZ5rm5f34g4EPR3J7 FtKIU0hnBh+Ab51ijfzkV4wudwvLJlzqNCJiAa/gssSqoiAMTmkDFtpU++1XBubW+JAW IRQoIRKxXdDKcOTVZqJxZfikgTJ8GBk8sbORQ0fHw0FTL6nac9D5BUPLzLGlBUsV1RBs 9hd6MYYfQ7WfS+5jC9iOzhbFrtj08c0TmF1Pl2WOucf9DY0ubIjT/MwhIiFtAGQakL0b 3BOw== X-Gm-Message-State: AOAM5303jp5BX75AZ6zX53gbu3if7fSqULRtWHnXH307yHlennc+8S9l 2kC6u/jmJf+Ilny4JS97cs/Rea6YWvo= X-Google-Smtp-Source: ABdhPJzfS7OXk1I+4nkHV+Kw5tu8zUwEDc0LGb6DNOykQGHi/kz/QOPs8kj/ca81OJeKYCmdiBDjfw== X-Received: by 2002:aa7:d8d4:: with SMTP id k20mr3410715eds.373.1627305441234; Mon, 26 Jul 2021 06:17:21 -0700 (PDT) Received: from atlantis.fritz.box ([2001:983:21dc:1:cab1:9dc7:2cbe:af04]) by smtp.gmail.com with ESMTPSA id di14sm929995edb.79.2021.07.26.06.17.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Jul 2021 06:17:20 -0700 (PDT) From: Shahab Vahedi To: gdb-patches@sourceware.org Cc: Shahab Vahedi , Simon Marchi Subject: [PUSHED master] gdb: Fix numerical field extraction for target description "flags" Date: Mon, 26 Jul 2021 15:17:41 +0200 Message-Id: <20210726131741.7931-1-shahab.vahedi@gmail.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210723123830.16536-1-shahab.vahedi@gmail.com> References: <20210723123830.16536-1-shahab.vahedi@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-11.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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: Mon, 26 Jul 2021 13:17:23 -0000 From: Shahab Vahedi The "val_print_type_code_flags ()" function is responsible for extraction of fields for "flags" data type. These data types are used when describing a custom register type in a target description XML. The logic used for the extraction though is not sound: unsigned field_len = TYPE_FIELD_BITSIZE (type, field); ULONGEST field_val = val >> (TYPE_FIELD_BITPOS (type, field) - field_len + 1); TYPE_FIELD_BITSIZE: The bit length of the field to be extracted. TYPE_FIELD_BITPOS: The starting position of the field; 0 is LSB. val: The register value. Imagine you have a field that starts at position 1 and its length is 4 bits. According to the third line of the code snippet the shifting right would become "val >> -2", or "val >> 0xfff...fe" to be precise. That will result in a "field_val" of 0. The correct extraction should be: ULONGEST field_val = val >> TYPE_FIELD_BITPOS (type, field); The rest of the algorithm that masks out the higher bits is OK. Co-Authored-By: Simon Marchi --- gdb/valprint.c | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/gdb/valprint.c b/gdb/valprint.c index fa2b64ef10a..324055da93f 100644 --- a/gdb/valprint.c +++ b/gdb/valprint.c @@ -43,6 +43,8 @@ #include "c-lang.h" #include "cp-abi.h" #include "inferior.h" +#include "gdbsupport/selftest.h" +#include "selftest-arch.h" /* Maximum number of wchars returned from wchar_iterate. */ #define MAX_WCHARS 4 @@ -1221,8 +1223,7 @@ val_print_type_code_flags (struct type *type, struct value *original_value, else { unsigned field_len = TYPE_FIELD_BITSIZE (type, field); - ULONGEST field_val - = val >> (TYPE_FIELD_BITPOS (type, field) - field_len + 1); + ULONGEST field_val = val >> TYPE_FIELD_BITPOS (type, field); if (field_len < sizeof (ULONGEST) * TARGET_CHAR_BIT) field_val &= ((ULONGEST) 1 << field_len) - 1; @@ -3137,10 +3138,41 @@ make_value_print_options_def_group (value_print_options *opts) return {{value_print_option_defs}, opts}; } +#if GDB_SELF_TEST + +/* Test printing of TYPE_CODE_FLAGS values. */ + +static void +test_print_flags (gdbarch *arch) +{ + type *flags_type = arch_flags_type (arch, "test_type", 32); + type *field_type = builtin_type (arch)->builtin_uint32; + + /* Value: 1010 1010 + Fields: CCCB BAAA */ + append_flags_type_field (flags_type, 0, 3, field_type, "A"); + append_flags_type_field (flags_type, 3, 2, field_type, "B"); + append_flags_type_field (flags_type, 5, 3, field_type, "C"); + + value *val = allocate_value (flags_type); + gdb_byte *contents = value_contents_writeable (val); + store_unsigned_integer (contents, 4, gdbarch_byte_order (arch), 0xaa); + + string_file out; + val_print_type_code_flags (flags_type, val, 0, &out); + SELF_CHECK (out.string () == "[ A=2 B=1 C=5 ]"); +} + +#endif + void _initialize_valprint (); void _initialize_valprint () { +#if GDB_SELF_TEST + selftests::register_test_foreach_arch ("print-flags", test_print_flags); +#endif + cmd_list_element *cmd; cmd_list_element *set_print_cmd -- 2.32.0