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_cmds.h 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. #ifndef ASMSH_SHELL_CMDS_H
  14. #define ASMSH_SHELL_CMDS_H
  15. #include "config.h"
  16. #include <errno.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <unistd.h>
  20. #include "logger.h"
  21. #include "syscalls.h"
  22. #include "asm_env.h"
  23. #include "compile.h"
  24. #define ASMSH_CMD_MAXLEN 31
  25. #define ASMSH_CMD_MAXARG 15
  26. typedef struct asmsh_cmd_s asmsh_cmd_t;
  27. typedef struct asmsh_cmd_args_s asmsh_cmd_args_t;
  28. #include "shell.h"
  29. #include "asm_env.h"
  30. #include "shell_cmds_lib.h"
  31. #include "shell_sym.h"
  32. /** @return <0 on recoverable error 0 on ok, 1+status on exit */
  33. typedef int (asmsh_cmd_f)(asmsh_t*, asmsh_cmd_args_t*);
  34. /** Represent a single command */
  35. struct asmsh_cmd_s
  36. {
  37. /** The command (with the leading '.') */
  38. const char *str;
  39. /** The command function pointer */
  40. asmsh_cmd_f *cmd;
  41. /**If non-zero indicate the minimum of chars to
  42. * match. ".quit" while have 2 to match starting
  43. * from ".q"
  44. */
  45. unsigned char sm;
  46. /* Help information */
  47. /** Small command name (like .h(elp) )*/
  48. const char *sms;
  49. /** Command usage */
  50. const char *usage;
  51. /** Command description */
  52. const char *desc;
  53. };
  54. /** A command arguments with command name & NULL terminated array of args */
  55. struct asmsh_cmd_args_s
  56. {
  57. char cmd[ASMSH_CMD_MAXLEN+1];
  58. int argc;
  59. char *args[ASMSH_CMD_MAXARG+1];
  60. };
  61. /**@param const char* the text to match
  62. * @param int the len of the text to match
  63. * @return a ptr or NULL
  64. */
  65. const asmsh_cmd_t *asmsh_cmd_match(const char *cmd, int stop);
  66. /** @return NULL on error or ptr on new cmd args */
  67. asmsh_cmd_args_t *asmsh_cmd_parse(const char *cmd);
  68. void asmsh_cmd_args_free(asmsh_cmd_args_t *args);
  69. const char *asmsh_cmd_help(asmsh_t *sh);
  70. /*
  71. * Commands declaration
  72. *
  73. * A command can return 0 if no errors, -X for a recoverable error
  74. * 1 for exit with status 0, 1+S for exit with status S
  75. */
  76. // Quit the shell
  77. int asmsh_cmd_quit(asmsh_t *sh, asmsh_cmd_args_t *args);
  78. // Defined in @ref shell_cmd_breakpoint.c
  79. int asmsh_cmd_breakpoint(asmsh_t *sh, asmsh_cmd_args_t *args);
  80. // Print an instruction bytecode
  81. int asmsh_cmd_bcode_(asmsh_t *sh, char *buf, int bufsz, int argc, char **args);
  82. int asmsh_cmd_bcode(asmsh_t *sh, asmsh_cmd_args_t *args);
  83. // Defined in @ref shell_cmd_label.c
  84. int asmsh_cmd_label(asmsh_t* sh, asmsh_cmd_args_t *args);
  85. int asmsh_cmd_maps(asmsh_t *sh, asmsh_cmd_args_t *args);
  86. int asmsh_cmd_print_regs(asmsh_t *sh, asmsh_cmd_args_t *args);
  87. int asmsh_cmd_step(asmsh_t *sh, asmsh_cmd_args_t *args);
  88. int asmsh_cmd_syscalls(asmsh_t *sh, asmsh_cmd_args_t *args);
  89. int asmsh_cmd_run(asmsh_t *sh, asmsh_cmd_args_t *args);
  90. // Reset the shell (restart the child etc)a
  91. int asmsh_cmd_reset(asmsh_t *sh, asmsh_cmd_args_t *args);
  92. // Display CPU flag resister values
  93. int asmsh_cmd_flags(asmsh_t *sh, asmsh_cmd_args_t *args);
  94. int asmsh_cmd_help_(asmsh_t *sh, asmsh_cmd_args_t *args);
  95. /*
  96. * The list of shell commands
  97. */
  98. static const asmsh_cmd_t asmsh_CMDS[] = {
  99. {".breakpoint", asmsh_cmd_breakpoint, 3,
  100. ".br(eakpoint)", "[add|del|list] [addr]",
  101. "Set a breakpoint"},
  102. {".bytecode", asmsh_cmd_bcode, 2,
  103. ".b(ytecode)", "",
  104. "display last instruction bytecode"},
  105. {".flags", asmsh_cmd_flags, 2,
  106. ".f(lags)", "",
  107. "display CPU flags"},
  108. {".help", asmsh_cmd_help_, 2,
  109. ".h(elp)","[cmd]",
  110. "display this help or the help of specified command"},
  111. {".label", asmsh_cmd_label, 2,
  112. ".l(abel)", "label_name [addr]",
  113. "Set a label at given address (or . if none given)"},
  114. {".maps", asmsh_cmd_maps, 2,
  115. ".m(aps)", "",
  116. "display memory maps"},
  117. {".quit", asmsh_cmd_quit, 2,
  118. ".q(uit)","",
  119. "quit asmsh"},
  120. {".regs", asmsh_cmd_print_regs, 1,
  121. ".(regs)", "",
  122. "display registers value"},
  123. {".run", asmsh_cmd_run, 3,
  124. ".ru(n)", "",
  125. "Run until a breakpoint is reached"},
  126. {".step", asmsh_cmd_step, 2,
  127. ".s(tep)", "",
  128. "Run the instruction pointed by RIP"},
  129. {".syscalls", asmsh_cmd_syscalls, 4,
  130. ".sys(calls)", "",
  131. "print syscalls name & numbers"},
  132. {".reset", asmsh_cmd_reset, 0,
  133. ".reset", "",
  134. "reset the shell"},
  135. {NULL, NULL, 0},
  136. };
  137. #endif