11541Srgrimes#!/bin/ksh -p
21541Srgrimes#
31541Srgrimes# CDDL HEADER START
41541Srgrimes#
524440Speter# The contents of this file are subject to the terms of the
61541Srgrimes# Common Development and Distribution License (the "License").
71541Srgrimes# You may not use this file except in compliance with the License.
81541Srgrimes#
91541Srgrimes# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
101541Srgrimes# or http://www.opensolaris.org/os/licensing.
111541Srgrimes# See the License for the specific language governing permissions
121541Srgrimes# and limitations under the License.
131541Srgrimes#
141541Srgrimes# When distributing Covered Code, include this CDDL HEADER in each
151541Srgrimes# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
161541Srgrimes# If applicable, add the following below this CDDL HEADER, with the
171541Srgrimes# fields enclosed by brackets "[]" replaced with your own identifying
181541Srgrimes# information: Portions Copyright [yyyy] [name of copyright owner]
191541Srgrimes#
201541Srgrimes# CDDL HEADER END
211541Srgrimes#
221541Srgrimes
231541Srgrimes#
241541Srgrimes# Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
251541Srgrimes# Use is subject to license terms.
261541Srgrimes#
271541Srgrimes# ident	"%Z%%M%	%I%	%E% SMI"
281541Srgrimes
291541Srgrimes#
301541Srgrimes# This test verifies that USDT providers are removed when its associated
311541Srgrimes# load object is closed via dlclose(3dl).
321541Srgrimes#
331541Srgrimes
341541Srgrimesif [ $# != 1 ]; then
351541Srgrimes	echo expected one argument: '<'dtrace-path'>'
361541Srgrimes	exit 2
371541Srgrimesfi
381541Srgrimes
391541Srgrimesdtrace=$1
401541SrgrimesDIR=/var/tmp/dtest.$$
411541Srgrimes
421541Srgrimesmkdir $DIR
431541Srgrimescd $DIR
441541Srgrimes
451541Srgrimescat > Makefile <<EOF
461541Srgrimesall: main livelib.so deadlib.so
471541Srgrimes
481541Srgrimesmain: main.o prov.o
491541Srgrimes	cc -o main main.o
501541Srgrimes
511541Srgrimesmain.o: main.c
521541Srgrimes	cc -c main.c
531541Srgrimes
541541Srgrimes
551541Srgrimeslivelib.so: livelib.o prov.o
561541Srgrimes	cc -z defs -G -o livelib.so livelib.o prov.o -lc
571541Srgrimes
581541Srgrimeslivelib.o: livelib.c prov.h
591541Srgrimes	cc -c livelib.c
601541Srgrimes
611541Srgrimesprov.o: livelib.o prov.d
621541Srgrimes	$dtrace -G -s prov.d livelib.o
631541Srgrimes
641541Srgrimesprov.h: prov.d
651541Srgrimes	$dtrace -h -s prov.d
661541Srgrimes
671541Srgrimes
681541Srgrimesdeadlib.so: deadlib.o
691541Srgrimes	cc -z defs -G -o deadlib.so deadlib.o -lc
701541Srgrimes
711541Srgrimesdeadlib.o: deadlib.c
721541Srgrimes	cc -c deadlib.c
731541Srgrimes
741541Srgrimesclean:
751541Srgrimes	rm -f main.o livelib.o prov.o prov.h deadlib.o
761541Srgrimes
771541Srgrimesclobber: clean
781541Srgrimes	rm -f main livelib.so deadlib.so
791541SrgrimesEOF
801541Srgrimes
811541Srgrimescat > prov.d <<EOF
821541Srgrimesprovider test_prov {
831541Srgrimes	probe go();
841541Srgrimes};
851541SrgrimesEOF
861541Srgrimes
871541Srgrimescat > livelib.c <<EOF
881541Srgrimes#include "prov.h"
891541Srgrimes
901541Srgrimesvoid
911541Srgrimesgo(void)
921541Srgrimes{
931541Srgrimes	TEST_PROV_GO();
941541Srgrimes}
951541SrgrimesEOF
961541Srgrimes
971541Srgrimescat > deadlib.c <<EOF
981541Srgrimesvoid
991541Srgrimesgo(void)
1001541Srgrimes{
1011541Srgrimes}
1021541SrgrimesEOF
1031541Srgrimes
1041541Srgrimes
1051541Srgrimescat > main.c <<EOF
1061541Srgrimes#include <dlfcn.h>
1071541Srgrimes#include <unistd.h>
1081541Srgrimes#include <stdio.h>
1091541Srgrimes
1101541Srgrimesint
1111541Srgrimesmain(int argc, char **argv)
1121541Srgrimes{
1131541Srgrimes	void *live;
1141541Srgrimes
1151541Srgrimes	if ((live = dlopen("./livelib.so", RTLD_LAZY | RTLD_LOCAL)) == NULL) {
1161541Srgrimes		printf("dlopen of livelib.so failed: %s\n", dlerror());
1171541Srgrimes		return (1);
1181541Srgrimes	}
1191541Srgrimes
1201541Srgrimes	(void) dlclose(live);
1211541Srgrimes
1221541Srgrimes	pause();
1231541Srgrimes
1241541Srgrimes	return (0);
1251541Srgrimes}
1261541SrgrimesEOF
1271541Srgrimes
12814220Speter/usr/bin/make > /dev/null
1291541Srgrimesif [ $? -ne 0 ]; then
1301541Srgrimes	print -u2 "failed to build"
1311541Srgrimes	exit 1
1321541Srgrimesfi
1331541Srgrimes
1341541Srgrimesscript() {
1358019Sache	$dtrace -w -x bufsize=1k -c ./main -qs /dev/stdin <<EOF
1368019Sache	syscall::pause:entry
1371541Srgrimes	/pid == \$target/
1381541Srgrimes	{
1391541Srgrimes		system("$dtrace -l -P test_prov*");
1401541Srgrimes		system("kill %d", \$target);
1411541Srgrimes		exit(0);
1421541Srgrimes	}
1431541Srgrimes
1441541Srgrimes	tick-1s
1451541Srgrimes	/i++ == 5/
1461541Srgrimes	{
1471541Srgrimes		printf("failed\n");
1481541Srgrimes		exit(1);
1491541Srgrimes	}
1501541SrgrimesEOF
1511541Srgrimes}
1521541Srgrimes
1531541Srgrimesscript 2>&1
1541541Srgrimesstatus=$?
1551541Srgrimes
1561541Srgrimescd /
1571541Srgrimes/bin/rm -rf $DIR
1581541Srgrimes
1591541Srgrimesexit $status
16014220Speter