xref: /illumos-gate/usr/src/cmd/dtrace/test/tst/common/usdt/tst.dlclose2.ksh (revision 13485e69b5429e6c7d27301fb3c0deee0e93768a)
1#!/bin/ksh -p
2#
3# CDDL HEADER START
4#
5# The contents of this file are subject to the terms of the
6# Common Development and Distribution License (the "License").
7# You may not use this file except in compliance with the License.
8#
9# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10# or http://www.opensolaris.org/os/licensing.
11# See the License for the specific language governing permissions
12# and limitations under the License.
13#
14# When distributing Covered Code, include this CDDL HEADER in each
15# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16# If applicable, add the following below this CDDL HEADER, with the
17# fields enclosed by brackets "[]" replaced with your own identifying
18# information: Portions Copyright [yyyy] [name of copyright owner]
19#
20# CDDL HEADER END
21#
22
23#
24# Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
25# Use is subject to license terms.
26#
27
28if [ $# != 1 ]; then
29	echo expected one argument: '<'dtrace-path'>'
30	exit 2
31fi
32
33dtrace=$1
34DIR=/var/tmp/dtest.$$
35
36mkdir $DIR
37cd $DIR
38
39cat > Makefile <<EOF
40all: main livelib.so deadlib.so
41
42main: main.o prov.o
43	gcc -m32 -o main main.o
44
45main.o: main.c
46	gcc -m32 -c main.c
47
48
49livelib.so: livelib.o prov.o
50	gcc -m32 -shared -o livelib.so livelib.o prov.o -lc
51
52livelib.o: livelib.c prov.h
53	gcc -m32 -fPIC -c livelib.c
54
55prov.o: livelib.o prov.d
56	$dtrace -G -s prov.d livelib.o
57
58prov.h: prov.d
59	$dtrace -h -s prov.d
60
61
62deadlib.so: deadlib.o
63	gcc -m32 -shared -o deadlib.so deadlib.o -lc
64
65deadlib.o: deadlib.c
66	gcc -m32 -fPIC -c deadlib.c
67
68clean:
69	rm -f main.o livelib.o prov.o prov.h deadlib.o
70
71clobber: clean
72	rm -f main livelib.so deadlib.so
73EOF
74
75cat > prov.d <<EOF
76provider test_prov {
77	probe go();
78};
79EOF
80
81cat > livelib.c <<EOF
82#include "prov.h"
83
84void
85go(void)
86{
87	TEST_PROV_GO();
88}
89EOF
90
91cat > deadlib.c <<EOF
92void
93go(void)
94{
95}
96EOF
97
98
99cat > main.c <<EOF
100#include <dlfcn.h>
101#include <unistd.h>
102#include <stdio.h>
103
104int
105main(int argc, char **argv)
106{
107	void *live, *dead;
108	void *go;
109
110	if ((live = dlopen("./livelib.so", RTLD_LAZY | RTLD_LOCAL)) == NULL) {
111		printf("dlopen of livelib.so failed: %s\n", dlerror());
112		return (1);
113	}
114
115	(void) dlclose(live);
116
117	if ((dead = dlopen("./deadlib.so", RTLD_LAZY | RTLD_LOCAL)) == NULL) {
118		printf("dlopen of deadlib.so failed: %s\n", dlerror());
119		return (1);
120	}
121
122	if ((live = dlopen("./livelib.so", RTLD_LAZY | RTLD_LOCAL)) == NULL) {
123		printf("dlopen of livelib.so failed: %s\n", dlerror());
124		return (1);
125	}
126
127	if ((go = dlsym(live, "go")) == NULL) {
128		printf("failed to lookup 'go' in livelib.so\n");
129		return (1);
130	}
131
132	((void (*)(void))go)();
133
134	return (0);
135}
136EOF
137
138make > /dev/null
139if [ $? -ne 0 ]; then
140	print -u2 "failed to build"
141	exit 1
142fi
143
144script() {
145	$dtrace -w -c ./main -Zqs /dev/stdin <<EOF
146	test_prov*:::
147	{
148		printf("%s:%s:%s\n", probemod, probefunc, probename);
149	}
150EOF
151}
152
153script
154status=$?
155
156cd /
157/usr/bin/rm -rf $DIR
158
159exit $status
160