1 /* TUI display source/assembly window.
3 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2006, 2007
4 Free Software Foundation, Inc.
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., 51 Franklin Street, Fifth Floor,
23 Boston, MA 02110-1301, USA. */
29 #include "breakpoint.h"
34 #include "tui/tui-data.h"
35 #include "tui/tui-stack.h"
36 #include "tui/tui-win.h"
37 #include "tui/tui-wingeneral.h"
38 #include "tui/tui-winsource.h"
39 #include "tui/tui-source.h"
40 #include "tui/tui-disasm.h"
42 #include "gdb_string.h"
43 #include "gdb_curses.h"
44 #include "gdb_assert.h"
46 /* Function to display the "main" routine. */
48 tui_display_main (void)
50 if ((tui_source_windows ())->count
> 0)
54 addr
= tui_get_begin_asm_address ();
55 if (addr
!= (CORE_ADDR
) 0)
57 struct symtab_and_line sal
;
59 tui_update_source_windows_with_addr (addr
);
60 sal
= find_pc_line (addr
, 0);
62 tui_update_locator_filename (sal
.symtab
->filename
);
64 tui_update_locator_filename ("??");
71 /* Function to display source in the source window. This function
72 initializes the horizontal scroll to 0. */
74 tui_update_source_window (struct tui_win_info
*win_info
, struct symtab
*s
,
75 struct tui_line_or_address line_or_addr
, int noerror
)
77 win_info
->detail
.source_info
.horizontal_offset
= 0;
78 tui_update_source_window_as_is (win_info
, s
, line_or_addr
, noerror
);
84 /* Function to display source in the source/asm window. This function
85 shows the source as specified by the horizontal offset. */
87 tui_update_source_window_as_is (struct tui_win_info
*win_info
, struct symtab
*s
,
88 struct tui_line_or_address line_or_addr
, int noerror
)
92 if (win_info
->generic
.type
== SRC_WIN
)
93 ret
= tui_set_source_content (s
, line_or_addr
.u
.line_no
, noerror
);
95 ret
= tui_set_disassem_content (line_or_addr
.u
.addr
);
97 if (ret
== TUI_FAILURE
)
99 tui_clear_source_content (win_info
, EMPTY_SOURCE_PROMPT
);
100 tui_clear_exec_info_content (win_info
);
104 tui_update_breakpoint_info (win_info
, 0);
105 tui_show_source_content (win_info
);
106 tui_update_exec_info (win_info
);
107 if (win_info
->generic
.type
== SRC_WIN
)
109 struct symtab_and_line sal
;
111 sal
.line
= line_or_addr
.u
.line_no
+
112 (win_info
->generic
.content_size
- 2);
114 set_current_source_symtab_and_line (&sal
);
115 /* If the focus was in the asm win, put it in the src win if
116 we don't have a split layout. */
117 if (tui_win_with_focus () == TUI_DISASM_WIN
&&
118 tui_current_layout () != SRC_DISASSEM_COMMAND
)
119 tui_set_win_focus_to (TUI_SRC_WIN
);
128 /* Function to ensure that the source and/or disassemly windows
129 reflect the input address. */
131 tui_update_source_windows_with_addr (CORE_ADDR addr
)
135 struct symtab_and_line sal
;
136 struct tui_line_or_address l
;
138 switch (tui_current_layout ())
140 case DISASSEM_COMMAND
:
141 case DISASSEM_DATA_COMMAND
:
142 tui_show_disassem (addr
);
144 case SRC_DISASSEM_COMMAND
:
145 tui_show_disassem_and_update_source (addr
);
148 sal
= find_pc_line (addr
, 0);
150 l
.u
.line_no
= sal
.line
;
151 tui_show_symtab_source (sal
.symtab
, l
, FALSE
);
159 for (i
= 0; i
< (tui_source_windows ())->count
; i
++)
161 struct tui_win_info
*win_info
= (tui_source_windows ())->list
[i
];
163 tui_clear_source_content (win_info
, EMPTY_SOURCE_PROMPT
);
164 tui_clear_exec_info_content (win_info
);
169 /* Function to ensure that the source and/or disassemly windows
170 reflect the input address. */
172 tui_update_source_windows_with_line (struct symtab
*s
, int line
)
175 struct tui_line_or_address l
;
177 switch (tui_current_layout ())
179 case DISASSEM_COMMAND
:
180 case DISASSEM_DATA_COMMAND
:
181 find_line_pc (s
, line
, &pc
);
182 tui_update_source_windows_with_addr (pc
);
187 tui_show_symtab_source (s
, l
, FALSE
);
188 if (tui_current_layout () == SRC_DISASSEM_COMMAND
)
190 find_line_pc (s
, line
, &pc
);
191 tui_show_disassem (pc
);
200 tui_clear_source_content (struct tui_win_info
*win_info
, int display_prompt
)
202 if (win_info
!= NULL
)
206 win_info
->generic
.content_in_use
= FALSE
;
207 tui_erase_source_content (win_info
, display_prompt
);
208 for (i
= 0; i
< win_info
->generic
.content_size
; i
++)
210 struct tui_win_element
*element
=
211 (struct tui_win_element
*) win_info
->generic
.content
[i
];
212 element
->which_element
.source
.has_break
= FALSE
;
213 element
->which_element
.source
.is_exec_point
= FALSE
;
220 tui_erase_source_content (struct tui_win_info
*win_info
, int display_prompt
)
223 int half_width
= (win_info
->generic
.width
- 2) / 2;
225 if (win_info
->generic
.handle
!= (WINDOW
*) NULL
)
227 werase (win_info
->generic
.handle
);
228 tui_check_and_display_highlight_if_needed (win_info
);
229 if (display_prompt
== EMPTY_SOURCE_PROMPT
)
233 if (win_info
->generic
.type
== SRC_WIN
)
234 no_src_str
= NO_SRC_STRING
;
236 no_src_str
= NO_DISASSEM_STRING
;
237 if (strlen (no_src_str
) >= half_width
)
240 x_pos
= half_width
- strlen (no_src_str
);
241 mvwaddstr (win_info
->generic
.handle
,
242 (win_info
->generic
.height
/ 2),
246 /* elz: Added this function call to set the real contents of
247 the window to what is on the screen, so that later calls
248 to refresh, do display the correct stuff, and not the old
251 tui_set_source_content_nil (win_info
, no_src_str
);
253 tui_refresh_win (&win_info
->generic
);
258 /* Redraw the complete line of a source or disassembly window. */
260 tui_show_source_line (struct tui_win_info
*win_info
, int lineno
)
262 struct tui_win_element
*line
;
265 line
= (struct tui_win_element
*) win_info
->generic
.content
[lineno
- 1];
266 if (line
->which_element
.source
.is_exec_point
)
267 wattron (win_info
->generic
.handle
, A_STANDOUT
);
269 mvwaddstr (win_info
->generic
.handle
, lineno
, 1,
270 line
->which_element
.source
.line
);
271 if (line
->which_element
.source
.is_exec_point
)
272 wattroff (win_info
->generic
.handle
, A_STANDOUT
);
274 /* Clear to end of line but stop before the border. */
275 getyx (win_info
->generic
.handle
, y
, x
);
276 while (x
+ 1 < win_info
->generic
.width
)
278 waddch (win_info
->generic
.handle
, ' ');
279 getyx (win_info
->generic
.handle
, y
, x
);
284 tui_show_source_content (struct tui_win_info
*win_info
)
286 if (win_info
->generic
.content_size
> 0)
290 for (lineno
= 1; lineno
<= win_info
->generic
.content_size
; lineno
++)
291 tui_show_source_line (win_info
, lineno
);
294 tui_erase_source_content (win_info
, TRUE
);
296 tui_check_and_display_highlight_if_needed (win_info
);
297 tui_refresh_win (&win_info
->generic
);
298 win_info
->generic
.content_in_use
= TRUE
;
302 /* Scroll the source forward or backward horizontally. */
304 tui_horizontal_source_scroll (struct tui_win_info
*win_info
,
305 enum tui_scroll_direction direction
,
308 if (win_info
->generic
.content
!= NULL
)
312 struct symtab_and_line cursal
= get_current_source_symtab_and_line ();
314 if (cursal
.symtab
== (struct symtab
*) NULL
)
315 s
= find_pc_symtab (get_frame_pc (get_selected_frame (NULL
)));
319 if (direction
== LEFT_SCROLL
)
320 offset
= win_info
->detail
.source_info
.horizontal_offset
+ num_to_scroll
;
324 win_info
->detail
.source_info
.horizontal_offset
- num_to_scroll
) < 0)
327 win_info
->detail
.source_info
.horizontal_offset
= offset
;
328 tui_update_source_window_as_is (win_info
, s
,
329 ((struct tui_win_element
*)
330 win_info
->generic
.content
[0])->which_element
.source
.line_or_addr
,
338 /* Set or clear the has_break flag in the line whose line is
342 tui_set_is_exec_point_at (struct tui_line_or_address l
, struct tui_win_info
*win_info
)
346 tui_win_content content
= (tui_win_content
) win_info
->generic
.content
;
349 while (i
< win_info
->generic
.content_size
)
352 struct tui_line_or_address content_loa
=
353 content
[i
]->which_element
.source
.line_or_addr
;
355 gdb_assert (l
.loa
== LOA_ADDRESS
|| l
.loa
== LOA_LINE
);
356 gdb_assert (content_loa
.loa
== LOA_LINE
357 || content_loa
.loa
== LOA_ADDRESS
);
358 if (content_loa
.loa
== l
.loa
359 && ((l
.loa
== LOA_LINE
&& content_loa
.u
.line_no
== l
.u
.line_no
)
360 || (content_loa
.u
.addr
== l
.u
.addr
)))
364 if (new_state
!= content
[i
]->which_element
.source
.is_exec_point
)
367 content
[i
]->which_element
.source
.is_exec_point
= new_state
;
368 tui_show_source_line (win_info
, i
+ 1);
373 tui_refresh_win (&win_info
->generic
);
376 /* Update the execution windows to show the active breakpoints.
377 This is called whenever a breakpoint is inserted, removed or
378 has its state changed. */
380 tui_update_all_breakpoint_info (void)
382 struct tui_list
*list
= tui_source_windows ();
385 for (i
= 0; i
< list
->count
; i
++)
387 struct tui_win_info
*win
= list
->list
[i
];
389 if (tui_update_breakpoint_info (win
, FALSE
))
391 tui_update_exec_info (win
);
397 /* Scan the source window and the breakpoints to update the has_break
398 information for each line.
400 Returns 1 if something changed and the execution window must be
404 tui_update_breakpoint_info (struct tui_win_info
*win
, int current_only
)
407 int need_refresh
= 0;
408 struct tui_source_info
*src
= &win
->detail
.source_info
;
410 for (i
= 0; i
< win
->generic
.content_size
; i
++)
412 struct breakpoint
*bp
;
413 extern struct breakpoint
*breakpoint_chain
;
415 struct tui_source_element
*line
;
417 line
= &((struct tui_win_element
*) win
->generic
.content
[i
])->which_element
.source
;
418 if (current_only
&& !line
->is_exec_point
)
421 /* Scan each breakpoint to see if the current line has something to
422 do with it. Identify enable/disabled breakpoints as well as
423 those that we already hit. */
425 for (bp
= breakpoint_chain
;
426 bp
!= (struct breakpoint
*) NULL
;
429 gdb_assert (line
->line_or_addr
.loa
== LOA_LINE
430 || line
->line_or_addr
.loa
== LOA_ADDRESS
);
431 if ((win
== TUI_SRC_WIN
433 && (strcmp (src
->filename
, bp
->source_file
) == 0)
434 && line
->line_or_addr
.loa
== LOA_LINE
435 && bp
->line_number
== line
->line_or_addr
.u
.line_no
)
436 || (win
== TUI_DISASM_WIN
437 && line
->line_or_addr
.loa
== LOA_ADDRESS
438 && bp
->loc
->address
== line
->line_or_addr
.u
.addr
))
440 if (bp
->enable_state
== bp_disabled
)
441 mode
|= TUI_BP_DISABLED
;
443 mode
|= TUI_BP_ENABLED
;
447 mode
|= TUI_BP_CONDITIONAL
;
448 if (bp
->type
== bp_hardware_breakpoint
)
449 mode
|= TUI_BP_HARDWARE
;
452 if (line
->has_break
!= mode
)
454 line
->has_break
= mode
;
462 /* Function to initialize the content of the execution info window,
463 based upon the input window which is either the source or
464 disassembly window. */
466 tui_set_exec_info_content (struct tui_win_info
*win_info
)
468 enum tui_status ret
= TUI_SUCCESS
;
470 if (win_info
->detail
.source_info
.execution_info
!= (struct tui_gen_win_info
*) NULL
)
472 struct tui_gen_win_info
*exec_info_ptr
= win_info
->detail
.source_info
.execution_info
;
474 if (exec_info_ptr
->content
== NULL
)
475 exec_info_ptr
->content
=
476 (void **) tui_alloc_content (win_info
->generic
.height
,
477 exec_info_ptr
->type
);
478 if (exec_info_ptr
->content
!= NULL
)
482 tui_update_breakpoint_info (win_info
, 1);
483 for (i
= 0; i
< win_info
->generic
.content_size
; i
++)
485 struct tui_win_element
*element
;
486 struct tui_win_element
*src_element
;
489 element
= (struct tui_win_element
*) exec_info_ptr
->content
[i
];
490 src_element
= (struct tui_win_element
*) win_info
->generic
.content
[i
];
492 memset(element
->which_element
.simple_string
, ' ',
493 sizeof(element
->which_element
.simple_string
));
494 element
->which_element
.simple_string
[TUI_EXECINFO_SIZE
- 1] = 0;
496 /* Now update the exec info content based upon the state
497 of each line as indicated by the source content. */
498 mode
= src_element
->which_element
.source
.has_break
;
499 if (mode
& TUI_BP_HIT
)
500 element
->which_element
.simple_string
[TUI_BP_HIT_POS
] =
501 (mode
& TUI_BP_HARDWARE
) ? 'H' : 'B';
502 else if (mode
& (TUI_BP_ENABLED
| TUI_BP_DISABLED
))
503 element
->which_element
.simple_string
[TUI_BP_HIT_POS
] =
504 (mode
& TUI_BP_HARDWARE
) ? 'h' : 'b';
506 if (mode
& TUI_BP_ENABLED
)
507 element
->which_element
.simple_string
[TUI_BP_BREAK_POS
] = '+';
508 else if (mode
& TUI_BP_DISABLED
)
509 element
->which_element
.simple_string
[TUI_BP_BREAK_POS
] = '-';
511 if (src_element
->which_element
.source
.is_exec_point
)
512 element
->which_element
.simple_string
[TUI_EXEC_POS
] = '>';
514 exec_info_ptr
->content_size
= win_info
->generic
.content_size
;
525 tui_show_exec_info_content (struct tui_win_info
*win_info
)
527 struct tui_gen_win_info
*exec_info
= win_info
->detail
.source_info
.execution_info
;
530 werase (exec_info
->handle
);
531 tui_refresh_win (exec_info
);
532 for (cur_line
= 1; (cur_line
<= exec_info
->content_size
); cur_line
++)
533 mvwaddstr (exec_info
->handle
,
536 ((struct tui_win_element
*)
537 exec_info
->content
[cur_line
- 1])->which_element
.simple_string
);
538 tui_refresh_win (exec_info
);
539 exec_info
->content_in_use
= TRUE
;
544 tui_erase_exec_info_content (struct tui_win_info
*win_info
)
546 struct tui_gen_win_info
*exec_info
= win_info
->detail
.source_info
.execution_info
;
548 werase (exec_info
->handle
);
549 tui_refresh_win (exec_info
);
553 tui_clear_exec_info_content (struct tui_win_info
*win_info
)
555 win_info
->detail
.source_info
.execution_info
->content_in_use
= FALSE
;
556 tui_erase_exec_info_content (win_info
);
561 /* Function to update the execution info window. */
563 tui_update_exec_info (struct tui_win_info
*win_info
)
565 tui_set_exec_info_content (win_info
);
566 tui_show_exec_info_content (win_info
);
570 tui_alloc_source_buffer (struct tui_win_info
*win_info
)
573 int i
, line_width
, max_lines
;
575 max_lines
= win_info
->generic
.height
; /* Less the highlight box. */
576 line_width
= win_info
->generic
.width
- 1;
578 * Allocate the buffer for the source lines. Do this only once
579 * since they will be re-used for all source displays. The only
580 * other time this will be done is when a window's size changes.
582 if (win_info
->generic
.content
== NULL
)
584 src_line_buf
= (char *)
585 xmalloc ((max_lines
* line_width
) * sizeof (char));
586 if (src_line_buf
== (char *) NULL
)
588 fputs_unfiltered ("Unable to Allocate Memory for Source or Disassembly Display.\n",
592 /* Allocate the content list. */
593 if ((win_info
->generic
.content
=
594 (void **) tui_alloc_content (max_lines
, SRC_WIN
)) == NULL
)
596 xfree (src_line_buf
);
597 fputs_unfiltered ("Unable to Allocate Memory for Source or Disassembly Display.\n",
601 for (i
= 0; i
< max_lines
; i
++)
602 ((struct tui_win_element
*)
603 win_info
->generic
.content
[i
])->which_element
.source
.line
=
604 src_line_buf
+ (line_width
* i
);
611 /* Answer whether the a particular line number or address is displayed
612 in the current source window. */
614 tui_line_is_displayed (int line
, struct tui_win_info
*win_info
,
617 int is_displayed
= FALSE
;
621 threshold
= SCROLL_THRESHOLD
;
625 while (i
< win_info
->generic
.content_size
- threshold
&& !is_displayed
)
627 is_displayed
= (((struct tui_win_element
*)
628 win_info
->generic
.content
[i
])->which_element
.source
.line_or_addr
.loa
630 && (((struct tui_win_element
*)
631 win_info
->generic
.content
[i
])->which_element
.source
.line_or_addr
.u
.line_no
640 /* Answer whether the a particular line number or address is displayed
641 in the current source window. */
643 tui_addr_is_displayed (CORE_ADDR addr
, struct tui_win_info
*win_info
,
646 int is_displayed
= FALSE
;
650 threshold
= SCROLL_THRESHOLD
;
654 while (i
< win_info
->generic
.content_size
- threshold
&& !is_displayed
)
656 is_displayed
= (((struct tui_win_element
*)
657 win_info
->generic
.content
[i
])->which_element
.source
.line_or_addr
.loa
659 && (((struct tui_win_element
*)
660 win_info
->generic
.content
[i
])->which_element
.source
.line_or_addr
.u
.addr
669 /*****************************************
670 ** STATIC LOCAL FUNCTIONS **
671 ******************************************/