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.
 
 
 
 
 
 

170 lines
5.3 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 "log.h"
  24. #include "trace.h"
  25. #include "target.h"
  26. int trace_point(target_t *target, uint32_t number)
  27. {
  28. trace_t *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. static int handle_trace_point_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  44. {
  45. target_t *target = get_current_target(cmd_ctx);
  46. trace_t *trace = target->trace_info;
  47. if (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(args[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(trace_point_t) * (trace->trace_points_size + 32));
  73. trace->trace_points_size += 32;
  74. }
  75. trace->trace_points[trace->num_trace_points].address = strtoul(args[0], NULL, 0);
  76. trace->trace_points[trace->num_trace_points].hit_counter = 0;
  77. trace->num_trace_points++;
  78. return ERROR_OK;
  79. }
  80. static int handle_trace_history_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  81. {
  82. target_t *target = get_current_target(cmd_ctx);
  83. trace_t *trace = target->trace_info;
  84. if (argc > 0)
  85. {
  86. trace->trace_history_pos = 0;
  87. trace->trace_history_overflowed = 0;
  88. if (!strcmp(args[0], "clear"))
  89. {
  90. /* clearing is implicit, we've just reset position anyway */
  91. return ERROR_OK;
  92. }
  93. if (trace->trace_history)
  94. free(trace->trace_history);
  95. trace->trace_history_size = strtoul(args[0], NULL, 0);
  96. trace->trace_history = malloc(sizeof(uint32_t) * trace->trace_history_size);
  97. command_print(cmd_ctx, "new trace history size: %i", (int)(trace->trace_history_size));
  98. }
  99. else
  100. {
  101. uint32_t i;
  102. uint32_t first = 0;
  103. uint32_t last = trace->trace_history_pos;
  104. if (!trace->trace_history_size) {
  105. command_print(cmd_ctx, "trace history buffer is not allocated");
  106. return ERROR_OK;
  107. }
  108. if (trace->trace_history_overflowed)
  109. {
  110. first = trace->trace_history_pos;
  111. last = trace->trace_history_pos - 1;
  112. }
  113. for (i = first; (i % trace->trace_history_size) != last; i++)
  114. {
  115. if (trace->trace_history[i % trace->trace_history_size] < trace->num_trace_points)
  116. {
  117. uint32_t address;
  118. address = trace->trace_points[trace->trace_history[i % trace->trace_history_size]].address;
  119. command_print(cmd_ctx, "trace point %i: 0x%8.8" PRIx32 "",
  120. (int)(trace->trace_history[i % trace->trace_history_size]),
  121. address);
  122. }
  123. else
  124. {
  125. command_print(cmd_ctx, "trace point %i: -not defined-", (int)(trace->trace_history[i % trace->trace_history_size]));
  126. }
  127. }
  128. }
  129. return ERROR_OK;
  130. }
  131. int trace_register_commands(struct command_context_s *cmd_ctx)
  132. {
  133. command_t *trace_cmd =
  134. register_command(cmd_ctx, NULL, "trace", NULL, COMMAND_ANY, "trace commands");
  135. register_command(cmd_ctx, trace_cmd, "history", handle_trace_history_command,
  136. COMMAND_EXEC, "display trace history, ['clear'] history or set [size]");
  137. register_command(cmd_ctx, trace_cmd, "point", handle_trace_point_command,
  138. COMMAND_EXEC, "display trace points, ['clear'] list of trace points, or add new tracepoint at [address]");
  139. return ERROR_OK;
  140. }