[PATCH] blktrace: fix get_subbuf() leak
[blktrace.git] / blkparse_fmt.c
CommitLineData
71d5d4c9
JA
1/*
2 * This file contains format parsing code for blkparse, allowing you to
3 * customize the individual action format and generel output format.
4 */
5#include <stdio.h>
6#include <string.h>
7#include <stdlib.h>
8#include <unistd.h>
9#include <ctype.h>
10
11#include "blktrace.h"
12
a8f30e64 13#define VALID_SPECS "ABCDFGMPQRSTUWX"
71d5d4c9
JA
14
15#define HEADER "%D %2c %8s %5T.%9t %5p %2a %3d "
16
17static char *override_format[256];
18
19static inline int valid_spec(int spec)
20{
21 return strchr(VALID_SPECS, spec) != NULL;
22}
23
e820abd7 24void set_all_format_specs(char *option)
71d5d4c9
JA
25{
26 char *p;
27
28 for (p = VALID_SPECS; *p; p++)
29 if (override_format[(int)(*p)] == NULL)
e820abd7 30 override_format[(int)(*p)] = strdup(option);
71d5d4c9
JA
31}
32
e820abd7 33int add_format_spec(char *option)
71d5d4c9
JA
34{
35 int spec = optarg[0];
36
37 if (!valid_spec(spec)) {
38 fprintf(stderr,"Bad format specifier %c\n", spec);
39 return 1;
40 }
41 if (optarg[1] != ',') {
e820abd7 42 fprintf(stderr,"Bad format specifier - need ',' %s\n", option);
71d5d4c9
JA
43 return 1;
44 }
e820abd7
JA
45 option += 2;
46 if (*option == '\0') {
47 fprintf(stderr,"Bad format specifier - need fmt %s\n", option);
71d5d4c9
JA
48 return 1;
49 }
50
51 /*
52 * Set both merges (front and back)
53 */
54 if (spec == 'M') {
e820abd7
JA
55 override_format['B'] = strdup(option);
56 override_format['M'] = strdup(option);
71d5d4c9 57 } else
e820abd7 58 override_format[spec] = strdup(option);
71d5d4c9
JA
59
60 return 0;
61}
62
768277cc
JA
63static inline void fill_rwbs(char *rwbs, struct blk_io_trace *t)
64{
65 int w = t->action & BLK_TC_ACT(BLK_TC_WRITE);
66 int b = t->action & BLK_TC_ACT(BLK_TC_BARRIER);
67 int s = t->action & BLK_TC_ACT(BLK_TC_SYNC);
68 int i = 0;
69
70 if (w)
71 rwbs[i++] = 'W';
72 else
73 rwbs[i++] = 'R';
74 if (b)
75 rwbs[i++] = 'B';
76 if (s)
77 rwbs[i++] = 'S';
78
79 rwbs[i] = '\0';
80}
81
63756b50 82static inline int pdu_rest_is_zero(unsigned char *pdu, int len)
1d6376a5 83{
63756b50 84 static char zero[4096];
1d6376a5 85
63756b50 86 return !memcmp(pdu, zero, len);
1d6376a5
JA
87}
88
768277cc
JA
89static char *dump_pdu(unsigned char *pdu_buf, int pdu_len)
90{
91 static char p[4096];
92 int i, len;
93
94 if (!pdu_buf || !pdu_len)
95 return NULL;
96
97 for (len = 0, i = 0; i < pdu_len; i++) {
98 if (i)
99 len += sprintf(p + len, " ");
100
101 len += sprintf(p + len, "%02x", pdu_buf[i]);
1d6376a5
JA
102
103 /*
104 * usually dump for cdb dumps where we can see lots of
105 * zeroes, stop when the rest is just zeroes and indicate
106 * so with a .. appended
107 */
108 if (!pdu_buf[i] && pdu_rest_is_zero(pdu_buf + i, pdu_len - i)) {
109 sprintf(p + len, " ..");
110 break;
111 }
768277cc
JA
112 }
113
114 return p;
115}
116
a8f30e64
JA
117#define pdu_start(t) (((void *) (t) + sizeof(struct blk_io_trace)))
118
768277cc
JA
119static unsigned int get_pdu_int(struct blk_io_trace *t)
120{
a8f30e64 121 __u64 *val = pdu_start(t);
768277cc
JA
122
123 return be64_to_cpu(*val);
124}
125
a8f30e64
JA
126static void get_pdu_remap(struct blk_io_trace *t, struct blk_io_trace_remap *r)
127{
128 struct blk_io_trace_remap *__r = pdu_start(t);
663af819 129 __u64 sector = __r->sector;
a8f30e64
JA
130
131 r->device = be32_to_cpu(__r->device);
663af819 132 r->sector = be64_to_cpu(sector);
a8f30e64
JA
133}
134
71d5d4c9
JA
135static void print_field(char *act, struct per_cpu_info *pci,
136 struct blk_io_trace *t, unsigned long long elapsed,
137 int pdu_len, unsigned char *pdu_buf, char field,
138 int minus, int has_w, int width)
139{
140 char format[64];
141
142 if (has_w) {
143 if (minus)
144 sprintf(format, "%%-%d", width);
145 else
146 sprintf(format, "%%%d", width);
147 } else
148 sprintf(format, "%%");
149
150 switch (field) {
151 case 'a':
152 fprintf(ofp, strcat(format, "s"), act);
153 break;
154 case 'c':
155 fprintf(ofp, strcat(format, "d"), pci->cpu);
156 break;
bfc70ad5
JA
157 case 'C': {
158 char *name = find_process_name(t->pid);
159
160 fprintf(ofp, strcat(format, "s"), name);
71d5d4c9 161 break;
bfc70ad5 162 }
71d5d4c9
JA
163 case 'd': {
164 char rwbs[4];
768277cc
JA
165
166 fill_rwbs(rwbs, t);
71d5d4c9
JA
167 fprintf(ofp, strcat(format, "s"), rwbs);
168 break;
169 }
170 case 'D': /* format width ignored */
171 fprintf(ofp,"%3d,%-3d", MAJOR(t->device), MINOR(t->device));
172 break;
173 case 'e':
174 fprintf(ofp, strcat(format, "d"), t->error);
175 break;
176 case 'M':
177 fprintf(ofp, strcat(format, "d"), MAJOR(t->device));
178 break;
179 case 'm':
180 fprintf(ofp, strcat(format, "d"), MINOR(t->device));
181 break;
182 case 'n':
ae957cbc 183 fprintf(ofp, strcat(format, "u"), t_sec(t));
71d5d4c9 184 break;
1c8ca7b5
JA
185 case 'N':
186 fprintf(ofp, strcat(format, "u"), t->bytes);
187 break;
71d5d4c9
JA
188 case 'p':
189 fprintf(ofp, strcat(format, "u"), t->pid);
190 break;
768277cc
JA
191 case 'P': { /* format width ignored */
192 char *p = dump_pdu(pdu_buf, pdu_len);
193 if (p)
194 fprintf(ofp, "%s", p);
71d5d4c9 195 break;
768277cc 196 }
71d5d4c9
JA
197 case 's':
198 fprintf(ofp, strcat(format, "ld"), t->sequence);
199 break;
200 case 'S':
201 fprintf(ofp, strcat(format, "lu"), t->sector);
202 break;
203 case 't':
204 sprintf(format, "%%0%dlu", has_w ? width : 9);
205 fprintf(ofp, format, NANO_SECONDS(t->time));
206 break;
207 case 'T':
208 fprintf(ofp, strcat(format, "d"), SECONDS(t->time));
209 break;
210 case 'u':
211 if (elapsed == -1ULL) {
212 fprintf(stderr, "Expecting elapsed value\n");
213 exit(1);
214 }
215 fprintf(ofp, strcat(format, "llu"), elapsed / 1000);
216 break;
217 case 'U': {
768277cc 218 fprintf(ofp, strcat(format, "u"), get_pdu_int(t));
71d5d4c9
JA
219 break;
220 }
221 default:
222 fprintf(ofp,strcat(format, "c"), field);
223 break;
224 }
225}
226
768277cc
JA
227static char *parse_field(char *act, struct per_cpu_info *pci,
228 struct blk_io_trace *t, unsigned long long elapsed,
229 int pdu_len, unsigned char *pdu_buf,
71d5d4c9
JA
230 char *master_format)
231{
232 int minus = 0;
233 int has_w = 0;
234 int width = 0;
235 char *p = master_format;
236
237 if (*p == '-') {
238 minus = 1;
239 p++;
240 }
241 if (isdigit(*p)) {
242 has_w = 1;
243 do {
244 width = (width * 10) + (*p++ - '0');
245 } while ((*p) && (isdigit(*p)));
246 }
247 if (*p) {
248 print_field(act, pci, t, elapsed, pdu_len, pdu_buf, *p++,
249 minus, has_w, width);
250 }
251 return p;
252}
253
768277cc
JA
254static void process_default(char *act, struct per_cpu_info *pci,
255 struct blk_io_trace *t, unsigned long long elapsed,
256 int pdu_len, unsigned char *pdu_buf)
71d5d4c9 257{
768277cc 258 char rwbs[4];
bfc70ad5 259 char *name;
71d5d4c9 260
768277cc
JA
261 fill_rwbs(rwbs, t);
262
263 /*
264 * The header is always the same
265 */
266 fprintf(ofp, "%3d,%-3d %2d %8d %5d.%09lu %5u %2s %3s ",
267 MAJOR(t->device), MINOR(t->device), pci->cpu, t->sequence,
268 (int) SECONDS(t->time), (unsigned long) NANO_SECONDS(t->time),
269 t->pid, act, rwbs);
71d5d4c9 270
bfc70ad5
JA
271 name = find_process_name(t->pid);
272
768277cc 273 switch (act[0]) {
80c53608 274 case 'R': /* Requeue */
71d5d4c9 275 case 'C': /* Complete */
768277cc
JA
276 if (t->action & BLK_TC_ACT(BLK_TC_PC)) {
277 char *p = dump_pdu(pdu_buf, pdu_len);
278 if (p)
279 fprintf(ofp, "(%s) ", p);
280 fprintf(ofp, "[%d]\n", t->error);
281 } else {
71d5d4c9 282 if (elapsed != -1ULL) {
768277cc
JA
283 fprintf(ofp, "%llu + %u (%8llu) [%d]\n",
284 (unsigned long long) t->sector,
ae957cbc 285 t_sec(t), elapsed, t->error);
768277cc
JA
286 } else {
287 fprintf(ofp, "%llu + %u [%d]\n",
288 (unsigned long long) t->sector,
ae957cbc 289 t_sec(t), t->error);
768277cc 290 }
71d5d4c9 291 }
71d5d4c9
JA
292 break;
293
294 case 'D': /* Issue */
71d5d4c9 295 case 'I': /* Insert */
71d5d4c9
JA
296 case 'Q': /* Queue */
297 case 'W': /* Bounce */
768277cc
JA
298 if (t->action & BLK_TC_ACT(BLK_TC_PC)) {
299 char *p;
1c8ca7b5 300 fprintf(ofp, "%u ", t->bytes);
768277cc
JA
301 p = dump_pdu(pdu_buf, pdu_len);
302 if (p)
303 fprintf(ofp, "(%s) ", p);
bfc70ad5 304 fprintf(ofp, "[%s]\n", name);
768277cc
JA
305 } else {
306 if (elapsed != -1ULL) {
307 fprintf(ofp, "%llu + %u (%8llu) [%s]\n",
308 (unsigned long long) t->sector,
bfc70ad5 309 t_sec(t), elapsed, name);
768277cc
JA
310 } else {
311 fprintf(ofp, "%llu + %u [%s]\n",
312 (unsigned long long) t->sector,
bfc70ad5 313 t_sec(t), name);
768277cc
JA
314 }
315 }
71d5d4c9
JA
316 break;
317
318 case 'B': /* Back merge */
319 case 'F': /* Front merge */
320 case 'M': /* Front or back merge */
768277cc
JA
321 case 'G': /* Get request */
322 case 'S': /* Sleep request */
323 fprintf(ofp, "%llu + %u [%s]\n", (unsigned long long) t->sector,
bfc70ad5 324 t_sec(t), name);
71d5d4c9
JA
325 break;
326
327 case 'P': /* Plug */
bfc70ad5 328 fprintf(ofp, "[%s]\n", name);
71d5d4c9
JA
329 break;
330
331 case 'U': /* Unplug IO */
a8f30e64 332 case 'T': /* Unplug timer */
bfc70ad5 333 fprintf(ofp, "[%s] %u\n", name, get_pdu_int(t));
71d5d4c9
JA
334 break;
335
a8f30e64
JA
336 case 'A': { /* remap */
337 struct blk_io_trace_remap r;
338
339 get_pdu_remap(t, &r);
340 fprintf(ofp, "%llu + %u <- (%d,%d) %llu\n",
ae957cbc 341 (unsigned long long) r.sector, t_sec(t),
a8f30e64
JA
342 MAJOR(r.device), MINOR(r.device),
343 (unsigned long long) t->sector);
344 break;
345 }
346
71d5d4c9 347 case 'X': /* Split */
768277cc 348 fprintf(ofp, "%llu / %u [%s]\n", (unsigned long long) t->sector,
bfc70ad5 349 get_pdu_int(t), name);
71d5d4c9
JA
350 break;
351
352 default:
768277cc
JA
353 fprintf(stderr, "Unknown action %c\n", act[0]);
354 break;
71d5d4c9
JA
355 }
356
71d5d4c9
JA
357}
358
359void process_fmt(char *act, struct per_cpu_info *pci, struct blk_io_trace *t,
360 unsigned long long elapsed, int pdu_len,
361 unsigned char *pdu_buf)
362{
768277cc
JA
363 char *p = override_format[(int) *act];
364
365 if (!p) {
366 process_default(act, pci, t, elapsed, pdu_len, pdu_buf);
367 return;
368 }
71d5d4c9
JA
369
370 while (*p) {
371 switch (*p) {
372 case '%': /* Field specifier */
373 p++;
374 if (*p == '%')
375 fprintf(ofp, "%c", *p++);
376 else if (!*p)
377 fprintf(ofp, "%c", '%');
378 else
379 p = parse_field(act, pci, t, elapsed,
380 pdu_len, pdu_buf, p);
381 break;
382 case '\\': { /* escape */
383 switch (p[1]) {
384 case 'b': fprintf(ofp, "\b"); break;
385 case 'n': fprintf(ofp, "\n"); break;
386 case 'r': fprintf(ofp, "\r"); break;
387 case 't': fprintf(ofp, "\t"); break;
388 default:
389 fprintf(stderr,
390 "Invalid escape char in format %c\n",
391 p[1]);
392 exit(1);
393 /*NOTREACHED*/
394 }
395 p += 2;
396 break;
397 }
398 default:
399 fprintf(ofp, "%c", *p++);
400 break;
401 }
402 }
403}
404
405