intel/tools/error: Fix null termination of ring name string.
[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 program {
214 const char *type;
215 const char *command;
216 uint64_t command_offset;
217 uint64_t instruction_base_address;
218 uint64_t ksp;
219 };
220
221 #define MAX_NUM_PROGRAMS 4096
222 static struct program programs[MAX_NUM_PROGRAMS];
223 static int idx_program = 0, num_programs = 0;
224
225 static int next_program(void)
226 {
227 int ret = idx_program;
228 idx_program = (idx_program + 1) % MAX_NUM_PROGRAMS;
229 num_programs = MIN(num_programs + 1, MAX_NUM_PROGRAMS);
230 return ret;
231 }
232
233 static void decode(struct gen_spec *spec,
234 uint64_t gtt_offset,
235 uint32_t *data,
236 int count)
237 {
238 uint32_t *p, *end = (data + count);
239 int length;
240 struct gen_group *inst;
241 uint64_t current_instruction_base_address = 0;
242
243 for (p = data; p < end; p += length) {
244 const char *color = option_full_decode ? BLUE_HEADER : NORMAL,
245 *reset_color = NORMAL;
246 uint64_t offset = gtt_offset + 4 * (p - data);
247
248 inst = gen_spec_find_instruction(spec, p);
249 length = gen_group_get_length(inst, p);
250 assert(inst == NULL || length > 0);
251 length = MAX2(1, length);
252 if (inst == NULL) {
253 printf("unknown instruction %08x\n", p[0]);
254 continue;
255 }
256 if (option_color == COLOR_NEVER) {
257 color = "";
258 reset_color = "";
259 }
260
261 printf("%s0x%08"PRIx64": 0x%08x: %-80s%s\n",
262 color, offset, p[0], gen_group_get_name(inst), reset_color);
263
264 gen_print_group(stdout, inst, offset, p,
265 option_color == COLOR_ALWAYS);
266
267 if (strcmp(inst->name, "MI_BATCH_BUFFER_END") == 0)
268 break;
269
270 if (strcmp(inst->name, "STATE_BASE_ADDRESS") == 0) {
271 struct gen_field_iterator iter;
272 gen_field_iterator_init(&iter, inst, p, false);
273
274 while (gen_field_iterator_next(&iter)) {
275 if (strcmp(iter.name, "Instruction Base Address") == 0) {
276 current_instruction_base_address = strtol(iter.value, NULL, 16);
277 }
278 }
279 } else if (strcmp(inst->name, "WM_STATE") == 0 ||
280 strcmp(inst->name, "3DSTATE_PS") == 0 ||
281 strcmp(inst->name, "3DSTATE_WM") == 0) {
282 struct gen_field_iterator iter;
283 gen_field_iterator_init(&iter, inst, p, false);
284 uint64_t ksp[3] = {0, 0, 0};
285 bool enabled[3] = {false, false, false};
286
287 while (gen_field_iterator_next(&iter)) {
288 if (strncmp(iter.name, "Kernel Start Pointer ",
289 strlen("Kernel Start Pointer ")) == 0) {
290 int idx = iter.name[strlen("Kernel Start Pointer ")] - '0';
291 ksp[idx] = strtol(iter.value, NULL, 16);
292 } else if (strcmp(iter.name, "8 Pixel Dispatch Enable") == 0) {
293 enabled[0] = strcmp(iter.value, "true") == 0;
294 } else if (strcmp(iter.name, "16 Pixel Dispatch Enable") == 0) {
295 enabled[1] = strcmp(iter.value, "true") == 0;
296 } else if (strcmp(iter.name, "32 Pixel Dispatch Enable") == 0) {
297 enabled[2] = strcmp(iter.value, "true") == 0;
298 }
299 }
300
301 /* FINISHME: Broken for multi-program WM_STATE,
302 * which Mesa does not use
303 */
304 if (enabled[0] + enabled[1] + enabled[2] == 1) {
305 const char *type = enabled[0] ? "SIMD8 fragment shader" :
306 enabled[1] ? "SIMD16 fragment shader" :
307 enabled[2] ? "SIMD32 fragment shader" : NULL;
308
309 programs[next_program()] = (struct program) {
310 .type = type,
311 .command = inst->name,
312 .command_offset = offset,
313 .instruction_base_address = current_instruction_base_address,
314 .ksp = ksp[0],
315 };
316 } else {
317 if (enabled[0]) /* SIMD8 */ {
318 programs[next_program()] = (struct program) {
319 .type = "SIMD8 fragment shader",
320 .command = inst->name,
321 .command_offset = offset,
322 .instruction_base_address = current_instruction_base_address,
323 .ksp = ksp[0], /* SIMD8 shader is specified by ksp[0] */
324 };
325 }
326 if (enabled[1]) /* SIMD16 */ {
327 programs[next_program()] = (struct program) {
328 .type = "SIMD16 fragment shader",
329 .command = inst->name,
330 .command_offset = offset,
331 .instruction_base_address = current_instruction_base_address,
332 .ksp = ksp[2], /* SIMD16 shader is specified by ksp[2] */
333 };
334 }
335 if (enabled[2]) /* SIMD32 */ {
336 programs[next_program()] = (struct program) {
337 .type = "SIMD32 fragment shader",
338 .command = inst->name,
339 .command_offset = offset,
340 .instruction_base_address = current_instruction_base_address,
341 .ksp = ksp[1], /* SIMD32 shader is specified by ksp[1] */
342 };
343 }
344 }
345 } else if (strcmp(inst->name, "VS_STATE") == 0 ||
346 strcmp(inst->name, "GS_STATE") == 0 ||
347 strcmp(inst->name, "SF_STATE") == 0 ||
348 strcmp(inst->name, "CLIP_STATE") == 0 ||
349 strcmp(inst->name, "3DSTATE_DS") == 0 ||
350 strcmp(inst->name, "3DSTATE_HS") == 0 ||
351 strcmp(inst->name, "3DSTATE_GS") == 0 ||
352 strcmp(inst->name, "3DSTATE_VS") == 0) {
353 struct gen_field_iterator iter;
354 gen_field_iterator_init(&iter, inst, p, false);
355 uint64_t ksp = 0;
356 bool is_simd8 = false; /* vertex shaders on Gen8+ only */
357 bool is_enabled = true;
358
359 while (gen_field_iterator_next(&iter)) {
360 if (strcmp(iter.name, "Kernel Start Pointer") == 0) {
361 ksp = strtol(iter.value, NULL, 16);
362 } else if (strcmp(iter.name, "SIMD8 Dispatch Enable") == 0) {
363 is_simd8 = strcmp(iter.value, "true") == 0;
364 } else if (strcmp(iter.name, "Dispatch Enable") == 0) {
365 is_simd8 = strcmp(iter.value, "SIMD8") == 0;
366 } else if (strcmp(iter.name, "Enable") == 0) {
367 is_enabled = strcmp(iter.value, "true") == 0;
368 }
369 }
370
371 const char *type =
372 strcmp(inst->name, "VS_STATE") == 0 ? "vertex shader" :
373 strcmp(inst->name, "GS_STATE") == 0 ? "geometry shader" :
374 strcmp(inst->name, "SF_STATE") == 0 ? "strips and fans shader" :
375 strcmp(inst->name, "CLIP_STATE") == 0 ? "clip shader" :
376 strcmp(inst->name, "3DSTATE_DS") == 0 ? "tessellation control shader" :
377 strcmp(inst->name, "3DSTATE_HS") == 0 ? "tessellation evaluation shader" :
378 strcmp(inst->name, "3DSTATE_VS") == 0 ? (is_simd8 ? "SIMD8 vertex shader" : "vec4 vertex shader") :
379 strcmp(inst->name, "3DSTATE_GS") == 0 ? (is_simd8 ? "SIMD8 geometry shader" : "vec4 geometry shader") :
380 NULL;
381
382 if (is_enabled) {
383 programs[next_program()] = (struct program) {
384 .type = type,
385 .command = inst->name,
386 .command_offset = offset,
387 .instruction_base_address = current_instruction_base_address,
388 .ksp = ksp,
389 };
390 }
391 }
392 }
393 }
394
395 static int zlib_inflate(uint32_t **ptr, int len)
396 {
397 struct z_stream_s zstream;
398 void *out;
399 const uint32_t out_size = 128*4096; /* approximate obj size */
400
401 memset(&zstream, 0, sizeof(zstream));
402
403 zstream.next_in = (unsigned char *)*ptr;
404 zstream.avail_in = 4*len;
405
406 if (inflateInit(&zstream) != Z_OK)
407 return 0;
408
409 out = malloc(out_size);
410 zstream.next_out = out;
411 zstream.avail_out = out_size;
412
413 do {
414 switch (inflate(&zstream, Z_SYNC_FLUSH)) {
415 case Z_STREAM_END:
416 goto end;
417 case Z_OK:
418 break;
419 default:
420 inflateEnd(&zstream);
421 return 0;
422 }
423
424 if (zstream.avail_out)
425 break;
426
427 out = realloc(out, 2*zstream.total_out);
428 if (out == NULL) {
429 inflateEnd(&zstream);
430 return 0;
431 }
432
433 zstream.next_out = (unsigned char *)out + zstream.total_out;
434 zstream.avail_out = zstream.total_out;
435 } while (1);
436 end:
437 inflateEnd(&zstream);
438 free(*ptr);
439 *ptr = out;
440 return zstream.total_out / 4;
441 }
442
443 static int ascii85_decode(const char *in, uint32_t **out, bool inflate)
444 {
445 int len = 0, size = 1024;
446
447 *out = realloc(*out, sizeof(uint32_t)*size);
448 if (*out == NULL)
449 return 0;
450
451 while (*in >= '!' && *in <= 'z') {
452 uint32_t v = 0;
453
454 if (len == size) {
455 size *= 2;
456 *out = realloc(*out, sizeof(uint32_t)*size);
457 if (*out == NULL)
458 return 0;
459 }
460
461 if (*in == 'z') {
462 in++;
463 } else {
464 v += in[0] - 33; v *= 85;
465 v += in[1] - 33; v *= 85;
466 v += in[2] - 33; v *= 85;
467 v += in[3] - 33; v *= 85;
468 v += in[4] - 33;
469 in += 5;
470 }
471 (*out)[len++] = v;
472 }
473
474 if (!inflate)
475 return len;
476
477 return zlib_inflate(out, len);
478 }
479
480 static void
481 read_data_file(FILE *file)
482 {
483 struct gen_spec *spec = NULL;
484 long long unsigned fence;
485 int matched;
486 char *line = NULL;
487 size_t line_size;
488 uint32_t offset, value;
489 uint64_t gtt_offset = 0;
490 const char *buffer_name = "batch buffer";
491 char *ring_name = NULL;
492 struct gen_device_info devinfo;
493 struct gen_disasm *disasm = NULL;
494
495 while (getline(&line, &line_size, file) > 0) {
496 char *new_ring_name = NULL;
497 char *dashes;
498
499 if (sscanf(line, "%m[^ ] command stream\n", &new_ring_name) > 0) {
500 free(ring_name);
501 ring_name = new_ring_name;
502 }
503
504 if (line[0] == ':' || line[0] == '~') {
505 uint32_t *data = NULL;
506 int count = ascii85_decode(line+1, &data, line[0] == ':');
507 if (count == 0) {
508 fprintf(stderr, "ASCII85 decode failed.\n");
509 exit(EXIT_FAILURE);
510 }
511
512 if (strcmp(buffer_name, "user") == 0) {
513 printf("Disassembly of programs in instruction buffer at "
514 "0x%08"PRIx64":\n", gtt_offset);
515 for (int i = 0; i < num_programs; i++) {
516 int idx = (idx_program + i) % MAX_NUM_PROGRAMS;
517 if (programs[idx].instruction_base_address == gtt_offset) {
518 printf("\n%s (specified by %s at batch offset "
519 "0x%08"PRIx64") at offset 0x%08"PRIx64"\n",
520 programs[idx].type,
521 programs[idx].command,
522 programs[idx].command_offset,
523 programs[idx].ksp);
524 gen_disasm_disassemble(disasm, data, programs[idx].ksp,
525 stdout);
526 }
527 }
528 } else if (strcmp(buffer_name, "batch buffer") == 0 ||
529 strcmp(buffer_name, "ring buffer") == 0 ||
530 strcmp(buffer_name, "HW Context") == 0) {
531 decode(spec, gtt_offset, data, count);
532 }
533 free(data);
534 continue;
535 }
536
537 dashes = strstr(line, "---");
538 if (dashes) {
539 const struct {
540 const char *match;
541 const char *name;
542 } buffers[] = {
543 { "ringbuffer", "ring buffer" },
544 { "gtt_offset", "batch buffer" },
545 { "hw context", "HW Context" },
546 { "hw status", "HW status" },
547 { "wa context", "WA context" },
548 { "wa batchbuffer", "WA batch" },
549 { "user", "user" },
550 { "semaphores", "semaphores", },
551 { "guc log buffer", "GuC log", },
552 { NULL, "unknown" },
553 }, *b;
554
555 printf("%s", line);
556
557 gtt_offset = 0;
558
559 free(ring_name);
560 ring_name = malloc(dashes - line);
561 strncpy(ring_name, line, dashes - line);
562 ring_name[dashes - line - 1] = '\0';
563
564 dashes += 4;
565 for (b = buffers; b->match; b++) {
566 if (strncasecmp(dashes, b->match, strlen(b->match)) == 0)
567 break;
568 }
569
570 buffer_name = b->name;
571
572 uint32_t hi, lo;
573 dashes = strchr(dashes, '=');
574 if (dashes && sscanf(dashes, "= 0x%08x %08x\n", &hi, &lo))
575 gtt_offset = ((uint64_t) hi) << 32 | lo;
576
577 continue;
578 }
579
580 matched = sscanf(line, "%08x : %08x", &offset, &value);
581 if (matched != 2) {
582 uint32_t reg, reg2;
583
584 /* display reg section is after the ringbuffers, don't mix them */
585 printf("%s", line);
586
587 matched = sscanf(line, "PCI ID: 0x%04x\n", &reg);
588 if (matched == 0)
589 matched = sscanf(line, " PCI ID: 0x%04x\n", &reg);
590 if (matched == 0) {
591 const char *pci_id_start = strstr(line, "PCI ID");
592 if (pci_id_start)
593 matched = sscanf(pci_id_start, "PCI ID: 0x%04x\n", &reg);
594 }
595 if (matched == 1) {
596 if (!gen_get_device_info(reg, &devinfo)) {
597 printf("Unable to identify devid=%x\n", reg);
598 exit(EXIT_FAILURE);
599 }
600
601 disasm = gen_disasm_create(reg);
602
603 printf("Detected GEN%i chipset\n", devinfo.gen);
604
605 if (xml_path == NULL)
606 spec = gen_spec_load(&devinfo);
607 else
608 spec = gen_spec_load_from_path(&devinfo, xml_path);
609 }
610
611 matched = sscanf(line, " CTL: 0x%08x\n", &reg);
612 if (matched == 1) {
613 print_register(spec,
614 register_name_from_ring(ctl_registers,
615 ARRAY_SIZE(ctl_registers),
616 ring_name), reg);
617 }
618
619 matched = sscanf(line, " HEAD: 0x%08x\n", &reg);
620 if (matched == 1)
621 print_head(reg);
622
623 matched = sscanf(line, " ACTHD: 0x%08x\n", &reg);
624 if (matched == 1) {
625 print_register(spec,
626 register_name_from_ring(acthd_registers,
627 ARRAY_SIZE(acthd_registers),
628 ring_name), reg);
629 }
630
631 matched = sscanf(line, " PGTBL_ER: 0x%08x\n", &reg);
632 if (matched == 1 && reg)
633 print_pgtbl_err(reg, &devinfo);
634
635 matched = sscanf(line, " ERROR: 0x%08x\n", &reg);
636 if (matched == 1 && reg) {
637 print_register(spec, "GFX_ARB_ERROR_RPT", reg);
638 }
639
640 matched = sscanf(line, " INSTDONE: 0x%08x\n", &reg);
641 if (matched == 1) {
642 const char *reg_name =
643 instdone_register_for_ring(&devinfo, ring_name);
644 if (reg_name)
645 print_register(spec, reg_name, reg);
646 }
647
648 matched = sscanf(line, " INSTDONE1: 0x%08x\n", &reg);
649 if (matched == 1)
650 print_register(spec, "INSTDONE_1", reg);
651
652 matched = sscanf(line, " fence[%i] = %Lx\n", &reg, &fence);
653 if (matched == 2)
654 print_fence(&devinfo, fence);
655
656 matched = sscanf(line, " FAULT_REG: 0x%08x\n", &reg);
657 if (matched == 1 && reg) {
658 const char *reg_name =
659 register_name_from_ring(fault_registers,
660 ARRAY_SIZE(fault_registers),
661 ring_name);
662 if (reg_name == NULL)
663 reg_name = "FAULT_REG";
664 print_register(spec, reg_name, reg);
665 }
666
667 matched = sscanf(line, " FAULT_TLB_DATA: 0x%08x 0x%08x\n", &reg, &reg2);
668 if (matched == 2)
669 print_fault_data(&devinfo, reg, reg2);
670
671 continue;
672 }
673 }
674
675 gen_disasm_destroy(disasm);
676 free(line);
677 free(ring_name);
678 }
679
680 static void
681 setup_pager(void)
682 {
683 int fds[2];
684 pid_t pid;
685
686 if (!isatty(1))
687 return;
688
689 if (pipe(fds) == -1)
690 return;
691
692 pid = fork();
693 if (pid == -1)
694 return;
695
696 if (pid == 0) {
697 close(fds[1]);
698 dup2(fds[0], 0);
699 execlp("less", "less", "-FRSi", NULL);
700 }
701
702 close(fds[0]);
703 dup2(fds[1], 1);
704 close(fds[1]);
705 }
706
707 static void
708 print_help(const char *progname, FILE *file)
709 {
710 fprintf(file,
711 "Usage: %s [OPTION]... [FILE]\n"
712 "Parse an Intel GPU i915_error_state.\n"
713 "With no FILE, debugfs-dri-directory is probed for in /debug and \n"
714 "/sys/kernel/debug. Otherwise, it may be specified. If a file is given,\n"
715 "it is parsed as an GPU dump in the format of /debug/dri/0/i915_error_state.\n\n"
716 " --help display this help and exit\n"
717 " --headers decode only command headers\n"
718 " --color[=WHEN] colorize the output; WHEN can be 'auto' (default\n"
719 " if omitted), 'always', or 'never'\n"
720 " --no-pager don't launch pager\n"
721 " --no-offsets don't print instruction offsets\n"
722 " --xml=DIR load hardware xml description from directory DIR\n",
723 progname);
724 }
725
726 int
727 main(int argc, char *argv[])
728 {
729 FILE *file;
730 const char *path;
731 struct stat st;
732 int c, i, error;
733 bool help = false, pager = true;
734 const struct option aubinator_opts[] = {
735 { "help", no_argument, (int *) &help, true },
736 { "no-pager", no_argument, (int *) &pager, false },
737 { "no-offsets", no_argument, (int *) &option_print_offsets, false },
738 { "headers", no_argument, (int *) &option_full_decode, false },
739 { "color", required_argument, NULL, 'c' },
740 { "xml", required_argument, NULL, 'x' },
741 { NULL, 0, NULL, 0 }
742 };
743
744 i = 0;
745 while ((c = getopt_long(argc, argv, "", aubinator_opts, &i)) != -1) {
746 switch (c) {
747 case 'c':
748 if (optarg == NULL || strcmp(optarg, "always") == 0)
749 option_color = COLOR_ALWAYS;
750 else if (strcmp(optarg, "never") == 0)
751 option_color = COLOR_NEVER;
752 else if (strcmp(optarg, "auto") == 0)
753 option_color = COLOR_AUTO;
754 else {
755 fprintf(stderr, "invalid value for --color: %s", optarg);
756 exit(EXIT_FAILURE);
757 }
758 break;
759 case 'x':
760 xml_path = strdup(optarg);
761 break;
762 default:
763 break;
764 }
765 }
766
767 if (help || argc == 1) {
768 print_help(argv[0], stderr);
769 exit(EXIT_SUCCESS);
770 }
771
772 if (optind >= argc) {
773 if (isatty(0)) {
774 path = "/sys/class/drm/card0/error";
775 error = stat(path, &st);
776 if (error != 0) {
777 path = "/debug/dri";
778 error = stat(path, &st);
779 }
780 if (error != 0) {
781 path = "/sys/kernel/debug/dri";
782 error = stat(path, &st);
783 }
784 if (error != 0) {
785 errx(1,
786 "Couldn't find i915 debugfs directory.\n\n"
787 "Is debugfs mounted? You might try mounting it with a command such as:\n\n"
788 "\tsudo mount -t debugfs debugfs /sys/kernel/debug\n");
789 }
790 } else {
791 read_data_file(stdin);
792 exit(EXIT_SUCCESS);
793 }
794 } else {
795 path = argv[optind];
796 error = stat(path, &st);
797 if (error != 0) {
798 fprintf(stderr, "Error opening %s: %s\n",
799 path, strerror(errno));
800 exit(EXIT_FAILURE);
801 }
802 }
803
804 if (option_color == COLOR_AUTO)
805 option_color = isatty(1) ? COLOR_ALWAYS : COLOR_NEVER;
806
807 if (isatty(1) && pager)
808 setup_pager();
809
810 if (S_ISDIR(st.st_mode)) {
811 int ret;
812 char *filename;
813
814 ret = asprintf(&filename, "%s/i915_error_state", path);
815 assert(ret > 0);
816 file = fopen(filename, "r");
817 if (!file) {
818 int minor;
819 free(filename);
820 for (minor = 0; minor < 64; minor++) {
821 ret = asprintf(&filename, "%s/%d/i915_error_state", path, minor);
822 assert(ret > 0);
823
824 file = fopen(filename, "r");
825 if (file)
826 break;
827
828 free(filename);
829 }
830 }
831 if (!file) {
832 fprintf(stderr, "Failed to find i915_error_state beneath %s\n",
833 path);
834 return EXIT_FAILURE;
835 }
836 } else {
837 file = fopen(path, "r");
838 if (!file) {
839 fprintf(stderr, "Failed to open %s: %s\n",
840 path, strerror(errno));
841 return EXIT_FAILURE;
842 }
843 }
844
845 read_data_file(file);
846 fclose(file);
847
848 /* close the stdout which is opened to write the output */
849 fflush(stdout);
850 close(1);
851 wait(NULL);
852
853 if (xml_path)
854 free(xml_path);
855
856 return EXIT_SUCCESS;
857 }
858
859 /* vim: set ts=8 sw=8 tw=0 cino=:0,(0 noet :*/