Simplified notification messages
[oweals/gnunet.git] / src / monkey / gnunet-monkey.c
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 */
20
21 /**
22  * @file monkey/gnunet-monkey.c
23  * @brief Monkey: gnunet automated debugging tool
24  */
25
26 #include <stdio.h>
27 #include "platform.h"
28 #include "gnunet_common.h"
29 #include "gnunet_getopt_lib.h"
30 #include "gnunet_program_lib.h"
31 #include "gnunet_monkey_action.h"
32
33 static const char* mode;
34 static const char* dumpFileName;
35 static const char* binaryName;
36 static const char* emailAddress;
37 static int ret = 0;
38
39 /**
40  * Main function that will launch the action api.
41  *
42  * @param cls closure
43  * @param args remaining command-line arguments
44  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
45  * @param c configuration
46  */
47 static void
48 run (void *cls,
49      char *const *args,
50      const char *cfgfile,
51      const struct GNUNET_CONFIGURATION_Handle *c)
52 {
53         int result;
54         struct GNUNET_MONKEY_ACTION_Context* cntxt;
55
56         if (strcasecmp(mode, "email") == 0) {
57                 if (NULL == emailAddress) {
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                 }
68         }
69
70         cntxt = GNUNET_malloc(sizeof(struct GNUNET_MONKEY_ACTION_Context));
71         cntxt->binary_name = binaryName;
72         result = GNUNET_MONKEY_ACTION_rerun_with_gdb(cntxt);
73         switch (result) {
74         case GDB_STATE_ERROR:
75                 break;
76         case GDB_STATE_EXIT_NORMALLY:
77                 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Debug with gdb, program exited normally!\n");
78                 /*FIXME: Valgrind should be launched here */
79                 break;
80         case GDB_STATE_STOPPED:
81                 /*FIXME: Expression Database should be inspected here (before writing the report) */
82                 if(GNUNET_OK != GNUNET_MONKEY_ACTION_format_report(cntxt)){
83                         GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Error in generating debug report!\n");
84                         ret = 1;
85                 }
86                 if (strcasecmp(mode, "email") == 0) {
87                         if (GNUNET_OK != GNUNET_MONKEY_ACTION_report_email(cntxt)) {
88                                 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Error sending email!\n");
89                                 ret = 1;
90                         }
91                 } else {
92                         /* text mode */
93                         if (GNUNET_OK != GNUNET_MONKEY_ACTION_report_file(cntxt, dumpFileName)) {
94                                 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Error in saving debug file!\n");
95                                 ret = 1;
96                         }
97                 }
98                 break;
99         default:
100                 break;
101         }
102 }
103
104
105 int main(int argc, char *argv[])
106 {
107  static const struct GNUNET_GETOPT_CommandLineOption options[] = {
108      {'m', "mode", NULL, gettext_noop ("monkey's mode of operation: options are \"text\" or \"email\""),
109       GNUNET_YES, &GNUNET_GETOPT_set_string, &mode},
110      {'b', "binary", NULL, gettext_noop ("binary for program to debug with monkey"),
111       GNUNET_YES, &GNUNET_GETOPT_set_string, &binaryName},
112      {'o', "output", NULL, gettext_noop ("path to file to dump monkey's output in case of text mode"),
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},
116       GNUNET_GETOPT_OPTION_END
117    };
118  
119  if (argc < 2) {
120          printf("%s", "Monkey should take arguments: Use --help to get a list of options.\n");
121          return 1;
122  }
123  
124  if (GNUNET_OK == GNUNET_PROGRAM_run (argc,
125                        argv,
126                        "gnunet-monkey",
127                        gettext_noop
128                        ("Automatically debug a service"),
129                        options, &run, NULL))
130      {
131        return ret;
132      }
133
134      return 1;
135 }