aubinator_viewer.cpp 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 <getopt.h>
28#include <unistd.h>
29#include <fcntl.h>
30#include <string.h>
31#include <errno.h>
32#include <sys/stat.h>
33#include <sys/mman.h>
34#include <sys/types.h>
35#include <ctype.h>
36
37#include "util/macros.h"
38
39#include "aub_read.h"
40#include "aub_mem.h"
41
42#include "common/gen_disasm.h"
43
44#define xtzalloc(name) ((decltype(&name)) calloc(1, sizeof(name)))
45#define xtalloc(name) ((decltype(&name)) malloc(sizeof(name)))
46
47struct aub_file {
48   uint8_t *map, *end, *cursor;
49
50   uint16_t pci_id;
51   char app_name[33];
52
53   /* List of batch buffers to process */
54   struct {
55      const uint8_t *start;
56      const uint8_t *end;
57   } *execs;
58   int n_execs;
59   int n_allocated_execs;
60
61   uint32_t idx_reg_write;
62
63   /* Device state */
64   struct gen_device_info devinfo;
65   struct gen_spec *spec;
66   struct gen_disasm *disasm;
67};
68
69static void
70store_exec_begin(struct aub_file *file)
71{
72   if (unlikely(file->n_execs >= file->n_allocated_execs)) {
73      file->n_allocated_execs = MAX2(2U * file->n_allocated_execs,
74                                     4096 / sizeof(file->execs[0]));
75      file->execs = (decltype(file->execs))
76         realloc(static_cast<void *>(file->execs),
77                 file->n_allocated_execs * sizeof(file->execs[0]));
78   }
79
80   file->execs[file->n_execs++].start = file->cursor;
81}
82
83static void
84store_exec_end(struct aub_file *file)
85{
86   if (file->n_execs > 0 && file->execs[file->n_execs - 1].end == NULL)
87      file->execs[file->n_execs - 1].end = file->cursor;
88}
89
90static void
91handle_mem_write(void *user_data, uint64_t phys_addr,
92                 const void *data, uint32_t data_len)
93{
94   struct aub_file *file = (struct aub_file *) user_data;
95   file->idx_reg_write = 0;
96   store_exec_end(file);
97}
98
99static void
100handle_ring_write(void *user_data, enum gen_engine engine,
101                  const void *ring_data, uint32_t ring_data_len)
102{
103   struct aub_file *file = (struct aub_file *) user_data;
104   file->idx_reg_write = 0;
105   store_exec_begin(file);
106}
107
108static void
109handle_reg_write(void *user_data, uint32_t reg_offset, uint32_t reg_value)
110{
111   struct aub_file *file = (struct aub_file *) user_data;
112
113   /* Only store the first register write of a series (execlist writes take
114    * involve 2 dwords).
115    */
116   if (file->idx_reg_write++ == 0)
117      store_exec_begin(file);
118}
119
120static void
121handle_info(void *user_data, int pci_id, const char *app_name)
122{
123   struct aub_file *file = (struct aub_file *) user_data;
124   store_exec_end(file);
125
126   file->pci_id = pci_id;
127   snprintf(file->app_name, sizeof(app_name), "%s", app_name);
128
129   if (!gen_get_device_info(file->pci_id, &file->devinfo)) {
130      fprintf(stderr, "can't find device information: pci_id=0x%x\n", file->pci_id);
131      exit(EXIT_FAILURE);
132   }
133   file->spec = gen_spec_load(&file->devinfo);
134   file->disasm = gen_disasm_create(&file->devinfo);
135}
136
137static void
138handle_error(void *user_data, const void *aub_data, const char *msg)
139{
140   fprintf(stderr, "ERROR: %s\n", msg);
141}
142
143static struct aub_file *
144aub_file_open(const char *filename)
145{
146   struct aub_file *file;
147   struct stat sb;
148   int fd;
149
150   file = xtzalloc(*file);
151   fd = open(filename, O_RDWR);
152   if (fd == -1) {
153      fprintf(stderr, "open %s failed: %s\n", filename, strerror(errno));
154      exit(EXIT_FAILURE);
155   }
156
157   if (fstat(fd, &sb) == -1) {
158      fprintf(stderr, "stat failed: %s\n", strerror(errno));
159      exit(EXIT_FAILURE);
160   }
161
162   file->map = (uint8_t *) mmap(NULL, sb.st_size,
163                                PROT_READ, MAP_SHARED, fd, 0);
164   if (file->map == MAP_FAILED) {
165      fprintf(stderr, "mmap failed: %s\n", strerror(errno));
166      exit(EXIT_FAILURE);
167   }
168
169   close(fd);
170
171   file->cursor = file->map;
172   file->end = file->map + sb.st_size;
173
174   struct aub_read aub_read = {};
175   aub_read.user_data = file;
176   aub_read.info = handle_info;
177   aub_read.error = handle_error;
178   aub_read.reg_write = handle_reg_write;
179   aub_read.ring_write = handle_ring_write;
180   aub_read.local_write = handle_mem_write;
181   aub_read.phys_write = handle_mem_write;
182   aub_read.ggtt_write = handle_mem_write;
183   aub_read.ggtt_entry_write = handle_mem_write;
184
185   int consumed;
186   while (file->cursor < file->end &&
187          (consumed = aub_read_command(&aub_read, file->cursor,
188                                       file->end - file->cursor)) > 0) {
189      file->cursor += consumed;
190   }
191
192   /* Ensure we have an end on the last register write. */
193   if (file->n_execs > 0 && file->execs[file->n_execs - 1].end == NULL)
194      file->execs[file->n_execs - 1].end = file->end;
195
196   return file;
197}
198
199/**/
200
201static void
202update_mem_for_exec(struct aub_mem *mem, struct aub_file *file, int exec_idx)
203{
204   struct aub_read read = {};
205   read.user_data = mem;
206   read.local_write = aub_mem_local_write;
207   read.phys_write = aub_mem_phys_write;
208   read.ggtt_write = aub_mem_ggtt_write;
209   read.ggtt_entry_write = aub_mem_ggtt_entry_write;
210
211   /* Replay the aub file from the beginning up to just before the
212    * commands we want to read. where the context setup happens.
213    */
214   const uint8_t *iter = file->map;
215   while (iter < file->execs[exec_idx].start) {
216      iter += aub_read_command(&read, iter, file->execs[exec_idx].start - iter);
217   }
218}
219
220/* UI */
221
222#include <epoxy/gl.h>
223
224#include "imgui.h"
225#include "imgui_impl_gtk3.h"
226#include "imgui_impl_opengl3.h"
227
228#include "aubinator_viewer.h"
229#include "aubinator_viewer_urb.h"
230#include "imgui_memory_editor.h"
231
232struct window {
233   struct list_head link; /* link in the global list of windows */
234   struct list_head parent_link; /* link in parent window list of children */
235
236   struct list_head children_windows; /* list of children windows */
237
238   char name[128];
239   bool opened;
240
241   ImVec2 position;
242   ImVec2 size;
243
244   void (*display)(struct window*);
245   void (*destroy)(struct window*);
246};
247
248struct edit_window {
249   struct window base;
250
251   struct aub_mem *mem;
252   uint64_t address;
253   uint32_t len;
254
255   struct gen_batch_decode_bo aub_bo;
256   uint64_t aub_offset;
257
258   struct gen_batch_decode_bo gtt_bo;
259   uint64_t gtt_offset;
260
261   struct MemoryEditor editor;
262};
263
264struct pml4_window {
265   struct window base;
266
267   struct aub_mem *mem;
268};
269
270struct shader_window {
271   struct window base;
272
273   uint64_t address;
274   char *shader;
275   size_t shader_size;
276};
277
278struct urb_window {
279   struct window base;
280
281   uint32_t end_urb_offset;
282   struct aub_decode_urb_stage_state urb_stages[AUB_DECODE_N_STAGE];
283
284   AubinatorViewerUrb urb_view;
285};
286
287struct batch_window {
288   struct window base;
289
290   struct aub_mem mem;
291   struct aub_read read;
292
293   bool uses_ppgtt;
294
295   bool collapsed;
296   int exec_idx;
297
298   struct aub_viewer_decode_cfg decode_cfg;
299   struct aub_viewer_decode_ctx decode_ctx;
300
301   struct pml4_window pml4_window;
302
303   char edit_address[20];
304};
305
306static struct Context {
307   struct aub_file *file;
308   char *input_file;
309   char *xml_path;
310
311   GtkWidget *gtk_window;
312
313   /* UI state*/
314   bool show_commands_window;
315   bool show_registers_window;
316
317   struct aub_viewer_cfg cfg;
318
319   struct list_head windows;
320
321   struct window file_window;
322   struct window commands_window;
323   struct window registers_window;
324} context;
325
326static int
327map_key(int k)
328{
329   return ImGuiKey_COUNT + k;
330}
331
332static bool
333has_ctrl_key(int key)
334{
335   return ImGui::GetIO().KeyCtrl && ImGui::IsKeyPressed(map_key(key));
336}
337
338static bool
339window_has_ctrl_key(int key)
340{
341   return ImGui::IsRootWindowOrAnyChildFocused() && has_ctrl_key(key);
342}
343
344static void
345destroy_window_noop(struct window *win)
346{
347}
348
349/* Shader windows */
350
351static void
352display_shader_window(struct window *win)
353{
354   struct shader_window *window = (struct shader_window *) win;
355
356   if (window->shader) {
357      ImGui::InputTextMultiline("Assembly",
358                                window->shader, window->shader_size,
359                                ImGui::GetContentRegionAvail(),
360                                ImGuiInputTextFlags_ReadOnly);
361   } else {
362      ImGui::Text("Shader not available");
363   }
364}
365
366static void
367destroy_shader_window(struct window *win)
368{
369   struct shader_window *window = (struct shader_window *) win;
370
371   free(window->shader);
372   free(window);
373}
374
375static struct shader_window *
376new_shader_window(struct aub_mem *mem, uint64_t address, const char *desc)
377{
378   struct shader_window *window = xtzalloc(*window);
379
380   snprintf(window->base.name, sizeof(window->base.name),
381            "%s (0x%lx)##%p", desc, address, window);
382
383   list_inithead(&window->base.parent_link);
384   window->base.position = ImVec2(-1, -1);
385   window->base.size = ImVec2(700, 300);
386   window->base.opened = true;
387   window->base.display = display_shader_window;
388   window->base.destroy = destroy_shader_window;
389
390   struct gen_batch_decode_bo shader_bo;
391   if (mem->pml4)
392      shader_bo = aub_mem_get_ppgtt_bo(mem, address);
393   else
394      shader_bo = aub_mem_get_ggtt_bo(mem, address);
395
396   if (shader_bo.map) {
397      FILE *f = open_memstream(&window->shader, &window->shader_size);
398      if (f) {
399         gen_disasm_disassemble(context.file->disasm, shader_bo.map, 0, f);
400         fclose(f);
401      }
402   }
403
404   list_addtail(&window->base.link, &context.windows);
405
406   return window;
407}
408
409/* URB windows */
410
411static void
412display_urb_window(struct window *win)
413{
414   struct urb_window *window = (struct urb_window *) win;
415   static const char *stages[] = {
416      [AUB_DECODE_STAGE_VS] = "VS",
417      [AUB_DECODE_STAGE_HS] = "HS",
418      [AUB_DECODE_STAGE_DS] = "DS",
419      [AUB_DECODE_STAGE_GS] = "GS",
420      [AUB_DECODE_STAGE_PS] = "PS",
421      [AUB_DECODE_STAGE_CS] = "CS",
422   };
423
424   ImGui::Text("URB allocation:");
425   window->urb_view.DrawAllocation("##urb",
426                                   ARRAY_SIZE(window->urb_stages),
427                                   window->end_urb_offset,
428                                   stages,
429                                   &window->urb_stages[0]);
430}
431
432static void
433destroy_urb_window(struct window *win)
434{
435   struct urb_window *window = (struct urb_window *) win;
436
437   free(window);
438}
439
440static struct urb_window *
441new_urb_window(struct aub_viewer_decode_ctx *decode_ctx, uint64_t address)
442{
443   struct urb_window *window = xtzalloc(*window);
444
445   snprintf(window->base.name, sizeof(window->base.name),
446            "URB view (0x%lx)##%p", address, window);
447
448   list_inithead(&window->base.parent_link);
449   window->base.position = ImVec2(-1, -1);
450   window->base.size = ImVec2(700, 300);
451   window->base.opened = true;
452   window->base.display = display_urb_window;
453   window->base.destroy = destroy_urb_window;
454
455   window->end_urb_offset = decode_ctx->end_urb_offset;
456   memcpy(window->urb_stages, decode_ctx->urb_stages, sizeof(window->urb_stages));
457   window->urb_view = AubinatorViewerUrb();
458
459   list_addtail(&window->base.link, &context.windows);
460
461   return window;
462}
463
464/* Memory editor windows */
465
466static uint8_t
467read_edit_window(const uint8_t *data, size_t off)
468{
469   struct edit_window *window = (struct edit_window *) data;
470
471   return *((const uint8_t *) window->gtt_bo.map + window->gtt_offset + off);
472}
473
474static void
475write_edit_window(uint8_t *data, size_t off, uint8_t d)
476{
477   struct edit_window *window = (struct edit_window *) data;
478   uint8_t *gtt = (uint8_t *) window->gtt_bo.map + window->gtt_offset + off;
479   uint8_t *aub = (uint8_t *) window->aub_bo.map + window->aub_offset + off;
480
481   *gtt = *aub = d;
482}
483
484static void
485display_edit_window(struct window *win)
486{
487   struct edit_window *window = (struct edit_window *) win;
488
489   if (window->aub_bo.map && window->gtt_bo.map) {
490      ImGui::BeginChild(ImGui::GetID("##block"));
491      window->editor.DrawContents((uint8_t *) window,
492                                  MIN3(window->len,
493                                       window->gtt_bo.size - window->gtt_offset,
494                                       window->aub_bo.size - window->aub_offset),
495                                  window->address);
496      ImGui::EndChild();
497   } else {
498      ImGui::Text("Memory view at 0x%lx not available", window->address);
499   }
500}
501
502static void
503destroy_edit_window(struct window *win)
504{
505   struct edit_window *window = (struct edit_window *) win;
506
507   if (window->aub_bo.map)
508      mprotect((void *) window->aub_bo.map, 4096, PROT_READ);
509   free(window);
510}
511
512static struct edit_window *
513new_edit_window(struct aub_mem *mem, uint64_t address, uint32_t len)
514{
515   struct edit_window *window = xtzalloc(*window);
516
517   snprintf(window->base.name, sizeof(window->base.name),
518            "Editing aub at 0x%lx##%p", address, window);
519
520   list_inithead(&window->base.parent_link);
521   window->base.position = ImVec2(-1, -1);
522   window->base.size = ImVec2(500, 600);
523   window->base.opened = true;
524   window->base.display = display_edit_window;
525   window->base.destroy = destroy_edit_window;
526
527   window->mem = mem;
528   window->address = address;
529   window->aub_bo = aub_mem_get_ppgtt_addr_aub_data(mem, address);
530   window->gtt_bo = aub_mem_get_ppgtt_addr_data(mem, address);
531   window->len = len;
532   window->editor = MemoryEditor();
533   window->editor.OptShowDataPreview = true;
534   window->editor.OptShowAscii = false;
535   window->editor.ReadFn = read_edit_window;
536   window->editor.WriteFn = write_edit_window;
537
538   if (window->aub_bo.map) {
539      uint64_t unaligned_map = (uint64_t) window->aub_bo.map;
540      window->aub_bo.map = (const void *)(unaligned_map & ~0xffful);
541      window->aub_offset = unaligned_map - (uint64_t) window->aub_bo.map;
542
543      if (mprotect((void *) window->aub_bo.map, window->aub_bo.size, PROT_READ | PROT_WRITE) != 0) {
544         window->aub_bo.map = NULL;
545      }
546   }
547
548   window->gtt_offset = address - window->gtt_bo.addr;
549
550   list_addtail(&window->base.link, &context.windows);
551
552   return window;
553}
554
555/* 4 level page table walk windows */
556
557static void
558display_pml4_level(struct aub_mem *mem, uint64_t table_addr, uint64_t table_virt_addr, int level)
559{
560   if (level == 0)
561      return;
562
563   struct gen_batch_decode_bo table_bo =
564      aub_mem_get_phys_addr_data(mem, table_addr);
565   const uint64_t *table = (const uint64_t *) ((const uint8_t *) table_bo.map +
566                                               table_addr - table_bo.addr);
567   if (!table) {
568      ImGui::TextColored(context.cfg.missing_color, "Page not available");
569      return;
570   }
571
572   uint64_t addr_increment = 1ULL << (12 + 9 * (level - 1));
573
574   if (level == 1) {
575      for (int e = 0; e < 512; e++) {
576         bool available = (table[e] & 1) != 0;
577         uint64_t entry_virt_addr = table_virt_addr + e * addr_increment;
578         if (!available)
579            continue;
580         ImGui::Text("Entry%03i - phys_addr=0x%lx - virt_addr=0x%lx",
581                     e, table[e], entry_virt_addr);
582      }
583   } else {
584      for (int e = 0; e < 512; e++) {
585         bool available = (table[e] & 1) != 0;
586         uint64_t entry_virt_addr = table_virt_addr + e * addr_increment;
587         if (available &&
588             ImGui::TreeNodeEx(&table[e],
589                               available ? ImGuiTreeNodeFlags_Framed : 0,
590                               "Entry%03i - phys_addr=0x%lx - virt_addr=0x%lx",
591                               e, table[e], entry_virt_addr)) {
592            display_pml4_level(mem, table[e] & ~0xffful, entry_virt_addr, level -1);
593            ImGui::TreePop();
594         }
595      }
596   }
597}
598
599static void
600display_pml4_window(struct window *win)
601{
602   struct pml4_window *window = (struct pml4_window *) win;
603
604   ImGui::Text("pml4: %lx", window->mem->pml4);
605   ImGui::BeginChild(ImGui::GetID("##block"));
606   display_pml4_level(window->mem, window->mem->pml4, 0, 4);
607   ImGui::EndChild();
608}
609
610static void
611show_pml4_window(struct pml4_window *window, struct aub_mem *mem)
612{
613   if (window->base.opened) {
614      window->base.opened = false;
615      return;
616   }
617
618   snprintf(window->base.name, sizeof(window->base.name),
619            "4-Level page tables##%p", window);
620
621   list_inithead(&window->base.parent_link);
622   window->base.position = ImVec2(-1, -1);
623   window->base.size = ImVec2(500, 600);
624   window->base.opened = true;
625   window->base.display = display_pml4_window;
626   window->base.destroy = destroy_window_noop;
627
628   window->mem = mem;
629
630   list_addtail(&window->base.link, &context.windows);
631}
632
633/* Batch decoding windows */
634
635static void
636display_decode_options(struct aub_viewer_decode_cfg *cfg)
637{
638   char name[40];
639   snprintf(name, sizeof(name), "command filter##%p", &cfg->command_filter);
640   cfg->command_filter.Draw(name); ImGui::SameLine();
641   snprintf(name, sizeof(name), "field filter##%p", &cfg->field_filter);
642   cfg->field_filter.Draw(name); ImGui::SameLine();
643   if (ImGui::Button("Dwords")) cfg->show_dwords ^= 1;
644}
645
646static void
647batch_display_shader(void *user_data, const char *shader_desc, uint64_t address)
648{
649   struct batch_window *window = (struct batch_window *) user_data;
650   struct shader_window *shader_window =
651      new_shader_window(&window->mem, address, shader_desc);
652
653   list_add(&shader_window->base.parent_link, &window->base.children_windows);
654}
655
656static void
657batch_display_urb(void *user_data, const struct aub_decode_urb_stage_state *stages)
658{
659   struct batch_window *window = (struct batch_window *) user_data;
660   struct urb_window *urb_window = new_urb_window(&window->decode_ctx, 0);
661
662   list_add(&urb_window->base.parent_link, &window->base.children_windows);
663}
664
665static void
666batch_edit_address(void *user_data, uint64_t address, uint32_t len)
667{
668   struct batch_window *window = (struct batch_window *) user_data;
669   struct edit_window *edit_window =
670      new_edit_window(&window->mem, address, len);
671
672   list_add(&edit_window->base.parent_link, &window->base.children_windows);
673}
674
675static struct gen_batch_decode_bo
676batch_get_bo(void *user_data, uint64_t address)
677{
678   struct batch_window *window = (struct batch_window *) user_data;
679
680   if (window->uses_ppgtt)
681      return aub_mem_get_ppgtt_bo(&window->mem, address);
682   else
683      return aub_mem_get_ggtt_bo(&window->mem, address);
684}
685
686static void
687update_batch_window(struct batch_window *window, bool reset, int exec_idx)
688{
689   if (reset)
690      aub_mem_fini(&window->mem);
691   aub_mem_init(&window->mem);
692
693   window->exec_idx = MAX2(MIN2(context.file->n_execs - 1, exec_idx), 0);
694   update_mem_for_exec(&window->mem, context.file, window->exec_idx);
695}
696
697static void
698display_batch_ring_write(void *user_data, enum gen_engine engine,
699                         const void *data, uint32_t data_len)
700{
701   struct batch_window *window = (struct batch_window *) user_data;
702
703   window->uses_ppgtt = false;
704
705   aub_viewer_render_batch(&window->decode_ctx, data, data_len, 0);
706}
707
708static void
709display_batch_execlist_write(void *user_data, enum gen_engine engine,
710                             uint64_t context_descriptor)
711{
712   struct batch_window *window = (struct batch_window *) user_data;
713
714   const uint32_t pphwsp_size = 4096;
715   uint32_t pphwsp_addr = context_descriptor & 0xfffff000;
716   struct gen_batch_decode_bo pphwsp_bo =
717      aub_mem_get_ggtt_bo(&window->mem, pphwsp_addr);
718   uint32_t *context_img = (uint32_t *)((uint8_t *)pphwsp_bo.map +
719                                        (pphwsp_addr - pphwsp_bo.addr) +
720                                        pphwsp_size);
721
722   uint32_t ring_buffer_head = context_img[5];
723   uint32_t ring_buffer_tail = context_img[7];
724   uint32_t ring_buffer_start = context_img[9];
725
726   window->mem.pml4 = (uint64_t)context_img[49] << 32 | context_img[51];
727
728   struct gen_batch_decode_bo ring_bo =
729      aub_mem_get_ggtt_bo(&window->mem, ring_buffer_start);
730   assert(ring_bo.size > 0);
731   void *commands = (uint8_t *)ring_bo.map + (ring_buffer_start - ring_bo.addr);
732
733   window->uses_ppgtt = true;
734
735   aub_viewer_render_batch(&window->decode_ctx, commands,
736                           ring_buffer_tail - ring_buffer_head,
737                           ring_buffer_start);
738}
739
740static void
741display_batch_window(struct window *win)
742{
743   struct batch_window *window = (struct batch_window *) win;
744
745   ImGui::PushItemWidth(ImGui::GetContentRegionAvailWidth() / (2 * 2));
746   if (window_has_ctrl_key('f')) ImGui::SetKeyboardFocusHere();
747   display_decode_options(&window->decode_cfg);
748   ImGui::PopItemWidth();
749
750   if (ImGui::InputInt("Execbuf", &window->exec_idx))
751      update_batch_window(window, true, window->exec_idx);
752
753   if (window_has_ctrl_key('p'))
754      update_batch_window(window, true, window->exec_idx - 1);
755   if (window_has_ctrl_key('n'))
756      update_batch_window(window, true, window->exec_idx + 1);
757
758   ImGui::Text("execbuf %i", window->exec_idx);
759   if (ImGui::Button("Show PPGTT")) { show_pml4_window(&window->pml4_window, &window->mem); }
760
761   ImGui::BeginChild(ImGui::GetID("##block"));
762
763   struct aub_read read = {};
764   read.user_data = window;
765   read.ring_write = display_batch_ring_write;
766   read.execlist_write = display_batch_execlist_write;
767
768   const uint8_t *iter = context.file->execs[window->exec_idx].start;
769   while (iter < context.file->execs[window->exec_idx].end) {
770      iter += aub_read_command(&read, iter,
771                               context.file->execs[window->exec_idx].end - iter);
772   }
773
774   ImGui::EndChild();
775}
776
777static void
778destroy_batch_window(struct window *win)
779{
780   struct batch_window *window = (struct batch_window *) win;
781
782   aub_mem_fini(&window->mem);
783
784   /* This works because children windows are inserted at the back of the
785    * list, ensuring the deletion loop goes through the children after calling
786    * this function.
787    */
788   list_for_each_entry(struct window, child_window,
789                       &window->base.children_windows, parent_link)
790      child_window->opened = false;
791   window->pml4_window.base.opened = false;
792
793   free(window);
794}
795
796static void
797new_batch_window(int exec_idx)
798{
799   struct batch_window *window = xtzalloc(*window);
800
801   snprintf(window->base.name, sizeof(window->base.name),
802            "Batch view##%p", window);
803
804   list_inithead(&window->base.parent_link);
805   list_inithead(&window->base.children_windows);
806   window->base.position = ImVec2(-1, -1);
807   window->base.size = ImVec2(600, 700);
808   window->base.opened = true;
809   window->base.display = display_batch_window;
810   window->base.destroy = destroy_batch_window;
811
812   window->collapsed = true;
813   window->decode_cfg = aub_viewer_decode_cfg();
814
815   aub_viewer_decode_ctx_init(&window->decode_ctx,
816                              &context.cfg,
817                              &window->decode_cfg,
818                              context.file->spec,
819                              context.file->disasm,
820                              batch_get_bo,
821                              NULL,
822                              window);
823   window->decode_ctx.display_shader = batch_display_shader;
824   window->decode_ctx.display_urb = batch_display_urb;
825   window->decode_ctx.edit_address = batch_edit_address;
826
827   update_batch_window(window, false, exec_idx);
828
829   list_addtail(&window->base.link, &context.windows);
830}
831
832/**/
833
834static void
835display_registers_window(struct window *win)
836{
837   static struct ImGuiTextFilter filter;
838   if (window_has_ctrl_key('f')) ImGui::SetKeyboardFocusHere();
839   filter.Draw();
840
841   ImGui::BeginChild(ImGui::GetID("##block"));
842   hash_table_foreach(context.file->spec->registers_by_name, entry) {
843      struct gen_group *reg = (struct gen_group *) entry->data;
844      if (filter.PassFilter(reg->name) &&
845          ImGui::CollapsingHeader(reg->name)) {
846         const struct gen_field *field = reg->fields;
847         while (field) {
848            ImGui::Text("%s : %i -> %i\n", field->name, field->start, field->end);
849            field = field->next;
850         }
851      }
852   }
853   ImGui::EndChild();
854}
855
856static void
857show_register_window(void)
858{
859   struct window *window = &context.registers_window;
860
861   if (window->opened) {
862      window->opened = false;
863      return;
864   }
865
866   snprintf(window->name, sizeof(window->name), "Registers");
867
868   list_inithead(&window->parent_link);
869   window->position = ImVec2(-1, -1);
870   window->size = ImVec2(200, 400);
871   window->opened = true;
872   window->display = display_registers_window;
873   window->destroy = destroy_window_noop;
874
875   list_addtail(&window->link, &context.windows);
876}
877
878static void
879display_commands_window(struct window *win)
880{
881   static struct ImGuiTextFilter cmd_filter;
882   if (window_has_ctrl_key('f')) ImGui::SetKeyboardFocusHere();
883   cmd_filter.Draw("name filter");
884   static struct ImGuiTextFilter field_filter;
885   field_filter.Draw("field filter");
886
887   static char opcode_str[9] = { 0, };
888   ImGui::InputText("opcode filter", opcode_str, sizeof(opcode_str),
889                    ImGuiInputTextFlags_CharsHexadecimal);
890   size_t opcode_len = strlen(opcode_str);
891   uint64_t opcode = strtol(opcode_str, NULL, 16);
892
893   static bool show_dwords = true;
894   if (ImGui::Button("Dwords")) show_dwords ^= 1;
895
896   ImGui::BeginChild(ImGui::GetID("##block"));
897   hash_table_foreach(context.file->spec->commands, entry) {
898      struct gen_group *cmd = (struct gen_group *) entry->data;
899      if ((cmd_filter.PassFilter(cmd->name) &&
900           (opcode_len == 0 || (opcode & cmd->opcode_mask) == cmd->opcode)) &&
901          ImGui::CollapsingHeader(cmd->name)) {
902         const struct gen_field *field = cmd->fields;
903         int32_t last_dword = -1;
904         while (field) {
905            if (show_dwords && field->start / 32 != last_dword) {
906               for (last_dword = MAX2(0, last_dword + 1);
907                    last_dword < field->start / 32; last_dword++) {
908                  ImGui::TextColored(context.cfg.dwords_color,
909                                     "Dword %d", last_dword);
910               }
911               ImGui::TextColored(context.cfg.dwords_color, "Dword %d", last_dword);
912            }
913            if (field_filter.PassFilter(field->name))
914               ImGui::Text("%s : %i -> %i\n", field->name, field->start, field->end);
915            field = field->next;
916         }
917      }
918   }
919   hash_table_foreach(context.file->spec->structs, entry) {
920      struct gen_group *cmd = (struct gen_group *) entry->data;
921      if (cmd_filter.PassFilter(cmd->name) && opcode_len == 0 &&
922          ImGui::CollapsingHeader(cmd->name)) {
923         const struct gen_field *field = cmd->fields;
924         int32_t last_dword = -1;
925         while (field) {
926            if (show_dwords && field->start / 32 != last_dword) {
927               last_dword = field->start / 32;
928               ImGui::TextColored(context.cfg.dwords_color,
929                                  "Dword %d", last_dword);
930            }
931            if (field_filter.PassFilter(field->name))
932               ImGui::Text("%s : %i -> %i\n", field->name, field->start, field->end);
933            field = field->next;
934         }
935      }
936   }
937   ImGui::EndChild();
938}
939
940static void
941show_commands_window(void)
942{
943   struct window *window = &context.commands_window;
944
945   if (window->opened) {
946      window->opened = false;
947      return;
948   }
949
950   snprintf(window->name, sizeof(window->name), "Commands & structs");
951
952   list_inithead(&window->parent_link);
953   window->position = ImVec2(-1, -1);
954   window->size = ImVec2(300, 400);
955   window->opened = true;
956   window->display = display_commands_window;
957   window->destroy = destroy_window_noop;
958
959   list_addtail(&window->link, &context.windows);
960}
961
962/* Main window */
963
964static const char *
965human_size(size_t size)
966{
967   unsigned divisions = 0;
968   double v = size;
969   double divider = 1024;
970   while (v >= divider) {
971      v /= divider;
972      divisions++;
973   }
974
975   static const char *units[] = { "Bytes", "Kilobytes", "Megabytes", "Gigabytes" };
976   static char result[20];
977   snprintf(result, sizeof(result), "%.2f %s",
978            v, divisions >= ARRAY_SIZE(units) ? "Too much!" : units[divisions]);
979   return result;
980}
981
982static void
983display_aubfile_window(struct window *win)
984{
985   ImGuiColorEditFlags cflags = (ImGuiColorEditFlags_NoAlpha |
986                                 ImGuiColorEditFlags_NoLabel |
987                                 ImGuiColorEditFlags_NoInputs);
988   struct aub_viewer_cfg *cfg = &context.cfg;
989
990   ImGui::ColorEdit3("background", (float *)&cfg->clear_color, cflags); ImGui::SameLine();
991   ImGui::ColorEdit3("missing", (float *)&cfg->missing_color, cflags); ImGui::SameLine();
992   ImGui::ColorEdit3("error", (float *)&cfg->error_color, cflags); ImGui::SameLine();
993   ImGui::ColorEdit3("highlight", (float *)&cfg->highlight_color, cflags); ImGui::SameLine();
994   ImGui::ColorEdit3("dwords", (float *)&cfg->dwords_color, cflags); ImGui::SameLine();
995
996   if (ImGui::Button("Commands list") || has_ctrl_key('c')) { show_commands_window(); } ImGui::SameLine();
997   if (ImGui::Button("Registers list") || has_ctrl_key('r')) { show_register_window(); } ImGui::SameLine();
998   if (ImGui::Button("Help") || has_ctrl_key('h')) { ImGui::OpenPopup("Help"); }
999
1000   if (ImGui::Button("New batch window") || has_ctrl_key('b')) { new_batch_window(0); }
1001
1002   ImGui::Text("File name:        %s", context.input_file);
1003   ImGui::Text("File size:        %s", human_size(context.file->end - context.file->map));
1004   ImGui::Text("Execbufs          %u", context.file->n_execs);
1005   ImGui::Text("PCI ID:           0x%x", context.file->pci_id);
1006   ImGui::Text("Application name: %s", context.file->app_name);
1007   ImGui::Text(gen_get_device_name(context.file->pci_id));
1008
1009   ImGui::SetNextWindowContentWidth(500);
1010   if (ImGui::BeginPopupModal("Help", NULL, ImGuiWindowFlags_AlwaysAutoResize)) {
1011      ImGui::Text("Some global keybindings:");
1012      ImGui::Separator();
1013
1014      static const char *texts[] = {
1015         "Ctrl-h",          "show this screen",
1016         "Ctrl-c",          "show commands list",
1017         "Ctrl-r",          "show registers list",
1018         "Ctrl-b",          "new batch window",
1019         "Ctrl-p/n",        "switch to previous/next batch buffer",
1020         "Ctrl-Tab",        "switch focus between window",
1021         "Ctrl-left/right", "align window to the side of the screen",
1022      };
1023      float align = 0.0f;
1024      for (uint32_t i = 0; i < ARRAY_SIZE(texts); i += 2)
1025         align = MAX2(align, ImGui::CalcTextSize(texts[i]).x);
1026      align += ImGui::GetStyle().WindowPadding.x + 10;
1027
1028      for (uint32_t i = 0; i < ARRAY_SIZE(texts); i += 2) {
1029         ImGui::Text(texts[i]); ImGui::SameLine(align); ImGui::Text(texts[i + 1]);
1030      }
1031
1032      if (ImGui::Button("Done") || ImGui::IsKeyPressed(ImGuiKey_Escape))
1033         ImGui::CloseCurrentPopup();
1034      ImGui::EndPopup();
1035   }
1036}
1037
1038static void
1039show_aubfile_window(void)
1040{
1041   struct window *window = &context.file_window;
1042
1043   if (window->opened)
1044      return;
1045
1046   snprintf(window->name, sizeof(window->name),
1047            "Aubinator Viewer: Intel AUB file decoder/editor");
1048
1049   list_inithead(&window->parent_link);
1050   window->size = ImVec2(-1, 250);
1051   window->position = ImVec2(0, 0);
1052   window->opened = true;
1053   window->display = display_aubfile_window;
1054   window->destroy = NULL;
1055
1056   list_addtail(&window->link, &context.windows);
1057}
1058
1059/* Main redrawing */
1060
1061static void
1062display_windows(void)
1063{
1064   /* Start by disposing closed windows, we don't want to destroy windows that
1065    * have already been scheduled to be painted. So destroy always happens on
1066    * the next draw cycle, prior to any drawing.
1067    */
1068   list_for_each_entry_safe(struct window, window, &context.windows, link) {
1069      if (window->opened)
1070         continue;
1071
1072      /* Can't close this one. */
1073      if (window == &context.file_window) {
1074         window->opened = true;
1075         continue;
1076      }
1077
1078      list_del(&window->link);
1079      list_del(&window->parent_link);
1080      if (window->destroy)
1081         window->destroy(window);
1082   }
1083
1084   list_for_each_entry(struct window, window, &context.windows, link) {
1085      ImGui::SetNextWindowPos(window->position, ImGuiCond_FirstUseEver);
1086      ImGui::SetNextWindowSize(window->size, ImGuiCond_FirstUseEver);
1087      if (ImGui::Begin(window->name, &window->opened)) {
1088         window->display(window);
1089         window->position = ImGui::GetWindowPos();
1090         window->size = ImGui::GetWindowSize();
1091      }
1092      if (window_has_ctrl_key('w'))
1093         window->opened = false;
1094      ImGui::End();
1095   }
1096}
1097
1098static void
1099repaint_area(GtkGLArea *area, GdkGLContext *gdk_gl_context)
1100{
1101   ImGui_ImplOpenGL3_NewFrame();
1102   ImGui_ImplGtk3_NewFrame();
1103   ImGui::NewFrame();
1104
1105   display_windows();
1106
1107   ImGui::EndFrame();
1108   ImGui::Render();
1109
1110   glClearColor(context.cfg.clear_color.Value.x,
1111                context.cfg.clear_color.Value.y,
1112                context.cfg.clear_color.Value.z, 1.0);
1113   glClear(GL_COLOR_BUFFER_BIT);
1114   ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
1115}
1116
1117static void
1118realize_area(GtkGLArea *area)
1119{
1120   ImGui::CreateContext();
1121   ImGui_ImplGtk3_Init(GTK_WIDGET(area), true);
1122   ImGui_ImplOpenGL3_Init("#version 130");
1123
1124   list_inithead(&context.windows);
1125
1126   ImGui::StyleColorsDark();
1127   context.cfg = aub_viewer_cfg();
1128
1129   ImGuiIO& io = ImGui::GetIO();
1130   io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
1131}
1132
1133static void
1134unrealize_area(GtkGLArea *area)
1135{
1136   gtk_gl_area_make_current(area);
1137
1138   ImGui_ImplOpenGL3_Shutdown();
1139   ImGui_ImplGtk3_Shutdown();
1140   ImGui::DestroyContext();
1141}
1142
1143static void
1144size_allocate_area(GtkGLArea *area,
1145                   GdkRectangle *allocation,
1146                   gpointer user_data)
1147{
1148   if (!gtk_widget_get_realized(GTK_WIDGET(area)))
1149      return;
1150
1151   /* We want to catch only initial size allocate. */
1152   g_signal_handlers_disconnect_by_func(area,
1153                                        (gpointer) size_allocate_area,
1154                                        user_data);
1155   show_aubfile_window();
1156}
1157
1158static void
1159print_help(const char *progname, FILE *file)
1160{
1161   fprintf(file,
1162           "Usage: %s [OPTION]... FILE\n"
1163           "Decode aub file contents from FILE.\n\n"
1164           "      --help             display this help and exit\n"
1165           "  -x, --xml=DIR          load hardware xml description from directory DIR\n",
1166           progname);
1167}
1168
1169int main(int argc, char *argv[])
1170{
1171   int c, i;
1172   bool help = false;
1173   const struct option aubinator_opts[] = {
1174      { "help",          no_argument,       (int *) &help,                 true },
1175      { "xml",           required_argument, NULL,                          'x' },
1176      { NULL,            0,                 NULL,                          0 }
1177   };
1178
1179   memset(&context, 0, sizeof(context));
1180
1181   i = 0;
1182   while ((c = getopt_long(argc, argv, "x:s:", aubinator_opts, &i)) != -1) {
1183      switch (c) {
1184      case 'x':
1185         context.xml_path = strdup(optarg);
1186         break;
1187      default:
1188         break;
1189      }
1190   }
1191
1192   if (optind < argc)
1193      context.input_file = argv[optind];
1194
1195   if (help || !context.input_file) {
1196      print_help(argv[0], stderr);
1197      exit(0);
1198   }
1199
1200   context.file = aub_file_open(context.input_file);
1201
1202   gtk_init(NULL, NULL);
1203
1204   context.gtk_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1205   gtk_window_set_title(GTK_WINDOW(context.gtk_window), "Aubinator Viewer");
1206   g_signal_connect(context.gtk_window, "delete-event", G_CALLBACK(gtk_main_quit), NULL);
1207   gtk_window_resize(GTK_WINDOW(context.gtk_window), 1280, 720);
1208
1209   GtkWidget* gl_area = gtk_gl_area_new();
1210   g_signal_connect(gl_area, "render", G_CALLBACK(repaint_area), NULL);
1211   g_signal_connect(gl_area, "realize", G_CALLBACK(realize_area), NULL);
1212   g_signal_connect(gl_area, "unrealize", G_CALLBACK(unrealize_area), NULL);
1213   g_signal_connect(gl_area, "size_allocate", G_CALLBACK(size_allocate_area), NULL);
1214   gtk_container_add(GTK_CONTAINER(context.gtk_window), gl_area);
1215
1216   gtk_widget_show_all(context.gtk_window);
1217
1218   gtk_main();
1219
1220   free(context.xml_path);
1221
1222   return EXIT_SUCCESS;
1223}
1224