asmsh/shell_cmds.h

157 lines
4.1 KiB
C

/* 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"
#include "syscalls.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"
#include "asm_env.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
int asmsh_cmd_quit(asmsh_t *sh, asmsh_cmd_args_t *args);
// Defined in @file shell_cmd_breakpoint.c
int asmsh_cmd_breakpoint(asmsh_t *sh, asmsh_cmd_args_t *args);
// Print an instruction bytecode
int asmsh_cmd_bcode_(asmsh_t *sh, char *buf, int bufsz, int argc, char **args);
int asmsh_cmd_bcode(asmsh_t *sh, asmsh_cmd_args_t *args);
int asmsh_cmd_maps(asmsh_t *sh, asmsh_cmd_args_t *args);
int asmsh_cmd_print_regs(asmsh_t *sh, asmsh_cmd_args_t *args);
int asmsh_cmd_step(asmsh_t *sh, asmsh_cmd_args_t *args);
int asmsh_cmd_syscalls(asmsh_t *sh, asmsh_cmd_args_t *args);
// Reset the shell (restart the child etc)a
int asmsh_cmd_reset(asmsh_t *sh, asmsh_cmd_args_t *args);
// Display CPU flag resister values
int asmsh_cmd_flags(asmsh_t *sh, asmsh_cmd_args_t *args);
int asmsh_cmd_help_(asmsh_t *sh, asmsh_cmd_args_t *args);
/*
* The list of shell commands
*/
static const asmsh_cmd_t asmsh_CMDS[] = {
{".breakpoint", asmsh_cmd_breakpoint, 3,
".br(eakpoint)", "[addr]",
"Set a breakpoint"},
{".bytecode", asmsh_cmd_bcode, 2,
".b(ytecode)", "",
"display last instruction bytecode"},
{".flags", asmsh_cmd_flags, 2,
".f(lags)", "",
"display CPU flags"},
{".help", asmsh_cmd_help_, 2,
".h(elp)","[cmd]",
"display this help or the help of specified command"},
{".maps", asmsh_cmd_maps, 2,
".m(aps)", "",
"display memory maps"},
{".quit", asmsh_cmd_quit, 2,
".q(uit)","",
"quit asmsh"},
{".regs", asmsh_cmd_print_regs, 1,
".(regs)", "",
"display registers value"},
{".step", asmsh_cmd_step, 2,
".s(tep)", "",
"Run the instruction pointed by RIP"},
{".syscalls", asmsh_cmd_syscalls, 4,
".sys(calls)", "",
"print syscalls name & numbers"},
{".reset", asmsh_cmd_reset, 0,
".reset", "",
"reset the shell"},
{NULL, NULL, 0},
};
#endif