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  *
   14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   24  * SUCH DAMAGE.
   25  */
   26 
   27 /*
   28  * This module holds the global variables used by KTR and the ktr_tracepoint()
   29  * function that does the actual tracing.
   30  */
   31 
   32 #include <sys/cdefs.h>
   33 __FBSDID("$FreeBSD: releng/10.3/sys/kern/kern_ktr.c 293853 2016-01-13 21:38:52Z marius $");
   34 
   35 #include "opt_ddb.h"
   36 #include "opt_ktr.h"
   37 #include "opt_alq.h"
   38 
   39 #include <sys/param.h>
   40 #include <sys/queue.h>
   41 #include <sys/alq.h>
   42 #include <sys/cons.h>
   43 #include <sys/cpuset.h>
   44 #include <sys/kernel.h>
   45 #include <sys/ktr.h>
   46 #include <sys/libkern.h>
   47 #include <sys/lock.h>
   48 #include <sys/malloc.h>
   49 #include <sys/mutex.h>
   50 #include <sys/proc.h>
   51 #include <sys/smp.h>
   52 #include <sys/sysctl.h>
   53 #include <sys/systm.h>
   54 #include <sys/time.h>
   55 
   56 #include <machine/cpu.h>
   57 
   58 #ifdef DDB
   59 #include <ddb/ddb.h>
   60 #include <ddb/db_output.h>
   61 #endif
   62 
   63 #ifndef KTR_BOOT_ENTRIES
   64 #define KTR_BOOT_ENTRIES        1024
   65 #endif
   66 
   67 #ifndef KTR_ENTRIES
   68 #define KTR_ENTRIES     1024
   69 #endif
   70 
   71 /* Limit the allocations to something manageable. */
   72 #define KTR_ENTRIES_MAX (8 * 1024 * 1024)
   73 
   74 #ifndef KTR_MASK
   75 #define KTR_MASK        (0)
   76 #endif
   77 
   78 #ifndef KTR_CPUMASK
   79 #define KTR_CPUMASK     CPUSET_FSET
   80 #endif
   81 
   82 #ifndef KTR_TIME
   83 #define KTR_TIME        get_cyclecount()
   84 #endif
   85 
   86 #ifndef KTR_CPU
   87 #define KTR_CPU         PCPU_GET(cpuid)
   88 #endif
   89 
   90 static MALLOC_DEFINE(M_KTR, "KTR", "KTR");
   91 
   92 FEATURE(ktr, "Kernel support for KTR kernel tracing facility");
   93 
   94 volatile int    ktr_idx = 0;
   95 int     ktr_mask = KTR_MASK;
   96 int     ktr_compile = KTR_COMPILE;
   97 int     ktr_entries = KTR_BOOT_ENTRIES;
   98 int     ktr_version = KTR_VERSION;
   99 struct  ktr_entry ktr_buf_init[KTR_BOOT_ENTRIES];
  100 struct  ktr_entry *ktr_buf = ktr_buf_init;
  101 cpuset_t ktr_cpumask = CPUSET_T_INITIALIZER(KTR_CPUMASK);
  102 static char ktr_cpumask_str[CPUSETBUFSIZ];
  103 
  104 TUNABLE_INT("debug.ktr.mask", &ktr_mask);
  105 
  106 TUNABLE_STR("debug.ktr.cpumask", ktr_cpumask_str, sizeof(ktr_cpumask_str));
  107 
  108 static SYSCTL_NODE(_debug, OID_AUTO, ktr, CTLFLAG_RD, 0, "KTR options");
  109 
  110 SYSCTL_INT(_debug_ktr, OID_AUTO, version, CTLFLAG_RD,
  111     &ktr_version, 0, "Version of the KTR interface");
  112 
  113 SYSCTL_UINT(_debug_ktr, OID_AUTO, compile, CTLFLAG_RD,
  114     &ktr_compile, 0, "Bitmask of KTR event classes compiled into the kernel");
  115 
  116 static void
  117 ktr_cpumask_initializer(void *dummy __unused)
  118 {
  119 
  120         /*
  121          * TUNABLE_STR() runs with SI_ORDER_MIDDLE priority, thus it must be
  122          * already set, if necessary.
  123          */
  124         if (ktr_cpumask_str[0] != '\0' &&
  125             cpusetobj_strscan(&ktr_cpumask, ktr_cpumask_str) == -1)
  126                 CPU_FILL(&ktr_cpumask);
  127 }
  128 SYSINIT(ktr_cpumask_initializer, SI_SUB_TUNABLES, SI_ORDER_ANY,
  129     ktr_cpumask_initializer, NULL);
  130 
  131 static int
  132 sysctl_debug_ktr_cpumask(SYSCTL_HANDLER_ARGS)
  133 {
  134         char lktr_cpumask_str[CPUSETBUFSIZ];
  135         cpuset_t imask;
  136         int error;
  137 
  138         cpusetobj_strprint(lktr_cpumask_str, &ktr_cpumask);
  139         error = sysctl_handle_string(oidp, lktr_cpumask_str,
  140             sizeof(lktr_cpumask_str), req);
  141         if (error != 0 || req->newptr == NULL)
  142                 return (error);
  143         if (cpusetobj_strscan(&imask, lktr_cpumask_str) == -1)
  144                 return (EINVAL);
  145         CPU_COPY(&imask, &ktr_cpumask);
  146 
  147         return (error);
  148 }
  149 SYSCTL_PROC(_debug_ktr, OID_AUTO, cpumask,
  150     CTLFLAG_RW | CTLFLAG_MPSAFE | CTLTYPE_STRING, NULL, 0,
  151     sysctl_debug_ktr_cpumask, "S",
  152     "Bitmask of CPUs on which KTR logging is enabled");
  153 
  154 static int
  155 sysctl_debug_ktr_clear(SYSCTL_HANDLER_ARGS)
  156 {
  157         int clear, error;
  158 
  159         clear = 0;
  160         error = sysctl_handle_int(oidp, &clear, 0, req);
  161         if (error || !req->newptr)
  162                 return (error);
  163 
  164         if (clear) {
  165                 bzero(ktr_buf, sizeof(*ktr_buf) * ktr_entries);
  166                 ktr_idx = 0;
  167         }
  168 
  169         return (error);
  170 }
  171 SYSCTL_PROC(_debug_ktr, OID_AUTO, clear, CTLTYPE_INT|CTLFLAG_RW, 0, 0,
  172     sysctl_debug_ktr_clear, "I", "Clear KTR Buffer");
  173 
  174 /*
  175  * This is a sysctl proc so that it is serialized as !MPSAFE along with
  176  * the other ktr sysctl procs.
  177  */
  178 static int
  179 sysctl_debug_ktr_mask(SYSCTL_HANDLER_ARGS)
  180 {
  181         int mask, error;
  182 
  183         mask = ktr_mask;
  184         error = sysctl_handle_int(oidp, &mask, 0, req);
  185         if (error || !req->newptr)
  186                 return (error);
  187         ktr_mask = mask;
  188         return (error);
  189 }
  190 
  191 SYSCTL_PROC(_debug_ktr, OID_AUTO, mask, CTLTYPE_UINT|CTLFLAG_RW, 0, 0,
  192     sysctl_debug_ktr_mask, "IU",
  193     "Bitmask of KTR event classes for which logging is enabled");
  194 
  195 #if KTR_ENTRIES > KTR_BOOT_ENTRIES
  196 /*
  197  * A simplified version of sysctl_debug_ktr_entries.
  198  * No need to care about SMP, scheduling, etc.
  199  */
  200 static void
  201 ktr_entries_initializer(void *dummy __unused)
  202 {
  203         int mask;
  204 
  205         /* Temporarily disable ktr in case malloc() is being traced. */
  206         mask = ktr_mask;
  207         ktr_mask = 0;
  208         ktr_buf = malloc(sizeof(*ktr_buf) * KTR_ENTRIES, M_KTR,
  209             M_WAITOK | M_ZERO);
  210         memcpy(ktr_buf, ktr_buf_init + ktr_idx,
  211             (KTR_BOOT_ENTRIES - ktr_idx) * sizeof(*ktr_buf));
  212         if (ktr_idx != 0)
  213                 memcpy(ktr_buf + KTR_BOOT_ENTRIES - ktr_idx, ktr_buf_init,
  214                     ktr_idx * sizeof(*ktr_buf));
  215         ktr_entries = KTR_ENTRIES;
  216         ktr_mask = mask;
  217 }
  218 SYSINIT(ktr_entries_initializer, SI_SUB_KMEM, SI_ORDER_ANY,
  219     ktr_entries_initializer, NULL);
  220 #endif
  221 
  222 static int
  223 sysctl_debug_ktr_entries(SYSCTL_HANDLER_ARGS)
  224 {
  225         int entries, error, mask;
  226         struct ktr_entry *buf, *oldbuf;
  227 
  228         entries = ktr_entries;
  229         error = sysctl_handle_int(oidp, &entries, 0, req);
  230         if (error || !req->newptr)
  231                 return (error);
  232         if (entries > KTR_ENTRIES_MAX)
  233                 return (ERANGE);
  234         /* Disable ktr temporarily. */
  235         mask = ktr_mask;
  236         atomic_store_rel_int(&ktr_mask, 0);
  237         /* Wait for threads to go idle. */
  238         if ((error = quiesce_all_cpus("ktrent", PCATCH)) != 0) {
  239                 ktr_mask = mask;
  240                 return (error);
  241         }
  242         if (ktr_buf != ktr_buf_init)
  243                 oldbuf = ktr_buf;
  244         else
  245                 oldbuf = NULL;
  246         /* Allocate a new buffer. */
  247         buf = malloc(sizeof(*buf) * entries, M_KTR, M_WAITOK | M_ZERO);
  248         /* Install the new buffer and restart ktr. */
  249         ktr_buf = buf;
  250         ktr_entries = entries;
  251         ktr_idx = 0;
  252         atomic_store_rel_int(&ktr_mask, mask);
  253         if (oldbuf != NULL)
  254                 free(oldbuf, M_KTR);
  255 
  256         return (error);
  257 }
  258 
  259 SYSCTL_PROC(_debug_ktr, OID_AUTO, entries, CTLTYPE_INT|CTLFLAG_RW, 0, 0,
  260     sysctl_debug_ktr_entries, "I", "Number of entries in the KTR buffer");
  261 
  262 #ifdef KTR_VERBOSE
  263 int     ktr_verbose = KTR_VERBOSE;
  264 TUNABLE_INT("debug.ktr.verbose", &ktr_verbose);
  265 SYSCTL_INT(_debug_ktr, OID_AUTO, verbose, CTLFLAG_RW, &ktr_verbose, 0, "");
  266 #endif
  267 
  268 #ifdef KTR_ALQ
  269 struct alq *ktr_alq;
  270 char    ktr_alq_file[MAXPATHLEN] = "/tmp/ktr.out";
  271 int     ktr_alq_cnt = 0;
  272 int     ktr_alq_depth = KTR_ENTRIES;
  273 int     ktr_alq_enabled = 0;
  274 int     ktr_alq_failed = 0;
  275 int     ktr_alq_max = 0;
  276 
  277 SYSCTL_INT(_debug_ktr, OID_AUTO, alq_max, CTLFLAG_RW, &ktr_alq_max, 0,
  278     "Maximum number of entries to write");
  279 SYSCTL_INT(_debug_ktr, OID_AUTO, alq_cnt, CTLFLAG_RD, &ktr_alq_cnt, 0,
  280     "Current number of written entries");
  281 SYSCTL_INT(_debug_ktr, OID_AUTO, alq_failed, CTLFLAG_RD, &ktr_alq_failed, 0,
  282     "Number of times we overran the buffer");
  283 SYSCTL_INT(_debug_ktr, OID_AUTO, alq_depth, CTLFLAG_RW, &ktr_alq_depth, 0,
  284     "Number of items in the write buffer");
  285 SYSCTL_STRING(_debug_ktr, OID_AUTO, alq_file, CTLFLAG_RW, ktr_alq_file,
  286     sizeof(ktr_alq_file), "KTR logging file");
  287 
  288 static int
  289 sysctl_debug_ktr_alq_enable(SYSCTL_HANDLER_ARGS)
  290 {
  291         int error;
  292         int enable;
  293 
  294         enable = ktr_alq_enabled;
  295 
  296         error = sysctl_handle_int(oidp, &enable, 0, req);
  297         if (error || !req->newptr)
  298                 return (error);
  299 
  300         if (enable) {
  301                 if (ktr_alq_enabled)
  302                         return (0);
  303                 error = alq_open(&ktr_alq, (const char *)ktr_alq_file,
  304                     req->td->td_ucred, ALQ_DEFAULT_CMODE,
  305                     sizeof(struct ktr_entry), ktr_alq_depth);
  306                 if (error == 0) {
  307                         ktr_alq_cnt = 0;
  308                         ktr_alq_failed = 0;
  309                         ktr_alq_enabled = 1;
  310                 }
  311         } else {
  312                 if (ktr_alq_enabled == 0)
  313                         return (0);
  314                 ktr_alq_enabled = 0;
  315                 alq_close(ktr_alq);
  316                 ktr_alq = NULL;
  317         }
  318 
  319         return (error);
  320 }
  321 SYSCTL_PROC(_debug_ktr, OID_AUTO, alq_enable,
  322     CTLTYPE_INT|CTLFLAG_RW, 0, 0, sysctl_debug_ktr_alq_enable,
  323     "I", "Enable KTR logging");
  324 #endif
  325 
  326 void
  327 ktr_tracepoint(u_int mask, const char *file, int line, const char *format,
  328     u_long arg1, u_long arg2, u_long arg3, u_long arg4, u_long arg5,
  329     u_long arg6)
  330 {
  331         struct ktr_entry *entry;
  332 #ifdef KTR_ALQ
  333         struct ale *ale = NULL;
  334 #endif
  335         int newindex, saveindex;
  336 #if defined(KTR_VERBOSE) || defined(KTR_ALQ)
  337         struct thread *td;
  338 #endif
  339         int cpu;
  340 
  341         if (panicstr)
  342                 return;
  343         if ((ktr_mask & mask) == 0 || ktr_buf == NULL)
  344                 return;
  345         cpu = KTR_CPU;
  346         if (!CPU_ISSET(cpu, &ktr_cpumask))
  347                 return;
  348 #if defined(KTR_VERBOSE) || defined(KTR_ALQ)
  349         td = curthread;
  350         if (td->td_pflags & TDP_INKTR)
  351                 return;
  352         td->td_pflags |= TDP_INKTR;
  353 #endif
  354 #ifdef KTR_ALQ
  355         if (ktr_alq_enabled) {
  356                 if (td->td_critnest == 0 &&
  357                     (td->td_flags & TDF_IDLETD) == 0 &&
  358                     td != ald_thread) {
  359                         if (ktr_alq_max && ktr_alq_cnt > ktr_alq_max)
  360                                 goto done;
  361                         if ((ale = alq_get(ktr_alq, ALQ_NOWAIT)) == NULL) {
  362                                 ktr_alq_failed++;
  363                                 goto done;
  364                         }
  365                         ktr_alq_cnt++;
  366                         entry = (struct ktr_entry *)ale->ae_data;
  367                 } else {
  368                         goto done;
  369                 }
  370         } else
  371 #endif
  372         {
  373                 do {
  374                         saveindex = ktr_idx;
  375                         newindex = (saveindex + 1) % ktr_entries;
  376                 } while (atomic_cmpset_rel_int(&ktr_idx, saveindex, newindex) == 0);
  377                 entry = &ktr_buf[saveindex];
  378         }
  379         entry->ktr_timestamp = KTR_TIME;
  380         entry->ktr_cpu = cpu;
  381         entry->ktr_thread = curthread;
  382         if (file != NULL)
  383                 while (strncmp(file, "../", 3) == 0)
  384                         file += 3;
  385         entry->ktr_file = file;
  386         entry->ktr_line = line;
  387 #ifdef KTR_VERBOSE
  388         if (ktr_verbose) {
  389 #ifdef SMP
  390                 printf("cpu%d ", cpu);
  391 #endif
  392                 if (ktr_verbose > 1) {
  393                         printf("%s.%d\t", entry->ktr_file,
  394                             entry->ktr_line);
  395                 }
  396                 printf(format, arg1, arg2, arg3, arg4, arg5, arg6);
  397                 printf("\n");
  398         }
  399 #endif
  400         entry->ktr_desc = format;
  401         entry->ktr_parms[0] = arg1;
  402         entry->ktr_parms[1] = arg2;
  403         entry->ktr_parms[2] = arg3;
  404         entry->ktr_parms[3] = arg4;
  405         entry->ktr_parms[4] = arg5;
  406         entry->ktr_parms[5] = arg6;
  407 #ifdef KTR_ALQ
  408         if (ktr_alq_enabled && ale)
  409                 alq_post(ktr_alq, ale);
  410 done:
  411 #endif
  412 #if defined(KTR_VERBOSE) || defined(KTR_ALQ)
  413         td->td_pflags &= ~TDP_INKTR;
  414 #endif
  415 }
  416 
  417 #ifdef DDB
  418 
  419 struct tstate {
  420         int     cur;
  421         int     first;
  422 };
  423 static  struct tstate tstate;
  424 static  int db_ktr_verbose;
  425 static  int db_mach_vtrace(void);
  426 
  427 DB_SHOW_COMMAND(ktr, db_ktr_all)
  428 {
  429         
  430         tstate.cur = (ktr_idx - 1) % ktr_entries;
  431         tstate.first = -1;
  432         db_ktr_verbose = 0;
  433         db_ktr_verbose |= (strchr(modif, 'v') != NULL) ? 2 : 0;
  434         db_ktr_verbose |= (strchr(modif, 'V') != NULL) ? 1 : 0; /* just timestap please */
  435         if (strchr(modif, 'a') != NULL) {
  436                 db_disable_pager();
  437                 while (cncheckc() != -1)
  438                         if (db_mach_vtrace() == 0)
  439                                 break;
  440         } else {
  441                 while (!db_pager_quit)
  442                         if (db_mach_vtrace() == 0)
  443                                 break;
  444         }
  445 }
  446 
  447 static int
  448 db_mach_vtrace(void)
  449 {
  450         struct ktr_entry        *kp;
  451 
  452         if (tstate.cur == tstate.first || ktr_buf == NULL) {
  453                 db_printf("--- End of trace buffer ---\n");
  454                 return (0);
  455         }
  456         kp = &ktr_buf[tstate.cur];
  457 
  458         /* Skip over unused entries. */
  459         if (kp->ktr_desc == NULL) {
  460                 db_printf("--- End of trace buffer ---\n");
  461                 return (0);
  462         }
  463         db_printf("%d (%p", tstate.cur, kp->ktr_thread);
  464 #ifdef SMP
  465         db_printf(":cpu%d", kp->ktr_cpu);
  466 #endif
  467         db_printf(")");
  468         if (db_ktr_verbose >= 1) {
  469                 db_printf(" %10.10lld", (long long)kp->ktr_timestamp);
  470         }
  471         if (db_ktr_verbose >= 2) {
  472                 db_printf(" %s.%d", kp->ktr_file, kp->ktr_line);
  473         }
  474         db_printf(": ");
  475         db_printf(kp->ktr_desc, kp->ktr_parms[0], kp->ktr_parms[1],
  476             kp->ktr_parms[2], kp->ktr_parms[3], kp->ktr_parms[4],
  477             kp->ktr_parms[5]);
  478         db_printf("\n");
  479 
  480         if (tstate.first == -1)
  481                 tstate.first = tstate.cur;
  482 
  483         if (--tstate.cur < 0)
  484                 tstate.cur = ktr_entries - 1;
  485 
  486         return (1);
  487 }
  488 
  489 #endif  /* DDB */

Cache object: 7eb93d7d8f0d8295f4ad055f4ae04bf6


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