Wednesday, June 15, 2016

Quick debug tip with Vi

Recently I had to debug a 2k lines perl file which was hanging somewhere without any message. Having never looked into the code before I was about to go through the traditional "print 'I am here!'" binary search in the code when I got an idea.

A couple of searches later and I built my vi command:

  • :%s/^$/\=printf('print "%-4d\\n";', line('.'))

What this does is replace every empty line in the code with a print of the number of the line.

e.g. if line 10 is empty then it will be be replaced with:
  • print "10\n";
Otherwise it will be left as is.

Turns out the code was blocking on line 954.


No comments:

Post a Comment