xref: /illumos-gate/usr/src/cmd/mdb/common/modules/genunix/bio.c (revision c559157643fef9f9afb0414e00a3579407ba3052)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <mdb/mdb_modapi.h>
28 #include <sys/buf.h>
29 #include <sys/var.h>
30 #include <vm/page.h>
31 
32 #include "bio.h"
33 
34 typedef struct buf_walk {
35 	uintptr_t bw_hbufbase;		/* Base address of hbuf buckets */
36 	struct hbuf *bw_hbufs;		/* Snapshot of hbuf buckets */
37 	size_t bw_nhbufs;		/* Number of hbuf buckets */
38 	size_t bw_hbufi;		/* Current hbuf index */
39 	buf_t *bw_bufp;			/* Current buffer */
40 } buf_walk_t;
41 
42 int
43 buf_walk_init(mdb_walk_state_t *wsp)
44 {
45 	struct hbuf *hbufs;
46 	struct var v;
47 
48 	uintptr_t hbuf_addr;
49 	size_t nbytes;
50 
51 	buf_walk_t *bwp;
52 
53 	if (wsp->walk_addr != 0) {
54 		mdb_warn("only global buf walk supported\n");
55 		return (WALK_ERR);
56 	}
57 
58 	if (mdb_readvar(&v, "v") == -1) {
59 		mdb_warn("failed to read var struct");
60 		return (WALK_ERR);
61 	}
62 
63 	if (mdb_readvar(&hbuf_addr, "hbuf") == -1) {
64 		mdb_warn("failed to read hbuf pointer");
65 		return (WALK_ERR);
66 	}
67 
68 	nbytes = sizeof (struct hbuf) * v.v_hbuf;
69 	hbufs = mdb_alloc(nbytes, UM_SLEEP);
70 
71 	if (mdb_vread(hbufs, nbytes, hbuf_addr) != nbytes) {
72 		mdb_warn("failed to read hbufs");
73 		mdb_free(hbufs, nbytes);
74 		return (WALK_ERR);
75 	}
76 
77 	bwp = mdb_alloc(sizeof (buf_walk_t), UM_SLEEP);
78 
79 	bwp->bw_hbufbase = hbuf_addr;
80 	bwp->bw_hbufs = hbufs;
81 	bwp->bw_nhbufs = v.v_hbuf;
82 	bwp->bw_hbufi = 0;
83 	bwp->bw_bufp = mdb_alloc(sizeof (buf_t), UM_SLEEP);
84 
85 	wsp->walk_addr = (uintptr_t)hbufs[0].b_forw;
86 	wsp->walk_data = bwp;
87 
88 	return (WALK_NEXT);
89 }
90 
91 int
92 buf_walk_step(mdb_walk_state_t *wsp)
93 {
94 	buf_walk_t *bwp = wsp->walk_data;
95 	uintptr_t addr;
96 
97 	/*
98 	 * If the next buf_t address we want is NULL or points back at the
99 	 * hbuf itself, advance to the next hash bucket.  When we reach
100 	 * bw_nhbufs, we're done.
101 	 */
102 	while (wsp->walk_addr == 0 || wsp->walk_addr == (bwp->bw_hbufbase +
103 	    bwp->bw_hbufi * sizeof (struct hbuf))) {
104 
105 		if (++bwp->bw_hbufi == bwp->bw_nhbufs)
106 			return (WALK_DONE);
107 
108 		wsp->walk_addr = (uintptr_t)
109 		    bwp->bw_hbufs[bwp->bw_hbufi].b_forw;
110 	}
111 
112 	/*
113 	 * When we have a buf_t address, read the buffer and invoke our
114 	 * walk callback.  We keep the next buf_t address in wsp->walk_addr.
115 	 */
116 	addr = wsp->walk_addr;
117 	(void) mdb_vread(bwp->bw_bufp, sizeof (buf_t), addr);
118 	wsp->walk_addr = (uintptr_t)bwp->bw_bufp->b_forw;
119 
120 	return (wsp->walk_callback(addr, bwp->bw_bufp, wsp->walk_cbdata));
121 }
122 
123 void
124 buf_walk_fini(mdb_walk_state_t *wsp)
125 {
126 	buf_walk_t *bwp = wsp->walk_data;
127 
128 	mdb_free(bwp->bw_hbufs, sizeof (struct hbuf) * bwp->bw_nhbufs);
129 	mdb_free(bwp->bw_bufp, sizeof (buf_t));
130 	mdb_free(bwp, sizeof (buf_walk_t));
131 }
132 
133 /*ARGSUSED*/
134 int
135 bufpagefind(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
136 {
137 	uintptr_t b_addr = addr;
138 	uintptr_t arg;
139 
140 	page_t p;
141 	buf_t b;
142 
143 	if (argc != 1)
144 		return (DCMD_USAGE);
145 
146 	if (argv->a_type == MDB_TYPE_IMMEDIATE)
147 		arg = (uintptr_t)argv->a_un.a_val;
148 	else
149 		arg = (uintptr_t)mdb_strtoull(argv->a_un.a_str);
150 
151 	if (mdb_vread(&b, sizeof (buf_t), b_addr) == -1)
152 		return (DCMD_ERR);
153 
154 	for (addr = (uintptr_t)b.b_pages; addr != 0;
155 	    addr = (uintptr_t)p.p_next) {
156 
157 		if (addr == arg) {
158 			mdb_printf("buf %p has page %p on b_pages list\n",
159 			    b_addr, addr);
160 			break;
161 		}
162 
163 		if (mdb_vread(&p, sizeof (page_t), addr) == -1)
164 			return (DCMD_ERR);
165 	}
166 
167 	return (DCMD_OK);
168 }
169