123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- /* Copyright Yann Weber <asmsh@yannweb.net>
- This file is part of asmsh.
-
- asmsh is free software: you can redistribute it and/or modify it under the
- terms of the GNU General Public License as published by the Free Software
- Foundation, either version 3 of the License, or any later version.
-
- asmsh is distributed in the hope that it will be useful, but WITHOUT ANY
- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
- FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
- details.
-
- You should have received a copy of the GNU General Public License along
- with asmsh. If not, see <https://www.gnu.org/licenses/>.
- */
- #include "shell_cmds.h"
-
- /** Add a breakpoint
- * @param asmsh_t* The shell
- * @param asmsh_cmd_args_t* The command arguments
- * @param int The first argument of the expression
- * @return -1 on error else 0
- */
- static int brk_add(asmsh_t *sh, asmsh_cmd_args_t *args, int expr_first);
-
- /** Remove a breakpoint
- * @param asmsh_t* The shell
- * @param asmsh_cmd_args_t* The command arguments
- * @param int The first argument of the expression
- * @return -1 on error else 0
- */
- static int brk_del(asmsh_t *sh, asmsh_cmd_args_t *args, int expr_first);
-
- /** Add a breakpoint after an instruction
- * @param asmsh_t* The shell
- * @param asmsh_cmd_args_t* The command arguments
- * @param int The first argument of the expression
- * @return -1 on error else 0
- */
- static int brk_after(asmsh_t *sh, asmsh_cmd_args_t *args, int first);
-
- /** List all breakpoints
- * @param asmsh_t* The shell
- * @param asmsh_cmd_args_t* The command arguments
- * @param int The first argument of the expression
- * @return -1 on error else 0
- * @todo add filters arguments
- */
- static int brk_ls(asmsh_t *sh, asmsh_cmd_args_t *args, int expr_first);
-
- /** Add a breakpoint and display a message about it
- * @param asmsh_t The shell
- * @param unsigned long The breakpoint address
- * @returns 0 if no error or if allready exist else -1
- */
- static int brk_add_or_err(asmsh_t *sh, unsigned long addr);
-
-
- void log_brk_usage()
- {
- asmsh_log_info("Available commands are :");
- asmsh_log_info("\tadd [addr_expr]");
- asmsh_log_info("\t\tAdd a new breakpoint (optionnal/implicit)");
- asmsh_log_info("\tdel|d addr_expr");
- asmsh_log_info("\t\tDelete a breakpoint");
- asmsh_log_info("\tafter|a INSTRUCTION");
- asmsh_log_info("\t\tAdd a new breakpoint after an instruction");
- asmsh_log_info("\tlist|ls|l");
- asmsh_log_info("\t\tList existing breakpoints");
- asmsh_log_addr_expr_usage();
- }
-
-
- int asmsh_cmd_breakpoint(asmsh_t *sh, asmsh_cmd_args_t *args)
- {
- int ret;
-
- if(args->argc == 0 || args->args[0][0] == '.' || \
- (args->args[0][0] >= '0' && args->args[0][0] <= '9'))
- {
- if((ret = brk_add(sh, args, 0)) < 0)
- {
- asmsh_log_addr_expr_usage();
- }
- return ret;
- }
- // At least 1 arg, 1st arg chr is not in [0-9] nor is '.'
- // Looking for specific commands argument
- if(strcmp(args->args[0], "a") == 0 || \
- strcmp(args->args[0], "after") == 0)
- {
- return brk_after(sh, args, 1);
- }
- else if(strcmp(args->args[0], "add") == 0)
- {
- if((ret = brk_add(sh, args, 1)) < 0)
- {
- asmsh_log_addr_expr_usage();
- }
- return ret;
- }
- else if(strcmp(args->args[0], "d") == 0 || \
- strcmp(args->args[0], "del") == 0 || \
- strcmp(args->args[0], "rm") == 0)
- {
- ret = brk_del(sh, args, 1);
- return ret;
- }
- else if(strcmp(args->args[0], "list") == 0 || \
- strcmp(args->args[0], "ls") == 0 || \
- strcmp(args->args[0], "l") == 0)
- {
- ret = brk_ls(sh, args, 1);
- return ret;
- }
- else
- {
- asmsh_log_error("Unrecognized action '%s'", args->args[0]);
- log_brk_usage();
- }
- return -1;
- }
-
- static int brk_after(asmsh_t *sh, asmsh_cmd_args_t *args, int first)
- {
- asmsh_bytecode_t bcode;
- size_t sz;
- sz = 0;
- for(int i=1; i<args->argc; i++)
- {
- sz += strlen(args->args[i]) + 1;
- }
- char instr[sz];
- char *iptr = instr;
- for(int i=1; i<args->argc; i++)
- {
- iptr += sprintf(iptr, "%s ", args->args[i]);
- }
- instr[sz-1] = '\0';
-
- if(asmsh_asmc_compile(sh->cctx, instr, &bcode) < 0 ||\
- asmsh_env_write_code(sh->env, &bcode) < 0)
- {
- asmsh_log_perror("Unable to add breakpoint");
- return -1;
- }
- if(brk_add_or_err(sh, sh->env->regs.rip + bcode.size) < 0)
- {
- return -1;
- }
-
- int status;
- int ret = asmsh_env_step(sh->env, &status);
-
- return ret;
- }
-
- static int brk_add(asmsh_t *sh, asmsh_cmd_args_t *args, int expr_first)
- {
- const unsigned long rip = sh->env->regs.rip;
-
- unsigned long addr;
-
- if(asmsh_cmd_parse_addr_expr(args, expr_first, rip, &addr) < 0)
- {
- return -1;
- }
- return brk_add_or_err(sh, addr);
- }
-
- static int brk_add_or_err(asmsh_t *sh, unsigned long addr)
- {
- int ret = asmsh_brk_add(&sh->env->brks, addr);
- if(ret < 0)
- {
- int err = errno;
- asmsh_log_error("Unable to set breakpoint @ %016lX : %s",
- addr, strerror(errno));
- errno = err;
- return -1;
- }
- else if(ret > 0)
- {
- asmsh_log_warning("Breakpoint @ %016lX allready set", addr);
- return 0;
- }
- asmsh_log_info("Set breakpoint @ %016lX", addr);
- return 0;
- }
-
- static int brk_del(asmsh_t *sh, asmsh_cmd_args_t *args, int expr_first)
- {
- const unsigned long rip = sh->env->regs.rip;
-
- unsigned long addr;
-
- if(asmsh_cmd_parse_addr_expr(args, expr_first, rip, &addr) < 0)
- {
- return -1;
- }
-
- int ret = asmsh_brk_del(&sh->env->brks, addr);
- if(ret < 0)
- {
- int err = errno;
- asmsh_log_error("Unable to remove breakpoint @ %016lX : %s",
- addr, strerror(errno));
- errno = err;
- return -1;
- }
- else if(ret > 0)
- {
- asmsh_log_warning("Breakpoint @ %016lX do not exists", addr);
- return -1;
- }
- asmsh_log_info("Removed breakpoint @ %016lX", addr);
- return 0;
- }
-
-
- static int brk_ls(asmsh_t *sh, asmsh_cmd_args_t *args, int expr_first)
- {
- for(size_t i=0; i<sh->env->brks.sz; i++)
- {
- printf("0x%016lx\n", sh->env->brks.addrs[i]);
- }
- printf("%ld breakpoints\n", sh->env->brks.sz);
- return 0;
- }
|