intel_dump_decode: Support the INTEL_DEVID_OVERRIDE env variable
[platform/upstream/intel-gpu-tools.git] / tools / intel_dump_decode.c
1 /*
2  * Copyright © 2010 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  * Authors:
24  *    Chris Wilson <chris@chris-wilson.co.uk>
25  *
26  */
27
28 #define _GNU_SOURCE
29 #include <stdint.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <errno.h>
34 #include <unistd.h>
35 #include <fcntl.h>
36 #include <getopt.h>
37
38 #include <intel_bufmgr.h>
39
40 struct drm_intel_decode *ctx;
41
42 static void
43 read_bin_file(const char * filename)
44 {
45         uint32_t buf[16384];
46         int fd, offset, ret;
47
48         if (!strcmp(filename, "-"))
49                 fd = fileno(stdin);
50         else
51                 fd = open (filename, O_RDONLY);
52         if (fd < 0) {
53                 fprintf (stderr, "Failed to open %s: %s\n",
54                          filename, strerror (errno));
55                 exit (1);
56         }
57
58         drm_intel_decode_set_dump_past_end(ctx, 1);
59
60         offset = 0;
61         while ((ret = read (fd, buf, sizeof(buf))) > 0) {
62                 drm_intel_decode_set_batch_pointer(ctx, buf, offset, ret/4);
63                 drm_intel_decode(ctx);
64                 offset += ret;
65         }
66         close (fd);
67 }
68
69 static void
70 read_data_file(const char * filename)
71 {
72     FILE *file;
73     uint32_t *data = NULL;
74     int data_size = 0, count = 0, line_number = 0, matched;
75     char *line = NULL;
76     size_t line_size;
77     uint32_t offset, value;
78     uint32_t gtt_offset = 0;
79
80         if (!strcmp(filename, "-"))
81                 file = stdin;
82         else
83                 file = fopen (filename, "r");
84
85     if (file == NULL) {
86         fprintf (stderr, "Failed to open %s: %s\n",
87                  filename, strerror (errno));
88         exit (1);
89     }
90
91     while (getline (&line, &line_size, file) > 0) {
92         line_number++;
93
94         matched = sscanf (line, "%08x : %08x", &offset, &value);
95         if (matched != 2) {
96             printf("ignoring line %s", line);
97
98             continue;
99         }
100
101         count++;
102
103         if (count > data_size) {
104             data_size = data_size ? data_size * 2 : 1024;
105             data = realloc (data, data_size * sizeof (uint32_t));
106             if (data == NULL) {
107                 fprintf (stderr, "Out of memory.\n");
108                 exit (1);
109             }
110         }
111
112         data[count-1] = value;
113     }
114
115     if (count) {
116         drm_intel_decode_set_batch_pointer(ctx, data, gtt_offset, count);
117         drm_intel_decode(ctx);
118     }
119
120     free (data);
121     free (line);
122
123     fclose (file);
124 }
125
126 static void
127 read_autodetect_file(const char * filename)
128 {
129         int binary = 0, c;
130         FILE *file;
131
132         file = fopen (filename, "r");
133         if (file == NULL) {
134                 fprintf (stderr, "Failed to open %s: %s\n",
135                          filename, strerror (errno));
136                 exit (1);
137         }
138
139         while ((c = fgetc(file)) != EOF) {
140                 /* totally lazy binary detector */
141                 if (c < 10) {
142                         binary = 1;
143                         break;
144                 }
145         }
146
147         fclose(file);
148
149         if (binary == 1)
150                 read_bin_file(filename);
151         else
152                 read_data_file(filename);
153
154 }
155
156
157 int
158 main (int argc, char *argv[])
159 {
160         uint32_t devid = 0xa011;
161         char *devid_str = NULL;
162         int i, c;
163         int option_index = 0;
164         int binary = -1;
165
166         static struct option long_options[] = {
167                 {"devid", 1, 0, 'd'},
168                 {"ascii", 0, 0, 'a'},
169                 {"binary", 0, 0, 'b'}
170         };
171
172         devid_str = getenv("INTEL_DEVID_OVERRIDE");
173
174         while((c = getopt_long(argc, argv, "ad:b",
175                                long_options, &option_index)) != -1) {
176                 switch(c) {
177                 case 'd':
178                         devid_str = optarg;
179                         break;
180                 case 'b':
181                         binary = 1;
182                         break;
183                 case 'a':
184                         binary = 0;
185                         break;
186                 default:
187                         printf("unkown command options\n");
188                         break;
189                 }
190         }
191
192         if (devid_str)
193                 devid = strtoul(devid_str, NULL, 0);
194
195         ctx = drm_intel_decode_context_alloc(devid);
196
197         if (optind == argc) {
198                 fprintf(stderr, "no input file given\n");
199                 exit(-1);
200         }
201
202         for (i = optind; i < argc; i++) {
203                 /* For stdin input, let's read as data file */
204                 if (!strcmp(argv[i], "-")) {
205                         read_data_file(argv[i]);
206                         continue;
207                 }
208                 if (binary == 1)
209                         read_bin_file(argv[i]);
210                 else if (binary == 0)
211                         read_data_file(argv[i]);
212                 else
213                         read_autodetect_file(argv[i]);
214         }
215
216         return 0;
217 }