da2a1dbffb4ffad70fc5e5f55f9aa2c474a9c7a8
[dragonfly.git] / sbin / hammer2 / cmd_debug.c
1 /*
2  * Copyright (c) 2011-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  * by Venkatesh Srinivas <vsrinivas@dragonflybsd.org>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the
17  *    distribution.
18  * 3. Neither the name of The DragonFly Project nor the names of its
19  *    contributors may be used to endorse or promote products derived
20  *    from this software without specific, prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
26  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35
36 #include "hammer2.h"
37
38 #define SHOW_TAB        2
39
40 static void shell_rcvmsg(hammer2_iocom_t *iocom, hammer2_msg_t *msg);
41 static void shell_ttymsg(hammer2_iocom_t *iocom);
42 static void hammer2_shell_parse(hammer2_iocom_t *iocom, hammer2_msg_t *msg);
43
44 /************************************************************************
45  *                                  SHELL                               *
46  ************************************************************************/
47
48 int
49 cmd_shell(const char *hostname)
50 {
51         struct sockaddr_in lsin;
52         struct hammer2_iocom iocom;
53         hammer2_msg_t *msg;
54         struct hostent *hen;
55         int fd;
56
57         /*
58          * Acquire socket and set options
59          */
60         if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
61                 fprintf(stderr, "cmd_debug: socket(): %s\n",
62                         strerror(errno));
63                 return 1;
64         }
65
66         /*
67          * Connect to the target
68          */
69         bzero(&lsin, sizeof(lsin));
70         lsin.sin_family = AF_INET;
71         lsin.sin_addr.s_addr = 0;
72         lsin.sin_port = htons(HAMMER2_LISTEN_PORT);
73
74         if (hostname) {
75                 hen = gethostbyname2(hostname, AF_INET);
76                 if (hen == NULL) {
77                         if (inet_pton(AF_INET, hostname, &lsin.sin_addr) != 1) {
78                                 fprintf(stderr,
79                                         "Cannot resolve %s\n", hostname);
80                                 return 1;
81                         }
82                 } else {
83                         bcopy(hen->h_addr, &lsin.sin_addr, hen->h_length);
84                 }
85         }
86         if (connect(fd, (struct sockaddr *)&lsin, sizeof(lsin)) < 0) {
87                 close(fd);
88                 fprintf(stderr, "debug: connect failed: %s\n",
89                         strerror(errno));
90                 return 0;
91         }
92
93         /*
94          * Run the session.  The remote end transmits our prompt.
95          */
96         hammer2_iocom_init(&iocom, fd, 0, NULL, shell_rcvmsg, shell_ttymsg);
97         printf("debug: connected\n");
98
99         msg = hammer2_msg_alloc(&iocom, 0, HAMMER2_DBG_SHELL);
100         hammer2_msg_write(&iocom, msg, NULL, NULL, NULL);
101         hammer2_iocom_core(&iocom);
102         fprintf(stderr, "debug: disconnected\n");
103         close(fd);
104         return 0;
105 }
106
107 /*
108  * Callback from hammer2_iocom_core() when messages might be present
109  * on the socket.
110  */
111 static
112 void
113 shell_rcvmsg(hammer2_iocom_t *iocom, hammer2_msg_t *msg)
114 {
115         switch(msg->any.head.cmd & HAMMER2_MSGF_CMDSWMASK) {
116         case HAMMER2_LNK_ERROR:
117         case HAMMER2_LNK_ERROR | HAMMER2_MSGF_REPLY:
118                 if (msg->any.head.error) {
119                         fprintf(stderr,
120                                 "Link Error: %d\n",
121                                 msg->any.head.error);
122                 } else {
123                         write(1, "debug> ", 7);
124                 }
125                 break;
126         case HAMMER2_DBG_SHELL:
127                 /*
128                  * We send the commands, not accept them.
129                  */
130                 hammer2_msg_reply(iocom, msg, HAMMER2_MSG_ERR_UNKNOWN);
131                 break;
132         case HAMMER2_DBG_SHELL | HAMMER2_MSGF_REPLY:
133                 /*
134                  * A reply from the remote is data we copy to stdout.
135                  */
136                 if (msg->aux_size) {
137                         msg->aux_data[msg->aux_size - 1] = 0;
138                         write(1, msg->aux_data, strlen(msg->aux_data));
139                 }
140                 break;
141         default:
142                 fprintf(stderr, "Unknown message: %08x\n",
143                         msg->any.head.cmd);
144                 assert((msg->any.head.cmd & HAMMER2_MSGF_REPLY) == 0);
145                 hammer2_msg_reply(iocom, msg, HAMMER2_MSG_ERR_UNKNOWN);
146                 break;
147         }
148 }
149
150 static
151 void
152 shell_ttymsg(hammer2_iocom_t *iocom)
153 {
154         hammer2_msg_t *msg;
155         char buf[256];
156         size_t len;
157
158         if (fgets(buf, sizeof(buf), stdin) != NULL) {
159                 len = strlen(buf);
160                 if (len && buf[len - 1] == '\n')
161                         buf[--len] = 0;
162                 ++len;
163                 msg = hammer2_msg_alloc(iocom, len, HAMMER2_DBG_SHELL);
164                 bcopy(buf, msg->aux_data, len);
165                 hammer2_msg_write(iocom, msg, NULL, NULL, NULL);
166         } else {
167                 /*
168                  * Set EOF flag without setting any error code for normal
169                  * EOF.
170                  */
171                 iocom->flags |= HAMMER2_IOCOMF_EOF;
172         }
173 }
174
175 /*
176  * This is called from the master node to process a received debug
177  * shell command.  We process the command, outputting the results,
178  * then finish up by outputting another prompt.
179  */
180 void
181 hammer2_msg_dbg(hammer2_iocom_t *iocom, hammer2_msg_t *msg)
182 {
183         switch(msg->any.head.cmd & HAMMER2_MSGF_CMDSWMASK) {
184         case HAMMER2_DBG_SHELL:
185                 /*
186                  * This is a command which we must process.
187                  * When we are finished we generate a final reply.
188                  */
189                 if (msg->aux_data)
190                         msg->aux_data[msg->aux_size - 1] = 0;
191                 hammer2_shell_parse(iocom, msg);
192                 hammer2_msg_reply(iocom, msg, 0);
193                 break;
194         case HAMMER2_DBG_SHELL | HAMMER2_MSGF_REPLY:
195                 /*
196                  * A reply just prints out the string.  No newline is added
197                  * (it is expected to be embedded if desired).
198                  */
199                 if (msg->aux_data)
200                         msg->aux_data[msg->aux_size - 1] = 0;
201                 if (msg->aux_data)
202                         write(2, msg->aux_data, strlen(msg->aux_data));
203                 break;
204         default:
205                 hammer2_msg_reply(iocom, msg, HAMMER2_MSG_ERR_UNKNOWN);
206                 break;
207         }
208 }
209
210 static void
211 hammer2_shell_parse(hammer2_iocom_t *iocom, hammer2_msg_t *msg)
212 {
213         char *cmdbuf = msg->aux_data;
214         char *cmd = strsep(&cmdbuf, " \t");
215
216         if (cmd == NULL || *cmd == 0) {
217                 ;
218         } else if (strcmp(cmd, "help") == 0 || strcmp(cmd, "?") == 0) {
219                 iocom_printf(iocom, 0, "help        Command help\n");
220         } else {
221                 iocom_printf(iocom, 0, "Unrecognized command: %s\n", cmd);
222         }
223 }
224
225 /*
226  * Returns text debug output to the original defined by (msg).  (msg) is
227  * not modified and stays intact.  We use a one-way message with REPLY set
228  * to distinguish between a debug command and debug terminal output.
229  *
230  * To prevent loops iocom_printf() can filter the message (cmd) related
231  * to the iocom_printf().  We filter out DBG messages.
232  */
233 void
234 iocom_printf(hammer2_iocom_t *iocom, uint32_t cmd, const char *ctl, ...)
235 {
236         hammer2_msg_t *rmsg;
237         va_list va;
238         char buf[1024];
239         size_t len;
240
241         if ((cmd & HAMMER2_MSGF_PROTOS) == HAMMER2_MSG_PROTO_DBG)
242                 return;
243
244         va_start(va, ctl);
245         vsnprintf(buf, sizeof(buf), ctl, va);
246         va_end(va);
247         len = strlen(buf) + 1;
248
249         rmsg = hammer2_msg_alloc(iocom, len, HAMMER2_DBG_SHELL |
250                                              HAMMER2_MSGF_REPLY);
251         bcopy(buf, rmsg->aux_data, len);
252
253         hammer2_msg_write(iocom, rmsg, NULL, NULL, NULL);
254 }
255
256 /************************************************************************
257  *                                  SHOW                                *
258  ************************************************************************/
259
260 static void show_bref(int fd, int tab, int bi, hammer2_blockref_t *bref);
261 static void tabprintf(int tab, const char *ctl, ...);
262
263 int
264 cmd_show(const char *devpath)
265 {
266         hammer2_blockref_t broot;
267         int fd;
268
269         fd = open(devpath, O_RDONLY);
270         if (fd < 0) {
271                 perror("open");
272                 return 1;
273         }
274         bzero(&broot, sizeof(broot));
275         broot.type = HAMMER2_BREF_TYPE_VOLUME;
276         broot.data_off = 0 | HAMMER2_PBUFRADIX;
277         show_bref(fd, 0, 0, &broot);
278         close(fd);
279
280         return 0;
281 }
282
283 static void
284 show_bref(int fd, int tab, int bi, hammer2_blockref_t *bref)
285 {
286         hammer2_media_data_t media;
287         hammer2_blockref_t *bscan;
288         int bcount;
289         int i;
290         int didnl;
291         int namelen;
292         int obrace = 1;
293         size_t bytes;
294         const char *type_str;
295         char *str = NULL;
296
297         switch(bref->type) {
298         case HAMMER2_BREF_TYPE_EMPTY:
299                 type_str = "empty";
300                 break;
301         case HAMMER2_BREF_TYPE_INODE:
302                 type_str = "inode";
303                 break;
304         case HAMMER2_BREF_TYPE_INDIRECT:
305                 type_str = "indblk";
306                 break;
307         case HAMMER2_BREF_TYPE_DATA:
308                 type_str = "data";
309                 break;
310         case HAMMER2_BREF_TYPE_VOLUME:
311                 type_str = "volume";
312                 break;
313         default:
314                 type_str = "unknown";
315                 break;
316         }
317
318
319         tabprintf(tab, "%s.%-3d %016jx %016jx/%-2d mir=%016jx mod=%016jx ",
320                type_str, bi, (intmax_t)bref->data_off,
321                (intmax_t)bref->key, (intmax_t)bref->keybits,
322                (intmax_t)bref->mirror_tid, (intmax_t)bref->modify_tid);
323         tab += SHOW_TAB;
324
325         bytes = (size_t)1 << (bref->data_off & HAMMER2_OFF_MASK_RADIX);
326         if (bytes < HAMMER2_MINIOSIZE || bytes > sizeof(media)) {
327                 printf("(bad block size %zd)\n", bytes);
328                 return;
329         }
330         if (bref->type != HAMMER2_BREF_TYPE_DATA || VerboseOpt >= 1) {
331                 lseek(fd, bref->data_off & ~HAMMER2_OFF_MASK_RADIX, 0);
332                 if (read(fd, &media, bytes) != (ssize_t)bytes) {
333                         printf("(media read failed)\n");
334                         return;
335                 }
336         }
337
338         bscan = NULL;
339         bcount = 0;
340         didnl = 0;
341
342         switch(bref->type) {
343         case HAMMER2_BREF_TYPE_EMPTY:
344                 obrace = 0;
345                 break;
346         case HAMMER2_BREF_TYPE_INODE:
347                 printf("{\n");
348                 if (media.ipdata.op_flags & HAMMER2_OPFLAG_DIRECTDATA) {
349                         /* no blockrefs */
350                 } else {
351                         bscan = &media.ipdata.u.blockset.blockref[0];
352                         bcount = HAMMER2_SET_COUNT;
353                 }
354                 namelen = media.ipdata.name_len;
355                 if (namelen > HAMMER2_INODE_MAXNAME)
356                         namelen = 0;
357                 tabprintf(tab, "filename \"%*.*s\"\n",
358                           namelen, namelen, media.ipdata.filename);
359                 tabprintf(tab, "version  %d\n", media.ipdata.version);
360                 tabprintf(tab, "uflags   0x%08x\n",
361                           media.ipdata.uflags);
362                 if (media.ipdata.rmajor || media.ipdata.rminor) {
363                         tabprintf(tab, "rmajor   %d\n",
364                                   media.ipdata.rmajor);
365                         tabprintf(tab, "rminor   %d\n",
366                                   media.ipdata.rminor);
367                 }
368                 tabprintf(tab, "ctime    %s\n",
369                           hammer2_time64_to_str(media.ipdata.ctime, &str));
370                 tabprintf(tab, "mtime    %s\n",
371                           hammer2_time64_to_str(media.ipdata.mtime, &str));
372                 tabprintf(tab, "atime    %s\n",
373                           hammer2_time64_to_str(media.ipdata.atime, &str));
374                 tabprintf(tab, "btime    %s\n",
375                           hammer2_time64_to_str(media.ipdata.btime, &str));
376                 tabprintf(tab, "uid      %s\n",
377                           hammer2_uuid_to_str(&media.ipdata.uid, &str));
378                 tabprintf(tab, "gid      %s\n",
379                           hammer2_uuid_to_str(&media.ipdata.gid, &str));
380                 tabprintf(tab, "type     %s\n",
381                           hammer2_iptype_to_str(media.ipdata.type));
382                 tabprintf(tab, "opflgs   0x%02x\n",
383                           media.ipdata.op_flags);
384                 tabprintf(tab, "capflgs  0x%04x\n",
385                           media.ipdata.cap_flags);
386                 tabprintf(tab, "mode     %-7o\n",
387                           media.ipdata.mode);
388                 tabprintf(tab, "inum     0x%016jx\n",
389                           media.ipdata.inum);
390                 tabprintf(tab, "size     %ju\n",
391                           (uintmax_t)media.ipdata.size);
392                 tabprintf(tab, "nlinks   %ju\n",
393                           (uintmax_t)media.ipdata.nlinks);
394                 tabprintf(tab, "iparent  0x%016jx\n",
395                           (uintmax_t)media.ipdata.iparent);
396                 tabprintf(tab, "name_key 0x%016jx\n",
397                           (uintmax_t)media.ipdata.name_key);
398                 tabprintf(tab, "name_len %u\n",
399                           media.ipdata.name_len);
400                 tabprintf(tab, "ncopies  %u\n",
401                           media.ipdata.ncopies);
402                 tabprintf(tab, "compalg  %u\n",
403                           media.ipdata.comp_algo);
404                 if (media.ipdata.op_flags & HAMMER2_OPFLAG_PFSROOT) {
405                         tabprintf(tab, "pfs_type %u (%s)\n",
406                                   media.ipdata.pfs_type,
407                                   hammer2_pfstype_to_str(media.ipdata.pfs_type));
408                         tabprintf(tab, "pfs_inum 0x%016jx\n",
409                                   (uintmax_t)media.ipdata.pfs_inum);
410                         tabprintf(tab, "pfs_clid %s\n",
411                                   hammer2_uuid_to_str(&media.ipdata.pfs_clid,
412                                                       &str));
413                         tabprintf(tab, "pfs_fsid %s\n",
414                                   hammer2_uuid_to_str(&media.ipdata.pfs_fsid,
415                                                       &str));
416                 }
417                 tabprintf(tab, "data_quota  %ju\n",
418                           (uintmax_t)media.ipdata.data_quota);
419                 tabprintf(tab, "data_count  %ju\n",
420                           (uintmax_t)media.ipdata.data_count);
421                 tabprintf(tab, "inode_quota %ju\n",
422                           (uintmax_t)media.ipdata.inode_quota);
423                 tabprintf(tab, "inode_count %ju\n",
424                           (uintmax_t)media.ipdata.inode_count);
425                 tabprintf(tab, "attr_tid    0x%016jx\n",
426                           (uintmax_t)media.ipdata.attr_tid);
427                 if (media.ipdata.type == HAMMER2_OBJTYPE_DIRECTORY) {
428                         tabprintf(tab, "dirent_tid  %016jx\n",
429                                   (uintmax_t)media.ipdata.dirent_tid);
430                 }
431                 break;
432         case HAMMER2_BREF_TYPE_INDIRECT:
433                 bscan = &media.npdata.blockref[0];
434                 bcount = bytes / sizeof(hammer2_blockref_t);
435                 didnl = 1;
436                 printf("{\n");
437                 break;
438         case HAMMER2_BREF_TYPE_DATA:
439                 if (VerboseOpt >= 2) {
440                         printf("{\n");
441                 } else {
442                         printf("\n");
443                         obrace = 0;
444                 }
445                 break;
446         case HAMMER2_BREF_TYPE_VOLUME:
447                 bscan = &media.voldata.sroot_blockset.blockref[0];
448                 bcount = HAMMER2_SET_COUNT;
449                 printf("{\n");
450                 break;
451         default:
452                 break;
453         }
454         if (str)
455                 free(str);
456         for (i = 0; i < bcount; ++i) {
457                 if (bscan[i].type != HAMMER2_BREF_TYPE_EMPTY) {
458                         if (didnl == 0) {
459                                 printf("\n");
460                                 didnl = 1;
461                         }
462                         show_bref(fd, tab, i, &bscan[i]);
463                 }
464         }
465         tab -= SHOW_TAB;
466         if (obrace) {
467                 if (bref->type == HAMMER2_BREF_TYPE_INODE)
468                         tabprintf(tab, "} (%s.%d, \"%s\")\n",
469                                   type_str, bi, media.ipdata.filename);
470                 else
471                         tabprintf(tab, "} (%s.%d)\n", type_str,bi);
472         }
473 }
474
475 static
476 void
477 tabprintf(int tab, const char *ctl, ...)
478 {
479         va_list va;
480
481         printf("%*.*s", tab, tab, "");
482         va_start(va, ctl);
483         vprintf(ctl, va);
484         va_end(va);
485 }