[profiler] More options reworking/refactoring.
[mono.git] / mono / profiler / log-args.c
1 #include <config.h>
2 #include <mono/utils/mono-logger-internals.h>
3 #include <mono/utils/mono-proclib.h>
4 #include "log.h"
5
6 #ifdef HAVE_UNISTD_H
7 #include <unistd.h>
8 #endif
9
10 typedef struct {
11         const char *event_name;
12         const int mask;
13 } NameAndMask;
14
15 static NameAndMask event_list[] = {
16         { "exception", PROFLOG_EXCEPTION_EVENTS },
17         { "monitor", PROFLOG_MONITOR_EVENTS },
18         { "gc", PROFLOG_GC_EVENTS },
19         { "gcalloc", PROFLOG_GC_ALLOCATION_EVENTS },
20         { "gcmove", PROFLOG_GC_MOVE_EVENTS },
21         { "gcroot", PROFLOG_GC_ROOT_EVENTS },
22         { "gchandle", PROFLOG_GC_HANDLE_EVENTS },
23         { "finalization", PROFLOG_FINALIZATION_EVENTS },
24         { "counter", PROFLOG_COUNTER_EVENTS },
25         { "jit", PROFLOG_JIT_EVENTS },
26
27         { "alloc", PROFLOG_ALLOC_ALIAS },
28         { "legacy", PROFLOG_LEGACY_ALIAS },
29 };
30
31 static void usage (void);
32 static void set_hsmode (ProfilerConfig *config, const char* val);
33 static void set_sample_freq (ProfilerConfig *config, const char *val);
34
35 static gboolean
36 match_option (const char *arg, const char *opt_name, const char **rval)
37 {
38         if (rval) {
39                 const char *end = strchr (arg, '=');
40
41                 *rval = NULL;
42                 if (!end)
43                         return !strcmp (arg, opt_name);
44
45                 if (strncmp (arg, opt_name, strlen (opt_name)) || (end - arg) > strlen (opt_name) + 1)
46                         return FALSE;
47                 *rval = end + 1;
48                 return TRUE;
49         } else {
50                 //FIXME how should we handle passing a value to an arg that doesn't expect it?
51                 return !strcmp (arg, opt_name);
52         }
53 }
54
55 static void
56 parse_arg (const char *arg, ProfilerConfig *config)
57 {
58         const char *val;
59
60         if (match_option (arg, "help", NULL)) {
61                 usage ();
62         } else if (match_option (arg, "report", NULL)) {
63                 config->do_report = TRUE;
64         } else if (match_option (arg, "debug", NULL)) {
65                 config->do_debug = TRUE;
66         } else if (match_option (arg, "heapshot", &val)) {
67                 set_hsmode (config, val);
68                 if (config->hs_mode != MONO_PROFILER_HEAPSHOT_NONE)
69                         config->enable_mask |= PROFLOG_HEAPSHOT_ALIAS;
70         } else if (match_option (arg, "sample", &val)) {
71                 set_sample_freq (config, val);
72                 config->sampling_mode = MONO_PROFILER_SAMPLE_MODE_PROCESS;
73                 config->enable_mask |= PROFLOG_SAMPLE_EVENTS;
74         } else if (match_option (arg, "sample-real", &val)) {
75                 set_sample_freq (config, val);
76                 config->sampling_mode = MONO_PROFILER_SAMPLE_MODE_REAL;
77                 config->enable_mask |= PROFLOG_SAMPLE_EVENTS;
78         } else if (match_option (arg, "calls", NULL)) {
79                 config->enter_leave = TRUE;
80         } else if (match_option (arg, "coverage", NULL)) {
81                 config->collect_coverage = TRUE;
82         } else if (match_option (arg, "zip", NULL)) {
83                 config->use_zip = TRUE;
84         } else if (match_option (arg, "output", &val)) {
85                 config->output_filename = g_strdup (val);
86         } else if (match_option (arg, "port", &val)) {
87                 char *end;
88                 config->command_port = strtoul (val, &end, 10);
89         } else if (match_option (arg, "maxframes", &val)) {
90                 char *end;
91                 int num_frames = strtoul (val, &end, 10);
92                 if (num_frames > MAX_FRAMES)
93                         num_frames = MAX_FRAMES;
94                 config->num_frames = num_frames;
95         } else if (match_option (arg, "maxsamples", &val)) {
96                 char *end;
97                 int max_samples = strtoul (val, &end, 10);
98                 if (max_samples)
99                         config->max_allocated_sample_hits = max_samples;
100         } else if (match_option (arg, "calldepth", &val)) {
101                 char *end;
102                 config->max_call_depth = strtoul (val, &end, 10);
103         } else if (match_option (arg, "covfilter-file", &val)) {
104                 if (config->cov_filter_files == NULL)
105                         config->cov_filter_files = g_ptr_array_new ();
106                 g_ptr_array_add (config->cov_filter_files, g_strdup (val));
107         } else {
108                 int i;
109
110                 for (i = 0; i < G_N_ELEMENTS (event_list); ++i){
111                         if (!strcmp (arg, event_list [i].event_name)) {
112                                 config->enable_mask |= event_list [i].mask;
113                                 break;
114                         } else if (!strncmp (arg, "no", 2) && !strcmp (arg + 2, event_list [i].event_name)) {
115                                 config->disable_mask |= event_list [i].mask;
116                                 break;
117                         }
118                 }
119
120                 if (i == G_N_ELEMENTS (event_list))
121                         mono_profiler_printf_err ("Could not parse argument: %s", arg);
122         }
123 }
124
125 static void
126 load_args_from_env_or_default (ProfilerConfig *config)
127 {
128         //XXX change this to header constants
129
130         config->max_allocated_sample_hits = mono_cpu_count () * 1000;
131         config->sampling_mode = MONO_PROFILER_SAMPLE_MODE_NONE;
132         config->sample_freq = 100;
133         config->max_call_depth = 100;
134         config->num_frames = MAX_FRAMES;
135 }
136
137
138 void
139 proflog_parse_args (ProfilerConfig *config, const char *desc)
140 {
141         const char *p;
142         gboolean in_quotes = FALSE;
143         char quote_char = '\0';
144         char *buffer = malloc (strlen (desc));
145         int buffer_pos = 0;
146
147         load_args_from_env_or_default (config);
148
149         for (p = desc; *p; p++){
150                 switch (*p){
151                 case ',':
152                         if (!in_quotes) {
153                                 if (buffer_pos != 0){
154                                         buffer [buffer_pos] = 0;
155                                         parse_arg (buffer, config);
156                                         buffer_pos = 0;
157                                 }
158                         } else {
159                                 buffer [buffer_pos++] = *p;
160                         }
161                         break;
162
163                 case '\\':
164                         if (p [1]) {
165                                 buffer [buffer_pos++] = p[1];
166                                 p++;
167                         }
168                         break;
169                 case '\'':
170                 case '"':
171                         if (in_quotes) {
172                                 if (quote_char == *p)
173                                         in_quotes = FALSE;
174                                 else
175                                         buffer [buffer_pos++] = *p;
176                         } else {
177                                 in_quotes = TRUE;
178                                 quote_char = *p;
179                         }
180                         break;
181                 default:
182                         buffer [buffer_pos++] = *p;
183                         break;
184                 }
185         }
186                 
187         if (buffer_pos != 0) {
188                 buffer [buffer_pos] = 0;
189                 parse_arg (buffer, config);
190         }
191
192         g_free (buffer);
193
194         //Compure config effective mask
195         config->effective_mask = config->enable_mask & ~config->disable_mask;
196 }
197
198 static void
199 set_hsmode (ProfilerConfig *config, const char* val)
200 {
201         if (!val) {
202                 config->hs_mode = MONO_PROFILER_HEAPSHOT_MAJOR;
203                 return;
204         }
205
206         if (strcmp (val, "ondemand") == 0) {
207                 config->hs_mode = MONO_PROFILER_HEAPSHOT_ON_DEMAND;
208                 return;
209         }
210
211         char *end;
212
213         unsigned int count = strtoul (val, &end, 10);
214
215         if (val == end) {
216                 usage ();
217                 return;
218         }
219
220         if (strcmp (end, "ms") == 0) {
221                 config->hs_mode = MONO_PROFILER_HEAPSHOT_X_MS;
222                 config->hs_freq_ms = count;
223         } else if (strcmp (end, "gc") == 0) {
224                 config->hs_mode = MONO_PROFILER_HEAPSHOT_X_GC;
225                 config->hs_freq_gc = count;
226         } else
227                 usage ();
228 }
229
230 static void
231 set_sample_freq (ProfilerConfig *config, const char *val)
232 {
233         if (!val)
234                 return;
235
236         char *end;
237
238         int freq = strtoul (val, &end, 10);
239
240         if (val == end) {
241                 usage ();
242                 return;
243         }
244
245         config->sample_freq = freq;
246 }
247
248 static void
249 usage (void)
250 {
251         mono_profiler_printf ("Mono log profiler version %d.%d (format: %d)", LOG_VERSION_MAJOR, LOG_VERSION_MINOR, LOG_DATA_VERSION);
252         mono_profiler_printf ("Usage: mono --profile=log[:OPTION1[,OPTION2...]] program.exe\n");
253         mono_profiler_printf ("Options:");
254         mono_profiler_printf ("\thelp                 show this usage info");
255         mono_profiler_printf ("\t[no]'EVENT'          enable/disable an individual profiling event");
256         mono_profiler_printf ("\t                     valid EVENT values:");
257
258         for (int i = 0; i < G_N_ELEMENTS (event_list); i++)
259                 mono_profiler_printf ("\t                         %s", event_list [i].event_name);
260
261         mono_profiler_printf ("\t[no]alloc            enable/disable recording allocation info");
262         mono_profiler_printf ("\t[no]legacy           enable/disable pre mono 5.4 default profiler events");
263         mono_profiler_printf ("\tsample[-real][=FREQ] enable/disable statistical sampling of threads");
264         mono_profiler_printf ("\t                     FREQ in Hz, 100 by default");
265         mono_profiler_printf ("\t                     the -real variant uses wall clock time instead of process time");
266         mono_profiler_printf ("\theapshot[=MODE]      record heapshot info (by default at each major collection)");
267         mono_profiler_printf ("\t                     MODE: every XXms milliseconds, every YYgc collections, ondemand");
268         mono_profiler_printf ("\tcalls                enable recording enter/leave method events (very heavy)");
269         mono_profiler_printf ("\tcoverage             enable collection of code coverage data");
270         mono_profiler_printf ("\tcovfilter=ASSEMBLY   add ASSEMBLY to the code coverage filters");
271         mono_profiler_printf ("\t                     prefix a + to include the assembly or a - to exclude it");
272         mono_profiler_printf ("\t                     e.g. covfilter=-mscorlib");
273         mono_profiler_printf ("\tcovfilter-file=FILE  use FILE to generate the list of assemblies to be filtered");
274         mono_profiler_printf ("\tmaxframes=NUM        collect up to NUM stack frames");
275         mono_profiler_printf ("\tcalldepth=NUM        ignore method events for call chain depth bigger than NUM");
276         mono_profiler_printf ("\toutput=FILENAME      write the data to file FILENAME (the file is always overwritten)");
277         mono_profiler_printf ("\toutput=+FILENAME     write the data to file FILENAME.pid (the file is always overwritten)");
278         mono_profiler_printf ("\toutput=|PROGRAM      write the data to the stdin of PROGRAM");
279         mono_profiler_printf ("\t                     %%t is substituted with date and time, %%p with the pid");
280         mono_profiler_printf ("\treport               create a report instead of writing the raw data to a file");
281         mono_profiler_printf ("\tzip                  compress the output data");
282         mono_profiler_printf ("\tport=PORTNUM         use PORTNUM for the listening command server");
283 }