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 /* Example ITM trace word (0xWWXXYYZZ) parsing for task events, sent
49 * on port 31 (Reserved for "the" RTOS in CMSIS v1.30)
50 * WWXX: event code (0..3 pre-assigned, 4..15 reserved)
54 * NOTE that this specific encoding could be space-optimized; and that
55 * trace data streams could also be history-sensitive.
57 static void show_task(int port, unsigned data)
59 unsigned code = data >> 16;
70 strcpy(buf, "create");
73 strcpy(buf, "destroy");
75 /* 4..15 reserved for other infrastructure ops */
77 sprintf(buf, "code %d", code);
80 printf("TASK %d, pri %d: %s",
86 static void show_reserved(FILE *f, char *label, int c)
90 printf("%s - %#02x", label, c);
92 for (i = 0; (c & 0x80) && i < 4; i++) {
95 printf("(ERROR %d - %s) ", errno, strerror(errno));
104 static bool read_varlen(FILE *f, int c, unsigned *value)
107 unsigned char buf[4];
122 printf("INVALID SIZE\n");
126 memset(buf, 0, sizeof buf);
127 if (fread(buf, 1, size, f) != size)
130 *value = (buf[3] << 24)
137 printf("(ERROR %d - %s)\n", errno, strerror(errno));
141 static void show_hard(FILE *f, int c)
143 unsigned type = c >> 3;
149 if (!read_varlen(f, c, &value))
151 printf("%#x", value);
154 case 0: /* event counter wrapping */
155 printf("overflow %s%s%s%s%s%s",
156 (value & (1 << 5)) ? "cyc " : "",
157 (value & (1 << 4)) ? "fold " : "",
158 (value & (1 << 3)) ? "lsu " : "",
159 (value & (1 << 2)) ? "slp " : "",
160 (value & (1 << 1)) ? "exc " : "",
161 (value & (1 << 0)) ? "cpi " : "");
163 case 1: /* exception tracing */
164 switch (value >> 12) {
178 printf("%s exception %d", label, value & 0x1ff);
180 case 2: /* PC sampling */
182 printf("PC - sleep");
184 printf("PC - %#08x", value);
186 case 8: /* data tracing, pc value */
190 printf("Data trace %d, PC %#08x", (c >> 4) & 3, value);
191 /* optionally followed by data value */
193 case 9: /* data tracing, address offset */
197 printf("Data trace %d, address offset %#04x",
198 (c >> 4) & 3, value);
199 /* always followed by data value */
201 case 16 ... 23: /* data tracing, data value */
202 printf("Data trace %d, ", (c >> 4) & 3);
203 label = (c & 0x8) ? "write" : "read";
206 printf("word %s, value %#08x", label, value);
209 printf("halfword %s, value %#04x", label, value);
212 printf("byte %s, value %#02x", label, value);
217 printf("UNDEFINED, rawtype: %x", type);
226 * Table of SWIT (SoftWare InstrumentTation) message dump formats, for
227 * ITM port 0..31 application data.
229 * Eventually this should be customizable; all usage is application defined.
231 * REVISIT there can be up to 256 trace ports, via "ITM Extension" packets
235 void (*show)(int port, unsigned data);
237 { .port = 31, .show = show_task, },
240 static void show_swit(FILE *f, int c)
242 unsigned port = c >> 3;
246 printf("SWIT %u - ", port);
248 if (!read_varlen(f, c, &value))
250 printf("%#08x", value);
252 for (i = 0; i < sizeof(format) / sizeof(format[0]); i++) {
253 if (format[i].port == port) {
255 format[i].show(port, value);
264 static void show_timestamp(FILE *f, int c)
266 unsigned counter = 0;
268 bool delayed = false;
270 printf("TIMESTAMP - ");
272 /* Format 2: header only */
275 case 0: /* sync packet -- coding error! */
276 case 0x70: /* overflow -- ditto! */
277 printf("ERROR - %#02x\n", c);
280 /* synchronous to ITM */
287 /* Format 1: one to four bytes of data too */
290 label = ", reserved control\n";
293 /* synchronous to ITM */
296 label = ", timestamp delayed";
300 label = ", packet delayed";
304 label = ", packet and timetamp delayed";
319 counter |= (c & 0x7f) << 7;
326 counter |= (c & 0x7f) << 14;
333 counter |= (c & 0x7f) << 21;
336 /* REVISIT should we try to convert from delta values? */
337 printf("+%u%s\n", counter, label);
341 printf("(ERROR %d - %s) ", errno, strerror(errno));
345 int main(int argc, char **argv)
350 /* parse arguments */
351 while ((c = getopt(argc, argv, "f:")) != EOF) {
354 /* e.g. from UART connected to /dev/ttyUSB0 */
355 f = fopen(optarg, "r");
362 fprintf(stderr, "usage: %s [-f input]",
368 /* Parse data ... records have a header then data bytes.
369 * NOTE: we assume getc() deals in 8-bit bytes.
371 bool overflow = false;
373 while ((c = getc(f)) != EOF) {
375 /* Sync packet ... 7 zeroes, 0x80 */
379 for (i = 0; i < 6; i++) {
392 printf("BAD SYNC\n");
396 /* Overflow packet */
398 /* REVISIT later, report just what overflowed!
399 * Timestamp and SWIT can happen. Non-ITM too?
402 printf("OVERFLOW ...\n");
408 case 0x00: /* Timestamp */
409 show_timestamp(f, c);
411 case 0x04: /* "Reserved" */
412 show_reserved(f, "RESERVED", c);
414 case 0x08: /* ITM Extension */
415 /* FIXME someday, handle these ... */
416 show_reserved(f, "ITM EXT", c);
418 case 0x0c: /* DWT Extension */
419 show_reserved(f, "DWT EXT", c);