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/*
23210767Srpaulo * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24178476Sjb * Use is subject to license terms.
25178476Sjb */
26178476Sjb
27178476Sjbimport org.opensolaris.os.dtrace.*;
28178476Sjb
29178476Sjb/**
30178476Sjb * Regression for bug 6413280 lookupKernelFunction() and
31178476Sjb * lookupUserFunction() truncate last character.
32178476Sjb */
33178476Sjbpublic class TestFunctionLookup {
34178476Sjb    static final String kernelLookupProgram = "sdt:::callout-start { " +
35178476Sjb           "@[((callout_t *)arg0)->c_func] = count(); }";
36178476Sjb    static final String userLookupProgram = "pid$target::f2:entry { " +
37178476Sjb           "@[arg0] = count(); }";
38178476Sjb
39178476Sjb    public static void
40178476Sjb    main(String[] args)
41178476Sjb    {
42178476Sjb	if (args.length != 1) {
43178476Sjb	    System.err.println("usage: java TestFunctionLookup <command>");
44178476Sjb	    System.exit(1);
45178476Sjb	}
46178476Sjb	String cmd = args[0];
47178476Sjb
48178476Sjb	Consumer consumer = new LocalConsumer();
49178476Sjb	try {
50178476Sjb	    consumer.open();
51178476Sjb	    consumer.compile(kernelLookupProgram);
52178476Sjb	    consumer.enable();
53178476Sjb	    consumer.go();
54178476Sjb	    Aggregate a;
55178476Sjb	    Number address;
56178476Sjb	    String f;
57178476Sjb	    boolean done = false;
58178476Sjb	    for (int i = 0; (i < 20) && !done; ++i) {
59178476Sjb		Thread.currentThread().sleep(100);
60178476Sjb		a = consumer.getAggregate();
61178476Sjb		for (Aggregation agg : a.getAggregations()) {
62178476Sjb		    for (Tuple tuple : agg.asMap().keySet()) {
63178476Sjb			address = (Number)tuple.get(0).getValue();
64178476Sjb			if (address instanceof Integer) {
65178476Sjb			    int addr = (Integer)address;
66178476Sjb			    f = consumer.lookupKernelFunction(addr);
67178476Sjb			} else {
68178476Sjb			    long addr = (Long)address;
69178476Sjb			    f = consumer.lookupKernelFunction(addr);
70178476Sjb			}
71210767Srpaulo			if (f.equals("genunix`cv_wakeup")) {
72178476Sjb			    System.out.println(f);
73178476Sjb			    done = true;
74178476Sjb			}
75178476Sjb		    }
76178476Sjb		}
77178476Sjb	    }
78178476Sjb	    consumer.close();
79178476Sjb	} catch (Exception e) {
80178476Sjb	    e.printStackTrace();
81178476Sjb	    System.exit(1);
82178476Sjb	}
83178476Sjb
84178476Sjb	consumer = new LocalConsumer();
85178476Sjb	try {
86178476Sjb	    consumer.open();
87178476Sjb	    int pid = consumer.createProcess(cmd);
88178476Sjb	    consumer.compile(userLookupProgram);
89178476Sjb	    consumer.enable();
90178476Sjb	    consumer.go();
91178476Sjb	    Thread.currentThread().sleep(500);
92178476Sjb	    Aggregate a = consumer.getAggregate();
93178476Sjb	    Number address;
94178476Sjb	    String f;
95178476Sjb	    for (Aggregation agg : a.getAggregations()) {
96178476Sjb		for (Tuple tuple : agg.asMap().keySet()) {
97178476Sjb		    address = (Number)tuple.get(0).getValue();
98178476Sjb		    if (address instanceof Integer) {
99178476Sjb			int addr = (Integer)address;
100178476Sjb			f = consumer.lookupUserFunction(pid, addr);
101178476Sjb		    } else {
102178476Sjb			long addr = (Long)address;
103178476Sjb			f = consumer.lookupUserFunction(pid, addr);
104178476Sjb		    }
105178476Sjb		    System.out.println(f);
106178476Sjb		}
107178476Sjb	    }
108178476Sjb	    consumer.close();
109178476Sjb	} catch (Exception e) {
110178476Sjb	    e.printStackTrace();
111178476Sjb	    System.exit(1);
112178476Sjb	}
113178476Sjb    }
114178476Sjb}
115