xref: /titanic_52/usr/src/cmd/dtrace/demo/mkdemo.pl (revision 10e6dadfe63181edabc58c8f42e3c56a1cd9ec95)
17c478bd9Sstevel@tonic-gate#!/usr/perl5/bin/perl -w
27c478bd9Sstevel@tonic-gate#
37c478bd9Sstevel@tonic-gate# CDDL HEADER START
47c478bd9Sstevel@tonic-gate#
57c478bd9Sstevel@tonic-gate# The contents of this file are subject to the terms of the
6dcafa303Sahl# Common Development and Distribution License (the "License").
7dcafa303Sahl# You may not use this file except in compliance with the License.
87c478bd9Sstevel@tonic-gate#
97c478bd9Sstevel@tonic-gate# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate# or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate# See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate# and limitations under the License.
137c478bd9Sstevel@tonic-gate#
147c478bd9Sstevel@tonic-gate# When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate# If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate# fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate# information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate#
207c478bd9Sstevel@tonic-gate# CDDL HEADER END
217c478bd9Sstevel@tonic-gate#
227c478bd9Sstevel@tonic-gate#
23*10e6dadfSbrendan# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate# Use is subject to license terms.
257c478bd9Sstevel@tonic-gate#
267c478bd9Sstevel@tonic-gate# ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gaterequire 5.005;
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gateuse strict;
317c478bd9Sstevel@tonic-gateuse warnings;
327c478bd9Sstevel@tonic-gateuse Time::localtime;
337c478bd9Sstevel@tonic-gateuse File::Basename;
347c478bd9Sstevel@tonic-gate
357c478bd9Sstevel@tonic-gateour ($cmd, $chapfile, $htmlfile, $dtrace_url, %chaps);
367c478bd9Sstevel@tonic-gate
377c478bd9Sstevel@tonic-gate$cmd = "mkdemo";
387c478bd9Sstevel@tonic-gate$chapfile = "chapters";
397c478bd9Sstevel@tonic-gate$htmlfile = "index.html";
407c478bd9Sstevel@tonic-gate$dtrace_url = "http://www.sun.com/bigadmin/content/dtrace";
417c478bd9Sstevel@tonic-gate
427c478bd9Sstevel@tonic-gatesub chaps_read {
437c478bd9Sstevel@tonic-gate	my $fatal;
447c478bd9Sstevel@tonic-gate	my %hash;
457c478bd9Sstevel@tonic-gate
467c478bd9Sstevel@tonic-gate	open(CHAPS, "$chapfile");
477c478bd9Sstevel@tonic-gate
487c478bd9Sstevel@tonic-gate	while (<CHAPS>) {
497c478bd9Sstevel@tonic-gate		my $field;
507c478bd9Sstevel@tonic-gate		my $value;
517c478bd9Sstevel@tonic-gate
527c478bd9Sstevel@tonic-gate		chop;
537c478bd9Sstevel@tonic-gate
547c478bd9Sstevel@tonic-gate		if (/^#/) {
557c478bd9Sstevel@tonic-gate			next;
567c478bd9Sstevel@tonic-gate		}
577c478bd9Sstevel@tonic-gate
587c478bd9Sstevel@tonic-gate		if (!/:/) {
597c478bd9Sstevel@tonic-gate			if (exists $hash{'name'}) {
607c478bd9Sstevel@tonic-gate				if (exists $chaps{$hash{'name'}}) {
617c478bd9Sstevel@tonic-gate					print "$cmd: chapter $hash{'name'} ";
627c478bd9Sstevel@tonic-gate					print "has two entries.\n";
637c478bd9Sstevel@tonic-gate					$fatal = 1;
647c478bd9Sstevel@tonic-gate				}
657c478bd9Sstevel@tonic-gate
667c478bd9Sstevel@tonic-gate				$chaps{$hash{'name'}} = { %hash };
677c478bd9Sstevel@tonic-gate				%hash = ();
687c478bd9Sstevel@tonic-gate				next;
697c478bd9Sstevel@tonic-gate			}
707c478bd9Sstevel@tonic-gate
717c478bd9Sstevel@tonic-gate			%hash = ();
727c478bd9Sstevel@tonic-gate			next;
737c478bd9Sstevel@tonic-gate		}
747c478bd9Sstevel@tonic-gate
757c478bd9Sstevel@tonic-gate		($field, $value) = split /:\s*/, $_, 2;
767c478bd9Sstevel@tonic-gate
777c478bd9Sstevel@tonic-gate		if ($field eq "descr") {
787c478bd9Sstevel@tonic-gate			$value .= " ";
797c478bd9Sstevel@tonic-gate		}
807c478bd9Sstevel@tonic-gate
817c478bd9Sstevel@tonic-gate		$hash{$field} .= $value;
827c478bd9Sstevel@tonic-gate	}
837c478bd9Sstevel@tonic-gate
847c478bd9Sstevel@tonic-gate	if ($fatal) {
857c478bd9Sstevel@tonic-gate		print "$cmd: fatal errors; cannot proceed.\n";
867c478bd9Sstevel@tonic-gate		exit;
877c478bd9Sstevel@tonic-gate	}
887c478bd9Sstevel@tonic-gate
897c478bd9Sstevel@tonic-gate	close (CHAPS);
907c478bd9Sstevel@tonic-gate}
917c478bd9Sstevel@tonic-gate
927c478bd9Sstevel@tonic-gatesub chaps_ascending {
93*10e6dadfSbrendan	$chaps{$a}{index} <=> $chaps{$b}{index};
947c478bd9Sstevel@tonic-gate}
957c478bd9Sstevel@tonic-gate
967c478bd9Sstevel@tonic-gatesub demo_process {
977c478bd9Sstevel@tonic-gate	my $chap = $_[0];
987c478bd9Sstevel@tonic-gate	my $demo = $_[1];
997c478bd9Sstevel@tonic-gate	my $year = localtime->year() + 1900;
1007c478bd9Sstevel@tonic-gate
1017c478bd9Sstevel@tonic-gate	open DEMO, "<$chap/$demo" or die "Can't open demo $chap/$demo";
1027c478bd9Sstevel@tonic-gate	open OUT, ">$demo" or die "Can't open $demo";
1037c478bd9Sstevel@tonic-gate
1047c478bd9Sstevel@tonic-gate	while (<DEMO>) {
1057c478bd9Sstevel@tonic-gate		print OUT $_;
1067c478bd9Sstevel@tonic-gate
1077c478bd9Sstevel@tonic-gate		if (/Use is subject to license terms/) {
1087c478bd9Sstevel@tonic-gate			print OUT <<EOF;
1097c478bd9Sstevel@tonic-gate *
1107c478bd9Sstevel@tonic-gate * This D script is used as an example in the Solaris Dynamic Tracing Guide
111*10e6dadfSbrendan * wiki in the \"$chaps{$chap}{title}\" Chapter.
1127c478bd9Sstevel@tonic-gate *
113*10e6dadfSbrendan * The full text of the this chapter may be found here:
1147c478bd9Sstevel@tonic-gate *
1157c478bd9Sstevel@tonic-gate *   $chaps{$chap}{url}
1167c478bd9Sstevel@tonic-gate *
1177c478bd9Sstevel@tonic-gate * On machines that have DTrace installed, this script is available as
1187c478bd9Sstevel@tonic-gate * $demo in /usr/demo/dtrace, a directory that contains all D scripts
1197c478bd9Sstevel@tonic-gate * used in the Solaris Dynamic Tracing Guide.  A table of the scripts and their
1207c478bd9Sstevel@tonic-gate * corresponding chapters may be found here:
1217c478bd9Sstevel@tonic-gate *
1227c478bd9Sstevel@tonic-gate *   file:///usr/demo/dtrace/index.html
1237c478bd9Sstevel@tonic-gateEOF
1247c478bd9Sstevel@tonic-gate		}
1257c478bd9Sstevel@tonic-gate	}
126*10e6dadfSbrendan
127*10e6dadfSbrendan	close (DEMO);
128*10e6dadfSbrendan	close (OUT);
1297c478bd9Sstevel@tonic-gate}
1307c478bd9Sstevel@tonic-gate
1317c478bd9Sstevel@tonic-gatesub demo_find {
1327c478bd9Sstevel@tonic-gate	my $demo = $_[0];
1337c478bd9Sstevel@tonic-gate	my $chap;
1347c478bd9Sstevel@tonic-gate
1357c478bd9Sstevel@tonic-gate	foreach $chap (keys %chaps) {
1367c478bd9Sstevel@tonic-gate		if (!stat("$chap/$demo")) {
1377c478bd9Sstevel@tonic-gate			next;
1387c478bd9Sstevel@tonic-gate		}
1397c478bd9Sstevel@tonic-gate
1407c478bd9Sstevel@tonic-gate		demo_process($chap, $demo);
1417c478bd9Sstevel@tonic-gate		return;
1427c478bd9Sstevel@tonic-gate	}
1437c478bd9Sstevel@tonic-gate
1447c478bd9Sstevel@tonic-gate	die "Couldn't find $demo in any chapter";
1457c478bd9Sstevel@tonic-gate}
1467c478bd9Sstevel@tonic-gate
1477c478bd9Sstevel@tonic-gatesub chaps_process {
1487c478bd9Sstevel@tonic-gate	my $outfile = $_[0];
1497c478bd9Sstevel@tonic-gate	my $chap;
1507c478bd9Sstevel@tonic-gate
1517c478bd9Sstevel@tonic-gate	open HTML, ">$outfile" or die "Can't open $outfile.";
1527c478bd9Sstevel@tonic-gate
1537c478bd9Sstevel@tonic-gate	print HTML "<html>\n<head>\n";
1547c478bd9Sstevel@tonic-gate	print HTML "<title>Example DTrace Scripts</title>\n";
1557c478bd9Sstevel@tonic-gate	print HTML "</head>\n<body bgcolor=\"#ffffff\">\n";
1567c478bd9Sstevel@tonic-gate
1577c478bd9Sstevel@tonic-gate	print HTML "<table width=\"85%\" border=0 align=\"center\"><tr><td>";
1587c478bd9Sstevel@tonic-gate	print HTML "<h2>DTrace Examples</h2>\n";
1597c478bd9Sstevel@tonic-gate
1607c478bd9Sstevel@tonic-gate	print HTML "<hr><p>\n";
1617c478bd9Sstevel@tonic-gate	print HTML "Here are the <a href=\"$dtrace_url\">DTrace</a> scripts\n";
1627c478bd9Sstevel@tonic-gate	print HTML "that are used as examples in the\n";
1637c478bd9Sstevel@tonic-gate	print HTML "<a href=\"$chaps{book}{url}\">$chaps{book}{title}</a>. ";
1647c478bd9Sstevel@tonic-gate	print HTML "For more information on any one script, follow the link\n";
1657c478bd9Sstevel@tonic-gate	print HTML "to its corresponding chapter.\n";
1667c478bd9Sstevel@tonic-gate	print HTML "<p>\n<hr><p>\n";
1677c478bd9Sstevel@tonic-gate
1687c478bd9Sstevel@tonic-gate	print HTML "<left><table width=\"85%\" border=1 cellpadding=4 ";
1697c478bd9Sstevel@tonic-gate	print HTML "cellspacing=0 align=\"center\" bgcolor=\"#ffffff\">\n";
1707c478bd9Sstevel@tonic-gate	print HTML "<tr bgcolor=\"#5882a1\"><td width=\"50%\">";
1717c478bd9Sstevel@tonic-gate	print HTML "<font color=\"#ffffff\"><b>Chapter</b></td></font>\n";
1727c478bd9Sstevel@tonic-gate	print HTML "<td><font color=\"#ffffff\"><b>Script</b></td>\n";
1737c478bd9Sstevel@tonic-gate	print HTML "</font></tr>\n";
1747c478bd9Sstevel@tonic-gate
1757c478bd9Sstevel@tonic-gate	foreach $chap (sort chaps_ascending (keys %chaps)) {
1767c478bd9Sstevel@tonic-gate		my @demos;
1777c478bd9Sstevel@tonic-gate		my $demo;
1787c478bd9Sstevel@tonic-gate
1797c478bd9Sstevel@tonic-gate		#
1807c478bd9Sstevel@tonic-gate		# Open the directory associated with the chapter.
1817c478bd9Sstevel@tonic-gate		#
1827c478bd9Sstevel@tonic-gate		if ($chap =~ /^book$/) {
1837c478bd9Sstevel@tonic-gate			next;
1847c478bd9Sstevel@tonic-gate		}
1857c478bd9Sstevel@tonic-gate
1867c478bd9Sstevel@tonic-gate		opendir(DEMOS, $chap) || die("Cannot open directory $chap");
1877c478bd9Sstevel@tonic-gate		@demos = readdir(DEMOS);
1887c478bd9Sstevel@tonic-gate		closedir(DEMOS);
1897c478bd9Sstevel@tonic-gate
1907c478bd9Sstevel@tonic-gate		print HTML "<tr>\n";
1917c478bd9Sstevel@tonic-gate		print HTML "<td align=left>";
1927c478bd9Sstevel@tonic-gate		print HTML "<a href=\"$chaps{$chap}{url}\">";
1937c478bd9Sstevel@tonic-gate		print HTML "$chaps{$chap}{title}</a></td>\n";
1947c478bd9Sstevel@tonic-gate
1957c478bd9Sstevel@tonic-gate		print HTML "<td><table border=0>\n";
1967c478bd9Sstevel@tonic-gate
197dcafa303Sahl		foreach $demo (sort(@demos)) {
1987c478bd9Sstevel@tonic-gate			if ($demo !~ /^[a-z].*\.d$/) {
1997c478bd9Sstevel@tonic-gate				next;
2007c478bd9Sstevel@tonic-gate			}
2017c478bd9Sstevel@tonic-gate
2027c478bd9Sstevel@tonic-gate			print HTML "<tr><td><a href=\"$demo\">$demo</a>";
2037c478bd9Sstevel@tonic-gate			print HTML "</td></tr>\n";
2047c478bd9Sstevel@tonic-gate
2057c478bd9Sstevel@tonic-gate			demo_process($chap, $demo);
2067c478bd9Sstevel@tonic-gate		}
2077c478bd9Sstevel@tonic-gate
2087c478bd9Sstevel@tonic-gate		print HTML "</table></td></tr>\n";
2097c478bd9Sstevel@tonic-gate	}
2107c478bd9Sstevel@tonic-gate
2117c478bd9Sstevel@tonic-gate	print HTML "</table>\n</td>\n<p>\n\n";
2127c478bd9Sstevel@tonic-gate	print HTML "</td></tr>\n";
2137c478bd9Sstevel@tonic-gate	print HTML "<tr><td><hr><small>Copyright ";
2147c478bd9Sstevel@tonic-gate	print HTML localtime->year() + 1900;
2157c478bd9Sstevel@tonic-gate	print HTML " Sun Microsystems</small>\n";
2167c478bd9Sstevel@tonic-gate	print HTML "</table>\n";
2177c478bd9Sstevel@tonic-gate	print HTML "</body>\n</html>\n";
2187c478bd9Sstevel@tonic-gate	close HTML;
2197c478bd9Sstevel@tonic-gate}
2207c478bd9Sstevel@tonic-gate
2217c478bd9Sstevel@tonic-gatechaps_read();
2227c478bd9Sstevel@tonic-gate
2237c478bd9Sstevel@tonic-gateif (basename($ARGV[0]) ne "$htmlfile") {
2247c478bd9Sstevel@tonic-gate	demo_find(basename($ARGV[0]));
2257c478bd9Sstevel@tonic-gate} else {
2267c478bd9Sstevel@tonic-gate	chaps_process($htmlfile);
2277c478bd9Sstevel@tonic-gate}
228