]> git.saurik.com Git - apple/xnu.git/blob - bsd/dev/dtrace/dtrace_subr.c
xnu-1228.15.4.tar.gz
[apple/xnu.git] / bsd / dev / dtrace / dtrace_subr.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
22 /*
23 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 */
26
27 /*
28 * #pragma ident "@(#)dtrace_subr.c 1.7 06/04/24 SMI"
29 */
30
31 #include <stdarg.h>
32 #include <string.h>
33 #include <sys/malloc.h>
34 #include <sys/time.h>
35 #include <sys/dtrace.h>
36 #include <sys/dtrace_impl.h>
37 #include <kern/debug.h>
38
39 #if defined(__APPLE__)
40 #define proc_t struct proc
41 #endif
42
43 /* Copied from an arch specific dtrace_subr.c. */
44 int (*dtrace_fasttrap_probe_ptr)(struct regs *);
45
46 /*
47 * Following DTrace hooks are taken from Solaris' dtrace_subr.c
48 * They're assigned in dtrace.c but Darwin never calls them.
49 */
50 void (*dtrace_cpu_init)(processorid_t);
51 void (*dtrace_modload)(struct modctl *);
52 void (*dtrace_modunload)(struct modctl *);
53 #if defined(__APPLE__)
54 void (*dtrace_helpers_cleanup)(proc_t *);
55 #endif
56 void (*dtrace_helpers_fork)(proc_t *, proc_t *);
57 void (*dtrace_cpustart_init)(void);
58 void (*dtrace_cpustart_fini)(void);
59
60 void (*dtrace_kreloc_init)(void);
61 void (*dtrace_kreloc_fini)(void);
62
63 void (*dtrace_debugger_init)(void);
64 void (*dtrace_debugger_fini)(void);
65
66 dtrace_vtime_state_t dtrace_vtime_active = 0;
67 dtrace_cacheid_t dtrace_predcache_id = DTRACE_CACHEIDNONE + 1;
68
69 void (*dtrace_fasttrap_fork_ptr)(proc_t *, proc_t *);
70 void (*dtrace_fasttrap_exec_ptr)(proc_t *);
71 void (*dtrace_fasttrap_exit_ptr)(proc_t *);
72
73 /*
74 * This function is called by cfork() in the event that it appears that
75 * there may be dtrace tracepoints active in the parent process's address
76 * space. This first confirms the existence of dtrace tracepoints in the
77 * parent process and calls into the fasttrap module to remove the
78 * corresponding tracepoints from the child. By knowing that there are
79 * existing tracepoints, and ensuring they can't be removed, we can rely
80 * on the fasttrap module remaining loaded.
81 */
82 void
83 dtrace_fasttrap_fork(proc_t *p, proc_t *cp)
84 {
85 #if !defined(__APPLE__)
86 ASSERT(p->p_proc_flag & P_PR_LOCK);
87 ASSERT(p->p_dtrace_count > 0);
88 #endif /* __APPLE__ */
89
90 if (dtrace_fasttrap_fork_ptr) {
91 (*dtrace_fasttrap_fork_ptr)(p, cp);
92 }
93 }
94
95 typedef struct dtrace_invop_hdlr {
96 int (*dtih_func)(uintptr_t, uintptr_t *, uintptr_t);
97 struct dtrace_invop_hdlr *dtih_next;
98 } dtrace_invop_hdlr_t;
99
100 dtrace_invop_hdlr_t *dtrace_invop_hdlr;
101
102 int
103 dtrace_invop(uintptr_t, uintptr_t *, uintptr_t);
104
105 int
106 dtrace_invop(uintptr_t addr, uintptr_t *stack, uintptr_t eax)
107 {
108 dtrace_invop_hdlr_t *hdlr;
109 int rval;
110
111 for (hdlr = dtrace_invop_hdlr; hdlr != NULL; hdlr = hdlr->dtih_next) {
112 if ((rval = hdlr->dtih_func(addr, stack, eax)) != 0)
113 return (rval);
114 }
115
116 return (0);
117 }
118
119 void
120 dtrace_invop_add(int (*func)(uintptr_t, uintptr_t *, uintptr_t))
121 {
122 dtrace_invop_hdlr_t *hdlr;
123
124 hdlr = kmem_alloc(sizeof (dtrace_invop_hdlr_t), KM_SLEEP);
125 hdlr->dtih_func = func;
126 hdlr->dtih_next = dtrace_invop_hdlr;
127 dtrace_invop_hdlr = hdlr;
128 }
129
130 void
131 dtrace_invop_remove(int (*func)(uintptr_t, uintptr_t *, uintptr_t))
132 {
133 dtrace_invop_hdlr_t *hdlr = dtrace_invop_hdlr, *prev = NULL;
134
135 for (;;) {
136 if (hdlr == NULL)
137 panic("attempt to remove non-existent invop handler");
138
139 if (hdlr->dtih_func == func)
140 break;
141
142 prev = hdlr;
143 hdlr = hdlr->dtih_next;
144 }
145
146 if (prev == NULL) {
147 ASSERT(dtrace_invop_hdlr == hdlr);
148 dtrace_invop_hdlr = hdlr->dtih_next;
149 } else {
150 ASSERT(dtrace_invop_hdlr != hdlr);
151 prev->dtih_next = hdlr->dtih_next;
152 }
153
154 kmem_free(hdlr, sizeof (dtrace_invop_hdlr_t));
155 }
156