1 /*-
2  * Copyright (c) 2007 Robert N. M. Watson
3  * Copyright (c) 2015 Allan Jude <allanjude@freebsd.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD$
28  */
29 
30 #include <sys/param.h>
31 #include <sys/sysctl.h>
32 #include <sys/user.h>
33 
34 #include <err.h>
35 #include <errno.h>
36 #include <libprocstat.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 
41 #include "procstat.h"
42 
43 /*
44  * Walk the stack trace provided by the kernel and reduce it to what we
45  * actually want to print.  This involves stripping true instruction pointers,
46  * frame numbers, and carriage returns as generated by stack(9).  If -kk is
47  * specified, print the function and offset, otherwise just the function.
48  */
49 enum trace_state { TS_FRAMENUM, TS_PC, TS_AT, TS_FUNC, TS_OFF };
50 
51 static enum trace_state
52 kstack_nextstate(enum trace_state ts)
53 {
54 
55 	switch (ts) {
56 	case TS_FRAMENUM:
57 		return (TS_PC);
58 
59 	case TS_PC:
60 		return (TS_AT);
61 
62 	case TS_AT:
63 		return (TS_FUNC);
64 
65 	case TS_FUNC:
66 		return (TS_OFF);
67 
68 	case TS_OFF:
69 		return TS_FRAMENUM;
70 
71 	default:
72 		errx(-1, "kstack_nextstate");
73 	}
74 }
75 
76 static void
77 kstack_cleanup(const char *old, char *new, int kflag)
78 {
79 	enum trace_state old_ts, ts;
80 	const char *cp_old;
81 	char *cp_new;
82 
83 	ts = TS_FRAMENUM;
84 	for (cp_old = old, cp_new = new; *cp_old != '\0'; cp_old++) {
85 		switch (*cp_old) {
86 		case ' ':
87 		case '\n':
88 		case '+':
89 			old_ts = ts;
90 			ts = kstack_nextstate(old_ts);
91 			if (old_ts == TS_OFF) {
92 				*cp_new = ' ';
93 				cp_new++;
94 			}
95 			if (kflag > 1 && old_ts == TS_FUNC) {
96 				*cp_new = '+';
97 				cp_new++;
98 			}
99 			continue;
100 		}
101 		if (ts == TS_FUNC || (kflag > 1 && ts == TS_OFF)) {
102 			*cp_new = *cp_old;
103 			cp_new++;
104 		}
105 	}
106 	*cp_new = '\0';
107 }
108 
109 static void
110 kstack_cleanup_encoded(const char *old, char *new, int kflag)
111 {
112 	enum trace_state old_ts, ts;
113 	const char *cp_old;
114 	char *cp_new, *cp_loop, *cp_tofree, *cp_line;
115 
116 	ts = TS_FRAMENUM;
117 	if (kflag == 1) {
118 		for (cp_old = old, cp_new = new; *cp_old != '\0'; cp_old++) {
119 			switch (*cp_old) {
120 			case '\n':
121 				*cp_new = *cp_old;
122 				cp_new++;
123 			case ' ':
124 			case '+':
125 				old_ts = ts;
126 				ts = kstack_nextstate(old_ts);
127 				continue;
128 			}
129 			if (ts == TS_FUNC) {
130 				*cp_new = *cp_old;
131 				cp_new++;
132 			}
133 		}
134 		*cp_new = '\0';
135 		cp_tofree = cp_loop = strdup(new);
136 	} else
137 		cp_tofree = cp_loop = strdup(old);
138         while ((cp_line = strsep(&cp_loop, "\n")) != NULL) {
139 		if (strlen(cp_line) != 0 && *cp_line != 127)
140 			xo_emit("{le:token/%s}", cp_line);
141 	}
142 	free(cp_tofree);
143 }
144 
145 /*
146  * Sort threads by tid.
147  */
148 static int
149 kinfo_kstack_compare(const void *a, const void *b)
150 {
151 
152         return ((const struct kinfo_kstack *)a)->kkst_tid -
153             ((const struct kinfo_kstack *)b)->kkst_tid;
154 }
155 
156 static void
157 kinfo_kstack_sort(struct kinfo_kstack *kkstp, int count)
158 {
159 
160         qsort(kkstp, count, sizeof(*kkstp), kinfo_kstack_compare);
161 }
162 
163 
164 void
165 procstat_kstack(struct procstat *procstat, struct kinfo_proc *kipp, int kflag)
166 {
167 	struct kinfo_kstack *kkstp, *kkstp_free;
168 	struct kinfo_proc *kip, *kip_free;
169 	char trace[KKST_MAXLEN], encoded_trace[KKST_MAXLEN];
170 	unsigned int i, j;
171 	unsigned int kip_count, kstk_count;
172 
173 	if (!hflag)
174 		xo_emit("{T:/%5s %6s %-16s %-16s %-29s}\n", "PID", "TID", "COMM",
175 		    "TDNAME", "KSTACK");
176 
177 	kkstp = kkstp_free = procstat_getkstack(procstat, kipp, &kstk_count);
178 	if (kkstp == NULL)
179 		return;
180 
181 	/*
182 	 * We need to re-query for thread information, so don't use *kipp.
183 	 */
184 	kip = kip_free = procstat_getprocs(procstat,
185 	    KERN_PROC_PID | KERN_PROC_INC_THREAD, kipp->ki_pid, &kip_count);
186 
187 	if (kip == NULL) {
188 		procstat_freekstack(procstat, kkstp_free);
189 		return;
190 	}
191 
192 	kinfo_kstack_sort(kkstp, kstk_count);
193 	for (i = 0; i < kstk_count; i++) {
194 		kkstp = &kkstp_free[i];
195 
196 		/*
197 		 * Look up the specific thread using its tid so we can
198 		 * display the per-thread command line.
199 		 */
200 		kipp = NULL;
201 		for (j = 0; j < kip_count; j++) {
202 			kipp = &kip_free[j];
203 			if (kkstp->kkst_tid == kipp->ki_tid)
204 				break;
205 		}
206 		if (kipp == NULL)
207 			continue;
208 
209 		xo_emit("{k:process_id/%5d/%d} ", kipp->ki_pid);
210 		xo_emit("{:thread_id/%6d/%d} ", kkstp->kkst_tid);
211 		xo_emit("{:command/%-16s/%s} ", kipp->ki_comm);
212 		xo_emit("{:thread_name/%-16s/%s} ", (strlen(kipp->ki_tdname) &&
213 		    (strcmp(kipp->ki_comm, kipp->ki_tdname) != 0)) ?
214 		    kipp->ki_tdname : "-");
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, kflag);
239 		xo_open_list("trace");
240 		kstack_cleanup_encoded(kkstp->kkst_trace, encoded_trace, kflag);
241 		xo_close_list("trace");
242 		xo_emit("{d:trace/%-29s}\n", trace);
243 	}
244 	procstat_freekstack(procstat, kkstp_free);
245 	procstat_freeprocs(procstat, kip_free);
246 }
247