More updated translations
[binutils-gdb.git] / gdb / tui / tui-disasm.c
blobcd828537a5a4824050157c6c1a765a1539874575
1 /* Disassembly display.
3 Copyright (C) 1998-2024 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 "arch-utils.h"
23 #include "symtab.h"
24 #include "breakpoint.h"
25 #include "frame.h"
26 #include "value.h"
27 #include "source.h"
28 #include "disasm.h"
29 #include "tui/tui.h"
30 #include "tui/tui-command.h"
31 #include "tui/tui-data.h"
32 #include "tui/tui-winsource.h"
33 #include "tui/tui-status.h"
34 #include "tui/tui-disasm.h"
35 #include "progspace.h"
36 #include "objfiles.h"
37 #include "cli/cli-style.h"
38 #include "tui/tui-location.h"
39 #include "gdbsupport/selftest.h"
40 #include "inferior.h"
42 struct tui_asm_line
44 CORE_ADDR addr;
45 std::string addr_string;
46 size_t addr_size;
47 std::string insn;
50 /* Helper function to find the number of characters in STR, skipping
51 any ANSI escape sequences. */
52 static size_t
53 len_without_escapes (const std::string &str)
55 size_t len = 0;
56 const char *ptr = str.c_str ();
57 char c;
59 while ((c = *ptr) != '\0')
61 if (c == '\033')
63 ui_file_style style;
64 size_t n_read;
65 if (style.parse (ptr, &n_read))
66 ptr += n_read;
67 else
69 /* Shouldn't happen, but just skip the ESC if it somehow
70 does. */
71 ++ptr;
74 else
76 ++len;
77 ++ptr;
80 return len;
83 /* Function to disassemble up to COUNT instructions starting from address
84 PC into the ASM_LINES vector (which will be emptied of any previous
85 contents). Return the address of the COUNT'th instruction after pc.
86 When ADDR_SIZE is non-null then place the maximum size of an address and
87 label into the value pointed to by ADDR_SIZE, and set the addr_size
88 field on each item in ASM_LINES, otherwise the addr_size fields within
89 ASM_LINES are undefined.
91 It is worth noting that ASM_LINES might not have COUNT entries when this
92 function returns. If the disassembly is truncated for some other
93 reason, for example, we hit invalid memory, then ASM_LINES can have
94 fewer entries than requested. */
95 static CORE_ADDR
96 tui_disassemble (struct gdbarch *gdbarch,
97 std::vector<tui_asm_line> &asm_lines,
98 CORE_ADDR pc, int count,
99 size_t *addr_size = nullptr)
101 bool term_out = source_styling && gdb_stdout->can_emit_style_escape ();
102 string_file gdb_dis_out (term_out);
104 /* Must start with an empty list. */
105 asm_lines.clear ();
107 /* Now construct each line. */
108 for (int i = 0; i < count; ++i)
110 tui_asm_line tal;
111 CORE_ADDR orig_pc = pc;
115 pc = pc + gdb_print_insn (gdbarch, pc, &gdb_dis_out, NULL);
117 catch (const gdb_exception_error &except)
119 /* If PC points to an invalid address then we'll catch a
120 MEMORY_ERROR here, this should stop the disassembly, but
121 otherwise is fine. */
122 if (except.error != MEMORY_ERROR)
123 throw;
124 return pc;
127 /* Capture the disassembled instruction. */
128 tal.insn = gdb_dis_out.release ();
130 /* And capture the address the instruction is at. */
131 tal.addr = orig_pc;
132 print_address (gdbarch, orig_pc, &gdb_dis_out);
133 tal.addr_string = gdb_dis_out.release ();
135 if (addr_size != nullptr)
137 size_t new_size;
139 if (term_out)
140 new_size = len_without_escapes (tal.addr_string);
141 else
142 new_size = tal.addr_string.size ();
143 *addr_size = std::max (*addr_size, new_size);
144 tal.addr_size = new_size;
147 asm_lines.push_back (std::move (tal));
149 return pc;
152 /* Look backward from ADDR for an address from which we can start
153 disassembling, this needs to be something we can be reasonably
154 confident will fall on an instruction boundary. We use msymbol
155 addresses, or the start of a section. */
157 static CORE_ADDR
158 tui_find_backward_disassembly_start_address (CORE_ADDR addr)
160 bound_minimal_symbol msym_prev;
161 bound_minimal_symbol msym
162 = lookup_minimal_symbol_by_pc_section (addr - 1, nullptr,
163 lookup_msym_prefer::TEXT,
164 &msym_prev);
165 if (msym.minsym != nullptr)
166 return msym.value_address ();
167 else if (msym_prev.minsym != nullptr)
168 return msym_prev.value_address ();
170 /* Find the section that ADDR is in, and look for the start of the
171 section. */
172 struct obj_section *section = find_pc_section (addr);
173 if (section != NULL)
174 return section->addr ();
176 return addr;
179 /* Find the disassembly address that corresponds to FROM lines above
180 or below the PC. Variable sized instructions are taken into
181 account by the algorithm. */
182 static CORE_ADDR
183 tui_find_disassembly_address (struct gdbarch *gdbarch, CORE_ADDR pc, int from)
185 CORE_ADDR new_low;
186 int max_lines;
188 max_lines = (from > 0) ? from : - from;
189 if (max_lines == 0)
190 return pc;
192 std::vector<tui_asm_line> asm_lines;
194 new_low = pc;
195 if (from > 0)
197 /* Always disassemble 1 extra instruction here, then if the last
198 instruction fails to disassemble we will take the address of the
199 previous instruction that did disassemble as the result. */
200 tui_disassemble (gdbarch, asm_lines, pc, max_lines + 1);
201 if (asm_lines.empty ())
202 return pc;
203 new_low = asm_lines.back ().addr;
205 else
207 /* In order to disassemble backwards we need to find a suitable
208 address to start disassembling from and then work forward until we
209 re-find the address we're currently at. We can then figure out
210 which address will be at the top of the TUI window after our
211 backward scroll. During our backward disassemble we need to be
212 able to distinguish between the case where the last address we
213 _can_ disassemble is ADDR, and the case where the disassembly
214 just happens to stop at ADDR, for this reason we increase
215 MAX_LINES by one. */
216 max_lines++;
218 /* When we disassemble a series of instructions this will hold the
219 address of the last instruction disassembled. */
220 CORE_ADDR last_addr;
222 /* And this will hold the address of the next instruction that would
223 have been disassembled. */
224 CORE_ADDR next_addr;
226 /* As we search backward if we find an address that looks like a
227 promising starting point then we record it in this structure. If
228 the next address we try is not a suitable starting point then we
229 will fall back to the address held here. */
230 std::optional<CORE_ADDR> possible_new_low;
232 /* The previous value of NEW_LOW so we know if the new value is
233 different or not. */
234 CORE_ADDR prev_low;
238 /* Find an address from which we can start disassembling. */
239 prev_low = new_low;
240 new_low = tui_find_backward_disassembly_start_address (new_low);
242 /* Disassemble forward. */
243 next_addr = tui_disassemble (gdbarch, asm_lines, new_low, max_lines);
244 if (asm_lines.empty ())
245 break;
246 last_addr = asm_lines.back ().addr;
248 /* If disassembling from the current value of NEW_LOW reached PC
249 (or went past it) then this would do as a starting point if we
250 can't find anything better, so remember it. */
251 if (last_addr >= pc && new_low != prev_low
252 && asm_lines.size () >= max_lines)
253 possible_new_low.emplace (new_low);
255 /* Continue searching until we find a value of NEW_LOW from which
256 disassembling MAX_LINES instructions doesn't reach PC. We
257 know this means we can find the required number of previous
258 instructions then. */
260 while ((last_addr > pc
261 || (last_addr == pc && asm_lines.size () < max_lines))
262 && new_low != prev_low);
264 /* If we failed to disassemble the required number of lines, try to fall
265 back to a previous possible start address in POSSIBLE_NEW_LOW. */
266 if (asm_lines.size () < max_lines)
268 if (!possible_new_low.has_value ())
269 return new_low;
271 /* Take the best possible match we have. */
272 new_low = *possible_new_low;
273 next_addr = tui_disassemble (gdbarch, asm_lines, new_low, max_lines);
276 /* The following walk forward assumes that ASM_LINES contains exactly
277 MAX_LINES entries. */
278 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 last_addr = asm_lines.back ().addr;
286 if (last_addr < pc)
289 pos++;
290 if (pos >= max_lines)
291 pos = 0;
293 CORE_ADDR old_next_addr = next_addr;
294 std::vector<tui_asm_line> single_asm_line;
295 next_addr = tui_disassemble (gdbarch, single_asm_line,
296 next_addr, 1);
297 /* If there are some problems while disassembling exit. */
298 if (next_addr <= old_next_addr)
299 return pc;
300 gdb_assert (single_asm_line.size () == 1);
301 asm_lines[pos] = single_asm_line[0];
302 } while (next_addr <= pc);
303 pos++;
304 if (pos >= max_lines)
305 pos = 0;
306 new_low = asm_lines[pos].addr;
308 /* When scrolling backward the addresses should move backward, or at
309 the very least stay the same if we are at the first address that
310 can be disassembled. */
311 gdb_assert (new_low <= pc);
313 return new_low;
316 /* Function to set the disassembly window's content. */
317 bool
318 tui_disasm_window::set_contents (struct gdbarch *arch,
319 const struct symtab_and_line &sal)
321 int i;
322 int max_lines;
323 CORE_ADDR cur_pc;
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 = tui_location.addr ();
336 /* Window size, excluding highlight box. */
337 max_lines = height - box_size ();
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 gdbarch *gdbarch = get_current_arch ();
388 CORE_ADDR addr = 0;
390 if (tui_location.addr () == 0)
392 if (have_full_symbols (current_program_space)
393 || have_partial_symbols (current_program_space))
395 set_default_source_symtab_and_line ();
396 symtab_and_line sal
397 = get_current_source_symtab_and_line (current_program_space);
399 if (sal.symtab != nullptr)
400 find_line_pc (sal.symtab, sal.line, &addr);
403 if (addr == 0)
405 bound_minimal_symbol main_symbol
406 = lookup_minimal_symbol (current_program_space, main_name ());
407 if (main_symbol.minsym != nullptr)
408 addr = main_symbol.value_address ();
411 else /* The target is executing. */
413 gdbarch = tui_location.gdbarch ();
414 addr = tui_location.addr ();
417 *gdbarch_p = gdbarch;
418 *addr_p = addr;
421 /* Determine what the low address will be to display in the TUI's
422 disassembly window. This may or may not be the same as the low
423 address input. */
424 CORE_ADDR
425 tui_get_low_disassembly_address (struct gdbarch *gdbarch,
426 CORE_ADDR low, CORE_ADDR pc)
428 int pos;
430 /* Determine where to start the disassembly so that the pc is about
431 in the middle of the viewport. */
432 if (tui_disasm_win () != nullptr)
433 pos = tui_disasm_win ()->height;
434 else if (tui_cmd_win () == nullptr)
435 pos = tui_term_height () / 2 - 2;
436 else
437 pos = tui_term_height () - tui_cmd_win ()->height - 2;
438 pos = (pos - 2) / 2;
440 pc = tui_find_disassembly_address (gdbarch, pc, -pos);
442 if (pc < low)
443 pc = low;
444 return pc;
447 /* Scroll the disassembly forward or backward vertically. */
448 void
449 tui_disasm_window::do_scroll_vertical (int num_to_scroll)
451 if (!m_content.empty ())
453 CORE_ADDR pc;
455 pc = m_start_line_or_addr.u.addr;
457 symtab_and_line sal {};
458 sal.pspace = current_program_space;
459 sal.pc = tui_find_disassembly_address (m_gdbarch, pc, num_to_scroll);
460 update_source_window_as_is (m_gdbarch, sal);
464 bool
465 tui_disasm_window::location_matches_p (struct bp_location *loc, int line_no)
467 return (m_content[line_no].line_or_addr.loa == LOA_ADDRESS
468 && m_content[line_no].line_or_addr.u.addr == loc->address);
471 bool
472 tui_disasm_window::addr_is_displayed (CORE_ADDR addr) const
474 if (m_content.size () < SCROLL_THRESHOLD)
475 return false;
477 for (size_t i = 0; i < m_content.size () - SCROLL_THRESHOLD; ++i)
479 if (m_content[i].line_or_addr.loa == LOA_ADDRESS
480 && m_content[i].line_or_addr.u.addr == addr)
481 return true;
484 return false;
487 void
488 tui_disasm_window::maybe_update (const frame_info_ptr &fi, symtab_and_line sal)
490 CORE_ADDR low;
492 struct gdbarch *frame_arch = get_frame_arch (fi);
494 if (find_pc_partial_function (sal.pc, NULL, &low, NULL) == 0)
496 /* There is no symbol available for current PC. There is no
497 safe way how to "disassemble backwards". */
498 low = sal.pc;
500 else
501 low = tui_get_low_disassembly_address (frame_arch, low, sal.pc);
503 struct tui_line_or_address a;
505 a.loa = LOA_ADDRESS;
506 a.u.addr = low;
507 if (!addr_is_displayed (sal.pc))
509 sal.pc = low;
510 update_source_window (frame_arch, sal);
512 else
514 a.u.addr = sal.pc;
515 set_is_exec_point_at (a);
519 void
520 tui_disasm_window::display_start_addr (struct gdbarch **gdbarch_p,
521 CORE_ADDR *addr_p)
523 *gdbarch_p = m_gdbarch;
524 *addr_p = m_start_line_or_addr.u.addr;
527 #if GDB_SELF_TEST
528 namespace selftests {
529 namespace tui {
530 namespace disasm {
532 static void
533 run_tests ()
535 if (current_inferior () != nullptr)
537 gdbarch *gdbarch = current_inferior ()->arch ();
539 /* Check that tui_find_disassembly_address robustly handles the case of
540 being passed a PC for which gdb_print_insn throws a MEMORY_ERROR. */
541 SELF_CHECK (tui_find_disassembly_address (gdbarch, 0, 1) == 0);
542 SELF_CHECK (tui_find_disassembly_address (gdbarch, 0, -1) == 0);
546 } /* namespace disasm */
547 } /* namespace tui */
548 } /* namespace selftests */
549 #endif /* GDB_SELF_TEST */
551 void _initialize_tui_disasm ();
552 void
553 _initialize_tui_disasm ()
555 #if GDB_SELF_TEST
556 selftests::register_test ("tui-disasm", selftests::tui::disasm::run_tests);
557 #endif