Windows x64 full AOT support for mono/mini regression tests.
[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/sgen/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 DEFAULT_PRINT()
114 #define CUSTOM_PRINT(_)
115
116 #define IS_ALWAYS_MATCH(_)
117 #define MATCH_INDEX(_)
118 #define IS_VTABLE_MATCH(_)
119
120 #define END_PROTOCOL_ENTRY
121 #define END_PROTOCOL_ENTRY_FLUSH
122 #define END_PROTOCOL_ENTRY_HEAVY
123
124 #include <mono/sgen/sgen-protocol-def.h>
125
126         default: assert (0);
127         }
128
129         if (size) {
130                 size_t size_read = read_stream (stream, data, size);
131                 g_assert (size_read == size);
132         }
133
134         return (int)type;
135 }
136
137 static gboolean
138 is_always_match (int type)
139 {
140         switch (TYPE (type)) {
141 #define BEGIN_PROTOCOL_ENTRY0(method) \
142         case PROTOCOL_ID(method):
143 #define BEGIN_PROTOCOL_ENTRY1(method,t1,f1) \
144         case PROTOCOL_ID(method):
145 #define BEGIN_PROTOCOL_ENTRY2(method,t1,f1,t2,f2) \
146         case PROTOCOL_ID(method):
147 #define BEGIN_PROTOCOL_ENTRY3(method,t1,f1,t2,f2,t3,f3) \
148         case PROTOCOL_ID(method):
149 #define BEGIN_PROTOCOL_ENTRY4(method,t1,f1,t2,f2,t3,f3,t4,f4) \
150         case PROTOCOL_ID(method):
151 #define BEGIN_PROTOCOL_ENTRY5(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5) \
152         case PROTOCOL_ID(method):
153 #define BEGIN_PROTOCOL_ENTRY6(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5,t6,f6) \
154         case PROTOCOL_ID(method):
155
156 #define BEGIN_PROTOCOL_ENTRY_HEAVY0(method) \
157         BEGIN_PROTOCOL_ENTRY0 (method)
158 #define BEGIN_PROTOCOL_ENTRY_HEAVY1(method,t1,f1) \
159         BEGIN_PROTOCOL_ENTRY1 (method,t1,f1)
160 #define BEGIN_PROTOCOL_ENTRY_HEAVY2(method,t1,f1,t2,f2) \
161         BEGIN_PROTOCOL_ENTRY2 (method,t1,f1,t2,f2)
162 #define BEGIN_PROTOCOL_ENTRY_HEAVY3(method,t1,f1,t2,f2,t3,f3) \
163         BEGIN_PROTOCOL_ENTRY3 (method,t1,f1,t2,f2,t3,f3)
164 #define BEGIN_PROTOCOL_ENTRY_HEAVY4(method,t1,f1,t2,f2,t3,f3,t4,f4) \
165         BEGIN_PROTOCOL_ENTRY4 (method,t1,f1,t2,f2,t3,f3,t4,f4)
166 #define BEGIN_PROTOCOL_ENTRY_HEAVY5(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5) \
167         BEGIN_PROTOCOL_ENTRY5 (method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5)
168 #define BEGIN_PROTOCOL_ENTRY_HEAVY6(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5,t6,f6) \
169         BEGIN_PROTOCOL_ENTRY6 (method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5,t6,f6)
170
171 #define DEFAULT_PRINT()
172 #define CUSTOM_PRINT(_)
173
174 #define IS_ALWAYS_MATCH(is_always_match) \
175                 return is_always_match;
176 #define MATCH_INDEX(_)
177 #define IS_VTABLE_MATCH(_)
178
179 #define END_PROTOCOL_ENTRY
180 #define END_PROTOCOL_ENTRY_FLUSH
181 #define END_PROTOCOL_ENTRY_HEAVY
182
183 #include <mono/sgen/sgen-protocol-def.h>
184
185         default: assert (0);
186         }
187 }
188
189 #define WORKER_PREFIX(t)        (WORKER ((t)) ? "w" : " ")
190
191 enum { NO_COLOR = -1 };
192
193 typedef struct {
194         int type;
195         const char *name;
196         void *data;
197         /* The index of the ANSI color with which to highlight
198          * this entry, or NO_COLOR for no highlighting.
199          */
200         int color;
201 } PrintEntry;
202
203
204 #define TYPE_INT 0
205 #define TYPE_LONGLONG 1
206 #define TYPE_SIZE 2
207 #define TYPE_POINTER 3
208 #define TYPE_BOOL 4
209
210 static void
211 print_entry_content (int entries_size, PrintEntry *entries, gboolean color_output)
212 {
213         int i;
214         for (i = 0; i < entries_size; ++i) {
215                 printf ("%s%s ", i == 0 ? "" : " ", entries [i].name);
216                 if (color_output && entries [i].color != NO_COLOR)
217                         /* Set foreground color, excluding black & white. */
218                         printf ("\x1B[%dm", 31 + (entries [i].color % 6));
219                 switch (entries [i].type) {
220                 case TYPE_INT:
221                         printf ("%d", *(int*) entries [i].data);
222                         break;
223                 case TYPE_LONGLONG:
224                         printf ("%lld", *(long long*) entries [i].data);
225                         break;
226                 case TYPE_SIZE:
227                         printf ("%lu", *(size_t*) entries [i].data);
228                         break;
229                 case TYPE_POINTER:
230                         printf ("%p", *(gpointer*) entries [i].data);
231                         break;
232                 case TYPE_BOOL:
233                         printf ("%s", *(gboolean*) entries [i].data ? "true" : "false");
234                         break;
235                 default:
236                         assert (0);
237                 }
238                 if (color_output && entries [i].color != NO_COLOR)
239                         /* Reset foreground color to default. */
240                         printf ("\x1B[0m");
241         }
242 }
243
244 static int
245 index_color (int index, int num_nums, int *match_indices)
246 {
247         int result;
248         for (result = 0; result < num_nums + 1; ++result)
249                 if (index == match_indices [result])
250                         return result;
251         return NO_COLOR;
252 }
253
254 static void
255 print_entry (int type, void *data, int num_nums, int *match_indices, gboolean color_output)
256 {
257         const char *always_prefix = is_always_match (type) ? "  " : "";
258         printf ("%s%s ", WORKER_PREFIX (type), always_prefix);
259
260         switch (TYPE (type)) {
261
262 #define BEGIN_PROTOCOL_ENTRY0(method) \
263         case PROTOCOL_ID(method): { \
264                 const int pes_size G_GNUC_UNUSED = 0; \
265                 PrintEntry pes [1] G_GNUC_UNUSED; \
266                 printf ("%s", #method + strlen ("binary_protocol_"));
267 #define BEGIN_PROTOCOL_ENTRY1(method,t1,f1) \
268         case PROTOCOL_ID(method): { \
269                 PROTOCOL_STRUCT (method) *entry = data; \
270                 const int pes_size G_GNUC_UNUSED = 1; \
271                 PrintEntry pes [1] G_GNUC_UNUSED; \
272                 pes [0].type = t1; \
273                 pes [0].name = #f1; \
274                 pes [0].data = &entry->f1; \
275                 pes [0].color = index_color(0, num_nums, match_indices); \
276                 printf ("%s ", #method + strlen ("binary_protocol_"));
277 #define BEGIN_PROTOCOL_ENTRY2(method,t1,f1,t2,f2) \
278         case PROTOCOL_ID(method): { \
279                 PROTOCOL_STRUCT (method) *entry = data; \
280                 const int pes_size G_GNUC_UNUSED = 2; \
281                 PrintEntry pes [2] G_GNUC_UNUSED; \
282                 pes [0].type = t1; \
283                 pes [0].name = #f1; \
284                 pes [0].data = &entry->f1; \
285                 pes [0].color = index_color(0, num_nums, match_indices); \
286                 pes [1].type = t2; \
287                 pes [1].name = #f2; \
288                 pes [1].data = &entry->f2; \
289                 pes [1].color = index_color(1, num_nums, match_indices); \
290                 printf ("%s ", #method + strlen ("binary_protocol_"));
291 #define BEGIN_PROTOCOL_ENTRY3(method,t1,f1,t2,f2,t3,f3) \
292         case PROTOCOL_ID(method): { \
293                 PROTOCOL_STRUCT (method) *entry = data; \
294                 const int pes_size G_GNUC_UNUSED = 3; \
295                 PrintEntry pes [3] G_GNUC_UNUSED; \
296                 pes [0].type = t1; \
297                 pes [0].name = #f1; \
298                 pes [0].data = &entry->f1; \
299                 pes [0].color = index_color(0, num_nums, match_indices); \
300                 pes [1].type = t2; \
301                 pes [1].name = #f2; \
302                 pes [1].data = &entry->f2; \
303                 pes [1].color = index_color(1, num_nums, match_indices); \
304                 pes [2].type = t3; \
305                 pes [2].name = #f3; \
306                 pes [2].data = &entry->f3; \
307                 pes [2].color = index_color(2, num_nums, match_indices); \
308                 printf ("%s ", #method + strlen ("binary_protocol_"));
309 #define BEGIN_PROTOCOL_ENTRY4(method,t1,f1,t2,f2,t3,f3,t4,f4) \
310         case PROTOCOL_ID(method): { \
311                 PROTOCOL_STRUCT (method) *entry = data; \
312                 const int pes_size G_GNUC_UNUSED = 4; \
313                 PrintEntry pes [4] G_GNUC_UNUSED; \
314                 pes [0].type = t1; \
315                 pes [0].name = #f1; \
316                 pes [0].data = &entry->f1; \
317                 pes [0].color = index_color(0, num_nums, match_indices); \
318                 pes [1].type = t2; \
319                 pes [1].name = #f2; \
320                 pes [1].data = &entry->f2; \
321                 pes [1].color = index_color(1, num_nums, match_indices); \
322                 pes [2].type = t3; \
323                 pes [2].name = #f3; \
324                 pes [2].data = &entry->f3; \
325                 pes [2].color = index_color(2, num_nums, match_indices); \
326                 pes [3].type = t4; \
327                 pes [3].name = #f4; \
328                 pes [3].data = &entry->f4; \
329                 pes [3].color = index_color(3, num_nums, match_indices); \
330                 printf ("%s ", #method + strlen ("binary_protocol_"));
331 #define BEGIN_PROTOCOL_ENTRY5(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5) \
332         case PROTOCOL_ID(method): { \
333                 PROTOCOL_STRUCT (method) *entry = data; \
334                 const int pes_size G_GNUC_UNUSED = 5; \
335                 PrintEntry pes [5] G_GNUC_UNUSED; \
336                 pes [0].type = t1; \
337                 pes [0].name = #f1; \
338                 pes [0].data = &entry->f1; \
339                 pes [0].color = index_color(0, num_nums, match_indices); \
340                 pes [1].type = t2; \
341                 pes [1].name = #f2; \
342                 pes [1].data = &entry->f2; \
343                 pes [1].color = index_color(1, num_nums, match_indices); \
344                 pes [2].type = t3; \
345                 pes [2].name = #f3; \
346                 pes [2].data = &entry->f3; \
347                 pes [2].color = index_color(2, num_nums, match_indices); \
348                 pes [3].type = t4; \
349                 pes [3].name = #f4; \
350                 pes [3].data = &entry->f4; \
351                 pes [3].color = index_color(3, num_nums, match_indices); \
352                 pes [4].type = t5; \
353                 pes [4].name = #f5; \
354                 pes [4].data = &entry->f5; \
355                 pes [4].color = index_color(4, num_nums, match_indices); \
356                 printf ("%s ", #method + strlen ("binary_protocol_"));
357 #define BEGIN_PROTOCOL_ENTRY6(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5,t6,f6) \
358         case PROTOCOL_ID(method): { \
359                 PROTOCOL_STRUCT (method) *entry = data; \
360                 const int pes_size G_GNUC_UNUSED = 6; \
361                 PrintEntry pes [6] G_GNUC_UNUSED; \
362                 pes [0].type = t1; \
363                 pes [0].name = #f1; \
364                 pes [0].data = &entry->f1; \
365                 pes [0].color = index_color(0, num_nums, match_indices); \
366                 pes [1].type = t2; \
367                 pes [1].name = #f2; \
368                 pes [1].data = &entry->f2; \
369                 pes [1].color = index_color(1, num_nums, match_indices); \
370                 pes [2].type = t3; \
371                 pes [2].name = #f3; \
372                 pes [2].data = &entry->f3; \
373                 pes [2].color = index_color(2, num_nums, match_indices); \
374                 pes [3].type = t4; \
375                 pes [3].name = #f4; \
376                 pes [3].data = &entry->f4; \
377                 pes [3].color = index_color(3, num_nums, match_indices); \
378                 pes [4].type = t5; \
379                 pes [4].name = #f5; \
380                 pes [4].data = &entry->f5; \
381                 pes [4].color = index_color(4, num_nums, match_indices); \
382                 pes [5].type = t6; \
383                 pes [5].name = #f6; \
384                 pes [5].data = &entry->f6; \
385                 pes [5].color = index_color(5, num_nums, match_indices); \
386                 printf ("%s ", #method + strlen ("binary_protocol_"));
387
388 #define BEGIN_PROTOCOL_ENTRY_HEAVY0(method) \
389         BEGIN_PROTOCOL_ENTRY0 (method)
390 #define BEGIN_PROTOCOL_ENTRY_HEAVY1(method,t1,f1) \
391         BEGIN_PROTOCOL_ENTRY1 (method,t1,f1)
392 #define BEGIN_PROTOCOL_ENTRY_HEAVY2(method,t1,f1,t2,f2) \
393         BEGIN_PROTOCOL_ENTRY2 (method,t1,f1,t2,f2)
394 #define BEGIN_PROTOCOL_ENTRY_HEAVY3(method,t1,f1,t2,f2,t3,f3) \
395         BEGIN_PROTOCOL_ENTRY3 (method,t1,f1,t2,f2,t3,f3)
396 #define BEGIN_PROTOCOL_ENTRY_HEAVY4(method,t1,f1,t2,f2,t3,f3,t4,f4) \
397         BEGIN_PROTOCOL_ENTRY4 (method,t1,f1,t2,f2,t3,f3,t4,f4)
398 #define BEGIN_PROTOCOL_ENTRY_HEAVY5(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5) \
399         BEGIN_PROTOCOL_ENTRY5 (method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5)
400 #define BEGIN_PROTOCOL_ENTRY_HEAVY6(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5,t6,f6) \
401         BEGIN_PROTOCOL_ENTRY6 (method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5,t6,f6)
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_FLUSH \
417         END_PROTOCOL_ENTRY
418 #define END_PROTOCOL_ENTRY_HEAVY \
419         END_PROTOCOL_ENTRY
420
421 #include <mono/sgen/sgen-protocol-def.h>
422
423         default: assert (0);
424         }
425 }
426
427 #undef TYPE_INT
428 #undef TYPE_LONGLONG
429 #undef TYPE_SIZE
430 #undef TYPE_POINTER
431
432 #define TYPE_INT int
433 #define TYPE_LONGLONG long long
434 #define TYPE_SIZE size_t
435 #define TYPE_POINTER gpointer
436
437 static gboolean
438 matches_interval (gpointer ptr, gpointer start, int size)
439 {
440         return ptr >= start && (char*)ptr < (char*)start + size;
441 }
442
443 /* Returns the index of the field where a match was found,
444  * BINARY_PROTOCOL_NO_MATCH for no match, or
445  * BINARY_PROTOCOL_MATCH for a match with no index.
446  */
447 static int
448 match_index (gpointer ptr, int type, void *data)
449 {
450         switch (TYPE (type)) {
451
452 #define BEGIN_PROTOCOL_ENTRY0(method) \
453         case PROTOCOL_ID (method): {
454 #define BEGIN_PROTOCOL_ENTRY1(method,t1,f1) \
455         case PROTOCOL_ID (method): { \
456                 PROTOCOL_STRUCT (method) *entry G_GNUC_UNUSED = data;
457 #define BEGIN_PROTOCOL_ENTRY2(method,t1,f1,t2,f2) \
458         case PROTOCOL_ID (method): { \
459                 PROTOCOL_STRUCT (method) *entry G_GNUC_UNUSED = data;
460 #define BEGIN_PROTOCOL_ENTRY3(method,t1,f1,t2,f2,t3,f3) \
461         case PROTOCOL_ID (method): { \
462                 PROTOCOL_STRUCT (method) *entry G_GNUC_UNUSED = data;
463 #define BEGIN_PROTOCOL_ENTRY4(method,t1,f1,t2,f2,t3,f3,t4,f4) \
464         case PROTOCOL_ID (method): { \
465                 PROTOCOL_STRUCT (method) *entry G_GNUC_UNUSED = data;
466 #define BEGIN_PROTOCOL_ENTRY5(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5) \
467         case PROTOCOL_ID (method): { \
468                 PROTOCOL_STRUCT (method) *entry G_GNUC_UNUSED = data;
469 #define BEGIN_PROTOCOL_ENTRY6(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5,t6,f6) \
470         case PROTOCOL_ID (method): { \
471                 PROTOCOL_STRUCT (method) *entry G_GNUC_UNUSED = data;
472
473 #define BEGIN_PROTOCOL_ENTRY_HEAVY0(method) \
474         BEGIN_PROTOCOL_ENTRY0 (method)
475 #define BEGIN_PROTOCOL_ENTRY_HEAVY1(method,t1,f1) \
476         BEGIN_PROTOCOL_ENTRY1 (method,t1,f1)
477 #define BEGIN_PROTOCOL_ENTRY_HEAVY2(method,t1,f1,t2,f2) \
478         BEGIN_PROTOCOL_ENTRY2 (method,t1,f1,t2,f2)
479 #define BEGIN_PROTOCOL_ENTRY_HEAVY3(method,t1,f1,t2,f2,t3,f3) \
480         BEGIN_PROTOCOL_ENTRY3 (method,t1,f1,t2,f2,t3,f3)
481 #define BEGIN_PROTOCOL_ENTRY_HEAVY4(method,t1,f1,t2,f2,t3,f3,t4,f4) \
482         BEGIN_PROTOCOL_ENTRY4 (method,t1,f1,t2,f2,t3,f3,t4,f4)
483 #define BEGIN_PROTOCOL_ENTRY_HEAVY5(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5) \
484         BEGIN_PROTOCOL_ENTRY5 (method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5)
485 #define BEGIN_PROTOCOL_ENTRY_HEAVY6(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5,t6,f6) \
486         BEGIN_PROTOCOL_ENTRY6 (method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5,t6,f6)
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_FLUSH \
500         END_PROTOCOL_ENTRY
501 #define END_PROTOCOL_ENTRY_HEAVY \
502         END_PROTOCOL_ENTRY
503
504 #include <mono/sgen/sgen-protocol-def.h>
505
506         default: assert (0);
507         }
508 }
509
510 static gboolean
511 is_vtable_match (gpointer ptr, int type, void *data)
512 {
513         switch (TYPE (type)) {
514
515 #define BEGIN_PROTOCOL_ENTRY0(method) \
516         case PROTOCOL_ID (method): {
517 #define BEGIN_PROTOCOL_ENTRY1(method,t1,f1) \
518         case PROTOCOL_ID (method): { \
519                 PROTOCOL_STRUCT (method) *entry G_GNUC_UNUSED = data;
520 #define BEGIN_PROTOCOL_ENTRY2(method,t1,f1,t2,f2) \
521         case PROTOCOL_ID (method): { \
522                 PROTOCOL_STRUCT (method) *entry G_GNUC_UNUSED = data;
523 #define BEGIN_PROTOCOL_ENTRY3(method,t1,f1,t2,f2,t3,f3) \
524         case PROTOCOL_ID (method): { \
525                 PROTOCOL_STRUCT (method) *entry G_GNUC_UNUSED = data;
526 #define BEGIN_PROTOCOL_ENTRY4(method,t1,f1,t2,f2,t3,f3,t4,f4) \
527         case PROTOCOL_ID (method): { \
528                 PROTOCOL_STRUCT (method) *entry G_GNUC_UNUSED = data;
529 #define BEGIN_PROTOCOL_ENTRY5(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5) \
530         case PROTOCOL_ID (method): { \
531                 PROTOCOL_STRUCT (method) *entry G_GNUC_UNUSED = data;
532 #define BEGIN_PROTOCOL_ENTRY6(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5,t6,f6) \
533         case PROTOCOL_ID (method): { \
534                 PROTOCOL_STRUCT (method) *entry G_GNUC_UNUSED = data;
535
536 #define BEGIN_PROTOCOL_ENTRY_HEAVY0(method) \
537         BEGIN_PROTOCOL_ENTRY0 (method)
538 #define BEGIN_PROTOCOL_ENTRY_HEAVY1(method,t1,f1) \
539         BEGIN_PROTOCOL_ENTRY1 (method,t1,f1)
540 #define BEGIN_PROTOCOL_ENTRY_HEAVY2(method,t1,f1,t2,f2) \
541         BEGIN_PROTOCOL_ENTRY2 (method,t1,f1,t2,f2)
542 #define BEGIN_PROTOCOL_ENTRY_HEAVY3(method,t1,f1,t2,f2,t3,f3) \
543         BEGIN_PROTOCOL_ENTRY3 (method,t1,f1,t2,f2,t3,f3)
544 #define BEGIN_PROTOCOL_ENTRY_HEAVY4(method,t1,f1,t2,f2,t3,f3,t4,f4) \
545         BEGIN_PROTOCOL_ENTRY4 (method,t1,f1,t2,f2,t3,f3,t4,f4)
546 #define BEGIN_PROTOCOL_ENTRY_HEAVY5(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5) \
547         BEGIN_PROTOCOL_ENTRY5 (method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5)
548 #define BEGIN_PROTOCOL_ENTRY_HEAVY6(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5,t6,f6) \
549         BEGIN_PROTOCOL_ENTRY6 (method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5,t6,f6)
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_FLUSH \
563         END_PROTOCOL_ENTRY
564 #define END_PROTOCOL_ENTRY_HEAVY \
565         END_PROTOCOL_ENTRY
566
567 #include <mono/sgen/sgen-protocol-def.h>
568
569         default: assert (0);
570         }
571 }
572
573 #undef TYPE_INT
574 #undef TYPE_LONGLONG
575 #undef TYPE_SIZE
576 #undef TYPE_POINTER
577
578 int
579 main (int argc, char *argv[])
580 {
581         int type;
582         void *data = g_malloc0 (MAX_ENTRY_SIZE);
583         int num_args = argc - 1;
584         int num_nums = 0;
585         int num_vtables = 0;
586         int i;
587         long nums [num_args];
588         long vtables [num_args];
589         gboolean dump_all = FALSE;
590         gboolean pause_times = FALSE;
591         gboolean pause_times_stopped = FALSE;
592         gboolean pause_times_concurrent = FALSE;
593         gboolean pause_times_finish = FALSE;
594         gboolean color_output = FALSE;
595         long long pause_times_ts = 0;
596         const char *input_path = NULL;
597         int input_file;
598         EntryStream stream;
599         unsigned long long entry_index;
600         unsigned long long first_entry_to_consider = 0;
601
602         for (i = 0; i < num_args; ++i) {
603                 char *arg = argv [i + 1];
604                 char *next_arg = argv [i + 2];
605                 if (!strcmp (arg, "--all")) {
606                         dump_all = TRUE;
607                 } else if (!strcmp (arg, "--pause-times")) {
608                         pause_times = TRUE;
609                 } else if (!strcmp (arg, "-v") || !strcmp (arg, "--vtable")) {
610                         vtables [num_vtables++] = strtoul (next_arg, NULL, 16);
611                         ++i;
612                 } else if (!strcmp (arg, "-s") || !strcmp (arg, "--start-at")) {
613                         first_entry_to_consider = strtoull (next_arg, NULL, 10);
614                         ++i;
615                 } else if (!strcmp (arg, "-c") || !strcmp (arg, "--color")) {
616                         color_output = TRUE;
617                 } else if (!strcmp (arg, "-i") || !strcmp (arg, "--input")) {
618                         input_path = next_arg;
619                         ++i;
620                 } else if (!strcmp (arg, "--help")) {
621                         printf (
622                                 "\n"
623                                 "Usage:\n"
624                                 "\n"
625                                 "\tsgen-grep-binprot [options] [pointer...]\n"
626                                 "\n"
627                                 "Examples:\n"
628                                 "\n"
629                                 "\tsgen-grep-binprot --all </tmp/binprot\n"
630                                 "\tsgen-grep-binprot --input /tmp/binprot --color 0xdeadbeef\n"
631                                 "\n"
632                                 "Options:\n"
633                                 "\n"
634                                 "\t--all                    Print all entries.\n"
635                                 "\t--color, -c              Highlight matches in color.\n"
636                                 "\t--help                   You're looking at it.\n"
637                                 "\t--input FILE, -i FILE    Read input from FILE instead of standard input.\n"
638                                 "\t--pause-times            Print GC pause times.\n"
639                                 "\t--start-at N, -s N       Begin filtering at the Nth entry.\n"
640                                 "\t--vtable PTR, -v PTR     Search for vtable pointer PTR.\n"
641                                 "\n");
642                         return 0;
643                 } else {
644                         nums [num_nums++] = strtoul (arg, NULL, 16);
645                 }
646         }
647
648         if (dump_all)
649                 assert (!pause_times);
650         if (pause_times)
651                 assert (!dump_all);
652
653         input_file = input_path ? open (input_path, O_RDONLY) : STDIN_FILENO;
654         init_stream (&stream, input_file);
655         entry_index = 0;
656         while ((type = read_entry (&stream, data)) != SGEN_PROTOCOL_EOF) {
657                 if (entry_index < first_entry_to_consider)
658                         goto next_entry;
659                 if (pause_times) {
660                         switch (type) {
661                         case PROTOCOL_ID (binary_protocol_world_stopping): {
662                                 PROTOCOL_STRUCT (binary_protocol_world_stopping) *entry = data;
663                                 assert (!pause_times_stopped);
664                                 pause_times_concurrent = FALSE;
665                                 pause_times_finish = FALSE;
666                                 pause_times_ts = entry->timestamp;
667                                 pause_times_stopped = TRUE;
668                                 break;
669                         }
670                         case PROTOCOL_ID (binary_protocol_concurrent_finish):
671                                 pause_times_finish = TRUE;
672                         case PROTOCOL_ID (binary_protocol_concurrent_start):
673                         case PROTOCOL_ID (binary_protocol_concurrent_update):
674                                 pause_times_concurrent = TRUE;
675                                 break;
676                         case PROTOCOL_ID (binary_protocol_world_restarted): {
677                                 PROTOCOL_STRUCT (binary_protocol_world_restarted) *entry = data;
678                                 assert (pause_times_stopped);
679                                 printf ("pause-time %d %d %d %lld %lld\n",
680                                                 entry->generation,
681                                                 pause_times_concurrent,
682                                                 pause_times_finish,
683                                                 entry->timestamp - pause_times_ts,
684                                                 pause_times_ts);
685                                 pause_times_stopped = FALSE;
686                                 break;
687                         }
688                         }
689                 } else {
690                         int match_indices [num_nums + 1];
691                         gboolean match = is_always_match (type);
692                         match_indices [num_nums] = num_nums == 0 ? match_index (NULL, type, data) : BINARY_PROTOCOL_NO_MATCH;
693                         match = match_indices [num_nums] != BINARY_PROTOCOL_NO_MATCH;
694                         for (i = 0; i < num_nums; ++i) {
695                                 match_indices [i] = match_index ((gpointer) nums [i], type, data);
696                                 match = match || match_indices [i] != BINARY_PROTOCOL_NO_MATCH;
697                         }
698                         if (!match) {
699                                 for (i = 0; i < num_vtables; ++i) {
700                                         if (is_vtable_match ((gpointer) vtables [i], type, data)) {
701                                                 match = TRUE;
702                                                 break;
703                                         }
704                                 }
705                         }
706                         if (match || dump_all)
707                                 printf ("%12lld ", entry_index);
708                         if (dump_all)
709                                 printf (match ? "* " : "  ");
710                         if (match || dump_all)
711                                 print_entry (type, data, num_nums, match_indices, color_output);
712                 }
713         next_entry:
714                 ++entry_index;
715         }
716         close_stream (&stream);
717         if (input_path)
718                 close (input_file);
719         g_free (data);
720
721         return 0;
722 }