[sgen] Fix heavy binary protocol build (#17037)
[mono-project.git] / tools / sgen / sgen-grep-binprot.c
bloba3b4fb29d47f74dbf61cd8d5e03af414ec12c522
1 /*
2 * sgen-grep-binprot.c: Platform specific binary protocol entries reader
4 * Copyright (C) 2016 Xamarin Inc
6 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
7 */
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"
21 static int file_version = 0;
23 #ifdef BINPROT_HAS_HEADER
24 #define PACKED_SUFFIX p
25 #else
26 #define PROTOCOL_STRUCT_ATTR
27 #define PACKED_SUFFIX
28 #endif
30 #ifndef BINPROT_SIZEOF_VOID_P
31 #define BINPROT_SIZEOF_VOID_P SIZEOF_VOID_P
32 #define ARCH_SUFFIX
33 #endif
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>
54 #define SGEN_PROTOCOL_EOF 255
56 #define TYPE(t) ((t) & 0x7f)
57 #define WORKER(t) ((t) & 0x80)
59 #define MAX_ENTRY_SIZE (1 << 10)
61 static int
62 read_entry (EntryStream *stream, void *data, unsigned char *windex)
64 unsigned char type;
65 ssize_t size;
67 if (read_stream (stream, &type, 1) <= 0)
68 return SGEN_PROTOCOL_EOF;
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));
79 switch (TYPE (type)) {
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;
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)
111 #define DEFAULT_PRINT()
112 #define CUSTOM_PRINT(_)
114 #define IS_ALWAYS_MATCH(_)
115 #define MATCH_INDEX(_)
116 #define IS_VTABLE_MATCH(_)
118 #define END_PROTOCOL_ENTRY
119 #define END_PROTOCOL_ENTRY_FLUSH
120 #define END_PROTOCOL_ENTRY_HEAVY
122 #include <mono/sgen/sgen-protocol-def.h>
124 default: assert (0);
127 if (size) {
128 size_t size_read = read_stream (stream, data, size);
129 g_assert (size_read == size);
132 return (int)type;
135 static gboolean
136 is_always_match (int type)
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):
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)
169 #define DEFAULT_PRINT()
170 #define CUSTOM_PRINT(_)
172 #define IS_ALWAYS_MATCH(is_always_match) \
173 return is_always_match;
174 #define MATCH_INDEX(_)
175 #define IS_VTABLE_MATCH(_)
177 #define END_PROTOCOL_ENTRY
178 #define END_PROTOCOL_ENTRY_FLUSH
179 #define END_PROTOCOL_ENTRY_HEAVY
181 #include <mono/sgen/sgen-protocol-def.h>
183 default:
184 assert (0);
185 return FALSE;
189 enum { NO_COLOR = -1 };
191 typedef struct {
192 int type;
193 const char *name;
194 void *data;
195 /* The index of the ANSI color with which to highlight
196 * this entry, or NO_COLOR for no highlighting.
198 int color;
199 } PrintEntry;
202 #define TYPE_INT 0
203 #define TYPE_LONGLONG 1
204 #define TYPE_SIZE 2
205 #define TYPE_POINTER 3
206 #define TYPE_BOOL 4
208 static void
209 print_entry_content (int entries_size, PrintEntry *entries, gboolean color_output)
211 int i;
212 for (i = 0; i < entries_size; ++i) {
213 printf ("%s%s ", i == 0 ? "" : " ", entries [i].name);
214 if (color_output && entries [i].color != NO_COLOR)
215 /* Set foreground color, excluding black & white. */
216 printf ("\x1B[%dm", 31 + (entries [i].color % 6));
217 switch (entries [i].type) {
218 case TYPE_INT:
219 printf ("%d", *(int*) entries [i].data);
220 break;
221 case TYPE_LONGLONG:
222 printf ("%lld", *(long long*) entries [i].data);
223 break;
224 case TYPE_SIZE:
225 printf ("%" MWORD_FORMAT_SPEC_D, *(mword*) entries [i].data);
226 break;
227 case TYPE_POINTER:
228 printf ("0x%" MWORD_FORMAT_SPEC_P, *(mword*) entries [i].data);
229 break;
230 case TYPE_BOOL:
231 printf ("%s", *(gboolean*) entries [i].data ? "true" : "false");
232 break;
233 default:
234 assert (0);
236 if (color_output && entries [i].color != NO_COLOR)
237 /* Reset foreground color to default. */
238 printf ("\x1B[0m");
242 static int
243 index_color (int index, int num_nums, int *match_indices)
245 int result;
246 for (result = 0; result < num_nums + 1; ++result)
247 if (index == match_indices [result])
248 return result;
249 return NO_COLOR;
252 static void
253 print_entry (int type, void *data, int num_nums, int *match_indices, gboolean color_output, unsigned char worker_index)
255 const char *always_prefix = is_always_match (type) ? " " : "";
256 if (worker_index)
257 printf ("w%-2d%s ", worker_index, always_prefix);
258 else
259 printf (" %s ", always_prefix);
261 switch (TYPE (type)) {
263 #define BEGIN_PROTOCOL_ENTRY0(method) \
264 case PROTOCOL_ID(method): { \
265 const int pes_size G_GNUC_UNUSED = 0; \
266 PrintEntry pes [1] G_GNUC_UNUSED; \
267 printf ("%s", &#method [sizeof ("binary_protocol_") - 1]);
268 #define BEGIN_PROTOCOL_ENTRY1(method,t1,f1) \
269 case PROTOCOL_ID(method): { \
270 PROTOCOL_STRUCT (method) *entry = (PROTOCOL_STRUCT (method)*)data; \
271 const int pes_size G_GNUC_UNUSED = 1; \
272 PrintEntry pes [1] G_GNUC_UNUSED; \
273 pes [0].type = t1; \
274 pes [0].name = #f1; \
275 pes [0].data = &entry->f1; \
276 pes [0].color = index_color(0, num_nums, match_indices); \
277 printf ("%s ", #method + strlen ("binary_protocol_"));
278 #define BEGIN_PROTOCOL_ENTRY2(method,t1,f1,t2,f2) \
279 case PROTOCOL_ID(method): { \
280 PROTOCOL_STRUCT (method) *entry = (PROTOCOL_STRUCT (method)*)data; \
281 const int pes_size G_GNUC_UNUSED = 2; \
282 PrintEntry pes [2] G_GNUC_UNUSED; \
283 pes [0].type = t1; \
284 pes [0].name = #f1; \
285 pes [0].data = &entry->f1; \
286 pes [0].color = index_color(0, num_nums, match_indices); \
287 pes [1].type = t2; \
288 pes [1].name = #f2; \
289 pes [1].data = &entry->f2; \
290 pes [1].color = index_color(1, num_nums, match_indices); \
291 printf ("%s ", #method + strlen ("binary_protocol_"));
292 #define BEGIN_PROTOCOL_ENTRY3(method,t1,f1,t2,f2,t3,f3) \
293 case PROTOCOL_ID(method): { \
294 PROTOCOL_STRUCT (method) *entry = (PROTOCOL_STRUCT (method)*)data; \
295 const int pes_size G_GNUC_UNUSED = 3; \
296 PrintEntry pes [3] G_GNUC_UNUSED; \
297 pes [0].type = t1; \
298 pes [0].name = #f1; \
299 pes [0].data = &entry->f1; \
300 pes [0].color = index_color(0, num_nums, match_indices); \
301 pes [1].type = t2; \
302 pes [1].name = #f2; \
303 pes [1].data = &entry->f2; \
304 pes [1].color = index_color(1, num_nums, match_indices); \
305 pes [2].type = t3; \
306 pes [2].name = #f3; \
307 pes [2].data = &entry->f3; \
308 pes [2].color = index_color(2, num_nums, match_indices); \
309 printf ("%s ", #method + strlen ("binary_protocol_"));
310 #define BEGIN_PROTOCOL_ENTRY4(method,t1,f1,t2,f2,t3,f3,t4,f4) \
311 case PROTOCOL_ID(method): { \
312 PROTOCOL_STRUCT (method) *entry = (PROTOCOL_STRUCT (method)*)data; \
313 const int pes_size G_GNUC_UNUSED = 4; \
314 PrintEntry pes [4] G_GNUC_UNUSED; \
315 pes [0].type = t1; \
316 pes [0].name = #f1; \
317 pes [0].data = &entry->f1; \
318 pes [0].color = index_color(0, num_nums, match_indices); \
319 pes [1].type = t2; \
320 pes [1].name = #f2; \
321 pes [1].data = &entry->f2; \
322 pes [1].color = index_color(1, num_nums, match_indices); \
323 pes [2].type = t3; \
324 pes [2].name = #f3; \
325 pes [2].data = &entry->f3; \
326 pes [2].color = index_color(2, num_nums, match_indices); \
327 pes [3].type = t4; \
328 pes [3].name = #f4; \
329 pes [3].data = &entry->f4; \
330 pes [3].color = index_color(3, num_nums, match_indices); \
331 printf ("%s ", #method + strlen ("binary_protocol_"));
332 #define BEGIN_PROTOCOL_ENTRY5(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5) \
333 case PROTOCOL_ID(method): { \
334 PROTOCOL_STRUCT (method) *entry = (PROTOCOL_STRUCT (method)*)data; \
335 const int pes_size G_GNUC_UNUSED = 5; \
336 PrintEntry pes [5] G_GNUC_UNUSED; \
337 pes [0].type = t1; \
338 pes [0].name = #f1; \
339 pes [0].data = &entry->f1; \
340 pes [0].color = index_color(0, num_nums, match_indices); \
341 pes [1].type = t2; \
342 pes [1].name = #f2; \
343 pes [1].data = &entry->f2; \
344 pes [1].color = index_color(1, num_nums, match_indices); \
345 pes [2].type = t3; \
346 pes [2].name = #f3; \
347 pes [2].data = &entry->f3; \
348 pes [2].color = index_color(2, num_nums, match_indices); \
349 pes [3].type = t4; \
350 pes [3].name = #f4; \
351 pes [3].data = &entry->f4; \
352 pes [3].color = index_color(3, num_nums, match_indices); \
353 pes [4].type = t5; \
354 pes [4].name = #f5; \
355 pes [4].data = &entry->f5; \
356 pes [4].color = index_color(4, num_nums, match_indices); \
357 printf ("%s ", #method + strlen ("binary_protocol_"));
358 #define BEGIN_PROTOCOL_ENTRY6(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5,t6,f6) \
359 case PROTOCOL_ID(method): { \
360 PROTOCOL_STRUCT (method) *entry = (PROTOCOL_STRUCT (method)*)data; \
361 const int pes_size G_GNUC_UNUSED = 6; \
362 PrintEntry pes [6] G_GNUC_UNUSED; \
363 pes [0].type = t1; \
364 pes [0].name = #f1; \
365 pes [0].data = &entry->f1; \
366 pes [0].color = index_color(0, num_nums, match_indices); \
367 pes [1].type = t2; \
368 pes [1].name = #f2; \
369 pes [1].data = &entry->f2; \
370 pes [1].color = index_color(1, num_nums, match_indices); \
371 pes [2].type = t3; \
372 pes [2].name = #f3; \
373 pes [2].data = &entry->f3; \
374 pes [2].color = index_color(2, num_nums, match_indices); \
375 pes [3].type = t4; \
376 pes [3].name = #f4; \
377 pes [3].data = &entry->f4; \
378 pes [3].color = index_color(3, num_nums, match_indices); \
379 pes [4].type = t5; \
380 pes [4].name = #f5; \
381 pes [4].data = &entry->f5; \
382 pes [4].color = index_color(4, num_nums, match_indices); \
383 pes [5].type = t6; \
384 pes [5].name = #f6; \
385 pes [5].data = &entry->f6; \
386 pes [5].color = index_color(5, num_nums, match_indices); \
387 printf ("%s ", #method + strlen ("binary_protocol_"));
389 #define BEGIN_PROTOCOL_ENTRY_HEAVY0(method) \
390 BEGIN_PROTOCOL_ENTRY0 (method)
391 #define BEGIN_PROTOCOL_ENTRY_HEAVY1(method,t1,f1) \
392 BEGIN_PROTOCOL_ENTRY1 (method,t1,f1)
393 #define BEGIN_PROTOCOL_ENTRY_HEAVY2(method,t1,f1,t2,f2) \
394 BEGIN_PROTOCOL_ENTRY2 (method,t1,f1,t2,f2)
395 #define BEGIN_PROTOCOL_ENTRY_HEAVY3(method,t1,f1,t2,f2,t3,f3) \
396 BEGIN_PROTOCOL_ENTRY3 (method,t1,f1,t2,f2,t3,f3)
397 #define BEGIN_PROTOCOL_ENTRY_HEAVY4(method,t1,f1,t2,f2,t3,f3,t4,f4) \
398 BEGIN_PROTOCOL_ENTRY4 (method,t1,f1,t2,f2,t3,f3,t4,f4)
399 #define BEGIN_PROTOCOL_ENTRY_HEAVY5(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5) \
400 BEGIN_PROTOCOL_ENTRY5 (method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5)
401 #define BEGIN_PROTOCOL_ENTRY_HEAVY6(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5,t6,f6) \
402 BEGIN_PROTOCOL_ENTRY6 (method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5,t6,f6)
404 #define DEFAULT_PRINT() \
405 print_entry_content (pes_size, pes, color_output);
406 #define CUSTOM_PRINT(print) \
407 print;
409 #define IS_ALWAYS_MATCH(_)
410 #define MATCH_INDEX(_)
411 #define IS_VTABLE_MATCH(_)
413 #define END_PROTOCOL_ENTRY \
414 printf ("\n"); \
415 break; \
417 #define END_PROTOCOL_ENTRY_FLUSH \
418 END_PROTOCOL_ENTRY
419 #define END_PROTOCOL_ENTRY_HEAVY \
420 END_PROTOCOL_ENTRY
422 #include <mono/sgen/sgen-protocol-def.h>
424 default: assert (0);
428 #undef TYPE_INT
429 #undef TYPE_LONGLONG
430 #undef TYPE_SIZE
431 #undef TYPE_POINTER
433 #define TYPE_INT int
434 #define TYPE_LONGLONG long long
435 #define TYPE_SIZE mword
436 #define TYPE_POINTER mword
438 static gboolean
439 matches_interval (mword ptr, mword start, int size)
441 return ptr >= start && ptr < start + size;
444 /* Returns the index of the field where a match was found,
445 * BINARY_PROTOCOL_NO_MATCH for no match, or
446 * BINARY_PROTOCOL_MATCH for a match with no index.
448 static int
449 match_index (mword ptr, int type, void *data)
451 switch (TYPE (type)) {
453 #define BEGIN_PROTOCOL_ENTRY0(method) \
454 case PROTOCOL_ID (method): {
455 #define BEGIN_PROTOCOL_ENTRY1(method,t1,f1) \
456 case PROTOCOL_ID (method): { \
457 PROTOCOL_STRUCT (method) *entry G_GNUC_UNUSED = (PROTOCOL_STRUCT (method)*)data;
458 #define BEGIN_PROTOCOL_ENTRY2(method,t1,f1,t2,f2) \
459 case PROTOCOL_ID (method): { \
460 PROTOCOL_STRUCT (method) *entry G_GNUC_UNUSED = (PROTOCOL_STRUCT (method)*)data;
461 #define BEGIN_PROTOCOL_ENTRY3(method,t1,f1,t2,f2,t3,f3) \
462 case PROTOCOL_ID (method): { \
463 PROTOCOL_STRUCT (method) *entry G_GNUC_UNUSED = (PROTOCOL_STRUCT (method)*)data;
464 #define BEGIN_PROTOCOL_ENTRY4(method,t1,f1,t2,f2,t3,f3,t4,f4) \
465 case PROTOCOL_ID (method): { \
466 PROTOCOL_STRUCT (method) *entry G_GNUC_UNUSED = (PROTOCOL_STRUCT (method)*)data;
467 #define BEGIN_PROTOCOL_ENTRY5(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5) \
468 case PROTOCOL_ID (method): { \
469 PROTOCOL_STRUCT (method) *entry G_GNUC_UNUSED = (PROTOCOL_STRUCT (method)*)data;
470 #define BEGIN_PROTOCOL_ENTRY6(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5,t6,f6) \
471 case PROTOCOL_ID (method): { \
472 PROTOCOL_STRUCT (method) *entry G_GNUC_UNUSED = (PROTOCOL_STRUCT (method)*)data;
474 #define BEGIN_PROTOCOL_ENTRY_HEAVY0(method) \
475 BEGIN_PROTOCOL_ENTRY0 (method)
476 #define BEGIN_PROTOCOL_ENTRY_HEAVY1(method,t1,f1) \
477 BEGIN_PROTOCOL_ENTRY1 (method,t1,f1)
478 #define BEGIN_PROTOCOL_ENTRY_HEAVY2(method,t1,f1,t2,f2) \
479 BEGIN_PROTOCOL_ENTRY2 (method,t1,f1,t2,f2)
480 #define BEGIN_PROTOCOL_ENTRY_HEAVY3(method,t1,f1,t2,f2,t3,f3) \
481 BEGIN_PROTOCOL_ENTRY3 (method,t1,f1,t2,f2,t3,f3)
482 #define BEGIN_PROTOCOL_ENTRY_HEAVY4(method,t1,f1,t2,f2,t3,f3,t4,f4) \
483 BEGIN_PROTOCOL_ENTRY4 (method,t1,f1,t2,f2,t3,f3,t4,f4)
484 #define BEGIN_PROTOCOL_ENTRY_HEAVY5(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5) \
485 BEGIN_PROTOCOL_ENTRY5 (method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5)
486 #define BEGIN_PROTOCOL_ENTRY_HEAVY6(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5,t6,f6) \
487 BEGIN_PROTOCOL_ENTRY6 (method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5,t6,f6)
489 #define DEFAULT_PRINT()
490 #define CUSTOM_PRINT(_)
492 #define IS_ALWAYS_MATCH(_)
493 #define MATCH_INDEX(block) \
494 return (block);
495 #define IS_VTABLE_MATCH(_)
497 #define END_PROTOCOL_ENTRY \
498 break; \
500 #define END_PROTOCOL_ENTRY_FLUSH \
501 END_PROTOCOL_ENTRY
502 #define END_PROTOCOL_ENTRY_HEAVY \
503 END_PROTOCOL_ENTRY
505 #include <mono/sgen/sgen-protocol-def.h>
507 default:
508 assert (0);
509 return 0;
513 static gboolean
514 is_vtable_match (mword ptr, int type, void *data)
516 switch (TYPE (type)) {
518 #define BEGIN_PROTOCOL_ENTRY0(method) \
519 case PROTOCOL_ID (method): {
520 #define BEGIN_PROTOCOL_ENTRY1(method,t1,f1) \
521 case PROTOCOL_ID (method): { \
522 PROTOCOL_STRUCT (method) *entry G_GNUC_UNUSED = (PROTOCOL_STRUCT (method)*)data;
523 #define BEGIN_PROTOCOL_ENTRY2(method,t1,f1,t2,f2) \
524 case PROTOCOL_ID (method): { \
525 PROTOCOL_STRUCT (method) *entry G_GNUC_UNUSED = (PROTOCOL_STRUCT (method)*)data;
526 #define BEGIN_PROTOCOL_ENTRY3(method,t1,f1,t2,f2,t3,f3) \
527 case PROTOCOL_ID (method): { \
528 PROTOCOL_STRUCT (method) *entry G_GNUC_UNUSED = (PROTOCOL_STRUCT (method)*)data;
529 #define BEGIN_PROTOCOL_ENTRY4(method,t1,f1,t2,f2,t3,f3,t4,f4) \
530 case PROTOCOL_ID (method): { \
531 PROTOCOL_STRUCT (method) *entry G_GNUC_UNUSED = (PROTOCOL_STRUCT (method)*)data;
532 #define BEGIN_PROTOCOL_ENTRY5(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5) \
533 case PROTOCOL_ID (method): { \
534 PROTOCOL_STRUCT (method) *entry G_GNUC_UNUSED = (PROTOCOL_STRUCT (method)*)data;
535 #define BEGIN_PROTOCOL_ENTRY6(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5,t6,f6) \
536 case PROTOCOL_ID (method): { \
537 PROTOCOL_STRUCT (method) *entry G_GNUC_UNUSED = (PROTOCOL_STRUCT (method)*)data;
539 #define BEGIN_PROTOCOL_ENTRY_HEAVY0(method) \
540 BEGIN_PROTOCOL_ENTRY0 (method)
541 #define BEGIN_PROTOCOL_ENTRY_HEAVY1(method,t1,f1) \
542 BEGIN_PROTOCOL_ENTRY1 (method,t1,f1)
543 #define BEGIN_PROTOCOL_ENTRY_HEAVY2(method,t1,f1,t2,f2) \
544 BEGIN_PROTOCOL_ENTRY2 (method,t1,f1,t2,f2)
545 #define BEGIN_PROTOCOL_ENTRY_HEAVY3(method,t1,f1,t2,f2,t3,f3) \
546 BEGIN_PROTOCOL_ENTRY3 (method,t1,f1,t2,f2,t3,f3)
547 #define BEGIN_PROTOCOL_ENTRY_HEAVY4(method,t1,f1,t2,f2,t3,f3,t4,f4) \
548 BEGIN_PROTOCOL_ENTRY4 (method,t1,f1,t2,f2,t3,f3,t4,f4)
549 #define BEGIN_PROTOCOL_ENTRY_HEAVY5(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5) \
550 BEGIN_PROTOCOL_ENTRY5 (method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5)
551 #define BEGIN_PROTOCOL_ENTRY_HEAVY6(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5,t6,f6) \
552 BEGIN_PROTOCOL_ENTRY6 (method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5,t6,f6)
554 #define DEFAULT_PRINT()
555 #define CUSTOM_PRINT(_)
557 #define IS_ALWAYS_MATCH(_)
558 #define MATCH_INDEX(block) \
559 return (block);
560 #define IS_VTABLE_MATCH(_)
562 #define END_PROTOCOL_ENTRY \
563 break; \
565 #define END_PROTOCOL_ENTRY_FLUSH \
566 END_PROTOCOL_ENTRY
567 #define END_PROTOCOL_ENTRY_HEAVY \
568 END_PROTOCOL_ENTRY
570 #include <mono/sgen/sgen-protocol-def.h>
572 default:
573 assert (0);
574 return FALSE;
578 #undef TYPE_INT
579 #undef TYPE_LONGLONG
580 #undef TYPE_SIZE
581 #undef TYPE_POINTER
583 static gboolean
584 sgen_binary_protocol_read_header (EntryStream *stream)
586 #ifdef BINPROT_HAS_HEADER
587 char data [MAX_ENTRY_SIZE];
588 int type = read_entry (stream, data, NULL);
589 if (type == SGEN_PROTOCOL_EOF)
590 return FALSE;
591 if (type == PROTOCOL_ID (binary_protocol_header)) {
592 PROTOCOL_STRUCT (binary_protocol_header) * str = (PROTOCOL_STRUCT (binary_protocol_header) *) data;
593 if (str->check == PROTOCOL_HEADER_CHECK && str->ptr_size == BINPROT_SIZEOF_VOID_P) {
594 if (str->version > PROTOCOL_HEADER_VERSION) {
595 fprintf (stderr, "The file contains a newer version %d. We support up to %d. Please update.\n", str->version, PROTOCOL_HEADER_VERSION);
596 exit (1);
598 file_version = str->version;
599 return TRUE;
602 return FALSE;
603 #else
605 * This implementation doesn't account for the presence of a header,
606 * reading all the entries with the default configuration of the host
607 * machine. It has to be used only after all other implementations
608 * fail to identify a header, for backward compatibility.
610 return TRUE;
611 #endif
614 #define CONC(A, B) CONC_(A, B)
615 #define CONC_(A, B) A##B
616 #define GREP_ENTRIES_FUNCTION_NAME CONC(sgen_binary_protocol_grep_entries, CONC(ARCH_SUFFIX,PACKED_SUFFIX))
618 gboolean
619 GREP_ENTRIES_FUNCTION_NAME (EntryStream *stream, int num_nums, long nums [], int num_vtables, long vtables [],
620 gboolean dump_all, gboolean pause_times, gboolean color_output, unsigned long long first_entry_to_consider)
622 int type;
623 unsigned char worker_index;
624 void *data = g_malloc0 (MAX_ENTRY_SIZE);
625 int i;
626 gboolean pause_times_stopped = FALSE;
627 gboolean pause_times_concurrent = FALSE;
628 gboolean pause_times_finish = FALSE;
629 long long pause_times_ts = 0;
630 unsigned long long entry_index;
632 if (!sgen_binary_protocol_read_header (stream))
633 return FALSE;
635 entry_index = 0;
636 while ((type = read_entry (stream, data, &worker_index)) != SGEN_PROTOCOL_EOF) {
637 if (entry_index < first_entry_to_consider)
638 goto next_entry;
639 if (pause_times) {
640 switch (type) {
641 case PROTOCOL_ID (binary_protocol_world_stopping): {
642 PROTOCOL_STRUCT (binary_protocol_world_stopping) *entry = (PROTOCOL_STRUCT (binary_protocol_world_stopping)*)data;
643 assert (!pause_times_stopped);
644 pause_times_concurrent = FALSE;
645 pause_times_finish = FALSE;
646 pause_times_ts = entry->timestamp;
647 pause_times_stopped = TRUE;
648 break;
650 case PROTOCOL_ID (binary_protocol_concurrent_finish):
651 pause_times_finish = TRUE;
652 case PROTOCOL_ID (binary_protocol_concurrent_start):
653 case PROTOCOL_ID (binary_protocol_concurrent_update):
654 pause_times_concurrent = TRUE;
655 break;
656 case PROTOCOL_ID (binary_protocol_world_restarted): {
657 PROTOCOL_STRUCT (binary_protocol_world_restarted) *entry = (PROTOCOL_STRUCT (binary_protocol_world_restarted)*)data;
658 assert (pause_times_stopped);
659 printf ("pause-time %d %d %d %lld %lld\n",
660 entry->generation,
661 pause_times_concurrent,
662 pause_times_finish,
663 entry->timestamp - pause_times_ts,
664 pause_times_ts);
665 pause_times_stopped = FALSE;
666 break;
669 } else {
670 int match_indices [num_nums + 1];
671 gboolean match = is_always_match (type);
672 match_indices [num_nums] = num_nums == 0 ? match_index (0, type, data) : BINARY_PROTOCOL_NO_MATCH;
673 match = match_indices [num_nums] != BINARY_PROTOCOL_NO_MATCH;
674 for (i = 0; i < num_nums; ++i) {
675 match_indices [i] = match_index ((mword) nums [i], type, data);
676 match = match || match_indices [i] != BINARY_PROTOCOL_NO_MATCH;
678 if (!match) {
679 for (i = 0; i < num_vtables; ++i) {
680 if (is_vtable_match ((mword) vtables [i], type, data)) {
681 match = TRUE;
682 break;
686 if (match || dump_all)
687 printf ("%12lld ", entry_index);
688 if (dump_all)
689 printf (match ? "* " : " ");
690 if (match || dump_all)
691 print_entry (type, data, num_nums, match_indices, color_output, worker_index);
693 next_entry:
694 ++entry_index;
696 g_free (data);
697 return TRUE;