Add outline of 'debug' command.
[libguestfs.git] / daemon / debug.c
1 /* libguestfs - the guestfsd daemon
2  * Copyright (C) 2009 Red Hat Inc. 
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25
26 #include "../src/guestfs_protocol.h"
27 #include "daemon.h"
28 #include "actions.h"
29
30 /* This command exposes debugging information, internals and
31  * status.  There is no comprehensive documentation for this
32  * command.  You have to look at the source code in this file
33  * to find out what you can do.
34  *
35  * Commands always output a freeform string.
36  */
37
38 #if ENABLE_DEBUG_COMMAND
39 struct cmd {
40   const char *cmd;
41   char * (*f) (const char *subcmd, int argc, char *const *const argv);
42 };
43
44 static char *debug_help (const char *subcmd, int argc, char *const *const argv);
45 #if 0
46 static char *debug_fds (const char *subcmd, int argc, char *const *const argv);
47 static char *debug_free (const char *subcmd, int argc, char *const *const argv);
48 static char *debug_mem (const char *subcmd, int argc, char *const *const argv);
49 static char *debug_ps (const char *subcmd, int argc, char *const *const argv);
50 #endif
51
52 static struct cmd cmds[] = {
53   { "help", debug_help },
54 #if 0
55   { "fds", debug_fds },
56   { "free", debug_free },
57   { "mem", debug_mem },
58   { "ps", debug_free },
59 #endif
60   { NULL, NULL }
61 };
62 #endif
63
64 char *
65 do_debug (const char *subcmd, char *const *const argv)
66 {
67 #if ENABLE_DEBUG_COMMAND
68   int argc, i;
69
70   for (i = argc = 0; argv[i] != NULL; ++i)
71     argc++;
72
73   for (i = 0; cmds[i].cmd != NULL; ++i) {
74     if (strcasecmp (subcmd, cmds[i].cmd) == 0)
75       return cmds[i].f (subcmd, argc, argv);
76   }
77
78   reply_with_error ("use 'debug help' to list the supported commands");
79   return NULL;
80 #else
81   reply_with_error ("guestfsd was not configured with --enable-debug-command");
82   return NULL;
83 #endif
84 }
85
86 #if ENABLE_DEBUG_COMMAND
87 static char *
88 debug_help (const char *subcmd, int argc, char *const *const argv)
89 {
90   int len, i;
91   char *r, *p;
92
93   r = strdup ("Commands supported:");
94   if (!r) {
95     reply_with_perror ("strdup");
96     return NULL;
97   }
98
99   len = strlen (r);
100   for (i = 0; cmds[i].cmd != NULL; ++i) {
101     len += strlen (cmds[i].cmd) + 1; /* space + new command */
102     p = realloc (r, len + 1);        /* +1 for the final NUL */
103     if (p == NULL) {
104       reply_with_perror ("realloc");
105       free (r);
106       return NULL;
107     }
108     r = p;
109
110     strcat (r, " ");
111     strcat (r, cmds[i].cmd);
112   }
113
114   return r;
115 }
116
117 #if 0
118 static char *
119 debug_fds (const char *subcmd, int argc, char *const *const argv)
120 {
121 }
122
123 static char *
124 debug_free (const char *subcmd, int argc, char *const *const argv)
125 {
126 }
127
128 static char *
129 debug_mem (const char *subcmd, int argc, char *const *const argv)
130 {
131 }
132
133 static char *
134 debug_ps (const char *subcmd, int argc, char *const *const argv)
135 {
136 }
137 #endif
138 #endif /* ENABLE_DEBUG_COMMAND */