1178476Sjb#!/bin/ksh -p
2178476Sjb#
3178476Sjb# CDDL HEADER START
4178476Sjb#
5178476Sjb# The contents of this file are subject to the terms of the
6178476Sjb# Common Development and Distribution License (the "License").
7178476Sjb# You may not use this file except in compliance with the License.
8178476Sjb#
9178476Sjb# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10178476Sjb# or http://www.opensolaris.org/os/licensing.
11178476Sjb# See the License for the specific language governing permissions
12178476Sjb# and limitations under the License.
13178476Sjb#
14178476Sjb# When distributing Covered Code, include this CDDL HEADER in each
15178476Sjb# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16178476Sjb# If applicable, add the following below this CDDL HEADER, with the
17178476Sjb# fields enclosed by brackets "[]" replaced with your own identifying
18178476Sjb# information: Portions Copyright [yyyy] [name of copyright owner]
19178476Sjb#
20178476Sjb# CDDL HEADER END
21178476Sjb#
22178476Sjb
23178476Sjb#
24178476Sjb# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
25178476Sjb# Use is subject to license terms.
26178476Sjb#
27178476Sjb# ident	"%Z%%M%	%I%	%E% SMI"
28178476Sjb
29178476Sjb#
30178476Sjb# This test verifies that USDT probes will be picked up after a dlopen(3C)
31178476Sjb# when a regex in the provider name matches both USDT probes and pid probes
32178476Sjb# (e.g., p*d$target matches both pid$target and pyramid$target.)
33178476Sjb#
34178476Sjb
35178476Sjbif [ $# != 1 ]; then
36178476Sjb	echo expected one argument: '<'dtrace-path'>'
37178476Sjb	exit 2
38178476Sjbfi
39178476Sjb
40178476Sjbdtrace=$1
41178476SjbDIR=${TMPDIR:-/tmp}/dtest.$$
42178476Sjb
43178476Sjbmkdir $DIR
44178476Sjbcd $DIR
45178476Sjb
46178476Sjbcat > Makefile <<EOF
47178476Sjball: main altlib.so
48178476Sjb
49178476Sjbmain: main.o provmain.o
50178476Sjb	cc -o main main.o provmain.o
51178476Sjb
52178476Sjbmain.o: main.c prov.h
53178476Sjb	cc -c main.c
54178476Sjb
55178476Sjbprov.h: prov.d
56178476Sjb	$dtrace -h -s prov.d
57178476Sjb
58178476Sjbprovmain.o: prov.d main.o
59178476Sjb	$dtrace -G -32 -o provmain.o -s prov.d main.o
60178476Sjb
61178476Sjbaltlib.so: altlib.o provalt.o
62178476Sjb	cc -z defs -G -o altlib.so altlib.o provalt.o -lc
63178476Sjb
64178476Sjbaltlib.o: altlib.c prov.h
65178476Sjb	cc -c altlib.c
66178476Sjb
67178476Sjbprovalt.o: prov.d altlib.o
68178476Sjb	$dtrace -G -32 -o provalt.o -s prov.d altlib.o
69178476SjbEOF
70178476Sjb
71178476Sjbcat > prov.d <<EOF
72178476Sjbprovider pyramid {
73178476Sjb	probe entry();
74178476Sjb};
75178476SjbEOF
76178476Sjb
77178476Sjbcat > altlib.c <<EOF
78178476Sjb#include <sys/sdt.h>
79178476Sjb#include "prov.h"
80178476Sjb
81178476Sjbvoid
82178476Sjbgo(void)
83178476Sjb{
84178476Sjb	PYRAMID_ENTRY();
85178476Sjb}
86178476SjbEOF
87178476Sjb
88178476Sjbcat > main.c <<EOF
89178476Sjb#include <dlfcn.h>
90178476Sjb#include <unistd.h>
91178476Sjb#include <stdio.h>
92178476Sjb#include <sys/sdt.h>
93178476Sjb#include "prov.h"
94178476Sjb
95178476Sjbvoid
96178476Sjbgo(void)
97178476Sjb{
98178476Sjb	PYRAMID_ENTRY();
99178476Sjb}
100178476Sjb
101178476Sjbint
102178476Sjbmain(int argc, char **argv)
103178476Sjb{
104178476Sjb	void *alt;
105178476Sjb	void *alt_go;
106178476Sjb
107178476Sjb	go();
108178476Sjb
109178476Sjb	if ((alt = dlopen("./altlib.so", RTLD_LAZY | RTLD_LOCAL)) 
110178476Sjb	    == NULL) {
111178476Sjb		printf("dlopen of altlib.so failed: %s\n", dlerror());
112178476Sjb		return (1);
113178476Sjb	}
114178476Sjb
115178476Sjb	if ((alt_go = dlsym(alt, "go")) == NULL) {
116178476Sjb		printf("failed to lookup 'go' in altlib.so\n");
117178476Sjb		return (1);
118178476Sjb	}
119178476Sjb
120178476Sjb	((void (*)(void))alt_go)();
121178476Sjb
122178476Sjb	return (0);
123178476Sjb}
124178476SjbEOF
125178476Sjb
126178476Sjbmake > /dev/null
127178476Sjbif [ $? -ne 0 ]; then
128178476Sjb	print -u2 "failed to build"
129178476Sjb	exit 1
130178476Sjbfi
131178476Sjb
132178476Sjbcat > main.d <<'EOF'
133178476Sjbp*d$target::go:entry
134178476Sjb{
135178476Sjb	@foo[probemod, probefunc, probename] = count();
136178476Sjb}
137178476Sjb
138178476SjbEND
139178476Sjb{
140178476Sjb	printa("%s:%s:%s %@u\n",@foo);
141178476Sjb}
142178476SjbEOF
143178476Sjb
144178476Sjbscript() {
145178476Sjb	$dtrace -q -s ./main.d -c ./main
146178476Sjb}
147178476Sjb
148178476Sjbscript
149178476Sjbstatus=$?
150178476Sjb
151178476Sjbcd /tmp
152211545Srpaulo/bin/rm -rf $DIR
153178476Sjb
154178476Sjbexit $status
155