binutils-gdb/gdb/testsuite/gdb.tui
Shahab Vahedi 1d5d29e73f gdb: Catch exceptions if the source file is not found
The source_cache::ensure method may throw an exception through
the invocation of source_cache::get_plain_source_lines. This
happens when the source file is not found. The expected behaviour
of "ensure" is only returning "true" or "false" according to the
documentation in the header file.

So far, if gdb is in source layout and a file is missing, you see
some outputs like below:

 ,---------------------------------------------.
 | test.c file is loaded in the source window. |
 |                                             |
 | int main()                                  |
 | ...                                         |
 |---------------------------------------------|
 | Remote debugging using :1234                |
 | __start () at /path/to/crt0.S:141           |
 | /path/to/crt0.S: No such file or directory. |
 | (gdb) p/x $pc                               |
 | $1 = 0x124                                  |
 | (gdb) n                                     |
 | /path/to/crt0.S: No such file or directory. |
 | (gdb) p/x $pc                               |
 | $2 = 0x128                                  |
 | (gdb) [pressing arrow-down key]             |
 | (gdb) terminate called after throwing an    |
 |       instance of 'gdb_exception_error'     |
 `---------------------------------------------'
Other issues have been encountered as well [1].

The patch from Pedro [2] which is about preventing exceptions
from crossing the "readline" mitigates the situation by not
causing gdb crash, but still there are lots of errors printed:

 ,---------------------------------------------.
 | test.c file is loaded in the source window. |
 |                                             |
 | int main()                                  |
 | ...                                         |
 |---------------------------------------------|
 | Remote debugging using :1234                |
 | __start () at /path/to/crt0.S:141           |
 | /path/to/crt0.S: No such file or directory. |
 | (gdb) [pressing arrow-down key]             |
 | /path/to/crt0.S: No such file or directory. |
 | (gdb) [pressing arrow-down key]             |
 | /path/to/crt0.S: No such file or directory. |
 | (gdb) [pressing arrow-up key]               |
 | /path/to/crt0.S: No such file or directory. |
 `---------------------------------------------'

With the changes of this patch, the behavior is like:
 ,---------------------------------------------.
 | initially, source window is empty because   |
 | crt0.S is not found and according to the    |
 | program counter that is the piece of code   |
 | being executed.                             |
 |                                             |
 | later, when we break at main (see commands  |
 | below), this window will be filled with the |
 | the contents of test.c file.                |
 |---------------------------------------------|
 | Remote debugging using :1234                |
 | __start () at /path/to/crt0.S:141           |
 | (gdb) p/x $pc                               |
 | $1 = 0x124                                  |
 | (gdb) n                                     |
 | (gdb) p/x $pc                               |
 | $2 = 0x128                                  |
 | (gdb) b main                                |
 | Breakpoint 1 at 0x334: file test.c, line 8. |
 | (gdb) cont                                  |
 | Continuing.                                 |
 | Breakpoint 1, main () at hello.c:8          |
 | (gdb) n                                     |
 | (gdb)                                       |
 `---------------------------------------------'

There is no crash and the error message is completely
gone. Maybe it is good practice that the error is
shown inside the source window.

I tested this change against gdb.base/list-missing-source.exp
and there was no regression.

[1]
It has also been observed in the past that the register
values are not transferred from qemu's gdb stub, see:
https://github.com/foss-for-synopsys-dwc-arc-processors/toolchain/issues/226

[2]
https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=2f267673f0fdee9287e6d404ecd4f2d29da0d2f2

gdb/ChangeLog:

	* source-cache.c (source_cache::ensure): Surround
	get_plain_source_lines with a try/catch.
	(source_cache::get_line_charpos): Get rid of try/catch
	and only check for the return value of "ensure".
	* tui/tui-source.c (tui_source_window::set_contents):
	Simplify "nlines" calculation.

gdb/testsuite/ChangeLog:

	* gdb.tui/tui-missing-src.exp: Add the "missing source
	file" test for the TUI.
2020-02-06 17:54:59 +01:00
..
basic.exp gdb/tui: Link source and assembler scrolling .... again 2020-01-09 23:11:47 +00:00
completion.exp Update copyright year range in all GDB files. 2020-01-01 10:20:53 +04:00
corefile-run.exp Update copyright year range in all GDB files. 2020-01-01 10:20:53 +04:00
empty.exp Update copyright year range in all GDB files. 2020-01-01 10:20:53 +04:00
list-before.exp Update copyright year range in all GDB files. 2020-01-01 10:20:53 +04:00
list.exp gdb: use tui_set_layout not show_layout to fix window focus 2020-01-05 21:49:05 +00:00
main.exp Make "file" clear TUI source window 2020-01-19 13:08:48 -07:00
regs.exp Update copyright year range in all GDB files. 2020-01-01 10:20:53 +04:00
resize.exp Update copyright year range in all GDB files. 2020-01-01 10:20:53 +04:00
tui-disasm-long-lines.c Update copyright year range in all GDB files. 2020-01-01 10:20:53 +04:00
tui-disasm-long-lines.exp Update copyright year range in all GDB files. 2020-01-01 10:20:53 +04:00
tui-layout-asm-short-prog.S gdb/tui: Disassembler scrolling of very small programs 2020-01-31 00:41:06 +00:00
tui-layout-asm-short-prog.exp gdb/tui: Disassembler scrolling of very small programs 2020-01-31 00:41:06 +00:00
tui-layout-asm.exp gdb/tui: asm window handles invalid memory and scrolls better 2020-01-24 00:10:33 +00:00
tui-layout.c Update copyright year range in all GDB files. 2020-01-01 10:20:53 +04:00
tui-layout.exp Update copyright year range in all GDB files. 2020-01-01 10:20:53 +04:00
tui-missing-src.exp gdb: Catch exceptions if the source file is not found 2020-02-06 17:54:59 +01:00
tui-nl-filtered-output.exp Update copyright year range in all GDB files. 2020-01-01 10:20:53 +04:00
winheight.exp Update copyright year range in all GDB files. 2020-01-01 10:20:53 +04:00