http://www.perlmonks.org?node_id=694095
Category:
Author/Contact Info
Description:

Recipe

  • Attach gdb to the process

    gdb -p YOUR-PID

  • Advance perl to a point between opcodes. This step may be optional but is a good idea. Omitting this step risks corrupting something. This is semi-equivalent to an "unsafe perl signal."

    set variable PL_sig_pending = 1
    b Perl_despatch_signals
    c
    
  • Run arbitrary perl. This returns a number which is a (SV*). You'll use this pointer in the next step. See How to dump perl data structures from gdb additionally when on threaded perl builds.

    call Perl_eval_pv("...", 0)

  • Deallocate the scalar returned by the previous eval. This is causing issues for me now. I don't know why. Use the number returned by Perl_eval_pv

    call Perl_sv_free(...)

Complete example

The following is a complete example showing a sample program being launched and gdb loading Carp into the program during its execution and calling the perl function cluck(). Carp::cluck is a convenient function to log the current perl stack to STDERR.

[server ~]$ perl -e 'while(1){foo()};sub foo { sleep 1 }' &
[1] 22480
[server ~]$ ps x
  PID TTY      STAT   TIME COMMAND
10351 ?        S      0:01 sshd: diotalevi@pts/77
10352 pts/77   Ss+    0:00 -bash
22311 ?        S      0:00 sshd: diotalevi@pts/37
22312 pts/37   Ss     0:00 -bash
22354 pts/37   S+     0:00 screen -x -R
22356 pts/41   Ss     0:00 -/bin/bash
22480 pts/41   S+     0:00 perl -e while(1){foo()};sub foo { sleep 1 }
22418 pts/51   Ss     0:00 -/bin/bash
22479 pts/51   R+     0:00 ps x
[diotalevi ~]$ gdb -p 22480
GNU gdb Red Hat Linux (6.3.0.0-1.132.EL3rh)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and y
+ou are
welcome to change it and/or distribute copies of it under certain cond
+itions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for det
+ails.
This GDB was configured as "i386-redhat-linux-gnu".
Attaching to process 22480

warning: The current VSYSCALL page code requires an existing execuitab
+le. 
Use "add-symbol-file-from-memory" to load the VSYSCALL page by hand
Reading symbols from /usr/local/bin/perl...(no debugging symbols found
+)...done.
Using host libthread_db library "/lib/tls/libthread_db.so.1".
Reading symbols from /lib/libnsl.so.1...(no debugging symbols found)..
+.done.
Loaded symbols for /lib/libnsl.so.1
Reading symbols from /lib/libdl.so.2...(no debugging symbols found)...
+done.
Loaded symbols for /lib/libdl.so.2
Reading symbols from /lib/tls/libm.so.6...(no debugging symbols found)
+...done.
Loaded symbols for /lib/tls/libm.so.6
Reading symbols from /lib/libcrypt.so.1...(no debugging symbols found)
+...done.
Loaded symbols for /lib/libcrypt.so.1
Reading symbols from /lib/libutil.so.1...
(no debugging symbols found)...done.
Loaded symbols for /lib/libutil.so.1
Reading symbols from /lib/tls/libc.so.6...(no debugging symbols found)
+...done.
Loaded symbols for /lib/tls/libc.so.6
Reading symbols from /lib/ld-linux.so.2...(no debugging symbols found)
+...done.
Loaded symbols for /lib/ld-linux.so.2
0xb7eebacb in __nanosleep_nocancel () from /lib/tls/libc.so.6
(gdb) set variable PL_sig_pending = 1
(gdb) b Perl_despatch_signals
Breakpoint 1 at 0x809ed59
(gdb) c
Continuing.

Breakpoint 1, 0x0809ed59 in Perl_despatch_signals ()
(gdb) delete breakpoints
Delete all breakpoints? (y or n) y
(gdb) call Perl_eval_pv("use Carp; Carp::cluck('Hello world')",0)
Hello world at (eval 1) line 1
        eval 'use Carp; Carp::cluck(\'Hello world\')
;' called at -e line 1
        main::foo() called at -e line 1
$1 = 135614820
(gdb) call Perl_sv_free(135614820)
$2 = 135389568