summaryrefslogtreecommitdiff
path: root/tools/intel_decode_file.c
blob: 9331e4e9e4c790279a05b40549e2ff68f0fc6472 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include "intel_decode.h"

static void
read_data_text (const char *filename, uint32_t devid, int is_batch)
{
    FILE *file;
    uint32_t *data = NULL;
    int data_size = 0, count = 0, line_number = 0, matched;
    char *line = NULL;
    size_t line_size;
    uint32_t offset, value;
    uint32_t gtt_offset = 0, new_gtt_offset;
    char *buffer_type = is_batch ? "batchbuffer" : "ringbuffer";

    file = fopen (filename, "r");
    if (file == NULL) {
	fprintf (stderr, "Failed to open %s: %s\n",
		 filename, strerror (errno));
	exit (1);
    }

    while (getline (&line, &line_size, file) > 0) {
	line_number++;

	matched = sscanf (line, "--- gtt_offset = 0x%08x\n", &new_gtt_offset);
	if (matched == 1) {
	    if (count) {
		printf("%s at 0x%08x:\n", buffer_type, gtt_offset);
		intel_decode (data, count, gtt_offset, devid, 0);
		count = 0;
	    }
	    gtt_offset = new_gtt_offset;
	    continue;
	}

	matched = sscanf (line, "%08x : %08x", &offset, &value);
	if (matched !=2 ) {
	    fprintf (stderr, "Warning: Ignoring unrecognized line at %s:%d:\n%s",
		     filename, line_number, line);
	    continue;
	}

	count++;

	if (count > data_size) {
	    data_size = data_size ? data_size * 2 : 1024;
	    data = realloc (data, data_size * sizeof (uint32_t));
	    if (data == NULL) {
		fprintf (stderr, "Out of memory.\n");
		exit (1);
	    }
	}

	data[count-1] = value;
    }

    if (count) {
	printf("%s at 0x%08x:\n", buffer_type, gtt_offset);
	intel_decode (data, count, gtt_offset, devid, 0);
    }

    free (data);
    free (line);

    fclose (file);
}

static int
read_data_file (const char *filename, uint32_t devid, int is_batch)
{
    FILE *file;
    uint32_t *buf;
    uint32_t len = 4096;
    int count = 0;

    buf = malloc (sizeof (uint32_t) * len);
    if (buf == NULL) {
	fprintf (stderr, "Failed to allocate memory for %s.\n",
		 filename);
	return 0;
    }

    file = fopen (filename, "rb");
    if (file == NULL) {
	free (buf);
	fprintf (stderr, "Failed to open %s: %s\n",
		 filename, strerror (errno));
	return 0;
    }

    do {
	uint32_t *newbuf;

	count += fread (buf + count, sizeof (uint32_t), len - count, file);
	if (count < len)
	    break;

	len *= 2;
	newbuf = realloc (buf, len * sizeof (uint32_t));
	if (newbuf == NULL) {
	    free (buf);
	    fclose (file);
	    fprintf (stderr, "Failed to allocate memory for %s.\n",
		     filename);
	    return 0;
	}

	buf = newbuf;
    } while (1);

    fclose (file);

    intel_decode (buf, count, 0x0, devid, 1);
    intel_decode_context_reset ();
    free (buf);

    return 1;
}

int
main (int argc, char **argv)
{
	int is_text = 0;
	int i;
	uint32_t devid =0x27A2;

	for (i = 1; i < argc; i++) {
		if (strncmp (argv[i], "--pci-id=", 9) == 0) {
			devid = atoi (argv[i] + 9);
			continue;
		}

		if (is_text)
			read_data_text (argv[i], devid, 1);
		else
			read_data_file (argv[i], devid, 1);
	}

	return 0;
}