Whamcloud - gitweb
debugfs: create inode_dump command to dump an inode in hex
authorDarrick J. Wong <darrick.wong@oracle.com>
Tue, 22 Jul 2014 16:44:42 +0000 (12:44 -0400)
committerTheodore Ts'o <tytso@mit.edu>
Tue, 22 Jul 2014 17:48:54 +0000 (13:48 -0400)
Create a command that will dump an entire inode's space in hex.

[ Modified by tytso to add a description to the man page, and to add
  the more formal command name, inode_dump, in addition to short
  command name of "idump". ]

Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
debugfs/debug_cmds.ct
debugfs/debugfs.8.in
debugfs/debugfs.c
debugfs/debugfs.h
debugfs/zap.c

index 6a18df6..ed3728f 100644 (file)
@@ -188,7 +188,7 @@ request do_zap_block, "Zap block: fill with 0, pattern, flip bits etc.",
        zap_block, zap;
 
 request do_block_dump, "Dump contents of a block",
-       block_dump, bd;
+       block_dump, bdump, bd;
 
 request do_list_quota, "List quota",
        list_quota, lq;
@@ -196,5 +196,9 @@ request do_list_quota, "List quota",
 request do_get_quota, "Get quota",
        get_quota, gq;
 
+request do_idump, "Dump the inode structure in hex",
+       inode_dump, idump, id;
+
+
 end;
 
index 73254d3..9a125f6 100644 (file)
@@ -346,6 +346,9 @@ showing its tree structure.
 Print a listing of the inodes which use the one or more blocks specified
 on the command line.
 .TP
+.BI inode_dump " filespec"
+Print the contents of the inode data structure in hex and ASCII format.
+.TP
 .BI imap " filespec"
 Print the location of the inode data structure (in the inode table)
 of the inode
index 1703aba..b41626c 100644 (file)
@@ -2145,6 +2145,39 @@ void do_imap(int argc, char *argv[])
 
 }
 
+void do_idump(int argc, char *argv[])
+{
+       ext2_ino_t      ino;
+       char            *buf;
+       errcode_t       err;
+       int             isize;
+
+       if (common_args_process(argc, argv, 2, 2, argv[0],
+                               "<file>", 0))
+               return;
+       ino = string_to_inode(argv[1]);
+       if (!ino)
+               return;
+
+       isize = EXT2_INODE_SIZE(current_fs->super);
+       err = ext2fs_get_mem(isize, &buf);
+       if (err) {
+               com_err(argv[0], err, "while allocating memory");
+               return;
+       }
+
+       err = ext2fs_read_inode_full(current_fs, ino,
+                                    (struct ext2_inode *)buf, isize);
+       if (err) {
+               com_err(argv[0], err, "while reading inode %d", ino);
+               goto err;
+       }
+
+       do_byte_hexdump(stdout, buf, isize);
+err:
+       ext2fs_free_mem(&buf);
+}
+
 #ifndef READ_ONLY
 void do_set_current_time(int argc, char *argv[])
 {
index 9b67f69..0e75cee 100644 (file)
@@ -183,3 +183,4 @@ extern time_t string_to_time(const char *arg);
 /* zap.c */
 extern void do_zap_block(int argc, char **argv);
 extern void do_block_dump(int argc, char **argv);
+extern void do_byte_hexdump(FILE *fp, unsigned char *buf, size_t bufsize);
index 8109209..917bddf 100644 (file)
@@ -176,7 +176,6 @@ void do_block_dump(int argc, char *argv[])
        char            *file = NULL;
        unsigned int    i, j;
        int             c, err;
-       int             suppress = -1;
 
        if (check_fs_open(argv[0]))
                return;
@@ -229,11 +228,21 @@ void do_block_dump(int argc, char *argv[])
                goto errout;
        }
 
-       for (i=0; i < current_fs->blocksize; i += 16) {
+       do_byte_hexdump(stdout, buf, current_fs->blocksize);
+errout:
+       free(buf);
+}
+
+void do_byte_hexdump(FILE *fp, unsigned char *buf, size_t bufsize)
+{
+       size_t          i, j;
+       int             suppress = -1;
+
+       for (i = 0; i < bufsize; i += 16) {
                if (suppress < 0) {
                        if (i && memcmp(buf + i, buf + i - 16, 16) == 0) {
                                suppress = i;
-                               printf("*\n");
+                               fprintf(fp, "*\n");
                                continue;
                        }
                } else {
@@ -241,20 +250,16 @@ void do_block_dump(int argc, char *argv[])
                                continue;
                        suppress = -1;
                }
-               printf("%04o  ", i);
+               fprintf(fp, "%04o  ", (unsigned int)i);
                for (j = 0; j < 16; j++) {
-                       printf("%02x", buf[i+j]);
+                       fprintf(fp, "%02x", buf[i+j]);
                        if ((j % 2) == 1)
-                               putchar(' ');
+                               fprintf(fp, " ");
                }
-               putchar(' ');
+               fprintf(fp, " ");
                for (j = 0; j < 16; j++)
-                       printf("%c", isprint(buf[i+j]) ? buf[i+j] : '.');
-               putchar('\n');
+                       fprintf(fp, "%c", isprint(buf[i+j]) ? buf[i+j] : '.');
+               fprintf(fp, "\n");
        }
-       putchar('\n');
-
-errout:
-       free(buf);
-       return;
+       fprintf(fp, "\n");
 }