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