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