C grammar
[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         asprintf(&message, "Bug detected in file:%s\nfunction:%s\nline:%d\nreason:%s\nreceived signal:%s\n%s\n",
61                 f->file, f->func, f->line, mi_reason_enum_to_str(sr->reason), sr->signal_name, sr->signal_meaning);
62         sendMail(message);
63 }
64
65
66 int wait_for_stop(mi_h *h)
67 {
68  int res=1;
69  mi_stop *sr;
70  mi_frames *f;
71
72  while (!mi_get_response(h))
73     usleep(1000);
74  /* The end of the async. */
75  sr=mi_res_stop(h);
76  if (sr)
77    {
78     f = gmi_stack_info_frame(h);
79     send_bug_mail(sr, f);
80     mi_free_stop(sr);
81     res = 0;
82    }
83  else
84    {
85     printf("Error while waiting\n");
86     printf("mi_error: %d\nmi_error_from_gdb: %s\n",mi_error,mi_error_from_gdb);
87     res=0;
88    }
89  return res;
90 }
91
92 int main(int argc, char *argv[])
93 {
94  mi_aux_term *xterm_tty=NULL;
95  const char* binaryName;
96  
97  binaryName = argv[1];
98  GNUNET_assert(NULL != binaryName);
99  
100  /* This is like a file-handle for fopen.
101     Here we have all the state of gdb "connection". */
102  mi_h *h;
103
104  /* Connect to gdb child. */
105  h=mi_connect_local();
106  if (!h)
107    {
108     printf("Connect failed\n");
109     return 1;
110    }
111  printf("Connected to gdb!\n");
112
113  /* Set all callbacks. */
114  mi_set_console_cb(h,cb_console,NULL);
115  mi_set_target_cb(h,cb_target,NULL);
116  mi_set_log_cb(h,cb_log,NULL);
117  mi_set_async_cb(h,cb_async,NULL);
118  mi_set_to_gdb_cb(h,cb_to,NULL);
119  mi_set_from_gdb_cb(h,cb_from,NULL);
120
121  /* Set the name of the child and the command line aguments. */
122  if (!gmi_set_exec(h, binaryName, NULL))
123    {
124     printf("Error setting exec y args\n");
125     mi_disconnect(h);
126     return 1;
127    }
128
129  /* Tell gdb to attach the child to a terminal. */
130  if (!gmi_target_terminal(h, ttyname(STDIN_FILENO)))
131    {
132     printf("Error selecting target terminal\n");
133     mi_disconnect(h);
134     return 1;
135    }
136
137  /* Run the program. */
138  if (!gmi_exec_run(h))
139    {
140     printf("Error in run!\n");
141     mi_disconnect(h);
142     return 1;
143    }
144  /* Here we should be stopped when the program crashes */
145  if (!wait_for_stop(h))
146    {
147     mi_disconnect(h);
148     return 1;
149    }
150
151  /* Continue execution. */
152  if (!gmi_exec_continue(h))
153    {
154     printf("Error in continue!\n");
155     mi_disconnect(h);
156     return 1;
157    }
158  /* Here we should be terminated. */
159  if (!wait_for_stop(h))
160    {
161     mi_disconnect(h);
162     return 1;
163    }
164
165  /* Exit from gdb. */
166  gmi_gdb_exit(h);
167  /* Close the connection. */
168  mi_disconnect(h);
169  /* Wait 5 seconds and close the auxiliar terminal. */
170  printf("Waiting 5 seconds\n");
171  sleep(5);
172  gmi_end_aux_term(xterm_tty);
173
174  return 0;
175 }