Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

ZLE widget to run gdb on command line



Hi.  I just thought I'd share a widget that I find helpful.  Invoking
gdb is something I need to do a lot lately, it seems, and I often think
I can pass the program's arguments as arguments to gdb:

   gdb myprog arg1 arg2 arg3

But gdb doesn't accept this because it expects other unnamed arguments.
Treating the whole command as a single argument to gdb also fails:

   gdb "myprog arg1 arg2 arg3"

The single argument is treated as a single executable name.  The only
way I know of to pass arguments to the executable is with gdb's run
command at the interpreter.  So, the following widget effectively
transforms the line

   myprog arg1 arg2 arg3

into

   gdb myprog
   gdb-prompt> run arg1 arg2 arg3

.  Any comments or alternative solutions are quite welcome.  I don't
know zle or history expansion very well.

--------------------------
run-in-gdb() {
   # This function is ZLE widget that runs the current command in gdb.
   # Since gdb doesn't take the program's arguments as arguments to gdb
   # itself but rather through the interpreter, a "run" command is
   # printed
   # out to a temporary file which is invoked as script for gdb to grab
   # commands from at startup.

   # Commit current command line to history, inset its words in a gdb
   # command, expand history to bypass HIST_VERIFY, and run it.
   print -s ${(z)BUFFER}
   BUFFER="gdb !!0 -x =(echo run !!1*)"
   zle expand-history
   zle accept-line
}
zle -N run-in-gdb
bindkey "^X^G" run-in-gdb
--------------------------

-- 
Chris Johnson
cjohnson@xxxxxxxxxx
http://www.cs.utk.edu/~cjohnson



Messages sorted by: Reverse Date, Date, Thread, Author