Add a way to invoke redefined (overridden) GDB commands

Pedro Alves pedro@palves.net
Mon Oct 12 11:50:27 GMT 2020


On 9/14/20 10:39 AM, Marco Barisione wrote:
> Currently, when a GDB command is redefined, the original implementation is not
> available any more.  This makes it difficult to build features on top of
> existing commands.
> 
> Last year I submitted a patch to fix this but I ran out of time to address the
> review comments (the original patch was sent on the 28th of October 2019).
> These patches restart that work and should address all the comments I got last
> time.  As the patchea are very different and a long time passed, I'm
> submitting as a new series.
> 
> My patches add a new "uplevel" command and a new gdb.Command.invoke_uplevel
> method inspired by TCL (as initially suggested by Andrew Burgess) so you can
> do this:
> 
>     (gdb) define run
>     echo Will run!\n
>     uplevel 0 run
>     end
>     (gdb) run
>     Will run!
>     [... normal output of run ...]
> 
> 
> There are a couple of other things which could be added to make the "uplevel"
> command more helpful, but I think they are out of scope and my patches are
> already useful as they are.

So I'm looking at this afresh, and really questioning this "uplevel N"
design.  This it not really like TCL's "uplevel".  With TCL's uplevel,
you are accessing a different scope or frame, not a previous implementation
of the function that was overwritten.  To me, the naming choice is
confusing, from that angle.  If someone extends GDB's CLI to gain support
for local variables, then a really-TCL-like uplevel is likely handy, and
then calling that feature "uplevel" would be good.

I also question whether "uplevel N" with "N>0" is really usable, since
in general you don't know what other scripts may have overridden.  E.g.,
you never know what "uplevel 3 cmd" will run, since you don't know how
many scripts redefined/overridden cmd.

If we stick with the TCL inspiration, I think a better approach would
be to add support for renaming commands, like TCL's rename command:

  https://www.tcl.tk/man/tcl8.4/TclCmd/rename.htm

So a user would do:

 (gdb) rename run org_run
 (gdb) define run
 > echo Will run!\n
 > org_run
 > end
 (gdb) run
  Will run!
  [... normal output of run ...]
 (gdb) org_run
  [... normal output of run ...]

(You can find many examples of TCL's rename in use in GDB's testsuite.)

Thanks,
Pedro Alves

> 
> The first thing is adding a way of accessing the untokenised arguments to a
> command via something like "$arg@" (Andrew Burgess suggested "$argv", but
> Pedro Alves pointed out that would look like an argument vector).
> 
> Another thing which could be added is the ability to do "uplevel -1 ..." to
> access the directly redefined command.
> This is implemented in Python but I couldn't find an obvious way of doing that
> for the "uplevel" command as there's no way of knowing which command you are
> currently executing (at least from what I could see).
> Maybe it could be implemented in a similar way to how command arguments are
> kept around with scoped_user_args_level, so we could keep a stack of all (user
> and non-user) commands which are being executed.  By checking the latest one
> you can know what "uplevel -1" would apply to.
> 



More information about the Gdb-patches mailing list