| 1 |
=head1 NAME |
| 2 |
|
| 3 |
AnyEvent::GDB - asynchronous GDB machine interface interface |
| 4 |
|
| 5 |
=head1 SYNOPSIS |
| 6 |
|
| 7 |
use AnyEvent::GDB; |
| 8 |
|
| 9 |
=head1 DESCRIPTION |
| 10 |
|
| 11 |
This module is an L<AnyEvent> user, you need to make sure that you use and |
| 12 |
run a supported event loop. |
| 13 |
|
| 14 |
It implements the GDB MI protocol, which can be used to talk to GDB |
| 15 |
without having to parse the ever changing command syntax aimed at humans. |
| 16 |
|
| 17 |
It properly quotes your commands and parses the data structures returned |
| 18 |
by GDB. |
| 19 |
|
| 20 |
At the moment, it's in an early stage of development, so expect changes, |
| 21 |
and, over time, further features (such as breakpoint-specific callbacks |
| 22 |
and so on). |
| 23 |
|
| 24 |
=head1 EXAMPLE PROGRAM |
| 25 |
|
| 26 |
To get you started, here is an example program that runs F</bin/ls>, |
| 27 |
displaying the stopped information when hitting a breakpoint on C<_exit>: |
| 28 |
|
| 29 |
use Data::Dump; |
| 30 |
use AnyEvent::GDB; |
| 31 |
|
| 32 |
our $gdb = new AnyEvent::GDB |
| 33 |
trace => 1, |
| 34 |
on_exec_stopped => sub { |
| 35 |
ddx $_[0]; |
| 36 |
}, |
| 37 |
; |
| 38 |
|
| 39 |
my $done |
| 40 |
|
| 41 |
ddx $gdb->cmd_sync (file_exec_and_symbols => "/bin/ls"); |
| 42 |
ddx $gdb->cmd_sync (break_insert => "_exit"); |
| 43 |
ddx $gdb->cmd_sync ("exec_run"); |
| 44 |
|
| 45 |
AE::cv->recv; |
| 46 |
|
| 47 |
=head2 PROTOCOL QUIRKS |
| 48 |
|
| 49 |
=head3 Minus vs. underscores |
| 50 |
|
| 51 |
The MI protocol uses C<-> to separate name components, while in Perl, you |
| 52 |
use C<_> for this purpose. |
| 53 |
|
| 54 |
This module usually accepts either form as input, and always converts |
| 55 |
names with C<-> to names with C<_>, so the C<library-loaded> notify might |
| 56 |
become C<notify_library_loaded>, and the C<host-name> result in that event |
| 57 |
is stored in the C<host_name> hash element in Perl. |
| 58 |
|
| 59 |
=head3 Output redirection |
| 60 |
|
| 61 |
Unfortunately, GDB has no (portable) provision to separate GDB |
| 62 |
input/output from program input/output. Obviously, without a distinction |
| 63 |
between program I/O and GDB I/O it becomes impossible to safely control |
| 64 |
GDB. |
| 65 |
|
| 66 |
There are two ways for you around it: redirect stdin/stdout yourself, or |
| 67 |
set a tty (eg. with the C<inferior_set_tty> command). |
| 68 |
|
| 69 |
Unfortunately, the MI interface does not seem to support any kind |
| 70 |
of I/O redirection, so this module helps you a bit, by setting the |
| 71 |
C<exec-wrapper> variable with a console C<set> commmand. That is, this |
| 72 |
module does soeQmthing like the following for you, providing proper file |
| 73 |
descriptors for your actual stdin and stdout: |
| 74 |
|
| 75 |
set exec-wrapper <&5 >&6 |
| 76 |
|
| 77 |
The actual I/O redirection operators are also stored in C<< $gdb->{stdio} |
| 78 |
>>, so you can even do it yourself, e.g. when providing your own wrapper: |
| 79 |
|
| 80 |
$self->cmd_raw ("set exec-wrapper $self->{stdio}", sub { }); |
| 81 |
|
| 82 |
(You need to use a raw command, as the "correct" C<gdb_set> MI command |
| 83 |
silently ignores any C<exec-wrapper> setting). |
| 84 |
|
| 85 |
=cut |
| 86 |
|
| 87 |
package AnyEvent::GDB; |
| 88 |
|
| 89 |
use common::sense; |
| 90 |
|
| 91 |
use Carp (); |
| 92 |
use Fcntl (); |
| 93 |
use Scalar::Util (); |
| 94 |
|
| 95 |
use AnyEvent (); |
| 96 |
use AnyEvent::Util (); |
| 97 |
|
| 98 |
our $VERSION = '0.2'; |
| 99 |
|
| 100 |
=head2 METHODS |
| 101 |
|
| 102 |
=over 4 |
| 103 |
|
| 104 |
=item $gdb = new AnyEvent::GDB key => value... |
| 105 |
|
| 106 |
Create a new GDB object using the given named parameters. |
| 107 |
|
| 108 |
For initial experiments, it is highly recommended to run with tracing or |
| 109 |
at least C<verbose> enabled. And don't forget to provide an C<on_eof> |
| 110 |
callback. |
| 111 |
|
| 112 |
my $gdb = new AnyEvent::GDB |
| 113 |
on_eof => sub { |
| 114 |
print "We are done.\n"; |
| 115 |
}, |
| 116 |
trace => 1; # or verbose => 1, for less output |
| 117 |
|
| 118 |
=over 4 |
| 119 |
|
| 120 |
=item exec => $path (default: "gdb") |
| 121 |
|
| 122 |
The path of the GDB executable. |
| 123 |
|
| 124 |
=item args => [$string...] (default: ["-n"]) |
| 125 |
|
| 126 |
An optional array of parameters to pass to GDB. This should not be |
| 127 |
used to load a program executable, use the C<file_exec_and_symbols>, |
| 128 |
C<target_attach> or similar MI commands instead. |
| 129 |
|
| 130 |
=item trace => $boolean (default: 0) |
| 131 |
|
| 132 |
If true, then all commands sent to GDB are printed to STDOUT prefixed with |
| 133 |
"> ", and all replies received from GDB are printed to STDOUT prefixed |
| 134 |
with "< ". |
| 135 |
|
| 136 |
=item verbose => $boolean (default: true if trace is enabled, false otherwise) |
| 137 |
|
| 138 |
If true, then log output and possibly other information is printed to |
| 139 |
STDOUT. |
| 140 |
|
| 141 |
=item on_xxxx => $callback->(...) |
| 142 |
|
| 143 |
This specifies a callback for a specific event - see the L<EVENTS> section |
| 144 |
later in this document. |
| 145 |
|
| 146 |
=back |
| 147 |
|
| 148 |
=cut |
| 149 |
|
| 150 |
sub new { |
| 151 |
my ($class, %arg) = @_; |
| 152 |
|
| 153 |
my $self = bless { |
| 154 |
%arg, |
| 155 |
}, $class; |
| 156 |
|
| 157 |
my $exe = delete $self->{exec} // "gdb"; |
| 158 |
my $arg = delete $self->{args} // [qw(-n)]; |
| 159 |
|
| 160 |
$self->{verbose} = 1 |
| 161 |
if $self->{trace} && !exists $self->{verbose}; |
| 162 |
|
| 163 |
($self->{fh}, my $fh2) = AnyEvent::Util::portable_socketpair; |
| 164 |
|
| 165 |
$self->{pid} = fork; |
| 166 |
|
| 167 |
open my $stdin , "<&STDIN" ; |
| 168 |
open my $stdout, ">&STDOUT"; |
| 169 |
|
| 170 |
unless ($self->{pid}) { |
| 171 |
if (defined $self->{pid}) { |
| 172 |
open STDIN , "<&", $fh2; |
| 173 |
open STDOUT, ">&", $fh2; |
| 174 |
fcntl $stdin , Fcntl::F_SETFD, 0; |
| 175 |
fcntl $stdout, Fcntl::F_SETFD, 0; |
| 176 |
exec $exe, qw(--interpreter=mi2 -q), @$arg; |
| 177 |
kill 9, 0; # don't want to load the POSIX module just for this |
| 178 |
} else { |
| 179 |
Carp::croak "cannot fork: $!"; |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
AnyEvent::Util::fh_nonblocking $self->{fh}, 1; |
| 184 |
|
| 185 |
{ |
| 186 |
Scalar::Util::weaken (my $self = $self); |
| 187 |
$self->{rw} = AE::io $self->{fh}, 0, sub { |
| 188 |
my $len = sysread $self->{fh}, $self->{rbuf}, 256, length $self->{rbuf}; |
| 189 |
|
| 190 |
defined $len || $self->eof; |
| 191 |
|
| 192 |
$self->feed ("$1") |
| 193 |
while $self->{rbuf} =~ s/^([^\r\n]*)\r?\n//; |
| 194 |
}; |
| 195 |
|
| 196 |
$self->{wcb} = sub { |
| 197 |
my $len = syswrite $self->{fh}, $self->{wbuf}; |
| 198 |
substr $self->{wbuf}, 0, $len, ""; |
| 199 |
delete $self->{ww} unless length $self->{wbuf}; |
| 200 |
}; |
| 201 |
} |
| 202 |
|
| 203 |
$self->{stdio} = sprintf "<&%d >&%d", fileno $stdin, fileno $stdout; |
| 204 |
|
| 205 |
$self->cmd_raw ("set exec-wrapper $self->{stdio}", sub { }); |
| 206 |
|
| 207 |
$self |
| 208 |
} |
| 209 |
|
| 210 |
#sub DESTROY { |
| 211 |
#)} |
| 212 |
|
| 213 |
sub eof { |
| 214 |
my ($self) = @_; |
| 215 |
|
| 216 |
$self->event ("eof"); |
| 217 |
|
| 218 |
%$self = (); |
| 219 |
} |
| 220 |
|
| 221 |
sub send { |
| 222 |
my ($self, $data) = @_; |
| 223 |
|
| 224 |
print "> $data" |
| 225 |
if $self->{trace}; |
| 226 |
|
| 227 |
$self->{wbuf} .= $data; |
| 228 |
$self->{ww} ||= AE::io $self->{fh}, 1, $self->{wcb}; |
| 229 |
} |
| 230 |
|
| 231 |
our %C_ESCAPE = ( |
| 232 |
"\\" => "\\", |
| 233 |
'"' => '"', |
| 234 |
"'" => "'", |
| 235 |
"?" => "?", |
| 236 |
|
| 237 |
a => "\x07", |
| 238 |
b => "\x08", |
| 239 |
t => "\x09", |
| 240 |
n => "\x0a", |
| 241 |
v => "\x0b", |
| 242 |
f => "\x0c", |
| 243 |
r => "\x0d", |
| 244 |
); |
| 245 |
|
| 246 |
sub _parse_c_string { |
| 247 |
my $r = ""; |
| 248 |
|
| 249 |
# syntax is not documented, so we do full C99, except unicode |
| 250 |
|
| 251 |
while () { |
| 252 |
if (/\G([^"\\\n]+)/gc) { |
| 253 |
$r .= $1; |
| 254 |
} elsif (/\G\\([abtnvfr\\"'?])/gc) { |
| 255 |
$r .= $C_ESCAPE{$1}; |
| 256 |
} elsif (/\G\\([0-8]{1,3})/gc) { |
| 257 |
$r .= chr oct $1; |
| 258 |
} elsif (/\G\\x([0-9a-fA-F]+)/gc) { |
| 259 |
$r .= chr hex $1; |
| 260 |
} elsif (/\G"/gc) { |
| 261 |
last; |
| 262 |
} else { |
| 263 |
die "invalid string syntax\n"; |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
$r |
| 268 |
} |
| 269 |
|
| 270 |
sub _parse_value { |
| 271 |
if (/\G"/gc) { # c-string |
| 272 |
&_parse_c_string |
| 273 |
|
| 274 |
} elsif (/\G\{/gc) { # tuple |
| 275 |
my $r = &_parse_results; |
| 276 |
|
| 277 |
/\G\}/gc |
| 278 |
or die "tuple does not end with '}'\n"; |
| 279 |
|
| 280 |
$r |
| 281 |
|
| 282 |
} elsif (/\G\[/gc) { # list |
| 283 |
my @r; |
| 284 |
|
| 285 |
until (/\G\]/gc) { |
| 286 |
# if GDB outputs "result" in lists, let me know and uncomment the following lines |
| 287 |
# # list might also contain key value pairs, but apparently |
| 288 |
# # those are supposed to be ordered, so we use an array in perl. |
| 289 |
# push @r, $1 |
| 290 |
# if /\G([^=,\[\]\{\}]+)=/gc; |
| 291 |
|
| 292 |
push @r, &_parse_value; |
| 293 |
|
| 294 |
/\G,/gc |
| 295 |
or last; |
| 296 |
} |
| 297 |
|
| 298 |
/\G\]/gc |
| 299 |
or die "list does not end with ']'\n"; |
| 300 |
|
| 301 |
\@r |
| 302 |
|
| 303 |
} else { |
| 304 |
die "value expected\n"; |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
sub _parse_results { |
| 309 |
my %r; |
| 310 |
|
| 311 |
# syntax for string is undocumented |
| 312 |
while (/\G([^=,\[\]\{\}]+)=/gc) { |
| 313 |
my $k = $1; |
| 314 |
|
| 315 |
$k =~ y/-/_/; |
| 316 |
|
| 317 |
$r{$k} = &_parse_value; |
| 318 |
|
| 319 |
/\G,/gc |
| 320 |
or last; |
| 321 |
} |
| 322 |
|
| 323 |
\%r |
| 324 |
} |
| 325 |
|
| 326 |
my %type_map = qw( |
| 327 |
* exec |
| 328 |
+ status |
| 329 |
= notify |
| 330 |
); |
| 331 |
|
| 332 |
sub feed { |
| 333 |
my ($self, $line) = @_; |
| 334 |
|
| 335 |
print "< $line\n" |
| 336 |
if $self->{trace}; |
| 337 |
|
| 338 |
for ($line) { |
| 339 |
if (/^\(gdb\)\s*$/gc) { # docs say "(gdb)", but reality says "(gdb) " |
| 340 |
# nop |
| 341 |
} else { |
| 342 |
/^([0-9]*)/gc; # [token], actually ([0-9]+)? |
| 343 |
my $token = $1; |
| 344 |
|
| 345 |
eval { |
| 346 |
if (/\G\^(done|running|connected|error|exit)/gc) { # result |
| 347 |
my $class = $1 eq "running" ? "done" : $1; |
| 348 |
# documented for error is an incompatible format, but in reality it is sane |
| 349 |
|
| 350 |
my $results = /\G,/gc ? &_parse_results : {}; |
| 351 |
|
| 352 |
if (my $cb = delete $self->{cb}{$token}) { |
| 353 |
# unfortunately, gdb sometimes outputs multiple result records for one command |
| 354 |
$cb->($class, $results, delete $self->{console}); |
| 355 |
} |
| 356 |
|
| 357 |
} elsif (/\G([*+=])([^,]+)/gc) { # *exec-async, +status-async, =notify-async |
| 358 |
my ($type, $class) = ($type_map{$1}, $2); |
| 359 |
|
| 360 |
my $results = /\G,/gc ? &_parse_results : {}; |
| 361 |
|
| 362 |
$class =~ y/-/_/; |
| 363 |
|
| 364 |
$self->event ($type => $class, $results); |
| 365 |
$self->event ("$type\_$class" => $results); |
| 366 |
|
| 367 |
} elsif (/\G~"/gc) { |
| 368 |
push @{ $self->{console} }, &_parse_c_string; |
| 369 |
} elsif (/\G&"/gc) { |
| 370 |
my $log = &_parse_c_string; |
| 371 |
chomp $log; |
| 372 |
print "$log\n" if $self->{verbose}; |
| 373 |
$self->event (log => $log); |
| 374 |
} elsif (/\G\@"/gc) { |
| 375 |
$self->event (target => &_parse_c_string); |
| 376 |
} |
| 377 |
}; |
| 378 |
|
| 379 |
/\G(.{0,16})/gcs; |
| 380 |
$@ = "extra data\n" if !$@ and length $1; |
| 381 |
|
| 382 |
if ($@) { |
| 383 |
chop $@; |
| 384 |
warn "AnyEvent::GDB: parse error: $@, at ...$1\n"; |
| 385 |
$self->eof; |
| 386 |
} |
| 387 |
} |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
sub _q($) { |
| 392 |
return $_[0] |
| 393 |
if $_[0] =~ /^[A-Za-z0-9_]+$/; # we are a lot more strict than the spec |
| 394 |
|
| 395 |
local $_ = shift; |
| 396 |
utf8::encode $_; # just in case |
| 397 |
s/([^\x20-\x21\x23-\x5b\x5d-\x7e])/sprintf "\\x%02x", ord $1/ge; |
| 398 |
"\"$_\"" |
| 399 |
} |
| 400 |
|
| 401 |
=item $gdb->cmd_raw ($command, $cb->($class, $results, $console)) |
| 402 |
|
| 403 |
Execute a raw command: C<$command> is sent unchanged to GDB. See C<cmd_> |
| 404 |
for a description of the callback arguments. |
| 405 |
|
| 406 |
Example: execute a CLI command and print its output. |
| 407 |
|
| 408 |
$gdb->cmd_raw ("info sh", sub { |
| 409 |
print "$_[3]\n"; |
| 410 |
}); |
| 411 |
|
| 412 |
=cut |
| 413 |
|
| 414 |
sub cmd_raw { |
| 415 |
my ($self, $cmd, $cb) = @_; |
| 416 |
|
| 417 |
my $token = ++$self->{token}; |
| 418 |
$self->send ("$token$cmd\n"); |
| 419 |
$self->{cb}{$token} = $cb; |
| 420 |
} |
| 421 |
|
| 422 |
=item $gdb->cmd ($command => [$option...], $parameter..., $cb->($class, $results, $console)) |
| 423 |
|
| 424 |
Execute a MI command and invoke the callback with the results. |
| 425 |
|
| 426 |
C<$command> is a MI command name. The leading minus sign can be omitted, |
| 427 |
and instead of minus signs, you can use underscores, i.e. all the |
| 428 |
following command names are equivalent: |
| 429 |
|
| 430 |
"-break-insert" # as documented in the GDB manual |
| 431 |
-break_insert # using underscores and _ to avoid having to quote |
| 432 |
break_insert # ditto, when e.g. used to the left of a => |
| 433 |
"break-insert" # no leading minus |
| 434 |
|
| 435 |
The second argument is an optional array reference with options (i.e. it |
| 436 |
can simply be missing). Each C<$option> is either an option name (similar |
| 437 |
rules as with command names, i.e. no initial C<-->) or an array reference |
| 438 |
with the first element being the option name, and the remaining elements |
| 439 |
being parameters: [$option, $parameter...]. |
| 440 |
|
| 441 |
The remaining arguments, excluding the last one, are simply the parameters |
| 442 |
passed to GDB. |
| 443 |
|
| 444 |
All options and parameters will be properly quoted. |
| 445 |
|
| 446 |
When the command is done, the callback C<$cb> will be invoked with |
| 447 |
C<$class> being one of C<done>, C<connected>, C<error> or C<exit> |
| 448 |
(note: not C<running>), C<$results> being a has reference with all the |
| 449 |
C<variable=value> pairs from the result list. |
| 450 |
|
| 451 |
C<$console> is an array reference with all the GDB console messages |
| 452 |
written while command executes (for MI commands, this should always be |
| 453 |
C<undef> and can be ignored). |
| 454 |
|
| 455 |
Example: #todo# |
| 456 |
|
| 457 |
=cut |
| 458 |
|
| 459 |
sub cmd { |
| 460 |
my $cb = pop; |
| 461 |
my ($self, $cmd, @arg) = @_; |
| 462 |
|
| 463 |
$cmd =~ s/^[\-_]?/_/; |
| 464 |
$cmd =~ y/_/-/; |
| 465 |
|
| 466 |
$cmd .= " "; |
| 467 |
|
| 468 |
my $opt = ref $arg[0] ? shift @arg : []; |
| 469 |
|
| 470 |
for (@$opt) { |
| 471 |
$cmd .= "-"; |
| 472 |
$cmd .= (_q $_) . " " |
| 473 |
for (ref) ? @$_ : $_; |
| 474 |
} |
| 475 |
|
| 476 |
# the MI syntax is inconsistent, providing "--" in case |
| 477 |
# parameters start with "-", but not allowing "-" as first |
| 478 |
# char of a parameter. in fact, "--" is flagged as unknown |
| 479 |
# option. |
| 480 |
if (@arg) { |
| 481 |
# $cmd .= "-- "; |
| 482 |
|
| 483 |
$cmd .= (_q $_) . " " |
| 484 |
for @arg; |
| 485 |
} |
| 486 |
|
| 487 |
# remove trailing " " |
| 488 |
substr $cmd, -1, 1, ""; |
| 489 |
|
| 490 |
$self->cmd_raw ($cmd, $cb); |
| 491 |
} |
| 492 |
|
| 493 |
=item ($results, $console) = $gdb->cmd_sync ($command => [$option...], $parameter...]) |
| 494 |
=item $results = $gdb->cmd_sync ($command => [$option...], $parameter...]) |
| 495 |
|
| 496 |
Like C<cmd>, but blocks execution until the command has been executed, and |
| 497 |
returns the results if sucessful. Croaks when GDB returns with an error. |
| 498 |
|
| 499 |
This is purely a convenience method for small scripts: since it blocks |
| 500 |
execution using a condvar, it is not suitable to be used inside callbacks |
| 501 |
or modules. |
| 502 |
|
| 503 |
That is, unless L<Coro> is used - with Coro, you can run multiple |
| 504 |
C<cmd_sync> methods concurrently form multiple threads, with no issues. |
| 505 |
|
| 506 |
=cut |
| 507 |
|
| 508 |
sub cmd_sync { |
| 509 |
push @_, my $cv = AE::cv; |
| 510 |
&cmd; |
| 511 |
|
| 512 |
my ($class, $results, $console) = $cv->recv; |
| 513 |
|
| 514 |
Carp::croak $results->{msg} |
| 515 |
if $class eq "error"; |
| 516 |
|
| 517 |
wantarray ? ($results, $console) : $results |
| 518 |
} |
| 519 |
|
| 520 |
sub event { |
| 521 |
my ($self, $event, @args) = @_; |
| 522 |
|
| 523 |
# if ($self->{verbose}) { |
| 524 |
# use Data::Dumper; |
| 525 |
# print Data::Dumper |
| 526 |
# ->new ([[$event, @args]]) |
| 527 |
# ->Pair ("=>") |
| 528 |
# ->Useqq (1) |
| 529 |
# ->Indent (0) |
| 530 |
# ->Terse (1) |
| 531 |
# ->Quotekeys (0) |
| 532 |
# ->Sortkeys (1) |
| 533 |
# ->Dump, |
| 534 |
# "\n"; |
| 535 |
# } |
| 536 |
|
| 537 |
my $cb; |
| 538 |
|
| 539 |
$cb = $self->can ("on_event") and $cb->($self, $event, @args); |
| 540 |
$cb = $self-> {on_event} and $cb->($self, $event, @args); |
| 541 |
$cb = $self->can ("on_$event") and $cb->($self, $event, @args); |
| 542 |
$cb = $self-> {"on_$event"} and $cb->($self, $event, @args); |
| 543 |
} |
| 544 |
|
| 545 |
# predefined events |
| 546 |
|
| 547 |
sub on_notify_thread_group_added { |
| 548 |
my ($self, undef, $r) = @_; |
| 549 |
|
| 550 |
$self->{thread_group}{$r->{id}} = $r; |
| 551 |
} |
| 552 |
|
| 553 |
sub on_notify_thread_group_removed { |
| 554 |
my ($self, undef, $r) = @_; |
| 555 |
|
| 556 |
delete $self->{thread_group}{$r->{id}}; |
| 557 |
} |
| 558 |
|
| 559 |
sub on_notify_thread_group_started { |
| 560 |
my ($self, undef, $r) = @_; |
| 561 |
|
| 562 |
delete $self->{thread_group}{exit_code}; |
| 563 |
$self->{thread_group}{$r->{id}}{pid} = $r->{pid}; |
| 564 |
} |
| 565 |
|
| 566 |
sub on_notify_thread_group_exited { |
| 567 |
my ($self, undef, $r) = @_; |
| 568 |
|
| 569 |
delete $self->{thread_group}{pid}; |
| 570 |
$self->{thread_group}{$r->{id}}{exit_code} = $r->{exit_code}; |
| 571 |
} |
| 572 |
|
| 573 |
sub on_notify_record_started { |
| 574 |
my ($self, undef, $r) = @_; |
| 575 |
|
| 576 |
$self->{thread_group}{$r->{id}}{recording} = 1; |
| 577 |
} |
| 578 |
|
| 579 |
sub on_notify_record_stopped { |
| 580 |
my ($self, undef, $r) = @_; |
| 581 |
|
| 582 |
$self->{thread_group}{$r->{id}}{recording} = 0; |
| 583 |
} |
| 584 |
|
| 585 |
sub on_notify_thread_created { |
| 586 |
my ($self, undef, $r) = @_; |
| 587 |
|
| 588 |
$self->{thread}{$r->{id}} = $r; |
| 589 |
} |
| 590 |
|
| 591 |
sub on_notify_thread_exited { |
| 592 |
my ($self, undef, $r) = @_; |
| 593 |
|
| 594 |
delete $self->{thread}{$r->{id}}; |
| 595 |
} |
| 596 |
|
| 597 |
sub _threads { |
| 598 |
my ($self, $id) = @_; |
| 599 |
|
| 600 |
ref $id |
| 601 |
? @{ $self->{thread} }{@$id} |
| 602 |
: $id eq "all" |
| 603 |
? values %{ $self->{thread} } |
| 604 |
: $self->{thread}{$id} |
| 605 |
} |
| 606 |
|
| 607 |
sub on_exec_running { |
| 608 |
my ($self, undef, $r) = @_; |
| 609 |
|
| 610 |
for ($self->_threads ($r->{thread_id})) { |
| 611 |
delete $_->{stopped}; |
| 612 |
$_->{running} = 1; |
| 613 |
} |
| 614 |
} |
| 615 |
|
| 616 |
sub on_exec_stopped { |
| 617 |
my ($self, undef, $r) = @_; |
| 618 |
|
| 619 |
for ($self->_threads ($r->{stopped_threads})) { |
| 620 |
delete $_->{running}; |
| 621 |
$_->{stopped} = $r; |
| 622 |
} |
| 623 |
|
| 624 |
# $self->event ("thread_$r->{reason}" => $r, [map $_->{id}, $self->_threads ($r)]); |
| 625 |
} |
| 626 |
|
| 627 |
sub _thread_groups { |
| 628 |
my ($self, $r) = @_; |
| 629 |
|
| 630 |
exists $r->{thread_group} |
| 631 |
? $self->{thread_group}{$r->{thread_group}} |
| 632 |
: values %{ $self->{thread_group} } |
| 633 |
} |
| 634 |
|
| 635 |
sub on_notify_library_loaded { |
| 636 |
my ($self, undef, $r) = @_; |
| 637 |
|
| 638 |
$_->{library}{$r->{id}} = $r |
| 639 |
for $self->_thread_groups ($r); |
| 640 |
} |
| 641 |
|
| 642 |
sub on_notify_library_unloaded { |
| 643 |
my ($self, undef, $r) = @_; |
| 644 |
|
| 645 |
delete $_->{library}{$r->{id}} |
| 646 |
for $self->_thread_groups ($r); |
| 647 |
} |
| 648 |
|
| 649 |
=back |
| 650 |
|
| 651 |
=head2 EVENTS |
| 652 |
|
| 653 |
AnyEvent::GDB is asynchronous in nature, as the goal of the MI interface |
| 654 |
is to be fully asynchronous. Due to this, a user of this interface must |
| 655 |
be prepared to handle various events. |
| 656 |
|
| 657 |
When an event is produced, the GDB object will look for the following four |
| 658 |
handlers and, if found, will call each one in order with the GDB object |
| 659 |
and event name (without C<on_>) as the first two arguments, followed by |
| 660 |
any event-specific arguments: |
| 661 |
|
| 662 |
=over 4 |
| 663 |
|
| 664 |
=item on_event method on the GDB object |
| 665 |
|
| 666 |
Useful when subclassing. |
| 667 |
|
| 668 |
=item on_event constructor parameter/object member |
| 669 |
|
| 670 |
The callback specified as C<on_event> parameter to the constructor. |
| 671 |
|
| 672 |
=item on_EVENTNAME method on the GDB object |
| 673 |
|
| 674 |
Again, mainly useful when subclassing. |
| 675 |
|
| 676 |
=item on_EVENTNAME constructor parameter/object member |
| 677 |
|
| 678 |
Any callback specified as C<on_EVENTNAME> parameter to the constructor. |
| 679 |
|
| 680 |
=back |
| 681 |
|
| 682 |
You can change callbacks dynamically by simply replacing the corresponding |
| 683 |
C<on_XXX> member in the C<$gdb> object: |
| 684 |
|
| 685 |
$gdb->{on_event} = sub { |
| 686 |
# new event handler |
| 687 |
}; |
| 688 |
|
| 689 |
Here's the list of events with a description of their arguments. |
| 690 |
|
| 691 |
=over 4 |
| 692 |
|
| 693 |
=item on_eof => $cb->($gdb, "eof") |
| 694 |
|
| 695 |
Called whenever GDB closes the connection. After this event, the object is |
| 696 |
partially destroyed and must not be accessed again. |
| 697 |
|
| 698 |
=item on_target => $cb->($gdb, "target", $string) |
| 699 |
|
| 700 |
Output received from the target. Normally, this is sent directly to STDOUT |
| 701 |
by GDB, but remote targets use this hook. |
| 702 |
|
| 703 |
=item on_log => $cb->($gdb, "log", $string) |
| 704 |
|
| 705 |
Log output from GDB. Best printed to STDOUT in interactive sessions. |
| 706 |
|
| 707 |
=item on_TYPE => $cb->($gdb, "TYPE", $class, $results) |
| 708 |
|
| 709 |
Called for GDB C<exec>, C<status> and C<notify> event (TYPE is one of |
| 710 |
these three strings). C<$class> is the class of the event, with C<-> |
| 711 |
replaced by C<_> everywhere. |
| 712 |
|
| 713 |
For each of these, the GDB object will create I<two> events: one for TYPE, |
| 714 |
and one for TYPE_CLASS. Usuaully you should provide the more specific |
| 715 |
event (TYPE_CLASS). |
| 716 |
|
| 717 |
=item on_TYPE_CLASS => $cb->($gdb, "TYPE_CLASS", $results) |
| 718 |
|
| 719 |
Called for GDB C<exec>, C<status> and C<notify> event: TYPE is one |
| 720 |
of these three strings, the class of the event (with C<-> replaced b |
| 721 |
C<_>s) is appended to it to form the TYPE_CLASS (e.g. C<exec_stopped> or |
| 722 |
C<notify_library_loaded>). |
| 723 |
|
| 724 |
=back |
| 725 |
|
| 726 |
=head2 STATUS STORAGE |
| 727 |
|
| 728 |
The default implementations of the event method store the thread, |
| 729 |
thread_group, recording, library and running status insid ethe C<$gdb> |
| 730 |
object. |
| 731 |
|
| 732 |
You can access these at any time. Specifically, the following information |
| 733 |
is available: |
| 734 |
|
| 735 |
=over 4 |
| 736 |
|
| 737 |
=item C<< $gdb->{thread_group}{I<id>} >> |
| 738 |
|
| 739 |
The C<thread_group> member stores a hash for each existing thread |
| 740 |
group. The hash always contains the C<id> member, but might also contain |
| 741 |
other members. |
| 742 |
|
| 743 |
=item C<< $gdb->{thread_group}{I<id>}{pid} >> |
| 744 |
|
| 745 |
The C<pid> member only exists while the thread group is running a program, |
| 746 |
and contaisn the PID of the program. |
| 747 |
|
| 748 |
=item C<< $gdb->{thread_group}{I<id>}{exit_code} >> |
| 749 |
|
| 750 |
The C<exit_code> member only exists after a program has finished |
| 751 |
executing, and before it is started again, and contains the exit code of |
| 752 |
the program. |
| 753 |
|
| 754 |
=item C<< $gdb->{thread_group}{I<id>}{recording} >> |
| 755 |
|
| 756 |
The C<recording> member only exists if recording has been previously |
| 757 |
started, and is C<1> if recoridng is currently active, and C<0> if it has |
| 758 |
been stopped again. |
| 759 |
|
| 760 |
=item C<< $gdb->{thread}{I<id>} >> |
| 761 |
|
| 762 |
The C<thread> member stores a hash for each existing thread. The hash |
| 763 |
always contains the C<id> member with the thread id, and the C<group_id> |
| 764 |
member with the corresponding thread group id. |
| 765 |
|
| 766 |
=item C<< $gdb->{thread}{I<id>}{running} >> |
| 767 |
|
| 768 |
The C<running> member is C<1> while the thread is, well, running, and is |
| 769 |
missing otherwise. |
| 770 |
|
| 771 |
=item C<< $gdb->{thread}{I<id>}{stopped} >> |
| 772 |
|
| 773 |
The C<stopped> member contains the result list from the C<on_exec_stopped> |
| 774 |
notification that caused the thread to stop, and only exists when the |
| 775 |
thread is topped. |
| 776 |
|
| 777 |
=item C<< $gdb->{library}{I<id>} >> |
| 778 |
|
| 779 |
The C<library> member contains all results from the C<on_library_loaded> |
| 780 |
event (such as C<id>, C<target_name>, C<host_name> and potentially a |
| 781 |
C<thread_group>. |
| 782 |
|
| 783 |
=back |
| 784 |
|
| 785 |
=head1 SEE ALSO |
| 786 |
|
| 787 |
L<AnyEvent>, L<http://sourceware.org/gdb/current/onlinedocs/gdb/GDB_002fMI.html#GDB_002fMI>. |
| 788 |
|
| 789 |
=head1 AUTHOR |
| 790 |
|
| 791 |
Marc Lehmann <schmorp@schmorp.de> |
| 792 |
http://home.schmorp.de/ |
| 793 |
|
| 794 |
=cut |
| 795 |
|
| 796 |
1 |