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 struct thread *td; 124 int cpu; 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 cpu = KTR_CPU; 137 if (((1 << cpu) & ktr_cpumask) == 0) 138 return; 139 td->td_inktr++; 140 do { 141 saveindex = ktr_idx; 142 newindex = (saveindex + 1) & (KTR_ENTRIES - 1); 143 } while (atomic_cmpset_rel_int(&ktr_idx, saveindex, newindex) == 0); 144 entry = &ktr_buf[saveindex]; 145 entry->ktr_cpu = cpu; 146 nanotime(&entry->ktr_tv); 147 #ifdef KTR_EXTEND 148 entry->ktr_filename = filename; 149 entry->ktr_line = line; 150 va_start(ap, format); 151 vsnprintf(entry->ktr_desc, KTRDESCSIZE, format, ap); 152 va_end(ap); 153 if (ktr_verbose) { 154 #ifdef SMP 155 printf("cpu%d ", entry->ktr_cpu); 156 #endif 157 if (ktr_verbose > 1) 158 printf("%s.%d\t", entry->ktr_filename, entry->ktr_line); 159 va_start(ap, format); 160 vprintf(format, ap); 161 printf("\n"); 162 va_end(ap); 163 } 164 #else 165 entry->ktr_desc = format; 166 entry->ktr_parm1 = arg1; 167 entry->ktr_parm2 = arg2; 168 entry->ktr_parm3 = arg3; 169 entry->ktr_parm4 = arg4; 170 entry->ktr_parm5 = arg5; 171 entry->ktr_parm6 = arg6; 172 #endif 173 td->td_inktr--; 174 } 175 176 #ifdef DDB 177 178 struct tstate { 179 int cur; 180 int first; 181 }; 182 static struct tstate tstate; 183 static int db_ktr_verbose; 184 static int db_mach_vtrace(void); 185 186 #define NUM_LINES_PER_PAGE 18 187 188 DB_SHOW_COMMAND(ktr, db_ktr_all) 189 { 190 int c, lines; 191 192 lines = NUM_LINES_PER_PAGE; 193 tstate.cur = (ktr_idx - 1) & (KTR_ENTRIES - 1); 194 tstate.first = -1; 195 if (strcmp(modif, "v") == 0) 196 db_ktr_verbose = 1; 197 else 198 db_ktr_verbose = 0; 199 while (db_mach_vtrace()) 200 if (--lines == 0) { 201 db_printf("--More--"); 202 c = cngetc(); 203 db_printf("\r"); 204 switch (c) { 205 case '\n': /* one more line */ 206 lines = 1; 207 break; 208 case ' ': /* one more page */ 209 lines = NUM_LINES_PER_PAGE; 210 break; 211 default: 212 db_printf("\n"); 213 return; 214 } 215 } 216 } 217 218 static int 219 db_mach_vtrace(void) 220 { 221 struct ktr_entry *kp; 222 223 if (tstate.cur == tstate.first) { 224 db_printf("--- End of trace buffer ---\n"); 225 return (0); 226 } 227 kp = &ktr_buf[tstate.cur]; 228 229 /* Skip over unused entries. */ 230 #ifdef KTR_EXTEND 231 if (kp->ktr_desc[0] == '\0') { 232 #else 233 if (kp->ktr_desc == NULL) { 234 #endif 235 db_printf("--- End of trace buffer ---\n"); 236 return (0); 237 } 238 db_printf("%d: ", tstate.cur); 239 if (db_ktr_verbose) 240 db_printf("%4ld.%06ld ", (long)kp->ktr_tv.tv_sec, 241 kp->ktr_tv.tv_nsec / 1000); 242 #ifdef KTR_EXTEND 243 #ifdef SMP 244 db_printf("cpu%d ", kp->ktr_cpu); 245 #endif 246 if (db_ktr_verbose) 247 db_printf("%s.%d\t", kp->ktr_filename, kp->ktr_line); 248 db_printf("%s", kp->ktr_desc); 249 #else 250 db_printf(kp->ktr_desc, kp->ktr_parm1, kp->ktr_parm2, kp->ktr_parm3, 251 kp->ktr_parm4, kp->ktr_parm5, kp->ktr_parm6); 252 #endif 253 db_printf("\n"); 254 255 if (tstate.first == -1) 256 tstate.first = tstate.cur; 257 258 if (--tstate.cur < 0) 259 tstate.cur = KTR_ENTRIES - 1; 260 261 return (1); 262 } 263 264 #endif /* DDB */ 265 #endif /* KTR */ 266