xref: /freebsd/usr.bin/procstat/procstat.c (revision 10b59a9b4add0320d52c15ce057dd697261e7dfc)
1 /*-
2  * Copyright (c) 2007, 2011 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 <libprocstat.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <sysexits.h>
38 #include <unistd.h>
39 
40 #include "procstat.h"
41 
42 static int aflag, bflag, cflag, fflag, iflag, jflag, kflag, sflag, tflag, vflag;
43 int	hflag, nflag, Cflag;
44 
45 static void
46 usage(void)
47 {
48 
49 	fprintf(stderr, "usage: procstat [-h] [-C] [-M core] [-N system] "
50 	    "[-w interval] [-b | -c | -f | -i | -j | -k | -s | -t | -v]\n");
51 	fprintf(stderr, "                [-a | pid ...]\n");
52 	exit(EX_USAGE);
53 }
54 
55 static void
56 procstat(struct procstat *prstat, struct kinfo_proc *kipp)
57 {
58 
59 	if (bflag)
60 		procstat_bin(kipp);
61 	else if (cflag)
62 		procstat_args(kipp);
63 	else if (fflag)
64 		procstat_files(prstat, kipp);
65 	else if (iflag)
66 		procstat_sigs(prstat, kipp);
67 	else if (jflag)
68 		procstat_threads_sigs(prstat, kipp);
69 	else if (kflag)
70 		procstat_kstack(kipp, kflag);
71 	else if (sflag)
72 		procstat_cred(kipp);
73 	else if (tflag)
74 		procstat_threads(kipp);
75 	else if (vflag)
76 		procstat_vm(kipp);
77 	else
78 		procstat_basic(kipp);
79 }
80 
81 /*
82  * Sort processes first by pid and then tid.
83  */
84 static int
85 kinfo_proc_compare(const void *a, const void *b)
86 {
87 	int i;
88 
89 	i = ((const struct kinfo_proc *)a)->ki_pid -
90 	    ((const struct kinfo_proc *)b)->ki_pid;
91 	if (i != 0)
92 		return (i);
93 	i = ((const struct kinfo_proc *)a)->ki_tid -
94 	    ((const struct kinfo_proc *)b)->ki_tid;
95 	return (i);
96 }
97 
98 void
99 kinfo_proc_sort(struct kinfo_proc *kipp, int count)
100 {
101 
102 	qsort(kipp, count, sizeof(*kipp), kinfo_proc_compare);
103 }
104 
105 int
106 main(int argc, char *argv[])
107 {
108 	int ch, interval, tmp;
109 	int i;
110 	struct kinfo_proc *p;
111 	struct procstat *prstat;
112 	long l;
113 	pid_t pid;
114 	char *dummy;
115 	char *nlistf, *memf;
116 	int cnt;
117 
118 	interval = 0;
119 	memf = nlistf = NULL;
120 	while ((ch = getopt(argc, argv, "CN:M:abcfijkhstvw:")) != -1) {
121 		switch (ch) {
122 		case 'C':
123 			Cflag++;
124 			break;
125 
126 		case 'M':
127 			memf = optarg;
128 			break;
129 		case 'N':
130 			nlistf = optarg;
131 			break;
132 		case 'a':
133 			aflag++;
134 			break;
135 
136 		case 'b':
137 			bflag++;
138 			break;
139 
140 		case 'c':
141 			cflag++;
142 			break;
143 
144 		case 'f':
145 			fflag++;
146 			break;
147 
148 		case 'i':
149 			iflag++;
150 			break;
151 
152 		case 'j':
153 			jflag++;
154 			break;
155 
156 		case 'k':
157 			kflag++;
158 			break;
159 
160 		case 'n':
161 			nflag++;
162 			break;
163 
164 		case 'h':
165 			hflag++;
166 			break;
167 
168 		case 's':
169 			sflag++;
170 			break;
171 
172 		case 't':
173 			tflag++;
174 			break;
175 
176 		case 'v':
177 			vflag++;
178 			break;
179 
180 		case 'w':
181 			l = strtol(optarg, &dummy, 10);
182 			if (*dummy != '\0')
183 				usage();
184 			if (l < 1 || l > INT_MAX)
185 				usage();
186 			interval = l;
187 			break;
188 
189 		case '?':
190 		default:
191 			usage();
192 		}
193 
194 	}
195 	argc -= optind;
196 	argv += optind;
197 
198 	/* We require that either 0 or 1 mode flags be set. */
199 	tmp = bflag + cflag + fflag + (kflag ? 1 : 0) + sflag + tflag + vflag;
200 	if (!(tmp == 0 || tmp == 1))
201 		usage();
202 
203 	/* We allow -k to be specified up to twice, but not more. */
204 	if (kflag > 2)
205 		usage();
206 
207 	/* Must specify either the -a flag or a list of pids. */
208 	if (!(aflag == 1 && argc == 0) && !(aflag == 0 && argc > 0))
209 		usage();
210 
211 	/* Only allow -C with -f. */
212 	if (Cflag && !fflag)
213 		usage();
214 
215 	if (memf != NULL)
216 		prstat = procstat_open_kvm(nlistf, memf);
217 	else
218 		prstat = procstat_open_sysctl();
219 	if (prstat == NULL)
220 		errx(1, "procstat_open()");
221 	do {
222 		if (aflag) {
223 			p = procstat_getprocs(prstat, KERN_PROC_PROC, 0, &cnt);
224 			if (p == NULL)
225 				errx(1, "procstat_getprocs()");
226 			kinfo_proc_sort(p, cnt);
227 			for (i = 0; i < cnt; i++) {
228 				procstat(prstat, &p[i]);
229 
230 				/* Suppress header after first process. */
231 				hflag = 1;
232 			}
233 			procstat_freeprocs(prstat, p);
234 		}
235 		for (i = 0; i < argc; i++) {
236 			l = strtol(argv[i], &dummy, 10);
237 			if (*dummy != '\0')
238 				usage();
239 			if (l < 0)
240 				usage();
241 			pid = l;
242 
243 			p = procstat_getprocs(prstat, KERN_PROC_PID, pid, &cnt);
244 			if (p == NULL)
245 				errx(1, "procstat_getprocs()");
246 			if (cnt != 0)
247 				procstat(prstat, p);
248 			procstat_freeprocs(prstat, p);
249 
250 			/* Suppress header after first process. */
251 			hflag = 1;
252 		}
253 		if (interval)
254 			sleep(interval);
255 	} while (interval);
256 	procstat_close(prstat);
257 	exit(0);
258 }
259