*** empty log message ***
[deliverable/binutils-gdb.git] / gdb / tui / tuiDisassem.c
1 /* Disassembly display.
2
3 Copyright 1998, 1999, 2000, 2001, 2002 Free Software Foundation,
4 Inc.
5
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. */
24
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
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
42 #include "defs.h"
43 #include "symtab.h"
44 #include "breakpoint.h"
45 #include "frame.h"
46 #include "value.h"
47 #include "source.h"
48
49 #include "tui.h"
50 #include "tuiData.h"
51 #include "tuiWin.h"
52 #include "tuiLayout.h"
53 #include "tuiSourceWin.h"
54 #include "tuiStack.h"
55 #include "tui-file.h"
56
57 struct 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. */
67 static CORE_ADDR
68 tui_disassemble (struct tui_asm_line* lines, CORE_ADDR pc, int count)
69 {
70 struct ui_file *gdb_dis_out;
71 disassemble_info asm_info;
72
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. */
115 static CORE_ADDR
116 tui_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. */
195 TuiStatus
196 tuiSetDisassemContent (CORE_ADDR pc)
197 {
198 TuiStatus ret = TUI_FAILURE;
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++)
236 {
237 size_t len = strlen (lines[i].addr_string);
238 if (len > addr_size)
239 addr_size = len;
240
241 len = strlen (lines[i].insn) + tab_len;
242 if (len > max_size)
243 max_size = len;
244 }
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);
284
285 xfree (lines[i].addr_string);
286 xfree (lines[i].insn);
287 }
288 disassemWin->generic.contentSize = i;
289 return TUI_SUCCESS;
290 }
291
292
293 /*
294 ** tuiShowDisassem().
295 ** Function to display the disassembly window with disassembled code.
296 */
297 void
298 tuiShowDisassem (CORE_ADDR startAddr)
299 {
300 struct symtab *s = find_pc_symtab (startAddr);
301 TuiWinInfoPtr winWithFocus = tuiWinWithFocus ();
302 TuiLineOrAddress val;
303
304 val.addr = startAddr;
305 tuiAddWinToLayout (DISASSEM_WIN);
306 tuiUpdateSourceWindow (disassemWin, s, val, FALSE);
307 /*
308 ** if the focus was in the src win, put it in the asm win, if the
309 ** source view isn't split
310 */
311 if (currentLayout () != SRC_DISASSEM_COMMAND && winWithFocus == srcWin)
312 tuiSetWinFocusTo (disassemWin);
313
314 return;
315 } /* tuiShowDisassem */
316
317
318 /*
319 ** tuiShowDisassemAndUpdateSource().
320 ** Function to display the disassembly window.
321 */
322 void
323 tuiShowDisassemAndUpdateSource (CORE_ADDR startAddr)
324 {
325 struct symtab_and_line sal;
326
327 tuiShowDisassem (startAddr);
328 if (currentLayout () == SRC_DISASSEM_COMMAND)
329 {
330 TuiLineOrAddress val;
331
332 /*
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 */
336 sal = find_pc_line (startAddr, 0);
337 val.lineNo = sal.line;
338 tuiUpdateSourceWindow (srcWin, sal.symtab, val, TRUE);
339 if (sal.symtab)
340 {
341 set_current_source_symtab_and_line (&sal);
342 tuiUpdateLocatorFilename (sal.symtab->filename);
343 }
344 else
345 tuiUpdateLocatorFilename ("?");
346 }
347
348 return;
349 } /* tuiShowDisassemAndUpdateSource */
350
351 /*
352 ** tuiGetBeginAsmAddress().
353 */
354 CORE_ADDR
355 tuiGetBeginAsmAddress (void)
356 {
357 TuiGenWinInfoPtr locator;
358 TuiLocatorElementPtr element;
359 CORE_ADDR addr;
360
361 locator = locatorWinInfoPtr ();
362 element = &((TuiWinElementPtr) locator->content[0])->whichElement.locator;
363
364 if (element->addr == 0)
365 {
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;
379 }
380 else /* the target is executing */
381 addr = element->addr;
382
383 return addr;
384 } /* tuiGetBeginAsmAddress */
385
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. */
389 CORE_ADDR
390 tuiGetLowDisassemblyAddress (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
404 /*
405 ** tuiVerticalDisassemScroll().
406 ** Scroll the disassembly forward or backward vertically
407 */
408 void
409 tuiVerticalDisassemScroll (TuiScrollDirection scrollDirection,
410 int numToScroll)
411 {
412 if (disassemWin->generic.content != (OpaquePtr) NULL)
413 {
414 CORE_ADDR pc;
415 TuiWinContent content;
416 struct symtab *s;
417 TuiLineOrAddress val;
418 int maxLines, dir;
419 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
420
421 content = (TuiWinContent) disassemWin->generic.content;
422 if (cursal.symtab == (struct symtab *) NULL)
423 s = find_pc_symtab (deprecated_selected_frame->pc);
424 else
425 s = cursal.symtab;
426
427 /* account for hilite */
428 maxLines = disassemWin->generic.height - 2;
429 pc = content[0]->whichElement.source.lineOrAddr.addr;
430 dir = (scrollDirection == FORWARD_SCROLL) ? maxLines : - maxLines;
431
432 val.addr = tui_find_disassembly_address (pc, dir);
433 tuiUpdateSourceWindowAsIs (disassemWin, s, val, FALSE);
434 }
435 }
This page took 0.038609 seconds and 4 git commands to generate.