#!/usr/bin/gawk -f
#open process' memory segments in radare2 w/o debugging
#usage: ./r2mem.awk /proc/<pid>/mem | xclip
# or, in r2 /proc/<pid>/mem
# .! ./r2mem.awk
#todo: heap only mode, segment omission
function r2p(cmd) \
{
printf "%s"r2pipe_postfix, cmd > r2pipe_outfile;
}
BEGIN \
{
r2pipe_out = ENVIRON["R2PIPE_OUT"];
if (r2pipe_out)
{
r2pipe_outfule = "/dev/fd/" r2pipe_out;
r2pipe_postfix = "\0";
}
else
{
r2pipe_outfile = "/dev/stdout";
r2pipe_postfix = "\n";
}
r2pipe_outfile = r2pipe_out ? ("/dev/fd/" r2pipe_out) : "/dev/stdout";
r2p("om-*");
if (!ARGV[1])
{
ARGV[1] = gensub("/mem$", "/maps", "g", ENVIRON["R2_FILE"]);
ARGC = 2;
}
HEAPONLY=ENVIRON["HEAPONLY"];
}
(NR==1) \
{
ofile = gensub("/maps$", "/mem", "g", FILENAME);
if (!ENVIRON["R2_FILE"]) r2p("o "ofile);
}
match($0, "^([0-9a-f]*)-([0-9a-f]*)", a) \
{
start = a[1];
end=a[2];
size = strtonum("0x"end) - strtonum("0x"start);
sizes="0x"sprintf("%x", size);
filename=$6;
out = ! HEAPONLY || ($6 == "" || $6 ~ /\[.*\]/) ;
if (out) r2p("om 3 0x"start" "sizes" 0x"start" rw \""filename"\"" )
}