removed unused variable
[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 #include "platform.h"
17 #include "gnunet_common.h"
18
19
20 extern void sendMail (const char *messageContents);
21
22 void cb_console(const char *str, void *data)
23 {
24  printf("CONSOLE> %s\n",str);
25 }
26
27 /* Note that unlike what's documented in gdb docs it isn't usable. */
28 void cb_target(const char *str, void *data)
29 {
30  printf("TARGET> %s\n",str);
31 }
32
33 void cb_log(const char *str, void *data)
34 {
35  printf("LOG> %s\n",str);
36 }
37
38 void cb_to(const char *str, void *data)
39 {
40  printf(">> %s",str);
41 }
42
43 void cb_from(const char *str, void *data)
44 {
45  printf("<< %s\n",str);
46 }
47
48 volatile int async_c=0;
49
50 void cb_async(mi_output *o, void *data)
51 {
52  printf("ASYNC\n");
53  async_c++;
54 }
55
56
57 void send_bug_mail(mi_stop* sr, mi_frames* f)
58 {
59         char *message;
60         GNUNET_asprintf(&message, 
61                         "Bug detected in file:%s\nfunction:%s\nline:%d\nreason:%s\nreceived signal:%s\n%s\n",
62                         f->file, f->func, f->line, mi_reason_enum_to_str(sr->reason), sr->signal_name, sr->signal_meaning);
63         sendMail(message);
64         GNUNET_free (message);
65 }
66
67
68 int wait_for_stop(mi_h *h)
69 {
70  int res=1;
71  mi_stop *sr;
72  mi_frames *f;
73
74  while (!mi_get_response(h))
75     usleep(1000);
76  /* The end of the async. */
77  sr=mi_res_stop(h);
78  if (sr)
79    {
80     f = gmi_stack_info_frame(h);
81     send_bug_mail(sr, f);
82     mi_free_stop(sr);
83     res = 0;
84    }
85  else
86    {
87     printf("Error while waiting\n");
88     printf("mi_error: %d\nmi_error_from_gdb: %s\n",mi_error,mi_error_from_gdb);
89     res=0;
90    }
91  return res;
92 }
93
94 int main(int argc, char *argv[])
95 {
96  mi_aux_term *xterm_tty=NULL;
97  const char* binaryName;
98  
99  binaryName = argv[1];
100  GNUNET_assert(NULL != binaryName);
101  
102  /* This is like a file-handle for fopen.
103     Here we have all the state of gdb "connection". */
104  mi_h *h;
105
106  /* Connect to gdb child. */
107  h=mi_connect_local();
108  if (!h)
109    {
110     printf("Connect failed\n");
111     return 1;
112    }
113  printf("Connected to gdb!\n");
114
115  /* Set all callbacks. */
116  mi_set_console_cb(h,cb_console,NULL);
117  mi_set_target_cb(h,cb_target,NULL);
118  mi_set_log_cb(h,cb_log,NULL);
119  mi_set_async_cb(h,cb_async,NULL);
120  mi_set_to_gdb_cb(h,cb_to,NULL);
121  mi_set_from_gdb_cb(h,cb_from,NULL);
122
123  /* Set the name of the child and the command line aguments. */
124  if (!gmi_set_exec(h, binaryName, NULL))
125    {
126     printf("Error setting exec y args\n");
127     mi_disconnect(h);
128     return 1;
129    }
130
131  /* Tell gdb to attach the child to a terminal. */
132  if (!gmi_target_terminal(h, ttyname(STDIN_FILENO)))
133    {
134     printf("Error selecting target terminal\n");
135     mi_disconnect(h);
136     return 1;
137    }
138
139  /* Run the program. */
140  if (!gmi_exec_run(h))
141    {
142     printf("Error in run!\n");
143     mi_disconnect(h);
144     return 1;
145    }
146  /* Here we should be stopped when the program crashes */
147  if (!wait_for_stop(h))
148    {
149     mi_disconnect(h);
150     return 1;
151    }
152
153  /* Continue execution. */
154  if (!gmi_exec_continue(h))
155    {
156     printf("Error in continue!\n");
157     mi_disconnect(h);
158     return 1;
159    }
160  /* Here we should be terminated. */
161  if (!wait_for_stop(h))
162    {
163     mi_disconnect(h);
164     return 1;
165    }
166
167  /* Exit from gdb. */
168  gmi_gdb_exit(h);
169  /* Close the connection. */
170  mi_disconnect(h);
171  /* Wait 5 seconds and close the auxiliar terminal. */
172  printf("Waiting 5 seconds\n");
173  sleep(5);
174  gmi_end_aux_term(xterm_tty);
175
176  return 0;
177 }