public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [RFC][gdb/cli] Ignore error in gdb command script
@ 2021-05-18  9:59 Tom de Vries
  2021-05-18 11:12 ` Marco Barisione
  0 siblings, 1 reply; 15+ messages in thread
From: Tom de Vries @ 2021-05-18  9:59 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

Hi,

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 command
failed which stopped script execution.

I could work around this by splitting the script at each error, but I realized
it would be nice if I could tell gdb to ignore the error.

Inspired by make, I chose the '-' prefix.

This demonstator works for my use case, but also has effect on the gdb
prompt, which is strictly speaking not necessary.

Another way to do this is to use a python workaround ignore-errors mentioned
here ( https://sourceware.org/legacy-ml/gdb/2010-06/msg00100.html ).  This
also mentions a bugzilla entry that adds "exception handling to the gdb
command language" but I was not able to find that.

This though:
...
$ cat script.gdb
-run
echo here
...
doesn't work very well:
...
$ gdb -q -batch -x script.gdb
here
<HANGS>
...

While this:
...
$ cat script.gdb
source ignore-errors.py
ignore-errors run
echo HERE
...
works fine:
...
$ gdb -q -batch -x script.gdb
HERE$
...

Any comments?

Thanks,
- Tom

[gdb/cli] Ignore error in gdb command script

---
 gdb/event-top.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/gdb/event-top.c b/gdb/event-top.c
index 002a7dc95e0..e7dd5e1319e 100644
--- a/gdb/event-top.c
+++ b/gdb/event-top.c
@@ -585,7 +585,19 @@ command_handler (const char *command)
     ;
   if (c[0] != '#')
     {
-      execute_command (command, ui->instream == ui->stdin_stream);
+      if (*c == '-')
+	{
+	  c++;
+	  try
+	    {
+	      execute_command (c, ui->instream == ui->stdin_stream);
+	    }
+	  catch (const gdb_exception_error &ex)
+	    {
+	    }
+	}
+      else
+	execute_command (c, ui->instream == ui->stdin_stream);
 
       /* Do any commands attached to breakpoint we stopped at.  */
       bpstat_do_actions ();

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [RFC][gdb/cli] Ignore error in gdb command script
  2021-05-18  9:59 [RFC][gdb/cli] Ignore error in gdb command script Tom de Vries
@ 2021-05-18 11:12 ` Marco Barisione
  2021-05-18 13:57   ` Tom de Vries
  0 siblings, 1 reply; 15+ messages in thread
From: Marco Barisione @ 2021-05-18 11:12 UTC (permalink / raw)
  To: Tom de Vries; +Cc: GDB patches mailing list, Tom Tromey

On 18 May 2021, at 10:59, Tom de Vries <tdevries@suse.de> wrote:
> Hi,
> 
> 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 command
> failed which stopped script execution.
> 
> I could work around this by splitting the script at each error, but I realized
> it would be nice if I could tell gdb to ignore the error.
> 
> Inspired by make, I chose the '-' prefix.

As MI commands are prefixed by “-“, isn’t there a risk of confusion?

There’s also a “-” command (see tui/tui-win.c) which will stop working with
your patch.

-- 
Marco Barisione


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [RFC][gdb/cli] Ignore error in gdb command script
  2021-05-18 11:12 ` Marco Barisione
@ 2021-05-18 13:57   ` Tom de Vries
  2021-05-18 14:10     ` Simon Marchi
  2021-05-18 14:23     ` Andrew Burgess
  0 siblings, 2 replies; 15+ messages in thread
From: Tom de Vries @ 2021-05-18 13:57 UTC (permalink / raw)
  To: Marco Barisione; +Cc: GDB patches mailing list, Tom Tromey

[-- Attachment #1: Type: text/plain, Size: 1282 bytes --]

On 5/18/21 1:12 PM, Marco Barisione wrote:
> On 18 May 2021, at 10:59, Tom de Vries <tdevries@suse.de> wrote:
>> Hi,
>>
>> 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 command
>> failed which stopped script execution.
>>
>> I could work around this by splitting the script at each error, but I realized
>> it would be nice if I could tell gdb to ignore the error.
>>
>> Inspired by make, I chose the '-' prefix.
> 
> As MI commands are prefixed by “-“, isn’t there a risk of confusion?
> 

Ah, right, I tend to forget about MI, good point.

> There’s also a “-” command (see tui/tui-win.c) which will stop working with
> your patch.

I see, that's:
...
$ gdb -q -batch -ex "help -"
Scroll window backward.
Usage: - [N] [WIN]
Scroll window WIN N lines backwards.  Both WIN and N are optional, N
defaults to 1, and WIN defaults to the currently focused window.
...

FWIW, did not find any documentation for this command.

Anyway, the '-' prefix seems a poor choice.

I've updated the patch to implement the ignore-errors idiom natively (so
it also works without python).

Also, I've managed to fix the hang, by copying some code from
execute_gdb_command.

Thanks,
- Tom


[-- Attachment #2: 0001-gdb-cli-Add-ignore-errors-command.patch --]
[-- Type: text/x-patch, Size: 2689 bytes --]

[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 command
failed which stopped script execution.

I could work around this by splitting the script at each error, but I realized
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 ).  This also
mentions a bugzilla entry that adds "exception handling to the gdb command
language" but I was not able to find that.

This patch adds native ignore-errors support (so no python needed).

So with this script:
...
$ cat script.gdb
ignore-errors run
echo here
...
we have:
...
$ gdb -q -batch -x script.gdb
here$
...

We could also implement this as first-class command, like so:
...
static void
ignore_errors_command (const char *args, int from_tty)
{
  try
    { execute_command (args, from_tty); }
  catch (const gdb_exception_error &ex)
    { async_enable_stdin (); }
}
...
and:
...
  add_cmd ("ignore-errors", class_support, ignore_errors_command,
           source_help_text, &cmdlist);
...
but that means we go twice through execute_command.  This seems cleaner, at
least in that aspect.

---
 gdb/event-top.c | 29 +++++++++++++++++++++++++++--
 1 file changed, 27 insertions(+), 2 deletions(-)

diff --git a/gdb/event-top.c b/gdb/event-top.c
index 002a7dc95e0..9d36b550dda 100644
--- a/gdb/event-top.c
+++ b/gdb/event-top.c
@@ -583,13 +583,38 @@ command_handler (const char *command)
   /* Do not execute commented lines.  */
   for (c = command; *c == ' ' || *c == '\t'; c++)
     ;
-  if (c[0] != '#')
+  if (c[0] == '#')
+    return;
+
+  /* Detect and skip "ignore-errors". */
+  const char * s = "ignore-errors";
+  const size_t s_len = strlen (s);
+  bool ignore_errors_p
+    = (strncmp (c, s, s_len) == 0
+       && strlen (c) > s_len
+       && (c[s_len] == ' ' || c[s_len] == '\t'));
+  if (ignore_errors_p)
     {
-      execute_command (command, ui->instream == ui->stdin_stream);
+      c += s_len;
+      for (; *c == ' ' || *c == '\t'; c++)
+	;
+    }
+
+  try
+    {
+      execute_command (c, ui->instream == ui->stdin_stream);
 
       /* Do any commands attached to breakpoint we stopped at.  */
       bpstat_do_actions ();
     }
+  catch (const gdb_exception_error &ex)
+    {
+      if (!ignore_errors_p)
+	throw;
+
+      /* See also execute_gdb_command.  */
+      async_enable_stdin ();
+    }
 }
 
 /* Append RL, an input line returned by readline or one of its

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [RFC][gdb/cli] Ignore error in gdb command script
  2021-05-18 13:57   ` Tom de Vries
@ 2021-05-18 14:10     ` Simon Marchi
  2021-05-18 14:42       ` Tom Tromey
  2021-05-18 14:23     ` Andrew Burgess
  1 sibling, 1 reply; 15+ messages in thread
From: Simon Marchi @ 2021-05-18 14:10 UTC (permalink / raw)
  To: Tom de Vries, Marco Barisione; +Cc: Tom Tromey, GDB patches mailing list

On 2021-05-18 9:57 a.m., Tom de Vries wrote:> On 5/18/21 1:12 PM, Marco Barisione wrote:
>> On 18 May 2021, at 10:59, Tom de Vries <tdevries@suse.de> wrote:
>>> Hi,
>>>
>>> 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 command
>>> failed which stopped script execution.
>>>
>>> I could work around this by splitting the script at each error, but I realized
>>> it would be nice if I could tell gdb to ignore the error.
>>>
>>> Inspired by make, I chose the '-' prefix.
>>
>> As MI commands are prefixed by “-“, isn’t there a risk of confusion?
>>
> 
> Ah, right, I tend to forget about MI, good point.
> 
>> There’s also a “-” command (see tui/tui-win.c) which will stop working with
>> your patch.
> 
> I see, that's:
> ...
> $ gdb -q -batch -ex "help -"
> Scroll window backward.
> Usage: - [N] [WIN]
> Scroll window WIN N lines backwards.  Both WIN and N are optional, N
> defaults to 1, and WIN defaults to the currently focused window.
> ...
> 
> FWIW, did not find any documentation for this command.
> 
> Anyway, the '-' prefix seems a poor choice.
> 
> I've updated the patch to implement the ignore-errors idiom natively (so
> it also works without python).
> 
> Also, I've managed to fix the hang, by copying some code from
> execute_gdb_command.

That sounds useful.  I think that "ignore-errors" is a good initial
name, because it's clear and self-describing.  We can always find and
add a short alias later.

I didn't look much at the patch itself, but read the commit message.  I
would vote for implementing it as a command, to avoid adding more
complexity / exceptions / special cases in the command handling code.

Simon

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [RFC][gdb/cli] Ignore error in gdb command script
  2021-05-18 13:57   ` Tom de Vries
  2021-05-18 14:10     ` Simon Marchi
@ 2021-05-18 14:23     ` Andrew Burgess
  1 sibling, 0 replies; 15+ messages in thread
From: Andrew Burgess @ 2021-05-18 14:23 UTC (permalink / raw)
  To: Tom de Vries; +Cc: Marco Barisione, Tom Tromey, GDB patches mailing list

* Tom de Vries <tdevries@suse.de> [2021-05-18 15:57:42 +0200]:

> On 5/18/21 1:12 PM, Marco Barisione wrote:
> > On 18 May 2021, at 10:59, Tom de Vries <tdevries@suse.de> wrote:
> >> Hi,
> >>
> >> 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 command
> >> failed which stopped script execution.
> >>
> >> I could work around this by splitting the script at each error, but I realized
> >> it would be nice if I could tell gdb to ignore the error.
> >>
> >> Inspired by make, I chose the '-' prefix.
> > 
> > As MI commands are prefixed by “-“, isn’t there a risk of confusion?
> > 
> 
> Ah, right, I tend to forget about MI, good point.
> 
> > There’s also a “-” command (see tui/tui-win.c) which will stop working with
> > your patch.
> 
> I see, that's:
> ...
> $ gdb -q -batch -ex "help -"
> Scroll window backward.
> Usage: - [N] [WIN]
> Scroll window WIN N lines backwards.  Both WIN and N are optional, N
> defaults to 1, and WIN defaults to the currently focused window.
> ...
> 
> FWIW, did not find any documentation for this command.
> 
> Anyway, the '-' prefix seems a poor choice.
> 
> I've updated the patch to implement the ignore-errors idiom natively (so
> it also works without python).
> 
> Also, I've managed to fix the hang, by copying some code from
> execute_gdb_command.
> 
> Thanks,
> - Tom
> 

> [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 command
> failed which stopped script execution.
> 
> I could work around this by splitting the script at each error, but I realized
> 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 ).  This also
> mentions a bugzilla entry that adds "exception handling to the gdb command
> language" but I was not able to find that.
> 
> This patch adds native ignore-errors support (so no python needed).
> 
> So with this script:
> ...
> $ cat script.gdb
> ignore-errors run
> echo here
> ...
> we have:
> ...
> $ gdb -q -batch -x script.gdb
> here$
> ...
> 
> We could also implement this as first-class command, like so:
> ...
> static void
> ignore_errors_command (const char *args, int from_tty)
> {
>   try
>     { execute_command (args, from_tty); }
>   catch (const gdb_exception_error &ex)
>     { async_enable_stdin (); }
> }
> ...
> and:
> ...
>   add_cmd ("ignore-errors", class_support, ignore_errors_command,
>            source_help_text, &cmdlist);
> ...
> but that means we go twice through execute_command.  This seems cleaner, at
> least in that aspect.

The benefit of implementing as a first class command is that we can
also get command completion on the second-level command.  See, for
example, the existing "with" command, so I can do:

  (gdb) with prin<TAB>

and get:

  (gdb) with print

It would be great it we got this for ignore-errors too.

Thanks,
Andrew



> 
> ---
>  gdb/event-top.c | 29 +++++++++++++++++++++++++++--
>  1 file changed, 27 insertions(+), 2 deletions(-)
> 
> diff --git a/gdb/event-top.c b/gdb/event-top.c
> index 002a7dc95e0..9d36b550dda 100644
> --- a/gdb/event-top.c
> +++ b/gdb/event-top.c
> @@ -583,13 +583,38 @@ command_handler (const char *command)
>    /* Do not execute commented lines.  */
>    for (c = command; *c == ' ' || *c == '\t'; c++)
>      ;
> -  if (c[0] != '#')
> +  if (c[0] == '#')
> +    return;
> +
> +  /* Detect and skip "ignore-errors". */
> +  const char * s = "ignore-errors";
> +  const size_t s_len = strlen (s);
> +  bool ignore_errors_p
> +    = (strncmp (c, s, s_len) == 0
> +       && strlen (c) > s_len
> +       && (c[s_len] == ' ' || c[s_len] == '\t'));
> +  if (ignore_errors_p)
>      {
> -      execute_command (command, ui->instream == ui->stdin_stream);
> +      c += s_len;
> +      for (; *c == ' ' || *c == '\t'; c++)
> +	;
> +    }
> +
> +  try
> +    {
> +      execute_command (c, ui->instream == ui->stdin_stream);
>  
>        /* Do any commands attached to breakpoint we stopped at.  */
>        bpstat_do_actions ();
>      }
> +  catch (const gdb_exception_error &ex)
> +    {
> +      if (!ignore_errors_p)
> +	throw;
> +
> +      /* See also execute_gdb_command.  */
> +      async_enable_stdin ();
> +    }
>  }
>  
>  /* Append RL, an input line returned by readline or one of its


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [RFC][gdb/cli] Ignore error in gdb command script
  2021-05-18 14:10     ` Simon Marchi
@ 2021-05-18 14:42       ` Tom Tromey
  2021-05-18 15:16         ` Tom de Vries
  0 siblings, 1 reply; 15+ messages in thread
From: Tom Tromey @ 2021-05-18 14:42 UTC (permalink / raw)
  To: Simon Marchi via Gdb-patches
  Cc: Tom de Vries, Marco Barisione, Simon Marchi, Tom Tromey

>>>>> "Simon" == Simon Marchi via Gdb-patches <gdb-patches@sourceware.org> writes:

Simon> That sounds useful.  I think that "ignore-errors" is a good initial
Simon> name, because it's clear and self-describing.  We can always find and
Simon> add a short alias later.

Fedora has shipped a Python implementation of exactly this for a while now.
So, +1 for this name and approach from me.

One question is whether it should catch 'quit'.  I tend to think not but
it should be considered.

There is some other prior art too:

    https://sourceware.org/bugzilla/show_bug.cgi?id=8487

ignore-errors covers all the uses I've ever wanted personally, though,
so I think it would be fine to just go with that.  However if someone is
feeling more maximal, try-catch would also be an ok addition.

Tom

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [RFC][gdb/cli] Ignore error in gdb command script
  2021-05-18 14:42       ` Tom Tromey
@ 2021-05-18 15:16         ` Tom de Vries
  2021-05-18 15:32           ` Tom Tromey
  2021-05-18 19:16           ` [RFC][gdb/cli] Ignore error in gdb command script Philippe Waroquiers
  0 siblings, 2 replies; 15+ messages in thread
From: Tom de Vries @ 2021-05-18 15:16 UTC (permalink / raw)
  To: Tom Tromey, Simon Marchi via Gdb-patches

[-- Attachment #1: Type: text/plain, Size: 1166 bytes --]

On 5/18/21 4:42 PM, Tom Tromey wrote:
>>>>>> "Simon" == Simon Marchi via Gdb-patches <gdb-patches@sourceware.org> writes:
> 
> Simon> That sounds useful.  I think that "ignore-errors" is a good initial
> Simon> name, because it's clear and self-describing.  We can always find and
> Simon> add a short alias later.
> 
> Fedora has shipped a Python implementation of exactly this for a while now.
> So, +1 for this name and approach from me.
> 
> One question is whether it should catch 'quit'.  I tend to think not but
> it should be considered.
> 
> There is some other prior art too:
> 
>     https://sourceware.org/bugzilla/show_bug.cgi?id=8487
> 
> ignore-errors covers all the uses I've ever wanted personally, though,
> so I think it would be fine to just go with that.  However if someone is
> feeling more maximal, try-catch would also be an ok addition.

[ Thanks all for the feedback.  Replying to latest email, CC-ing Andrew. ]

Changes:
- now a proper command
- added error reporting
- added completion
- mention try-catch patch
- mention pre-existing implementation on distros
- mention quit behaviour.

I'm doing a build & reg-test now.

Thanks,
- Tom

[-- Attachment #2: 0001-gdb-cli-Add-ignore-errors-command.patch --]
[-- Type: text/x-patch, Size: 3159 bytes --]

[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 command
failed which stopped script execution.

I could work around this by splitting the script at each error, but I realized
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=8487 ).

This patch adds native ignore-errors support (so no python needed).

So with this script:
...
$ cat script.gdb
ignore-errors run
echo here
...
we have:
...
$ gdb -q -batch -x script.gdb
No executable file specified.
Use the "file" or "exec-file" command.
here$
...

Note that quit is not caught:
...
$ gdb.sh -q
(gdb) ignore-errors quit
$
...
which is the same behaviour as with the python implementation.

gdb/ChangeLog:

2021-05-18  Tom de Vries  <tdevries@suse.de>

	* cli/cli-cmds.c (ignore_errors_command_completer)
	(ignore_errors_command): New function.
	(_initialize_cli_cmds): Add "ignore-errors" cmd.

---
 gdb/cli/cli-cmds.c | 37 +++++++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)

diff --git a/gdb/cli/cli-cmds.c b/gdb/cli/cli-cmds.c
index 0bf418e510e..c2c3ee49eb3 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"
 
 #include "ui-out.h"
 #include "interps.h"
@@ -2249,6 +2250,34 @@ gdb_maint_setting_str_internal_fn (struct gdbarch *gdbarch,
 				 gdbarch);
 }
 
+/* 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,12 @@ when GDB is started."), GDBINIT);
   c = add_cmd ("source", class_support, source_command,
 	       source_help_text, &cmdlist);
   set_cmd_completer (c, filename_completer);
+
+  const char *ignore_errors_help_text
+    = ("Execute a single command, ignoring all errors.\n"
+       "Only one-line commands are supported.\n"
+       "This is primarily useful in scripts.");
+  c = add_cmd ("ignore-errors", class_support, ignore_errors_command,
+	       ignore_errors_help_text, &cmdlist);
+  set_cmd_completer (c, ignore_errors_command_completer);
 }

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [RFC][gdb/cli] Ignore error in gdb command script
  2021-05-18 15:16         ` Tom de Vries
@ 2021-05-18 15:32           ` Tom Tromey
  2021-05-19  6:25             ` [PATCH][gdb/cli] Add ignore-errors command Tom de Vries
  2021-05-18 19:16           ` [RFC][gdb/cli] Ignore error in gdb command script Philippe Waroquiers
  1 sibling, 1 reply; 15+ messages in thread
From: Tom Tromey @ 2021-05-18 15:32 UTC (permalink / raw)
  To: Tom de Vries; +Cc: Tom Tromey, Simon Marchi via Gdb-patches

>>>>> "Tom" == Tom de Vries <tdevries@suse.de> 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 _().

Tom

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [RFC][gdb/cli] Ignore error in gdb command script
  2021-05-18 15:16         ` Tom de Vries
  2021-05-18 15:32           ` Tom Tromey
@ 2021-05-18 19:16           ` Philippe Waroquiers
  2021-05-18 21:59             ` Tom de Vries
  1 sibling, 1 reply; 15+ messages in thread
From: Philippe Waroquiers @ 2021-05-18 19:16 UTC (permalink / raw)
  To: Tom de Vries, Tom Tromey, Simon Marchi via Gdb-patches



On Tue, 2021-05-18 at 17:16 +0200, Tom de Vries wrote:
> On 5/18/21 4:42 PM, Tom Tromey wrote:
> > > > > > > "Simon" == Simon Marchi via Gdb-patches <gdb-patches@sourceware.org> writes:
> > 
> > Simon> That sounds useful.  I think that "ignore-errors" is a good initial
> > Simon> name, because it's clear and self-describing.  We can always find and
> > Simon> add a short alias later.
> > 
> > Fedora has shipped a Python implementation of exactly this for a while now.
> > So, +1 for this name and approach from me.
> > 
> > One question is whether it should catch 'quit'.  I tend to think not but
> > it should be considered.
> > 
> > There is some other prior art too:
> > 
> >     https://sourceware.org/bugzilla/show_bug.cgi?id=8487
> > 
> > ignore-errors covers all the uses I've ever wanted personally, though,
> > so I think it would be fine to just go with that.  However if someone is
> > feeling more maximal, try-catch would also be an ok addition.
> 
> [ Thanks all for the feedback.  Replying to latest email, CC-ing Andrew. ]
> 
> Changes:
> - now a proper command
Instead of adding an ignore-errors command, an alternative could be to implement
this via a new GDB setting 'set ignore-errors on|off'.

This allows to do e.g.
  with ignore-errors on -- some-command
or (shorter form): 
  with ignore-errors -- some-command
or when needed
  with ignore-errors off -- some-command

This also allows to use
  set ignore-errors on
  ...
at the beginning of a script and/or in .gdbinit 
  and/or as a -ex 'set ignore-errors on' gdb startup arg.


Also, an alias can be defined such as:
  alias ie = with ignore-errors on --

Philippe


> - added error reporting
> - added completion
> - mention try-catch patch
> - mention pre-existing implementation on distros
> - mention quit behaviour.
> 
> I'm doing a build & reg-test now.
> 
> Thanks,
> - Tom


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [RFC][gdb/cli] Ignore error in gdb command script
  2021-05-18 19:16           ` [RFC][gdb/cli] Ignore error in gdb command script Philippe Waroquiers
@ 2021-05-18 21:59             ` Tom de Vries
  2021-05-20  8:02               ` Philippe Waroquiers
  0 siblings, 1 reply; 15+ messages in thread
From: Tom de Vries @ 2021-05-18 21:59 UTC (permalink / raw)
  To: Philippe Waroquiers, Tom Tromey, Simon Marchi via Gdb-patches

On 5/18/21 9:16 PM, Philippe Waroquiers wrote:
> 
> 
> On Tue, 2021-05-18 at 17:16 +0200, Tom de Vries wrote:
>> On 5/18/21 4:42 PM, Tom Tromey wrote:
>>>>>>>> "Simon" == Simon Marchi via Gdb-patches <gdb-patches@sourceware.org> writes:
>>>
>>> Simon> That sounds useful.  I think that "ignore-errors" is a good initial
>>> Simon> name, because it's clear and self-describing.  We can always find and
>>> Simon> add a short alias later.
>>>
>>> Fedora has shipped a Python implementation of exactly this for a while now.
>>> So, +1 for this name and approach from me.
>>>
>>> One question is whether it should catch 'quit'.  I tend to think not but
>>> it should be considered.
>>>
>>> There is some other prior art too:
>>>
>>>     https://sourceware.org/bugzilla/show_bug.cgi?id=8487
>>>
>>> ignore-errors covers all the uses I've ever wanted personally, though,
>>> so I think it would be fine to just go with that.  However if someone is
>>> feeling more maximal, try-catch would also be an ok addition.
>>
>> [ Thanks all for the feedback.  Replying to latest email, CC-ing Andrew. ]
>>
>> Changes:
>> - now a proper command
> Instead of adding an ignore-errors command, an alternative could be to implement
> this via a new GDB setting 'set ignore-errors on|off'.
> 
> This allows to do e.g.
>   with ignore-errors on -- some-command
> or (shorter form): 
>   with ignore-errors -- some-command
> or when needed
>   with ignore-errors off -- some-command
> 
> This also allows to use
>   set ignore-errors on
>   ...
> at the beginning of a script and/or in .gdbinit 
>   and/or as a -ex 'set ignore-errors on' gdb startup arg.
> 
> 
> Also, an alias can be defined such as:
>   alias ie = with ignore-errors on --
> 

Hi Philippe,

good ideas, I'd say, but for now I just want to do the basic
functionality as it has been available and used for some time.

This could be implemented as follow-up patch, and I don't see a problem
with that approach.

Thanks,
- Tom

^ permalink raw reply	[flat|nested] 15+ messages in thread

* [PATCH][gdb/cli] Add ignore-errors command
  2021-05-18 15:32           ` Tom Tromey
@ 2021-05-19  6:25             ` Tom de Vries
  2021-05-19  7:32               ` Andreas Schwab
  0 siblings, 1 reply; 15+ messages in thread
From: Tom de Vries @ 2021-05-19  6:25 UTC (permalink / raw)
  To: Tom Tromey; +Cc: Simon Marchi via Gdb-patches

[-- Attachment #1: Type: text/plain, Size: 700 bytes --]

[ was: [RFC][gdb/cli] Ignore error in gdb command script ]

On 5/18/21 5:32 PM, Tom Tromey wrote:
>>>>>> "Tom" == Tom de Vries <tdevries@suse.de> 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


[-- Attachment #2: 0001-gdb-cli-Add-ignore-errors-command.patch --]
[-- Type: text/x-patch, Size: 6241 bytes --]

[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 command
failed which stopped script execution.

I could work around this by splitting the script at each error, but I realized
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=8487 ).

This patch adds native ignore-errors support (so no python needed).

So with this script:
...
$ cat script.gdb
ignore-errors run
echo here
...
we have:
...
$ gdb -q -batch -x script.gdb
No executable file specified.
Use the "file" or "exec-file" command.
here$
...

Note that quit is not caught:
...
$ gdb -q
(gdb) ignore-errors quit
$
...
which is the same behaviour as with the python implementation.

Tested on x86_64-linux.

gdb/ChangeLog:

2021-05-18  Tom de Vries  <tdevries@suse.de>

	* 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  <tdevries@suse.de>

	* gdb.texinfo (Command Files): Document command ignore-errors.

gdb/testsuite/ChangeLog:

2021-05-18  Tom de Vries  <tdevries@suse.de>

	* 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"
 
 #include "ui-out.h"
 #include "interps.h"
@@ -2249,6 +2250,34 @@ gdb_maint_setting_str_internal_fn (struct gdbarch *gdbarch,
 				 gdbarch);
 }
 
+/* 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 = add_cmd ("source", class_support, source_command,
 	       source_help_text, &cmdlist);
   set_cmd_completer (c, filename_completer);
+
+  c = 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 executed 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.
 
 @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
 
 
diff --git a/gdb/testsuite/gdb.base/ignore-errors.exp b/gdb/testsuite/gdb.base/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 <http://www.gnu.org/licenses/>.  
*/
+
+# 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.base/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

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH][gdb/cli] Add ignore-errors command
  2021-05-19  6:25             ` [PATCH][gdb/cli] Add ignore-errors command Tom de Vries
@ 2021-05-19  7:32               ` Andreas Schwab
  2021-05-19  7:36                 ` Tom de Vries
  0 siblings, 1 reply; 15+ messages in thread
From: Andreas Schwab @ 2021-05-19  7:32 UTC (permalink / raw)
  To: Tom de Vries; +Cc: Tom Tromey, Simon Marchi via Gdb-patches

On Mai 19 2021, Tom de Vries wrote:

> This patch adds native ignore-errors support (so no python needed).

This makes abbreviations of "ignore" abigous.

Andreas.

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH][gdb/cli] Add ignore-errors command
  2021-05-19  7:32               ` Andreas Schwab
@ 2021-05-19  7:36                 ` Tom de Vries
  0 siblings, 0 replies; 15+ messages in thread
From: Tom de Vries @ 2021-05-19  7:36 UTC (permalink / raw)
  To: Andreas Schwab; +Cc: Tom Tromey, Simon Marchi via Gdb-patches

On 5/19/21 9:32 AM, Andreas Schwab wrote:
> On Mai 19 2021, Tom de Vries wrote:
> 
>> This patch adds native ignore-errors support (so no python needed).
> 
> This makes abbreviations of "ignore" abigous.

Indeed.

Can already be demonstrated using the system gdb on openSUSE:
...
(gdb) ign<TAB>
Ambiguous command "ign": ignore, ignore-errors.
(gdb)
...

Thanks,
- Tom


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [RFC][gdb/cli] Ignore error in gdb command script
  2021-05-18 21:59             ` Tom de Vries
@ 2021-05-20  8:02               ` Philippe Waroquiers
  2021-05-20 15:01                 ` Simon Marchi
  0 siblings, 1 reply; 15+ messages in thread
From: Philippe Waroquiers @ 2021-05-20  8:02 UTC (permalink / raw)
  To: Tom de Vries, Tom Tromey, Simon Marchi via Gdb-patches

On Tue, 2021-05-18 at 23:59 +0200, Tom de Vries wrote:
> On 5/18/21 9:16 PM, Philippe Waroquiers wrote:
> > 
> > On Tue, 2021-05-18 at 17:16 +0200, Tom de Vries wrote:
> > > On 5/18/21 4:42 PM, Tom Tromey wrote:
> > > > > > > > > "Simon" == Simon Marchi via Gdb-patches <gdb-patches@sourceware.org> writes:
> > > > 
> > > > Simon> That sounds useful.  I think that "ignore-errors" is a good initial
> > > > Simon> name, because it's clear and self-describing.  We can always find and
> > > > Simon> add a short alias later.
> > > > 
> > > > Fedora has shipped a Python implementation of exactly this for a while now.
> > > > So, +1 for this name and approach from me.
> > > > 
> > > > One question is whether it should catch 'quit'.  I tend to think not but
> > > > it should be considered.
> > > > 
> > > > There is some other prior art too:
> > > > 
> > > >     https://sourceware.org/bugzilla/show_bug.cgi?id=8487
> > > > 
> > > > ignore-errors covers all the uses I've ever wanted personally, though,
> > > > so I think it would be fine to just go with that.  However if someone is
> > > > feeling more maximal, try-catch would also be an ok addition.
> > > 
> > > [ Thanks all for the feedback.  Replying to latest email, CC-ing Andrew. ]
> > > 
> > > Changes:
> > > - now a proper command
> > Instead of adding an ignore-errors command, an alternative could be to implement
> > this via a new GDB setting 'set ignore-errors on|off'.
> > 
> > This allows to do e.g.
> >   with ignore-errors on -- some-command
> > or (shorter form): 
> >   with ignore-errors -- some-command
> > or when needed
> >   with ignore-errors off -- some-command
> > 
> > This also allows to use
> >   set ignore-errors on
> >   ...
> > at the beginning of a script and/or in .gdbinit 
> >   and/or as a -ex 'set ignore-errors on' gdb startup arg.
> > 
> > 
> > Also, an alias can be defined such as:
> >   alias ie = with ignore-errors on --
> > 
> 
> Hi Philippe,
> 
> good ideas, I'd say, but for now I just want to do the basic
> functionality as it has been available and used for some time.
> 
> This could be implemented as follow-up patch, and I don't see a problem
> with that approach.

Not too sure to understand what is meant by 'follow-up' patch as the approaches
are not really aligned:  implementing the 'setting approach' later will then add
a different way rather than extend this one.

For what concerns the naming of the ignore-errors command (discussed in another mail
e.g. as it it makes 'ignore' abbreviations ambiguous): it is worth
mentioning that 'thread apply' and 'frame apply' have -c and -s
arguments to indicate respectively  'print any error and continue'
and 'silently ignore any error'.

So, maybe the naming of the new setting could be
  set error-handling [abort-execution|print-and-continue|silently-ignore]
(default value abort-execution).

If the 'setting approach' is  not the initial absolutely to do preferred approach :),
then the command could be:
   error-handle [-a|-c|-s] [--]  COMMAND
(with -c being the default if no flag specified).

Thanks

Philippe




^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [RFC][gdb/cli] Ignore error in gdb command script
  2021-05-20  8:02               ` Philippe Waroquiers
@ 2021-05-20 15:01                 ` Simon Marchi
  0 siblings, 0 replies; 15+ messages in thread
From: Simon Marchi @ 2021-05-20 15:01 UTC (permalink / raw)
  To: Philippe Waroquiers, Tom de Vries, Tom Tromey,
	Simon Marchi via Gdb-patches

On 2021-05-20 4:02 a.m., Philippe Waroquiers via Gdb-patches wrote:
> Not too sure to understand what is meant by 'follow-up' patch as the approaches
> are not really aligned:  implementing the 'setting approach' later will then add
> a different way rather than extend this one.
> 
> For what concerns the naming of the ignore-errors command (discussed in another mail
> e.g. as it it makes 'ignore' abbreviations ambiguous): it is worth
> mentioning that 'thread apply' and 'frame apply' have -c and -s
> arguments to indicate respectively  'print any error and continue'
> and 'silently ignore any error'.
> 
> So, maybe the naming of the new setting could be
>   set error-handling [abort-execution|print-and-continue|silently-ignore]
> (default value abort-execution).
> 
> If the 'setting approach' is  not the initial absolutely to do preferred approach :),
> then the command could be:
>    error-handle [-a|-c|-s] [--]  COMMAND
> (with -c being the default if no flag specified).

I agree that we should choose whether we want a command or a setting,
but not add two things to do the same thing.

And I think Philippe has a good point about the naming (that applies to
both if it's a command or a setting) to ensure extensibility.

Simon

^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2021-05-20 15:01 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-18  9:59 [RFC][gdb/cli] Ignore error in gdb command script Tom de Vries
2021-05-18 11:12 ` Marco Barisione
2021-05-18 13:57   ` Tom de Vries
2021-05-18 14:10     ` Simon Marchi
2021-05-18 14:42       ` Tom Tromey
2021-05-18 15:16         ` Tom de Vries
2021-05-18 15:32           ` Tom Tromey
2021-05-19  6:25             ` [PATCH][gdb/cli] Add ignore-errors command Tom de Vries
2021-05-19  7:32               ` Andreas Schwab
2021-05-19  7:36                 ` Tom de Vries
2021-05-18 19:16           ` [RFC][gdb/cli] Ignore error in gdb command script Philippe Waroquiers
2021-05-18 21:59             ` Tom de Vries
2021-05-20  8:02               ` Philippe Waroquiers
2021-05-20 15:01                 ` Simon Marchi
2021-05-18 14:23     ` Andrew Burgess

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).