2000-09-07 01:29:44 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2000
|
|
|
|
* John Baldwin <jhb@FreeBSD.org>. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 4. Neither the name of the author nor the names of any co-contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
2000-11-21 21:10:15 +00:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY JOHN BALDWIN AND CONTRIBUTORS ``AS IS'' AND
|
2000-09-07 01:29:44 +00:00
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
2000-11-21 21:10:15 +00:00
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL JOHN BALDWIN OR THE VOICES IN HIS HEAD
|
2000-09-07 01:29:44 +00:00
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
|
|
|
* THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* $FreeBSD$
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2000-11-07 01:49:48 +00:00
|
|
|
* This module holds the global variables used by KTR and the ktr_tracepoint()
|
|
|
|
* function that does the actual tracing.
|
2000-09-07 01:29:44 +00:00
|
|
|
*/
|
|
|
|
|
2000-12-15 00:01:20 +00:00
|
|
|
#include "opt_ddb.h"
|
2000-11-07 01:49:48 +00:00
|
|
|
#include "opt_ktr.h"
|
2002-09-22 07:13:45 +00:00
|
|
|
#include "opt_alq.h"
|
2000-11-07 01:49:48 +00:00
|
|
|
|
2000-12-15 00:01:20 +00:00
|
|
|
#include <sys/param.h>
|
2002-09-22 07:13:45 +00:00
|
|
|
#include <sys/alq.h>
|
2000-12-15 00:01:20 +00:00
|
|
|
#include <sys/cons.h>
|
2001-01-06 06:51:43 +00:00
|
|
|
#include <sys/kernel.h>
|
2000-09-07 01:29:44 +00:00
|
|
|
#include <sys/ktr.h>
|
2000-11-07 01:49:48 +00:00
|
|
|
#include <sys/libkern.h>
|
2001-12-13 10:33:20 +00:00
|
|
|
#include <sys/proc.h>
|
2000-09-07 01:29:44 +00:00
|
|
|
#include <sys/sysctl.h>
|
2000-11-07 01:49:48 +00:00
|
|
|
#include <sys/systm.h>
|
2001-06-25 23:09:31 +00:00
|
|
|
#include <sys/time.h>
|
2002-04-01 05:35:26 +00:00
|
|
|
|
|
|
|
#include <machine/cpu.h>
|
2002-04-06 08:43:17 +00:00
|
|
|
#ifdef __sparc64__
|
|
|
|
#include <machine/ktr.h>
|
|
|
|
#endif
|
2000-11-07 01:49:48 +00:00
|
|
|
|
2002-09-22 07:13:45 +00:00
|
|
|
|
2000-12-15 00:01:20 +00:00
|
|
|
#include <ddb/ddb.h>
|
|
|
|
|
2001-06-25 23:09:31 +00:00
|
|
|
#ifndef KTR_ENTRIES
|
|
|
|
#define KTR_ENTRIES 1024
|
|
|
|
#endif
|
|
|
|
|
2000-11-07 01:49:48 +00:00
|
|
|
#ifndef KTR_MASK
|
|
|
|
#define KTR_MASK (KTR_GEN)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef KTR_CPUMASK
|
|
|
|
#define KTR_CPUMASK (~0)
|
|
|
|
#endif
|
|
|
|
|
2002-04-01 05:35:26 +00:00
|
|
|
#ifndef KTR_TIME
|
|
|
|
#define KTR_TIME get_cyclecount()
|
2001-01-06 06:51:43 +00:00
|
|
|
#endif
|
|
|
|
|
2002-04-01 05:35:26 +00:00
|
|
|
#ifndef KTR_CPU
|
|
|
|
#define KTR_CPU PCPU_GET(cpuid)
|
2001-01-06 06:51:43 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
SYSCTL_NODE(_debug, OID_AUTO, ktr, CTLFLAG_RD, 0, "KTR options");
|
|
|
|
|
2001-06-06 22:17:08 +00:00
|
|
|
int ktr_cpumask = KTR_CPUMASK;
|
2001-06-08 05:24:21 +00:00
|
|
|
TUNABLE_INT("debug.ktr.cpumask", &ktr_cpumask);
|
2001-01-06 06:51:43 +00:00
|
|
|
SYSCTL_INT(_debug_ktr, OID_AUTO, cpumask, CTLFLAG_RW, &ktr_cpumask, 0, "");
|
2000-09-07 01:29:44 +00:00
|
|
|
|
2001-06-06 22:17:08 +00:00
|
|
|
int ktr_mask = KTR_MASK;
|
2001-06-08 05:24:21 +00:00
|
|
|
TUNABLE_INT("debug.ktr.mask", &ktr_mask);
|
2001-01-06 06:51:43 +00:00
|
|
|
SYSCTL_INT(_debug_ktr, OID_AUTO, mask, CTLFLAG_RW, &ktr_mask, 0, "");
|
2000-09-07 01:29:44 +00:00
|
|
|
|
|
|
|
int ktr_entries = KTR_ENTRIES;
|
2001-01-06 06:51:43 +00:00
|
|
|
SYSCTL_INT(_debug_ktr, OID_AUTO, entries, CTLFLAG_RD, &ktr_entries, 0, "");
|
2000-09-07 01:29:44 +00:00
|
|
|
|
2002-04-01 05:35:26 +00:00
|
|
|
int ktr_version = KTR_VERSION;
|
|
|
|
SYSCTL_INT(_debug_ktr, OID_AUTO, version, CTLFLAG_RD, &ktr_version, 0, "");
|
|
|
|
|
2000-09-07 01:29:44 +00:00
|
|
|
volatile int ktr_idx = 0;
|
|
|
|
struct ktr_entry ktr_buf[KTR_ENTRIES];
|
2000-11-07 01:49:48 +00:00
|
|
|
|
2002-04-01 05:35:26 +00:00
|
|
|
#ifdef KTR_VERBOSE
|
|
|
|
int ktr_verbose = KTR_VERBOSE;
|
2001-06-08 05:24:21 +00:00
|
|
|
TUNABLE_INT("debug.ktr.verbose", &ktr_verbose);
|
2001-01-06 06:51:43 +00:00
|
|
|
SYSCTL_INT(_debug_ktr, OID_AUTO, verbose, CTLFLAG_RW, &ktr_verbose, 0, "");
|
2002-04-01 05:35:26 +00:00
|
|
|
#endif
|
2000-11-07 01:49:48 +00:00
|
|
|
|
2002-09-22 07:13:45 +00:00
|
|
|
#ifdef KTR_ALQ
|
|
|
|
struct alq *ktr_alq;
|
|
|
|
char ktr_alq_file[MAXPATHLEN] = "/tmp/ktr.out";
|
|
|
|
int ktr_alq_cnt = 0;
|
|
|
|
int ktr_alq_depth = KTR_ENTRIES;
|
|
|
|
int ktr_alq_enabled = 0;
|
|
|
|
int ktr_alq_failed = 0;
|
|
|
|
int ktr_alq_max = 0;
|
|
|
|
|
|
|
|
SYSCTL_INT(_debug_ktr, OID_AUTO, alq_max, CTLFLAG_RW, &ktr_alq_max, 0,
|
|
|
|
"Maximum number of entries to write");
|
|
|
|
SYSCTL_INT(_debug_ktr, OID_AUTO, alq_cnt, CTLFLAG_RD, &ktr_alq_cnt, 0,
|
|
|
|
"Current number of written entries");
|
|
|
|
SYSCTL_INT(_debug_ktr, OID_AUTO, alq_failed, CTLFLAG_RD, &ktr_alq_failed, 0,
|
|
|
|
"Number of times we overran the buffer");
|
|
|
|
SYSCTL_INT(_debug_ktr, OID_AUTO, alq_depth, CTLFLAG_RW, &ktr_alq_depth, 0,
|
|
|
|
"Number of items in the write buffer");
|
|
|
|
SYSCTL_STRING(_debug_ktr, OID_AUTO, alq_file, CTLFLAG_RW, ktr_alq_file,
|
|
|
|
sizeof(ktr_alq_file), "KTR logging file");
|
|
|
|
|
|
|
|
static int
|
|
|
|
sysctl_debug_ktr_alq_enable(SYSCTL_HANDLER_ARGS)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
int enable;
|
|
|
|
|
|
|
|
enable = ktr_alq_enabled;
|
|
|
|
|
|
|
|
error = sysctl_handle_int(oidp, &enable, 0, req);
|
|
|
|
if (error || !req->newptr)
|
|
|
|
return (error);
|
|
|
|
|
|
|
|
if (enable) {
|
|
|
|
if (ktr_alq_enabled)
|
|
|
|
return (0);
|
|
|
|
error = suser(curthread);
|
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
error = alq_open(&ktr_alq, (const char *)ktr_alq_file,
|
|
|
|
sizeof(struct ktr_entry), ktr_alq_depth);
|
|
|
|
if (error == 0) {
|
|
|
|
ktr_mask &= ~KTR_ALQ_MASK;
|
|
|
|
ktr_alq_cnt = 0;
|
|
|
|
ktr_alq_failed = 0;
|
|
|
|
ktr_alq_enabled = 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (ktr_alq_enabled == 0)
|
|
|
|
return (0);
|
|
|
|
ktr_alq_enabled = 0;
|
|
|
|
alq_close(ktr_alq);
|
|
|
|
ktr_alq = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
SYSCTL_PROC(_debug_ktr, OID_AUTO, alq_enable,
|
|
|
|
CTLTYPE_INT|CTLFLAG_RW, 0, 0, sysctl_debug_ktr_alq_enable,
|
|
|
|
"I", "Enable KTR logging");
|
|
|
|
#endif
|
|
|
|
|
2000-11-07 01:49:48 +00:00
|
|
|
void
|
2002-04-01 05:35:26 +00:00
|
|
|
ktr_tracepoint(u_int mask, const char *file, int line, const char *format,
|
|
|
|
u_long arg1, u_long arg2, u_long arg3, u_long arg4, u_long arg5,
|
|
|
|
u_long arg6)
|
2000-11-07 01:49:48 +00:00
|
|
|
{
|
|
|
|
struct ktr_entry *entry;
|
2002-09-22 07:13:45 +00:00
|
|
|
#ifdef KTR_ALQ
|
|
|
|
struct ale *ale = NULL;
|
|
|
|
#else
|
2001-03-28 03:06:10 +00:00
|
|
|
int newindex, saveindex;
|
2002-09-22 07:13:45 +00:00
|
|
|
#endif
|
|
|
|
#if defined(KTR_VERBOSE) || defined(KTR_ALQ)
|
2001-12-13 10:33:20 +00:00
|
|
|
struct thread *td;
|
2000-11-07 01:49:48 +00:00
|
|
|
#endif
|
2002-04-01 05:35:26 +00:00
|
|
|
int cpu;
|
2000-11-07 01:49:48 +00:00
|
|
|
|
2000-12-12 00:43:50 +00:00
|
|
|
if (panicstr)
|
|
|
|
return;
|
2000-11-07 01:49:48 +00:00
|
|
|
if ((ktr_mask & mask) == 0)
|
|
|
|
return;
|
2002-03-08 18:55:59 +00:00
|
|
|
cpu = KTR_CPU;
|
|
|
|
if (((1 << cpu) & ktr_cpumask) == 0)
|
2001-06-25 23:09:31 +00:00
|
|
|
return;
|
2002-09-22 07:13:45 +00:00
|
|
|
#if defined(KTR_VERBOSE) || defined(KTR_ALQ)
|
2002-04-01 05:35:26 +00:00
|
|
|
td = curthread;
|
|
|
|
if (td->td_inktr)
|
|
|
|
return;
|
2001-12-13 10:33:20 +00:00
|
|
|
td->td_inktr++;
|
2002-04-01 05:35:26 +00:00
|
|
|
#endif
|
2002-09-22 07:13:45 +00:00
|
|
|
#ifdef KTR_ALQ
|
|
|
|
if (ktr_alq_enabled &&
|
|
|
|
td->td_critnest == 0 &&
|
2002-09-26 07:38:56 +00:00
|
|
|
(td->td_kse->ke_flags & KEF_IDLEKSE) == 0 &&
|
|
|
|
td != ald_thread) {
|
2002-09-22 07:13:45 +00:00
|
|
|
if (ktr_alq_max && ktr_alq_cnt > ktr_alq_max)
|
|
|
|
goto done;
|
|
|
|
if ((ale = alq_get(ktr_alq, ALQ_NOWAIT)) == NULL) {
|
|
|
|
ktr_alq_failed++;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
ktr_alq_cnt++;
|
|
|
|
entry = (struct ktr_entry *)ale->ae_data;
|
|
|
|
} else
|
|
|
|
goto done;
|
|
|
|
#else
|
2000-11-07 01:49:48 +00:00
|
|
|
do {
|
|
|
|
saveindex = ktr_idx;
|
|
|
|
newindex = (saveindex + 1) & (KTR_ENTRIES - 1);
|
|
|
|
} while (atomic_cmpset_rel_int(&ktr_idx, saveindex, newindex) == 0);
|
|
|
|
entry = &ktr_buf[saveindex];
|
2002-09-22 07:13:45 +00:00
|
|
|
#endif
|
2002-04-01 05:35:26 +00:00
|
|
|
entry->ktr_timestamp = KTR_TIME;
|
2002-03-08 18:55:59 +00:00
|
|
|
entry->ktr_cpu = cpu;
|
2003-03-11 19:56:16 +00:00
|
|
|
if (file != NULL)
|
|
|
|
while (strncmp(file, "../", 3) == 0)
|
|
|
|
file += 3;
|
2002-04-01 05:35:26 +00:00
|
|
|
entry->ktr_file = file;
|
2000-11-07 01:49:48 +00:00
|
|
|
entry->ktr_line = line;
|
2002-04-01 05:35:26 +00:00
|
|
|
#ifdef KTR_VERBOSE
|
2000-11-07 01:49:48 +00:00
|
|
|
if (ktr_verbose) {
|
2000-11-15 21:51:53 +00:00
|
|
|
#ifdef SMP
|
2002-04-01 05:35:26 +00:00
|
|
|
printf("cpu%d ", cpu);
|
2000-11-15 21:51:53 +00:00
|
|
|
#endif
|
2002-04-01 05:35:26 +00:00
|
|
|
if (ktr_verbose > 1) {
|
|
|
|
printf("%s.%d\t", entry->ktr_file,
|
|
|
|
entry->ktr_line);
|
|
|
|
}
|
|
|
|
printf(format, arg1, arg2, arg3, arg4, arg5, arg6);
|
2000-11-07 01:49:48 +00:00
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
#endif
|
2002-04-01 05:35:26 +00:00
|
|
|
entry->ktr_desc = format;
|
|
|
|
entry->ktr_parms[0] = arg1;
|
|
|
|
entry->ktr_parms[1] = arg2;
|
|
|
|
entry->ktr_parms[2] = arg3;
|
|
|
|
entry->ktr_parms[3] = arg4;
|
|
|
|
entry->ktr_parms[4] = arg5;
|
|
|
|
entry->ktr_parms[5] = arg6;
|
2002-09-22 07:13:45 +00:00
|
|
|
#ifdef KTR_ALQ
|
|
|
|
if (ale)
|
|
|
|
alq_post(ktr_alq, ale);
|
|
|
|
done:
|
|
|
|
#endif
|
|
|
|
#if defined(KTR_VERBOSE) || defined(KTR_ALQ)
|
2001-12-13 10:33:20 +00:00
|
|
|
td->td_inktr--;
|
2002-04-01 05:35:26 +00:00
|
|
|
#endif
|
2000-11-07 01:49:48 +00:00
|
|
|
}
|
2000-12-15 00:01:20 +00:00
|
|
|
|
|
|
|
#ifdef DDB
|
|
|
|
|
|
|
|
struct tstate {
|
|
|
|
int cur;
|
|
|
|
int first;
|
|
|
|
};
|
|
|
|
static struct tstate tstate;
|
|
|
|
static int db_ktr_verbose;
|
|
|
|
static int db_mach_vtrace(void);
|
|
|
|
|
2001-02-20 09:53:27 +00:00
|
|
|
#define NUM_LINES_PER_PAGE 18
|
|
|
|
|
|
|
|
DB_SHOW_COMMAND(ktr, db_ktr_all)
|
2000-12-15 00:01:20 +00:00
|
|
|
{
|
2001-02-20 09:53:27 +00:00
|
|
|
int c, lines;
|
2003-02-22 23:30:37 +00:00
|
|
|
int all = 0;
|
2000-12-15 00:01:20 +00:00
|
|
|
|
2001-02-20 09:53:27 +00:00
|
|
|
lines = NUM_LINES_PER_PAGE;
|
2000-12-15 00:01:20 +00:00
|
|
|
tstate.cur = (ktr_idx - 1) & (KTR_ENTRIES - 1);
|
|
|
|
tstate.first = -1;
|
|
|
|
if (strcmp(modif, "v") == 0)
|
|
|
|
db_ktr_verbose = 1;
|
|
|
|
else
|
|
|
|
db_ktr_verbose = 0;
|
2003-02-22 23:30:37 +00:00
|
|
|
if (strcmp(modif, "a") == 0)
|
|
|
|
all = 1;
|
2001-02-20 09:53:27 +00:00
|
|
|
while (db_mach_vtrace())
|
2003-02-22 23:30:37 +00:00
|
|
|
if (all) {
|
|
|
|
if (cncheckc() != -1)
|
|
|
|
return;
|
|
|
|
} else if (--lines == 0) {
|
2001-02-20 09:53:27 +00:00
|
|
|
db_printf("--More--");
|
|
|
|
c = cngetc();
|
|
|
|
db_printf("\r");
|
|
|
|
switch (c) {
|
|
|
|
case '\n': /* one more line */
|
|
|
|
lines = 1;
|
|
|
|
break;
|
|
|
|
case ' ': /* one more page */
|
|
|
|
lines = NUM_LINES_PER_PAGE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
db_printf("\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2000-12-15 00:01:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
db_mach_vtrace(void)
|
|
|
|
{
|
|
|
|
struct ktr_entry *kp;
|
|
|
|
|
|
|
|
if (tstate.cur == tstate.first) {
|
|
|
|
db_printf("--- End of trace buffer ---\n");
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
kp = &ktr_buf[tstate.cur];
|
|
|
|
|
|
|
|
/* Skip over unused entries. */
|
2001-02-20 09:53:27 +00:00
|
|
|
if (kp->ktr_desc == NULL) {
|
|
|
|
db_printf("--- End of trace buffer ---\n");
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
db_printf("%d: ", tstate.cur);
|
2000-12-15 00:01:20 +00:00
|
|
|
#ifdef SMP
|
2001-02-20 09:53:27 +00:00
|
|
|
db_printf("cpu%d ", kp->ktr_cpu);
|
2000-12-15 00:01:20 +00:00
|
|
|
#endif
|
2002-04-01 05:35:26 +00:00
|
|
|
if (db_ktr_verbose) {
|
|
|
|
db_printf("%10.10lld %s.%d\t", (long long)kp->ktr_timestamp,
|
|
|
|
kp->ktr_file, kp->ktr_line);
|
|
|
|
}
|
|
|
|
db_printf(kp->ktr_desc, kp->ktr_parms[0], kp->ktr_parms[1],
|
|
|
|
kp->ktr_parms[2], kp->ktr_parms[3], kp->ktr_parms[4],
|
|
|
|
kp->ktr_parms[5]);
|
2001-02-20 09:53:27 +00:00
|
|
|
db_printf("\n");
|
2000-12-15 00:01:20 +00:00
|
|
|
|
|
|
|
if (tstate.first == -1)
|
|
|
|
tstate.first = tstate.cur;
|
|
|
|
|
|
|
|
if (--tstate.cur < 0)
|
|
|
|
tstate.cur = KTR_ENTRIES - 1;
|
|
|
|
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* DDB */
|