123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- /* 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/>.
- */
- #ifndef ASMSH_SHELL_CMDS_H
- #define ASMSH_SHELL_CMDS_H
- #include "config.h"
-
- #include <errno.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
-
- #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*);
-
- struct asmsh_cmd_s
- {
- const char *str;
- 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;
-
- const char *sms;
- const char *usage;
- const char *desc;
- };
-
- struct asmsh_cmd_args_s
- {
- char cmd[ASMSH_CMD_MAXLEN+1];
- char *args[ASMSH_CMD_MAXARG+1];
- };
-
- static int _quit(asmsh_t *sh, asmsh_cmd_args_t *args)
- {
- asmsh_cleanup(sh);
- return 1;
- }
-
- 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<<b))?l:'-' )
-
- printf("rax: %016lx rbx: %016lx rcx: %016lx rdx: %016lx\n\
- rbp: %016lx rsi: %016lx rdi: %016lx rsp: %016lx\n\
- r8: %016lx r9: %016lx r10: %016lx r11: %016lx\n\
- r12: %016lx r13: %016lx r14: %016lx r15: %016lx\n\
- rip: %016lx flg: %016lx\n\
- cs: %04x ds: %04x es: %04x fs:%04x gs: %04x ss:%04x\n\
- flags: %c%c%c%c|%c%c%c\n\
- ODSZ|APC\n\
- ", r->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;
- }
-
- 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);
- }
-
- 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<<b))?1:0);
- printFLG("Overflow", 11);
- printFLG("Direction", 10);
- printFLG("Sign", 7);
- printFLG("Zero", 6);
- printFLG("Auxiliary carry", 4);
- printFLG("Parity", 2);
- printFLG("Carry", 0);
- #undef printFLG
- #define EFLG(b,n) ((sh->env->regs.eflags & (1<<b))?n:'-')
- printf("%c%c%c%c %c%c%c\n",
- EFLG(11,'O'), EFLG(10, 'D'), EFLG(7,'S'), EFLG(6,'Z'),
- EFLG(4,'A'), EFLG(2, 'P'), EFLG(0, 'C'));
- return 0;
- }
-
- static int _help(asmsh_t *sh, asmsh_cmd_args_t *args);
-
- static const asmsh_cmd_t asmsh_CMDS[] = {
- {".flags", _flags, 2,
- ".f(lags)", "",
- "display CPU flags"},
- {".help", _help, 2,
- ".h(elp)","[cmd]",
- "display this help or the help of specified command"},
- {".quit", _quit, 2,
- ".q(uit)","",
- "quit asmsh"},
- {".regs", _print_regs, 1,
- ".(regs)", "",
- "display registers value"},
- {".reset", _reset, 0,
- ".reset", "",
- "reset the shell"},
- {NULL, NULL, 0},
- };
-
- static int _help(asmsh_t *sh, asmsh_cmd_args_t *args)
- {
- char buf[4096], abuf[64];
- int ret, pret;
-
- ret = snprintf(buf, 4096, "Available commands :\n");
- pret = ret;
- for(const asmsh_cmd_t *cmd = asmsh_CMDS; cmd->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;
- }
-
- /**@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);
- static void asmsh_cmd_args_free(asmsh_cmd_args_t *args)
- {
- if(!args) { return; }
- for(char **a=args->args; *a; a++)
- {
- free(*a);
- }
- free(args);
- }
-
-
- const char *asmsh_cmd_help(asmsh_t *sh);
-
- #endif
|