Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging Vim plugins with call traces

Tags:

vim

vim-plugin

What is the preferable, general method of debugging/tracing vim plugins ? Suppose I have got a fairly sophisticated plugin Foo, which on a key pressed F9 opens new window with file-browser and gives the possibility to choose a file, after that the filename is copied into the main window. I would like to see what is called when I press the F9 key, some kind of call trace.

like image 323
Darek Avatar asked Mar 11 '12 15:03

Darek


People also ask

Is there a debugger in Vim?

The Termdebug Plugin This plugin is available since version 8.1 of Vim and it's part of its terminal window feature. The first view is where the gdb command is executed. Here, we can type debug commands so that we can start the debugging process, set breakpoints, move to the next line, and more.

What is Vimspector?

vimspector - A multi-language debugging system for Vim.


3 Answers

The best way I have found is to use the -V flag when starting (g)vim. You can specify a level of tracing N and a filename for the written log:

$ vim -V[N]{filename}

Then trace messages will be given for each file that is sourced. (See :help -V for more info.)

Trawling through the resulting logfile can be painful, but it is usually pretty informative. I find it best to view the logfile before and after the trigger event (pressing <F9> in your case) to get a picture of when is happening.

like image 191
Prince Goulash Avatar answered Oct 18 '22 19:10

Prince Goulash


If you already have vim open, try executing the command manually under VIM's built in debugger.

1) Find out what vim does when you press the key

:map <F-9>

2) Run the mapped command manually under debugger

:debug _mapped_command_

3) Right now you should be dropped in to the debugger, so

set verbose=20

4) And finally press n and Enter key to continue running the script

At this point you should see a whole bunch of output on the screen. You can press Space to scroll the screen, j/k to move by line.

Any output that starts with "Line #:" is the line vim is executing at that time.

like image 45
Frison Alexander Avatar answered Oct 18 '22 17:10

Frison Alexander


For sophisticated plugins, normally command line debugging or tracing is not enough.

You can use BreakPts to do a visual debug inside vim.

It is based on remote debugging, so you need to debug a server instance of vim.

Basically:

Terminal 1:

$ vim --servername Foo
...
set breakpoint on any Foo function
do whatever operation which trigger Foo logic
...

Terminal 2:

$ vim
:BreakPts
:BPRemoteServ FOO
:BPDWhere locate (actual debug execution point)
:BPDNext or F12 (next execution line)
:BPDStep or F11 (step inside functions, dictionary functions)
:BPDEvaluate or F8 (if pressed on visual selection evaluates that)
:BPDCont or F5 (continue execution)

See some plugins are loaded dinamically so you need to operate with them prior to set breakpoints.

Once loaded you can set breakpoints from connected vim with:

:BPFunctions (Show debuggeable fuctions on RemoteServer)
:BPScripts (Show debuggeable scripts on RemoteServer)
:BPPoints (Show defined breakpoints on RemoteServer)

I have fix/tweak/evolve a lot of vim plugins thanks to this great plugin.

like image 44
albfan Avatar answered Oct 18 '22 18:10

albfan