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 3.9KB

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