| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
use v5.14; |
| 4 |
use warnings; |
| 5 |
use Feature::Compat::Try; |
| 6 |
|
| 7 |
use Devel::MAT::Cmd::Terminal; |
| 8 |
use Getopt::Long qw( :config no_permute ); |
| 9 |
use Commandable::Invocation 0.03; # ->new_from_tokens |
| 10 |
|
| 11 |
use constant CAN_COLOUR => -t STDERR; |
| 12 |
|
| 13 |
GetOptions( |
| 14 |
'quiet|q' => \( my $QUIET ), |
| 15 |
'blib' => sub { require blib; blib->import }, |
| 16 |
) or exit 1; |
| 17 |
|
| 18 |
# Some tools might want to draw pretty graphs with line drawing / similar |
| 19 |
STDOUT->binmode( ":encoding(UTF-8)" ); |
| 20 |
STDOUT->autoflush(1); |
| 21 |
|
| 22 |
require Devel::MAT; |
| 23 |
|
| 24 |
my $file = shift @ARGV or die "Need dumpfile\n"; |
| 25 |
|
| 26 |
my $progress = ( CAN_COLOUR && !$QUIET ? |
| 27 |
sub { print STDERR "\r\e[K" . ( shift // "" ); } : |
| 28 |
undef |
| 29 |
); |
| 30 |
|
| 31 |
my $pmat = Devel::MAT->load( $file, |
| 32 |
progress => $progress, |
| 33 |
); |
| 34 |
|
| 35 |
$progress->() if $progress; |
| 36 |
|
| 37 |
my $df = $pmat->dumpfile; |
| 38 |
|
| 39 |
if( !$QUIET ) { |
| 40 |
$pmat->run_command( Commandable::Invocation->new( "summary" ) ); |
| 41 |
} |
| 42 |
|
| 43 |
if( @ARGV ) { |
| 44 |
$pmat->run_command( |
| 45 |
Commandable::Invocation->new_from_tokens( @ARGV ), |
| 46 |
progress => $progress, |
| 47 |
); |
| 48 |
|
| 49 |
# Finish the pagination output |
| 50 |
Devel::MAT::Tool::more->run while Devel::MAT::Tool::more->can_more; |
| 51 |
|
| 52 |
exit |
| 53 |
} |
| 54 |
|
| 55 |
require Term::ReadLine; |
| 56 |
|
| 57 |
my $rl = Term::ReadLine->new( 'pmat' ); |
| 58 |
while( defined( my $line = $rl->readline( |
| 59 |
sprintf 'pmat%s> ', Devel::MAT::Tool::more->can_more ? " [more]" : "" |
| 60 |
) ) ) { |
| 61 |
my $inv = Commandable::Invocation->new( $line ); |
| 62 |
defined $inv->peek_token or |
| 63 |
$inv = Commandable::Invocation->new( "more" ) if Devel::MAT::Tool::more->can_more; |
| 64 |
|
| 65 |
next unless defined $inv->peek_token; # blank line |
| 66 |
|
| 67 |
last if $inv->peek_token eq "exit" or $inv->peek_token eq "quit"; |
| 68 |
|
| 69 |
try { |
| 70 |
# We just have to hope nobody catches this one. |
| 71 |
# It would be nice to next COMMAND but awkward perl internals reasons |
| 72 |
# mean we can't do that from a signal handler |
| 73 |
local $SIG{INT} = sub { die "\nAborted\n"; }; |
| 74 |
|
| 75 |
$pmat->run_command( $inv, |
| 76 |
progress => $progress, |
| 77 |
); |
| 78 |
} |
| 79 |
catch ($e) { |
| 80 |
print STDERR "$e"; |
| 81 |
} |
| 82 |
|
| 83 |
print "\n"; |
| 84 |
} |
| 85 |
|
| 86 |
print "\n"; |
| 87 |
|
| 88 |
=head1 NAME |
| 89 |
|
| 90 |
pmat - Perl Memory Analysis Tool |
| 91 |
|
| 92 |
=head1 SYNOPSIS |
| 93 |
|
| 94 |
$ pmat my-file.pmat |
| 95 |
|
| 96 |
=head1 OPTIONS |
| 97 |
|
| 98 |
=over 4 |
| 99 |
|
| 100 |
=item --quiet, -q |
| 101 |
|
| 102 |
Don't print progress reports or welcome banner. |
| 103 |
|
| 104 |
=back |
| 105 |
|
| 106 |
=head1 DESCRIPTION |
| 107 |
|
| 108 |
See L<Devel::MAT::UserGuide>. |
| 109 |
|
| 110 |
=head1 AUTHOR |
| 111 |
|
| 112 |
Paul Evans <[email protected]> |
| 113 |
|
| 114 |
=cut |