A shell that runs x86_64 assembly
c
x86-64
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.

breakpoints.c 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* Copyright Yann Weber <asmsh@yannweb.net>
  2. This file is part of asmsh.
  3. asmsh is free software: you can redistribute it and/or modify it under the
  4. terms of the GNU General Public License as published by the Free Software
  5. Foundation, either version 3 of the License, or any later version.
  6. asmsh is distributed in the hope that it will be useful, but WITHOUT ANY
  7. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  9. details.
  10. You should have received a copy of the GNU General Public License along
  11. with asmsh. If not, see <https://www.gnu.org/licenses/>.
  12. */
  13. #include "breakpoints.h"
  14. /** Efficient search of a breakpoint address
  15. *
  16. * @param brks pointer on the breakpoint list
  17. * @param addr the address to search
  18. * @param idx a pointer on the breakpoint index
  19. * @return -1 if not found else 0
  20. *
  21. * @note idx is set to a value suitable for insertion if value is not found
  22. */
  23. static int asmsh_brk_index(asmsh_brk_t *brks, unsigned long addr, size_t *idx);
  24. int asmsh_brk_init(asmsh_brk_t *brks)
  25. {
  26. brks->addrs = NULL;
  27. brks->sz = 0;
  28. return 0;
  29. }
  30. void asmsh_brk_free(asmsh_brk_t *brks)
  31. {
  32. free(brks->addrs);
  33. }
  34. int asmsh_brk_add(asmsh_brk_t *brks, unsigned long addr)
  35. {
  36. size_t idx;
  37. int ret;
  38. void *tmp;
  39. ret = asmsh_brk_index(brks, addr, &idx);
  40. if(ret == 0)
  41. {
  42. return 1;
  43. }
  44. if(brks->sz > 0)
  45. {
  46. tmp = realloc(brks->addrs, sizeof(*brks->addrs)*(brks->sz + 1));
  47. if(tmp == NULL)
  48. {
  49. int err = errno;
  50. asmsh_log_perror("Unable to reallocate breakpoint list");
  51. errno = err;
  52. return -1;
  53. }
  54. brks->addrs=tmp;
  55. }
  56. else
  57. {
  58. if(!(brks->addrs = malloc(sizeof(*brks->addrs))))
  59. {
  60. int err = errno;
  61. asmsh_log_perror("Unable to allocate breakpoint list");
  62. errno = err;
  63. return -1;
  64. }
  65. }
  66. if(idx == brks->sz)
  67. {
  68. brks->addrs[brks->sz] = addr;
  69. }
  70. else
  71. {
  72. memmove(&(brks->addrs[idx+1]),&(brks->addrs[idx]),
  73. (brks->sz - idx)*sizeof(*brks->addrs));
  74. brks->addrs[idx] = addr;
  75. }
  76. brks->sz++;
  77. return 0;
  78. }
  79. int asmsh_brk_del(asmsh_brk_t *brks, unsigned long addr)
  80. {
  81. size_t idx;
  82. int ret;
  83. void *tmp;
  84. ret = asmsh_brk_index(brks, addr, &idx);
  85. if(ret < 0)
  86. {
  87. return 1;
  88. }
  89. brks->sz--;
  90. if(idx < brks->sz)
  91. {
  92. memmove(&(brks->addrs[idx]), &(brks->addrs[idx+1]),
  93. sizeof(*brks->addrs)*brks->sz);
  94. }
  95. if(brks->sz)
  96. {
  97. tmp = realloc(brks->addrs, brks->sz);
  98. if(tmp == NULL)
  99. {
  100. int err = errno;
  101. asmsh_log_perror("Unable to reallocate breakpoint list when removing");
  102. errno = err;
  103. return -1;
  104. }
  105. brks->addrs=tmp;
  106. }
  107. else
  108. {
  109. free(brks->addrs);
  110. brks->addrs = NULL;
  111. }
  112. return 0;
  113. }
  114. int asmsh_brk_isset(asmsh_brk_t *brks, unsigned long addr)
  115. {
  116. size_t idx;
  117. return asmsh_brk_index(brks, addr, &idx) == 0;
  118. }
  119. static int asmsh_brk_index(asmsh_brk_t *brks, unsigned long addr, size_t *idx)
  120. {
  121. size_t beg, end, mid;
  122. if(brks->sz == 0)
  123. {
  124. *idx = 0;
  125. return -1;
  126. }
  127. beg = 0;
  128. end = brks->sz;
  129. while(end >= beg)
  130. {
  131. mid = (beg + end) / 2;
  132. if(brks->addrs[mid] == addr)
  133. {
  134. *idx = mid;
  135. return 0;
  136. }
  137. else if(brks->addrs[mid] > addr)
  138. {
  139. if(mid == 0) { break; }
  140. end = mid - 1;
  141. }
  142. else
  143. {
  144. beg = mid + 1;
  145. if(beg >= brks->sz) { break; }
  146. }
  147. }
  148. *idx = beg;
  149. return -1;
  150. }