1 /* 2 * Copyright (c) 2000 3 * John Baldwin <jhb@FreeBSD.org>. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 4. Neither the name of the author nor the names of any co-contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY JOHN BALDWIN AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL JOHN BALDWIN OR THE VOICES IN HIS HEAD 21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 27 * THE POSSIBILITY OF SUCH DAMAGE. 28 * 29 * $FreeBSD$ 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 #include "opt_ddb.h" 38 #include "opt_ktr.h" 39 40 #include <sys/param.h> 41 #include <sys/cons.h> 42 #include <sys/kernel.h> 43 #include <sys/ktr.h> 44 #include <sys/libkern.h> 45 #include <sys/proc.h> 46 #include <sys/sysctl.h> 47 #include <sys/systm.h> 48 #include <sys/time.h> 49 #include <machine/stdarg.h> 50 51 #include <ddb/ddb.h> 52 53 #ifndef KTR_ENTRIES 54 #define KTR_ENTRIES 1024 55 #endif 56 57 #ifndef KTR_MASK 58 #define KTR_MASK (KTR_GEN) 59 #endif 60 61 #ifndef KTR_CPUMASK 62 #define KTR_CPUMASK (~0) 63 #endif 64 65 #ifdef SMP 66 #define KTR_CPU PCPU_GET(cpuid) 67 #else 68 #define KTR_CPU 0 69 #endif 70 71 #ifdef KTR_EXTEND 72 #define KTR_EXTEND_DEFAULT 1 73 #else 74 #define KTR_EXTEND_DEFAULT 0 75 #endif 76 77 #ifdef KTR_VERBOSE 78 #define KTR_VERBOSE_DEFAULT 1 79 #else 80 #define KTR_VERBOSE_DEFAULT 0 81 #endif 82 83 SYSCTL_NODE(_debug, OID_AUTO, ktr, CTLFLAG_RD, 0, "KTR options"); 84 85 /* 86 * This variable is used only by gdb to work out what fields are in 87 * ktr_entry. 88 */ 89 int ktr_extend = KTR_EXTEND_DEFAULT; 90 SYSCTL_INT(_debug_ktr, OID_AUTO, extend, CTLFLAG_RD, &ktr_extend, 0, ""); 91 92 int ktr_cpumask = KTR_CPUMASK; 93 TUNABLE_INT("debug.ktr.cpumask", &ktr_cpumask); 94 SYSCTL_INT(_debug_ktr, OID_AUTO, cpumask, CTLFLAG_RW, &ktr_cpumask, 0, ""); 95 96 int ktr_mask = KTR_MASK; 97 TUNABLE_INT("debug.ktr.mask", &ktr_mask); 98 SYSCTL_INT(_debug_ktr, OID_AUTO, mask, CTLFLAG_RW, &ktr_mask, 0, ""); 99 100 int ktr_entries = KTR_ENTRIES; 101 SYSCTL_INT(_debug_ktr, OID_AUTO, entries, CTLFLAG_RD, &ktr_entries, 0, ""); 102 103 volatile int ktr_idx = 0; 104 struct ktr_entry ktr_buf[KTR_ENTRIES]; 105 106 int ktr_verbose = KTR_VERBOSE_DEFAULT; 107 TUNABLE_INT("debug.ktr.verbose", &ktr_verbose); 108 SYSCTL_INT(_debug_ktr, OID_AUTO, verbose, CTLFLAG_RW, &ktr_verbose, 0, ""); 109 110 #ifdef KTR 111 #ifdef KTR_EXTEND 112 void 113 ktr_tracepoint(u_int mask, const char *filename, u_int line, 114 const char *format, ...) 115 #else 116 void 117 ktr_tracepoint(u_int mask, const char *format, u_long arg1, u_long arg2, 118 u_long arg3, u_long arg4, u_long arg5, u_long arg6) 119 #endif 120 { 121 struct ktr_entry *entry; 122 int newindex, saveindex; 123 critical_t savecrit; 124 struct thread *td; 125 #ifdef KTR_EXTEND 126 va_list ap; 127 #endif 128 129 if (panicstr) 130 return; 131 if ((ktr_mask & mask) == 0) 132 return; 133 td = curthread; 134 if (td->td_inktr) 135 return; 136 savecrit = cpu_critical_enter(); 137 if (((1 << KTR_CPU) & ktr_cpumask) == 0) { 138 cpu_critical_exit(savecrit); 139 return; 140 } 141 td->td_inktr++; 142 do { 143 saveindex = ktr_idx; 144 newindex = (saveindex + 1) & (KTR_ENTRIES - 1); 145 } while (atomic_cmpset_rel_int(&ktr_idx, saveindex, newindex) == 0); 146 entry = &ktr_buf[saveindex]; 147 entry->ktr_cpu = KTR_CPU; 148 cpu_critical_exit(savecrit); 149 nanotime(&entry->ktr_tv); 150 #ifdef KTR_EXTEND 151 entry->ktr_filename = filename; 152 entry->ktr_line = line; 153 va_start(ap, format); 154 vsnprintf(entry->ktr_desc, KTRDESCSIZE, format, ap); 155 va_end(ap); 156 if (ktr_verbose) { 157 #ifdef SMP 158 printf("cpu%d ", entry->ktr_cpu); 159 #endif 160 if (ktr_verbose > 1) 161 printf("%s.%d\t", entry->ktr_filename, entry->ktr_line); 162 va_start(ap, format); 163 vprintf(format, ap); 164 printf("\n"); 165 va_end(ap); 166 } 167 #else 168 entry->ktr_desc = format; 169 entry->ktr_parm1 = arg1; 170 entry->ktr_parm2 = arg2; 171 entry->ktr_parm3 = arg3; 172 entry->ktr_parm4 = arg4; 173 entry->ktr_parm5 = arg5; 174 entry->ktr_parm6 = arg6; 175 #endif 176 td->td_inktr--; 177 } 178 179 #ifdef DDB 180 181 struct tstate { 182 int cur; 183 int first; 184 }; 185 static struct tstate tstate; 186 static int db_ktr_verbose; 187 static int db_mach_vtrace(void); 188 189 #define NUM_LINES_PER_PAGE 18 190 191 DB_SHOW_COMMAND(ktr, db_ktr_all) 192 { 193 int c, lines; 194 195 lines = NUM_LINES_PER_PAGE; 196 tstate.cur = (ktr_idx - 1) & (KTR_ENTRIES - 1); 197 tstate.first = -1; 198 if (strcmp(modif, "v") == 0) 199 db_ktr_verbose = 1; 200 else 201 db_ktr_verbose = 0; 202 while (db_mach_vtrace()) 203 if (--lines == 0) { 204 db_printf("--More--"); 205 c = cngetc(); 206 db_printf("\r"); 207 switch (c) { 208 case '\n': /* one more line */ 209 lines = 1; 210 break; 211 case ' ': /* one more page */ 212 lines = NUM_LINES_PER_PAGE; 213 break; 214 default: 215 db_printf("\n"); 216 return; 217 } 218 } 219 } 220 221 static int 222 db_mach_vtrace(void) 223 { 224 struct ktr_entry *kp; 225 226 if (tstate.cur == tstate.first) { 227 db_printf("--- End of trace buffer ---\n"); 228 return (0); 229 } 230 kp = &ktr_buf[tstate.cur]; 231 232 /* Skip over unused entries. */ 233 #ifdef KTR_EXTEND 234 if (kp->ktr_desc[0] == '\0') { 235 #else 236 if (kp->ktr_desc == NULL) { 237 #endif 238 db_printf("--- End of trace buffer ---\n"); 239 return (0); 240 } 241 db_printf("%d: ", tstate.cur); 242 if (db_ktr_verbose) 243 db_printf("%4ld.%06ld ", (long)kp->ktr_tv.tv_sec, 244 kp->ktr_tv.tv_nsec / 1000); 245 #ifdef KTR_EXTEND 246 #ifdef SMP 247 db_printf("cpu%d ", kp->ktr_cpu); 248 #endif 249 if (db_ktr_verbose) 250 db_printf("%s.%d\t", kp->ktr_filename, kp->ktr_line); 251 db_printf("%s", kp->ktr_desc); 252 #else 253 db_printf(kp->ktr_desc, kp->ktr_parm1, kp->ktr_parm2, kp->ktr_parm3, 254 kp->ktr_parm4, kp->ktr_parm5, kp->ktr_parm6); 255 #endif 256 db_printf("\n"); 257 258 if (tstate.first == -1) 259 tstate.first = tstate.cur; 260 261 if (--tstate.cur < 0) 262 tstate.cur = KTR_ENTRIES - 1; 263 264 return (1); 265 } 266 267 #endif /* DDB */ 268 #endif /* KTR */ 269