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 * Regression test for the LocalConsumer state machine.  Calls Consumer
33 * methods before and after open(), compile(), enable(), go(), stop(),
34 * and close() to verify that the calls succeed as expected or fail with
35 * the expected Java exception.
36 */
37public class TestStateMachine {
38    static Program program;
39
40    static void
41    exit(int status)
42    {
43	exit(status, null);
44    }
45
46    static void
47    exit(int status, String msg)
48    {
49	if (msg != null) {
50	    System.out.println(msg);
51	}
52	System.out.flush();
53	System.err.flush();
54	System.exit(status);
55    }
56
57    static void
58    printState(Consumer consumer)
59    {
60	System.out.println("open: " + consumer.isOpen());
61	System.out.println("enabled: " + consumer.isEnabled());
62	System.out.println("closed: " + consumer.isClosed());
63    }
64
65    static void
66    beforeOpen(Consumer consumer)
67    {
68	System.out.println("before open");
69	printState(consumer);
70
71	// compile
72	try {
73	    consumer.compile("syscall:::entry");
74	    exit(1, "compile before open");
75	} catch (IllegalStateException e) {
76	    System.out.println(e);
77	} catch (Exception e) {
78	    e.printStackTrace();
79	    exit(1, "compile before open");
80	}
81
82	// enable
83	try {
84	    consumer.enable();
85	    exit(1, "enable before open");
86	} catch (IllegalStateException e) {
87	    System.out.println(e);
88	} catch (Exception e) {
89	    e.printStackTrace();
90	    exit(1, "enable before open");
91	}
92
93	// getOption, setOption, unsetOption
94	try {
95	    consumer.getOption(Option.bufsize);
96	    exit(1, "getOption before open");
97	} catch (IllegalStateException e) {
98	    System.out.println(e);
99	} catch (Exception e) {
100	    e.printStackTrace();
101	    exit(1, "getOption before open");
102	}
103	try {
104	    consumer.setOption(Option.bufsize, Option.mb(1));
105	    exit(1, "setOption before open");
106	} catch (IllegalStateException e) {
107	    System.out.println(e);
108	} catch (Exception e) {
109	    e.printStackTrace();
110	    exit(1, "setOption before open");
111	}
112	try {
113	    consumer.unsetOption(Option.quiet);
114	    exit(1, "unsetOption before open");
115	} catch (IllegalStateException e) {
116	    System.out.println(e);
117	} catch (Exception e) {
118	    e.printStackTrace();
119	    exit(1, "unsetOption before open");
120	}
121
122	// createProcess, grabProcess
123	try {
124	    consumer.createProcess("date");
125	    exit(1, "createProcess before open");
126	} catch (IllegalStateException e) {
127	    System.out.println(e);
128	} catch (Exception e) {
129	    e.printStackTrace();
130	    exit(1, "createProcess before open");
131	}
132	try {
133	    consumer.grabProcess(1);
134	    exit(1, "grabProcess before open");
135	} catch (IllegalStateException e) {
136	    System.out.println(e);
137	} catch (Exception e) {
138	    e.printStackTrace();
139	    exit(1, "grabProcess before open");
140	}
141
142	// listProbes
143	try {
144	    consumer.listProbes(ProbeDescription.EMPTY);
145	    exit(1, "listProbes before open");
146	} catch (IllegalStateException e) {
147	    System.out.println(e);
148	} catch (Exception e) {
149	    e.printStackTrace();
150	    exit(1, "listProbes before open");
151	}
152
153	// getAggregate
154	try {
155	    consumer.getAggregate();
156	    exit(1, "getAggregate before open");
157	} catch (IllegalStateException e) {
158	    System.out.println(e);
159	} catch (Exception e) {
160	    e.printStackTrace();
161	    exit(1, "getAggregate before open");
162	}
163
164	// getVersion
165	try {
166	    consumer.getVersion(); // allowed
167	} catch (Exception e) {
168	    e.printStackTrace();
169	    exit(1, "getVersion before open");
170	}
171    }
172
173    static void
174    beforeCompile(Consumer consumer)
175    {
176	System.out.println("before compile");
177	printState(consumer);
178
179	// open
180	try {
181	    consumer.open();
182	    exit(1, "open after open");
183	} catch (IllegalStateException e) {
184	    System.out.println(e);
185	} catch (Exception e) {
186	    e.printStackTrace();
187	    exit(1, "open after open");
188	}
189
190	// enable
191	try {
192	    consumer.enable();
193	    exit(1, "enable before compile");
194	} catch (IllegalStateException e) {
195	    System.out.println(e);
196	} catch (Exception e) {
197	    e.printStackTrace();
198	    exit(1, "enable before compile");
199	}
200    }
201
202    static void
203    beforeEnable(Consumer consumer)
204    {
205	System.out.println("before enable");
206	printState(consumer);
207
208	// go
209	try {
210	    consumer.go();
211	    exit(1, "go before enable");
212	} catch (IllegalStateException e) {
213	    System.out.println(e);
214	} catch (Exception e) {
215	    e.printStackTrace();
216	    exit(1, "go before enable");
217	}
218    }
219
220    static void
221    beforeGo(Consumer consumer)
222    {
223	System.out.println("before go");
224	printState(consumer);
225
226	// getAggregate
227	try {
228	    consumer.getAggregate();
229	    exit(1, "getAggregate before go");
230	} catch (IllegalStateException e) {
231	    System.out.println(e);
232	} catch (Exception e) {
233	    e.printStackTrace();
234	    exit(1, "getAggregate before go");
235	}
236
237	// lookupKernelFunction, lookupUserFunction
238	try {
239	    consumer.lookupKernelFunction(1);
240	    exit(1, "lookupKernelFunction before go");
241	} catch (IllegalStateException e) {
242	    System.out.println(e);
243	} catch (Exception e) {
244	    e.printStackTrace();
245	    exit(1, "lookupKernelFunction before go");
246	}
247	try {
248	    consumer.lookupUserFunction(1, 1);
249	    exit(1, "lookupUserFunction before go");
250	} catch (IllegalStateException e) {
251	    System.out.println(e);
252	} catch (Exception e) {
253	    e.printStackTrace();
254	    exit(1, "lookupUserFunction before go");
255	}
256
257	// stop
258	try {
259	    consumer.stop();
260	    exit(1, "stop before go");
261	} catch (IllegalStateException e) {
262	    System.out.println(e);
263	} catch (Exception e) {
264	    e.printStackTrace();
265	    exit(1, "stop before go");
266	}
267    }
268
269    static void
270    afterGo(Consumer consumer, Program program)
271    {
272	System.out.println("after go");
273	printState(consumer);
274
275	// go
276	try {
277	    consumer.go();
278	    exit(1, "go after go");
279	} catch (IllegalStateException e) {
280	    System.out.println(e);
281	} catch (Exception e) {
282	    e.printStackTrace();
283	    exit(1, "go after go");
284	}
285
286	// createProcess, grabProcess
287	try {
288	    consumer.createProcess("date");
289	    exit(1, "createProcess after go");
290	} catch (IllegalStateException e) {
291	    System.out.println(e);
292	} catch (Exception e) {
293	    e.printStackTrace();
294	    exit(1, "createProcess after go");
295	}
296	try {
297	    consumer.grabProcess(1);
298	    exit(1, "grabProcess after go");
299	} catch (IllegalStateException e) {
300	    System.out.println(e);
301	} catch (Exception e) {
302	    e.printStackTrace();
303	    exit(1, "grabProcess after go");
304	}
305
306	// listProbes
307	try {
308	    consumer.listProbes(ProbeDescription.EMPTY);
309	    exit(1, "listProbes after go");
310	} catch (IllegalStateException e) {
311	    System.out.println(e);
312	} catch (Exception e) {
313	    e.printStackTrace();
314	    exit(1, "listProbes after go");
315	}
316
317	// compile
318	try {
319	    consumer.compile("syscall:::entry");
320	    exit(1, "compile after go");
321	} catch (IllegalStateException e) {
322	    System.out.println(e);
323	} catch (Exception e) {
324	    e.printStackTrace();
325	    exit(1, "compile after go");
326	}
327
328	// enable
329	try {
330	    consumer.enable();
331	    exit(1, "enable after go");
332	} catch (IllegalStateException e) {
333	    System.out.println(e);
334	} catch (Exception e) {
335	    e.printStackTrace();
336	    exit(1, "enable after go");
337	}
338
339	// getAggregate
340	try {
341	    consumer.getAggregate();
342	} catch (Exception e) {
343	    e.printStackTrace();
344	    exit(1, "getAggregate after go");
345	}
346
347	// getProgramInfo
348	try {
349	    consumer.getProgramInfo(program);
350	} catch (Exception e) {
351	    e.printStackTrace();
352	    exit(1, "getProgramInfo after go");
353	}
354
355	// getOption, setOption, unsetOption
356	try {
357	    consumer.getOption(Option.quiet);
358	    consumer.setOption(Option.quiet);
359	    consumer.unsetOption(Option.quiet);
360	} catch (Exception e) {
361	    e.printStackTrace();
362	    exit(1, "get, set, unset option after go");
363	}
364    }
365
366    static void
367    afterStop(Consumer consumer, Program program)
368    {
369	System.out.println("after stop");
370	printState(consumer);
371
372	// stop
373	try {
374	    consumer.stop();
375	    exit(1, "stop after stop");
376	} catch (IllegalStateException e) {
377	    System.out.println(e);
378	} catch (Exception e) {
379	    e.printStackTrace();
380	    exit(1, "stop after stop");
381	}
382
383	// getAggregate
384	try {
385	    consumer.getAggregate();
386	} catch (Exception e) {
387	    e.printStackTrace();
388	    exit(1, "getAggregate after stop");
389	}
390
391	// getProgramInfo
392	try {
393	    consumer.getProgramInfo(program);
394	} catch (Exception e) {
395	    e.printStackTrace();
396	    exit(1, "getProgramInfo after stop");
397	}
398
399	// getOption, setOption, unsetOption
400	try {
401	    consumer.getOption(Option.quiet);
402	    consumer.setOption(Option.quiet);
403	    consumer.unsetOption(Option.quiet);
404	} catch (Exception e) {
405	    e.printStackTrace();
406	    exit(1, "get, set, unset option after stop");
407	}
408    }
409
410    static void
411    afterClose(Consumer consumer, Program program)
412    {
413	System.out.println("after close");
414	printState(consumer);
415
416	// open
417	try {
418	    consumer.open();
419	    exit(1, "open after close");
420	} catch (IllegalStateException e) {
421	    System.out.println(e);
422	} catch (Exception e) {
423	    e.printStackTrace();
424	    exit(1, "open after close");
425	}
426
427	// compile
428	try {
429	    consumer.compile("syscall:::entry");
430	    exit(1, "compile after close");
431	} catch (IllegalStateException e) {
432	    System.out.println(e);
433	} catch (Exception e) {
434	    e.printStackTrace();
435	    exit(1, "compile after close");
436	}
437
438	// enable
439	try {
440	    consumer.enable();
441	    exit(1, "enable after close");
442	} catch (IllegalStateException e) {
443	    System.out.println(e);
444	} catch (Exception e) {
445	    e.printStackTrace();
446	    exit(1, "enable after close");
447	}
448
449	// getOption, setOption, unsetOption
450	try {
451	    consumer.getOption(Option.bufsize);
452	    exit(1, "getOption after close");
453	} catch (IllegalStateException e) {
454	    System.out.println(e);
455	} catch (Exception e) {
456	    e.printStackTrace();
457	    exit(1, "getOption after close");
458	}
459	try {
460	    consumer.setOption(Option.bufsize, Option.mb(1));
461	    exit(1, "setOption after close");
462	} catch (IllegalStateException e) {
463	    System.out.println(e);
464	} catch (Exception e) {
465	    e.printStackTrace();
466	    exit(1, "setOption after close");
467	}
468	try {
469	    consumer.unsetOption(Option.quiet);
470	    exit(1, "unsetOption after close");
471	} catch (IllegalStateException e) {
472	    System.out.println(e);
473	} catch (Exception e) {
474	    e.printStackTrace();
475	    exit(1, "unsetOption after close");
476	}
477
478	// createProcess, grabProcess
479	try {
480	    consumer.createProcess("date");
481	    exit(1, "createProcess after close");
482	} catch (IllegalStateException e) {
483	    System.out.println(e);
484	} catch (Exception e) {
485	    e.printStackTrace();
486	    exit(1, "createProcess after close");
487	}
488	try {
489	    consumer.grabProcess(1);
490	    exit(1, "grabProcess after close");
491	} catch (IllegalStateException e) {
492	    System.out.println(e);
493	} catch (Exception e) {
494	    e.printStackTrace();
495	    exit(1, "grabProcess after close");
496	}
497
498	// listProbes
499	try {
500	    consumer.listProbes(ProbeDescription.EMPTY);
501	    exit(1, "listProbes after close");
502	} catch (IllegalStateException e) {
503	    System.out.println(e);
504	} catch (Exception e) {
505	    e.printStackTrace();
506	    exit(1, "listProbes after close");
507	}
508
509	// getAggregate
510	try {
511	    consumer.getAggregate();
512	    exit(1, "getAggregate after close");
513	} catch (IllegalStateException e) {
514	    System.out.println(e);
515	} catch (Exception e) {
516	    e.printStackTrace();
517	    exit(1, "getAggregate after close");
518	}
519
520	// getVersion
521	try {
522	    consumer.getVersion(); // allowed
523	} catch (Exception e) {
524	    e.printStackTrace();
525	    exit(1, "getVersion after close");
526	}
527
528	// go
529	try {
530	    consumer.go();
531	    exit(1, "go after close");
532	} catch (IllegalStateException e) {
533	    System.out.println(e);
534	} catch (Exception e) {
535	    e.printStackTrace();
536	    exit(1, "go after close");
537	}
538
539	// lookupKernelFunction, lookupUserFunction
540	try {
541	    consumer.lookupKernelFunction(1);
542	    exit(1, "lookupKernelFunction after close");
543	} catch (IllegalStateException e) {
544	    System.out.println(e);
545	} catch (Exception e) {
546	    e.printStackTrace();
547	    exit(1, "lookupKernelFunction after close");
548	}
549	try {
550	    consumer.lookupUserFunction(1, 1);
551	    exit(1, "lookupUserFunction after close");
552	} catch (IllegalStateException e) {
553	    System.out.println(e);
554	} catch (Exception e) {
555	    e.printStackTrace();
556	    exit(1, "lookupUserFunction after close");
557	}
558
559	// stop
560	try {
561	    consumer.stop();
562	    exit(1, "stop after close");
563	} catch (IllegalStateException e) {
564	    System.out.println(e);
565	} catch (Exception e) {
566	    e.printStackTrace();
567	    exit(1, "stop after close");
568	}
569
570	// getProgramInfo
571	try {
572	    consumer.getProgramInfo(program);
573	    exit(1, "getProgramInfo after close");
574	} catch (IllegalStateException e) {
575	    System.out.println(e);
576	} catch (Exception e) {
577	    e.printStackTrace();
578	    exit(1, "getProgramInfo after close");
579	}
580    }
581
582    public static void
583    main(String[] args)
584    {
585	final Consumer consumer = new LocalConsumer();
586	consumer.addConsumerListener(new ConsumerAdapter() {
587	    public void consumerStarted(ConsumerEvent e) {
588		System.out.println("consumerStarted, running: " +
589			consumer.isRunning());
590		afterGo(consumer, program);
591	    }
592	    public void consumerStopped(ConsumerEvent e) {
593		System.out.println("consumerStopped, running: " +
594			consumer.isRunning());
595	    }
596	});
597
598	try {
599	    beforeOpen(consumer);
600	    consumer.open();
601	    beforeCompile(consumer);
602	    program = consumer.compile(
603		    "syscall:::entry { @[execname] = count(); } " +
604		    "tick-101ms { printa(@); }");
605	    beforeEnable(consumer);
606	    consumer.enable();
607	    beforeGo(consumer);
608	    System.out.println("before go, running: " + consumer.isRunning());
609	    consumer.go();
610	    // Avoid race, call afterGo() in ConsumerListener
611	    try {
612		Thread.currentThread().sleep(300);
613	    } catch (InterruptedException e) {
614		e.printStackTrace();
615		exit(1);
616	    }
617	    consumer.stop();
618	    System.out.println("after stop, running: " + consumer.isRunning());
619	    afterStop(consumer, program);
620	    consumer.close();
621	    afterClose(consumer, program);
622	} catch (DTraceException e) {
623	    e.printStackTrace();
624	    exit(1);
625	}
626    }
627}
628