xref: /titanic_52/usr/src/cmd/mdb/common/modules/genunix/bio.c (revision 0eb822a1c0c2bea495647510b75f77f0e57633eb)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <mdb/mdb_modapi.h>
30 #include <sys/buf.h>
31 #include <sys/var.h>
32 #include <vm/page.h>
33 
34 #include "bio.h"
35 
36 typedef struct buf_walk {
37 	uintptr_t bw_hbufbase;		/* Base address of hbuf buckets */
38 	struct hbuf *bw_hbufs;		/* Snapshot of hbuf buckets */
39 	size_t bw_nhbufs;		/* Number of hbuf buckets */
40 	size_t bw_hbufi;		/* Current hbuf index */
41 	buf_t *bw_bufp;			/* Current buffer */
42 } buf_walk_t;
43 
44 int
45 buf_walk_init(mdb_walk_state_t *wsp)
46 {
47 	struct hbuf *hbufs;
48 	struct var v;
49 
50 	uintptr_t hbuf_addr;
51 	size_t nbytes;
52 
53 	buf_walk_t *bwp;
54 
55 	if (wsp->walk_addr != NULL) {
56 		mdb_warn("only global buf walk supported\n");
57 		return (WALK_ERR);
58 	}
59 
60 	if (mdb_readvar(&v, "v") == -1) {
61 		mdb_warn("failed to read var struct");
62 		return (WALK_ERR);
63 	}
64 
65 	if (mdb_readvar(&hbuf_addr, "hbuf") == -1) {
66 		mdb_warn("failed to read hbuf pointer");
67 		return (WALK_ERR);
68 	}
69 
70 	nbytes = sizeof (struct hbuf) * v.v_hbuf;
71 	hbufs = mdb_alloc(nbytes, UM_SLEEP);
72 
73 	if (mdb_vread(hbufs, nbytes, hbuf_addr) != nbytes) {
74 		mdb_warn("failed to read hbufs");
75 		mdb_free(hbufs, nbytes);
76 		return (WALK_ERR);
77 	}
78 
79 	bwp = mdb_alloc(sizeof (buf_walk_t), UM_SLEEP);
80 
81 	bwp->bw_hbufbase = hbuf_addr;
82 	bwp->bw_hbufs = hbufs;
83 	bwp->bw_nhbufs = v.v_hbuf;
84 	bwp->bw_hbufi = 0;
85 	bwp->bw_bufp = mdb_alloc(sizeof (buf_t), UM_SLEEP);
86 
87 	wsp->walk_addr = (uintptr_t)hbufs[0].b_forw;
88 	wsp->walk_data = bwp;
89 
90 	return (WALK_NEXT);
91 }
92 
93 int
94 buf_walk_step(mdb_walk_state_t *wsp)
95 {
96 	buf_walk_t *bwp = wsp->walk_data;
97 	uintptr_t addr;
98 
99 	/*
100 	 * If the next buf_t address we want is NULL or points back at the
101 	 * hbuf itself, advance to the next hash bucket.  When we reach
102 	 * bw_nhbufs, we're done.
103 	 */
104 	while (wsp->walk_addr == NULL || wsp->walk_addr == (bwp->bw_hbufbase +
105 	    bwp->bw_hbufi * sizeof (struct hbuf))) {
106 
107 		if (++bwp->bw_hbufi == bwp->bw_nhbufs)
108 			return (WALK_DONE);
109 
110 		wsp->walk_addr = (uintptr_t)
111 		    bwp->bw_hbufs[bwp->bw_hbufi].b_forw;
112 	}
113 
114 	/*
115 	 * When we have a buf_t address, read the buffer and invoke our
116 	 * walk callback.  We keep the next buf_t address in wsp->walk_addr.
117 	 */
118 	addr = wsp->walk_addr;
119 	(void) mdb_vread(bwp->bw_bufp, sizeof (buf_t), addr);
120 	wsp->walk_addr = (uintptr_t)bwp->bw_bufp->b_forw;
121 
122 	return (wsp->walk_callback(addr, bwp->bw_bufp, wsp->walk_cbdata));
123 }
124 
125 void
126 buf_walk_fini(mdb_walk_state_t *wsp)
127 {
128 	buf_walk_t *bwp = wsp->walk_data;
129 
130 	mdb_free(bwp->bw_hbufs, sizeof (struct hbuf) * bwp->bw_nhbufs);
131 	mdb_free(bwp->bw_bufp, sizeof (buf_t));
132 	mdb_free(bwp, sizeof (buf_walk_t));
133 }
134 
135 /*ARGSUSED*/
136 int
137 bufpagefind(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
138 {
139 	uintptr_t b_addr = addr;
140 	uintptr_t arg;
141 
142 	page_t p;
143 	buf_t b;
144 
145 	if (argc != 1)
146 		return (DCMD_USAGE);
147 
148 	if (argv->a_type == MDB_TYPE_IMMEDIATE)
149 		arg = (uintptr_t)argv->a_un.a_val;
150 	else
151 		arg = (uintptr_t)mdb_strtoull(argv->a_un.a_str);
152 
153 	if (mdb_vread(&b, sizeof (buf_t), b_addr) == -1)
154 		return (DCMD_ERR);
155 
156 	for (addr = (uintptr_t)b.b_pages; addr != NULL;
157 	    addr = (uintptr_t)p.p_next) {
158 
159 		if (addr == arg) {
160 			mdb_printf("buf %p has page %p on b_pages list\n",
161 			    b_addr, addr);
162 			break;
163 		}
164 
165 		if (mdb_vread(&p, sizeof (page_t), addr) == -1)
166 			return (DCMD_ERR);
167 	}
168 
169 	return (DCMD_OK);
170 }
171