xref: /freebsd/usr.bin/procstat/procstat_kstack.c (revision 1e413cf93298b5b97441a21d9a50fdcd0ee9945e)
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/types.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 ((struct kinfo_kstack *)a)->kkst_tid -
115             ((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, i, j, name[4];
133 	size_t kip_len, kstk_len;
134 
135 	if (!hflag)
136 		printf("%5s %6s %-16s %-16s %-29s\n", "PID", "TID", "COMM",
137 		    "TDNAME", "KSTACK");
138 
139 	name[0] = CTL_KERN;
140 	name[1] = KERN_PROC;
141 	name[2] = KERN_PROC_KSTACK;
142 	name[3] = pid;
143 
144 	kstk_len = 0;
145 	error = sysctl(name, 4, NULL, &kstk_len, NULL, 0);
146 	if (error < 0 && errno != ESRCH && errno != EPERM && errno != ENOENT) {
147 		warn("sysctl: kern.proc.kstack: %d", pid);
148 		return;
149 	}
150 	if (error < 0 && errno == ENOENT) {
151 		warnx("sysctl: kern.proc.kstack unavailable");
152 		errx(-1, "options DDB or options STACK required in kernel");
153 	}
154 	if (error < 0)
155 		return;
156 
157 	kkstp = kkstp_free = malloc(kstk_len);
158 	if (kkstp == NULL)
159 		err(-1, "malloc");
160 
161 	if (sysctl(name, 4, kkstp, &kstk_len, NULL, 0) < 0) {
162 		warn("sysctl: kern.proc.pid: %d", pid);
163 		free(kkstp);
164 		return;
165 	}
166 
167 	/*
168 	 * We need to re-query for thread information, so don't use *kipp.
169 	 */
170 	name[0] = CTL_KERN;
171 	name[1] = KERN_PROC;
172 	name[2] = KERN_PROC_PID | KERN_PROC_INC_THREAD;
173 	name[3] = pid;
174 
175 	kip_len = 0;
176 	error = sysctl(name, 4, NULL, &kip_len, NULL, 0);
177 	if (error < 0 && errno != ESRCH) {
178 		warn("sysctl: kern.proc.pid: %d", pid);
179 		return;
180 	}
181 	if (error < 0)
182 		return;
183 
184 	kip = kip_free = malloc(kip_len);
185 	if (kip == NULL)
186 		err(-1, "malloc");
187 
188 	if (sysctl(name, 4, kip, &kip_len, NULL, 0) < 0) {
189 		warn("sysctl: kern.proc.pid: %d", pid);
190 		free(kip);
191 		return;
192 	}
193 
194 	kinfo_kstack_sort(kkstp, kstk_len / sizeof(*kkstp));
195 	for (i = 0; i < kstk_len / sizeof(*kkstp); i++) {
196 		kkstp = &kkstp_free[i];
197 
198 		/*
199 		 * Look up the specific thread using its tid so we can
200 		 * display the per-thread command line.
201 		 */
202 		kipp = NULL;
203 		for (j = 0; j < kip_len / sizeof(*kipp); j++) {
204 			kipp = &kip_free[j];
205 			if (kkstp->kkst_tid == kipp->ki_tid)
206 				break;
207 		}
208 		if (kipp == NULL)
209 			continue;
210 
211 		printf("%5d ", pid);
212 		printf("%6d ", kkstp->kkst_tid);
213 		printf("%-16s ", kipp->ki_comm);
214 		printf("%-16s ", (strlen(kipp->ki_ocomm) &&
215 		    (strcmp(kipp->ki_comm, kipp->ki_ocomm) != 0)) ?
216 		    kipp->ki_ocomm : "-");
217 
218 		switch (kkstp->kkst_state) {
219 		case KKST_STATE_RUNNING:
220 			printf("%-29s\n", "<running>");
221 			continue;
222 
223 		case KKST_STATE_SWAPPED:
224 			printf("%-29s\n", "<swapped>");
225 			continue;
226 
227 		case KKST_STATE_STACKOK:
228 			break;
229 
230 		default:
231 			printf("%-29s\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, kflag);
241 		printf("%-29s\n", trace);
242 	}
243 	free(kip_free);
244 	free(kkstp_free);
245 }
246