Depuración con netcat

Ejemplo Simple

Se ejecuta el programa con la opción RemotePort :

35  # ####################################################################################
36  # lhp@nereida:~/Lperl/src/properldebugging/Chapter09$ \
        PERLDB_OPTS="RemotePort=nereida:12345" perl -d hello.pl
37  # 4 5
38  #
En el otro lado se pone un servidor a esperar con netcat :
 9  ####################################################################################
10  # lhp@nereida:~/Lperl/src/properldebugging/Chapter09$ nc  -v -l nereida -p 12345
11  # listening on [any] 12345 ...
12  # connect to [193.145.105.252] from nereida [193.145.105.252] 51615
13  #
14  # Loading DB routines from perl5db.pl version 1.28
15  # Editor support available.
16  #
17  # Enter h or `h h' for help, or `man perldebug' for more help.
18  #
19  # main::(hello.pl:4):     my $a = 4;
20  #   DB<1> n
21  # main::(hello.pl:5):     my $b = 5;
22  #   DB<1> p $a
23  # 4
24  #   DB<2> n
25  # main::(hello.pl:6):     print "$a $b\n";
26  #   DB<2> p $b
27  # 5
28  #   DB<3> n
29  # Debugged program terminated.  Use q to quit or R to restart,
30  #   use o inhibit_exit to avoid stopping after program termination,
31  #   h q, h R or h o to get additional info.
32  #   DB<3>
33
34
lhp@nereida:~/Lperl/src/properldebugging/Chapter09$

El programa:

lhp@nereida:~/Lperl/src/properldebugging/Chapter09$ cat -n hello.pl
 1  #!/usr/bin/perl -w
 2  # Example of remote debugging
 3  use strict;
 4
 5  my $a = 4;
 6  my $b = 5;
 7  print "$a $b\n";
 8

IPC::PerlSSH

lhp@nereida:/tmp$ cat -n ipcssh2.pl
     1  #!/usr/local/bin/perl -w
     2  use strict;
     3  use IPC::PerlSSH;
     4
     5  my $ips = IPC::PerlSSH->new(
     6    Command => q{ssh nereida 'PERLDB_OPTS="RemotePort=nereida:12345" perl -d'},
     7  );
     8
     9  $ips->eval( "use POSIX qw( uname )" );
    10  my @remote_uname = $ips->eval( "uname()" );
    11
    12  print "@remote_uname\n";

lhp@nereida:/tmp$ ipcssh2.pl
@Linux nereida.deioc.ull.es 2.6.18-5-686 #1 SMP Mon Dec 24 16:41:07 UTC 2007 i686

lhp@nereida:/tmp$ nc  -v -l nereida -p 12345  
listening on [any] 12345 ...
connect to [193.145.105.252] from nereida [193.145.105.252] 48416

Loading DB routines from perl5db.pl version 1.28
Editor support available.

Enter h or `h h' for help, or `man perldebug' for more help.

main::(-:58):   $| = 1;
  DB<1> n
main::(-:60):   my %stored_procedures;
  DB<1>
main::(-:70):   };
  DB<1>
main::(-:74):   };
  DB<1>
main::(-:76):   while( 1 ) {
  DB<1>
main::(-:77):      my ( $operation, @args ) = read_operation( $readfunc );
  DB<1>
main::(-:79):      if( $operation eq "QUIT" ) {
  DB<1>
main::(-:84):      if( $operation eq "EVAL" ) {
  DB<1> x $operation
0  'EVAL'
  DB<2> n
main::(-:85):         my $code = shift @args;
  DB<2> c
Debugged program terminated.  Use q to quit or R to restart,
  use o inhibit_exit to avoid stopping after program termination,
  h q, h R or h o to get additional info.
  DB<2> q

Depuración de Programas GRID::Machine con la Opción debug

El programa:

pp2@nereida:~/LGRID_Machine/examples$ cat -n debug1.pl
 1  #!/usr/local/bin/perl -w
 2  use strict;
 3  use GRID::Machine;
 4
 5  my $hostport = shift || $ENV{GRID_REMOTE_MACHINE} || usage();
 6  $hostport =~ m{^([\w.]+)(?::(\d+))?$} or usage();
 7  my $host = $1;
 8  my $port = $2;
 9
10  my $machine = GRID::Machine->new(
11     host => $host,
12     debug => $port,
13     includes => [ qw{SomeFunc} ],
14  );
15
16  my $r =  $machine->eval(q{
17    use vars qw{$a $b $c};
18
19    $a = 4;
20    $b = 2*$a;
21    $c = $a+$b;
22    gprint "machine: ".hostname().": Inside first eval a = $a, b = $b, c = $c\n";
23  });
24
25  $r = $machine->max( 1, 4, 9, 12, 3, 7);
26  show($r);
27
28  $r =  $machine->eval(q{
29    gprint "machine: ".hostname().": Inside second eval\n";
30    gprint "$a $b $c\n";
31    $a+$b+$c;
32  });
33
34  show($r);
35
36  sub usage {
37    warn "Usage:\n$0 host[:port]\n";
38    exit(1);
39  }
40
41  sub show {
42    my $r = shift;
43
44    print $r."result = ".$r->result."\n"
45  }
El fichero someFunc.pm:

pp2@nereida:~/LGRID_Machine/examples$ cat -n SomeFunc.pm
     1  use List::Util qw{max};
     2  use Sys::Hostname;
     3
     4  sub max {
     5    gprint "machine: ".hostname().": Inside sub two(@_)\n";
     6    List::Util::max(@_)
     7  }

Ejecutar nc primero:

casiano@beowulf:~$ netcat -v -l -p 54321
listening on [any] 54321 ...
Ejecutamos el programa:
pp2@nereida:~/LGRID_Machine/examples$ debug1.pl beowulf:54321
Debugging with 'ssh beowulf PERLDB_OPTS="RemotePort=beowulf:54321" perl -d'
Remember to run 'netcat -v -l -p 54321' in beowulf
Ahora tendremos el prompt del depurador en la terminal en la que ejecutamos netcat:
casiano@beowulf:~$ netcat -v -l -p 54321
listening on [any] 54321 ...
connect to [193.145.102.240] from beowulf.pcg.ull.es [193.145.102.240] 60473

Loading DB routines from perl5db.pl version 1.28
Editor support available.

Enter h or `h h' for help, or `man perldebug' for more help.

GRID::Machine::MakeAccessors::(/home/pp2/LGRID_Machine/lib/GRID/Machine/MakeAccessors.pm:33):
33:     1;
  DB<1>

El fichero .perldb permite adaptar la conducta del depurador:

casiano@beowulf:~$ cat -n .perldb
 1  #sub ::beval {
 2  #  print DB::OUT 'b GRID::Machine::EVAL $_[1] =~ m{'.$_[0].'}'
 3  #}
 4
 5  sub ::get_stdout_name {
 6    my $a = `ls rperl*$$*.log`;
 7    chomp($a);
 8    $a;
 9  }
10
11  sub ::sys {
12    my $command = shift;
13
14    $var = `$command`;
15    print DB::OUT $var;
16  }
17
18  #our %alias;
19  $alias{beval} = 's/beval\s+(.+)/b GRID::Machine::EVAL \$_[1] =~ m{$1}/';
20  $alias{bsub}  = 's/bsub\s+(.+)/b GRID::Machine::CALL \$_[1] =~ m{$1}/';
21  $alias{fg}  = 's/fg\s+(\d+)/f eval $1/';
22  $alias{whatf} = 's/whatf\s+(.+)/x SERVER->{stored_procedures}{$1}/';
23  $alias{stdout} = q{s/stdout/::get_stdout_name()/e};
24  $alias{cgs} = 's/cgs\s+(.+)/c GRID::Machine::$1/';
25  $alias{'!!'} = 's/!!\s*(.+)/p `$1`/';
26  $alias{main} = 's/main/c GRID::Machine::main/';
27  $alias{new} = 's/new/c GRID::Machine::new/';

Depurando GRID::Machine en Máquina Remota

pp2@nereida:~/LGRID_Machine/examples$ cat -n netcat3.pl
     1  #!/usr/local/bin/perl -w
     2  use strict;
     3  use GRID::Machine;
     4
     5  my $machine = GRID::Machine->new(
     6     command => q{ssh beowulf 'PERLDB_OPTS="RemotePort=beowulf:12345" perl -d'},
     7     #command => q{ssh nereida perl},
     8  );
     9
    10  print $machine->eval(q{
    11    system('ls');
    12    print %ENV,"\n";
    13  });
    14
    15  __END__

En la máquina remota ejecutamos primero netcat

casiano@beowulf:~$ nc  -v -l beowulf -p 12345
listening on [any] 12345 ...
Ahora arrancamos el programa en la terminal que tenemos en nereida:

pp2@nereida:~/LGRID_Machine/examples$ netcat3.pl

En la terminal de netcat nos aparece el depurador:

connect to [193.145.102.240] from beowulf.pcg.ull.es [193.145.102.240] 48308

Loading DB routines from perl5db.pl version 1.28
Editor support available.

Enter h or `h h' for help, or `man perldebug' for more help.

GRID::Machine::MakeAccessors::(/usr/local/share/perl/5.8.8/GRID/Machine/MakeAccessors.pm:33):
33:     1;
  DB<1> n
GRID::Machine::(/usr/local/share/perl/5.8.8/GRID/Machine/Message.pm:74):
74:     1;
  DB<1> c GRID::Machine::main
GRID::Machine::main(/usr/local/share/perl/5.8.8/GRID/Machine/REMOTE.pm:508):
508:      my $server = shift;
  DB<2> n
GRID::Machine::main(/usr/local/share/perl/5.8.8/GRID/Machine/REMOTE.pm:514):
514:      package main;
  DB<2>
GRID::Machine::main(/usr/local/share/perl/5.8.8/GRID/Machine/REMOTE.pm:515):
515:      while( 1 ) {
  DB<2>
GRID::Machine::main(/usr/local/share/perl/5.8.8/GRID/Machine/REMOTE.pm:516):
516:         my ( $operation, @args ) = $server->read_operation( );
  DB<2>
GRID::Machine::main(/usr/local/share/perl/5.8.8/GRID/Machine/REMOTE.pm:518):
518:         if ($server->can($operation)) {
  DB<2> x ( $operation, @args )
0  'GRID::Machine::EVAL'
1  'use strict;'
2  ARRAY(0x86820fc)
     empty array
  DB<3> c
Debugged program terminated.  Use q to quit or R to restart,
  use o inhibit_exit to avoid stopping after program termination,
  h q, h R or h o to get additional info.
  DB<3> q
casiano@beowulf:~$

Y la salida ocurre en la terminal de netcat:

Algorithm-Knap01DP-0.25
Algorithm-Knap01DP-0.25.tar
..................
HOME/home/casianoSSH_CLIENT193.145.105.252 42097....



Subsecciones
Casiano Rodríguez León
Licencia de Creative Commons
Programación Distribuida y Mejora del Rendimiento
por Casiano Rodríguez León is licensed under a Creative Commons Reconocimiento 3.0 Unported License.

Permissions beyond the scope of this license may be available at http://campusvirtual.ull.es/ocw/course/view.php?id=44.
2012-06-19