1 /**[txh]********************************************************************
3 Copyright (c) 2004-2009 by Salvador E. Tropea.
4 Covered by the GPL license.
7 This is an example of how to use libmigdb with something different than
8 gdb. In this particular case we use a program called icepic and we load
9 test_bcd.cod on it. Note that these files aren't distributed and are very
10 specific, this is just an example.
11 X11 example/test of the libmigdb.
12 Run it from an X11 terminal (xterm, Eterm, etc.).
14 ***************************************************************************/
17 #include <unistd.h> //usleep
20 void cb_console(const char *str, void *data)
22 printf("CONSOLE> %s\n",str);
25 /* Note that unlike what's documented in gdb docs it isn't usable. */
26 void cb_target(const char *str, void *data)
28 printf("TARGET> %s\n",str);
31 void cb_log(const char *str, void *data)
33 printf("LOG> %s\n",str);
36 void cb_to(const char *str, void *data)
41 void cb_from(const char *str, void *data)
43 printf("<< %s\n",str);
46 volatile int async_c=0;
48 void cb_async(mi_output *o, void *data)
54 void print_frames(mi_frames *f, int free_f)
60 printf("Error! empty frames info\n");
65 printf("Level %d, addr %p, func %s, where: %s:%d args? %c\n",f->level,f->addr,
66 f->func,f->file,f->line,f->args ? 'y' : 'n');
73 int wait_for_stop(mi_h *h)
78 while (!mi_get_response(h))
80 /* The end of the async. */
84 printf("Stopped, reason: %s\n",mi_reason_enum_to_str(sr->reason));
85 print_frames(sr->frame,0);
90 printf("Error while waiting\n");
96 void print_gvar(mi_gvar *v)
100 printf("Error! failed to define variable\n");
103 printf("Variable name: '%s', type: '%s', number of children: %d format: %s expression: %s lang: %s editable: %c\n",
104 v->name,v->type,v->numchild,mi_format_enum_to_str(v->format),
105 v->exp,mi_lang_enum_to_str(v->lang),v->attr & MI_ATTR_EDITABLE ? 'y' : 'n');
108 void print_update(mi_gvar_chg *changed)
110 printf("List of changed variables:\n");
113 printf("Name: %s\nIn scope: %c\n",changed->name,changed->in_scope ? 'y' : 'n');
114 if (changed->in_scope && changed->new_type)
116 printf("New type: %s\nNew num children: %d\n",changed->new_type,changed->new_num_children);
118 changed=changed->next;
123 void print_children(mi_gvar *ch)
130 printf("Error! getting children list\n");
133 printf("\nChildren List (%d):\n",ch->numchild);
134 for (i=0, s=ch->child; i<ch->numchild; s++, i++)
136 printf("Name: %s Exp: %s Children: %d",s->name,s->exp,s->numchild);
138 printf(" Type: %s",s->type);
140 printf(" Value: %s",s->value);
147 int main(int argc, char *argv[])
149 mi_aux_term *xterm_tty=NULL;
152 /* This is like a file-handle for fopen.
153 Here we have all the state of gdb "connection". */
156 mi_gvar_chg *changed;
160 /* You can use any gdb you want: */
161 mi_set_gdb_exe("./icepic");
162 /* You can use a terminal different than xterm: */
163 /*mi_set_xterm_exe("/usr/bin/Eterm");*/
165 /* Connect to gdb child. */
166 h=mi_connect_local();
169 printf("Connect failed\n");
172 printf("Connected to gdb!\n");
174 /* Set all callbacks. */
175 mi_set_console_cb(h,cb_console,NULL);
176 mi_set_target_cb(h,cb_target,NULL);
177 mi_set_log_cb(h,cb_log,NULL);
178 mi_set_async_cb(h,cb_async,NULL);
179 mi_set_to_gdb_cb(h,cb_to,NULL);
180 mi_set_from_gdb_cb(h,cb_from,NULL);
182 /* Set the name of the child and the command line aguments. */
183 if (!gmi_set_exec(h,"./test_bcd.cod",""))
185 printf("Error setting exec y args\n");
192 /* Close the connection. */
194 /* Wait 5 seconds and close the auxiliar terminal. */
195 printf("Waiting 5 seconds\n");
197 gmi_end_aux_term(xterm_tty);