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