cvs -z3 -d:pserver:anonymous@libmigdb.cvs.sourceforge.net:/cvsroot/libmigdb co -P...
[gdbmicli.git] / libmigdb / examples / remote_test.c
1 /**[txh]********************************************************************
2
3   Copyright (c) 2004 by Salvador E. Tropea.
4   Covered by the GPL license.
5
6   Comment:
7   Remote example/test of the libmigdb.
8   To run it you must:
9
10 1) Copy "test_target" to the "remote" host (it could be the same machine if
11 you want).
12 2) In the "remote" end run "gdbserver :6000 ./test_target 1 2 3"
13 3) Run this example in the "local" end.
14
15 You should edit the remote IP and port before compiling.
16   
17 ***************************************************************************/
18
19 #include <stdio.h>
20 #include <unistd.h> //usleep
21 #include "mi_gdb.h"
22
23 // Example: "192.168.1.3:5500"
24 #define REMOTE_MACHINE ":6000"
25
26 void cb_console(const char *str, void *data)
27 {
28  printf("CONSOLE> %s\n",str);
29 }
30
31 /* Note that unlike what's documented in gdb docs it isn't usable. */
32 void cb_target(const char *str, void *data)
33 {
34  printf("TARGET> %s\n",str);
35 }
36
37 void cb_log(const char *str, void *data)
38 {
39  printf("LOG> %s\n",str);
40 }
41
42 void cb_to(const char *str, void *data)
43 {
44  printf(">> %s",str);
45 }
46
47 void cb_from(const char *str, void *data)
48 {
49  printf("<< %s\n",str);
50 }
51
52 volatile int async_c=0;
53
54 void cb_async(mi_output *o, void *data)
55 {
56  printf("ASYNC\n");
57  async_c++;
58 }
59
60 int wait_for_stop(mi_h *h)
61 {
62  mi_output *o;
63  int res=1;
64  char *aux;
65
66  while (!mi_get_response(h))
67     usleep(1000);
68  /* The end of the async. */
69  o=mi_retire_response(h);
70  if (mi_get_async_stop_reason(o,&aux))
71     printf("Stopped, reason: %s\n",aux);
72  else
73    {
74     printf("Error while waiting: %s\n",aux);
75     res=0;
76    }
77  mi_free_output(o);
78  return res;
79 }
80
81 int main(int argc, char *argv[])
82 {
83  mi_bkpt *bk;
84  /* This is like a file-handle for fopen.
85     Here we have all the state of gdb "connection". */
86  mi_h *h;
87
88  /* You can use any gdb you want: */
89  /*mi_set_gdb_exe("/usr/src/gdb-6.1.1/gdb/gdb");*/
90  /* You can use a terminal different than xterm: */
91  /*mi_set_xterm_exe("/usr/bin/Eterm");*/
92
93  /* Connect to gdb child. */
94  h=mi_connect_local();
95  if (!h)
96    {
97     printf("Connect failed\n");
98     return 1;
99    }
100  printf("Connected to gdb!\n");
101
102  /* Set all callbacks. */
103  mi_set_console_cb(h,cb_console,NULL);
104  mi_set_target_cb(h,cb_target,NULL);
105  mi_set_log_cb(h,cb_log,NULL);
106  mi_set_async_cb(h,cb_async,NULL);
107  mi_set_to_gdb_cb(h,cb_to,NULL);
108  mi_set_from_gdb_cb(h,cb_from,NULL);
109
110  /* Tell gdb to load symbols from the local copy. */
111  if (!gmi_file_symbol_file(h,"./test_target"))
112    {
113     printf("Error loading symbols\n");
114     mi_disconnect(h);
115     return 1;
116    }
117
118  /* Connect to remote machine using TCP/IP. */
119  if (!gmi_target_select(h,"extended-remote",REMOTE_MACHINE))
120    {
121     printf("Error connecting to gdb server\n");
122     mi_disconnect(h);
123     return 1;
124    }
125
126  /* Set a breakpoint. */
127  bk=gmi_break_insert(h,"test_target.cc",12);
128  if (!bk)
129    {
130     printf("Error setting breakpoint\n");
131     mi_disconnect(h);
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  /* Run the program. */
145  /* Note that remote targets starts running and we must use continue! */
146  if (!gmi_exec_continue(h))
147    {
148     printf("Error in continue!\n");
149     mi_disconnect(h);
150     return 1;
151    }
152  /* Here we should be stopped at the breakpoint. */
153  if (!wait_for_stop(h))
154    {
155     mi_disconnect(h);
156     return 1;
157    }
158
159  /* Continue execution. */
160  if (!gmi_exec_continue(h))
161    {
162     printf("Error in continue!\n");
163     mi_disconnect(h);
164     return 1;
165    }
166  /* Here we should be terminated. */
167  if (!wait_for_stop(h))
168    {
169     mi_disconnect(h);
170     return 1;
171    }
172
173  /* Exit from gdb. */
174  gmi_gdb_exit(h);
175  /* Close the connection. */
176  mi_disconnect(h);
177
178  return 0;
179 }