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