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