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