AnyEvent-GDB
view release on metacpan or search on metacpan
=item $results = $gdb->cmd_sync ($command => [$option...], $parameter...])
Like C<cmd>, but blocks execution until the command has been executed, and
returns the results if sucessful. Croaks when GDB returns with an error.
This is purely a convenience method for small scripts: since it blocks
execution using a condvar, it is not suitable to be used inside callbacks
or modules.
That is, unless L<Coro> is used - with Coro, you can run multiple
C<cmd_sync> methods concurrently form multiple threads, with no issues.
=cut
sub cmd_sync {
push @_, my $cv = AE::cv;
&cmd;
my ($class, $results, $console) = $cv->recv;
Carp::croak $results->{msg}
my $cb;
$cb = $self->can ("on_event") and $cb->($self, $event, @args);
$cb = $self-> {on_event} and $cb->($self, $event, @args);
$cb = $self->can ("on_$event") and $cb->($self, $event, @args);
$cb = $self-> {"on_$event"} and $cb->($self, $event, @args);
}
# predefined events
sub on_notify_thread_group_added {
my ($self, undef, $r) = @_;
$self->{thread_group}{$r->{id}} = $r;
}
sub on_notify_thread_group_removed {
my ($self, undef, $r) = @_;
delete $self->{thread_group}{$r->{id}};
}
sub on_notify_thread_group_started {
my ($self, undef, $r) = @_;
delete $self->{thread_group}{exit_code};
$self->{thread_group}{$r->{id}}{pid} = $r->{pid};
}
sub on_notify_thread_group_exited {
my ($self, undef, $r) = @_;
delete $self->{thread_group}{pid};
$self->{thread_group}{$r->{id}}{exit_code} = $r->{exit_code};
}
sub on_notify_record_started {
my ($self, undef, $r) = @_;
$self->{thread_group}{$r->{id}}{recording} = 1;
}
sub on_notify_record_stopped {
my ($self, undef, $r) = @_;
$self->{thread_group}{$r->{id}}{recording} = 0;
}
sub on_notify_thread_created {
my ($self, undef, $r) = @_;
$self->{thread}{$r->{id}} = $r;
}
sub on_notify_thread_exited {
my ($self, undef, $r) = @_;
delete $self->{thread}{$r->{id}};
}
sub _threads {
my ($self, $id) = @_;
ref $id
? @{ $self->{thread} }{@$id}
: $id eq "all"
? values %{ $self->{thread} }
: $self->{thread}{$id}
}
sub on_exec_running {
my ($self, undef, $r) = @_;
for ($self->_threads ($r->{thread_id})) {
delete $_->{stopped};
$_->{running} = 1;
}
}
sub on_exec_stopped {
my ($self, undef, $r) = @_;
for ($self->_threads ($r->{stopped_threads})) {
delete $_->{running};
$_->{stopped} = $r;
}
# $self->event ("thread_$r->{reason}" => $r, [map $_->{id}, $self->_threads ($r)]);
}
sub _thread_groups {
my ($self, $r) = @_;
exists $r->{thread_group}
? $self->{thread_group}{$r->{thread_group}}
: values %{ $self->{thread_group} }
}
sub on_notify_library_loaded {
my ($self, undef, $r) = @_;
$_->{library}{$r->{id}} = $r
for $self->_thread_groups ($r);
}
sub on_notify_library_unloaded {
my ($self, undef, $r) = @_;
delete $_->{library}{$r->{id}}
for $self->_thread_groups ($r);
}
=back
=head2 EVENTS
AnyEvent::GDB is asynchronous in nature, as the goal of the MI interface
is to be fully asynchronous. Due to this, a user of this interface must
be prepared to handle various events.
Called for GDB C<exec>, C<status> and C<notify> event: TYPE is one
of these three strings, the class of the event (with C<-> replaced b
C<_>s) is appended to it to form the TYPE_CLASS (e.g. C<exec_stopped> or
C<notify_library_loaded>).
=back
=head2 STATUS STORAGE
The default implementations of the event method store the thread,
thread_group, recording, library and running status insid ethe C<$gdb>
object.
You can access these at any time. Specifically, the following information
is available:
=over 4
=item C<< $gdb->{thread_group}{I<id>} >>
The C<thread_group> member stores a hash for each existing thread
group. The hash always contains the C<id> member, but might also contain
other members.
=item C<< $gdb->{thread_group}{I<id>}{pid} >>
The C<pid> member only exists while the thread group is running a program,
and contaisn the PID of the program.
=item C<< $gdb->{thread_group}{I<id>}{exit_code} >>
The C<exit_code> member only exists after a program has finished
executing, and before it is started again, and contains the exit code of
the program.
=item C<< $gdb->{thread_group}{I<id>}{recording} >>
The C<recording> member only exists if recording has been previously
started, and is C<1> if recoridng is currently active, and C<0> if it has
been stopped again.
=item C<< $gdb->{thread}{I<id>} >>
The C<thread> member stores a hash for each existing thread. The hash
always contains the C<id> member with the thread id, and the C<group_id>
member with the corresponding thread group id.
=item C<< $gdb->{thread}{I<id>}{running} >>
The C<running> member is C<1> while the thread is, well, running, and is
missing otherwise.
=item C<< $gdb->{thread}{I<id>}{stopped} >>
The C<stopped> member contains the result list from the C<on_exec_stopped>
notification that caused the thread to stop, and only exists when the
thread is topped.
=item C<< $gdb->{library}{I<id>} >>
The C<library> member contains all results from the C<on_library_loaded>
event (such as C<id>, C<target_name>, C<host_name> and potentially a
C<thread_group>.
=back
=head1 SEE ALSO
L<AnyEvent>, L<http://sourceware.org/gdb/current/onlinedocs/gdb/GDB_002fMI.html#GDB_002fMI>.
=head1 AUTHOR
Marc Lehmann <schmorp@schmorp.de>
[$option...], $parameter...])
Like "cmd", but blocks execution until the command has been
executed, and returns the results if sucessful. Croaks when GDB
returns with an error.
This is purely a convenience method for small scripts: since it
blocks execution using a condvar, it is not suitable to be used
inside callbacks or modules.
That is, unless Coro is used - with Coro, you can run multiple
"cmd_sync" methods concurrently form multiple threads, with no
issues.
EVENTS
AnyEvent::GDB is asynchronous in nature, as the goal of the MI interface
is to be fully asynchronous. Due to this, a user of this interface must
be prepared to handle various events.
When an event is produced, the GDB object will look for the following
four handlers and, if found, will call each one in order with the GDB
object and event name (without "on_") as the first two arguments,
TYPE, and one for TYPE_CLASS. Usuaully you should provide the more
specific event (TYPE_CLASS).
on_TYPE_CLASS => $cb->($gdb, "TYPE_CLASS", $results)
Called for GDB "exec", "status" and "notify" event: TYPE is one of
these three strings, the class of the event (with "-" replaced b
"_"s) is appended to it to form the TYPE_CLASS (e.g. "exec_stopped"
or "notify_library_loaded").
STATUS STORAGE
The default implementations of the event method store the thread,
thread_group, recording, library and running status insid ethe $gdb
object.
You can access these at any time. Specifically, the following
information is available:
"$gdb->{thread_group}{*id*}"
The "thread_group" member stores a hash for each existing thread
group. The hash always contains the "id" member, but might also
contain other members.
"$gdb->{thread_group}{*id*}{pid}"
The "pid" member only exists while the thread group is running a
program, and contaisn the PID of the program.
"$gdb->{thread_group}{*id*}{exit_code}"
The "exit_code" member only exists after a program has finished
executing, and before it is started again, and contains the exit
code of the program.
"$gdb->{thread_group}{*id*}{recording}"
The "recording" member only exists if recording has been previously
started, and is 1 if recoridng is currently active, and 0 if it has
been stopped again.
"$gdb->{thread}{*id*}"
The "thread" member stores a hash for each existing thread. The hash
always contains the "id" member with the thread id, and the
"group_id" member with the corresponding thread group id.
"$gdb->{thread}{*id*}{running}"
The "running" member is 1 while the thread is, well, running, and is
missing otherwise.
"$gdb->{thread}{*id*}{stopped}"
The "stopped" member contains the result list from the
"on_exec_stopped" notification that caused the thread to stop, and
only exists when the thread is topped.
"$gdb->{library}{*id*}"
The "library" member contains all results from the
"on_library_loaded" event (such as "id", "target_name", "host_name"
and potentially a "thread_group".
SEE ALSO
AnyEvent,
<http://sourceware.org/gdb/current/onlinedocs/gdb/GDB_002fMI.html#GDB_00
2fMI>.
AUTHOR
Marc Lehmann <schmorp@schmorp.de>
http://home.schmorp.de/
( run in 0.782 second using v1.01-cache-2.11-cpan-3cd7ad12f66 )