summaryrefslogtreecommitdiffstats
path: root/src/intel/tools/i965_disasm.c
blob: 79434e6462af6a563d8497a410ab17fada413bd0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
 * Copyright © 2018 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>

#include "compiler/brw_eu.h"
#include "dev/gen_device_info.h"

uint64_t INTEL_DEBUG;

/* Return size of file in bytes pointed by fp */
static size_t
i965_disasm_get_file_size(FILE *fp)
{
   size_t size;

   fseek(fp, 0L, SEEK_END);
   size = ftell(fp);
   fseek(fp, 0L, SEEK_SET);

   return size;
}

static void *
i965_disasm_read_binary(FILE *fp, size_t *end)
{
   size_t size;
   void *assembly;

   *end = i965_disasm_get_file_size(fp);
   if (!*end)
      return NULL;

   assembly = malloc(*end + 1);
   if (assembly == NULL)
      return NULL;

   size = fread(assembly, *end, 1, fp);
   fclose(fp);
   if (!size) {
      free(assembly);
      return NULL;
   }
   return assembly;
}

static struct gen_device_info *
i965_disasm_init(uint16_t pci_id)
{
   struct gen_device_info *devinfo;

   devinfo = malloc(sizeof *devinfo);
   if (devinfo == NULL)
      return NULL;

   if (!gen_get_device_info(pci_id, devinfo)) {
      fprintf(stderr, "can't find device information: pci_id=0x%x\n",
              pci_id);
      exit(EXIT_FAILURE);
   }

   /* initialize compaction table in order to handle compacted instructions */
   brw_init_compaction_tables(devinfo);

   return devinfo;
}

static void
print_help(const char *progname, FILE *file)
{
   fprintf(file,
           "Usage: %s [OPTION]...\n"
           "Disassemble i965 instructions from binary file.\n\n"
           "      --help             display this help and exit\n"
           "      --binary-path=PATH read binary file from binary file PATH\n"
           "      --gen=platform     disassemble instructions for given \n"
           "                         platform (3 letter platform name)\n",
           progname);
}

int main(int argc, char *argv[])
{
   FILE *fp = NULL;
   void *assembly = NULL;
   char *binary_path = NULL;
   size_t start = 0, end = 0;
   uint16_t pci_id = 0;
   int c, i;
   struct gen_device_info *devinfo;

   bool help = false;
   const struct option i965_disasm_opts[] = {
      { "help",          no_argument,       (int *) &help,      true },
      { "binary-path",   required_argument, NULL,               'b' },
      { "gen",           required_argument, NULL,               'g'},
      { NULL,            0,                 NULL,                0 }
   };

   i = 0;
   while ((c = getopt_long(argc, argv, "", i965_disasm_opts, &i)) != -1) {
      switch (c) {
      case 'g': {
         const int id = gen_device_name_to_pci_device_id(optarg);
         if (id < 0) {
            fprintf(stderr, "can't parse gen: '%s', expected 3 letter "
                            "platform name\n", optarg);
            /* Clean up binary path if given pci id is wrong */
            if (binary_path) {
               free(binary_path);
               fclose(fp);
            }
            exit(EXIT_FAILURE);
         } else {
            pci_id = id;
         }
         break;
      }
      case 'b':
         binary_path = strdup(optarg);
         fp = fopen(binary_path, "rb");
         if (!fp) {
            fprintf(stderr, "Unable to read input binary file : %s\n",
                    binary_path);
            /* free binary_path if path is wrong */
            free(binary_path);
            exit(EXIT_FAILURE);
         }
         break;
      default:
         /* Clean up binary path if given option is wrong */
         if (binary_path) {
            free(binary_path);
            fclose(fp);
         }
         break;
      }
   }

   if (help || !binary_path || !pci_id) {
      print_help(argv[0], stderr);
      exit(0);
   }

   devinfo = i965_disasm_init(pci_id);
   if (!devinfo) {
      fprintf(stderr, "Unable to allocate memory for "
                      "gen_device_info struct instance.\n");
      exit(EXIT_FAILURE);
   }

   assembly = i965_disasm_read_binary(fp, &end);
   if (!assembly) {
      if (end)
        fprintf(stderr, "Unable to allocate buffer to read binary file\n");
      else
        fprintf(stderr, "Input file is empty\n");

      exit(EXIT_FAILURE);
   }

   /* Disassemble i965 instructions from buffer assembly */
   brw_disassemble(devinfo, assembly, start, end, stdout);

   free(binary_path);
   free(assembly);
   free(devinfo);

   return EXIT_SUCCESS;
}