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.
 
 
 
 
 
 

172 lines
5.2 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. #include <inttypes.h>
  27. int trace_point(target_t *target, u32 number)
  28. {
  29. trace_t *trace = target->trace_info;
  30. LOG_DEBUG("tracepoint: %i", number);
  31. if (number < trace->num_trace_points)
  32. trace->trace_points[number].hit_counter++;
  33. if (trace->trace_history_size)
  34. {
  35. trace->trace_history[trace->trace_history_pos++] = number;
  36. if (trace->trace_history_pos == trace->trace_history_size)
  37. {
  38. trace->trace_history_pos = 0;
  39. trace->trace_history_overflowed = 1;
  40. }
  41. }
  42. return ERROR_OK;
  43. }
  44. static int handle_trace_point_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  45. {
  46. target_t *target = get_current_target(cmd_ctx);
  47. trace_t *trace = target->trace_info;
  48. if (argc == 0)
  49. {
  50. u32 i;
  51. for (i = 0; i < trace->num_trace_points; i++)
  52. {
  53. command_print(cmd_ctx, "trace point 0x%8.8x (%lld times hit)",
  54. trace->trace_points[i].address,
  55. trace->trace_points[i].hit_counter);
  56. }
  57. return ERROR_OK;
  58. }
  59. if (!strcmp(args[0], "clear"))
  60. {
  61. if (trace->trace_points)
  62. {
  63. free(trace->trace_points);
  64. trace->trace_points = NULL;
  65. }
  66. trace->num_trace_points = 0;
  67. trace->trace_points_size = 0;
  68. return ERROR_OK;
  69. }
  70. /* resize array if necessary */
  71. if (!trace->trace_points || (trace->trace_points_size == trace->num_trace_points))
  72. {
  73. trace->trace_points = realloc(trace->trace_points, sizeof(trace_point_t) * (trace->trace_points_size + 32));
  74. trace->trace_points_size += 32;
  75. }
  76. trace->trace_points[trace->num_trace_points].address = strtoul(args[0], NULL, 0);
  77. trace->trace_points[trace->num_trace_points].hit_counter = 0;
  78. trace->num_trace_points++;
  79. return ERROR_OK;
  80. }
  81. static int handle_trace_history_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  82. {
  83. target_t *target = get_current_target(cmd_ctx);
  84. trace_t *trace = target->trace_info;
  85. if (argc > 0)
  86. {
  87. trace->trace_history_pos = 0;
  88. trace->trace_history_overflowed = 0;
  89. if (!strcmp(args[0], "clear"))
  90. {
  91. /* clearing is implicit, we've just reset position anyway */
  92. return ERROR_OK;
  93. }
  94. if (trace->trace_history)
  95. free(trace->trace_history);
  96. trace->trace_history_size = strtoul(args[0], NULL, 0);
  97. trace->trace_history = malloc(sizeof(u32) * trace->trace_history_size);
  98. command_print(cmd_ctx, "new trace history size: %i", trace->trace_history_size);
  99. }
  100. else
  101. {
  102. u32 i;
  103. u32 first = 0;
  104. u32 last = trace->trace_history_pos;
  105. if ( !trace->trace_history_size ) {
  106. command_print(cmd_ctx, "trace history buffer is not allocated");
  107. return ERROR_OK;
  108. }
  109. if (trace->trace_history_overflowed)
  110. {
  111. first = trace->trace_history_pos;
  112. last = trace->trace_history_pos - 1;
  113. }
  114. for (i = first; (i % trace->trace_history_size) != last; i++)
  115. {
  116. if (trace->trace_history[i % trace->trace_history_size] < trace->num_trace_points)
  117. {
  118. u32 address;
  119. address = trace->trace_points[trace->trace_history[i % trace->trace_history_size]].address;
  120. command_print(cmd_ctx, "trace point %i: 0x%8.8x",
  121. trace->trace_history[i % trace->trace_history_size],
  122. address);
  123. }
  124. else
  125. {
  126. command_print(cmd_ctx, "trace point %i: -not defined-", trace->trace_history[i % trace->trace_history_size]);
  127. }
  128. }
  129. }
  130. return ERROR_OK;
  131. }
  132. int trace_register_commands(struct command_context_s *cmd_ctx)
  133. {
  134. command_t *trace_cmd =
  135. register_command(cmd_ctx, NULL, "trace", NULL, COMMAND_ANY, "trace commands");
  136. register_command(cmd_ctx, trace_cmd, "history", handle_trace_history_command,
  137. COMMAND_EXEC, "display trace history, ['clear'] history or set [size]");
  138. register_command(cmd_ctx, trace_cmd, "point", handle_trace_point_command,
  139. COMMAND_EXEC, "display trace points, ['clear'] list of trace points, or add new tracepoint at [address]");
  140. return ERROR_OK;
  141. }