1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2007, 2011 Robert N. M. Watson
5 * Copyright (c) 2015 Allan Jude <allanjude@freebsd.org>
6 * Copyright (c) 2017 Dell EMC
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 #include <sys/cdefs.h>
32 #include <sys/param.h>
33 #include <sys/sysctl.h>
34 #include <sys/user.h>
35
36 #include <err.h>
37 #include <libprocstat.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <sysexits.h>
42 #include <unistd.h>
43
44 #include "procstat.h"
45
46 enum {
47 PS_CMP_NORMAL = 0x00,
48 PS_CMP_PLURAL = 0x01,
49 PS_CMP_SUBSTR = 0x02
50 };
51
52 struct procstat_cmd {
53 const char *command;
54 const char *xocontainer;
55 const char *usage;
56 void (*cmd)(struct procstat *, struct kinfo_proc *);
57 void (*opt)(int, char * const *);
58 int cmp;
59 };
60
61 int procstat_opts = 0;
62
63 static void cmdopt_none(int argc, char * const argv[]);
64 static void cmdopt_verbose(int argc, char * const argv[]);
65 static void cmdopt_signals(int argc, char * const argv[]);
66 static void cmdopt_rusage(int argc, char * const argv[]);
67 static void cmdopt_files(int argc, char * const argv[]);
68 static void cmdopt_cpuset(int argc, char * const argv[]);
69 static void cmdopt_kqueue(int argc, char * const argv[]);
70
71 static const char *progname;
72
73 /* aliased program parameters and arguments
74 * - usage field is abused to hold the pointer to the function
75 * displaying program usage
76 */
77 static const struct procstat_cmd pacmd_table[] = {
78 /* arguments are the same as for pwdx: pid or core file */
79 { "pargs", "args", NULL, &procstat_pargs, &cmdopt_none,
80 PS_CMP_NORMAL | PS_MODE_COMPAT },
81 { "penv", "env", NULL, &procstat_penv, &cmdopt_none,
82 PS_CMP_NORMAL | PS_MODE_COMPAT },
83 { "pwdx", "pwd", NULL, &procstat_pwdx, &cmdopt_none,
84 PS_CMP_NORMAL | PS_MODE_COMPAT }
85 };
86
87 /* procstat parameters and arguments */
88 static const struct procstat_cmd cmd_table[] = {
89 { "advlock", "advisory_locks", NULL, &procstat_advlocks, &cmdopt_none,
90 PS_CMP_PLURAL | PS_CMP_SUBSTR | PS_MODE_NO_KINFO_PROC },
91 { "argument", "arguments", NULL, &procstat_args, &cmdopt_none,
92 PS_CMP_PLURAL | PS_CMP_SUBSTR },
93 { "auxv", "auxv", NULL, &procstat_auxv, &cmdopt_none, PS_CMP_NORMAL },
94 { "basic", "basic", NULL, &procstat_basic, &cmdopt_none,
95 PS_CMP_NORMAL },
96 { "binary", "binary", NULL, &procstat_bin, &cmdopt_none,
97 PS_CMP_SUBSTR },
98 { "cpuset", "cs", NULL, &procstat_cs, &cmdopt_cpuset, PS_CMP_NORMAL },
99 { "cs", "cs", NULL, &procstat_cs, &cmdopt_cpuset, PS_CMP_NORMAL },
100 { "credential", "credentials", NULL, &procstat_cred, &cmdopt_none,
101 PS_CMP_PLURAL | PS_CMP_SUBSTR },
102 { "environment", "environment", NULL, &procstat_env, &cmdopt_none,
103 PS_CMP_SUBSTR },
104 { "fd", "files", "[-C]", &procstat_files, &cmdopt_files,
105 PS_CMP_PLURAL },
106 { "file", "files", "[-C]", &procstat_files, &cmdopt_files,
107 PS_CMP_PLURAL },
108 { "kqueue", "kqueues", NULL, &procstat_kqueues, &cmdopt_kqueue,
109 PS_CMP_PLURAL },
110 { "kstack", "kstack", "[-v]", &procstat_kstack, &cmdopt_verbose,
111 PS_CMP_NORMAL },
112 { "pargs", "args", NULL, &procstat_pargs, &cmdopt_none,
113 PS_CMP_NORMAL },
114 { "penv", "env", NULL, &procstat_penv, &cmdopt_none,
115 PS_CMP_NORMAL },
116 { "ptlwpinfo", "ptlwpinfo", NULL, &procstat_ptlwpinfo, &cmdopt_none,
117 PS_CMP_NORMAL },
118 { "pwdx", "pwd", NULL, &procstat_pwdx, &cmdopt_none,
119 PS_CMP_NORMAL },
120 { "rlimit", "rlimit", NULL, &procstat_rlimit, &cmdopt_none,
121 PS_CMP_NORMAL },
122 { "rlimitusage", "rlimitusage", NULL, &procstat_rlimitusage,
123 &cmdopt_none, PS_CMP_NORMAL },
124 { "rusage", "rusage", "[-Ht]", &procstat_rusage, &cmdopt_rusage,
125 PS_CMP_NORMAL },
126 { "sigfastblock", "sigfastblock", NULL, &procstat_sigfastblock,
127 &cmdopt_none, PS_CMP_NORMAL },
128 { "signal", "signals", "[-n]", &procstat_sigs, &cmdopt_signals,
129 PS_CMP_PLURAL | PS_CMP_SUBSTR },
130 { "thread", "threads", NULL, &procstat_threads, &cmdopt_none,
131 PS_CMP_PLURAL },
132 { "tsignal", "thread_signals", "[-n]", &procstat_threads_sigs,
133 &cmdopt_signals, PS_CMP_PLURAL | PS_CMP_SUBSTR },
134 { "vm", "vm", NULL, &procstat_vm, &cmdopt_none, PS_CMP_NORMAL }
135 };
136
137 static void
usage(const struct procstat_cmd * cmd)138 usage(const struct procstat_cmd *cmd)
139 {
140 size_t i, l;
141 int multi;
142
143 if (cmd == NULL || (cmd->cmp & PS_MODE_COMPAT) == 0) {
144 xo_error("usage: procstat [--libxo] [-h] [-M core] [-N system]"
145 " [-w interval] command\n"
146 " [pid ... | core ...]\n"
147 " procstat [--libxo] -a [-h] [-M core] [-N system] "
148 " [-w interval] command\n"
149 " procstat [--libxo] [-h] [-M core] [-N system]"
150 " [-w interval]\n"
151 " [-S | -b | -c | -e | -f [-C] | -i [-n] | "
152 "-j [-n] | -k [-k] |\n"
153 " -l | -r [-H] | -s | -t | -v | -x] "
154 "[pid ... | core ...]\n"
155 " procstat [--libxo] -a [-h] [-M core] [-N system]"
156 " [-w interval]\n"
157 " [-S | -b | -c | -e | -f [-C] | -i [-n] | "
158 "-j [-n] | -k [-k] |\n"
159 " -l | -r [-H] | -s | -t | -v | -x]\n"
160 " procstat [--libxo] -L [-h] [-M core] [-N system] core ...\n"
161 "Available commands:\n");
162 for (i = 0, l = nitems(cmd_table); i < l; i++) {
163 multi = i + 1 < l && cmd_table[i].cmd ==
164 cmd_table[i + 1].cmd;
165 xo_error(" %s%s%s", multi ? "[" : "",
166 cmd_table[i].command, (cmd_table[i].cmp &
167 PS_CMP_PLURAL) ? "(s)" : "");
168 for (; i + 1 < l && cmd_table[i].cmd ==
169 cmd_table[i + 1].cmd; i++)
170 xo_error(" | %s%s", cmd_table[i + 1].command,
171 (cmd_table[i].cmp & PS_CMP_PLURAL) ?
172 "(s)" : "");
173 if (multi)
174 xo_error("]");
175 if (cmd_table[i].usage != NULL)
176 xo_error(" %s", cmd_table[i].usage);
177 xo_error("\n");
178 }
179 } else {
180 xo_error("usage: %s [--libxo] pid ...\n", progname);
181 }
182 xo_finish();
183 exit(EX_USAGE);
184 }
185
186 static void
procstat(const struct procstat_cmd * cmd,struct procstat * prstat,struct kinfo_proc * kipp)187 procstat(const struct procstat_cmd *cmd, struct procstat *prstat,
188 struct kinfo_proc *kipp)
189 {
190 char *pidstr = NULL;
191
192 asprintf(&pidstr, "%d", kipp->ki_pid);
193 if (pidstr == NULL)
194 xo_errc(1, ENOMEM, "Failed to allocate memory in procstat()");
195 xo_open_container(pidstr);
196 cmd->cmd(prstat, kipp);
197 xo_close_container(pidstr);
198 free(pidstr);
199 }
200
201 /*
202 * Sort processes first by pid and then tid.
203 */
204 static int
kinfo_proc_compare(const void * a,const void * b)205 kinfo_proc_compare(const void *a, const void *b)
206 {
207 int i;
208
209 i = ((const struct kinfo_proc *)a)->ki_pid -
210 ((const struct kinfo_proc *)b)->ki_pid;
211 if (i != 0)
212 return (i);
213 i = ((const struct kinfo_proc *)a)->ki_tid -
214 ((const struct kinfo_proc *)b)->ki_tid;
215 return (i);
216 }
217
218 void
kinfo_proc_sort(struct kinfo_proc * kipp,int count)219 kinfo_proc_sort(struct kinfo_proc *kipp, int count)
220 {
221
222 qsort(kipp, count, sizeof(*kipp), kinfo_proc_compare);
223 }
224
225 const char *
kinfo_proc_thread_name(const struct kinfo_proc * kipp)226 kinfo_proc_thread_name(const struct kinfo_proc *kipp)
227 {
228 static char name[MAXCOMLEN+1];
229
230 strlcpy(name, kipp->ki_tdname, sizeof(name));
231 strlcat(name, kipp->ki_moretdname, sizeof(name));
232 if (name[0] == '\0' || strcmp(kipp->ki_comm, name) == 0) {
233 name[0] = '-';
234 name[1] = '\0';
235 }
236
237 return (name);
238 }
239
240 static const struct procstat_cmd *
getcmdbyprogname(const char * pprogname)241 getcmdbyprogname(const char *pprogname)
242 {
243 const char *ca;
244 size_t i;
245
246 if (pprogname == NULL)
247 return (NULL);
248
249 for (i = 0; i < nitems(pacmd_table); i++) {
250 ca = pacmd_table[i].command;
251 if (ca != NULL && strcmp(ca, pprogname) == 0)
252 return (&pacmd_table[i]);
253 }
254
255 return (NULL);
256 }
257
258 static const struct procstat_cmd *
getcmd(const char * str)259 getcmd(const char *str)
260 {
261 const struct procstat_cmd *cmd;
262 size_t i, l;
263 int cmp, s;
264
265 if (str == NULL)
266 return (NULL);
267 cmd = NULL;
268 if ((l = strlen(str)) == 0)
269 return (getcmd("basic"));
270 s = l > 1 && strcasecmp(str + l - 1, "s") == 0;
271 for (i = 0; i < nitems(cmd_table); i++) {
272 /*
273 * After the first match substring matches are disabled,
274 * allowing subsequent full matches to take precedence.
275 */
276 if (cmd == NULL && (cmd_table[i].cmp & PS_CMP_SUBSTR))
277 cmp = strncasecmp(str, cmd_table[i].command, l -
278 ((cmd_table[i].cmp & PS_CMP_PLURAL) && s ? 1 : 0));
279 else if ((cmd_table[i].cmp & PS_CMP_PLURAL) && s &&
280 l == strlen(cmd_table[i].command) + 1)
281 cmp = strncasecmp(str, cmd_table[i].command, l - 1);
282 else
283 cmp = strcasecmp(str, cmd_table[i].command);
284 if (cmp == 0)
285 cmd = &cmd_table[i];
286 }
287 return (cmd);
288 }
289
290 int
main(int argc,char * argv[])291 main(int argc, char *argv[])
292 {
293 struct kinfo_proc *p;
294 const struct procstat_cmd *cmd;
295 struct procstat *prstat, *cprstat;
296 char *dummy, *nlistf, *memf;
297 const char *xocontainer;
298 long l;
299 pid_t pid;
300 int aflag, ch, cnt, i, interval;
301
302 interval = 0;
303 cmd = NULL;
304 memf = nlistf = NULL;
305 aflag = 0;
306 argc = xo_parse_args(argc, argv);
307
308 progname = getprogname();
309 cmd = getcmdbyprogname(progname);
310
311 while ((ch = getopt(argc, argv, "abCcefHhijkLlM:N:nrSstvw:x")) != -1) {
312 switch (ch) {
313 case 'a':
314 aflag++;
315 break;
316 case 'b':
317 if (cmd != NULL)
318 usage(cmd);
319 cmd = getcmd("binary");
320 break;
321 case 'C':
322 procstat_opts |= PS_OPT_CAPABILITIES;
323 break;
324 case 'c':
325 if (cmd != NULL)
326 usage(cmd);
327 cmd = getcmd("arguments");
328 break;
329 case 'e':
330 if (cmd != NULL)
331 usage(cmd);
332 cmd = getcmd("environment");
333 break;
334 case 'f':
335 if (cmd != NULL)
336 usage(cmd);
337 cmd = getcmd("files");
338 break;
339 case 'H':
340 procstat_opts |= PS_OPT_PERTHREAD;
341 break;
342 case 'h':
343 procstat_opts |= PS_OPT_NOHEADER;
344 break;
345 case 'i':
346 if (cmd != NULL)
347 usage(cmd);
348 cmd = getcmd("signals");
349 break;
350 case 'j':
351 if (cmd != NULL)
352 usage(cmd);
353 cmd = getcmd("tsignals");
354 break;
355 case 'k':
356 if (cmd != NULL && cmd->cmd == procstat_kstack) {
357 if ((procstat_opts & PS_OPT_VERBOSE) != 0)
358 usage(cmd);
359 procstat_opts |= PS_OPT_VERBOSE;
360 } else {
361 if (cmd != NULL)
362 usage(cmd);
363 cmd = getcmd("kstack");
364 }
365 break;
366 case 'L':
367 if (cmd != NULL)
368 usage(cmd);
369 cmd = getcmd("ptlwpinfo");
370 break;
371 case 'l':
372 if (cmd != NULL)
373 usage(cmd);
374 cmd = getcmd("rlimit");
375 break;
376 case 'M':
377 memf = optarg;
378 break;
379 case 'N':
380 nlistf = optarg;
381 break;
382 case 'n':
383 procstat_opts |= PS_OPT_SIGNUM;
384 break;
385 case 'r':
386 if (cmd != NULL)
387 usage(cmd);
388 cmd = getcmd("rusage");
389 break;
390 case 'S':
391 if (cmd != NULL)
392 usage(cmd);
393 cmd = getcmd("cpuset");
394 break;
395 case 's':
396 if (cmd != NULL)
397 usage(cmd);
398 cmd = getcmd("credentials");
399 break;
400 case 't':
401 if (cmd != NULL)
402 usage(cmd);
403 cmd = getcmd("threads");
404 break;
405 case 'v':
406 if (cmd != NULL)
407 usage(cmd);
408 cmd = getcmd("vm");
409 break;
410 case 'w':
411 l = strtol(optarg, &dummy, 10);
412 if (*dummy != '\0')
413 usage(cmd);
414 if (l < 1 || l > INT_MAX)
415 usage(cmd);
416 interval = l;
417 break;
418 case 'x':
419 if (cmd != NULL)
420 usage(cmd);
421 cmd = getcmd("auxv");
422 break;
423 case '?':
424 default:
425 usage(cmd);
426 }
427
428 }
429 argc -= optind;
430 argv += optind;
431
432 if (cmd == NULL && argv[0] != NULL && (cmd = getcmd(argv[0])) != NULL) {
433 if ((procstat_opts & PS_SUBCOMMAND_OPTS) != 0)
434 usage(cmd);
435 if (cmd->opt != NULL) {
436 optreset = 1;
437 optind = 1;
438 cmd->opt(argc, argv);
439 if ((cmd->cmp & PS_MODE_COMPAT) == 0) {
440 argc -= optind;
441 argv += optind;
442 }
443 } else {
444 argc -= 1;
445 argv += 1;
446 }
447 } else {
448 if (cmd == NULL)
449 cmd = getcmd("basic");
450 if (cmd->cmd != procstat_files &&
451 (procstat_opts & PS_OPT_CAPABILITIES) != 0 &&
452 (cmd->cmp & PS_MODE_COMPAT) == 0)
453 usage(cmd);
454 }
455
456 /* Must specify either the -a flag or a list of pids. */
457 if (!(aflag == 1 && argc == 0) && !(aflag == 0 && argc > 0) &&
458 (cmd->cmp & PS_MODE_NO_KINFO_PROC) == 0)
459 usage(cmd);
460
461 if (memf != NULL)
462 prstat = procstat_open_kvm(nlistf, memf);
463 else
464 prstat = procstat_open_sysctl();
465 if (prstat == NULL)
466 xo_errx(1, "procstat_open()");
467 do {
468 xocontainer = cmd->xocontainer != NULL ? cmd->xocontainer :
469 cmd->command;
470 xo_set_version(PROCSTAT_XO_VERSION);
471 xo_open_container(progname);
472 xo_open_container(xocontainer);
473
474 if ((cmd->cmp & PS_MODE_NO_KINFO_PROC) != 0) {
475 cmd->cmd(prstat, NULL);
476 goto iter;
477 }
478
479 if (aflag) {
480 p = procstat_getprocs(prstat, KERN_PROC_PROC, 0, &cnt);
481 if (p == NULL)
482 xo_errx(1, "procstat_getprocs()");
483 kinfo_proc_sort(p, cnt);
484 for (i = 0; i < cnt; i++) {
485 procstat(cmd, prstat, &p[i]);
486
487 /* Suppress header after first process. */
488 procstat_opts |= PS_OPT_NOHEADER;
489 xo_flush();
490 }
491 procstat_freeprocs(prstat, p);
492 }
493 for (i = 0; i < argc; i++) {
494 l = strtol(argv[i], &dummy, 10);
495 if (*dummy == '\0') {
496 if (l < 0)
497 usage(cmd);
498 pid = l;
499
500 p = procstat_getprocs(prstat, KERN_PROC_PID,
501 pid, &cnt);
502 if (p == NULL)
503 xo_errx(1, "procstat_getprocs()");
504 if (cnt != 0)
505 procstat(cmd, prstat, p);
506 procstat_freeprocs(prstat, p);
507 } else {
508 if ((cmd->cmp & PS_MODE_COMPAT) == 0) {
509 cprstat = procstat_open_core(argv[i]);
510 if (cprstat == NULL) {
511 warnx("procstat_open()");
512 continue;
513 }
514 p = procstat_getprocs(cprstat,
515 KERN_PROC_PID, -1, &cnt);
516 if (p == NULL) {
517 xo_errx(1,
518 "procstat_getprocs()");
519 }
520 if (cnt != 0)
521 procstat(cmd, cprstat, p);
522 procstat_freeprocs(cprstat, p);
523 procstat_close(cprstat);
524 } else {
525 usage(cmd);
526 }
527 }
528 if ((cmd->cmp & PS_MODE_COMPAT) == 0) {
529 /* Suppress header after first process. */
530 procstat_opts |= PS_OPT_NOHEADER;
531 }
532 }
533
534 iter:
535 xo_close_container(xocontainer);
536 xo_close_container(progname);
537 xo_finish();
538 if (interval)
539 sleep(interval);
540 } while (interval);
541
542 procstat_close(prstat);
543
544 exit(0);
545 }
546
547 void
cmdopt_none(int argc,char * const argv[])548 cmdopt_none(int argc, char * const argv[])
549 {
550 int ch;
551
552 while ((ch = getopt(argc, argv, "")) != -1) {
553 switch (ch) {
554 case '?':
555 default:
556 usage(NULL);
557 }
558 }
559 }
560
561 void
cmdopt_verbose(int argc,char * const argv[])562 cmdopt_verbose(int argc, char * const argv[])
563 {
564 int ch;
565
566 while ((ch = getopt(argc, argv, "v")) != -1) {
567 switch (ch) {
568 case 'v':
569 procstat_opts |= PS_OPT_VERBOSE;
570 break;
571 case '?':
572 default:
573 usage(NULL);
574 }
575 }
576 }
577
578 void
cmdopt_signals(int argc,char * const argv[])579 cmdopt_signals(int argc, char * const argv[])
580 {
581 int ch;
582
583 while ((ch = getopt(argc, argv, "n")) != -1) {
584 switch (ch) {
585 case 'n':
586 procstat_opts |= PS_OPT_SIGNUM;
587 break;
588 case '?':
589 default:
590 usage(NULL);
591 }
592 }
593 }
594
595 void
cmdopt_rusage(int argc,char * const argv[])596 cmdopt_rusage(int argc, char * const argv[])
597 {
598 int ch;
599
600 while ((ch = getopt(argc, argv, "Ht")) != -1) {
601 switch (ch) {
602 case 'H':
603 /* FALLTHROUGH */
604 case 't':
605 procstat_opts |= PS_OPT_PERTHREAD;
606 break;
607 case '?':
608 default:
609 usage(NULL);
610 }
611 }
612 }
613
614 void
cmdopt_files(int argc,char * const argv[])615 cmdopt_files(int argc, char * const argv[])
616 {
617 int ch;
618
619 while ((ch = getopt(argc, argv, "C")) != -1) {
620 switch (ch) {
621 case 'C':
622 procstat_opts |= PS_OPT_CAPABILITIES;
623 break;
624 case '?':
625 default:
626 usage(NULL);
627 }
628 }
629 }
630
631 void
cmdopt_cpuset(int argc,char * const argv[])632 cmdopt_cpuset(int argc, char * const argv[])
633 {
634
635 procstat_opts |= PS_OPT_PERTHREAD;
636 cmdopt_none(argc, argv);
637 }
638
639 void
cmdopt_kqueue(int argc,char * const argv[])640 cmdopt_kqueue(int argc, char * const argv[])
641 {
642 int ch;
643
644 while ((ch = getopt(argc, argv, "v")) != -1) {
645 switch (ch) {
646 case 'v':
647 procstat_opts |= PS_OPT_VERBOSE;
648 break;
649 case '?':
650 default:
651 usage(NULL);
652 }
653 }
654 }
655