summaryrefslogtreecommitdiff
path: root/scripts/dheadgen.pl
blob: 69ece2e7c81a6dfb28c030fa78fd4184fd3e82c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# Copyright (c) 2008, 2017, Oracle and/or its affiliates. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 
#    * Redistributions of source code must retain the above copyright
#      notice, this list of conditions and the following disclaimer. 
#    * Redistributions in binary form must reproduce the above copyright
#      notice, this list of conditions and the following disclaimer in  
#      the documentation and/or other materials provided with the       
#      distribution.
#    * Neither the name of the above-listed copyright holders nor the names
#      of its contributors may be used to endorse or promote products derived
#      from this software without specific prior written permission.  
#       
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED  
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
# OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,     
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR      
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF  
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING    
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS      
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# ident	"@(#)dheadgen.pl	1.4	07/06/24 SMI"

#
# DTrace Header Generator
# -----------------------
#
# This script is meant to mimic the output of dtrace(1M) with the -h
# (headergen) flag on system that lack native support for DTrace. This script
# is intended to be integrated into projects that use DTrace's static tracing
# facilities (USDT), and invoked as part of the build process to have a
# common build process on all target systems. To facilitate this, this script
# is licensed under a BSD license. On system with native DTrace support, the
# dtrace(1M) command will be invoked to create the full header file; on other
# systems, this script will generated a stub header file.
#
# Normally, generated macros take the form PROVIDER_PROBENAME().  It may be
# desirable to customize the output of this script and of dtrace(1M) to
# tailor the precise macro name. To do this, edit the emit_dtrace() subroutine
# to pattern match for the lines you want to customize.
#

use strict;

my @lines;
my @tokens = ();
my $lineno = 0;
my $newline = 1;
my $eof = 0;
my $infile;
my $outfile;
my $force = 0;

sub emit_dtrace {
	my ($line) = @_;

	#
	# Insert customization here. For example, if you want to change the
	# name of the macros you may do something like this:
	#
	# $line =~ s/(\s)[A-Z]+_/\1TRACE_MOZILLA_/;
	#

	print $line;
}

#
# The remaining code deals with parsing D provider definitions and emitting
# the stub header file. There should be no need to edit this absent a bug.
#

#
# Emit the two relevant macros for each probe in the given provider:
#    PROVIDER_PROBENAME(<args>)
#    PROVIDER_PROBENAME_ENABLED() (0)
#
sub emit_provider {
	my ($provname, @probes) = @_;

	$provname = uc($provname);

	foreach my $probe (@probes) {
		my $probename = uc($$probe{'name'});
		my $argc = $$probe{'argc'};
		my $line;

		$probename =~ s/__/_/g;

		$line = "#define\t${provname}_${probename}(";
		for (my $i = 0; $i < $argc; $i++) {
			$line .= ($i == 0 ? '' : ', ');
			$line .= "arg$i";
		}
		$line .= ")\n";
		emit_dtrace($line);
		
		$line = "#define\t${provname}_${probename}_ENABLED() (0)\n";
		emit_dtrace($line);
	}

	emit_dtrace("\n");
}

sub emit_prologue {
	my ($filename) = @_;

	$filename =~ s/.*\///g;
	$filename = uc($filename);
	$filename =~ s/\./_/g;

	emit_dtrace <<"EOF";
/*
 * Generated by dheadgen(1).
 */

#ifndef\t_${filename}
#define\t_${filename}

#ifdef\t__cplusplus
extern "C" {
#endif

EOF
}

sub emit_epilogue {
	my ($filename) = @_;

	$filename =~ s/.*\///g;
	$filename = uc($filename);
	$filename =~ s/\./_/g;

	emit_dtrace <<"EOF";
#ifdef  __cplusplus
}
#endif

#endif  /* _$filename */
EOF
}

#
# Get the next token from the file keeping track of the line number.
#
sub get_token {
	my ($eof_ok) = @_;
	my $tok;

	while (1) {
		while (scalar(@tokens) == 0) {
			if (scalar(@lines) == 0) {
				$eof = 1;
				return if ($eof_ok);
				die "expected more data at line $lineno";
			}

			$lineno++;
			push(@tokens, split(/(\s+|\n|[(){},#;]|\/\*|\*\/)/,
			    shift(@lines)));
		}

		$tok = shift(@tokens);
		next if ($tok eq '');
		next if ($tok =~ /^[ \t]+$/);

		return ($tok);
	}
}

#
# Ignore newlines, comments and typedefs
#
sub next_token {
	my ($eof_ok) = @_;
	my $tok;

	while (1) {
		$tok = get_token($eof_ok);
		return if ($eof_ok && $eof);
		if ($tok eq "typedef" or $tok =~ /^#/) {
		  while (1) {
		    $tok = get_token(0);
		    last if ($tok eq "\n");
		  }
		  next;
		} elsif ($tok eq '/*') {
			while (get_token(0) ne '*/') {
				next;
			}
			next;
		} elsif ($tok eq "\n") {
			next;
		}

		last;
	}

	return ($tok);
}

sub expect_token {
	my ($t) = @_;
	my $tok;

	while (($tok = next_token(0)) eq "\n") {
		next;
	}

	die "expected '$t' at line $lineno rather than '$tok'" if ($t ne $tok);
}

sub get_args {
	expect_token('(');

	my $tok = next_token(0);
	my @args = ();

	return (@args) if ($tok eq ')');

	if ($tok eq 'void') {
		expect_token(')');
		return (@args);
	}

	my $arg = $tok;

	while (1) {
		$tok = next_token(0);
		if ($tok eq ',' || $tok eq ')') {
			push(@args, $arg);
			$arg = '';
			last if ($tok eq ')');
		} else {
			$arg = "$arg $tok";
		}
	}

	return (@args);
}

sub usage {
	die "usage: $0 [-f] <filename.d>\n";
}

usage() if (scalar(@ARGV) < 1);
if ($ARGV[0] eq '-f') {
	usage() if (scalar(@ARGV < 2));
	$force = 1;
	shift;
}
$infile = $ARGV[0];
usage() if ($infile !~ /(.+)\.d$/);

#
# If the system has native support for DTrace, we'll use that binary instead.
#
if (-x '/usr/sbin/dtrace' && !$force) {
	open(DTRACE, "-| /usr/sbin/dtrace -C -h -s $infile -o /dev/stdout")
	    or die "can't invoke dtrace(1M)";

	while (<DTRACE>) {
		emit_dtrace($_);
	}

	close(DTRACE);

	exit(0);
}

emit_prologue($infile);

open(D, "< $infile") or die "couldn't open $infile";
@lines = <D>;
close(D);

while (1) {
	my $nl = 0;
	my $tok = next_token(1);
	last if $eof;

	if ($newline && $tok eq '#') {
		while (1) {
			$tok = get_token(0);

			last if ($tok eq "\n");
		}
		$nl = 1;
	} elsif ($tok eq "\n") {
		$nl = 1;
	} elsif ($tok eq 'provider') {
		my $provname = next_token(0);
		my @probes = ();
		expect_token('{');

		while (1) {
			$tok = next_token(0);
			if ($tok eq 'probe') {
				my $probename = next_token(0);
				my @args = get_args();

				next while (next_token(0) ne ';');

				push(@probes, {
				    'name' => $probename,
				    'argc' => scalar(@args)
				});

			} elsif ($tok eq '}') {
				expect_token(';');

				emit_provider($provname, @probes);

				last;
			}
		}

	} else {
		die "syntax error at line $lineno near '$tok'\n";
	}

	$newline = $nl;
}

emit_epilogue($infile);

exit(0);