Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limiting the amount of information printed by Perl debugger

Tags:

debugging

perl

One of my pet peeves with debugging Perl code (in command line debbugger, perl -d) is the fact that mistakenly printing (via x command) the contents of a huge datastructure is guaranteed to freeze up your terminal for forever and a half while 100s of pages of data are printed. Epecially if that happens across slowish network.

As such, I'd like to be able to limit the amount of data that x prints.

I see two approaches - I'd be willing to try either if someone knows how to do.

  1. Limit the amount of data any single command in debugger prints.

  2. Better yet, somehow replace the built-in x command with a custom Perl method (which would calculate the "size" of the data structure, and refuse to print its contents without confirmation).

I'm specifically asking "how to replace x with custom code" - building a Good Enough "is the data structure too big" Perl method is something I can likely do on my own without too much effort although I see enough pitfalls preventing the "perfect" one from being a fairly frustrating endeavour. Heck, merely doing Data::Dumper->Dump and taking the length of the string might do the trick :)

Please note that I'm perfectly well aware of how to manually avoid the issue by recursively examining layers of datastructure (e.g. print the ref, print the count of keys/array elements, etc...)... the whole point is I want to be able to avoid thoughtlessly typing x $huge_pile_of_data without thinking - or stumbling on a bug populating said huge pile of data into what should be a scalar.

like image 800
DVK Avatar asked Mar 02 '10 15:03

DVK


2 Answers

The x command takes an optional argument for the maximum depth to display. That's not quite the same as limiting the amount of data to N pages, but it's definitely useful to prevent overload.

  DB<1> %h = (a => { b => { c => 1 } } )

  DB<2> x %h
0  'a'
1  HASH(0x1d5ff44)
   'b' => HASH(0x1d61424)
      'c' => 1

  DB<3> x 2 %h
0  'a'
1  HASH(0x1d5ff44)
   'b' => HASH(0x1d61424)

You can specify the default depth to print via the o command, e.g.

DB<1>o dumpDepth=1

Add that to your .perldb file to apply it to all debugger sessions.

Otherwise, it looks like the x command invokes DB::dumpit() which is just a wrapper for dumpval.pl (or, more specifically, the main::dumpValue() sub it defines). You could modify/replace that script as you see fit. I'm not sure how you'd make it interactive, though.

like image 52
Michael Carman Avatar answered Oct 07 '22 13:10

Michael Carman


The | command in the debugger pipes another command's output to your pager, e.g.

  DB<1> |x %huge_datastructure
like image 42
Greg Bacon Avatar answered Oct 07 '22 14:10

Greg Bacon