Re: [PATCH 2/2] gdb: Allow parenthesis to group arguments to user-defined commands

Tom Tromey
2018-08-31T20:59:18Z

>>>>> "Philippe" == Philippe Waroquiers <philippe.waroquiers@skynet.be> writes:

Philippe> For what concerns the parenthesis based solution, it looks to not work
Philippe> for some cases.

Philippe> E.g. if I want to pass one argument (I am using single quotes to show the args
Philippe> I want in the below examples):
Philippe> 'a b) (c d'
Philippe> then I need to use:
Philippe> some_user_defined (a b) (c d)
Philippe> but that is the same notation as if I want to pass 2 arguments
Philippe> 'a b' and 'c d'

Can you think of an actual example where this would be useful?
My feeling is that there isn't one, though I'd be interested in
tricky-but-plausible counter-examples.

This feeling is why I'm generally ok with Andrew's idea.

Philippe> And it would be nice if the mechanism used to quote args would
Philippe> be compatible between user defined commands and native gdb commands.

There is no universal quoting in gdb. Instead there are 3 common cases,
plus the extras (neglecting MI, which is totally different and not
relevant here):

1. Commands that take an expression. These are things like "print".
Expressions are passed to the language parser, but language parsers
have to follow some gdb rules: optionally terminate parsing at a
top-level (unparenthesized) comma, and also terminate parsing at "if"
or some other things (there is a mildly odd case for Ada tasks).

2. Commands that take a linespec. These can also take an expression via
the "*" linespec, so linespecs end up following some expression
rules. Often there are expressions after the linespec too, like
break LINESPEC if EXPR
or
dprintf LINESPEC, "string", EXPR, EXPR...

3. Commands that use gdb_argv (or libiberty's buildargv, which is the
same thing). These respect some simple quoting. However, the
quoting is very simple, not like what you might expect from the
shell, for example I don't think you can quote an embedded quotation
mark of the same kind (that is, no "this has \"quotes\"").

4. Everything else. gdb commands are just given a string and some do
whatever the author liked.

Philippe> I have not understood the reference given by Tom that expressions
Philippe> are terminated with , and that parenthesis stops this termination.
Philippe> Is that described somewhere in the doc ?
Philippe> The doc (or an example if this is not documented) will help
Philippe> me to understand.

I think it's largely undocumented, since some of these quirks are just
constraints arising from gdb's implementation choices.

Not having any rhyme or reason to command argument parsing has good and
bad facets.

The good is that the generally DWIM nature of commands means that
usually you don't have to contort yourself to satisfy some parser.
Like, you can "break foo.c:93" or "break function" rather than something
like the old dbx "stop in" / "stop at" split.

The bad of course is that you may sometimes want to quote something and
not have any idea of how to do it: because there's no consistency;
because the gdb_argv idea was not really thought through (that's my
conclusion, I don't know the actual story); or perhaps because you've
just tripped across some command that was implemented in a particularly
bad way.

Now, it would be great to fix this, at least for some subset of things.
I find it hard to see a way forward, though. Breaking compatibility
(see my post upthread) is unpleasant, unless maybe it is done in a very
dramatic way, coupled with a major version bump and some kind of huge
warning for users -- but that's also very hard to implement and release.

One idea is to add a new standard way to parse arguments, for new
commands. But of course then gdb just has 5 ways to do it ... :(

Tom