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