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];
123 printf("INVALID SIZE\n");
127 memset(buf, 0, sizeof buf);
128 if (fread(buf, 1, size, f) != size)
131 *value = (buf[3] << 24)
138 printf("(ERROR %d - %s)\n", errno, strerror(errno));
142 static void show_hard(FILE *f, int c)
144 unsigned type = c >> 3;
149 printf("DWT - ", type);
151 if (!read_varlen(f, c, &value))
153 printf("%#x", value);
156 case 0: /* event counter wrapping */
157 printf("overflow %s%s%s%s%s%s",
158 (value & (1 << 5)) ? "cyc " : "",
159 (value & (1 << 4)) ? "fold " : "",
160 (value & (1 << 3)) ? "lsu " : "",
161 (value & (1 << 2)) ? "slp " : "",
162 (value & (1 << 1)) ? "exc " : "",
163 (value & (1 << 0)) ? "cpi " : "");
165 case 1: /* exception tracing */
166 switch (value >> 12) {
180 printf("%s exception %d", label, value & 0x1ff);
182 case 2: /* PC sampling */
184 printf("PC - sleep");
186 printf("PC - %#08x", value);
188 case 8: /* data tracing, pc value */
192 printf("Data trace %d, PC %#08x", (c >> 4) & 3, value);
193 /* optionally followed by data value */
195 case 9: /* data tracing, address offset */
199 printf("Data trace %d, address offset %#04x",
200 (c >> 4) & 3, value);
201 /* always followed by data value */
203 case 16 ... 23: /* data tracing, data value */
204 printf("Data trace %d, ", (c >> 4) & 3);
205 label = (c & 0x8) ? "write" : "read";
208 printf("word %s, value %#08x", label, value);
211 printf("halfword %s, value %#04x", label, value);
214 printf("byte %s, value %#02x", label, value);
228 * Table of SWIT (SoftWare InstrumentTation) message dump formats, for
229 * ITM port 0..31 application data.
231 * Eventually this should be customizable; all usage is application defined.
233 * REVISIT there can be up to 256 trace ports, via "ITM Extension" packets
237 void (*show)(int port, unsigned data);
239 { .port = 31, .show = show_task, },
242 static void show_swit(FILE *f, int c)
245 unsigned port = c >> 3;
246 unsigned char buf[4];
250 printf("SWIT %u - ", port);
252 if (!read_varlen(f, c, &value))
254 printf("%#08x", value);
256 for (i = 0; i <= sizeof(format) / sizeof(format[0]); i++) {
257 if (format[i].port == port) {
259 format[i].show(port, value);
268 printf("(ERROR %d - %s)\n", errno, strerror(errno));
272 static void show_timestamp(FILE *f, int c)
274 unsigned counter = 0;
276 bool delayed = false;
278 printf("TIMESTAMP - ");
280 /* Format 2: header only */
283 case 0: /* sync packet -- coding error! */
284 case 0x70: /* overflow -- ditto! */
285 printf("ERROR - %#02x\n", c);
288 /* synchronous to ITM */
295 /* Format 1: one to four bytes of data too */
298 label = ", reserved control\n";
301 /* synchronous to ITM */
304 label = ", timestamp delayed";
308 label = ", packet delayed";
312 label = ", packet and timetamp delayed";
327 counter |= (c & 0x7f) << 7;
334 counter |= (c & 0x7f) << 14;
341 counter |= (c & 0x7f) << 21;
344 /* REVISIT should we try to convert from delta values? */
345 printf("+%u%s\n", counter, label);
349 printf("(ERROR %d - %s) ", errno, strerror(errno));
353 int main(int argc, char **argv)
358 /* parse arguments */
359 while ((c = getopt(argc, argv, "f:")) != EOF) {
362 /* e.g. from UART connected to /dev/ttyUSB0 */
363 f = fopen(optarg, "r");
371 fprintf(stderr, "usage: %s [-f input]",
377 /* Parse data ... records have a header then data bytes.
378 * NOTE: we assume getc() deals in 8-bit bytes.
380 bool overflow = false;
382 while ((c = getc(f)) != EOF) {
384 /* Sync packet ... 7 zeroes, 0x80 */
388 for (i = 0; i < 6; i++) {
401 printf("BAD SYNC\n");
405 /* Overflow packet */
407 /* REVISIT later, report just what overflowed!
408 * Timestamp and SWIT can happen. Non-ITM too?
411 printf("OVERFLOW ...\n");
417 case 0x00: /* Timestamp */
418 show_timestamp(f, c);
420 case 0x04: /* "Reserved" */
421 show_reserved(f, "RESERVED", c);
423 case 0x08: /* ITM Extension */
424 /* FIXME someday, handle these ... */
425 show_reserved(f, "ITM EXT", c);
427 case 0x0c: /* DWT Extension */
428 show_reserved(f, "DWT EXT", c);