1 /*- 2 * Copyright (c) 2000 John Baldwin <jhb@FreeBSD.org> 3 * 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 * 3. 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 THE AUTHOR 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 THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 /* 31 * This module holds the global variables used by KTR and the ktr_tracepoint() 32 * function that does the actual tracing. 33 */ 34 35 #include <sys/cdefs.h> 36 __FBSDID("$FreeBSD$"); 37 38 #include "opt_ddb.h" 39 #include "opt_ktr.h" 40 #include "opt_alq.h" 41 42 #include <sys/param.h> 43 #include <sys/queue.h> 44 #include <sys/alq.h> 45 #include <sys/cons.h> 46 #include <sys/cpuset.h> 47 #include <sys/kernel.h> 48 #include <sys/ktr.h> 49 #include <sys/libkern.h> 50 #include <sys/lock.h> 51 #include <sys/malloc.h> 52 #include <sys/mutex.h> 53 #include <sys/proc.h> 54 #include <sys/smp.h> 55 #include <sys/sysctl.h> 56 #include <sys/systm.h> 57 #include <sys/time.h> 58 59 #include <machine/cpu.h> 60 #ifdef __sparc64__ 61 #include <machine/ktr.h> 62 #endif 63 64 #ifdef DDB 65 #include <ddb/ddb.h> 66 #include <ddb/db_output.h> 67 #endif 68 69 #ifndef KTR_ENTRIES 70 #define KTR_ENTRIES 1024 71 #endif 72 73 /* Limit the allocations to something manageable. */ 74 #define KTR_ENTRIES_MAX (8 * 1024 * 1024) 75 76 #ifndef KTR_MASK 77 #define KTR_MASK (0) 78 #endif 79 80 #ifndef KTR_CPUMASK 81 #define KTR_CPUMASK CPUSET_FSET 82 #endif 83 84 #ifndef KTR_TIME 85 #define KTR_TIME get_cyclecount() 86 #endif 87 88 #ifndef KTR_CPU 89 #define KTR_CPU PCPU_GET(cpuid) 90 #endif 91 92 static MALLOC_DEFINE(M_KTR, "KTR", "KTR"); 93 94 FEATURE(ktr, "Kernel support for KTR kernel tracing facility"); 95 96 volatile int ktr_idx = 0; 97 int ktr_mask = KTR_MASK; 98 int ktr_compile = KTR_COMPILE; 99 int ktr_entries = KTR_ENTRIES; 100 int ktr_version = KTR_VERSION; 101 struct ktr_entry ktr_buf_init[KTR_ENTRIES]; 102 struct ktr_entry *ktr_buf = ktr_buf_init; 103 cpuset_t ktr_cpumask = CPUSET_T_INITIALIZER(KTR_CPUMASK); 104 static char ktr_cpumask_str[CPUSETBUFSIZ]; 105 106 TUNABLE_INT("debug.ktr.mask", &ktr_mask); 107 108 TUNABLE_STR("debug.ktr.cpumask", ktr_cpumask_str, sizeof(ktr_cpumask_str)); 109 110 static SYSCTL_NODE(_debug, OID_AUTO, ktr, CTLFLAG_RD, 0, "KTR options"); 111 112 SYSCTL_INT(_debug_ktr, OID_AUTO, version, CTLFLAG_RD, 113 &ktr_version, 0, "Version of the KTR interface"); 114 115 SYSCTL_UINT(_debug_ktr, OID_AUTO, compile, CTLFLAG_RD, 116 &ktr_compile, 0, "Bitmask of KTR event classes compiled into the kernel"); 117 118 static void 119 ktr_cpumask_initializer(void *dummy __unused) 120 { 121 122 /* 123 * TUNABLE_STR() runs with SI_ORDER_MIDDLE priority, thus it must be 124 * already set, if necessary. 125 */ 126 if (ktr_cpumask_str[0] != '\0' && 127 cpusetobj_strscan(&ktr_cpumask, ktr_cpumask_str) == -1) 128 CPU_FILL(&ktr_cpumask); 129 } 130 SYSINIT(ktr_cpumask_initializer, SI_SUB_TUNABLES, SI_ORDER_ANY, 131 ktr_cpumask_initializer, NULL); 132 133 static int 134 sysctl_debug_ktr_cpumask(SYSCTL_HANDLER_ARGS) 135 { 136 char lktr_cpumask_str[CPUSETBUFSIZ]; 137 cpuset_t imask; 138 int error; 139 140 cpusetobj_strprint(lktr_cpumask_str, &ktr_cpumask); 141 error = sysctl_handle_string(oidp, lktr_cpumask_str, 142 sizeof(lktr_cpumask_str), req); 143 if (error != 0 || req->newptr == NULL) 144 return (error); 145 if (cpusetobj_strscan(&imask, lktr_cpumask_str) == -1) 146 return (EINVAL); 147 CPU_COPY(&imask, &ktr_cpumask); 148 149 return (error); 150 } 151 SYSCTL_PROC(_debug_ktr, OID_AUTO, cpumask, 152 CTLFLAG_RW | CTLFLAG_MPSAFE | CTLTYPE_STRING, NULL, 0, 153 sysctl_debug_ktr_cpumask, "S", 154 "Bitmask of CPUs on which KTR logging is enabled"); 155 156 static int 157 sysctl_debug_ktr_clear(SYSCTL_HANDLER_ARGS) 158 { 159 int clear, error; 160 161 clear = 0; 162 error = sysctl_handle_int(oidp, &clear, 0, req); 163 if (error || !req->newptr) 164 return (error); 165 166 if (clear) { 167 bzero(ktr_buf, sizeof(*ktr_buf) * ktr_entries); 168 ktr_idx = 0; 169 } 170 171 return (error); 172 } 173 SYSCTL_PROC(_debug_ktr, OID_AUTO, clear, CTLTYPE_INT|CTLFLAG_RW, 0, 0, 174 sysctl_debug_ktr_clear, "I", "Clear KTR Buffer"); 175 176 /* 177 * This is a sysctl proc so that it is serialized as !MPSAFE along with 178 * the other ktr sysctl procs. 179 */ 180 static int 181 sysctl_debug_ktr_mask(SYSCTL_HANDLER_ARGS) 182 { 183 int mask, error; 184 185 mask = ktr_mask; 186 error = sysctl_handle_int(oidp, &mask, 0, req); 187 if (error || !req->newptr) 188 return (error); 189 ktr_mask = mask; 190 return (error); 191 } 192 193 SYSCTL_PROC(_debug_ktr, OID_AUTO, mask, CTLTYPE_UINT|CTLFLAG_RW, 0, 0, 194 sysctl_debug_ktr_mask, "IU", 195 "Bitmask of KTR event classes for which logging is enabled"); 196 197 static int 198 sysctl_debug_ktr_entries(SYSCTL_HANDLER_ARGS) 199 { 200 int entries, error, mask; 201 struct ktr_entry *buf, *oldbuf; 202 203 entries = ktr_entries; 204 error = sysctl_handle_int(oidp, &entries, 0, req); 205 if (error || !req->newptr) 206 return (error); 207 if (entries > KTR_ENTRIES_MAX) 208 return (ERANGE); 209 /* Disable ktr temporarily. */ 210 mask = ktr_mask; 211 atomic_store_rel_int(&ktr_mask, 0); 212 /* Wait for threads to go idle. */ 213 if ((error = quiesce_all_cpus("ktrent", PCATCH)) != 0) { 214 ktr_mask = mask; 215 return (error); 216 } 217 if (ktr_buf != ktr_buf_init) 218 oldbuf = ktr_buf; 219 else 220 oldbuf = NULL; 221 /* Allocate a new buffer. */ 222 buf = malloc(sizeof(*buf) * entries, M_KTR, M_WAITOK | M_ZERO); 223 /* Install the new buffer and restart ktr. */ 224 ktr_buf = buf; 225 ktr_entries = entries; 226 ktr_idx = 0; 227 atomic_store_rel_int(&ktr_mask, mask); 228 if (oldbuf != NULL) 229 free(oldbuf, M_KTR); 230 231 return (error); 232 } 233 234 SYSCTL_PROC(_debug_ktr, OID_AUTO, entries, CTLTYPE_INT|CTLFLAG_RW, 0, 0, 235 sysctl_debug_ktr_entries, "I", "Number of entries in the KTR buffer"); 236 237 #ifdef KTR_VERBOSE 238 int ktr_verbose = KTR_VERBOSE; 239 TUNABLE_INT("debug.ktr.verbose", &ktr_verbose); 240 SYSCTL_INT(_debug_ktr, OID_AUTO, verbose, CTLFLAG_RW, &ktr_verbose, 0, ""); 241 #endif 242 243 #ifdef KTR_ALQ 244 struct alq *ktr_alq; 245 char ktr_alq_file[MAXPATHLEN] = "/tmp/ktr.out"; 246 int ktr_alq_cnt = 0; 247 int ktr_alq_depth = KTR_ENTRIES; 248 int ktr_alq_enabled = 0; 249 int ktr_alq_failed = 0; 250 int ktr_alq_max = 0; 251 252 SYSCTL_INT(_debug_ktr, OID_AUTO, alq_max, CTLFLAG_RW, &ktr_alq_max, 0, 253 "Maximum number of entries to write"); 254 SYSCTL_INT(_debug_ktr, OID_AUTO, alq_cnt, CTLFLAG_RD, &ktr_alq_cnt, 0, 255 "Current number of written entries"); 256 SYSCTL_INT(_debug_ktr, OID_AUTO, alq_failed, CTLFLAG_RD, &ktr_alq_failed, 0, 257 "Number of times we overran the buffer"); 258 SYSCTL_INT(_debug_ktr, OID_AUTO, alq_depth, CTLFLAG_RW, &ktr_alq_depth, 0, 259 "Number of items in the write buffer"); 260 SYSCTL_STRING(_debug_ktr, OID_AUTO, alq_file, CTLFLAG_RW, ktr_alq_file, 261 sizeof(ktr_alq_file), "KTR logging file"); 262 263 static int 264 sysctl_debug_ktr_alq_enable(SYSCTL_HANDLER_ARGS) 265 { 266 int error; 267 int enable; 268 269 enable = ktr_alq_enabled; 270 271 error = sysctl_handle_int(oidp, &enable, 0, req); 272 if (error || !req->newptr) 273 return (error); 274 275 if (enable) { 276 if (ktr_alq_enabled) 277 return (0); 278 error = alq_open(&ktr_alq, (const char *)ktr_alq_file, 279 req->td->td_ucred, ALQ_DEFAULT_CMODE, 280 sizeof(struct ktr_entry), ktr_alq_depth); 281 if (error == 0) { 282 ktr_alq_cnt = 0; 283 ktr_alq_failed = 0; 284 ktr_alq_enabled = 1; 285 } 286 } else { 287 if (ktr_alq_enabled == 0) 288 return (0); 289 ktr_alq_enabled = 0; 290 alq_close(ktr_alq); 291 ktr_alq = NULL; 292 } 293 294 return (error); 295 } 296 SYSCTL_PROC(_debug_ktr, OID_AUTO, alq_enable, 297 CTLTYPE_INT|CTLFLAG_RW, 0, 0, sysctl_debug_ktr_alq_enable, 298 "I", "Enable KTR logging"); 299 #endif 300 301 void 302 ktr_tracepoint(u_int mask, const char *file, int line, const char *format, 303 u_long arg1, u_long arg2, u_long arg3, u_long arg4, u_long arg5, 304 u_long arg6) 305 { 306 struct ktr_entry *entry; 307 #ifdef KTR_ALQ 308 struct ale *ale = NULL; 309 #endif 310 int newindex, saveindex; 311 #if defined(KTR_VERBOSE) || defined(KTR_ALQ) 312 struct thread *td; 313 #endif 314 int cpu; 315 316 if (panicstr) 317 return; 318 if ((ktr_mask & mask) == 0 || ktr_buf == NULL) 319 return; 320 cpu = KTR_CPU; 321 if (!CPU_ISSET(cpu, &ktr_cpumask)) 322 return; 323 #if defined(KTR_VERBOSE) || defined(KTR_ALQ) 324 td = curthread; 325 if (td->td_pflags & TDP_INKTR) 326 return; 327 td->td_pflags |= TDP_INKTR; 328 #endif 329 #ifdef KTR_ALQ 330 if (ktr_alq_enabled) { 331 if (td->td_critnest == 0 && 332 (td->td_flags & TDF_IDLETD) == 0 && 333 td != ald_thread) { 334 if (ktr_alq_max && ktr_alq_cnt > ktr_alq_max) 335 goto done; 336 if ((ale = alq_get(ktr_alq, ALQ_NOWAIT)) == NULL) { 337 ktr_alq_failed++; 338 goto done; 339 } 340 ktr_alq_cnt++; 341 entry = (struct ktr_entry *)ale->ae_data; 342 } else { 343 goto done; 344 } 345 } else 346 #endif 347 { 348 do { 349 saveindex = ktr_idx; 350 newindex = (saveindex + 1) % ktr_entries; 351 } while (atomic_cmpset_rel_int(&ktr_idx, saveindex, newindex) == 0); 352 entry = &ktr_buf[saveindex]; 353 } 354 entry->ktr_timestamp = KTR_TIME; 355 entry->ktr_cpu = cpu; 356 entry->ktr_thread = curthread; 357 if (file != NULL) 358 while (strncmp(file, "../", 3) == 0) 359 file += 3; 360 entry->ktr_file = file; 361 entry->ktr_line = line; 362 #ifdef KTR_VERBOSE 363 if (ktr_verbose) { 364 #ifdef SMP 365 printf("cpu%d ", cpu); 366 #endif 367 if (ktr_verbose > 1) { 368 printf("%s.%d\t", entry->ktr_file, 369 entry->ktr_line); 370 } 371 printf(format, arg1, arg2, arg3, arg4, arg5, arg6); 372 printf("\n"); 373 } 374 #endif 375 entry->ktr_desc = format; 376 entry->ktr_parms[0] = arg1; 377 entry->ktr_parms[1] = arg2; 378 entry->ktr_parms[2] = arg3; 379 entry->ktr_parms[3] = arg4; 380 entry->ktr_parms[4] = arg5; 381 entry->ktr_parms[5] = arg6; 382 #ifdef KTR_ALQ 383 if (ktr_alq_enabled && ale) 384 alq_post(ktr_alq, ale); 385 done: 386 #endif 387 #if defined(KTR_VERBOSE) || defined(KTR_ALQ) 388 td->td_pflags &= ~TDP_INKTR; 389 #endif 390 } 391 392 #ifdef DDB 393 394 struct tstate { 395 int cur; 396 int first; 397 }; 398 static struct tstate tstate; 399 static int db_ktr_verbose; 400 static int db_mach_vtrace(void); 401 402 DB_SHOW_COMMAND(ktr, db_ktr_all) 403 { 404 405 tstate.cur = (ktr_idx - 1) % ktr_entries; 406 tstate.first = -1; 407 db_ktr_verbose = 0; 408 db_ktr_verbose |= (strchr(modif, 'v') != NULL) ? 2 : 0; 409 db_ktr_verbose |= (strchr(modif, 'V') != NULL) ? 1 : 0; /* just timestap please */ 410 if (strchr(modif, 'a') != NULL) { 411 db_disable_pager(); 412 while (cncheckc() != -1) 413 if (db_mach_vtrace() == 0) 414 break; 415 } else { 416 while (!db_pager_quit) 417 if (db_mach_vtrace() == 0) 418 break; 419 } 420 } 421 422 static int 423 db_mach_vtrace(void) 424 { 425 struct ktr_entry *kp; 426 427 if (tstate.cur == tstate.first || ktr_buf == NULL) { 428 db_printf("--- End of trace buffer ---\n"); 429 return (0); 430 } 431 kp = &ktr_buf[tstate.cur]; 432 433 /* Skip over unused entries. */ 434 if (kp->ktr_desc == NULL) { 435 db_printf("--- End of trace buffer ---\n"); 436 return (0); 437 } 438 db_printf("%d (%p", tstate.cur, kp->ktr_thread); 439 #ifdef SMP 440 db_printf(":cpu%d", kp->ktr_cpu); 441 #endif 442 db_printf(")"); 443 if (db_ktr_verbose >= 1) { 444 db_printf(" %10.10lld", (long long)kp->ktr_timestamp); 445 } 446 if (db_ktr_verbose >= 2) { 447 db_printf(" %s.%d", kp->ktr_file, kp->ktr_line); 448 } 449 db_printf(": "); 450 db_printf(kp->ktr_desc, kp->ktr_parms[0], kp->ktr_parms[1], 451 kp->ktr_parms[2], kp->ktr_parms[3], kp->ktr_parms[4], 452 kp->ktr_parms[5]); 453 db_printf("\n"); 454 455 if (tstate.first == -1) 456 tstate.first = tstate.cur; 457 458 if (--tstate.cur < 0) 459 tstate.cur = ktr_entries - 1; 460 461 return (1); 462 } 463 464 #endif /* DDB */ 465