tui-disasm.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. /* Disassembly display.
  2. Copyright (C) 1998-2022 Free Software Foundation, Inc.
  3. Contributed by Hewlett-Packard 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 "symtab.h"
  18. #include "breakpoint.h"
  19. #include "frame.h"
  20. #include "value.h"
  21. #include "source.h"
  22. #include "disasm.h"
  23. #include "tui/tui.h"
  24. #include "tui/tui-command.h"
  25. #include "tui/tui-data.h"
  26. #include "tui/tui-win.h"
  27. #include "tui/tui-layout.h"
  28. #include "tui/tui-winsource.h"
  29. #include "tui/tui-stack.h"
  30. #include "tui/tui-file.h"
  31. #include "tui/tui-disasm.h"
  32. #include "tui/tui-source.h"
  33. #include "progspace.h"
  34. #include "objfiles.h"
  35. #include "cli/cli-style.h"
  36. #include "tui/tui-location.h"
  37. #include "gdb_curses.h"
  38. struct tui_asm_line
  39. {
  40. CORE_ADDR addr;
  41. std::string addr_string;
  42. size_t addr_size;
  43. std::string insn;
  44. };
  45. /* Helper function to find the number of characters in STR, skipping
  46. any ANSI escape sequences. */
  47. static size_t
  48. len_without_escapes (const std::string &str)
  49. {
  50. size_t len = 0;
  51. const char *ptr = str.c_str ();
  52. char c;
  53. while ((c = *ptr) != '\0')
  54. {
  55. if (c == '\033')
  56. {
  57. ui_file_style style;
  58. size_t n_read;
  59. if (style.parse (ptr, &n_read))
  60. ptr += n_read;
  61. else
  62. {
  63. /* Shouldn't happen, but just skip the ESC if it somehow
  64. does. */
  65. ++ptr;
  66. }
  67. }
  68. else
  69. {
  70. ++len;
  71. ++ptr;
  72. }
  73. }
  74. return len;
  75. }
  76. /* Function to disassemble up to COUNT instructions starting from address
  77. PC into the ASM_LINES vector (which will be emptied of any previous
  78. contents). Return the address of the COUNT'th instruction after pc.
  79. When ADDR_SIZE is non-null then place the maximum size of an address and
  80. label into the value pointed to by ADDR_SIZE, and set the addr_size
  81. field on each item in ASM_LINES, otherwise the addr_size fields within
  82. ASM_LINES are undefined.
  83. It is worth noting that ASM_LINES might not have COUNT entries when this
  84. function returns. If the disassembly is truncated for some other
  85. reason, for example, we hit invalid memory, then ASM_LINES can have
  86. fewer entries than requested. */
  87. static CORE_ADDR
  88. tui_disassemble (struct gdbarch *gdbarch,
  89. std::vector<tui_asm_line> &asm_lines,
  90. CORE_ADDR pc, int count,
  91. size_t *addr_size = nullptr)
  92. {
  93. bool term_out = source_styling && gdb_stdout->can_emit_style_escape ();
  94. string_file gdb_dis_out (term_out);
  95. /* Must start with an empty list. */
  96. asm_lines.clear ();
  97. /* Now construct each line. */
  98. for (int i = 0; i < count; ++i)
  99. {
  100. tui_asm_line tal;
  101. CORE_ADDR orig_pc = pc;
  102. try
  103. {
  104. pc = pc + gdb_print_insn (gdbarch, pc, &gdb_dis_out, NULL);
  105. }
  106. catch (const gdb_exception_error &except)
  107. {
  108. /* If PC points to an invalid address then we'll catch a
  109. MEMORY_ERROR here, this should stop the disassembly, but
  110. otherwise is fine. */
  111. if (except.error != MEMORY_ERROR)
  112. throw;
  113. return pc;
  114. }
  115. /* Capture the disassembled instruction. */
  116. tal.insn = gdb_dis_out.release ();
  117. /* And capture the address the instruction is at. */
  118. tal.addr = orig_pc;
  119. print_address (gdbarch, orig_pc, &gdb_dis_out);
  120. tal.addr_string = gdb_dis_out.release ();
  121. if (addr_size != nullptr)
  122. {
  123. size_t new_size;
  124. if (term_out)
  125. new_size = len_without_escapes (tal.addr_string);
  126. else
  127. new_size = tal.addr_string.size ();
  128. *addr_size = std::max (*addr_size, new_size);
  129. tal.addr_size = new_size;
  130. }
  131. asm_lines.push_back (std::move (tal));
  132. }
  133. return pc;
  134. }
  135. /* Look backward from ADDR for an address from which we can start
  136. disassembling, this needs to be something we can be reasonably
  137. confident will fall on an instruction boundary. We use msymbol
  138. addresses, or the start of a section. */
  139. static CORE_ADDR
  140. tui_find_backward_disassembly_start_address (CORE_ADDR addr)
  141. {
  142. struct bound_minimal_symbol msym, msym_prev;
  143. msym = lookup_minimal_symbol_by_pc_section (addr - 1, nullptr,
  144. lookup_msym_prefer::TEXT,
  145. &msym_prev);
  146. if (msym.minsym != nullptr)
  147. return BMSYMBOL_VALUE_ADDRESS (msym);
  148. else if (msym_prev.minsym != nullptr)
  149. return BMSYMBOL_VALUE_ADDRESS (msym_prev);
  150. /* Find the section that ADDR is in, and look for the start of the
  151. section. */
  152. struct obj_section *section = find_pc_section (addr);
  153. if (section != NULL)
  154. return section->addr ();
  155. return addr;
  156. }
  157. /* Find the disassembly address that corresponds to FROM lines above
  158. or below the PC. Variable sized instructions are taken into
  159. account by the algorithm. */
  160. static CORE_ADDR
  161. tui_find_disassembly_address (struct gdbarch *gdbarch, CORE_ADDR pc, int from)
  162. {
  163. CORE_ADDR new_low;
  164. int max_lines;
  165. max_lines = (from > 0) ? from : - from;
  166. if (max_lines == 0)
  167. return pc;
  168. std::vector<tui_asm_line> asm_lines;
  169. new_low = pc;
  170. if (from > 0)
  171. {
  172. /* Always disassemble 1 extra instruction here, then if the last
  173. instruction fails to disassemble we will take the address of the
  174. previous instruction that did disassemble as the result. */
  175. tui_disassemble (gdbarch, asm_lines, pc, max_lines + 1);
  176. new_low = asm_lines.back ().addr;
  177. }
  178. else
  179. {
  180. /* In order to disassemble backwards we need to find a suitable
  181. address to start disassembling from and then work forward until we
  182. re-find the address we're currently at. We can then figure out
  183. which address will be at the top of the TUI window after our
  184. backward scroll. During our backward disassemble we need to be
  185. able to distinguish between the case where the last address we
  186. _can_ disassemble is ADDR, and the case where the disassembly
  187. just happens to stop at ADDR, for this reason we increase
  188. MAX_LINES by one. */
  189. max_lines++;
  190. /* When we disassemble a series of instructions this will hold the
  191. address of the last instruction disassembled. */
  192. CORE_ADDR last_addr;
  193. /* And this will hold the address of the next instruction that would
  194. have been disassembled. */
  195. CORE_ADDR next_addr;
  196. /* As we search backward if we find an address that looks like a
  197. promising starting point then we record it in this structure. If
  198. the next address we try is not a suitable starting point then we
  199. will fall back to the address held here. */
  200. gdb::optional<CORE_ADDR> possible_new_low;
  201. /* The previous value of NEW_LOW so we know if the new value is
  202. different or not. */
  203. CORE_ADDR prev_low;
  204. do
  205. {
  206. /* Find an address from which we can start disassembling. */
  207. prev_low = new_low;
  208. new_low = tui_find_backward_disassembly_start_address (new_low);
  209. /* Disassemble forward. */
  210. next_addr = tui_disassemble (gdbarch, asm_lines, new_low, max_lines);
  211. last_addr = asm_lines.back ().addr;
  212. /* If disassembling from the current value of NEW_LOW reached PC
  213. (or went past it) then this would do as a starting point if we
  214. can't find anything better, so remember it. */
  215. if (last_addr >= pc && new_low != prev_low
  216. && asm_lines.size () >= max_lines)
  217. possible_new_low.emplace (new_low);
  218. /* Continue searching until we find a value of NEW_LOW from which
  219. disassembling MAX_LINES instructions doesn't reach PC. We
  220. know this means we can find the required number of previous
  221. instructions then. */
  222. }
  223. while ((last_addr > pc
  224. || (last_addr == pc && asm_lines.size () < max_lines))
  225. && new_low != prev_low);
  226. /* If we failed to disassemble the required number of lines then the
  227. following walk forward is not going to work, it assumes that
  228. ASM_LINES contains exactly MAX_LINES entries. Instead we should
  229. consider falling back to a previous possible start address in
  230. POSSIBLE_NEW_LOW. */
  231. if (asm_lines.size () < max_lines)
  232. {
  233. if (!possible_new_low.has_value ())
  234. return new_low;
  235. /* Take the best possible match we have. */
  236. new_low = *possible_new_low;
  237. next_addr = tui_disassemble (gdbarch, asm_lines, new_low, max_lines);
  238. last_addr = asm_lines.back ().addr;
  239. gdb_assert (asm_lines.size () >= max_lines);
  240. }
  241. /* Scan forward disassembling one instruction at a time until
  242. the last visible instruction of the window matches the pc.
  243. We keep the disassembled instructions in the 'lines' window
  244. and shift it downward (increasing its addresses). */
  245. int pos = max_lines - 1;
  246. if (last_addr < pc)
  247. do
  248. {
  249. pos++;
  250. if (pos >= max_lines)
  251. pos = 0;
  252. CORE_ADDR old_next_addr = next_addr;
  253. std::vector<tui_asm_line> single_asm_line;
  254. next_addr = tui_disassemble (gdbarch, single_asm_line,
  255. next_addr, 1);
  256. /* If there are some problems while disassembling exit. */
  257. if (next_addr <= old_next_addr)
  258. return pc;
  259. gdb_assert (single_asm_line.size () == 1);
  260. asm_lines[pos] = single_asm_line[0];
  261. } while (next_addr <= pc);
  262. pos++;
  263. if (pos >= max_lines)
  264. pos = 0;
  265. new_low = asm_lines[pos].addr;
  266. /* When scrolling backward the addresses should move backward, or at
  267. the very least stay the same if we are at the first address that
  268. can be disassembled. */
  269. gdb_assert (new_low <= pc);
  270. }
  271. return new_low;
  272. }
  273. /* Function to set the disassembly window's content. */
  274. bool
  275. tui_disasm_window::set_contents (struct gdbarch *arch,
  276. const struct symtab_and_line &sal)
  277. {
  278. int i;
  279. int max_lines;
  280. CORE_ADDR cur_pc;
  281. int tab_len = tui_tab_width;
  282. int insn_pos;
  283. CORE_ADDR pc = sal.pc;
  284. if (pc == 0)
  285. return false;
  286. m_gdbarch = arch;
  287. m_start_line_or_addr.loa = LOA_ADDRESS;
  288. m_start_line_or_addr.u.addr = pc;
  289. cur_pc = tui_location.addr ();
  290. /* Window size, excluding highlight box. */
  291. max_lines = height - 2;
  292. /* Get temporary table that will hold all strings (addr & insn). */
  293. std::vector<tui_asm_line> asm_lines;
  294. size_t addr_size = 0;
  295. tui_disassemble (m_gdbarch, asm_lines, pc, max_lines, &addr_size);
  296. /* Align instructions to the same column. */
  297. insn_pos = (1 + (addr_size / tab_len)) * tab_len;
  298. /* Now construct each line. */
  299. m_content.resize (max_lines);
  300. m_max_length = -1;
  301. for (i = 0; i < max_lines; i++)
  302. {
  303. tui_source_element *src = &m_content[i];
  304. std::string line;
  305. CORE_ADDR addr;
  306. if (i < asm_lines.size ())
  307. {
  308. line
  309. = (asm_lines[i].addr_string
  310. + n_spaces (insn_pos - asm_lines[i].addr_size)
  311. + asm_lines[i].insn);
  312. addr = asm_lines[i].addr;
  313. }
  314. else
  315. {
  316. line = "";
  317. addr = 0;
  318. }
  319. const char *ptr = line.c_str ();
  320. int line_len;
  321. src->line = tui_copy_source_line (&ptr, &line_len);
  322. m_max_length = std::max (m_max_length, line_len);
  323. src->line_or_addr.loa = LOA_ADDRESS;
  324. src->line_or_addr.u.addr = addr;
  325. src->is_exec_point = (addr == cur_pc && line.size () > 0);
  326. }
  327. return true;
  328. }
  329. void
  330. tui_get_begin_asm_address (struct gdbarch **gdbarch_p, CORE_ADDR *addr_p)
  331. {
  332. struct gdbarch *gdbarch = get_current_arch ();
  333. CORE_ADDR addr = 0;
  334. if (tui_location.addr () == 0)
  335. {
  336. if (have_full_symbols () || have_partial_symbols ())
  337. {
  338. set_default_source_symtab_and_line ();
  339. struct symtab_and_line sal = get_current_source_symtab_and_line ();
  340. if (sal.symtab != nullptr)
  341. find_line_pc (sal.symtab, sal.line, &addr);
  342. }
  343. if (addr == 0)
  344. {
  345. struct bound_minimal_symbol main_symbol
  346. = lookup_minimal_symbol (main_name (), nullptr, nullptr);
  347. if (main_symbol.minsym != nullptr)
  348. addr = BMSYMBOL_VALUE_ADDRESS (main_symbol);
  349. }
  350. }
  351. else /* The target is executing. */
  352. {
  353. gdbarch = tui_location.gdbarch ();
  354. addr = tui_location.addr ();
  355. }
  356. *gdbarch_p = gdbarch;
  357. *addr_p = addr;
  358. }
  359. /* Determine what the low address will be to display in the TUI's
  360. disassembly window. This may or may not be the same as the low
  361. address input. */
  362. CORE_ADDR
  363. tui_get_low_disassembly_address (struct gdbarch *gdbarch,
  364. CORE_ADDR low, CORE_ADDR pc)
  365. {
  366. int pos;
  367. /* Determine where to start the disassembly so that the pc is about
  368. in the middle of the viewport. */
  369. if (TUI_DISASM_WIN != NULL)
  370. pos = TUI_DISASM_WIN->height;
  371. else if (TUI_CMD_WIN == NULL)
  372. pos = tui_term_height () / 2 - 2;
  373. else
  374. pos = tui_term_height () - TUI_CMD_WIN->height - 2;
  375. pos = (pos - 2) / 2;
  376. pc = tui_find_disassembly_address (gdbarch, pc, -pos);
  377. if (pc < low)
  378. pc = low;
  379. return pc;
  380. }
  381. /* Scroll the disassembly forward or backward vertically. */
  382. void
  383. tui_disasm_window::do_scroll_vertical (int num_to_scroll)
  384. {
  385. if (!m_content.empty ())
  386. {
  387. CORE_ADDR pc;
  388. pc = m_start_line_or_addr.u.addr;
  389. symtab_and_line sal {};
  390. sal.pspace = current_program_space;
  391. sal.pc = tui_find_disassembly_address (m_gdbarch, pc, num_to_scroll);
  392. update_source_window_as_is (m_gdbarch, sal);
  393. }
  394. }
  395. bool
  396. tui_disasm_window::location_matches_p (struct bp_location *loc, int line_no)
  397. {
  398. return (m_content[line_no].line_or_addr.loa == LOA_ADDRESS
  399. && m_content[line_no].line_or_addr.u.addr == loc->address);
  400. }
  401. bool
  402. tui_disasm_window::addr_is_displayed (CORE_ADDR addr) const
  403. {
  404. if (m_content.size () < SCROLL_THRESHOLD)
  405. return false;
  406. for (size_t i = 0; i < m_content.size () - SCROLL_THRESHOLD; ++i)
  407. {
  408. if (m_content[i].line_or_addr.loa == LOA_ADDRESS
  409. && m_content[i].line_or_addr.u.addr == addr)
  410. return true;
  411. }
  412. return false;
  413. }
  414. void
  415. tui_disasm_window::maybe_update (struct frame_info *fi, symtab_and_line sal)
  416. {
  417. CORE_ADDR low;
  418. struct gdbarch *frame_arch = get_frame_arch (fi);
  419. if (find_pc_partial_function (sal.pc, NULL, &low, NULL) == 0)
  420. {
  421. /* There is no symbol available for current PC. There is no
  422. safe way how to "disassemble backwards". */
  423. low = sal.pc;
  424. }
  425. else
  426. low = tui_get_low_disassembly_address (frame_arch, low, sal.pc);
  427. struct tui_line_or_address a;
  428. a.loa = LOA_ADDRESS;
  429. a.u.addr = low;
  430. if (!addr_is_displayed (sal.pc))
  431. {
  432. sal.pc = low;
  433. update_source_window (frame_arch, sal);
  434. }
  435. else
  436. {
  437. a.u.addr = sal.pc;
  438. set_is_exec_point_at (a);
  439. }
  440. }
  441. void
  442. tui_disasm_window::display_start_addr (struct gdbarch **gdbarch_p,
  443. CORE_ADDR *addr_p)
  444. {
  445. *gdbarch_p = m_gdbarch;
  446. *addr_p = m_start_line_or_addr.u.addr;
  447. }