You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

191 lines
5.6 KiB

  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. #include <helper/log.h>
  24. #include "trace.h"
  25. #include "target.h"
  26. int trace_point(struct target *target, uint32_t number)
  27. {
  28. struct trace *trace = target->trace_info;
  29. LOG_DEBUG("tracepoint: %i", (int)number);
  30. if (number < trace->num_trace_points)
  31. trace->trace_points[number].hit_counter++;
  32. if (trace->trace_history_size)
  33. {
  34. trace->trace_history[trace->trace_history_pos++] = number;
  35. if (trace->trace_history_pos == trace->trace_history_size)
  36. {
  37. trace->trace_history_pos = 0;
  38. trace->trace_history_overflowed = 1;
  39. }
  40. }
  41. return ERROR_OK;
  42. }
  43. COMMAND_HANDLER(handle_trace_point_command)
  44. {
  45. struct target *target = get_current_target(CMD_CTX);
  46. struct trace *trace = target->trace_info;
  47. if (CMD_ARGC == 0)
  48. {
  49. uint32_t i;
  50. for (i = 0; i < trace->num_trace_points; i++)
  51. {
  52. command_print(CMD_CTX, "trace point 0x%8.8" PRIx32 " (%lld times hit)",
  53. trace->trace_points[i].address,
  54. (long long)trace->trace_points[i].hit_counter);
  55. }
  56. return ERROR_OK;
  57. }
  58. if (!strcmp(CMD_ARGV[0], "clear"))
  59. {
  60. if (trace->trace_points)
  61. {
  62. free(trace->trace_points);
  63. trace->trace_points = NULL;
  64. }
  65. trace->num_trace_points = 0;
  66. trace->trace_points_size = 0;
  67. return ERROR_OK;
  68. }
  69. /* resize array if necessary */
  70. if (!trace->trace_points || (trace->trace_points_size == trace->num_trace_points))
  71. {
  72. trace->trace_points = realloc(trace->trace_points, sizeof(struct trace_point) * (trace->trace_points_size + 32));
  73. trace->trace_points_size += 32;
  74. }
  75. uint32_t address;
  76. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
  77. trace->trace_points[trace->num_trace_points].address = address;
  78. trace->trace_points[trace->num_trace_points].hit_counter = 0;
  79. trace->num_trace_points++;
  80. return ERROR_OK;
  81. }
  82. COMMAND_HANDLER(handle_trace_history_command)
  83. {
  84. struct target *target = get_current_target(CMD_CTX);
  85. struct trace *trace = target->trace_info;
  86. if (CMD_ARGC > 0)
  87. {
  88. trace->trace_history_pos = 0;
  89. trace->trace_history_overflowed = 0;
  90. if (!strcmp(CMD_ARGV[0], "clear"))
  91. {
  92. /* clearing is implicit, we've just reset position anyway */
  93. return ERROR_OK;
  94. }
  95. if (trace->trace_history)
  96. free(trace->trace_history);
  97. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], trace->trace_history_size);
  98. trace->trace_history = malloc(sizeof(uint32_t) * trace->trace_history_size);
  99. command_print(CMD_CTX, "new trace history size: %i", (int)(trace->trace_history_size));
  100. }
  101. else
  102. {
  103. uint32_t i;
  104. uint32_t first = 0;
  105. uint32_t last = trace->trace_history_pos;
  106. if (!trace->trace_history_size) {
  107. command_print(CMD_CTX, "trace history buffer is not allocated");
  108. return ERROR_OK;
  109. }
  110. if (trace->trace_history_overflowed)
  111. {
  112. first = trace->trace_history_pos;
  113. last = trace->trace_history_pos - 1;
  114. }
  115. for (i = first; (i % trace->trace_history_size) != last; i++)
  116. {
  117. if (trace->trace_history[i % trace->trace_history_size] < trace->num_trace_points)
  118. {
  119. uint32_t address;
  120. address = trace->trace_points[trace->trace_history[i % trace->trace_history_size]].address;
  121. command_print(CMD_CTX, "trace point %i: 0x%8.8" PRIx32 "",
  122. (int)(trace->trace_history[i % trace->trace_history_size]),
  123. address);
  124. }
  125. else
  126. {
  127. command_print(CMD_CTX, "trace point %i: -not defined-", (int)(trace->trace_history[i % trace->trace_history_size]));
  128. }
  129. }
  130. }
  131. return ERROR_OK;
  132. }
  133. static const struct command_registration trace_exec_command_handlers[] = {
  134. {
  135. .name = "history",
  136. .handler = handle_trace_history_command,
  137. .mode = COMMAND_EXEC,
  138. .help = "display trace history, clear history or set size",
  139. .usage = "['clear'|size]",
  140. },
  141. {
  142. .name = "point",
  143. .handler = handle_trace_point_command,
  144. .mode = COMMAND_EXEC,
  145. .help = "display trace points, clear list of trace points, "
  146. "or add new tracepoint at address",
  147. .usage = "['clear'|address]",
  148. },
  149. COMMAND_REGISTRATION_DONE
  150. };
  151. static const struct command_registration trace_command_handlers[] = {
  152. {
  153. .name = "trace",
  154. .mode = COMMAND_EXEC,
  155. .help = "trace command group",
  156. .chain = trace_exec_command_handlers,
  157. },
  158. COMMAND_REGISTRATION_DONE
  159. };
  160. int trace_register_commands(struct command_context *cmd_ctx)
  161. {
  162. return register_commands(cmd_ctx, NULL, trace_command_handlers);
  163. }