aboutsummaryrefslogtreecommitdiff
path: root/src/monkey
diff options
context:
space:
mode:
authorSafey A.Halim <safey.allah@gmail.com>2010-11-05 07:14:52 +0000
committerSafey A.Halim <safey.allah@gmail.com>2010-11-05 07:14:52 +0000
commit405cb054459748b52e557f903b313219e9ce1687 (patch)
tree744c042d27df4a213b84c956cd91f6525e2cb831 /src/monkey
parentd46a351d5fe6e106ade023eceb6cd58cff1536ae (diff)
downloadgnunet-405cb054459748b52e557f903b313219e9ce1687.tar.gz
gnunet-405cb054459748b52e557f903b313219e9ce1687.zip
Monkey parse command arguments using gnunet_getopt_lib.
Adding a man page for Monkey
Diffstat (limited to 'src/monkey')
-rw-r--r--src/monkey/gnunet-monkey.c241
1 files changed, 137 insertions, 104 deletions
diff --git a/src/monkey/gnunet-monkey.c b/src/monkey/gnunet-monkey.c
index b7feb1c51..be0a73687 100644
--- a/src/monkey/gnunet-monkey.c
+++ b/src/monkey/gnunet-monkey.c
@@ -15,10 +15,14 @@
15#include "gdbmi.h" 15#include "gdbmi.h"
16#include "platform.h" 16#include "platform.h"
17#include "gnunet_common.h" 17#include "gnunet_common.h"
18#include "gnunet_getopt_lib.h"
19#include "gnunet_program_lib.h"
18 20
19extern void sendMail (const char *messageContents); 21extern void sendMail (const char *messageContents);
20static const char* mode; 22static const char* mode;
21static const char* dumpFileName; 23static const char* dumpFileName;
24static const char* binaryName;
25static int ret = 0;
22 26
23void cb_console(const char *str, void *data) 27void cb_console(const char *str, void *data)
24{ 28{
@@ -59,7 +63,7 @@ static void dumpText(const char* message)
59{ 63{
60 FILE* file = fopen(dumpFileName, "w"); 64 FILE* file = fopen(dumpFileName, "w");
61 GNUNET_assert(NULL != file); 65 GNUNET_assert(NULL != file);
62 /* FIXME: fprintf(file, message); */ 66 fprintf(file, message);
63 fclose(file); 67 fclose(file);
64} 68}
65 69
@@ -105,111 +109,140 @@ int wait_for_stop(mi_h *h)
105 return res; 109 return res;
106} 110}
107 111
108int main(int argc, char *argv[])
109{
110 mi_aux_term *xterm_tty=NULL;
111 const char* binaryName;
112 const char* argument;
113 int i = 1;
114
115 argument = argv[i];
116 GNUNET_assert(NULL != argument);
117 do {
118 if (strcasecmp(argument, "--mode") == 0) {
119 argument = argv[++i];
120 GNUNET_assert(NULL != argument);
121 mode = argument;
122 }
123 else if (strcasecmp(argument, "--binary") == 0) {
124 argument = argv[++i];
125 GNUNET_assert(NULL != argument);
126 binaryName = argument;
127 }
128 else if (strcasecmp(argument, "--output") == 0) {
129 argument = argv[++i];
130 GNUNET_assert(NULL != argument);
131 dumpFileName = argument;
132 }
133 else
134 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Monkey: Error: Unexpected argument\n"));
135 } while (NULL != (argument = argv[++i]));
136 GNUNET_assert((NULL != binaryName) || (NULL != mode));
137 GNUNET_assert(!((strcasecmp(mode, "text") == 0) && (NULL == dumpFileName)));
138
139
140 /* This is like a file-handle for fopen.
141 Here we have all the state of gdb "connection". */
142 mi_h *h;
143
144 /* Connect to gdb child. */
145 h=mi_connect_local();
146 if (!h)
147 {
148 printf("Connect failed\n");
149 return 1;
150 }
151 printf("Connected to gdb!\n");
152
153 /* Set all callbacks. */
154 mi_set_console_cb(h,cb_console,NULL);
155 mi_set_target_cb(h,cb_target,NULL);
156 mi_set_log_cb(h,cb_log,NULL);
157 mi_set_async_cb(h,cb_async,NULL);
158 mi_set_to_gdb_cb(h,cb_to,NULL);
159 mi_set_from_gdb_cb(h,cb_from,NULL);
160
161 /* Set the name of the child and the command line aguments. */
162 if (!gmi_set_exec(h, binaryName, NULL))
163 {
164 printf("Error setting exec y args\n");
165 mi_disconnect(h);
166 return 1;
167 }
168 112
169 /* Tell gdb to attach the child to a terminal. */
170 if (!gmi_target_terminal(h, ttyname(STDIN_FILENO)))
171 {
172 printf("Error selecting target terminal\n");
173 mi_disconnect(h);
174 return 1;
175 }
176
177 /* Run the program. */
178 if (!gmi_exec_run(h))
179 {
180 printf("Error in run!\n");
181 mi_disconnect(h);
182 return 1;
183 }
184 /* Here we should be stopped when the program crashes */
185 if (!wait_for_stop(h))
186 {
187 mi_disconnect(h);
188 return 1;
189 }
190 113
191 /* Continue execution. */ 114/**
192 if (!gmi_exec_continue(h)) 115 * Main function that will be run by the scheduler.
193 { 116 *
194 printf("Error in continue!\n"); 117 * @param cls closure
195 mi_disconnect(h); 118 * @param s the scheduler to use
196 return 1; 119 * @param args remaining command-line arguments
197 } 120 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
198 /* Here we should be terminated. */ 121 * @param c configuration
199 if (!wait_for_stop(h)) 122 */
200 { 123static void
201 mi_disconnect(h); 124run (void *cls,
202 return 1; 125 struct GNUNET_SCHEDULER_Handle *s,
203 } 126 char *const *args,
127 const char *cfgfile,
128 const struct GNUNET_CONFIGURATION_Handle *c)
129{
130 mi_aux_term *xterm_tty=NULL;
131
132 /* This is like a file-handle for fopen.
133 Here we have all the state of gdb "connection". */
134 mi_h *h;
135
136 /* Connect to gdb child. */
137 h=mi_connect_local();
138 if (!h)
139 {
140 printf("Connect failed\n");
141 ret = 1;
142 return;
143 }
144 printf("Connected to gdb!\n");
145
146 /* Set all callbacks. */
147 mi_set_console_cb(h,cb_console,NULL);
148 mi_set_target_cb(h,cb_target,NULL);
149 mi_set_log_cb(h,cb_log,NULL);
150 mi_set_async_cb(h,cb_async,NULL);
151 mi_set_to_gdb_cb(h,cb_to,NULL);
152 mi_set_from_gdb_cb(h,cb_from,NULL);
153
154 /* Set the name of the child and the command line aguments. */
155 if (!gmi_set_exec(h, binaryName, NULL))
156 {
157 printf("Error setting exec y args\n");
158 mi_disconnect(h);
159 ret = 1;
160 return;
161 }
162
163 /* Tell gdb to attach the child to a terminal. */
164 if (!gmi_target_terminal(h, ttyname(STDIN_FILENO)))
165 {
166 printf("Error selecting target terminal\n");
167 mi_disconnect(h);
168 ret = 1;
169 return;
170 }
171
172 /* Run the program. */
173 if (!gmi_exec_run(h))
174 {
175 printf("Error in run!\n");
176 mi_disconnect(h);
177 ret = 1;
178 return;
179 }
180 /* Here we should be stopped when the program crashes */
181 if (!wait_for_stop(h))
182 {
183 mi_disconnect(h);
184 ret = 1;
185 return;
186 }
187
188 /* Continue execution. */
189 if (!gmi_exec_continue(h))
190 {
191 printf("Error in continue!\n");
192 mi_disconnect(h);
193 ret = 1;
194 return;
195 }
196 /* Here we should be terminated. */
197 if (!wait_for_stop(h))
198 {
199 mi_disconnect(h);
200 ret = 1;
201 return;
202 }
203
204 /* Exit from gdb. */
205 gmi_gdb_exit(h);
206 /* Close the connection. */
207 mi_disconnect(h);
208 /* Wait 5 seconds and close the auxiliar terminal. */
209 printf("Waiting 5 seconds\n");
210 sleep(5);
211 gmi_end_aux_term(xterm_tty);
212}
204 213
205 /* Exit from gdb. */
206 gmi_gdb_exit(h);
207 /* Close the connection. */
208 mi_disconnect(h);
209 /* Wait 5 seconds and close the auxiliar terminal. */
210 printf("Waiting 5 seconds\n");
211 sleep(5);
212 gmi_end_aux_term(xterm_tty);
213 214
214 return 0; 215int main(int argc, char *argv[])
216{
217 /*
218 * FIXME:
219 * Command should accept email address to which monkey sends the debugging report.
220 * The email address can also be read from the configuration file.
221 */
222 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
223 {'m', "mode", NULL, gettext_noop ("monkey's mode of operation: options are \"text\" or \"email\""),
224 GNUNET_YES, &GNUNET_GETOPT_set_string, &mode},
225 {'b', "binary", NULL, gettext_noop ("binary for program to debug with monkey"),
226 GNUNET_YES, &GNUNET_GETOPT_set_string, &binaryName},
227 {'o', "output", NULL, gettext_noop ("path to file to dump monkey's output in case of working in text mode"),
228 GNUNET_YES, &GNUNET_GETOPT_set_string, &dumpFileName},
229 GNUNET_GETOPT_OPTION_END
230 };
231
232 if (argc < 2) {
233 printf("%s", "Monkey should take arguments: Use --help to get a list of options.\n");
234 return 1;
235 }
236
237 if (GNUNET_OK == GNUNET_PROGRAM_run (argc,
238 argv,
239 "gnunet-monkey",
240 gettext_noop
241 ("Automatically debug a service"),
242 options, &run, NULL))
243 {
244 return ret;
245 }
246
247 return 1;
215} 248}