Remove tui_show_disassem
[deliverable/binutils-gdb.git] / gdb / tui / tui-disasm.c
1 /* Disassembly display.
2
3 Copyright (C) 1998-2019 Free Software Foundation, Inc.
4
5 Contributed by Hewlett-Packard Company.
6
7 This file is part of GDB.
8
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.
13
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.
18
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/>. */
21
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"
43
44 #include "gdb_curses.h"
45
46 struct tui_asm_line
47 {
48 CORE_ADDR addr;
49 std::string addr_string;
50 size_t addr_size;
51 std::string insn;
52 };
53
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)
58 {
59 size_t len = 0;
60 const char *ptr = str.c_str ();
61 char c;
62
63 while ((c = *ptr++) != '\0')
64 {
65 if (c == '\033')
66 {
67 ui_file_style style;
68 size_t n_read;
69 if (style.parse (ptr, &n_read))
70 ptr += n_read;
71 else
72 {
73 /* Shouldn't happen, but just skip the ESC if it somehow
74 does. */
75 ++ptr;
76 }
77 }
78 else
79 ++len;
80 }
81 return len;
82 }
83
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. */
87 static CORE_ADDR
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)
92 {
93 bool term_out = source_styling && gdb_stdout->can_emit_style_escape ();
94 string_file gdb_dis_out (term_out);
95
96 /* Now construct each line. */
97 for (int i = 0; i < count; ++i)
98 {
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 ());
102
103 gdb_dis_out.clear ();
104
105 if (addr_size != nullptr)
106 {
107 size_t new_size;
108
109 if (term_out)
110 new_size = len_without_escapes (asm_lines[pos + i].addr_string);
111 else
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;
115 }
116
117 pc = pc + gdb_print_insn (gdbarch, pc, &gdb_dis_out, NULL);
118
119 asm_lines[pos + i].insn = std::move (gdb_dis_out.string ());
120
121 /* Reset the buffer to empty. */
122 gdb_dis_out.clear ();
123 }
124 return pc;
125 }
126
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. */
130 static CORE_ADDR
131 tui_find_disassembly_address (struct gdbarch *gdbarch, CORE_ADDR pc, int from)
132 {
133 CORE_ADDR new_low;
134 int max_lines;
135
136 max_lines = (from > 0) ? from : - from;
137 if (max_lines <= 1)
138 return pc;
139
140 std::vector<tui_asm_line> asm_lines (max_lines);
141
142 new_low = pc;
143 if (from > 0)
144 {
145 tui_disassemble (gdbarch, asm_lines, pc, 0, max_lines);
146 new_low = asm_lines[max_lines - 1].addr;
147 }
148 else
149 {
150 CORE_ADDR last_addr;
151 int pos;
152 struct bound_minimal_symbol msymbol;
153
154 /* Find backward an address which is a symbol and for which
155 disassembling from that address will fill completely the
156 window. */
157 pos = max_lines - 1;
158 do {
159 new_low -= 1 * max_lines;
160 msymbol = lookup_minimal_symbol_by_pc_section (new_low, 0);
161
162 if (msymbol.minsym)
163 new_low = BMSYMBOL_VALUE_ADDRESS (msymbol);
164 else
165 new_low += 1 * max_lines;
166
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);
170
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). */
175 if (last_addr < pc)
176 do
177 {
178 CORE_ADDR next_addr;
179
180 pos++;
181 if (pos >= max_lines)
182 pos = 0;
183
184 next_addr = tui_disassemble (gdbarch, asm_lines,
185 last_addr, pos, 1);
186
187 /* If there are some problems while disassembling exit. */
188 if (next_addr <= last_addr)
189 break;
190 last_addr = next_addr;
191 } while (last_addr <= pc);
192 pos++;
193 if (pos >= max_lines)
194 pos = 0;
195 new_low = asm_lines[pos].addr;
196 }
197 return new_low;
198 }
199
200 /* Function to set the disassembly window's content. */
201 enum tui_status
202 tui_disasm_window::set_contents (struct gdbarch *arch,
203 struct symtab *s,
204 struct tui_line_or_address line_or_addr)
205 {
206 int i;
207 int offset = horizontal_offset;
208 int max_lines, line_width;
209 CORE_ADDR cur_pc;
210 struct tui_locator_window *locator = tui_locator_win_info_ptr ();
211 int tab_len = tui_tab_width;
212 int insn_pos;
213
214 gdb_assert (line_or_addr.loa == LOA_ADDRESS);
215 CORE_ADDR pc = line_or_addr.u.addr;
216 if (pc == 0)
217 return TUI_FAILURE;
218
219 gdbarch = arch;
220 start_line_or_addr.loa = LOA_ADDRESS;
221 start_line_or_addr.u.addr = pc;
222 cur_pc = locator->addr;
223
224 /* Window size, excluding highlight box. */
225 max_lines = height - 2;
226 line_width = width - TUI_EXECINFO_SIZE - 2;
227
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);
232
233 /* Align instructions to the same column. */
234 insn_pos = (1 + (addr_size / tab_len)) * tab_len;
235
236 /* Now construct each line. */
237 content.resize (max_lines);
238 for (i = 0; i < max_lines; i++)
239 {
240 tui_source_element *src = &content[i];
241
242 std::string line
243 = (asm_lines[i].addr_string
244 + n_spaces (insn_pos - asm_lines[i].addr_size)
245 + asm_lines[i].insn);
246
247 const char *ptr = line.c_str ();
248 src->line = tui_copy_source_line (&ptr, -1, offset, line_width, 0);
249
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;
253 }
254 return TUI_SUCCESS;
255 }
256
257
258 void
259 tui_get_begin_asm_address (struct gdbarch **gdbarch_p, CORE_ADDR *addr_p)
260 {
261 struct tui_locator_window *locator;
262 struct gdbarch *gdbarch = get_current_arch ();
263 CORE_ADDR addr;
264
265 locator = tui_locator_win_info_ptr ();
266
267 if (locator->addr == 0)
268 {
269 struct bound_minimal_symbol main_symbol;
270
271 /* Find address of the start of program.
272 Note: this should be language specific. */
273 main_symbol = lookup_minimal_symbol ("main", NULL, NULL);
274 if (main_symbol.minsym == 0)
275 main_symbol = lookup_minimal_symbol ("MAIN", NULL, NULL);
276 if (main_symbol.minsym == 0)
277 main_symbol = lookup_minimal_symbol ("_start", NULL, NULL);
278 if (main_symbol.minsym)
279 addr = BMSYMBOL_VALUE_ADDRESS (main_symbol);
280 else
281 addr = 0;
282 }
283 else /* The target is executing. */
284 {
285 gdbarch = locator->gdbarch;
286 addr = locator->addr;
287 }
288
289 *gdbarch_p = gdbarch;
290 *addr_p = addr;
291 }
292
293 /* Determine what the low address will be to display in the TUI's
294 disassembly window. This may or may not be the same as the low
295 address input. */
296 CORE_ADDR
297 tui_get_low_disassembly_address (struct gdbarch *gdbarch,
298 CORE_ADDR low, CORE_ADDR pc)
299 {
300 int pos;
301
302 /* Determine where to start the disassembly so that the pc is about
303 in the middle of the viewport. */
304 if (tui_win_list[DISASSEM_WIN] != NULL)
305 pos = tui_win_list[DISASSEM_WIN]->height;
306 else if (TUI_CMD_WIN == NULL)
307 pos = tui_term_height () / 2 - 2;
308 else
309 pos = tui_term_height () - TUI_CMD_WIN->height - 2;
310 pos = (pos - 2) / 2;
311
312 pc = tui_find_disassembly_address (gdbarch, pc, -pos);
313
314 if (pc < low)
315 pc = low;
316 return pc;
317 }
318
319 /* Scroll the disassembly forward or backward vertically. */
320 void
321 tui_disasm_window::do_scroll_vertical (int num_to_scroll)
322 {
323 if (!content.empty ())
324 {
325 CORE_ADDR pc;
326 struct tui_line_or_address val;
327
328 pc = content[0].line_or_addr.u.addr;
329 if (num_to_scroll >= 0)
330 num_to_scroll++;
331 else
332 --num_to_scroll;
333
334 val.loa = LOA_ADDRESS;
335 val.u.addr = tui_find_disassembly_address (gdbarch, pc, num_to_scroll);
336 update_source_window_as_is (gdbarch, NULL, val);
337 }
338 }
339
340 bool
341 tui_disasm_window::location_matches_p (struct bp_location *loc, int line_no)
342 {
343 return (content[line_no].line_or_addr.loa == LOA_ADDRESS
344 && content[line_no].line_or_addr.u.addr == loc->address);
345 }
346
347 bool
348 tui_disasm_window::addr_is_displayed (CORE_ADDR addr) const
349 {
350 bool is_displayed = false;
351 int threshold = SCROLL_THRESHOLD;
352
353 int i = 0;
354 while (i < content.size () - threshold && !is_displayed)
355 {
356 is_displayed
357 = (content[i].line_or_addr.loa == LOA_ADDRESS
358 && content[i].line_or_addr.u.addr == addr);
359 i++;
360 }
361
362 return is_displayed;
363 }
364
365 void
366 tui_disasm_window::maybe_update (struct frame_info *fi, symtab_and_line sal)
367 {
368 CORE_ADDR low;
369
370 struct gdbarch *frame_arch = get_frame_arch (fi);
371
372 if (find_pc_partial_function (sal.pc, NULL, &low, NULL) == 0)
373 {
374 /* There is no symbol available for current PC. There is no
375 safe way how to "disassemble backwards". */
376 low = sal.pc;
377 }
378 else
379 low = tui_get_low_disassembly_address (frame_arch, low, sal.pc);
380
381 struct tui_line_or_address a;
382
383 a.loa = LOA_ADDRESS;
384 a.u.addr = low;
385 if (!addr_is_displayed (sal.pc))
386 update_source_window (frame_arch, sal.symtab, a);
387 else
388 {
389 a.u.addr = sal.pc;
390 set_is_exec_point_at (a);
391 }
392 }
This page took 0.037248 seconds and 4 git commands to generate.