Merge pull request #5234 from vkargov/vk-kiokunegi
[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_GC_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, "heapshot-on-shutdown", NULL)) {
71                 config->hs_on_shutdown = TRUE;
72                 config->enable_mask |= PROFLOG_HEAPSHOT_ALIAS;
73         } else if (match_option (arg, "sample", &val)) {
74                 set_sample_freq (config, val);
75                 config->sampling_mode = MONO_PROFILER_SAMPLE_MODE_PROCESS;
76                 config->enable_mask |= PROFLOG_SAMPLE_EVENTS;
77         } else if (match_option (arg, "sample-real", &val)) {
78                 set_sample_freq (config, val);
79                 config->sampling_mode = MONO_PROFILER_SAMPLE_MODE_REAL;
80                 config->enable_mask |= PROFLOG_SAMPLE_EVENTS;
81         } else if (match_option (arg, "calls", NULL)) {
82                 config->enter_leave = TRUE;
83         } else if (match_option (arg, "coverage", NULL)) {
84                 config->collect_coverage = TRUE;
85         } else if (match_option (arg, "zip", NULL)) {
86                 config->use_zip = TRUE;
87         } else if (match_option (arg, "output", &val)) {
88                 config->output_filename = g_strdup (val);
89         } else if (match_option (arg, "port", &val)) {
90                 char *end;
91                 config->command_port = strtoul (val, &end, 10);
92         } else if (match_option (arg, "maxframes", &val)) {
93                 char *end;
94                 int num_frames = strtoul (val, &end, 10);
95                 if (num_frames > MAX_FRAMES)
96                         num_frames = MAX_FRAMES;
97                 config->num_frames = num_frames;
98         } else if (match_option (arg, "maxsamples", &val)) {
99                 char *end;
100                 int max_samples = strtoul (val, &end, 10);
101                 if (max_samples)
102                         config->max_allocated_sample_hits = max_samples;
103         } else if (match_option (arg, "calldepth", &val)) {
104                 char *end;
105                 config->max_call_depth = strtoul (val, &end, 10);
106         } else if (match_option (arg, "covfilter-file", &val)) {
107                 if (config->cov_filter_files == NULL)
108                         config->cov_filter_files = g_ptr_array_new ();
109                 g_ptr_array_add (config->cov_filter_files, g_strdup (val));
110         } else {
111                 int i;
112
113                 for (i = 0; i < G_N_ELEMENTS (event_list); ++i){
114                         if (!strcmp (arg, event_list [i].event_name)) {
115                                 config->enable_mask |= event_list [i].mask;
116                                 break;
117                         } else if (!strncmp (arg, "no", 2) && !strcmp (arg + 2, event_list [i].event_name)) {
118                                 config->disable_mask |= event_list [i].mask;
119                                 break;
120                         }
121                 }
122
123                 if (i == G_N_ELEMENTS (event_list))
124                         mono_profiler_printf_err ("Could not parse argument: %s", arg);
125         }
126 }
127
128 static void
129 load_args_from_env_or_default (ProfilerConfig *config)
130 {
131         //XXX change this to header constants
132
133         config->max_allocated_sample_hits = mono_cpu_count () * 1000;
134         config->sampling_mode = MONO_PROFILER_SAMPLE_MODE_NONE;
135         config->sample_freq = 100;
136         config->max_call_depth = 100;
137         config->num_frames = MAX_FRAMES;
138 }
139
140
141 void
142 proflog_parse_args (ProfilerConfig *config, const char *desc)
143 {
144         const char *p;
145         gboolean in_quotes = FALSE;
146         char quote_char = '\0';
147         char *buffer = malloc (strlen (desc));
148         int buffer_pos = 0;
149
150         load_args_from_env_or_default (config);
151
152         for (p = desc; *p; p++){
153                 switch (*p){
154                 case ',':
155                         if (!in_quotes) {
156                                 if (buffer_pos != 0){
157                                         buffer [buffer_pos] = 0;
158                                         parse_arg (buffer, config);
159                                         buffer_pos = 0;
160                                 }
161                         } else {
162                                 buffer [buffer_pos++] = *p;
163                         }
164                         break;
165
166                 case '\\':
167                         if (p [1]) {
168                                 buffer [buffer_pos++] = p[1];
169                                 p++;
170                         }
171                         break;
172                 case '\'':
173                 case '"':
174                         if (in_quotes) {
175                                 if (quote_char == *p)
176                                         in_quotes = FALSE;
177                                 else
178                                         buffer [buffer_pos++] = *p;
179                         } else {
180                                 in_quotes = TRUE;
181                                 quote_char = *p;
182                         }
183                         break;
184                 default:
185                         buffer [buffer_pos++] = *p;
186                         break;
187                 }
188         }
189                 
190         if (buffer_pos != 0) {
191                 buffer [buffer_pos] = 0;
192                 parse_arg (buffer, config);
193         }
194
195         g_free (buffer);
196
197         //Compure config effective mask
198         config->effective_mask = config->enable_mask & ~config->disable_mask;
199 }
200
201 static void
202 set_hsmode (ProfilerConfig *config, const char* val)
203 {
204         if (!val) {
205                 config->hs_mode = MONO_PROFILER_HEAPSHOT_MAJOR;
206                 return;
207         }
208
209         if (strcmp (val, "ondemand") == 0) {
210                 config->hs_mode = MONO_PROFILER_HEAPSHOT_ON_DEMAND;
211                 return;
212         }
213
214         char *end;
215
216         unsigned int count = strtoul (val, &end, 10);
217
218         if (val == end) {
219                 usage ();
220                 return;
221         }
222
223         if (strcmp (end, "ms") == 0) {
224                 config->hs_mode = MONO_PROFILER_HEAPSHOT_X_MS;
225                 config->hs_freq_ms = count;
226         } else if (strcmp (end, "gc") == 0) {
227                 config->hs_mode = MONO_PROFILER_HEAPSHOT_X_GC;
228                 config->hs_freq_gc = count;
229         } else
230                 usage ();
231 }
232
233 static void
234 set_sample_freq (ProfilerConfig *config, const char *val)
235 {
236         if (!val)
237                 return;
238
239         char *end;
240
241         int freq = strtoul (val, &end, 10);
242
243         if (val == end) {
244                 usage ();
245                 return;
246         }
247
248         config->sample_freq = freq;
249 }
250
251 static void
252 usage (void)
253 {
254         mono_profiler_printf ("Mono log profiler version %d.%d (format: %d)", LOG_VERSION_MAJOR, LOG_VERSION_MINOR, LOG_DATA_VERSION);
255         mono_profiler_printf ("Usage: mono --profile=log[:OPTION1[,OPTION2...]] program.exe\n");
256         mono_profiler_printf ("Options:");
257         mono_profiler_printf ("\thelp                 show this usage info");
258         mono_profiler_printf ("\t[no]'EVENT'          enable/disable an individual profiling event");
259         mono_profiler_printf ("\t                     valid EVENT values:");
260
261         for (int i = 0; i < G_N_ELEMENTS (event_list); i++)
262                 mono_profiler_printf ("\t                         %s", event_list [i].event_name);
263
264         mono_profiler_printf ("\t[no]alloc            enable/disable recording allocation info");
265         mono_profiler_printf ("\t[no]legacy           enable/disable pre mono 5.4 default profiler events");
266         mono_profiler_printf ("\tsample[-real][=FREQ] enable/disable statistical sampling of threads");
267         mono_profiler_printf ("\t                     FREQ in Hz, 100 by default");
268         mono_profiler_printf ("\t                     the -real variant uses wall clock time instead of process time");
269         mono_profiler_printf ("\theapshot[=MODE]      record heapshot info (by default at each major collection)");
270         mono_profiler_printf ("\t                     MODE: every XXms milliseconds, every YYgc collections, ondemand");
271         mono_profiler_printf ("\theapshot-on-shutdown do a heapshot on runtime shutdown");
272         mono_profiler_printf ("\t                     this option is independent of the above option");
273         mono_profiler_printf ("\tcalls                enable recording enter/leave method events (very heavy)");
274         mono_profiler_printf ("\tcoverage             enable collection of code coverage data");
275         mono_profiler_printf ("\tcovfilter=ASSEMBLY   add ASSEMBLY to the code coverage filters");
276         mono_profiler_printf ("\t                     prefix a + to include the assembly or a - to exclude it");
277         mono_profiler_printf ("\t                     e.g. covfilter=-mscorlib");
278         mono_profiler_printf ("\tcovfilter-file=FILE  use FILE to generate the list of assemblies to be filtered");
279         mono_profiler_printf ("\tmaxframes=NUM        collect up to NUM stack frames");
280         mono_profiler_printf ("\tcalldepth=NUM        ignore method events for call chain depth bigger than NUM");
281         mono_profiler_printf ("\toutput=FILENAME      write the data to file FILENAME (the file is always overwritten)");
282         mono_profiler_printf ("\toutput=+FILENAME     write the data to file FILENAME.pid (the file is always overwritten)");
283         mono_profiler_printf ("\toutput=|PROGRAM      write the data to the stdin of PROGRAM");
284         mono_profiler_printf ("\t                     %%t is substituted with date and time, %%p with the pid");
285         mono_profiler_printf ("\treport               create a report instead of writing the raw data to a file");
286         mono_profiler_printf ("\tzip                  compress the output data");
287         mono_profiler_printf ("\tport=PORTNUM         use PORTNUM for the listening command server");
288 }