2008-02-21 Pedro Alves <pedro@codesorcery.com>
[external/binutils.git] / gdb / tui / tui-disasm.c
1 /* Disassembly display.
2
3    Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2007, 2008, 2009
4    Free Software Foundation, 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 3 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, see <http://www.gnu.org/licenses/>.  */
22
23 #include "defs.h"
24 #include "symtab.h"
25 #include "breakpoint.h"
26 #include "frame.h"
27 #include "value.h"
28 #include "source.h"
29 #include "disasm.h"
30 #include "gdb_string.h"
31 #include "tui/tui.h"
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"
38 #include "tui/tui-disasm.h"
39
40 #include "gdb_curses.h"
41
42 struct tui_asm_line 
43 {
44   CORE_ADDR addr;
45   char *addr_string;
46   char *insn;
47 };
48
49 /* Function to set the disassembly window's content.
50    Disassemble count lines starting at pc.
51    Return address of the count'th instruction after pc.  */
52 static CORE_ADDR
53 tui_disassemble (struct tui_asm_line *asm_lines, 
54                  CORE_ADDR pc, int count)
55 {
56   struct ui_file *gdb_dis_out;
57
58   /* Now init the ui_file structure.  */
59   gdb_dis_out = tui_sfileopen (256);
60
61   /* Now construct each line.  */
62   for (; count > 0; count--, asm_lines++)
63     {
64       if (asm_lines->addr_string)
65         xfree (asm_lines->addr_string);
66       if (asm_lines->insn)
67         xfree (asm_lines->insn);
68       
69       print_address (pc, gdb_dis_out);
70       asm_lines->addr = pc;
71       asm_lines->addr_string = xstrdup (tui_file_get_strbuf (gdb_dis_out));
72
73       ui_file_rewind (gdb_dis_out);
74
75       pc = pc + gdb_print_insn (pc, gdb_dis_out, NULL);
76
77       asm_lines->insn = xstrdup (tui_file_get_strbuf (gdb_dis_out));
78
79       /* Reset the buffer to empty.  */
80       ui_file_rewind (gdb_dis_out);
81     }
82   ui_file_delete (gdb_dis_out);
83   return pc;
84 }
85
86 /* Find the disassembly address that corresponds to FROM lines above
87    or below the PC.  Variable sized instructions are taken into
88    account by the algorithm.  */
89 static CORE_ADDR
90 tui_find_disassembly_address (CORE_ADDR pc, int from)
91 {
92   CORE_ADDR new_low;
93   int max_lines;
94   int i;
95   struct tui_asm_line *asm_lines;
96
97   max_lines = (from > 0) ? from : - from;
98   if (max_lines <= 1)
99      return pc;
100
101   asm_lines = (struct tui_asm_line*) alloca (sizeof (struct tui_asm_line)
102                                          * max_lines);
103   memset (asm_lines, 0, sizeof (struct tui_asm_line) * max_lines);
104
105   new_low = pc;
106   if (from > 0)
107     {
108       tui_disassemble (asm_lines, pc, max_lines);
109       new_low = asm_lines[max_lines - 1].addr;
110     }
111   else
112     {
113       CORE_ADDR last_addr;
114       int pos;
115       struct minimal_symbol *msymbol;
116               
117       /* Find backward an address which is a symbol and for which
118          disassembling from that address will fill completely the
119          window.  */
120       pos = max_lines - 1;
121       do {
122          new_low -= 1 * max_lines;
123          msymbol = lookup_minimal_symbol_by_pc_section (new_low, 0);
124
125          if (msymbol)
126             new_low = SYMBOL_VALUE_ADDRESS (msymbol);
127          else
128             new_low += 1 * max_lines;
129
130          tui_disassemble (asm_lines, new_low, max_lines);
131          last_addr = asm_lines[pos].addr;
132       } while (last_addr > pc && msymbol);
133
134       /* Scan forward disassembling one instruction at a time until
135          the last visible instruction of the window matches the pc.
136          We keep the disassembled instructions in the 'lines' window
137          and shift it downward (increasing its addresses).  */
138       if (last_addr < pc)
139         do
140           {
141             CORE_ADDR next_addr;
142                  
143             pos++;
144             if (pos >= max_lines)
145               pos = 0;
146
147             next_addr = tui_disassemble (&asm_lines[pos], last_addr, 1);
148
149             /* If there are some problems while disassembling exit.  */
150             if (next_addr <= last_addr)
151               break;
152             last_addr = next_addr;
153           } while (last_addr <= pc);
154       pos++;
155       if (pos >= max_lines)
156          pos = 0;
157       new_low = asm_lines[pos].addr;
158     }
159   for (i = 0; i < max_lines; i++)
160     {
161       xfree (asm_lines[i].addr_string);
162       xfree (asm_lines[i].insn);
163     }
164   return new_low;
165 }
166
167 /* Function to set the disassembly window's content.  */
168 enum tui_status
169 tui_set_disassem_content (CORE_ADDR pc)
170 {
171   enum tui_status ret = TUI_FAILURE;
172   int i;
173   int offset = TUI_DISASM_WIN->detail.source_info.horizontal_offset;
174   int line_width, max_lines;
175   CORE_ADDR cur_pc;
176   struct tui_gen_win_info *locator = tui_locator_win_info_ptr ();
177   int tab_len = tui_default_tab_len ();
178   struct tui_asm_line *asm_lines;
179   int insn_pos;
180   int addr_size, max_size;
181   char *line;
182   
183   if (pc == 0)
184     return TUI_FAILURE;
185
186   ret = tui_alloc_source_buffer (TUI_DISASM_WIN);
187   if (ret != TUI_SUCCESS)
188     return ret;
189
190   TUI_DISASM_WIN->detail.source_info.start_line_or_addr.loa = LOA_ADDRESS;
191   TUI_DISASM_WIN->detail.source_info.start_line_or_addr.u.addr = pc;
192   cur_pc = (CORE_ADDR)
193     (((struct tui_win_element *) locator->content[0])->which_element.locator.addr);
194
195   max_lines = TUI_DISASM_WIN->generic.height - 2;       /* Account for
196                                                            hilite.  */
197
198   /* Get temporary table that will hold all strings (addr & insn).  */
199   asm_lines = (struct tui_asm_line*) alloca (sizeof (struct tui_asm_line)
200                                          * max_lines);
201   memset (asm_lines, 0, sizeof (struct tui_asm_line) * max_lines);
202
203   line_width = TUI_DISASM_WIN->generic.width - 1;
204
205   tui_disassemble (asm_lines, pc, max_lines);
206
207   /* See what is the maximum length of an address and of a line.  */
208   addr_size = 0;
209   max_size = 0;
210   for (i = 0; i < max_lines; i++)
211     {
212       size_t len = strlen (asm_lines[i].addr_string);
213       if (len > addr_size)
214         addr_size = len;
215
216       len = strlen (asm_lines[i].insn) + tab_len;
217       if (len > max_size)
218         max_size = len;
219     }
220   max_size += addr_size + tab_len;
221
222   /* Allocate memory to create each line.  */
223   line = (char*) alloca (max_size);
224   insn_pos = (1 + (addr_size / tab_len)) * tab_len;
225
226   /* Now construct each line.  */
227   for (i = 0; i < max_lines; i++)
228     {
229       struct tui_win_element *element;
230       struct tui_source_element *src;
231       int cur_len;
232
233       element = (struct tui_win_element *) TUI_DISASM_WIN->generic.content[i];
234       src = &element->which_element.source;
235       strcpy (line, asm_lines[i].addr_string);
236       cur_len = strlen (line);
237
238       /* Add spaces to make the instructions start on the same
239          column.  */
240       while (cur_len < insn_pos)
241         {
242           strcat (line, " ");
243           cur_len++;
244         }
245
246       strcat (line, asm_lines[i].insn);
247
248       /* Now copy the line taking the offset into account.  */
249       if (strlen (line) > offset)
250         strcpy (src->line, &line[offset]);
251       else
252         src->line[0] = '\0';
253
254       src->line_or_addr.loa = LOA_ADDRESS;
255       src->line_or_addr.u.addr = asm_lines[i].addr;
256       src->is_exec_point = asm_lines[i].addr == cur_pc;
257
258       /* See whether there is a breakpoint installed.  */
259       src->has_break = (!src->is_exec_point
260                        && breakpoint_here_p (pc) != no_breakpoint_here);
261
262       xfree (asm_lines[i].addr_string);
263       xfree (asm_lines[i].insn);
264     }
265   TUI_DISASM_WIN->generic.content_size = i;
266   return TUI_SUCCESS;
267 }
268
269
270 /* Function to display the disassembly window with disassembled code.  */
271 void
272 tui_show_disassem (CORE_ADDR start_addr)
273 {
274   struct symtab *s = find_pc_symtab (start_addr);
275   struct tui_win_info *win_with_focus = tui_win_with_focus ();
276   struct tui_line_or_address val;
277
278   val.loa = LOA_ADDRESS;
279   val.u.addr = start_addr;
280   tui_add_win_to_layout (DISASSEM_WIN);
281   tui_update_source_window (TUI_DISASM_WIN, s, val, FALSE);
282
283   /* If the focus was in the src win, put it in the asm win, if the
284      source view isn't split.  */
285   if (tui_current_layout () != SRC_DISASSEM_COMMAND 
286       && win_with_focus == TUI_SRC_WIN)
287     tui_set_win_focus_to (TUI_DISASM_WIN);
288
289   return;
290 }
291
292
293 /* Function to display the disassembly window.  */
294 void
295 tui_show_disassem_and_update_source (CORE_ADDR start_addr)
296 {
297   struct symtab_and_line sal;
298
299   tui_show_disassem (start_addr);
300   if (tui_current_layout () == SRC_DISASSEM_COMMAND)
301     {
302       struct tui_line_or_address val;
303
304       /* Update what is in the source window if it is displayed too,
305          note that it follows what is in the disassembly window and
306          visa-versa.  */
307       sal = find_pc_line (start_addr, 0);
308       val.loa = LOA_LINE;
309       val.u.line_no = sal.line;
310       tui_update_source_window (TUI_SRC_WIN, sal.symtab, val, TRUE);
311       if (sal.symtab)
312         {
313           set_current_source_symtab_and_line (&sal);
314           tui_update_locator_filename (sal.symtab->filename);
315         }
316       else
317         tui_update_locator_filename ("?");
318     }
319
320   return;
321 }
322
323 CORE_ADDR
324 tui_get_begin_asm_address (void)
325 {
326   struct tui_gen_win_info *locator;
327   struct tui_locator_element *element;
328   CORE_ADDR addr;
329
330   locator = tui_locator_win_info_ptr ();
331   element = &((struct tui_win_element *) locator->content[0])->which_element.locator;
332
333   if (element->addr == 0)
334     {
335       struct minimal_symbol *main_symbol;
336
337       /* Find address of the start of program.
338          Note: this should be language specific.  */
339       main_symbol = lookup_minimal_symbol ("main", NULL, NULL);
340       if (main_symbol == 0)
341         main_symbol = lookup_minimal_symbol ("MAIN", NULL, NULL);
342       if (main_symbol == 0)
343         main_symbol = lookup_minimal_symbol ("_start", NULL, NULL);
344       if (main_symbol)
345         addr = SYMBOL_VALUE_ADDRESS (main_symbol);
346       else
347         addr = 0;
348     }
349   else                          /* The target is executing.  */
350     addr = element->addr;
351
352   return addr;
353 }
354
355 /* Determine what the low address will be to display in the TUI's
356    disassembly window.  This may or may not be the same as the low
357    address input.  */
358 CORE_ADDR
359 tui_get_low_disassembly_address (CORE_ADDR low, 
360                                  CORE_ADDR pc)
361 {
362   int pos;
363
364   /* Determine where to start the disassembly so that the pc is about
365      in the middle of the viewport.  */
366   pos = tui_default_win_viewport_height (DISASSEM_WIN, DISASSEM_COMMAND) / 2;
367   pc = tui_find_disassembly_address (pc, -pos);
368
369   if (pc < low)
370     pc = low;
371   return pc;
372 }
373
374 /* Scroll the disassembly forward or backward vertically.  */
375 void
376 tui_vertical_disassem_scroll (enum tui_scroll_direction scroll_direction,
377                               int num_to_scroll)
378 {
379   if (TUI_DISASM_WIN->generic.content != NULL)
380     {
381       CORE_ADDR pc;
382       tui_win_content content;
383       struct tui_line_or_address val;
384       int dir;
385
386       content = (tui_win_content) TUI_DISASM_WIN->generic.content;
387
388       pc = content[0]->which_element.source.line_or_addr.u.addr;
389       num_to_scroll++;
390       dir = (scroll_direction == FORWARD_SCROLL) ? num_to_scroll : -num_to_scroll;
391
392       val.loa = LOA_ADDRESS;
393       val.u.addr = tui_find_disassembly_address (pc, dir);
394       tui_update_source_window_as_is (TUI_DISASM_WIN, NULL, val, FALSE);
395     }
396 }