1 /**[txh]********************************************************************
3 Copyright (c) 2004 by Salvador E. Tropea.
4 Covered by the GPL license.
7 X11 example/test of the libmigdb.
8 Run it from an X11 terminal (xterm, Eterm, etc.).
10 ***************************************************************************/
13 #include <unistd.h> //usleep
17 #include "gnunet_common.h"
18 #include "gnunet_getopt_lib.h"
19 #include "gnunet_program_lib.h"
21 extern void sendMail (const char *messageContents);
22 static const char* mode;
23 static const char* dumpFileName;
24 static const char* binaryName;
27 void cb_console(const char *str, void *data)
29 printf("CONSOLE> %s\n",str);
32 /* Note that unlike what's documented in gdb docs it isn't usable. */
33 void cb_target(const char *str, void *data)
35 printf("TARGET> %s\n",str);
38 void cb_log(const char *str, void *data)
40 printf("LOG> %s\n",str);
43 void cb_to(const char *str, void *data)
48 void cb_from(const char *str, void *data)
50 printf("<< %s\n",str);
55 void cb_async(mi_output *o, void *data)
62 static void dumpText(const char* message)
64 FILE* file = fopen(dumpFileName, "w");
65 GNUNET_assert(NULL != file);
66 fprintf(file, message);
71 void send_bug_mail(mi_stop* sr, mi_frames* f)
74 GNUNET_asprintf(&message,
75 "Bug detected in file:%s\nfunction:%s\nline:%d\nreason:%s\nreceived signal:%s\n%s\n",
76 f->file, f->func, f->line, mi_reason_enum_to_str(sr->reason), sr->signal_name, sr->signal_meaning);
77 if (strcasecmp(mode, "mail") == 0)
82 GNUNET_free (message);
86 int wait_for_stop(mi_h *h)
92 while (!mi_get_response(h))
94 /* The end of the async. */
98 f = gmi_stack_info_frame(h);
105 printf("Error while waiting\n");
106 printf("mi_error: %d\nmi_error_from_gdb: %s\n",mi_error,mi_error_from_gdb);
115 * Main function that will be run by the scheduler.
118 * @param s the scheduler to use
119 * @param args remaining command-line arguments
120 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
121 * @param c configuration
125 struct GNUNET_SCHEDULER_Handle *s,
128 const struct GNUNET_CONFIGURATION_Handle *c)
130 mi_aux_term *xterm_tty=NULL;
132 /* This is like a file-handle for fopen.
133 Here we have all the state of gdb "connection". */
136 /* Connect to gdb child. */
137 h=mi_connect_local();
140 printf("Connect failed\n");
144 printf("Connected to gdb!\n");
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);
154 /* Set the name of the child and the command line aguments. */
155 if (!gmi_set_exec(h, binaryName, NULL))
157 printf("Error setting exec y args\n");
163 /* Tell gdb to attach the child to a terminal. */
164 if (!gmi_target_terminal(h, ttyname(STDIN_FILENO)))
166 printf("Error selecting target terminal\n");
172 /* Run the program. */
173 if (!gmi_exec_run(h))
175 printf("Error in run!\n");
180 /* Here we should be stopped when the program crashes */
181 if (!wait_for_stop(h))
188 /* Continue execution. */
189 if (!gmi_exec_continue(h))
191 printf("Error in continue!\n");
196 /* Here we should be terminated. */
197 if (!wait_for_stop(h))
206 /* Close the connection. */
208 /* Wait 5 seconds and close the auxiliar terminal. */
209 printf("Waiting 5 seconds\n");
211 gmi_end_aux_term(xterm_tty);
215 int main(int argc, char *argv[])
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.
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
233 printf("%s", "Monkey should take arguments: Use --help to get a list of options.\n");
237 if (GNUNET_OK == GNUNET_PROGRAM_run (argc,
241 ("Automatically debug a service"),
242 options, &run, NULL))