1 /* Disassembly display.
3 Copyright (C) 1998-2019 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/>. */
23 #include "arch-utils.h"
25 #include "breakpoint.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"
42 #include "cli/cli-style.h"
44 #include "gdb_curses.h"
49 std::string addr_string
;
54 /* Helper function to find the number of characters in STR, skipping
55 any ANSI escape sequences. */
57 len_without_escapes (const std::string
&str
)
60 const char *ptr
= str
.c_str ();
63 while ((c
= *ptr
++) != '\0')
69 if (style
.parse (ptr
, &n_read
))
73 /* Shouldn't happen, but just skip the ESC if it somehow
84 /* Function to set the disassembly window's content.
85 Disassemble count lines starting at pc.
86 Return address of the count'th instruction after pc. */
88 tui_disassemble (struct gdbarch
*gdbarch
,
89 std::vector
<tui_asm_line
> &asm_lines
,
90 CORE_ADDR pc
, int pos
, int count
,
91 size_t *addr_size
= nullptr)
93 bool term_out
= source_styling
&& gdb_stdout
->can_emit_style_escape ();
94 string_file
gdb_dis_out (term_out
);
96 /* Now construct each line. */
97 for (int i
= 0; i
< count
; ++i
)
99 print_address (gdbarch
, pc
, &gdb_dis_out
);
100 asm_lines
[pos
+ i
].addr
= pc
;
101 asm_lines
[pos
+ i
].addr_string
= std::move (gdb_dis_out
.string ());
103 gdb_dis_out
.clear ();
105 if (addr_size
!= nullptr)
110 new_size
= len_without_escapes (asm_lines
[pos
+ i
].addr_string
);
112 new_size
= asm_lines
[pos
+ i
].addr_string
.size ();
113 *addr_size
= std::max (*addr_size
, new_size
);
114 asm_lines
[pos
+ i
].addr_size
= new_size
;
117 pc
= pc
+ gdb_print_insn (gdbarch
, pc
, &gdb_dis_out
, NULL
);
119 asm_lines
[pos
+ i
].insn
= std::move (gdb_dis_out
.string ());
121 /* Reset the buffer to empty. */
122 gdb_dis_out
.clear ();
127 /* Find the disassembly address that corresponds to FROM lines above
128 or below the PC. Variable sized instructions are taken into
129 account by the algorithm. */
131 tui_find_disassembly_address (struct gdbarch
*gdbarch
, CORE_ADDR pc
, int from
)
136 max_lines
= (from
> 0) ? from
: - from
;
140 std::vector
<tui_asm_line
> asm_lines (max_lines
);
145 tui_disassemble (gdbarch
, asm_lines
, pc
, 0, max_lines
);
146 new_low
= asm_lines
[max_lines
- 1].addr
;
152 struct bound_minimal_symbol msymbol
;
154 /* Find backward an address which is a symbol and for which
155 disassembling from that address will fill completely the
159 new_low
-= 1 * max_lines
;
160 msymbol
= lookup_minimal_symbol_by_pc_section (new_low
, 0);
163 new_low
= BMSYMBOL_VALUE_ADDRESS (msymbol
);
165 new_low
+= 1 * max_lines
;
167 tui_disassemble (gdbarch
, asm_lines
, new_low
, 0, max_lines
);
168 last_addr
= asm_lines
[pos
].addr
;
169 } while (last_addr
> pc
&& msymbol
.minsym
);
171 /* Scan forward disassembling one instruction at a time until
172 the last visible instruction of the window matches the pc.
173 We keep the disassembled instructions in the 'lines' window
174 and shift it downward (increasing its addresses). */
181 if (pos
>= max_lines
)
184 next_addr
= tui_disassemble (gdbarch
, asm_lines
,
187 /* If there are some problems while disassembling exit. */
188 if (next_addr
<= last_addr
)
190 last_addr
= next_addr
;
191 } while (last_addr
<= pc
);
193 if (pos
>= max_lines
)
195 new_low
= asm_lines
[pos
].addr
;
200 /* Function to set the disassembly window's content. */
202 tui_disasm_window::set_contents (struct gdbarch
*arch
,
204 struct tui_line_or_address line_or_addr
)
207 int offset
= horizontal_offset
;
208 int max_lines
, line_width
;
210 struct tui_locator_window
*locator
= tui_locator_win_info_ptr ();
211 int tab_len
= tui_tab_width
;
214 gdb_assert (line_or_addr
.loa
== LOA_ADDRESS
);
215 CORE_ADDR pc
= line_or_addr
.u
.addr
;
220 start_line_or_addr
.loa
= LOA_ADDRESS
;
221 start_line_or_addr
.u
.addr
= pc
;
222 cur_pc
= locator
->addr
;
224 /* Window size, excluding highlight box. */
225 max_lines
= height
- 2;
226 line_width
= width
- TUI_EXECINFO_SIZE
- 2;
228 /* Get temporary table that will hold all strings (addr & insn). */
229 std::vector
<tui_asm_line
> asm_lines (max_lines
);
230 size_t addr_size
= 0;
231 tui_disassemble (gdbarch
, asm_lines
, pc
, 0, max_lines
, &addr_size
);
233 /* Align instructions to the same column. */
234 insn_pos
= (1 + (addr_size
/ tab_len
)) * tab_len
;
236 /* Now construct each line. */
237 content
.resize (max_lines
);
238 for (i
= 0; i
< max_lines
; i
++)
240 tui_source_element
*src
= &content
[i
];
243 = (asm_lines
[i
].addr_string
244 + n_spaces (insn_pos
- asm_lines
[i
].addr_size
)
245 + asm_lines
[i
].insn
);
247 const char *ptr
= line
.c_str ();
248 src
->line
= tui_copy_source_line (&ptr
, -1, offset
, line_width
);
250 src
->line_or_addr
.loa
= LOA_ADDRESS
;
251 src
->line_or_addr
.u
.addr
= asm_lines
[i
].addr
;
252 src
->is_exec_point
= asm_lines
[i
].addr
== cur_pc
;
258 /* Function to display the disassembly window with disassembled code. */
260 tui_show_disassem (struct gdbarch
*gdbarch
, CORE_ADDR start_addr
)
262 struct symtab
*s
= find_pc_line_symtab (start_addr
);
263 struct tui_win_info
*win_with_focus
= tui_win_with_focus ();
264 struct tui_line_or_address val
;
266 gdb_assert (TUI_DISASM_WIN
!= nullptr && TUI_DISASM_WIN
->is_visible ());
268 val
.loa
= LOA_ADDRESS
;
269 val
.u
.addr
= start_addr
;
270 TUI_DISASM_WIN
->update_source_window (gdbarch
, s
, val
);
272 /* If the focus was in the src win, put it in the asm win, if the
273 source view isn't split. */
274 if (tui_current_layout () != SRC_DISASSEM_COMMAND
275 && win_with_focus
== TUI_SRC_WIN
)
276 tui_set_win_focus_to (TUI_DISASM_WIN
);
280 /* Function to display the disassembly window. */
282 tui_show_disassem_and_update_source (struct gdbarch
*gdbarch
,
283 CORE_ADDR start_addr
)
285 struct symtab_and_line sal
;
287 tui_show_disassem (gdbarch
, start_addr
);
288 if (tui_current_layout () == SRC_DISASSEM_COMMAND
)
290 struct tui_line_or_address val
;
292 /* Update what is in the source window if it is displayed too,
293 note that it follows what is in the disassembly window and
295 sal
= find_pc_line (start_addr
, 0);
297 val
.u
.line_no
= sal
.line
;
298 TUI_SRC_WIN
->update_source_window (gdbarch
, sal
.symtab
, val
);
301 set_current_source_symtab_and_line (sal
);
302 tui_update_locator_fullname (symtab_to_fullname (sal
.symtab
));
305 tui_update_locator_fullname ("?");
310 tui_get_begin_asm_address (struct gdbarch
**gdbarch_p
, CORE_ADDR
*addr_p
)
312 struct tui_locator_window
*locator
;
313 struct gdbarch
*gdbarch
= get_current_arch ();
316 locator
= tui_locator_win_info_ptr ();
318 if (locator
->addr
== 0)
320 struct bound_minimal_symbol main_symbol
;
322 /* Find address of the start of program.
323 Note: this should be language specific. */
324 main_symbol
= lookup_minimal_symbol ("main", NULL
, NULL
);
325 if (main_symbol
.minsym
== 0)
326 main_symbol
= lookup_minimal_symbol ("MAIN", NULL
, NULL
);
327 if (main_symbol
.minsym
== 0)
328 main_symbol
= lookup_minimal_symbol ("_start", NULL
, NULL
);
329 if (main_symbol
.minsym
)
330 addr
= BMSYMBOL_VALUE_ADDRESS (main_symbol
);
334 else /* The target is executing. */
336 gdbarch
= locator
->gdbarch
;
337 addr
= locator
->addr
;
340 *gdbarch_p
= gdbarch
;
344 /* Determine what the low address will be to display in the TUI's
345 disassembly window. This may or may not be the same as the low
348 tui_get_low_disassembly_address (struct gdbarch
*gdbarch
,
349 CORE_ADDR low
, CORE_ADDR pc
)
353 /* Determine where to start the disassembly so that the pc is about
354 in the middle of the viewport. */
355 if (tui_win_list
[DISASSEM_WIN
] != NULL
)
356 pos
= tui_win_list
[DISASSEM_WIN
]->height
;
357 else if (TUI_CMD_WIN
== NULL
)
358 pos
= tui_term_height () / 2 - 2;
360 pos
= tui_term_height () - TUI_CMD_WIN
->height
- 2;
363 pc
= tui_find_disassembly_address (gdbarch
, pc
, -pos
);
370 /* Scroll the disassembly forward or backward vertically. */
372 tui_disasm_window::do_scroll_vertical (int num_to_scroll
)
374 if (!content
.empty ())
377 struct tui_line_or_address val
;
379 pc
= content
[0].line_or_addr
.u
.addr
;
380 if (num_to_scroll
>= 0)
385 val
.loa
= LOA_ADDRESS
;
386 val
.u
.addr
= tui_find_disassembly_address (gdbarch
, pc
, num_to_scroll
);
387 update_source_window_as_is (gdbarch
, NULL
, val
);
392 tui_disasm_window::location_matches_p (struct bp_location
*loc
, int line_no
)
394 return (content
[line_no
].line_or_addr
.loa
== LOA_ADDRESS
395 && content
[line_no
].line_or_addr
.u
.addr
== loc
->address
);
399 tui_disasm_window::addr_is_displayed (CORE_ADDR addr
) const
401 bool is_displayed
= false;
402 int threshold
= SCROLL_THRESHOLD
;
405 while (i
< content
.size () - threshold
&& !is_displayed
)
408 = (content
[i
].line_or_addr
.loa
== LOA_ADDRESS
409 && content
[i
].line_or_addr
.u
.addr
== addr
);
417 tui_disasm_window::maybe_update (struct frame_info
*fi
, symtab_and_line sal
,
418 int line_no
, CORE_ADDR addr
)
422 if (find_pc_partial_function (get_frame_pc (fi
),
423 NULL
, &low
, NULL
) == 0)
425 /* There is no symbol available for current PC. There is no
426 safe way how to "disassemble backwards". */
427 low
= get_frame_pc (fi
);
430 low
= tui_get_low_disassembly_address (get_frame_arch (fi
),
431 low
, get_frame_pc (fi
));
433 struct tui_line_or_address a
;
437 if (!addr_is_displayed (addr
))
438 update_source_window (get_frame_arch (fi
), sal
.symtab
, a
);
442 set_is_exec_point_at (a
);