]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/cddl/dev/dtrace/dtrace_unload.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / cddl / dev / dtrace / dtrace_unload.c
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  *
21  * $FreeBSD$
22  *
23  */
24
25 static int
26 dtrace_unload()
27 {
28         dtrace_state_t *state;
29         int error = 0;
30
31         destroy_dev(dtrace_dev);
32
33         mutex_enter(&dtrace_provider_lock);
34         mutex_enter(&dtrace_lock);
35         mutex_enter(&cpu_lock);
36
37         ASSERT(dtrace_opens == 0);
38
39         if (dtrace_helpers > 0) {
40                 mutex_exit(&cpu_lock);
41                 mutex_exit(&dtrace_lock);
42                 mutex_exit(&dtrace_provider_lock);
43                 return (EBUSY);
44         }
45
46         if (dtrace_unregister((dtrace_provider_id_t)dtrace_provider) != 0) {
47                 mutex_exit(&cpu_lock);
48                 mutex_exit(&dtrace_lock);
49                 mutex_exit(&dtrace_provider_lock);
50                 return (EBUSY);
51         }
52
53         dtrace_provider = NULL;
54
55         if ((state = dtrace_anon_grab()) != NULL) {
56                 /*
57                  * If there were ECBs on this state, the provider should
58                  * have not been allowed to detach; assert that there is
59                  * none.
60                  */
61                 ASSERT(state->dts_necbs == 0);
62                 dtrace_state_destroy(state);
63         }
64
65         bzero(&dtrace_anon, sizeof (dtrace_anon_t));
66
67         mutex_exit(&cpu_lock);
68
69         if (dtrace_helptrace_enabled) {
70                 kmem_free(dtrace_helptrace_buffer, 0);
71                 dtrace_helptrace_buffer = NULL;
72         }
73
74         if (dtrace_probes != NULL) {
75                 kmem_free(dtrace_probes, 0);
76                 dtrace_probes = NULL;
77                 dtrace_nprobes = 0;
78         }
79
80         dtrace_hash_destroy(dtrace_bymod);
81         dtrace_hash_destroy(dtrace_byfunc);
82         dtrace_hash_destroy(dtrace_byname);
83         dtrace_bymod = NULL;
84         dtrace_byfunc = NULL;
85         dtrace_byname = NULL;
86
87         kmem_cache_destroy(dtrace_state_cache);
88
89         delete_unrhdr(dtrace_arena);
90
91         if (dtrace_toxrange != NULL) {
92                 kmem_free(dtrace_toxrange, 0);
93                 dtrace_toxrange = NULL;
94                 dtrace_toxranges = 0;
95                 dtrace_toxranges_max = 0;
96         }
97
98         ASSERT(dtrace_vtime_references == 0);
99         ASSERT(dtrace_opens == 0);
100         ASSERT(dtrace_retained == NULL);
101
102         mutex_exit(&dtrace_lock);
103         mutex_exit(&dtrace_provider_lock);
104
105         mutex_destroy(&dtrace_meta_lock);
106         mutex_destroy(&dtrace_provider_lock);
107         mutex_destroy(&dtrace_lock);
108         mutex_destroy(&dtrace_errlock);
109
110         /* XXX Hack */
111         mutex_destroy(&mod_lock);
112
113         /* Reset our hook for exceptions. */
114         dtrace_invop_uninit();
115
116         /*
117          * Reset our hook for thread switches, but ensure that vtime isn't
118          * active first.
119          */
120         dtrace_vtime_active = 0;
121         dtrace_vtime_switch_func = NULL;
122
123         /* Unhook from the trap handler. */
124         dtrace_trap_func = NULL;
125
126         return (error);
127 }