fixing 1568
[oweals/gnunet.git] / src / monkey / gnunet-monkey.c
1 /**[txh]********************************************************************
2
3   Copyright (c) 2004 by Salvador E. Tropea.
4   Covered by the GPL license.
5
6   Comment:
7   X11 example/test of the libmigdb.
8   Run it from an X11 terminal (xterm, Eterm, etc.).
9   
10 ***************************************************************************/
11
12 #include <stdio.h>
13 #include <unistd.h> //usleep
14 #include <libesmtp.h>
15 #include "gdbmi.h"
16
17 void cb_console(const char *str, void *data)
18 {
19  printf("CONSOLE> %s\n",str);
20 }
21
22 /* Note that unlike what's documented in gdb docs it isn't usable. */
23 void cb_target(const char *str, void *data)
24 {
25  printf("TARGET> %s\n",str);
26 }
27
28 void cb_log(const char *str, void *data)
29 {
30  printf("LOG> %s\n",str);
31 }
32
33 void cb_to(const char *str, void *data)
34 {
35  printf(">> %s",str);
36 }
37
38 void cb_from(const char *str, void *data)
39 {
40  printf("<< %s\n",str);
41 }
42
43 volatile int async_c=0;
44
45 void cb_async(mi_output *o, void *data)
46 {
47  printf("ASYNC\n");
48  async_c++;
49 }
50
51 int wait_for_stop(mi_h *h)
52 {
53  int res=1;
54  mi_stop *sr;
55  mi_frames *f;
56
57  while (!mi_get_response(h))
58     usleep(1000);
59  /* The end of the async. */
60  sr=mi_res_stop(h);
61  if (sr)
62    {
63     printf("Stopped, reason: %s\n",mi_reason_enum_to_str(sr->reason));
64     printf("Received signal name: %s\n", sr->signal_name);
65     printf("Received signal meaning: %s\n", sr->signal_meaning);
66     //printf("In file: %s\n", sr->frame->file);
67     //printf("Line Number: %d\n", sr->frame->line);
68     f = gmi_stack_info_frame(h);
69     mi_free_stop(sr);
70    }
71  else
72    {
73     printf("Error while waiting\n");
74     printf("mi_error: %d\nmi_error_from_gdb: %s\n",mi_error,mi_error_from_gdb);
75     res=0;
76    }
77  return res;
78 }
79
80 int main(int argc, char *argv[])
81 {
82  mi_aux_term *xterm_tty=NULL;
83  
84  /* This is like a file-handle for fopen.
85     Here we have all the state of gdb "connection". */
86  mi_h *h;
87
88  /* Connect to gdb child. */
89  h=mi_connect_local();
90  if (!h)
91    {
92     printf("Connect failed\n");
93     return 1;
94    }
95  printf("Connected to gdb!\n");
96
97  /* Set all callbacks. */
98  mi_set_console_cb(h,cb_console,NULL);
99  mi_set_target_cb(h,cb_target,NULL);
100  mi_set_log_cb(h,cb_log,NULL);
101  mi_set_async_cb(h,cb_async,NULL);
102  mi_set_to_gdb_cb(h,cb_to,NULL);
103  mi_set_from_gdb_cb(h,cb_from,NULL);
104
105  /* Set the name of the child and the command line aguments. */
106  if (!gmi_set_exec(h,"bug_null_pointer_exception", NULL))
107    {
108     printf("Error setting exec y args\n");
109     mi_disconnect(h);
110     return 1;
111    }
112
113  /* Tell gdb to attach the child to a terminal. */
114  if (!gmi_target_terminal(h, ttyname(STDIN_FILENO)))
115    {
116     printf("Error selecting target terminal\n");
117     mi_disconnect(h);
118     return 1;
119    }
120
121  /* Run the program. */
122  if (!gmi_exec_run(h))
123    {
124     printf("Error in run!\n");
125     mi_disconnect(h);
126     return 1;
127    }
128  /* Here we should be stopped when the program crashes */
129  if (!wait_for_stop(h))
130    {
131     mi_disconnect(h);
132     return 1;
133    }
134
135  /* Continue execution. */
136  if (!gmi_exec_continue(h))
137    {
138     printf("Error in continue!\n");
139     mi_disconnect(h);
140     return 1;
141    }
142  /* Here we should be terminated. */
143  if (!wait_for_stop(h))
144    {
145     mi_disconnect(h);
146     return 1;
147    }
148
149  /* Exit from gdb. */
150  gmi_gdb_exit(h);
151  /* Close the connection. */
152  mi_disconnect(h);
153  /* Wait 5 seconds and close the auxiliar terminal. */
154  printf("Waiting 5 seconds\n");
155  sleep(5);
156  gmi_end_aux_term(xterm_tty);
157
158  return 0;
159 }