xref: /freebsd/usr.bin/procstat/procstat_kstack.c (revision adf376485712c8fffbf3be330d505a969647f479)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2007 Robert N. M. Watson
5  * Copyright (c) 2015 Allan Jude <allanjude@freebsd.org>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
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 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <sys/param.h>
34 #include <sys/sysctl.h>
35 #include <sys/user.h>
36 
37 #include <err.h>
38 #include <errno.h>
39 #include <libprocstat.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 
44 #include "procstat.h"
45 
46 /*
47  * Walk the stack trace provided by the kernel and reduce it to what we
48  * actually want to print.  This involves stripping true instruction pointers,
49  * frame numbers, and carriage returns as generated by stack(9).  If -kk is
50  * specified, print the function and offset, otherwise just the function.
51  */
52 enum trace_state { TS_FRAMENUM, TS_PC, TS_AT, TS_FUNC, TS_OFF };
53 
54 static enum trace_state
55 kstack_nextstate(enum trace_state ts)
56 {
57 
58 	switch (ts) {
59 	case TS_FRAMENUM:
60 		return (TS_PC);
61 
62 	case TS_PC:
63 		return (TS_AT);
64 
65 	case TS_AT:
66 		return (TS_FUNC);
67 
68 	case TS_FUNC:
69 		return (TS_OFF);
70 
71 	case TS_OFF:
72 		return (TS_FRAMENUM);
73 
74 	default:
75 		errx(-1, "kstack_nextstate");
76 	}
77 }
78 
79 static void
80 kstack_cleanup(const char *old, char *new, int kflag)
81 {
82 	enum trace_state old_ts, ts;
83 	const char *cp_old;
84 	char *cp_new;
85 
86 	ts = TS_FRAMENUM;
87 	for (cp_old = old, cp_new = new; *cp_old != '\0'; cp_old++) {
88 		switch (*cp_old) {
89 		case ' ':
90 		case '\n':
91 		case '+':
92 			old_ts = ts;
93 			ts = kstack_nextstate(old_ts);
94 			if (old_ts == TS_OFF) {
95 				*cp_new = ' ';
96 				cp_new++;
97 			}
98 			if (kflag > 1 && old_ts == TS_FUNC) {
99 				*cp_new = '+';
100 				cp_new++;
101 			}
102 			continue;
103 		}
104 		if (ts == TS_FUNC || (kflag > 1 && ts == TS_OFF)) {
105 			*cp_new = *cp_old;
106 			cp_new++;
107 		}
108 	}
109 	*cp_new = '\0';
110 }
111 
112 static void
113 kstack_cleanup_encoded(const char *old, char *new, int kflag)
114 {
115 	enum trace_state old_ts, ts;
116 	const char *cp_old;
117 	char *cp_new, *cp_loop, *cp_tofree, *cp_line;
118 
119 	ts = TS_FRAMENUM;
120 	if (kflag == 1) {
121 		for (cp_old = old, cp_new = new; *cp_old != '\0'; cp_old++) {
122 			switch (*cp_old) {
123 			case '\n':
124 				*cp_new = *cp_old;
125 				cp_new++;
126 			case ' ':
127 			case '+':
128 				old_ts = ts;
129 				ts = kstack_nextstate(old_ts);
130 				continue;
131 			}
132 			if (ts == TS_FUNC) {
133 				*cp_new = *cp_old;
134 				cp_new++;
135 			}
136 		}
137 		*cp_new = '\0';
138 		cp_tofree = cp_loop = strdup(new);
139 	} else
140 		cp_tofree = cp_loop = strdup(old);
141         while ((cp_line = strsep(&cp_loop, "\n")) != NULL) {
142 		if (strlen(cp_line) != 0 && *cp_line != 127)
143 			xo_emit("{le:token/%s}", cp_line);
144 	}
145 	free(cp_tofree);
146 }
147 
148 /*
149  * Sort threads by tid.
150  */
151 static int
152 kinfo_kstack_compare(const void *a, const void *b)
153 {
154 
155         return ((const struct kinfo_kstack *)a)->kkst_tid -
156             ((const struct kinfo_kstack *)b)->kkst_tid;
157 }
158 
159 static void
160 kinfo_kstack_sort(struct kinfo_kstack *kkstp, int count)
161 {
162 
163         qsort(kkstp, count, sizeof(*kkstp), kinfo_kstack_compare);
164 }
165 
166 
167 void
168 procstat_kstack(struct procstat *procstat, struct kinfo_proc *kipp)
169 {
170 	struct kinfo_kstack *kkstp, *kkstp_free;
171 	struct kinfo_proc *kip, *kip_free;
172 	char trace[KKST_MAXLEN], encoded_trace[KKST_MAXLEN];
173 	unsigned int i, j;
174 	unsigned int kip_count, kstk_count;
175 
176 	if ((procstat_opts & PS_OPT_NOHEADER) == 0)
177 		xo_emit("{T:/%5s %6s %-19s %-19s %-29s}\n", "PID", "TID", "COMM",
178 		    "TDNAME", "KSTACK");
179 
180 	kkstp = kkstp_free = procstat_getkstack(procstat, kipp, &kstk_count);
181 	if (kkstp == NULL)
182 		return;
183 
184 	/*
185 	 * We need to re-query for thread information, so don't use *kipp.
186 	 */
187 	kip = kip_free = procstat_getprocs(procstat,
188 	    KERN_PROC_PID | KERN_PROC_INC_THREAD, kipp->ki_pid, &kip_count);
189 
190 	if (kip == NULL) {
191 		procstat_freekstack(procstat, kkstp_free);
192 		return;
193 	}
194 
195 	kinfo_kstack_sort(kkstp, kstk_count);
196 	for (i = 0; i < kstk_count; i++) {
197 		kkstp = &kkstp_free[i];
198 
199 		/*
200 		 * Look up the specific thread using its tid so we can
201 		 * display the per-thread command line.
202 		 */
203 		kipp = NULL;
204 		for (j = 0; j < kip_count; j++) {
205 			kipp = &kip_free[j];
206 			if (kkstp->kkst_tid == kipp->ki_tid)
207 				break;
208 		}
209 		if (kipp == NULL)
210 			continue;
211 
212 		xo_emit("{k:process_id/%5d/%d} ", kipp->ki_pid);
213 		xo_emit("{:thread_id/%6d/%d} ", kkstp->kkst_tid);
214 		xo_emit("{:command/%-19s/%s} ", kipp->ki_comm);
215 		xo_emit("{:thread_name/%-19s/%s} ",
216                     kinfo_proc_thread_name(kipp));
217 
218 		switch (kkstp->kkst_state) {
219 		case KKST_STATE_RUNNING:
220 			xo_emit("{:state/%-29s/%s}\n", "<running>");
221 			continue;
222 
223 		case KKST_STATE_SWAPPED:
224 			xo_emit("{:state/%-29s/%s}\n", "<swapped>");
225 			continue;
226 
227 		case KKST_STATE_STACKOK:
228 			break;
229 
230 		default:
231 			xo_emit("{:state/%-29s/%s}\n", "<unknown>");
232 			continue;
233 		}
234 
235 		/*
236 		 * The kernel generates a trace with carriage returns between
237 		 * entries, but for a more compact view, we convert carriage
238 		 * returns to spaces.
239 		 */
240 		kstack_cleanup(kkstp->kkst_trace, trace,
241 		    (procstat_opts & PS_OPT_VERBOSE) != 0 ? 2 : 1);
242 		xo_open_list("trace");
243 		kstack_cleanup_encoded(kkstp->kkst_trace, encoded_trace,
244 		    (procstat_opts & PS_OPT_VERBOSE) != 0 ? 2 : 1);
245 		xo_close_list("trace");
246 		xo_emit("{d:trace/%-29s}\n", trace);
247 	}
248 	procstat_freekstack(procstat, kkstp_free);
249 	procstat_freeprocs(procstat, kip_free);
250 }
251