tools: beef up intel_dump_decode
[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_decode.h"
39
40 static void
41 read_bin_file(uint32_t devid, const char * filename)
42 {
43         uint32_t buf[16384];
44         int fd, offset, ret;
45
46         fd = open (filename, O_RDONLY);
47         if (fd < 0) {
48                 fprintf (stderr, "Failed to open %s: %s\n",
49                          filename, strerror (errno));
50                 exit (1);
51         }
52
53         offset = 0;
54         while ((ret = read (fd, buf, sizeof(buf))) > 0) {
55                 intel_decode (buf, ret/4, offset, devid, 1);
56                 offset += ret;
57         }
58         close (fd);
59 }
60
61 static void
62 read_data_file(uint32_t devid, const char * filename)
63 {
64     FILE *file;
65     uint32_t *data = NULL;
66     int data_size = 0, count = 0, line_number = 0, matched;
67     char *line = NULL;
68     size_t line_size;
69     uint32_t offset, value;
70     uint32_t gtt_offset = 0;
71
72     file = fopen (filename, "r");
73     if (file == NULL) {
74         fprintf (stderr, "Failed to open %s: %s\n",
75                  filename, strerror (errno));
76         exit (1);
77     }
78
79     while (getline (&line, &line_size, file) > 0) {
80         line_number++;
81
82         matched = sscanf (line, "%08x : %08x", &offset, &value);
83         if (matched != 2) {
84             printf("ignoring line %s", line);
85
86             continue;
87         }
88
89         count++;
90
91         if (count > data_size) {
92             data_size = data_size ? data_size * 2 : 1024;
93             data = realloc (data, data_size * sizeof (uint32_t));
94             if (data == NULL) {
95                 fprintf (stderr, "Out of memory.\n");
96                 exit (1);
97             }
98         }
99
100         data[count-1] = value;
101     }
102
103     if (count) {
104         intel_decode (data, count, gtt_offset, devid, 0);
105     }
106
107     free (data);
108     free (line);
109
110     fclose (file);
111 }
112
113 static void
114 read_autodetect_file(uint32_t devid, const char * filename)
115 {
116         int binary = 0, c;
117         FILE *file;
118
119         file = fopen (filename, "r");
120         if (file == NULL) {
121                 fprintf (stderr, "Failed to open %s: %s\n",
122                          filename, strerror (errno));
123                 exit (1);
124         }
125
126         while ((c = fgetc(file)) != EOF) {
127                 /* totally lazy binary detector */
128                 if (c < 10) {
129                         binary = 1;
130                         break;
131                 }
132         }
133
134         fclose(file);
135
136         if (binary == 1)
137                 read_bin_file(devid, filename);
138         else
139                 read_data_file(devid, filename);
140
141 }
142
143
144 int
145 main (int argc, char *argv[])
146 {
147         uint32_t devid = 0xa011;
148         int i, c;
149         int option_index = 0;
150         int binary = -1;
151
152         static struct option long_options[] = {
153                 {"devid", 1, 0, 'd'},
154                 {"ascii", 0, 0, 'a'},
155                 {"binary", 0, 0, 'b'}
156         };
157
158         while((c = getopt_long(argc, argv, "ab",
159                                long_options, &option_index)) != -1) {
160                 switch(c) {
161                 case 'd':
162                         devid = atoi(optarg);
163                         break;
164                 case 'b':
165                         binary = 1;
166                         break;
167                 case 'a':
168                         binary = 0;
169                         break;
170                 default:
171                         printf("unkown command options\n");
172                         break;
173                 }
174         }
175
176         if (optind == argc) {
177                 fprintf(stderr, "no input file given\n");
178                 exit(-1);
179         }
180
181
182         for (i = optind; i < argc; i++) {
183                 if (binary == 1)
184                         read_bin_file(devid, argv[i]);
185                 else if (binary == 0)
186                         read_data_file(devid, argv[i]);
187                 else
188                         read_autodetect_file(devid, argv[i]);
189         }
190
191         return 0;
192 }