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"
20 #include "gnunet_monkey_action.h"
22 extern void sendMail (const char *messageContents);
23 static const char* mode;
24 static const char* dumpFileName;
25 static const char* binaryName;
28 void cb_console(const char *str, void *data)
30 printf("CONSOLE> %s\n",str);
33 /* Note that unlike what's documented in gdb docs it isn't usable. */
34 void cb_target(const char *str, void *data)
36 printf("TARGET> %s\n",str);
39 void cb_log(const char *str, void *data)
41 printf("LOG> %s\n",str);
44 void cb_to(const char *str, void *data)
49 void cb_from(const char *str, void *data)
51 printf("<< %s\n",str);
56 void cb_async(mi_output *o, void *data)
63 static void dumpText(const char* message)
65 FILE* file = fopen(dumpFileName, "w");
66 GNUNET_assert(NULL != file);
67 fprintf(file,"%s", message);
72 void send_bug_mail(mi_stop* sr, mi_frames* f)
75 GNUNET_asprintf(&message,
76 "Bug detected in file:%s\nfunction:%s\nline:%d\nreason:%s\nreceived signal:%s\n%s\n",
77 f->file, f->func, f->line, mi_reason_enum_to_str(sr->reason), sr->signal_name, sr->signal_meaning);
78 if (strcasecmp(mode, "mail") == 0)
83 GNUNET_free (message);
87 int wait_for_stop(mi_h *h)
93 while (!mi_get_response(h))
95 /* The end of the async. */
99 f = gmi_stack_info_frame(h);
101 send_bug_mail(sr, f);
109 printf("Error while waiting\n");
110 printf("mi_error: %d\nmi_error_from_gdb: %s\n",mi_error,mi_error_from_gdb);
119 * Main function that will be run by the scheduler.
122 * @param args remaining command-line arguments
123 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
124 * @param c configuration
130 const struct GNUNET_CONFIGURATION_Handle *c)
132 struct GNUNET_MONKEY_ACTION_Context* cntxt =
133 GNUNET_malloc(sizeof(struct GNUNET_MONKEY_ACTION_Context));
134 cntxt->binary_name = binaryName;
135 if (GNUNET_OK == GNUNET_MONKEY_ACTION_rerun_with_gdb(cntxt)) {
136 GNUNET_MONKEY_ACTION_format_report(cntxt);
137 GNUNET_MONKEY_ACTION_report_file(cntxt, dumpFileName);
140 // mi_aux_term *xterm_tty=NULL;
142 // /* This is like a file-handle for fopen.
143 // Here we have all the state of gdb "connection". */
146 // /* Connect to gdb child. */
147 // h=mi_connect_local();
150 // printf("Connect failed\n");
154 // printf("Connected to gdb!\n");
156 // /* Set all callbacks. */
157 // mi_set_console_cb(h,cb_console,NULL);
158 // mi_set_target_cb(h,cb_target,NULL);
159 // mi_set_log_cb(h,cb_log,NULL);
160 // mi_set_async_cb(h,cb_async,NULL);
161 // mi_set_to_gdb_cb(h,cb_to,NULL);
162 // mi_set_from_gdb_cb(h,cb_from,NULL);
164 // /* Set the name of the child and the command line aguments. */
165 // if (!gmi_set_exec(h, binaryName, NULL))
167 // printf("Error setting exec y args\n");
173 // /* Tell gdb to attach the child to a terminal. */
174 // if (!gmi_target_terminal(h, ttyname(STDIN_FILENO)))
176 // printf("Error selecting target terminal\n");
182 // /* Run the program. */
183 // if (!gmi_exec_run(h))
185 // printf("Error in run!\n");
190 // /* Here we should be stopped when the program crashes */
191 // if (!wait_for_stop(h))
198 // /* Continue execution. */
199 // if (!gmi_exec_continue(h))
201 // printf("Error in continue!\n");
206 // /* Here we should be terminated. */
207 // if (!wait_for_stop(h))
214 // /* Exit from gdb. */
216 // /* Close the connection. */
218 // /* Wait 5 seconds and close the auxiliar terminal. */
219 // printf("Waiting 5 seconds\n");
221 // gmi_end_aux_term(xterm_tty);
225 int main(int argc, char *argv[])
229 * Command should accept email address to which monkey sends the debugging report.
230 * The email address can also be read from the configuration file.
232 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
233 {'m', "mode", NULL, gettext_noop ("monkey's mode of operation: options are \"text\" or \"email\""),
234 GNUNET_YES, &GNUNET_GETOPT_set_string, &mode},
235 {'b', "binary", NULL, gettext_noop ("binary for program to debug with monkey"),
236 GNUNET_YES, &GNUNET_GETOPT_set_string, &binaryName},
237 {'o', "output", NULL, gettext_noop ("path to file to dump monkey's output in case of working in text mode"),
238 GNUNET_YES, &GNUNET_GETOPT_set_string, &dumpFileName},
239 GNUNET_GETOPT_OPTION_END
243 printf("%s", "Monkey should take arguments: Use --help to get a list of options.\n");
247 if (GNUNET_OK == GNUNET_PROGRAM_run (argc,
251 ("Automatically debug a service"),
252 options, &run, NULL))