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