2 * Copyright (C) 2010 by David Brownell
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or (at
7 * your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * Simple utility to parse and dump ARM Cortex-M3 SWO trace output. Once the
20 * mechanisms work right, this information can be used for various purposes
21 * including profiling (particularly easy for flat PC-sample profiles) and
24 * SWO is the Single Wire Output found on some ARM cores, most notably on the
25 * Cortex-M3. It combines data from several sources:
27 * - Software trace (ITM): so-called "printf-style" application messaging
28 * using "ITM stimulus ports"; and differential timestamps.
29 * - Hardware trace (DWT): for profiling counters and comparator matches.
30 * - TPIU may issue sync packets.
32 * The trace data format is defined in Appendix E, "Debug ITM and DWT packet
33 * protocol", of the ARMv7-M Architecture Reference Manual (DDI 0403C). It
34 * is a superset of the ITM data format from the Coresight TRM.
36 * The trace data has two encodings. The working assumption is that data
37 * gets into this program using the UART encoding.
48 unsigned int dump_swit
;
50 /* Example ITM trace word (0xWWXXYYZZ) parsing for task events, sent
51 * on port 31 (Reserved for "the" RTOS in CMSIS v1.30)
52 * WWXX: event code (0..3 pre-assigned, 4..15 reserved)
56 * NOTE that this specific encoding could be space-optimized; and that
57 * trace data streams could also be history-sensitive.
59 static void show_task(int port
, unsigned data
)
61 unsigned code
= data
>> 16;
75 strcpy(buf
, "create");
78 strcpy(buf
, "destroy");
80 /* 4..15 reserved for other infrastructure ops */
82 sprintf(buf
, "code %d", code
);
85 printf("TASK %d, pri %d: %s",
91 static void show_reserved(FILE *f
, char *label
, int c
)
98 printf("%s - %#02x", label
, c
);
100 for (i
= 0; (c
& 0x80) && i
< 4; i
++) {
103 printf("(ERROR %d - %s) ", errno
, strerror(errno
));
112 static bool read_varlen(FILE *f
, int c
, unsigned *value
)
115 unsigned char buf
[4];
130 printf("INVALID SIZE\n");
134 memset(buf
, 0, sizeof buf
);
135 if (fread(buf
, 1, size
, f
) != size
)
138 *value
= (buf
[3] << 24)
145 printf("(ERROR %d - %s)\n", errno
, strerror(errno
));
149 static void show_hard(FILE *f
, int c
)
151 unsigned type
= c
>> 3;
160 if (!read_varlen(f
, c
, &value
))
162 printf("%#x", value
);
165 case 0: /* event counter wrapping */
166 printf("overflow %s%s%s%s%s%s",
167 (value
& (1 << 5)) ? "cyc " : "",
168 (value
& (1 << 4)) ? "fold " : "",
169 (value
& (1 << 3)) ? "lsu " : "",
170 (value
& (1 << 2)) ? "slp " : "",
171 (value
& (1 << 1)) ? "exc " : "",
172 (value
& (1 << 0)) ? "cpi " : "");
174 case 1: /* exception tracing */
175 switch (value
>> 12) {
189 printf("%s exception %d", label
, value
& 0x1ff);
191 case 2: /* PC sampling */
193 printf("PC - sleep");
195 printf("PC - %#08x", value
);
197 case 8: /* data tracing, pc value */
201 printf("Data trace %d, PC %#08x", (c
>> 4) & 3, value
);
202 /* optionally followed by data value */
204 case 9: /* data tracing, address offset */
208 printf("Data trace %d, address offset %#04x",
209 (c
>> 4) & 3, value
);
210 /* always followed by data value */
212 case 16 ... 23: /* data tracing, data value */
213 printf("Data trace %d, ", (c
>> 4) & 3);
214 label
= (c
& 0x8) ? "write" : "read";
217 printf("word %s, value %#08x", label
, value
);
220 printf("halfword %s, value %#04x", label
, value
);
223 printf("byte %s, value %#02x", label
, value
);
228 printf("UNDEFINED, rawtype: %x", type
);
237 * Table of SWIT (SoftWare InstrumentTation) message dump formats, for
238 * ITM port 0..31 application data.
240 * Eventually this should be customizable; all usage is application defined.
242 * REVISIT there can be up to 256 trace ports, via "ITM Extension" packets
246 void (*show
)(int port
, unsigned data
);
248 { .port
= 31, .show
= show_task
, },
251 static void show_swit(FILE *f
, int c
)
253 unsigned port
= c
>> 3;
257 if (port
+ 1 == dump_swit
) {
258 if (!read_varlen(f
, c
, &value
))
264 if (!read_varlen(f
, c
, &value
))
270 printf("SWIT %u - ", port
);
272 printf("%#08x", value
);
274 for (i
= 0; i
< sizeof(format
) / sizeof(format
[0]); i
++) {
275 if (format
[i
].port
== port
) {
277 format
[i
].show(port
, value
);
286 static void show_timestamp(FILE *f
, int c
)
288 unsigned counter
= 0;
290 bool delayed
= false;
295 printf("TIMESTAMP - ");
297 /* Format 2: header only */
300 case 0: /* sync packet -- coding error! */
301 case 0x70: /* overflow -- ditto! */
302 printf("ERROR - %#02x\n", c
);
305 /* synchronous to ITM */
312 /* Format 1: one to four bytes of data too */
315 label
= ", reserved control\n";
318 /* synchronous to ITM */
321 label
= ", timestamp delayed";
325 label
= ", packet delayed";
329 label
= ", packet and timestamp delayed";
344 counter
|= (c
& 0x7f) << 7;
351 counter
|= (c
& 0x7f) << 14;
358 counter
|= (c
& 0x7f) << 21;
361 /* REVISIT should we try to convert from delta values? */
362 printf("+%u%s\n", counter
, label
);
366 printf("(ERROR %d - %s) ", errno
, strerror(errno
));
370 int main(int argc
, char **argv
)
375 /* parse arguments */
376 while ((c
= getopt(argc
, argv
, "f:d:")) != EOF
) {
379 /* e.g. from UART connected to /dev/ttyUSB0 */
380 f
= fopen(optarg
, "r");
387 dump_swit
= atoi(optarg
);
390 fprintf(stderr
, "usage: %s [-f input]",
396 /* Parse data ... records have a header then data bytes.
397 * NOTE: we assume getc() deals in 8-bit bytes.
399 bool overflow
= false;
401 while ((c
= getc(f
)) != EOF
) {
403 /* Sync packet ... 7 zeroes, 0x80 */
407 for (i
= 0; i
< 6; i
++) {
420 printf("BAD SYNC\n");
424 /* Overflow packet */
426 /* REVISIT later, report just what overflowed!
427 * Timestamp and SWIT can happen. Non-ITM too?
430 printf("OVERFLOW ...\n");
436 case 0x00: /* Timestamp */
437 show_timestamp(f
, c
);
439 case 0x04: /* "Reserved" */
440 show_reserved(f
, "RESERVED", c
);
442 case 0x08: /* ITM Extension */
443 /* FIXME someday, handle these ... */
444 show_reserved(f
, "ITM EXT", c
);
446 case 0x0c: /* DWT Extension */
447 show_reserved(f
, "DWT EXT", c
);