These recent blog posts are veering in the “here’s a horrible thing I just did!” direction. No apologies.
Recently, I was working on a weird problem where I wanted to snapshot /proc/$pid/maps
before and after a couple of mmap and madvise calls. But I didn’t particularly want to write C++ code to do it. So:
JS_LOG_FMT(debug, Info, "About to mmap at {:x}", ptr);
JS_LOG_FMT(debug, Info, "SKYNET: mkdir /tmp/js-{0}/before /tmp/js-{0}/after", getpid());
JS_LOG_FMT(debug, Info, "SKYNET: cp /proc/{0}/maps /tmp/js-{0}/before", getpid());
sleep(3);
mmap(...);
JS_LOG_FMT(debug, Info, "SKYNET: cp /proc/{0}/maps /tmp/js-{0}/after", getpid());
sleep(3);
That produces “SKYNET: …” log messages, with a pause. If only someone were reading those log messages and quickly cutting & pasting the commands… let’s give them 3 seconds to do each.
Then I run in my terminal:
MOZ_LOG=debug:5 $JS blahblah.js |& perl -lpe 'system($1) if /SKYNET: (.*)/'
Whenever one of these messages is produced, the Perl script grabs it and runs it in a new shell. Victory!
Note: the Skynet reference is from way back when we all watched Terminator and thought that it would be possible to prevent the AIs from taking over the world just by keeping one company from giving it so much power, in the form of tools and permission to do arbitrary things. We didn’t predict that in only a few decades, thousands of people would be doing exactly that on a daily basis.
Tags: automation, mozilla, planet