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.