1 /**[txh]********************************************************************
3 Copyright (c) 2004 by Salvador E. Tropea.
4 Covered by the GPL license.
7 Remote example/test of the libmigdb.
10 1) Copy "test_target" to the "remote" host (it could be the same machine if
12 2) In the "remote" end run "gdbserver :6000 ./test_target 1 2 3"
13 3) Run this example in the "local" end.
15 You should edit the remote IP and port before compiling.
17 ***************************************************************************/
20 #include <unistd.h> //usleep
23 // Example: "192.168.1.3:5500"
24 #define REMOTE_MACHINE ":6000"
26 void cb_console(const char *str, void *data)
28 printf("CONSOLE> %s\n",str);
31 /* Note that unlike what's documented in gdb docs it isn't usable. */
32 void cb_target(const char *str, void *data)
34 printf("TARGET> %s\n",str);
37 void cb_log(const char *str, void *data)
39 printf("LOG> %s\n",str);
42 void cb_to(const char *str, void *data)
47 void cb_from(const char *str, void *data)
49 printf("<< %s\n",str);
52 volatile int async_c=0;
54 void cb_async(mi_output *o, void *data)
60 int wait_for_stop(mi_h *h)
66 while (!mi_get_response(h))
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);
74 printf("Error while waiting: %s\n",aux);
81 int main(int argc, char *argv[])
84 /* This is like a file-handle for fopen.
85 Here we have all the state of gdb "connection". */
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");*/
93 /* Connect to gdb child. */
97 printf("Connect failed\n");
100 printf("Connected to gdb!\n");
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);
110 /* Tell gdb to load symbols from the local copy. */
111 if (!gmi_file_symbol_file(h,"./test_target"))
113 printf("Error loading symbols\n");
118 /* Connect to remote machine using TCP/IP. */
119 if (!gmi_target_select(h,"extended-remote",REMOTE_MACHINE))
121 printf("Error connecting to gdb server\n");
126 /* Set a breakpoint. */
127 bk=gmi_break_insert(h,"test_target.cc",12);
130 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 /* Run the program. */
145 /* Note that remote targets starts running and we must use continue! */
146 if (!gmi_exec_continue(h))
148 printf("Error in continue!\n");
152 /* Here we should be stopped at the breakpoint. */
153 if (!wait_for_stop(h))
159 /* Continue execution. */
160 if (!gmi_exec_continue(h))
162 printf("Error in continue!\n");
166 /* Here we should be terminated. */
167 if (!wait_for_stop(h))
175 /* Close the connection. */