2 * Copyright © 2018 Intel Corporation
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:
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
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
29 #include "compiler/brw_eu.h"
30 #include "dev/gen_device_info.h"
34 /* Return size of file in bytes pointed by fp */
36 i965_disasm_get_file_size(FILE *fp
)
40 fseek(fp
, 0L, SEEK_END
);
42 fseek(fp
, 0L, SEEK_SET
);
48 i965_disasm_read_binary(FILE *fp
, size_t *end
)
53 *end
= i965_disasm_get_file_size(fp
);
57 assembly
= malloc(*end
+ 1);
61 size
= fread(assembly
, *end
, 1, fp
);
70 static struct gen_device_info
*
71 i965_disasm_init(uint16_t pci_id
)
73 struct gen_device_info
*devinfo
;
75 devinfo
= malloc(sizeof *devinfo
);
79 if (!gen_get_device_info(pci_id
, devinfo
)) {
80 fprintf(stderr
, "can't find device information: pci_id=0x%x\n",
85 /* initialize compaction table in order to handle compacted instructions */
86 brw_init_compaction_tables(devinfo
);
92 print_help(const char *progname
, FILE *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",
104 int main(int argc
, char *argv
[])
107 void *assembly
= NULL
;
108 char *binary_path
= NULL
;
109 size_t start
= 0, end
= 0;
112 struct gen_device_info
*devinfo
;
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'},
123 while ((c
= getopt_long(argc
, argv
, "", i965_disasm_opts
, &i
)) != -1) {
126 const int id
= gen_device_name_to_pci_device_id(optarg
);
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 */
142 binary_path
= strdup(optarg
);
143 fp
= fopen(binary_path
, "rb");
145 fprintf(stderr
, "Unable to read input binary file : %s\n",
147 /* free binary_path if path is wrong */
153 /* Clean up binary path if given option is wrong */
162 if (help
|| !binary_path
|| !pci_id
) {
163 print_help(argv
[0], stderr
);
167 devinfo
= i965_disasm_init(pci_id
);
169 fprintf(stderr
, "Unable to allocate memory for "
170 "gen_device_info struct instance.\n");
174 assembly
= i965_disasm_read_binary(fp
, &end
);
177 fprintf(stderr
, "Unable to allocate buffer to read binary file\n");
179 fprintf(stderr
, "Input file is empty\n");
184 /* Disassemble i965 instructions from buffer assembly */
185 brw_disassemble(devinfo
, assembly
, start
, end
, stdout
);