1*5071e670SMatthew Dillon /*
2*5071e670SMatthew Dillon * H2DIO.C
3*5071e670SMatthew Dillon *
4*5071e670SMatthew Dillon * cc -I/usr/src/sys h2dio.c -o ~/bin/h2dio -lkvm
5*5071e670SMatthew Dillon *
6*5071e670SMatthew Dillon * h2dio <hmpaddr>
7*5071e670SMatthew Dillon *
8*5071e670SMatthew Dillon * Copyright (c) 2018 The DragonFly Project. All rights reserved.
9*5071e670SMatthew Dillon *
10*5071e670SMatthew Dillon * This code is derived from software contributed to The DragonFly Project
11*5071e670SMatthew Dillon * by Matthew Dillon <dillon@backplane.com>
12*5071e670SMatthew Dillon *
13*5071e670SMatthew Dillon * Redistribution and use in source and binary forms, with or without
14*5071e670SMatthew Dillon * modification, are permitted provided that the following conditions
15*5071e670SMatthew Dillon * are met:
16*5071e670SMatthew Dillon *
17*5071e670SMatthew Dillon * 1. Redistributions of source code must retain the above copyright
18*5071e670SMatthew Dillon * notice, this list of conditions and the following disclaimer.
19*5071e670SMatthew Dillon * 2. Redistributions in binary form must reproduce the above copyright
20*5071e670SMatthew Dillon * notice, this list of conditions and the following disclaimer in
21*5071e670SMatthew Dillon * the documentation and/or other materials provided with the
22*5071e670SMatthew Dillon * distribution.
23*5071e670SMatthew Dillon *
24*5071e670SMatthew Dillon * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25*5071e670SMatthew Dillon * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26*5071e670SMatthew Dillon * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27*5071e670SMatthew Dillon * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28*5071e670SMatthew Dillon * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29*5071e670SMatthew Dillon * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
30*5071e670SMatthew Dillon * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31*5071e670SMatthew Dillon * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
32*5071e670SMatthew Dillon * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
33*5071e670SMatthew Dillon * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
34*5071e670SMatthew Dillon * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35*5071e670SMatthew Dillon * SUCH DAMAGE.
36*5071e670SMatthew Dillon */
37*5071e670SMatthew Dillon
38*5071e670SMatthew Dillon #include <sys/types.h>
39*5071e670SMatthew Dillon #include <sys/user.h>
40*5071e670SMatthew Dillon #include <vfs/hammer2/hammer2.h>
41*5071e670SMatthew Dillon
42*5071e670SMatthew Dillon #include <vm/vm.h>
43*5071e670SMatthew Dillon #include <vm/vm_page.h>
44*5071e670SMatthew Dillon #include <vm/vm_kern.h>
45*5071e670SMatthew Dillon #include <vm/swap_pager.h>
46*5071e670SMatthew Dillon #include <vm/vnode_pager.h>
47*5071e670SMatthew Dillon
48*5071e670SMatthew Dillon #include <stdio.h>
49*5071e670SMatthew Dillon #include <stdlib.h>
50*5071e670SMatthew Dillon #include <string.h>
51*5071e670SMatthew Dillon #include <fcntl.h>
52*5071e670SMatthew Dillon #include <kvm.h>
53*5071e670SMatthew Dillon #include <nlist.h>
54*5071e670SMatthew Dillon #include <getopt.h>
55*5071e670SMatthew Dillon
56*5071e670SMatthew Dillon static void h2dioscan(kvm_t *kd, int tab, uintptr_t cp);
57*5071e670SMatthew Dillon static void kkread(kvm_t *kd, u_long addr, void *buf, size_t nbytes);
58*5071e670SMatthew Dillon
59*5071e670SMatthew Dillon #if 0
60*5071e670SMatthew Dillon struct nlist Nl[] = {
61*5071e670SMatthew Dillon { "_rootnch" },
62*5071e670SMatthew Dillon { "_mountlist" },
63*5071e670SMatthew Dillon { NULL }
64*5071e670SMatthew Dillon };
65*5071e670SMatthew Dillon #endif
66*5071e670SMatthew Dillon
67*5071e670SMatthew Dillon int
main(int ac,char ** av)68*5071e670SMatthew Dillon main(int ac, char **av)
69*5071e670SMatthew Dillon {
70*5071e670SMatthew Dillon kvm_t *kd;
71*5071e670SMatthew Dillon const char *corefile = NULL;
72*5071e670SMatthew Dillon const char *sysfile = NULL;
73*5071e670SMatthew Dillon hammer2_dev_t hmp;
74*5071e670SMatthew Dillon uintptr_t base;
75*5071e670SMatthew Dillon uintptr_t cp;
76*5071e670SMatthew Dillon int ch;
77*5071e670SMatthew Dillon
78*5071e670SMatthew Dillon while ((ch = getopt(ac, av, "M:N:")) != -1) {
79*5071e670SMatthew Dillon switch(ch) {
80*5071e670SMatthew Dillon case 'M':
81*5071e670SMatthew Dillon corefile = optarg;
82*5071e670SMatthew Dillon break;
83*5071e670SMatthew Dillon case 'N':
84*5071e670SMatthew Dillon sysfile = optarg;
85*5071e670SMatthew Dillon break;
86*5071e670SMatthew Dillon default:
87*5071e670SMatthew Dillon fprintf(stderr, "%s [-M core] [-N system]\n", av[0]);
88*5071e670SMatthew Dillon exit(1);
89*5071e670SMatthew Dillon }
90*5071e670SMatthew Dillon }
91*5071e670SMatthew Dillon ac -= optind;
92*5071e670SMatthew Dillon av += optind;
93*5071e670SMatthew Dillon
94*5071e670SMatthew Dillon if ((kd = kvm_open(sysfile, corefile, NULL, O_RDONLY, "kvm:")) == NULL) {
95*5071e670SMatthew Dillon perror("kvm_open");
96*5071e670SMatthew Dillon exit(1);
97*5071e670SMatthew Dillon }
98*5071e670SMatthew Dillon #if 0
99*5071e670SMatthew Dillon if (kvm_nlist(kd, Nl) != 0) {
100*5071e670SMatthew Dillon perror("kvm_nlist");
101*5071e670SMatthew Dillon exit(1);
102*5071e670SMatthew Dillon }
103*5071e670SMatthew Dillon kkread(kd, Nl[0].n_value, &nch, sizeof(nch));
104*5071e670SMatthew Dillon kkread(kd, Nl[1].n_value, &list, sizeof(list));
105*5071e670SMatthew Dillon #endif
106*5071e670SMatthew Dillon
107*5071e670SMatthew Dillon base = strtoul(av[0], NULL, 0);
108*5071e670SMatthew Dillon
109*5071e670SMatthew Dillon kkread(kd, base, &hmp, sizeof(hmp));
110*5071e670SMatthew Dillon cp = (uintptr_t)hmp.iotree.rbh_root;
111*5071e670SMatthew Dillon if (cp)
112*5071e670SMatthew Dillon h2dioscan(kd, 4, cp);
113*5071e670SMatthew Dillon printf("\n");
114*5071e670SMatthew Dillon return 0;
115*5071e670SMatthew Dillon }
116*5071e670SMatthew Dillon
117*5071e670SMatthew Dillon static
118*5071e670SMatthew Dillon void
h2dioscan(kvm_t * kd,int tab,uintptr_t cp)119*5071e670SMatthew Dillon h2dioscan(kvm_t *kd, int tab, uintptr_t cp)
120*5071e670SMatthew Dillon {
121*5071e670SMatthew Dillon hammer2_io_t dio;
122*5071e670SMatthew Dillon
123*5071e670SMatthew Dillon kkread(kd, cp, &dio, sizeof(dio));
124*5071e670SMatthew Dillon if (dio.rbnode.rbe_left)
125*5071e670SMatthew Dillon h2dioscan(kd, tab, (uintptr_t)dio.rbnode.rbe_left);
126*5071e670SMatthew Dillon printf("%*.*s dio %p pbase=%016lx refs=%08x "
127*5071e670SMatthew Dillon "bp=%p psize=%d ticks=%d err=%d\n",
128*5071e670SMatthew Dillon tab, tab, "", (void *)cp, dio.pbase, dio.refs,
129*5071e670SMatthew Dillon dio.bp, dio.psize, dio.ticks, dio.error);
130*5071e670SMatthew Dillon if (dio.rbnode.rbe_right)
131*5071e670SMatthew Dillon h2dioscan(kd, tab, (uintptr_t)dio.rbnode.rbe_right);
132*5071e670SMatthew Dillon }
133*5071e670SMatthew Dillon
134*5071e670SMatthew Dillon static void
kkread(kvm_t * kd,u_long addr,void * buf,size_t nbytes)135*5071e670SMatthew Dillon kkread(kvm_t *kd, u_long addr, void *buf, size_t nbytes)
136*5071e670SMatthew Dillon {
137*5071e670SMatthew Dillon if (kvm_read(kd, addr, buf, nbytes) != nbytes) {
138*5071e670SMatthew Dillon perror("kvm_read");
139*5071e670SMatthew Dillon exit(1);
140*5071e670SMatthew Dillon }
141*5071e670SMatthew Dillon }
142