Skip to content

Instantly share code, notes, and snippets.

@cjsmeele
Created July 24, 2017 12:39
Show Gist options
  • Save cjsmeele/b9e7541791172042bb34fff79cf5c77d to your computer and use it in GitHub Desktop.
Save cjsmeele/b9e7541791172042bb34fff79cf5c77d to your computer and use it in GitHub Desktop.
Attach GDB to a spawned program matching a certain name within ~100ms
#!/usr/bin/env perl
use 5.12.0;
use warnings;
my $pname = $ARGV[0] // die "usage: $0 NAME\n";
for (my $i = 0;; ++$i) {
state @pids;
chomp(my @new_pids = `pgrep $pname`);
if ($i and @new_pids > @pids) {
my ($new) = grep { my $a = $_; not grep {$_ == $a} @pids } @new_pids;
say "$new spawned! attaching gdb...\n";
exec "gdb", "attach", $new;
}
@pids = @new_pids;
select undef, undef, undef, 0.1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment