1 /***************************************************************************
2 * Copyright (C) 2005, 2007 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
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. *
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. *
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 ***************************************************************************/
24 #include "replacements.h"
34 int trace_point(target_t *target, int number)
36 trace_t *trace = target->trace_info;
38 LOG_DEBUG("tracepoint: %i", number);
40 if (number < trace->num_trace_points)
41 trace->trace_points[number].hit_counter++;
43 if (trace->trace_history_size)
45 trace->trace_history[trace->trace_history_pos++] = number;
46 if (trace->trace_history_pos == trace->trace_history_size)
48 trace->trace_history_pos = 0;
49 trace->trace_history_overflowed = 1;
56 int handle_trace_point_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
58 target_t *target = get_current_target(cmd_ctx);
59 trace_t *trace = target->trace_info;
65 for (i = 0; i < trace->num_trace_points; i++)
67 command_print(cmd_ctx, "trace point 0x%8.8x (%"PRIi64" times hit)",
68 trace->trace_points[i].address,
69 trace->trace_points[i].hit_counter);
75 if (!strcmp(args[0], "clear"))
77 if (trace->trace_points)
78 free(trace->trace_points);
79 trace->num_trace_points = 0;
80 trace->trace_points_size = 0;
85 /* resize array if necessary */
86 if (!trace->trace_points || (trace->trace_points_size == trace->num_trace_points))
88 trace->trace_points = realloc(trace->trace_points, sizeof(trace_point_t) * (trace->trace_points_size + 32));
89 trace->trace_points_size += 32;
92 trace->trace_points[trace->num_trace_points].address = strtoul(args[0], NULL, 0);
93 trace->trace_points[trace->num_trace_points].hit_counter = 0;
94 trace->num_trace_points++;
99 int handle_trace_history_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
101 target_t *target = get_current_target(cmd_ctx);
102 trace_t *trace = target->trace_info;
106 trace->trace_history_pos = 0;
107 trace->trace_history_overflowed = 0;
109 if (!strcmp(args[0], "clear"))
111 /* clearing is implicit, we've just reset position anyway */
115 if (trace->trace_history)
116 free(trace->trace_history);
118 trace->trace_history_size = strtoul(args[0], NULL, 0);
119 trace->trace_history = malloc(sizeof(u32) * trace->trace_history_size);
121 command_print(cmd_ctx, "new trace history size: %i", trace->trace_history_size);
127 int last = trace->trace_history_pos;
129 if (trace->trace_history_overflowed)
131 first = trace->trace_history_pos;
132 last = trace->trace_history_pos - 1;
135 for (i = first; (i % trace->trace_history_size) != last; i++)
137 if (trace->trace_history[i % trace->trace_history_size] < trace->num_trace_points)
140 address = trace->trace_points[trace->trace_history[i % trace->trace_history_size]].address;
141 command_print(cmd_ctx, "trace point %i: 0x%8.8x",
142 trace->trace_history[i % trace->trace_history_size],
148 command_print(cmd_ctx, "trace point %i: -not defined-", trace->trace_history[i % trace->trace_history_size]);
156 int trace_register_commands(struct command_context_s *cmd_ctx)
158 command_t *trace_cmd =
159 register_command(cmd_ctx, NULL, "trace", NULL, COMMAND_ANY, "trace commands");
161 register_command(cmd_ctx, trace_cmd, "history", handle_trace_history_command,
162 COMMAND_EXEC, "display trace history, ['clear'] history or set [size]");
164 register_command(cmd_ctx, trace_cmd, "point", handle_trace_point_command,
165 COMMAND_EXEC, "display trace points, ['clear'] list of trace points, or add new tracepoint at [address]");