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