aubinator.c revision 01e04c3f
1/*
2 * Copyright © 2016 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 <stdint.h>
27#include <stdbool.h>
28#include <getopt.h>
29
30#include <unistd.h>
31#include <fcntl.h>
32#include <string.h>
33#include <signal.h>
34#include <errno.h>
35#include <inttypes.h>
36#include <sys/types.h>
37#include <sys/stat.h>
38#include <sys/wait.h>
39#include <sys/mman.h>
40
41#include "util/macros.h"
42
43#include "common/gen_decoder.h"
44#include "aub_read.h"
45#include "aub_mem.h"
46
47#define CSI "\e["
48#define BLUE_HEADER  CSI "0;44m"
49#define GREEN_HEADER CSI "1;42m"
50#define NORMAL       CSI "0m"
51
52/* options */
53
54static int option_full_decode = true;
55static int option_print_offsets = true;
56static int max_vbo_lines = -1;
57static enum { COLOR_AUTO, COLOR_ALWAYS, COLOR_NEVER } option_color;
58
59/* state */
60
61uint16_t pci_id = 0;
62char *input_file = NULL, *xml_path = NULL;
63struct gen_device_info devinfo;
64struct gen_batch_decode_ctx batch_ctx;
65struct aub_mem mem;
66
67FILE *outfile;
68
69struct brw_instruction;
70
71static void
72aubinator_error(void *user_data, const void *aub_data, const char *msg)
73{
74   fprintf(stderr, "%s", msg);
75}
76
77static void
78aubinator_init(void *user_data, int aub_pci_id, const char *app_name)
79{
80   pci_id = aub_pci_id;
81
82   if (!gen_get_device_info(pci_id, &devinfo)) {
83      fprintf(stderr, "can't find device information: pci_id=0x%x\n", pci_id);
84      exit(EXIT_FAILURE);
85   }
86
87   enum gen_batch_decode_flags batch_flags = 0;
88   if (option_color == COLOR_ALWAYS)
89      batch_flags |= GEN_BATCH_DECODE_IN_COLOR;
90   if (option_full_decode)
91      batch_flags |= GEN_BATCH_DECODE_FULL;
92   if (option_print_offsets)
93      batch_flags |= GEN_BATCH_DECODE_OFFSETS;
94   batch_flags |= GEN_BATCH_DECODE_FLOATS;
95
96   gen_batch_decode_ctx_init(&batch_ctx, &devinfo, outfile, batch_flags,
97                             xml_path, NULL, NULL, NULL);
98
99   /* Check for valid spec instance, if wrong xml_path is passed then spec
100    * instance is not initialized properly
101    */
102   if (!batch_ctx.spec) {
103      fprintf(stderr, "Failed to initialize gen_batch_decode_ctx "
104                      "spec instance\n");
105      free(xml_path);
106      gen_batch_decode_ctx_finish(&batch_ctx);
107      exit(EXIT_FAILURE);
108   }
109
110   batch_ctx.max_vbo_decoded_lines = max_vbo_lines;
111
112   char *color = GREEN_HEADER, *reset_color = NORMAL;
113   if (option_color == COLOR_NEVER)
114      color = reset_color = "";
115
116   fprintf(outfile, "%sAubinator: Intel AUB file decoder.%-80s%s\n",
117           color, "", reset_color);
118
119   if (input_file)
120      fprintf(outfile, "File name:        %s\n", input_file);
121
122   if (aub_pci_id)
123      fprintf(outfile, "PCI ID:           0x%x\n", aub_pci_id);
124
125   fprintf(outfile, "Application name: %s\n", app_name);
126
127   fprintf(outfile, "Decoding as:      %s\n", gen_get_device_name(pci_id));
128
129   /* Throw in a new line before the first batch */
130   fprintf(outfile, "\n");
131}
132
133static void
134handle_execlist_write(void *user_data, enum gen_engine engine, uint64_t context_descriptor)
135{
136   const uint32_t pphwsp_size = 4096;
137   uint32_t pphwsp_addr = context_descriptor & 0xfffff000;
138   struct gen_batch_decode_bo pphwsp_bo = aub_mem_get_ggtt_bo(&mem, pphwsp_addr);
139   uint32_t *context = (uint32_t *)((uint8_t *)pphwsp_bo.map +
140                                    (pphwsp_addr - pphwsp_bo.addr) +
141                                    pphwsp_size);
142
143   uint32_t ring_buffer_head = context[5];
144   uint32_t ring_buffer_tail = context[7];
145   uint32_t ring_buffer_start = context[9];
146
147   mem.pml4 = (uint64_t)context[49] << 32 | context[51];
148   batch_ctx.user_data = &mem;
149
150   struct gen_batch_decode_bo ring_bo = aub_mem_get_ggtt_bo(&mem,
151                                                            ring_buffer_start);
152   assert(ring_bo.size > 0);
153   void *commands = (uint8_t *)ring_bo.map + (ring_buffer_start - ring_bo.addr);
154
155   if (context_descriptor & 0x100 /* ppgtt */) {
156      batch_ctx.get_bo = aub_mem_get_ppgtt_bo;
157   } else {
158      batch_ctx.get_bo = aub_mem_get_ggtt_bo;
159   }
160
161   (void)engine; /* TODO */
162   gen_print_batch(&batch_ctx, commands, ring_buffer_tail - ring_buffer_head,
163                   0);
164   aub_mem_clear_bo_maps(&mem);
165}
166
167static void
168handle_ring_write(void *user_data, enum gen_engine engine,
169                  const void *data, uint32_t data_len)
170{
171   batch_ctx.user_data = &mem;
172   batch_ctx.get_bo = aub_mem_get_ggtt_bo;
173
174   gen_print_batch(&batch_ctx, data, data_len, 0);
175
176   aub_mem_clear_bo_maps(&mem);
177}
178
179struct aub_file {
180   FILE *stream;
181
182   void *map, *end, *cursor;
183};
184
185static struct aub_file *
186aub_file_open(const char *filename)
187{
188   struct aub_file *file;
189   struct stat sb;
190   int fd;
191
192   file = calloc(1, sizeof *file);
193   if (file == NULL)
194      return NULL;
195
196   fd = open(filename, O_RDONLY);
197   if (fd == -1) {
198      fprintf(stderr, "open %s failed: %s\n", filename, strerror(errno));
199      free(file);
200      exit(EXIT_FAILURE);
201   }
202
203   if (fstat(fd, &sb) == -1) {
204      fprintf(stderr, "stat failed: %s\n", strerror(errno));
205      free(file);
206      exit(EXIT_FAILURE);
207   }
208
209   file->map = mmap(NULL, sb.st_size,
210                    PROT_READ, MAP_SHARED, fd, 0);
211   if (file->map == MAP_FAILED) {
212      fprintf(stderr, "mmap failed: %s\n", strerror(errno));
213      free(file);
214      exit(EXIT_FAILURE);
215   }
216
217   close(fd);
218
219   file->cursor = file->map;
220   file->end = file->map + sb.st_size;
221
222   return file;
223}
224
225static int
226aub_file_more_stuff(struct aub_file *file)
227{
228   return file->cursor < file->end || (file->stream && !feof(file->stream));
229}
230
231static void
232setup_pager(void)
233{
234   int fds[2];
235   pid_t pid;
236
237   if (!isatty(1))
238      return;
239
240   if (pipe(fds) == -1)
241      return;
242
243   pid = fork();
244   if (pid == -1)
245      return;
246
247   if (pid == 0) {
248      close(fds[1]);
249      dup2(fds[0], 0);
250      execlp("less", "less", "-FRSi", NULL);
251   }
252
253   close(fds[0]);
254   dup2(fds[1], 1);
255   close(fds[1]);
256}
257
258static void
259print_help(const char *progname, FILE *file)
260{
261   fprintf(file,
262           "Usage: %s [OPTION]... FILE\n"
263           "Decode aub file contents from FILE.\n\n"
264           "      --help             display this help and exit\n"
265           "      --gen=platform     decode for given platform (3 letter platform name)\n"
266           "      --headers          decode only command headers\n"
267           "      --color[=WHEN]     colorize the output; WHEN can be 'auto' (default\n"
268           "                         if omitted), 'always', or 'never'\n"
269           "      --max-vbo-lines=N  limit the number of decoded VBO lines\n"
270           "      --no-pager         don't launch pager\n"
271           "      --no-offsets       don't print instruction offsets\n"
272           "      --xml=DIR          load hardware xml description from directory DIR\n",
273           progname);
274}
275
276int main(int argc, char *argv[])
277{
278   struct aub_file *file;
279   int c, i;
280   bool help = false, pager = true;
281   const struct option aubinator_opts[] = {
282      { "help",          no_argument,       (int *) &help,                 true },
283      { "no-pager",      no_argument,       (int *) &pager,                false },
284      { "no-offsets",    no_argument,       (int *) &option_print_offsets, false },
285      { "gen",           required_argument, NULL,                          'g' },
286      { "headers",       no_argument,       (int *) &option_full_decode,   false },
287      { "color",         required_argument, NULL,                          'c' },
288      { "xml",           required_argument, NULL,                          'x' },
289      { "max-vbo-lines", required_argument, NULL,                          'v' },
290      { NULL,            0,                 NULL,                          0 }
291   };
292
293   outfile = stdout;
294
295   i = 0;
296   while ((c = getopt_long(argc, argv, "", aubinator_opts, &i)) != -1) {
297      switch (c) {
298      case 'g': {
299         const int id = gen_device_name_to_pci_device_id(optarg);
300         if (id < 0) {
301            fprintf(stderr, "can't parse gen: '%s', expected brw, g4x, ilk, "
302                            "snb, ivb, hsw, byt, bdw, chv, skl, bxt, kbl, "
303                            "aml, glk, cfl, whl, cnl, icl", optarg);
304            exit(EXIT_FAILURE);
305         } else {
306            pci_id = id;
307         }
308         break;
309      }
310      case 'c':
311         if (optarg == NULL || strcmp(optarg, "always") == 0)
312            option_color = COLOR_ALWAYS;
313         else if (strcmp(optarg, "never") == 0)
314            option_color = COLOR_NEVER;
315         else if (strcmp(optarg, "auto") == 0)
316            option_color = COLOR_AUTO;
317         else {
318            fprintf(stderr, "invalid value for --color: %s", optarg);
319            exit(EXIT_FAILURE);
320         }
321         break;
322      case 'x':
323         xml_path = strdup(optarg);
324         break;
325      case 'v':
326         max_vbo_lines = atoi(optarg);
327         break;
328      default:
329         break;
330      }
331   }
332
333   if (optind < argc)
334      input_file = argv[optind];
335
336   if (help || !input_file) {
337      print_help(argv[0], stderr);
338      exit(0);
339   }
340
341   /* Do this before we redirect stdout to pager. */
342   if (option_color == COLOR_AUTO)
343      option_color = isatty(1) ? COLOR_ALWAYS : COLOR_NEVER;
344
345   if (isatty(1) && pager)
346      setup_pager();
347
348   if (!aub_mem_init(&mem)) {
349      fprintf(stderr, "Unable to create GTT\n");
350      exit(EXIT_FAILURE);
351   }
352
353   file = aub_file_open(input_file);
354   if (!file) {
355      fprintf(stderr, "Unable to allocate buffer to open aub file\n");
356      free(xml_path);
357      exit(EXIT_FAILURE);
358   }
359
360   struct aub_read aub_read = {
361      .user_data = &mem,
362      .error = aubinator_error,
363      .info = aubinator_init,
364
365      .local_write = aub_mem_local_write,
366      .phys_write = aub_mem_phys_write,
367      .ggtt_write = aub_mem_ggtt_write,
368      .ggtt_entry_write = aub_mem_ggtt_entry_write,
369
370      .execlist_write = handle_execlist_write,
371      .ring_write = handle_ring_write,
372   };
373   int consumed;
374   while (aub_file_more_stuff(file) &&
375          (consumed = aub_read_command(&aub_read, file->cursor,
376                                       file->end - file->cursor)) > 0) {
377      file->cursor += consumed;
378   }
379
380   aub_mem_fini(&mem);
381
382   fflush(stdout);
383   /* close the stdout which is opened to write the output */
384   close(1);
385   free(file);
386   free(xml_path);
387
388   wait(NULL);
389   gen_batch_decode_ctx_finish(&batch_ctx);
390
391   return EXIT_SUCCESS;
392}
393