| 1 | # -*- coding: utf-8 -*- | ||||||||||||||
| 2 | # Copyright (C) 2011-2012 Rocky Bernstein <rocky@cpan.org> | ||||||||||||||
| 3 | 13 | ▶ | use strict; use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 13 | ▶ | use rlib '../../..'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | |||||||||||||||
| 6 | 13 | ▶ | use Class::Struct; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 13 | ▶ | use Time::HiRes; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | |||||||||||||||
| 9 | 13 | struct CmdProcessorHook => { | |||||||||||||
| 10 | priority => '$', | ||||||||||||||
| 11 | name => '$', | ||||||||||||||
| 12 | fn => '$' | ||||||||||||||
| 13 | }; | ||||||||||||||
| 14 | |||||||||||||||
| 15 | |||||||||||||||
| 16 | package Devel::Trepan::CmdProcessor::Hook; | ||||||||||||||
| 17 | # attr_accessor :list | ||||||||||||||
| 18 | |||||||||||||||
| 19 | sub new($;$) | ||||||||||||||
| 20 | 40 | ▶ | { | ||||||||||||
|
Subroutine
new: called CC=2 SCAR=10.0
Pod
undocumented
| |||||||||||||||
| 21 | 0 | ▶ | my ($class, $list) = @_; | ||||||||||||
|
Subroutine
new: not called CC=2 SCAR=10.0
Pod
undocumented
| |||||||||||||||
| 22 | 0 | my $self = {}; | |||||||||||||
| 23 | 0 | ▶ | $list = [] unless defined $list; | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 24 | 0 | $self->{list} = $list; | |||||||||||||
| 25 | 0 | bless $self, $class; | |||||||||||||
| 26 | 0 | $self; | |||||||||||||
| 27 | } | ||||||||||||||
| 28 | |||||||||||||||
| 29 | sub delete_by_name($) | ||||||||||||||
| 30 | 0 | ▶ | { | ||||||||||||
|
Subroutine
delete_by_name: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||
| 31 | 0 | ▶ | my ($self, $delete_name) = @_; | ||||||||||||
|
Subroutine
delete_by_name: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||
| 32 | 0 | my @new_list = (); | |||||||||||||
| 33 | 0 | for my $elt (@{$self->{list}}) { | |||||||||||||
| 34 | 0 | ▶ | push(@new_list, $elt) unless $elt->name eq $delete_name; | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 35 | } | ||||||||||||||
| 36 | 0 | $self->{list} = \@new_list; | |||||||||||||
| 37 | } | ||||||||||||||
| 38 | |||||||||||||||
| 39 | sub is_empty($) | ||||||||||||||
| 40 | 0 | ▶ | { | ||||||||||||
|
Subroutine
is_empty: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 41 | 0 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
is_empty: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 42 | 0 | return 0 == scalar(@{$self->{list}}); | |||||||||||||
| 43 | } | ||||||||||||||
| 44 | |||||||||||||||
| 45 | sub insert($$$$) | ||||||||||||||
| 46 | 2 | ▶ | { | ||||||||||||
|
Subroutine
insert: called CC=3 SCAR=18.4
Pod
undocumented
| |||||||||||||||
| 47 | 0 | ▶ | my ($self, $priority, $name, $hook) = @_; | ||||||||||||
|
Subroutine
insert: not called CC=3 SCAR=18.4
Pod
undocumented
| |||||||||||||||
| 48 | 0 | my $insert_loc; | |||||||||||||
| 49 | 0 | my @list = $self->{list}; | |||||||||||||
| 50 | 0 | ▶ | for ($insert_loc=0; $insert_loc < $#list; $insert_loc++) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 51 | 0 | my $entry = $self->{list}[$insert_loc]; | |||||||||||||
| 52 | 0 | ▶ | if ($priority > $entry->priority) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 53 | 0 | last; | |||||||||||||
| 54 | } | ||||||||||||||
| 55 | } | ||||||||||||||
| 56 | 0 | my $new_item = CmdProcessorHook->new(name => $name, priority=>$priority, fn => $hook); | |||||||||||||
| 57 | 0 | splice(@{$self->{list}}, $insert_loc, 0, $new_item); | |||||||||||||
| 58 | } | ||||||||||||||
| 59 | |||||||||||||||
| 60 | sub insert_if_new($$$$) | ||||||||||||||
| 61 | 2 | ▶ | { | ||||||||||||
|
Subroutine
insert_if_new: called CC=4 SCAR=17.9
Pod
undocumented
| |||||||||||||||
| 62 | 0 | ▶ | my ($self, $priority, $name, $hook) = @_; | ||||||||||||
|
Subroutine
insert_if_new: not called CC=4 SCAR=17.9
Pod
undocumented
| |||||||||||||||
| 63 | 0 | my $found = 0; | |||||||||||||
| 64 | 0 | for my $item (@{$self->{list}}) { | |||||||||||||
| 65 | 0 | ▶ | if ($item->name eq $name) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 66 | 0 | $found = 1; | |||||||||||||
| 67 | 0 | last; | |||||||||||||
| 68 | } | ||||||||||||||
| 69 | } | ||||||||||||||
| 70 | 0 | ▶ | $self->insert($priority, $name, $hook) unless ($found); | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 71 | } | ||||||||||||||
| 72 | |||||||||||||||
| 73 | # Run each function in `hooks' with args | ||||||||||||||
| 74 | sub run($) | ||||||||||||||
| 75 | 2 | ▶ | { | ||||||||||||
|
Subroutine
run: called CC=2 SCAR=9.2
Pod
undocumented
| |||||||||||||||
| 76 | 0 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
run: not called CC=2 SCAR=9.2
Pod
undocumented
| |||||||||||||||
| 77 | 0 | for my $hook (@{$self->{list}}) { | |||||||||||||
| 78 | 0 | $hook->fn->($hook->name, \@_); | |||||||||||||
| 79 | } | ||||||||||||||
| 80 | } | ||||||||||||||
| 81 | |||||||||||||||
| 82 | package Devel::Trepan::CmdProcessor; | ||||||||||||||
| 83 | |||||||||||||||
| 84 | # # Command processor hooks. | ||||||||||||||
| 85 | # attr_reader :autolist_hook | ||||||||||||||
| 86 | # attr_reader :timer_hook | ||||||||||||||
| 87 | # attr_reader :trace_hook | ||||||||||||||
| 88 | # attr_reader :tracebuf_hook | ||||||||||||||
| 89 | # attr_reader :unconditional_prehooks | ||||||||||||||
| 90 | # attr_reader :cmdloop_posthooks | ||||||||||||||
| 91 | # attr_reader :cmdloop_prehooks | ||||||||||||||
| 92 | |||||||||||||||
| 93 | # # Used to time how long a debugger action takes | ||||||||||||||
| 94 | # attr_accessor :time_last | ||||||||||||||
| 95 | |||||||||||||||
| 96 | sub hook_initialize($) | ||||||||||||||
| 97 | 13 | ▶ | { | ||||||||||||
|
Subroutine
hook_initialize: called CC=1 SCAR=3.1
Pod
undocumented
| |||||||||||||||
| 98 | 0 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
hook_initialize: not called CC=1 SCAR=3.1
Pod
undocumented
| |||||||||||||||
| 99 | 0 | my $commands = $self->{commands}; | |||||||||||||
| 100 | 0 | $self->{cmdloop_posthooks} = Devel::Trepan::CmdProcessor::Hook->new; | |||||||||||||
| 101 | 0 | $self->{cmdloop_prehooks} = Devel::Trepan::CmdProcessor::Hook->new; | |||||||||||||
| 102 | 0 | $self->{unconditional_prehooks} = Devel::Trepan::CmdProcessor::Hook->new; | |||||||||||||
| 103 | |||||||||||||||
| 104 | 0 | my $list_cmd = $commands->{'list'}; | |||||||||||||
| 105 | $self->{autolist_hook} = ['autolist', | ||||||||||||||
| 106 | 0 | ▶ | sub{ $list_cmd->run(['list']) if $list_cmd}]; | ||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=14.0
Branch: 1 branch
| |||||||||||||||
| 107 | |||||||||||||||
| 108 | $self->{timer_hook} = ['timer', | ||||||||||||||
| 109 | 0 | ▶ | sub{ | ||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||
| 110 | 0 | my $now = Time::HiRes::time; | |||||||||||||
| 111 | $self->{time_last} = $now unless | ||||||||||||||
| 112 | 0 | ▶ | defined $self->{time_last}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 113 | 0 | my $mess = sprintf("%g seconds", $now - $self->{time_last}); | |||||||||||||
| 114 | 0 | $self->msg($mess); | |||||||||||||
| 115 | 0 | $self->{time_last} = $now; | |||||||||||||
| 116 | 0 | }]; | |||||||||||||
| 117 | $self->{timer_posthook} = ['timer', | ||||||||||||||
| 118 | 0 | ▶ | sub{ | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=2.6
| |||||||||||||||
| 119 | 0 | $self->{time_last} = Time::HiRes::time}]; | |||||||||||||
| 120 | $self->{trace_hook} = ['trace', | ||||||||||||||
| 121 | 0 | ▶ | sub{ | ||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=14.0
| |||||||||||||||
| 122 | $self->print_location unless | ||||||||||||||
| 123 | 0 | ▶ | $self->{terminated} } ]; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 124 | $self->{tracebuf_hook} = ['tracebuffer', | ||||||||||||||
| 125 | 0 | ▶ | sub{ | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 126 | 0 | push(@{$self->{eventbuf}}, | |||||||||||||
| 127 | 0 | ($self->{event}, $self->{frame})); | |||||||||||||
| 128 | 0 | }]; | |||||||||||||
| 129 | } | ||||||||||||||
| 130 | |||||||||||||||
| 131 | 13 | ▶ | unless (caller) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 132 | # Demo it. | ||||||||||||||
| 133 | 0 | my $hooks = Devel::Trepan::CmdProcessor::Hook->new(); | |||||||||||||
| 134 | 0 | $hooks->run(5); | |||||||||||||
| 135 | 0 | ▶ | my $hook1 = sub($$) { | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 136 | 0 | my ($name, $a) = @_; | |||||||||||||
| 137 | 0 | my $args = join(', ', @$a); | |||||||||||||
| 138 | 0 | print "${name} called with $args\n"; | |||||||||||||
| 139 | 0 | }; | |||||||||||||
| 140 | 0 | $hooks = Devel::Trepan::CmdProcessor::Hook->new(); | |||||||||||||
| 141 | 0 | $hooks->insert(-1, 'hook1', $hook1); | |||||||||||||
| 142 | 0 | $hooks->insert_if_new(-1, 'hook1', $hook1); | |||||||||||||
| 143 | 0 | my $dash_line = '-' x 30 . "\n"; | |||||||||||||
| 144 | 0 | print $dash_line; | |||||||||||||
| 145 | 0 | print join(', ', @{$hooks->{list}}), "\n"; | |||||||||||||
| 146 | 0 | $hooks->run(10); | |||||||||||||
| 147 | 0 | print $dash_line; | |||||||||||||
| 148 | 0 | $hooks->insert(-1, 'hook2', $hook1); | |||||||||||||
| 149 | 0 | $hooks->run(20); | |||||||||||||
| 150 | 0 | print $dash_line; | |||||||||||||
| 151 | 0 | $hooks->delete_by_name('hook2'); | |||||||||||||
| 152 | 0 | $hooks->run(30); | |||||||||||||
| 153 | 0 | print $dash_line; | |||||||||||||
| 154 | 0 | $hooks->delete_by_name('hook1'); | |||||||||||||
| 155 | 0 | $hooks->run(30); | |||||||||||||
| 156 | 0 | print $dash_line; | |||||||||||||
| 157 | } | ||||||||||||||
| 158 | |||||||||||||||
| 159 | 13 | 1; | |||||||||||||