1 /**[txh]********************************************************************
3 Copyright (c) 2004 by Salvador E. Tropea.
4 Covered by the GPL license.
7 X11 example/test of the libmigdb.
8 Run it from an X11 terminal (xterm, Eterm, etc.).
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(MIDebugger &d)
57 /* The end of the async. */
60 printf("Stopped, reason: %s\n",mi_reason_enum_to_str(sr->reason));
65 printf("Error while waiting\n");
66 printf("mi_error: %d\nmi_error_from_gdb: %s\n",mi_error,mi_error_from_gdb);
72 int DoTryRun(int res, MIDebugger &d)
76 printf("Error in executing!\n");
79 if (!wait_for_stop(d))
84 #define TryRun(a,b) if (!DoTryRun(a,b)) return 1
86 int main(int argc, char *argv[])
91 // Debugging object, used as an auto var.
94 // You can use any gdb you want:
95 //MIDebugger::SetGDBExe("/usr/src/gdb-6.1.1/gdb/gdb");
96 // You can use a terminal different than xterm:
97 //MIDebugger::SetXTerm("/usr/bin/Eterm");
98 // You can specify commands for gdb
99 MIDebugger::SetGDBStartFile("cmds.txt");
101 // Connect to gdb child.
104 printf("Connect failed\n");
107 printf("Connected to gdb!\n");
109 /* Set all callbacks. */
110 d.SetConsoleCB(cb_console);
111 d.SetTargetCB(cb_target);
113 d.SetAsyncCB(cb_async);
115 d.SetFromGDBCB(cb_from);
117 // Set the name of the child and the command line aguments.
118 // It also opens the xterm.
119 if (!d.SelectTargetX11("./test_target","prb1 2 prb3"))
121 printf("Error setting exec y args\n");
125 /* Set a breakpoint. */
126 bk=d.Breakpoint("test_target.cc",15);
129 printf("Error setting breakpoint\n");
132 printf("Breakpoint %d @ function: %s\n",bk->number,bk->func);
134 /* You can do things like:
135 gmi_break_delete(h,bk->number);
136 gmi_break_set_times(h,bk->number,2);
137 gmi_break_set_condition(h,bk->number,"1");
138 gmi_break_state(h,bk->number,0);*/
139 /* If we no longer need this data we can release it. */
142 /* Set a watchpoint, that's a data breakpoint. */
143 wp=d.Watchpoint(wm_write,"v");
146 printf("Error al setting watchpoint\n");
149 printf("Watchpoint %d for expression: %s\n",wp->number,wp->exp);
152 TryRun(d.StepOver(),d);
153 /* Run the program. */
154 TryRun(d.RunOrContinue(),d);
155 /* Here we should be stopped at the breakpoint. */
157 /* Continue execution. */
158 TryRun(d.RunOrContinue(),d);
159 /* Here we should be terminated. */
161 /* Wait 5 seconds and close the auxiliar terminal. */
162 printf("Waiting 5 seconds\n");