Mj1234 has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

I am fairly new to the Perl threads and I got a perl script to debug an exe using GDB and GDBServer. The code snippet is attached below.

sub reset { my $self_ph = shift; $self_ph->kill(); $self_ph->{GDBSERVERTHREAD} = threads::async { `$self_ph->{GDBSERVER} :6000 MyProgram.exe 1> NUL 2> NUL`; threads->exit(); }; Time::HiRes::sleep(0.05); $self_ph->{IN} = new FileHandle; $self_ph->{OUT} = new FileHandle; $self_ph->{PID} = IPC::Open2::open2($self_ph->{IN},$self_ph->{OUT} +, $self_ph->{GDB} . " --silent --interpreter=mi " . $self_ph->{EXE} . " -ex \"set target-async off\" -ex \"target remote :6000\" +" ); $self_ph->{IN_QUEUE} = new Thread::Queue; Time::HiRes::sleep(0.05); $self_ph->{READER} = threads::async { $SIG{'KILL'} = sub { threads->exit(); }; while ( my $content_s = readline( $self_ph->{IN} ) ) { $self_ph->{IN_QUEUE}->enqueue($content_s); } }; $self_ph->{IN}->autoflush(1); $self_ph->{OUT}->autoflush(1); return (1); } sub kill { my $self_ph = shift; if ( defined( $self_ph->{READER} ) ) { $self_ph->{READER}->kill('KILL')->detach(); $self_ph->{READER} = undef; } if ( defined( $self_ph->{GDBSERVERTHREAD} ) ) { `taskkill /F /IM MyProgram.exe`; $self_ph->{GDBSERVERTHREAD}->join(); $self_ph->{GDBSERVERTHREAD} = undef; } if ( 0 != $self_ph->{PID} ) { `taskkill /F /IM gdb.exe`; $self_ph->{PID} = 0; } return (1); }

The reset module starts the gdb.exe and gdbserver.exe and MyProgram.exe. The problem arises with the kill module. When the control reaches the join of the GDBServer the control does not return at all.

I tried detach instead of join but there are warnings and exceptions. Can someone help as to what is wrong in the script or what should be changed so that at the end of kill all the threads are exited correctly and my script does not get terminated?

Replies are listed 'Best First'.
Re: Debugging an exe with GDB using PERL
by Corion (Patriarch) on Dec 14, 2016 at 11:00 UTC

    This looks very much like a continuation of join() in perl threads does not return.

    I don't see how the discussion there entered this code.

    In your subroutine kill, you wait for gdb.exe to end before trying to taskkill it. I think that is the wrong way around. If gdb.exe waits for some input, either give it the input or start it in a way that it doesn't expect user input in any way. Personally, I would redirect all its output to logfiles instead of NUL: to see where it hangs maybe.

    Also, if you're just trying to run some commands asynchronously on Windows, consider using the system(1, ...) form of system (see perlport), as I recommended in my last reply to your questions. But maybe you have already done so and can tell us how that failed to reach your goal(s).