I'm trying to read a child process's stack but with no luck.
I know it is possible using ptrace
, but ptrace
's interface allows you to read only one word at a time, and I'm trying to scan a larger portions of the stack.
I've also tried reading the /proc/$pid/mem
from the boundries of the stack as extracted from the /proc/$pid/maps
file after first using ptrace to attach to it
(as suggested here) but the read keeps failing (even when running as root) although the same code succeeds when tried reading from different parts of the process (e.g. heap).
What am I doing wrong? Is there any other option?
waitpid
betweenptrace(PTRACE_ATTACH,…)
andread
(otherwise there's a possible race condition)? What error doesread
return? Is the child doing anything peculiar with its memory mapping — can you try your code with a simple child likesleep
? – Gilles 'SO- stop being evil' Feb 20 '11 at 19:11perl -e '$p=shift;open MAPS, "/proc/$p/maps";($m)=grep /\[stack\]/, <MAPS>;($a,$b)=map hex, $m =~ /[\da-f]+/g;open MEM, "/proc/$p/mem" or die "open mem: $!";seek MEM,$a,0 or die "seek: $!";read MEM, $c,$b-$a or die "read";print $c' "$$" | hd
works for me. What do you get when you run it? – Stéphane Chazelas Jul 27 '16 at 09:33