2002-09-29 Elena Zannoni <ezannoni@redhat.com>
[deliverable/binutils-gdb.git] / gdb / tui / tuiDisassem.c
CommitLineData
f377b406 1/* Disassembly display.
f33c6cbf
AC
2
3 Copyright 1998, 1999, 2000, 2001, 2002 Free Software Foundation,
4 Inc.
5
f377b406
SC
6 Contributed by Hewlett-Packard Company.
7
8 This file is part of GDB.
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330,
23 Boston, MA 02111-1307, USA. */
c906108c 24
f33c6cbf
AC
25/* FIXME: cagney/2002-02-28: The GDB coding standard indicates that
26 "defs.h" should be included first. Unfortunatly some systems
27 (currently Debian GNU/Linux) include the <stdbool.h> via <curses.h>
28 and they clash with "bfd.h"'s definiton of true/false. The correct
29 fix is to remove true/false from "bfd.h", however, until that
30 happens, hack around it by including "config.h" and <curses.h>
31 first. */
32
4e8f7a8b
DJ
33#include "config.h"
34#ifdef HAVE_NCURSES_H
35#include <ncurses.h>
36#else
37#ifdef HAVE_CURSES_H
38#include <curses.h>
39#endif
40#endif
41
c906108c
SS
42#include "defs.h"
43#include "symtab.h"
44#include "breakpoint.h"
45#include "frame.h"
fd0407d6 46#include "value.h"
52575520 47#include "source.h"
c906108c
SS
48
49#include "tui.h"
50#include "tuiData.h"
a8080b7f 51#include "tuiWin.h"
c906108c
SS
52#include "tuiLayout.h"
53#include "tuiSourceWin.h"
54#include "tuiStack.h"
a8080b7f 55#include "tui-file.h"
c906108c 56
aec2f747
SC
57struct tui_asm_line
58{
59 CORE_ADDR addr;
60 char* addr_string;
61 char* insn;
62};
63
64/* Function to set the disassembly window's content.
65 Disassemble count lines starting at pc.
66 Return address of the count'th instruction after pc. */
67static CORE_ADDR
68tui_disassemble (struct tui_asm_line* lines, CORE_ADDR pc, int count)
69{
70 struct ui_file *gdb_dis_out;
71 disassemble_info asm_info;
c906108c 72
aec2f747
SC
73 /* now init the ui_file structure */
74 gdb_dis_out = tui_sfileopen (256);
75
76 memcpy (&asm_info, TARGET_PRINT_INSN_INFO, sizeof (asm_info));
77 asm_info.stream = gdb_dis_out;
78
79 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
80 asm_info.endian = BFD_ENDIAN_BIG;
81 else
82 asm_info.endian = BFD_ENDIAN_LITTLE;
83
84 if (TARGET_ARCHITECTURE != NULL)
85 asm_info.mach = TARGET_ARCHITECTURE->mach;
86
87 /* Now construct each line */
88 for (; count > 0; count--, lines++)
89 {
90 if (lines->addr_string)
91 xfree (lines->addr_string);
92 if (lines->insn)
93 xfree (lines->insn);
94
95 print_address (pc, gdb_dis_out);
96 lines->addr = pc;
97 lines->addr_string = xstrdup (tui_file_get_strbuf (gdb_dis_out));
98
99 ui_file_rewind (gdb_dis_out);
100
101 pc = pc + TARGET_PRINT_INSN (pc, &asm_info);
102
103 lines->insn = xstrdup (tui_file_get_strbuf (gdb_dis_out));
104
105 /* reset the buffer to empty */
106 ui_file_rewind (gdb_dis_out);
107 }
108 ui_file_delete (gdb_dis_out);
109 return pc;
110}
111
112/* Find the disassembly address that corresponds to FROM lines
113 above or below the PC. Variable sized instructions are taken
114 into account by the algorithm. */
115static CORE_ADDR
116tui_find_disassembly_address (CORE_ADDR pc, int from)
117{
118 register CORE_ADDR newLow;
119 int maxLines;
120 int i;
121 struct tui_asm_line* lines;
122
123 maxLines = (from > 0) ? from : - from;
124 if (maxLines <= 1)
125 return pc;
126
127 lines = (struct tui_asm_line*) alloca (sizeof (struct tui_asm_line)
128 * maxLines);
129 memset (lines, 0, sizeof (struct tui_asm_line) * maxLines);
130
131 newLow = pc;
132 if (from > 0)
133 {
134 tui_disassemble (lines, pc, maxLines);
135 newLow = lines[maxLines - 1].addr;
136 }
137 else
138 {
139 CORE_ADDR last_addr;
140 int pos;
141 struct minimal_symbol* msymbol;
142
143 /* Find backward an address which is a symbol
144 and for which disassembling from that address will fill
145 completely the window. */
146 pos = maxLines - 1;
147 do {
148 newLow -= 1 * maxLines;
149 msymbol = lookup_minimal_symbol_by_pc_section (newLow, 0);
150
151 if (msymbol)
152 newLow = SYMBOL_VALUE_ADDRESS (msymbol);
153 else
154 newLow += 1 * maxLines;
155
156 tui_disassemble (lines, newLow, maxLines);
157 last_addr = lines[pos].addr;
158 } while (last_addr > pc && msymbol);
159
160 /* Scan forward disassembling one instruction at a time
161 until the last visible instruction of the window
162 matches the pc. We keep the disassembled instructions
163 in the 'lines' window and shift it downward (increasing
164 its addresses). */
165 if (last_addr < pc)
166 do
167 {
168 CORE_ADDR next_addr;
169
170 pos++;
171 if (pos >= maxLines)
172 pos = 0;
173
174 next_addr = tui_disassemble (&lines[pos], last_addr, 1);
175
176 /* If there are some problems while disassembling exit. */
177 if (next_addr <= last_addr)
178 break;
179 last_addr = next_addr;
180 } while (last_addr <= pc);
181 pos++;
182 if (pos >= maxLines)
183 pos = 0;
184 newLow = lines[pos].addr;
185 }
186 for (i = 0; i < maxLines; i++)
187 {
188 xfree (lines[i].addr_string);
189 xfree (lines[i].insn);
190 }
191 return newLow;
192}
193
194/* Function to set the disassembly window's content. */
c906108c 195TuiStatus
aec2f747 196tuiSetDisassemContent (CORE_ADDR pc)
c906108c
SS
197{
198 TuiStatus ret = TUI_FAILURE;
aec2f747
SC
199 register int i;
200 register int offset = disassemWin->detail.sourceInfo.horizontalOffset;
201 register int lineWidth, maxLines;
202 CORE_ADDR cur_pc;
203 TuiGenWinInfoPtr locator = locatorWinInfoPtr ();
204 int tab_len = tuiDefaultTabLen ();
205 struct tui_asm_line* lines;
206 int insn_pos;
207 int addr_size, max_size;
208 char* line;
209
210 if (pc == 0)
211 return TUI_FAILURE;
212
213 ret = tuiAllocSourceBuffer (disassemWin);
214 if (ret != TUI_SUCCESS)
215 return ret;
216
217 disassemWin->detail.sourceInfo.startLineOrAddr.addr = pc;
218 cur_pc = (CORE_ADDR)
219 (((TuiWinElementPtr) locator->content[0])->whichElement.locator.addr);
220
221 maxLines = disassemWin->generic.height - 2; /* account for hilite */
222
223 /* Get temporary table that will hold all strings (addr & insn). */
224 lines = (struct tui_asm_line*) alloca (sizeof (struct tui_asm_line)
225 * maxLines);
226 memset (lines, 0, sizeof (struct tui_asm_line) * maxLines);
227
228 lineWidth = disassemWin->generic.width - 1;
229
230 tui_disassemble (lines, pc, maxLines);
231
232 /* See what is the maximum length of an address and of a line. */
233 addr_size = 0;
234 max_size = 0;
235 for (i = 0; i < maxLines; i++)
c906108c 236 {
aec2f747
SC
237 size_t len = strlen (lines[i].addr_string);
238 if (len > addr_size)
239 addr_size = len;
c906108c 240
aec2f747
SC
241 len = strlen (lines[i].insn) + tab_len;
242 if (len > max_size)
243 max_size = len;
c906108c 244 }
aec2f747
SC
245 max_size += addr_size + tab_len;
246
247 /* Allocate memory to create each line. */
248 line = (char*) alloca (max_size);
249 insn_pos = (1 + (addr_size / tab_len)) * tab_len;
250
251 /* Now construct each line */
252 for (i = 0; i < maxLines; i++)
253 {
254 TuiWinElementPtr element;
255 TuiSourceElement* src;
256 int curLen;
257
258 element = (TuiWinElementPtr) disassemWin->generic.content[i];
259 src = &element->whichElement.source;
260 strcpy (line, lines[i].addr_string);
261 curLen = strlen (line);
262
263 /* Add spaces to make the instructions start on the same column */
264 while (curLen < insn_pos)
265 {
266 strcat (line, " ");
267 curLen++;
268 }
269
270 strcat (line, lines[i].insn);
271
272 /* Now copy the line taking the offset into account */
273 if (strlen (line) > offset)
274 strcpy (src->line, &line[offset]);
275 else
276 src->line[0] = '\0';
277
278 src->lineOrAddr.addr = lines[i].addr;
279 src->isExecPoint = lines[i].addr == cur_pc;
280
281 /* See whether there is a breakpoint installed. */
282 src->hasBreak = (!src->isExecPoint
283 && breakpoint_here_p (pc) != no_breakpoint_here);
c906108c 284
aec2f747
SC
285 xfree (lines[i].addr_string);
286 xfree (lines[i].insn);
287 }
288 disassemWin->generic.contentSize = i;
289 return TUI_SUCCESS;
290}
c906108c
SS
291
292
293/*
c5aa993b
JM
294 ** tuiShowDisassem().
295 ** Function to display the disassembly window with disassembled code.
296 */
c906108c 297void
c774cec6 298tuiShowDisassem (CORE_ADDR startAddr)
c906108c 299{
c774cec6 300 struct symtab *s = find_pc_symtab (startAddr);
c906108c 301 TuiWinInfoPtr winWithFocus = tuiWinWithFocus ();
a4b99e53 302 TuiLineOrAddress val;
c906108c 303
a4b99e53 304 val.addr = startAddr;
c906108c 305 tuiAddWinToLayout (DISASSEM_WIN);
a4b99e53 306 tuiUpdateSourceWindow (disassemWin, s, val, FALSE);
c906108c 307 /*
c5aa993b
JM
308 ** if the focus was in the src win, put it in the asm win, if the
309 ** source view isn't split
310 */
c906108c
SS
311 if (currentLayout () != SRC_DISASSEM_COMMAND && winWithFocus == srcWin)
312 tuiSetWinFocusTo (disassemWin);
313
314 return;
315} /* tuiShowDisassem */
316
317
318/*
c5aa993b
JM
319 ** tuiShowDisassemAndUpdateSource().
320 ** Function to display the disassembly window.
321 */
c906108c 322void
c774cec6 323tuiShowDisassemAndUpdateSource (CORE_ADDR startAddr)
c906108c
SS
324{
325 struct symtab_and_line sal;
326
327 tuiShowDisassem (startAddr);
328 if (currentLayout () == SRC_DISASSEM_COMMAND)
329 {
a4b99e53 330 TuiLineOrAddress val;
aec2f747 331
c906108c 332 /*
c5aa993b
JM
333 ** Update what is in the source window if it is displayed too,
334 ** note that it follows what is in the disassembly window and visa-versa
335 */
c774cec6 336 sal = find_pc_line (startAddr, 0);
a4b99e53
SC
337 val.lineNo = sal.line;
338 tuiUpdateSourceWindow (srcWin, sal.symtab, val, TRUE);
3024f13a
SC
339 if (sal.symtab)
340 {
52575520 341 set_current_source_symtab_and_line (&sal);
3024f13a
SC
342 tuiUpdateLocatorFilename (sal.symtab->filename);
343 }
344 else
345 tuiUpdateLocatorFilename ("?");
c906108c
SS
346 }
347
348 return;
349} /* tuiShowDisassemAndUpdateSource */
350
c906108c 351/*
c5aa993b
JM
352 ** tuiGetBeginAsmAddress().
353 */
c774cec6 354CORE_ADDR
c906108c 355tuiGetBeginAsmAddress (void)
c906108c
SS
356{
357 TuiGenWinInfoPtr locator;
358 TuiLocatorElementPtr element;
c774cec6 359 CORE_ADDR addr;
c906108c
SS
360
361 locator = locatorWinInfoPtr ();
362 element = &((TuiWinElementPtr) locator->content[0])->whichElement.locator;
363
c774cec6 364 if (element->addr == 0)
c906108c 365 {
0510ab86
SC
366 struct minimal_symbol *main_symbol;
367
368 /* Find address of the start of program.
369 Note: this should be language specific. */
370 main_symbol = lookup_minimal_symbol ("main", NULL, NULL);
371 if (main_symbol == 0)
372 main_symbol = lookup_minimal_symbol ("MAIN", NULL, NULL);
373 if (main_symbol == 0)
374 main_symbol = lookup_minimal_symbol ("_start", NULL, NULL);
375 if (main_symbol)
376 addr = SYMBOL_VALUE_ADDRESS (main_symbol);
377 else
378 addr = 0;
c906108c
SS
379 }
380 else /* the target is executing */
381 addr = element->addr;
382
383 return addr;
aec2f747 384} /* tuiGetBeginAsmAddress */
c906108c 385
77cad3ba
SC
386/* Determine what the low address will be to display in the TUI's
387 disassembly window. This may or may not be the same as the
388 low address input. */
389CORE_ADDR
390tuiGetLowDisassemblyAddress (CORE_ADDR low, CORE_ADDR pc)
391{
392 int pos;
393
394 /* Determine where to start the disassembly so that the pc is about in the
395 middle of the viewport. */
396 pos = tuiDefaultWinViewportHeight (DISASSEM_WIN, DISASSEM_COMMAND) / 2;
397 pc = tui_find_disassembly_address (pc, -pos);
398
399 if (pc < low)
400 pc = low;
401 return pc;
402}
403
c906108c 404/*
c5aa993b
JM
405 ** tuiVerticalDisassemScroll().
406 ** Scroll the disassembly forward or backward vertically
407 */
c906108c 408void
eca6576c
SC
409tuiVerticalDisassemScroll (TuiScrollDirection scrollDirection,
410 int numToScroll)
c906108c
SS
411{
412 if (disassemWin->generic.content != (OpaquePtr) NULL)
413 {
aec2f747 414 CORE_ADDR pc;
c906108c
SS
415 TuiWinContent content;
416 struct symtab *s;
aec2f747
SC
417 TuiLineOrAddress val;
418 int maxLines, dir;
52575520 419 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
c906108c
SS
420
421 content = (TuiWinContent) disassemWin->generic.content;
52575520 422 if (cursal.symtab == (struct symtab *) NULL)
c906108c
SS
423 s = find_pc_symtab (selected_frame->pc);
424 else
52575520 425 s = cursal.symtab;
c906108c 426
aec2f747
SC
427 /* account for hilite */
428 maxLines = disassemWin->generic.height - 2;
c906108c 429 pc = content[0]->whichElement.source.lineOrAddr.addr;
aec2f747 430 dir = (scrollDirection == FORWARD_SCROLL) ? maxLines : - maxLines;
c906108c 431
aec2f747
SC
432 val.addr = tui_find_disassembly_address (pc, dir);
433 tuiUpdateSourceWindowAsIs (disassemWin, s, val, FALSE);
434 }
435}
This page took 0.31142 seconds and 4 git commands to generate.