]> git.sur5r.net Git - openocd/blob - src/target/trace.c
target/cortex_a: remove buggy memory AP accesses
[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_CTX, "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                 if (trace->trace_points) {
66                         free(trace->trace_points);
67                         trace->trace_points = NULL;
68                 }
69                 trace->num_trace_points = 0;
70                 trace->trace_points_size = 0;
71
72                 return ERROR_OK;
73         }
74
75         /* resize array if necessary */
76         if (!trace->trace_points || (trace->trace_points_size == trace->num_trace_points)) {
77                 trace->trace_points = realloc(trace->trace_points,
78                                 sizeof(struct trace_point) * (trace->trace_points_size + 32));
79                 trace->trace_points_size += 32;
80         }
81
82         uint32_t address;
83         COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
84         trace->trace_points[trace->num_trace_points].address = address;
85         trace->trace_points[trace->num_trace_points].hit_counter = 0;
86         trace->num_trace_points++;
87
88         return ERROR_OK;
89 }
90
91 COMMAND_HANDLER(handle_trace_history_command)
92 {
93         struct target *target = get_current_target(CMD_CTX);
94         struct trace *trace = target->trace_info;
95
96         if (CMD_ARGC > 0) {
97                 trace->trace_history_pos = 0;
98                 trace->trace_history_overflowed = 0;
99
100                 if (!strcmp(CMD_ARGV[0], "clear")) {
101                         /* clearing is implicit, we've just reset position anyway */
102                         return ERROR_OK;
103                 }
104
105                 if (trace->trace_history)
106                         free(trace->trace_history);
107
108                 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], trace->trace_history_size);
109                 trace->trace_history = malloc(sizeof(uint32_t) * trace->trace_history_size);
110
111                 command_print(CMD_CTX, "new trace history size: %i", (int)(trace->trace_history_size));
112         } else {
113                 uint32_t i;
114                 uint32_t first = 0;
115                 uint32_t last = trace->trace_history_pos;
116
117                 if (!trace->trace_history_size) {
118                         command_print(CMD_CTX, "trace history buffer is not allocated");
119                         return ERROR_OK;
120                 }
121
122                 if (trace->trace_history_overflowed) {
123                         first = trace->trace_history_pos;
124                         last = trace->trace_history_pos - 1;
125                 }
126
127                 for (i = first; (i % trace->trace_history_size) != last; i++) {
128                         if (trace->trace_history[i % trace->trace_history_size] < trace->num_trace_points) {
129                                 uint32_t address;
130                                 address = trace->trace_points[trace->trace_history[i % trace->trace_history_size]].address;
131                                 command_print(CMD_CTX, "trace point %i: 0x%8.8" PRIx32 "",
132                                                 (int)(trace->trace_history[i % trace->trace_history_size]),
133                                                 address);
134                         } else
135                                 command_print(CMD_CTX, "trace point %i: -not defined-",
136                                                 (int)(trace->trace_history[i % trace->trace_history_size]));
137                 }
138         }
139
140         return ERROR_OK;
141 }
142
143 static const struct command_registration trace_exec_command_handlers[] = {
144         {
145                 .name = "history",
146                 .handler = handle_trace_history_command,
147                 .mode = COMMAND_EXEC,
148                 .help = "display trace history, clear history or set size",
149                 .usage = "['clear'|size]",
150         },
151         {
152                 .name = "point",
153                 .handler = handle_trace_point_command,
154                 .mode = COMMAND_EXEC,
155                 .help = "display trace points, clear list of trace points, "
156                         "or add new tracepoint at address",
157                 .usage = "['clear'|address]",
158         },
159         COMMAND_REGISTRATION_DONE
160 };
161 static const struct command_registration trace_command_handlers[] = {
162         {
163                 .name = "trace",
164                 .mode = COMMAND_EXEC,
165                 .help = "trace command group",
166                 .usage = "",
167                 .chain = trace_exec_command_handlers,
168         },
169         COMMAND_REGISTRATION_DONE
170 };
171
172 int trace_register_commands(struct command_context *cmd_ctx)
173 {
174         return register_commands(cmd_ctx, NULL, trace_command_handlers);
175 }