From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 063873858D1E for ; Thu, 8 Sep 2022 10:27:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 063873858D1E Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-258-KD4Lq8XvO5uGCTQYsucegg-1; Thu, 08 Sep 2022 06:27:52 -0400 X-MC-Unique: KD4Lq8XvO5uGCTQYsucegg-1 Received: by mail-wm1-f69.google.com with SMTP id 203-20020a1c02d4000000b003a5f5bce876so938377wmc.2 for ; Thu, 08 Sep 2022 03:27:52 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=mime-version:message-id:date:references:in-reply-to:subject:to:from :x-gm-message-state:from:to:cc:subject:date; bh=HX5Z6fQTqSglJ5aVonGUjI3HrFcUtMfy1LQ5dzV2lCg=; b=OhmcyBSoDv54VW7mSOHU8Ct3O18HnNEa3Dt3XqgjsAJ+NdplHPlkyGaT699MVdMu/g jdD/cyDgn/Pq29ScIsqiMrMK2haY1n+gYYqraY1kvSki/IaAYssI5EjrLzlXKIWhzS7P XE9UeUhtfxp6gS79zdAhJEhgHh9mxSLUQSXHNJe53jZjHX5Mes4deNREGM+RoJAM6Vqb dcNVrBkUGSqiidDUGwyFb9LwblXapKL7vSq1IgawqHLyfjfgeK1RsBJG8EGCkGWwnilV AZ1Y3+f3KPG33piYhpx8PPBhVROU5Hia7xelowMrqiO89+2nZzUBrtYtUwnhINDGwp/Q xqWg== X-Gm-Message-State: ACgBeo2E0r5mqjR2ZldC14NzGL5KgQEtZMPLW2BkDk181ATWfFEZJHvJ AhMwh33ISCt8alCrc5VO1aohSlnarCXdl2hP7EB7xbzBsRpY9dq0buwGw6orxhMCsATJgQn9xuN mlNjS9Ir3jhv5ClsNT3ETfg== X-Received: by 2002:a1c:4c11:0:b0:3a5:4d01:54be with SMTP id z17-20020a1c4c11000000b003a54d0154bemr1849879wmf.32.1662632871007; Thu, 08 Sep 2022 03:27:51 -0700 (PDT) X-Google-Smtp-Source: AA6agR7mOXRZnx6+awb56a8CzDcNAsEfy2CKSyy8aL5HdUwShGGPl4OU7JBMNm+VHpFwhV5oeC/F4Q== X-Received: by 2002:a1c:4c11:0:b0:3a5:4d01:54be with SMTP id z17-20020a1c4c11000000b003a54d0154bemr1849859wmf.32.1662632870474; Thu, 08 Sep 2022 03:27:50 -0700 (PDT) Received: from localhost (52.72.115.87.dyn.plus.net. [87.115.72.52]) by smtp.gmail.com with ESMTPSA id l16-20020a056000023000b002285f73f11dsm21334332wrz.81.2022.09.08.03.27.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Sep 2022 03:27:50 -0700 (PDT) From: Andrew Burgess To: Gareth Rees , gdb-patches@sourceware.org Subject: Re: [PATCH] [gdb/mi] Don't treat references to compound values as "simple". In-Reply-To: <20220908071831.10166-1-grees@undo.io> References: <20220908071831.10166-1-grees@undo.io> Date: Thu, 08 Sep 2022 11:27:49 +0100 Message-ID: <877d2ema8q.fsf@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain X-Spam-Status: No, score=-10.6 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_NONE, 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 10:27:57 -0000 Thanks for this patch. I have a few notes, in line below. Gareth Rees via Gdb-patches writes: > 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. I'm agree that this feels like a bug, and that solution #1 seems like the way to go. > --- > 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) This new function should have a comment like: /* See mi-cmds.h. */ And also, should be returning `bool`. > +{ > + type = check_typedef (type); > + > + if (type->code () == TYPE_CODE_REF || type->code () == TYPE_CODE_RVALUE_REF) > + type = TYPE_TARGET_TYPE (type); > + > + return (type Checks for nullptr should be written as: type != nullptr but, can you ever get nullptr here? Certainly, if type is nullptr on entry then an assert in check_typedef will trigger, I'd expect that after calling check_typedef and TYPE_TARGET_TYPE you should still have a valid type pointer ... but maybe there's some case you've seen where this is not true? Unless you know a case where type could be nullptr I would rather the type check be moved into a separate: gdb_assert (type != nullptr); before the return. > + && 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); Update the comment to reflect an update of the return type to bool. > + > #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. */ Two spaces before '*/' to close this comment. Test programs should follow GNU style unless specifically required not to for some test reason. > + > +struct s > +{ > + int v; It seems like everything is over indented in the test, 4 spaces instead of 2. > +}; > + > +int > +f(int a, int &b, int &&c, struct s s, struct s &t, struct s &&u) Space after 'f'. > +{ > + return a + b + c + s.v + t.v + u.v; Indentation. > +} > + > +int > +main(void) Space after 'main'. > +{ > + int a = 1, b = 2; > + struct s s = { 4 }, t = { 5 }; > + return f(a, b, 3, s, t, (struct s){ 6 }); Space after 'f' and after '(struct s)' (I think). > +} > 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 . > + Test scripts should have a brief summary here of what the test is checking. This will be similar to the comment you have in the source file, so maybe you just want to move that here, but you can keep the source comment and add an extra description here, whatever works. > +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" We still try to wrap lines to under 80 characters when possible, so here, and maybe the previous line too should be wrapped. > + > +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" But lines like these can't easily be wrapped, so its fine for these to exist. Thanks, Andrew > + > +mi_gdb_exit > -- > 2.26.0