kern_ktr.c revision 70464
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 70464 2000-12-29 06:27:39Z grog $ 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/types.h> 42#include <sys/cons.h> 43#include <sys/time.h> 44#include <sys/ktr.h> 45#include <sys/libkern.h> 46#include <sys/linker_set.h> 47#include <sys/sysctl.h> 48#include <sys/systm.h> 49#include <machine/globals.h> 50#include <machine/stdarg.h> 51 52#include <ddb/ddb.h> 53 54#ifndef KTR_MASK 55#define KTR_MASK (KTR_GEN) 56#endif 57 58#ifndef KTR_CPUMASK 59#define KTR_CPUMASK (~0) 60#endif 61 62#ifdef SMP 63#define KTR_CPU cpuid 64#else 65#define KTR_CPU 0 66#endif 67 68#ifdef KTR_EXTEND 69/* 70 * This variable is used only by gdb to work out what fields are in 71 * ktr_entry. 72 */ 73int ktr_extend = 1; 74SYSCTL_INT(_debug, OID_AUTO, ktr_extend, CTLFLAG_RD, &ktr_extend, 1, ""); 75#else 76int ktr_extend = 0; 77SYSCTL_INT(_debug, OID_AUTO, ktr_extend, CTLFLAG_RD, &ktr_extend, 0, ""); 78#endif /* KTR_EXTEND */ 79 80int ktr_cpumask = KTR_CPUMASK; 81SYSCTL_INT(_debug, OID_AUTO, ktr_cpumask, CTLFLAG_RW, &ktr_cpumask, KTR_CPUMASK, ""); 82 83int ktr_mask = KTR_MASK; 84SYSCTL_INT(_debug, OID_AUTO, ktr_mask, CTLFLAG_RW, &ktr_mask, KTR_MASK, ""); 85 86int ktr_entries = KTR_ENTRIES; 87SYSCTL_INT(_debug, OID_AUTO, ktr_entries, CTLFLAG_RD, &ktr_entries, KTR_ENTRIES, ""); 88 89volatile int ktr_idx = 0; 90struct ktr_entry ktr_buf[KTR_ENTRIES]; 91 92#ifdef KTR_VERBOSE 93int ktr_verbose = 1; 94#else 95int ktr_verbose = 0; 96#endif 97SYSCTL_INT(_debug, OID_AUTO, ktr_verbose, CTLFLAG_RW, &ktr_verbose, 0, ""); 98 99#ifdef KTR 100#ifdef KTR_EXTEND 101void 102ktr_tracepoint(u_int mask, char *filename, u_int line, char *format, ...) 103#else 104void 105ktr_tracepoint(u_int mask, char *format, u_long arg1, u_long arg2, u_long arg3, 106 u_long arg4, u_long arg5) 107#endif 108{ 109 struct ktr_entry *entry; 110 int newindex, saveindex, saveintr; 111#ifdef KTR_EXTEND 112 va_list ap; 113#endif 114 115 if (panicstr) 116 return; 117 if ((ktr_mask & mask) == 0) 118 return; 119#ifdef KTR_EXTEND 120 if (((1 << KTR_CPU) & ktr_cpumask) == 0) 121 return; 122#endif 123 saveintr = save_intr(); 124 disable_intr(); 125 do { 126 saveindex = ktr_idx; 127 newindex = (saveindex + 1) & (KTR_ENTRIES - 1); 128 } while (atomic_cmpset_rel_int(&ktr_idx, saveindex, newindex) == 0); 129 entry = &ktr_buf[saveindex]; 130 restore_intr(saveintr); 131 if (ktr_mask & KTR_LOCK) 132 /* 133 * We can't use nanotime with KTR_LOCK, it would cause 134 * endless recursion, at least under the Intel 135 * architecture. 136 */ 137 getnanotime(&entry->ktr_tv); 138 else 139 nanotime(&entry->ktr_tv); 140#ifdef KTR_EXTEND 141 strncpy(entry->ktr_filename, filename, KTRFILENAMESIZE - 1); 142 entry->ktr_filename[KTRFILENAMESIZE - 1] = '\0'; 143 entry->ktr_line = line; 144 entry->ktr_cpu = KTR_CPU; 145 va_start(ap, format); 146 vsnprintf(entry->ktr_desc, KTRDESCSIZE, format, ap); 147 va_end(ap); 148 if (ktr_verbose) { 149#ifdef SMP 150 printf("cpu%d ", entry->ktr_cpu); 151#endif 152 if (ktr_verbose > 1) 153 printf("%s.%d\t", entry->ktr_filename, entry->ktr_line); 154 va_start(ap, format); 155 vprintf(format, ap); 156 printf("\n"); 157 va_end(ap); 158 } 159#else 160 entry->ktr_desc = format; 161 entry->ktr_parm1 = arg1; 162 entry->ktr_parm2 = arg2; 163 entry->ktr_parm3 = arg3; 164 entry->ktr_parm4 = arg4; 165 entry->ktr_parm5 = arg5; 166#endif 167} 168 169#ifdef DDB 170 171struct tstate { 172 int cur; 173 int first; 174}; 175static struct tstate tstate; 176static int db_ktr_verbose; 177static int db_mach_vtrace(void); 178 179DB_COMMAND(tbuf, db_mach_tbuf) 180{ 181 182 tstate.cur = (ktr_idx - 1) & (KTR_ENTRIES - 1); 183 tstate.first = -1; 184 if (strcmp(modif, "v") == 0) 185 db_ktr_verbose = 1; 186 else 187 db_ktr_verbose = 0; 188 db_mach_vtrace(); 189 190 return; 191} 192 193DB_COMMAND(tall, db_mach_tall) 194{ 195 int c; 196 197 db_mach_tbuf(addr, have_addr, count, modif); 198 while (db_mach_vtrace()) { 199 c = cncheckc(); 200 if (c != -1) 201 break; 202 } 203 204 return; 205} 206 207DB_COMMAND(tnext, db_mach_tnext) 208{ 209 210 if (strcmp(modif, "v") == 0) 211 db_ktr_verbose ^= 1; 212 db_mach_vtrace(); 213} 214 215static int 216db_mach_vtrace(void) 217{ 218 struct ktr_entry *kp; 219 220 if (tstate.cur == tstate.first) { 221 db_printf("--- End of trace buffer ---\n"); 222 return (0); 223 } 224 kp = &ktr_buf[tstate.cur]; 225 226 /* Skip over unused entries. */ 227#ifdef KTR_EXTEND 228 if (kp->ktr_desc[0] != '\0') { 229#else 230 if (kp->ktr_desc != NULL) { 231#endif 232 db_printf("%d: ", tstate.cur); 233 if (db_ktr_verbose) 234 db_printf("%4ld.%06ld ", kp->ktr_tv.tv_sec, 235 kp->ktr_tv.tv_nsec / 1000); 236#ifdef KTR_EXTEND 237#ifdef SMP 238 db_printf("cpu%d ", kp->ktr_cpu); 239#endif 240 if (db_ktr_verbose) 241 db_printf("%s.%d\t", kp->ktr_filename, kp->ktr_line); 242 db_printf("%s", kp->ktr_desc); 243#else 244 db_printf(kp->ktr_desc, kp->ktr_parm1, kp->ktr_parm2, 245 kp->ktr_parm3, kp->ktr_parm4, kp->ktr_parm5); 246#endif 247 db_printf("\n"); 248 } 249 250 if (tstate.first == -1) 251 tstate.first = tstate.cur; 252 253 if (--tstate.cur < 0) 254 tstate.cur = KTR_ENTRIES - 1; 255 256 return (1); 257} 258 259#endif /* DDB */ 260#endif /* KTR */ 261