xref: /freebsd/sys/geom/geom_dump.c (revision 42c159fe388a3765f69860c84183700af37aca8a)
1 /*-
2  * Copyright (c) 2002 Poul-Henning Kamp
3  * Copyright (c) 2002 Networks Associates Technology, Inc.
4  * All rights reserved.
5  *
6  * This software was developed for the FreeBSD Project by Poul-Henning Kamp
7  * and NAI Labs, the Security Research Division of Network Associates, Inc.
8  * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
9  * DARPA CHATS research program.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. The names of the authors may not be used to endorse or promote
20  *    products derived from this software without specific prior written
21  *    permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  * $FreeBSD$
36  */
37 
38 
39 #include <sys/param.h>
40 #include <sys/sbuf.h>
41 #ifndef _KERNEL
42 #include <stdio.h>
43 #include <unistd.h>
44 #include <stdlib.h>
45 #include <err.h>
46 #else
47 #include <sys/systm.h>
48 #include <sys/malloc.h>
49 #endif
50 #include <machine/stdarg.h>
51 
52 #include <geom/geom.h>
53 
54 
55 static void
56 g_confdot_consumer(struct sbuf *sb, struct g_consumer *cp)
57 {
58 
59 	sbuf_printf(sb, "z%p [label=\"r%dw%de%d\\nbio #%d\"];\n",
60 	    cp, cp->acr, cp->acw, cp->ace, cp->biocount);
61 	if (cp->provider)
62 		sbuf_printf(sb, "z%p -> z%p;\n", cp, cp->provider);
63 }
64 
65 static void
66 g_confdot_provider(struct sbuf *sb, struct g_provider *pp)
67 {
68 
69 	sbuf_printf(sb, "z%p [shape=hexagon,label=\"%s\\nr%dw%de%d\\nerr#%d\"];\n",
70 	    pp, pp->name, pp->acr, pp->acw, pp->ace, pp->error);
71 }
72 
73 static void
74 g_confdot_geom(struct sbuf *sb, struct g_geom *gp)
75 {
76 	struct g_consumer *cp;
77 	struct g_provider *pp;
78 
79 	sbuf_printf(sb, "z%p [shape=box,label=\"%s\\n%s\\nr#%d\"];\n",
80 	    gp, gp->method->name, gp->name, gp->rank);
81 	LIST_FOREACH(cp, &gp->consumer, consumer) {
82 		g_confdot_consumer(sb, cp);
83 		sbuf_printf(sb, "z%p -> z%p;\n", gp, cp);
84 	}
85 
86 	LIST_FOREACH(pp, &gp->provider, provider) {
87 		g_confdot_provider(sb, pp);
88 		sbuf_printf(sb, "z%p -> z%p;\n", pp, gp);
89 	}
90 }
91 
92 static void
93 g_confdot_method(struct sbuf *sb, struct g_method *mp)
94 {
95 	struct g_geom *gp;
96 
97 	LIST_FOREACH(gp, &mp->geom, geom)
98 		g_confdot_geom(sb, gp);
99 }
100 
101 struct sbuf *
102 g_confdot(void)
103 {
104 	struct g_method *mp;
105 	struct sbuf *sb;
106 
107 	sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
108 	sbuf_clear(sb);
109 	sbuf_printf(sb, "digraph geom {\n");
110 	LIST_FOREACH(mp, &g_methods, method)
111 		g_confdot_method(sb, mp);
112 	sbuf_printf(sb, "};\n");
113 	sbuf_finish(sb);
114 	return (sb);
115 }
116 
117 
118 static void
119 g_conf_consumer(struct sbuf *sb, struct g_consumer *cp)
120 {
121 
122 	sbuf_printf(sb, "\t<consumer>\n");
123 	sbuf_printf(sb, "\t  <ref>%p</ref>\n", cp);
124 	sbuf_printf(sb, "\t  <geom><ref>%p</ref></geom>\n", cp->geom);
125 	sbuf_printf(sb, "\t  <provider><ref>%p</ref></provider>\n", cp->provider);
126 	sbuf_printf(sb, "\t  <mode>r%dw%de%d</mode>\n",
127 	    cp->acr, cp->acw, cp->ace);
128 	if (cp->geom->dumpconf) {
129 		sbuf_printf(sb, "\t  <config>\n");
130 		cp->geom->dumpconf(sb, "\t    ", cp->geom, cp, NULL);
131 		sbuf_printf(sb, "\t  </config>\n");
132 	}
133 	sbuf_printf(sb, "\t</consumer>\n");
134 }
135 
136 static void
137 g_conf_provider(struct sbuf *sb, struct g_provider *pp)
138 {
139 
140 	sbuf_printf(sb, "\t<provider>\n", pp);
141 	sbuf_printf(sb, "\t  <ref>%p</ref>\n", pp);
142 	sbuf_printf(sb, "\t  <geom><ref>%p</ref></geom>\n", pp->geom);
143 	sbuf_printf(sb, "\t  <mode>r%dw%de%d</mode>\n",
144 	    pp->acr, pp->acw, pp->ace);
145 	sbuf_printf(sb, "\t  <name>%s</name>\n", pp->name);
146 	if (pp->geom->dumpconf) {
147 		sbuf_printf(sb, "\t  <config>\n");
148 		pp->geom->dumpconf(sb, "\t    ", pp->geom, NULL, pp);
149 		sbuf_printf(sb, "\t  </config>\n");
150 	}
151 	sbuf_printf(sb, "\t</provider>\n");
152 }
153 
154 
155 static void
156 g_conf_geom(struct sbuf *sb, struct g_geom *gp, struct g_provider *pp, struct g_consumer *cp)
157 {
158 	struct g_consumer *cp2;
159 	struct g_provider *pp2;
160 
161 	sbuf_printf(sb, "    <geom>\n");
162 	sbuf_printf(sb, "      <ref>%p</ref>\n", gp);
163 	sbuf_printf(sb, "      <method><ref>%p</ref></method>\n", gp->method);
164 	sbuf_printf(sb, "      <name>%s</name>\n", gp->name);
165 	sbuf_printf(sb, "      <rank>%d</rank>\n", gp->rank);
166 	if (gp->dumpconf) {
167 		sbuf_printf(sb, "      <config>\n");
168 		gp->dumpconf(sb, "\t", gp, NULL, NULL);
169 		sbuf_printf(sb, "      </config>\n");
170 	}
171 	LIST_FOREACH(cp2, &gp->consumer, consumer) {
172 		if (cp != NULL && cp != cp2)
173 			continue;
174 		g_conf_consumer(sb, cp2);
175 	}
176 
177 	LIST_FOREACH(pp2, &gp->provider, provider) {
178 		if (pp != NULL && pp != pp2)
179 			continue;
180 		g_conf_provider(sb, pp2);
181 	}
182 	sbuf_printf(sb, "    </geom>\n");
183 }
184 
185 static void
186 g_conf_method(struct sbuf *sb, struct g_method *mp, struct g_geom *gp, struct g_provider *pp, struct g_consumer *cp)
187 {
188 	struct g_geom *gp2;
189 
190 	sbuf_printf(sb, "  <method>\n");
191 	sbuf_printf(sb, "    <ref>%p</ref>\n", mp);
192 	sbuf_printf(sb, "    <name>%s</name>\n", mp->name);
193 	LIST_FOREACH(gp2, &mp->geom, geom) {
194 		if (gp != NULL && gp != gp2)
195 			continue;
196 		g_conf_geom(sb, gp2, pp, cp);
197 	}
198 	sbuf_printf(sb, "  </method>\n");
199 }
200 
201 struct sbuf *
202 g_conf_specific(struct g_method *mp, struct g_geom *gp, struct g_provider *pp, struct g_consumer *cp)
203 {
204 	struct g_method *mp2;
205 	struct sbuf *sb;
206 
207 	sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
208 	sbuf_clear(sb);
209 	sbuf_printf(sb, "<mesh>\n");
210 	LIST_FOREACH(mp2, &g_methods, method) {
211 		if (mp != NULL && mp != mp2)
212 			continue;
213 		g_conf_method(sb, mp2, gp, pp, cp);
214 	}
215 	sbuf_printf(sb, "</mesh>\n");
216 	sbuf_finish(sb);
217 	return (sb);
218 }
219 
220 struct sbuf *
221 g_conf()
222 {
223 
224 	return (g_conf_specific(NULL, NULL, NULL, NULL));
225 }
226 
227 
228 void
229 g_trace(int level, char *fmt, ...)
230 {
231 	va_list ap;
232 	struct sbuf *sb;
233 
234 	if (!(g_debugflags & level))
235 		return;
236 	va_start(ap, fmt);
237 	mtx_lock(&Giant);
238 	sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
239 	sbuf_vprintf(sb, fmt, ap);
240 	sbuf_finish(sb);
241 	mtx_unlock(&Giant);
242 	printf("%s\n", sbuf_data(sb));
243 	sbuf_delete(sb);
244 }
245 
246 void
247 g_hexdump(void *ptr, int length)
248 {
249 	int i, j, k;
250 	unsigned char *cp;
251 
252 	cp = ptr;
253 	for (i = 0; i < length; i+= 16) {
254 		printf("%04x  ", i);
255 		for (j = 0; j < 16; j++) {
256 			k = i + j;
257 			if (k < length)
258 				printf(" %02x", cp[k]);
259 			else
260 				printf("   ");
261 		}
262 		printf("  |");
263 		for (j = 0; j < 16; j++) {
264 			k = i + j;
265 			if (k >= length)
266 				printf(" ");
267 			else if (cp[k] >= ' ' && cp[k] <= '~')
268 				printf("%c", cp[k]);
269 			else
270 				printf(".");
271 		}
272 		printf("|\n");
273 	}
274 }
275 
276