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.

shell_cmd_breakpoint.c 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 "shell_cmds.h"
  14. /** Add a breakpoint
  15. * @param asmsh_t* The shell
  16. * @param asmsh_cmd_args_t* The command arguments
  17. * @param int The first argument of the expression
  18. * @return -1 on error else 0
  19. */
  20. static int brk_add(asmsh_t *sh, asmsh_cmd_args_t *args, int expr_first);
  21. /** Remove a breakpoint
  22. * @param asmsh_t* The shell
  23. * @param asmsh_cmd_args_t* The command arguments
  24. * @param int The first argument of the expression
  25. * @return -1 on error else 0
  26. */
  27. static int brk_del(asmsh_t *sh, asmsh_cmd_args_t *args, int expr_first);
  28. /** List all breakpoints
  29. * @param asmsh_t* The shell
  30. * @param asmsh_cmd_args_t* The command arguments
  31. * @param int The first argument of the expression
  32. * @return -1 on error else 0
  33. * @todo add filters arguments
  34. */
  35. static int brk_ls(asmsh_t *sh, asmsh_cmd_args_t *args, int expr_first);
  36. /** Parse a breakpoint address expression
  37. *
  38. * An expression must be of the form A [OP B] with A and B
  39. * an integer or '.' for current RIP value and OP one of + or -
  40. *
  41. * @param asmsh_cmd_args_t* The breakpoint command argument list
  42. * @param int The first argument of the address expression
  43. * @param const unsigned long RIP value
  44. * @param unsigned long* A pointer on the result address
  45. *
  46. * @returns -1 on error and 0 if ok
  47. */
  48. static int parse_addr_expr(asmsh_cmd_args_t *args, int first,
  49. const unsigned long rip, unsigned long *addr);
  50. /** Utility function for @ref parse_addr_expr() that parse an expression value.
  51. *
  52. * An expression can be . for rip value or an unsigned long, possibly in hex, etc
  53. * @param const char* The value
  54. * @param unsigned long The value of RIP
  55. * @param unsigned long* A pointer on the result
  56. * @return -1 on error else 0
  57. */
  58. static int addr_expr_val(const char *val, unsigned long rip, unsigned long *res);
  59. void log_brk_usage()
  60. {
  61. asmsh_log_info("Available commands are : add, a, del, d, rm, list, ls, l");
  62. }
  63. void log_expr_usage()
  64. {
  65. asmsh_log_info("Bad argument, expected an expression of the form A + B or A - B with A and B an integer or '.' (for current RIP value)");
  66. }
  67. int asmsh_cmd_breakpoint(asmsh_t *sh, asmsh_cmd_args_t *args)
  68. {
  69. int ret;
  70. if(args->argc == 0 || args->args[0][0] == '.' || \
  71. (args->args[0][0] >= '0' && args->args[0][0] <= '9'))
  72. {
  73. if((ret = brk_add(sh, args, 0)) < 0)
  74. {
  75. log_expr_usage();
  76. }
  77. return ret;
  78. }
  79. // At least 1 arg, 1st arg chr is not in [0-9] nor is '.'
  80. // Looking for specific commands argument
  81. if(strcmp(args->args[0], "a") == 0 || \
  82. strcmp(args->args[0], "add") == 0)
  83. {
  84. if((ret = brk_add(sh, args, 1)) < 0)
  85. {
  86. log_expr_usage();
  87. }
  88. return ret;
  89. }
  90. else if(strcmp(args->args[0], "d") == 0 || \
  91. strcmp(args->args[0], "del") == 0 || \
  92. strcmp(args->args[0], "rm") == 0)
  93. {
  94. ret = brk_del(sh, args, 1);
  95. return ret;
  96. }
  97. else if(strcmp(args->args[0], "list") == 0 || \
  98. strcmp(args->args[0], "ls") == 0 || \
  99. strcmp(args->args[0], "l") == 0)
  100. {
  101. ret = brk_ls(sh, args, 1);
  102. return ret;
  103. }
  104. else
  105. {
  106. asmsh_log_error("Unrecognized action '%s'", args->args[0]);
  107. log_brk_usage();
  108. }
  109. return -1;
  110. }
  111. static int brk_add(asmsh_t *sh, asmsh_cmd_args_t *args, int expr_first)
  112. {
  113. const unsigned long rip = sh->env->regs.rip;
  114. unsigned long addr;
  115. if(parse_addr_expr(args, expr_first, rip, &addr) < 0)
  116. {
  117. return -1;
  118. }
  119. int ret = asmsh_brk_add(&sh->env->brks, addr);
  120. if(ret < 0)
  121. {
  122. int err = errno;
  123. asmsh_log_error("Unable to set breakpoint @ %016lX : %s",
  124. addr, strerror(errno));
  125. errno = err;
  126. return -1;
  127. }
  128. else if(ret > 0)
  129. {
  130. asmsh_log_warning("Breakpoint @ %016lX allready set", addr);
  131. return -1;
  132. }
  133. asmsh_log_info("Set breakpoint @ %016lX", addr);
  134. return 0;
  135. }
  136. static int brk_del(asmsh_t *sh, asmsh_cmd_args_t *args, int expr_first)
  137. {
  138. const unsigned long rip = sh->env->regs.rip;
  139. unsigned long addr;
  140. if(parse_addr_expr(args, expr_first, rip, &addr) < 0)
  141. {
  142. return -1;
  143. }
  144. int ret = asmsh_brk_del(&sh->env->brks, addr);
  145. if(ret < 0)
  146. {
  147. int err = errno;
  148. asmsh_log_error("Unable to remove breakpoint @ %016lX : %s",
  149. addr, strerror(errno));
  150. errno = err;
  151. return -1;
  152. }
  153. else if(ret > 0)
  154. {
  155. asmsh_log_warning("Breakpoint @ %016lX do not exists", addr);
  156. return -1;
  157. }
  158. asmsh_log_info("Removed breakpoint @ %016lX", addr);
  159. return 0;
  160. }
  161. static int brk_ls(asmsh_t *sh, asmsh_cmd_args_t *args, int expr_first)
  162. {
  163. for(size_t i=0; i<sh->env->brks.sz; i++)
  164. {
  165. printf("0x%016lx\n", sh->env->brks.addrs[i]);
  166. }
  167. printf("%ld breakpoints\n", sh->env->brks.sz);
  168. return 0;
  169. }
  170. static int addr_expr_val(const char *val, unsigned long rip, unsigned long *res)
  171. {
  172. if(strcmp(".", val) == 0)
  173. {
  174. *res = rip;
  175. return 0;
  176. }
  177. char *endptr;
  178. errno = 0;
  179. *res = strtoul(val, &endptr, 0);
  180. if(errno != 0)
  181. {
  182. return -1;
  183. }
  184. else if(*endptr != '\0')
  185. {
  186. errno = EINVAL;
  187. return -1;
  188. }
  189. return 0;
  190. }
  191. static int parse_addr_expr(asmsh_cmd_args_t *args, int first,
  192. const unsigned long rip, unsigned long *addr)
  193. {
  194. char * const * argv = args->args + first;
  195. const int argc = args->argc - first;
  196. unsigned long val[2];
  197. switch(argc)
  198. {
  199. case 0:
  200. *addr = rip;
  201. break;
  202. case 1:
  203. if(addr_expr_val(argv[0], rip, addr) < 0)
  204. {
  205. int err = errno;
  206. asmsh_log_error("Invalid expression value '%s': %s",
  207. argv[0], strerror(errno));
  208. errno = err;
  209. return -1;
  210. }
  211. break;
  212. case 3:
  213. for(int i=0; i<2; i++)
  214. {
  215. const char *arg = i ? argv[2]:argv[0];
  216. if(addr_expr_val(arg, rip, &val[i]) < 0)
  217. {
  218. asmsh_log_error("Invalid value '%s' in expression '%s %s %s' : %s",
  219. arg,
  220. argv[0], argv[1], argv[2],
  221. strerror(errno));
  222. return -1;
  223. }
  224. }
  225. const char operator = strlen(argv[1]) == 1 ? argv[1][0]: '\0';
  226. switch(operator)
  227. {
  228. case '+':
  229. *addr = val[0] + val[1];
  230. break;
  231. case '-':
  232. *addr = val[0] - val[1];
  233. break;
  234. default:
  235. asmsh_log_error("Invalid operator '%s' in expression '%s %s %s'",
  236. argv[1],
  237. argv[0], argv[1], argv[2]);
  238. return -1;
  239. }
  240. break;
  241. default:
  242. //USAGE !
  243. asmsh_log_error("Unexexpected argument count for an expression. Expecting one of 0, 1 or 3 but got %d", argc);
  244. return -1;
  245. }
  246. return 0;
  247. }