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"
19 extern void sendMail (const char *messageContents);
20 static const char* mode;
21 static const char* dumpFileName;
23 void cb_console(const char *str, void *data)
25 printf("CONSOLE> %s\n",str);
28 /* Note that unlike what's documented in gdb docs it isn't usable. */
29 void cb_target(const char *str, void *data)
31 printf("TARGET> %s\n",str);
34 void cb_log(const char *str, void *data)
36 printf("LOG> %s\n",str);
39 void cb_to(const char *str, void *data)
44 void cb_from(const char *str, void *data)
46 printf("<< %s\n",str);
51 void cb_async(mi_output *o, void *data)
58 static void dumpText(const char* message)
60 FILE* file = fopen(dumpFileName, "w");
61 GNUNET_assert(NULL != file);
62 /* FIXME: fprintf(file, message); */
67 void send_bug_mail(mi_stop* sr, mi_frames* f)
70 GNUNET_asprintf(&message,
71 "Bug detected in file:%s\nfunction:%s\nline:%d\nreason:%s\nreceived signal:%s\n%s\n",
72 f->file, f->func, f->line, mi_reason_enum_to_str(sr->reason), sr->signal_name, sr->signal_meaning);
73 if (strcasecmp(mode, "mail") == 0)
78 GNUNET_free (message);
82 int wait_for_stop(mi_h *h)
88 while (!mi_get_response(h))
90 /* The end of the async. */
94 f = gmi_stack_info_frame(h);
101 printf("Error while waiting\n");
102 printf("mi_error: %d\nmi_error_from_gdb: %s\n",mi_error,mi_error_from_gdb);
108 int main(int argc, char *argv[])
110 mi_aux_term *xterm_tty=NULL;
111 const char* binaryName;
112 const char* argument;
116 GNUNET_assert(NULL != argument);
118 if (strcasecmp(argument, "--mode") == 0) {
119 argument = argv[++i];
120 GNUNET_assert(NULL != argument);
123 else if (strcasecmp(argument, "--binary") == 0) {
124 argument = argv[++i];
125 GNUNET_assert(NULL != argument);
126 binaryName = argument;
128 else if (strcasecmp(argument, "--output") == 0) {
129 argument = argv[++i];
130 GNUNET_assert(NULL != argument);
131 dumpFileName = argument;
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)));
140 /* This is like a file-handle for fopen.
141 Here we have all the state of gdb "connection". */
144 /* Connect to gdb child. */
145 h=mi_connect_local();
148 printf("Connect failed\n");
151 printf("Connected to gdb!\n");
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);
161 /* Set the name of the child and the command line aguments. */
162 if (!gmi_set_exec(h, binaryName, NULL))
164 printf("Error setting exec y args\n");
169 /* Tell gdb to attach the child to a terminal. */
170 if (!gmi_target_terminal(h, ttyname(STDIN_FILENO)))
172 printf("Error selecting target terminal\n");
177 /* Run the program. */
178 if (!gmi_exec_run(h))
180 printf("Error in run!\n");
184 /* Here we should be stopped when the program crashes */
185 if (!wait_for_stop(h))
191 /* Continue execution. */
192 if (!gmi_exec_continue(h))
194 printf("Error in continue!\n");
198 /* Here we should be terminated. */
199 if (!wait_for_stop(h))
207 /* Close the connection. */
209 /* Wait 5 seconds and close the auxiliar terminal. */
210 printf("Waiting 5 seconds\n");
212 gmi_end_aux_term(xterm_tty);