xref: /dflybsd-src/lib/libdmsg/debug.c (revision 6f25d5554e6df42aa5235bf7e3e8e1cc9da25eec)
1 /*
2  * Copyright (c) 2012 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@dragonflybsd.org>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #include "dmsg_local.h"
36 
37 const char *
38 dmsg_basecmd_str(uint32_t cmd)
39 {
40 	static char buf[64];
41 	char protobuf[32];
42 	char cmdbuf[32];
43 	const char *protostr;
44 	const char *cmdstr;
45 
46 	switch(cmd & DMSGF_PROTOS) {
47 	case DMSG_PROTO_LNK:
48 		protostr = "LNK_";
49 		break;
50 	case DMSG_PROTO_DBG:
51 		protostr = "DBG_";
52 		break;
53 	case DMSG_PROTO_DOM:
54 		protostr = "DOM_";
55 		break;
56 	case DMSG_PROTO_CAC:
57 		protostr = "CAC_";
58 		break;
59 	case DMSG_PROTO_QRM:
60 		protostr = "QRM_";
61 		break;
62 	case DMSG_PROTO_BLK:
63 		protostr = "BLK_";
64 		break;
65 	case DMSG_PROTO_VOP:
66 		protostr = "VOP_";
67 		break;
68 	default:
69 		snprintf(protobuf, sizeof(protobuf), "%x_",
70 			(cmd & DMSGF_PROTOS) >> 20);
71 		protostr = protobuf;
72 		break;
73 	}
74 
75 	switch(cmd & (DMSGF_PROTOS |
76 		      DMSGF_CMDS |
77 		      DMSGF_SIZE)) {
78 	case DMSG_LNK_PAD:
79 		cmdstr = "PAD";
80 		break;
81 	case DMSG_LNK_PING:
82 		cmdstr = "PING";
83 		break;
84 	case DMSG_LNK_AUTH:
85 		cmdstr = "AUTH";
86 		break;
87 	case DMSG_LNK_CONN:
88 		cmdstr = "CONN";
89 		break;
90 	case DMSG_LNK_SPAN:
91 		cmdstr = "SPAN";
92 		break;
93 	case DMSG_LNK_ERROR:
94 		if (cmd & DMSGF_DELETE)
95 			cmdstr = "RETURN";
96 		else
97 			cmdstr = "RESULT";
98 		break;
99 	case DMSG_DBG_SHELL:
100 		cmdstr = "SHELL";
101 		break;
102 	default:
103 		snprintf(cmdbuf, sizeof(cmdbuf),
104 			 "%06x", (cmd & (DMSGF_PROTOS |
105 					 DMSGF_CMDS |
106 					 DMSGF_SIZE)));
107 		cmdstr = cmdbuf;
108 		break;
109 	}
110 	snprintf(buf, sizeof(buf), "%s%s", protostr, cmdstr);
111 	return (buf);
112 }
113 
114 const char *
115 dmsg_msg_str(dmsg_msg_t *msg)
116 {
117 	dmsg_state_t *state;
118 	static char buf[256];
119 	char errbuf[16];
120 	char statebuf[64];
121 	char flagbuf[64];
122 	const char *statestr;
123 	const char *errstr;
124 	uint32_t basecmd;
125 	int i;
126 
127 	/*
128 	 * Parse the state
129 	 */
130 	if ((state = msg->state) != NULL) {
131 		basecmd = (state->rxcmd & DMSGF_REPLY) ?
132 			  state->txcmd : state->rxcmd;
133 		snprintf(statebuf, sizeof(statebuf),
134 			 " %s=%s,L=%s%s,R=%s%s",
135 			 ((state->txcmd & DMSGF_REPLY) ?
136 				"rcvcmd" : "sndcmd"),
137 			 dmsg_basecmd_str(basecmd),
138 			 ((state->txcmd & DMSGF_CREATE) ? "C" : ""),
139 			 ((state->txcmd & DMSGF_DELETE) ? "D" : ""),
140 			 ((state->rxcmd & DMSGF_CREATE) ? "C" : ""),
141 			 ((state->rxcmd & DMSGF_DELETE) ? "D" : "")
142 		);
143 		statestr = statebuf;
144 	} else {
145 		statestr = "";
146 	}
147 
148 	/*
149 	 * Parse the error
150 	 */
151 	switch(msg->any.head.error) {
152 	case 0:
153 		errstr = "";
154 		break;
155 	case DMSG_IOQ_ERROR_SYNC:
156 		errstr = "err=IOQ:NOSYNC";
157 		break;
158 	case DMSG_IOQ_ERROR_EOF:
159 		errstr = "err=IOQ:STREAMEOF";
160 		break;
161 	case DMSG_IOQ_ERROR_SOCK:
162 		errstr = "err=IOQ:SOCKERR";
163 		break;
164 	case DMSG_IOQ_ERROR_FIELD:
165 		errstr = "err=IOQ:BADFIELD";
166 		break;
167 	case DMSG_IOQ_ERROR_HCRC:
168 		errstr = "err=IOQ:BADHCRC";
169 		break;
170 	case DMSG_IOQ_ERROR_XCRC:
171 		errstr = "err=IOQ:BADXCRC";
172 		break;
173 	case DMSG_IOQ_ERROR_ACRC:
174 		errstr = "err=IOQ:BADACRC";
175 		break;
176 	case DMSG_IOQ_ERROR_STATE:
177 		errstr = "err=IOQ:BADSTATE";
178 		break;
179 	case DMSG_IOQ_ERROR_NOPEER:
180 		errstr = "err=IOQ:PEERCONFIG";
181 		break;
182 	case DMSG_IOQ_ERROR_NORKEY:
183 		errstr = "err=IOQ:BADRKEY";
184 		break;
185 	case DMSG_IOQ_ERROR_NOLKEY:
186 		errstr = "err=IOQ:BADLKEY";
187 		break;
188 	case DMSG_IOQ_ERROR_KEYXCHGFAIL:
189 		errstr = "err=IOQ:BADKEYXCHG";
190 		break;
191 	case DMSG_IOQ_ERROR_KEYFMT:
192 		errstr = "err=IOQ:BADFMT";
193 		break;
194 	case DMSG_IOQ_ERROR_BADURANDOM:
195 		errstr = "err=IOQ:BADRANDOM";
196 		break;
197 	case DMSG_IOQ_ERROR_MSGSEQ:
198 		errstr = "err=IOQ:BADSEQ";
199 		break;
200 	case DMSG_IOQ_ERROR_EALREADY:
201 		errstr = "err=IOQ:DUPMSG";
202 		break;
203 	case DMSG_IOQ_ERROR_TRANS:
204 		errstr = "err=IOQ:BADTRANS";
205 		break;
206 	case DMSG_IOQ_ERROR_IVWRAP:
207 		errstr = "err=IOQ:IVWRAP";
208 		break;
209 	case DMSG_IOQ_ERROR_MACFAIL:
210 		errstr = "err=IOQ:MACFAIL";
211 		break;
212 	case DMSG_IOQ_ERROR_ALGO:
213 		errstr = "err=IOQ:ALGOFAIL";
214 		break;
215 	case DMSG_ERR_NOSUPP:
216 		errstr = "err=NOSUPPORT";
217 		break;
218 	default:
219 		snprintf(errbuf, sizeof(errbuf),
220 			 " err=%d", msg->any.head.error);
221 		errstr = errbuf;
222 		break;
223 	}
224 
225 	/*
226 	 * Message flags
227 	 */
228 	i = 0;
229 	if (msg->any.head.cmd & (DMSGF_CREATE | DMSGF_DELETE |
230 				 DMSGF_ABORT | DMSGF_REPLY)) {
231 		flagbuf[i++] = '|';
232 		if (msg->any.head.cmd & DMSGF_CREATE)
233 			flagbuf[i++] = 'C';
234 		if (msg->any.head.cmd & DMSGF_DELETE)
235 			flagbuf[i++] = 'D';
236 		if (msg->any.head.cmd & DMSGF_REPLY)
237 			flagbuf[i++] = 'R';
238 		if (msg->any.head.cmd & DMSGF_ABORT)
239 			flagbuf[i++] = 'A';
240 	}
241 	flagbuf[i] = 0;
242 
243 	/*
244 	 * Generate the buf
245 	 */
246 	snprintf(buf, sizeof(buf),
247 		"msg=%s%s %s msgid=%08x %s",
248 		 dmsg_basecmd_str(msg->any.head.cmd),
249 		 flagbuf,
250 		 errstr,
251 		 (uint32_t)(intmax_t)msg->any.head.msgid,   /* for brevity */
252 		 statestr);
253 
254 	return(buf);
255 }
256