File: ebug_client

package info (click to toggle)
libdevel-ebug-perl 0.48-3
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 332 kB
  • ctags: 108
  • sloc: perl: 1,933; makefile: 6
file content (122 lines) | stat: -rw-r--r-- 2,745 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/perl

=head1 NAME

ebug-client - Debugger client for remote debugging Perl script

=head1 SYNOPSIS

ebug-client -key <secret> -port <port>

=head1 DESCRIPTION

ebug-client is an debugger front end for attaching to
remotely executing perl script, running on ebug-server.

To invoke ebug-client, you must first invoke ebug-server to
run debuggee and generate "key" and "port" needed for connection.
When ebug-server is invoked, it will show exact command-line to
invoke ebug-client.

=head1 EXAMPLE

 # Attach to ebug-server running on port 4023, with key "begripra"
 $ ebug-client -key begripra -port 4023

=head1 NOTE

Currently, you cannot "restart" debuggee. You'll need to
restart one manually by re-invoking both ebug-server and
ebug-client.

Since Devel::ebug::Backend listens to "localhost" socket,
you will need to relay I/O using tool like socat for true
remote debugging.

Internally, thanks for it being free software, it is almost
a complete ripoff of Devel::ebug source.

=head1 SEE ALSO

L<Devel::debug>, L<ebug>, L<ebug-server>

=head1 AUTHOR

Taisuke Yamada, <tyamadajp!list!rakugaki!org>

=cut

package Devel::ebug;

use Devel::ebug;
use IO::Socket::INET;
use strict;
use warnings;

our $VERSION = $Devel::ebug::VERSION;
our $ARG     = {};

*load = *attach;

sub attach {
    my $self    = shift;
    my $program = $self->program;

    # just to see if my "load" is in control instead of Devel::ebug::load
    print STDERR "Attaching to $program...\n";

    # import all the plugins into our namespace
    do { eval "use $_ " }
        for $self->plugins;

    # try and connect to the server
    my $socket;
    foreach ( 1 .. 10 ) {
        $socket = IO::Socket::INET->new(
            PeerAddr   => "localhost",
            PeerPort   => $ARG->{port},
            Proto      => 'tcp',
            Reuse      => 1,
            ReuserAddr => 1,
        );
        last if $socket;
        sleep 1;
    }
    die "Could not connect: $!" unless $socket;
    $self->socket($socket);

    my $response = $self->talk(
        {   command => "ping",
            version => $VERSION,
            secret  => $ARG->{key},
        }
    );
    my $version = $response->{version};
    die "Client version $version != our version $VERSION"
        unless $version eq $VERSION;

    $self->basic;    # get basic information for the first line
}

package main;

use Devel::ebug::Console;
use Getopt::Long;
use strict;
use warnings;

GetOptions( my $OPT = {}, 'help', 'port=i', 'key=s' ) || help();
help() if $OPT->{help};

$Devel::ebug::ARG = $OPT;
my $console = Devel::ebug::Console->new();
$console->run();
exit(0);

sub help {
    print STDERR <<EOF;
Usage: $0 -key <secret> -port <port> -- debuggee args...
EOF
    exit(1);
}