The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/kern_ktr.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 2000 John Baldwin <jhb@FreeBSD.org>
    3  * 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  * 3. 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 THE AUTHOR 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 THE AUTHOR OR CONTRIBUTORS BE LIABLE
   21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   27  * SUCH DAMAGE.
   28  */
   29 
   30 /*
   31  * This module holds the global variables used by KTR and the ktr_tracepoint()
   32  * function that does the actual tracing.
   33  */
   34 
   35 #include <sys/cdefs.h>
   36 __FBSDID("$FreeBSD: releng/7.4/sys/kern/kern_ktr.c 162181 2006-09-09 16:09:01Z rwatson $");
   37 
   38 #include "opt_ddb.h"
   39 #include "opt_ktr.h"
   40 #include "opt_alq.h"
   41 
   42 #include <sys/param.h>
   43 #include <sys/alq.h>
   44 #include <sys/cons.h>
   45 #include <sys/kernel.h>
   46 #include <sys/ktr.h>
   47 #include <sys/libkern.h>
   48 #include <sys/proc.h>
   49 #include <sys/sysctl.h>
   50 #include <sys/systm.h>
   51 #include <sys/time.h>
   52 
   53 #include <machine/cpu.h>
   54 #ifdef __sparc64__
   55 #include <machine/ktr.h>
   56 #endif
   57 
   58 #ifdef DDB
   59 #include <ddb/ddb.h>
   60 #include <ddb/db_output.h>
   61 #endif
   62 
   63 #ifndef KTR_ENTRIES
   64 #define KTR_ENTRIES     1024
   65 #endif
   66 
   67 #ifndef KTR_MASK
   68 #define KTR_MASK        (KTR_GEN)
   69 #endif
   70 
   71 #ifndef KTR_CPUMASK
   72 #define KTR_CPUMASK     (~0)
   73 #endif
   74 
   75 #ifndef KTR_TIME
   76 #define KTR_TIME        get_cyclecount()
   77 #endif
   78 
   79 #ifndef KTR_CPU
   80 #define KTR_CPU         PCPU_GET(cpuid)
   81 #endif
   82 
   83 SYSCTL_NODE(_debug, OID_AUTO, ktr, CTLFLAG_RD, 0, "KTR options");
   84 
   85 int     ktr_cpumask = KTR_CPUMASK;
   86 TUNABLE_INT("debug.ktr.cpumask", &ktr_cpumask);
   87 SYSCTL_INT(_debug_ktr, OID_AUTO, cpumask, CTLFLAG_RW, &ktr_cpumask, 0, "");
   88 
   89 int     ktr_mask = KTR_MASK;
   90 TUNABLE_INT("debug.ktr.mask", &ktr_mask);
   91 SYSCTL_INT(_debug_ktr, OID_AUTO, mask, CTLFLAG_RW, &ktr_mask, 0, "");
   92 
   93 int     ktr_compile = KTR_COMPILE;
   94 SYSCTL_INT(_debug_ktr, OID_AUTO, compile, CTLFLAG_RD, &ktr_compile, 0, "");
   95 
   96 int     ktr_entries = KTR_ENTRIES;
   97 SYSCTL_INT(_debug_ktr, OID_AUTO, entries, CTLFLAG_RD, &ktr_entries, 0, "");
   98 
   99 int     ktr_version = KTR_VERSION;
  100 SYSCTL_INT(_debug_ktr, OID_AUTO, version, CTLFLAG_RD, &ktr_version, 0, "");
  101 
  102 volatile int    ktr_idx = 0;
  103 struct  ktr_entry ktr_buf[KTR_ENTRIES];
  104 
  105 static int
  106 sysctl_debug_ktr_clear(SYSCTL_HANDLER_ARGS)
  107 {
  108         int clear, error;
  109 
  110         clear = 0;
  111         error = sysctl_handle_int(oidp, &clear, 0, req);
  112         if (error || !req->newptr)
  113                 return (error);
  114 
  115         if (clear) {
  116                 bzero(ktr_buf, sizeof(ktr_buf));
  117                 ktr_idx = 0;
  118         }
  119 
  120         return (error);
  121 }
  122 SYSCTL_PROC(_debug_ktr, OID_AUTO, clear, CTLTYPE_INT|CTLFLAG_RW, 0, 0,
  123     sysctl_debug_ktr_clear, "I", "Clear KTR Buffer");
  124 
  125 #ifdef KTR_VERBOSE
  126 int     ktr_verbose = KTR_VERBOSE;
  127 TUNABLE_INT("debug.ktr.verbose", &ktr_verbose);
  128 SYSCTL_INT(_debug_ktr, OID_AUTO, verbose, CTLFLAG_RW, &ktr_verbose, 0, "");
  129 #endif
  130 
  131 #ifdef KTR_ALQ
  132 struct alq *ktr_alq;
  133 char    ktr_alq_file[MAXPATHLEN] = "/tmp/ktr.out";
  134 int     ktr_alq_cnt = 0;
  135 int     ktr_alq_depth = KTR_ENTRIES;
  136 int     ktr_alq_enabled = 0;
  137 int     ktr_alq_failed = 0;
  138 int     ktr_alq_max = 0;
  139 
  140 SYSCTL_INT(_debug_ktr, OID_AUTO, alq_max, CTLFLAG_RW, &ktr_alq_max, 0,
  141     "Maximum number of entries to write");
  142 SYSCTL_INT(_debug_ktr, OID_AUTO, alq_cnt, CTLFLAG_RD, &ktr_alq_cnt, 0,
  143     "Current number of written entries");
  144 SYSCTL_INT(_debug_ktr, OID_AUTO, alq_failed, CTLFLAG_RD, &ktr_alq_failed, 0,
  145     "Number of times we overran the buffer");
  146 SYSCTL_INT(_debug_ktr, OID_AUTO, alq_depth, CTLFLAG_RW, &ktr_alq_depth, 0,
  147     "Number of items in the write buffer");
  148 SYSCTL_STRING(_debug_ktr, OID_AUTO, alq_file, CTLFLAG_RW, ktr_alq_file,
  149     sizeof(ktr_alq_file), "KTR logging file");
  150 
  151 static int
  152 sysctl_debug_ktr_alq_enable(SYSCTL_HANDLER_ARGS)
  153 {
  154         int error;
  155         int enable;
  156 
  157         enable = ktr_alq_enabled;
  158 
  159         error = sysctl_handle_int(oidp, &enable, 0, req);
  160         if (error || !req->newptr)
  161                 return (error);
  162 
  163         if (enable) {
  164                 if (ktr_alq_enabled)
  165                         return (0);
  166                 error = alq_open(&ktr_alq, (const char *)ktr_alq_file,
  167                     req->td->td_ucred, ALQ_DEFAULT_CMODE,
  168                     sizeof(struct ktr_entry), ktr_alq_depth);
  169                 if (error == 0) {
  170                         ktr_alq_cnt = 0;
  171                         ktr_alq_failed = 0;
  172                         ktr_alq_enabled = 1;
  173                 }
  174         } else {
  175                 if (ktr_alq_enabled == 0)
  176                         return (0);
  177                 ktr_alq_enabled = 0;
  178                 alq_close(ktr_alq);
  179                 ktr_alq = NULL;
  180         }
  181 
  182         return (error);
  183 }
  184 SYSCTL_PROC(_debug_ktr, OID_AUTO, alq_enable,
  185     CTLTYPE_INT|CTLFLAG_RW, 0, 0, sysctl_debug_ktr_alq_enable,
  186     "I", "Enable KTR logging");
  187 #endif
  188 
  189 void
  190 ktr_tracepoint(u_int mask, const char *file, int line, const char *format,
  191     u_long arg1, u_long arg2, u_long arg3, u_long arg4, u_long arg5,
  192     u_long arg6)
  193 {
  194         struct ktr_entry *entry;
  195 #ifdef KTR_ALQ
  196         struct ale *ale = NULL;
  197 #else
  198         int newindex, saveindex;
  199 #endif
  200 #if defined(KTR_VERBOSE) || defined(KTR_ALQ)
  201         struct thread *td;
  202 #endif
  203         int cpu;
  204 
  205         if (panicstr)
  206                 return;
  207         if ((ktr_mask & mask) == 0)
  208                 return;
  209         cpu = KTR_CPU;
  210         if (((1 << cpu) & ktr_cpumask) == 0)
  211                 return;
  212 #if defined(KTR_VERBOSE) || defined(KTR_ALQ)
  213         td = curthread;
  214         if (td->td_pflags & TDP_INKTR)
  215                 return;
  216         td->td_pflags |= TDP_INKTR;
  217 #endif
  218 #ifdef KTR_ALQ
  219         if (ktr_alq_enabled &&
  220             td->td_critnest == 0 &&
  221             (td->td_flags & TDF_IDLETD) == 0 &&
  222             td != ald_thread) {
  223                 if (ktr_alq_max && ktr_alq_cnt > ktr_alq_max)
  224                         goto done;
  225                 if ((ale = alq_get(ktr_alq, ALQ_NOWAIT)) == NULL) {
  226                         ktr_alq_failed++;
  227                         goto done;
  228                 }
  229                 ktr_alq_cnt++;
  230                 entry = (struct ktr_entry *)ale->ae_data;
  231         } else
  232                 goto done;
  233 #else
  234         do {
  235                 saveindex = ktr_idx;
  236                 newindex = (saveindex + 1) & (KTR_ENTRIES - 1);
  237         } while (atomic_cmpset_rel_int(&ktr_idx, saveindex, newindex) == 0);
  238         entry = &ktr_buf[saveindex];
  239 #endif
  240         entry->ktr_timestamp = KTR_TIME;
  241         entry->ktr_cpu = cpu;
  242         entry->ktr_thread = curthread;
  243         if (file != NULL)
  244                 while (strncmp(file, "../", 3) == 0)
  245                         file += 3;
  246         entry->ktr_file = file;
  247         entry->ktr_line = line;
  248 #ifdef KTR_VERBOSE
  249         if (ktr_verbose) {
  250 #ifdef SMP
  251                 printf("cpu%d ", cpu);
  252 #endif
  253                 if (ktr_verbose > 1) {
  254                         printf("%s.%d\t", entry->ktr_file,
  255                             entry->ktr_line);
  256                 }
  257                 printf(format, arg1, arg2, arg3, arg4, arg5, arg6);
  258                 printf("\n");
  259         }
  260 #endif
  261         entry->ktr_desc = format;
  262         entry->ktr_parms[0] = arg1;
  263         entry->ktr_parms[1] = arg2;
  264         entry->ktr_parms[2] = arg3;
  265         entry->ktr_parms[3] = arg4;
  266         entry->ktr_parms[4] = arg5;
  267         entry->ktr_parms[5] = arg6;
  268 #ifdef KTR_ALQ
  269         if (ale)
  270                 alq_post(ktr_alq, ale);
  271 done:
  272 #endif
  273 #if defined(KTR_VERBOSE) || defined(KTR_ALQ)
  274         td->td_pflags &= ~TDP_INKTR;
  275 #endif
  276 }
  277 
  278 #ifdef DDB
  279 
  280 struct tstate {
  281         int     cur;
  282         int     first;
  283 };
  284 static  struct tstate tstate;
  285 static  int db_ktr_verbose;
  286 static  int db_mach_vtrace(void);
  287 
  288 DB_SHOW_COMMAND(ktr, db_ktr_all)
  289 {
  290         
  291         tstate.cur = (ktr_idx - 1) & (KTR_ENTRIES - 1);
  292         tstate.first = -1;
  293         db_ktr_verbose = index(modif, 'v') != NULL;
  294         if (index(modif, 'a') != NULL) {
  295                 db_disable_pager();
  296                 while (cncheckc() != -1)
  297                         if (db_mach_vtrace() == 0)
  298                                 break;
  299         } else {
  300                 while (!db_pager_quit)
  301                         if (db_mach_vtrace() == 0)
  302                                 break;
  303         }
  304 }
  305 
  306 static int
  307 db_mach_vtrace(void)
  308 {
  309         struct ktr_entry        *kp;
  310 
  311         if (tstate.cur == tstate.first) {
  312                 db_printf("--- End of trace buffer ---\n");
  313                 return (0);
  314         }
  315         kp = &ktr_buf[tstate.cur];
  316 
  317         /* Skip over unused entries. */
  318         if (kp->ktr_desc == NULL) {
  319                 db_printf("--- End of trace buffer ---\n");
  320                 return (0);
  321         }
  322         db_printf("%d (%p", tstate.cur, kp->ktr_thread);
  323 #ifdef SMP
  324         db_printf(":cpu%d", kp->ktr_cpu);
  325 #endif
  326         db_printf(")");
  327         if (db_ktr_verbose) {
  328                 db_printf(" %10.10lld %s.%d", (long long)kp->ktr_timestamp,
  329                     kp->ktr_file, kp->ktr_line);
  330         }
  331         db_printf(": ");
  332         db_printf(kp->ktr_desc, kp->ktr_parms[0], kp->ktr_parms[1],
  333             kp->ktr_parms[2], kp->ktr_parms[3], kp->ktr_parms[4],
  334             kp->ktr_parms[5]);
  335         db_printf("\n");
  336 
  337         if (tstate.first == -1)
  338                 tstate.first = tstate.cur;
  339 
  340         if (--tstate.cur < 0)
  341                 tstate.cur = KTR_ENTRIES - 1;
  342 
  343         return (1);
  344 }
  345 
  346 #endif  /* DDB */

Cache object: 0cfd664cc6e42d7a895d6830ae7b98f3


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.