1#!/usr/bin/env perl
2use strict;
3use warnings;
4use Digest::MD5 qw(md5_hex);
5
6my $HDR = "** $0 ($$):";
7$\="\n";
8
9my $DT_SRC = shift @ARGV;
10my $CMD = shift @ARGV;
11
12my @O_FILES = grep { $_ =~ /\.o$/ } @ARGV;
13if (!scalar @O_FILES) {
14    # Assume this isn't an actual link command?
15    print"$HDR Assuming this isn't an LD/AR invocation. Continuing..";
16    exec($CMD,@ARGV);
17}
18
19# Copy .o files to a temporary location before DTrace messes with them
20chomp(my $tmpdir = `mktemp -d -t $$`);
21if (system("tar cf - @O_FILES | tar xf - -C $tmpdir") != 0) {
22    system("rm -r $tmpdir");
23    exit(1);
24}
25
26my $ss = join('_', @O_FILES);
27my $hexstr = md5_hex($ss);
28
29# From now, we work with files in the temporary location, update @ARGV
30map { $_ =~ s,.+\.o$,$tmpdir/$&, } @ARGV;
31
32my $INSTRUMENTED = "generated/probes_${hexstr}_$$.o";
33# Run DTrace instrumentation. Assuming running from build directory:
34my @args = (
35    'dtrace', '-C', '-G',
36    '-s', $DT_SRC,
37    '-o', $INSTRUMENTED,
38    grep { $_ =~ /\.o$/ } @ARGV);
39
40print "$HDR: Creating instrumented DTrace object: @args";
41if (system(@args) != 0) {
42    system("rm -r $tmpdir");
43    exit(1);
44}
45
46unshift @ARGV, $CMD;
47push @ARGV, $INSTRUMENTED;
48print "$HDR: Linking with instrumented DTrace object: @ARGV";
49my $rc = system(@ARGV);
50system("rm -r $tmpdir");
51exit($rc);
52