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

Cache object: b4493d1a83137efe6d029416bea961c2


[ 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.