| 1 | package AnyEvent::ITM; | ||||||||||||||||||||||||||||
| 2 | 2 | our $AUTHORITY = 'cpan:GETTY'; | |||||||||||||||||||||||||||
| 3 | # ABSTRACT: Debug ITM/SWD stream deserializer for AnyEvent | ||||||||||||||||||||||||||||
| 4 | 2 | $AnyEvent::ITM::VERSION = '0.100'; | |||||||||||||||||||||||||||
| 5 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use bytes; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use AnyEvent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use AnyEvent::Handle; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use ITM; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use Fcntl qw(O_RDONLY O_RDWR O_NONBLOCK O_NOCTTY); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use AnyEvent::Util qw(run_cmd portable_pipe); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 9 | ▶ | AnyEvent::Handle::register_read_type(itm => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.1
| |||||||||||||||||||||||||||||
| 17 | 9 | my ( $self, $cb ) = @_; | |||||||||||||||||||||||||||
| 18 | 10 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=20.1
| |||||||||||||||||||||||||||||
| 19 | 10 | ▶ | if (defined $_[0]{rbuf}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 20 | 9 | my $first = substr($_[0]{rbuf},0,1); | |||||||||||||||||||||||||||
| 21 | 9 | my $len = length($_[0]{rbuf}); | |||||||||||||||||||||||||||
| 22 | 9 | my $f = ord($first); | |||||||||||||||||||||||||||
| 23 | 9 | my $header = itm_header($first); | |||||||||||||||||||||||||||
| 24 | 9 | ▶ | if ($header) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | 9 | ▶ | my $size = $header->{size} ? $header->{size} : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 9 | my $payload = substr($_[0]{rbuf},1,$size); | |||||||||||||||||||||||||||
| 27 | 9 | ▶ | if (defined $payload && length($payload) == $size) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $payload and length $payload == $size
MC/DC0%0/2
defined $payload and length $payload == $size
defined $payload
length $payload == $size
Truth table33%1/3
A and B
A= defined $payloadB= length $payload == $size
| |||||||||||||||||||||||||||||
| 28 | 9 | ▶ | my $itm = itm_parse($header,$size ? ($payload) : ()); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 9 | $_[0]{rbuf} = substr($_[0]{rbuf},$size + 1); | |||||||||||||||||||||||||||
| 30 | 9 | $cb->( $_[0], $itm ); | |||||||||||||||||||||||||||
| 31 | 9 | return 1; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | 0 | return 0; | |||||||||||||||||||||||||||
| 34 | } else { | ||||||||||||||||||||||||||||
| 35 | 0 | croak sprintf("unknown packet type"); | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | 1 | return 0; | |||||||||||||||||||||||||||
| 39 | 9 | }; | |||||||||||||||||||||||||||
| 40 | 2 | }); | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 0 | ▶ | sub _ts { | ||||||||||||||||||||||||||
|
Subroutine
_ts: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 43 | 0 | my @t = localtime; | |||||||||||||||||||||||||||
| 44 | 0 | return sprintf "%04d-%02d-%02d %02d:%02d:%02d", | |||||||||||||||||||||||||||
| 45 | $t[5]+1900, $t[4]+1, $t[3], $t[2], $t[1], $t[0]; | ||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 0 | ▶ | sub _print_ts { | ||||||||||||||||||||||||||
|
Subroutine
_print_ts: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 49 | 0 | my ( $label, $line ) = @_; | |||||||||||||||||||||||||||
| 50 | 0 | chomp $line; | |||||||||||||||||||||||||||
| 51 | 0 | ▶ | if ($label eq '2>') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 0 | print STDERR _ts()." $label $line\n"; | |||||||||||||||||||||||||||
| 53 | } else { | ||||||||||||||||||||||||||||
| 54 | 0 | print STDOUT _ts()." $label $line\n"; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | }; | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 0 | ▶ | sub handle { | ||||||||||||||||||||||||||
|
Subroutine
handle: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 59 | 0 | my ( $class, $file, $payload_sub, $cv ) = @_; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 0 | ▶ | my $has_cv = defined $cv ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 0 | ▶ | $cv = AE::cv unless $has_cv; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | # Choose flags so open won't block | ||||||||||||||||||||||||||||
| 66 | 0 | my $flags; | |||||||||||||||||||||||||||
| 67 | 0 | ▶ | if (-p $file) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 0 | $flags = O_RDWR | O_NONBLOCK; | |||||||||||||||||||||||||||
| 69 | } else { | ||||||||||||||||||||||||||||
| 70 | 0 | $flags = O_RDONLY | O_NONBLOCK; | |||||||||||||||||||||||||||
| 71 | 0 | ▶ | $flags |= O_NOCTTY if -c $file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 0 | ▶ | sysopen(my $fh, $file, $flags) or die "sysopen $file: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 0 | binmode($fh, ':raw'); | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | my $handle = AnyEvent::Handle->new( | ||||||||||||||||||||||||||||
| 78 | fh => $fh, | ||||||||||||||||||||||||||||
| 79 | 0 | ▶ | on_error => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 80 | 0 | my ( $handle, $fatal, $message ) = @_; | |||||||||||||||||||||||||||
| 81 | 0 | $handle->destroy; | |||||||||||||||||||||||||||
| 82 | 0 | $cv->send("$fatal: $message"); | |||||||||||||||||||||||||||
| 83 | }, | ||||||||||||||||||||||||||||
| 84 | 0 | ▶ | on_eof => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 85 | 0 | my ( $handle ) = @_; | |||||||||||||||||||||||||||
| 86 | 0 | $handle->destroy; | |||||||||||||||||||||||||||
| 87 | 0 | $cv->send("EOF"); | |||||||||||||||||||||||||||
| 88 | }, | ||||||||||||||||||||||||||||
| 89 | 0 | ▶ | on_read => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 90 | 0 | my $handle = shift; | |||||||||||||||||||||||||||
| 91 | 0 | $handle->push_read( itm => $payload_sub ); | |||||||||||||||||||||||||||
| 92 | }, | ||||||||||||||||||||||||||||
| 93 | 0 | ); | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 0 | ▶ | $cv->recv unless $has_cv; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 0 | return $handle; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 0 | ▶ | sub _run_cmd { | ||||||||||||||||||||||||||
|
Subroutine
_run_cmd: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 101 | 0 | my ($class, @cmd) = @_; | |||||||||||||||||||||||||||
| 102 | 0 | ▶ | die "run_cmd: no command" unless @cmd; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 0 | my ($out_r, $out_w) = portable_pipe; | |||||||||||||||||||||||||||
| 105 | 0 | my ($err_r, $err_w) = portable_pipe; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 0 | my $proc = run_cmd \@cmd, '>' => $out_w, '2>' => $err_w, close_all => 1; | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 0 | close $out_w; | |||||||||||||||||||||||||||
| 110 | 0 | close $err_w; | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 0 | my %cmd = ( | |||||||||||||||||||||||||||
| 113 | cv => AE::cv, | ||||||||||||||||||||||||||||
| 114 | proc => $proc, | ||||||||||||||||||||||||||||
| 115 | ); | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | $cmd{hout} = AnyEvent::Handle->new( | ||||||||||||||||||||||||||||
| 118 | fh => $out_r, | ||||||||||||||||||||||||||||
| 119 | 0 | ▶ | on_read => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 120 | 0 | my ($h) = @_; | |||||||||||||||||||||||||||
| 121 | 0 | ▶ | $h->push_read(line => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 122 | 0 | my ($h, $line) = @_; | |||||||||||||||||||||||||||
| 123 | 0 | _print_ts('>', $line); | |||||||||||||||||||||||||||
| 124 | 0 | }); | |||||||||||||||||||||||||||
| 125 | }, | ||||||||||||||||||||||||||||
| 126 | 0 | ▶ | on_eof => sub { shift->destroy }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 127 | 0 | ▶ | on_error => sub { shift->destroy }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 128 | 0 | ); | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | $cmd{herr} = AnyEvent::Handle->new( | ||||||||||||||||||||||||||||
| 131 | fh => $err_r, | ||||||||||||||||||||||||||||
| 132 | 0 | ▶ | on_read => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 133 | 0 | my ($h) = @_; | |||||||||||||||||||||||||||
| 134 | 0 | ▶ | $h->push_read(line => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 135 | 0 | my ($h, $line) = @_; | |||||||||||||||||||||||||||
| 136 | 0 | _print_ts('2>', $line); | |||||||||||||||||||||||||||
| 137 | 0 | }); | |||||||||||||||||||||||||||
| 138 | }, | ||||||||||||||||||||||||||||
| 139 | 0 | ▶ | on_eof => sub { shift->destroy }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 140 | 0 | ▶ | on_error => sub { shift->destroy }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 141 | 0 | ); | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 0 | ▶ | $proc->cb(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 144 | 0 | my $raw = shift->recv; # like $? | |||||||||||||||||||||||||||
| 145 | 0 | my $code = ($raw >> 8) & 0xff; | |||||||||||||||||||||||||||
| 146 | 0 | my $sig = $raw & 0x7f; | |||||||||||||||||||||||||||
| 147 | 0 | $cmd{exit_code} = $code; | |||||||||||||||||||||||||||
| 148 | 0 | ▶ | $cmd{signal} = $sig if $sig; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | 0 | $cmd{cv}->send($code); | |||||||||||||||||||||||||||
| 150 | 0 | }); | |||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 0 | return \%cmd; # keep this in scope; wait via $obj->{cv}->recv | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 2 | 1; | |||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | __END__ | ||||||||||||||||||||||||||||