David Brownell <david-b@pacbell.net>: This patch adds annotations to
[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, 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 "log.h"
25 #include "trace.h"
26 #include "target.h"
27
28 #include <inttypes.h>
29
30 int trace_point(target_t *target, u32 number)
31 {
32         trace_t *trace = target->trace_info;
33
34         LOG_DEBUG("tracepoint: %i", number);
35         
36         if (number < trace->num_trace_points)
37                 trace->trace_points[number].hit_counter++;
38
39         if (trace->trace_history_size)
40         {
41                 trace->trace_history[trace->trace_history_pos++] = number;
42                 if (trace->trace_history_pos == trace->trace_history_size)
43                 {
44                         trace->trace_history_pos = 0;
45                         trace->trace_history_overflowed = 1;
46                 }
47         }
48         
49         return ERROR_OK;
50 }
51
52 static int handle_trace_point_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
53 {
54         target_t *target = get_current_target(cmd_ctx);
55         trace_t *trace = target->trace_info;
56         
57         if (argc == 0)
58         {
59                 u32 i;
60                 
61                 for (i = 0; i < trace->num_trace_points; i++)
62                 {
63                         command_print(cmd_ctx, "trace point 0x%8.8x (%lld times hit)",
64                                         trace->trace_points[i].address,
65                                         trace->trace_points[i].hit_counter);
66                 }
67
68                 return ERROR_OK;
69         }
70         
71         if (!strcmp(args[0], "clear"))
72         {
73                 if (trace->trace_points)
74                 {
75                         free(trace->trace_points);
76                         trace->trace_points = NULL;
77                 }
78                 trace->num_trace_points = 0;
79                 trace->trace_points_size = 0;
80                 
81                 return ERROR_OK;
82         }
83         
84         /* resize array if necessary */
85         if (!trace->trace_points || (trace->trace_points_size == trace->num_trace_points))
86         {
87                 trace->trace_points = realloc(trace->trace_points, sizeof(trace_point_t) * (trace->trace_points_size + 32));
88                 trace->trace_points_size += 32;
89         }
90         
91         trace->trace_points[trace->num_trace_points].address = strtoul(args[0], NULL, 0);
92         trace->trace_points[trace->num_trace_points].hit_counter = 0;
93         trace->num_trace_points++;
94         
95         return ERROR_OK;
96 }
97
98 static int handle_trace_history_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
99 {
100         target_t *target = get_current_target(cmd_ctx);
101         trace_t *trace = target->trace_info;
102         
103         if (argc > 0)
104         {
105                 trace->trace_history_pos = 0;
106                 trace->trace_history_overflowed = 0;
107
108                 if (!strcmp(args[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                 trace->trace_history_size = strtoul(args[0], NULL, 0);
118                 trace->trace_history = malloc(sizeof(u32) * trace->trace_history_size);
119                 
120                 command_print(cmd_ctx, "new trace history size: %i", trace->trace_history_size);
121         }
122         else
123         {
124                 u32 i;
125                 u32 first = 0;
126                 u32 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                                 u32 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.8x",
145                                         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-", trace->trace_history[i % trace->trace_history_size]);
152                         }
153                 }
154         }
155
156         return ERROR_OK;
157 }
158
159 int trace_register_commands(struct command_context_s *cmd_ctx)
160 {
161         command_t *trace_cmd =
162                 register_command(cmd_ctx, NULL, "trace", NULL, COMMAND_ANY, "trace commands");
163         
164         register_command(cmd_ctx, trace_cmd, "history", handle_trace_history_command,
165                 COMMAND_EXEC, "display trace history, ['clear'] history or set [size]");
166
167         register_command(cmd_ctx, trace_cmd, "point", handle_trace_point_command,
168                 COMMAND_EXEC, "display trace points, ['clear'] list of trace points, or add new tracepoint at [address]");
169
170         return ERROR_OK;
171 }