From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx2.suse.de (mx2.suse.de [195.135.220.15]) by sourceware.org (Postfix) with ESMTPS id 02DB93891C1A for ; Wed, 19 May 2021 06:25:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 02DB93891C1A Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=tdevries@suse.de X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 0F147ADB3; Wed, 19 May 2021 06:25:21 +0000 (UTC) Subject: [PATCH][gdb/cli] Add ignore-errors command To: Tom Tromey Cc: Simon Marchi via Gdb-patches References: <20210518095958.GA22771@delia> <44B64C9E-9E19-47BD-80CD-0C660C7A9D94@undo.io> <453ffcaa-2a21-62a7-d449-28c7c187231a@polymtl.ca> <87im3g14ss.fsf@tromey.com> <875yzg12io.fsf@tromey.com> From: Tom de Vries Message-ID: <74cad703-347f-6685-9cee-428b8ef438b9@suse.de> Date: Wed, 19 May 2021 08:25:20 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.10.0 MIME-Version: 1.0 In-Reply-To: <875yzg12io.fsf@tromey.com> Content-Type: multipart/mixed; boundary="------------6FC2AA14C0A083653C2174AF" Content-Language: en-US X-Spam-Status: No, score=-12.2 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, KAM_SHORT, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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: Wed, 19 May 2021 06:25:24 -0000 This is a multi-part message in MIME format. --------------6FC2AA14C0A083653C2174AF Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit [ was: [RFC][gdb/cli] Ignore error in gdb command script ] On 5/18/21 5:32 PM, Tom Tromey wrote: >>>>>> "Tom" == Tom de Vries writes: > > Tom> + const char *ignore_errors_help_text > Tom> + = ("Execute a single command, ignoring all errors.\n" > Tom> + "Only one-line commands are supported.\n" > Tom> + "This is primarily useful in scripts."); > Tom> + c = add_cmd ("ignore-errors", class_support, ignore_errors_command, > Tom> + ignore_errors_help_text, &cmdlist); > > It's more normal to inline the help text into the call; but either way > it should be wrapped in _(). Ack, done. Also added test-case and docs. Any other comments? Thanks, - Tom --------------6FC2AA14C0A083653C2174AF Content-Type: text/x-patch; charset=UTF-8; name="0001-gdb-cli-Add-ignore-errors-command.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline; filename="0001-gdb-cli-Add-ignore-errors-command.patch" [gdb/cli] Add ignore-errors command While trying to reproduce a failing test-case from the testsuite on the command line using a gdb command script, I ran into the problem that a co= mmand failed which stopped script execution. I could work around this by splitting the script at each error, but I rea= lized it would be nice if I could tell gdb to ignore the error. A python workaround ignore-errors exists, mentioned here ( https://sourceware.org/legacy-ml/gdb/2010-06/msg00100.html ), which is already supplied by distros like Fedora and openSUSE. FTR, a more elaborate try-catch solution was posted here ( https://sourceware.org/bugzilla/show_bug.cgi?id=3D8487 ). This patch adds native ignore-errors support (so no python needed). So with this script: =2E.. $ cat script.gdb ignore-errors run echo here =2E.. we have: =2E.. $ gdb -q -batch -x script.gdb No executable file specified. Use the "file" or "exec-file" command. here$ =2E.. Note that quit is not caught: =2E.. $ gdb -q (gdb) ignore-errors quit $ =2E.. which is the same behaviour as with the python implementation. Tested on x86_64-linux. gdb/ChangeLog: 2021-05-18 Tom de Vries * cli/cli-cmds.c (ignore_errors_command_completer) (ignore_errors_command): New function. (_initialize_cli_cmds): Add "ignore-errors" cmd. gdb/doc/ChangeLog: 2021-05-18 Tom de Vries * gdb.texinfo (Command Files): Document command ignore-errors. gdb/testsuite/ChangeLog: 2021-05-18 Tom de Vries * gdb.base/ignore-errors.exp: New test. * gdb.base/ignore-errors.gdb: New command file. --- gdb/cli/cli-cmds.c | 35 ++++++++++++++++++++++++++= ++++++ gdb/doc/gdb.texinfo | 8 +++++++- gdb/testsuite/gdb.base/ignore-errors.exp | 24 ++++++++++++++++++++++ gdb/testsuite/gdb.base/ignore-errors.gdb | 2 ++ 4 files changed, 68 insertions(+), 1 deletion(-) diff --git a/gdb/cli/cli-cmds.c b/gdb/cli/cli-cmds.c index 0bf418e510e..3f2478ed782 100644 --- a/gdb/cli/cli-cmds.c +++ b/gdb/cli/cli-cmds.c @@ -39,6 +39,7 @@ #include "gdbsupport/filestuff.h" #include "location.h" #include "block.h" +#include "event-top.h" =20 #include "ui-out.h" #include "interps.h" @@ -2249,6 +2250,34 @@ gdb_maint_setting_str_internal_fn (struct gdbarch = *gdbarch, gdbarch); } =20 +/* Completer for "ignore-errors". */ + +static void +ignore_errors_command_completer (cmd_list_element *ignore, + completion_tracker &tracker, + const char *text, const char * /*word*/) +{ + complete_nested_command_line (tracker, text); +} + +/* Implementation of the ignore-errors command. */ + +static void +ignore_errors_command (const char *args, int from_tty) +{ + try + { + execute_command (args, from_tty); + } + catch (const gdb_exception_error &ex) + { + exception_print (gdb_stderr, ex); + + /* See also execute_gdb_command. */ + async_enable_stdin (); + } +} + void _initialize_cli_cmds (); void _initialize_cli_cmds () @@ -2618,4 +2647,10 @@ when GDB is started."), GDBINIT); c =3D add_cmd ("source", class_support, source_command, source_help_text, &cmdlist); set_cmd_completer (c, filename_completer); + + c =3D add_cmd ("ignore-errors", class_support, ignore_errors_command, + _("Execute a single command, ignoring all errors.\n" + "Only one-line commands are supported.\n" + "This is primarily useful in scripts."), &cmdlist); + set_cmd_completer (c, ignore_errors_command_completer); } diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo index 56f37eb2288..4b15fb51fef 100644 --- a/gdb/doc/gdb.texinfo +++ b/gdb/doc/gdb.texinfo @@ -27466,7 +27466,8 @@ The lines in a command file are generally execute= d sequentially, unless the order of execution is changed by one of the @emph{flow-control commands} described below. The commands are not printed as they are executed. An error in any command terminates -execution of the command file and control is returned to the console. +execution of the command file and control is returned to the console, +unless the line is prefixed with the @code{ignore-errors} command. =20 @value{GDBN} first searches for @var{filename} in the current directory.= If the file is not found there, and @var{filename} does not specify a @@ -27561,6 +27562,11 @@ the controlling expression. @item end Terminate the block of commands that are the body of @code{if}, @code{else}, or @code{while} flow-control commands. + +@kindex ignore-errors +@item ignore-errors +This command executes the command specified by its arguments, but +doesn't stop execution of the script if the command fails. @end table =20 =20 diff --git a/gdb/testsuite/gdb.base/ignore-errors.exp b/gdb/testsuite/gdb= =2Ebase/ignore-errors.exp new file mode 100644 index 00000000000..30dac7a94e2 --- /dev/null +++ b/gdb/testsuite/gdb.base/ignore-errors.exp @@ -0,0 +1,24 @@ +# Copyright 2021 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 . =20 */ + +# Check command ignore-errors. + +clean_restart + +gdb_test "source ignore-errors.gdb" \ + [multi_line \ + "No executable file specified\\." \ + "Use the \"file\" or \"exec-file\" command\\." \ + "here"] diff --git a/gdb/testsuite/gdb.base/ignore-errors.gdb b/gdb/testsuite/gdb= =2Ebase/ignore-errors.gdb new file mode 100644 index 00000000000..5962ff49b11 --- /dev/null +++ b/gdb/testsuite/gdb.base/ignore-errors.gdb @@ -0,0 +1,2 @@ +ignore-errors run +echo here\n --------------6FC2AA14C0A083653C2174AF--