SlideShare a Scribd company logo
Basics of perl -d

   Steven Lembark
Workhorse Computing
lembark@wrkhors.com
Introducing Perl's Debugger
●   Overview of the obvious uses, common commands.
●   Few less obvious uses:
    ●   Debugging regexen.
    ●   Evaluating structure bloat.
●   Some pitfalls and how to avoid them.
What is the Perl Debugger?
●   The Perl debugger comes with perl, in fact it is perl.
●   '­d' is built into perl's command line.
●   It functions like a perly shell, evaluating source code 
    from files or the command line or executing 
    debugger commands.
●   You also use perl ­d with profiling utilities like 
    NYTprof.
Smarter Than Your Average Code
●   The most obvious thing you can do is walking code 
    to track down bugs.
●   You can also test Perl syntax: just type it in.
●   Use it as a “perly shell” when your one­liners run 
    into multiple lines or you have to eyeball data 
    structures  between commands.
●   Q&D interactive data mining.
●   Good for examining data structures when writing 
    talks on data manglement or module guts.
The Basics: Getting In
●   At the command          $ perl ­d ­e 42;
    line: perl ­d enters    $ perl ­d foo.pm;
    the debugger.
                            $ perl ­d bar;
●   You can start it 
    'vanilla', with          $ perl ­MDevel::Size ­d ­e foo;
    modules to watch 
    startup, with code, 
    or using modules to 
    pull in utilities (e.g. 
    regex debugging).
Basics: The Prompt
●   Once in you get the command prompt:
    $ perl ­d ­e 42
    Loading DB routines from perl5db.pl version 1.22 
    Editor support available.
    Enter h or `h h' for help, or `man perldebug' for more help.
    main::(­e:1):   42
    DB<1>

●   This is a “vanilla” session: there is no running code, 
    you can enter debugger commands, perl syntax.
●   The single “<1>” indicate that this is the outermost 
    call level.
●   The “1” says that this is the first command.
Executing Perl Statements
●   Q: Who remembers what localtime returns?
●   A: How to find out?
     DB<1> x
    localtime
    0 31
    1 31
    2 15
    3 15
    4 5
    5 111
    6 3
    7 165
    8 1
     DB<2>
●   Notice that now I'm at command #2.
Gotchas
●   Each command you type is run its own block.
    ●   Lexical variables like “my $foo” will vanish.
    ●   Local values like “local $” or “local $foo{ bar } = ...” 
        will also be unavailable after the line completes.
●   You can put multiple statements onto a line with 
    semi­colon separators.
●   You can only input one line at a time.
    ●   Cut­and­paste of multiple lines won't work.
Debugger Commands
●   These intentionally look pretty much like gdb.
    ●   On the other hand, if you didn't grow up debugging C 
        code this may not help you much.
●   The most common commands are for running code, 
    managing breakpoints (i.e. stopping code), 
    interrogating values.
●   Please note: “q” gets you out.
    ●   Not “quit”, ^C, ^D, or “getmeoutofhere!”.
Setting Up the Debugger
●   You may want to edit your commands.
●   Installing Term::ReadKey &Term::ReadLine.
●   perl will use your .inputrc if you have one.
●   For example, my .inputrc looks like:
    set editing-mode vi
    set show-all-if-ambiguous on
    with allows ^[k to pull up the last line for editing.
●   Check the doc's if you use Emacs.
Running Code
●   Say some code blows up.
●   You could just run it with “r” from the start.
●   That is handy once to see where it blows up.
●   Usually you want to stop at a particular place to see 
    why it blows up.
●   You can continue to a line no or sub name with:
      c 15
      c call_foomatic
Stepping Code
●   You can also watch the code one line at a time.
    ●   “n” (“next”) steps over the subroutine calls.
    ●   “s” (“step”) steps into the subroutine calls. 
    ●   “r” (“return”) goes back to the caller if you accidentally 
        step one level too deep.
●   One really common combination: 
    ●   “c” to a subroutine that blows up.
    ●   “n” to the point before it dies.
    ●   “s” into the call that failed and see what happens.
Getting Out of a Hole: “r”
●   Sometimes you 's' into the wrong sub (say a DBI 
    call).
    ●   You don't want to abort the session.
    ●   You don't want to “n” your way through DBI.
    ●   Use “r” to return from the current call.
●   This also shows you the return value passed back to 
    the caller. 
    ●   Nice for checking that what you expect gets returned.
    ●   Beware if the structure is really large.
Stopping Code: Breakpoints
●   Breakpoints stop the code.
●   They can include a condition.
●   Say the code blows up at line 842 with a non­
    reference value in $thingy after roughly 8_000 
    iterations.
●   Set a breakpoint and continue:
      <1> b 842 ! ref $thingy
      <2> c
Examining Values
●   “p” prints a value.
●   “x” examines it (similar to Data::Dumper).
  DB<6> p @a = map { $_ => [ 1 ] } ( 'a' .. 'c' )
aARRAY(0xc0f100)bARRAY(0xc12078)cARRAY(0xc0f0d0)

    DB<7> x @a = map { $_ => [ 1 ] } ( 'a' .. 'c' )
0    'a'
1    ARRAY(0xc12060)
     0 1
2    'b'
3    ARRAY(0xc11dc0)
     0 1
4    'c'
5    ARRAY(0xc0e1d8)
     0 1
Hashes Are Lists to “x”
●   Hashes look a little odd at first:
        DB<8> x %a = map { $_ => [ 1 ] } ( 'a' .. 'c' )
    0    'a'
    1    ARRAY(0xc122a0)
         0 1
    2    'b'
    3    ARRAY(0xb07fe0)
         0 1
    4    'c'
    5    ARRAY(0xc122e8)
         0 1

●   They look exactly like the array: a list.
Hashref's Are Structures
●   Examining a hashref shows it as key => value pairs:
      DB<9> x %a
    0 HASH(0xc47008)
       'a' => ARRAY(0xc122a0)
          0 1
       'b' => ARRAY(0xb07fe0)
          0 1
       'c' => ARRAY(0xc122e8)
          0 1
You Don't Always Want It All
●   Occasionally you'll get something like:
    0     ARRAY(0xc99050)
          0 ARRAY(0xc99080)
             0 ARRAY(0xc990b0)
                0 ARRAY(0xc990e0)
                   0 ARRAY(0xc99110)
                      0 ARRAY(0xc99140)
                          empty array
        DB<17>

●   This was a structure that didn't fit onto the screen.
●   Use “x” with a limit to display less of it.
Getting What You Need
●   A digit following “x” limits the depth:
    DB<26> $i = 'z'
    DB<27> $a = $b = []
    DB<28> for( 1 .. 100 ) { $b = $b->[0] = [], $b->[1] = ++$i }
    DB<29> x 6 $a
    0 ARRAY(0xc90e38)
       0 ARRAY(0xc917f8)
          0 ARRAY(0xc988e8)
             0 ARRAY(0xc98af8)
                0 ARRAY(0xc98a20)
                   0 ARRAY(0xc98b10)
                   1 'ad'
                1 'ac'
             1 'ab'
          1 'aa'
Mining Large Data Structures
●   x 2 $struct will show the top level, including hash 
    keys or offset lists.
●   x 2 $struct­>{ key } will show the single hash value.
●   To walk through a structure in viewable chunks:
    ●   x 2 $struct­>{ key1 }{ key2 }
    ●   See what matters, paste on the next key/offset and keep 
        looking: 
    ●   x 2 $struct­>{ key1 }{ key2 }[ offset ]
You Are Here
●   The “T” command provides a stack trace.
    ●   Useful with hardwired breakpoints.
    ●   They show the calling line numbers and values.
    ●   Makes it easier to set breakpoints up the stack to see how 
        values are [mis­]managed down the call stack.
●   Viewing the code uses “l” and “v”.
    ●   “l” (list) shows the next line to be executed.
    ●   “v” (view) shows a small window around the line.
Finding Out What You Can Do
                      35:         my $frag
                      36:         = WCurve::Fragment->new
                      37:         (
●   “m”  shows the    38:
                      39:         );
                                      FloatCyl => ( $base x $length ), $name


    methods of an      DB<1> n
                      Testify::(01-FloatCart-basic-geometry.t:41):
                      41:         ok $frag eq $name, "Name: $frag ($name)";
    object.            DB<1> m $frag
                      add_skip_chain
                      carp
●   Items with        confess
                      converge_limit
                      looks_like_number
    package           via   WCurve::Fragment:   (""
    prefixes are      via
                      via
                            WCurve::Fragment:
                            WCurve::Fragment:
                                                ()
                                                (0+
                      via   WCurve::Fragment:   (bool
    inherited.        via
                      via
                            WCurve::Fragment:
                            WCurve::Fragment:
                                                (int
                                                stop_offset

●   Leading '(' is    via
                      via
                            WCurve::Fragment: sum
                            WCurve::Fragment -> ArrayObj: (<=>

    an overload.      via
                      via
                            WCurve::Fragment -> ArrayObj: (cmp
                            WCurve::Fragment -> ArrayObj: DESTROY

                      via   UNIVERSAL:   DOES
                      via   UNIVERSAL:   VERSION
                      via   UNIVERSAL:   can
                      via   UNIVERSAL:   isa
Hardwired Breakpoints
●   Because the perl debugger is written in perl, you can 
    also set “hardwired” breakpoints:
      $DB::single = 1;
      $DB::single = 1 unless ref $thingy;
      $DB::single = 1 if $counter > @itemz;
●   These can be useful in permanent code:
      eval { … }
      or do
      { print $@; $DB::single = 1; 0 };
Tracing Code
●   Tracing code usually produces too much output.
●   To turn on tracing use $DB::trace = 1.
    ●   You can localize it to trace a code block.
    ●   Add if­logic to trace code leading up to errors:
        $DB::trace = 1 if ! ref $foo;
●   One trick for re­startable subs is to eval them and 
    trace the failures:
        eval { foo } or do{ trace = 1; foo }
Ever Wonder How a Regex Works?
●   The “re” module allows debugging regexen:
      use re 'debug';
      use re 'debugcolor';
●   There is more info in “perldoc perldebug”.
●   A monochrome example:
DB<7> do { use re 'debug'; $a = qr/ (w+) $/x; print 'this is a test' =~ /
$a/; }
Compiling REx " (w+) $"
Final program:
    1: OPEN1 (3)
    3:   PLUS (5)
    4:     ALNUM (0)
    5: CLOSE1 (7)
    7: EOL (8)
    8: END (0)
floating ""$ at 1..2147483647 (checking floating) stclass ALNUM plus minlen 1
Guessing start of match in sv for REx " (w+) $" against "this is a test"
Found floating substr ""$ at offset 14...
start_shift: 1 check_at: 14 s: 0 endpos: 14
Does not contradict STCLASS...
Guessed: match at offset 0
Matching REx " (w+) $" against "this is a test"
Matching stclass ALNUM against "this is a test" (14 chars)
    0 <this is a >| 1:OPEN1(3)
    0 <this is a >| 3:PLUS(5)
                                  ALNUM can match 4 times out of 2147483647...
    4 <this is a test>| 5: CLOSE1(7)
    4 <this is a test>| 7: EOL(8)
                                    failed...
...
    1 <this is a t>| 5: CLOSE1(7)
    1 <this is a t>| 7: EOL(8)
...
    7 <this is a test>| 5: CLOSE1(7)
    7 <this is a test>| 7: EOL(8)
                                    failed...
...
  10 <this is a test>| 3:PLUS(5)
                                  ALNUM can match 4 times out of 2147483647...
  14 <this is a test>| 5: CLOSE1(7)
  14 <this is a test>| 7: EOL(8)
  14 <this is a test>| 8: END(0)
Match successful!
test
  DB<8>
Benchmarking Size
●   Devel::Peek && Devel::Size show the contents and 
    size of structures inside of perl.
●   There are lots of examples in Perl Memory 
    Manglement, which is mostly a session of
      perl -Mdevel::Size -d -e 0;
●   The advantage to dealing with this in the debugger is 
    being able to interactively query the sizes of sub­
    structures to see where bloat comes from.
Knowing When You're There
●   The variable $^P will be true when code is running 
    in the debugger.
●   This allows you to automatically set hardwired 
    breakpoints or verbosity:
    my $max_verbose = $cmdline{ verbose } > 1 || $^P;
Spoon Feeding
●   The debugger does not handle forks automatically.
    ●   The problem is that multiple processes latch on to the tty 
        device files for input and output.
    ●   You can set the display to a set of per­initialized ttys 
        (usually pre­opened xterm's). 
    ●   At that point you can switch to the alternate terminals to 
        handle each session.
Semi­automated Forks
●   You can usually dodge the issue by simply not 
    forking in the debugger:
    if( my $pid = $^P ? '' : fork )
    {
        # parent
    }
    elsif( defined $pid )
    {
        # child, debugger
    }
    else
    {
        die "Phorkafobia: $!";
    }
A Modern Version


given( $^P ? '' : fork )
{
    when( ''    ) { ... }
    when( undef ) { die "Phorkafobia: $!" }

    my $child = wait;

    # parent processes results.
}
Further Reference
●   A always, perldoc is your friend.
    ●   perldoc perldebug
    ●   perldoc perlre
    ●   Perldoc DB
●   For examples of querying memory use:
    ●   perldoc Devel::Peek
    ●   perldoc Devel::Size
    ●   http://www.slideshare.net/lembark/perl5­memorymanglement
Ad

More Related Content

What's hot (20)

Hypers and Gathers and Takes! Oh my!
Hypers and Gathers and Takes! Oh my!Hypers and Gathers and Takes! Oh my!
Hypers and Gathers and Takes! Oh my!
Workhorse Computing
 
Memory Manglement in Raku
Memory Manglement in RakuMemory Manglement in Raku
Memory Manglement in Raku
Workhorse Computing
 
BSDM with BASH: Command Interpolation
BSDM with BASH: Command InterpolationBSDM with BASH: Command Interpolation
BSDM with BASH: Command Interpolation
Workhorse Computing
 
Getting Testy With Perl6
Getting Testy With Perl6Getting Testy With Perl6
Getting Testy With Perl6
Workhorse Computing
 
Building and Distributing PostgreSQL Extensions Without Learning C
Building and Distributing PostgreSQL Extensions Without Learning CBuilding and Distributing PostgreSQL Extensions Without Learning C
Building and Distributing PostgreSQL Extensions Without Learning C
David Wheeler
 
Perl6 Regexen: Reduce the line noise in your code.
Perl6 Regexen: Reduce the line noise in your code.Perl6 Regexen: Reduce the line noise in your code.
Perl6 Regexen: Reduce the line noise in your code.
Workhorse Computing
 
Static Optimization of PHP bytecode (PHPSC 2017)
Static Optimization of PHP bytecode (PHPSC 2017)Static Optimization of PHP bytecode (PHPSC 2017)
Static Optimization of PHP bytecode (PHPSC 2017)
Nikita Popov
 
Just-In-Time Compiler in PHP 8
Just-In-Time Compiler in PHP 8Just-In-Time Compiler in PHP 8
Just-In-Time Compiler in PHP 8
Nikita Popov
 
Perl.Hacks.On.Vim
Perl.Hacks.On.VimPerl.Hacks.On.Vim
Perl.Hacks.On.Vim
Lin Yo-An
 
I, For One, Welcome Our New Perl6 Overlords
I, For One, Welcome Our New Perl6 OverlordsI, For One, Welcome Our New Perl6 Overlords
I, For One, Welcome Our New Perl6 Overlords
heumann
 
What's new in PHP 8.0?
What's new in PHP 8.0?What's new in PHP 8.0?
What's new in PHP 8.0?
Nikita Popov
 
PHP Enums - PHPCon Japan 2021
PHP Enums - PHPCon Japan 2021PHP Enums - PHPCon Japan 2021
PHP Enums - PHPCon Japan 2021
Ayesh Karunaratne
 
Nikita Popov "What’s new in PHP 8.0?"
Nikita Popov "What’s new in PHP 8.0?"Nikita Popov "What’s new in PHP 8.0?"
Nikita Popov "What’s new in PHP 8.0?"
Fwdays
 
Melhorando sua API com DSLs
Melhorando sua API com DSLsMelhorando sua API com DSLs
Melhorando sua API com DSLs
Augusto Pascutti
 
Typed Properties and more: What's coming in PHP 7.4?
Typed Properties and more: What's coming in PHP 7.4?Typed Properties and more: What's coming in PHP 7.4?
Typed Properties and more: What's coming in PHP 7.4?
Nikita Popov
 
groovy & grails - lecture 2
groovy & grails - lecture 2groovy & grails - lecture 2
groovy & grails - lecture 2
Alexandre Masselot
 
Barely Legal Xxx Perl Presentation
Barely Legal Xxx Perl PresentationBarely Legal Xxx Perl Presentation
Barely Legal Xxx Perl Presentation
Attila Balazs
 
November Camp - Spec BDD with PHPSpec 2
November Camp - Spec BDD with PHPSpec 2November Camp - Spec BDD with PHPSpec 2
November Camp - Spec BDD with PHPSpec 2
Kacper Gunia
 
Perl web frameworks
Perl web frameworksPerl web frameworks
Perl web frameworks
diego_k
 
Perl6 in-production
Perl6 in-productionPerl6 in-production
Perl6 in-production
Andrew Shitov
 
Hypers and Gathers and Takes! Oh my!
Hypers and Gathers and Takes! Oh my!Hypers and Gathers and Takes! Oh my!
Hypers and Gathers and Takes! Oh my!
Workhorse Computing
 
BSDM with BASH: Command Interpolation
BSDM with BASH: Command InterpolationBSDM with BASH: Command Interpolation
BSDM with BASH: Command Interpolation
Workhorse Computing
 
Building and Distributing PostgreSQL Extensions Without Learning C
Building and Distributing PostgreSQL Extensions Without Learning CBuilding and Distributing PostgreSQL Extensions Without Learning C
Building and Distributing PostgreSQL Extensions Without Learning C
David Wheeler
 
Perl6 Regexen: Reduce the line noise in your code.
Perl6 Regexen: Reduce the line noise in your code.Perl6 Regexen: Reduce the line noise in your code.
Perl6 Regexen: Reduce the line noise in your code.
Workhorse Computing
 
Static Optimization of PHP bytecode (PHPSC 2017)
Static Optimization of PHP bytecode (PHPSC 2017)Static Optimization of PHP bytecode (PHPSC 2017)
Static Optimization of PHP bytecode (PHPSC 2017)
Nikita Popov
 
Just-In-Time Compiler in PHP 8
Just-In-Time Compiler in PHP 8Just-In-Time Compiler in PHP 8
Just-In-Time Compiler in PHP 8
Nikita Popov
 
Perl.Hacks.On.Vim
Perl.Hacks.On.VimPerl.Hacks.On.Vim
Perl.Hacks.On.Vim
Lin Yo-An
 
I, For One, Welcome Our New Perl6 Overlords
I, For One, Welcome Our New Perl6 OverlordsI, For One, Welcome Our New Perl6 Overlords
I, For One, Welcome Our New Perl6 Overlords
heumann
 
What's new in PHP 8.0?
What's new in PHP 8.0?What's new in PHP 8.0?
What's new in PHP 8.0?
Nikita Popov
 
PHP Enums - PHPCon Japan 2021
PHP Enums - PHPCon Japan 2021PHP Enums - PHPCon Japan 2021
PHP Enums - PHPCon Japan 2021
Ayesh Karunaratne
 
Nikita Popov "What’s new in PHP 8.0?"
Nikita Popov "What’s new in PHP 8.0?"Nikita Popov "What’s new in PHP 8.0?"
Nikita Popov "What’s new in PHP 8.0?"
Fwdays
 
Melhorando sua API com DSLs
Melhorando sua API com DSLsMelhorando sua API com DSLs
Melhorando sua API com DSLs
Augusto Pascutti
 
Typed Properties and more: What's coming in PHP 7.4?
Typed Properties and more: What's coming in PHP 7.4?Typed Properties and more: What's coming in PHP 7.4?
Typed Properties and more: What's coming in PHP 7.4?
Nikita Popov
 
Barely Legal Xxx Perl Presentation
Barely Legal Xxx Perl PresentationBarely Legal Xxx Perl Presentation
Barely Legal Xxx Perl Presentation
Attila Balazs
 
November Camp - Spec BDD with PHPSpec 2
November Camp - Spec BDD with PHPSpec 2November Camp - Spec BDD with PHPSpec 2
November Camp - Spec BDD with PHPSpec 2
Kacper Gunia
 
Perl web frameworks
Perl web frameworksPerl web frameworks
Perl web frameworks
diego_k
 

Similar to Short Introduction To "perl -d" (20)

PHP and MySQL
PHP and MySQLPHP and MySQL
PHP and MySQL
Sanketkumar Biswas
 
Marat-Slides
Marat-SlidesMarat-Slides
Marat-Slides
Marat Vyshegorodtsev
 
3
33
3
Marat Vyshegorodtsev
 
Beijing Perl Workshop 2008 Hiveminder Secret Sauce
Beijing Perl Workshop 2008 Hiveminder Secret SauceBeijing Perl Workshop 2008 Hiveminder Secret Sauce
Beijing Perl Workshop 2008 Hiveminder Secret Sauce
Jesse Vincent
 
Perl training-in-navi mumbai
Perl training-in-navi mumbaiPerl training-in-navi mumbai
Perl training-in-navi mumbai
vibrantuser
 
Sdl Basic
Sdl BasicSdl Basic
Sdl Basic
roberto viola
 
Linux shell
Linux shellLinux shell
Linux shell
Kenny (netman)
 
Perl5 Memory Manglement
Perl5 Memory ManglementPerl5 Memory Manglement
Perl5 Memory Manglement
Workhorse Computing
 
Perl at SkyCon'12
Perl at SkyCon'12Perl at SkyCon'12
Perl at SkyCon'12
Tim Bunce
 
Gun make
Gun makeGun make
Gun make
psychesnet Hsieh
 
20160520 what youneedtoknowaboutlambdas
20160520 what youneedtoknowaboutlambdas20160520 what youneedtoknowaboutlambdas
20160520 what youneedtoknowaboutlambdas
shinolajla
 
Debugger Principle Overview & GDB Tricks
Debugger Principle Overview & GDB TricksDebugger Principle Overview & GDB Tricks
Debugger Principle Overview & GDB Tricks
dutor
 
Hiveminder - Everything but the Secret Sauce
Hiveminder - Everything but the Secret SauceHiveminder - Everything but the Secret Sauce
Hiveminder - Everything but the Secret Sauce
Jesse Vincent
 
Writing Macros
Writing MacrosWriting Macros
Writing Macros
RueiCi Wang
 
Scheme on WebAssembly: It is happening!
Scheme on WebAssembly:  It is happening!Scheme on WebAssembly:  It is happening!
Scheme on WebAssembly: It is happening!
Igalia
 
BITS: Introduction to relational databases and MySQL - Schema design
BITS: Introduction to relational databases and MySQL - Schema designBITS: Introduction to relational databases and MySQL - Schema design
BITS: Introduction to relational databases and MySQL - Schema design
BITS
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
NBACriteria2SICET
 
Basic C++ 11/14 for Python Programmers
Basic C++ 11/14 for Python ProgrammersBasic C++ 11/14 for Python Programmers
Basic C++ 11/14 for Python Programmers
Appier
 
Big Data Day LA 2016/ Hadoop/ Spark/ Kafka track - Iterative Spark Developmen...
Big Data Day LA 2016/ Hadoop/ Spark/ Kafka track - Iterative Spark Developmen...Big Data Day LA 2016/ Hadoop/ Spark/ Kafka track - Iterative Spark Developmen...
Big Data Day LA 2016/ Hadoop/ Spark/ Kafka track - Iterative Spark Developmen...
Data Con LA
 
Our Friends the Utils: A highway traveled by wheels we didn't re-invent.
Our Friends the Utils: A highway traveled by wheels we didn't re-invent. Our Friends the Utils: A highway traveled by wheels we didn't re-invent.
Our Friends the Utils: A highway traveled by wheels we didn't re-invent.
Workhorse Computing
 
Beijing Perl Workshop 2008 Hiveminder Secret Sauce
Beijing Perl Workshop 2008 Hiveminder Secret SauceBeijing Perl Workshop 2008 Hiveminder Secret Sauce
Beijing Perl Workshop 2008 Hiveminder Secret Sauce
Jesse Vincent
 
Perl training-in-navi mumbai
Perl training-in-navi mumbaiPerl training-in-navi mumbai
Perl training-in-navi mumbai
vibrantuser
 
Perl at SkyCon'12
Perl at SkyCon'12Perl at SkyCon'12
Perl at SkyCon'12
Tim Bunce
 
20160520 what youneedtoknowaboutlambdas
20160520 what youneedtoknowaboutlambdas20160520 what youneedtoknowaboutlambdas
20160520 what youneedtoknowaboutlambdas
shinolajla
 
Debugger Principle Overview & GDB Tricks
Debugger Principle Overview & GDB TricksDebugger Principle Overview & GDB Tricks
Debugger Principle Overview & GDB Tricks
dutor
 
Hiveminder - Everything but the Secret Sauce
Hiveminder - Everything but the Secret SauceHiveminder - Everything but the Secret Sauce
Hiveminder - Everything but the Secret Sauce
Jesse Vincent
 
Scheme on WebAssembly: It is happening!
Scheme on WebAssembly:  It is happening!Scheme on WebAssembly:  It is happening!
Scheme on WebAssembly: It is happening!
Igalia
 
BITS: Introduction to relational databases and MySQL - Schema design
BITS: Introduction to relational databases and MySQL - Schema designBITS: Introduction to relational databases and MySQL - Schema design
BITS: Introduction to relational databases and MySQL - Schema design
BITS
 
Basic C++ 11/14 for Python Programmers
Basic C++ 11/14 for Python ProgrammersBasic C++ 11/14 for Python Programmers
Basic C++ 11/14 for Python Programmers
Appier
 
Big Data Day LA 2016/ Hadoop/ Spark/ Kafka track - Iterative Spark Developmen...
Big Data Day LA 2016/ Hadoop/ Spark/ Kafka track - Iterative Spark Developmen...Big Data Day LA 2016/ Hadoop/ Spark/ Kafka track - Iterative Spark Developmen...
Big Data Day LA 2016/ Hadoop/ Spark/ Kafka track - Iterative Spark Developmen...
Data Con LA
 
Our Friends the Utils: A highway traveled by wheels we didn't re-invent.
Our Friends the Utils: A highway traveled by wheels we didn't re-invent. Our Friends the Utils: A highway traveled by wheels we didn't re-invent.
Our Friends the Utils: A highway traveled by wheels we didn't re-invent.
Workhorse Computing
 
Ad

More from Workhorse Computing (19)

Wheels we didn't re-invent: Perl's Utility Modules
Wheels we didn't re-invent: Perl's Utility ModulesWheels we didn't re-invent: Perl's Utility Modules
Wheels we didn't re-invent: Perl's Utility Modules
Workhorse Computing
 
mro-every.pdf
mro-every.pdfmro-every.pdf
mro-every.pdf
Workhorse Computing
 
Paranormal statistics: Counting What Doesn't Add Up
Paranormal statistics: Counting What Doesn't Add UpParanormal statistics: Counting What Doesn't Add Up
Paranormal statistics: Counting What Doesn't Add Up
Workhorse Computing
 
Unit Testing Lots of Perl
Unit Testing Lots of PerlUnit Testing Lots of Perl
Unit Testing Lots of Perl
Workhorse Computing
 
Generating & Querying Calendar Tables in Posgresql
Generating & Querying Calendar Tables in PosgresqlGenerating & Querying Calendar Tables in Posgresql
Generating & Querying Calendar Tables in Posgresql
Workhorse Computing
 
Effective Benchmarks
Effective BenchmarksEffective Benchmarks
Effective Benchmarks
Workhorse Computing
 
The W-curve and its application.
The W-curve and its application.The W-curve and its application.
The W-curve and its application.
Workhorse Computing
 
Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6
Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6
Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6
Workhorse Computing
 
Neatly folding-a-tree
Neatly folding-a-treeNeatly folding-a-tree
Neatly folding-a-tree
Workhorse Computing
 
Light my-fuse
Light my-fuseLight my-fuse
Light my-fuse
Workhorse Computing
 
Paranormal stats
Paranormal statsParanormal stats
Paranormal stats
Workhorse Computing
 
Shared Object images in Docker: What you need is what you want.
Shared Object images in Docker: What you need is what you want.Shared Object images in Docker: What you need is what you want.
Shared Object images in Docker: What you need is what you want.
Workhorse Computing
 
Putting some "logic" in LVM.
Putting some "logic" in LVM.Putting some "logic" in LVM.
Putting some "logic" in LVM.
Workhorse Computing
 
Selenium sandwich-3: Being where you aren't.
Selenium sandwich-3: Being where you aren't.Selenium sandwich-3: Being where you aren't.
Selenium sandwich-3: Being where you aren't.
Workhorse Computing
 
Selenium sandwich-2
Selenium sandwich-2Selenium sandwich-2
Selenium sandwich-2
Workhorse Computing
 
Selenium Sandwich Part 1: Data driven Selenium
Selenium Sandwich Part 1: Data driven Selenium Selenium Sandwich Part 1: Data driven Selenium
Selenium Sandwich Part 1: Data driven Selenium
Workhorse Computing
 
Docker perl build
Docker perl buildDocker perl build
Docker perl build
Workhorse Computing
 
Designing net-aws-glacier
Designing net-aws-glacierDesigning net-aws-glacier
Designing net-aws-glacier
Workhorse Computing
 
Ethiopian multiplication in Perl6
Ethiopian multiplication in Perl6Ethiopian multiplication in Perl6
Ethiopian multiplication in Perl6
Workhorse Computing
 
Wheels we didn't re-invent: Perl's Utility Modules
Wheels we didn't re-invent: Perl's Utility ModulesWheels we didn't re-invent: Perl's Utility Modules
Wheels we didn't re-invent: Perl's Utility Modules
Workhorse Computing
 
Paranormal statistics: Counting What Doesn't Add Up
Paranormal statistics: Counting What Doesn't Add UpParanormal statistics: Counting What Doesn't Add Up
Paranormal statistics: Counting What Doesn't Add Up
Workhorse Computing
 
Generating & Querying Calendar Tables in Posgresql
Generating & Querying Calendar Tables in PosgresqlGenerating & Querying Calendar Tables in Posgresql
Generating & Querying Calendar Tables in Posgresql
Workhorse Computing
 
The W-curve and its application.
The W-curve and its application.The W-curve and its application.
The W-curve and its application.
Workhorse Computing
 
Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6
Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6
Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6
Workhorse Computing
 
Shared Object images in Docker: What you need is what you want.
Shared Object images in Docker: What you need is what you want.Shared Object images in Docker: What you need is what you want.
Shared Object images in Docker: What you need is what you want.
Workhorse Computing
 
Selenium sandwich-3: Being where you aren't.
Selenium sandwich-3: Being where you aren't.Selenium sandwich-3: Being where you aren't.
Selenium sandwich-3: Being where you aren't.
Workhorse Computing
 
Selenium Sandwich Part 1: Data driven Selenium
Selenium Sandwich Part 1: Data driven Selenium Selenium Sandwich Part 1: Data driven Selenium
Selenium Sandwich Part 1: Data driven Selenium
Workhorse Computing
 
Ethiopian multiplication in Perl6
Ethiopian multiplication in Perl6Ethiopian multiplication in Perl6
Ethiopian multiplication in Perl6
Workhorse Computing
 
Ad

Recently uploaded (20)

Heap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and DeletionHeap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and Deletion
Jaydeep Kale
 
Big Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur MorganBig Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur Morgan
Arthur Morgan
 
How analogue intelligence complements AI
How analogue intelligence complements AIHow analogue intelligence complements AI
How analogue intelligence complements AI
Paul Rowe
 
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdfComplete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Software Company
 
TrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business ConsultingTrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business Consulting
Trs Labs
 
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded DevelopersLinux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Toradex
 
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
SOFTTECHHUB
 
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes
 
Rusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond SparkRusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond Spark
carlyakerly1
 
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Aqusag Technologies
 
Technology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data AnalyticsTechnology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data Analytics
InData Labs
 
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-UmgebungenHCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
panagenda
 
Procurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptxProcurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptx
Jon Hansen
 
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager APIUiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPathCommunity
 
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In France
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In FranceManifest Pre-Seed Update | A Humanoid OEM Deeptech In France
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In France
chb3
 
Andrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell: Transforming Business Strategy Through Data-Driven InsightsAndrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell
 
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
BookNet Canada
 
Semantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AISemantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AI
artmondano
 
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath MaestroDev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
UiPathCommunity
 
Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)
Ortus Solutions, Corp
 
Heap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and DeletionHeap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and Deletion
Jaydeep Kale
 
Big Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur MorganBig Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur Morgan
Arthur Morgan
 
How analogue intelligence complements AI
How analogue intelligence complements AIHow analogue intelligence complements AI
How analogue intelligence complements AI
Paul Rowe
 
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdfComplete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Software Company
 
TrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business ConsultingTrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business Consulting
Trs Labs
 
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded DevelopersLinux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Toradex
 
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
SOFTTECHHUB
 
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes
 
Rusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond SparkRusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond Spark
carlyakerly1
 
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Aqusag Technologies
 
Technology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data AnalyticsTechnology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data Analytics
InData Labs
 
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-UmgebungenHCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
panagenda
 
Procurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptxProcurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptx
Jon Hansen
 
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager APIUiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPathCommunity
 
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In France
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In FranceManifest Pre-Seed Update | A Humanoid OEM Deeptech In France
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In France
chb3
 
Andrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell: Transforming Business Strategy Through Data-Driven InsightsAndrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell
 
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
BookNet Canada
 
Semantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AISemantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AI
artmondano
 
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath MaestroDev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
UiPathCommunity
 
Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)
Ortus Solutions, Corp
 

Short Introduction To "perl -d"

  • 1. Basics of perl -d Steven Lembark Workhorse Computing [email protected]
  • 2. Introducing Perl's Debugger ● Overview of the obvious uses, common commands. ● Few less obvious uses: ● Debugging regexen. ● Evaluating structure bloat. ● Some pitfalls and how to avoid them.
  • 3. What is the Perl Debugger? ● The Perl debugger comes with perl, in fact it is perl. ● '­d' is built into perl's command line. ● It functions like a perly shell, evaluating source code  from files or the command line or executing  debugger commands. ● You also use perl ­d with profiling utilities like  NYTprof.
  • 4. Smarter Than Your Average Code ● The most obvious thing you can do is walking code  to track down bugs. ● You can also test Perl syntax: just type it in. ● Use it as a “perly shell” when your one­liners run  into multiple lines or you have to eyeball data  structures  between commands. ● Q&D interactive data mining. ● Good for examining data structures when writing  talks on data manglement or module guts.
  • 5. The Basics: Getting In ● At the command  $ perl ­d ­e 42; line: perl ­d enters  $ perl ­d foo.pm; the debugger. $ perl ­d bar; ● You can start it  'vanilla', with  $ perl ­MDevel::Size ­d ­e foo; modules to watch  startup, with code,  or using modules to  pull in utilities (e.g.  regex debugging).
  • 6. Basics: The Prompt ● Once in you get the command prompt: $ perl ­d ­e 42 Loading DB routines from perl5db.pl version 1.22  Editor support available. Enter h or `h h' for help, or `man perldebug' for more help. main::(­e:1):   42 DB<1> ● This is a “vanilla” session: there is no running code,  you can enter debugger commands, perl syntax. ● The single “<1>” indicate that this is the outermost  call level. ● The “1” says that this is the first command.
  • 7. Executing Perl Statements ● Q: Who remembers what localtime returns? ● A: How to find out? DB<1> x localtime 0 31 1 31 2 15 3 15 4 5 5 111 6 3 7 165 8 1 DB<2> ● Notice that now I'm at command #2.
  • 8. Gotchas ● Each command you type is run its own block. ● Lexical variables like “my $foo” will vanish. ● Local values like “local $” or “local $foo{ bar } = ...”  will also be unavailable after the line completes. ● You can put multiple statements onto a line with  semi­colon separators. ● You can only input one line at a time. ● Cut­and­paste of multiple lines won't work.
  • 9. Debugger Commands ● These intentionally look pretty much like gdb. ● On the other hand, if you didn't grow up debugging C  code this may not help you much. ● The most common commands are for running code,  managing breakpoints (i.e. stopping code),  interrogating values. ● Please note: “q” gets you out. ● Not “quit”, ^C, ^D, or “getmeoutofhere!”.
  • 10. Setting Up the Debugger ● You may want to edit your commands. ● Installing Term::ReadKey &Term::ReadLine. ● perl will use your .inputrc if you have one. ● For example, my .inputrc looks like: set editing-mode vi set show-all-if-ambiguous on with allows ^[k to pull up the last line for editing. ● Check the doc's if you use Emacs.
  • 11. Running Code ● Say some code blows up. ● You could just run it with “r” from the start. ● That is handy once to see where it blows up. ● Usually you want to stop at a particular place to see  why it blows up. ● You can continue to a line no or sub name with: c 15 c call_foomatic
  • 12. Stepping Code ● You can also watch the code one line at a time. ● “n” (“next”) steps over the subroutine calls. ● “s” (“step”) steps into the subroutine calls.  ● “r” (“return”) goes back to the caller if you accidentally  step one level too deep. ● One really common combination:  ● “c” to a subroutine that blows up. ● “n” to the point before it dies. ● “s” into the call that failed and see what happens.
  • 13. Getting Out of a Hole: “r” ● Sometimes you 's' into the wrong sub (say a DBI  call). ● You don't want to abort the session. ● You don't want to “n” your way through DBI. ● Use “r” to return from the current call. ● This also shows you the return value passed back to  the caller.  ● Nice for checking that what you expect gets returned. ● Beware if the structure is really large.
  • 14. Stopping Code: Breakpoints ● Breakpoints stop the code. ● They can include a condition. ● Say the code blows up at line 842 with a non­ reference value in $thingy after roughly 8_000  iterations. ● Set a breakpoint and continue: <1> b 842 ! ref $thingy <2> c
  • 15. Examining Values ● “p” prints a value. ● “x” examines it (similar to Data::Dumper). DB<6> p @a = map { $_ => [ 1 ] } ( 'a' .. 'c' ) aARRAY(0xc0f100)bARRAY(0xc12078)cARRAY(0xc0f0d0) DB<7> x @a = map { $_ => [ 1 ] } ( 'a' .. 'c' ) 0 'a' 1 ARRAY(0xc12060) 0 1 2 'b' 3 ARRAY(0xc11dc0) 0 1 4 'c' 5 ARRAY(0xc0e1d8) 0 1
  • 16. Hashes Are Lists to “x” ● Hashes look a little odd at first: DB<8> x %a = map { $_ => [ 1 ] } ( 'a' .. 'c' ) 0 'a' 1 ARRAY(0xc122a0) 0 1 2 'b' 3 ARRAY(0xb07fe0) 0 1 4 'c' 5 ARRAY(0xc122e8) 0 1 ● They look exactly like the array: a list.
  • 17. Hashref's Are Structures ● Examining a hashref shows it as key => value pairs: DB<9> x %a 0 HASH(0xc47008) 'a' => ARRAY(0xc122a0) 0 1 'b' => ARRAY(0xb07fe0) 0 1 'c' => ARRAY(0xc122e8) 0 1
  • 18. You Don't Always Want It All ● Occasionally you'll get something like: 0 ARRAY(0xc99050) 0 ARRAY(0xc99080) 0 ARRAY(0xc990b0) 0 ARRAY(0xc990e0) 0 ARRAY(0xc99110) 0 ARRAY(0xc99140) empty array DB<17> ● This was a structure that didn't fit onto the screen. ● Use “x” with a limit to display less of it.
  • 19. Getting What You Need ● A digit following “x” limits the depth: DB<26> $i = 'z' DB<27> $a = $b = [] DB<28> for( 1 .. 100 ) { $b = $b->[0] = [], $b->[1] = ++$i } DB<29> x 6 $a 0 ARRAY(0xc90e38) 0 ARRAY(0xc917f8) 0 ARRAY(0xc988e8) 0 ARRAY(0xc98af8) 0 ARRAY(0xc98a20) 0 ARRAY(0xc98b10) 1 'ad' 1 'ac' 1 'ab' 1 'aa'
  • 20. Mining Large Data Structures ● x 2 $struct will show the top level, including hash  keys or offset lists. ● x 2 $struct­>{ key } will show the single hash value. ● To walk through a structure in viewable chunks: ● x 2 $struct­>{ key1 }{ key2 } ● See what matters, paste on the next key/offset and keep  looking:  ● x 2 $struct­>{ key1 }{ key2 }[ offset ]
  • 21. You Are Here ● The “T” command provides a stack trace. ● Useful with hardwired breakpoints. ● They show the calling line numbers and values. ● Makes it easier to set breakpoints up the stack to see how  values are [mis­]managed down the call stack. ● Viewing the code uses “l” and “v”. ● “l” (list) shows the next line to be executed. ● “v” (view) shows a small window around the line.
  • 22. Finding Out What You Can Do 35: my $frag 36: = WCurve::Fragment->new 37: ( ● “m”  shows the  38: 39: ); FloatCyl => ( $base x $length ), $name methods of an  DB<1> n Testify::(01-FloatCart-basic-geometry.t:41): 41: ok $frag eq $name, "Name: $frag ($name)"; object. DB<1> m $frag add_skip_chain carp ● Items with  confess converge_limit looks_like_number package  via WCurve::Fragment: ("" prefixes are  via via WCurve::Fragment: WCurve::Fragment: () (0+ via WCurve::Fragment: (bool inherited. via via WCurve::Fragment: WCurve::Fragment: (int stop_offset ● Leading '(' is  via via WCurve::Fragment: sum WCurve::Fragment -> ArrayObj: (<=> an overload. via via WCurve::Fragment -> ArrayObj: (cmp WCurve::Fragment -> ArrayObj: DESTROY via UNIVERSAL: DOES via UNIVERSAL: VERSION via UNIVERSAL: can via UNIVERSAL: isa
  • 23. Hardwired Breakpoints ● Because the perl debugger is written in perl, you can  also set “hardwired” breakpoints: $DB::single = 1; $DB::single = 1 unless ref $thingy; $DB::single = 1 if $counter > @itemz; ● These can be useful in permanent code: eval { … } or do { print $@; $DB::single = 1; 0 };
  • 24. Tracing Code ● Tracing code usually produces too much output. ● To turn on tracing use $DB::trace = 1. ● You can localize it to trace a code block. ● Add if­logic to trace code leading up to errors: $DB::trace = 1 if ! ref $foo; ● One trick for re­startable subs is to eval them and  trace the failures: eval { foo } or do{ trace = 1; foo }
  • 25. Ever Wonder How a Regex Works? ● The “re” module allows debugging regexen: use re 'debug'; use re 'debugcolor'; ● There is more info in “perldoc perldebug”. ● A monochrome example:
  • 26. DB<7> do { use re 'debug'; $a = qr/ (w+) $/x; print 'this is a test' =~ / $a/; } Compiling REx " (w+) $" Final program: 1: OPEN1 (3) 3: PLUS (5) 4: ALNUM (0) 5: CLOSE1 (7) 7: EOL (8) 8: END (0) floating ""$ at 1..2147483647 (checking floating) stclass ALNUM plus minlen 1 Guessing start of match in sv for REx " (w+) $" against "this is a test" Found floating substr ""$ at offset 14... start_shift: 1 check_at: 14 s: 0 endpos: 14 Does not contradict STCLASS... Guessed: match at offset 0 Matching REx " (w+) $" against "this is a test" Matching stclass ALNUM against "this is a test" (14 chars) 0 <this is a >| 1:OPEN1(3) 0 <this is a >| 3:PLUS(5) ALNUM can match 4 times out of 2147483647... 4 <this is a test>| 5: CLOSE1(7) 4 <this is a test>| 7: EOL(8) failed... ... 1 <this is a t>| 5: CLOSE1(7) 1 <this is a t>| 7: EOL(8) ... 7 <this is a test>| 5: CLOSE1(7) 7 <this is a test>| 7: EOL(8) failed... ... 10 <this is a test>| 3:PLUS(5) ALNUM can match 4 times out of 2147483647... 14 <this is a test>| 5: CLOSE1(7) 14 <this is a test>| 7: EOL(8) 14 <this is a test>| 8: END(0) Match successful! test DB<8>
  • 27. Benchmarking Size ● Devel::Peek && Devel::Size show the contents and  size of structures inside of perl. ● There are lots of examples in Perl Memory  Manglement, which is mostly a session of perl -Mdevel::Size -d -e 0; ● The advantage to dealing with this in the debugger is  being able to interactively query the sizes of sub­ structures to see where bloat comes from.
  • 28. Knowing When You're There ● The variable $^P will be true when code is running  in the debugger. ● This allows you to automatically set hardwired  breakpoints or verbosity: my $max_verbose = $cmdline{ verbose } > 1 || $^P;
  • 29. Spoon Feeding ● The debugger does not handle forks automatically. ● The problem is that multiple processes latch on to the tty  device files for input and output. ● You can set the display to a set of per­initialized ttys  (usually pre­opened xterm's).  ● At that point you can switch to the alternate terminals to  handle each session.
  • 30. Semi­automated Forks ● You can usually dodge the issue by simply not  forking in the debugger: if( my $pid = $^P ? '' : fork ) { # parent } elsif( defined $pid ) { # child, debugger } else { die "Phorkafobia: $!"; }
  • 31. A Modern Version given( $^P ? '' : fork ) { when( '' ) { ... } when( undef ) { die "Phorkafobia: $!" } my $child = wait; # parent processes results. }
  • 32. Further Reference ● A always, perldoc is your friend. ● perldoc perldebug ● perldoc perlre ● Perldoc DB ● For examples of querying memory use: ● perldoc Devel::Peek ● perldoc Devel::Size ● http://www.slideshare.net/lembark/perl5­memorymanglement