aboutsummaryrefslogtreecommitdiff
path: root/gdbsupport
diff options
context:
space:
mode:
authorAndrew Burgess <aburgess@redhat.com>2024-10-26 16:15:27 +0100
committerAndrew Burgess <aburgess@redhat.com>2024-12-09 11:01:00 +0000
commitd9df3857da0cef29ed9c0ef75f90700c5f392986 (patch)
treeacfedb801859bced1eca5bd14dac3df1b51b9150 /gdbsupport
parentcced05faea3a51115a4203827e778057607372cc (diff)
downloadbinutils-d9df3857da0cef29ed9c0ef75f90700c5f392986.zip
binutils-d9df3857da0cef29ed9c0ef75f90700c5f392986.tar.gz
binutils-d9df3857da0cef29ed9c0ef75f90700c5f392986.tar.bz2
gdb: allow core file containing special characters on the command line
After the commit: commit 03ad29c86c232484f9090582bbe6f221bc87c323 Date: Wed Jun 19 11:14:08 2024 +0100 gdb: 'target ...' commands now expect quoted/escaped filenames it was no longer possible to pass GDB the name of a core file containing any special characters (white space or quote characters) on the command line. For example: $ gdb -c /tmp/core\ file.core Junk after filename "/tmp/core": file.core (gdb) The problem is that the above commit changed the 'target core' command to expect quoted filenames, so before the above commit a user could write: (gdb) target core /tmp/core file.core [New LWP 2345783] Core was generated by `./mkcore'. Program terminated with signal SIGSEGV, Segmentation fault. #0 0x0000000000401111 in ?? () (gdb) But after the above commit the user must write: (gdb) target core /tmp/core\ file.core or (gdb) target core "/tmp/core file.core" This is part of a move to make GDB's filename argument handling consistent. Anyway, the problem with the '-c' command line flag is that it forwards the filename unmodified through to the 'core-file' command, which in turn forwards to the 'target core' command. So when the user, at a shell writes: $ gdb -c "core file.core" this arrives in GDB as the unquoted string 'core file.core' (without the single quotes). GDB then forwards this to the 'core-file' command as if the user had written this at a GDB prompt: (gdb) core-file core file.core Which then fails to parse due to the unquoted white space between 'core' and 'file.core'. The solution I propose is to escape any special characters in the core file name passed from the command line before calling 'core-file' command from main.c. I've updated the corefile.exp test to include a test for passing a core file containing a white space character. While I was at it I've modernised the part of corefile.exp that I was touching.
Diffstat (limited to 'gdbsupport')
-rw-r--r--gdbsupport/common-utils.cc21
-rw-r--r--gdbsupport/common-utils.h5
2 files changed, 26 insertions, 0 deletions
diff --git a/gdbsupport/common-utils.cc b/gdbsupport/common-utils.cc
index 91c14a0..af25bf0 100644
--- a/gdbsupport/common-utils.cc
+++ b/gdbsupport/common-utils.cc
@@ -222,6 +222,27 @@ extract_string_maybe_quoted (const char **arg)
return result;
}
+/* See gdbsupport/common-utils.h. */
+
+std::string
+make_quoted_string (const char *str)
+{
+ gdb_assert (str != nullptr);
+
+ std::string result;
+
+ for (; *str != '\0'; ++str)
+ {
+ const char ch = *str;
+
+ if (strchr ("\"' \t\n", ch) != nullptr)
+ result.push_back ('\\');
+ result.push_back (ch);
+ }
+
+ return result;
+}
+
/* The bit offset of the highest byte in a ULONGEST, for overflow
checking. */
diff --git a/gdbsupport/common-utils.h b/gdbsupport/common-utils.h
index 2fb2291..6515838 100644
--- a/gdbsupport/common-utils.h
+++ b/gdbsupport/common-utils.h
@@ -84,6 +84,11 @@ char *savestring (const char *ptr, size_t len);
std::string extract_string_maybe_quoted (const char **arg);
+/* Return a copy of STR, but with any white space, single quote, or
+ double quote characters escaped with a backslash. */
+
+std::string make_quoted_string (const char *str);
+
/* The strerror() function can return NULL for errno values that are
out of range. Provide a "safe" version that always returns a
printable string. This version is also thread-safe. */