1 /* Disassembly display.
3 Copyright 1998, 1999, 2000, 2001, 2002 Free Software Foundation,
6 Contributed by Hewlett-Packard Company.
8 This file is part of GDB.
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.
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.
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. */
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>
44 #include "breakpoint.h"
52 #include "tuiLayout.h"
53 #include "tuiSourceWin.h"
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. */
68 tui_disassemble (struct tui_asm_line
* lines
, CORE_ADDR pc
, int count
)
70 struct ui_file
*gdb_dis_out
;
71 disassemble_info asm_info
;
73 /* now init the ui_file structure */
74 gdb_dis_out
= tui_sfileopen (256);
76 memcpy (&asm_info
, TARGET_PRINT_INSN_INFO
, sizeof (asm_info
));
77 asm_info
.stream
= gdb_dis_out
;
79 if (TARGET_BYTE_ORDER
== BFD_ENDIAN_BIG
)
80 asm_info
.endian
= BFD_ENDIAN_BIG
;
82 asm_info
.endian
= BFD_ENDIAN_LITTLE
;
84 if (TARGET_ARCHITECTURE
!= NULL
)
85 asm_info
.mach
= TARGET_ARCHITECTURE
->mach
;
87 /* Now construct each line */
88 for (; count
> 0; count
--, lines
++)
90 if (lines
->addr_string
)
91 xfree (lines
->addr_string
);
95 print_address (pc
, gdb_dis_out
);
97 lines
->addr_string
= xstrdup (tui_file_get_strbuf (gdb_dis_out
));
99 ui_file_rewind (gdb_dis_out
);
101 pc
= pc
+ TARGET_PRINT_INSN (pc
, &asm_info
);
103 lines
->insn
= xstrdup (tui_file_get_strbuf (gdb_dis_out
));
105 /* reset the buffer to empty */
106 ui_file_rewind (gdb_dis_out
);
108 ui_file_delete (gdb_dis_out
);
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. */
116 tui_find_disassembly_address (CORE_ADDR pc
, int from
)
118 register CORE_ADDR newLow
;
121 struct tui_asm_line
* lines
;
123 maxLines
= (from
> 0) ? from
: - from
;
127 lines
= (struct tui_asm_line
*) alloca (sizeof (struct tui_asm_line
)
129 memset (lines
, 0, sizeof (struct tui_asm_line
) * maxLines
);
134 tui_disassemble (lines
, pc
, maxLines
);
135 newLow
= lines
[maxLines
- 1].addr
;
141 struct minimal_symbol
* msymbol
;
143 /* Find backward an address which is a symbol
144 and for which disassembling from that address will fill
145 completely the window. */
148 newLow
-= 1 * maxLines
;
149 msymbol
= lookup_minimal_symbol_by_pc_section (newLow
, 0);
152 newLow
= SYMBOL_VALUE_ADDRESS (msymbol
);
154 newLow
+= 1 * maxLines
;
156 tui_disassemble (lines
, newLow
, maxLines
);
157 last_addr
= lines
[pos
].addr
;
158 } while (last_addr
> pc
&& msymbol
);
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
174 next_addr
= tui_disassemble (&lines
[pos
], last_addr
, 1);
176 /* If there are some problems while disassembling exit. */
177 if (next_addr
<= last_addr
)
179 last_addr
= next_addr
;
180 } while (last_addr
<= pc
);
184 newLow
= lines
[pos
].addr
;
186 for (i
= 0; i
< maxLines
; i
++)
188 xfree (lines
[i
].addr_string
);
189 xfree (lines
[i
].insn
);
194 /* Function to set the disassembly window's content. */
196 tuiSetDisassemContent (CORE_ADDR pc
)
198 TuiStatus ret
= TUI_FAILURE
;
200 register int offset
= disassemWin
->detail
.sourceInfo
.horizontalOffset
;
201 register int lineWidth
, maxLines
;
203 TuiGenWinInfoPtr locator
= locatorWinInfoPtr ();
204 int tab_len
= tuiDefaultTabLen ();
205 struct tui_asm_line
* lines
;
207 int addr_size
, max_size
;
213 ret
= tuiAllocSourceBuffer (disassemWin
);
214 if (ret
!= TUI_SUCCESS
)
217 disassemWin
->detail
.sourceInfo
.startLineOrAddr
.addr
= pc
;
219 (((TuiWinElementPtr
) locator
->content
[0])->whichElement
.locator
.addr
);
221 maxLines
= disassemWin
->generic
.height
- 2; /* account for hilite */
223 /* Get temporary table that will hold all strings (addr & insn). */
224 lines
= (struct tui_asm_line
*) alloca (sizeof (struct tui_asm_line
)
226 memset (lines
, 0, sizeof (struct tui_asm_line
) * maxLines
);
228 lineWidth
= disassemWin
->generic
.width
- 1;
230 tui_disassemble (lines
, pc
, maxLines
);
232 /* See what is the maximum length of an address and of a line. */
235 for (i
= 0; i
< maxLines
; i
++)
237 size_t len
= strlen (lines
[i
].addr_string
);
241 len
= strlen (lines
[i
].insn
) + tab_len
;
245 max_size
+= addr_size
+ tab_len
;
247 /* Allocate memory to create each line. */
248 line
= (char*) alloca (max_size
);
249 insn_pos
= (1 + (addr_size
/ tab_len
)) * tab_len
;
251 /* Now construct each line */
252 for (i
= 0; i
< maxLines
; i
++)
254 TuiWinElementPtr element
;
255 TuiSourceElement
* src
;
258 element
= (TuiWinElementPtr
) disassemWin
->generic
.content
[i
];
259 src
= &element
->whichElement
.source
;
260 strcpy (line
, lines
[i
].addr_string
);
261 curLen
= strlen (line
);
263 /* Add spaces to make the instructions start on the same column */
264 while (curLen
< insn_pos
)
270 strcat (line
, lines
[i
].insn
);
272 /* Now copy the line taking the offset into account */
273 if (strlen (line
) > offset
)
274 strcpy (src
->line
, &line
[offset
]);
278 src
->lineOrAddr
.addr
= lines
[i
].addr
;
279 src
->isExecPoint
= lines
[i
].addr
== cur_pc
;
281 /* See whether there is a breakpoint installed. */
282 src
->hasBreak
= (!src
->isExecPoint
283 && breakpoint_here_p (pc
) != no_breakpoint_here
);
285 xfree (lines
[i
].addr_string
);
286 xfree (lines
[i
].insn
);
288 disassemWin
->generic
.contentSize
= i
;
294 ** tuiShowDisassem().
295 ** Function to display the disassembly window with disassembled code.
298 tuiShowDisassem (CORE_ADDR startAddr
)
300 struct symtab
*s
= find_pc_symtab (startAddr
);
301 TuiWinInfoPtr winWithFocus
= tuiWinWithFocus ();
302 TuiLineOrAddress val
;
304 val
.addr
= startAddr
;
305 tuiAddWinToLayout (DISASSEM_WIN
);
306 tuiUpdateSourceWindow (disassemWin
, s
, val
, FALSE
);
308 ** if the focus was in the src win, put it in the asm win, if the
309 ** source view isn't split
311 if (currentLayout () != SRC_DISASSEM_COMMAND
&& winWithFocus
== srcWin
)
312 tuiSetWinFocusTo (disassemWin
);
315 } /* tuiShowDisassem */
319 ** tuiShowDisassemAndUpdateSource().
320 ** Function to display the disassembly window.
323 tuiShowDisassemAndUpdateSource (CORE_ADDR startAddr
)
325 struct symtab_and_line sal
;
327 tuiShowDisassem (startAddr
);
328 if (currentLayout () == SRC_DISASSEM_COMMAND
)
330 TuiLineOrAddress val
;
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
336 sal
= find_pc_line (startAddr
, 0);
337 val
.lineNo
= sal
.line
;
338 tuiUpdateSourceWindow (srcWin
, sal
.symtab
, val
, TRUE
);
341 set_current_source_symtab_and_line (&sal
);
342 tuiUpdateLocatorFilename (sal
.symtab
->filename
);
345 tuiUpdateLocatorFilename ("?");
349 } /* tuiShowDisassemAndUpdateSource */
352 ** tuiGetBeginAsmAddress().
355 tuiGetBeginAsmAddress (void)
357 TuiGenWinInfoPtr locator
;
358 TuiLocatorElementPtr element
;
361 locator
= locatorWinInfoPtr ();
362 element
= &((TuiWinElementPtr
) locator
->content
[0])->whichElement
.locator
;
364 if (element
->addr
== 0)
366 struct minimal_symbol
*main_symbol
;
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
);
376 addr
= SYMBOL_VALUE_ADDRESS (main_symbol
);
380 else /* the target is executing */
381 addr
= element
->addr
;
384 } /* tuiGetBeginAsmAddress */
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. */
390 tuiGetLowDisassemblyAddress (CORE_ADDR low
, CORE_ADDR pc
)
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
);
405 ** tuiVerticalDisassemScroll().
406 ** Scroll the disassembly forward or backward vertically
409 tuiVerticalDisassemScroll (TuiScrollDirection scrollDirection
,
412 if (disassemWin
->generic
.content
!= (OpaquePtr
) NULL
)
415 TuiWinContent content
;
417 TuiLineOrAddress val
;
419 struct symtab_and_line cursal
= get_current_source_symtab_and_line ();
421 content
= (TuiWinContent
) disassemWin
->generic
.content
;
422 if (cursal
.symtab
== (struct symtab
*) NULL
)
423 s
= find_pc_symtab (selected_frame
->pc
);
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
;
432 val
.addr
= tui_find_disassembly_address (pc
, dir
);
433 tuiUpdateSourceWindowAsIs (disassemWin
, s
, val
, FALSE
);