]> git.sur5r.net Git - openocd/blob - src/target/trace.c
Fix load_image for ELF with all p_paddr set to zero
[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, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include <helper/log.h>
25 #include "trace.h"
26 #include "target.h"
27
28 int trace_point(struct target *target, uint32_t number)
29 {
30         struct trace *trace = target->trace_info;
31
32         LOG_DEBUG("tracepoint: %i", (int)number);
33
34         if (number < trace->num_trace_points)
35                 trace->trace_points[number].hit_counter++;
36
37         if (trace->trace_history_size)
38         {
39                 trace->trace_history[trace->trace_history_pos++] = number;
40                 if (trace->trace_history_pos == trace->trace_history_size)
41                 {
42                         trace->trace_history_pos = 0;
43                         trace->trace_history_overflowed = 1;
44                 }
45         }
46
47         return ERROR_OK;
48 }
49
50 COMMAND_HANDLER(handle_trace_point_command)
51 {
52         struct target *target = get_current_target(CMD_CTX);
53         struct trace *trace = target->trace_info;
54
55         if (CMD_ARGC == 0)
56         {
57                 uint32_t i;
58
59                 for (i = 0; i < trace->num_trace_points; i++)
60                 {
61                         command_print(CMD_CTX, "trace point 0x%8.8" PRIx32 " (%lld times hit)",
62                                         trace->trace_points[i].address,
63                                         (long long)trace->trace_points[i].hit_counter);
64                 }
65
66                 return ERROR_OK;
67         }
68
69         if (!strcmp(CMD_ARGV[0], "clear"))
70         {
71                 if (trace->trace_points)
72                 {
73                         free(trace->trace_points);
74                         trace->trace_points = NULL;
75                 }
76                 trace->num_trace_points = 0;
77                 trace->trace_points_size = 0;
78
79                 return ERROR_OK;
80         }
81
82         /* resize array if necessary */
83         if (!trace->trace_points || (trace->trace_points_size == trace->num_trace_points))
84         {
85                 trace->trace_points = realloc(trace->trace_points, sizeof(struct trace_point) * (trace->trace_points_size + 32));
86                 trace->trace_points_size += 32;
87         }
88
89         uint32_t address;
90         COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
91         trace->trace_points[trace->num_trace_points].address = address;
92         trace->trace_points[trace->num_trace_points].hit_counter = 0;
93         trace->num_trace_points++;
94
95         return ERROR_OK;
96 }
97
98 COMMAND_HANDLER(handle_trace_history_command)
99 {
100         struct target *target = get_current_target(CMD_CTX);
101         struct trace *trace = target->trace_info;
102
103         if (CMD_ARGC > 0)
104         {
105                 trace->trace_history_pos = 0;
106                 trace->trace_history_overflowed = 0;
107
108                 if (!strcmp(CMD_ARGV[0], "clear"))
109                 {
110                         /* clearing is implicit, we've just reset position anyway */
111                         return ERROR_OK;
112                 }
113
114                 if (trace->trace_history)
115                         free(trace->trace_history);
116
117                 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], trace->trace_history_size);
118                 trace->trace_history = malloc(sizeof(uint32_t) * trace->trace_history_size);
119
120                 command_print(CMD_CTX, "new trace history size: %i", (int)(trace->trace_history_size));
121         }
122         else
123         {
124                 uint32_t i;
125                 uint32_t first = 0;
126                 uint32_t last = trace->trace_history_pos;
127
128                 if (!trace->trace_history_size) {
129                         command_print(CMD_CTX, "trace history buffer is not allocated");
130                         return ERROR_OK;
131                 }
132                 if (trace->trace_history_overflowed)
133                 {
134                         first = trace->trace_history_pos;
135                         last = trace->trace_history_pos - 1;
136                 }
137
138                 for (i = first; (i % trace->trace_history_size) != last; i++)
139                 {
140                         if (trace->trace_history[i % trace->trace_history_size] < trace->num_trace_points)
141                         {
142                                 uint32_t address;
143                                 address = trace->trace_points[trace->trace_history[i % trace->trace_history_size]].address;
144                                 command_print(CMD_CTX, "trace point %i: 0x%8.8" PRIx32 "",
145                                               (int)(trace->trace_history[i % trace->trace_history_size]),
146                                               address);
147                         }
148
149                         else
150                         {
151                                 command_print(CMD_CTX, "trace point %i: -not defined-", (int)(trace->trace_history[i % trace->trace_history_size]));
152                         }
153                 }
154         }
155
156         return ERROR_OK;
157 }
158
159 static const struct command_registration trace_exec_command_handlers[] = {
160         {
161                 .name = "history",
162                 .handler = handle_trace_history_command,
163                 .mode = COMMAND_EXEC,
164                 .help = "display trace history, clear history or set size",
165                 .usage = "['clear'|size]",
166         },
167         {
168                 .name = "point",
169                 .handler = handle_trace_point_command,
170                 .mode = COMMAND_EXEC,
171                 .help = "display trace points, clear list of trace points, "
172                         "or add new tracepoint at address",
173                 .usage = "['clear'|address]",
174         },
175         COMMAND_REGISTRATION_DONE
176 };
177 static const struct command_registration trace_command_handlers[] = {
178         {
179                 .name = "trace",
180                 .mode = COMMAND_EXEC,
181                 .help = "trace command group",
182                 .chain = trace_exec_command_handlers,
183         },
184         COMMAND_REGISTRATION_DONE
185 };
186
187 int trace_register_commands(struct command_context *cmd_ctx)
188 {
189         return register_commands(cmd_ctx, NULL, trace_command_handlers);
190 }