intel/tools: make sure the binary file is properly read
[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
32 uint64_t INTEL_DEBUG;
33
34 /* Return size of file in bytes pointed by fp */
35 static size_t
36 i965_disasm_get_file_size(FILE *fp)
37 {
38 size_t size;
39
40 fseek(fp, 0L, SEEK_END);
41 size = ftell(fp);
42 fseek(fp, 0L, SEEK_SET);
43
44 return size;
45 }
46
47 static void *
48 i965_disasm_read_binary(FILE *fp, size_t *end)
49 {
50 size_t size;
51 void *assembly;
52
53 *end = i965_disasm_get_file_size(fp);
54 if (!*end)
55 return NULL;
56
57 assembly = malloc(*end + 1);
58 if (assembly == NULL)
59 return NULL;
60
61 size = fread(assembly, *end, 1, fp);
62 fclose(fp);
63 if (!size) {
64 free(assembly);
65 return NULL;
66 }
67 return assembly;
68 }
69
70 static struct gen_device_info *
71 i965_disasm_init(uint16_t pci_id)
72 {
73 struct gen_device_info *devinfo;
74
75 devinfo = malloc(sizeof *devinfo);
76 if (devinfo == NULL)
77 return NULL;
78
79 if (!gen_get_device_info(pci_id, devinfo)) {
80 fprintf(stderr, "can't find device information: pci_id=0x%x\n",
81 pci_id);
82 exit(EXIT_FAILURE);
83 }
84
85 /* initialize compaction table in order to handle compacted instructions */
86 brw_init_compaction_tables(devinfo);
87
88 return devinfo;
89 }
90
91 static void
92 print_help(const char *progname, FILE *file)
93 {
94 fprintf(file,
95 "Usage: %s [OPTION]...\n"
96 "Disassemble i965 instructions from binary file.\n\n"
97 " --help display this help and exit\n"
98 " --binary-path=PATH read binary file from binary file PATH\n"
99 " --gen=platform disassemble instructions for given \n"
100 " platform (3 letter platform name)\n",
101 progname);
102 }
103
104 int main(int argc, char *argv[])
105 {
106 FILE *fp = NULL;
107 void *assembly = NULL;
108 char *binary_path = NULL;
109 size_t start = 0, end = 0;
110 uint16_t pci_id = 0;
111 int c, i;
112 struct gen_device_info *devinfo;
113
114 bool help = false;
115 const struct option i965_disasm_opts[] = {
116 { "help", no_argument, (int *) &help, true },
117 { "binary-path", required_argument, NULL, 'b' },
118 { "gen", required_argument, NULL, 'g'},
119 { NULL, 0, NULL, 0 }
120 };
121
122 i = 0;
123 while ((c = getopt_long(argc, argv, "", i965_disasm_opts, &i)) != -1) {
124 switch (c) {
125 case 'g': {
126 const int id = gen_device_name_to_pci_device_id(optarg);
127 if (id < 0) {
128 fprintf(stderr, "can't parse gen: '%s', expected 3 letter "
129 "platform name\n", optarg);
130 /* Clean up binary path if given pci id is wrong */
131 if (binary_path) {
132 free(binary_path);
133 fclose(fp);
134 }
135 exit(EXIT_FAILURE);
136 } else {
137 pci_id = id;
138 }
139 break;
140 }
141 case 'b':
142 binary_path = strdup(optarg);
143 fp = fopen(binary_path, "rb");
144 if (!fp) {
145 fprintf(stderr, "Unable to read input binary file : %s\n",
146 binary_path);
147 /* free binary_path if path is wrong */
148 free(binary_path);
149 exit(EXIT_FAILURE);
150 }
151 break;
152 default:
153 /* Clean up binary path if given option is wrong */
154 if (binary_path) {
155 free(binary_path);
156 fclose(fp);
157 }
158 break;
159 }
160 }
161
162 if (help || !binary_path || !pci_id) {
163 print_help(argv[0], stderr);
164 exit(0);
165 }
166
167 devinfo = i965_disasm_init(pci_id);
168 if (!devinfo) {
169 fprintf(stderr, "Unable to allocate memory for "
170 "gen_device_info struct instance.\n");
171 exit(EXIT_FAILURE);
172 }
173
174 assembly = i965_disasm_read_binary(fp, &end);
175 if (!assembly) {
176 if (end)
177 fprintf(stderr, "Unable to allocate buffer to read binary file\n");
178 else
179 fprintf(stderr, "Input file is empty\n");
180
181 exit(EXIT_FAILURE);
182 }
183
184 /* Disassemble i965 instructions from buffer assembly */
185 brw_disassemble(devinfo, assembly, start, end, stdout);
186
187 free(binary_path);
188 free(assembly);
189 free(devinfo);
190
191 return EXIT_SUCCESS;
192 }