dtrace_load.c revision 262038
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: stable/9/sys/cddl/dev/dtrace/dtrace_load.c 262038 2014-02-17 12:42:57Z avg $ 22 * 23 */ 24 25static void 26dtrace_ap_start(void *dummy) 27{ 28 int i; 29 30 mutex_enter(&cpu_lock); 31 32 /* Setup the rest of the CPUs. */ 33 CPU_FOREACH(i) { 34 if (i == 0) 35 continue; 36 37 (void) dtrace_cpu_setup(CPU_CONFIG, i); 38 } 39 40 mutex_exit(&cpu_lock); 41} 42 43SYSINIT(dtrace_ap_start, SI_SUB_SMP, SI_ORDER_ANY, dtrace_ap_start, NULL); 44 45static void 46dtrace_load(void *dummy) 47{ 48 dtrace_provider_id_t id; 49 50 /* Hook into the trap handler. */ 51 dtrace_trap_func = dtrace_trap; 52 53 /* Hang our hook for thread switches. */ 54 dtrace_vtime_switch_func = dtrace_vtime_switch; 55 56 /* Hang our hook for exceptions. */ 57 dtrace_invop_init(); 58 59 /* Register callbacks for linker file load and unload events. */ 60 dtrace_kld_load_tag = EVENTHANDLER_REGISTER(kld_load, 61 dtrace_kld_load, NULL, EVENTHANDLER_PRI_ANY); 62 dtrace_kld_unload_try_tag = EVENTHANDLER_REGISTER(kld_unload_try, 63 dtrace_kld_unload_try, NULL, EVENTHANDLER_PRI_ANY); 64 65 /* 66 * XXX This is a short term hack to avoid having to comment 67 * out lots and lots of lock/unlock calls. 68 */ 69 mutex_init(&mod_lock,"XXX mod_lock hack", MUTEX_DEFAULT, NULL); 70 71 /* 72 * Initialise the mutexes without 'witness' because the dtrace 73 * code is mostly written to wait for memory. To have the 74 * witness code change a malloc() from M_WAITOK to M_NOWAIT 75 * because a lock is held would surely create a panic in a 76 * low memory situation. And that low memory situation might be 77 * the very problem we are trying to trace. 78 */ 79 mutex_init(&dtrace_lock,"dtrace probe state", MUTEX_DEFAULT, NULL); 80 mutex_init(&dtrace_provider_lock,"dtrace provider state", MUTEX_DEFAULT, NULL); 81 mutex_init(&dtrace_meta_lock,"dtrace meta-provider state", MUTEX_DEFAULT, NULL); 82#ifdef DEBUG 83 mutex_init(&dtrace_errlock,"dtrace error lock", MUTEX_DEFAULT, NULL); 84#endif 85 86 mutex_enter(&dtrace_provider_lock); 87 mutex_enter(&dtrace_lock); 88 mutex_enter(&cpu_lock); 89 90 ASSERT(MUTEX_HELD(&cpu_lock)); 91 92 dtrace_arena = new_unrhdr(1, INT_MAX, &dtrace_unr_mtx); 93 94 dtrace_state_cache = kmem_cache_create("dtrace_state_cache", 95 sizeof (dtrace_dstate_percpu_t) * NCPU, DTRACE_STATE_ALIGN, 96 NULL, NULL, NULL, NULL, NULL, 0); 97 98 ASSERT(MUTEX_HELD(&cpu_lock)); 99 dtrace_bymod = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_mod), 100 offsetof(dtrace_probe_t, dtpr_nextmod), 101 offsetof(dtrace_probe_t, dtpr_prevmod)); 102 103 dtrace_byfunc = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_func), 104 offsetof(dtrace_probe_t, dtpr_nextfunc), 105 offsetof(dtrace_probe_t, dtpr_prevfunc)); 106 107 dtrace_byname = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_name), 108 offsetof(dtrace_probe_t, dtpr_nextname), 109 offsetof(dtrace_probe_t, dtpr_prevname)); 110 111 if (dtrace_retain_max < 1) { 112 cmn_err(CE_WARN, "illegal value (%lu) for dtrace_retain_max; " 113 "setting to 1", dtrace_retain_max); 114 dtrace_retain_max = 1; 115 } 116 117 /* 118 * Now discover our toxic ranges. 119 */ 120 dtrace_toxic_ranges(dtrace_toxrange_add); 121 122 /* 123 * Before we register ourselves as a provider to our own framework, 124 * we would like to assert that dtrace_provider is NULL -- but that's 125 * not true if we were loaded as a dependency of a DTrace provider. 126 * Once we've registered, we can assert that dtrace_provider is our 127 * pseudo provider. 128 */ 129 (void) dtrace_register("dtrace", &dtrace_provider_attr, 130 DTRACE_PRIV_NONE, 0, &dtrace_provider_ops, NULL, &id); 131 132 ASSERT(dtrace_provider != NULL); 133 ASSERT((dtrace_provider_id_t)dtrace_provider == id); 134 135 dtrace_probeid_begin = dtrace_probe_create((dtrace_provider_id_t) 136 dtrace_provider, NULL, NULL, "BEGIN", 0, NULL); 137 dtrace_probeid_end = dtrace_probe_create((dtrace_provider_id_t) 138 dtrace_provider, NULL, NULL, "END", 0, NULL); 139 dtrace_probeid_error = dtrace_probe_create((dtrace_provider_id_t) 140 dtrace_provider, NULL, NULL, "ERROR", 1, NULL); 141 142 mutex_exit(&cpu_lock); 143 144 /* 145 * If DTrace helper tracing is enabled, we need to allocate the 146 * trace buffer and initialize the values. 147 */ 148 if (dtrace_helptrace_enabled) { 149 ASSERT(dtrace_helptrace_buffer == NULL); 150 dtrace_helptrace_buffer = 151 kmem_zalloc(dtrace_helptrace_bufsize, KM_SLEEP); 152 dtrace_helptrace_next = 0; 153 } 154 155 mutex_exit(&dtrace_lock); 156 mutex_exit(&dtrace_provider_lock); 157 158 mutex_enter(&cpu_lock); 159 160 /* Setup the boot CPU */ 161 (void) dtrace_cpu_setup(CPU_CONFIG, 0); 162 163 mutex_exit(&cpu_lock); 164 165#if __FreeBSD_version < 800039 166 /* Enable device cloning. */ 167 clone_setup(&dtrace_clones); 168 169 /* Setup device cloning events. */ 170 eh_tag = EVENTHANDLER_REGISTER(dev_clone, dtrace_clone, 0, 1000); 171#else 172 dtrace_dev = make_dev(&dtrace_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, 173 "dtrace/dtrace"); 174 helper_dev = make_dev(&helper_cdevsw, 0, UID_ROOT, GID_WHEEL, 0660, 175 "dtrace/helper"); 176#endif 177 178 return; 179} 180