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 
29 import java.util.*;
30 import org.opensolaris.os.dtrace.*;
31 
32 /**
33  * Regression test verifies that ProbeData instances sort as expected
34  * with multiple enabled probe IDs and multiple records including byte
35  * array (sorted as unsigned bytes), stand-alone ufunc() action, and
36  * signed integer.
37  */
38 public class TestProbeData {
39     public static final String PROGRAM =
40 	    "pid$target::fN:entry\n" +
41 	    "{\n" +
42 	    "        tracemem(copyin(arg1, 6), 6);\n" +
43 	    "        ufunc(arg0);\n" +
44 	    "        trace((int)arg2);\n" +
45 	    "}" +
46 	    "" +
47 	    "pid$target::fN2:entry\n" +
48 	    "{\n" +
49 	    "        tracemem(copyin(arg1, 6), 6);\n" +
50 	    "        ufunc(arg0);\n" +
51 	    "        trace((int)arg2);\n" +
52 	    "}";
53 
54     static String
getString(ProbeData p)55     getString(ProbeData p)
56     {
57 	StringBuilder buf = new StringBuilder();
58 	buf.append("[probe ");
59 	buf.append(p.getEnabledProbeID());
60 	buf.append(' ');
61 	ProbeDescription d = p.getEnabledProbeDescription();
62 	buf.append("pid$target");
63 	buf.append(':');
64 	buf.append(d.getModule());
65 	buf.append(':');
66 	buf.append(d.getFunction());
67 	buf.append(':');
68 	buf.append(d.getName());
69 	buf.append(' ');
70 	buf.append(p.getRecords());
71 	buf.append("]");
72 	return buf.toString();
73     }
74 
75     public static void
main(String[] args)76     main(String[] args)
77     {
78 	if (args.length != 1) {
79 	    System.err.println("usage: java TestProbedata <command>");
80 	    System.exit(2);
81 	}
82 
83 	String command = args[0];
84 	final Consumer consumer = new LocalConsumer();
85 	final List <ProbeData> list = new ArrayList <ProbeData> ();
86 	consumer.addConsumerListener(new ConsumerAdapter() {
87 	    public void dataReceived(DataEvent e) {
88 		list.add(e.getProbeData());
89 	    }
90 	    public void consumerStopped(ConsumerEvent e) {
91 		Collections.sort(list);
92 		for (ProbeData p : list) {
93 		    System.out.println(getString(p));
94 		    System.out.println();
95 		}
96 		consumer.close();
97 	    }
98 	});
99 
100 	try {
101 	    consumer.open();
102 	    consumer.createProcess(command);
103 	    consumer.compile(PROGRAM);
104 	    consumer.enable();
105 	    consumer.go();
106 	} catch (DTraceException e) {
107 	    e.printStackTrace();
108 	}
109     }
110 }
111