cvs -z3 -d:pserver:anonymous@libmigdb.cvs.sourceforge.net:/cvsroot/libmigdb co -P...
[gdbmicli.git] / examples / linux_test.c
1 /**[txh]********************************************************************
2
3   Copyright (c) 2004 by Salvador E. Tropea.
4   Covered by the GPL license.
5
6   Comment:
7   Linux console example/test of the libmigdb.
8   Run it from a Linux console.
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     res=0;
68    }
69  return res;
70 }
71
72 int main(int argc, char *argv[])
73 {
74  mi_aux_term *child_vt=NULL;
75  mi_bkpt *bk;
76  mi_wp *wp;
77  /* This is like a file-handle for fopen.
78     Here we have all the state of gdb "connection". */
79  mi_h *h;
80
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");*/
85
86  /* Connect to gdb child. */
87  h=mi_connect_local();
88  if (!h)
89    {
90     printf("Connect failed\n");
91     return 1;
92    }
93  printf("Connected to gdb!\n");
94
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);
102
103  /* Look for a free VT where we can run the child. */
104  child_vt=gmi_look_for_free_vt();
105  if (!child_vt)
106     printf("Error opening auxiliar terminal, we'll use current one.\n");
107  else
108    {
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");
113    }
114
115  /* Tell gdb to attach the child to a terminal. */
116  if (!gmi_target_terminal(h,child_vt ? child_vt->tty : ttyname(STDIN_FILENO)))
117    {
118     printf("Error selecting target terminal\n");
119     mi_disconnect(h);
120     return 1;
121    }
122
123  /* Set the name of the child and the command line aguments. */
124  if (!gmi_set_exec(h,"./test_target","prb1 2 prb3"))
125    {
126     printf("Error setting exec y args\n");
127     mi_disconnect(h);
128     return 1;
129    }
130
131  /* Set a breakpoint. */
132  bk=gmi_break_insert(h,"test_target.cc",12);
133  if (!bk)
134    {
135     printf("Error setting breakpoint\n");
136     mi_disconnect(h);
137     return 1;
138    }
139  printf("Breakpoint %d @ function: %s\n",bk->number,bk->func);
140
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. */
147  mi_free_bkpt(bk);
148
149  /* Set a watchpoint, that's a data breakpoint. */
150  wp=gmi_break_watch(h,wm_write,"v");
151  if (!wp)
152    {
153     printf("Error al setting watchpoint\n");
154     mi_disconnect(h);
155     return 1;
156    }
157  printf("Watchpoint %d for expression: %s\n",wp->number,wp->exp);
158  mi_free_wp(wp);
159
160  /* Run the program. */
161  if (!gmi_exec_run(h))
162    {
163     printf("Error in run!\n");
164     mi_disconnect(h);
165     return 1;
166    }
167  /* Here we should be stopped at the breakpoint. */
168  if (!wait_for_stop(h))
169    {
170     mi_disconnect(h);
171     return 1;
172    }
173
174  /* Continue execution. */
175  if (!gmi_exec_continue(h))
176    {
177     printf("Error in continue!\n");
178     mi_disconnect(h);
179     return 1;
180    }
181  /* Here we should be terminated. */
182  if (!wait_for_stop(h))
183    {
184     mi_disconnect(h);
185     return 1;
186    }
187
188  /* Exit from gdb. */
189  gmi_gdb_exit(h);
190  /* Close the connection. */
191  mi_disconnect(h);
192  gmi_end_aux_term(child_vt);
193
194  return 0;
195 }