TestFunctionLookup.java revision 178476
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 for bug 6413280 lookupKernelFunction() and
33 * lookupUserFunction() truncate last character.
34 */
35public class TestFunctionLookup {
36    static final String kernelLookupProgram = "sdt:::callout-start { " +
37           "@[((callout_t *)arg0)->c_func] = count(); }";
38    static final String userLookupProgram = "pid$target::f2:entry { " +
39           "@[arg0] = count(); }";
40
41    public static void
42    main(String[] args)
43    {
44	if (args.length != 1) {
45	    System.err.println("usage: java TestFunctionLookup <command>");
46	    System.exit(1);
47	}
48	String cmd = args[0];
49
50	Consumer consumer = new LocalConsumer();
51	try {
52	    consumer.open();
53	    consumer.compile(kernelLookupProgram);
54	    consumer.enable();
55	    consumer.go();
56	    Aggregate a;
57	    Number address;
58	    String f;
59	    boolean done = false;
60	    for (int i = 0; (i < 20) && !done; ++i) {
61		Thread.currentThread().sleep(100);
62		a = consumer.getAggregate();
63		for (Aggregation agg : a.getAggregations()) {
64		    for (Tuple tuple : agg.asMap().keySet()) {
65			address = (Number)tuple.get(0).getValue();
66			if (address instanceof Integer) {
67			    int addr = (Integer)address;
68			    f = consumer.lookupKernelFunction(addr);
69			} else {
70			    long addr = (Long)address;
71			    f = consumer.lookupKernelFunction(addr);
72			}
73			if (f.equals("genunix`setrun")) {
74			    System.out.println(f);
75			    done = true;
76			}
77		    }
78		}
79	    }
80	    consumer.close();
81	} catch (Exception e) {
82	    e.printStackTrace();
83	    System.exit(1);
84	}
85
86	consumer = new LocalConsumer();
87	try {
88	    consumer.open();
89	    int pid = consumer.createProcess(cmd);
90	    consumer.compile(userLookupProgram);
91	    consumer.enable();
92	    consumer.go();
93	    Thread.currentThread().sleep(500);
94	    Aggregate a = consumer.getAggregate();
95	    Number address;
96	    String f;
97	    for (Aggregation agg : a.getAggregations()) {
98		for (Tuple tuple : agg.asMap().keySet()) {
99		    address = (Number)tuple.get(0).getValue();
100		    if (address instanceof Integer) {
101			int addr = (Integer)address;
102			f = consumer.lookupUserFunction(pid, addr);
103		    } else {
104			long addr = (Long)address;
105			f = consumer.lookupUserFunction(pid, addr);
106		    }
107		    System.out.println(f);
108		}
109	    }
110	    consumer.close();
111	} catch (Exception e) {
112	    e.printStackTrace();
113	    System.exit(1);
114	}
115    }
116}
117