1*23b5c241Stomee /*
2*23b5c241Stomee  * CDDL HEADER START
3*23b5c241Stomee  *
4*23b5c241Stomee  * The contents of this file are subject to the terms of the
5*23b5c241Stomee  * Common Development and Distribution License (the "License").
6*23b5c241Stomee  * You may not use this file except in compliance with the License.
7*23b5c241Stomee  *
8*23b5c241Stomee  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*23b5c241Stomee  * or http://www.opensolaris.org/os/licensing.
10*23b5c241Stomee  * See the License for the specific language governing permissions
11*23b5c241Stomee  * and limitations under the License.
12*23b5c241Stomee  *
13*23b5c241Stomee  * When distributing Covered Code, include this CDDL HEADER in each
14*23b5c241Stomee  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*23b5c241Stomee  * If applicable, add the following below this CDDL HEADER, with the
16*23b5c241Stomee  * fields enclosed by brackets "[]" replaced with your own identifying
17*23b5c241Stomee  * information: Portions Copyright [yyyy] [name of copyright owner]
18*23b5c241Stomee  *
19*23b5c241Stomee  * CDDL HEADER END
20*23b5c241Stomee  */
21*23b5c241Stomee 
22*23b5c241Stomee /*
23*23b5c241Stomee  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24*23b5c241Stomee  * Use is subject to license terms.
25*23b5c241Stomee  *
26*23b5c241Stomee  * ident	"%Z%%M%	%I%	%E% SMI"
27*23b5c241Stomee  */
28*23b5c241Stomee 
29*23b5c241Stomee import org.opensolaris.os.dtrace.*;
30*23b5c241Stomee 
31*23b5c241Stomee /**
32*23b5c241Stomee  * Prove that enable() handles multiple programs, recognizing programs
33*23b5c241Stomee  * that are already enabled and programs that were compiled by another
34*23b5c241Stomee  * consumer.
35*23b5c241Stomee  */
36*23b5c241Stomee public class TestEnable {
37*23b5c241Stomee     static void
exit(int status)38*23b5c241Stomee     exit(int status)
39*23b5c241Stomee     {
40*23b5c241Stomee 	System.out.flush();
41*23b5c241Stomee 	System.err.flush();
42*23b5c241Stomee 	System.exit(status);
43*23b5c241Stomee     }
44*23b5c241Stomee 
45*23b5c241Stomee     public static void
main(String[] args)46*23b5c241Stomee     main(String[] args)
47*23b5c241Stomee     {
48*23b5c241Stomee 	Consumer consumer = new LocalConsumer();
49*23b5c241Stomee 
50*23b5c241Stomee 	try {
51*23b5c241Stomee 	    consumer.open();
52*23b5c241Stomee 	    Program p0 = consumer.compile("dtrace:::BEGIN");
53*23b5c241Stomee 	    Program p1 = consumer.compile("syscall:::entry");
54*23b5c241Stomee 	    Program p2 = consumer.compile("dtrace:::END");
55*23b5c241Stomee 	    consumer.enable(p0);
56*23b5c241Stomee 	    consumer.enable(p1);
57*23b5c241Stomee 	    try {
58*23b5c241Stomee 		consumer.go();
59*23b5c241Stomee 		System.err.println("go() illegal, not all programs " +
60*23b5c241Stomee 			"enabled (p0, p1)");
61*23b5c241Stomee 		exit(1);
62*23b5c241Stomee 	    } catch (IllegalStateException e) {
63*23b5c241Stomee 		System.out.println(e);
64*23b5c241Stomee 	    } catch (Exception e) {
65*23b5c241Stomee 		e.printStackTrace();
66*23b5c241Stomee 		exit(1);
67*23b5c241Stomee 	    }
68*23b5c241Stomee 	    try {
69*23b5c241Stomee 		consumer.enable();
70*23b5c241Stomee 		System.err.println("enable() illegal, some programs " +
71*23b5c241Stomee 			"already enabled (p0, p1)");
72*23b5c241Stomee 		exit(1);
73*23b5c241Stomee 	    } catch (IllegalStateException e) {
74*23b5c241Stomee 		System.out.println(e);
75*23b5c241Stomee 	    } catch (Exception e) {
76*23b5c241Stomee 		e.printStackTrace();
77*23b5c241Stomee 		exit(1);
78*23b5c241Stomee 	    }
79*23b5c241Stomee 	    try {
80*23b5c241Stomee 		consumer.enable(p0);
81*23b5c241Stomee 		System.err.println("cannot enable a program that " +
82*23b5c241Stomee 			"has already been enabled (p0)");
83*23b5c241Stomee 		exit(1);
84*23b5c241Stomee 	    } catch (IllegalStateException e) {
85*23b5c241Stomee 		System.out.println(e);
86*23b5c241Stomee 	    } catch (Exception e) {
87*23b5c241Stomee 		e.printStackTrace();
88*23b5c241Stomee 		exit(1);
89*23b5c241Stomee 	    }
90*23b5c241Stomee 	    consumer.enable(p2);
91*23b5c241Stomee 	    Program p3 = consumer.compile("syscall:::return");
92*23b5c241Stomee 	    try {
93*23b5c241Stomee 		consumer.go();
94*23b5c241Stomee 		System.err.println("go() illegal, not all programs " +
95*23b5c241Stomee 			"enabled (p0, p1, p2)");
96*23b5c241Stomee 		exit(1);
97*23b5c241Stomee 	    } catch (IllegalStateException e) {
98*23b5c241Stomee 		System.out.println(e);
99*23b5c241Stomee 	    } catch (Exception e) {
100*23b5c241Stomee 		e.printStackTrace();
101*23b5c241Stomee 		exit(1);
102*23b5c241Stomee 	    }
103*23b5c241Stomee 	    try {
104*23b5c241Stomee 		consumer.enable();
105*23b5c241Stomee 		System.err.println("enable() illegal, some programs " +
106*23b5c241Stomee 			"already enabled (p0, p1, p2)");
107*23b5c241Stomee 		exit(1);
108*23b5c241Stomee 	    } catch (IllegalStateException e) {
109*23b5c241Stomee 		System.out.println(e);
110*23b5c241Stomee 	    } catch (Exception e) {
111*23b5c241Stomee 		e.printStackTrace();
112*23b5c241Stomee 		exit(1);
113*23b5c241Stomee 	    }
114*23b5c241Stomee 	    // Try to fool the consumer with a program compiled by
115*23b5c241Stomee 	    // another consumer
116*23b5c241Stomee 	    Consumer consumer2 = new LocalConsumer();
117*23b5c241Stomee 	    consumer2.open();
118*23b5c241Stomee 	    Program p3x = consumer2.compile("syscall:::return");
119*23b5c241Stomee 	    try {
120*23b5c241Stomee 		consumer.enable(p3x);
121*23b5c241Stomee 		System.err.println("cannot enable program compiled " +
122*23b5c241Stomee 			"by another consumer");
123*23b5c241Stomee 		exit(1);
124*23b5c241Stomee 	    } catch (IllegalArgumentException e) {
125*23b5c241Stomee 		System.out.println(e);
126*23b5c241Stomee 	    } catch (Exception e) {
127*23b5c241Stomee 		e.printStackTrace();
128*23b5c241Stomee 		exit(1);
129*23b5c241Stomee 	    } finally {
130*23b5c241Stomee 		consumer2.close();
131*23b5c241Stomee 	    }
132*23b5c241Stomee 	    consumer.enable(p3);
133*23b5c241Stomee 	    consumer.go();
134*23b5c241Stomee 	    consumer.close();
135*23b5c241Stomee 
136*23b5c241Stomee 	    // Enable all compiled programs at once
137*23b5c241Stomee 	    consumer = new LocalConsumer();
138*23b5c241Stomee 	    consumer.open();
139*23b5c241Stomee 	    consumer.compile("dtrace:::BEGIN");
140*23b5c241Stomee 	    consumer.compile("syscall:::entry");
141*23b5c241Stomee 	    consumer.compile("dtrace:::END");
142*23b5c241Stomee 	    consumer.enable();
143*23b5c241Stomee 	    consumer.go();
144*23b5c241Stomee 	    consumer.close();
145*23b5c241Stomee 	    exit(0);
146*23b5c241Stomee 	} catch (DTraceException e) {
147*23b5c241Stomee 	    e.printStackTrace();
148*23b5c241Stomee 	    exit(1);
149*23b5c241Stomee 	}
150*23b5c241Stomee     }
151*23b5c241Stomee }
152