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