Trepan-Debuggers / zshdb

gdb-like "trepan" debugger for zsh
GNU General Public License v3.0
295 stars 23 forks source link

Debugging a function #15

Closed psprint closed 5 years ago

psprint commented 6 years ago

Much Zsh things today are functions. Scripts may be more important, if someone runs Zsh on backend in some company, however this rather rarely happens as Bash is used in such situations, and there's bashdb. So maybe it's even more correct to say "functions are more important" if it's in Zsh world.

I once had to debug function -fast-highlight-process() located in https://github.com/zdharma/fast-syntax-highlighting/blob/master/fast-highlight. I remember that in order to use zshdb I had to convert fsh to script. From that point debugging went well.

If I recall correctly, I created a script file, sourced fsh there and called the function manually, then started zshdb on that script. I wonder if anything can be done to ease this step. To debug a function that is already loaded into interactive Zsh, zshdb would have to be itself a function. But that's probably hard?

It would be cool to have zshdb easily appearing in current session, waiting at beginning of a requested function.

rocky commented 6 years ago

Although this isn't documented in zshdb and is poorly documented in bashdb, it is possible for a function to call the debugger directly. Here is an example:

#!/usr/bin/zsh

source /usr/share/zshdb/dbg-trace.sh  # adjust location

myfn()
{
  echo $$
  typeset -i i=0
  for ((i=0; i<=200; i++)); do
      date=$(date)
      echo "$date"
      if ((i == 0 )); then
    _Dbg_debugger; :  # debugger is called here
      fi
      sleep 1
  done
}

myfn

Would this do the trick?

psprint commented 6 years ago

It looks very good, I plan to add Zplugin integration that will wrap existing function along following lines (SE answer):

functions[git-old]=$functions[git]
git() {
  _Dbg_debugger
  git-old "$@"
}

So user will be able to choose any function and debug it. But right now I'm working on the things I mentioned earlier.

rocky commented 6 years ago

Many thanks. You probably know this, but just in case....

The sourcing of the trace of dbg-trace needs to be only done once. And it can be done as late as possible, so as not have any possibility of impacting the program otherwise.

rocky commented 5 years ago

Since I haven't heard back, I'll assume this is fixed.

psprint commented 5 years ago

I now can implement a zplugin extension that would automatically wrap functions to debug them. However, how to load zshdb in the current session? So that it's loaded in the background, waiting for activation by the _Dbg_debugger call?

rocky commented 5 years ago

From https://github.com/rocky/zshdb/wiki/Installation

source path-to-zshdb/zshdb/dbg-trace.sh
    # work, work, work.

    _Dbg_debugger
    # start debugging here

Admitedly this needs to be documented better elsehwere (like it is in bashdb.)

psprint commented 5 years ago

It in general works, however after just sourcing the file the shell stops working in multiple aspects, namely the history gets truncated (checked that's because HISTFILE=$HOME/.zshdb_hist) and the completion doesn't work, and running compinit doesn't help.

What steps does the zshdb do to initialize itself? Before I go deeper into dbg-main.sh.

rocky commented 5 years ago

What steps does the zshdb do to initialize itself? Before I go deeper into dbg-main.sh.

Best to go deepr in to dbg-main.sh. It isn't that hard or complicated.

Normally a debugger is in its own read-eval-print loop and so its read is custom for debugger commands not normal zsh interaction. There is however a "shell" command (with aliases "zsh" and "sh" that is more geared for interactive. In other debuggers such as the one for nodejs you can issue debugger commands from inside the more interactive shell window, and perhaps that's what you want to do instead. I don't know.