OpenOCD
trace.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /***************************************************************************
4  * Copyright (C) 2005, 2007 by Dominic Rath *
5  * Dominic.Rath@gmx.de *
6  ***************************************************************************/
7 
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif
11 
12 #include <helper/log.h>
13 #include "trace.h"
14 #include "target.h"
15 
16 int trace_point(struct target *target, uint32_t number)
17 {
18  struct trace *trace = target->trace_info;
19 
20  LOG_DEBUG("tracepoint: %i", (int)number);
21 
22  if (number < trace->num_trace_points)
24 
30  }
31  }
32 
33  return ERROR_OK;
34 }
35 
36 COMMAND_HANDLER(handle_trace_point_command)
37 {
39  struct trace *trace = target->trace_info;
40 
41  if (CMD_ARGC == 0) {
42  uint32_t i;
43 
44  for (i = 0; i < trace->num_trace_points; i++) {
45  command_print(CMD, "trace point 0x%8.8" PRIx32 " (%lld times hit)",
47  (long long)trace->trace_points[i].hit_counter);
48  }
49 
50  return ERROR_OK;
51  }
52 
53  if (!strcmp(CMD_ARGV[0], "clear")) {
54  free(trace->trace_points);
56 
59 
60  return ERROR_OK;
61  }
62 
63  /* resize array if necessary */
66  sizeof(struct trace_point) * (trace->trace_points_size + 32));
67  trace->trace_points_size += 32;
68  }
69 
70  uint32_t address;
71  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
75 
76  return ERROR_OK;
77 }
78 
79 COMMAND_HANDLER(handle_trace_history_command)
80 {
82  struct trace *trace = target->trace_info;
83 
84  if (CMD_ARGC > 0) {
87 
88  if (!strcmp(CMD_ARGV[0], "clear")) {
89  /* clearing is implicit, we've just reset position anyway */
90  return ERROR_OK;
91  }
92 
93  free(trace->trace_history);
94 
96  trace->trace_history = malloc(sizeof(uint32_t) * trace->trace_history_size);
97 
98  command_print(CMD, "new trace history size: %i", (int)(trace->trace_history_size));
99  } else {
100  uint32_t i;
101  uint32_t first = 0;
102  uint32_t last = trace->trace_history_pos;
103 
104  if (!trace->trace_history_size) {
105  command_print(CMD, "trace history buffer is not allocated");
106  return ERROR_OK;
107  }
108 
110  first = trace->trace_history_pos;
111  last = trace->trace_history_pos - 1;
112  }
113 
114  for (i = first; (i % trace->trace_history_size) != last; i++) {
116  uint32_t address;
118  command_print(CMD, "trace point %i: 0x%8.8" PRIx32 "",
120  address);
121  } else
122  command_print(CMD, "trace point %i: -not defined-",
124  }
125  }
126 
127  return ERROR_OK;
128 }
129 
130 static const struct command_registration trace_exec_command_handlers[] = {
131  {
132  .name = "history",
133  .handler = handle_trace_history_command,
134  .mode = COMMAND_EXEC,
135  .help = "display trace history, clear history or set size",
136  .usage = "['clear'|size]",
137  },
138  {
139  .name = "point",
140  .handler = handle_trace_point_command,
141  .mode = COMMAND_EXEC,
142  .help = "display trace points, clear list of trace points, "
143  "or add new tracepoint at address",
144  .usage = "['clear'|address]",
145  },
147 };
148 static const struct command_registration trace_command_handlers[] = {
149  {
150  .name = "trace",
151  .mode = COMMAND_EXEC,
152  .help = "trace command group",
153  .usage = "",
155  },
157 };
158 
160 {
162 }
void command_print(struct command_invocation *cmd, const char *format,...)
Definition: command.c:473
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
Definition: command.h:140
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
Definition: command.h:155
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
Definition: command.h:150
#define COMMAND_PARSE_NUMBER(type, in, out)
parses the string in into out as a type, or prints a command error and passes the error code to the c...
Definition: command.h:425
#define CMD_CTX
Use this macro to access the context of the command being handled, rather than accessing the variable...
Definition: command.h:145
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:247
static int register_commands(struct command_context *cmd_ctx, const char *cmd_prefix, const struct command_registration *cmds)
Register one or more commands in the specified context, as children of parent (or top-level commends,...
Definition: command.h:268
@ COMMAND_EXEC
Definition: command.h:40
enum esirisc_reg_num number
Definition: esirisc.c:87
#define LOG_DEBUG(expr ...)
Definition: log.h:109
#define ERROR_OK
Definition: log.h:155
const char * name
Definition: command.h:229
Definition: target.h:120
struct trace * trace_info
Definition: target.h:166
uint32_t address
Definition: trace.h:17
uint64_t hit_counter
Definition: trace.h:18
Definition: trace.h:21
struct trace_point * trace_points
Definition: trace.h:24
uint32_t trace_history_pos
Definition: trace.h:27
uint32_t trace_points_size
Definition: trace.h:23
uint32_t * trace_history
Definition: trace.h:26
uint32_t trace_history_size
Definition: trace.h:25
uint32_t num_trace_points
Definition: trace.h:22
int trace_history_overflowed
Definition: trace.h:28
struct target * get_current_target(struct command_context *cmd_ctx)
Definition: target.c:536
static const struct command_registration trace_command_handlers[]
Definition: trace.c:148
static const struct command_registration trace_exec_command_handlers[]
Definition: trace.c:130
int trace_point(struct target *target, uint32_t number)
Definition: trace.c:16
int trace_register_commands(struct command_context *cmd_ctx)
Definition: trace.c:159
COMMAND_HANDLER(handle_trace_point_command)
Definition: trace.c:36
#define NULL
Definition: usb.h:16