4 * cc -I/usr/src/sys h2chains.c -o /usr/local/bin/h2chains -lkvm
8 * Copyright (c) 2017 The DragonFly Project. All rights reserved.
10 * This code is derived from software contributed to The DragonFly Project
11 * by Matthew Dillon <dillon@backplane.com>
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
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
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
32 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
33 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
34 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #include <sys/types.h>
40 #include <vfs/hammer2/hammer2.h>
43 #include <vm/vm_page.h>
44 #include <vm/vm_kern.h>
45 #include <vm/swap_pager.h>
46 #include <vm/vnode_pager.h>
56 static void h2chainscan(kvm_t
*kd
, int tab
, uintptr_t cp
,
57 uintptr_t pcp
, int pflags
);
58 static void kkread(kvm_t
*kd
, u_long addr
, void *buf
, size_t nbytes
);
69 main(int ac
, char **av
)
72 const char *corefile
= NULL
;
73 const char *sysfile
= NULL
;
79 while ((ch
= getopt(ac
, av
, "M:N:")) != -1) {
88 fprintf(stderr
, "%s [-M core] [-N system]\n", av
[0]);
95 if ((kd
= kvm_open(sysfile
, corefile
, NULL
, O_RDONLY
, "kvm:")) == NULL
) {
100 if (kvm_nlist(kd
, Nl
) != 0) {
104 kkread(kd
, Nl
[0].n_value
, &nch
, sizeof(nch
));
105 kkread(kd
, Nl
[1].n_value
, &list
, sizeof(list
));
108 base
= strtoul(av
[0], NULL
, 0);
110 kkread(kd
, base
, &hmp
, sizeof(hmp
));
111 cp
= (uintptr_t)hmp
.vchain
.core
.rbtree
.rbh_root
;
112 printf("VCHAIN %08x\n", hmp
.vchain
.flags
);
114 h2chainscan(kd
, 4, cp
,
115 base
+ offsetof(struct hammer2_dev
, vchain
),
119 cp
= (uintptr_t)hmp
.fchain
.core
.rbtree
.rbh_root
;
120 printf("FCHAIN %08x\n", hmp
.fchain
.flags
);
122 h2chainscan(kd
, 4, cp
,
123 base
+ offsetof(struct hammer2_dev
, fchain
),
131 h2chainscan(kvm_t
*kd
, int tab
, uintptr_t cp
, uintptr_t pcp
, int pflags
)
133 hammer2_chain_t chain
;
135 kkread(kd
, cp
, &chain
, sizeof(chain
));
136 if (chain
.rbnode
.rbe_left
)
137 h2chainscan(kd
, tab
, (uintptr_t)chain
.rbnode
.rbe_left
,
140 printf("%*.*s chain %p %08x type %02x ", tab
, tab
, "",
141 (void *)cp
, chain
.flags
, chain
.bref
.type
);
142 if (chain
.flags
& HAMMER2_CHAIN_ONFLUSH
)
144 if (chain
.flags
& HAMMER2_CHAIN_MODIFIED
)
146 if (chain
.flags
& (HAMMER2_CHAIN_ONFLUSH
|HAMMER2_CHAIN_MODIFIED
)) {
147 if ((pflags
& HAMMER2_CHAIN_ONFLUSH
) == 0)
150 if ((uintptr_t)chain
.parent
!= pcp
)
153 if (chain
.core
.rbtree
.rbh_root
)
154 h2chainscan(kd
, tab
+ 4,
155 (uintptr_t)chain
.core
.rbtree
.rbh_root
,
158 if (chain
.rbnode
.rbe_right
)
159 h2chainscan(kd
, tab
, (uintptr_t)chain
.rbnode
.rbe_right
,
165 kkread(kvm_t
*kd
, u_long addr
, void *buf
, size_t nbytes
)
167 if (kvm_read(kd
, addr
, buf
, nbytes
) != nbytes
) {