Whoops, forgot to commit this yesterday:
[deliverable/binutils-gdb.git] / gdb / tui / tuiDisassem.c
1 /* Disassembly display.
2 Copyright 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
3 Contributed by Hewlett-Packard Company.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 #include "defs.h"
23 #include "symtab.h"
24 #include "breakpoint.h"
25 #include "frame.h"
26 #include "value.h"
27
28 #include "tui.h"
29 #include "tuiData.h"
30 #include "tuiWin.h"
31 #include "tuiLayout.h"
32 #include "tuiSourceWin.h"
33 #include "tuiStack.h"
34 #include "tui-file.h"
35
36
37 /*****************************************
38 ** STATIC LOCAL FUNCTIONS FORWARD DECLS **
39 ******************************************/
40
41 static struct breakpoint *_hasBreak (CORE_ADDR);
42
43
44 /*****************************************
45 ** PUBLIC FUNCTIONS **
46 ******************************************/
47
48 /*
49 ** tuiSetDisassemContent().
50 ** Function to set the disassembly window's content.
51 */
52 TuiStatus
53 tuiSetDisassemContent (struct symtab *s, CORE_ADDR startAddr)
54 {
55 TuiStatus ret = TUI_FAILURE;
56 struct ui_file *gdb_dis_out;
57
58 if (startAddr != 0)
59 {
60 register int i, desc;
61
62 if ((ret = tuiAllocSourceBuffer (disassemWin)) == TUI_SUCCESS)
63 {
64 register int offset = disassemWin->detail.sourceInfo.horizontalOffset;
65 register int threshold, curLine = 0, lineWidth, maxLines;
66 CORE_ADDR newpc, pc;
67 disassemble_info asmInfo;
68 TuiGenWinInfoPtr locator = locatorWinInfoPtr ();
69 extern void strcat_address (CORE_ADDR, char *, int);
70 extern void strcat_address_numeric (CORE_ADDR, int, char *, int);
71 int curLen = 0;
72 int tab_len = tuiDefaultTabLen ();
73
74 maxLines = disassemWin->generic.height - 2; /* account for hilite */
75 lineWidth = disassemWin->generic.width - 1;
76 threshold = (lineWidth - 1) + offset;
77
78 /* now init the ui_file structure */
79 gdb_dis_out = tui_sfileopen (threshold);
80
81 asmInfo = tm_print_insn_info;
82 asmInfo.stream = gdb_dis_out;
83
84 disassemWin->detail.sourceInfo.startLineOrAddr.addr = startAddr;
85
86 /* Now construct each line */
87 for (curLine = 0, pc = startAddr; (curLine < maxLines);)
88 {
89 TuiWinElementPtr element = (TuiWinElementPtr) disassemWin->generic.content[curLine];
90 struct breakpoint *bp;
91
92 print_address (pc, gdb_dis_out);
93
94 curLen = strlen (tui_file_get_strbuf (gdb_dis_out));
95 i = curLen - ((curLen / tab_len) * tab_len);
96
97 /* adjust buffer length if necessary */
98 tui_file_adjust_strbuf ((tab_len - i > 0) ? (tab_len - i) : 0, gdb_dis_out);
99
100 /* Add spaces to make the instructions start onthe same column */
101 while (i < tab_len)
102 {
103 tui_file_get_strbuf (gdb_dis_out)[curLen] = ' ';
104 i++;
105 curLen++;
106 }
107 tui_file_get_strbuf (gdb_dis_out)[curLen] = '\0';
108
109 newpc = pc + ((*tm_print_insn) (pc, &asmInfo));
110
111 /* Now copy the line taking the offset into account */
112 if (strlen (tui_file_get_strbuf (gdb_dis_out)) > offset)
113 strcpy (element->whichElement.source.line,
114 &(tui_file_get_strbuf (gdb_dis_out)[offset]));
115 else
116 element->whichElement.source.line[0] = '\0';
117 element->whichElement.source.lineOrAddr.addr = pc;
118 element->whichElement.source.isExecPoint =
119 (pc == (CORE_ADDR) ((TuiWinElementPtr) locator->content[0])->whichElement.locator.addr);
120 bp = _hasBreak (pc);
121 element->whichElement.source.hasBreak =
122 (bp != (struct breakpoint *) NULL &&
123 (!element->whichElement.source.isExecPoint ||
124 (bp->disposition != disp_del || bp->hit_count <= 0)));
125 curLine++;
126 pc = newpc;
127 /* reset the buffer to empty */
128 tui_file_get_strbuf (gdb_dis_out)[0] = '\0';
129 }
130 ui_file_delete (gdb_dis_out);
131 gdb_dis_out = NULL;
132 disassemWin->generic.contentSize = curLine;
133 ret = TUI_SUCCESS;
134 }
135 }
136
137 return ret;
138 } /* tuiSetDisassemContent */
139
140
141 /*
142 ** tuiShowDisassem().
143 ** Function to display the disassembly window with disassembled code.
144 */
145 void
146 tuiShowDisassem (CORE_ADDR startAddr)
147 {
148 struct symtab *s = find_pc_symtab (startAddr);
149 TuiWinInfoPtr winWithFocus = tuiWinWithFocus ();
150 TuiLineOrAddress val;
151
152 val.addr = startAddr;
153 tuiAddWinToLayout (DISASSEM_WIN);
154 tuiUpdateSourceWindow (disassemWin, s, val, FALSE);
155 /*
156 ** if the focus was in the src win, put it in the asm win, if the
157 ** source view isn't split
158 */
159 if (currentLayout () != SRC_DISASSEM_COMMAND && winWithFocus == srcWin)
160 tuiSetWinFocusTo (disassemWin);
161
162 return;
163 } /* tuiShowDisassem */
164
165
166 /*
167 ** tuiShowDisassemAndUpdateSource().
168 ** Function to display the disassembly window.
169 */
170 void
171 tuiShowDisassemAndUpdateSource (CORE_ADDR startAddr)
172 {
173 struct symtab_and_line sal;
174
175 tuiShowDisassem (startAddr);
176 if (currentLayout () == SRC_DISASSEM_COMMAND)
177 {
178 TuiLineOrAddress val;
179 TuiGenWinInfoPtr locator = locatorWinInfoPtr ();
180 /*
181 ** Update what is in the source window if it is displayed too,
182 ** note that it follows what is in the disassembly window and visa-versa
183 */
184 sal = find_pc_line (startAddr, 0);
185 val.lineNo = sal.line;
186 tuiUpdateSourceWindow (srcWin, sal.symtab, val, TRUE);
187 if (sal.symtab)
188 {
189 current_source_symtab = sal.symtab;
190 tuiUpdateLocatorFilename (sal.symtab->filename);
191 }
192 else
193 tuiUpdateLocatorFilename ("?");
194 }
195
196 return;
197 } /* tuiShowDisassemAndUpdateSource */
198
199 /*
200 ** tuiGetBeginAsmAddress().
201 */
202 CORE_ADDR
203 tuiGetBeginAsmAddress (void)
204 {
205 TuiGenWinInfoPtr locator;
206 TuiLocatorElementPtr element;
207 CORE_ADDR addr;
208
209 locator = locatorWinInfoPtr ();
210 element = &((TuiWinElementPtr) locator->content[0])->whichElement.locator;
211
212 if (element->addr == 0)
213 {
214 /*the target is not executing, because the pc is 0 */
215
216 addr = parse_and_eval_address ("main");
217
218 if (addr == 0)
219 addr = parse_and_eval_address ("MAIN");
220
221 }
222 else /* the target is executing */
223 addr = element->addr;
224
225 return addr;
226 } /* tuiGetBeginAsmAddress */
227
228
229 /*
230 ** tuiVerticalDisassemScroll().
231 ** Scroll the disassembly forward or backward vertically
232 */
233 void
234 tuiVerticalDisassemScroll (TuiScrollDirection scrollDirection,
235 int numToScroll)
236 {
237 if (disassemWin->generic.content != (OpaquePtr) NULL)
238 {
239 CORE_ADDR pc, lowAddr;
240 TuiWinContent content;
241 struct symtab *s;
242
243 content = (TuiWinContent) disassemWin->generic.content;
244 if (current_source_symtab == (struct symtab *) NULL)
245 s = find_pc_symtab (selected_frame->pc);
246 else
247 s = current_source_symtab;
248
249 pc = content[0]->whichElement.source.lineOrAddr.addr;
250 if (find_pc_partial_function (pc, (char **) NULL, &lowAddr,
251 (CORE_ADDR) 0) == 0)
252 error ("No function contains program counter for selected frame.\n");
253 else
254 {
255 register int line = 0;
256 register CORE_ADDR newLow;
257 bfd_byte buffer[4];
258 TuiLineOrAddress val;
259
260 newLow = pc;
261 if (scrollDirection == FORWARD_SCROLL)
262 {
263 for (; line < numToScroll; line++)
264 newLow += sizeof (bfd_getb32 (buffer));
265 }
266 else
267 {
268 for (; newLow != 0 && line < numToScroll; line++)
269 newLow -= sizeof (bfd_getb32 (buffer));
270 }
271 val.addr = newLow;
272 tuiUpdateSourceWindowAsIs (disassemWin, s, val, FALSE);
273 }
274 }
275
276 return;
277 } /* tuiVerticalDisassemScroll */
278
279
280
281 /*****************************************
282 ** STATIC LOCAL FUNCTIONS **
283 ******************************************/
284 /*
285 ** _hasBreak().
286 ** Answer whether there is a break point at the input line in the
287 ** source file indicated
288 */
289 static struct breakpoint *
290 _hasBreak (CORE_ADDR addr)
291 {
292 struct breakpoint *bpWithBreak = (struct breakpoint *) NULL;
293 struct breakpoint *bp;
294 extern struct breakpoint *breakpoint_chain;
295
296
297 for (bp = breakpoint_chain;
298 (bp != (struct breakpoint *) NULL &&
299 bpWithBreak == (struct breakpoint *) NULL);
300 bp = bp->next)
301 if (addr == bp->address)
302 bpWithBreak = bp;
303
304 return bpWithBreak;
305 } /* _hasBreak */
This page took 0.035631 seconds and 4 git commands to generate.