xref: /dragonfly/lib/libdmsg/debug.c (revision 0a9eefca514ba2a75efead673e50071f516ed2d1)
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 *
dmsg_basecmd_str(uint32_t cmd)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_HM2:
54                     protostr = "HM2_";
55                     break;
56           case DMSG_PROTO_BLK:
57                     protostr = "BLK_";
58                     break;
59           case DMSG_PROTO_VOP:
60                     protostr = "VOP_";
61                     break;
62           default:
63                     snprintf(protobuf, sizeof(protobuf), "%x_",
64                               (cmd & DMSGF_PROTOS) >> 20);
65                     protostr = protobuf;
66                     break;
67           }
68 
69           switch(cmd & (DMSGF_PROTOS |
70                           DMSGF_CMDS |
71                           DMSGF_SIZE)) {
72           case DMSG_LNK_PAD:
73                     cmdstr = "PAD";
74                     break;
75           case DMSG_LNK_PING:
76                     cmdstr = "PING";
77                     break;
78           case DMSG_LNK_AUTH:
79                     cmdstr = "AUTH";
80                     break;
81           case DMSG_LNK_CONN:
82                     cmdstr = "CONN";
83                     break;
84           case DMSG_LNK_SPAN:
85                     cmdstr = "SPAN";
86                     break;
87           case DMSG_LNK_ERROR:
88                     if (cmd & DMSGF_DELETE)
89                               cmdstr = "RETURN";
90                     else
91                               cmdstr = "RESULT";
92                     break;
93           case DMSG_DBG_SHELL:
94                     cmdstr = "SHELL";
95                     break;
96           default:
97                     snprintf(cmdbuf, sizeof(cmdbuf),
98                                "%06x", (cmd & (DMSGF_PROTOS |
99                                                    DMSGF_CMDS |
100                                                    DMSGF_SIZE)));
101                     cmdstr = cmdbuf;
102                     break;
103           }
104           snprintf(buf, sizeof(buf), "%s%s", protostr, cmdstr);
105           return (buf);
106 }
107 
108 const char *
dmsg_msg_str(dmsg_msg_t * msg)109 dmsg_msg_str(dmsg_msg_t *msg)
110 {
111           dmsg_state_t *state;
112           static char buf[256];
113           char errbuf[16];
114           char statebuf[64];
115           char flagbuf[64];
116           const char *statestr;
117           const char *errstr;
118           uint32_t basecmd;
119           int i;
120 
121           /*
122            * Parse the state
123            */
124           if ((state = msg->state) != NULL) {
125                     basecmd = (state->rxcmd & DMSGF_REPLY) ?
126                                 state->txcmd : state->rxcmd;
127                     snprintf(statebuf, sizeof(statebuf),
128                                " %s=%s,L=%s%s,R=%s%s",
129                                ((state->txcmd & DMSGF_REPLY) ?
130                                         "rcvcmd" : "sndcmd"),
131                                dmsg_basecmd_str(basecmd),
132                                ((state->txcmd & DMSGF_CREATE) ? "C" : ""),
133                                ((state->txcmd & DMSGF_DELETE) ? "D" : ""),
134                                ((state->rxcmd & DMSGF_CREATE) ? "C" : ""),
135                                ((state->rxcmd & DMSGF_DELETE) ? "D" : "")
136                     );
137                     statestr = statebuf;
138           } else {
139                     statestr = "";
140           }
141 
142           /*
143            * Parse the error
144            */
145           switch(msg->any.head.error) {
146           case 0:
147                     errstr = "";
148                     break;
149           case DMSG_IOQ_ERROR_SYNC:
150                     errstr = "err=IOQ:NOSYNC";
151                     break;
152           case DMSG_IOQ_ERROR_EOF:
153                     errstr = "err=IOQ:STREAMEOF";
154                     break;
155           case DMSG_IOQ_ERROR_SOCK:
156                     errstr = "err=IOQ:SOCKERR";
157                     break;
158           case DMSG_IOQ_ERROR_FIELD:
159                     errstr = "err=IOQ:BADFIELD";
160                     break;
161           case DMSG_IOQ_ERROR_HCRC:
162                     errstr = "err=IOQ:BADHCRC";
163                     break;
164           case DMSG_IOQ_ERROR_XCRC:
165                     errstr = "err=IOQ:BADXCRC";
166                     break;
167           case DMSG_IOQ_ERROR_ACRC:
168                     errstr = "err=IOQ:BADACRC";
169                     break;
170           case DMSG_IOQ_ERROR_STATE:
171                     errstr = "err=IOQ:BADSTATE";
172                     break;
173           case DMSG_IOQ_ERROR_NOPEER:
174                     errstr = "err=IOQ:PEERCONFIG";
175                     break;
176           case DMSG_IOQ_ERROR_NORKEY:
177                     errstr = "err=IOQ:BADRKEY";
178                     break;
179           case DMSG_IOQ_ERROR_NOLKEY:
180                     errstr = "err=IOQ:BADLKEY";
181                     break;
182           case DMSG_IOQ_ERROR_KEYXCHGFAIL:
183                     errstr = "err=IOQ:BADKEYXCHG";
184                     break;
185           case DMSG_IOQ_ERROR_KEYFMT:
186                     errstr = "err=IOQ:BADFMT";
187                     break;
188           case DMSG_IOQ_ERROR_BADURANDOM:
189                     errstr = "err=IOQ:BADRANDOM";
190                     break;
191           case DMSG_IOQ_ERROR_MSGSEQ:
192                     errstr = "err=IOQ:BADSEQ";
193                     break;
194           case DMSG_IOQ_ERROR_EALREADY:
195                     errstr = "err=IOQ:DUPMSG";
196                     break;
197           case DMSG_IOQ_ERROR_TRANS:
198                     errstr = "err=IOQ:BADTRANS";
199                     break;
200           case DMSG_IOQ_ERROR_IVWRAP:
201                     errstr = "err=IOQ:IVWRAP";
202                     break;
203           case DMSG_IOQ_ERROR_MACFAIL:
204                     errstr = "err=IOQ:MACFAIL";
205                     break;
206           case DMSG_IOQ_ERROR_ALGO:
207                     errstr = "err=IOQ:ALGOFAIL";
208                     break;
209           case DMSG_ERR_NOSUPP:
210                     errstr = "err=NOSUPPORT";
211                     break;
212           default:
213                     snprintf(errbuf, sizeof(errbuf),
214                                " err=%d", msg->any.head.error);
215                     errstr = errbuf;
216                     break;
217           }
218 
219           /*
220            * Message flags
221            */
222           i = 0;
223           if (msg->any.head.cmd & (DMSGF_CREATE | DMSGF_DELETE |
224                                          DMSGF_ABORT | DMSGF_REPLY)) {
225                     flagbuf[i++] = '|';
226                     if (msg->any.head.cmd & DMSGF_CREATE)
227                               flagbuf[i++] = 'C';
228                     if (msg->any.head.cmd & DMSGF_DELETE)
229                               flagbuf[i++] = 'D';
230                     if (msg->any.head.cmd & DMSGF_REPLY)
231                               flagbuf[i++] = 'R';
232                     if (msg->any.head.cmd & DMSGF_ABORT)
233                               flagbuf[i++] = 'A';
234           }
235           flagbuf[i] = 0;
236 
237           /*
238            * Generate the buf
239            */
240           snprintf(buf, sizeof(buf),
241                     "msg=%s%s %s %s hcrc=%08x id=%016jx",
242                      dmsg_basecmd_str(msg->any.head.cmd),
243                      flagbuf,
244                      errstr,
245                      statestr,
246                      msg->any.head.hdr_crc,
247                      msg->any.head.msgid);
248 
249           return(buf);
250 }
251