trace.c 5.29 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/***************************************************************************
 *   Copyright (C) 2005, 2007 by Dominic Rath                              *
 *   Dominic.Rath@gmx.de                                                   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "replacements.h"
#include "log.h"
#include "trace.h"
#include "target.h"
28
#include "command.h"
29
30

#include <stdlib.h>
31
32
33
#include <string.h>
#include <inttypes.h>

34
int trace_point(target_t *target, u32 number)
35
36
37
{
	trace_t *trace = target->trace_info;

38
	LOG_DEBUG("tracepoint: %i", number);
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
	
	if (number < trace->num_trace_points)
		trace->trace_points[number].hit_counter++;

	if (trace->trace_history_size)
	{
		trace->trace_history[trace->trace_history_pos++] = number;
		if (trace->trace_history_pos == trace->trace_history_size)
		{
			trace->trace_history_pos = 0;
			trace->trace_history_overflowed = 1;
		}
	}
	
	return ERROR_OK;
}

int handle_trace_point_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
{
	target_t *target = get_current_target(cmd_ctx);
	trace_t *trace = target->trace_info;
	
	if (argc == 0)
	{
63
		u32 i;
64
65
66
67
68
69
70
71
72
73
74
75
76
77
		
		for (i = 0; i < trace->num_trace_points; i++)
		{
			command_print(cmd_ctx, "trace point 0x%8.8x (%"PRIi64" times hit)",
					trace->trace_points[i].address,
					trace->trace_points[i].hit_counter);
		}

		return ERROR_OK;
	}
	
	if (!strcmp(args[0], "clear"))
	{
		if (trace->trace_points)
78
		{
79
			free(trace->trace_points);
80
81
			trace->trace_points = NULL;
		}
82
		trace->num_trace_points = 0;
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
		trace->trace_points_size = 0;
		
		return ERROR_OK;
	}
	
	/* resize array if necessary */
	if (!trace->trace_points || (trace->trace_points_size == trace->num_trace_points))
	{
		trace->trace_points = realloc(trace->trace_points, sizeof(trace_point_t) * (trace->trace_points_size + 32));
		trace->trace_points_size += 32;
	}
	
	trace->trace_points[trace->num_trace_points].address = strtoul(args[0], NULL, 0);
	trace->trace_points[trace->num_trace_points].hit_counter = 0;
	trace->num_trace_points++;
	
	return ERROR_OK;
}
101
102
103
104
105
106
107
108

int handle_trace_history_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
{
	target_t *target = get_current_target(cmd_ctx);
	trace_t *trace = target->trace_info;
	
	if (argc > 0)
	{
109
110
111
112
113
114
115
116
117
		trace->trace_history_pos = 0;
		trace->trace_history_overflowed = 0;

		if (!strcmp(args[0], "clear"))
		{
			/* clearing is implicit, we've just reset position anyway */
			return ERROR_OK;
		}
		
118
119
120
121
122
123
124
125
126
127
		if (trace->trace_history)
			free(trace->trace_history);
		
		trace->trace_history_size = strtoul(args[0], NULL, 0);
		trace->trace_history = malloc(sizeof(u32) * trace->trace_history_size);
		
		command_print(cmd_ctx, "new trace history size: %i", trace->trace_history_size);
	}
	else
	{
128
129
130
		u32 i;
		u32 first = 0;
		u32 last = trace->trace_history_pos;
131
132
133
134
135

		if ( !trace->trace_history_size ) {
			command_print(cmd_ctx, "trace history buffer is not allocated");
			return ERROR_OK;
		}
136
137
138
139
140
		if (trace->trace_history_overflowed)
		{
			first = trace->trace_history_pos;
			last = trace->trace_history_pos - 1;
		}
141
		
142
		for (i = first; (i % trace->trace_history_size) != last; i++)
143
		{
144
			if (trace->trace_history[i % trace->trace_history_size] < trace->num_trace_points)
145
146
			{
				u32 address;
147
				address = trace->trace_points[trace->trace_history[i % trace->trace_history_size]].address;
148
				command_print(cmd_ctx, "trace point %i: 0x%8.8x",
149
					trace->trace_history[i % trace->trace_history_size],
150
151
152
153
154
					address);
			}

			else
			{
155
				command_print(cmd_ctx, "trace point %i: -not defined-", trace->trace_history[i % trace->trace_history_size]);
156
157
158
159
160
161
			}
		}
	}

	return ERROR_OK;
}
162
163
164
165
166
167
168
169
170
171
172
173
174
175

int trace_register_commands(struct command_context_s *cmd_ctx)
{
	command_t *trace_cmd =
		register_command(cmd_ctx, NULL, "trace", NULL, COMMAND_ANY, "trace commands");
	
	register_command(cmd_ctx, trace_cmd, "history", handle_trace_history_command,
		COMMAND_EXEC, "display trace history, ['clear'] history or set [size]");

	register_command(cmd_ctx, trace_cmd, "point", handle_trace_point_command,
		COMMAND_EXEC, "display trace points, ['clear'] list of trace points, or add new tracepoint at [address]");

	return ERROR_OK;
}