#!/usr/bin/env perl
# PODNAME: dip
# ABSTRACT: Dynamic instrumentation like DTrace, using aspects
use warnings;
use strict;
use Getopt::Long;
my %opt;
GetOptions(\%opt, qw(script|s=s@ exec|e=s@ delay|d verbose|v));
$opt{command} //= '';
my $spec = '';
$spec .= "run q!$_!; " for @{ $opt{script} || [] };
$spec .= "$_; "        for @{ $opt{exec}   || [] };

# horrible hack to pass some options to dip.pm during exec()
$spec = "-delay,$spec" if $opt{delay};
my $cmd = "$^X -Mdip='$spec' @ARGV";
print "$cmd\n" if $opt{verbose};
exec $cmd;

=pod

=for stopwords DTrace

=for test_synopsis 1;
__END__

=head1 NAME

dip - Dynamic instrumentation like DTrace, using aspects

=head1 SYNOPSIS

    # run a dip script from a file; pass perl switches after the '--'
    $ dip -s toolkit/count_new.dip -- -S myapp.pl

    # run an inline dip script
    $ dip -e 'before { count("constructor", ARGS(1), ustack(5)); $c{total}++ }
        call "URI::new"' test.pl

    # a more complex dip script
    $ cat quant-requests.dip
    # quantize request handling time, separated by request URI
    before { $ts_start = [gettimeofday] }
        call 'Dancer::Handler::handle_request';
    after { quantize ARGS(1)->request_uri => 10**6*tv_interval($ts_start) }
        call qr/Dancer::Handler::handle_request/;
    $ dip -s request-quant.dip test.pl
    ...
    /
           value  ------------------ Distribution ------------------ count
            1024 |                                                   0
            2048 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@    95
            4096 |@@                                                 4
            8192 |                                                   0
           16384 |@                                                  1
           32768 |                                                   0

    /login
           value  ------------------ Distribution ------------------ count
             512 |                                                   0
            1024 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@                70
            2048 |@@@@@@@@@@@@@@@                                    30
            4096 |                                                   0

    # The next example relies on Aspect::Library::Profiler, so
    # if something goes wrong, you need to look in the Aspect modules.
    $ dip -e 'aspect Profiler => call qr/^Person::set_/' myapp.pl

=head1 DESCRIPTION

C<dip> is a dynamic instrumentation framework for troubleshooting Perl
programs in real time. C<dip> can provide fine-grained information,
such as a log of the arguments with which a specific function is being
called.

Please see the documentation of the L<dip> module for more information
(C<perldoc dip>).

=head1 COMMANDS

=over 4

=item -s, --script

Takes a path to the dip script that should be run.

=item -e, --exec

Expects a dip script to be passed inline, much like C<perl -e> expects
an inline program.

=item -d, --delay

Tells L<dip> not to activate the instrumentation at the beginning of
the program. Instead the program to be instrumented should activate it
manually using:

    $dip::dip && $dip::dip->();

This is useful if your program loads other code that should be
instrumented at runtime. For example, to test a web application that
uses Plack you might use:

    use Plack::Util;
    use Plack::Test;
    use HTTP::Request;

    test_psgi
        app => Plack::Util::load_psgi('mywebapp.pl'),
        client => sub {
            my $cb = shift;
            # now we're sure that mywebapp.pl has been loaded
            $dip::dip && $dip::dip->();
            # ... now make requests and test the responses ...
        };

=item -v, --verbose

The command-line options given to C<dip> are used to call the program
to be instrumented in a special way. Using the C<--verbose> option
tells C<dip> to print that command-line.

=back

=head1 SEE ALSO

L<dip>

=head1 AUTHOR

The following person is the author of all the files provided in
this distribution unless explicitly noted otherwise.

Marcel Gruenauer <marcel@cpan.org>, L<http://perlservices.at>

=head1 COPYRIGHT AND LICENSE

The following copyright notice applies to all the files provided in
this distribution, including binary files, unless explicitly noted
otherwise.

This software is copyright (c) 2011 by Marcel Gruenauer.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
