title: How can I attach a debugger to a running Perl process?
body: <p>I have a running Perl process that’s stuck, I’d like to poke inside with a debugger to see what’s wrong. I can’t restart the process. Can I attach the debugger to the running process? I know I can do <code>gdb -p</code>, but <code>gdb</code> does not help me. I’ve tried <a href="http://search.cpan.org/perldoc?Enbugger">Enbugger</a>, but failed:</p>
  
  <pre><code>$ perl -e 'while (1) {}'&amp;
  [1] 86836
  $ gdb -p 86836
  …
  Attaching to process 86836.
  Reading symbols for shared libraries . done
  Reading symbols for shared libraries ............................. done
  Reading symbols for shared libraries + done
  0x000000010c1694c6 in Perl_pp_stub ()
  (gdb) call (void*)Perl_eval_pv("require Enbugger;Enbugger-&gt;stop;",0)
  perl(86836) malloc: *** error for object 0x3: pointer being realloc'd was not allocated
  *** set a breakpoint in malloc_error_break to debug
  
  Program received signal SIGABRT, Aborted.
  0x00007fff8269d82a in __kill ()
  The program being debugged was signaled while in a function called from GDB.
  GDB remains in the frame where the signal was received.
  To change this behavior use "set unwindonsignal on"
  Evaluation of the expression containing the function (Perl_eval_pv) will be abandoned.
  (gdb) 
  </code></pre>
  
  <p>Am I doing it wrong? Are there other options?</p>
  
  <hr>
  
  <p>P.S. If you think you could benefit from a debugger attached to a running process yourself, you can insert a debugger back door triggered by SIGUSR1:</p>
  
  <pre><code>use Enbugger::OnError 'USR1';
  </code></pre>
  
  <p>Then you can simply <code>kill -USR1 pid</code> and your process will jump into the debugger.</p>