GDB fileIO stdout support
[openocd.git] / src / target / trace.c
blob63c477fbfcad2ee0904ba780b4cf817a7d900a0d
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 ***************************************************************************/
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
23 #include <helper/log.h>
24 #include "trace.h"
25 #include "target.h"
27 int trace_point(struct target *target, uint32_t number)
29 struct trace *trace = target->trace_info;
31 LOG_DEBUG("tracepoint: %i", (int)number);
33 if (number < trace->num_trace_points)
34 trace->trace_points[number].hit_counter++;
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;
44 return ERROR_OK;
47 COMMAND_HANDLER(handle_trace_point_command)
49 struct target *target = get_current_target(CMD_CTX);
50 struct trace *trace = target->trace_info;
52 if (CMD_ARGC == 0) {
53 uint32_t i;
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);
61 return ERROR_OK;
64 if (!strcmp(CMD_ARGV[0], "clear")) {
65 if (trace->trace_points) {
66 free(trace->trace_points);
67 trace->trace_points = NULL;
69 trace->num_trace_points = 0;
70 trace->trace_points_size = 0;
72 return ERROR_OK;
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;
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++;
88 return ERROR_OK;
91 COMMAND_HANDLER(handle_trace_history_command)
93 struct target *target = get_current_target(CMD_CTX);
94 struct trace *trace = target->trace_info;
96 if (CMD_ARGC > 0) {
97 trace->trace_history_pos = 0;
98 trace->trace_history_overflowed = 0;
100 if (!strcmp(CMD_ARGV[0], "clear")) {
101 /* clearing is implicit, we've just reset position anyway */
102 return ERROR_OK;
105 if (trace->trace_history)
106 free(trace->trace_history);
108 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], trace->trace_history_size);
109 trace->trace_history = malloc(sizeof(uint32_t) * trace->trace_history_size);
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;
117 if (!trace->trace_history_size) {
118 command_print(CMD_CTX, "trace history buffer is not allocated");
119 return ERROR_OK;
122 if (trace->trace_history_overflowed) {
123 first = trace->trace_history_pos;
124 last = trace->trace_history_pos - 1;
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]));
140 return ERROR_OK;
143 static const struct command_registration trace_exec_command_handlers[] = {
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]",
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]",
159 COMMAND_REGISTRATION_DONE
161 static const struct command_registration trace_command_handlers[] = {
163 .name = "trace",
164 .mode = COMMAND_EXEC,
165 .help = "trace command group",
166 .usage = "",
167 .chain = trace_exec_command_handlers,
169 COMMAND_REGISTRATION_DONE
172 int trace_register_commands(struct command_context *cmd_ctx)
174 return register_commands(cmd_ctx, NULL, trace_command_handlers);