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"
32 /* Return size of file in bytes pointed by fp */
34 i965_disasm_get_file_size(FILE *fp
)
38 fseek(fp
, 0L, SEEK_END
);
40 fseek(fp
, 0L, SEEK_SET
);
46 i965_disasm_read_binary(FILE *fp
, size_t *end
)
51 *end
= i965_disasm_get_file_size(fp
);
55 assembly
= malloc(*end
+ 1);
59 size
= fread(assembly
, *end
, 1, fp
);
68 static struct gen_device_info
*
69 i965_disasm_init(uint16_t pci_id
)
71 struct gen_device_info
*devinfo
;
73 devinfo
= malloc(sizeof *devinfo
);
77 if (!gen_get_device_info_from_pci_id(pci_id
, devinfo
)) {
78 fprintf(stderr
, "can't find device information: pci_id=0x%x\n",
83 /* initialize compaction table in order to handle compacted instructions */
84 brw_init_compaction_tables(devinfo
);
90 print_help(const char *progname
, FILE *file
)
93 "Usage: %s [OPTION]...\n"
94 "Disassemble i965 instructions from binary file.\n\n"
95 " --help display this help and exit\n"
96 " --binary-path=PATH read binary file from binary file PATH\n"
97 " --gen=platform disassemble instructions for given \n"
98 " platform (3 letter platform name)\n",
102 int main(int argc
, char *argv
[])
105 void *assembly
= NULL
;
106 char *binary_path
= NULL
;
107 size_t start
= 0, end
= 0;
110 struct gen_device_info
*devinfo
;
113 const struct option i965_disasm_opts
[] = {
114 { "help", no_argument
, (int *) &help
, true },
115 { "binary-path", required_argument
, NULL
, 'b' },
116 { "gen", required_argument
, NULL
, 'g'},
121 while ((c
= getopt_long(argc
, argv
, "", i965_disasm_opts
, &i
)) != -1) {
124 const int id
= gen_device_name_to_pci_device_id(optarg
);
126 fprintf(stderr
, "can't parse gen: '%s', expected 3 letter "
127 "platform name\n", optarg
);
128 /* Clean up binary path if given pci id is wrong */
140 binary_path
= strdup(optarg
);
141 fp
= fopen(binary_path
, "rb");
143 fprintf(stderr
, "Unable to read input binary file : %s\n",
145 /* free binary_path if path is wrong */
151 /* Clean up binary path if given option is wrong */
160 if (help
|| !binary_path
|| !pci_id
) {
161 print_help(argv
[0], stderr
);
165 devinfo
= i965_disasm_init(pci_id
);
167 fprintf(stderr
, "Unable to allocate memory for "
168 "gen_device_info struct instance.\n");
172 assembly
= i965_disasm_read_binary(fp
, &end
);
175 fprintf(stderr
, "Unable to allocate buffer to read binary file\n");
177 fprintf(stderr
, "Input file is empty\n");
182 /* Disassemble i965 instructions from buffer assembly */
183 brw_disassemble(devinfo
, assembly
, start
, end
, stdout
);