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