aboutsummaryrefslogtreecommitdiff
path: root/src/monkey
diff options
context:
space:
mode:
authorSafey A.Halim <safey.allah@gmail.com>2011-03-21 07:58:04 +0000
committerSafey A.Halim <safey.allah@gmail.com>2011-03-21 07:58:04 +0000
commitab8d56ea330a41dfb0a7b82e91a097a41f56254d (patch)
tree62dc4df824f7858a00d7af0b901b1075acb9e326 /src/monkey
parent5434ca0cc3cdd8905d9018debc24bc2c8e938b6f (diff)
downloadgnunet-ab8d56ea330a41dfb0a7b82e91a097a41f56254d.tar.gz
gnunet-ab8d56ea330a41dfb0a7b82e91a097a41f56254d.zip
Action API fixed
Diffstat (limited to 'src/monkey')
-rw-r--r--src/monkey/action_api.c75
-rw-r--r--src/monkey/gnunet-monkey.c273
-rw-r--r--src/monkey/gnunet_monkey_action.h10
-rw-r--r--src/monkey/mail_sender.c4
4 files changed, 106 insertions, 256 deletions
diff --git a/src/monkey/action_api.c b/src/monkey/action_api.c
index 6c7441ad7..ff5c59bf9 100644
--- a/src/monkey/action_api.c
+++ b/src/monkey/action_api.c
@@ -28,12 +28,7 @@
28#include "gnunet_monkey_action.h" 28#include "gnunet_monkey_action.h"
29#include <libesmtp.h> 29#include <libesmtp.h>
30 30
31 31extern void sendMail (const char *messageContents, const char *emailAddress);
32#define DEBUG_MODE_GDB 0
33#define DEBUG_MODE_VALGRIND 1
34#define DEBUG_MODE_REPORT_READY 2
35
36extern void sendMail (const char *messageContents);
37 32
38 33
39static int async_c=0; 34static int async_c=0;
@@ -77,32 +72,24 @@ static void cb_async(mi_output *o, void *data)
77} 72}
78 73
79 74
80static int wait_for_stop(mi_h *h) 75static int wait_for_stop(mi_h *h, struct GNUNET_MONKEY_ACTION_Context *cntxt)
81{ 76{
82 int res=1;
83 mi_stop *sr;
84 mi_frames *f;
85
86 while (!mi_get_response(h)) 77 while (!mi_get_response(h))
87 usleep(1000); 78 usleep(1000);
88 /* The end of the async. */ 79 /* The end of the async. */
89 sr=mi_res_stop(h); 80 cntxt->gdb_stop_reason=mi_res_stop(h);
90 if (sr) 81 if (cntxt->gdb_stop_reason)
91 { 82 {
92 f = gmi_stack_info_frame(h); 83 if (cntxt->gdb_stop_reason->reason == sr_exited_normally)
93 if (NULL == f) 84 return GDB_STATE_EXIT_NORMALLY;
94 printf("f is NULL!\n"); 85
95 if (NULL == f) 86 cntxt->gdb_frames = gmi_stack_info_frame(h);
87 if (NULL == cntxt->gdb_frames)
96 GNUNET_break(0); 88 GNUNET_break(0);
97 89
98 mi_free_stop(sr); 90 return GDB_STATE_STOPPED;
99 res = 0;
100 }
101 else
102 {
103 res=0;
104 } 91 }
105 return res; 92 return GDB_STATE_ERROR;
106} 93}
107 94
108 95
@@ -119,7 +106,7 @@ int GNUNET_MONKEY_ACTION_report_file(struct GNUNET_MONKEY_ACTION_Context* cntxt,
119int GNUNET_MONKEY_ACTION_report_email(struct GNUNET_MONKEY_ACTION_Context* cntxt) 106int GNUNET_MONKEY_ACTION_report_email(struct GNUNET_MONKEY_ACTION_Context* cntxt)
120{ 107{
121 if (cntxt->debug_mode == DEBUG_MODE_REPORT_READY) 108 if (cntxt->debug_mode == DEBUG_MODE_REPORT_READY)
122 sendMail(cntxt->debug_report); 109 sendMail(cntxt->debug_report, cntxt->email_address);
123 110
124 return GNUNET_OK; 111 return GNUNET_OK;
125} 112}
@@ -135,11 +122,12 @@ int GNUNET_MONKEY_ACTION_rerun_with_valgrind()
135int GNUNET_MONKEY_ACTION_rerun_with_gdb(struct GNUNET_MONKEY_ACTION_Context* cntxt) 122int GNUNET_MONKEY_ACTION_rerun_with_gdb(struct GNUNET_MONKEY_ACTION_Context* cntxt)
136{ 123{
137 cntxt->debug_mode = DEBUG_MODE_GDB; 124 cntxt->debug_mode = DEBUG_MODE_GDB;
138 mi_aux_term *xterm_tty=NULL;
139 125
140 /* This is like a file-handle for fopen. 126 /* This is like a file-handle for fopen.
141 Here we have all the state of gdb "connection". */ 127 Here we have all the state of gdb "connection". */
142 mi_h *h; 128 mi_set_gdb_exe("/tmp/gdb/bin/gdb");
129 mi_h *h;
130 int ret;
143 131
144 /* Connect to gdb child. */ 132 /* Connect to gdb child. */
145 h = mi_connect_local(); 133 h = mi_connect_local();
@@ -182,36 +170,11 @@ int GNUNET_MONKEY_ACTION_rerun_with_gdb(struct GNUNET_MONKEY_ACTION_Context* cnt
182 return GNUNET_NO; 170 return GNUNET_NO;
183 } 171 }
184 /* Here we should be stopped when the program crashes */ 172 /* Here we should be stopped when the program crashes */
185 if (!wait_for_stop(h)) 173 ret = wait_for_stop(h, cntxt);
186 { 174 if (ret != GDB_STATE_ERROR)
187 mi_disconnect(h);
188 return GNUNET_NO;
189 }
190
191 /* Continue execution. */
192 if (!gmi_exec_continue(h))
193 {
194 printf("Error in continue!\n");
195 mi_disconnect(h);
196 return GNUNET_NO;
197 }
198 /* Here we should be terminated. */
199 if (!wait_for_stop(h))
200 {
201 mi_disconnect(h); 175 mi_disconnect(h);
202 return GNUNET_NO;
203 }
204 176
205 /* Exit from gdb. */ 177 return ret;
206 gmi_gdb_exit(h);
207 /* Close the connection. */
208 mi_disconnect(h);
209 /* Wait 5 seconds and close the auxiliar terminal. */
210 printf("Waiting 5 seconds\n");
211 sleep(5);
212 gmi_end_aux_term(xterm_tty);
213
214 return GNUNET_OK;
215} 178}
216 179
217 180
@@ -219,7 +182,7 @@ int GNUNET_MONKEY_ACTION_format_report(struct GNUNET_MONKEY_ACTION_Context* cntx
219{ 182{
220 switch (cntxt->debug_mode) { 183 switch (cntxt->debug_mode) {
221 case DEBUG_MODE_GDB: 184 case DEBUG_MODE_GDB:
222 GNUNET_asprintf(&cntxt->debug_report, 185 GNUNET_asprintf(&(cntxt->debug_report),
223 "Bug detected in file:%s\nfunction:%s\nline:%d\nreason:%s\nreceived signal:%s\n%s\n", 186 "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); 187 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);
225 break; 188 break;
diff --git a/src/monkey/gnunet-monkey.c b/src/monkey/gnunet-monkey.c
index 13bd71e84..c8ac4369c 100644
--- a/src/monkey/gnunet-monkey.c
+++ b/src/monkey/gnunet-monkey.c
@@ -1,122 +1,43 @@
1/**[txh]******************************************************************** 1/*
2 This file is part of GNUnet.
3 (C) 2010, 2011 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19*/
2 20
3 Copyright (c) 2004 by Salvador E. Tropea. 21/**
4 Covered by the GPL license. 22 * @file monkey/gnunet-monkey.c
5 23 * @brief Monkey: gnunet automated debugging tool
6 Comment: 24 */
7 X11 example/test of the libmigdb.
8 Run it from an X11 terminal (xterm, Eterm, etc.).
9
10***************************************************************************/
11 25
12#include <stdio.h> 26#include <stdio.h>
13#include <unistd.h> //usleep
14#include <libesmtp.h>
15#include "gdbmi.h"
16#include "platform.h" 27#include "platform.h"
17#include "gnunet_common.h" 28#include "gnunet_common.h"
18#include "gnunet_getopt_lib.h" 29#include "gnunet_getopt_lib.h"
19#include "gnunet_program_lib.h" 30#include "gnunet_program_lib.h"
20#include "gnunet_monkey_action.h" 31#include "gnunet_monkey_action.h"
21 32
22extern void sendMail (const char *messageContents);
23static const char* mode; 33static const char* mode;
24static const char* dumpFileName; 34static const char* dumpFileName;
25static const char* binaryName; 35static const char* binaryName;
36static const char* emailAddress;
26static int ret = 0; 37static int ret = 0;
27 38
28void cb_console(const char *str, void *data)
29{
30 printf("CONSOLE> %s\n",str);
31}
32
33/* Note that unlike what's documented in gdb docs it isn't usable. */
34void cb_target(const char *str, void *data)
35{
36 printf("TARGET> %s\n",str);
37}
38
39void cb_log(const char *str, void *data)
40{
41 printf("LOG> %s\n",str);
42}
43
44void cb_to(const char *str, void *data)
45{
46 printf(">> %s",str);
47}
48
49void cb_from(const char *str, void *data)
50{
51 printf("<< %s\n",str);
52}
53
54static int async_c=0;
55
56void cb_async(mi_output *o, void *data)
57{
58 printf("ASYNC\n");
59 async_c++;
60}
61
62
63static void dumpText(const char* message)
64{
65 FILE* file = fopen(dumpFileName, "w");
66 GNUNET_assert(NULL != file);
67 fprintf(file,"%s", message);
68 fclose(file);
69}
70
71
72void send_bug_mail(mi_stop* sr, mi_frames* f)
73{
74 char *message;
75 GNUNET_asprintf(&message,
76 "Bug detected in file:%s\nfunction:%s\nline:%d\nreason:%s\nreceived signal:%s\n%s\n",
77 f->file, f->func, f->line, mi_reason_enum_to_str(sr->reason), sr->signal_name, sr->signal_meaning);
78 if (strcasecmp(mode, "mail") == 0)
79 sendMail(message);
80 else
81 dumpText(message);
82
83 GNUNET_free (message);
84}
85
86
87int wait_for_stop(mi_h *h)
88{
89 int res=1;
90 mi_stop *sr;
91 mi_frames *f;
92
93 while (!mi_get_response(h))
94 usleep(1000);
95 /* The end of the async. */
96 sr=mi_res_stop(h);
97 if (sr)
98 {
99 f = gmi_stack_info_frame(h);
100 if (f != NULL)
101 send_bug_mail(sr, f);
102 else
103 GNUNET_break (0);
104 mi_free_stop(sr);
105 res = 0;
106 }
107 else
108 {
109 printf("Error while waiting\n");
110 printf("mi_error: %d\nmi_error_from_gdb: %s\n",mi_error,mi_error_from_gdb);
111 res=0;
112 }
113 return res;
114}
115
116
117
118/** 39/**
119 * Main function that will be run by the scheduler. 40 * Main function that will launch the action api.
120 * 41 *
121 * @param cls closure 42 * @param cls closure
122 * @param args remaining command-line arguments 43 * @param args remaining command-line arguments
@@ -129,113 +50,69 @@ run (void *cls,
129 const char *cfgfile, 50 const char *cfgfile,
130 const struct GNUNET_CONFIGURATION_Handle *c) 51 const struct GNUNET_CONFIGURATION_Handle *c)
131{ 52{
132 struct GNUNET_MONKEY_ACTION_Context* cntxt = 53 int result;
133 GNUNET_malloc(sizeof(struct GNUNET_MONKEY_ACTION_Context)); 54 struct GNUNET_MONKEY_ACTION_Context* cntxt;
134 cntxt->binary_name = binaryName; 55
135 if (GNUNET_OK == GNUNET_MONKEY_ACTION_rerun_with_gdb(cntxt)) { 56 if (strcasecmp(mode, "email") == 0) {
136 GNUNET_MONKEY_ACTION_format_report(cntxt); 57 if (NULL == emailAddress) {
137 GNUNET_MONKEY_ACTION_report_file(cntxt, dumpFileName); 58 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Working in email mode requires an email address!\n");
59 ret = 1;
60 return;
61 }
62 } else if (strcasecmp(mode, "text") == 0) {
63 if (NULL == dumpFileName) {
64 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Working in text mode requires a path for the dump file!\n");
65 ret = 1;
66 return;
67 }
138 } 68 }
139 69
140// mi_aux_term *xterm_tty=NULL; 70 cntxt = GNUNET_malloc(sizeof(struct GNUNET_MONKEY_ACTION_Context));
141// 71 cntxt->binary_name = binaryName;
142// /* This is like a file-handle for fopen. 72 result = GNUNET_MONKEY_ACTION_rerun_with_gdb(cntxt);
143// Here we have all the state of gdb "connection". */ 73 switch (result) {
144// mi_h *h; 74 case GDB_STATE_ERROR:
145// 75 break;
146// /* Connect to gdb child. */ 76 case GDB_STATE_EXIT_NORMALLY:
147// h=mi_connect_local(); 77 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Debug with gdb, program exited normally!\n");
148// if (!h) 78 /*FIXME: Valgrind should be launched here */
149// { 79 break;
150// printf("Connect failed\n"); 80 case GDB_STATE_STOPPED:
151// ret = 1; 81 /*FIXME: Expression Database should be inspected here (before writing the report) */
152// return; 82 if(GNUNET_OK != GNUNET_MONKEY_ACTION_format_report(cntxt)){
153// } 83 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Error in generating debug report!\n");
154// printf("Connected to gdb!\n"); 84 ret = 1;
155// 85 }
156// /* Set all callbacks. */ 86 if (strcasecmp(mode, "email") == 0) {
157// mi_set_console_cb(h,cb_console,NULL); 87 if (GNUNET_OK != GNUNET_MONKEY_ACTION_report_email(cntxt)) {
158// mi_set_target_cb(h,cb_target,NULL); 88 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Error sending email!\n");
159// mi_set_log_cb(h,cb_log,NULL); 89 ret = 1;
160// mi_set_async_cb(h,cb_async,NULL); 90 }
161// mi_set_to_gdb_cb(h,cb_to,NULL); 91 } else {
162// mi_set_from_gdb_cb(h,cb_from,NULL); 92 /* text mode */
163// 93 if (GNUNET_OK != GNUNET_MONKEY_ACTION_report_file(cntxt, dumpFileName)) {
164// /* Set the name of the child and the command line aguments. */ 94 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Error in saving debug file!\n");
165// if (!gmi_set_exec(h, binaryName, NULL)) 95 ret = 1;
166// { 96 }
167// printf("Error setting exec y args\n"); 97 }
168// mi_disconnect(h); 98 break;
169// ret = 1; 99 default:
170// return; 100 break;
171// } 101 }
172//
173// /* Tell gdb to attach the child to a terminal. */
174// if (!gmi_target_terminal(h, ttyname(STDIN_FILENO)))
175// {
176// printf("Error selecting target terminal\n");
177// mi_disconnect(h);
178// ret = 1;
179// return;
180// }
181//
182// /* Run the program. */
183// if (!gmi_exec_run(h))
184// {
185// printf("Error in run!\n");
186// mi_disconnect(h);
187// ret = 1;
188// return;
189// }
190// /* Here we should be stopped when the program crashes */
191// if (!wait_for_stop(h))
192// {
193// mi_disconnect(h);
194// ret = 1;
195// return;
196// }
197//
198// /* Continue execution. */
199// if (!gmi_exec_continue(h))
200// {
201// printf("Error in continue!\n");
202// mi_disconnect(h);
203// ret = 1;
204// return;
205// }
206// /* Here we should be terminated. */
207// if (!wait_for_stop(h))
208// {
209// mi_disconnect(h);
210// ret = 1;
211// return;
212// }
213//
214// /* Exit from gdb. */
215// gmi_gdb_exit(h);
216// /* Close the connection. */
217// mi_disconnect(h);
218// /* Wait 5 seconds and close the auxiliar terminal. */
219// printf("Waiting 5 seconds\n");
220// sleep(5);
221// gmi_end_aux_term(xterm_tty);
222} 102}
223 103
224 104
225int main(int argc, char *argv[]) 105int main(int argc, char *argv[])
226{ 106{
227 /*
228 * FIXME:
229 * Command should accept email address to which monkey sends the debugging report.
230 * The email address can also be read from the configuration file.
231 */
232 static const struct GNUNET_GETOPT_CommandLineOption options[] = { 107 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
233 {'m', "mode", NULL, gettext_noop ("monkey's mode of operation: options are \"text\" or \"email\""), 108 {'m', "mode", NULL, gettext_noop ("monkey's mode of operation: options are \"text\" or \"email\""),
234 GNUNET_YES, &GNUNET_GETOPT_set_string, &mode}, 109 GNUNET_YES, &GNUNET_GETOPT_set_string, &mode},
235 {'b', "binary", NULL, gettext_noop ("binary for program to debug with monkey"), 110 {'b', "binary", NULL, gettext_noop ("binary for program to debug with monkey"),
236 GNUNET_YES, &GNUNET_GETOPT_set_string, &binaryName}, 111 GNUNET_YES, &GNUNET_GETOPT_set_string, &binaryName},
237 {'o', "output", NULL, gettext_noop ("path to file to dump monkey's output in case of working in text mode"), 112 {'o', "output", NULL, gettext_noop ("path to file to dump monkey's output in case of text mode"),
238 GNUNET_YES, &GNUNET_GETOPT_set_string, &dumpFileName}, 113 GNUNET_YES, &GNUNET_GETOPT_set_string, &dumpFileName},
114 {'a', "address", NULL, gettext_noop ("address to send email to in case of email mode"),
115 GNUNET_YES, &GNUNET_GETOPT_set_string, &emailAddress},
239 GNUNET_GETOPT_OPTION_END 116 GNUNET_GETOPT_OPTION_END
240 }; 117 };
241 118
diff --git a/src/monkey/gnunet_monkey_action.h b/src/monkey/gnunet_monkey_action.h
index 8430c9f88..93f2dea4e 100644
--- a/src/monkey/gnunet_monkey_action.h
+++ b/src/monkey/gnunet_monkey_action.h
@@ -37,12 +37,22 @@ extern "C"
37#endif 37#endif
38 38
39 39
40/* Debug constants */
41#define DEBUG_MODE_GDB 0
42#define GDB_STATE_STOPPED 1
43#define GDB_STATE_EXIT_NORMALLY 2
44#define GDB_STATE_ERROR 3
45#define DEBUG_MODE_VALGRIND 4
46#define DEBUG_MODE_REPORT_READY 5
47
48
40/** 49/**
41 * Context for the Action API 50 * Context for the Action API
42 */ 51 */
43struct GNUNET_MONKEY_ACTION_Context 52struct GNUNET_MONKEY_ACTION_Context
44{ 53{
45 const char* binary_name; 54 const char* binary_name;
55 const char* email_address;
46 int debug_mode; 56 int debug_mode;
47 char* debug_report; 57 char* debug_report;
48 58
diff --git a/src/monkey/mail_sender.c b/src/monkey/mail_sender.c
index 6279ad984..122a4d0ab 100644
--- a/src/monkey/mail_sender.c
+++ b/src/monkey/mail_sender.c
@@ -144,7 +144,7 @@ print_recipient_status (smtp_recipient_t recipient,
144} 144}
145 145
146 146
147void sendMail(const char *messageContents) 147void sendMail(const char *messageContents, const char *emailAddress)
148{ 148{
149 smtp_session_t session; 149 smtp_session_t session;
150 smtp_message_t message; 150 smtp_message_t message;
@@ -154,7 +154,7 @@ void sendMail(const char *messageContents)
154 char *host = "localhost:25"; 154 char *host = "localhost:25";
155 char *from = "gnunet-monkey"; 155 char *from = "gnunet-monkey";
156 char *subject = "e-mail from Libesmtp!"; 156 char *subject = "e-mail from Libesmtp!";
157 const char *recipient_address = "safey.allah@gmail.com"; 157 const char *recipient_address = emailAddress;
158 char tempFileName[1000]; 158 char tempFileName[1000];
159 int tempFd; 159 int tempFd;
160 FILE *fp; 160 FILE *fp;