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