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
16 void cb_console(const char *str, void *data)
18 printf("CONSOLE> %s\n",str);
21 /* Note that unlike what's documented in gdb docs it isn't usable. */
22 void cb_target(const char *str, void *data)
24 printf("TARGET> %s\n",str);
27 void cb_log(const char *str, void *data)
29 printf("LOG> %s\n",str);
32 void cb_to(const char *str, void *data)
37 void cb_from(const char *str, void *data)
39 printf("<< %s\n",str);
42 volatile int async_c=0;
44 void cb_async(mi_output *o, void *data)
50 int wait_for_stop(mi_h *h)
55 while (!mi_get_response(h))
57 /* The end of the async. */
61 printf("Stopped, reason: %s\n",mi_reason_enum_to_str(sr->reason));
66 printf("Error while waiting\n");
67 printf("mi_error: %d\nmi_error_from_gdb: %s\n",mi_error,mi_error_from_gdb);
73 int main(int argc, char *argv[])
75 mi_aux_term *xterm_tty=NULL;
78 /* This is like a file-handle for fopen.
79 Here we have all the state of gdb "connection". */
82 /* You can use any gdb you want: */
83 /*mi_set_gdb_exe("/usr/src/gdb-6.1.1/gdb/gdb");*/
84 /* You can use a terminal different than xterm: */
85 /*mi_set_xterm_exe("/usr/bin/Eterm");*/
87 /* Connect to gdb child. */
91 printf("Connect failed\n");
94 printf("Connected to gdb!\n");
96 /* Set all callbacks. */
97 mi_set_console_cb(h,cb_console,NULL);
98 mi_set_target_cb(h,cb_target,NULL);
99 mi_set_log_cb(h,cb_log,NULL);
100 mi_set_async_cb(h,cb_async,NULL);
101 mi_set_to_gdb_cb(h,cb_to,NULL);
102 mi_set_from_gdb_cb(h,cb_from,NULL);
104 /* Set the name of the child and the command line aguments. */
105 if (!gmi_set_exec(h,"./test_target","prb1 2 prb3"))
107 printf("Error setting exec y args\n");
112 /* Open an xterm to be used as terminal for the debuggy. */
113 xterm_tty=gmi_start_xterm();
115 printf("Error opening auxiliar terminal, we'll use current one.\n");
117 printf("XTerm opened @ %s\n",xterm_tty->tty);
119 /* Tell gdb to attach the child to a terminal. */
120 if (!gmi_target_terminal(h,xterm_tty ? xterm_tty->tty : ttyname(STDIN_FILENO)))
122 printf("Error selecting target terminal\n");
127 /* Set a breakpoint. */
128 bk=gmi_break_insert(h,"test_target.cc",12);
131 printf("Error setting breakpoint\n");
134 printf("Breakpoint %d @ function: %s\n",bk->number,bk->func);
136 /* You can do things like:
137 gmi_break_delete(h,bk->number);
138 gmi_break_set_times(h,bk->number,2);
139 gmi_break_set_condition(h,bk->number,"1");
140 gmi_break_state(h,bk->number,0);*/
141 /* If we no longer need this data we can release it. */
144 /* Set a watchpoint, that's a data breakpoint. */
145 wp=gmi_break_watch(h,wm_write,"v");
148 printf("Error al setting watchpoint\n");
151 printf("Watchpoint %d for expression: %s\n",wp->number,wp->exp);
154 /* Run the program. */
155 if (!gmi_exec_run(h))
157 printf("Error in run!\n");
161 /* Here we should be stopped at the breakpoint. */
162 if (!wait_for_stop(h))
168 /* Continue execution. */
169 if (!gmi_exec_continue(h))
171 printf("Error in continue!\n");
175 /* Here we should be terminated. */
176 if (!wait_for_stop(h))
184 /* Close the connection. */
186 /* Wait 5 seconds and close the auxiliar terminal. */
187 printf("Waiting 5 seconds\n");
189 gmi_end_aux_term(xterm_tty);