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