]>
Commit | Line | Data |
---|---|---|
26f7227b SH |
1 | /* |
2 | * Simple trace backend | |
3 | * | |
4 | * Copyright IBM, Corp. 2010 | |
5 | * | |
6 | * This work is licensed under the terms of the GNU GPL, version 2. See | |
7 | * the COPYING file in the top-level directory. | |
8 | * | |
9 | */ | |
10 | ||
11 | #include <stdlib.h> | |
12 | #include <stdint.h> | |
13 | #include <stdio.h> | |
14 | #include <time.h> | |
15 | #include "trace.h" | |
16 | ||
17 | /** Trace file header event ID */ | |
18 | #define HEADER_EVENT_ID (~(uint64_t)0) /* avoids conflicting with TraceEventIDs */ | |
19 | ||
20 | /** Trace file magic number */ | |
21 | #define HEADER_MAGIC 0xf2b177cb0aa429b4ULL | |
22 | ||
23 | /** Trace file version number, bump if format changes */ | |
24 | #define HEADER_VERSION 0 | |
25 | ||
26 | /** Trace buffer entry */ | |
27 | typedef struct { | |
28 | uint64_t event; | |
29 | uint64_t timestamp_ns; | |
30 | uint64_t x1; | |
31 | uint64_t x2; | |
32 | uint64_t x3; | |
33 | uint64_t x4; | |
34 | uint64_t x5; | |
35 | uint64_t x6; | |
36 | } TraceRecord; | |
37 | ||
38 | enum { | |
39 | TRACE_BUF_LEN = 64 * 1024 / sizeof(TraceRecord), | |
40 | }; | |
41 | ||
42 | static TraceRecord trace_buf[TRACE_BUF_LEN]; | |
43 | static unsigned int trace_idx; | |
44 | static FILE *trace_fp; | |
45 | ||
46 | static bool write_header(FILE *fp) | |
47 | { | |
48 | static const TraceRecord header = { | |
49 | .event = HEADER_EVENT_ID, | |
50 | .timestamp_ns = HEADER_MAGIC, | |
51 | .x1 = HEADER_VERSION, | |
52 | }; | |
53 | ||
54 | return fwrite(&header, sizeof header, 1, fp) == 1; | |
55 | } | |
56 | ||
9410b56c PS |
57 | static bool open_trace_file(void) |
58 | { | |
59 | char *filename; | |
60 | ||
61 | if (asprintf(&filename, CONFIG_TRACE_FILE, getpid()) < 0) { | |
62 | return false; | |
63 | } | |
64 | ||
65 | trace_fp = fopen(filename, "w"); | |
66 | free(filename); | |
67 | if (!trace_fp) { | |
68 | return false; | |
69 | } | |
70 | return write_header(trace_fp); | |
71 | } | |
72 | ||
26f7227b SH |
73 | static void flush_trace_buffer(void) |
74 | { | |
75 | if (!trace_fp) { | |
9410b56c | 76 | open_trace_file(); |
26f7227b SH |
77 | } |
78 | if (trace_fp) { | |
79 | size_t unused; /* for when fwrite(3) is declared warn_unused_result */ | |
80 | unused = fwrite(trace_buf, trace_idx * sizeof(trace_buf[0]), 1, trace_fp); | |
81 | } | |
82 | ||
83 | /* Discard written trace records */ | |
84 | trace_idx = 0; | |
85 | } | |
86 | ||
87 | void st_set_trace_file_enabled(bool enable) | |
88 | { | |
89 | if (enable == trace_file_enabled) { | |
90 | return; /* no change */ | |
91 | } | |
92 | ||
93 | /* Flush/discard trace buffer */ | |
94 | st_flush_trace_buffer(); | |
95 | ||
96 | /* To disable, close trace file */ | |
97 | if (!enable) { | |
98 | fclose(trace_fp); | |
99 | trace_fp = NULL; | |
100 | } | |
101 | ||
102 | trace_file_enabled = enable; | |
103 | } | |
104 | ||
105 | static void trace(TraceEventID event, uint64_t x1, uint64_t x2, uint64_t x3, | |
106 | uint64_t x4, uint64_t x5, uint64_t x6) | |
107 | { | |
108 | TraceRecord *rec = &trace_buf[trace_idx]; | |
109 | struct timespec ts; | |
110 | ||
111 | /* TODO Windows? It would be good to use qemu-timer here but that isn't | |
112 | * linked into qemu-tools. Also we should avoid recursion in the tracing | |
113 | * code, therefore it is useful to be self-contained. | |
114 | */ | |
115 | clock_gettime(CLOCK_MONOTONIC, &ts); | |
116 | ||
22890ab5 PS |
117 | if (!trace_list[event].state) { |
118 | return; | |
119 | } | |
120 | ||
26f7227b SH |
121 | rec->event = event; |
122 | rec->timestamp_ns = ts.tv_sec * 1000000000LL + ts.tv_nsec; | |
123 | rec->x1 = x1; | |
124 | rec->x2 = x2; | |
125 | rec->x3 = x3; | |
126 | rec->x4 = x4; | |
127 | rec->x5 = x5; | |
128 | rec->x6 = x6; | |
129 | ||
130 | if (++trace_idx == TRACE_BUF_LEN) { | |
131 | flush_trace_buffer(); | |
132 | } | |
133 | } | |
134 | ||
135 | void trace0(TraceEventID event) | |
136 | { | |
137 | trace(event, 0, 0, 0, 0, 0, 0); | |
138 | } | |
139 | ||
140 | void trace1(TraceEventID event, uint64_t x1) | |
141 | { | |
142 | trace(event, x1, 0, 0, 0, 0, 0); | |
143 | } | |
144 | ||
145 | void trace2(TraceEventID event, uint64_t x1, uint64_t x2) | |
146 | { | |
147 | trace(event, x1, x2, 0, 0, 0, 0); | |
148 | } | |
149 | ||
150 | void trace3(TraceEventID event, uint64_t x1, uint64_t x2, uint64_t x3) | |
151 | { | |
152 | trace(event, x1, x2, x3, 0, 0, 0); | |
153 | } | |
154 | ||
155 | void trace4(TraceEventID event, uint64_t x1, uint64_t x2, uint64_t x3, uint64_t x4) | |
156 | { | |
157 | trace(event, x1, x2, x3, x4, 0, 0); | |
158 | } | |
159 | ||
160 | void trace5(TraceEventID event, uint64_t x1, uint64_t x2, uint64_t x3, uint64_t x4, uint64_t x5) | |
161 | { | |
162 | trace(event, x1, x2, x3, x4, x5, 0); | |
163 | } | |
164 | ||
165 | void trace6(TraceEventID event, uint64_t x1, uint64_t x2, uint64_t x3, uint64_t x4, uint64_t x5, uint64_t x6) | |
166 | { | |
167 | trace(event, x1, x2, x3, x4, x5, x6); | |
168 | } | |
169 | ||
170 | /** | |
171 | * Flush the trace buffer on exit | |
172 | */ | |
173 | static void __attribute__((constructor)) st_init(void) | |
174 | { | |
175 | atexit(st_flush_trace_buffer); | |
176 | } | |
22890ab5 PS |
177 | |
178 | void st_print_trace(FILE *stream, int (*stream_printf)(FILE *stream, const char *fmt, ...)) | |
179 | { | |
180 | unsigned int i; | |
181 | ||
182 | for (i = 0; i < trace_idx; i++) { | |
183 | stream_printf(stream, "Event %lu : %lx %lx %lx %lx %lx\n", | |
184 | trace_buf[i].event, trace_buf[i].x1, trace_buf[i].x2, | |
185 | trace_buf[i].x3, trace_buf[i].x4, trace_buf[i].x5); | |
186 | } | |
187 | } | |
188 | ||
189 | void st_print_trace_events(FILE *stream, int (*stream_printf)(FILE *stream, const char *fmt, ...)) | |
190 | { | |
191 | unsigned int i; | |
192 | ||
193 | for (i = 0; i < NR_TRACE_EVENTS; i++) { | |
194 | stream_printf(stream, "%s [Event ID %u] : state %u\n", | |
195 | trace_list[i].tp_name, i, trace_list[i].state); | |
196 | } | |
197 | } | |
198 | ||
199 | static TraceEvent* find_trace_event_by_name(const char *tname) | |
200 | { | |
201 | unsigned int i; | |
202 | ||
203 | if (!tname) { | |
204 | return NULL; | |
205 | } | |
206 | ||
207 | for (i = 0; i < NR_TRACE_EVENTS; i++) { | |
208 | if (!strcmp(trace_list[i].tp_name, tname)) { | |
209 | return &trace_list[i]; | |
210 | } | |
211 | } | |
212 | return NULL; /* indicates end of list reached without a match */ | |
213 | } | |
214 | ||
215 | void st_change_trace_event_state(const char *tname, bool tstate) | |
216 | { | |
217 | TraceEvent *tp; | |
218 | ||
219 | tp = find_trace_event_by_name(tname); | |
220 | if (tp) { | |
221 | tp->state = tstate; | |
222 | } | |
223 | } |