From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-x435.google.com (mail-wr1-x435.google.com [IPv6:2a00:1450:4864:20::435]) by sourceware.org (Postfix) with ESMTPS id AD9333858D1E for ; Thu, 8 Sep 2022 07:18:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org AD9333858D1E Received: by mail-wr1-x435.google.com with SMTP id k9so24487564wri.0 for ; Thu, 08 Sep 2022 00:18:36 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date; bh=QXp7aGaNUim06BqVUg+iBzMZikKxeHsHSuVYzoyDXYY=; b=JA73mJCuOqEc/cgsefhxCfo5dmPYDw60avS5JF7dkYdwN2sXvNtj+tRien4n9BV1Bn RhFnYHXWg5s9nOgkJTF07heGCrWvCtGyL9UeyRMVRJ4FP/QZsPH6o3eAeMhAy5J0oCcL YDg0F53/uEHdX+DKZkzoDDqqJUdFO16hjcjp2GtXcjh4JUkvvQuOp/U6zM3LmipRvhWD lszMU65kBXiJ51voCJxpuQzGxw0aLPqbcFRsHvWroonFSQaJH6ChOpnplSgSdOUSgKF0 mzGU3HOTpdYandlTlbRYnx+RQnrU9dqe/JZw9r8N/viREqPFbW7ZiJL9X9FlSP2W5FXo RWhw== X-Gm-Message-State: ACgBeo2gUmfokIu4jqF1Qxgzj+uqpdR2ObXG1flOdfpdz0/gMuWQv+tw lHd4/YICToEs1RA9kbH63+bh55gg1dj5RhGRrOB7zirJCtmDaorw5j+w+wIyqWDBUU90nj4XQMN piJrsD0sxMEJH8WClkpwbV4RSbc2NC0rX2j2d6O2m+/GiQjv9OWZu0r4E57OJk050acU= X-Google-Smtp-Source: AA6agR67Nx4YG+VPnrEy75N6iIoqV0IqQGBjjMMo33b8ncXbmYsJAjMYqydr4Dg8YulBo/BPkAhYOw== X-Received: by 2002:adf:e781:0:b0:228:b44c:d0f7 with SMTP id n1-20020adfe781000000b00228b44cd0f7mr3873165wrm.243.1662621515111; Thu, 08 Sep 2022 00:18:35 -0700 (PDT) Received: from tilsit-thinkpad.undoers.io (cpc91202-cmbg18-2-0-cust74.5-4.cable.virginm.net. [81.100.88.75]) by smtp.gmail.com with ESMTPSA id m11-20020a056000180b00b002287d99b455sm14366861wrh.15.2022.09.08.00.18.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Sep 2022 00:18:34 -0700 (PDT) From: Gareth Rees To: gdb-patches@sourceware.org Subject: [PATCH] [gdb/mi] Don't treat references to compound values as "simple". Date: Thu, 8 Sep 2022 08:18:31 +0100 Message-Id: <20220908071831.10166-1-grees@undo.io> X-Mailer: git-send-email 2.26.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-13.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE 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: , X-List-Received-Date: Thu, 08 Sep 2022 07:18:41 -0000 SUMMARY The '--simple-values' argument to '-stack-list-arguments' and similar GDB/MI commands does not take reference types into account, so that references to arbitrarily large structures are considered "simple" and printed. This means that the '--simple-values' argument cannot be used by IDEs when tracing the stack due to the time taken to print large structures passed by reference. DETAILS Various GDB/MI commands ('-stack-list-arguments', '-stack-list-locals', '-stack-list-variables' and so on) take a PRINT-VALUES argument which may be '--no-values' (0), '--all-values' (1) or '--simple-values' (2). In the '--simple-values' case, the command is supposed to print the name, type, and value of variables with simple types, and print only the name and type of variables with compound types. The '--simple-values' argument ought to be suitable for IDEs that need to update their user interface with the program's call stack every time the program stops. However, it does not take C++ reference types into account, and this makes the argument unsuitable for this purpose. For example, consider the following C++ program: struct s { int v[10]; }; int sum(const struct s &s) { int total = 0; for (int i = 0; i < 10; ++i) total += s.v[i]; return total; } int main(void) { struct s s = { { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 } }; return sum(s); } If we start GDB in MI mode and continue to 'sum', the behaviour of '-stack-list-arguments' is as follows: (gdb) -stack-list-arguments --simple-values ^done,stack-args=[frame={level="0",args=[{name="s",type="const s &",value="@0x7fffffffe310: {v = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}}"}]},frame={level="1",args=[]}] Note that the value of the argument 's' was printed, even though 's' is a reference to a structure, which is not a simple value. See https://github.com/microsoft/MIEngine/pull/673 for a case where this behaviour caused Microsoft to avoid the use of '--simple-values' in their MIEngine debug adapter, because it caused Visual Studio Code to take too long to refresh the call stack in the user interface. SOLUTIONS There are two ways we could fix this problem, depending on whether we consider the current behaviour to be a bug. 1. If the current behaviour is a bug, then we can update the behaviour of '--simple-values' so that it takes reference types into account: that is, a value is simple if it is neither an array, struct, or union, nor a reference to an array, struct or union. In this case we must add a feature to the '-list-features' command so that IDEs can detect that it is safe to use the '--simple-values' argument when refreshing the call stack. 2. If the current behaviour is not a bug, then we can add a new option for the PRINT-VALUES argument, for example, '--simplest-values' (3), that would be suitable for use by IDEs. In this case we must add a feature to the '-list-features' command so that IDEs can detect that the '--simplest-values' argument is available for use when refreshing the call stack. PATCH This patch implements solution (1) as I think the current behaviour of not printing structures, but printing references to structures, is surprising. However, if you prefer solution (2) I would be happy to implement that instead. --- gdb/NEWS | 12 ++++++ gdb/doc/gdb.texinfo | 7 ++++ gdb/mi/mi-cmd-stack.c | 6 +-- gdb/mi/mi-cmd-var.c | 22 ++++++---- gdb/mi/mi-cmds.h | 5 +++ gdb/mi/mi-main.c | 7 +--- gdb/python/py-framefilter.c | 6 +-- gdb/testsuite/gdb.mi/print-simple-values.cc | 44 ++++++++++++++++++++ gdb/testsuite/gdb.mi/print-simple-values.exp | 40 ++++++++++++++++++ 9 files changed, 126 insertions(+), 23 deletions(-) create mode 100644 gdb/testsuite/gdb.mi/print-simple-values.cc create mode 100644 gdb/testsuite/gdb.mi/print-simple-values.exp diff --git a/gdb/NEWS b/gdb/NEWS index dee0ac2ecd8..855f42a9549 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -114,6 +114,18 @@ maintenance info line-table entry corresponds to an address where a breakpoint should be placed to be at the first instruction past a function's prologue. +* MI changes + + ** The '--simple-values' argument to the '-stack-list-arguments', + '-stack-list-locals', '-stack-list-variables', and + '-var-list-children' commands takes reference types into account: + that is, a value is now considered simple if it is neither an + array, structure, or union, nor a reference to an array, structure, + or union. (Previously all references were considered simple.) + Support for this feature can be verified by using the + '-list-features' command, which should contain + "simple-values-ref-types". + * New targets GNU/Linux/LoongArch (gdbserver) loongarch*-*-linux* diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo index 238a49b027d..5ccf6609709 100644 --- a/gdb/doc/gdb.texinfo +++ b/gdb/doc/gdb.texinfo @@ -37295,6 +37295,13 @@ option (@pxref{GDB/MI Program Execution}). @item data-disassemble-a-option Indicates that the @code{-data-disassemble} command supports the @option{-a} option (@pxref{GDB/MI Data Manipulation}). +@item simple-values-ref-types +Indicates that the @code{--simple-values} argument to the +@code{-stack-list-arguments}, @code{-stack-list-locals}, +@code{-stack-list-variables}, and @code{-var-list-children} commands +takes reference types into account: that is, a value is considered +simple if it neither an array, structure, or union, nor a reference to +an array, structure, or union. @end ftable @subheading The @code{-list-target-features} Command diff --git a/gdb/mi/mi-cmd-stack.c b/gdb/mi/mi-cmd-stack.c index 0fe204dbc66..f8f4ad266d0 100644 --- a/gdb/mi/mi-cmd-stack.c +++ b/gdb/mi/mi-cmd-stack.c @@ -571,7 +571,6 @@ list_args_or_locals (const frame_print_options &fp_opts, const struct block *block; struct symbol *sym; struct block_iterator iter; - struct type *type; const char *name_of_result; struct ui_out *uiout = current_uiout; @@ -650,10 +649,7 @@ list_args_or_locals (const frame_print_options &fp_opts, switch (values) { case PRINT_SIMPLE_VALUES: - type = check_typedef (sym2->type ()); - if (type->code () != TYPE_CODE_ARRAY - && type->code () != TYPE_CODE_STRUCT - && type->code () != TYPE_CODE_UNION) + if (mi_simple_values_type_p (sym2->type ())) { case PRINT_ALL_VALUES: if (sym->is_argument ()) diff --git a/gdb/mi/mi-cmd-var.c b/gdb/mi/mi-cmd-var.c index 3db09cf7815..f9d332d13a4 100644 --- a/gdb/mi/mi-cmd-var.c +++ b/gdb/mi/mi-cmd-var.c @@ -335,15 +335,21 @@ mi_print_value_p (struct varobj *var, enum print_values print_values) if (type == NULL) return 1; else - { - type = check_typedef (type); + return mi_simple_values_type_p (type); +} - /* For PRINT_SIMPLE_VALUES, only print the value if it has a type - and that type is not a compound type. */ - return (type->code () != TYPE_CODE_ARRAY - && type->code () != TYPE_CODE_STRUCT - && type->code () != TYPE_CODE_UNION); - } +int +mi_simple_values_type_p (struct type *type) +{ + type = check_typedef (type); + + if (type->code () == TYPE_CODE_REF || type->code () == TYPE_CODE_RVALUE_REF) + type = TYPE_TARGET_TYPE (type); + + return (type + && type->code () != TYPE_CODE_ARRAY + && type->code () != TYPE_CODE_STRUCT + && type->code () != TYPE_CODE_UNION); } void diff --git a/gdb/mi/mi-cmds.h b/gdb/mi/mi-cmds.h index 9ffb11bf997..81a5578ca1c 100644 --- a/gdb/mi/mi-cmds.h +++ b/gdb/mi/mi-cmds.h @@ -226,4 +226,9 @@ using remove_mi_cmd_entries_ftype = gdb::function_view; extern void remove_mi_cmd_entries (remove_mi_cmd_entries_ftype callback); +/* Return 1 if type is suitable for printing with PRINT_SIMPLE_VALUES, that is, + if type is neither a compound type nor a reference to a compound type. */ + +extern int mi_simple_values_type_p (struct type *type); + #endif /* MI_MI_CMDS_H */ diff --git a/gdb/mi/mi-main.c b/gdb/mi/mi-main.c index b758f398e2a..dd1962fd0e2 100644 --- a/gdb/mi/mi-main.c +++ b/gdb/mi/mi-main.c @@ -1663,6 +1663,7 @@ mi_cmd_list_features (const char *command, char **argv, int argc) uiout->field_string (NULL, "undefined-command-error-code"); uiout->field_string (NULL, "exec-run-start-option"); uiout->field_string (NULL, "data-disassemble-a-option"); + uiout->field_string (NULL, "simple-values-ref-types"); if (ext_lang_initialized_p (get_ext_lang_defn (EXT_LANG_PYTHON))) uiout->field_string (NULL, "python"); @@ -2462,7 +2463,6 @@ static void print_variable_or_computed (const char *expression, enum print_values values) { struct value *val; - struct type *type; struct ui_out *uiout = current_uiout; string_file stb; @@ -2482,12 +2482,9 @@ print_variable_or_computed (const char *expression, enum print_values values) switch (values) { case PRINT_SIMPLE_VALUES: - type = check_typedef (value_type (val)); type_print (value_type (val), "", &stb, -1); uiout->field_stream ("type", stb); - if (type->code () != TYPE_CODE_ARRAY - && type->code () != TYPE_CODE_STRUCT - && type->code () != TYPE_CODE_UNION) + if (mi_simple_values_type_p (value_type (val))) { struct value_print_options opts; diff --git a/gdb/python/py-framefilter.c b/gdb/python/py-framefilter.c index 366f3745b9d..227a9ecb4f1 100644 --- a/gdb/python/py-framefilter.c +++ b/gdb/python/py-framefilter.c @@ -235,14 +235,10 @@ py_print_value (struct ui_out *out, struct value *val, if (args_type == MI_PRINT_SIMPLE_VALUES || args_type == MI_PRINT_ALL_VALUES) { - struct type *type = check_typedef (value_type (val)); - if (args_type == MI_PRINT_ALL_VALUES) should_print = 1; else if (args_type == MI_PRINT_SIMPLE_VALUES - && type->code () != TYPE_CODE_ARRAY - && type->code () != TYPE_CODE_STRUCT - && type->code () != TYPE_CODE_UNION) + && mi_simple_values_type_p (value_type (val))) should_print = 1; } else if (args_type != NO_VALUES) diff --git a/gdb/testsuite/gdb.mi/print-simple-values.cc b/gdb/testsuite/gdb.mi/print-simple-values.cc new file mode 100644 index 00000000000..15567c740a0 --- /dev/null +++ b/gdb/testsuite/gdb.mi/print-simple-values.cc @@ -0,0 +1,44 @@ +/* This test case is part of GDB, the GNU debugger. + + Copyright 2022 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 . */ + +/* PRINT_SIMPLE_VALUES test program. + + Test that PRINT_SIMPLE_VALUES handles C++ reference types correctly. + + In the function f(), arguments a, b, and c are either ints or references to + int, and their values should be printed by PRINT_SIMPLE_VALUES, but arguments + s, t, and u are structures or references to structures, and their values + should not be printed by PRINT_SIMPLE_VALUES. */ + +struct s +{ + int v; +}; + +int +f(int a, int &b, int &&c, struct s s, struct s &t, struct s &&u) +{ + return a + b + c + s.v + t.v + u.v; +} + +int +main(void) +{ + int a = 1, b = 2; + struct s s = { 4 }, t = { 5 }; + return f(a, b, 3, s, t, (struct s){ 6 }); +} diff --git a/gdb/testsuite/gdb.mi/print-simple-values.exp b/gdb/testsuite/gdb.mi/print-simple-values.exp new file mode 100644 index 00000000000..c1b5a54ad3b --- /dev/null +++ b/gdb/testsuite/gdb.mi/print-simple-values.exp @@ -0,0 +1,40 @@ +# Copyright 2022 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 . + +if { [skip_cplus_tests] } { continue } + +load_lib mi-support.exp +standard_testfile .cc + +if { [gdb_compile "$srcdir/$subdir/$srcfile" $binfile executable {debug c++}] != "" } { + untested "failed to compile" + return -1 +} + +mi_clean_restart $binfile + +mi_runto_main + +mi_gdb_test "-break-insert f" "\\^done.*" "set breakpoint on f" + +mi_send_resuming_command "exec-continue" "exec-continue to breakpoint on f" + +mi_expect_stop "breakpoint-hit" "f" ".*" ".*" ".*" {.* disp="keep"} "run until breakpoint on f" + +mi_gdb_test "-stack-list-arguments 2" \ + "\\^done,stack-args=\\\[frame=\{level=\"0\",args=\\\[\{name=\"a\",type=\"int\",value=\"1\"\},\{name=\"b\",type=\"int &\",value=\"@0x\[0-9a-f\]+: 2\"\},\{name=\"c\",type=\"int &&\",value=\"@0x\[0-9a-f\]+: 3\"\},\{name=\"s\",type=\"s\"\},\{name=\"t\",type=\"s &\"\},\{name=\"u\",type=\"s &&\"\}\\\]\},frame=\{level=\"1\",args=\\\[\\\]\}\\\]" \ + "stack list arguments, simple types: names, types and values, compound types: names and types" + +mi_gdb_exit -- 2.26.0