1*1449Stomee /*
2*1449Stomee  * CDDL HEADER START
3*1449Stomee  *
4*1449Stomee  * The contents of this file are subject to the terms of the
5*1449Stomee  * Common Development and Distribution License (the "License").
6*1449Stomee  * You may not use this file except in compliance with the License.
7*1449Stomee  *
8*1449Stomee  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*1449Stomee  * or http://www.opensolaris.org/os/licensing.
10*1449Stomee  * See the License for the specific language governing permissions
11*1449Stomee  * and limitations under the License.
12*1449Stomee  *
13*1449Stomee  * When distributing Covered Code, include this CDDL HEADER in each
14*1449Stomee  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*1449Stomee  * If applicable, add the following below this CDDL HEADER, with the
16*1449Stomee  * fields enclosed by brackets "[]" replaced with your own identifying
17*1449Stomee  * information: Portions Copyright [yyyy] [name of copyright owner]
18*1449Stomee  *
19*1449Stomee  * CDDL HEADER END
20*1449Stomee  */
21*1449Stomee 
22*1449Stomee /*
23*1449Stomee  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24*1449Stomee  * Use is subject to license terms.
25*1449Stomee  *
26*1449Stomee  * ident	"%Z%%M%	%I%	%E% SMI"
27*1449Stomee  */
28*1449Stomee 
29*1449Stomee import org.opensolaris.os.dtrace.*;
30*1449Stomee import java.io.File;
31*1449Stomee 
32*1449Stomee public class TestTarget {
33*1449Stomee     public static void
main(String[] args)34*1449Stomee     main(String[] args)
35*1449Stomee     {
36*1449Stomee 	if (args.length != 2) {
37*1449Stomee 	    System.err.println("Usage: java TestTarget <script> <command>");
38*1449Stomee 	    System.exit(2);
39*1449Stomee 	}
40*1449Stomee 
41*1449Stomee 	File file = new File(args[0]);
42*1449Stomee 	String command = args[1];
43*1449Stomee 
44*1449Stomee 	final Consumer consumer = new LocalConsumer();
45*1449Stomee 	consumer.addConsumerListener(new ConsumerAdapter() {
46*1449Stomee 	    public void dataReceived(DataEvent e) {
47*1449Stomee 		System.out.println(e.getProbeData());
48*1449Stomee 	    }
49*1449Stomee 	    public void consumerStopped(ConsumerEvent e) {
50*1449Stomee 		try {
51*1449Stomee 		    Aggregate a = consumer.getAggregate();
52*1449Stomee 		    for (Aggregation agg : a.asMap().values()) {
53*1449Stomee 			for (AggregationRecord rec : agg.asMap().values()) {
54*1449Stomee 			    System.out.println(rec.getTuple() + " " +
55*1449Stomee 				    rec.getValue());
56*1449Stomee 			}
57*1449Stomee 		    }
58*1449Stomee 		} catch (Exception x) {
59*1449Stomee 		    x.printStackTrace();
60*1449Stomee 		    System.exit(1);
61*1449Stomee 		}
62*1449Stomee 		consumer.close();
63*1449Stomee 	    }
64*1449Stomee 	    public void processStateChanged(ProcessEvent e) {
65*1449Stomee 		System.out.println(e.getProcessState());
66*1449Stomee 	    }
67*1449Stomee 	});
68*1449Stomee 
69*1449Stomee 	try {
70*1449Stomee 	    consumer.open();
71*1449Stomee 	    // pid replaces $target variable in D script
72*1449Stomee 	    consumer.createProcess(command);
73*1449Stomee 	    consumer.compile(file);
74*1449Stomee 	    consumer.enable();
75*1449Stomee 	    consumer.go();
76*1449Stomee 	} catch (Exception e) {
77*1449Stomee 	    e.printStackTrace();
78*1449Stomee 	    System.exit(1);
79*1449Stomee 	}
80*1449Stomee     }
81*1449Stomee }
82