cvs -z3 -d:pserver:anonymous@libmigdb.cvs.sourceforge.net:/cvsroot/libmigdb co -P...
[gdbmicli.git] / examples / ticepic.c
1 /**[txh]********************************************************************
2
3   Copyright (c) 2004-2009 by Salvador E. Tropea.
4   Covered by the GPL license.
5
6   Comment:
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.).
13   
14 ***************************************************************************/
15
16 #include <stdio.h>
17 #include <unistd.h> //usleep
18 #include "mi_gdb.h"
19
20 void cb_console(const char *str, void *data)
21 {
22  printf("CONSOLE> %s\n",str);
23 }
24
25 /* Note that unlike what's documented in gdb docs it isn't usable. */
26 void cb_target(const char *str, void *data)
27 {
28  printf("TARGET> %s\n",str);
29 }
30
31 void cb_log(const char *str, void *data)
32 {
33  printf("LOG> %s\n",str);
34 }
35
36 void cb_to(const char *str, void *data)
37 {
38  printf(">> %s",str);
39 }
40
41 void cb_from(const char *str, void *data)
42 {
43  printf("<< %s\n",str);
44 }
45
46 volatile int async_c=0;
47
48 void cb_async(mi_output *o, void *data)
49 {
50  printf("ASYNC\n");
51  async_c++;
52 }
53
54 void print_frames(mi_frames *f, int free_f)
55 {
56  mi_frames *ff=f;
57
58  if (!f)
59    {
60     printf("Error! empty frames info\n");
61     return;
62    }
63  while (f)
64    {
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');
67     f=f->next;
68    }
69  if (free_f)
70     mi_free_frames(ff);
71 }
72
73 int wait_for_stop(mi_h *h)
74 {
75  int res=1;
76  mi_stop *sr;
77
78  while (!mi_get_response(h))
79     usleep(1000);
80  /* The end of the async. */
81  sr=mi_res_stop(h);
82  if (sr)
83    {
84     printf("Stopped, reason: %s\n",mi_reason_enum_to_str(sr->reason));
85     print_frames(sr->frame,0);
86     mi_free_stop(sr);
87    }
88  else
89    {
90     printf("Error while waiting\n");
91     res=0;
92    }
93  return res;
94 }
95
96 void print_gvar(mi_gvar *v)
97 {
98  if (!v)
99    {
100     printf("Error! failed to define variable\n");
101     return;
102    }
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');
106 }
107
108 void print_update(mi_gvar_chg *changed)
109 {
110  printf("List of changed variables:\n");
111  while (changed)
112    {
113     printf("Name: %s\nIn scope: %c\n",changed->name,changed->in_scope ? 'y' : 'n');
114     if (changed->in_scope && changed->new_type)
115       {
116        printf("New type: %s\nNew num children: %d\n",changed->new_type,changed->new_num_children);
117       }
118     changed=changed->next;
119     printf("\n");
120    }
121 }
122
123 void print_children(mi_gvar *ch)
124 {
125  int i;
126  mi_gvar *s;
127
128  if (!ch->child)
129    {
130     printf("Error! getting children list\n");
131     return;
132    }
133  printf("\nChildren List (%d):\n",ch->numchild);
134  for (i=0, s=ch->child; i<ch->numchild; s++, i++)
135     {
136      printf("Name: %s Exp: %s Children: %d",s->name,s->exp,s->numchild);
137      if (s->type)
138         printf(" Type: %s",s->type);
139      if (s->value)
140         printf(" Value: %s",s->value);
141      printf("\n");
142     }
143  printf("\n");
144 }
145
146
147 int main(int argc, char *argv[])
148 {
149  mi_aux_term *xterm_tty=NULL;
150  mi_bkpt *bk;
151  mi_frames *fr;
152  /* This is like a file-handle for fopen.
153     Here we have all the state of gdb "connection". */
154  mi_h *h;
155  mi_gvar *gv, *gv2;
156  mi_gvar_chg *changed;
157  char *value;
158  int r_assign;
159
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");*/
164
165  /* Connect to gdb child. */
166  h=mi_connect_local();
167  if (!h)
168    {
169     printf("Connect failed\n");
170     return 1;
171    }
172  printf("Connected to gdb!\n");
173
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);
181
182  /* Set the name of the child and the command line aguments. */
183  if (!gmi_set_exec(h,"./test_bcd.cod",""))
184    {
185     printf("Error setting exec y args\n");
186     mi_disconnect(h);
187     return 1;
188    }
189
190  /* Exit from gdb. */
191  gmi_gdb_exit(h);
192  /* Close the connection. */
193  mi_disconnect(h);
194  /* Wait 5 seconds and close the auxiliar terminal. */
195  printf("Waiting 5 seconds\n");
196  sleep(5);
197  gmi_end_aux_term(xterm_tty);
198
199  return 0;
200 }