[sgen] Add worker index to the binary protocol entries
[mono.git] / tools / sgen / sgen-grep-binprot.c
index bb7fd5e681a0e9976cbe98ef8f8685c4f8d47713..322f47a72ad518931ebcd2e8b1a8ae56807fba78 100644 (file)
@@ -1,26 +1,81 @@
+/*
+ * sgen-grep-binprot.c: Platform specific binary protocol entries reader
+ *
+ * Copyright (C) 2016 Xamarin Inc
+ *
+ * Licensed under the MIT license. See LICENSE file in the project root for full license information.
+ */
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <assert.h>
 #include <glib.h>
-
-#define SGEN_BINARY_PROTOCOL
-#define MONO_INTERNAL
-
-#include <mono/metadata/sgen-protocol.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <stdint.h>
+#include <inttypes.h>
+#include <config.h>
+#include "sgen-entry-stream.h"
+#include "sgen-grep-binprot.h"
+
+static int file_version = 0;
+
+#ifdef BINPROT_HAS_HEADER
+#define PACKED_SUFFIX  p
+#else
+#define PROTOCOL_STRUCT_ATTR
+#define PACKED_SUFFIX
+#endif
+
+#ifndef BINPROT_SIZEOF_VOID_P
+#define BINPROT_SIZEOF_VOID_P SIZEOF_VOID_P
+#define ARCH_SUFFIX
+#endif
+
+#if BINPROT_SIZEOF_VOID_P == 4
+typedef int32_t mword;
+#define MWORD_FORMAT_SPEC_D PRId32
+#define MWORD_FORMAT_SPEC_P PRIx32
+#ifndef ARCH_SUFFIX
+#define ARCH_SUFFIX    32
+#endif
+#else
+typedef int64_t mword;
+#define MWORD_FORMAT_SPEC_D PRId64
+#define MWORD_FORMAT_SPEC_P PRIx64
+#ifndef ARCH_SUFFIX
+#define ARCH_SUFFIX    64
+#endif
+#endif
+#define TYPE_SIZE      mword
+#define TYPE_POINTER   mword
+#include <mono/sgen/sgen-protocol.h>
 
 #define SGEN_PROTOCOL_EOF      255
 
 #define TYPE(t)                ((t) & 0x7f)
 #define WORKER(t)      ((t) & 0x80)
 
+#define MAX_ENTRY_SIZE (1 << 10)
+
 static int
-read_entry (FILE *in, void **data)
+read_entry (EntryStream *stream, void *data, unsigned char *windex)
 {
        unsigned char type;
-       int size;
+       ssize_t size;
 
-       if (fread (&type, 1, 1, in) != 1)
+       if (read_stream (stream, &type, 1) <= 0)
                return SGEN_PROTOCOL_EOF;
+
+       if (windex) {
+               if (file_version >= 2) {
+                       if (read_stream (stream, windex, 1) <= 0)
+                               return SGEN_PROTOCOL_EOF;
+               } else {
+                       *windex = !!(WORKER (type));
+               }
+       }
+
        switch (TYPE (type)) {
 
 #define BEGIN_PROTOCOL_ENTRY0(method) \
@@ -53,8 +108,6 @@ read_entry (FILE *in, void **data)
 #define BEGIN_PROTOCOL_ENTRY_HEAVY6(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5,t6,f6) \
        BEGIN_PROTOCOL_ENTRY6 (method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5,t6,f6)
 
-#define FLUSH()
-
 #define DEFAULT_PRINT()
 #define CUSTOM_PRINT(_)
 
@@ -63,19 +116,17 @@ read_entry (FILE *in, void **data)
 #define IS_VTABLE_MATCH(_)
 
 #define END_PROTOCOL_ENTRY
+#define END_PROTOCOL_ENTRY_FLUSH
 #define END_PROTOCOL_ENTRY_HEAVY
 
-#include <mono/metadata/sgen-protocol-def.h>
+#include <mono/sgen/sgen-protocol-def.h>
 
        default: assert (0);
        }
 
        if (size) {
-               *data = malloc (size);
-               if (fread (*data, size, 1, in) != 1)
-                       assert (0);
-       } else {
-               *data = NULL;
+               size_t size_read = read_stream (stream, data, size);
+               g_assert (size_read == size);
        }
 
        return (int)type;
@@ -115,8 +166,6 @@ is_always_match (int type)
 #define BEGIN_PROTOCOL_ENTRY_HEAVY6(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5,t6,f6) \
        BEGIN_PROTOCOL_ENTRY6 (method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5,t6,f6)
 
-#define FLUSH()
-
 #define DEFAULT_PRINT()
 #define CUSTOM_PRINT(_)
 
@@ -126,16 +175,15 @@ is_always_match (int type)
 #define IS_VTABLE_MATCH(_)
 
 #define END_PROTOCOL_ENTRY
+#define END_PROTOCOL_ENTRY_FLUSH
 #define END_PROTOCOL_ENTRY_HEAVY
 
-#include <mono/metadata/sgen-protocol-def.h>
+#include <mono/sgen/sgen-protocol-def.h>
 
        default: assert (0);
        }
 }
 
-#define WORKER_PREFIX(t)       (WORKER ((t)) ? "w" : " ")
-
 enum { NO_COLOR = -1 };
 
 typedef struct {
@@ -153,6 +201,7 @@ typedef struct {
 #define TYPE_LONGLONG 1
 #define TYPE_SIZE 2
 #define TYPE_POINTER 3
+#define TYPE_BOOL 4
 
 static void
 print_entry_content (int entries_size, PrintEntry *entries, gboolean color_output)
@@ -171,10 +220,13 @@ print_entry_content (int entries_size, PrintEntry *entries, gboolean color_outpu
                        printf ("%lld", *(long long*) entries [i].data);
                        break;
                case TYPE_SIZE:
-                       printf ("%lu", *(size_t*) entries [i].data);
+                       printf ("%"MWORD_FORMAT_SPEC_D, *(mword*) entries [i].data);
                        break;
                case TYPE_POINTER:
-                       printf ("%p", *(gpointer*) entries [i].data);
+                       printf ("0x%"MWORD_FORMAT_SPEC_P, *(mword*) entries [i].data);
+                       break;
+               case TYPE_BOOL:
+                       printf ("%s", *(gboolean*) entries [i].data ? "true" : "false");
                        break;
                default:
                        assert (0);
@@ -196,10 +248,13 @@ index_color (int index, int num_nums, int *match_indices)
 }
 
 static void
-print_entry (int type, void *data, int num_nums, int *match_indices, gboolean color_output)
+print_entry (int type, void *data, int num_nums, int *match_indices, gboolean color_output, unsigned char worker_index)
 {
        const char *always_prefix = is_always_match (type) ? "  " : "";
-       printf ("%s%s ", WORKER_PREFIX (type), always_prefix);
+       if (worker_index)
+               printf ("w%-2d%s ", worker_index, always_prefix);
+       else
+               printf ("   %s ", always_prefix);
 
        switch (TYPE (type)) {
 
@@ -344,8 +399,6 @@ print_entry (int type, void *data, int num_nums, int *match_indices, gboolean co
 #define BEGIN_PROTOCOL_ENTRY_HEAVY6(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5,t6,f6) \
        BEGIN_PROTOCOL_ENTRY6 (method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5,t6,f6)
 
-#define FLUSH()
-
 #define DEFAULT_PRINT() \
        print_entry_content (pes_size, pes, color_output);
 #define CUSTOM_PRINT(print) \
@@ -359,10 +412,12 @@ print_entry (int type, void *data, int num_nums, int *match_indices, gboolean co
                printf ("\n"); \
                break; \
        }
+#define END_PROTOCOL_ENTRY_FLUSH \
+       END_PROTOCOL_ENTRY
 #define END_PROTOCOL_ENTRY_HEAVY \
        END_PROTOCOL_ENTRY
 
-#include <mono/metadata/sgen-protocol-def.h>
+#include <mono/sgen/sgen-protocol-def.h>
 
        default: assert (0);
        }
@@ -375,13 +430,13 @@ print_entry (int type, void *data, int num_nums, int *match_indices, gboolean co
 
 #define TYPE_INT int
 #define TYPE_LONGLONG long long
-#define TYPE_SIZE size_t
-#define TYPE_POINTER gpointer
+#define TYPE_SIZE mword
+#define TYPE_POINTER mword
 
 static gboolean
-matches_interval (gpointer ptr, gpointer start, int size)
+matches_interval (mword ptr, mword start, int size)
 {
-       return ptr >= start && (char*)ptr < (char*)start + size;
+       return ptr >= start && ptr < start + size;
 }
 
 /* Returns the index of the field where a match was found,
@@ -389,7 +444,7 @@ matches_interval (gpointer ptr, gpointer start, int size)
  * BINARY_PROTOCOL_MATCH for a match with no index.
  */
 static int
-match_index (gpointer ptr, int type, void *data)
+match_index (mword ptr, int type, void *data)
 {
        switch (TYPE (type)) {
 
@@ -429,8 +484,6 @@ match_index (gpointer ptr, int type, void *data)
 #define BEGIN_PROTOCOL_ENTRY_HEAVY6(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5,t6,f6) \
        BEGIN_PROTOCOL_ENTRY6 (method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5,t6,f6)
 
-#define FLUSH()
-
 #define DEFAULT_PRINT()
 #define CUSTOM_PRINT(_)
 
@@ -442,17 +495,19 @@ match_index (gpointer ptr, int type, void *data)
 #define END_PROTOCOL_ENTRY \
                break; \
        }
+#define END_PROTOCOL_ENTRY_FLUSH \
+       END_PROTOCOL_ENTRY
 #define END_PROTOCOL_ENTRY_HEAVY \
        END_PROTOCOL_ENTRY
 
-#include <mono/metadata/sgen-protocol-def.h>
+#include <mono/sgen/sgen-protocol-def.h>
 
        default: assert (0);
        }
 }
 
 static gboolean
-is_vtable_match (gpointer ptr, int type, void *data)
+is_vtable_match (mword ptr, int type, void *data)
 {
        switch (TYPE (type)) {
 
@@ -492,8 +547,6 @@ is_vtable_match (gpointer ptr, int type, void *data)
 #define BEGIN_PROTOCOL_ENTRY_HEAVY6(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5,t6,f6) \
        BEGIN_PROTOCOL_ENTRY6 (method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5,t6,f6)
 
-#define FLUSH()
-
 #define DEFAULT_PRINT()
 #define CUSTOM_PRINT(_)
 
@@ -505,10 +558,12 @@ is_vtable_match (gpointer ptr, int type, void *data)
 #define END_PROTOCOL_ENTRY \
                break; \
        }
+#define END_PROTOCOL_ENTRY_FLUSH \
+       END_PROTOCOL_ENTRY
 #define END_PROTOCOL_ENTRY_HEAVY \
        END_PROTOCOL_ENTRY
 
-#include <mono/metadata/sgen-protocol-def.h>
+#include <mono/sgen/sgen-protocol-def.h>
 
        default: assert (0);
        }
@@ -519,48 +574,62 @@ is_vtable_match (gpointer ptr, int type, void *data)
 #undef TYPE_SIZE
 #undef TYPE_POINTER
 
-int
-main (int argc, char *argv[])
+static gboolean
+sgen_binary_protocol_read_header (EntryStream *stream)
+{
+#ifdef BINPROT_HAS_HEADER
+       char data [MAX_ENTRY_SIZE];
+       int type = read_entry (stream, data, NULL);
+       if (type == SGEN_PROTOCOL_EOF)
+               return FALSE;
+       if (type == PROTOCOL_ID (binary_protocol_header)) {
+               PROTOCOL_STRUCT (binary_protocol_header) * str = (PROTOCOL_STRUCT (binary_protocol_header) *) data;
+               if (str->check == PROTOCOL_HEADER_CHECK && str->ptr_size == BINPROT_SIZEOF_VOID_P) {
+                       if (str->version > PROTOCOL_HEADER_VERSION) {
+                               fprintf (stderr, "The file contains a newer version %d. We support up to %d. Please update.\n", str->version, PROTOCOL_HEADER_VERSION);
+                               exit (1);
+                       }
+                       file_version = str->version;
+                       return TRUE;
+               }
+       }
+       return FALSE;
+#else
+       /*
+        * This implementation doesn't account for the presence of a header,
+        * reading all the entries with the default configuration of the host
+        * machine. It has to be used only after all other implementations
+        * fail to identify a header, for backward compatibility.
+        */
+       return TRUE;
+#endif
+}
+
+#define CONC(A, B) CONC_(A, B)
+#define CONC_(A, B) A##B
+#define GREP_ENTRIES_FUNCTION_NAME CONC(sgen_binary_protocol_grep_entries, CONC(ARCH_SUFFIX,PACKED_SUFFIX))
+
+gboolean
+GREP_ENTRIES_FUNCTION_NAME (EntryStream *stream, int num_nums, long nums [], int num_vtables, long vtables [],
+                       gboolean dump_all, gboolean pause_times, gboolean color_output, unsigned long long first_entry_to_consider)
 {
        int type;
-       void *data;
-       int num_args = argc - 1;
-       int num_nums = 0;
-       int num_vtables = 0;
+       unsigned char worker_index;
+       void *data = g_malloc0 (MAX_ENTRY_SIZE);
        int i;
-       long nums [num_args];
-       long vtables [num_args];
-       gboolean dump_all = FALSE;
-       gboolean pause_times = FALSE;
        gboolean pause_times_stopped = FALSE;
        gboolean pause_times_concurrent = FALSE;
        gboolean pause_times_finish = FALSE;
-       gboolean color_output = FALSE;
        long long pause_times_ts = 0;
+       unsigned long long entry_index;
 
-       for (i = 0; i < num_args; ++i) {
-               char *arg = argv [i + 1];
-               char *next_arg = argv [i + 2];
-               if (!strcmp (arg, "--all")) {
-                       dump_all = TRUE;
-               } else if (!strcmp (arg, "--pause-times")) {
-                       pause_times = TRUE;
-               } else if (!strcmp (arg, "-v") || !strcmp (arg, "--vtable")) {
-                       vtables [num_vtables++] = strtoul (next_arg, NULL, 16);
-                       ++i;
-               } else if (!strcmp (arg, "-c") || !strcmp (arg, "--color")) {
-                       color_output = TRUE;
-               } else {
-                       nums [num_nums++] = strtoul (arg, NULL, 16);
-               }
-       }
-
-       if (dump_all)
-               assert (!pause_times);
-       if (pause_times)
-               assert (!dump_all);
+       if (!sgen_binary_protocol_read_header (stream))
+               return FALSE;
 
-       while ((type = read_entry (stdin, &data)) != SGEN_PROTOCOL_EOF) {
+       entry_index = 0;
+       while ((type = read_entry (stream, data, &worker_index)) != SGEN_PROTOCOL_EOF) {
+               if (entry_index < first_entry_to_consider)
+                       goto next_entry;
                if (pause_times) {
                        switch (type) {
                        case PROTOCOL_ID (binary_protocol_world_stopping): {
@@ -594,27 +663,30 @@ main (int argc, char *argv[])
                } else {
                        int match_indices [num_nums + 1];
                        gboolean match = is_always_match (type);
-                       match_indices [num_nums] = num_nums == 0 ? match_index (NULL, type, data) : BINARY_PROTOCOL_NO_MATCH;
+                       match_indices [num_nums] = num_nums == 0 ? match_index (0, type, data) : BINARY_PROTOCOL_NO_MATCH;
                        match = match_indices [num_nums] != BINARY_PROTOCOL_NO_MATCH;
                        for (i = 0; i < num_nums; ++i) {
-                               match_indices [i] = match_index ((gpointer) nums [i], type, data);
+                               match_indices [i] = match_index ((mword) nums [i], type, data);
                                match = match || match_indices [i] != BINARY_PROTOCOL_NO_MATCH;
                        }
                        if (!match) {
                                for (i = 0; i < num_vtables; ++i) {
-                                       if (is_vtable_match ((gpointer) vtables [i], type, data)) {
+                                       if (is_vtable_match ((mword) vtables [i], type, data)) {
                                                match = TRUE;
                                                break;
                                        }
                                }
                        }
+                       if (match || dump_all)
+                               printf ("%12lld ", entry_index);
                        if (dump_all)
                                printf (match ? "* " : "  ");
                        if (match || dump_all)
-                               print_entry (type, data, num_nums, match_indices, color_output);
+                               print_entry (type, data, num_nums, match_indices, color_output, worker_index);
                }
-               free (data);
+       next_entry:
+               ++entry_index;
        }
-
-       return 0;
+       g_free (data);
+       return TRUE;
 }