Merge branch 'vendor/LIBEDIT'
[dragonfly.git] / test / debug / ksyscalls.c
1 /*
2  * KSYSCALLS.C
3  *
4  * cc -I/usr/src/sys ksyscalls.c -o /usr/local/bin/ksyscalls -lkvm
5  *
6  * Dump syscall debugging info
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 Matthew Dillon <dillon@backplane.com>
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 #define _KERNEL_STRUCTURES_
42 #include <sys/param.h>
43 #include <sys/user.h>
44 #include <sys/malloc.h>
45 #include <sys/signalvar.h>
46 #include <sys/vnode.h>
47 #include <sys/namecache.h>
48 #include <sys/syscall.h>
49
50 #include <vm/vm.h>
51 #include <vm/vm_page.h>
52 #include <vm/vm_kern.h>
53 #include <vm/vm_page.h>
54 #include <vm/vm_object.h>
55 #include <vm/vm_map.h>
56 #include <vm/swap_pager.h>
57 #include <vm/vnode_pager.h>
58
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <fcntl.h>
63 #include <kvm.h>
64 #include <nlist.h>
65 #include <getopt.h>
66
67 struct nlist Nl[] = {
68     { "_SysCallsWorstCase" },
69     { NULL }
70 };
71
72 static void kkread(kvm_t *kd, u_long addr, void *buf, size_t nbytes);
73
74 int
75 main(int ac, char **av)
76 {
77     const char *corefile = NULL;
78     const char *sysfile = NULL;
79     kvm_t *kd;
80     int ch;
81     int i;
82     uint64_t syscallsworstcase[SYS_MAXSYSCALL];
83
84     while ((ch = getopt(ac, av, "M:N:")) != -1) {
85         switch(ch) {
86         case 'M':
87             corefile = optarg;
88             break;
89         case 'N':
90             sysfile = optarg;
91             break;
92         default:
93             fprintf(stderr, "%s [-M core] [-N system]\n", av[0]);
94             exit(1);
95         }
96     }
97     ac -= optind;
98     av += optind;
99
100     if ((kd = kvm_open(sysfile, corefile, NULL, O_RDONLY, "kvm:")) == NULL) {
101         perror("kvm_open");
102         exit(1);
103     }
104     if (kvm_nlist(kd, Nl) != 0) {
105         perror("kvm_nlist");
106         exit(1);
107     }
108     kkread(kd, Nl[0].n_value, syscallsworstcase, sizeof(syscallsworstcase));
109
110     for (i = 0; i < SYS_MAXSYSCALL; ++i) {
111         if (syscallsworstcase[i])
112                 printf("call %3d %6jduS\n", i, (intmax_t)syscallsworstcase[i]);
113     }
114 }
115
116 static void
117 kkread(kvm_t *kd, u_long addr, void *buf, size_t nbytes)
118 {
119     if (kvm_read(kd, addr, buf, nbytes) != nbytes) {
120         perror("kvm_read");
121         exit(1);
122     }
123 }