2 This file is part of GNUnet.
3 (C) 2010, 2011 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file monkey/action_api.c
23 * @brief Monkey API for actions taken by Monkey while debugging
27 #include "gnunet_common.h"
28 #include "gnunet_monkey_action.h"
32 #define DEBUG_MODE_GDB 0
33 #define DEBUG_MODE_VALGRIND 1
34 #define DEBUG_MODE_REPORT_READY 2
36 extern void sendMail (const char *messageContents);
42 static void cb_console(const char *str, void *data)
44 printf("CONSOLE> %s\n",str);
48 /* Note that unlike what's documented in gdb docs it isn't usable. */
49 static void cb_target(const char *str, void *data)
51 printf("TARGET> %s\n",str);
55 static void cb_log(const char *str, void *data)
57 printf("LOG> %s\n",str);
61 static void cb_to(const char *str, void *data)
67 static void cb_from(const char *str, void *data)
69 printf("<< %s\n",str);
73 static void cb_async(mi_output *o, void *data)
80 static int wait_for_stop(mi_h *h)
86 while (!mi_get_response(h))
88 /* The end of the async. */
92 f = gmi_stack_info_frame(h);
94 printf("f is NULL!\n");
109 int GNUNET_MONKEY_ACTION_report_file(struct GNUNET_MONKEY_ACTION_Context* cntxt, const char* dumpFileName)
111 FILE* file = fopen(dumpFileName, "w");
112 GNUNET_assert(NULL != file);
113 fprintf(file,"%s", cntxt->debug_report);
119 int GNUNET_MONKEY_ACTION_report_email(struct GNUNET_MONKEY_ACTION_Context* cntxt)
121 if (cntxt->debug_mode == DEBUG_MODE_REPORT_READY)
122 sendMail(cntxt->debug_report);
129 int GNUNET_MONKEY_ACTION_rerun_with_valgrind()
135 int GNUNET_MONKEY_ACTION_rerun_with_gdb(struct GNUNET_MONKEY_ACTION_Context* cntxt)
137 cntxt->debug_mode = DEBUG_MODE_GDB;
138 mi_aux_term *xterm_tty=NULL;
140 /* This is like a file-handle for fopen.
141 Here we have all the state of gdb "connection". */
144 /* Connect to gdb child. */
145 h = mi_connect_local();
148 printf("Connect failed\n");
151 printf("Connected to gdb!\n");
153 /* Set all callbacks. */
154 mi_set_console_cb(h,cb_console,NULL);
155 mi_set_target_cb(h,cb_target,NULL);
156 mi_set_log_cb(h,cb_log,NULL);
157 mi_set_async_cb(h,cb_async,NULL);
158 mi_set_to_gdb_cb(h,cb_to,NULL);
159 mi_set_from_gdb_cb(h,cb_from,NULL);
161 /* Set the name of the child and the command line aguments. */
162 if (!gmi_set_exec(h, cntxt->binary_name, NULL))
164 printf("Error setting exec y args\n");
169 /* Tell gdb to attach the child to a terminal. */
170 if (!gmi_target_terminal(h, ttyname(STDIN_FILENO)))
172 printf("Error selecting target terminal\n");
177 /* Run the program. */
178 if (!gmi_exec_run(h))
180 printf("Error in run!\n");
184 /* Here we should be stopped when the program crashes */
185 if (!wait_for_stop(h))
191 /* Continue execution. */
192 if (!gmi_exec_continue(h))
194 printf("Error in continue!\n");
198 /* Here we should be terminated. */
199 if (!wait_for_stop(h))
207 /* Close the connection. */
209 /* Wait 5 seconds and close the auxiliar terminal. */
210 printf("Waiting 5 seconds\n");
212 gmi_end_aux_term(xterm_tty);
218 int GNUNET_MONKEY_ACTION_format_report(struct GNUNET_MONKEY_ACTION_Context* cntxt)
220 switch (cntxt->debug_mode) {
222 GNUNET_asprintf(&cntxt->debug_report,
223 "Bug detected in file:%s\nfunction:%s\nline:%d\nreason:%s\nreceived signal:%s\n%s\n",
224 cntxt->gdb_frames->file, cntxt->gdb_frames->func, cntxt->gdb_frames->line, mi_reason_enum_to_str(cntxt->gdb_stop_reason->reason), cntxt->gdb_stop_reason->signal_name, cntxt->gdb_stop_reason->signal_meaning);
226 case DEBUG_MODE_VALGRIND:
232 cntxt->debug_mode = DEBUG_MODE_REPORT_READY;
237 int GNUNET_MONKEY_ACTION_check_bug_redundancy()