1 /**[txh]********************************************************************
3 Copyright (c) 2004 by Salvador E. Tropea.
4 Covered by the GPL license.
7 Linux console example/test of the libmigdb.
8 Run it from a Linux console.
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");
72 int main(int argc, char *argv[])
74 mi_aux_term *child_vt=NULL;
77 /* This is like a file-handle for fopen.
78 Here we have all the state of gdb "connection". */
81 /* You can use any gdb you want: */
82 /*mi_set_gdb_exe("/usr/src/gdb-6.1.1/gdb/gdb");*/
83 /* You can use a terminal different than xterm: */
84 /*mi_set_xterm_exe("/usr/bin/Eterm");*/
86 /* Connect to gdb child. */
90 printf("Connect failed\n");
93 printf("Connected to gdb!\n");
95 /* Set all callbacks. */
96 mi_set_console_cb(h,cb_console,NULL);
97 mi_set_target_cb(h,cb_target,NULL);
98 mi_set_log_cb(h,cb_log,NULL);
99 mi_set_async_cb(h,cb_async,NULL);
100 mi_set_to_gdb_cb(h,cb_to,NULL);
101 mi_set_from_gdb_cb(h,cb_from,NULL);
103 /* Look for a free VT where we can run the child. */
104 child_vt=gmi_look_for_free_vt();
106 printf("Error opening auxiliar terminal, we'll use current one.\n");
109 printf("Free VT @ %s\n",child_vt->tty);
110 printf("\n\n***************************************\n");
111 printf("Switch to the above mentioned terminal!\n");
112 printf("***************************************\n\n\n");
115 /* Tell gdb to attach the child to a terminal. */
116 if (!gmi_target_terminal(h,child_vt ? child_vt->tty : ttyname(STDIN_FILENO)))
118 printf("Error selecting target terminal\n");
123 /* Set the name of the child and the command line aguments. */
124 if (!gmi_set_exec(h,"./test_target","prb1 2 prb3"))
126 printf("Error setting exec y args\n");
131 /* Set a breakpoint. */
132 bk=gmi_break_insert(h,"test_target.cc",12);
135 printf("Error setting breakpoint\n");
139 printf("Breakpoint %d @ function: %s\n",bk->number,bk->func);
141 /* You can do things like:
142 gmi_break_delete(h,bk->number);
143 gmi_break_set_times(h,bk->number,2);
144 gmi_break_set_condition(h,bk->number,"1");
145 gmi_break_state(h,bk->number,0);*/
146 /* If we no longer need this data we can release it. */
149 /* Set a watchpoint, that's a data breakpoint. */
150 wp=gmi_break_watch(h,wm_write,"v");
153 printf("Error al setting watchpoint\n");
157 printf("Watchpoint %d for expression: %s\n",wp->number,wp->exp);
160 /* Run the program. */
161 if (!gmi_exec_run(h))
163 printf("Error in run!\n");
167 /* Here we should be stopped at the breakpoint. */
168 if (!wait_for_stop(h))
174 /* Continue execution. */
175 if (!gmi_exec_continue(h))
177 printf("Error in continue!\n");
181 /* Here we should be terminated. */
182 if (!wait_for_stop(h))
190 /* Close the connection. */
192 gmi_end_aux_term(child_vt);