From patchwork Mon Jan 23 23:13:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Maciej W. Rozycki" X-Patchwork-Id: 63601 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E67FD3858C62 for ; Mon, 23 Jan 2023 23:13:56 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-lf1-x135.google.com (mail-lf1-x135.google.com [IPv6:2a00:1450:4864:20::135]) by sourceware.org (Postfix) with ESMTPS id F06193858D33 for ; Mon, 23 Jan 2023 23:13:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org F06193858D33 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=embecosm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=embecosm.com Received: by mail-lf1-x135.google.com with SMTP id b3so20627447lfv.2 for ; Mon, 23 Jan 2023 15:13:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; h=mime-version:user-agent:references:message-id:in-reply-to:subject :cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=KJVkCNaeZVHNu5eiVB4S5twoaK8RIGWZ2RTPj3WWfxo=; b=OiJUsBMylFSZtw/IS78CaErCgL9X4s5xlsIBukpwd6cWab5Xy7U7tZts9pXdsZyA8r 80Rgp63yJAS70oJQfCQkWUP8YujAP1S9O545bPIswl18g8j1Nu6mw1oyDuHg7Fu9y+5f jc/f/PDA5ExsnQQ7A0BDGb1++4m/8bHd07yASSbayulMZwUO4dNBPfiVOZ5X0duYpae6 34Xgs06LU1MbBgzPX6TDlm33+A+WQ4ww6XIAm2f9puFmnMWE8ZqW0EMF9Wqh71UZQidn /cXd7TPj4rhdGn4ooAbQYIwl5NaSMLT8fDKqry0IOf0rEthIv7cx+xSs4gH4M23deTsf fTHQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=mime-version:user-agent:references:message-id:in-reply-to:subject :cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=KJVkCNaeZVHNu5eiVB4S5twoaK8RIGWZ2RTPj3WWfxo=; b=sX5LPGIQlcdsvcjaVwNLS/pKXt/NfAQxfoZ2+NIN7xiIBplp7xdEUDDuvkKDLWvO/C V/FCrHeRjzdTck7hi58C8MJCAMUnQAM3JW2HEzfZ0+kuhRU04fCm3y4VFgn6/4vbON5y h+yICyTWgSe1+rDr4JukAHm6DvVGphZkFpP3Cl+md8pNPC649iQuQA1CV1nWYIH7VQGz tlssRJIPkwp1LMWeNqkMn7v62Icvqm+1pGOJetoOyYN4KH6CCExh7om8EjVf5XdKayZV 1pif7N0VSejFifNeVqnudKTZkArIZKGNDOrELBbA9+m8GQNLLj8VngM4GEX0fRrU1CQf Z1Nw== X-Gm-Message-State: AFqh2kqb3XStr96vVBGQLchrpVsItERwHb9xUSVtqBjNOjgFcBUmQkWg cxOX4mZchQvfgDc77xB3GMvaZ+iCDjAnomPZ X-Google-Smtp-Source: AMrXdXuw9FtwKvy+aynhrohUVqMND43XjHF0ofYrxQuuAVxOBMUr5qHCwTpfPpVqaW12vTL5/oypHg== X-Received: by 2002:a05:6512:ac7:b0:4b6:ed8b:4f16 with SMTP id n7-20020a0565120ac700b004b6ed8b4f16mr8254903lfu.52.1674515614506; Mon, 23 Jan 2023 15:13:34 -0800 (PST) Received: from [192.168.219.3] ([78.8.192.131]) by smtp.gmail.com with ESMTPSA id u14-20020ac243ce000000b004b4ea0f4e25sm17775lfl.301.2023.01.23.15.13.12 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 23 Jan 2023 15:13:23 -0800 (PST) Date: Mon, 23 Jan 2023 23:13:08 +0000 (GMT) From: "Maciej W. Rozycki" To: gdb-patches@sourceware.org cc: Andrew Burgess , Tom Tromey , Richard Bunt Subject: [PATCH v3 1/5] GDB: Ignore `max-value-size' setting with value history accesses In-Reply-To: Message-ID: References: User-Agent: Alpine 2.20 (DEB 67 2015-01-07) MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, KAM_ASCII_DIVIDERS, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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: , Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" We have an inconsistency in value history accesses where array element accesses cause an error for entries exceeding the currently selected `max-value-size' setting even where such accesses successfully complete for elements located in the inferior, e.g.: (gdb) p/d one $1 = 0 (gdb) p/d one_hundred $2 = {0 } (gdb) p/d one_hundred[99] $3 = 0 (gdb) set max-value-size 25 (gdb) p/d one_hundred value requires 100 bytes, which is more than max-value-size (gdb) p/d one_hundred[99] $7 = 0 (gdb) p/d $2 value requires 100 bytes, which is more than max-value-size (gdb) p/d $2[99] value requires 100 bytes, which is more than max-value-size (gdb) According to our documentation the `max-value-size' setting is a safety guard against allocating an overly large amount of memory. Moreover a statement in documentation says, concerning this setting, that: "Setting this variable does not effect values that have already been allocated within GDB, only future allocations." While in the implementer-speak the sentence may be unambiguous I think the outside user may well infer that the setting only applies to values that need to be retrieved from the debuggee. Therefore rather than just fixing this inconsistency it seems reasonable to lift the setting for value history accesses, under an implication that by having been retrieved from the debuggee they have already passed the safety check. Do it then, by suppressing the value size check in `value_copy' -- under an observation that if the original value has been already loaded (i.e. it's not lazy), then it must have previously passed said check -- making the last two commands succeed: (gdb) p/d $2 $8 = {0 } (gdb) p/d $2 [99] $9 = 0 (gdb) Expand the testsuite accordingly, covering both value history handling and the use of `value_copy' by `make_cv_value', used by Python code. --- Changes from v2: - Always suppress the size check in `value_copy' (keeping the grammatical fix to the function description). - Add Python coverage for `make_cv_value' vs `max-value-size'. New change in v2. --- gdb/testsuite/gdb.base/max-value-size.exp | 3 ++ gdb/testsuite/gdb.python/py-xmethods.exp | 17 +++++++++++++ gdb/testsuite/gdb.python/py-xmethods.py | 17 +++++++++++++ gdb/value.c | 38 ++++++++++++++++++------------ 4 files changed, 61 insertions(+), 14 deletions(-) gdb-value-history-size.diff Index: src/gdb/testsuite/gdb.base/max-value-size.exp =================================================================== --- src.orig/gdb/testsuite/gdb.base/max-value-size.exp +++ src/gdb/testsuite/gdb.base/max-value-size.exp @@ -53,6 +53,9 @@ proc do_value_printing { max_value_size gdb_test "p/d one_hundred" " = \\{0 \\}" } gdb_test "p/d one_hundred \[99\]" " = 0" + # Verify that accessing value history is undisturbed. + gdb_test "p/d \$2" " = \\{0 \\}" + gdb_test "p/d \$2 \[99\]" " = 0" } } Index: src/gdb/testsuite/gdb.python/py-xmethods.exp =================================================================== --- src.orig/gdb/testsuite/gdb.python/py-xmethods.exp +++ src/gdb/testsuite/gdb.python/py-xmethods.exp @@ -160,3 +160,20 @@ gdb_test_no_output "enable xmethod progs gdb_test "pt e.method('a')" "type = void" gdb_test "pt e.method(10)" \ "NotImplementedError.*Error while fetching result type of an xmethod worker defined in Python." + +# Verify `max-value-size' obedience with `gdb.Value.const_value'. +gdb_test "p a1.getarray()" \ + "From Python .* = \\{0, 1, 2, 3, 4, 5, 6, 7, 8, 9\\}" \ + "after: a1.getarray" +gdb_test "p b1.getarray()" \ + "From Python .* = \\{0, 1, 2, 3, 4, 5, 6, 7, 8, 9\\}" \ + "after: b1.getarray" +gdb_test_no_output "python gdb.set_parameter('max-value-size', 16)" +gdb_test "p a1.getarray()" \ + "From Python .*value requires $decimal bytes,\ + which is more than max-value-size" \ + "after: a1.getarray (max-value-size)" +gdb_test "p b1.getarray()" \ + "From Python .*value requires $decimal bytes,\ + which is more than max-value-size" \ + "after: b1.getarray (max-value-size)" Index: src/gdb/testsuite/gdb.python/py-xmethods.py =================================================================== --- src.orig/gdb/testsuite/gdb.python/py-xmethods.py +++ src/gdb/testsuite/gdb.python/py-xmethods.py @@ -39,6 +39,11 @@ from gdb.xmethod import SimpleXMethodMat return obj["a"] +def A_getarray(obj): + print("From Python :") + return obj["array"] + + def A_getarrayind(obj, index): print("From Python :") return obj["array"][index] @@ -48,12 +53,18 @@ from gdb.xmethod import SimpleXMethodMat return obj["array"][index].reference_value() +def B_getarray(obj): + print("From Python :") + return obj["array"].const_value() + + def B_indexoper(obj, index): return obj["array"][index].const_value().reference_value() type_A = gdb.parse_and_eval("(dop::A *) 0").type.target() type_B = gdb.parse_and_eval("(dop::B *) 0").type.target() +type_array = gdb.parse_and_eval("(int[10] *) 0").type.target() type_int = gdb.parse_and_eval("(int *) 0").type.target() @@ -211,12 +222,18 @@ global_dm_list = [ SimpleXMethodMatcher(r"plus_plus_A", r"^dop::A$", r"operator\+\+", plus_plus_A), SimpleXMethodMatcher(r"A_geta", r"^dop::A$", r"^geta$", A_geta), SimpleXMethodMatcher( + r"A_getarray", r"^dop::A$", r"^getarray$", A_getarray, type_array + ), + SimpleXMethodMatcher( r"A_getarrayind", r"^dop::A$", r"^getarrayind$", A_getarrayind, type_int ), SimpleXMethodMatcher( r"A_indexoper", r"^dop::A$", r"operator\[\]", A_indexoper, type_int ), SimpleXMethodMatcher( + r"B_getarray", r"^dop::B$", r"^getarray$", B_getarray, type_array + ), + SimpleXMethodMatcher( r"B_indexoper", r"^dop::B$", r"operator\[\]", B_indexoper, type_int ), ] Index: src/gdb/value.c =================================================================== --- src.orig/gdb/value.c +++ src/gdb/value.c @@ -1034,31 +1034,42 @@ check_type_length_before_alloc (const st } } -/* Allocate the contents of VAL if it has not been allocated yet. */ +/* Allocate the contents of VAL if it has not been allocated yet. + If CHECK_SIZE is true, then apply the usual max-value-size checks. */ static void -allocate_value_contents (struct value *val) +allocate_value_contents (struct value *val, bool check_size) { if (!val->contents) { - check_type_length_before_alloc (val->enclosing_type); + if (check_size) + check_type_length_before_alloc (val->enclosing_type); val->contents.reset ((gdb_byte *) xzalloc (val->enclosing_type->length ())); } } -/* Allocate a value and its contents for type TYPE. */ +/* Allocate a value and its contents for type TYPE. If CHECK_SIZE is true, + then apply the usual max-value-size checks. */ -struct value * -allocate_value (struct type *type) +static struct value * +allocate_value (struct type *type, bool check_size) { struct value *val = allocate_value_lazy (type); - allocate_value_contents (val); + allocate_value_contents (val, check_size); val->lazy = 0; return val; } +/* Allocate a value and its contents for type TYPE. */ + +struct value * +allocate_value (struct type *type) +{ + return allocate_value (type, true); +} + /* Allocate a value that has the correct length for COUNT repetitions of type TYPE. */ @@ -1169,7 +1180,7 @@ value_contents_raw (struct value *value) struct gdbarch *arch = get_value_arch (value); int unit_size = gdbarch_addressable_memory_unit_size (arch); - allocate_value_contents (value); + allocate_value_contents (value, true); ULONGEST length = value_type (value)->length (); return gdb::make_array_view @@ -1179,7 +1190,7 @@ value_contents_raw (struct value *value) gdb::array_view value_contents_all_raw (struct value *value) { - allocate_value_contents (value); + allocate_value_contents (value, true); ULONGEST length = value_enclosing_type (value)->length (); return gdb::make_array_view (value->contents.get (), length); @@ -1752,9 +1763,8 @@ value_release_to_mark (const struct valu return result; } -/* Return a copy of the value ARG. - It contains the same contents, for same memory address, - but it's a different block of storage. */ +/* Return a copy of the value ARG. It contains the same contents, + for the same memory address, but it's a different block of storage. */ struct value * value_copy (const value *arg) @@ -1765,7 +1775,7 @@ value_copy (const value *arg) if (value_lazy (arg)) val = allocate_value_lazy (encl_type); else - val = allocate_value (encl_type); + val = allocate_value (encl_type, false); val->type = arg->type; VALUE_LVAL (val) = arg->lval; val->location = arg->location; @@ -4162,7 +4172,7 @@ void value_fetch_lazy (struct value *val) { gdb_assert (value_lazy (val)); - allocate_value_contents (val); + allocate_value_contents (val, true); /* A value is either lazy, or fully fetched. The availability/validity is only established as we try to fetch a value. */ From patchwork Mon Jan 23 23:13:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Maciej W. Rozycki" X-Patchwork-Id: 63602 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4AA633858C2F for ; Mon, 23 Jan 2023 23:14:19 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-lf1-x12c.google.com (mail-lf1-x12c.google.com [IPv6:2a00:1450:4864:20::12c]) by sourceware.org (Postfix) with ESMTPS id 9F37A3858C2B for ; Mon, 23 Jan 2023 23:13:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9F37A3858C2B Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=embecosm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=embecosm.com Received: by mail-lf1-x12c.google.com with SMTP id bp15so20541114lfb.13 for ; Mon, 23 Jan 2023 15:13:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; h=mime-version:user-agent:references:message-id:in-reply-to:subject :cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=WfekJsD2yf32GXC8bBKliX8UYayl1iou3O9p2kr6+jU=; b=XwWd8524xr20GRn6wLGEyDw9okl4guAwvB8+xV8z5WnI6T5Vqcs7Hdx3Hl1GScRAmV wTQhMxRm+3j0gWT0cfByRgobpQC70lqE8LtHlzBskscHnUPCACzKsZUWgrVEn0gr50QW AaRFzpgdlUsJLj7jbSP5JffPqtiWV4Liwa/xV0pbODvXOiD4DkkdDGZUfN89s0GKjc24 KWk2wh8m0uDWlDmQVoNyLfJlMHjiPK6hlc+V9CwcVb6zhsFu2505fE0L76kBmtCAy4WR vNdpqNPNONltiDFTzS3gUaQotinAt2xvXNLQC1sUaVBjQ8/NhcwgvQCoA2K/lD2UBwrb AZLg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=mime-version:user-agent:references:message-id:in-reply-to:subject :cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=WfekJsD2yf32GXC8bBKliX8UYayl1iou3O9p2kr6+jU=; b=hORwrt4WaB3LqMoRNqE04DrvH31XAQ/FZ6NqzD1vnp722Pt2SjJP+a4KQRU5r/1Zn7 hiU51fJVfx5Y48hFdwj7I65khivxipndPiwyOoDvBr+vXUJl1CpOOtTSRi+Up0qKt/+L CGEA/7HWWsPSoXPNTlXCqOkMcj+n2NnI6r7W1r6HwomnlBgJ1QPjgFKXooAly8HZzlbI pp3AhaVzrMIQnpuqrXwmdOXrlZd4fXgfN1oKuVM9x5R5zHvGkILvf6eJsMq/xCgB8+4K jY8coxpwsLlwh44PxUUnrCFrTAu3axB6MbMP4b7zU9l7UyamFkWPu8cAdLrxhNi8bsYp qfBg== X-Gm-Message-State: AFqh2kq6OswTABr/P2Xz3NwuDzzaOCRVB1J9Mc7DZNR5Hl/sAMIs4VFE ntmHrHZnUTk4ZGrSz8zqVnild/l1qkOGsYU2 X-Google-Smtp-Source: AMrXdXsovgHkqfBSO+ED5mRXPXdvHSVA6VPfiNcOmS/gHMNYZXPtTXkrF9y1KvQhHCtWWDT0KBD5dQ== X-Received: by 2002:ac2:5699:0:b0:4cb:2b23:9965 with SMTP id 25-20020ac25699000000b004cb2b239965mr6108711lfr.63.1674515636218; Mon, 23 Jan 2023 15:13:56 -0800 (PST) Received: from [192.168.219.3] ([78.8.192.131]) by smtp.gmail.com with ESMTPSA id c6-20020ac25306000000b004cc5f44747dsm18690lfh.220.2023.01.23.15.13.53 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 23 Jan 2023 15:13:55 -0800 (PST) Date: Mon, 23 Jan 2023 23:13:51 +0000 (GMT) From: "Maciej W. Rozycki" To: gdb-patches@sourceware.org cc: Andrew Burgess , Tom Tromey , Richard Bunt Subject: [PATCH v3 2/5] GDB: Fix the mess with value byte/bit range types In-Reply-To: Message-ID: References: User-Agent: Alpine 2.20 (DEB 67 2015-01-07) MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, KAM_ASCII_DIVIDERS, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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: , Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" Consistently use the LONGEST and ULONGEST types for value byte/bit offsets and lengths respectively, avoiding slient truncation for ranges exceeding the 32-bit span, which may cause incorrect matching. Also report a conversion overflow on byte ranges that cannot be expressed in terms of bits with these data types, e.g.: (gdb) print one_hundred[1LL << 58] Integer overflow in data location calculation (gdb) print one_hundred[(-1LL << 58) - 1] Integer overflow in data location calculation (gdb) Previously such accesses would be let through with unpredictable results produced. --- No change from v2. New change in v2. --- gdb/value.c | 43 ++++++++++++++++++++++++++----------------- gdb/value.h | 8 ++++---- 2 files changed, 30 insertions(+), 21 deletions(-) gdb-value-range-types.diff Index: src/gdb/value.c =================================================================== --- src.orig/gdb/value.c +++ src/gdb/value.c @@ -74,7 +74,7 @@ struct range LONGEST offset; /* Length of the range. */ - LONGEST length; + ULONGEST length; /* Returns true if THIS is strictly less than OTHER, useful for searching. We keep ranges sorted by offset and coalesce @@ -97,10 +97,10 @@ struct range [offset2, offset2+len2) overlap. */ static int -ranges_overlap (LONGEST offset1, LONGEST len1, - LONGEST offset2, LONGEST len2) +ranges_overlap (LONGEST offset1, ULONGEST len1, + LONGEST offset2, ULONGEST len2) { - ULONGEST h, l; + LONGEST h, l; l = std::max (offset1, offset2); h = std::min (offset1 + len1, offset2 + len2); @@ -112,7 +112,7 @@ ranges_overlap (LONGEST offset1, LONGEST static int ranges_contain (const std::vector &ranges, LONGEST offset, - LONGEST length) + ULONGEST length) { range what; @@ -380,7 +380,8 @@ get_value_arch (const struct value *valu } int -value_bits_available (const struct value *value, LONGEST offset, LONGEST length) +value_bits_available (const struct value *value, + LONGEST offset, ULONGEST length) { gdb_assert (!value->lazy); @@ -389,8 +390,16 @@ value_bits_available (const struct value int value_bytes_available (const struct value *value, - LONGEST offset, LONGEST length) + LONGEST offset, ULONGEST length) { + ULONGEST sign = (1ULL << (sizeof (ULONGEST) * 8 - 1)) / TARGET_CHAR_BIT; + ULONGEST mask = (sign << 1) - 1; + + if (offset != ((offset & mask) ^ sign) - sign + || length != ((length & mask) ^ sign) - sign + || (length > 0 && (~offset & (offset + length - 1) & sign) != 0)) + error (_("Integer overflow in data location calculation")); + return value_bits_available (value, offset * TARGET_CHAR_BIT, length * TARGET_CHAR_BIT); @@ -460,7 +469,7 @@ value_entirely_optimized_out (struct val static void insert_into_bit_range_vector (std::vector *vectorp, - LONGEST offset, LONGEST length) + LONGEST offset, ULONGEST length) { range newr; @@ -558,8 +567,8 @@ insert_into_bit_range_vector (std::vecto if (ranges_overlap (bef.offset, bef.length, offset, length)) { /* #1 */ - ULONGEST l = std::min (bef.offset, offset); - ULONGEST h = std::max (bef.offset + bef.length, offset + length); + LONGEST l = std::min (bef.offset, offset); + LONGEST h = std::max (bef.offset + bef.length, offset + length); bef.offset = l; bef.length = h - l; @@ -600,7 +609,7 @@ insert_into_bit_range_vector (std::vecto struct range &r = *i; if (r.offset <= t.offset + t.length) { - ULONGEST l, h; + LONGEST l, h; l = std::min (t.offset, r.offset); h = std::max (t.offset + t.length, r.offset + r.length); @@ -626,14 +635,14 @@ insert_into_bit_range_vector (std::vecto void mark_value_bits_unavailable (struct value *value, - LONGEST offset, LONGEST length) + LONGEST offset, ULONGEST length) { insert_into_bit_range_vector (&value->unavailable, offset, length); } void mark_value_bytes_unavailable (struct value *value, - LONGEST offset, LONGEST length) + LONGEST offset, ULONGEST length) { mark_value_bits_unavailable (value, offset * TARGET_CHAR_BIT, @@ -786,7 +795,7 @@ static int find_first_range_overlap_and_match (struct ranges_and_idx *rp1, struct ranges_and_idx *rp2, LONGEST offset1, LONGEST offset2, - LONGEST length, ULONGEST *l, ULONGEST *h) + ULONGEST length, ULONGEST *l, ULONGEST *h) { rp1->idx = find_first_range_overlap (rp1->ranges, rp1->idx, offset1, length); @@ -1306,14 +1315,14 @@ value_contents_all (struct value *value) static void ranges_copy_adjusted (std::vector *dst_range, int dst_bit_offset, const std::vector &src_range, int src_bit_offset, - int bit_length) + unsigned int bit_length) { for (const range &r : src_range) { - ULONGEST h, l; + LONGEST h, l; l = std::max (r.offset, (LONGEST) src_bit_offset); - h = std::min (r.offset + r.length, + h = std::min ((LONGEST) (r.offset + r.length), (LONGEST) src_bit_offset + bit_length); if (l < h) Index: src/gdb/value.h =================================================================== --- src.orig/gdb/value.h +++ src/gdb/value.h @@ -512,7 +512,7 @@ extern int value_bits_synthetic_pointer byte is unavailable. */ extern int value_bytes_available (const struct value *value, - LONGEST offset, LONGEST length); + LONGEST offset, ULONGEST length); /* Given a value, determine whether the contents bits starting at OFFSET and extending for LENGTH bits are available. This returns @@ -520,7 +520,7 @@ extern int value_bytes_available (const bit is unavailable. */ extern int value_bits_available (const struct value *value, - LONGEST offset, LONGEST length); + LONGEST offset, ULONGEST length); /* Like value_bytes_available, but return false if any byte in the whole object is unavailable. */ @@ -534,13 +534,13 @@ extern int value_entirely_unavailable (s LENGTH bytes as unavailable. */ extern void mark_value_bytes_unavailable (struct value *value, - LONGEST offset, LONGEST length); + LONGEST offset, ULONGEST length); /* Mark VALUE's content bits starting at OFFSET and extending for LENGTH bits as unavailable. */ extern void mark_value_bits_unavailable (struct value *value, - LONGEST offset, LONGEST length); + LONGEST offset, ULONGEST length); /* Compare LENGTH bytes of VAL1's contents starting at OFFSET1 with LENGTH bytes of VAL2's contents starting at OFFSET2. From patchwork Mon Jan 23 23:14:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Maciej W. Rozycki" X-Patchwork-Id: 63603 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C5ABC38582B7 for ; Mon, 23 Jan 2023 23:14:29 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-lf1-x12e.google.com (mail-lf1-x12e.google.com [IPv6:2a00:1450:4864:20::12e]) by sourceware.org (Postfix) with ESMTPS id 6D7EF385840D for ; Mon, 23 Jan 2023 23:14:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6D7EF385840D Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=embecosm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=embecosm.com Received: by mail-lf1-x12e.google.com with SMTP id w11so16916916lfu.11 for ; Mon, 23 Jan 2023 15:14:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; h=mime-version:user-agent:references:message-id:in-reply-to:subject :cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=UaY3JJ8IUq0JqmBZTd+OO/66F7sl6U+ZaIQ2SQQVJ7M=; b=QqqLych6rcqxcBEgUbMG6908jW2ETclXb2x73QDrzsbg+2/Ji+1hRtS8cqlF3b588P D1l6u7w5iYJKDR2fKV4u9+R9xl5r4GmA0ZI/pXlUZgsN7uaTt7YpQtNRBPPuNdWWckAe 2x3jaeF4KcgmJxjzRqp/3rsg4Ka+xkVNmr+s6VoVGYYWx6BWquCdKtPyaHXAl7Aa2NQ+ X1BOKjgg7pnHKqh3mHL9j40tdJPSyVyCFgzKxSYqtmM2jgIgW1A7MpL0Pnoct5KGeUpM gFUrmr7iEKr56Vf1BySYmaNvdoUToZbx5QeM5HLeaHPfKH2uxLetwY58jXN0yQ1dUXCL 4wmg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=mime-version:user-agent:references:message-id:in-reply-to:subject :cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=UaY3JJ8IUq0JqmBZTd+OO/66F7sl6U+ZaIQ2SQQVJ7M=; b=MpqQAt8dga3DIfd7EqBI0I31dbnUXyH7pxV3yUjBtamhkVjik38gOlqp3lz4w+dzFz ksVTZRsL7HKBT3xdvtqNnfMTdbeXF7qqHm3OH5eYuNzcTiV1UMa4jY40dxebEOlJ9b4P DnVwCkLZxJjj+AHW172oGBtONkj4ogg+H1a1bpsYfmUKVr4oGlb8cT6skG+GdRXvmtQi 8nnj5+z/7ORgt4k9g82KeMpBb+ip4FfJj5PVZsZwYRkKwcTYUys85TrdoqCi3AgLRVdY j7Nqrk9iWscY8bruoaKk7sJXg5Mq0YrbId0Qm59/j/0AHwiqJtmAfhhytaG9n6imKuPo gvdQ== X-Gm-Message-State: AFqh2krksB/u5FLxXaRd4zvF4SNAuy5kmd+pHfGXfN8I+vZw4e/rNAqF wkrj4h7C0qcu7DmSagXqyvcHgAjyGIHjHlNO X-Google-Smtp-Source: AMrXdXsR7RYUizJYB4pF9+DO5Cu/dX5eowZvCE5D8tHvkQwQlmcikUAD7rBi/TxY4pYxtFOb9C7f1w== X-Received: by 2002:a05:6512:114e:b0:4d0:8469:b196 with SMTP id m14-20020a056512114e00b004d08469b196mr8197057lfg.3.1674515650941; Mon, 23 Jan 2023 15:14:10 -0800 (PST) Received: from [192.168.219.3] ([78.8.192.131]) by smtp.gmail.com with ESMTPSA id w6-20020ac254a6000000b004d5a689c884sm18157lfk.276.2023.01.23.15.14.07 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 23 Jan 2023 15:14:09 -0800 (PST) Date: Mon, 23 Jan 2023 23:14:06 +0000 (GMT) From: "Maciej W. Rozycki" To: gdb-patches@sourceware.org cc: Andrew Burgess , Tom Tromey , Richard Bunt Subject: [PATCH v3 3/5] GDB: Only make data actually retrieved into value history available In-Reply-To: Message-ID: References: User-Agent: Alpine 2.20 (DEB 67 2015-01-07) MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, KAM_ASCII_DIVIDERS, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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: , Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" While it makes sense to allow accessing out-of-bounds elements in the debuggee and see whatever there might happen to be there in memory (we are a debugger and not a programming rules enforcement facility and we want to make people's life easier in chasing bugs), e.g.: (gdb) print one_hundred[-1] $1 = 0 (gdb) print one_hundred[100] $2 = 0 (gdb) we shouldn't really pretend that we have any meaningful data around values recorded in history (what these commands really retrieve are current debuggee memory contents outside the original data accessed, really confusing in my opinion). Add code to mark data around values recorded in history unavailable then: (gdb) print one_hundred[-1] $1 = (gdb) print one_hundred[100] $2 = Adjust `value_entirely_available' accordingly; also zero-length types need special handling in `value_entirely_covered_by_range_vector'. Add a suitable test case, which also covers integer overflows in data location calculation. --- Hi, While at it I noticed that while values recorded in the history are just as one would expect immutable, one can poke at the original data object data in history has originated from by writing to the value in the history, e.g.: (gdb) print -elements 3 -- one_hundred $1 = {0, 1, 2...} (gdb) print -elements 3 -- $1 $2 = {0, 1, 2...} (gdb) set $1[0] = -1 (gdb) print -elements 3 -- $1 $3 = {0, 1, 2...} (gdb) print -elements 3 -- one_hundred $4 = {-1, 1, 2...} (gdb) This is undocumented and I find it surprising to say the least. Shall I file a bug for this phenomenon? LONG_MIN is not used, but I have thought we can have it anyway for consistency and so that people do not try to invent local variants. Maciej Changes from v2: - Correct the copyright year in new files. New change in v2. --- gdb/defs.h | 8 ++ gdb/testsuite/gdb.base/value-history-unavailable.c | 29 +++++++ gdb/testsuite/gdb.base/value-history-unavailable.exp | 73 +++++++++++++++++++ gdb/valarith.c | 15 +++ gdb/value.c | 24 ++++-- 5 files changed, 144 insertions(+), 5 deletions(-) gdb-value-history-unavailable.diff Index: src/gdb/defs.h =================================================================== --- src.orig/gdb/defs.h +++ src/gdb/defs.h @@ -469,6 +469,10 @@ enum val_prettyformat #define LONG_MAX ((long)(ULONG_MAX >> 1)) /* 0x7FFFFFFF for 32-bits */ #endif +#if !defined (LONG_MIN) /* 0x80000000 for 32-bits */ +#define LONG_MIN ((long) (~(long) 0 ^ LONG_MAX)) +#endif + #if !defined (ULONGEST_MAX) #define ULONGEST_MAX (~(ULONGEST)0) /* 0xFFFFFFFFFFFFFFFF for 64-bits */ #endif @@ -477,6 +481,10 @@ enum val_prettyformat #define LONGEST_MAX ((LONGEST)(ULONGEST_MAX >> 1)) #endif +#if !defined (LONGEST_MIN) /* 0x8000000000000000 for 64-bits */ +#define LONGEST_MIN ((LONGEST) (~(LONGEST) 0 ^ LONGEST_MAX)) +#endif + /* * Convert a LONGEST to an int. This is used in contexts (e.g. number of arguments to a function, number in a value history, register number, etc.) where the value must not be larger than can fit in an int. */ Index: src/gdb/testsuite/gdb.base/value-history-unavailable.c =================================================================== --- /dev/null +++ src/gdb/testsuite/gdb.base/value-history-unavailable.c @@ -0,0 +1,29 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright (C) 2023 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +struct +{ + unsigned char x[1024]; + unsigned char a[1024]; + unsigned char y[1024]; +} a = { {-1} }; + +int +main () +{ + return 0; +} Index: src/gdb/testsuite/gdb.base/value-history-unavailable.exp =================================================================== --- /dev/null +++ src/gdb/testsuite/gdb.base/value-history-unavailable.exp @@ -0,0 +1,73 @@ +# Copyright (C) 2023 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# Test GDB's value availability ranges. + +standard_testfile + +if { [prepare_for_testing "failed to prepare" $testfile $srcfile] } { + return -1 +} + +if ![runto_main] then { + perror "couldn't run to breakpoint" + continue +} + +set target_char_mask [get_valueof "/u" "a.x\[0]" "255" "get target char mask"] +set target_char_bit 0 +for {set i $target_char_mask} {$i > 0} {set i [expr $i >> 1]} { + incr target_char_bit +} +set target_char_rank -1 +for {set i $target_char_bit} {$i > 0} {set i [expr $i >> 1]} { + incr target_char_rank +} + +# Verify accesses to original inferior data. +gdb_test "print a.a" "\\\$2 = '\\\\000' " +gdb_test "print a.a\[-1\]" "\\\$3 = 0 '\\\\000'" +gdb_test "print a.a\[1024\]" "\\\$4 = 0 '\\\\000'" + +# Verify in-range value history accesses. +gdb_test "print \$2" "\\\$5 = '\\\\000' " +gdb_test "print \$2\[0\]" "\\\$6 = 0 '\\\\000'" +gdb_test "print \$2\[1023\]" "\\\$7 = 0 '\\\\000'" + +# Values outside the array recorded will have not been retrieved. +gdb_test "print \$2\[-1\]" "\\\$8 = " +gdb_test "print \$2\[1024\]" "\\\$9 = " +gdb_test "print \$2\[-1LL << 63 - $target_char_rank\]" \ + "\\\$10 = " +gdb_test "print \$2\[(1LL << 63 - $target_char_rank) - 1\]" \ + "\\\$11 = " + +# Accesses through pointers in history go straight to the inferior though. +gdb_test "print \$2\[0\]@1" "\\\$12 = \"\"" +gdb_test "print \$2\[-1\]@1" "\\\$13 = \"\"" +gdb_test "print \$2\[1024\]@1" "\\\$14 = \"\"" + +# Verify out-of-range value history accesses. +gdb_test "print \$2\[(-1LL << 63 - $target_char_rank) - 1\]" \ + "Integer overflow in data location calculation" +gdb_test "print \$2\[(1LL << 63 - $target_char_rank)\]" \ + "Integer overflow in data location calculation" +gdb_test "print \$2\[-1LL << 63\]" \ + "Integer overflow in data location calculation" +gdb_test "print \$2\[(1ULL << 63) - 1\]" \ + "Integer overflow in data location calculation" + +# Sanity-check a copy of an unavailable value. +gdb_test "print \$11" "\\\$15 = " Index: src/gdb/valarith.c =================================================================== --- src.orig/gdb/valarith.c +++ src/gdb/valarith.c @@ -182,6 +182,21 @@ value_subscript (struct value *array, LO } index -= *lowerbound; + + /* Do not try to dereference a pointer to an unavailable value. + Instead mock up a new one and give it the original address. */ + struct type *elt_type = check_typedef (tarray->target_type ()); + LONGEST elt_size = type_length_units (elt_type); + if (!value_lazy (array) + && !value_bytes_available (array, elt_size * index, elt_size)) + { + struct value *val = allocate_value (elt_type); + mark_value_bytes_unavailable (val, 0, elt_size); + VALUE_LVAL (val) = lval_memory; + set_value_address (val, value_address (array) + elt_size * index); + return val; + } + array = value_coerce_array (array); } Index: src/gdb/value.c =================================================================== --- src.orig/gdb/value.c +++ src/gdb/value.c @@ -421,7 +421,9 @@ value_entirely_available (struct value * if (value->lazy) value_fetch_lazy (value); - if (value->unavailable.empty ()) + if (value->unavailable.empty () + || value_bytes_available (value, 0, + value_enclosing_type (value)->length ())) return 1; return 0; } @@ -441,11 +443,12 @@ value_entirely_covered_by_range_vector ( if (ranges.size () == 1) { + ULONGEST length = value_enclosing_type (value)->length (); const struct range &t = ranges[0]; - if (t.offset == 0 - && t.length == (TARGET_CHAR_BIT - * value_enclosing_type (value)->length ())) + if (length == 0) + return t.offset == 0 && t.length == 0; + if (t.offset <= 0 && t.offset + t.length >= TARGET_CHAR_BIT * length) return 1; } @@ -1277,7 +1280,9 @@ require_not_optimized_out (const struct static void require_available (const struct value *value) { - if (!value->unavailable.empty ()) + if (!value->unavailable.empty () + && !value_bytes_available (value, 0, + value_enclosing_type (value)->length ())) throw_error (NOT_AVAILABLE_ERROR, _("value is not available")); } @@ -1950,6 +1955,15 @@ record_latest_value (struct value *val) a value on the value history never changes. */ if (value_lazy (val)) value_fetch_lazy (val); + + /* Don't pretend we have anything available there in the history beyond + the boundaries of the value recorded. It's not like inferior memory + where there is actual stuff underneath. */ + ULONGEST length = value_enclosing_type (val)->length (); + mark_value_bits_unavailable (val, LONGEST_MIN, 0 ^ LONGEST_MIN); + mark_value_bits_unavailable (val, length * TARGET_CHAR_BIT, + LONGEST_MAX - length * TARGET_CHAR_BIT); + /* We preserve VALUE_LVAL so that the user can find out where it was fetched from. This is a bit dubious, because then *&$1 does not just return $1 but the current contents of that location. c'est la vie... */ From patchwork Mon Jan 23 23:14:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Maciej W. Rozycki" X-Patchwork-Id: 63604 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 63D393858C66 for ; Mon, 23 Jan 2023 23:14:46 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-lf1-x12f.google.com (mail-lf1-x12f.google.com [IPv6:2a00:1450:4864:20::12f]) by sourceware.org (Postfix) with ESMTPS id 8AF083858C66 for ; Mon, 23 Jan 2023 23:14:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8AF083858C66 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=embecosm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=embecosm.com Received: by mail-lf1-x12f.google.com with SMTP id g13so20597495lfv.7 for ; Mon, 23 Jan 2023 15:14:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; h=mime-version:user-agent:references:message-id:in-reply-to:subject :cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=unxEyM5WlAy3A+GGF4j+odcNoPQnlYf2+fV4bqVzyEY=; b=ZF5rrc6O0fAtl1rKZNQoKbJ83hQtBO4Ya2f24u49Tik6fvC+b0SeUI6oAPEWsiGXNg b4S910mKwJHqErYnmdB8SZJTx74xtLW5Q7zK4K93DolBxZu+SnljyravYm7vtufKTRUd HmEwA9Y27c8qx3AWOLT+c/pl8bciqgBKsu95lmmPcZXMMy+zVHqLlevz/UHUl+7PwI6J F3NZ2OmxW0ieCKo3RqhgUFwaGo8wuQvSa1PnWw2bwfsfHZEh9FgQwFxG9XheCiD91OoN vG6YDxszLXB9+fsfFJ0zZi1bpCqGUmVPbC9o9u7sTjQpowsuj40Ya7TfNryXvdQisqCd C24w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=mime-version:user-agent:references:message-id:in-reply-to:subject :cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=unxEyM5WlAy3A+GGF4j+odcNoPQnlYf2+fV4bqVzyEY=; b=CfL6/5T0ck+UxBJsmFI2YwQRDfIPhhdsh2ghb5Vj8y8y9QcTj8s/5Rs4I2T3Kkw5jN vUvrTnNNWh1+3ywcGsLXk9MVkT4FiYXKWXs9MrZfyPivWJ89pzHmVJtU06k85zMR2cf+ ON53cGgi2sI8ek2podjaqQ0w5NeuYyLVmguGM8P2ZrRqkiaf68YHO5nZ+2dkvL7FSEDt jyVwIAUlYKS08D0WtOxukLRxqNWaiVp9rLW62IO4FupNXk5+X16FLiNv4PtaSicFWTxD IST1ZP52jc/Sy/E78llTg9vmdFLFTFhGM7jNal63lx0L7kHO80T+DUQ0nyfhOlNqkmgL KWBg== X-Gm-Message-State: AFqh2krikOM9aZ35oJAwoVTMMjXfF2OipTrSxCxdMQKGBfh1kNakoqBa N47XC4yGhe9necv2rKlf0IPdZ5LnDWTexzfS X-Google-Smtp-Source: AMrXdXt0maoAdjR2A8DnCilMCnIrrHSDhys1EaaOKhDhL7O+puB8xS4smWHrCQOK+GMBibgCEWkclQ== X-Received: by 2002:ac2:4c31:0:b0:4b3:9313:b2cf with SMTP id u17-20020ac24c31000000b004b39313b2cfmr6307662lfq.25.1674515666191; Mon, 23 Jan 2023 15:14:26 -0800 (PST) Received: from [192.168.219.3] ([78.8.192.131]) by smtp.gmail.com with ESMTPSA id x17-20020a19f611000000b004d583c83789sm18836lfe.147.2023.01.23.15.14.22 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 23 Jan 2023 15:14:24 -0800 (PST) Date: Mon, 23 Jan 2023 23:14:20 +0000 (GMT) From: "Maciej W. Rozycki" To: gdb-patches@sourceware.org cc: Andrew Burgess , Tom Tromey , Richard Bunt Subject: [PATCH v3 4/5] GDB/testsuite: Add `-nonl' option to `gdb_test' In-Reply-To: Message-ID: References: User-Agent: Alpine 2.20 (DEB 67 2015-01-07) MIME-Version: 1.0 X-Spam-Status: No, score=-2.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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: , Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" Add a `-nonl' option to `gdb_test' making it possible to match output from commands such as `output' that do not produce a new line sequence at the end, e.g.: (gdb) output 0 0(gdb) --- No change from v2. New change in v2. --- gdb/testsuite/lib/gdb.exp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) gdb-test-nonl.diff Index: src/gdb/testsuite/lib/gdb.exp =================================================================== --- src.orig/gdb/testsuite/lib/gdb.exp +++ src/gdb/testsuite/lib/gdb.exp @@ -1395,6 +1395,8 @@ proc gdb_test_multiline { name args } { # have any effect if -prompt is specified. # -lbl specifies that line-by-line matching will be used. # -nopass specifies that a PASS should not be issued. +# -nonl specifies that no \r\n sequence is expected between PATTERN +# and the gdb prompt. # # Returns: # 1 if the test failed, @@ -1410,6 +1412,7 @@ proc gdb_test { args } { {no-prompt-anchor} {lbl} {nopass} + {nonl} } lassign $args command pattern message question response @@ -1424,12 +1427,13 @@ proc gdb_test { args } { } set prompt [fill_in_default_prompt $prompt [expr !${no-prompt-anchor}]] + set nl [expr ${nonl} ? {""} : {"\[\r\n\]+"}] set saw_question 0 set user_code {} lappend user_code { - -re "\[\r\n\]*(?:$pattern)\[\r\n\]+$prompt" { + -re "\[\r\n\]*(?:$pattern)$nl$prompt" { if { $question != "" & !$saw_question} { fail $message } elseif {!$nopass} { From patchwork Mon Jan 23 23:14:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Maciej W. Rozycki" X-Patchwork-Id: 63605 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C07D53858C36 for ; Mon, 23 Jan 2023 23:15:25 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-lf1-x132.google.com (mail-lf1-x132.google.com [IPv6:2a00:1450:4864:20::132]) by sourceware.org (Postfix) with ESMTPS id 861953858D33 for ; Mon, 23 Jan 2023 23:14:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 861953858D33 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=embecosm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=embecosm.com Received: by mail-lf1-x132.google.com with SMTP id bp15so20544673lfb.13 for ; Mon, 23 Jan 2023 15:14:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; h=mime-version:user-agent:references:message-id:in-reply-to:subject :cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=cZKdDhRIDOpdTLjHbkECwKfFNvBGjaf/mzFxq9gAdLM=; b=YXWkkmxOkpFndtokA3N2a/9+4T3AUtbQhCsRck0W0ERp1iRGM7hv3eti7cRTBhHoW0 0EErmjpkwwRlwkJfbzd7pP5c9W+EQun5qLoQ67dCFBm6laFF/8fyNfASuDkBJn8IYOtZ qehn8fj40n3JJSYZ/4ZeJiztvHt3ENrbmrqOkLgeVc+6/K5ckRZo5hTALQey68Xt+tDl iQv1waNPHbIaNk4GWNGPF/HZx09IDtpvo+VbOWcpQIiXlgRFrsbqZchJ9yLz47kye/am GX/s/qIuq8LpxvOsA5SGDjXd1F8ghvrlv6Ar9/z4H33yzSLHJsz6KI6pj0PUyw4DlRse t4dQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=mime-version:user-agent:references:message-id:in-reply-to:subject :cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=cZKdDhRIDOpdTLjHbkECwKfFNvBGjaf/mzFxq9gAdLM=; b=IqQp5rbzkaSKRQNqWKzQiPUfE5/mZSR7Uaspkw2ipB3QQtRxaDGPIswaw4P5daXBTq dFm8hjHm9ZlBB99Ccy3w6MqS3g9U0ikT4RZ7x55T9Ric9jkNVfE7eH3aRb7CAunsON/B P53dfsM9jc84SZjRSPj/hfQ1SDMRYYyAByyEVriRx/KY9SnPKvR2pVLTuWterYB+Wy4h kwLoaeaXxiEPrb1mhNj5FbLZQl/R8eREvI9j14yE9reBiqIv9YYWfNF1ipEiRUT8yjik OzkOXbDlev9+WUe8d6WPC+AlmZXqfE24ltbaDdNj2V1O+lnkbZIbJYKojnCJH0Rp3x/r mdmg== X-Gm-Message-State: AFqh2krhGyuFab9IVdLZJRjWBTLsALD4iC4ZCkQB7Ftwmx50eFetF0VD xRMiVF8b0VIkTf5DwLhrXzyfar2e0c0EG+ys X-Google-Smtp-Source: AMrXdXsT38GTKOo8O7DThEXw2Va0F98sQHjKj5lnlYAxB01axuqLJwacIT+3oRLbxYIaEZTmbGE8Tw== X-Received: by 2002:ac2:5686:0:b0:4cb:20b3:e7f6 with SMTP id 6-20020ac25686000000b004cb20b3e7f6mr5872303lfr.19.1674515697746; Mon, 23 Jan 2023 15:14:57 -0800 (PST) Received: from [192.168.219.3] ([78.8.192.131]) by smtp.gmail.com with ESMTPSA id 3-20020a05651c008300b0027fb7d64775sm39501ljq.62.2023.01.23.15.14.37 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 23 Jan 2023 15:14:45 -0800 (PST) Date: Mon, 23 Jan 2023 23:14:35 +0000 (GMT) From: "Maciej W. Rozycki" To: gdb-patches@sourceware.org cc: Andrew Burgess , Tom Tromey , Richard Bunt Subject: [PATCH v3 5/5] GDB: Introduce limited array lengths while printing values In-Reply-To: Message-ID: References: User-Agent: Alpine 2.20 (DEB 67 2015-01-07) MIME-Version: 1.0 X-Spam-Status: No, score=1.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, KAM_ASCII_DIVIDERS, KAM_SHORT, RCVD_IN_DNSWL_NONE, SCC_10_SHORT_WORD_LINES, SCC_20_SHORT_WORD_LINES, SCC_35_SHORT_WORD_LINES, SCC_5_SHORT_WORD_LINES, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=no autolearn_force=no version=3.4.6 X-Spam-Level: * X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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: , Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" From: Andrew Burgess This commit introduces the idea of loading only part of an array in order to print it, what I call "limited length" arrays. The motivation behind this work is to make it possible to print slices of very large arrays, where very large means bigger than `max-value-size'. Consider this GDB session with the current GDB: (gdb) set max-value-size 100 (gdb) p large_1d_array value requires 400 bytes, which is more than max-value-size (gdb) p -elements 10 -- large_1d_array value requires 400 bytes, which is more than max-value-size notice that the request to print 10 elements still fails, even though 10 elements should be less than the max-value-size. With a patched version of GDB: (gdb) p -elements 10 -- large_1d_array $1 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9...} So now the print has succeeded. It also has loaded `max-value-size' worth of data into value history, so the recorded value can be accessed consistently: (gdb) p -elements 10 -- $1 $2 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9...} (gdb) p $1 $3 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, } (gdb) Accesses with other languages work similarly, although for Ada only C-style [] array element/dimension accesses use history. For both Ada and Fortran () array element/dimension accesses go straight to the inferior, bypassing the value history just as with C pointers. Co-Authored-By: Maciej W. Rozycki --- Changes from v2: - Adjust for the `value_copy' update in 1/5. - Remove the handling of (dst_len != src_len) in `value_copy' where `dst_len' is always the same as `src_len' (from the corresponding settings in `val' assigned to from `arg' right beforehand). - Switch Ada and Fortran test cases to using `allow_ada_tests' and `allow_fortran_tests' respectively. - Fix a couple of formatting issues involving spaces used instead of tabs. Changes from v1: - Load `max-value-size' worth data into the value history for limited length accesses and mark the area beyond unavailable. - Handle the `output' command. - Expand test coverage. --- gdb/NEWS | 6 gdb/doc/gdb.texinfo | 9 gdb/f-valprint.c | 32 ++- gdb/printcmd.c | 16 + gdb/testsuite/gdb.ada/limited-length.exp | 264 +++++++++++++++++++++++++++ gdb/testsuite/gdb.ada/limited-length/foo.adb | 37 +++ gdb/testsuite/gdb.ada/limited-length/pck.adb | 25 ++ gdb/testsuite/gdb.ada/limited-length/pck.ads | 21 ++ gdb/testsuite/gdb.base/limited-length.c | 48 ++++ gdb/testsuite/gdb.base/limited-length.exp | 242 ++++++++++++++++++++++++ gdb/testsuite/gdb.fortran/limited-length.exp | 220 ++++++++++++++++++++++ gdb/testsuite/gdb.fortran/limited-length.f90 | 39 +++ gdb/valprint.c | 10 - gdb/value.c | 186 +++++++++++++++++-- gdb/value.h | 17 + 15 files changed, 1147 insertions(+), 25 deletions(-) create mode 100644 gdb/testsuite/gdb.ada/limited-length.exp create mode 100644 gdb/testsuite/gdb.ada/limited-length/foo.adb create mode 100644 gdb/testsuite/gdb.ada/limited-length/pck.adb create mode 100644 gdb/testsuite/gdb.ada/limited-length/pck.ads create mode 100644 gdb/testsuite/gdb.base/limited-length.c create mode 100644 gdb/testsuite/gdb.base/limited-length.exp create mode 100644 gdb/testsuite/gdb.fortran/limited-length.exp create mode 100644 gdb/testsuite/gdb.fortran/limited-length.f90 gdb-aburgess-limited-length-array.diff Index: src/gdb/NEWS =================================================================== --- src.orig/gdb/NEWS +++ src/gdb/NEWS @@ -354,6 +354,12 @@ GDB now supports floating-point on Loong Disabling this can cause a performance penalty when there are a lot of symbols to load, but is useful for debugging purposes. +* The 'set print elements' setting now helps when printing large arrays. + If an array would otherwise exceed max-value-size, but 'print elements' + is set such that the size of elements to print is less than or equal + to 'max-value-size', GDB will now still print the array, however only + 'max-value-size' worth of data will be added into the value history. + * New commands maint set backtrace-on-fatal-signal on|off Index: src/gdb/doc/gdb.texinfo =================================================================== --- src.orig/gdb/doc/gdb.texinfo +++ src/gdb/doc/gdb.texinfo @@ -11756,6 +11756,14 @@ When @value{GDBN} starts, this limit is Setting @var{number-of-elements} to @code{unlimited} or zero means that the number of elements to print is unlimited. +When printing very large arrays, whose size is greater than +@code{max-value-size} (@pxref{set max-value-size,,max-value-size}), +if the @code{print elements} is set such that the size of the elements +being printed is less than or equal to @code{max-value-size}, then +@value{GDBN} will print the array (up to the @code{print elements} limit), +and only @code{max-value-size} worth of data will be added into the value +history (@pxref{Value History, ,Value History}). + @item show print elements Display the number of elements of a large array that @value{GDBN} will print. @@ -14171,6 +14179,7 @@ may indicate a value that is incorrectly @value{GDBN} to try and allocate an overly large amount of memory. @table @code +@anchor{set max-value-size} @kindex set max-value-size @item set max-value-size @var{bytes} @itemx set max-value-size unlimited Index: src/gdb/f-valprint.c =================================================================== --- src.orig/gdb/f-valprint.c +++ src/gdb/f-valprint.c @@ -261,10 +261,20 @@ class fortran_array_printer_impl : publi size_t dim_indx = m_dimension - 1; struct type *elt_type_prev = m_elt_type_prev; LONGEST elt_off_prev = m_elt_off_prev; - bool repeated = (m_options->repeat_count_threshold < UINT_MAX - && elt_type_prev != nullptr - && value_contents_eq (m_val, elt_off_prev, m_val, elt_off, - elt_type->length ())); + bool repeated = false; + + if (m_options->repeat_count_threshold < UINT_MAX + && elt_type_prev != nullptr) + { + struct value *e_val = value_from_component (m_val, elt_type, elt_off); + struct value *e_prev = value_from_component (m_val, elt_type, + elt_off_prev); + repeated = ((value_entirely_available (e_prev) + && value_entirely_available (e_val) + && value_contents_eq (e_prev, e_val)) + || (value_entirely_unavailable (e_prev) + && value_entirely_unavailable (e_val))); + } if (repeated) m_nrepeats++; @@ -333,7 +343,7 @@ class fortran_array_printer_impl : publi have been sliced and we do not want to compare any memory contents present between the slices requested. */ bool - dimension_contents_eq (const struct value *val, struct type *type, + dimension_contents_eq (struct value *val, struct type *type, LONGEST offset1, LONGEST offset2) { if (type->code () == TYPE_CODE_ARRAY @@ -362,8 +372,16 @@ class fortran_array_printer_impl : publi return true; } else - return value_contents_eq (val, offset1, val, offset2, - type->length ()); + { + struct value *e_val1 = value_from_component (val, type, offset1); + struct value *e_val2 = value_from_component (val, type, offset2); + + return ((value_entirely_available (e_val1) + && value_entirely_available (e_val2) + && value_contents_eq (e_val1, e_val2)) + || (value_entirely_unavailable (e_val1) + && value_entirely_unavailable (e_val2))); + } } /* The number of elements printed so far. */ Index: src/gdb/printcmd.c =================================================================== --- src.orig/gdb/printcmd.c +++ src/gdb/printcmd.c @@ -1242,6 +1242,11 @@ print_command_parse_format (const char * void print_value (value *val, const value_print_options &opts) { + /* This setting allows large arrays to be printed by limiting the + number of elements that are loaded into GDB's memory; we only + need to load as many array elements as we plan to print. */ + scoped_array_length_limiting limit_large_arrays (opts.print_max); + int histindex = record_latest_value (val); annotate_value_history_begin (histindex, value_type (val)); @@ -1301,6 +1306,11 @@ process_print_command_args (const char * if (exp != nullptr && *exp) { + /* This setting allows large arrays to be printed by limiting the + number of elements that are loaded into GDB's memory; we only + need to load as many array elements as we plan to print. */ + scoped_array_length_limiting limit_large_arrays (print_opts->print_max); + /* VOIDPRINT is true to indicate that we do want to print a void value, so invert it for parse_expression. */ expression_up expr = parse_expression (exp, nullptr, !voidprint); @@ -1489,6 +1499,12 @@ output_command (const char *exp, int fro get_formatted_print_options (&opts, format); opts.raw = fmt.raw; + + /* This setting allows large arrays to be printed by limiting the + number of elements that are loaded into GDB's memory; we only + need to load as many array elements as we plan to print. */ + scoped_array_length_limiting limit_large_arrays (opts.print_max); + print_formatted (val, fmt.size, &opts, gdb_stdout); annotate_value_end (); Index: src/gdb/testsuite/gdb.ada/limited-length.exp =================================================================== --- /dev/null +++ src/gdb/testsuite/gdb.ada/limited-length.exp @@ -0,0 +1,264 @@ +# Copyright 2023 Free Software Foundation, Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +load_lib "ada.exp" + +require allow_ada_tests + +standard_ada_testfile foo + +if {[gdb_compile_ada "${srcfile}" "${binfile}" executable \ + [list debug ]] != "" } { + return -1 +} + +clean_restart ${testfile} + +set bp_location [gdb_get_line_number "STOP" ${testdir}/foo.adb] +if {![runto "foo.adb:$bp_location"]} { + perror "Couldn't run ${testfile}" + return +} + +with_test_prefix "with standard max-value size" { + gdb_test "print Large_1d_Array" \ + " = \\(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,\ + 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,\ + 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,\ + 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,\ + 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,\ + 61, 62, 63, 64\\)" + gdb_test -nonl "output Large_1d_Array" \ + "\\(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,\ + 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,\ + 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,\ + 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,\ + 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,\ + 61, 62, 63, 64\\)" + gdb_test "print Large_3d_Array" \ + " = \\(\\(\\(1, 2, 3, 4\\), \\(5, 6, 7, 8\\),\ + \\(9, 10, 11, 12\\), \\(13, 14, 15, 16\\)\\),\ + \\(\\(17, 18, 19, 20\\), \\(21, 22, 23, 24\\),\ + \\(25, 26, 27, 28\\), \\(29, 30, 31, 32\\)\\),\ + \\(\\(33, 34, 35, 36\\), \\(37, 38, 39, 40\\),\ + \\(41, 42, 43, 44\\), \\(45, 46, 47, 48\\)\\),\ + \\(\\(49, 50, 51, 52\\), \\(53, 54, 55, 56\\),\ + \\(57, 58, 59, 60\\), \\(61, 62, 63, 64\\)\\)\\)" + gdb_test -nonl "output Large_3d_Array" \ + "\\(\\(\\(1, 2, 3, 4\\), \\(5, 6, 7, 8\\),\ + \\(9, 10, 11, 12\\), \\(13, 14, 15, 16\\)\\),\ + \\(\\(17, 18, 19, 20\\), \\(21, 22, 23, 24\\),\ + \\(25, 26, 27, 28\\), \\(29, 30, 31, 32\\)\\),\ + \\(\\(33, 34, 35, 36\\), \\(37, 38, 39, 40\\),\ + \\(41, 42, 43, 44\\), \\(45, 46, 47, 48\\)\\),\ + \\(\\(49, 50, 51, 52\\), \\(53, 54, 55, 56\\),\ + \\(57, 58, 59, 60\\), \\(61, 62, 63, 64\\)\\)\\)" +} + +# Set the max-value-size so we can only print 33 elements. +set elements 33 +set elem_size [get_valueof "/d" "(Large_1d_Array(1)'Size + 7) / 8" "*unknown*"] +gdb_test_no_output "set max-value-size [expr $elem_size * $elements]" + +with_test_prefix "with reduced max-value size" { + gdb_test "print Large_1d_Array" \ + "value of type `.*' requires $decimal bytes,\ + which is more than max-value-size" + gdb_test "output Large_1d_Array" \ + "value of type `.*' requires $decimal bytes,\ + which is more than max-value-size" + gdb_test "print Large_3d_Array" \ + "value of type `.*' requires $decimal bytes,\ + which is more than max-value-size" + gdb_test "output Large_3d_Array" \ + "value of type `.*' requires $decimal bytes,\ + which is more than max-value-size" +} + +with_test_prefix "with reduced print -elements flag" { + gdb_test "print -elements 2 -- Large_1d_Array" \ + " = \\(1, 2\\.\\.\\.\\)" + gdb_test "print -elements 2 -- Large_3d_Array" \ + " = \\(\\(\\(1, 2\\.\\.\\.\\), \\(5, 6\\.\\.\\.\\)\\.\\.\\.\\),\ + \\(\\(17, 18\\.\\.\\.\\),\ + \\(21, 22\\.\\.\\.\\)\\.\\.\\.\\)\\.\\.\\.\\)" +} + +gdb_test_no_output "set print elements 2" + +with_test_prefix "with reduced print elements" { + gdb_test "print Large_1d_Array" \ + " = \\(1, 2\\.\\.\\.\\)" + gdb_test -nonl "output Large_1d_Array" \ + "\\(1, 2\\.\\.\\.\\)" + + gdb_test "print \$" \ + " = \\(1, 2\\.\\.\\.\\)" \ + "print Large_1d_Array from history" + gdb_test -nonl "output \$\$" \ + "\\(1, 2\\.\\.\\.\\)" \ + "output Large_1d_Array from history" + + gdb_test "print Large_3d_Array" \ + " = \\(\\(\\(1, 2\\.\\.\\.\\), \\(5, 6\\.\\.\\.\\)\\.\\.\\.\\),\ + \\(\\(17, 18\\.\\.\\.\\),\ + \\(21, 22\\.\\.\\.\\)\\.\\.\\.\\)\\.\\.\\.\\)" + gdb_test -nonl "output Large_3d_Array" \ + "\\(\\(\\(1, 2\\.\\.\\.\\), \\(5, 6\\.\\.\\.\\)\\.\\.\\.\\),\ + \\(\\(17, 18\\.\\.\\.\\),\ + \\(21, 22\\.\\.\\.\\)\\.\\.\\.\\)\\.\\.\\.\\)" + + gdb_test "print \$" \ + " = \\(\\(\\(1, 2\\.\\.\\.\\), \\(5, 6\\.\\.\\.\\)\\.\\.\\.\\),\ + \\(\\(17, 18\\.\\.\\.\\),\ + \\(21, 22\\.\\.\\.\\)\\.\\.\\.\\)\\.\\.\\.\\)" \ + "print Large_3d_Array from history" + gdb_test -nonl "output \$\$" \ + "\\(\\(\\(1, 2\\.\\.\\.\\), \\(5, 6\\.\\.\\.\\)\\.\\.\\.\\),\ + \\(\\(17, 18\\.\\.\\.\\),\ + \\(21, 22\\.\\.\\.\\)\\.\\.\\.\\)\\.\\.\\.\\)" \ + "output Large_3d_Array from history" +} + +gdb_test_no_output "set print elements $elements" + +with_test_prefix "with print elements matching max-value size" { + gdb_test "print \$\$2" \ + " = \\(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,\ + 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,\ + 25, 26, 27, 28, 29, 30, 31, 32, 33\\.\\.\\.\\)" \ + "print Large_1d_Array from history" + gdb_test -nonl "output \$\$3" \ + "\\(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,\ + 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,\ + 25, 26, 27, 28, 29, 30, 31, 32, 33\\.\\.\\.\\)" \ + "output Large_1d_Array from history" + + gdb_test "print \$\$2" \ + " = \\(\\(\\(1, 2, 3, 4\\), \\(5, 6, 7, 8\\),\ + \\(9, 10, 11, 12\\), \\(13, 14, 15, 16\\)\\),\ + \\(\\(17, 18, 19, 20\\), \\(21, 22, 23, 24\\),\ + \\(25, 26, 27, 28\\), \\(29, 30, 31, 32\\)\\),\ + \\(\\(33(?:, )\{3\}\\)(?:,\ + \\((?:, )\{3\}\\))\{3\}\\),\ + \\(\\((?:, )\{3\}\\)(?:,\ + \\((?:, )\{3\}\\))\{3\}\\)\\)" \ + "print Large_3d_Array from history" + gdb_test -nonl "output \$\$3" \ + "\\(\\(\\(1, 2, 3, 4\\), \\(5, 6, 7, 8\\),\ + \\(9, 10, 11, 12\\), \\(13, 14, 15, 16\\)\\),\ + \\(\\(17, 18, 19, 20\\), \\(21, 22, 23, 24\\),\ + \\(25, 26, 27, 28\\), \\(29, 30, 31, 32\\)\\),\ + \\(\\(33(?:, )\{3\}\\)(?:,\ + \\((?:, )\{3\}\\))\{3\}\\),\ + \\(\\((?:, )\{3\}\\)(?:,\ + \\((?:, )\{3\}\\))\{3\}\\)\\)" \ + "output Large_3d_Array from history" +} + +gdb_test_no_output "set max-value-size unlimited" +gdb_test_no_output "set print elements unlimited" +gdb_test_no_output "set print repeats 2" + +with_test_prefix "with unlimited print elements" { + gdb_test "print \$\$" \ + " = \\(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,\ + 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,\ + 25, 26, 27, 28, 29, 30, 31, 32, 33,\ + \\)" \ + "print Large_1d_Array from history" + gdb_test -nonl "output \$\$2" \ + "\\(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,\ + 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,\ + 25, 26, 27, 28, 29, 30, 31, 32, 33,\ + \\)" \ + "output Large_1d_Array from history" + + gdb_test "print \$\$" \ + " = \\(\\(\\(1, 2, 3, 4\\), \\(5, 6, 7, 8\\),\ + \\(9, 10, 11, 12\\), \\(13, 14, 15, 16\\)\\),\ + \\(\\(17, 18, 19, 20\\), \\(21, 22, 23, 24\\),\ + \\(25, 26, 27, 28\\), \\(29, 30, 31, 32\\)\\),\ + \\(\\(33, \\),\ + \\( \\) \\),\ + \\(\\( \\)\ + \\)\\)" \ + "print Large_3d_Array from history" + gdb_test -nonl "output \$\$2" \ + "\\(\\(\\(1, 2, 3, 4\\), \\(5, 6, 7, 8\\),\ + \\(9, 10, 11, 12\\), \\(13, 14, 15, 16\\)\\),\ + \\(\\(17, 18, 19, 20\\), \\(21, 22, 23, 24\\),\ + \\(25, 26, 27, 28\\), \\(29, 30, 31, 32\\)\\),\ + \\(\\(33, \\),\ + \\( \\) \\),\ + \\(\\( \\) \\)\\)" \ + "output Large_3d_Array from history" + + gdb_test "print \$\[2\]" \ + " = \\(\\(17, 18, 19, 20\\), \\(21, 22, 23, 24\\),\ + \\(25, 26, 27, 28\\), \\(29, 30, 31, 32\\)\\)" \ + "print available Large_3d_Array row from history" + gdb_test -nonl "output \$\$\[2\]" \ + "\\(\\(17, 18, 19, 20\\), \\(21, 22, 23, 24\\),\ + \\(25, 26, 27, 28\\), \\(29, 30, 31, 32\\)\\)" \ + "output available Large_3d_Array row from history" + + gdb_test "print \$\$\[3\]" \ + " = \\(\\(33, \\),\ + \\( \\) \\)" \ + "print partially available Large_3d_Array row from history" + gdb_test -nonl "output \$\$2\[3\]" \ + "\\(\\(33, \\),\ + \\( \\) \\)" \ + "output partially available Large_3d_Array row from history" + + # These go straigth to the inferior. + gdb_test "print \$\$2(3)" \ + " = \\(\\(33, 34, 35, 36\\), \\(37, 38, 39, 40\\),\ + \\(41, 42, 43, 44\\), \\(45, 46, 47, 48\\)\\)" \ + "print partially available Large_3d_Array row bypassing history" + gdb_test -nonl "output \$\$3(3)" \ + "\\(\\(33, 34, 35, 36\\), \\(37, 38, 39, 40\\),\ + \\(41, 42, 43, 44\\), \\(45, 46, 47, 48\\)\\)" \ + "output partially available Large_3d_Array row bypassing history" + + gdb_test "print \$\$3\[4\]" \ + " = " \ + "print unavailable Large_3d_Array row from history" + gdb_test -nonl "output \$\$4\[4\]" \ + "" \ + "output unavailable Large_3d_Array row from history" + + gdb_test "print \$\$4\[3\]\[1\]\[1\]" \ + " = 33" \ + "print available Large_3d_Array element from history" + gdb_test -nonl "output \$\$5\[3\]\[1\]\[1\]" \ + "33" \ + "output available Large_3d_Array element from history" + + gdb_test "print \$\$5\[3\]\[1\]\[2\]" \ + " = " \ + "print unavailable Large_3d_Array element from history" + gdb_test -nonl "output \$\$6\[3\]\[1\]\[2\]" \ + "" \ + "output unavailable Large_3d_Array element from history" + + gdb_test "print \$\$6\[3\]\[1\]\[1\] + \$\$6\[3\]\[1\]\[2\]" \ + "value is not available" \ + "print expression referring unavailable element from history" + gdb_test "output \$\$6\[3\]\[1\]\[1\] + \$\$6\[3\]\[1\]\[2\]" \ + "value is not available" \ + "output expression referring unavailable element from history" +} Index: src/gdb/testsuite/gdb.ada/limited-length/foo.adb =================================================================== --- /dev/null +++ src/gdb/testsuite/gdb.ada/limited-length/foo.adb @@ -0,0 +1,37 @@ +-- This testcase is part of GDB, the GNU debugger. +-- +-- Copyright 2023 Free Software Foundation, Inc. +-- +-- This program is free software; you can redistribute it and/or modify +-- it under the terms of the GNU General Public License as published by +-- the Free Software Foundation; either version 3 of the License, or +-- (at your option) any later version. +-- +-- This program is distributed in the hope that it will be useful, +-- but WITHOUT ANY WARRANTY; without even the implied warranty of +-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +-- GNU General Public License for more details. +-- +-- You should have received a copy of the GNU General Public License +-- along with this program. If not, see . + +with Pck; use Pck; + +procedure Foo is + Large_1d_Array : array (1..64) of Integer; + Large_3d_Array : array (1..4,1..4,1..4) of Integer; + Count : Integer := 1; +begin + for i in 1 .. 4 loop + for j in 1 .. 4 loop + for k in 1 .. 4 loop + Large_1d_Array (Count) := Count; + Large_3d_Array (i,j,k) := Count; + Count := Count + 1; + end loop; + end loop; + end loop; + Do_Nothing (Large_1d_Array'Address); + Do_Nothing (Large_3d_Array'Address); -- STOP +end Foo; + Index: src/gdb/testsuite/gdb.ada/limited-length/pck.adb =================================================================== --- /dev/null +++ src/gdb/testsuite/gdb.ada/limited-length/pck.adb @@ -0,0 +1,25 @@ +-- This testcase is part of GDB, the GNU debugger. +-- +-- Copyright 2023 Free Software Foundation, Inc. +-- +-- This program is free software; you can redistribute it and/or modify +-- it under the terms of the GNU General Public License as published by +-- the Free Software Foundation; either version 3 of the License, or +-- (at your option) any later version. +-- +-- This program is distributed in the hope that it will be useful, +-- but WITHOUT ANY WARRANTY; without even the implied warranty of +-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +-- GNU General Public License for more details. +-- +-- You should have received a copy of the GNU General Public License +-- along with this program. If not, see . + +package body Pck is + + procedure Do_Nothing (A : System.Address) is + begin + null; + end Do_Nothing; + +end Pck; Index: src/gdb/testsuite/gdb.ada/limited-length/pck.ads =================================================================== --- /dev/null +++ src/gdb/testsuite/gdb.ada/limited-length/pck.ads @@ -0,0 +1,21 @@ +-- This testcase is part of GDB, the GNU debugger. +-- +-- Copyright 2023 Free Software Foundation, Inc. +-- +-- This program is free software; you can redistribute it and/or modify +-- it under the terms of the GNU General Public License as published by +-- the Free Software Foundation; either version 3 of the License, or +-- (at your option) any later version. +-- +-- This program is distributed in the hope that it will be useful, +-- but WITHOUT ANY WARRANTY; without even the implied warranty of +-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +-- GNU General Public License for more details. +-- +-- You should have received a copy of the GNU General Public License +-- along with this program. If not, see . + +with System; +package Pck is + procedure Do_Nothing (A : System.Address); +end Pck; Index: src/gdb/testsuite/gdb.base/limited-length.c =================================================================== --- /dev/null +++ src/gdb/testsuite/gdb.base/limited-length.c @@ -0,0 +1,48 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright (C) 2023 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +int large_1d_array[] = { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, + 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, + 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, + 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, + 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, + 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, + 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, + 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, + 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, + 90, 91, 92, 93, 94, 95, 96, 97, 98, 99 +}; + +int large_2d_array[][10] = { + {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, + {10, 11, 12, 13, 14, 15, 16, 17, 18, 19}, + {20, 21, 22, 23, 24, 25, 26, 27, 28, 29}, + {30, 31, 32, 33, 34, 35, 36, 37, 38, 39}, + {40, 41, 42, 43, 44, 45, 46, 47, 48, 49}, + {50, 51, 52, 53, 54, 55, 56, 57, 58, 59}, + {60, 61, 62, 63, 64, 65, 66, 67, 68, 69}, + {70, 71, 72, 73, 74, 75, 76, 77, 78, 79}, + {80, 81, 82, 83, 84, 85, 86, 87, 88, 89}, + {90, 91, 92, 93, 94, 95, 96, 97, 98, 99} +}; + +int +main () +{ + return 0; +} Index: src/gdb/testsuite/gdb.base/limited-length.exp =================================================================== --- /dev/null +++ src/gdb/testsuite/gdb.base/limited-length.exp @@ -0,0 +1,242 @@ +# Copyright 2023 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# Test GDB's limited array printing. + +standard_testfile + +if {[prepare_for_testing "failed to prepare" $testfile $srcfile]} { + return -1 +} + +if {![runto_main]} { + perror "couldn't run to breakpoint" + continue +} + +with_test_prefix "with standard max-value size" { + gdb_test "print large_1d_array" \ + " = \\\{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,\ + 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,\ + 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,\ + 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,\ + 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,\ + 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71,\ + 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83,\ + 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,\ + 96, 97, 98, 99\\\}" + gdb_test -nonl "output large_1d_array" \ + "\\\{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,\ + 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,\ + 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,\ + 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,\ + 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,\ + 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71,\ + 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83,\ + 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,\ + 96, 97, 98, 99\\\}" + gdb_test "print large_2d_array" \ + " = \\\{\\\{0, 1, 2, 3, 4, 5, 6, 7, 8, 9\\\},\ + \\\{10, 11, 12, 13, 14, 15, 16, 17, 18, 19\\\},\ + \\\{20, 21, 22, 23, 24, 25, 26, 27, 28, 29\\\},\ + \\\{30, 31, 32, 33, 34, 35, 36, 37, 38, 39\\\},\ + \\\{40, 41, 42, 43, 44, 45, 46, 47, 48, 49\\\},\ + \\\{50, 51, 52, 53, 54, 55, 56, 57, 58, 59\\\},\ + \\\{60, 61, 62, 63, 64, 65, 66, 67, 68, 69\\\},\ + \\\{70, 71, 72, 73, 74, 75, 76, 77, 78, 79\\\},\ + \\\{80, 81, 82, 83, 84, 85, 86, 87, 88, 89\\\},\ + \\\{90, 91, 92, 93, 94, 95, 96, 97, 98, 99\\\}\\\}" + gdb_test -nonl "output large_2d_array" \ + "\\\{\\\{0, 1, 2, 3, 4, 5, 6, 7, 8, 9\\\},\ + \\\{10, 11, 12, 13, 14, 15, 16, 17, 18, 19\\\},\ + \\\{20, 21, 22, 23, 24, 25, 26, 27, 28, 29\\\},\ + \\\{30, 31, 32, 33, 34, 35, 36, 37, 38, 39\\\},\ + \\\{40, 41, 42, 43, 44, 45, 46, 47, 48, 49\\\},\ + \\\{50, 51, 52, 53, 54, 55, 56, 57, 58, 59\\\},\ + \\\{60, 61, 62, 63, 64, 65, 66, 67, 68, 69\\\},\ + \\\{70, 71, 72, 73, 74, 75, 76, 77, 78, 79\\\},\ + \\\{80, 81, 82, 83, 84, 85, 86, 87, 88, 89\\\},\ + \\\{90, 91, 92, 93, 94, 95, 96, 97, 98, 99\\\}\\\}" +} + +# Set the max-value-size so we can only print 51 elements. +set elements 51 +set int_size [get_valueof "/d" "sizeof(large_1d_array\[0\])" "*unknown*"] +gdb_test_no_output "set max-value-size [expr $int_size * $elements]" + +with_test_prefix "with reduced max-value size" { + gdb_test "print large_1d_array" \ + "\r\nvalue requires $decimal bytes, which is more than max-value-size" + gdb_test "output large_1d_array" \ + "\r\nvalue requires $decimal bytes, which is more than max-value-size" + gdb_test "print large_2d_array" \ + "\r\nvalue requires $decimal bytes, which is more than max-value-size" + gdb_test "output large_2d_array" \ + "\r\nvalue requires $decimal bytes, which is more than max-value-size" +} + +gdb_test_no_output "set print elements 3" + +with_test_prefix "with reduced print elements" { + gdb_test "print large_1d_array" \ + " = \\\{0, 1, 2\\.\\.\\.\\\}" + gdb_test -nonl "output large_1d_array" \ + "\\\{0, 1, 2\\.\\.\\.\\\}" + + gdb_test "print \$" \ + " = \\\{0, 1, 2\\.\\.\\.\\\}" \ + "print large_1d_array from history" + gdb_test -nonl "output \$\$" \ + "\\\{0, 1, 2\\.\\.\\.\\\}" \ + "output large_1d_array from history" + + gdb_test "print large_2d_array" \ + " = \\\{\\\{0, 1, 2\\.\\.\\.\\\}, \\\{10, 11, 12\\.\\.\\.\\\},\ + \\\{20, 21, 22\\.\\.\\.\\\}\\.\\.\\.\\\}" + gdb_test -nonl "output large_2d_array" \ + "\\\{\\\{0, 1, 2\\.\\.\\.\\\}, \\\{10, 11, 12\\.\\.\\.\\\},\ + \\\{20, 21, 22\\.\\.\\.\\\}\\.\\.\\.\\\}" + + gdb_test "print \$" \ + " = \\\{\\\{0, 1, 2\\.\\.\\.\\\}, \\\{10, 11, 12\\.\\.\\.\\\},\ + \\\{20, 21, 22\\.\\.\\.\\\}\\.\\.\\.\\\}" \ + "print large_2d_array from history" + gdb_test -nonl "output \$\$" \ + "\\\{\\\{0, 1, 2\\.\\.\\.\\\}, \\\{10, 11, 12\\.\\.\\.\\\},\ + \\\{20, 21, 22\\.\\.\\.\\\}\\.\\.\\.\\\}" \ + "output large_2d_array from history" +} + +gdb_test_no_output "set print elements $elements" + +with_test_prefix "with print elements matching max-value size" { + gdb_test "print \$\$2" \ + " = \\\{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,\ + 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,\ + 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,\ + 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,\ + 48, 49, 50\\.\\.\\.\\\}" \ + "print large_1d_array from history" + gdb_test -nonl "output \$\$3" \ + "\\\{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,\ + 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,\ + 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,\ + 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,\ + 48, 49, 50\\.\\.\\.\\\}" \ + "output large_1d_array from history" + + gdb_test "print \$\$2" \ + " = \\\{\\\{0, 1, 2, 3, 4, 5, 6, 7, 8, 9\\\},\ + \\\{10, 11, 12, 13, 14, 15, 16, 17, 18, 19\\\},\ + \\\{20, 21, 22, 23, 24, 25, 26, 27, 28, 29\\\},\ + \\\{30, 31, 32, 33, 34, 35, 36, 37, 38, 39\\\},\ + \\\{40, 41, 42, 43, 44, 45, 46, 47, 48, 49\\\},\ + \\\{50(?:, )\{9\}\\\}(?:,\ + \\\{(?:, )\{9\}\\\})\{4\}\\\}" \ + "print large_2d_array from history" + gdb_test -nonl "output \$\$3" \ + "\\\{\\\{0, 1, 2, 3, 4, 5, 6, 7, 8, 9\\\},\ + \\\{10, 11, 12, 13, 14, 15, 16, 17, 18, 19\\\},\ + \\\{20, 21, 22, 23, 24, 25, 26, 27, 28, 29\\\},\ + \\\{30, 31, 32, 33, 34, 35, 36, 37, 38, 39\\\},\ + \\\{40, 41, 42, 43, 44, 45, 46, 47, 48, 49\\\},\ + \\\{50(?:, )\{9\}\\\}(?:,\ + \\\{(?:, )\{9\}\\\})\{4\}\\\}" \ + "output large_2d_array from history" +} + +gdb_test_no_output "set max-value-size unlimited" +gdb_test_no_output "set print elements unlimited" +gdb_test_no_output "set print repeats 3" + +with_test_prefix "with unlimited print elements" { + gdb_test "print \$\$" \ + " = \\\{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,\ + 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,\ + 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,\ + 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,\ + 48, 49, 50, \\\}" \ + "print large_1d_array from history" + gdb_test -nonl "output \$\$2" \ + "\\\{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,\ + 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,\ + 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,\ + 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,\ + 48, 49, 50, \\\}" \ + "output large_1d_array from history" + + gdb_test "print \$\$" \ + " = \\\{\\\{0, 1, 2, 3, 4, 5, 6, 7, 8, 9\\\},\ + \\\{10, 11, 12, 13, 14, 15, 16, 17, 18, 19\\\},\ + \\\{20, 21, 22, 23, 24, 25, 26, 27, 28, 29\\\},\ + \\\{30, 31, 32, 33, 34, 35, 36, 37, 38, 39\\\},\ + \\\{40, 41, 42, 43, 44, 45, 46, 47, 48, 49\\\},\ + \\\{50, \\\},\ + \\\{ \\\}\ + \\\}" \ + "print large_2d_array from history" + gdb_test -nonl "output \$\$2" \ + "\\\{\\\{0, 1, 2, 3, 4, 5, 6, 7, 8, 9\\\},\ + \\\{10, 11, 12, 13, 14, 15, 16, 17, 18, 19\\\},\ + \\\{20, 21, 22, 23, 24, 25, 26, 27, 28, 29\\\},\ + \\\{30, 31, 32, 33, 34, 35, 36, 37, 38, 39\\\},\ + \\\{40, 41, 42, 43, 44, 45, 46, 47, 48, 49\\\},\ + \\\{50, \\\},\ + \\\{ \\\}\ + \\\}" \ + "output large_2d_array from history" + + gdb_test "print \$\[4\]" \ + " = \\\{40, 41, 42, 43, 44, 45, 46, 47, 48, 49\\\}" \ + "print available large_2d_array row from history" + gdb_test -nonl "output \$\$\[4\]" \ + "\\\{40, 41, 42, 43, 44, 45, 46, 47, 48, 49\\\}" \ + "output available large_2d_array row from history" + + gdb_test "print \$\$\[5\]" \ + " = \\\{50, \\\}" \ + "print partially available large_2d_array row from history" + gdb_test -nonl "output \$\$2\[5\]" \ + "\\\{50, \\\}" \ + "output partially available large_2d_array row from history" + + gdb_test "print \$\$2\[6\]" \ + " = " \ + "print unavailable large_2d_array row from history" + gdb_test -nonl "output \$\$3\[6\]" \ + "" \ + "output unavailable large_2d_array row from history" + + gdb_test "print \$\$3\[5\]\[0\]" \ + " = 50" \ + "print available large_2d_array element from history" + gdb_test -nonl "output \$\$4\[5\]\[0\]" \ + "50" \ + "output available large_2d_array element from history" + + gdb_test "print \$\$4\[5\]\[1\]" \ + " = " \ + "print unavailable large_2d_array element from history" + gdb_test -nonl "output \$\$5\[5\]\[1\]" \ + "" \ + "output unavailable large_2d_array element from history" + + gdb_test "print \$\$5\[5\]\[0\] + \$\$5\[5\]\[1\]" \ + "value is not available" \ + "print expression referring unavailable element from history" + gdb_test "output \$\$5\[5\]\[0\] + \$\$5\[5\]\[1\]" \ + "value is not available" \ + "output expression referring unavailable element from history" +} Index: src/gdb/testsuite/gdb.fortran/limited-length.exp =================================================================== --- /dev/null +++ src/gdb/testsuite/gdb.fortran/limited-length.exp @@ -0,0 +1,220 @@ +# Copyright 2023 Free Software Foundation, Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# This file tests GDB's limited length array printing. + +load_lib "fortran.exp" + +require allow_fortran_tests + +standard_testfile .f90 + +if {[prepare_for_testing "failed to prepare" $testfile $srcfile {debug f90}]} { + return -1 +} + +if {![fortran_runto_main]} { + perror "Could not run to main." + continue +} + +gdb_breakpoint [gdb_get_line_number "Break Here"] +gdb_continue_to_breakpoint "stop-here" ".*Break Here.*" + +with_test_prefix "with standard max-value size" { + gdb_test "print large_4d_array" \ + " = \\(\\(\\(\\(1, 2, 3\\) \\(4, 5, 6\\) \\(7, 8, 9\\)\\)\ + \\(\\(10, 11, 12\\) \\(13, 14, 15\\) \\(16, 17, 18\\)\\)\ + \\(\\(19, 20, 21\\) \\(22, 23, 24\\) \\(25, 26, 27\\)\\)\\)\ + \\(\\(\\(28, 29, 30\\) \\(31, 32, 33\\) \\(34, 35, 36\\)\\)\ + \\(\\(37, 38, 39\\) \\(40, 41, 42\\) \\(43, 44, 45\\)\\)\ + \\(\\(46, 47, 48\\) \\(49, 50, 51\\) \\(52, 53, 54\\)\\)\\)\ + \\(\\(\\(55, 56, 57\\) \\(58, 59, 60\\) \\(61, 62, 63\\)\\)\ + \\(\\(64, 65, 66\\) \\(67, 68, 69\\) \\(70, 71, 72\\)\\)\ + \\(\\(73, 74, 75\\) \\(76, 77, 78\\)\ + \\(79, 80, 81\\)\\)\\)\\)" + gdb_test -nonl "output large_4d_array" \ + "\\(\\(\\(\\(1, 2, 3\\) \\(4, 5, 6\\) \\(7, 8, 9\\)\\)\ + \\(\\(10, 11, 12\\) \\(13, 14, 15\\) \\(16, 17, 18\\)\\)\ + \\(\\(19, 20, 21\\) \\(22, 23, 24\\) \\(25, 26, 27\\)\\)\\)\ + \\(\\(\\(28, 29, 30\\) \\(31, 32, 33\\) \\(34, 35, 36\\)\\)\ + \\(\\(37, 38, 39\\) \\(40, 41, 42\\) \\(43, 44, 45\\)\\)\ + \\(\\(46, 47, 48\\) \\(49, 50, 51\\) \\(52, 53, 54\\)\\)\\)\ + \\(\\(\\(55, 56, 57\\) \\(58, 59, 60\\) \\(61, 62, 63\\)\\)\ + \\(\\(64, 65, 66\\) \\(67, 68, 69\\) \\(70, 71, 72\\)\\)\ + \\(\\(73, 74, 75\\) \\(76, 77, 78\\) \\(79, 80, 81\\)\\)\\)\\)" + gdb_test "print large_1d_array" \ + " = \\(1, 2, 3, 4, 5, 6, 7, 8, 9,\ + 10, 11, 12, 13, 14, 15, 16, 17, 18,\ + 19, 20, 21, 22, 23, 24, 25, 26, 27,\ + 28, 29, 30, 31, 32, 33, 34, 35, 36,\ + 37, 38, 39, 40, 41, 42, 43, 44, 45,\ + 46, 47, 48, 49, 50, 51, 52, 53, 54,\ + 55, 56, 57, 58, 59, 60, 61, 62, 63,\ + 64, 65, 66, 67, 68, 69, 70, 71, 72,\ + 73, 74, 75, 76, 77, 78, 79, 80, 81\\)" + gdb_test -nonl "output large_1d_array" \ + "\\(1, 2, 3, 4, 5, 6, 7, 8, 9,\ + 10, 11, 12, 13, 14, 15, 16, 17, 18,\ + 19, 20, 21, 22, 23, 24, 25, 26, 27,\ + 28, 29, 30, 31, 32, 33, 34, 35, 36,\ + 37, 38, 39, 40, 41, 42, 43, 44, 45,\ + 46, 47, 48, 49, 50, 51, 52, 53, 54,\ + 55, 56, 57, 58, 59, 60, 61, 62, 63,\ + 64, 65, 66, 67, 68, 69, 70, 71, 72,\ + 73, 74, 75, 76, 77, 78, 79, 80, 81\\)" +} + +# Set the max-value-size so we can only print 50 elements. +set elements 50 +set elem_size [get_valueof "/d" "sizeof(large_1d_array(1))" "*unknown*"] +gdb_test_no_output "set max-value-size [expr $elem_size * $elements]" + +with_test_prefix "with reduced max-value size" { + gdb_test "print large_4d_array" \ + "value requires $decimal bytes, which is more than max-value-size" + gdb_test "output large_4d_array" \ + "value requires $decimal bytes, which is more than max-value-size" + gdb_test "print large_1d_array" \ + "value requires $decimal bytes, which is more than max-value-size" + gdb_test "output large_1d_array" \ + "value requires $decimal bytes, which is more than max-value-size" +} + +with_test_prefix "with reduced print -elements flag" { + gdb_test "print -elements 5 -- large_4d_array" \ + " = \\(\\(\\(\\(1, 2, 3\\) \\(4, 5, \\.\\.\\.\\)\ + \\.\\.\\.\\) \\.\\.\\.\\) \\.\\.\\.\\)" + gdb_test "print -elements 5 -- large_1d_array" \ + " = \\(1, 2, 3, 4, 5, \\.\\.\\.\\)" +} + +gdb_test_no_output "set print elements 5" + +with_test_prefix "with reduced print elements" { + gdb_test "print large_4d_array" \ + " = \\(\\(\\(\\(1, 2, 3\\) \\(4, 5, \\.\\.\\.\\)\ + \\.\\.\\.\\) \\.\\.\\.\\) \\.\\.\\.\\)" + gdb_test -nonl "output large_4d_array" \ + "\\(\\(\\(\\(1, 2, 3\\) \\(4, 5, \\.\\.\\.\\)\ + \\.\\.\\.\\) \\.\\.\\.\\) \\.\\.\\.\\)" + + gdb_test "print \$" \ + " = \\(\\(\\(\\(1, 2, 3\\) \\(4, 5, \\.\\.\\.\\)\ + \\.\\.\\.\\) \\.\\.\\.\\) \\.\\.\\.\\)" \ + "print large_4d_array from history" + gdb_test -nonl "output \$\$" \ + "\\(\\(\\(\\(1, 2, 3\\) \\(4, 5, \\.\\.\\.\\)\ + \\.\\.\\.\\) \\.\\.\\.\\) \\.\\.\\.\\)" \ + "output large_4d_array from history" + + gdb_test "print large_1d_array" \ + " = \\(1, 2, 3, 4, 5, \\.\\.\\.\\)" + gdb_test -nonl "output large_1d_array" \ + "\\(1, 2, 3, 4, 5, \\.\\.\\.\\)" + + gdb_test "print \$" \ + " = \\(1, 2, 3, 4, 5, \\.\\.\\.\\)" \ + "print large_1d_array from history" + gdb_test -nonl "output \$\$" \ + "\\(1, 2, 3, 4, 5, \\.\\.\\.\\)" \ + "output large_1d_array from history" +} + +gdb_test_no_output "set print elements $elements" + +with_test_prefix "with print elements matching max-value size" { + gdb_test "print \$\$2" \ + " = \\(\\(\\(\\(1, 2, 3\\) \\(4, 5, 6\\) \\(7, 8, 9\\)\\)\ + \\(\\(10, 11, 12\\) \\(13, 14, 15\\) \\(16, 17, 18\\)\\)\ + \\(\\(19, 20, 21\\) \\(22, 23, 24\\) \\(25, 26, 27\\)\\)\\)\ + \\(\\(\\(28, 29, 30\\) \\(31, 32, 33\\) \\(34, 35, 36\\)\\)\ + \\(\\(37, 38, 39\\) \\(40, 41, 42\\) \\(43, 44, 45\\)\\)\ + \\(\\(46, 47, 48\\) \\(49, 50, \\.\\.\\.\\) \\.\\.\\.\\)\\)\ + \\.\\.\\.\\)" \ + "print large_4d_array from history" + gdb_test -nonl "output \$\$3" \ + "\\(\\(\\(\\(1, 2, 3\\) \\(4, 5, 6\\) \\(7, 8, 9\\)\\)\ + \\(\\(10, 11, 12\\) \\(13, 14, 15\\) \\(16, 17, 18\\)\\)\ + \\(\\(19, 20, 21\\) \\(22, 23, 24\\) \\(25, 26, 27\\)\\)\\)\ + \\(\\(\\(28, 29, 30\\) \\(31, 32, 33\\) \\(34, 35, 36\\)\\)\ + \\(\\(37, 38, 39\\) \\(40, 41, 42\\) \\(43, 44, 45\\)\\)\ + \\(\\(46, 47, 48\\) \\(49, 50, \\.\\.\\.\\) \\.\\.\\.\\)\\)\ + \\.\\.\\.\\)" \ + "output large_4d_array from history" + + gdb_test "print \$\$2" \ + " = \\(1, 2, 3, 4, 5, 6, 7, 8, 9,\ + 10, 11, 12, 13, 14, 15, 16, 17, 18,\ + 19, 20, 21, 22, 23, 24, 25, 26, 27,\ + 28, 29, 30, 31, 32, 33, 34, 35, 36,\ + 37, 38, 39, 40, 41, 42, 43, 44, 45,\ + 46, 47, 48, 49, 50, \\.\\.\\.\\)" \ + "print large_1d_array from history" + gdb_test -nonl "output \$\$2" \ + "\\(1, 2, 3, 4, 5, 6, 7, 8, 9,\ + 10, 11, 12, 13, 14, 15, 16, 17, 18,\ + 19, 20, 21, 22, 23, 24, 25, 26, 27,\ + 28, 29, 30, 31, 32, 33, 34, 35, 36,\ + 37, 38, 39, 40, 41, 42, 43, 44, 45,\ + 46, 47, 48, 49, 50, \\.\\.\\.\\)" \ + "output large_1d_array from history" +} + +gdb_test_no_output "set max-value-size unlimited" +gdb_test_no_output "set print elements unlimited" +gdb_test_no_output "set print repeats 2" + +with_test_prefix "with unlimited print elements" { + gdb_test "print \$\$" \ + " = \\(\\(\\(\\(1, 2, 3\\) \\(4, 5, 6\\) \\(7, 8, 9\\)\\)\ + \\(\\(10, 11, 12\\) \\(13, 14, 15\\) \\(16, 17, 18\\)\\)\ + \\(\\(19, 20, 21\\) \\(22, 23, 24\\) \\(25, 26, 27\\)\\)\\)\ + \\(\\(\\(28, 29, 30\\) \\(31, 32, 33\\) \\(34, 35, 36\\)\\)\ + \\(\\(37, 38, 39\\) \\(40, 41, 42\\) \\(43, 44, 45\\)\\)\ + \\(\\(46, 47, 48\\) \\(49, 50, \\)\ + \\(, \\)\\)\\)\ + \\(\\(\\(, \\)\ + \\) \\)\\)" \ + "print large_4d_array from history" + gdb_test -nonl "output \$\$2" \ + "\\(\\(\\(\\(1, 2, 3\\) \\(4, 5, 6\\) \\(7, 8, 9\\)\\)\ + \\(\\(10, 11, 12\\) \\(13, 14, 15\\) \\(16, 17, 18\\)\\)\ + \\(\\(19, 20, 21\\) \\(22, 23, 24\\) \\(25, 26, 27\\)\\)\\)\ + \\(\\(\\(28, 29, 30\\) \\(31, 32, 33\\) \\(34, 35, 36\\)\\)\ + \\(\\(37, 38, 39\\) \\(40, 41, 42\\) \\(43, 44, 45\\)\\)\ + \\(\\(46, 47, 48\\) \\(49, 50, \\)\ + \\(, \\)\\)\\)\ + \\(\\(\\(, \\)\ + \\) \\)\\)" \ + "output large_4d_array from history" + + gdb_test "print \$\$" \ + " = \\(1, 2, 3, 4, 5, 6, 7, 8, 9,\ + 10, 11, 12, 13, 14, 15, 16, 17, 18,\ + 19, 20, 21, 22, 23, 24, 25, 26, 27,\ + 28, 29, 30, 31, 32, 33, 34, 35, 36,\ + 37, 38, 39, 40, 41, 42, 43, 44, 45,\ + 46, 47, 48, 49, 50, , \\)" \ + "print large_1d_array from history" + gdb_test -nonl "output \$\$2" \ + "\\(1, 2, 3, 4, 5, 6, 7, 8, 9,\ + 10, 11, 12, 13, 14, 15, 16, 17, 18,\ + 19, 20, 21, 22, 23, 24, 25, 26, 27,\ + 28, 29, 30, 31, 32, 33, 34, 35, 36,\ + 37, 38, 39, 40, 41, 42, 43, 44, 45,\ + 46, 47, 48, 49, 50, , \\)" \ + "output large_1d_array from history" +} Index: src/gdb/testsuite/gdb.fortran/limited-length.f90 =================================================================== --- /dev/null +++ src/gdb/testsuite/gdb.fortran/limited-length.f90 @@ -0,0 +1,39 @@ +! This testcase is part of GDB, the GNU debugger. +! +! Copyright 2023 Free Software Foundation, Inc. +! +! This program is free software; you can redistribute it and/or modify +! it under the terms of the GNU General Public License as published by +! the Free Software Foundation; either version 2 of the License, or +! (at your option) any later version. +! +! This program is distributed in the hope that it will be useful, +! but WITHOUT ANY WARRANTY; without even the implied warranty of +! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +! GNU General Public License for more details. +! +! You should have received a copy of the GNU General Public License +! along with this program. If not, see . + +program main + integer(kind=8), dimension (3, 3, 3, 3) :: large_4d_array = reshape ((/ & + 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, & + 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, & + 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, & + 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, & + 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, & + 78, 79, 80, 81/), (/3, 3, 3, 3/)) + + integer(kind=8), dimension (81) :: large_1d_array = reshape ((/ & + 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, & + 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, & + 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, & + 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, & + 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, & + 78, 79, 80, 81/), (/81/)) + + print *, "" + print *, "" ! Break Here + print *, large_4d_array + print *, large_1d_array +end program main Index: src/gdb/valprint.c =================================================================== --- src.orig/gdb/valprint.c +++ src/gdb/valprint.c @@ -2018,13 +2018,21 @@ value_print_array_elements (struct value UINT_MAX (unlimited). */ if (options->repeat_count_threshold < UINT_MAX) { + bool unavailable = value_entirely_unavailable (element); + bool available = value_entirely_available (element); + while (rep1 < len) { struct value *rep_elt = value_from_component_bitsize (val, elttype, rep1 * bit_stride, bit_stride); - if (!value_contents_eq (element, rep_elt)) + bool repeated = ((available + && value_entirely_available (rep_elt) + && value_contents_eq (element, rep_elt)) + || (unavailable + && value_entirely_unavailable (rep_elt))); + if (!repeated) break; ++reps; ++rep1; Index: src/gdb/value.c =================================================================== --- src.orig/gdb/value.c +++ src/gdb/value.c @@ -369,6 +369,14 @@ struct value treated pretty much the same, except not-saved registers have a different string representation and related error strings. */ std::vector optimized_out; + + /* This is only non-zero for values of TYPE_CODE_ARRAY and if the size of + the array in inferior memory is greater than max_value_size. If these + conditions are met then, when the value is loaded from the inferior + GDB will only load a portion of the array into memory, and + limited_length will be set to indicate the length in octets that were + loaded from the inferior. */ + ULONGEST limited_length = 0; }; /* See value.h. */ @@ -1046,6 +1054,93 @@ check_type_length_before_alloc (const st } } +/* When this has a value, it is used to limit the number of array elements + of an array that are loaded into memory when an array value is made + non-lazy. */ +static gdb::optional array_length_limiting_element_count; + +/* See value.h. */ +scoped_array_length_limiting::scoped_array_length_limiting (int elements) +{ + m_old_value = array_length_limiting_element_count; + array_length_limiting_element_count.emplace (elements); +} + +/* See value.h. */ +scoped_array_length_limiting::~scoped_array_length_limiting () +{ + array_length_limiting_element_count = m_old_value; +} + +/* Find the inner element type for ARRAY_TYPE. */ + +static struct type * +find_array_element_type (struct type *array_type) +{ + array_type = check_typedef (array_type); + gdb_assert (array_type->code () == TYPE_CODE_ARRAY); + + if (current_language->la_language == language_fortran) + while (array_type->code () == TYPE_CODE_ARRAY) + { + array_type = array_type->target_type (); + array_type = check_typedef (array_type); + } + else + { + array_type = array_type->target_type (); + array_type = check_typedef (array_type); + } + + return array_type; +} + +/* Return the limited length of ARRAY_TYPE, which must be of + TYPE_CODE_ARRAY. This function can only be called when the global + ARRAY_LENGTH_LIMITING_ELEMENT_COUNT has a value. + + The limited length of an array is the smallest of either (1) the total + size of the array type, or (2) the array target type multiplies by the + array_length_limiting_element_count. */ + +static ULONGEST +calculate_limited_array_length (struct type *array_type) +{ + gdb_assert (array_length_limiting_element_count.has_value ()); + + array_type = check_typedef (array_type); + gdb_assert (array_type->code () == TYPE_CODE_ARRAY); + + struct type *elm_type = find_array_element_type (array_type); + ULONGEST len = (elm_type->length () + * (*array_length_limiting_element_count)); + len = std::min (len, array_type->length ()); + + return len; +} + +/* Try to limit ourselves to only fetching the limited number of + elements. However, if this limited number of elements still + puts us over max_value_size, then we still throw an error. */ + +static bool +set_limited_array_length (struct value *val) +{ + ULONGEST limit = val->limited_length; + ULONGEST len = value_type (val)->length (); + + if (array_length_limiting_element_count.has_value ()) + len = calculate_limited_array_length (value_type (val)); + + if (limit != 0 && len > limit) + len = limit; + if (len > max_value_size) + return false; + + val->limited_length = max_value_size; + return true; +} + /* Allocate the contents of VAL if it has not been allocated yet. If CHECK_SIZE is true, then apply the usual max-value-size checks. */ @@ -1054,10 +1149,26 @@ allocate_value_contents (struct value *v { if (!val->contents) { + struct type *enclosing_type = value_enclosing_type (val); + ULONGEST len = enclosing_type->length (); + if (check_size) - check_type_length_before_alloc (val->enclosing_type); - val->contents.reset - ((gdb_byte *) xzalloc (val->enclosing_type->length ())); + { + /* If we are allocating the contents of an array, which + is greater in size than max_value_size, and there is + an element limit in effect, then we can possibly try + to load only a sub-set of the array contents into + GDB's memory. */ + if (value_type (val) == enclosing_type + && value_type (val)->code () == TYPE_CODE_ARRAY + && len > max_value_size + && set_limited_array_length (val)) + len = val->limited_length; + else + check_type_length_before_alloc (enclosing_type); + } + + val->contents.reset ((gdb_byte *) xzalloc (len)); } } @@ -1786,10 +1897,7 @@ value_copy (const value *arg) struct type *encl_type = value_enclosing_type (arg); struct value *val; - if (value_lazy (arg)) - val = allocate_value_lazy (encl_type); - else - val = allocate_value (encl_type, false); + val = allocate_value_lazy (encl_type); val->type = arg->type; VALUE_LVAL (val) = arg->lval; val->location = arg->location; @@ -1805,17 +1913,28 @@ value_copy (const value *arg) val->initialized = arg->initialized; val->unavailable = arg->unavailable; val->optimized_out = arg->optimized_out; + val->parent = arg->parent; + val->limited_length = arg->limited_length; - if (!value_lazy (val) && !value_entirely_optimized_out (val)) + if (!value_lazy (val) + && !(value_entirely_optimized_out (val) + || value_entirely_unavailable (val))) { + ULONGEST length = val->limited_length; + if (length == 0) + length = value_enclosing_type (val)->length (); + gdb_assert (arg->contents != nullptr); - ULONGEST length = value_enclosing_type (arg)->length (); const auto &arg_view = gdb::make_array_view (arg->contents.get (), length); - copy (arg_view, value_contents_all_raw (val)); + + allocate_value_contents (val, false); + gdb::array_view val_contents + = value_contents_all_raw (val).slice (0, length); + + copy (arg_view, val_contents); } - val->parent = arg->parent; if (VALUE_LVAL (val) == lval_computed) { const struct lval_funcs *funcs = val->location.computed.funcs; @@ -1949,20 +2068,40 @@ set_value_component_location (struct val int record_latest_value (struct value *val) { + struct type *enclosing_type = value_enclosing_type (val); + struct type *type = value_type (val); + /* We don't want this value to have anything to do with the inferior anymore. In particular, "set $1 = 50" should not affect the variable from which the value was taken, and fast watchpoints should be able to assume that a value on the value history never changes. */ if (value_lazy (val)) - value_fetch_lazy (val); + { + /* We know that this is a _huge_ array, any attempt to fetch this + is going to cause GDB to throw an error. However, to allow + the array to still be displayed we fetch its contents up to + `max_value_size' and mark anything beyond "unavailable" in + the history. */ + if (type->code () == TYPE_CODE_ARRAY + && type->length () > max_value_size + && array_length_limiting_element_count.has_value () + && enclosing_type == type + && calculate_limited_array_length (type) <= max_value_size) + val->limited_length = max_value_size; + + value_fetch_lazy (val); + } /* Don't pretend we have anything available there in the history beyond the boundaries of the value recorded. It's not like inferior memory where there is actual stuff underneath. */ - ULONGEST length = value_enclosing_type (val)->length (); + ULONGEST length = enclosing_type->length (); mark_value_bits_unavailable (val, LONGEST_MIN, 0 ^ LONGEST_MIN); mark_value_bits_unavailable (val, length * TARGET_CHAR_BIT, LONGEST_MAX - length * TARGET_CHAR_BIT); + if (val->limited_length != 0) + mark_value_bytes_unavailable (val, val->limited_length, + length - val->limited_length); /* We preserve VALUE_LVAL so that the user can find out where it was fetched from. This is a bit dubious, because then *&$1 does not just return $1 @@ -4059,10 +4198,23 @@ value_fetch_lazy_memory (struct value *v CORE_ADDR addr = value_address (val); struct type *type = check_typedef (value_enclosing_type (val)); - if (type->length ()) - read_value_memory (val, 0, value_stack (val), - addr, value_contents_all_raw (val).data (), - type_length_units (type)); + /* Figure out how much we should copy from memory. Usually, this is just + the size of the type, but, for arrays, we might only be loading a + small part of the array (this is only done for very large arrays). */ + int len = 0; + if (val->limited_length > 0) + { + gdb_assert (value_type (val)->code () == TYPE_CODE_ARRAY); + len = val->limited_length; + } + else if (type->length () > 0) + len = type_length_units (type); + + gdb_assert (len >= 0); + + if (len > 0) + read_value_memory (val, 0, value_stack (val), addr, + value_contents_all_raw (val).data (), len); } /* Helper for value_fetch_lazy when the value is in a register. */ Index: src/gdb/value.h =================================================================== --- src.orig/gdb/value.h +++ src/gdb/value.h @@ -1235,4 +1235,21 @@ extern void finalize_values (); of floating-point, fixed-point, or integer type. */ extern gdb_mpq value_to_gdb_mpq (struct value *value); +/* While an instance of this class is live, and array values that are + created, that are larger than max_value_size, will be restricted in size + to a particular number of elements. */ + +struct scoped_array_length_limiting +{ + /* Limit any large array values to only contain ELEMENTS elements. */ + scoped_array_length_limiting (int elements); + + /* Restore the previous array value limit. */ + ~scoped_array_length_limiting (); + +private: + /* Used to hold the previous array value element limit. */ + gdb::optional m_old_value; +}; + #endif /* !defined (VALUE_H) */