intel/perf: export performance counters sorted by [group|set] and name
[mesa.git] / src / intel / tools / i965_disasm.c
1 /*
2 * Copyright © 2018 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <getopt.h>
28
29 #include "compiler/brw_eu.h"
30 #include "dev/gen_device_info.h"
31 #include "util/u_dynarray.h"
32
33 enum opt_input_type {
34 OPT_INPUT_BINARY,
35 OPT_INPUT_C_LITERAL,
36 };
37
38 static enum opt_input_type input_type = OPT_INPUT_BINARY;
39
40 /* Return size of file in bytes pointed by fp */
41 static size_t
42 i965_disasm_get_file_size(FILE *fp)
43 {
44 size_t size;
45
46 fseek(fp, 0L, SEEK_END);
47 size = ftell(fp);
48 fseek(fp, 0L, SEEK_SET);
49
50 return size;
51 }
52
53 /* Read hex file which should be in following format:
54 * for example :
55 * { 0x00000000, 0x00000000, 0x00000000, 0x00000000 }
56 */
57 static void *
58 i965_disasm_read_c_literal_file(FILE *fp, size_t *end)
59 {
60 struct util_dynarray assembly = {};
61 uint32_t temp[2];
62
63 if (fscanf(fp, " { ") == EOF) {
64 fprintf(stderr, "Couldn't find opening `{`\n");
65 return NULL;
66 }
67
68 if (fscanf(fp, "0x%x , 0x%x", &temp[0], &temp[1]) == 2) {
69 util_dynarray_append(&assembly, uint32_t, temp[0]);
70 util_dynarray_append(&assembly, uint32_t, temp[1]);
71 } else {
72 fprintf(stderr, "Couldn't read hex values\n");
73 return NULL;
74 }
75
76 while (fscanf(fp, " , 0x%x , 0x%x ", &temp[0], &temp[1]) == 2) {
77 util_dynarray_append(&assembly, uint32_t, temp[0]);
78 util_dynarray_append(&assembly, uint32_t, temp[1]);
79 }
80
81 if (fscanf(fp, "}") == EOF) {
82 fprintf(stderr, "Couldn't find closing `}`\n");
83 return NULL;
84 }
85
86 *end = assembly.size;
87 return assembly.data;
88 }
89
90 static void *
91 i965_disasm_read_binary(FILE *fp, size_t *end)
92 {
93 size_t size;
94 void *assembly;
95
96 *end = i965_disasm_get_file_size(fp);
97 if (!*end)
98 return NULL;
99
100 assembly = malloc(*end + 1);
101 if (assembly == NULL)
102 return NULL;
103
104 size = fread(assembly, *end, 1, fp);
105 if (!size) {
106 return NULL;
107 }
108 return assembly;
109 }
110
111 static struct gen_device_info *
112 i965_disasm_init(uint16_t pci_id)
113 {
114 struct gen_device_info *devinfo;
115
116 devinfo = malloc(sizeof *devinfo);
117 if (devinfo == NULL)
118 return NULL;
119
120 if (!gen_get_device_info_from_pci_id(pci_id, devinfo)) {
121 fprintf(stderr, "can't find device information: pci_id=0x%x\n",
122 pci_id);
123 exit(EXIT_FAILURE);
124 }
125
126 /* initialize compaction table in order to handle compacted instructions */
127 brw_init_compaction_tables(devinfo);
128
129 return devinfo;
130 }
131
132 static void
133 print_help(const char *progname, FILE *file)
134 {
135 fprintf(file,
136 "Usage: %s [OPTION]...\n"
137 "Disassemble i965 instructions from binary file.\n\n"
138 " --help display this help and exit\n"
139 " --input-path=PATH read binary file from binary file PATH\n"
140 " --type=INPUT_TYPE INPUT_TYPE can be 'bin' (default if omitted),\n"
141 " 'c_literal'.\n"
142 " --gen=platform disassemble instructions for given \n"
143 " platform (3 letter platform name)\n",
144 progname);
145 }
146
147 int main(int argc, char *argv[])
148 {
149 FILE *fp = NULL;
150 void *assembly = NULL;
151 char *file_path = NULL;
152 size_t start = 0, end = 0;
153 uint16_t pci_id = 0;
154 int c;
155 struct gen_device_info *devinfo = NULL;
156 int result = EXIT_FAILURE;
157
158 bool help = false;
159 const struct option i965_disasm_opts[] = {
160 { "help", no_argument, (int *) &help, true },
161 { "input-path", required_argument, NULL, 'i' },
162 { "type", required_argument, NULL, 't' },
163 { "gen", required_argument, NULL, 'g'},
164 { NULL, 0, NULL, 0 }
165 };
166
167 while ((c = getopt_long(argc, argv, ":i:t:g:h", i965_disasm_opts, NULL)) != -1) {
168 switch (c) {
169 case 'g': {
170 const int id = gen_device_name_to_pci_device_id(optarg);
171 if (id < 0) {
172 fprintf(stderr, "can't parse gen: '%s', expected 3 letter "
173 "platform name\n", optarg);
174 goto end;
175 } else {
176 pci_id = id;
177 }
178 break;
179 }
180 case 'i':
181 file_path = strdup(optarg);
182 fp = fopen(file_path, "r");
183 if (!fp) {
184 fprintf(stderr, "Unable to read input file : %s\n",
185 file_path);
186 goto end;
187 }
188 break;
189 case 't':
190 if (strcmp(optarg, "c_literal") == 0) {
191 input_type = OPT_INPUT_C_LITERAL;
192 } else if (strcmp(optarg, "bin") == 0) {
193 input_type = OPT_INPUT_BINARY;
194 } else {
195 fprintf(stderr, "invalid value for --type: %s\n", optarg);
196 goto end;
197 }
198 break;
199 case 'h':
200 help = true;
201 print_help(argv[0], stderr);
202 goto end;
203 case 0:
204 break;
205 case ':':
206 fprintf(stderr, "%s: option `-%c' requires an argument\n",
207 argv[0], optopt);
208 goto end;
209 case '?':
210 default:
211 fprintf(stderr, "%s: option `-%c' is invalid: ignored\n",
212 argv[0], optopt);
213 goto end;
214 }
215 }
216
217 if (help || !file_path || !pci_id) {
218 print_help(argv[0], stderr);
219 exit(0);
220 }
221
222 devinfo = i965_disasm_init(pci_id);
223 if (!devinfo) {
224 fprintf(stderr, "Unable to allocate memory for "
225 "gen_device_info struct instance.\n");
226 goto end;
227 }
228
229 if (input_type == OPT_INPUT_BINARY)
230 assembly = i965_disasm_read_binary(fp, &end);
231 else if (input_type == OPT_INPUT_C_LITERAL)
232 assembly = i965_disasm_read_c_literal_file(fp, &end);
233
234 if (!assembly) {
235 if (end)
236 fprintf(stderr, "Unable to allocate buffer to read input file\n");
237 else
238 fprintf(stderr, "Failed to read input file\n");
239
240 goto end;
241 }
242
243 /* Disassemble i965 instructions from buffer assembly */
244 brw_disassemble(devinfo, assembly, start, end, stdout);
245
246 result = EXIT_SUCCESS;
247
248 end:
249 if (fp)
250 fclose(fp);
251
252 free(file_path);
253 free(assembly);
254 free(devinfo);
255
256 exit(result);
257 }