mi-cmd-disas.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /* MI Command Set - disassemble commands.
  2. Copyright (C) 2000-2022 Free Software Foundation, Inc.
  3. Contributed by Cygnus Solutions (a Red Hat company).
  4. This file is part of GDB.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. #include "defs.h"
  16. #include "arch-utils.h"
  17. #include "target.h"
  18. #include "value.h"
  19. #include "mi-cmds.h"
  20. #include "mi-getopt.h"
  21. #include "ui-out.h"
  22. #include "disasm.h"
  23. /* The arguments to be passed on the command line and parsed here are
  24. either:
  25. START-ADDRESS: address to start the disassembly at.
  26. END-ADDRESS: address to end the disassembly at.
  27. or:
  28. FILENAME: The name of the file where we want disassemble from.
  29. LINE: The line around which we want to disassemble. It will
  30. disassemble the function that contains that line.
  31. HOW_MANY: Number of disassembly lines to display. With source, it
  32. is the number of disassembly lines only, not counting the source
  33. lines.
  34. always required:
  35. MODE: 0 -- disassembly.
  36. 1 -- disassembly and source (with deprecated source-centric view).
  37. 2 -- disassembly and opcodes.
  38. 3 -- disassembly, source-centric and opcodes.
  39. 4 -- disassembly, and source (with pc-centric view).
  40. 5 -- disassembly, source (pc-centric) and opcodes. */
  41. void
  42. mi_cmd_disassemble (const char *command, char **argv, int argc)
  43. {
  44. struct gdbarch *gdbarch = get_current_arch ();
  45. struct ui_out *uiout = current_uiout;
  46. CORE_ADDR start;
  47. int mode;
  48. gdb_disassembly_flags disasm_flags;
  49. struct symtab *s;
  50. /* Which options have we processed ... */
  51. int file_seen = 0;
  52. int line_seen = 0;
  53. int num_seen = 0;
  54. int start_seen = 0;
  55. int end_seen = 0;
  56. int addr_seen = 0;
  57. /* ... and their corresponding value. */
  58. char *file_string = NULL;
  59. int line_num = -1;
  60. int how_many = -1;
  61. CORE_ADDR low = 0;
  62. CORE_ADDR high = 0;
  63. CORE_ADDR addr = 0;
  64. /* Options processing stuff. */
  65. int oind = 0;
  66. char *oarg;
  67. enum opt
  68. {
  69. FILE_OPT, LINE_OPT, NUM_OPT, START_OPT, END_OPT, ADDR_OPT
  70. };
  71. static const struct mi_opt opts[] =
  72. {
  73. {"f", FILE_OPT, 1},
  74. {"l", LINE_OPT, 1},
  75. {"n", NUM_OPT, 1},
  76. {"s", START_OPT, 1},
  77. {"e", END_OPT, 1},
  78. {"a", ADDR_OPT, 1},
  79. { 0, 0, 0 }
  80. };
  81. /* Get the options with their arguments. Keep track of what we
  82. encountered. */
  83. while (1)
  84. {
  85. int opt = mi_getopt ("-data-disassemble", argc, argv, opts,
  86. &oind, &oarg);
  87. if (opt < 0)
  88. break;
  89. switch ((enum opt) opt)
  90. {
  91. case FILE_OPT:
  92. file_string = oarg;
  93. file_seen = 1;
  94. break;
  95. case LINE_OPT:
  96. line_num = atoi (oarg);
  97. line_seen = 1;
  98. break;
  99. case NUM_OPT:
  100. how_many = atoi (oarg);
  101. num_seen = 1;
  102. break;
  103. case START_OPT:
  104. low = parse_and_eval_address (oarg);
  105. start_seen = 1;
  106. break;
  107. case END_OPT:
  108. high = parse_and_eval_address (oarg);
  109. end_seen = 1;
  110. break;
  111. case ADDR_OPT:
  112. addr = parse_and_eval_address (oarg);
  113. addr_seen = 1;
  114. break;
  115. }
  116. }
  117. argv += oind;
  118. argc -= oind;
  119. /* Allow only filename + linenum (with how_many which is not
  120. required) OR start_addr + end_addr OR addr. */
  121. if (!(
  122. ( line_seen && file_seen && !start_seen && !end_seen
  123. && !addr_seen)
  124. || (!line_seen && !file_seen && !num_seen && start_seen && end_seen
  125. && !addr_seen)
  126. || (!line_seen && !file_seen && !num_seen && !start_seen && !end_seen
  127. && addr_seen))
  128. || argc != 1)
  129. error (_("-data-disassemble: Usage: ( [-f filename -l linenum "
  130. "[-n howmany]] | [-s startaddr -e endaddr] | [-a addr] ) [--] mode."));
  131. mode = atoi (argv[0]);
  132. if (mode < 0 || mode > 5)
  133. error (_("-data-disassemble: Mode argument must be in the range 0-5."));
  134. /* Convert the mode into a set of disassembly flags. */
  135. disasm_flags = 0; /* Initialize here for -Wall. */
  136. switch (mode)
  137. {
  138. case 0:
  139. break;
  140. case 1:
  141. disasm_flags |= DISASSEMBLY_SOURCE_DEPRECATED;
  142. break;
  143. case 2:
  144. disasm_flags |= DISASSEMBLY_RAW_INSN;
  145. break;
  146. case 3:
  147. disasm_flags |= DISASSEMBLY_SOURCE_DEPRECATED | DISASSEMBLY_RAW_INSN;
  148. break;
  149. case 4:
  150. disasm_flags |= DISASSEMBLY_SOURCE;
  151. break;
  152. case 5:
  153. disasm_flags |= DISASSEMBLY_SOURCE | DISASSEMBLY_RAW_INSN;
  154. break;
  155. default:
  156. gdb_assert_not_reached ("bad disassembly mode");
  157. }
  158. /* We must get the function beginning and end where line_num is
  159. contained. */
  160. if (line_seen && file_seen)
  161. {
  162. s = lookup_symtab (file_string);
  163. if (s == NULL)
  164. error (_("-data-disassemble: Invalid filename."));
  165. if (!find_line_pc (s, line_num, &start))
  166. error (_("-data-disassemble: Invalid line number"));
  167. if (find_pc_partial_function (start, NULL, &low, &high) == 0)
  168. error (_("-data-disassemble: "
  169. "No function contains specified address"));
  170. }
  171. else if (addr_seen)
  172. {
  173. if (find_pc_partial_function (addr, NULL, &low, &high) == 0)
  174. error (_("-data-disassemble: "
  175. "No function contains specified address"));
  176. }
  177. gdb_disassembly (gdbarch, uiout,
  178. disasm_flags,
  179. how_many, low, high);
  180. }