kern_ktr.c revision 87622
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: head/sys/kern/kern_ktr.c 87622 2001-12-10 20:18:17Z obrien $ 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/sysctl.h> 46#include <sys/systm.h> 47#include <sys/time.h> 48#include <machine/globals.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 83SYSCTL_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 */ 89int ktr_extend = KTR_EXTEND_DEFAULT; 90SYSCTL_INT(_debug_ktr, OID_AUTO, extend, CTLFLAG_RD, &ktr_extend, 0, ""); 91 92int ktr_cpumask = KTR_CPUMASK; 93TUNABLE_INT("debug.ktr.cpumask", &ktr_cpumask); 94SYSCTL_INT(_debug_ktr, OID_AUTO, cpumask, CTLFLAG_RW, &ktr_cpumask, 0, ""); 95 96int ktr_mask = KTR_MASK; 97TUNABLE_INT("debug.ktr.mask", &ktr_mask); 98SYSCTL_INT(_debug_ktr, OID_AUTO, mask, CTLFLAG_RW, &ktr_mask, 0, ""); 99 100int ktr_entries = KTR_ENTRIES; 101SYSCTL_INT(_debug_ktr, OID_AUTO, entries, CTLFLAG_RD, &ktr_entries, 0, ""); 102 103volatile int ktr_idx = 0; 104struct ktr_entry ktr_buf[KTR_ENTRIES]; 105 106int ktr_verbose = KTR_VERBOSE_DEFAULT; 107TUNABLE_INT("debug.ktr.verbose", &ktr_verbose); 108SYSCTL_INT(_debug_ktr, OID_AUTO, verbose, CTLFLAG_RW, &ktr_verbose, 0, ""); 109 110#ifdef KTR 111#ifdef KTR_EXTEND 112void 113ktr_tracepoint(u_int mask, const char *filename, u_int line, 114 const char *format, ...) 115#else 116void 117ktr_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#ifdef KTR_EXTEND 125 va_list ap; 126#endif 127 128 if (panicstr) 129 return; 130 if ((ktr_mask & mask) == 0) 131 return; 132 savecrit = critical_enter(); 133 if (((1 << KTR_CPU) & ktr_cpumask) == 0) { 134 critical_exit(savecrit); 135 return; 136 } 137 atomic_clear_int(&ktr_cpumask, 1 << KTR_CPU); 138 do { 139 saveindex = ktr_idx; 140 newindex = (saveindex + 1) & (KTR_ENTRIES - 1); 141 } while (atomic_cmpset_rel_int(&ktr_idx, saveindex, newindex) == 0); 142 entry = &ktr_buf[saveindex]; 143 /* 144 * XXX: The ktr_cpumask atomic ops should make this unnecessary. 145 */ 146 if ((ktr_mask & (KTR_LOCK | KTR_WITNESS)) != 0) 147 getnanotime(&entry->ktr_tv); 148 else 149 nanotime(&entry->ktr_tv); 150 atomic_set_int(&ktr_cpumask, 1 << KTR_CPU); 151 entry->ktr_cpu = KTR_CPU; 152 critical_exit(savecrit); 153#ifdef KTR_EXTEND 154 entry->ktr_filename = filename; 155 entry->ktr_line = line; 156 va_start(ap, format); 157 vsnprintf(entry->ktr_desc, KTRDESCSIZE, format, ap); 158 va_end(ap); 159 if (ktr_verbose) { 160#ifdef SMP 161 printf("cpu%d ", entry->ktr_cpu); 162#endif 163 if (ktr_verbose > 1) 164 printf("%s.%d\t", entry->ktr_filename, entry->ktr_line); 165 va_start(ap, format); 166 vprintf(format, ap); 167 printf("\n"); 168 va_end(ap); 169 } 170#else 171 entry->ktr_desc = format; 172 entry->ktr_parm1 = arg1; 173 entry->ktr_parm2 = arg2; 174 entry->ktr_parm3 = arg3; 175 entry->ktr_parm4 = arg4; 176 entry->ktr_parm5 = arg5; 177#endif 178} 179 180#ifdef DDB 181 182struct tstate { 183 int cur; 184 int first; 185}; 186static struct tstate tstate; 187static int db_ktr_verbose; 188static int db_mach_vtrace(void); 189 190#define NUM_LINES_PER_PAGE 18 191 192DB_SHOW_COMMAND(ktr, db_ktr_all) 193{ 194 int c, lines; 195 196 lines = NUM_LINES_PER_PAGE; 197 tstate.cur = (ktr_idx - 1) & (KTR_ENTRIES - 1); 198 tstate.first = -1; 199 if (strcmp(modif, "v") == 0) 200 db_ktr_verbose = 1; 201 else 202 db_ktr_verbose = 0; 203 while (db_mach_vtrace()) 204 if (--lines == 0) { 205 db_printf("--More--"); 206 c = cngetc(); 207 db_printf("\r"); 208 switch (c) { 209 case '\n': /* one more line */ 210 lines = 1; 211 break; 212 case ' ': /* one more page */ 213 lines = NUM_LINES_PER_PAGE; 214 break; 215 default: 216 db_printf("\n"); 217 return; 218 } 219 } 220} 221 222static int 223db_mach_vtrace(void) 224{ 225 struct ktr_entry *kp; 226 227 if (tstate.cur == tstate.first) { 228 db_printf("--- End of trace buffer ---\n"); 229 return (0); 230 } 231 kp = &ktr_buf[tstate.cur]; 232 233 /* Skip over unused entries. */ 234#ifdef KTR_EXTEND 235 if (kp->ktr_desc[0] == '\0') { 236#else 237 if (kp->ktr_desc == NULL) { 238#endif 239 db_printf("--- End of trace buffer ---\n"); 240 return (0); 241 } 242 db_printf("%d: ", tstate.cur); 243 if (db_ktr_verbose) 244 db_printf("%4ld.%06ld ", (long)kp->ktr_tv.tv_sec, 245 kp->ktr_tv.tv_nsec / 1000); 246#ifdef KTR_EXTEND 247#ifdef SMP 248 db_printf("cpu%d ", kp->ktr_cpu); 249#endif 250 if (db_ktr_verbose) 251 db_printf("%s.%d\t", kp->ktr_filename, kp->ktr_line); 252 db_printf("%s", kp->ktr_desc); 253#else 254 db_printf(kp->ktr_desc, kp->ktr_parm1, kp->ktr_parm2, kp->ktr_parm3, 255 kp->ktr_parm4, kp->ktr_parm5); 256#endif 257 db_printf("\n"); 258 259 if (tstate.first == -1) 260 tstate.first = tstate.cur; 261 262 if (--tstate.cur < 0) 263 tstate.cur = KTR_ENTRIES - 1; 264 265 return (1); 266} 267 268#endif /* DDB */ 269#endif /* KTR */ 270