target/cortex_m: faster reading of all CPU registers
[fw/openocd] / src / target / trace.c
1 /***************************************************************************
2  *   Copyright (C) 2005, 2007 by Dominic Rath                              *
3  *   Dominic.Rath@gmx.de                                                   *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
17  ***************************************************************************/
18
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22
23 #include <helper/log.h>
24 #include "trace.h"
25 #include "target.h"
26
27 int trace_point(struct target *target, uint32_t number)
28 {
29         struct trace *trace = target->trace_info;
30
31         LOG_DEBUG("tracepoint: %i", (int)number);
32
33         if (number < trace->num_trace_points)
34                 trace->trace_points[number].hit_counter++;
35
36         if (trace->trace_history_size) {
37                 trace->trace_history[trace->trace_history_pos++] = number;
38                 if (trace->trace_history_pos == trace->trace_history_size) {
39                         trace->trace_history_pos = 0;
40                         trace->trace_history_overflowed = 1;
41                 }
42         }
43
44         return ERROR_OK;
45 }
46
47 COMMAND_HANDLER(handle_trace_point_command)
48 {
49         struct target *target = get_current_target(CMD_CTX);
50         struct trace *trace = target->trace_info;
51
52         if (CMD_ARGC == 0) {
53                 uint32_t i;
54
55                 for (i = 0; i < trace->num_trace_points; i++) {
56                         command_print(CMD, "trace point 0x%8.8" PRIx32 " (%lld times hit)",
57                                         trace->trace_points[i].address,
58                                         (long long)trace->trace_points[i].hit_counter);
59                 }
60
61                 return ERROR_OK;
62         }
63
64         if (!strcmp(CMD_ARGV[0], "clear")) {
65                 free(trace->trace_points);
66                 trace->trace_points = NULL;
67
68                 trace->num_trace_points = 0;
69                 trace->trace_points_size = 0;
70
71                 return ERROR_OK;
72         }
73
74         /* resize array if necessary */
75         if (!trace->trace_points || (trace->trace_points_size == trace->num_trace_points)) {
76                 trace->trace_points = realloc(trace->trace_points,
77                                 sizeof(struct trace_point) * (trace->trace_points_size + 32));
78                 trace->trace_points_size += 32;
79         }
80
81         uint32_t address;
82         COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
83         trace->trace_points[trace->num_trace_points].address = address;
84         trace->trace_points[trace->num_trace_points].hit_counter = 0;
85         trace->num_trace_points++;
86
87         return ERROR_OK;
88 }
89
90 COMMAND_HANDLER(handle_trace_history_command)
91 {
92         struct target *target = get_current_target(CMD_CTX);
93         struct trace *trace = target->trace_info;
94
95         if (CMD_ARGC > 0) {
96                 trace->trace_history_pos = 0;
97                 trace->trace_history_overflowed = 0;
98
99                 if (!strcmp(CMD_ARGV[0], "clear")) {
100                         /* clearing is implicit, we've just reset position anyway */
101                         return ERROR_OK;
102                 }
103
104                 free(trace->trace_history);
105
106                 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], trace->trace_history_size);
107                 trace->trace_history = malloc(sizeof(uint32_t) * trace->trace_history_size);
108
109                 command_print(CMD, "new trace history size: %i", (int)(trace->trace_history_size));
110         } else {
111                 uint32_t i;
112                 uint32_t first = 0;
113                 uint32_t last = trace->trace_history_pos;
114
115                 if (!trace->trace_history_size) {
116                         command_print(CMD, "trace history buffer is not allocated");
117                         return ERROR_OK;
118                 }
119
120                 if (trace->trace_history_overflowed) {
121                         first = trace->trace_history_pos;
122                         last = trace->trace_history_pos - 1;
123                 }
124
125                 for (i = first; (i % trace->trace_history_size) != last; i++) {
126                         if (trace->trace_history[i % trace->trace_history_size] < trace->num_trace_points) {
127                                 uint32_t address;
128                                 address = trace->trace_points[trace->trace_history[i % trace->trace_history_size]].address;
129                                 command_print(CMD, "trace point %i: 0x%8.8" PRIx32 "",
130                                                 (int)(trace->trace_history[i % trace->trace_history_size]),
131                                                 address);
132                         } else
133                                 command_print(CMD, "trace point %i: -not defined-",
134                                                 (int)(trace->trace_history[i % trace->trace_history_size]));
135                 }
136         }
137
138         return ERROR_OK;
139 }
140
141 static const struct command_registration trace_exec_command_handlers[] = {
142         {
143                 .name = "history",
144                 .handler = handle_trace_history_command,
145                 .mode = COMMAND_EXEC,
146                 .help = "display trace history, clear history or set size",
147                 .usage = "['clear'|size]",
148         },
149         {
150                 .name = "point",
151                 .handler = handle_trace_point_command,
152                 .mode = COMMAND_EXEC,
153                 .help = "display trace points, clear list of trace points, "
154                         "or add new tracepoint at address",
155                 .usage = "['clear'|address]",
156         },
157         COMMAND_REGISTRATION_DONE
158 };
159 static const struct command_registration trace_command_handlers[] = {
160         {
161                 .name = "trace",
162                 .mode = COMMAND_EXEC,
163                 .help = "trace command group",
164                 .usage = "",
165                 .chain = trace_exec_command_handlers,
166         },
167         COMMAND_REGISTRATION_DONE
168 };
169
170 int trace_register_commands(struct command_context *cmd_ctx)
171 {
172         return register_commands(cmd_ctx, NULL, trace_command_handlers);
173 }