Fix TARGET_CHAR_BIT/HOST_CHAR_BIT confusion in gmp-utils.c
[binutils-gdb.git] / gdb / tui / tui-disasm.c
blob44a49041f811a8d88a12c09cfa3e1ef0e55727f7
1 /* Disassembly display.
3 Copyright (C) 1998-2020 Free Software Foundation, Inc.
5 Contributed by Hewlett-Packard Company.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include "defs.h"
23 #include "arch-utils.h"
24 #include "symtab.h"
25 #include "breakpoint.h"
26 #include "frame.h"
27 #include "value.h"
28 #include "source.h"
29 #include "disasm.h"
30 #include "tui/tui.h"
31 #include "tui/tui-command.h"
32 #include "tui/tui-data.h"
33 #include "tui/tui-win.h"
34 #include "tui/tui-layout.h"
35 #include "tui/tui-winsource.h"
36 #include "tui/tui-stack.h"
37 #include "tui/tui-file.h"
38 #include "tui/tui-disasm.h"
39 #include "tui/tui-source.h"
40 #include "progspace.h"
41 #include "objfiles.h"
42 #include "cli/cli-style.h"
44 #include "gdb_curses.h"
46 struct tui_asm_line
48 CORE_ADDR addr;
49 std::string addr_string;
50 size_t addr_size;
51 std::string insn;
54 /* Helper function to find the number of characters in STR, skipping
55 any ANSI escape sequences. */
56 static size_t
57 len_without_escapes (const std::string &str)
59 size_t len = 0;
60 const char *ptr = str.c_str ();
61 char c;
63 while ((c = *ptr++) != '\0')
65 if (c == '\033')
67 ui_file_style style;
68 size_t n_read;
69 if (style.parse (ptr, &n_read))
70 ptr += n_read;
71 else
73 /* Shouldn't happen, but just skip the ESC if it somehow
74 does. */
75 ++ptr;
78 else
79 ++len;
81 return len;
84 /* Function to disassemble up to COUNT instructions starting from address
85 PC into the ASM_LINES vector (which will be emptied of any previous
86 contents). Return the address of the COUNT'th instruction after pc.
87 When ADDR_SIZE is non-null then place the maximum size of an address and
88 label into the value pointed to by ADDR_SIZE, and set the addr_size
89 field on each item in ASM_LINES, otherwise the addr_size fields within
90 ASM_LINES are undefined.
92 It is worth noting that ASM_LINES might not have COUNT entries when this
93 function returns. If the disassembly is truncated for some other
94 reason, for example, we hit invalid memory, then ASM_LINES can have
95 fewer entries than requested. */
96 static CORE_ADDR
97 tui_disassemble (struct gdbarch *gdbarch,
98 std::vector<tui_asm_line> &asm_lines,
99 CORE_ADDR pc, int count,
100 size_t *addr_size = nullptr)
102 bool term_out = source_styling && gdb_stdout->can_emit_style_escape ();
103 string_file gdb_dis_out (term_out);
105 /* Must start with an empty list. */
106 asm_lines.clear ();
108 /* Now construct each line. */
109 for (int i = 0; i < count; ++i)
111 tui_asm_line tal;
112 CORE_ADDR orig_pc = pc;
116 pc = pc + gdb_print_insn (gdbarch, pc, &gdb_dis_out, NULL);
118 catch (const gdb_exception_error &except)
120 /* If PC points to an invalid address then we'll catch a
121 MEMORY_ERROR here, this should stop the disassembly, but
122 otherwise is fine. */
123 if (except.error != MEMORY_ERROR)
124 throw;
125 return pc;
128 /* Capture the disassembled instruction. */
129 tal.insn = std::move (gdb_dis_out.string ());
130 gdb_dis_out.clear ();
132 /* And capture the address the instruction is at. */
133 tal.addr = orig_pc;
134 print_address (gdbarch, orig_pc, &gdb_dis_out);
135 tal.addr_string = std::move (gdb_dis_out.string ());
136 gdb_dis_out.clear ();
138 if (addr_size != nullptr)
140 size_t new_size;
142 if (term_out)
143 new_size = len_without_escapes (tal.addr_string);
144 else
145 new_size = tal.addr_string.size ();
146 *addr_size = std::max (*addr_size, new_size);
147 tal.addr_size = new_size;
150 asm_lines.push_back (std::move (tal));
152 return pc;
155 /* Look backward from ADDR for an address from which we can start
156 disassembling, this needs to be something we can be reasonably
157 confident will fall on an instruction boundary. We use msymbol
158 addresses, or the start of a section. */
160 static CORE_ADDR
161 tui_find_backward_disassembly_start_address (CORE_ADDR addr)
163 struct bound_minimal_symbol msym, msym_prev;
165 msym = lookup_minimal_symbol_by_pc_section (addr - 1, nullptr,
166 lookup_msym_prefer::TEXT,
167 &msym_prev);
168 if (msym.minsym != nullptr)
169 return BMSYMBOL_VALUE_ADDRESS (msym);
170 else if (msym_prev.minsym != nullptr)
171 return BMSYMBOL_VALUE_ADDRESS (msym_prev);
173 /* Find the section that ADDR is in, and look for the start of the
174 section. */
175 struct obj_section *section = find_pc_section (addr);
176 if (section != NULL)
177 return obj_section_addr (section);
179 return addr;
182 /* Find the disassembly address that corresponds to FROM lines above
183 or below the PC. Variable sized instructions are taken into
184 account by the algorithm. */
185 static CORE_ADDR
186 tui_find_disassembly_address (struct gdbarch *gdbarch, CORE_ADDR pc, int from)
188 CORE_ADDR new_low;
189 int max_lines;
191 max_lines = (from > 0) ? from : - from;
192 if (max_lines == 0)
193 return pc;
195 std::vector<tui_asm_line> asm_lines;
197 new_low = pc;
198 if (from > 0)
200 /* Always disassemble 1 extra instruction here, then if the last
201 instruction fails to disassemble we will take the address of the
202 previous instruction that did disassemble as the result. */
203 tui_disassemble (gdbarch, asm_lines, pc, max_lines + 1);
204 new_low = asm_lines.back ().addr;
206 else
208 /* In order to disassemble backwards we need to find a suitable
209 address to start disassembling from and then work forward until we
210 re-find the address we're currently at. We can then figure out
211 which address will be at the top of the TUI window after our
212 backward scroll. During our backward disassemble we need to be
213 able to distinguish between the case where the last address we
214 _can_ disassemble is ADDR, and the case where the disassembly
215 just happens to stop at ADDR, for this reason we increase
216 MAX_LINES by one. */
217 max_lines++;
219 /* When we disassemble a series of instructions this will hold the
220 address of the last instruction disassembled. */
221 CORE_ADDR last_addr;
223 /* And this will hold the address of the next instruction that would
224 have been disassembled. */
225 CORE_ADDR next_addr;
227 /* As we search backward if we find an address that looks like a
228 promising starting point then we record it in this structure. If
229 the next address we try is not a suitable starting point then we
230 will fall back to the address held here. */
231 gdb::optional<CORE_ADDR> possible_new_low;
233 /* The previous value of NEW_LOW so we know if the new value is
234 different or not. */
235 CORE_ADDR prev_low;
239 /* Find an address from which we can start disassembling. */
240 prev_low = new_low;
241 new_low = tui_find_backward_disassembly_start_address (new_low);
243 /* Disassemble forward. */
244 next_addr = tui_disassemble (gdbarch, asm_lines, new_low, max_lines);
245 last_addr = asm_lines.back ().addr;
247 /* If disassembling from the current value of NEW_LOW reached PC
248 (or went past it) then this would do as a starting point if we
249 can't find anything better, so remember it. */
250 if (last_addr >= pc && new_low != prev_low
251 && asm_lines.size () >= max_lines)
252 possible_new_low.emplace (new_low);
254 /* Continue searching until we find a value of NEW_LOW from which
255 disassembling MAX_LINES instructions doesn't reach PC. We
256 know this means we can find the required number of previous
257 instructions then. */
259 while ((last_addr > pc
260 || (last_addr == pc && asm_lines.size () < max_lines))
261 && new_low != prev_low);
263 /* If we failed to disassemble the required number of lines then the
264 following walk forward is not going to work, it assumes that
265 ASM_LINES contains exactly MAX_LINES entries. Instead we should
266 consider falling back to a previous possible start address in
267 POSSIBLE_NEW_LOW. */
268 if (asm_lines.size () < max_lines)
270 if (!possible_new_low.has_value ())
271 return new_low;
273 /* Take the best possible match we have. */
274 new_low = *possible_new_low;
275 next_addr = tui_disassemble (gdbarch, asm_lines, new_low, max_lines);
276 last_addr = asm_lines.back ().addr;
277 gdb_assert (asm_lines.size () >= max_lines);
280 /* Scan forward disassembling one instruction at a time until
281 the last visible instruction of the window matches the pc.
282 We keep the disassembled instructions in the 'lines' window
283 and shift it downward (increasing its addresses). */
284 int pos = max_lines - 1;
285 if (last_addr < pc)
288 pos++;
289 if (pos >= max_lines)
290 pos = 0;
292 CORE_ADDR old_next_addr = next_addr;
293 std::vector<tui_asm_line> single_asm_line;
294 next_addr = tui_disassemble (gdbarch, single_asm_line,
295 next_addr, 1);
296 /* If there are some problems while disassembling exit. */
297 if (next_addr <= old_next_addr)
298 return pc;
299 gdb_assert (single_asm_line.size () == 1);
300 asm_lines[pos] = single_asm_line[0];
301 } while (next_addr <= pc);
302 pos++;
303 if (pos >= max_lines)
304 pos = 0;
305 new_low = asm_lines[pos].addr;
307 /* When scrolling backward the addresses should move backward, or at
308 the very least stay the same if we are at the first address that
309 can be disassembled. */
310 gdb_assert (new_low <= pc);
312 return new_low;
315 /* Function to set the disassembly window's content. */
316 bool
317 tui_disasm_window::set_contents (struct gdbarch *arch,
318 const struct symtab_and_line &sal)
320 int i;
321 int max_lines;
322 CORE_ADDR cur_pc;
323 struct tui_locator_window *locator = tui_locator_win_info_ptr ();
324 int tab_len = tui_tab_width;
325 int insn_pos;
327 CORE_ADDR pc = sal.pc;
328 if (pc == 0)
329 return false;
331 m_gdbarch = arch;
332 m_start_line_or_addr.loa = LOA_ADDRESS;
333 m_start_line_or_addr.u.addr = pc;
334 cur_pc = locator->addr;
336 /* Window size, excluding highlight box. */
337 max_lines = height - 2;
339 /* Get temporary table that will hold all strings (addr & insn). */
340 std::vector<tui_asm_line> asm_lines;
341 size_t addr_size = 0;
342 tui_disassemble (m_gdbarch, asm_lines, pc, max_lines, &addr_size);
344 /* Align instructions to the same column. */
345 insn_pos = (1 + (addr_size / tab_len)) * tab_len;
347 /* Now construct each line. */
348 m_content.resize (max_lines);
349 m_max_length = -1;
350 for (i = 0; i < max_lines; i++)
352 tui_source_element *src = &m_content[i];
354 std::string line;
355 CORE_ADDR addr;
357 if (i < asm_lines.size ())
359 line
360 = (asm_lines[i].addr_string
361 + n_spaces (insn_pos - asm_lines[i].addr_size)
362 + asm_lines[i].insn);
363 addr = asm_lines[i].addr;
365 else
367 line = "";
368 addr = 0;
371 const char *ptr = line.c_str ();
372 int line_len;
373 src->line = tui_copy_source_line (&ptr, &line_len);
374 m_max_length = std::max (m_max_length, line_len);
376 src->line_or_addr.loa = LOA_ADDRESS;
377 src->line_or_addr.u.addr = addr;
378 src->is_exec_point = (addr == cur_pc && line.size () > 0);
380 return true;
384 void
385 tui_get_begin_asm_address (struct gdbarch **gdbarch_p, CORE_ADDR *addr_p)
387 struct tui_locator_window *locator;
388 struct gdbarch *gdbarch = get_current_arch ();
389 CORE_ADDR addr = 0;
391 locator = tui_locator_win_info_ptr ();
393 if (locator->addr == 0)
395 if (have_full_symbols () || have_partial_symbols ())
397 set_default_source_symtab_and_line ();
398 struct symtab_and_line sal = get_current_source_symtab_and_line ();
400 if (sal.symtab != nullptr)
401 find_line_pc (sal.symtab, sal.line, &addr);
404 if (addr == 0)
406 struct bound_minimal_symbol main_symbol
407 = lookup_minimal_symbol (main_name (), nullptr, nullptr);
408 if (main_symbol.minsym != nullptr)
409 addr = BMSYMBOL_VALUE_ADDRESS (main_symbol);
412 else /* The target is executing. */
414 gdbarch = locator->gdbarch;
415 addr = locator->addr;
418 *gdbarch_p = gdbarch;
419 *addr_p = addr;
422 /* Determine what the low address will be to display in the TUI's
423 disassembly window. This may or may not be the same as the low
424 address input. */
425 CORE_ADDR
426 tui_get_low_disassembly_address (struct gdbarch *gdbarch,
427 CORE_ADDR low, CORE_ADDR pc)
429 int pos;
431 /* Determine where to start the disassembly so that the pc is about
432 in the middle of the viewport. */
433 if (TUI_DISASM_WIN != NULL)
434 pos = TUI_DISASM_WIN->height;
435 else if (TUI_CMD_WIN == NULL)
436 pos = tui_term_height () / 2 - 2;
437 else
438 pos = tui_term_height () - TUI_CMD_WIN->height - 2;
439 pos = (pos - 2) / 2;
441 pc = tui_find_disassembly_address (gdbarch, pc, -pos);
443 if (pc < low)
444 pc = low;
445 return pc;
448 /* Scroll the disassembly forward or backward vertically. */
449 void
450 tui_disasm_window::do_scroll_vertical (int num_to_scroll)
452 if (!m_content.empty ())
454 CORE_ADDR pc;
456 pc = m_start_line_or_addr.u.addr;
458 symtab_and_line sal {};
459 sal.pspace = current_program_space;
460 sal.pc = tui_find_disassembly_address (m_gdbarch, pc, num_to_scroll);
461 update_source_window_as_is (m_gdbarch, sal);
465 bool
466 tui_disasm_window::location_matches_p (struct bp_location *loc, int line_no)
468 return (m_content[line_no].line_or_addr.loa == LOA_ADDRESS
469 && m_content[line_no].line_or_addr.u.addr == loc->address);
472 bool
473 tui_disasm_window::addr_is_displayed (CORE_ADDR addr) const
475 if (m_content.size () < SCROLL_THRESHOLD)
476 return false;
478 for (size_t i = 0; i < m_content.size () - SCROLL_THRESHOLD; ++i)
480 if (m_content[i].line_or_addr.loa == LOA_ADDRESS
481 && m_content[i].line_or_addr.u.addr == addr)
482 return true;
485 return false;
488 void
489 tui_disasm_window::maybe_update (struct frame_info *fi, symtab_and_line sal)
491 CORE_ADDR low;
493 struct gdbarch *frame_arch = get_frame_arch (fi);
495 if (find_pc_partial_function (sal.pc, NULL, &low, NULL) == 0)
497 /* There is no symbol available for current PC. There is no
498 safe way how to "disassemble backwards". */
499 low = sal.pc;
501 else
502 low = tui_get_low_disassembly_address (frame_arch, low, sal.pc);
504 struct tui_line_or_address a;
506 a.loa = LOA_ADDRESS;
507 a.u.addr = low;
508 if (!addr_is_displayed (sal.pc))
510 sal.pc = low;
511 update_source_window (frame_arch, sal);
513 else
515 a.u.addr = sal.pc;
516 set_is_exec_point_at (a);
520 void
521 tui_disasm_window::display_start_addr (struct gdbarch **gdbarch_p,
522 CORE_ADDR *addr_p)
524 *gdbarch_p = m_gdbarch;
525 *addr_p = m_start_line_or_addr.u.addr;