xref: /freebsd/sys/kern/kern_tslog.c (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
1 /*-
2  * Copyright (c) 2017 Colin Percival
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 #include <sys/cdefs.h>
28 #include <sys/param.h>
29 #include <sys/kernel.h>
30 #include <sys/linker.h>
31 #include <sys/malloc.h>
32 #include <sys/proc.h>
33 #include <sys/sbuf.h>
34 #include <sys/sysctl.h>
35 #include <sys/systm.h>
36 #include <sys/tslog.h>
37 
38 #include <machine/atomic.h>
39 #include <machine/cpu.h>
40 
41 #ifndef TSLOGSIZE
42 #define TSLOGSIZE 262144
43 #endif
44 
45 static volatile long nrecs = 0;
46 static struct timestamp {
47 	void * td;
48 	int type;
49 	const char * f;
50 	const char * s;
51 	uint64_t tsc;
52 } timestamps[TSLOGSIZE];
53 
54 void
55 tslog(void * td, int type, const char * f, const char * s)
56 {
57 	uint64_t tsc = get_cyclecount();
58 	long pos;
59 
60 	/* A NULL thread is thread0 before curthread is set. */
61 	if (td == NULL)
62 		td = &thread0;
63 
64 	/* Grab a slot. */
65 	pos = atomic_fetchadd_long(&nrecs, 1);
66 
67 	/* Store record. */
68 	if (pos < nitems(timestamps)) {
69 		timestamps[pos].td = td;
70 		timestamps[pos].type = type;
71 		timestamps[pos].f = f;
72 		timestamps[pos].s = s;
73 		timestamps[pos].tsc = tsc;
74 	}
75 }
76 
77 static int
78 sysctl_debug_tslog(SYSCTL_HANDLER_ARGS)
79 {
80 	int error;
81 	struct sbuf *sb;
82 	size_t i, limit;
83 	caddr_t loader_tslog;
84 	void * loader_tslog_buf;
85 	size_t loader_tslog_len;
86 
87 	/*
88 	 * This code can race against the code in tslog() which stores
89 	 * records: Theoretically we could end up reading a record after
90 	 * its slots have been reserved but before it has been written.
91 	 * Since this code takes orders of magnitude longer to run than
92 	 * tslog() takes to write a record, it is highly unlikely that
93 	 * anyone will ever experience this race.
94 	 */
95 	sb = sbuf_new_for_sysctl(NULL, NULL, 1024, req);
96 
97 	/* Get data from the boot loader, if it provided any. */
98 	loader_tslog = preload_search_by_type("TSLOG data");
99 	if (loader_tslog != NULL) {
100 		loader_tslog_buf = preload_fetch_addr(loader_tslog);
101 		loader_tslog_len = preload_fetch_size(loader_tslog);
102 		sbuf_bcat(sb, loader_tslog_buf, loader_tslog_len);
103 	}
104 
105 	/* Add data logged within the kernel. */
106 	limit = MIN(nrecs, nitems(timestamps));
107 	for (i = 0; i < limit; i++) {
108 		sbuf_printf(sb, "%p", timestamps[i].td);
109 		sbuf_printf(sb, " %llu",
110 		    (unsigned long long)timestamps[i].tsc);
111 		switch (timestamps[i].type) {
112 		case TS_ENTER:
113 			sbuf_printf(sb, " ENTER");
114 			break;
115 		case TS_EXIT:
116 			sbuf_printf(sb, " EXIT");
117 			break;
118 		case TS_THREAD:
119 			sbuf_printf(sb, " THREAD");
120 			break;
121 		case TS_EVENT:
122 			sbuf_printf(sb, " EVENT");
123 			break;
124 		}
125 		sbuf_printf(sb, " %s", timestamps[i].f ? timestamps[i].f : "(null)");
126 		if (timestamps[i].s)
127 			sbuf_printf(sb, " %s\n", timestamps[i].s);
128 		else
129 			sbuf_printf(sb, "\n");
130 	}
131 	error = sbuf_finish(sb);
132 	sbuf_delete(sb);
133 	return (error);
134 }
135 
136 SYSCTL_PROC(_debug, OID_AUTO, tslog,
137     CTLTYPE_STRING|CTLFLAG_RD|CTLFLAG_MPSAFE|CTLFLAG_SKIP,
138     0, 0, sysctl_debug_tslog, "", "Dump recorded event timestamps");
139 
140 MALLOC_DEFINE(M_TSLOGUSER, "tsloguser", "Strings used by userland tslog");
141 static struct procdata {
142 	pid_t ppid;
143 	uint64_t tsc_forked;
144 	uint64_t tsc_exited;
145 	char * execname;
146 	char * namei;
147 	int reused;
148 } procs[PID_MAX + 1];
149 
150 void
151 tslog_user(pid_t pid, pid_t ppid, const char * execname, const char * namei)
152 {
153 	uint64_t tsc = get_cyclecount();
154 
155 	/* If we wrapped, do nothing. */
156 	if (procs[pid].reused)
157 		return;
158 
159 	/* If we have a ppid, we're recording a fork. */
160 	if (ppid != (pid_t)(-1)) {
161 		/* If we have a ppid already, we wrapped. */
162 		if (procs[pid].ppid) {
163 			procs[pid].reused = 1;
164 			return;
165 		}
166 
167 		/* Fill in some fields. */
168 		procs[pid].ppid = ppid;
169 		procs[pid].tsc_forked = tsc;
170 		return;
171 	}
172 
173 	/* If we have an execname, record it. */
174 	if (execname != NULL) {
175 		if (procs[pid].execname != NULL)
176 			free(procs[pid].execname, M_TSLOGUSER);
177 		procs[pid].execname = strdup(execname, M_TSLOGUSER);
178 		return;
179 	}
180 
181 	/* Record the first namei for the process. */
182 	if (namei != NULL) {
183 		if (procs[pid].namei == NULL)
184 			procs[pid].namei = strdup(namei, M_TSLOGUSER);
185 		return;
186 	}
187 
188 	/* Otherwise we're recording an exit. */
189 	procs[pid].tsc_exited = tsc;
190 }
191 
192 static int
193 sysctl_debug_tslog_user(SYSCTL_HANDLER_ARGS)
194 {
195 	int error;
196 	struct sbuf *sb;
197 	pid_t pid;
198 
199 	sb = sbuf_new_for_sysctl(NULL, NULL, 1024, req);
200 
201 	/* Export the data we logged. */
202 	for (pid = 0; pid <= PID_MAX; pid++) {
203 		sbuf_printf(sb, "%zu", (size_t)pid);
204 		sbuf_printf(sb, " %zu", (size_t)procs[pid].ppid);
205 		sbuf_printf(sb, " %llu",
206 		    (unsigned long long)procs[pid].tsc_forked);
207 		sbuf_printf(sb, " %llu",
208 		    (unsigned long long)procs[pid].tsc_exited);
209 		sbuf_printf(sb, " \"%s\"", procs[pid].execname ?
210 		    procs[pid].execname : "");
211 		sbuf_printf(sb, " \"%s\"", procs[pid].namei ?
212 		    procs[pid].namei : "");
213 		sbuf_printf(sb, "\n");
214 	}
215 	error = sbuf_finish(sb);
216 	sbuf_delete(sb);
217 	return (error);
218 }
219 
220 SYSCTL_PROC(_debug, OID_AUTO, tslog_user,
221     CTLTYPE_STRING|CTLFLAG_RD|CTLFLAG_MPSAFE|CTLFLAG_SKIP,
222     0, 0, sysctl_debug_tslog_user,
223     "", "Dump recorded userland event timestamps");
224