/* Copyright Yann Weber 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 . */ #ifndef ASMSH_SHELL_CMDS_H #define ASMSH_SHELL_CMDS_H #include "config.h" #include #include #include #include #include "logger.h" #define ASMSH_CMD_MAXLEN 31 #define ASMSH_CMD_MAXARG 15 typedef struct asmsh_cmd_s asmsh_cmd_t; typedef struct asmsh_cmd_args_s asmsh_cmd_args_t; #include "shell.h" /** @return <0 on recoverable error 0 on ok, 1+status on exit */ typedef int (asmsh_cmd_f)(asmsh_t*, asmsh_cmd_args_t*); /** Represent a single command */ struct asmsh_cmd_s { /** The command (with the leading '.') */ const char *str; /** The command function pointer */ asmsh_cmd_f *cmd; /**If non-zero indicate the minimum of chars to * match. ".quit" while have 2 to match starting * from ".q" */ unsigned char sm; /* Help information */ /** Small command name (like .h(elp) )*/ const char *sms; /** Command usage */ const char *usage; /** Command description */ const char *desc; }; /** A command arguments with command name & NULL terminated array of args */ struct asmsh_cmd_args_s { char cmd[ASMSH_CMD_MAXLEN+1]; int argc; char *args[ASMSH_CMD_MAXARG+1]; }; /**@param const char* the text to match * @param int the len of the text to match * @return a ptr or NULL */ const asmsh_cmd_t *asmsh_cmd_match(const char *cmd, int stop); /** @return NULL on error or ptr on new cmd args */ asmsh_cmd_args_t *asmsh_cmd_parse(const char *cmd); void asmsh_cmd_args_free(asmsh_cmd_args_t *args); const char *asmsh_cmd_help(asmsh_t *sh); /* * Commands declaration * * A command can return 0 if no errors, -X for a recoverable error * 1 for exit with status 0, 1+S for exit with status S */ // Quit the shell static int _quit(asmsh_t *sh, asmsh_cmd_args_t *args) { asmsh_cleanup(sh); return 1; } // Print an instruction bytecode int asmsh_cmd_bcode(asmsh_t *sh, char *buf, int bufsz, int argc, char **args); static int _bcode(asmsh_t *sh, asmsh_cmd_args_t *args) { char str[256]; int ret; ret = asmsh_cmd_bcode(sh, str, 256, args->argc, args->args); if(ret) { return ret; } printf("%s\n", str); return 0; } // Print the registers static int _print_regs(asmsh_t *sh, asmsh_cmd_args_t *args) { asmsh_env_t *env = sh->env; asmsh_env_update_regs(env); struct user_regs_struct *r = &env->regs; #define FLG(b, l) ( (r->eflags & (1<rax, r->rbx, r->rcx, r->rdx,\ r->rbp, r->rsi, r->rdi, r->rsp,\ r->r8, r->r9, r->r10, r->r11,\ r->r12, r->r13, r->r14, r->r15,\ r->rip, r->eflags,\ r->cs, r->ds, r->es, r->fs, r->gs, r->ss, FLG(11,'O'), FLG(10, 'D'), FLG(7, 'S'), FLG(6, 'Z'), FLG(4, 'A'), FLG(2, 'P'), FLG(0, 'C')); #undef FLG return 0; } // Reset the shell (restart the child etc) static int _reset(asmsh_t *sh, asmsh_cmd_args_t *args) { char *childpath = strdup(sh->child_path); asmsh_cleanup(sh); asmsh_init(sh, childpath); free(childpath); return 0; } // Display CPU flag resister values static int _flags(asmsh_t *sh, asmsh_cmd_args_t *args) { printf("Flags :\n"); #define printFLG(name, b) printf(" (%2d)%16s : %d\n", b, name,\ (sh->env->regs.eflags & (1<env->regs.eflags & (1<str; cmd++) { snprintf(abuf, 64, "%s %s", cmd->sms, cmd->usage); ret += snprintf(buf+ret, 4096-ret, " %-18s : %s\n", abuf, cmd->desc); } dprintf(2, buf); return 0; } #endif