Use symbol_set_language to set a symbol's language
[deliverable/binutils-gdb.git] / gdb / tui / tui-disasm.c
CommitLineData
f377b406 1/* Disassembly display.
f33c6cbf 2
42a4f53d 3 Copyright (C) 1998-2019 Free Software Foundation, Inc.
f33c6cbf 4
f377b406
SC
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
a9762ec7 11 the Free Software Foundation; either version 3 of the License, or
f377b406
SC
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
a9762ec7 20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
21
22#include "defs.h"
957b8b5a 23#include "arch-utils.h"
c906108c
SS
24#include "symtab.h"
25#include "breakpoint.h"
26#include "frame.h"
fd0407d6 27#include "value.h"
52575520 28#include "source.h"
f70a7d61 29#include "disasm.h"
d7b2e967 30#include "tui/tui.h"
6d7fd9aa 31#include "tui/tui-command.h"
d7b2e967
AC
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"
2c0b251b 38#include "tui/tui-disasm.h"
bfad4537 39#include "tui/tui-source.h"
6c95b8df 40#include "progspace.h"
77e371c0 41#include "objfiles.h"
1df2f9ef 42#include "cli/cli-style.h"
c906108c 43
6a83354a 44#include "gdb_curses.h"
96ec9981 45
aec2f747
SC
46struct tui_asm_line
47{
48 CORE_ADDR addr;
6b915f7d 49 std::string addr_string;
825165c5 50 size_t addr_size;
6b915f7d 51 std::string insn;
aec2f747
SC
52};
53
1df2f9ef
TT
54/* Helper function to find the number of characters in STR, skipping
55 any ANSI escape sequences. */
56static size_t
57len_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
aec2f747
SC
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. */
87static CORE_ADDR
6b915f7d
TT
88tui_disassemble (struct gdbarch *gdbarch,
89 std::vector<tui_asm_line> &asm_lines,
1df2f9ef
TT
90 CORE_ADDR pc, int pos, int count,
91 size_t *addr_size = nullptr)
aec2f747 92{
1df2f9ef
TT
93 bool term_out = source_styling && gdb_stdout->can_emit_style_escape ();
94 string_file gdb_dis_out (term_out);
aec2f747 95
1cc6d956 96 /* Now construct each line. */
6b915f7d 97 for (int i = 0; i < count; ++i)
aec2f747 98 {
d7e74731 99 print_address (gdbarch, pc, &gdb_dis_out);
6b915f7d
TT
100 asm_lines[pos + i].addr = pc;
101 asm_lines[pos + i].addr_string = std::move (gdb_dis_out.string ());
aec2f747 102
d7e74731 103 gdb_dis_out.clear ();
aec2f747 104
1df2f9ef
TT
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);
825165c5 114 asm_lines[pos + i].addr_size = new_size;
1df2f9ef
TT
115 }
116
d7e74731 117 pc = pc + gdb_print_insn (gdbarch, pc, &gdb_dis_out, NULL);
aec2f747 118
6b915f7d 119 asm_lines[pos + i].insn = std::move (gdb_dis_out.string ());
aec2f747 120
1cc6d956 121 /* Reset the buffer to empty. */
d7e74731 122 gdb_dis_out.clear ();
aec2f747 123 }
aec2f747
SC
124 return pc;
125}
126
1cc6d956
MS
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. */
aec2f747 130static CORE_ADDR
13274fc3 131tui_find_disassembly_address (struct gdbarch *gdbarch, CORE_ADDR pc, int from)
aec2f747 132{
d02c80cd 133 CORE_ADDR new_low;
6ba8e26f 134 int max_lines;
aec2f747 135
6ba8e26f
AC
136 max_lines = (from > 0) ? from : - from;
137 if (max_lines <= 1)
6b915f7d 138 return pc;
aec2f747 139
6b915f7d 140 std::vector<tui_asm_line> asm_lines (max_lines);
aec2f747 141
6ba8e26f 142 new_low = pc;
aec2f747
SC
143 if (from > 0)
144 {
6b915f7d 145 tui_disassemble (gdbarch, asm_lines, pc, 0, max_lines);
4cfcaf21 146 new_low = asm_lines[max_lines - 1].addr;
aec2f747
SC
147 }
148 else
149 {
150 CORE_ADDR last_addr;
151 int pos;
77e371c0 152 struct bound_minimal_symbol msymbol;
aec2f747 153
1cc6d956
MS
154 /* Find backward an address which is a symbol and for which
155 disassembling from that address will fill completely the
156 window. */
6ba8e26f 157 pos = max_lines - 1;
aec2f747 158 do {
6ba8e26f 159 new_low -= 1 * max_lines;
77e371c0 160 msymbol = lookup_minimal_symbol_by_pc_section (new_low, 0);
aec2f747 161
77e371c0
TT
162 if (msymbol.minsym)
163 new_low = BMSYMBOL_VALUE_ADDRESS (msymbol);
aec2f747 164 else
6ba8e26f 165 new_low += 1 * max_lines;
aec2f747 166
6b915f7d 167 tui_disassemble (gdbarch, asm_lines, new_low, 0, max_lines);
4cfcaf21 168 last_addr = asm_lines[pos].addr;
77e371c0 169 } while (last_addr > pc && msymbol.minsym);
aec2f747 170
1cc6d956
MS
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). */
aec2f747
SC
175 if (last_addr < pc)
176 do
177 {
178 CORE_ADDR next_addr;
179
180 pos++;
6ba8e26f 181 if (pos >= max_lines)
aec2f747
SC
182 pos = 0;
183
6b915f7d
TT
184 next_addr = tui_disassemble (gdbarch, asm_lines,
185 last_addr, pos, 1);
aec2f747
SC
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++;
6ba8e26f 193 if (pos >= max_lines)
aec2f747 194 pos = 0;
4cfcaf21 195 new_low = asm_lines[pos].addr;
aec2f747 196 }
6ba8e26f 197 return new_low;
aec2f747
SC
198}
199
200/* Function to set the disassembly window's content. */
65f05602 201enum tui_status
81c82c4b
TT
202tui_disasm_window::set_contents (struct gdbarch *arch,
203 struct symtab *s,
204 struct tui_line_or_address line_or_addr)
c906108c 205{
d02c80cd 206 int i;
81c82c4b 207 int offset = horizontal_offset;
0bb65f1e 208 int max_lines, line_width;
aec2f747 209 CORE_ADDR cur_pc;
3add462f 210 struct tui_locator_window *locator = tui_locator_win_info_ptr ();
7806cea7 211 int tab_len = tui_tab_width;
aec2f747 212 int insn_pos;
1df2f9ef 213
81c82c4b
TT
214 gdb_assert (line_or_addr.loa == LOA_ADDRESS);
215 CORE_ADDR pc = line_or_addr.u.addr;
aec2f747
SC
216 if (pc == 0)
217 return TUI_FAILURE;
218
81c82c4b
TT
219 gdbarch = arch;
220 start_line_or_addr.loa = LOA_ADDRESS;
221 start_line_or_addr.u.addr = pc;
3add462f 222 cur_pc = locator->addr;
aec2f747 223
0bb65f1e 224 /* Window size, excluding highlight box. */
81c82c4b 225 max_lines = height - 2;
398fdd60 226 line_width = width - TUI_EXECINFO_SIZE - 2;
aec2f747
SC
227
228 /* Get temporary table that will hold all strings (addr & insn). */
6b915f7d 229 std::vector<tui_asm_line> asm_lines (max_lines);
1df2f9ef
TT
230 size_t addr_size = 0;
231 tui_disassemble (gdbarch, asm_lines, pc, 0, max_lines, &addr_size);
aec2f747 232
f5396833 233 /* Align instructions to the same column. */
aec2f747
SC
234 insn_pos = (1 + (addr_size / tab_len)) * tab_len;
235
1cc6d956 236 /* Now construct each line. */
81c82c4b 237 content.resize (max_lines);
6ba8e26f 238 for (i = 0; i < max_lines; i++)
aec2f747 239 {
81c82c4b 240 tui_source_element *src = &content[i];
6b915f7d
TT
241
242 std::string line
243 = (asm_lines[i].addr_string
825165c5 244 + n_spaces (insn_pos - asm_lines[i].addr_size)
6b915f7d 245 + asm_lines[i].insn);
aec2f747 246
1df2f9ef 247 const char *ptr = line.c_str ();
d1da6b01 248 src->line = tui_copy_source_line (&ptr, -1, offset, line_width, 0);
aec2f747 249
362c05fe
AS
250 src->line_or_addr.loa = LOA_ADDRESS;
251 src->line_or_addr.u.addr = asm_lines[i].addr;
4cfcaf21 252 src->is_exec_point = asm_lines[i].addr == cur_pc;
aec2f747 253 }
aec2f747
SC
254 return TUI_SUCCESS;
255}
c906108c
SS
256
257
1cc6d956 258/* Function to display the disassembly window with disassembled code. */
c906108c 259void
13274fc3 260tui_show_disassem (struct gdbarch *gdbarch, CORE_ADDR start_addr)
c906108c 261{
34248c3a 262 struct symtab *s = find_pc_line_symtab (start_addr);
5b6fe301 263 struct tui_win_info *win_with_focus = tui_win_with_focus ();
362c05fe 264 struct tui_line_or_address val;
c906108c 265
2d83e710 266 gdb_assert (TUI_DISASM_WIN != nullptr && TUI_DISASM_WIN->is_visible ());
22c3f490 267
362c05fe
AS
268 val.loa = LOA_ADDRESS;
269 val.u.addr = start_addr;
017f9828 270 TUI_DISASM_WIN->update_source_window (gdbarch, s, val);
ef5eab5a
MS
271
272 /* If the focus was in the src win, put it in the asm win, if the
273 source view isn't split. */
e5908723
MS
274 if (tui_current_layout () != SRC_DISASSEM_COMMAND
275 && win_with_focus == TUI_SRC_WIN)
6d012f14 276 tui_set_win_focus_to (TUI_DISASM_WIN);
65f05602 277}
c906108c
SS
278
279
1cc6d956 280/* Function to display the disassembly window. */
c906108c 281void
13274fc3
UW
282tui_show_disassem_and_update_source (struct gdbarch *gdbarch,
283 CORE_ADDR start_addr)
c906108c
SS
284{
285 struct symtab_and_line sal;
286
13274fc3 287 tui_show_disassem (gdbarch, start_addr);
dd1abb8c 288 if (tui_current_layout () == SRC_DISASSEM_COMMAND)
c906108c 289 {
362c05fe 290 struct tui_line_or_address val;
aec2f747 291
ef5eab5a
MS
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
294 visa-versa. */
6ba8e26f 295 sal = find_pc_line (start_addr, 0);
362c05fe
AS
296 val.loa = LOA_LINE;
297 val.u.line_no = sal.line;
017f9828 298 TUI_SRC_WIN->update_source_window (gdbarch, sal.symtab, val);
3024f13a
SC
299 if (sal.symtab)
300 {
51abb421 301 set_current_source_symtab_and_line (sal);
56d397a3 302 tui_update_locator_fullname (symtab_to_fullname (sal.symtab));
3024f13a
SC
303 }
304 else
56d397a3 305 tui_update_locator_fullname ("?");
c906108c 306 }
65f05602 307}
c906108c 308
13274fc3
UW
309void
310tui_get_begin_asm_address (struct gdbarch **gdbarch_p, CORE_ADDR *addr_p)
c906108c 311{
3add462f 312 struct tui_locator_window *locator;
957b8b5a 313 struct gdbarch *gdbarch = get_current_arch ();
c774cec6 314 CORE_ADDR addr;
c906108c 315
dd1abb8c 316 locator = tui_locator_win_info_ptr ();
c906108c 317
3add462f 318 if (locator->addr == 0)
c906108c 319 {
3b7344d5 320 struct bound_minimal_symbol main_symbol;
0510ab86
SC
321
322 /* Find address of the start of program.
323 Note: this should be language specific. */
324 main_symbol = lookup_minimal_symbol ("main", NULL, NULL);
3b7344d5 325 if (main_symbol.minsym == 0)
0510ab86 326 main_symbol = lookup_minimal_symbol ("MAIN", NULL, NULL);
3b7344d5 327 if (main_symbol.minsym == 0)
0510ab86 328 main_symbol = lookup_minimal_symbol ("_start", NULL, NULL);
3b7344d5 329 if (main_symbol.minsym)
77e371c0 330 addr = BMSYMBOL_VALUE_ADDRESS (main_symbol);
0510ab86
SC
331 else
332 addr = 0;
c906108c 333 }
1cc6d956 334 else /* The target is executing. */
13274fc3 335 {
3add462f
TT
336 gdbarch = locator->gdbarch;
337 addr = locator->addr;
13274fc3 338 }
c906108c 339
13274fc3
UW
340 *gdbarch_p = gdbarch;
341 *addr_p = addr;
65f05602 342}
c906108c 343
77cad3ba 344/* Determine what the low address will be to display in the TUI's
1cc6d956
MS
345 disassembly window. This may or may not be the same as the low
346 address input. */
77cad3ba 347CORE_ADDR
13274fc3
UW
348tui_get_low_disassembly_address (struct gdbarch *gdbarch,
349 CORE_ADDR low, CORE_ADDR pc)
77cad3ba
SC
350{
351 int pos;
352
1cc6d956
MS
353 /* Determine where to start the disassembly so that the pc is about
354 in the middle of the viewport. */
6d7fd9aa
TT
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;
359 else
360 pos = tui_term_height () - TUI_CMD_WIN->height - 2;
361 pos = (pos - 2) / 2;
362
13274fc3 363 pc = tui_find_disassembly_address (gdbarch, pc, -pos);
77cad3ba
SC
364
365 if (pc < low)
366 pc = low;
367 return pc;
368}
369
65f05602 370/* Scroll the disassembly forward or backward vertically. */
c906108c 371void
c3bd716f 372tui_disasm_window::do_scroll_vertical (int num_to_scroll)
c906108c 373{
53e7cdba 374 if (!content.empty ())
c906108c 375 {
aec2f747 376 CORE_ADDR pc;
362c05fe 377 struct tui_line_or_address val;
c906108c 378
53e7cdba 379 pc = content[0].line_or_addr.u.addr;
c3bd716f
TT
380 if (num_to_scroll >= 0)
381 num_to_scroll++;
382 else
383 --num_to_scroll;
c906108c 384
362c05fe 385 val.loa = LOA_ADDRESS;
c3bd716f 386 val.u.addr = tui_find_disassembly_address (gdbarch, pc, num_to_scroll);
ed8358e9 387 update_source_window_as_is (gdbarch, NULL, val);
aec2f747
SC
388 }
389}
c2cd8994
TT
390
391bool
392tui_disasm_window::location_matches_p (struct bp_location *loc, int line_no)
393{
394 return (content[line_no].line_or_addr.loa == LOA_ADDRESS
395 && content[line_no].line_or_addr.u.addr == loc->address);
396}
a54700c6 397
088f37dd
TT
398bool
399tui_disasm_window::addr_is_displayed (CORE_ADDR addr) const
400{
401 bool is_displayed = false;
402 int threshold = SCROLL_THRESHOLD;
403
404 int i = 0;
405 while (i < content.size () - threshold && !is_displayed)
406 {
407 is_displayed
408 = (content[i].line_or_addr.loa == LOA_ADDRESS
409 && content[i].line_or_addr.u.addr == addr);
410 i++;
411 }
412
413 return is_displayed;
414}
415
a54700c6
TT
416void
417tui_disasm_window::maybe_update (struct frame_info *fi, symtab_and_line sal,
418 int line_no, CORE_ADDR addr)
419{
420 CORE_ADDR low;
421
422 if (find_pc_partial_function (get_frame_pc (fi),
423 NULL, &low, NULL) == 0)
424 {
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);
428 }
429 else
430 low = tui_get_low_disassembly_address (get_frame_arch (fi),
431 low, get_frame_pc (fi));
432
433 struct tui_line_or_address a;
434
435 a.loa = LOA_ADDRESS;
436 a.u.addr = low;
088f37dd 437 if (!addr_is_displayed (addr))
017f9828 438 update_source_window (get_frame_arch (fi), sal.symtab, a);
a54700c6
TT
439 else
440 {
441 a.u.addr = addr;
442 set_is_exec_point_at (a);
443 }
444}
This page took 2.098997 seconds and 4 git commands to generate.