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 2006 Sun Microsystems, Inc.  All rights reserved.
25178476Sjb# Use is subject to license terms.
26178476Sjb#
27178476Sjb#ident	"%Z%%M%	%I%	%E% SMI"
28178476Sjb
29178476Sjbif [ $# != 1 ]; then
30178476Sjb	echo expected one argument: '<'dtrace-path'>'
31178476Sjb	exit 2
32178476Sjbfi
33178476Sjb
34178476Sjbdtrace=$1
35178476SjbDIR=/var/tmp/dtest.$$
36178476Sjb
37178476Sjbmkdir $DIR
38178476Sjbcd $DIR
39178476Sjb
40178476Sjbcat > Makefile <<EOF
41178476Sjball: main livelib.so deadlib.so
42178476Sjb
43178476Sjbmain: main.o prov.o
44178476Sjb	cc -o main main.o
45178476Sjb
46178476Sjbmain.o: main.c
47178476Sjb	cc -c main.c
48178476Sjb
49178476Sjb
50178476Sjblivelib.so: livelib.o prov.o
51279417Smarkj	cc -shared -o livelib.so livelib.o prov.o -lc
52178476Sjb
53178476Sjblivelib.o: livelib.c prov.h
54178476Sjb	cc -c livelib.c
55178476Sjb
56178476Sjbprov.o: livelib.o prov.d
57178476Sjb	$dtrace -G -s prov.d livelib.o
58178476Sjb
59178476Sjbprov.h: prov.d
60178476Sjb	$dtrace -h -s prov.d
61178476Sjb
62178476Sjb
63178476Sjbdeadlib.so: deadlib.o
64279417Smarkj	cc -shared -o deadlib.so deadlib.o -lc
65178476Sjb
66178476Sjbdeadlib.o: deadlib.c
67178476Sjb	cc -c deadlib.c
68178476Sjb
69178476Sjbclean:
70178476Sjb	rm -f main.o livelib.o prov.o prov.h deadlib.o
71178476Sjb
72178476Sjbclobber: clean
73178476Sjb	rm -f main livelib.so deadlib.so
74178476SjbEOF
75178476Sjb
76178476Sjbcat > prov.d <<EOF
77178476Sjbprovider test_prov {
78178476Sjb	probe go();
79178476Sjb};
80178476SjbEOF
81178476Sjb
82178476Sjbcat > livelib.c <<EOF
83178476Sjb#include "prov.h"
84178476Sjb
85178476Sjbvoid
86178476Sjbgo(void)
87178476Sjb{
88178476Sjb	TEST_PROV_GO();
89178476Sjb}
90178476SjbEOF
91178476Sjb
92178476Sjbcat > deadlib.c <<EOF
93178476Sjbvoid
94178476Sjbgo(void)
95178476Sjb{
96178476Sjb}
97178476SjbEOF
98178476Sjb
99178476Sjb
100178476Sjbcat > main.c <<EOF
101178476Sjb#include <dlfcn.h>
102178476Sjb#include <unistd.h>
103178476Sjb#include <stdio.h>
104178476Sjb
105178476Sjbint
106178476Sjbmain(int argc, char **argv)
107178476Sjb{
108178476Sjb	void *live, *dead;
109178476Sjb	void *go;
110178476Sjb
111178476Sjb	if ((live = dlopen("./livelib.so", RTLD_LAZY | RTLD_LOCAL)) == NULL) {
112178476Sjb		printf("dlopen of livelib.so failed: %s\n", dlerror());
113178476Sjb		return (1);
114178476Sjb	}
115178476Sjb
116178476Sjb	(void) dlclose(live);
117178476Sjb
118178476Sjb	if ((dead = dlopen("./deadlib.so", RTLD_LAZY | RTLD_LOCAL)) == NULL) {
119178476Sjb		printf("dlopen of deadlib.so failed: %s\n", dlerror());
120178476Sjb		return (1);
121178476Sjb	}
122178476Sjb
123178476Sjb	if ((live = dlopen("./livelib.so", RTLD_LAZY | RTLD_LOCAL)) == NULL) {
124178476Sjb		printf("dlopen of livelib.so failed: %s\n", dlerror());
125178476Sjb		return (1);
126178476Sjb	}
127178476Sjb
128178476Sjb	if ((go = dlsym(live, "go")) == NULL) {
129178476Sjb		printf("failed to lookup 'go' in livelib.so\n");
130178476Sjb		return (1);
131178476Sjb	}
132178476Sjb
133178476Sjb	((void (*)(void))go)();
134178476Sjb
135178476Sjb	return (0);
136178476Sjb}
137178476SjbEOF
138178476Sjb
139211545Srpaulo/usr/bin/make > /dev/null
140178476Sjbif [ $? -ne 0 ]; then
141178476Sjb	print -u2 "failed to build"
142178476Sjb	exit 1
143178476Sjbfi
144178476Sjb
145178476Sjbscript() {
146178476Sjb	$dtrace -w -c ./main -Zqs /dev/stdin <<EOF
147178476Sjb	test_prov*:::
148178476Sjb	{
149178476Sjb		printf("%s:%s:%s\n", probemod, probefunc, probename);
150178476Sjb	}
151178476SjbEOF
152178476Sjb}
153178476Sjb
154178476Sjbscript
155178476Sjbstatus=$?
156178476Sjb
157178476Sjbcd /
158211545Srpaulo/bin/rm -rf $DIR
159178476Sjb
160178476Sjbexit $status
161