xref: /freebsd/usr.bin/procstat/procstat.c (revision 884a2a699669ec61e2366e3e358342dbc94be24a)
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 <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;
44 
45 static void
46 usage(void)
47 {
48 
49 	fprintf(stderr, "usage: procstat [-h] [-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, "N:M:abcfijkhstvw:")) != -1) {
121 		switch (ch) {
122 		case 'M':
123 			memf = optarg;
124 			break;
125 		case 'N':
126 			nlistf = optarg;
127 			break;
128 		case 'a':
129 			aflag++;
130 			break;
131 
132 		case 'b':
133 			bflag++;
134 			break;
135 
136 		case 'c':
137 			cflag++;
138 			break;
139 
140 		case 'f':
141 			fflag++;
142 			break;
143 
144 		case 'i':
145 			iflag++;
146 			break;
147 
148 		case 'j':
149 			jflag++;
150 			break;
151 
152 		case 'k':
153 			kflag++;
154 			break;
155 
156 		case 'n':
157 			nflag++;
158 			break;
159 
160 		case 'h':
161 			hflag++;
162 			break;
163 
164 		case 's':
165 			sflag++;
166 			break;
167 
168 		case 't':
169 			tflag++;
170 			break;
171 
172 		case 'v':
173 			vflag++;
174 			break;
175 
176 		case 'w':
177 			l = strtol(optarg, &dummy, 10);
178 			if (*dummy != '\0')
179 				usage();
180 			if (l < 1 || l > INT_MAX)
181 				usage();
182 			interval = l;
183 			break;
184 
185 		case '?':
186 		default:
187 			usage();
188 		}
189 
190 	}
191 	argc -= optind;
192 	argv += optind;
193 
194 	/* We require that either 0 or 1 mode flags be set. */
195 	tmp = bflag + cflag + fflag + (kflag ? 1 : 0) + sflag + tflag + vflag;
196 	if (!(tmp == 0 || tmp == 1))
197 		usage();
198 
199 	/* We allow -k to be specified up to twice, but not more. */
200 	if (kflag > 2)
201 		usage();
202 
203 	/* Must specify either the -a flag or a list of pids. */
204 	if (!(aflag == 1 && argc == 0) && !(aflag == 0 && argc > 0))
205 		usage();
206 
207 	if (memf != NULL)
208 		prstat = procstat_open_kvm(nlistf, memf);
209 	else
210 		prstat = procstat_open_sysctl();
211 	if (prstat == NULL)
212 		errx(1, "procstat_open()");
213 	do {
214 		if (aflag) {
215 			p = procstat_getprocs(prstat, KERN_PROC_PROC, 0, &cnt);
216 			if (p == NULL)
217 				errx(1, "procstat_getprocs()");
218 			kinfo_proc_sort(p, cnt);
219 			for (i = 0; i < cnt; i++) {
220 				procstat(prstat, &p[i]);
221 
222 				/* Suppress header after first process. */
223 				hflag = 1;
224 			}
225 			procstat_freeprocs(prstat, p);
226 		}
227 		for (i = 0; i < argc; i++) {
228 			l = strtol(argv[i], &dummy, 10);
229 			if (*dummy != '\0')
230 				usage();
231 			if (l < 0)
232 				usage();
233 			pid = l;
234 
235 			p = procstat_getprocs(prstat, KERN_PROC_PID, pid, &cnt);
236 			if (p == NULL)
237 				errx(1, "procstat_getprocs()");
238 			if (cnt != 0)
239 				procstat(prstat, p);
240 			procstat_freeprocs(prstat, p);
241 
242 			/* Suppress header after first process. */
243 			hflag = 1;
244 		}
245 		if (interval)
246 			sleep(interval);
247 	} while (interval);
248 	procstat_close(prstat);
249 	exit(0);
250 }
251