Makefile_upgrade.inc: Remove /usr/share/libg++
[dragonfly.git] / test / debug / kshowprocs.c
1 /*
2  * KSHOWPROCS.C
3  *
4  * cc kshowprocs.c -o /usr/local/bin/kshowprocs -g -O0 -pipe -lkvm
5  *
6  * Dump kernel processes
7  *
8  * Copyright (c) 2011 The DragonFly Project.  All rights reserved.
9  *
10  * This code is derived from software contributed to The DragonFly Project
11  * by Antonio Huete <tuxillo@quantumachine.net>
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  *
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in
21  *    the documentation and/or other materials provided with the
22  *    distribution.
23  * 3. Neither the name of The DragonFly Project nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific, prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
31  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
33  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
34  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
35  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
36  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
37  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  */
40
41
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <fcntl.h>
46 #include <kvm.h>
47 #include <nlist.h>
48 #include <getopt.h>
49
50 #include <sys/user.h>
51 #include <sys/sysctl.h>
52
53 int debugopt;
54 int verboseopt;
55
56 int
57 main(int ac, char **av)
58 {
59     const char *corefile = NULL;
60     const char *sysfile = NULL;
61     struct kinfo_proc *kp;
62     kvm_t *kd;
63     int ch;
64     int hv;
65     int i;
66     int nprocs;
67
68     while ((ch = getopt(ac, av, "M:N:v")) != -1) {
69         switch(ch) {
70         case 'v':
71             ++verboseopt;
72             break;
73         case 'M':
74             corefile = optarg;
75             break;
76         case 'N':
77             sysfile = optarg;
78             break;
79         default:
80             fprintf(stderr, "%s [-M core] [-N system]\n", av[0]);
81             exit(1);
82         }
83     }
84     ac -= optind;
85     av += optind;
86
87     if ((kd = kvm_open(sysfile, corefile, NULL, O_RDONLY, "kvm:")) == NULL) {
88         perror("kvm_open");
89         exit(1);
90     }
91
92     if ((kp = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nprocs)) == NULL)
93         errx(1, "%s", kvm_geterr(kd));
94
95     fprintf(stdout, "%-6s %-6s %-20s %-10s %-5s\n",
96         "PID",
97         "PPID",
98         "COMMAND",
99         "LOGIN",
100         "NICE");
101
102     for (i = 0; i < nprocs; i++) {
103             fprintf(stdout, "%-6d %-6d %-20s %-10s %-5d\n",
104                 kp[i].kp_pid,
105                 kp[i].kp_ppid,
106                 kp[i].kp_comm,
107                 kp[i].kp_login,
108                 kp[i].kp_nice);
109     }
110
111     kvm_close(kd);
112
113     return 0;
114
115 }