1178476Sjb/*
2178476Sjb * CDDL HEADER START
3178476Sjb *
4178476Sjb * The contents of this file are subject to the terms of the
5178476Sjb * Common Development and Distribution License (the "License").
6178476Sjb * You may not use this file except in compliance with the License.
7178476Sjb *
8178476Sjb * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9178476Sjb * or http://www.opensolaris.org/os/licensing.
10178476Sjb * See the License for the specific language governing permissions
11178476Sjb * and limitations under the License.
12178476Sjb *
13178476Sjb * When distributing Covered Code, include this CDDL HEADER in each
14178476Sjb * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15178476Sjb * If applicable, add the following below this CDDL HEADER, with the
16178476Sjb * fields enclosed by brackets "[]" replaced with your own identifying
17178476Sjb * information: Portions Copyright [yyyy] [name of copyright owner]
18178476Sjb *
19178476Sjb * CDDL HEADER END
20178476Sjb */
21178476Sjb
22178476Sjb/*
23178476Sjb * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24178476Sjb * Use is subject to license terms.
25178476Sjb *
26178476Sjb * ident	"%Z%%M%	%I%	%E% SMI"
27178476Sjb */
28178476Sjb
29178476Sjbimport org.opensolaris.os.dtrace.*;
30178476Sjb
31178476Sjb/**
32178476Sjb * Prove that enable() handles multiple programs, recognizing programs
33178476Sjb * that are already enabled and programs that were compiled by another
34178476Sjb * consumer.
35178476Sjb */
36178476Sjbpublic class TestEnable {
37178476Sjb    static void
38178476Sjb    exit(int status)
39178476Sjb    {
40178476Sjb	System.out.flush();
41178476Sjb	System.err.flush();
42178476Sjb	System.exit(status);
43178476Sjb    }
44178476Sjb
45178476Sjb    public static void
46178476Sjb    main(String[] args)
47178476Sjb    {
48178476Sjb	Consumer consumer = new LocalConsumer();
49178476Sjb
50178476Sjb	try {
51178476Sjb	    consumer.open();
52178476Sjb	    Program p0 = consumer.compile("dtrace:::BEGIN");
53178476Sjb	    Program p1 = consumer.compile("syscall:::entry");
54178476Sjb	    Program p2 = consumer.compile("dtrace:::END");
55178476Sjb	    consumer.enable(p0);
56178476Sjb	    consumer.enable(p1);
57178476Sjb	    try {
58178476Sjb		consumer.go();
59178476Sjb		System.err.println("go() illegal, not all programs " +
60178476Sjb			"enabled (p0, p1)");
61178476Sjb		exit(1);
62178476Sjb	    } catch (IllegalStateException e) {
63178476Sjb		System.out.println(e);
64178476Sjb	    } catch (Exception e) {
65178476Sjb		e.printStackTrace();
66178476Sjb		exit(1);
67178476Sjb	    }
68178476Sjb	    try {
69178476Sjb		consumer.enable();
70178476Sjb		System.err.println("enable() illegal, some programs " +
71178476Sjb			"already enabled (p0, p1)");
72178476Sjb		exit(1);
73178476Sjb	    } catch (IllegalStateException e) {
74178476Sjb		System.out.println(e);
75178476Sjb	    } catch (Exception e) {
76178476Sjb		e.printStackTrace();
77178476Sjb		exit(1);
78178476Sjb	    }
79178476Sjb	    try {
80178476Sjb		consumer.enable(p0);
81178476Sjb		System.err.println("cannot enable a program that " +
82178476Sjb			"has already been enabled (p0)");
83178476Sjb		exit(1);
84178476Sjb	    } catch (IllegalStateException e) {
85178476Sjb		System.out.println(e);
86178476Sjb	    } catch (Exception e) {
87178476Sjb		e.printStackTrace();
88178476Sjb		exit(1);
89178476Sjb	    }
90178476Sjb	    consumer.enable(p2);
91178476Sjb	    Program p3 = consumer.compile("syscall:::return");
92178476Sjb	    try {
93178476Sjb		consumer.go();
94178476Sjb		System.err.println("go() illegal, not all programs " +
95178476Sjb			"enabled (p0, p1, p2)");
96178476Sjb		exit(1);
97178476Sjb	    } catch (IllegalStateException e) {
98178476Sjb		System.out.println(e);
99178476Sjb	    } catch (Exception e) {
100178476Sjb		e.printStackTrace();
101178476Sjb		exit(1);
102178476Sjb	    }
103178476Sjb	    try {
104178476Sjb		consumer.enable();
105178476Sjb		System.err.println("enable() illegal, some programs " +
106178476Sjb			"already enabled (p0, p1, p2)");
107178476Sjb		exit(1);
108178476Sjb	    } catch (IllegalStateException e) {
109178476Sjb		System.out.println(e);
110178476Sjb	    } catch (Exception e) {
111178476Sjb		e.printStackTrace();
112178476Sjb		exit(1);
113178476Sjb	    }
114178476Sjb	    // Try to fool the consumer with a program compiled by
115178476Sjb	    // another consumer
116178476Sjb	    Consumer consumer2 = new LocalConsumer();
117178476Sjb	    consumer2.open();
118178476Sjb	    Program p3x = consumer2.compile("syscall:::return");
119178476Sjb	    try {
120178476Sjb		consumer.enable(p3x);
121178476Sjb		System.err.println("cannot enable program compiled " +
122178476Sjb			"by another consumer");
123178476Sjb		exit(1);
124178476Sjb	    } catch (IllegalArgumentException e) {
125178476Sjb		System.out.println(e);
126178476Sjb	    } catch (Exception e) {
127178476Sjb		e.printStackTrace();
128178476Sjb		exit(1);
129178476Sjb	    } finally {
130178476Sjb		consumer2.close();
131178476Sjb	    }
132178476Sjb	    consumer.enable(p3);
133178476Sjb	    consumer.go();
134178476Sjb	    consumer.close();
135178476Sjb
136178476Sjb	    // Enable all compiled programs at once
137178476Sjb	    consumer = new LocalConsumer();
138178476Sjb	    consumer.open();
139178476Sjb	    consumer.compile("dtrace:::BEGIN");
140178476Sjb	    consumer.compile("syscall:::entry");
141178476Sjb	    consumer.compile("dtrace:::END");
142178476Sjb	    consumer.enable();
143178476Sjb	    consumer.go();
144178476Sjb	    consumer.close();
145178476Sjb	    exit(0);
146178476Sjb	} catch (DTraceException e) {
147178476Sjb	    e.printStackTrace();
148178476Sjb	    exit(1);
149178476Sjb	}
150178476Sjb    }
151178476Sjb}
152