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"
20 extern void sendMail (const char *messageContents);
22 void cb_console(const char *str, void *data)
24 printf("CONSOLE> %s\n",str);
27 /* Note that unlike what's documented in gdb docs it isn't usable. */
28 void cb_target(const char *str, void *data)
30 printf("TARGET> %s\n",str);
33 void cb_log(const char *str, void *data)
35 printf("LOG> %s\n",str);
38 void cb_to(const char *str, void *data)
43 void cb_from(const char *str, void *data)
45 printf("<< %s\n",str);
48 volatile int async_c=0;
50 void cb_async(mi_output *o, void *data)
57 void send_bug_mail(mi_stop* sr, mi_frames* f)
60 GNUNET_asprintf(&message,
61 "Bug detected in file:%s\nfunction:%s\nline:%d\nreason:%s\nreceived signal:%s\n%s\n",
62 f->file, f->func, f->line, mi_reason_enum_to_str(sr->reason), sr->signal_name, sr->signal_meaning);
64 GNUNET_free (message);
68 int wait_for_stop(mi_h *h)
74 while (!mi_get_response(h))
76 /* The end of the async. */
80 f = gmi_stack_info_frame(h);
87 printf("Error while waiting\n");
88 printf("mi_error: %d\nmi_error_from_gdb: %s\n",mi_error,mi_error_from_gdb);
94 int main(int argc, char *argv[])
96 mi_aux_term *xterm_tty=NULL;
97 const char* binaryName;
100 GNUNET_assert(NULL != binaryName);
102 /* This is like a file-handle for fopen.
103 Here we have all the state of gdb "connection". */
106 /* Connect to gdb child. */
107 h=mi_connect_local();
110 printf("Connect failed\n");
113 printf("Connected to gdb!\n");
115 /* Set all callbacks. */
116 mi_set_console_cb(h,cb_console,NULL);
117 mi_set_target_cb(h,cb_target,NULL);
118 mi_set_log_cb(h,cb_log,NULL);
119 mi_set_async_cb(h,cb_async,NULL);
120 mi_set_to_gdb_cb(h,cb_to,NULL);
121 mi_set_from_gdb_cb(h,cb_from,NULL);
123 /* Set the name of the child and the command line aguments. */
124 if (!gmi_set_exec(h, binaryName, NULL))
126 printf("Error setting exec y args\n");
131 /* Tell gdb to attach the child to a terminal. */
132 if (!gmi_target_terminal(h, ttyname(STDIN_FILENO)))
134 printf("Error selecting target terminal\n");
139 /* Run the program. */
140 if (!gmi_exec_run(h))
142 printf("Error in run!\n");
146 /* Here we should be stopped when the program crashes */
147 if (!wait_for_stop(h))
153 /* Continue execution. */
154 if (!gmi_exec_continue(h))
156 printf("Error in continue!\n");
160 /* Here we should be terminated. */
161 if (!wait_for_stop(h))
169 /* Close the connection. */
171 /* Wait 5 seconds and close the auxiliar terminal. */
172 printf("Waiting 5 seconds\n");
174 gmi_end_aux_term(xterm_tty);