111e5515bSMark Nunberg#!/usr/bin/env perl
211e5515bSMark Nunberguse strict;
311e5515bSMark Nunberguse warnings;
411e5515bSMark Nunberguse Digest::MD5 qw(md5_hex);
511e5515bSMark Nunberg
69efaeb9eSSergey Avseyevmy $HDR = "** $0 ($$):";
711e5515bSMark Nunberg$\="\n";
811e5515bSMark Nunberg
911e5515bSMark Nunbergmy $DT_SRC = shift @ARGV;
1011e5515bSMark Nunbergmy $CMD = shift @ARGV;
1111e5515bSMark Nunberg
1211e5515bSMark Nunbergmy @O_FILES = grep { $_ =~ /\.o$/ } @ARGV;
1311e5515bSMark Nunbergif (!scalar @O_FILES) {
1411e5515bSMark Nunberg    # Assume this isn't an actual link command?
1511e5515bSMark Nunberg    print"$HDR Assuming this isn't an LD/AR invocation. Continuing..";
1611e5515bSMark Nunberg    exec($CMD,@ARGV);
1711e5515bSMark Nunberg}
1811e5515bSMark Nunberg
199efaeb9eSSergey Avseyev# Copy .o files to a temporary location before DTrace messes with them
209efaeb9eSSergey Avseyevchomp(my $tmpdir = `mktemp -d -t $$`);
219efaeb9eSSergey Avseyevif (system("tar cf - @O_FILES | tar xf - -C $tmpdir") != 0) {
229efaeb9eSSergey Avseyev    system("rm -r $tmpdir");
239efaeb9eSSergey Avseyev    exit(1);
249efaeb9eSSergey Avseyev}
259efaeb9eSSergey Avseyev
2611e5515bSMark Nunbergmy $ss = join('_', @O_FILES);
2711e5515bSMark Nunbergmy $hexstr = md5_hex($ss);
2811e5515bSMark Nunberg
299efaeb9eSSergey Avseyev# From now, we work with files in the temporary location, update @ARGV
309efaeb9eSSergey Avseyevmap { $_ =~ s,.+\.o$,$tmpdir/$&, } @ARGV;
319efaeb9eSSergey Avseyev
329efaeb9eSSergey Avseyevmy $INSTRUMENTED = "generated/probes_${hexstr}_$$.o";
3311e5515bSMark Nunberg# Run DTrace instrumentation. Assuming running from build directory:
3411e5515bSMark Nunbergmy @args = (
3511e5515bSMark Nunberg    'dtrace', '-C', '-G',
3611e5515bSMark Nunberg    '-s', $DT_SRC,
3711e5515bSMark Nunberg    '-o', $INSTRUMENTED,
389efaeb9eSSergey Avseyev    grep { $_ =~ /\.o$/ } @ARGV);
3911e5515bSMark Nunberg
4011e5515bSMark Nunbergprint "$HDR: Creating instrumented DTrace object: @args";
4111e5515bSMark Nunbergif (system(@args) != 0) {
429efaeb9eSSergey Avseyev    system("rm -r $tmpdir");
4311e5515bSMark Nunberg    exit(1);
4411e5515bSMark Nunberg}
4511e5515bSMark Nunberg
4611e5515bSMark Nunbergunshift @ARGV, $CMD;
4711e5515bSMark Nunbergpush @ARGV, $INSTRUMENTED;
4811e5515bSMark Nunbergprint "$HDR: Linking with instrumented DTrace object: @ARGV";
499efaeb9eSSergey Avseyevmy $rc = system(@ARGV);
509efaeb9eSSergey Avseyevsystem("rm -r $tmpdir");
519efaeb9eSSergey Avseyevexit($rc);