1 /*- 2 * Copyright (c) 2004 Eirik Nygaard <eirikn@kerneled.com> 3 * All rights reserved. 4 * Copyright (c) 2000 John Baldwin <jhb@FreeBSD.org> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the author nor the names of any co-contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 /* 33 * This module holds the global variables used by KTR and the ktr_tracepoint() 34 * function that does the actual tracing. 35 */ 36 37 /* 38 * $FreeBSD: /repoman/r/ncvs/src/sys/kern/kern_ktr.c,v 1.43 2003/09/10 01:09:32 jhb Exp $ 39 * $DragonFly: src/sys/kern/kern_ktr.c,v 1.1 2004/09/20 20:38:17 eirikn Exp $ 40 */ 41 42 #include "opt_ddb.h" 43 #include "opt_ktr.h" 44 45 #include <sys/param.h> 46 #include <sys/cons.h> 47 #include <sys/kernel.h> 48 #include <sys/ktr.h> 49 #include <sys/libkern.h> 50 #include <sys/proc.h> 51 #include <sys/sysctl.h> 52 #include <sys/systm.h> 53 #include <sys/time.h> 54 55 #include <machine/cpu.h> 56 #include <machine/cpufunc.h> 57 #include <machine/specialreg.h> 58 #include <machine/md_var.h> 59 60 #include <ddb/ddb.h> 61 62 #ifndef KTR_ENTRIES 63 #define KTR_ENTRIES 1024 64 #endif 65 66 #ifndef KTR_MASK 67 #define KTR_MASK (KTR_ALL) 68 #endif 69 70 #ifndef KTR_CPUMASK 71 #define KTR_CPUMASK (~0) 72 #endif 73 74 #ifndef KTR_TIME 75 #define KTR_TIME ktr_getts() 76 #endif 77 78 #ifndef KTR_CPU 79 #define KTR_CPU mycpu->gd_cpuid; 80 #endif 81 82 SYSCTL_NODE(_debug, OID_AUTO, ktr, CTLFLAG_RD, 0, "KTR options"); 83 84 int ktr_cpumask = KTR_CPUMASK; 85 TUNABLE_INT("debug.ktr.cpumask", &ktr_cpumask); 86 SYSCTL_INT(_debug_ktr, OID_AUTO, cpumask, CTLFLAG_RW, &ktr_cpumask, 0, ""); 87 88 int ktr_mask = KTR_MASK; 89 TUNABLE_INT("debug.ktr.mask", &ktr_mask); 90 SYSCTL_INT(_debug_ktr, OID_AUTO, mask, CTLFLAG_RW, &ktr_mask, 0, ""); 91 92 int ktr_entries = KTR_ENTRIES; 93 SYSCTL_INT(_debug_ktr, OID_AUTO, entries, CTLFLAG_RD, &ktr_entries, 0, ""); 94 95 int ktr_version = KTR_VERSION; 96 SYSCTL_INT(_debug_ktr, OID_AUTO, version, CTLFLAG_RD, &ktr_version, 0, ""); 97 98 volatile int ktr_idx = 0; 99 struct ktr_entry ktr_buf[KTR_ENTRIES]; 100 struct lwkt_token ktr_token; 101 102 #ifdef KTR_VERBOSE 103 int ktr_verbose = KTR_VERBOSE; 104 TUNABLE_INT("debug.ktr.verbose", &ktr_verbose); 105 SYSCTL_INT(_debug_ktr, OID_AUTO, verbose, CTLFLAG_RW, &ktr_verbose, 0, ""); 106 #endif 107 108 static void 109 ktr_sysinit(void *dummy) 110 { 111 lwkt_token_init(&ktr_token); 112 } 113 SYSINIT(announce, SI_SUB_INTRINSIC, SI_ORDER_FIRST, ktr_sysinit, NULL); 114 115 static __inline int 116 ktr_nextindex(void) 117 { 118 lwkt_tokref ilock; 119 120 lwkt_gettoken(&ilock, &ktr_token); 121 ktr_idx = (ktr_idx + 1) & (KTR_ENTRIES - 1); 122 lwkt_reltoken(&ilock); 123 124 return(ktr_idx); 125 } 126 127 static __inline uint64_t 128 ktr_getts(void) 129 { 130 struct globaldata *gd = curthread->td_gd; 131 if (cpu_feature & CPUID_TSC) 132 return(rdtsc()); 133 return(gd->gd_time_seconds + basetime.tv_sec); 134 } 135 136 void 137 ktr_tracepoint(u_int mask, const char *file, int line, const char *format, 138 u_long arg1, u_long arg2, u_long arg3, u_long arg4, u_long arg5, 139 u_long arg6) 140 { 141 struct ktr_entry *entry; 142 int cpu, newindex; 143 144 if (panicstr) 145 return; 146 if ((ktr_mask & mask) == 0) 147 return; 148 cpu = KTR_CPU; 149 if (((1 << cpu) & ktr_cpumask) == 0) 150 return; 151 newindex = ktr_nextindex(); 152 entry = &ktr_buf[newindex]; 153 entry->ktr_timestamp = KTR_TIME; 154 entry->ktr_cpu = cpu; 155 if (file != NULL) 156 while (strncmp(file, "../", 3) == 0) 157 file += 3; 158 entry->ktr_file = file; 159 entry->ktr_line = line; 160 #ifdef KTR_VERBOSE 161 if (ktr_verbose) { 162 #ifdef SMP 163 printf("cpu%d ", cpu); 164 #endif 165 if (ktr_verbose > 1) { 166 printf("%s.%d\t", entry->ktr_file, 167 entry->ktr_line); 168 } 169 printf(format, arg1, arg2, arg3, arg4, arg5, arg6); 170 printf("\n"); 171 } 172 #endif 173 entry->ktr_desc = format; 174 entry->ktr_parms[0] = arg1; 175 entry->ktr_parms[1] = arg2; 176 entry->ktr_parms[2] = arg3; 177 entry->ktr_parms[3] = arg4; 178 entry->ktr_parms[4] = arg5; 179 entry->ktr_parms[5] = arg6; 180 } 181 182 #ifdef DDB 183 184 #define db_more_ktr(x) \ 185 db_more(x); \ 186 if (nl > NUM_LINES_PER_PAGE) \ 187 break 188 189 struct tstate { 190 int cur; 191 int first; 192 }; 193 static struct tstate tstate; 194 static int db_ktr_verbose; 195 static int db_mach_vtrace(void); 196 197 #define NUM_LINES_PER_PAGE 18 198 199 DB_SHOW_COMMAND(ktr, db_ktr_all) 200 { 201 int nl = 0; 202 203 tstate.cur = (ktr_idx - 1) & (KTR_ENTRIES - 1); 204 tstate.first = -1; 205 if (strcmp(modif, "v") == 0) 206 db_ktr_verbose = 1; 207 else 208 db_ktr_verbose = 0; 209 if (strcmp(modif, "a") == 0) { 210 while (cncheckc() != -1) { 211 db_more_ktr(&nl); 212 if (db_mach_vtrace() == 0) 213 break; 214 } 215 } else { 216 while (1) { 217 db_more_ktr(&nl); 218 if (db_mach_vtrace() == 0) 219 break; 220 } 221 } 222 } 223 224 static int 225 db_mach_vtrace(void) 226 { 227 struct ktr_entry *kp; 228 229 if (tstate.cur == tstate.first) { 230 db_printf("--- End of trace buffer ---\n"); 231 return (0); 232 } 233 kp = &ktr_buf[tstate.cur]; 234 235 /* Skip over unused entries. */ 236 if (kp->ktr_desc == NULL) { 237 db_printf("--- End of trace buffer ---\n"); 238 return (0); 239 } 240 db_printf("%d: ", tstate.cur); 241 #ifdef SMP 242 db_printf("cpu%d ", kp->ktr_cpu); 243 #endif 244 if (db_ktr_verbose) { 245 db_printf("%10.10lld %s.%d\t", (long long)kp->ktr_timestamp, 246 kp->ktr_file, kp->ktr_line); 247 } 248 db_printf(kp->ktr_desc, kp->ktr_parms[0], kp->ktr_parms[1], 249 kp->ktr_parms[2], kp->ktr_parms[3], kp->ktr_parms[4], 250 kp->ktr_parms[5]); 251 db_printf("\n"); 252 253 if (tstate.first == -1) 254 tstate.first = tstate.cur; 255 256 if (--tstate.cur < 0) 257 tstate.cur = KTR_ENTRIES - 1; 258 259 return (1); 260 } 261 262 #endif /* DDB */ 263