2 * Copyright (c) 2007 Robert N. M. Watson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/param.h>
30 #include <sys/sysctl.h>
35 #include <libprocstat.h>
43 * Walk the stack trace provided by the kernel and reduce it to what we
44 * actually want to print. This involves stripping true instruction pointers,
45 * frame numbers, and carriage returns as generated by stack(9). If -kk is
46 * specified, print the function and offset, otherwise just the function.
48 enum trace_state { TS_FRAMENUM, TS_PC, TS_AT, TS_FUNC, TS_OFF };
50 static enum trace_state
51 kstack_nextstate(enum trace_state ts)
71 errx(-1, "kstack_nextstate");
76 kstack_cleanup(const char *old, char *new, int kflag)
78 enum trace_state old_ts, ts;
83 for (cp_old = old, cp_new = new; *cp_old != '\0'; cp_old++) {
89 ts = kstack_nextstate(old_ts);
90 if (old_ts == TS_OFF) {
94 if (kflag > 1 && old_ts == TS_FUNC) {
100 if (ts == TS_FUNC || (kflag > 1 && ts == TS_OFF)) {
109 * Sort threads by tid.
112 kinfo_kstack_compare(const void *a, const void *b)
115 return ((const struct kinfo_kstack *)a)->kkst_tid -
116 ((const struct kinfo_kstack *)b)->kkst_tid;
120 kinfo_kstack_sort(struct kinfo_kstack *kkstp, int count)
123 qsort(kkstp, count, sizeof(*kkstp), kinfo_kstack_compare);
128 procstat_kstack(struct kinfo_proc *kipp, int kflag)
130 struct kinfo_kstack *kkstp, *kkstp_free;
131 struct kinfo_proc *kip, *kip_free;
132 char trace[KKST_MAXLEN];
135 size_t kip_len, kstk_len;
138 printf("%5s %6s %-16s %-16s %-29s\n", "PID", "TID", "COMM",
143 name[2] = KERN_PROC_KSTACK;
144 name[3] = kipp->ki_pid;
147 error = sysctl(name, 4, NULL, &kstk_len, NULL, 0);
148 if (error < 0 && errno != ESRCH && errno != EPERM && errno != ENOENT) {
149 warn("sysctl: kern.proc.kstack: %d", kipp->ki_pid);
152 if (error < 0 && errno == ENOENT) {
153 warnx("sysctl: kern.proc.kstack unavailable");
154 errx(-1, "options DDB or options STACK required in kernel");
159 kkstp = kkstp_free = malloc(kstk_len);
163 if (sysctl(name, 4, kkstp, &kstk_len, NULL, 0) < 0) {
164 warn("sysctl: kern.proc.pid: %d", kipp->ki_pid);
170 * We need to re-query for thread information, so don't use *kipp.
174 name[2] = KERN_PROC_PID | KERN_PROC_INC_THREAD;
175 name[3] = kipp->ki_pid;
178 error = sysctl(name, 4, NULL, &kip_len, NULL, 0);
179 if (error < 0 && errno != ESRCH) {
180 warn("sysctl: kern.proc.pid: %d", kipp->ki_pid);
186 kip = kip_free = malloc(kip_len);
190 if (sysctl(name, 4, kip, &kip_len, NULL, 0) < 0) {
191 warn("sysctl: kern.proc.pid: %d", kipp->ki_pid);
196 kinfo_kstack_sort(kkstp, kstk_len / sizeof(*kkstp));
197 for (i = 0; i < kstk_len / sizeof(*kkstp); i++) {
198 kkstp = &kkstp_free[i];
201 * Look up the specific thread using its tid so we can
202 * display the per-thread command line.
205 for (j = 0; j < kip_len / sizeof(*kipp); j++) {
207 if (kkstp->kkst_tid == kipp->ki_tid)
213 printf("%5d ", kipp->ki_pid);
214 printf("%6d ", kkstp->kkst_tid);
215 printf("%-16s ", kipp->ki_comm);
216 printf("%-16s ", (strlen(kipp->ki_tdname) &&
217 (strcmp(kipp->ki_comm, kipp->ki_tdname) != 0)) ?
218 kipp->ki_tdname : "-");
220 switch (kkstp->kkst_state) {
221 case KKST_STATE_RUNNING:
222 printf("%-29s\n", "<running>");
225 case KKST_STATE_SWAPPED:
226 printf("%-29s\n", "<swapped>");
229 case KKST_STATE_STACKOK:
233 printf("%-29s\n", "<unknown>");
238 * The kernel generates a trace with carriage returns between
239 * entries, but for a more compact view, we convert carriage
242 kstack_cleanup(kkstp->kkst_trace, trace, kflag);
243 printf("%-29s\n", trace);