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 void print_frames(mi_frames *f, int free_f)
56 printf("Error! empty frames info\n");
61 printf("Level %d, addr %p, func %s, where: %s:%d args? %c\n",f->level,f->addr,
62 f->func,f->file,f->line,f->args ? 'y' : 'n');
69 int wait_for_stop(mi_h *h)
74 while (!mi_get_response(h))
76 /* The end of the async. */
80 printf("Stopped, reason: %s\n",mi_reason_enum_to_str(sr->reason));
81 print_frames(sr->frame,0);
86 printf("Error while waiting\n");
92 void print_gvar(mi_gvar *v)
96 printf("Error! failed to define variable\n");
99 printf("Variable name: '%s', type: '%s', number of children: %d format: %s expression: %s lang: %s editable: %c\n",
100 v->name,v->type,v->numchild,mi_format_enum_to_str(v->format),
101 v->exp,mi_lang_enum_to_str(v->lang),v->attr & MI_ATTR_EDITABLE ? 'y' : 'n');
104 void print_update(mi_gvar_chg *changed)
106 printf("List of changed variables:\n");
109 printf("Name: %s\nIn scope: %c\n",changed->name,changed->in_scope ? 'y' : 'n');
110 if (changed->in_scope && changed->new_type)
112 printf("New type: %s\nNew num children: %d\n",changed->new_type,changed->new_num_children);
114 changed=changed->next;
119 void print_children(mi_gvar *ch)
126 printf("Error! getting children list\n");
129 printf("\nChildren List (%d):\n",ch->numchild);
130 for (i=0, s=ch->child; i<ch->numchild; s++, i++)
132 printf("Name: %s Exp: %s Children: %d",s->name,s->exp,s->numchild);
134 printf(" Type: %s",s->type);
136 printf(" Value: %s",s->value);
143 int main(int argc, char *argv[])
145 mi_aux_term *xterm_tty=NULL;
148 /* This is like a file-handle for fopen.
149 Here we have all the state of gdb "connection". */
152 mi_gvar_chg *changed;
156 /* You can use any gdb you want: */
157 /*mi_set_gdb_exe("/usr/src/gdb-6.1.1/gdb/gdb");*/
158 /* You can use a terminal different than xterm: */
159 /*mi_set_xterm_exe("/usr/bin/Eterm");*/
161 /* Connect to gdb child. */
162 h=mi_connect_local();
165 printf("Connect failed\n");
168 printf("Connected to gdb!\n");
170 /* Set all callbacks. */
171 mi_set_console_cb(h,cb_console,NULL);
172 mi_set_target_cb(h,cb_target,NULL);
173 mi_set_log_cb(h,cb_log,NULL);
174 mi_set_async_cb(h,cb_async,NULL);
175 mi_set_to_gdb_cb(h,cb_to,NULL);
176 mi_set_from_gdb_cb(h,cb_from,NULL);
178 /* Set the name of the child and the command line aguments. */
179 if (!gmi_set_exec(h,"./target_frames",""))
181 printf("Error setting exec y args\n");
186 /* Open an xterm to be used as terminal for the debuggy. */
187 xterm_tty=gmi_start_xterm();
189 printf("Error opening auxiliar terminal, we'll use current one.\n");
191 printf("XTerm opened @ %s\n",xterm_tty->tty);
193 /* Tell gdb to attach the child to a terminal. */
194 if (!gmi_target_terminal(h,xterm_tty ? xterm_tty->tty : ttyname(STDIN_FILENO)))
196 printf("Error selecting target terminal\n");
201 /* Set a breakpoint. */
202 bk=gmi_break_insert(h,"target_frames.cc",7);
205 printf("Error setting breakpoint\n");
209 printf("Breakpoint %d @ function: %s\n",bk->number,bk->func);
211 /* If we no longer need this data we can release it. */
214 /* Run the program. */
215 if (!gmi_exec_run(h))
217 printf("Error in run!\n");
221 /* Here we should be stopped at the breakpoint. */
222 if (!wait_for_stop(h))
228 /* Get information about the calling stack. */
229 fr=gmi_stack_list_frames(h);
230 printf("\nCalling stack:\n\n");
233 fr=gmi_stack_info_frame(h);
234 printf("\nCurrent frame:\n\n");
237 printf("Stack depth: %d\n",gmi_stack_info_depth_get(h));
238 gmi_stack_select_frame(h,1);
239 fr=gmi_stack_info_frame(h);
240 printf("\nFrame 1:\n\n");
246 gv=gmi_var_create(h,-1,"v");
248 gmi_var_show_format(h,gv);
250 gmi_var_info_num_children(h,gv);
252 gmi_var_info_type(h,gv);
254 gmi_var_info_expression(h,gv);
256 gmi_var_show_attributes(h,gv);
261 gv=gmi_full_var_create(h,-1,"v");
264 gv2=gmi_full_var_create(h,-1,"sup");
266 gmi_var_set_format(h,gv,fm_hexadecimal);
269 /* Continue execution. */
270 if (!gmi_exec_until(h,"target_frames.cc",21))
272 printf("Error in exec until!\n");
276 if (!wait_for_stop(h))
282 gmi_var_update(h,NULL,&changed);
283 print_update(changed);
284 mi_free_gvar_chg(changed);
286 r_assign=gmi_var_assign(h,gv,"i+5");
287 if (r_assign && gv->value)
288 printf("\nAssigned v=%s\n\n",gv->value);
290 gmi_var_list_children(h,gv2);
293 gmi_var_evaluate_expression(h,gv2);
294 printf("\n%s = %s\n\n",gv2->exp,gv2->value);
296 value=gmi_data_evaluate_expression(h,gv2->exp);
297 printf("\n%s = %s\n\n",gv2->exp,value);
300 gmi_var_delete(h,gv);
302 gmi_var_delete(h,gv2);
305 /* Continue execution. */
306 if (!gmi_exec_continue(h))
308 printf("Error in continue!\n");
312 /* Here we should be terminated. */
313 if (!wait_for_stop(h))
321 /* Close the connection. */
323 /* Wait 5 seconds and close the auxiliar terminal. */
324 printf("Waiting 5 seconds\n");
326 gmi_end_aux_term(xterm_tty);