Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I attach a debugger to a running Perl process?

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 gdb -p, but gdb does not help me. I’ve tried Enbugger, but failed:

$ perl -e 'while (1) {}'& [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->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)  

Am I doing it wrong? Are there other options?


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:

use Enbugger::OnError 'USR1'; 

Then you can simply kill -USR1 pid and your process will jump into the debugger.

like image 467
zoul Avatar asked Jan 12 '12 14:01

zoul


People also ask

Does Perl have a debugger?

In Perl, the debugger is not a separate program the way it usually is in the typical compiled environment. Instead, the -d flag tells the compiler to insert source information into the parse trees it's about to hand off to the interpreter.

How do I create a breakpoint in a Perl script?

b-command is used to set a breakpoint in a program. Whenever a specified line is about to be executed, this command tells the debugger to halt the program. Note : There can be any number of breakpoints in a program.


1 Answers

First, please use a DEBUGGING perl, if you want to inspect it with gdb.

Please define "stuck". Busy or non-busy waiting (high or low CPU), eating memory or not? With while 1 it is busy waiting. I usually get busy waiting (endless cycles) on HV corruption in Perl_hfree_next_entry() since 5.15. Non-busy waiting is usually waiting on a blocking IO read.

I get the correct:

`0x00007fba15ab35c1 in Perl_runops_debug () at dump.c:2266` `2266       } while ((PL_op = PL_op->op_ppaddr(aTHX)));` 

and can inspect everything, much more than with a simple perl debugger. With a non-threaded perl you have to type less.

`(gdb) p Perl_op_dump(PL_op)` 

and so on.

If you have to do with perl: Inside the pp_stub function it is not a good idea to enter the Enbugger runloop, you should be in the main runloop in dump.c. Set a breakpoint to the shown line.

"error for object 0x3" on eval sound like internal corruption in the context, so you should look at the cx and stack pointers. Probably because you started it in a bad context.

like image 55
rurban Avatar answered Nov 08 '22 13:11

rurban