intel/tools/error: Decode shaders while decoding batch commands.
[mesa.git] / src / intel / tools / aubinator_error_decode.c
1 /*
2 * Copyright © 2007-2017 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
25 #include <stdbool.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <stdarg.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <inttypes.h>
32 #include <errno.h>
33 #include <sys/stat.h>
34 #include <sys/types.h>
35 #include <sys/wait.h>
36 #include <err.h>
37 #include <assert.h>
38 #include <getopt.h>
39 #include <zlib.h>
40
41 #include "common/gen_decoder.h"
42 #include "util/macros.h"
43 #include "gen_disasm.h"
44
45 #define CSI "\e["
46 #define BLUE_HEADER CSI "0;44m"
47 #define GREEN_HEADER CSI "1;42m"
48 #define NORMAL CSI "0m"
49
50 #define MIN(a, b) ((a) < (b) ? (a) : (b))
51
52 /* options */
53
54 static bool option_full_decode = true;
55 static bool option_print_offsets = true;
56 static enum { COLOR_AUTO, COLOR_ALWAYS, COLOR_NEVER } option_color;
57 static char *xml_path = NULL;
58
59 static uint32_t
60 print_head(unsigned int reg)
61 {
62 printf(" head = 0x%08x, wraps = %d\n", reg & (0x7ffff<<2), reg >> 21);
63 return reg & (0x7ffff<<2);
64 }
65
66 static void
67 print_register(struct gen_spec *spec, const char *name, uint32_t reg)
68 {
69 struct gen_group *reg_spec = gen_spec_find_register_by_name(spec, name);
70
71 if (reg_spec)
72 gen_print_group(stdout, reg_spec, 0, &reg, option_color == COLOR_ALWAYS);
73 }
74
75 struct ring_register_mapping {
76 const char *ring_name;
77 const char *register_name;
78 };
79
80 static const struct ring_register_mapping acthd_registers[] = {
81 { "blt", "BCS_ACTHD_UDW" },
82 { "bsd", "VCS_ACTHD_UDW" },
83 { "bsd2", "VCS2_ACTHD_UDW" },
84 { "render", "ACTHD_UDW" },
85 { "vebox", "VECS_ACTHD_UDW" },
86 };
87
88 static const struct ring_register_mapping ctl_registers[] = {
89 { "blt", "BCS_RING_BUFFER_CTL" },
90 { "bsd", "VCS_RING_BUFFER_CTL" },
91 { "bsd2", "VCS2_RING_BUFFER_CTL" },
92 { "render", "RCS_RING_BUFFER_CTL" },
93 { "vebox", "VECS_RING_BUFFER_CTL" },
94 };
95
96 static const struct ring_register_mapping fault_registers[] = {
97 { "blt", "BCS_FAULT_REG" },
98 { "bsd", "VCS_FAULT_REG" },
99 { "render", "RCS_FAULT_REG" },
100 { "vebox", "VECS_FAULT_REG" },
101 };
102
103 static const char *
104 register_name_from_ring(const struct ring_register_mapping *mapping,
105 unsigned nb_mapping,
106 const char *ring_name)
107 {
108 for (unsigned i = 0; i < nb_mapping; i++) {
109 if (strcmp(mapping[i].ring_name, ring_name) == 0)
110 return mapping[i].register_name;
111 }
112 return NULL;
113 }
114
115 static const char *
116 instdone_register_for_ring(const struct gen_device_info *devinfo,
117 const char *ring_name)
118 {
119 if (strcmp(ring_name, "blt") == 0)
120 return "BCS_INSTDONE";
121 else if (strcmp(ring_name, "vebox") == 0)
122 return "VECS_INSTDONE";
123 else if (strcmp(ring_name, "bsd") == 0)
124 return "VCS_INSTDONE";
125 else if (strcmp(ring_name, "render") == 0) {
126 if (devinfo->gen == 6)
127 return "INSTDONE_2";
128 return "INSTDONE_1";
129 }
130
131 return NULL;
132 }
133
134 static void
135 print_pgtbl_err(unsigned int reg, struct gen_device_info *devinfo)
136 {
137 if (reg & (1 << 26))
138 printf(" Invalid Sampler Cache GTT entry\n");
139 if (reg & (1 << 24))
140 printf(" Invalid Render Cache GTT entry\n");
141 if (reg & (1 << 23))
142 printf(" Invalid Instruction/State Cache GTT entry\n");
143 if (reg & (1 << 22))
144 printf(" There is no ROC, this cannot occur!\n");
145 if (reg & (1 << 21))
146 printf(" Invalid GTT entry during Vertex Fetch\n");
147 if (reg & (1 << 20))
148 printf(" Invalid GTT entry during Command Fetch\n");
149 if (reg & (1 << 19))
150 printf(" Invalid GTT entry during CS\n");
151 if (reg & (1 << 18))
152 printf(" Invalid GTT entry during Cursor Fetch\n");
153 if (reg & (1 << 17))
154 printf(" Invalid GTT entry during Overlay Fetch\n");
155 if (reg & (1 << 8))
156 printf(" Invalid GTT entry during Display B Fetch\n");
157 if (reg & (1 << 4))
158 printf(" Invalid GTT entry during Display A Fetch\n");
159 if (reg & (1 << 1))
160 printf(" Valid PTE references illegal memory\n");
161 if (reg & (1 << 0))
162 printf(" Invalid GTT entry during fetch for host\n");
163 }
164
165 static void
166 print_snb_fence(struct gen_device_info *devinfo, uint64_t fence)
167 {
168 printf(" %svalid, %c-tiled, pitch: %i, start: 0x%08x, size: %u\n",
169 fence & 1 ? "" : "in",
170 fence & (1<<1) ? 'y' : 'x',
171 (int)(((fence>>32)&0xfff)+1)*128,
172 (uint32_t)fence & 0xfffff000,
173 (uint32_t)(((fence>>32)&0xfffff000) - (fence&0xfffff000) + 4096));
174 }
175
176 static void
177 print_i965_fence(struct gen_device_info *devinfo, uint64_t fence)
178 {
179 printf(" %svalid, %c-tiled, pitch: %i, start: 0x%08x, size: %u\n",
180 fence & 1 ? "" : "in",
181 fence & (1<<1) ? 'y' : 'x',
182 (int)(((fence>>2)&0x1ff)+1)*128,
183 (uint32_t)fence & 0xfffff000,
184 (uint32_t)(((fence>>32)&0xfffff000) - (fence&0xfffff000) + 4096));
185 }
186
187 static void
188 print_fence(struct gen_device_info *devinfo, uint64_t fence)
189 {
190 if (devinfo->gen == 6 || devinfo->gen == 7) {
191 return print_snb_fence(devinfo, fence);
192 } else if (devinfo->gen == 4 || devinfo->gen == 5) {
193 return print_i965_fence(devinfo, fence);
194 }
195 }
196
197 static void
198 print_fault_data(struct gen_device_info *devinfo, uint32_t data1, uint32_t data0)
199 {
200 uint64_t address;
201
202 if (devinfo->gen < 8)
203 return;
204
205 address = ((uint64_t)(data0) << 12) | ((uint64_t)data1 & 0xf) << 44;
206 printf(" Address 0x%016" PRIx64 " %s\n", address,
207 data1 & (1 << 4) ? "GGTT" : "PPGTT");
208 }
209
210 #define CSI "\e["
211 #define NORMAL CSI "0m"
212
213 struct section {
214 uint64_t gtt_offset;
215 char *ring_name;
216 const char *buffer_name;
217 uint32_t *data;
218 int count;
219 };
220
221 #define MAX_SECTIONS 30
222 static struct section sections[MAX_SECTIONS];
223
224 static void
225 disassemble_program(struct gen_disasm *disasm, const char *type,
226 const struct section *instruction_section,
227 uint64_t ksp)
228 {
229 if (!instruction_section)
230 return;
231
232 printf("\nReferenced %s:\n", type);
233 gen_disasm_disassemble(disasm, instruction_section->data, ksp, stdout);
234 }
235
236 static void
237 decode(struct gen_spec *spec, struct gen_disasm *disasm,
238 const struct section *section)
239 {
240 uint64_t gtt_offset = section->gtt_offset;
241 uint32_t *data = section->data;
242 uint32_t *p, *end = (data + section->count);
243 int length;
244 struct gen_group *inst;
245 const struct section *current_instruction_buffer = NULL;
246
247 for (p = data; p < end; p += length) {
248 const char *color = option_full_decode ? BLUE_HEADER : NORMAL,
249 *reset_color = NORMAL;
250 uint64_t offset = gtt_offset + 4 * (p - data);
251
252 inst = gen_spec_find_instruction(spec, p);
253 length = gen_group_get_length(inst, p);
254 assert(inst == NULL || length > 0);
255 length = MAX2(1, length);
256 if (inst == NULL) {
257 printf("unknown instruction %08x\n", p[0]);
258 continue;
259 }
260 if (option_color == COLOR_NEVER) {
261 color = "";
262 reset_color = "";
263 }
264
265 printf("%s0x%08"PRIx64": 0x%08x: %-80s%s\n",
266 color, offset, p[0], gen_group_get_name(inst), reset_color);
267
268 gen_print_group(stdout, inst, offset, p,
269 option_color == COLOR_ALWAYS);
270
271 if (strcmp(inst->name, "MI_BATCH_BUFFER_END") == 0)
272 break;
273
274 if (strcmp(inst->name, "STATE_BASE_ADDRESS") == 0) {
275 struct gen_field_iterator iter;
276 gen_field_iterator_init(&iter, inst, p, false);
277
278 while (gen_field_iterator_next(&iter)) {
279 if (strcmp(iter.name, "Instruction Base Address") == 0) {
280 uint64_t instr_base_address = strtol(iter.value, NULL, 16);
281 current_instruction_buffer = NULL;
282 for (int s = 0; s < MAX_SECTIONS; s++) {
283 if (sections[s].gtt_offset == instr_base_address) {
284 current_instruction_buffer = &sections[s];
285 }
286 }
287 }
288 }
289 } else if (strcmp(inst->name, "WM_STATE") == 0 ||
290 strcmp(inst->name, "3DSTATE_PS") == 0 ||
291 strcmp(inst->name, "3DSTATE_WM") == 0) {
292 struct gen_field_iterator iter;
293 gen_field_iterator_init(&iter, inst, p, false);
294 uint64_t ksp[3] = {0, 0, 0};
295 bool enabled[3] = {false, false, false};
296
297 while (gen_field_iterator_next(&iter)) {
298 if (strncmp(iter.name, "Kernel Start Pointer ",
299 strlen("Kernel Start Pointer ")) == 0) {
300 int idx = iter.name[strlen("Kernel Start Pointer ")] - '0';
301 ksp[idx] = strtol(iter.value, NULL, 16);
302 } else if (strcmp(iter.name, "8 Pixel Dispatch Enable") == 0) {
303 enabled[0] = strcmp(iter.value, "true") == 0;
304 } else if (strcmp(iter.name, "16 Pixel Dispatch Enable") == 0) {
305 enabled[1] = strcmp(iter.value, "true") == 0;
306 } else if (strcmp(iter.name, "32 Pixel Dispatch Enable") == 0) {
307 enabled[2] = strcmp(iter.value, "true") == 0;
308 }
309 }
310
311 /* Reorder KSPs to be [8, 16, 32] instead of the hardware order. */
312 if (enabled[0] + enabled[1] + enabled[2] == 1) {
313 if (enabled[1]) {
314 ksp[1] = ksp[0];
315 ksp[0] = 0;
316 } else if (enabled[2]) {
317 ksp[2] = ksp[0];
318 ksp[0] = 0;
319 }
320 } else {
321 uint64_t tmp = ksp[1];
322 ksp[1] = ksp[2];
323 ksp[2] = tmp;
324 }
325
326 /* FINISHME: Broken for multi-program WM_STATE,
327 * which Mesa does not use
328 */
329 if (enabled[0]) {
330 disassemble_program(disasm, "SIMD8 fragment shader",
331 current_instruction_buffer, ksp[0]);
332 }
333 if (enabled[1]) {
334 disassemble_program(disasm, "SIMD16 fragment shader",
335 current_instruction_buffer, ksp[1]);
336 }
337 if (enabled[2]) {
338 disassemble_program(disasm, "SIMD32 fragment shader",
339 current_instruction_buffer, ksp[2]);
340 }
341 printf("\n");
342 } else if (strcmp(inst->name, "VS_STATE") == 0 ||
343 strcmp(inst->name, "GS_STATE") == 0 ||
344 strcmp(inst->name, "SF_STATE") == 0 ||
345 strcmp(inst->name, "CLIP_STATE") == 0 ||
346 strcmp(inst->name, "3DSTATE_DS") == 0 ||
347 strcmp(inst->name, "3DSTATE_HS") == 0 ||
348 strcmp(inst->name, "3DSTATE_GS") == 0 ||
349 strcmp(inst->name, "3DSTATE_VS") == 0) {
350 struct gen_field_iterator iter;
351 gen_field_iterator_init(&iter, inst, p, false);
352 uint64_t ksp = 0;
353 bool is_simd8 = false; /* vertex shaders on Gen8+ only */
354 bool is_enabled = true;
355
356 while (gen_field_iterator_next(&iter)) {
357 if (strcmp(iter.name, "Kernel Start Pointer") == 0) {
358 ksp = strtol(iter.value, NULL, 16);
359 } else if (strcmp(iter.name, "SIMD8 Dispatch Enable") == 0) {
360 is_simd8 = strcmp(iter.value, "true") == 0;
361 } else if (strcmp(iter.name, "Dispatch Enable") == 0) {
362 is_simd8 = strcmp(iter.value, "SIMD8") == 0;
363 } else if (strcmp(iter.name, "Enable") == 0) {
364 is_enabled = strcmp(iter.value, "true") == 0;
365 }
366 }
367
368 const char *type =
369 strcmp(inst->name, "VS_STATE") == 0 ? "vertex shader" :
370 strcmp(inst->name, "GS_STATE") == 0 ? "geometry shader" :
371 strcmp(inst->name, "SF_STATE") == 0 ? "strips and fans shader" :
372 strcmp(inst->name, "CLIP_STATE") == 0 ? "clip shader" :
373 strcmp(inst->name, "3DSTATE_DS") == 0 ? "tessellation control shader" :
374 strcmp(inst->name, "3DSTATE_HS") == 0 ? "tessellation evaluation shader" :
375 strcmp(inst->name, "3DSTATE_VS") == 0 ? (is_simd8 ? "SIMD8 vertex shader" : "vec4 vertex shader") :
376 strcmp(inst->name, "3DSTATE_GS") == 0 ? (is_simd8 ? "SIMD8 geometry shader" : "vec4 geometry shader") :
377 NULL;
378
379 if (is_enabled) {
380 disassemble_program(disasm, type, current_instruction_buffer, ksp);
381 printf("\n");
382 }
383 }
384 }
385 }
386
387 static int zlib_inflate(uint32_t **ptr, int len)
388 {
389 struct z_stream_s zstream;
390 void *out;
391 const uint32_t out_size = 128*4096; /* approximate obj size */
392
393 memset(&zstream, 0, sizeof(zstream));
394
395 zstream.next_in = (unsigned char *)*ptr;
396 zstream.avail_in = 4*len;
397
398 if (inflateInit(&zstream) != Z_OK)
399 return 0;
400
401 out = malloc(out_size);
402 zstream.next_out = out;
403 zstream.avail_out = out_size;
404
405 do {
406 switch (inflate(&zstream, Z_SYNC_FLUSH)) {
407 case Z_STREAM_END:
408 goto end;
409 case Z_OK:
410 break;
411 default:
412 inflateEnd(&zstream);
413 return 0;
414 }
415
416 if (zstream.avail_out)
417 break;
418
419 out = realloc(out, 2*zstream.total_out);
420 if (out == NULL) {
421 inflateEnd(&zstream);
422 return 0;
423 }
424
425 zstream.next_out = (unsigned char *)out + zstream.total_out;
426 zstream.avail_out = zstream.total_out;
427 } while (1);
428 end:
429 inflateEnd(&zstream);
430 free(*ptr);
431 *ptr = out;
432 return zstream.total_out / 4;
433 }
434
435 static int ascii85_decode(const char *in, uint32_t **out, bool inflate)
436 {
437 int len = 0, size = 1024;
438
439 *out = realloc(*out, sizeof(uint32_t)*size);
440 if (*out == NULL)
441 return 0;
442
443 while (*in >= '!' && *in <= 'z') {
444 uint32_t v = 0;
445
446 if (len == size) {
447 size *= 2;
448 *out = realloc(*out, sizeof(uint32_t)*size);
449 if (*out == NULL)
450 return 0;
451 }
452
453 if (*in == 'z') {
454 in++;
455 } else {
456 v += in[0] - 33; v *= 85;
457 v += in[1] - 33; v *= 85;
458 v += in[2] - 33; v *= 85;
459 v += in[3] - 33; v *= 85;
460 v += in[4] - 33;
461 in += 5;
462 }
463 (*out)[len++] = v;
464 }
465
466 if (!inflate)
467 return len;
468
469 return zlib_inflate(out, len);
470 }
471
472 static void
473 read_data_file(FILE *file)
474 {
475 struct gen_spec *spec = NULL;
476 long long unsigned fence;
477 int matched;
478 char *line = NULL;
479 size_t line_size;
480 uint32_t offset, value;
481 char *ring_name = NULL;
482 struct gen_device_info devinfo;
483 struct gen_disasm *disasm = NULL;
484 int sect_num = 0;
485
486 while (getline(&line, &line_size, file) > 0) {
487 char *new_ring_name = NULL;
488 char *dashes;
489
490 if (sscanf(line, "%m[^ ] command stream\n", &new_ring_name) > 0) {
491 free(ring_name);
492 ring_name = new_ring_name;
493 }
494
495 if (line[0] == ':' || line[0] == '~') {
496 uint32_t *data = NULL;
497 int count = ascii85_decode(line+1, &data, line[0] == ':');
498 if (count == 0) {
499 fprintf(stderr, "ASCII85 decode failed.\n");
500 exit(EXIT_FAILURE);
501 }
502 sections[sect_num].data = data;
503 sections[sect_num].count = count;
504 sect_num++;
505 continue;
506 }
507
508 dashes = strstr(line, "---");
509 if (dashes) {
510 const struct {
511 const char *match;
512 const char *name;
513 } buffers[] = {
514 { "ringbuffer", "ring buffer" },
515 { "gtt_offset", "batch buffer" },
516 { "hw context", "HW Context" },
517 { "hw status", "HW status" },
518 { "wa context", "WA context" },
519 { "wa batchbuffer", "WA batch" },
520 { "user", "user" },
521 { "semaphores", "semaphores", },
522 { "guc log buffer", "GuC log", },
523 { NULL, "unknown" },
524 }, *b;
525
526 free(ring_name);
527 ring_name = malloc(dashes - line);
528 strncpy(ring_name, line, dashes - line);
529 ring_name[dashes - line - 1] = '\0';
530
531 dashes += 4;
532 for (b = buffers; b->match; b++) {
533 if (strncasecmp(dashes, b->match, strlen(b->match)) == 0)
534 break;
535 }
536
537 sections[sect_num].buffer_name = b->name;
538 sections[sect_num].ring_name = strdup(ring_name);
539
540 uint32_t hi, lo;
541 dashes = strchr(dashes, '=');
542 if (dashes && sscanf(dashes, "= 0x%08x %08x\n", &hi, &lo))
543 sections[sect_num].gtt_offset = ((uint64_t) hi) << 32 | lo;
544
545 continue;
546 }
547
548 matched = sscanf(line, "%08x : %08x", &offset, &value);
549 if (matched != 2) {
550 uint32_t reg, reg2;
551
552 /* display reg section is after the ringbuffers, don't mix them */
553 printf("%s", line);
554
555 matched = sscanf(line, "PCI ID: 0x%04x\n", &reg);
556 if (matched == 0)
557 matched = sscanf(line, " PCI ID: 0x%04x\n", &reg);
558 if (matched == 0) {
559 const char *pci_id_start = strstr(line, "PCI ID");
560 if (pci_id_start)
561 matched = sscanf(pci_id_start, "PCI ID: 0x%04x\n", &reg);
562 }
563 if (matched == 1) {
564 if (!gen_get_device_info(reg, &devinfo)) {
565 printf("Unable to identify devid=%x\n", reg);
566 exit(EXIT_FAILURE);
567 }
568
569 disasm = gen_disasm_create(reg);
570
571 printf("Detected GEN%i chipset\n", devinfo.gen);
572
573 if (xml_path == NULL)
574 spec = gen_spec_load(&devinfo);
575 else
576 spec = gen_spec_load_from_path(&devinfo, xml_path);
577 }
578
579 matched = sscanf(line, " CTL: 0x%08x\n", &reg);
580 if (matched == 1) {
581 print_register(spec,
582 register_name_from_ring(ctl_registers,
583 ARRAY_SIZE(ctl_registers),
584 ring_name), reg);
585 }
586
587 matched = sscanf(line, " HEAD: 0x%08x\n", &reg);
588 if (matched == 1)
589 print_head(reg);
590
591 matched = sscanf(line, " ACTHD: 0x%08x\n", &reg);
592 if (matched == 1) {
593 print_register(spec,
594 register_name_from_ring(acthd_registers,
595 ARRAY_SIZE(acthd_registers),
596 ring_name), reg);
597 }
598
599 matched = sscanf(line, " PGTBL_ER: 0x%08x\n", &reg);
600 if (matched == 1 && reg)
601 print_pgtbl_err(reg, &devinfo);
602
603 matched = sscanf(line, " ERROR: 0x%08x\n", &reg);
604 if (matched == 1 && reg) {
605 print_register(spec, "GFX_ARB_ERROR_RPT", reg);
606 }
607
608 matched = sscanf(line, " INSTDONE: 0x%08x\n", &reg);
609 if (matched == 1) {
610 const char *reg_name =
611 instdone_register_for_ring(&devinfo, ring_name);
612 if (reg_name)
613 print_register(spec, reg_name, reg);
614 }
615
616 matched = sscanf(line, " INSTDONE1: 0x%08x\n", &reg);
617 if (matched == 1)
618 print_register(spec, "INSTDONE_1", reg);
619
620 matched = sscanf(line, " fence[%i] = %Lx\n", &reg, &fence);
621 if (matched == 2)
622 print_fence(&devinfo, fence);
623
624 matched = sscanf(line, " FAULT_REG: 0x%08x\n", &reg);
625 if (matched == 1 && reg) {
626 const char *reg_name =
627 register_name_from_ring(fault_registers,
628 ARRAY_SIZE(fault_registers),
629 ring_name);
630 if (reg_name == NULL)
631 reg_name = "FAULT_REG";
632 print_register(spec, reg_name, reg);
633 }
634
635 matched = sscanf(line, " FAULT_TLB_DATA: 0x%08x 0x%08x\n", &reg, &reg2);
636 if (matched == 2)
637 print_fault_data(&devinfo, reg, reg2);
638
639 continue;
640 }
641 }
642
643 free(line);
644 free(ring_name);
645
646 for (int s = 0; s < sect_num; s++) {
647 printf("--- %s (%s) at 0x%08x %08x\n",
648 sections[s].buffer_name, sections[s].ring_name,
649 (unsigned) (sections[s].gtt_offset >> 32),
650 (unsigned) sections[s].gtt_offset);
651
652 if (strcmp(sections[s].buffer_name, "batch buffer") == 0 ||
653 strcmp(sections[s].buffer_name, "ring buffer") == 0 ||
654 strcmp(sections[s].buffer_name, "HW Context") == 0) {
655 decode(spec, disasm, &sections[s]);
656 }
657
658 free(sections[s].ring_name);
659 free(sections[s].data);
660 }
661
662 gen_disasm_destroy(disasm);
663 }
664
665 static void
666 setup_pager(void)
667 {
668 int fds[2];
669 pid_t pid;
670
671 if (!isatty(1))
672 return;
673
674 if (pipe(fds) == -1)
675 return;
676
677 pid = fork();
678 if (pid == -1)
679 return;
680
681 if (pid == 0) {
682 close(fds[1]);
683 dup2(fds[0], 0);
684 execlp("less", "less", "-FRSi", NULL);
685 }
686
687 close(fds[0]);
688 dup2(fds[1], 1);
689 close(fds[1]);
690 }
691
692 static void
693 print_help(const char *progname, FILE *file)
694 {
695 fprintf(file,
696 "Usage: %s [OPTION]... [FILE]\n"
697 "Parse an Intel GPU i915_error_state.\n"
698 "With no FILE, debugfs-dri-directory is probed for in /debug and \n"
699 "/sys/kernel/debug. Otherwise, it may be specified. If a file is given,\n"
700 "it is parsed as an GPU dump in the format of /debug/dri/0/i915_error_state.\n\n"
701 " --help display this help and exit\n"
702 " --headers decode only command headers\n"
703 " --color[=WHEN] colorize the output; WHEN can be 'auto' (default\n"
704 " if omitted), 'always', or 'never'\n"
705 " --no-pager don't launch pager\n"
706 " --no-offsets don't print instruction offsets\n"
707 " --xml=DIR load hardware xml description from directory DIR\n",
708 progname);
709 }
710
711 int
712 main(int argc, char *argv[])
713 {
714 FILE *file;
715 const char *path;
716 struct stat st;
717 int c, i, error;
718 bool help = false, pager = true;
719 const struct option aubinator_opts[] = {
720 { "help", no_argument, (int *) &help, true },
721 { "no-pager", no_argument, (int *) &pager, false },
722 { "no-offsets", no_argument, (int *) &option_print_offsets, false },
723 { "headers", no_argument, (int *) &option_full_decode, false },
724 { "color", required_argument, NULL, 'c' },
725 { "xml", required_argument, NULL, 'x' },
726 { NULL, 0, NULL, 0 }
727 };
728
729 i = 0;
730 while ((c = getopt_long(argc, argv, "", aubinator_opts, &i)) != -1) {
731 switch (c) {
732 case 'c':
733 if (optarg == NULL || strcmp(optarg, "always") == 0)
734 option_color = COLOR_ALWAYS;
735 else if (strcmp(optarg, "never") == 0)
736 option_color = COLOR_NEVER;
737 else if (strcmp(optarg, "auto") == 0)
738 option_color = COLOR_AUTO;
739 else {
740 fprintf(stderr, "invalid value for --color: %s", optarg);
741 exit(EXIT_FAILURE);
742 }
743 break;
744 case 'x':
745 xml_path = strdup(optarg);
746 break;
747 default:
748 break;
749 }
750 }
751
752 if (help || argc == 1) {
753 print_help(argv[0], stderr);
754 exit(EXIT_SUCCESS);
755 }
756
757 if (optind >= argc) {
758 if (isatty(0)) {
759 path = "/sys/class/drm/card0/error";
760 error = stat(path, &st);
761 if (error != 0) {
762 path = "/debug/dri";
763 error = stat(path, &st);
764 }
765 if (error != 0) {
766 path = "/sys/kernel/debug/dri";
767 error = stat(path, &st);
768 }
769 if (error != 0) {
770 errx(1,
771 "Couldn't find i915 debugfs directory.\n\n"
772 "Is debugfs mounted? You might try mounting it with a command such as:\n\n"
773 "\tsudo mount -t debugfs debugfs /sys/kernel/debug\n");
774 }
775 } else {
776 read_data_file(stdin);
777 exit(EXIT_SUCCESS);
778 }
779 } else {
780 path = argv[optind];
781 error = stat(path, &st);
782 if (error != 0) {
783 fprintf(stderr, "Error opening %s: %s\n",
784 path, strerror(errno));
785 exit(EXIT_FAILURE);
786 }
787 }
788
789 if (option_color == COLOR_AUTO)
790 option_color = isatty(1) ? COLOR_ALWAYS : COLOR_NEVER;
791
792 if (isatty(1) && pager)
793 setup_pager();
794
795 if (S_ISDIR(st.st_mode)) {
796 int ret;
797 char *filename;
798
799 ret = asprintf(&filename, "%s/i915_error_state", path);
800 assert(ret > 0);
801 file = fopen(filename, "r");
802 if (!file) {
803 int minor;
804 free(filename);
805 for (minor = 0; minor < 64; minor++) {
806 ret = asprintf(&filename, "%s/%d/i915_error_state", path, minor);
807 assert(ret > 0);
808
809 file = fopen(filename, "r");
810 if (file)
811 break;
812
813 free(filename);
814 }
815 }
816 if (!file) {
817 fprintf(stderr, "Failed to find i915_error_state beneath %s\n",
818 path);
819 return EXIT_FAILURE;
820 }
821 } else {
822 file = fopen(path, "r");
823 if (!file) {
824 fprintf(stderr, "Failed to open %s: %s\n",
825 path, strerror(errno));
826 return EXIT_FAILURE;
827 }
828 }
829
830 read_data_file(file);
831 fclose(file);
832
833 /* close the stdout which is opened to write the output */
834 fflush(stdout);
835 close(1);
836 wait(NULL);
837
838 if (xml_path)
839 free(xml_path);
840
841 return EXIT_SUCCESS;
842 }
843
844 /* vim: set ts=8 sw=8 tw=0 cino=:0,(0 noet :*/