#!/usr/bin/env ruby # usage: redirect-process pid output require 'tempfile' pid = ARGV.shift.to_i output = ARGV.shift f = open(output, 'w') t = Tempfile.new("redirect-process.gdb") t.puts <<"End" attach #{pid} print dup2(open("/proc/#{$$}/fd/#{f.fileno}", #{File::WRONLY}), #{STDOUT.fileno}) End t.close # shut up gdb. STDOUT.reopen("/dev/null") STDERR.reopen("/dev/null") system("gdb", "-batch", "-n", "-x", t.path)