Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / tools / sgen / sgen-grep-binprot-main.c
1 /*
2  * sgen-grep-binprot-main.c: 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 "sgen-entry-stream.h"
16 #include "sgen-grep-binprot.h"
17
18 /* FIXME Add grepers for specific endianness */
19 GrepEntriesFunction grepers [] = {
20         sgen_binary_protocol_grep_entries32p, /* We have header, structures are packed, 32 bit word */
21         sgen_binary_protocol_grep_entries64p, /* We have header, structures are packed, 64 bit word */
22         sgen_binary_protocol_grep_entries /* No header, uses default word size and structure layout */
23 };
24
25 int
26 main (int argc, char *argv[])
27 {
28         int num_args = argc - 1;
29         int num_nums = 0;
30         int num_vtables = 0;
31         int i;
32         long nums [num_args];
33         long vtables [num_args];
34         gboolean dump_all = FALSE;
35         gboolean color_output = FALSE;
36         gboolean pause_times = FALSE;
37         const char *input_path = NULL;
38         int input_file;
39         EntryStream stream;
40         unsigned long long first_entry_to_consider = 0;
41
42         for (i = 0; i < num_args; ++i) {
43                 char *arg = argv [i + 1];
44                 char *next_arg = argv [i + 2];
45                 if (!strcmp (arg, "--all")) {
46                         dump_all = TRUE;
47                 } else if (!strcmp (arg, "--pause-times")) {
48                         pause_times = TRUE;
49                 } else if (!strcmp (arg, "-v") || !strcmp (arg, "--vtable")) {
50                         vtables [num_vtables++] = strtoul (next_arg, NULL, 16);
51                         ++i;
52                 } else if (!strcmp (arg, "-s") || !strcmp (arg, "--start-at")) {
53                         first_entry_to_consider = strtoull (next_arg, NULL, 10);
54                         ++i;
55                 } else if (!strcmp (arg, "-c") || !strcmp (arg, "--color")) {
56                         color_output = TRUE;
57                 } else if (!strcmp (arg, "-i") || !strcmp (arg, "--input")) {
58                         input_path = next_arg;
59                         ++i;
60                 } else if (!strcmp (arg, "--help")) {
61                         printf (
62                                 "\n"
63                                 "Usage:\n"
64                                 "\n"
65                                 "\tsgen-grep-binprot [options] [pointer...]\n"
66                                 "\n"
67                                 "Examples:\n"
68                                 "\n"
69                                 "\tsgen-grep-binprot --all </tmp/binprot\n"
70                                 "\tsgen-grep-binprot --input /tmp/binprot --color 0xdeadbeef\n"
71                                 "\n"
72                                 "Options:\n"
73                                 "\n"
74                                 "\t--all                    Print all entries.\n"
75                                 "\t--color, -c              Highlight matches in color.\n"
76                                 "\t--help                   You're looking at it.\n"
77                                 "\t--input FILE, -i FILE    Read input from FILE instead of standard input.\n"
78                                 "\t--pause-times            Print GC pause times.\n"
79                                 "\t--start-at N, -s N       Begin filtering at the Nth entry.\n"
80                                 "\t--vtable PTR, -v PTR     Search for vtable pointer PTR.\n"
81                                 "\n");
82                         return 0;
83                 } else {
84                         nums [num_nums++] = strtoul (arg, NULL, 16);
85                 }
86         }
87
88         if (dump_all)
89                 assert (!pause_times);
90         if (pause_times)
91                 assert (!dump_all);
92
93         input_file = input_path ? open (input_path, O_RDONLY) : STDIN_FILENO;
94         init_stream (&stream, input_file);
95         for (i = 0; i < sizeof (grepers) / sizeof (GrepEntriesFunction); i++) {
96                 if (grepers [i] (&stream, num_nums, nums, num_vtables, vtables, dump_all,
97                                 pause_times, color_output, first_entry_to_consider)) {
98                         /* Success */
99                         break;
100                 }
101                 reset_stream (&stream);
102         }
103         close_stream (&stream);
104         if (input_path)
105                 close (input_file);
106
107         return 0;
108 }