xref: /freebsd/usr.bin/procstat/procstat_kstack.c (revision 195ebc7e9e4b129de810833791a19dfb4349d6a9)
1 /*-
2  * Copyright (c) 2007 Robert N. M. Watson
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  * $FreeBSD$
27  */
28 
29 #include <sys/param.h>
30 #include <sys/sysctl.h>
31 #include <sys/user.h>
32 
33 #include <err.h>
34 #include <errno.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 
39 #include "procstat.h"
40 
41 /*
42  * Walk the stack trace provided by the kernel and reduce it to what we
43  * actually want to print.  This involves stripping true instruction pointers,
44  * frame numbers, and carriage returns as generated by stack(9).  If -kk is
45  * specified, print the function and offset, otherwise just the function.
46  */
47 enum trace_state { TS_FRAMENUM, TS_PC, TS_AT, TS_FUNC, TS_OFF };
48 
49 static enum trace_state
50 kstack_nextstate(enum trace_state ts)
51 {
52 
53 	switch (ts) {
54 	case TS_FRAMENUM:
55 		return (TS_PC);
56 
57 	case TS_PC:
58 		return (TS_AT);
59 
60 	case TS_AT:
61 		return (TS_FUNC);
62 
63 	case TS_FUNC:
64 		return (TS_OFF);
65 
66 	case TS_OFF:
67 		return TS_FRAMENUM;
68 
69 	default:
70 		errx(-1, "kstack_nextstate");
71 	}
72 }
73 
74 static void
75 kstack_cleanup(const char *old, char *new, int kflag)
76 {
77 	enum trace_state old_ts, ts;
78 	const char *cp_old;
79 	char *cp_new;
80 
81 	ts = TS_FRAMENUM;
82 	for (cp_old = old, cp_new = new; *cp_old != '\0'; cp_old++) {
83 		switch (*cp_old) {
84 		case ' ':
85 		case '\n':
86 		case '+':
87 			old_ts = ts;
88 			ts = kstack_nextstate(old_ts);
89 			if (old_ts == TS_OFF) {
90 				*cp_new = ' ';
91 				cp_new++;
92 			}
93 			if (kflag > 1 && old_ts == TS_FUNC) {
94 				*cp_new = '+';
95 				cp_new++;
96 			}
97 			continue;
98 		}
99 		if (ts == TS_FUNC || (kflag > 1 && ts == TS_OFF)) {
100 			*cp_new = *cp_old;
101 			cp_new++;
102 		}
103 	}
104 	*cp_new = '\0';
105 }
106 
107 /*
108  * Sort threads by tid.
109  */
110 static int
111 kinfo_kstack_compare(const void *a, const void *b)
112 {
113 
114         return ((const struct kinfo_kstack *)a)->kkst_tid -
115             ((const struct kinfo_kstack *)b)->kkst_tid;
116 }
117 
118 static void
119 kinfo_kstack_sort(struct kinfo_kstack *kkstp, int count)
120 {
121 
122         qsort(kkstp, count, sizeof(*kkstp), kinfo_kstack_compare);
123 }
124 
125 
126 void
127 procstat_kstack(pid_t pid, struct kinfo_proc *kipp, int kflag)
128 {
129 	struct kinfo_kstack *kkstp, *kkstp_free;
130 	struct kinfo_proc *kip, *kip_free;
131 	char trace[KKST_MAXLEN];
132 	int error, name[4];
133 	unsigned int i, j;
134 	size_t kip_len, kstk_len;
135 
136 	if (!hflag)
137 		printf("%5s %6s %-16s %-16s %-29s\n", "PID", "TID", "COMM",
138 		    "TDNAME", "KSTACK");
139 
140 	name[0] = CTL_KERN;
141 	name[1] = KERN_PROC;
142 	name[2] = KERN_PROC_KSTACK;
143 	name[3] = pid;
144 
145 	kstk_len = 0;
146 	error = sysctl(name, 4, NULL, &kstk_len, NULL, 0);
147 	if (error < 0 && errno != ESRCH && errno != EPERM && errno != ENOENT) {
148 		warn("sysctl: kern.proc.kstack: %d", pid);
149 		return;
150 	}
151 	if (error < 0 && errno == ENOENT) {
152 		warnx("sysctl: kern.proc.kstack unavailable");
153 		errx(-1, "options DDB or options STACK required in kernel");
154 	}
155 	if (error < 0)
156 		return;
157 
158 	kkstp = kkstp_free = malloc(kstk_len);
159 	if (kkstp == NULL)
160 		err(-1, "malloc");
161 
162 	if (sysctl(name, 4, kkstp, &kstk_len, NULL, 0) < 0) {
163 		warn("sysctl: kern.proc.pid: %d", pid);
164 		free(kkstp);
165 		return;
166 	}
167 
168 	/*
169 	 * We need to re-query for thread information, so don't use *kipp.
170 	 */
171 	name[0] = CTL_KERN;
172 	name[1] = KERN_PROC;
173 	name[2] = KERN_PROC_PID | KERN_PROC_INC_THREAD;
174 	name[3] = pid;
175 
176 	kip_len = 0;
177 	error = sysctl(name, 4, NULL, &kip_len, NULL, 0);
178 	if (error < 0 && errno != ESRCH) {
179 		warn("sysctl: kern.proc.pid: %d", pid);
180 		return;
181 	}
182 	if (error < 0)
183 		return;
184 
185 	kip = kip_free = malloc(kip_len);
186 	if (kip == NULL)
187 		err(-1, "malloc");
188 
189 	if (sysctl(name, 4, kip, &kip_len, NULL, 0) < 0) {
190 		warn("sysctl: kern.proc.pid: %d", pid);
191 		free(kip);
192 		return;
193 	}
194 
195 	kinfo_kstack_sort(kkstp, kstk_len / sizeof(*kkstp));
196 	for (i = 0; i < kstk_len / sizeof(*kkstp); 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_len / sizeof(*kipp); 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 		printf("%5d ", pid);
213 		printf("%6d ", kkstp->kkst_tid);
214 		printf("%-16s ", kipp->ki_comm);
215 		printf("%-16s ", (strlen(kipp->ki_ocomm) &&
216 		    (strcmp(kipp->ki_comm, kipp->ki_ocomm) != 0)) ?
217 		    kipp->ki_ocomm : "-");
218 
219 		switch (kkstp->kkst_state) {
220 		case KKST_STATE_RUNNING:
221 			printf("%-29s\n", "<running>");
222 			continue;
223 
224 		case KKST_STATE_SWAPPED:
225 			printf("%-29s\n", "<swapped>");
226 			continue;
227 
228 		case KKST_STATE_STACKOK:
229 			break;
230 
231 		default:
232 			printf("%-29s\n", "<unknown>");
233 			continue;
234 		}
235 
236 		/*
237 		 * The kernel generates a trace with carriage returns between
238 		 * entries, but for a more compact view, we convert carriage
239 		 * returns to spaces.
240 		 */
241 		kstack_cleanup(kkstp->kkst_trace, trace, kflag);
242 		printf("%-29s\n", trace);
243 	}
244 	free(kip_free);
245 	free(kkstp_free);
246 }
247