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 *
|
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#include "opt_alq.h" 40 41#include <sys/param.h> 42#include <sys/alq.h> 43#include <sys/cons.h> 44#include <sys/kernel.h> 45#include <sys/ktr.h> 46#include <sys/libkern.h> 47#include <sys/proc.h> 48#include <sys/sysctl.h> 49#include <sys/systm.h> 50#include <sys/time.h> 51 52#include <machine/cpu.h> 53#ifdef __sparc64__ 54#include <machine/ktr.h> 55#endif 56 57 58#include <ddb/ddb.h> 59 60#ifndef KTR_ENTRIES 61#define KTR_ENTRIES 1024 62#endif 63 64#ifndef KTR_MASK 65#define KTR_MASK (KTR_GEN) 66#endif 67 68#ifndef KTR_CPUMASK 69#define KTR_CPUMASK (~0) 70#endif 71 72#ifndef KTR_TIME 73#define KTR_TIME get_cyclecount() 74#endif 75 76#ifndef KTR_CPU 77#define KTR_CPU PCPU_GET(cpuid) 78#endif 79 80SYSCTL_NODE(_debug, OID_AUTO, ktr, CTLFLAG_RD, 0, "KTR options"); 81 82int ktr_cpumask = KTR_CPUMASK; 83TUNABLE_INT("debug.ktr.cpumask", &ktr_cpumask); 84SYSCTL_INT(_debug_ktr, OID_AUTO, cpumask, CTLFLAG_RW, &ktr_cpumask, 0, ""); 85 86int ktr_mask = KTR_MASK; 87TUNABLE_INT("debug.ktr.mask", &ktr_mask); 88SYSCTL_INT(_debug_ktr, OID_AUTO, mask, CTLFLAG_RW, &ktr_mask, 0, ""); 89 90int ktr_entries = KTR_ENTRIES; 91SYSCTL_INT(_debug_ktr, OID_AUTO, entries, CTLFLAG_RD, &ktr_entries, 0, ""); 92 93int ktr_version = KTR_VERSION; 94SYSCTL_INT(_debug_ktr, OID_AUTO, version, CTLFLAG_RD, &ktr_version, 0, ""); 95 96volatile int ktr_idx = 0; 97struct ktr_entry ktr_buf[KTR_ENTRIES]; 98 99#ifdef KTR_VERBOSE 100int ktr_verbose = KTR_VERBOSE; 101TUNABLE_INT("debug.ktr.verbose", &ktr_verbose); 102SYSCTL_INT(_debug_ktr, OID_AUTO, verbose, CTLFLAG_RW, &ktr_verbose, 0, ""); 103#endif 104 105#ifdef KTR_ALQ 106struct alq *ktr_alq; 107char ktr_alq_file[MAXPATHLEN] = "/tmp/ktr.out"; 108int ktr_alq_cnt = 0; 109int ktr_alq_depth = KTR_ENTRIES; 110int ktr_alq_enabled = 0; 111int ktr_alq_failed = 0; 112int ktr_alq_max = 0; 113 114SYSCTL_INT(_debug_ktr, OID_AUTO, alq_max, CTLFLAG_RW, &ktr_alq_max, 0, 115 "Maximum number of entries to write"); 116SYSCTL_INT(_debug_ktr, OID_AUTO, alq_cnt, CTLFLAG_RD, &ktr_alq_cnt, 0, 117 "Current number of written entries"); 118SYSCTL_INT(_debug_ktr, OID_AUTO, alq_failed, CTLFLAG_RD, &ktr_alq_failed, 0, 119 "Number of times we overran the buffer"); 120SYSCTL_INT(_debug_ktr, OID_AUTO, alq_depth, CTLFLAG_RW, &ktr_alq_depth, 0, 121 "Number of items in the write buffer"); 122SYSCTL_STRING(_debug_ktr, OID_AUTO, alq_file, CTLFLAG_RW, ktr_alq_file, 123 sizeof(ktr_alq_file), "KTR logging file"); 124 125static int 126sysctl_debug_ktr_alq_enable(SYSCTL_HANDLER_ARGS) 127{ 128 int error; 129 int enable; 130 131 enable = ktr_alq_enabled; 132 133 error = sysctl_handle_int(oidp, &enable, 0, req); 134 if (error || !req->newptr) 135 return (error); 136 137 if (enable) { 138 if (ktr_alq_enabled) 139 return (0); 140 error = suser(curthread); 141 if (error) 142 return (error); 143 error = alq_open(&ktr_alq, (const char *)ktr_alq_file, 144 sizeof(struct ktr_entry), ktr_alq_depth); 145 if (error == 0) { 146 ktr_mask &= ~KTR_ALQ_MASK; 147 ktr_alq_cnt = 0; 148 ktr_alq_failed = 0; 149 ktr_alq_enabled = 1; 150 } 151 } else { 152 if (ktr_alq_enabled == 0) 153 return (0); 154 ktr_alq_enabled = 0; 155 alq_close(ktr_alq); 156 ktr_alq = NULL; 157 } 158 159 return (error); 160} 161SYSCTL_PROC(_debug_ktr, OID_AUTO, alq_enable, 162 CTLTYPE_INT|CTLFLAG_RW, 0, 0, sysctl_debug_ktr_alq_enable, 163 "I", "Enable KTR logging"); 164#endif 165 166void 167ktr_tracepoint(u_int mask, const char *file, int line, const char *format, 168 u_long arg1, u_long arg2, u_long arg3, u_long arg4, u_long arg5, 169 u_long arg6) 170{ 171 struct ktr_entry *entry; 172#ifdef KTR_ALQ 173 struct ale *ale = NULL; 174#else 175 int newindex, saveindex; 176#endif 177#if defined(KTR_VERBOSE) || defined(KTR_ALQ) 178 struct thread *td; 179#endif 180 int cpu; 181 182 if (panicstr) 183 return; 184 if ((ktr_mask & mask) == 0) 185 return; 186 cpu = KTR_CPU; 187 if (((1 << cpu) & ktr_cpumask) == 0) 188 return; 189#if defined(KTR_VERBOSE) || defined(KTR_ALQ) 190 td = curthread; 191 if (td->td_inktr) 192 return; 193 td->td_inktr++; 194#endif 195#ifdef KTR_ALQ 196 if (ktr_alq_enabled && 197 td->td_critnest == 0 &&
|
199 td != ald_thread) { 200 if (ktr_alq_max && ktr_alq_cnt > ktr_alq_max) 201 goto done; 202 if ((ale = alq_get(ktr_alq, ALQ_NOWAIT)) == NULL) { 203 ktr_alq_failed++; 204 goto done; 205 } 206 ktr_alq_cnt++; 207 entry = (struct ktr_entry *)ale->ae_data; 208 } else 209 goto done; 210#else 211 do { 212 saveindex = ktr_idx; 213 newindex = (saveindex + 1) & (KTR_ENTRIES - 1); 214 } while (atomic_cmpset_rel_int(&ktr_idx, saveindex, newindex) == 0); 215 entry = &ktr_buf[saveindex]; 216#endif 217 entry->ktr_timestamp = KTR_TIME; 218 entry->ktr_cpu = cpu; 219 if (file != NULL) 220 while (strncmp(file, "../", 3) == 0) 221 file += 3; 222 entry->ktr_file = file; 223 entry->ktr_line = line; 224#ifdef KTR_VERBOSE 225 if (ktr_verbose) { 226#ifdef SMP 227 printf("cpu%d ", cpu); 228#endif 229 if (ktr_verbose > 1) { 230 printf("%s.%d\t", entry->ktr_file, 231 entry->ktr_line); 232 } 233 printf(format, arg1, arg2, arg3, arg4, arg5, arg6); 234 printf("\n"); 235 } 236#endif 237 entry->ktr_desc = format; 238 entry->ktr_parms[0] = arg1; 239 entry->ktr_parms[1] = arg2; 240 entry->ktr_parms[2] = arg3; 241 entry->ktr_parms[3] = arg4; 242 entry->ktr_parms[4] = arg5; 243 entry->ktr_parms[5] = arg6; 244#ifdef KTR_ALQ 245 if (ale) 246 alq_post(ktr_alq, ale); 247done: 248#endif 249#if defined(KTR_VERBOSE) || defined(KTR_ALQ) 250 td->td_inktr--; 251#endif 252} 253 254#ifdef DDB 255 256struct tstate { 257 int cur; 258 int first; 259}; 260static struct tstate tstate; 261static int db_ktr_verbose; 262static int db_mach_vtrace(void); 263 264#define NUM_LINES_PER_PAGE 18 265 266DB_SHOW_COMMAND(ktr, db_ktr_all) 267{ 268 int c, lines; 269 int all = 0; 270 271 lines = NUM_LINES_PER_PAGE; 272 tstate.cur = (ktr_idx - 1) & (KTR_ENTRIES - 1); 273 tstate.first = -1; 274 if (strcmp(modif, "v") == 0) 275 db_ktr_verbose = 1; 276 else 277 db_ktr_verbose = 0; 278 if (strcmp(modif, "a") == 0) 279 all = 1; 280 while (db_mach_vtrace()) 281 if (all) { 282 if (cncheckc() != -1) 283 return; 284 } else if (--lines == 0) { 285 db_printf("--More--"); 286 c = cngetc(); 287 db_printf("\r"); 288 switch (c) { 289 case '\n': /* one more line */ 290 lines = 1; 291 break; 292 case ' ': /* one more page */ 293 lines = NUM_LINES_PER_PAGE; 294 break; 295 default: 296 db_printf("\n"); 297 return; 298 } 299 } 300} 301 302static int 303db_mach_vtrace(void) 304{ 305 struct ktr_entry *kp; 306 307 if (tstate.cur == tstate.first) { 308 db_printf("--- End of trace buffer ---\n"); 309 return (0); 310 } 311 kp = &ktr_buf[tstate.cur]; 312 313 /* Skip over unused entries. */ 314 if (kp->ktr_desc == NULL) { 315 db_printf("--- End of trace buffer ---\n"); 316 return (0); 317 } 318 db_printf("%d: ", tstate.cur); 319#ifdef SMP 320 db_printf("cpu%d ", kp->ktr_cpu); 321#endif 322 if (db_ktr_verbose) { 323 db_printf("%10.10lld %s.%d\t", (long long)kp->ktr_timestamp, 324 kp->ktr_file, kp->ktr_line); 325 } 326 db_printf(kp->ktr_desc, kp->ktr_parms[0], kp->ktr_parms[1], 327 kp->ktr_parms[2], kp->ktr_parms[3], kp->ktr_parms[4], 328 kp->ktr_parms[5]); 329 db_printf("\n"); 330 331 if (tstate.first == -1) 332 tstate.first = tstate.cur; 333 334 if (--tstate.cur < 0) 335 tstate.cur = KTR_ENTRIES - 1; 336 337 return (1); 338} 339 340#endif /* DDB */
|