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