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