Merge pull request #1659 from alexanderkyte/stringbuilder-referencesource
[mono.git] / tools / sgen / sgen-grep-binprot.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
4 #include <glib.h>
5 #include <unistd.h>
6 #include <fcntl.h>
7
8 #define SGEN_BINARY_PROTOCOL
9 #define MONO_INTERNAL
10
11 #include <mono/metadata/sgen-protocol.h>
12
13 #define SGEN_PROTOCOL_EOF       255
14
15 #define TYPE(t)         ((t) & 0x7f)
16 #define WORKER(t)       ((t) & 0x80)
17
18 #define MAX_ENTRY_SIZE (1 << 10)
19 #define BUFFER_SIZE (1 << 20)
20
21 typedef struct {
22         int file;
23         char *buffer;
24         const char *end;
25         const char *pos;
26 } EntryStream;
27
28 static void
29 init_stream (EntryStream *stream, int file)
30 {
31         stream->file = file;
32         stream->buffer = g_malloc0 (BUFFER_SIZE);
33         stream->end = stream->buffer + BUFFER_SIZE;
34         stream->pos = stream->end;
35 }
36
37 static void
38 close_stream (EntryStream *stream)
39 {
40         g_free (stream->buffer);
41 }
42
43 static gboolean
44 refill_stream (EntryStream *in, size_t size)
45 {
46         size_t remainder = in->end - in->pos;
47         ssize_t refilled;
48         g_assert (size > 0);
49         g_assert (in->pos >= in->buffer);
50         if (in->pos + size <= in->end)
51                 return TRUE;
52         memmove (in->buffer, in->pos, remainder);
53         in->pos = in->buffer;
54         refilled = read (in->file, in->buffer + remainder, BUFFER_SIZE - remainder);
55         if (refilled < 0)
56                 return FALSE;
57         g_assert (refilled + remainder <= BUFFER_SIZE);
58         in->end = in->buffer + refilled + remainder;
59         return in->end - in->buffer >= size;
60 }
61
62 static ssize_t
63 read_stream (EntryStream *stream, void *out, size_t size)
64 {
65         if (refill_stream (stream, size)) {
66                 memcpy (out, stream->pos, size);
67                 stream->pos += size;
68                 return size;
69         }
70         return 0;
71 }
72
73 static int
74 read_entry (EntryStream *stream, void *data)
75 {
76         unsigned char type;
77         ssize_t size;
78
79         if (read_stream (stream, &type, 1) <= 0)
80                 return SGEN_PROTOCOL_EOF;
81         switch (TYPE (type)) {
82
83 #define BEGIN_PROTOCOL_ENTRY0(method) \
84         case PROTOCOL_ID(method): size = 0; break;
85 #define BEGIN_PROTOCOL_ENTRY1(method,t1,f1) \
86         case PROTOCOL_ID(method): size = sizeof (PROTOCOL_STRUCT(method)); break;
87 #define BEGIN_PROTOCOL_ENTRY2(method,t1,f1,t2,f2) \
88         case PROTOCOL_ID(method): size = sizeof (PROTOCOL_STRUCT(method)); break;
89 #define BEGIN_PROTOCOL_ENTRY3(method,t1,f1,t2,f2,t3,f3) \
90         case PROTOCOL_ID(method): size = sizeof (PROTOCOL_STRUCT(method)); break;
91 #define BEGIN_PROTOCOL_ENTRY4(method,t1,f1,t2,f2,t3,f3,t4,f4) \
92         case PROTOCOL_ID(method): size = sizeof (PROTOCOL_STRUCT(method)); break;
93 #define BEGIN_PROTOCOL_ENTRY5(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5) \
94         case PROTOCOL_ID(method): size = sizeof (PROTOCOL_STRUCT(method)); break;
95 #define BEGIN_PROTOCOL_ENTRY6(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5,t6,f6) \
96         case PROTOCOL_ID(method): size = sizeof (PROTOCOL_STRUCT(method)); break;
97
98 #define BEGIN_PROTOCOL_ENTRY_HEAVY0(method) \
99         BEGIN_PROTOCOL_ENTRY0 (method)
100 #define BEGIN_PROTOCOL_ENTRY_HEAVY1(method,t1,f1) \
101         BEGIN_PROTOCOL_ENTRY1 (method,t1,f1)
102 #define BEGIN_PROTOCOL_ENTRY_HEAVY2(method,t1,f1,t2,f2) \
103         BEGIN_PROTOCOL_ENTRY2 (method,t1,f1,t2,f2)
104 #define BEGIN_PROTOCOL_ENTRY_HEAVY3(method,t1,f1,t2,f2,t3,f3) \
105         BEGIN_PROTOCOL_ENTRY3 (method,t1,f1,t2,f2,t3,f3)
106 #define BEGIN_PROTOCOL_ENTRY_HEAVY4(method,t1,f1,t2,f2,t3,f3,t4,f4) \
107         BEGIN_PROTOCOL_ENTRY4 (method,t1,f1,t2,f2,t3,f3,t4,f4)
108 #define BEGIN_PROTOCOL_ENTRY_HEAVY5(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5) \
109         BEGIN_PROTOCOL_ENTRY5 (method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5)
110 #define BEGIN_PROTOCOL_ENTRY_HEAVY6(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5,t6,f6) \
111         BEGIN_PROTOCOL_ENTRY6 (method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5,t6,f6)
112
113 #define FLUSH()
114
115 #define DEFAULT_PRINT()
116 #define CUSTOM_PRINT(_)
117
118 #define IS_ALWAYS_MATCH(_)
119 #define MATCH_INDEX(_)
120 #define IS_VTABLE_MATCH(_)
121
122 #define END_PROTOCOL_ENTRY
123 #define END_PROTOCOL_ENTRY_HEAVY
124
125 #include <mono/metadata/sgen-protocol-def.h>
126
127         default: assert (0);
128         }
129
130         if (size) {
131                 size_t size_read = read_stream (stream, data, size);
132                 g_assert (size_read == size);
133         }
134
135         return (int)type;
136 }
137
138 static gboolean
139 is_always_match (int type)
140 {
141         switch (TYPE (type)) {
142 #define BEGIN_PROTOCOL_ENTRY0(method) \
143         case PROTOCOL_ID(method):
144 #define BEGIN_PROTOCOL_ENTRY1(method,t1,f1) \
145         case PROTOCOL_ID(method):
146 #define BEGIN_PROTOCOL_ENTRY2(method,t1,f1,t2,f2) \
147         case PROTOCOL_ID(method):
148 #define BEGIN_PROTOCOL_ENTRY3(method,t1,f1,t2,f2,t3,f3) \
149         case PROTOCOL_ID(method):
150 #define BEGIN_PROTOCOL_ENTRY4(method,t1,f1,t2,f2,t3,f3,t4,f4) \
151         case PROTOCOL_ID(method):
152 #define BEGIN_PROTOCOL_ENTRY5(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5) \
153         case PROTOCOL_ID(method):
154 #define BEGIN_PROTOCOL_ENTRY6(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5,t6,f6) \
155         case PROTOCOL_ID(method):
156
157 #define BEGIN_PROTOCOL_ENTRY_HEAVY0(method) \
158         BEGIN_PROTOCOL_ENTRY0 (method)
159 #define BEGIN_PROTOCOL_ENTRY_HEAVY1(method,t1,f1) \
160         BEGIN_PROTOCOL_ENTRY1 (method,t1,f1)
161 #define BEGIN_PROTOCOL_ENTRY_HEAVY2(method,t1,f1,t2,f2) \
162         BEGIN_PROTOCOL_ENTRY2 (method,t1,f1,t2,f2)
163 #define BEGIN_PROTOCOL_ENTRY_HEAVY3(method,t1,f1,t2,f2,t3,f3) \
164         BEGIN_PROTOCOL_ENTRY3 (method,t1,f1,t2,f2,t3,f3)
165 #define BEGIN_PROTOCOL_ENTRY_HEAVY4(method,t1,f1,t2,f2,t3,f3,t4,f4) \
166         BEGIN_PROTOCOL_ENTRY4 (method,t1,f1,t2,f2,t3,f3,t4,f4)
167 #define BEGIN_PROTOCOL_ENTRY_HEAVY5(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5) \
168         BEGIN_PROTOCOL_ENTRY5 (method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5)
169 #define BEGIN_PROTOCOL_ENTRY_HEAVY6(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5,t6,f6) \
170         BEGIN_PROTOCOL_ENTRY6 (method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5,t6,f6)
171
172 #define FLUSH()
173
174 #define DEFAULT_PRINT()
175 #define CUSTOM_PRINT(_)
176
177 #define IS_ALWAYS_MATCH(is_always_match) \
178                 return is_always_match;
179 #define MATCH_INDEX(_)
180 #define IS_VTABLE_MATCH(_)
181
182 #define END_PROTOCOL_ENTRY
183 #define END_PROTOCOL_ENTRY_HEAVY
184
185 #include <mono/metadata/sgen-protocol-def.h>
186
187         default: assert (0);
188         }
189 }
190
191 #define WORKER_PREFIX(t)        (WORKER ((t)) ? "w" : " ")
192
193 enum { NO_COLOR = -1 };
194
195 typedef struct {
196         int type;
197         const char *name;
198         void *data;
199         /* The index of the ANSI color with which to highlight
200          * this entry, or NO_COLOR for no highlighting.
201          */
202         int color;
203 } PrintEntry;
204
205
206 #define TYPE_INT 0
207 #define TYPE_LONGLONG 1
208 #define TYPE_SIZE 2
209 #define TYPE_POINTER 3
210
211 static void
212 print_entry_content (int entries_size, PrintEntry *entries, gboolean color_output)
213 {
214         int i;
215         for (i = 0; i < entries_size; ++i) {
216                 printf ("%s%s ", i == 0 ? "" : " ", entries [i].name);
217                 if (color_output && entries [i].color != NO_COLOR)
218                         /* Set foreground color, excluding black & white. */
219                         printf ("\x1B[%dm", 31 + (entries [i].color % 6));
220                 switch (entries [i].type) {
221                 case TYPE_INT:
222                         printf ("%d", *(int*) entries [i].data);
223                         break;
224                 case TYPE_LONGLONG:
225                         printf ("%lld", *(long long*) entries [i].data);
226                         break;
227                 case TYPE_SIZE:
228                         printf ("%lu", *(size_t*) entries [i].data);
229                         break;
230                 case TYPE_POINTER:
231                         printf ("%p", *(gpointer*) entries [i].data);
232                         break;
233                 default:
234                         assert (0);
235                 }
236                 if (color_output && entries [i].color != NO_COLOR)
237                         /* Reset foreground color to default. */
238                         printf ("\x1B[0m");
239         }
240 }
241
242 static int
243 index_color (int index, int num_nums, int *match_indices)
244 {
245         int result;
246         for (result = 0; result < num_nums + 1; ++result)
247                 if (index == match_indices [result])
248                         return result;
249         return NO_COLOR;
250 }
251
252 static void
253 print_entry (int type, void *data, int num_nums, int *match_indices, gboolean color_output)
254 {
255         const char *always_prefix = is_always_match (type) ? "  " : "";
256         printf ("%s%s ", WORKER_PREFIX (type), always_prefix);
257
258         switch (TYPE (type)) {
259
260 #define BEGIN_PROTOCOL_ENTRY0(method) \
261         case PROTOCOL_ID(method): { \
262                 const int pes_size G_GNUC_UNUSED = 0; \
263                 PrintEntry pes [1] G_GNUC_UNUSED; \
264                 printf ("%s", #method + strlen ("binary_protocol_"));
265 #define BEGIN_PROTOCOL_ENTRY1(method,t1,f1) \
266         case PROTOCOL_ID(method): { \
267                 PROTOCOL_STRUCT (method) *entry = data; \
268                 const int pes_size G_GNUC_UNUSED = 1; \
269                 PrintEntry pes [1] G_GNUC_UNUSED; \
270                 pes [0].type = t1; \
271                 pes [0].name = #f1; \
272                 pes [0].data = &entry->f1; \
273                 pes [0].color = index_color(0, num_nums, match_indices); \
274                 printf ("%s ", #method + strlen ("binary_protocol_"));
275 #define BEGIN_PROTOCOL_ENTRY2(method,t1,f1,t2,f2) \
276         case PROTOCOL_ID(method): { \
277                 PROTOCOL_STRUCT (method) *entry = data; \
278                 const int pes_size G_GNUC_UNUSED = 2; \
279                 PrintEntry pes [2] G_GNUC_UNUSED; \
280                 pes [0].type = t1; \
281                 pes [0].name = #f1; \
282                 pes [0].data = &entry->f1; \
283                 pes [0].color = index_color(0, num_nums, match_indices); \
284                 pes [1].type = t2; \
285                 pes [1].name = #f2; \
286                 pes [1].data = &entry->f2; \
287                 pes [1].color = index_color(1, num_nums, match_indices); \
288                 printf ("%s ", #method + strlen ("binary_protocol_"));
289 #define BEGIN_PROTOCOL_ENTRY3(method,t1,f1,t2,f2,t3,f3) \
290         case PROTOCOL_ID(method): { \
291                 PROTOCOL_STRUCT (method) *entry = data; \
292                 const int pes_size G_GNUC_UNUSED = 3; \
293                 PrintEntry pes [3] G_GNUC_UNUSED; \
294                 pes [0].type = t1; \
295                 pes [0].name = #f1; \
296                 pes [0].data = &entry->f1; \
297                 pes [0].color = index_color(0, num_nums, match_indices); \
298                 pes [1].type = t2; \
299                 pes [1].name = #f2; \
300                 pes [1].data = &entry->f2; \
301                 pes [1].color = index_color(1, num_nums, match_indices); \
302                 pes [2].type = t3; \
303                 pes [2].name = #f3; \
304                 pes [2].data = &entry->f3; \
305                 pes [2].color = index_color(2, num_nums, match_indices); \
306                 printf ("%s ", #method + strlen ("binary_protocol_"));
307 #define BEGIN_PROTOCOL_ENTRY4(method,t1,f1,t2,f2,t3,f3,t4,f4) \
308         case PROTOCOL_ID(method): { \
309                 PROTOCOL_STRUCT (method) *entry = data; \
310                 const int pes_size G_GNUC_UNUSED = 4; \
311                 PrintEntry pes [4] G_GNUC_UNUSED; \
312                 pes [0].type = t1; \
313                 pes [0].name = #f1; \
314                 pes [0].data = &entry->f1; \
315                 pes [0].color = index_color(0, num_nums, match_indices); \
316                 pes [1].type = t2; \
317                 pes [1].name = #f2; \
318                 pes [1].data = &entry->f2; \
319                 pes [1].color = index_color(1, num_nums, match_indices); \
320                 pes [2].type = t3; \
321                 pes [2].name = #f3; \
322                 pes [2].data = &entry->f3; \
323                 pes [2].color = index_color(2, num_nums, match_indices); \
324                 pes [3].type = t4; \
325                 pes [3].name = #f4; \
326                 pes [3].data = &entry->f4; \
327                 pes [3].color = index_color(3, num_nums, match_indices); \
328                 printf ("%s ", #method + strlen ("binary_protocol_"));
329 #define BEGIN_PROTOCOL_ENTRY5(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5) \
330         case PROTOCOL_ID(method): { \
331                 PROTOCOL_STRUCT (method) *entry = data; \
332                 const int pes_size G_GNUC_UNUSED = 5; \
333                 PrintEntry pes [5] G_GNUC_UNUSED; \
334                 pes [0].type = t1; \
335                 pes [0].name = #f1; \
336                 pes [0].data = &entry->f1; \
337                 pes [0].color = index_color(0, num_nums, match_indices); \
338                 pes [1].type = t2; \
339                 pes [1].name = #f2; \
340                 pes [1].data = &entry->f2; \
341                 pes [1].color = index_color(1, num_nums, match_indices); \
342                 pes [2].type = t3; \
343                 pes [2].name = #f3; \
344                 pes [2].data = &entry->f3; \
345                 pes [2].color = index_color(2, num_nums, match_indices); \
346                 pes [3].type = t4; \
347                 pes [3].name = #f4; \
348                 pes [3].data = &entry->f4; \
349                 pes [3].color = index_color(3, num_nums, match_indices); \
350                 pes [4].type = t5; \
351                 pes [4].name = #f5; \
352                 pes [4].data = &entry->f5; \
353                 pes [4].color = index_color(4, num_nums, match_indices); \
354                 printf ("%s ", #method + strlen ("binary_protocol_"));
355 #define BEGIN_PROTOCOL_ENTRY6(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5,t6,f6) \
356         case PROTOCOL_ID(method): { \
357                 PROTOCOL_STRUCT (method) *entry = data; \
358                 const int pes_size G_GNUC_UNUSED = 6; \
359                 PrintEntry pes [6] G_GNUC_UNUSED; \
360                 pes [0].type = t1; \
361                 pes [0].name = #f1; \
362                 pes [0].data = &entry->f1; \
363                 pes [0].color = index_color(0, num_nums, match_indices); \
364                 pes [1].type = t2; \
365                 pes [1].name = #f2; \
366                 pes [1].data = &entry->f2; \
367                 pes [1].color = index_color(1, num_nums, match_indices); \
368                 pes [2].type = t3; \
369                 pes [2].name = #f3; \
370                 pes [2].data = &entry->f3; \
371                 pes [2].color = index_color(2, num_nums, match_indices); \
372                 pes [3].type = t4; \
373                 pes [3].name = #f4; \
374                 pes [3].data = &entry->f4; \
375                 pes [3].color = index_color(3, num_nums, match_indices); \
376                 pes [4].type = t5; \
377                 pes [4].name = #f5; \
378                 pes [4].data = &entry->f5; \
379                 pes [4].color = index_color(4, num_nums, match_indices); \
380                 pes [5].type = t6; \
381                 pes [5].name = #f6; \
382                 pes [5].data = &entry->f6; \
383                 pes [5].color = index_color(5, num_nums, match_indices); \
384                 printf ("%s ", #method + strlen ("binary_protocol_"));
385
386 #define BEGIN_PROTOCOL_ENTRY_HEAVY0(method) \
387         BEGIN_PROTOCOL_ENTRY0 (method)
388 #define BEGIN_PROTOCOL_ENTRY_HEAVY1(method,t1,f1) \
389         BEGIN_PROTOCOL_ENTRY1 (method,t1,f1)
390 #define BEGIN_PROTOCOL_ENTRY_HEAVY2(method,t1,f1,t2,f2) \
391         BEGIN_PROTOCOL_ENTRY2 (method,t1,f1,t2,f2)
392 #define BEGIN_PROTOCOL_ENTRY_HEAVY3(method,t1,f1,t2,f2,t3,f3) \
393         BEGIN_PROTOCOL_ENTRY3 (method,t1,f1,t2,f2,t3,f3)
394 #define BEGIN_PROTOCOL_ENTRY_HEAVY4(method,t1,f1,t2,f2,t3,f3,t4,f4) \
395         BEGIN_PROTOCOL_ENTRY4 (method,t1,f1,t2,f2,t3,f3,t4,f4)
396 #define BEGIN_PROTOCOL_ENTRY_HEAVY5(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5) \
397         BEGIN_PROTOCOL_ENTRY5 (method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5)
398 #define BEGIN_PROTOCOL_ENTRY_HEAVY6(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5,t6,f6) \
399         BEGIN_PROTOCOL_ENTRY6 (method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5,t6,f6)
400
401 #define FLUSH()
402
403 #define DEFAULT_PRINT() \
404         print_entry_content (pes_size, pes, color_output);
405 #define CUSTOM_PRINT(print) \
406         print;
407
408 #define IS_ALWAYS_MATCH(_)
409 #define MATCH_INDEX(_)
410 #define IS_VTABLE_MATCH(_)
411
412 #define END_PROTOCOL_ENTRY \
413                 printf ("\n"); \
414                 break; \
415         }
416 #define END_PROTOCOL_ENTRY_HEAVY \
417         END_PROTOCOL_ENTRY
418
419 #include <mono/metadata/sgen-protocol-def.h>
420
421         default: assert (0);
422         }
423 }
424
425 #undef TYPE_INT
426 #undef TYPE_LONGLONG
427 #undef TYPE_SIZE
428 #undef TYPE_POINTER
429
430 #define TYPE_INT int
431 #define TYPE_LONGLONG long long
432 #define TYPE_SIZE size_t
433 #define TYPE_POINTER gpointer
434
435 static gboolean
436 matches_interval (gpointer ptr, gpointer start, int size)
437 {
438         return ptr >= start && (char*)ptr < (char*)start + size;
439 }
440
441 /* Returns the index of the field where a match was found,
442  * BINARY_PROTOCOL_NO_MATCH for no match, or
443  * BINARY_PROTOCOL_MATCH for a match with no index.
444  */
445 static int
446 match_index (gpointer ptr, int type, void *data)
447 {
448         switch (TYPE (type)) {
449
450 #define BEGIN_PROTOCOL_ENTRY0(method) \
451         case PROTOCOL_ID (method): {
452 #define BEGIN_PROTOCOL_ENTRY1(method,t1,f1) \
453         case PROTOCOL_ID (method): { \
454                 PROTOCOL_STRUCT (method) *entry G_GNUC_UNUSED = data;
455 #define BEGIN_PROTOCOL_ENTRY2(method,t1,f1,t2,f2) \
456         case PROTOCOL_ID (method): { \
457                 PROTOCOL_STRUCT (method) *entry G_GNUC_UNUSED = data;
458 #define BEGIN_PROTOCOL_ENTRY3(method,t1,f1,t2,f2,t3,f3) \
459         case PROTOCOL_ID (method): { \
460                 PROTOCOL_STRUCT (method) *entry G_GNUC_UNUSED = data;
461 #define BEGIN_PROTOCOL_ENTRY4(method,t1,f1,t2,f2,t3,f3,t4,f4) \
462         case PROTOCOL_ID (method): { \
463                 PROTOCOL_STRUCT (method) *entry G_GNUC_UNUSED = data;
464 #define BEGIN_PROTOCOL_ENTRY5(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5) \
465         case PROTOCOL_ID (method): { \
466                 PROTOCOL_STRUCT (method) *entry G_GNUC_UNUSED = data;
467 #define BEGIN_PROTOCOL_ENTRY6(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5,t6,f6) \
468         case PROTOCOL_ID (method): { \
469                 PROTOCOL_STRUCT (method) *entry G_GNUC_UNUSED = data;
470
471 #define BEGIN_PROTOCOL_ENTRY_HEAVY0(method) \
472         BEGIN_PROTOCOL_ENTRY0 (method)
473 #define BEGIN_PROTOCOL_ENTRY_HEAVY1(method,t1,f1) \
474         BEGIN_PROTOCOL_ENTRY1 (method,t1,f1)
475 #define BEGIN_PROTOCOL_ENTRY_HEAVY2(method,t1,f1,t2,f2) \
476         BEGIN_PROTOCOL_ENTRY2 (method,t1,f1,t2,f2)
477 #define BEGIN_PROTOCOL_ENTRY_HEAVY3(method,t1,f1,t2,f2,t3,f3) \
478         BEGIN_PROTOCOL_ENTRY3 (method,t1,f1,t2,f2,t3,f3)
479 #define BEGIN_PROTOCOL_ENTRY_HEAVY4(method,t1,f1,t2,f2,t3,f3,t4,f4) \
480         BEGIN_PROTOCOL_ENTRY4 (method,t1,f1,t2,f2,t3,f3,t4,f4)
481 #define BEGIN_PROTOCOL_ENTRY_HEAVY5(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5) \
482         BEGIN_PROTOCOL_ENTRY5 (method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5)
483 #define BEGIN_PROTOCOL_ENTRY_HEAVY6(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5,t6,f6) \
484         BEGIN_PROTOCOL_ENTRY6 (method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5,t6,f6)
485
486 #define FLUSH()
487
488 #define DEFAULT_PRINT()
489 #define CUSTOM_PRINT(_)
490
491 #define IS_ALWAYS_MATCH(_)
492 #define MATCH_INDEX(block) \
493                 return (block);
494 #define IS_VTABLE_MATCH(_)
495
496 #define END_PROTOCOL_ENTRY \
497                 break; \
498         }
499 #define END_PROTOCOL_ENTRY_HEAVY \
500         END_PROTOCOL_ENTRY
501
502 #include <mono/metadata/sgen-protocol-def.h>
503
504         default: assert (0);
505         }
506 }
507
508 static gboolean
509 is_vtable_match (gpointer ptr, int type, void *data)
510 {
511         switch (TYPE (type)) {
512
513 #define BEGIN_PROTOCOL_ENTRY0(method) \
514         case PROTOCOL_ID (method): {
515 #define BEGIN_PROTOCOL_ENTRY1(method,t1,f1) \
516         case PROTOCOL_ID (method): { \
517                 PROTOCOL_STRUCT (method) *entry G_GNUC_UNUSED = data;
518 #define BEGIN_PROTOCOL_ENTRY2(method,t1,f1,t2,f2) \
519         case PROTOCOL_ID (method): { \
520                 PROTOCOL_STRUCT (method) *entry G_GNUC_UNUSED = data;
521 #define BEGIN_PROTOCOL_ENTRY3(method,t1,f1,t2,f2,t3,f3) \
522         case PROTOCOL_ID (method): { \
523                 PROTOCOL_STRUCT (method) *entry G_GNUC_UNUSED = data;
524 #define BEGIN_PROTOCOL_ENTRY4(method,t1,f1,t2,f2,t3,f3,t4,f4) \
525         case PROTOCOL_ID (method): { \
526                 PROTOCOL_STRUCT (method) *entry G_GNUC_UNUSED = data;
527 #define BEGIN_PROTOCOL_ENTRY5(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5) \
528         case PROTOCOL_ID (method): { \
529                 PROTOCOL_STRUCT (method) *entry G_GNUC_UNUSED = data;
530 #define BEGIN_PROTOCOL_ENTRY6(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5,t6,f6) \
531         case PROTOCOL_ID (method): { \
532                 PROTOCOL_STRUCT (method) *entry G_GNUC_UNUSED = data;
533
534 #define BEGIN_PROTOCOL_ENTRY_HEAVY0(method) \
535         BEGIN_PROTOCOL_ENTRY0 (method)
536 #define BEGIN_PROTOCOL_ENTRY_HEAVY1(method,t1,f1) \
537         BEGIN_PROTOCOL_ENTRY1 (method,t1,f1)
538 #define BEGIN_PROTOCOL_ENTRY_HEAVY2(method,t1,f1,t2,f2) \
539         BEGIN_PROTOCOL_ENTRY2 (method,t1,f1,t2,f2)
540 #define BEGIN_PROTOCOL_ENTRY_HEAVY3(method,t1,f1,t2,f2,t3,f3) \
541         BEGIN_PROTOCOL_ENTRY3 (method,t1,f1,t2,f2,t3,f3)
542 #define BEGIN_PROTOCOL_ENTRY_HEAVY4(method,t1,f1,t2,f2,t3,f3,t4,f4) \
543         BEGIN_PROTOCOL_ENTRY4 (method,t1,f1,t2,f2,t3,f3,t4,f4)
544 #define BEGIN_PROTOCOL_ENTRY_HEAVY5(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5) \
545         BEGIN_PROTOCOL_ENTRY5 (method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5)
546 #define BEGIN_PROTOCOL_ENTRY_HEAVY6(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5,t6,f6) \
547         BEGIN_PROTOCOL_ENTRY6 (method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5,t6,f6)
548
549 #define FLUSH()
550
551 #define DEFAULT_PRINT()
552 #define CUSTOM_PRINT(_)
553
554 #define IS_ALWAYS_MATCH(_)
555 #define MATCH_INDEX(block) \
556                 return (block);
557 #define IS_VTABLE_MATCH(_)
558
559 #define END_PROTOCOL_ENTRY \
560                 break; \
561         }
562 #define END_PROTOCOL_ENTRY_HEAVY \
563         END_PROTOCOL_ENTRY
564
565 #include <mono/metadata/sgen-protocol-def.h>
566
567         default: assert (0);
568         }
569 }
570
571 #undef TYPE_INT
572 #undef TYPE_LONGLONG
573 #undef TYPE_SIZE
574 #undef TYPE_POINTER
575
576 int
577 main (int argc, char *argv[])
578 {
579         int type;
580         void *data = g_malloc0 (MAX_ENTRY_SIZE);
581         int num_args = argc - 1;
582         int num_nums = 0;
583         int num_vtables = 0;
584         int i;
585         long nums [num_args];
586         long vtables [num_args];
587         gboolean dump_all = FALSE;
588         gboolean pause_times = FALSE;
589         gboolean pause_times_stopped = FALSE;
590         gboolean pause_times_concurrent = FALSE;
591         gboolean pause_times_finish = FALSE;
592         gboolean color_output = FALSE;
593         long long pause_times_ts = 0;
594         const char *input_path = NULL;
595         int input_file;
596         EntryStream stream;
597         unsigned long long entry_index;
598         unsigned long long first_entry_to_consider = 0;
599
600         for (i = 0; i < num_args; ++i) {
601                 char *arg = argv [i + 1];
602                 char *next_arg = argv [i + 2];
603                 if (!strcmp (arg, "--all")) {
604                         dump_all = TRUE;
605                 } else if (!strcmp (arg, "--pause-times")) {
606                         pause_times = TRUE;
607                 } else if (!strcmp (arg, "-v") || !strcmp (arg, "--vtable")) {
608                         vtables [num_vtables++] = strtoul (next_arg, NULL, 16);
609                         ++i;
610                 } else if (!strcmp (arg, "-s") || !strcmp (arg, "--start-at")) {
611                         first_entry_to_consider = strtoull (next_arg, NULL, 10);
612                         ++i;
613                 } else if (!strcmp (arg, "-c") || !strcmp (arg, "--color")) {
614                         color_output = TRUE;
615                 } else if (!strcmp (arg, "-i") || !strcmp (arg, "--input")) {
616                         input_path = next_arg;
617                         ++i;
618                 } else {
619                         nums [num_nums++] = strtoul (arg, NULL, 16);
620                 }
621         }
622
623         if (dump_all)
624                 assert (!pause_times);
625         if (pause_times)
626                 assert (!dump_all);
627
628         input_file = input_path ? open (input_path, O_RDONLY) : STDIN_FILENO;
629         init_stream (&stream, input_file);
630         entry_index = 0;
631         while ((type = read_entry (&stream, data)) != SGEN_PROTOCOL_EOF) {
632                 if (entry_index < first_entry_to_consider)
633                         goto next_entry;
634                 if (pause_times) {
635                         switch (type) {
636                         case PROTOCOL_ID (binary_protocol_world_stopping): {
637                                 PROTOCOL_STRUCT (binary_protocol_world_stopping) *entry = data;
638                                 assert (!pause_times_stopped);
639                                 pause_times_concurrent = FALSE;
640                                 pause_times_finish = FALSE;
641                                 pause_times_ts = entry->timestamp;
642                                 pause_times_stopped = TRUE;
643                                 break;
644                         }
645                         case PROTOCOL_ID (binary_protocol_concurrent_finish):
646                                 pause_times_finish = TRUE;
647                         case PROTOCOL_ID (binary_protocol_concurrent_start):
648                         case PROTOCOL_ID (binary_protocol_concurrent_update):
649                                 pause_times_concurrent = TRUE;
650                                 break;
651                         case PROTOCOL_ID (binary_protocol_world_restarted): {
652                                 PROTOCOL_STRUCT (binary_protocol_world_restarted) *entry = data;
653                                 assert (pause_times_stopped);
654                                 printf ("pause-time %d %d %d %lld %lld\n",
655                                                 entry->generation,
656                                                 pause_times_concurrent,
657                                                 pause_times_finish,
658                                                 entry->timestamp - pause_times_ts,
659                                                 pause_times_ts);
660                                 pause_times_stopped = FALSE;
661                                 break;
662                         }
663                         }
664                 } else {
665                         int match_indices [num_nums + 1];
666                         gboolean match = is_always_match (type);
667                         match_indices [num_nums] = num_nums == 0 ? match_index (NULL, type, data) : BINARY_PROTOCOL_NO_MATCH;
668                         match = match_indices [num_nums] != BINARY_PROTOCOL_NO_MATCH;
669                         for (i = 0; i < num_nums; ++i) {
670                                 match_indices [i] = match_index ((gpointer) nums [i], type, data);
671                                 match = match || match_indices [i] != BINARY_PROTOCOL_NO_MATCH;
672                         }
673                         if (!match) {
674                                 for (i = 0; i < num_vtables; ++i) {
675                                         if (is_vtable_match ((gpointer) vtables [i], type, data)) {
676                                                 match = TRUE;
677                                                 break;
678                                         }
679                                 }
680                         }
681                         if (match || dump_all)
682                                 printf ("%12lld ", entry_index);
683                         if (dump_all)
684                                 printf (match ? "* " : "  ");
685                         if (match || dump_all)
686                                 print_entry (type, data, num_nums, match_indices, color_output);
687                 }
688         next_entry:
689                 ++entry_index;
690         }
691         close_stream (&stream);
692         if (input_path)
693                 close (input_file);
694         g_free (data);
695
696         return 0;
697 }