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 void cb_console(const char *str, void *data)
19 printf("CONSOLE> %s\n",str);
22 /* Note that unlike what's documented in gdb docs it isn't usable. */
23 void cb_target(const char *str, void *data)
25 printf("TARGET> %s\n",str);
28 void cb_log(const char *str, void *data)
30 printf("LOG> %s\n",str);
33 void cb_to(const char *str, void *data)
38 void cb_from(const char *str, void *data)
40 printf("<< %s\n",str);
43 volatile int async_c=0;
45 void cb_async(mi_output *o, void *data)
51 int wait_for_stop(mi_h *h)
57 while (!mi_get_response(h))
59 /* The end of the async. */
63 printf("Stopped, reason: %s\n",mi_reason_enum_to_str(sr->reason));
64 printf("Received signal name: %s\n", sr->signal_name);
65 printf("Received signal meaning: %s\n", sr->signal_meaning);
66 //printf("In file: %s\n", sr->frame->file);
67 //printf("Line Number: %d\n", sr->frame->line);
68 f = gmi_stack_info_frame(h);
73 printf("Error while waiting\n");
74 printf("mi_error: %d\nmi_error_from_gdb: %s\n",mi_error,mi_error_from_gdb);
80 int main(int argc, char *argv[])
82 mi_aux_term *xterm_tty=NULL;
84 /* This is like a file-handle for fopen.
85 Here we have all the state of gdb "connection". */
88 /* Connect to gdb child. */
92 printf("Connect failed\n");
95 printf("Connected to gdb!\n");
97 /* Set all callbacks. */
98 mi_set_console_cb(h,cb_console,NULL);
99 mi_set_target_cb(h,cb_target,NULL);
100 mi_set_log_cb(h,cb_log,NULL);
101 mi_set_async_cb(h,cb_async,NULL);
102 mi_set_to_gdb_cb(h,cb_to,NULL);
103 mi_set_from_gdb_cb(h,cb_from,NULL);
105 /* Set the name of the child and the command line aguments. */
106 if (!gmi_set_exec(h,"bug_null_pointer_exception", NULL))
108 printf("Error setting exec y args\n");
113 /* Tell gdb to attach the child to a terminal. */
114 if (!gmi_target_terminal(h, ttyname(STDIN_FILENO)))
116 printf("Error selecting target terminal\n");
121 /* Run the program. */
122 if (!gmi_exec_run(h))
124 printf("Error in run!\n");
128 /* Here we should be stopped when the program crashes */
129 if (!wait_for_stop(h))
135 /* Continue execution. */
136 if (!gmi_exec_continue(h))
138 printf("Error in continue!\n");
142 /* Here we should be terminated. */
143 if (!wait_for_stop(h))
151 /* Close the connection. */
153 /* Wait 5 seconds and close the auxiliar terminal. */
154 printf("Waiting 5 seconds\n");
156 gmi_end_aux_term(xterm_tty);