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