dynamic_execution_trace.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* Copyright (C) 2016 Jeremiah Orians
  2. * This file is part of stage0.
  3. *
  4. * stage0 is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * stage0 is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with stage0. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <stdint.h>
  20. #include <stdbool.h>
  21. #include <string.h>
  22. /* Unique instruction type */
  23. struct instruction_trace
  24. {
  25. uint64_t count;
  26. char name[255];
  27. struct instruction_trace* next;
  28. struct instruction_trace* prev;
  29. };
  30. static struct instruction_trace* traces;
  31. struct instruction_trace* create_trace(char* c)
  32. {
  33. struct instruction_trace* p;
  34. p = calloc(1, sizeof(struct instruction_trace));
  35. strncpy(p->name, c, 255);
  36. p->count = 1;
  37. return p;
  38. }
  39. struct instruction_trace* add_trace(struct instruction_trace* head, struct instruction_trace* p)
  40. {
  41. if(NULL == head)
  42. {
  43. return p;
  44. }
  45. if(NULL == head->next)
  46. {
  47. head->next = p;
  48. p->prev = head;
  49. }
  50. else
  51. {
  52. add_trace(head->next, p);
  53. }
  54. return head;
  55. }
  56. bool update_trace(struct instruction_trace* p, char* c)
  57. {
  58. if(0 == strncmp(p->name, c, 255))
  59. {
  60. p->count = p->count + 1;
  61. return true;
  62. }
  63. if(NULL != p->next)
  64. {
  65. return update_trace(p->next, c);
  66. }
  67. return false;
  68. }
  69. void record_trace(char* c)
  70. {
  71. if((NULL != traces) && (update_trace(traces, c)))
  72. {
  73. return;
  74. }
  75. traces = add_trace(traces, create_trace(c));
  76. }
  77. struct instruction_trace* print_trace(struct instruction_trace* p)
  78. {
  79. printf("%s\t%u\n", p->name, (unsigned int)p->count);
  80. return p->next;
  81. }
  82. void print_traces()
  83. {
  84. struct instruction_trace* i = traces;
  85. while(NULL != i)
  86. {
  87. i = print_trace(i);
  88. }
  89. }