cvs -z3 -d:pserver:anonymous@libmigdb.cvs.sourceforge.net:/cvsroot/libmigdb co -P...
[gdbmicli.git] / libmigdb / examples / x11_test.c
1 /**[txh]********************************************************************
2
3   Copyright (c) 2004 by Salvador E. Tropea.
4   Covered by the GPL license.
5
6   Comment:
7   X11 example/test of the libmigdb.
8   Run it from an X11 terminal (xterm, Eterm, etc.).
9   
10 ***************************************************************************/
11
12 #include <stdio.h>
13 #include <unistd.h> //usleep
14 #include "mi_gdb.h"
15
16 void cb_console(const char *str, void *data)
17 {
18  printf("CONSOLE> %s\n",str);
19 }
20
21 /* Note that unlike what's documented in gdb docs it isn't usable. */
22 void cb_target(const char *str, void *data)
23 {
24  printf("TARGET> %s\n",str);
25 }
26
27 void cb_log(const char *str, void *data)
28 {
29  printf("LOG> %s\n",str);
30 }
31
32 void cb_to(const char *str, void *data)
33 {
34  printf(">> %s",str);
35 }
36
37 void cb_from(const char *str, void *data)
38 {
39  printf("<< %s\n",str);
40 }
41
42 volatile int async_c=0;
43
44 void cb_async(mi_output *o, void *data)
45 {
46  printf("ASYNC\n");
47  async_c++;
48 }
49
50 int wait_for_stop(mi_h *h)
51 {
52  int res=1;
53  mi_stop *sr;
54
55  while (!mi_get_response(h))
56     usleep(1000);
57  /* The end of the async. */
58  sr=mi_res_stop(h);
59  if (sr)
60    {
61     printf("Stopped, reason: %s\n",mi_reason_enum_to_str(sr->reason));
62     mi_free_stop(sr);
63    }
64  else
65    {
66     printf("Error while waiting\n");
67     printf("mi_error: %d\nmi_error_from_gdb: %s\n",mi_error,mi_error_from_gdb);
68     res=0;
69    }
70  return res;
71 }
72
73 int main(int argc, char *argv[])
74 {
75  mi_aux_term *xterm_tty=NULL;
76  mi_bkpt *bk;
77  mi_wp *wp;
78  /* This is like a file-handle for fopen.
79     Here we have all the state of gdb "connection". */
80  mi_h *h;
81
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");*/
86
87  /* Connect to gdb child. */
88  h=mi_connect_local();
89  if (!h)
90    {
91     printf("Connect failed\n");
92     return 1;
93    }
94  printf("Connected to gdb!\n");
95
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);
103
104  /* Set the name of the child and the command line aguments. */
105  if (!gmi_set_exec(h,"./test_target","prb1 2 prb3"))
106    {
107     printf("Error setting exec y args\n");
108     mi_disconnect(h);
109     return 1;
110    }
111
112  /* Open an xterm to be used as terminal for the debuggy. */
113  xterm_tty=gmi_start_xterm();
114  if (!xterm_tty)
115     printf("Error opening auxiliar terminal, we'll use current one.\n");
116  else
117     printf("XTerm opened @ %s\n",xterm_tty->tty);
118
119  /* Tell gdb to attach the child to a terminal. */
120  if (!gmi_target_terminal(h,xterm_tty ? xterm_tty->tty : ttyname(STDIN_FILENO)))
121    {
122     printf("Error selecting target terminal\n");
123     mi_disconnect(h);
124     return 1;
125    }
126
127  /* Set a breakpoint. */
128  bk=gmi_break_insert(h,"test_target.cc",12);
129  if (!bk)
130    {
131     printf("Error setting breakpoint\n");
132     return 1;
133    }
134  printf("Breakpoint %d @ function: %s\n",bk->number,bk->func);
135
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. */
142  mi_free_bkpt(bk);
143
144  /* Set a watchpoint, that's a data breakpoint. */
145  wp=gmi_break_watch(h,wm_write,"v");
146  if (!wp)
147    {
148     printf("Error al setting watchpoint\n");
149     return 1;
150    }
151  printf("Watchpoint %d for expression: %s\n",wp->number,wp->exp);
152  mi_free_wp(wp);
153
154  /* Run the program. */
155  if (!gmi_exec_run(h))
156    {
157     printf("Error in run!\n");
158     mi_disconnect(h);
159     return 1;
160    }
161  /* Here we should be stopped at the breakpoint. */
162  if (!wait_for_stop(h))
163    {
164     mi_disconnect(h);
165     return 1;
166    }
167
168  /* Continue execution. */
169  if (!gmi_exec_continue(h))
170    {
171     printf("Error in continue!\n");
172     mi_disconnect(h);
173     return 1;
174    }
175  /* Here we should be terminated. */
176  if (!wait_for_stop(h))
177    {
178     mi_disconnect(h);
179     return 1;
180    }
181
182  /* Exit from gdb. */
183  gmi_gdb_exit(h);
184  /* Close the connection. */
185  mi_disconnect(h);
186  /* Wait 5 seconds and close the auxiliar terminal. */
187  printf("Waiting 5 seconds\n");
188  sleep(5);
189  gmi_end_aux_term(xterm_tty);
190
191  return 0;
192 }