xref: /illumos-gate/usr/src/cmd/dtrace/test/tst/common/usdt/tst.dlclose1.ksh (revision 1fceb383a3f0b59711832b9dc4e8329d7f216604)
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 2007 Sun Microsystems, Inc.  All rights reserved.
25# Use is subject to license terms.
26#
27# ident	"%Z%%M%	%I%	%E% SMI"
28
29#
30# This test verifies that USDT providers are removed when its associated
31# load object is closed via dlclose(3dl).
32#
33
34if [ $# != 1 ]; then
35	echo expected one argument: '<'dtrace-path'>'
36	exit 2
37fi
38
39dtrace=$1
40DIR=/var/tmp/dtest.$$
41
42mkdir $DIR
43cd $DIR
44
45cat > Makefile <<EOF
46all: main livelib.so deadlib.so
47
48main: main.o prov.o
49	cc -o main main.o
50
51main.o: main.c
52	cc -c main.c
53
54
55livelib.so: livelib.o prov.o
56	cc -z defs -G -o livelib.so livelib.o prov.o -lc
57
58livelib.o: livelib.c prov.h
59	cc -c livelib.c
60
61prov.o: livelib.o prov.d
62	$dtrace -G -s prov.d livelib.o
63
64prov.h: prov.d
65	$dtrace -h -s prov.d
66
67
68deadlib.so: deadlib.o
69	cc -z defs -G -o deadlib.so deadlib.o -lc
70
71deadlib.o: deadlib.c
72	cc -c deadlib.c
73
74clean:
75	rm -f main.o livelib.o prov.o prov.h deadlib.o
76
77clobber: clean
78	rm -f main livelib.so deadlib.so
79EOF
80
81cat > prov.d <<EOF
82provider test_prov {
83	probe go();
84};
85EOF
86
87cat > livelib.c <<EOF
88#include "prov.h"
89
90void
91go(void)
92{
93	TEST_PROV_GO();
94}
95EOF
96
97cat > deadlib.c <<EOF
98void
99go(void)
100{
101}
102EOF
103
104
105cat > main.c <<EOF
106#include <dlfcn.h>
107#include <unistd.h>
108#include <stdio.h>
109
110int
111main(int argc, char **argv)
112{
113	void *live;
114
115	if ((live = dlopen("./livelib.so", RTLD_LAZY | RTLD_LOCAL)) == NULL) {
116		printf("dlopen of livelib.so failed: %s\n", dlerror());
117		return (1);
118	}
119
120	(void) dlclose(live);
121
122	pause();
123
124	return (0);
125}
126EOF
127
128/usr/ccs/bin/make > /dev/null
129if [ $? -ne 0 ]; then
130	print -u2 "failed to build"
131	exit 1
132fi
133
134script() {
135	$dtrace -w -x bufsize=1k -c ./main -qs /dev/stdin <<EOF
136	syscall::pause:entry
137	/pid == \$target/
138	{
139		system("$dtrace -l -P test_prov*");
140		system("kill %d", \$target);
141		exit(0);
142	}
143
144	tick-1s
145	/i++ == 5/
146	{
147		printf("failed\n");
148		exit(1);
149	}
150EOF
151}
152
153script 2>&1
154status=$?
155
156cd /
157/usr/bin/rm -rf $DIR
158
159exit $status
160