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