aboutsummaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.base
AgeCommit message (Collapse)AuthorFilesLines
2024-03-03Bump GDB's version number to 14.2.90.DATE-git.Joel Brobecker1-1/+1
This commit changes gdb/version.in to 14.2.90.DATE-git. This commit also makes the following changes in gdb/testsuite: * gdb.base/default.exp: Change $_gdb_minor to 3.
2024-01-23gdb: fix "list ." related crashGuinevere Larsen2-0/+58
When a user attempts to use the "list ." command with an inferior that doesn't have debug symbols, GDB would crash. This was reported as PR gdb/31256. The crash would happen when attempting to get the current symtab_and_line for the stop location, because the symtab would return a null pointer and we'd attempt to dereference it to print the line. This commit fixes that by checking for an empty symtab and erroring out of the function if it happens. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31256 Approved-By: Tom Tromey <tom@tromey.com>
2023-12-03Bump GDB's version number to 14.1.90.DATE-git.Joel Brobecker1-1/+1
This commit changes gdb/version.in to 14.1.90.DATE-git. This commit also makes the following changes in gdb/testsuite: * gdb.base/default.exp: Change $_gdb_minor to 2.
2023-10-16Only allow closure lookup by address if there are threads displaced-steppingLuis Machado2-0/+60
Since commit 1e5ccb9c5ff4fd8ade4a8694676f99f4abf2d679, we have an assertion in displaced_step_buffers::copy_insn_closure_by_addr that makes sure a closure is available whenever we have a match between the provided address argument and the buffer address. That is fine, but the report in PR30872 shows this assertion triggering when it really shouldn't. After some investigation, here's what I found out. The 32-bit Arm architecture is the only one that calls gdbarch_displaced_step_copy_insn_closure_by_addr directly, and that's because 32-bit Arm needs to figure out the thumb state of the original instruction that we displaced-stepped through the displaced-step buffer. Before the assertion was put in place by commit 1e5ccb9c5ff4fd8ade4a8694676f99f4abf2d679, there was the possibility of getting nullptr back, which meant we were not doing a displaced-stepping operation. Now, with the assertion in place, this is running into issues. It looks like displaced_step_buffers::copy_insn_closure_by_addr is being used to return a couple different answers depending on the state we're in: 1 - If we are actively displaced-stepping, then copy_insn_closure_by_addr is supposed to return a valid closure for us, so we can determine the thumb mode. 2 - If we are not actively displaced-stepping, then copy_insn_closure_by_addr should return nullptr to signal that there isn't any displaced-step buffers in use, because we don't have a valid closure (but we should always have this). Since the displaced-step buffers are always allocated, but not always used, that means the buffers will always contain data. In particular, the buffer addr field cannot be used to determine if the buffer is active or not. For instance, we cannot set the buffer addr field to 0x0, as that can be a valid PC in some cases. My understanding is that the current_thread field should be a good candidate to signal that a particular displaced-step buffer is active or not. If it is nullptr, we have no threads using that buffer to displaced-step. Otherwise, it is an active buffer in use by a particular thread. The following fix modifies the displaced_step_buffers::copy_insn_closure_by_addr function so we only attempt to return a closure if the buffer has an assigned current_thread and if the buffer address matches the address argument. Alternatively, I think we could use a function to answer the question of whether we're actively displaced-stepping (so we have an active buffer) or not. I've also added a testcase that exercises the problem. It should reproduce reliably on Arm, as that is the only architecture that faces this problem at the moment. Regression-tested on Ubuntu 20.04. OK? Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30872 Approved-By: Simon Marchi <simon.marchi@efficios.com>
2023-10-06gdb/testsuite: cleanup in gdb.base/args.expAndrew Burgess1-52/+36
The last few commits resolved the KFAILs in gdb.base/args.exp. With those out of the way we can clean up this test script a little. In this commit I have: - Stopped passing 'nowarnings' flag when building the source file. I see no reason why this source should issue a warning, - Moved setup of GDBFLAGS into args_test proc, callers that passed a newline needed a small tweak, and also the matching code needs updating for newline handling, but I think this is nicer, the argument lists are now given just once, - Updated comment on args_test, - Updated other comments. There should be no change in what is tested after this commit. Approved-By: Tom Tromey <tom@tromey.com>
2023-10-06gdbserver: handle newlines in inferior argumentsAndrew Burgess1-15/+5
Similarly to how single quotes were mishandled, which was fixed two commits ago, this commit fixes handling of newlines in arguments passed to gdbserver. We already had a test that covered this, gdb.base/args.exp, which, when run with the native-extended-gdbserver board contained several KFAIL covering this situation. In this commit I remove the unnecessary, attempt to quote incoming newlines within arguments, and do some minimal cleanup of the related code. There is additional cleanup that can be done, but I'm leaving that for the next commit. Then I've removed the KFAIL from the test case, and performed some minimal cleanup there too. After this commit the gdb.base/args.exp is 100% passing with the native-extended-gdbserver board file. During review I was pointed to this older series: https://inbox.sourceware.org/gdb-patches/20211022071933.3478427-1-m.weghorn@posteo.de/ which also includes this fix as part of a larger set of changes. I'm giving a Co-Authored-By credit to the author of that original series. I believe this smaller fix brings some benefits on its own, though the original series does offer additional improvements. Once this is merged I'll take a look at rebasing and resubmitting the original series. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=27989 Co-Authored-By: Michael Weghorn <m.weghorn@posteo.de> Approved-By: Tom Tromey <tom@tromey.com>
2023-10-06gdbserver: fix handling of trailing empty argumentAndrew Burgess1-3/+6
When I posted the previous patch for review Andreas Schwab pointed out that passing a trailing empty argument also doesn't work. The fix for this is in the same area of code as the previous patch, but is sufficiently different that I felt it deserved a patch of its own. I noticed that passing arguments containing single quotes to gdbserver didn't work correctly: gdb -ex 'set sysroot' --args /tmp/show-args Reading symbols from /tmp/show-args... (gdb) target extended-remote | gdbserver --once --multi - /tmp/show-args Remote debugging using | gdbserver --once --multi - /tmp/show-args stdin/stdout redirected Process /tmp/show-args created; pid = 176054 Remote debugging using stdio Reading symbols from /lib64/ld-linux-x86-64.so.2... (No debugging symbols found in /lib64/ld-linux-x86-64.so.2) 0x00007ffff7fd3110 in _start () from /lib64/ld-linux-x86-64.so.2 (gdb) set args abc "" (gdb) run The program being debugged has been started already. Start it from the beginning? (y or n) y Starting program: /tmp/show-args \' stdin/stdout redirected Process /tmp/show-args created; pid = 176088 2 args are: /tmp/show-args abc Done. [Inferior 1 (process 176088) exited normally] (gdb) target native Done. Use the "run" command to start a process. (gdb) run Starting program: /tmp/show-args \' 2 args are: /tmp/show-args abc Done. [Inferior 1 (process 176095) exited normally] (gdb) q The 'shows-args' program used here just prints the arguments passed to the inferior. Notice that when starting the inferior using the extended-remote target there is only a single argument 'abc', while when using the native target there is a second argument, the blank line, representing the empty argument. The problem here is that the vRun packet coming from GDB looks like this (I've removing the trailing checksum): $vRun;PROGRAM_NAME;616263; If we compare this to a packet with only a single argument and no trailing empty argument: $vRun;PROGRAM_NAME;616263 Notice the lack of the trailing ';' character here. The problem is that gdbserver processes this string in a loop. At each point we maintain a pointer to the character just after a ';', and then we process everything up to either the next ';' character, or to the end of the string. We break out of this loop when the character we start with (in that loop iteration) is the null-character. This means in the trailing empty argument case, we abort the loop before doing anything with the empty argument. In this commit I've updated the loop, we now break out using a 'break' statement at the end of the loop if the (sub-)string we just processed was empty, with this change we now notice the trailing empty argument. I've updated the test case to cover this issue. Approved-By: Tom Tromey <tom@tromey.com>
2023-10-06gdbserver: fix handling of single quote argumentsAndrew Burgess2-4/+7
I noticed that passing arguments containing single quotes to gdbserver didn't work correctly: gdb -ex 'set sysroot' --args /tmp/show-args Reading symbols from /tmp/show-args... (gdb) target extended-remote | gdbserver --once --multi - /tmp/show-args Remote debugging using | gdbserver --once --multi - /tmp/show-args stdin/stdout redirected Process /tmp/show-args created; pid = 176054 Remote debugging using stdio Reading symbols from /lib64/ld-linux-x86-64.so.2... (No debugging symbols found in /lib64/ld-linux-x86-64.so.2) 0x00007ffff7fd3110 in _start () from /lib64/ld-linux-x86-64.so.2 (gdb) set args \' (gdb) r The program being debugged has been started already. Start it from the beginning? (y or n) y Starting program: /tmp/show-args \' stdin/stdout redirected Process /tmp/show-args created; pid = 176088 2 args are: /tmp/show-args \' Done. [Inferior 1 (process 176088) exited normally] (gdb) target native Done. Use the "run" command to start a process. (gdb) run Starting program: /tmp/show-args \' 2 args are: /tmp/show-args ' Done. [Inferior 1 (process 176095) exited normally] (gdb) q The 'shows-args' program used here just prints the arguments passed to the inferior. Notice that when starting the inferior using the extended-remote target the second argument is "\'", while when running using native target the argument is "'". The second of these is correct, the \' used with the "set args" command is just to show GDB that the single quote is not opening an argument string. It turns out that the extra backslash is injected on the gdbserver side when gdbserver processes the arguments that GDB passes it, the code that does this was added as part of this much larger commit: commit 2090129c36c7e582943b7d300968d19b46160d84 Date: Thu Dec 22 21:11:11 2016 -0500 Share fork_inferior et al with gdbserver In this commit I propose removing the specific code that adds what I believe is a stray backslash. I've extended an existing test to cover this case, and I now see identical behaviour when using an extended-remote target as with the native target. This partially fixes PR gdb/27989, though there are still some issues with newline handling which I'll address in a later commit. During review I was pointed to this older series: https://inbox.sourceware.org/gdb-patches/20211022071933.3478427-1-m.weghorn@posteo.de/ which also includes this fix as part of a larger set of changes. I'm giving a Co-Authored-By credit to the author of that original series. I believe this smaller fix brings some benefits on its own, though the original series does offer additional improvements. Once this is merged I'll take a look at rebasing and resubmitting the original series. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=27989 Co-Authored-By: Michael Weghorn <m.weghorn@posteo.de> Approved-By: Tom Tromey <tom@tromey.com>
2023-10-05gdb: remove print_sys_errmsgAndrew Burgess1-2/+2
This started with me running into this comment in symfile.c: /* FIXME, should use print_sys_errmsg but it's not filtered. */ gdb_printf (_("`%ps' has disappeared; keeping its symbols.\n"), styled_string (file_name_style.style (), filename)); In this particular case I think I disagree with the comment; I think the output should be a warning rather than just a message printed to gdb_stdout, I think when the executable, or some other objfile that is currently being debugged, disappears from disk, this is likely an unexpected situation, and worth warning the user about. So, in theory, I could just call print_sys_errmsg and remove the comment, but that would mean loosing the filename styling in the output... so in the end I remove the comment and updated the code to call warning. But that got me looking at print_sys_errmsg and how it's used. Currently the function takes a string and an errno, and prints, to stderr, the string followed by the result of calling strerror on the errno. In some places the string passed to print_sys_errmsg is just a filename, and this is used when something goes wrong. In these cases, I think calling warning rather than gdb_printf to gdb_stderr, would be better, and in fact, in a couple of places we manually print a "warning" prefix, and then call print_sys_errmsg. And so, for these users I have added a new function warning_filename_and_errno, which takes a filename, which is printed with styling, and an errno, which is passed through strerror and the resulting string printed. This new function calls warning to print its output. I then updated some of the print_sys_errmsg users to use this new function. Some other users of print_sys_errmsg are also emitting what is clearly a warning, however, the string being passed in is more than just a filename, so the new warning_filename_and_errno function can't be used, it would style the whole string. For these users I have switched to calling warning directly, this allows me to style the warning message correctly. Finally, in inflow.c there is one last call to print_sys_errmsg, in this case I just inlined the definition of print_sys_errmsg. This is a really weird case, as after printing this message GDB just does a hard exit. This is pretty old code, dating back to the initial GDB import, I guess it should be updated to call error() maybe, but I'm reluctant to make this change as part of this commit, just in case there's some reason why we can't throw an error at this point. With that done there are now no users of print_sys_errmsg, and so the old function can be removed. While I was doing all of the above I added some additional filename styling in soure.c, this is in an else block where the if contained the print_sys_errmsg call, so these felt related. And finally, while I was updating the uses of print_sys_errmsg in procfs.c, I noticed that we used a static errmsg buffer to format some error strings. As the above changes got rid of one of the users of errmsg I also removed the other two users, and the static buffer. There were a couple of tests that depended on the existing output message format that needed updating. In one case we gained an extra 'warning: ' prefix, and in the other 'Warning: ' becomes 'warning: ', I think in both cases the new output is an improvement. Approved-By: Tom Tromey <tom@tromey.com>
2023-10-04gdb/testsuite: XFAIL some gdb.base/fileio.expGuinevere Larsen1-1/+8
Some gdb.base/fileio.exp tests expect the inferior to not have write access to some files. If the test is being run as root, this is never possible. This commit adds a way to identify if the user is root and xfails the tests that expect no write access. Approved-By: Tom de Vries <tdevries@suse.de>
2023-10-02gdb/testsuite: accept variable number of spaces in ↵Simon Marchi1-2/+2
gdb.base/jit-reader-simple.exp regex I see this failure: FAIL: gdb.base/jit-reader-simple.exp: standalone: change addr: initial run: maint info breakpoints shows jit breakpoint The jit breakpoint expected by the test is there, it's just that the number of spaces doesn't match what the test expects, after "jit events": -2 jit events keep y 0x0000555555555119 <__jit_debug_register_code> inf 1 Fix that by relaxing the regex a bit. Change-Id: Ia3b04e6d5978399d940fd1a590f95f15275ca7ac
2023-09-29Support the NO_COLOR environment variableTom Tromey5-17/+10
I ran across this site: https://no-color.org/ ... which lobbies for tools to recognize the NO_COLOR environment variable and disable any terminal styling when it is seen. This patch implements this for gdb. Regression tested on x86-64 Fedora 38. Co-Authored-By: Andrew Burgess <aburgess@redhat.com> Reviewed-by: Kevin Buettner <kevinb@redhat.com> Reviewed-By: Eli Zaretskii <eliz@gnu.org> Approved-By: Andrew Burgess <aburgess@redhat.com>
2023-09-28[gdb/testsuite] Add nopie to gdb.base/unwind-on-each-insn-amd64-2.expTom de Vries5-7/+23
When running test-case gdb.base/unwind-on-each-insn-amd64-2.exp with target board unix/-fPIE/-pie, I run into: ... gdb compile failed, ld: unwind-on-each-insn-amd64-21.o: relocation \ R_X86_64_32S against `.text' can not be used when making a PIE object; \ recompile with -fPIE ld: failed to set dynamic section sizes: bad value ... Fix this by hardcoding nopie in the test-case, and for good measure in the other test-cases that source unwind-on-each-insn.exp.tcl and use a .s file. Tested on x86_64-linux. Approved-by: Kevin Buettner <kevinb@redhat.com>
2023-09-19gdb, breakpoint: add a destructor to the watchpoint structMohamed Bouhaouel2-0/+66
Make sure to unlink the related breakpoint when the watchpoint instance is deleted. This prevents having a wp-related breakpoint that is linked to a NULL watchpoint (e.g. the watchpoint instance is being deleted when the 'watch' command fails). With the below scenario, having such a left out breakpoint will lead to a GDB hang, and this is due to an infinite loop when deleting all inferior breakpoints. Scenario: (gdb) set can-use-hw-watchpoints 0 (gdb) awatch <SCOPE VAR> Can't set read/access watchpoint when hardware watchpoints are disabled. (gdb) rwatch <SCOPE VAR> Can't set read/access watchpoint when hardware watchpoints are disabled. (gdb) <continue the program until the end> >> HANG << Signed-off-by: Mohamed Bouhaouel <mohamed.bouhaouel@intel.com> Reviewed-by: Bruno Larsen <blarsen@redhat.com>
2023-09-19gdb/cli: fixes to newly added "list ." commandGuinevere Larsen1-11/+13
After the series that added this command was pushed, Pedro mentioned that the news description could easily be misinterpreted, as well as some code and test improvements that should be made. While fixing the test, I realized that code repetition wasn't happening as it should, so I took care of that too. Approved-By: Andrew Burgess <aburgess@redhat.com> Reviewed-By: Eli Zaretskii <eliz@gnu.org>
2023-09-14[gdb/exp] Clean up asap in value_print_array_elementsTom de Vries1-3/+25
I've been running the test-suite on an i686-linux laptop with 1GB of memory, and 1 GB of swap, and noticed problems after running gdb.base/huge.exp: gdb not being able to spawn for a large number of test-cases afterwards. So I investigated the memory usage, on my usual x86_64-linux development platform. The test-case is compiled with -DCRASH_GDB=2097152, so this: ... static int a[CRASH_GDB], b[CRASH_GDB]; ... with sizeof (int) == 4 represents two arrays of 8MB each. Say we add a loop around the "print a" command and print space usage statistics: ... gdb_test "maint set per-command space on" for {set i 0} {$i < 100} {incr i} { gdb_test "print a" } ... This gets us: ... (gdb) print a^M $1 = {0 <repeats 2097152 times>}^M Space used: 478248960 (+469356544 for this command)^M (gdb) print a^M $2 = {0 <repeats 2097152 times>}^M Space used: 486629376 (+8380416 for this command)^M (gdb) print a^M $3 = {0 <repeats 2097152 times>}^M Space used: 495009792 (+8380416 for this command)^M ... (gdb) print a^M $100 = {0 <repeats 2097152 times>}^M Space used: 1308721152 (+8380416 for this command)^M ... In other words, we start out at 8MB, and the first print costs us about 469MB, and subsequent prints 8MB, which accumulates to 1.3 GB usage. [ On the i686-linux laptop, the first print costs us 335MB. ] The subsequent 8MBs are consistent with the values being saved into the value history, but the usage for the initial print seems somewhat excessive. There is a PR open about needing sparse representation of large arrays (PR8819), but this memory usage points to an independent problem. The function value_print_array_elements contains a scoped_value_mark to free allocated values in the outer loop, but it doesn't prevent the inner loop from allocating a lot of values. Fix this by adding a scoped_value_mark in the inner loop, after which we have: ... (gdb) print a^M $1 = {0 <repeats 2097152 times>}^M Space used: 8892416 (+0 for this command)^M (gdb) print a^M $2 = {0 <repeats 2097152 times>}^M Space used: 8892416 (+0 for this command)^M (gdb) print a^M $3 = {0 <repeats 2097152 times>}^M Space used: 8892416 (+0 for this command)^M ... (gdb) print a^M $100 = {0 <repeats 2097152 times>}^M Space used: 8892416 (+0 for this command)^M ... Note that the +0 here just means that the mallocs did not trigger an sbrk. This is dependent on malloc (which can use either mmap or sbrk or some pre-allocated memory) and will likely vary between different tunings, versions and implementations, so this does not give us a reliable way detect the problem in a minimal way. A more reliable way of detecting the problem is: ... void value_free_to_mark (const struct value *mark) { + size_t before = all_values.size (); auto iter = std::find (all_values.begin (), all_values.end (), mark); if (iter == all_values.end ()) all_values.clear (); else all_values.erase (iter + 1, all_values.end ()); + size_t after = all_values.size (); + if (before - after >= 1024) + fprintf (stderr, "value_free_to_mark freed %zu items\n", before - after); ... which without the fix tells us: ... +print a value_free_to_mark freed 2097152 items $1 = {0 <repeats 2097152 times>} ... Fix a similar problem for Fortran: ... +print array1 value_free_to_mark freed 4194303 items $1 = (0, <repeats 2097152 times>) ... in fortran_array_printer_impl::process_element. The problem also exists for Ada: ... +print Arr value_free_to_mark freed 2097152 items $1 = (0 <repeats 2097152 times>) ... but is fixed by the fix for C. Add Fortran and Ada variants of the test-case. The *.exp files are similar enough to the original to keep the copyright years range. While writing the Fortran test-case, I ran into needing an additional print setting to print the entire array in repeat form, filed as PR exp/30817. I managed to apply the compilation loop for the Ada variant as well, but with a cumbersome repetition style. I noticed no other test-case uses gnateD, so perhaps there's a better way of implementing this. The regression test included in the patch is formulated in its weakest form, to avoid false positive FAILs, which also means that smaller regressions may not get detected. Tested on x86_64-linux. Approved-By: Tom Tromey <tom@tromey.com>
2023-09-14[gdb/testsuite] Modernize gdb.base/huge.expTom de Vries1-18/+33
Rewrite test-case gdb.base/huge.exp: - use build_executable rather than gdb_compile, - use save_vars, - factor out hardcoded loop limits min and max, - handle compilation failure using require, and - avoid using . in regexp to match $, {} and <>. Tested on x86_64-linux. Approved-By: Tom Tromey <tom@tromey.com>
2023-09-05gdb/testsuite: Make hook-stop.exp ignore termination message from GDB stubSandra Loosemore1-1/+1
When a GDB stub is run via "target remote |", it sometimes produces extra output that ends up mixed with GDB's own output. For example, QEMU's built-in GDB stub responds to the vKill packet by printing nios2-elf-qemu-system: QEMU: Terminated via GDBstub before exiting. This patch fixes the regexp in gdb.base/hook-stop.exp to allow such messages between GDB's "continuing" and "Inferior killed" messages. Reviewed-By: Tom Tromey <tom@tromey.com> Approved-By: Tom Tromey <tom@tromey.com>
2023-09-05gdb/testsuite: Disable some tests that are broken on remote Windows hostSandra Loosemore3-0/+9
These testcases assume host==build or that the remote host has a Posix shell to run commands in. Don't try to run them if that's not the case. Reviewed-By: Tom Tromey <tom@tromey.com> Approved-By: Tom Tromey <tom@tromey.com>
2023-09-05gdb/testsuite: Adjust some testcases to allow Windows pathnamesSandra Loosemore2-4/+15
This patch fixes some testcases that formerly had patterns with hardwired "/" pathname separators in them, which broke when testing on (remote) Windows host. Reviewed-By: Tom Tromey <tom@tromey.com> Approved-By: Tom Tromey <tom@tromey.com>
2023-09-05gdb/testsuite: Fix style.exp failures on targets without argc/argv supportSandra Loosemore1-5/+18
Some embedded targets don't have full support for argc/argv. argv may print as "0x0" or as an address with a symbol name following. This causes problems for the regexps in the style.exp line-wrapping tests that assume it always prints as an ordinary address in backtrace output. This patch generalizes the regexps to handle these additional forms and reworks some of the line-wrapping tests to account for the argv address string being shorter or longer than a regular address. Reviewed-By: Tom Tromey <tom@tromey.com> Approved-By: Tom Tromey <tom@tromey.com>
2023-09-04[gdb/testsuite] Fix race in gdb.base/add-symbol-file-attach.expTom de Vries1-2/+2
When running test-case gdb.base/add-symbol-file-attach.exp with target board unix/-m32, we run into: ... (gdb) attach 3955^M Attaching to process 3955^M Load new symbol table from "add-symbol-file-attach"? (y or n) y^M Reading symbols from add-symbol-file-attach/add-symbol-file-attach...^M Reading symbols from /lib/libm.so.6...^M Reading symbols from /usr/lib/debug/lib/libm-2.31.so-i386.debug...^M Reading symbols from /lib/libc.so.6...^M Reading symbols from /usr/lib/debug/lib/libc-2.31.so-i386.debug...^M Reading symbols from /lib/ld-linux.so.2...^M Reading symbols from /usr/lib/debug/lib/ld-2.31.so-i386.debug...^M 0xf7f53549 in __kernel_vsyscall ()^M (gdb) FAIL: gdb.base/add-symbol-file-attach.exp: attach ... The test fails because this regexp is used: ... -re ".*in \[_A-Za-z0-9\]*pause.*$gdb_prompt $" { ... The regexp attempts to detect that the exec is somewhere in pause (): ... int main (int argc, char **argv) { pause (); return 0; } ... but when the exec is blocked in pause, the backtrace is: ... (gdb) bt #0 0xf7fd2549 in __kernel_vsyscall () #1 0xf7d84966 in __libc_pause () at ../sysdeps/unix/sysv/linux/pause.c:29 #2 0x0804844c in main (argc=1, argv=0xffffce84) at /data/vries/gdb/src/gdb/testsuite/gdb.base/add-symbol-file-attach.c:26 ... We could simply extend the regexp to also match __kernel_vsyscall, but the more fundamental problem is that the test is racy. The attach can happen before the exec is blocked in pause (), somewhere in the dynamic linker resolving the call to pause, in main or even earlier. Note that for the test-case to be effective, the exec is not required to be in pause (). I added a "while (1);" loop at the start of main, reverted the patch fixing the corresponding PR and reproduced the problem it's supposed to detect. Fix this by simply matching the "Reading symbols from" line, similar to what an earlier test is doing. While we're at it, rewrite the earlier test to also use the -wrap idiom. Tested on x86_64-linux.
2023-08-31[symtab/27831] New test case: gdb.base/add-symbol-file-attach.expKevin Buettner2-0/+110
This commit adds a new test case for bug 27831. See the contents of the .exp file for a description of what it's about. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=27831 Approved-By: Tom Tromey <tom@tromey.com>
2023-08-26Use get_frame_address_in_block in print_frameTom Tromey3-0/+104
The author of 'mold' pointed out that with a certain shared library, gdb would fail to find the shared library's name in 'bt'. The function in question appeared at the end of the .so's .text segment and ended with a call to 'abort'. This turned out to be a classic case of calling get_frame_pc when get_frame_address_in_block is needed -- the former will be off-by-one for purposes of finding the enclosing function or shared library. The included test fails without the patch on my system. However, I imagine it can't be assumed to reliably fail. Nevertheless it seemed worth doing. Regression tested on x86-64 Fedora 38. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=29074 Reviewed-by: Kevin Buettner <kevinb@redhat.com>
2023-08-23gdb: MI stopped events when unwindonsignal is onAndrew Burgess1-1/+6
This recent commit: commit b1c0ab20809a502b2d2224fecb0dca3ada2e9b22 Date: Wed Jul 12 21:56:50 2023 +0100 gdb: avoid double stop after failed breakpoint condition check Addressed a problem where two MI stopped events would be reported if a breakpoint condition failed due to a signal, this commit was a replacement for this commit: commit 2e411b8c68eb2b035b31d5b00d940d4be1a0928b Date: Fri Oct 14 14:53:15 2022 +0100 gdb: don't always print breakpoint location after failed condition check which solved the two stop problem, but only for the CLI. Before both of these commits, if a b/p condition failed due to a signal then the user would see two stops reported, the first at the location where the signal occurred, and the second at the location of the breakpoint. By default GDB remains at the location where the signal occurred, so the second reported stop can be confusing, this is the problem that commit 2e411b8c68eb tried to solve (for the CLI) and b1c0ab20809a extended also address the issue for MI too. However, while working on another patch I realised that there was a problem with GDB after the above commits. Neither of the above commits considered 'set unwindonsignal on'. With this setting on, when an inferior function call fails with a signal GDB will unwind the stack back to the location where the inferior function call started. In the b/p case we're looking at, the stop should be reported at the location of the breakpoint, not at the location where the signal occurred, and this isn't what happens. This commit fixes this by ensuring that when unwindonsignal is 'on', GDB reports a single stop event at the location of the breakpoint, this fixes things for both CLI and MI. The function call_thread_fsm::should_notify_stop is called when the inferior function call completes and GDB is figuring out if the user should be notified about this stop event by calling normal_stop from fetch_inferior_event in infrun.c. If normal_stop is called, then this notification will be for the location where the inferior call stopped, which will be the location at which the signal occurred. Prior to this commit, the only time that normal_stop was not called, was if the inferior function call completed successfully, this was controlled by ::should_notify_stop, which only turns false when the inferior function call has completed successfully. In this commit I have extended the logic in ::should_notify_stop. Now there are three cases in which ::should_notify_stop will return false, and we will not announce the first stop (by calling normal_stop). These three reasons are: 1. If the inferior function call completes successfully, this is unchanged behaviour, 2. If the inferior function call stopped due to a signal and 'set unwindonsignal on' is in effect, and 3. If the inferior function call stopped due to an uncaught C++ exception, and 'set unwind-on-terminating-exception on' is in effect. However, if we don't call normal_stop then we need to call async_enable_stdin in call_thread_fsm::should_stop. Prior to this commit this was only done for the case where the inferior function call completed successfully. In this commit I now call ::should_notify_stop and use this to determine if we need to call async_enable_stdin. With this done we now call async_enable_stdin for each of the three cases listed above, which means that GDB will exit wait_sync_command_done correctly (see run_inferior_call in infcall.c). With these two changes the problem is mostly resolved. However, the solution isn't ideal, we've still lost some information. Here is how GDB 13.1 behaves, this is before commits b1c0ab20809a and 2e411b8c68eb: $ gdb -q /tmp/mi-condbreak-fail \ -ex 'set unwindonsignal on' \ -ex 'break 30 if (cond_fail())' \ -ex 'run' Reading symbols from /tmp/mi-condbreak-fail... Breakpoint 1 at 0x40111e: file /tmp/mi-condbreak-fail.c, line 30. Starting program: /tmp/mi-condbreak-fail Program received signal SIGSEGV, Segmentation fault. 0x0000000000401116 in cond_fail () at /tmp/mi-condbreak-fail.c:24 24 return *p; /* Crash here. */ Error in testing breakpoint condition: The program being debugged was signaled while in a function called from GDB. GDB has restored the context to what it was before the call. To change this behavior use "set unwindonsignal off". Evaluation of the expression containing the function (cond_fail) will be abandoned. Breakpoint 1, foo () at /tmp/mi-condbreak-fail.c:30 30 global_counter += 1; /* Set breakpoint here. */ (gdb) In this state we see two stop notifications, the first is where the signal occurred, while the second is where the breakpoint is located. As GDB has unwound the stack (thanks to unwindonsignal) the second stop notification reflects where the inferior is actually located. Then after commits b1c0ab20809a and 2e411b8c68eb the behaviour changed to this: $ gdb -q /tmp/mi-condbreak-fail \ -ex 'set unwindonsignal on' \ -ex 'break 30 if (cond_fail())' \ -ex 'run' Reading symbols from /tmp/mi-condbreak-fail... Breakpoint 1 at 0x40111e: file /tmp/mi-condbreak-fail.c, line 30. Starting program: /tmp/mi-condbreak-fail Program received signal SIGSEGV, Segmentation fault. 0x0000000000401116 in cond_fail () at /tmp/mi-condbreak-fail.c:24 24 return *p; /* Crash here. */ Error in testing condition for breakpoint 1: The program being debugged was signaled while in a function called from GDB. GDB has restored the context to what it was before the call. To change this behavior use "set unwindonsignal off". Evaluation of the expression containing the function (cond_fail) will be abandoned. (gdb) bt 1 #0 foo () at /tmp/mi-condbreak-fail.c:30 (More stack frames follow...) (gdb) This is the broken state. GDB is reports the SIGSEGV location, but not the unwound breakpoint location. The final 'bt 1' shows that the inferior is not located in cond_fail, which is the only location GDB reported, so this is clearly wrong. After implementing the fixes described above we now get this behaviour: $ gdb -q /tmp/mi-condbreak-fail \ -ex 'set unwindonsignal on' \ -ex 'break 30 if (cond_fail())' \ -ex 'run' Reading symbols from /tmp/mi-condbreak-fail... Breakpoint 1 at 0x40111e: file /tmp/mi-condbreak-fail.c, line 30. Starting program: /tmp/mi-condbreak-fail Error in testing breakpoint condition for breakpoint 1: The program being debugged was signaled while in a function called from GDB. GDB has restored the context to what it was before the call. To change this behavior use "set unwindonsignal off". Evaluation of the expression containing the function (cond_fail) will be abandoned. Breakpoint 1, foo () at /tmp/mi-condbreak-fail.c:30 30 global_counter += 1; /* Set breakpoint here. */ (gdb) This is better. GDB now reports a single stop at the location of the breakpoint, which is where the inferior is actually located. However, by removing the first stop notification we have lost some potentially useful information about which signal caused the inferior to stop. To address this I've reworked the message that is printed to include the signal information. GDB now reports this: $ gdb -q /tmp/mi-condbreak-fail \ -ex 'set unwindonsignal on' \ -ex 'break 30 if (cond_fail())' \ -ex 'run' Reading symbols from /tmp/mi-condbreak-fail... Breakpoint 1 at 0x40111e: file /tmp/mi-condbreak-fail.c, line 30. Starting program: /tmp/mi-condbreak-fail Error in testing condition for breakpoint 1: The program being debugged received signal SIGSEGV, Segmentation fault while in a function called from GDB. GDB has restored the context to what it was before the call. To change this behavior use "set unwindonsignal off". Evaluation of the expression containing the function (cond_fail) will be abandoned. Breakpoint 1, foo () at /tmp/mi-condbreak-fail.c:30 30 global_counter += 1; /* Set breakpoint here. */ (gdb) This is better, the user now sees a single stop notification at the correct location, and the error message describes which signal caused the inferior function call to stop. However, we have lost the information about where the signal occurred. I did consider trying to include this information in the error message, but, in the end, I opted not too. I wasn't sure it was worth the effort. If the user has selected to unwind on signal, then surely this implies they maybe aren't interested in debugging failed inferior calls, so, hopefully, just knowing the signal name will be enough. I figure we can always add this information in later if there's a demand for it.
2023-08-17[gdb/testsuite] Fix copy-to-remote in gdb.base/vfork-follow-parent.expTom de Vries1-1/+3
When running test-case gdb.base/vfork-follow-parent.exp, I run into: ... ERROR: tcl error sourcing gdb/testsuite/gdb.base/vfork-follow-parent.exp. ERROR: error copying "vforked-prog": no such file or directory while executing "file copy -force $fromfile $tofile" (procedure "gdb_remote_download" line 29) invoked from within "gdb_remote_download target $binfile3" ... Fix this by: - making the copy-to-remote conditional on is_remote target, and - allowing gdb_remote_download to find $binfile3 by using standard_output_file. Also remove unused variable remote_exec_prog. Tested on x86_64-linux.
2023-08-16gdb: fix vfork regressions when target-non-stop is offAndrew Burgess2-9/+70
It was pointed out on the mailing list[1] that after this commit: commit b1e0126ec56e099d753c20e91a9f8623aabd6b46 Date: Wed Jun 21 14:18:54 2023 +0100 gdb: don't resume vfork parent while child is still running the test gdb.base/vfork-follow-parent.exp now has some failures when run with the native-gdbserver or native-extended-gdbserver boards: FAIL: gdb.base/vfork-follow-parent.exp: resolution_method=schedule-multiple: continue to end of inferior 2 (timeout) FAIL: gdb.base/vfork-follow-parent.exp: resolution_method=schedule-multiple: inferior 1 (timeout) FAIL: gdb.base/vfork-follow-parent.exp: resolution_method=schedule-multiple: print unblock_parent = 1 (timeout) FAIL: gdb.base/vfork-follow-parent.exp: resolution_method=schedule-multiple: continue to break_parent (timeout) The reason that these failures don't show up when run on the standard unix board is that the test is only run in the default operating mode, so for Linux this will be all-stop on top of non-stop. If we adjust the test script so that it runs in the default mode and with target-non-stop turned off, then we see the same failures on the unix board. This commit includes this change. The way that the test is written means that it is not (currently) possible to turn on non-stop mode and have the test still work, so this commit does not do that. I have also updated the test script so that the vfork child performs an exec as well as the current exit. Exec and exit are the two ways in which a vfork child can release the vfork parent, so testing both of these cases is useful I think. In this test the inferior performs a vfork and the vfork-child immediately exits. The vfork-parent will wait for the vfork-child and then blocks waiting for gdb. Once gdb has released the vfork-parent, the vfork-parent also exits. In the test that fails, GDB sets 'detach-on-fork off' and then runs to the vfork. At this point the test tries to just "continue", but this fails as the vfork-parent is still selected, and the parent can't continue until the vfork-child completes. As the vfork-child is stopped by GDB the parent will never stop once resumed, so GDB refuses to resume it. The test script then sets 'schedule-multiple on' and once again continues. This time GDB, in theory, resumes both the parent and the child, the parent will be held blocked by the kernel, but the child will run until it exits, and which point GDB stops again, this time with inferior 2, the newly exited vfork-child, selected. What happens after this in the test script is irrelevant as far as this failure is concerned. To understand why the test started failing we should consider the behaviour of four different cases: 1. All-stop-on-non-stop before commit b1e0126ec56e, 2. All-stop-on-non-stop after commit b1e0126ec56e, 3. All-stop-on-all-stop before commit b1e0126ec56e, and 4. All-stop-on-all-stop after commit b1e0126ec56e. Only case #4 is failing after commit b1e0126ec56e, but I think the other cases are interesting because, (a) they inform how we might fix the regression, and (b) it turns out the behaviour of #2 changed too with the commit, but the change was harmless. For #1 All-stop-on-non-stop before commit b1e0126ec56e, what happens is: 1. GDB calls proceed with the vfork-parent selected, as schedule multiple is on user_visible_resume_ptid returns -1 (everything) as the resume_ptid (see proceed function), 2. As this is all-stop-on-non-stop, every thread is resumed individually, so GDB tries to resume both the vfork-parent and the vfork-child, both of which succeed, 3. The vfork-parent is held stopped by the kernel, 4. The vfork-child completes (exits) at which point the GDB sees the EXITED event for the vfork-child and the VFORK_DONE event for the vfork-parent, 5. At this point we might take two paths depending on which event GDB handles first, if GDB handles the VFORK_DONE first then: (a) As GDB is controlling both parent and child the VFORK_DONE is ignored (see handle_vfork_done), the vfork-parent will be resumed, (b) GDB processes the EXITED event, selects the (now defunct) vfork-child, and stops, returning control to the user. Alternatively, if GDB selects the EXITED event first then: (c) GDB processes the EXITED event, selects the (now defunct) vfork-child, and stops, returning control to the user. (d) At some future time the user resumes the vfork-parent, at which point the VFORK_DONE is reported to GDB, however, GDB is ignoring the VFORK_DONE (see handle_vfork_done), so the parent is resumed. For case #2, all-stop-on-non-stop after commit b1e0126ec56e, the important difference is in step (2) above, now, instead of resuming both the vfork-parent and the vfork-child, only the vfork-child is resumed. As such, when we get to step (5), only a single event, the EXITED event is reported. GDB handles the EXITED just as in (5)(c), then, later, when the user resumes the vfork-parent, the VFORKED_DONE is immediately delivered from the kernel, but this is ignored just as in (5)(d), and so, though the pattern of when the vfork-parent is resumed changes, the overall pattern of which events are reported and when, doesn't actually change. In fact, by not resuming the vfork-parent, the order of events (in this test) is now deterministic, which (maybe?) is a good thing. If we now consider case #3, all-stop-on-all-stop before commit b1e0126ec56e, then what happens is: 1. GDB calls proceed with the vfork-parent selected, as schedule multiple is on user_visible_resume_ptid returns -1 (everything) as the resume_ptid (see proceed function), 2. As this is all-stop-on-all-stop, the resume is passed down to the linux-nat target, the vfork-parent is the event thread, while the vfork-child is a sibling of the event thread, 3. In linux_nat_target::resume, GDB calls linux_nat_resume_callback for all threads, this causes the vfork-child to be resumed. Then in linux_nat_target::resume, the event thread, the vfork-parent, is also resumed. 4. The vfork-parent is held stopped by the kernel, 5. The vfork-child completes (exits) at which point the GDB sees the EXITED event for the vfork-child and the VFORK_DONE event for the vfork-parent, 6. We are now in a situation identical to step (5) as for all-stop-on-non-stop above, GDB selects one of the events to handle, and whichever we select the user sees the correct behaviour. And so, finally, we can consider #4, all-stop-on-all-stop after commit b1e0126ec56e, this is the case that started failing. We start out just like above, in proceed, the resume_ptid is -1 (resume everything), due to schedule multiple being on. And just like above, due to the target being all-stop, we call proceed_resume_thread_checked just once, for the current thread, which, remember, is the vfork-parent thread. The change in commit b1e0126ec56e was to avoid resuming a vfork-parent thread, read the commit message for the justification for this change. However, this means that GDB now rejects resuming the vfork-parent in this case, which means that nothing gets resumed! Obviously, if nothing resumes, then nothing will ever stop, and so GDB appears to hang. I considered a couple of solutions which, in the end, I didn't go with, these were: 1. Move the vfork-parent check out of proceed_resume_thread_checked, and place it in proceed, but only on the all-stop-on-non-stop path, this should still address the issue seen in b1e0126ec56e, but would avoid the issue seen here. I rejected this just because it didn't feel great to split the checks that exist in proceed_resume_thread_checked like this, 2. Extend the condition in proceed_resume_thread_checked by adding a target_is_non_stop_p check. This would have the same effect as idea 1, but leaves all the checks in the same place, which I think would be better, but this still just didn't feel right to me, and so, What I noticed was that for the all-stop-on-non-stop, after commit b1e0126ec56e, we only resumed the vfork-child, and this seems fine. The vfork-parent isn't going to run anyway (the kernel will hold it back), so if feels like we there's no harm in just waiting for the child to complete, and then resuming the parent. So then I started looking at follow_fork, which is called from the top of proceed. This function already has the task of switching between the parent and child based on which the user wishes to follow. So, I wondered, could we use this to switch to the vfork-child in the case that we are attached to both? Turns out this is pretty simple to do. Having done that, now the process is for all-stop-on-all-stop after commit b1e0126ec56e, and with this new fix is: 1. GDB calls proceed with the vfork-parent selected, but, 2. In follow_fork, and follow_fork_inferior, GDB switches the selected thread to be that of the vfork-child, 3. Back in proceed user_visible_resume_ptid returns -1 (everything) as the resume_ptid still, but now, 4. When GDB calls proceed_resume_thread_checked, the vfork-child is the current selected thread, this is not a vfork-parent, and so GDB allows the proceed to continue to the linux-nat target, 5. In linux_nat_target::resume, GDB calls linux_nat_resume_callback for all threads, this does not resume the vfork-parent (because it is a vfork-parent), and then the vfork-child is resumed as this is the event thread, At this point we are back in the same situation as for all-stop-on-non-stop after commit b1e0126ec56e, that is, the vfork-child is resumed, while the vfork-parent is held stopped by GDB. Eventually the vfork-child will exit or exec, at which point the vfork-parent will be resumed. [1] https://inbox.sourceware.org/gdb-patches/3e1e1db0-13d9-dd32-b4bb-051149ae6e76@simark.ca/
2023-08-16gdb, infcmd: support jump command in multi-inferior casePuputti, Matti1-86/+95
Fixes the issue where jump failed if multiple inferiors run the same source. See the below example $ gdb -q ./simple Reading symbols from ./simple... (gdb) break 2 Breakpoint 1 at 0x114e: file simple.c, line 2. (gdb) run Starting program: /temp/simple Breakpoint 1, main () at simple.c:2 2 int a = 42; (gdb) add-inferior [New inferior 2] Added inferior 2 on connection 1 (native) (gdb) inferior 2 [Switching to inferior 2 [<null>] (<noexec>)] (gdb) info inferiors Num Description Connection Executable 1 process 6250 1 (native) /temp/simple * 2 <null> 1 (native) (gdb) file ./simple Reading symbols from ./simple... (gdb) run Starting program: /temp/simple Thread 2.1 "simple" hit Breakpoint 1, main () at simple.c:2 2 int a = 42; (gdb) info inferiors Num Description Connection Executable 1 process 6250 1 (native) /temp/simple * 2 process 6705 1 (native) /temp/simple (gdb) jump 3 Unreasonable jump request (gdb) In this example, jump fails because the debugger finds two different locations, one for each inferior. Solution is to limit the search to the current program space. This is done by having the jump_command function use decode_line_with_current_source rather than decode_line_with_last_displayed, which makes sense, the *_current_source function always looks up a location based on the current thread's location -- if a user is asking the current thread to jump, then surely their destination should be relative to where the current thread is located. Then, inside decode_line_with_current_source, the call to decode_line_1 is updated to pass through the current program_space, which will limit the returned locations to those in the current program space. Approved-By: Andrew Burgess <aburgess@redhat.com>
2023-08-05[gdb/symtab] Find main language without symtab expansionTom de Vries1-0/+29
When loading an executable using "file a.out", the language is set according to a.out, which can involve looking up the language of symbol "main", which will cause the symtab expansion for the containing CU. Expansion of lto debug info can be slow, so in commit d3214198119 ("[gdb] Use partial symbol table to find language for main") a feature was added to avoid the symtab expansion. This feature stopped working after commit 7f4307436fd ("Fix "start" for D, Rust, etc"). [ The commit addresses problems related to command start, which requires finding the main function: - for language D, "main" was found instead of "D main", and - for Rust, the correct function was found, but attributed the wrong name (not fully qualified). ] Reimplement the feature by adding cooked_index_functions::lookup_global_symbol_language. Tested on x86_64-linux. PR symtab/30661 Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30661
2023-08-04[gdb/testsuite] Extend gdb.base/index-cache.exp furtherTom de Vries1-0/+9
Add lookup of a non-existing symbol to test-case gdb.base/index-cache.exp. This serves as regression test for PR symtab/30718. PR symtab/30718 Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30718
2023-08-04[gdb/testsuite] Move "maint wait-for-index-cache" ALAP in ↵Tom de Vries1-18/+18
gdb.base/index-cache.exp In test-case gdb.base/index-cache.exp proc run_test_with_flags contains: ... clean_restart ${testfile} # The tests generally want to check the cache, so make sure it # has completed its work. gdb_test_no_output "maintenance wait-for-index-cache" ... This however hides data races between: - index-cache writing (due to file $exec), and - symbol lookups (due to subsequent ptype commands). Fix this by: - moving the "maintenance wait-for-index-cache" to proc check_cache_stats, and - moving all calls to proc check_cache_stats ALAP. Tested on x86_64-linux.
2023-08-04[gdb/testsuite] Extend gdb.base/index-cache.expTom de Vries3-4/+48
The test-case gdb.base/index-cache.exp uses only one source file, which contains main. While doing "file $exec", in set_initial_language a symbol lookup of "main" is done, causing the symtab containing main to be expanded. Handling of main is special, and a future optimization may skip the lookup and expansion. Reliably exercise: - the lookup of main, expanding the symtab containing main, by doing "ptype main", and - the lookup of another symbol, expanding a symtab not containing main, by: - adding another source file containing function foo, and - doing "ptype foo". This triggered a segfault with target board native-extended-gdbserver, filed as PR symtab/30712, but that seems to be fixed by a previous commit in this series. Tested on x86_64-linux. Approved-By: Tom Tromey <tom@tromey.com> Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30712
2023-07-26[gdb/testsuite] Fix regexps in gdb.base/step-over-syscall.expTom de Vries1-4/+4
When running test-case gdb.base/step-over-syscall.exp without glibc debuginfo installed, I get: ... (gdb) continue^M Continuing.^M ^M Breakpoint 2, 0x00007ffff7d4405e in vfork () from /lib64/libc.so.6^M (gdb) PASS: gdb.base/step-over-syscall.exp: vfork: displaced=off: \ continue to vfork (1st time) ... but with glibc debuginfo installed I get instead: ... (gdb) continue^M Continuing.^M ^M Breakpoint 2, 0x00007ffff7d4405e in __libc_vfork () at \ ../sysdeps/unix/sysv/linux/x86_64/vfork.S:44^M 44 ENTRY (__vfork)^M (gdb) FAIL: gdb.base/step-over-syscall.exp: vfork: displaced=off: \ continue to vfork (1st time) ... The FAIL is due to a mismatch with regexp: ... "Breakpoint \[0-9\]+, (.* in |__libc_|)$syscall \\(\\).*" ... because it cannot match both ".* in " and the __libc_ prefix. Fix this by using instead the regexp: ... "Breakpoint \[0-9\]+, (.* in )?(__libc_)?$syscall \\(\\).*" ... Tested on x86_64-linux.
2023-07-23gdb/testsuite: replace $testfile with $binfile in one caseAndrew Burgess1-1/+1
For *reasons* I was hacking on gdb.base/foll-vfork.exp and wanted to change the name of the binary that was created. Should be easy, I adjusted the global $binfile variable .... but that didn't work. In one place the script uses $testfile instead of $binfile. Fixed this to use $binfile, now I can easily change the name of the generated binary, and the test still works. There's no change in what is tested after this commit.
2023-07-22[gdb/testsuite] Use proc readnow in two test-casesTom de Vries1-7/+4
Use "require !readnow" in two test-cases, instead of the written-out variant. Tested on x86_64-linux, with target boards unix and readnow.
2023-07-17gdb/testsuite: remove use of sleep from gdb.base/foll-vfork.expAndrew Burgess1-48/+0
While working on gdb.base/foll-vfork.exp I noticed that there are several random 'sleep' calls throughout the test. The comment suggests these are to allow for output from a vforked child to arrive, but in each case the test is about to close and restart GDB, so I don't see how random output from a child process could impact testing. I removed the sleep calls and couldn't reproduce any failures from this test, I left the test running for a couple of hours, and tried loading my machine, and the test seems fine with these removed. I've left this as a separate commit so that if, in the future, someone can show that these are required, it will be easy to revert this one patch and bring them back. There should be no change in what is tested after this commit.
2023-07-17gdb/testsuite: expand gdb.base/foll-vfork.expAndrew Burgess1-337/+304
This commit provides tests for all of the bugs fixed in the previous four commits, this is achieved by expanding gdb.base/foll-vfork.exp to run with different configurations: * target-non-stop on/off * non-stop on/off * schedule-multiple on/off We don't test with schedule-multiple on if we are using a remote target, this is due to bug gdb/30574. I've added a link to that bug in this commit, but all this commit does is expose that bug, there's no fixes here. Some of the bugs fixed in the previous commits are very timing dependent, as such, they don't always show up. I've had more success when running this test on a very loaded machine -- I usually run ~8 copies of the test in parallel, then the bugs would normally show up pretty quickly. Other than running the test in more configurations, I've not made any changes to what is actually being tested, other than in one place where, when testing with non-stop mode, GDB stops in a different inferior, as such I had to add a new 'inferior 2' call, this can be found in vfork_relations_in_info_inferiors. I have cleaned things up a little, for example, making use of proc_with_prefix to remove some with_test_prefix calls. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30574
2023-07-17gdb: catch more errors in gdb.base/foll-vfork.expAndrew Burgess1-2/+8
For *reasons* I was looking at gdb.base/foll-vfork.exp. This test script has a proc 'setup_gdb' that could (potentially) fail. The setup_gdb proc is called from many places and I, initially, didn't think that we were checking if setup_gdb had failed or not. My confusion was because I didn't understand what this tcl construct did: return -code return this will actually act as a return in the context of a proc's caller, effectively returning two levels of the call stack. Neat (I guess). So it turns out my worries were misplaced, everywhere setup_gdb is called, if setup_gdb fails then we will (magically) return. However, I did spot one place where we managed to confuse ourselves with our cleverness. In check_vfork_catchpoints, this proc is called to check that GDB is able to catch vforks or not. This proc is called early in the test script, and the intention is that, should this proc fail, we'll mark the whole test script as unsupported and then move onto the next test script. However, check_vfork_catchpoints also uses setup_gdb, and so, if that call to setup_gdb fails we'll end up returning immediately from check_vfork_catchpoints, and then continue with the test of _this_ test script, which is not correct. To fix this I see two choices, one is remove the use of 'return -code return' from setup_gdb, however, this would require every use of setup_gdb to then be placed inside: if { ![setup_gdb] } { return } Or, I can wrap the one call to setup_gdb in check_vfork_catchpoints and check its return code. I chose the second option as this is the smaller code change. There should be no change in what is tested after this commit.
2023-07-14Test that native targets can read a tdesc without a process attached.John Baldwin1-0/+27
This ensures that 'unset tdesc filename' does not generate any output on a "bare" native target inferior without an attached process.
2023-07-14Add a have_native_target helper function for use with require.John Baldwin1-17/+1
Move logic from auto-connect-native-target.exp into this helper.
2023-07-14gdb/cli: Improve UX when using list with no argsBruno Larsen1-4/+4
When using "list" with no arguments, GDB will first print the lines around where the inferior is stopped, then print the next N lines until reaching the end of file, at which point it warns the user "Line X out of range, file Y only has X-1 lines.". This is usually desirable, but if the user can no longer see the original line, they may have forgotten the current line or that a list command was used at all, making GDB's error message look cryptic. It was reported in bugzilla as PR cli/30497. This commit improves the user experience by changing the behavior of "list" slightly when a user passes no arguments. It now prints that the end of the file has been reached and recommends that the user use the command "list ." instead. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30497 Reviewed-By: Eli Zaretskii <eliz@gnu.org> Approved-By: Tom Tromey <tom@tromey.com>
2023-07-14gdb/cli: add '.' as an argument for 'list' commandBruno Larsen2-1/+40
Currently, after the user has used the list command once, there is no self-contained way to ask GDB to print the location where the inferior is stopped. The current best options require either using a separate command to scope out where the inferior is stopped, or using "list *$pc" requiring knowledge of GDB standard registers. This commit adds a way to do that using '.' as a new argument for the 'list' command. If the inferior isn't running, the command prints around the main function. Because this necessitated having the inferior running and the test was (seemingly unnecessarily) using printf in a non-essential way and it would make the resulting log harder to read for no benefit, it was replaced by a different statement. Reviewed-By: Eli Zaretskii <eliz@gnu.org> Approved-By: Tom Tromey <tom@tromey.com>
2023-07-10gdb/testsuite: avoid stack addresses in test namesAndrew Burgess1-1/+2
When comparing the test results for two different runs of GDB I noticed a difference in the results for gdb.base/frame-view.exp. The difference was caused by one of the tests including a stack address in the test name: PASS: gdb.base/frame-view.exp: with_pretty_printer=false: select-frame view 0x7ffff7c5cea8 0x40115b and PASS: gdb.base/frame-view.exp: with_pretty_printer=true: select-frame view 0x7ffff7c5cea8 0x40115b If for whatever reason the stack address changes between test runs then it becomes harder to compare results. Fix this by giving the test a descriptive name that doesn't include a stack address: PASS: gdb.base/frame-view.exp: with_pretty_printer=false: select thread 2 frame from thread 1 and PASS: gdb.base/frame-view.exp: with_pretty_printer=true: select thread 2 frame from thread 1 There's no change to what is actually tested after this commit.
2023-07-10gdb: include location number in breakpoint error messageAndrew Burgess2-12/+34
This commit improves the output of this previous commit: commit 2dc3457a454a35d0617dc1f9cc1db77468471f95 Date: Fri Oct 14 13:22:55 2022 +0100 gdb: include breakpoint number in testing condition error message The earlier commit extended the error message: Error in testing breakpoint condition: to include the breakpoint number, e.g.: Error in testing breakpoint condition 3: This commit extends takes this further, and includes the location number if the breakpoint has multiple locations, so we might now see: Error in testing breakpoint condition 3.2: Just as with how GDB reports a normal breakpoint stop, if a breakpoint only has a single location then the location number is not included, this keeps things nice and consistent. I've extended one of the tests to cover the new functionality. Approved-By: Pedro Alves <pedro@palves.net>
2023-07-07gdb: check max-value-size when reading strings for printfAndrew Burgess4-0/+15
I noticed that the printf code for strings, printf_c_string and printf_wide_c_string, don't take max-value-size into account, but do load a complete string from the inferior into a GDB buffer. As such it would be possible for an badly behaved inferior to cause GDB to try and allocate an excessively large buffer, potentially crashing GDB, or at least causing GDB to swap lots, which isn't great. We already have a setting to protect against this sort of thing, the 'max-value-size'. So this commit updates the two function mentioned above to check the max-value-size and give an error if the max-value-size is exceeded. If the max-value-size is exceeded, I chose to continue reading inferior memory to figure out how long the string actually is, we just don't store the results. The benefit of this is that when we give the user an error we can tell the user how big the string actually is, which would allow them to correctly adjust max-value-size, if that's what they choose to do. The default for max-value-size is 64k so there should be no user visible changes after this commit, unless the user was previously printing very large strings. If that is the case then the user will now need to increase max-value-size.
2023-07-07gdb: fix printf of wchar_t early in a gdb sessionAndrew Burgess2-0/+52
Given this test program: #include <wchar.h> const wchar_t wide_str[] = L"wide string"; int main (void) { return 0; } I observed this GDB behaviour: $ gdb -q /tmp/printf-wchar_t Reading symbols from /tmp/printf-wchar_t... (gdb) start Temporary breakpoint 1 at 0x40110a: file /tmp/printf-wchar_t.c, line 8. Starting program: /tmp/printf-wchar_t Temporary breakpoint 1, main () at /tmp/printf-wchar_t.c:8 25 return 0; (gdb) printf "%ls\n", wide_str (gdb) Notice that the printf results in a blank line rather than the expected 'wide string' output. I tracked the problem down to printf_wide_c_string (in printcmd.c), in this function we do this: struct type *wctype = lookup_typename (current_language, "wchar_t", NULL, 0); int wcwidth = wctype->length (); the problem here is that 'wchar_t' is a typedef. If we look at the comment on type::length() we see this: /* Note that if thistype is a TYPEDEF type, you have to call check_typedef. But check_typedef does set the TYPE_LENGTH of the TYPEDEF type, so you only have to call check_typedef once. Since value::allocate calls check_typedef, X->type ()->length () is safe. */ What this means is that after calling lookup_typename we should call check_typedef in order to ensure that the length of the typedef has been setup correctly. We are not doing this in printf_wide_c_string, and so wcwidth is incorrectly calculated as 0. This is what leads GDB to print an empty string. We can see in c_string_operation::evaluate (in c-lang.c) an example of calling check_typedef specifically to fix this exact issue. Initially I did fix this problem by adding a check_typedef call into printf_wide_c_string, but then I figured why not move the check_typedef call up into lookup_typename itself, that feels like it should be harmless when looking up a non-typedef type, but will avoid bugs like this when looking up a typedef. So that's what I did. I can then remove the extra check_typedef call from c-lang.c, I don't see any other places where we had extra check_typedef calls. This doesn't mean we definitely had bugs -- so long as we never checked the length, or, if we knew that check_typedef had already been called, then we would be fine. I don't see any test regressions after this change, and my new test case is now passing. Reviewed-By: Tom Tromey <tom@tromey.com>
2023-06-21[gdb/testsuite] Add have_host_localeTom de Vries1-0/+1
With test-case gdb.tui/pr30056.exp, I run into: ... sh: warning: setlocale: LC_ALL: cannot change locale (C.UTF-8)^M ... and then subsequently into: ... WARNING: timeout in accept_gdb_output FAIL: gdb.tui/pr30056.exp: Control-C ... This is on a CentOS 7 distro for powerpc64le. Either it has no C.UTF-8 support, or it's not installed: ... $ locale -a | grep ^C C $ ... Fix this by: - adding a new proc have_host_locale, and - using it in all test-cases using setenv LC_ALL. Tested on powerpc64le-linux and x86_64-linux.
2023-06-14[gdb/testsuite] Fix gdb.base/step-over-exit.exp with glibc debuginfoTom de Vries1-1/+1
In test-case gdb.base/step-over-exit.exp, we set a breakpoint on _exit and continue, expecting to hit the breakpoint. Without glibc debug info installed, we have with target board unix/-m64: ... Thread 2.1 "step-over-exit" hit Breakpoint 2.2, 0x00007ffff7d46aee in \ _exit () from /lib64/libc.so.6^M (gdb) PASS: gdb.base/step-over-exit.exp: continue to exit ... and with target board unix/-m32: ... Thread 2.1 "step-over-exit" hit Breakpoint 2.2, 0xf7d84c25 in _exit () from \ /lib/libc.so.6^M (gdb) PASS: gdb.base/step-over-exit.exp: continue to exit ... However after installing debug info (packages glibc-debuginfo and glibc-32bit-debuginfo), we have for -m64 (note: __GI__exit instead of _exit): ... Thread 2.1 "step-over-exit" hit Breakpoint 2.2, \ __GI__exit (status=<optimized out>) at \ ../sysdeps/unix/sysv/linux/_exit.c:27^M 27 {^M (gdb) PASS: gdb.base/step-over-exit.exp: continue to exit ... and -m32 (note: _Exit instead of _exit): ... Thread 2.1 "step-over-exit" hit Breakpoint 2.2, _Exit () at \ ../sysdeps/unix/sysv/linux/i386/_exit.S:24^M 24 ../sysdeps/unix/sysv/linux/i386/_exit.S: No such file or directory.^M (gdb) FAIL: gdb.base/step-over-exit.exp: continue to exit ... The gdb_test allows for both _exit and __GI__exit, but not _Exit: ... gdb_test "continue" \ "Continuing\\..*Breakpoint $decimal.*_exit \\(.*\\).*" \ "continue to exit" ... Fix this by allowing _Exit as well. Tested on x86_64-linux.
2023-06-05gdb: building inferior strings from within GDBAndrew Burgess5-1/+432
History Of This Patch ===================== This commit aims to address PR gdb/21699. There have now been a couple of attempts to fix this issue. Simon originally posted two patches back in 2021: https://sourceware.org/pipermail/gdb-patches/2021-July/180894.html https://sourceware.org/pipermail/gdb-patches/2021-July/180896.html Before Pedro then posted a version of his own: https://sourceware.org/pipermail/gdb-patches/2021-July/180970.html After this the conversation halted. Then in 2023 I (Andrew) also took a look at this bug and posted two versions: https://sourceware.org/pipermail/gdb-patches/2023-April/198570.html https://sourceware.org/pipermail/gdb-patches/2023-April/198680.html The approach taken in my first patch was pretty similar to what Simon originally posted back in 2021. My second attempt was only a slight variation on the first. Pedro then pointed out his older patch, and so we arrive at this patch. The GDB changes here are mostly Pedro's work, but updated by me (Andrew), any mistakes are mine. The tests here are a combinations of everyone's work, and the commit message is new, but copies bits from everyone's earlier work. Problem Description =================== Bug PR gdb/21699 makes the observation that using $_as_string with GDB's printf can cause GDB to print unexpected data from the inferior. The reproducer is pretty simple: #include <stddef.h> static char arena[100]; /* Override malloc() so value_coerce_to_target() gets a known pointer, and we know we"ll see an error if $_as_string() gives a string that isn't null terminated. */ void *malloc (size_t size) { memset (arena, 'x', sizeof (arena)); if (size > sizeof (arena)) return NULL; return arena; } int main () { return 0; } And then in a GDB session: $ gdb -q test Reading symbols from /tmp/test... (gdb) start Temporary breakpoint 1 at 0x4004c8: file test.c, line 17. Starting program: /tmp/test Temporary breakpoint 1, main () at test.c:17 17 return 0; (gdb) printf "%s\n", $_as_string("hello") "hello"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx (gdb) quit The problem above is caused by how value_cstring is used within py-value.c, but once we understand the issue then it turns out that value_cstring is used in an unexpected way in many places within GDB. Within py-value.c we have a null-terminated C-style string. We then pass a pointer to this string, along with the length of this string (so not including the null-character) to value_cstring. In value_cstring GDB allocates an array value of the given character type, and copies in requested number of characters. However value_cstring does not add a null-character of its own. This means that the value created by calling value_cstring is only null-terminated if the null-character is included in the passed in length. In py-value.c this is not the case, and indeed, in most uses of value_cstring, this is not the case. When GDB tries to print one of these strings the value contents are pushed to the inferior, and then read back as a C-style string, that is, GDB reads inferior memory until it finds a null-terminator. For the py-value.c case, no null-terminator is pushed into the inferior, so GDB will continue reading inferior memory until a null-terminator is found, with unpredictable results. Patch Description ================= The first thing this patch does is better define what the arguments for the two function value_cstring and value_string should represent. The comments in the header file are updated to describe whether the length argument should, or should not, include a null-character. Also, the data argument is changed to type gdb_byte. The functions as they currently exist will handle wide-characters, in which case more than one 'char' would be needed for each character. As such using gdb_byte seems to make more sense. To avoid adding casts throughout GDB, I've also added an overload that still takes a 'char *', but asserts that the character type being used is of size '1'. The value_cstring function is now responsible for adding a null character at the end of the string value it creates. However, once we start looking at how value_cstring is used, we realise there's another, related, problem. Not every language's strings are null terminated. Fortran and Ada strings, for example, are just an array of characters, GDB already has the function value_string which can be used to create such values. Consider this example using current GDB: (gdb) set language ada (gdb) p $_gdb_setting("arch") $1 = (97, 117, 116, 111) (gdb) ptype $ type = array (1 .. 4) of char (gdb) p $_gdb_maint_setting("test-settings string") $2 = (0) (gdb) ptype $ type = array (1 .. 1) of char This shows two problems, first, the $_gdb_setting and $_gdb_maint_setting functions are calling value_cstring using the builtin_char character, rather than a language appropriate type. In the first call, the 'arch' case, the value_cstring call doesn't include the null character, so the returned array only contains the expected characters. But, in the $_gdb_maint_setting example we do end up including the null-character, even though this is not expected for Ada strings. This commit adds a new language method language_defn::value_string, this function takes a pointer and length and creates a language appropriate value that represents the string. For C, C++, etc this will be a null-terminated string (by calling value_cstring), and for Fortran and Ada this can be a bounded array of characters with no null terminator. Additionally, this new language_defn::value_string function is responsible for selecting a language appropriate character type. After this commit the only calls to value_cstring are from the C expression evaluator and from the default language_defn::value_string. And the only calls to value_string are from Fortan, Ada, and ObjectC related code. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=21699 Co-Authored-By: Simon Marchi <simon.marchi@efficios.com> Co-Authored-By: Andrew Burgess <aburgess@redhat.com> Co-Authored-By: Pedro Alves <pedro@palves.net> Approved-By: Simon Marchi <simon.marchi@efficios.com>
2023-06-05[gdb] Fix grammar in comments and docsTom de Vries1-1/+1
Fix grammar in some comments and docs: - machines that doesn't -> machines that don't - its a -> it's a - its the -> it's the - if does its not -> if it does it's not - one more instructions if doesn't match -> one more instruction if it doesn't match - it's own -> its own - it's first -> its first - it's pointer -> its pointer I also came across "it's performance" in gdb/stubs/*-stub.c in the HP public domain notice, I've left that alone. Tested on x86_64-linux.