binutils-gdb/gdbsupport/pathstuff.h
Pedro Alves 7b0af7864c Windows: Normalize backslashes to forward slashes
Currently, on native Windows, GDB presents a mix of backslashes and
forward slashes to users, like e.g.:

 ...
 attach 15044
 Attaching to program: C:\msys2\home\alves\gdb\build-testsuite\outputs\gdb.base\attach\attach.exe, process 15044
 ...
 [Switching to thread 1 (Thread 15044.0x1294)]
 main () at C:/rocgdb/src/gdb/testsuite/gdb.base/attach.c:19
 19        while (! should_exit)
 (gdb) FAIL: gdb.base/attach.exp: do_attach_failure_tests: first attach
 ...

Note how above, the "attach" command uses backslashes, and the source
path uses forward slashes.

Most annoyingly, depending on compiler, you can end up with mixed
slash styles in the same path:

  Temporary breakpoint 1 at 0x1400010ec: file C:/rocgdb/src/gdb/testsuite/gdb.rocm\simple.cpp, line 33.
                                                                                  ^

A while ago, in commit a63213cd37 ("MSYS2+MinGW testing: Unix <->
Windows path conversion"), I had made the testsuite normalize
backslashes to forward slashes, among other things.  I had said in the
commit log there:

    "It's arguable whether GDB itself should do this sanitization.  I
    suspect it should.  I personally dislike seeing backward slashes in
    e.g., "info shared" output, or worse, mixed backward and forward
    slashes.  Still, I propose starting with a testsuite adjustment that
    moves us forward, and handle that separately.  I won't be surprised if
    we need the new routine for some cases even if we adjust GDB."

After running into some other tests that would need backslash
adjustment, and also because this is something user-visible, not just
a testsuite issue, I decided to bite the bullet and make GDB itself do
the slashes normalization.

This patch fixes all the cases of backslashes that I could find:

 - source files and compilation directory
 - executable name, and shared library names
 - cd/pwd commands

It does this by normalizing slashes at some strategic places.  The
conversion is only attempted if needed -- i.e., if debugging on
Windows, or cross-debugging Windows programs from a non-Windows host.

For executable/shared library names, this is doing the backslash ->
forward slash conversion completely on the host side in common code,
without touching the target backend files in either GDB or GDBserver,
by design, so that this is all completely a client side decision, and
works the same against any Windows GDBserver version, against remote
servers other than ours (e.g., Wine's), so we can change it later if
we want, etc.

I did tweak the gdb and gdbserver backends for the other way around
(forward slash => backslash), to make sure that they pass backslashes
to CreateProcess, so that inferiors see native backslashes in their
own argv[0].  For GDBserver, it would already have been possible to
launch an executable with forward slashes before (e.g., when debugging
from a Linux or Cygwin client), so this makes Windows gdbserver
behavior more consistent, independently of client, too.

I did not add an option to make this configurable at runtime on
purpose.  I don't see the point, it'd just add more complication and
testing surface.  Always normalizing keeps it simple.

For the source paths, I considered doing the normalization earlier in
the DWARF reader, but decided against it, because that would force
extra copying of strings -- the DWARF reader works mostly by passing
around const char pointers.  Also, the places I picked in principle
will automatically handle PDB if/when we get to it.

The buildsym.c change takes care of compilation directory paths.

The allocate_symtab change takes care of source paths.  That one
required a (temporary) copy, but I think that that's acceptable there.

The gdb_realpath change is needed because just before the touched code
there's a GetFullPathName call, which itself normalizes to
backslashes.

The exec.c and solib.c changes take care of executable and shared
libraries.

The change to testsuite/lib/gdb.exp, to adjust to forward-slash style
for DOS full names, are necessary otherwise MI tests break.  E.g.:

  *stopped,reason="breakpoint-hit",disp="del",bkptno="1",frame={addr="0x00007ff780461508",
  func="main",args=[],file="C:/rocgdb/src/gdb/testsuite/gdb.mi/basics.c",
  fullname="C:/rocgdb/src/gdb/testsuite/gdb.mi/basics.c",line="64",arch="i386:x86-64"},
  thread-id="1",stopped-threads="all"
  FAIL: gdb.mi/gdb2549.exp: mi runto main (unknown output after running)

gdb.base/set-cwd.exp was the only test I found (so far at least) that
required adjustment, but note that without this patch that testcase
fails and throws TCL errors, like:

  ...
  FAIL: gdb.base/set-cwd.exp: test_cwd_reset: inferior cwd is correctly set
  ERROR: couldn't compile regular expression pattern: invalid escape \ sequence
  ...

It passes cleanly with the patch.

Below's the before / after comparison of different commands / output.

Before:

  Reading symbols from ./outputs/gdb.rocm/simple/simple...
  (gdb)
  start
  Temporary breakpoint 1 at 0x1400010ec: file C:/rocgdb/src/gdb/testsuite/gdb.rocm\simple.cpp, line 33.
  Starting program: C:\msys2\home\alves\gdb\build-testsuite\outputs\gdb.rocm\simple\simple

  Thread 1 hit Temporary breakpoint 1, main () at C:/rocgdb/src/gdb/testsuite/gdb.rocm\simple.cpp:33
  33        hipError_t error = hipMalloc (&result_ptr, sizeof (int));

  Thread 1 hit Temporary breakpoint 1, main () at C:/rocgdb/src/gdb/testsuite/gdb.rocm\simple.cpp:33
  33        hipError_t error = hipMalloc (&result_ptr, sizeof (int));
  (gdb)

  (gdb) info inferiors
    Num  Description       Connection           Executable
  * 1    process 15620     1 (native)           C:\msys2\home\alves\gdb\build-testsuite\outputs\gdb.rocm\simple\simple

  (gdb) info sharedlibrary
  From                To                  Syms Read   Shared Object Library
  0x00007ffd22f91000  0x00007ffd231a69a8  Yes (*)     C:\Windows\SYSTEM32\ntdll.dll
  0x00007ffd21af1000  0x00007ffd21bb2520  Yes (*)     C:\Windows\System32\kernel32.dll
  0x00007ffd20091000  0x00007ffd20432548  Yes (*)     C:\Windows\System32\KernelBase.dll
  0x00007ffcc89c1000  0x00007ffcc9b145bc  Yes (*)     C:\Windows\SYSTEM32\amdhip64_7.dll
  0x00007ffd22911000  0x00007ffd22d829b0  Yes (*)     C:\Windows\System32\setupapi.dll
  0x00007ffd216c1000  0x00007ffd217653f0  Yes (*)     C:\Windows\System32\msvcrt.dll
  0x00007ffd22431000  0x00007ffd224de5c8  Yes (*)     C:\Windows\System32\advapi32.dll
  0x00007ffd21a41000  0x00007ffd21ae52b8  Yes (*)     C:\Windows\System32\sechost.dll
  0x00007ffd20651000  0x00007ffd20676af0  Yes (*)     C:\Windows\System32\bcrypt.dll
  0x00007ffd21f31000  0x00007ffd22045860  Yes (*)     C:\Windows\System32\rpcrt4.dll
  0x00007ffd22d91000  0x00007ffd22f40e50  Yes (*)     C:\Windows\System32\user32.dll
  0x00007ffd20781000  0x00007ffd207a43d8  Yes (*)     C:\Windows\System32\win32u.dll
  0x00007ffd228d1000  0x00007ffd228f73d8  Yes (*)     C:\Windows\System32\gdi32.dll
  0x00007ffd207b1000  0x00007ffd208d09c8  Yes (*)     C:\Windows\System32\gdi32full.dll
  0x00007ffd20b91000  0x00007ffd20c28410  Yes (*)     C:\Windows\System32\msvcp_win.dll
  0x00007ffd20471000  0x00007ffd2057f410  Yes (*)     C:\Windows\System32\ucrtbase.dll
  0x00007ffd21541000  0x00007ffd215afbc8  Yes (*)     C:\Windows\System32\ws2_32.dll
  0x00007ffd223f1000  0x00007ffd2241fc40  Yes (*)     C:\Windows\System32\imm32.dll

  (gdb) info source
  Current source file is C:/rocgdb/src/gdb/testsuite/gdb.rocm\simple.cpp
  Compilation directory is C:\msys2\home\alves\gdb\build-testsuite
  Located in C:\rocgdb\src\gdb\testsuite\gdb.rocm\simple.cpp

  (gdb) info sources
  C:\msys2\home\alves\gdb\build-testsuite\outputs\gdb.rocm\simple\simple:

  C:\rocgdb\src\gdb\testsuite\gdb.rocm\simple.cpp, C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\ucrt\corecrt_stdio_config.h,
  C:\Program Files\AMD\ROCm\7.1\include\hip\hip_runtime_api.h, C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\include\stdint.h,
  C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\ucrt\stdio.h, C:\Program Files\AMD\ROCm\7.1\lib\clang\21\include\__stddef_nullptr_t.h,
  C:\Program Files\AMD\ROCm\7.1\lib\clang\21\include\__stddef_size_t.h, C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\include\cstdlib,
  C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\ucrt\stdlib.h, C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\ucrt\corecrt_search.h,
  C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\ucrt\corecrt_malloc.h,
  ...

  (gdb) detach
  Detaching from program: C:\msys2\home\alves\gdb\build-testsuite\outputs\gdb.rocm\simple\simple, process 20800
  [Inferior 1 (process 20800) detached]

  (gdb) cd /tmp
  Working directory C:\tmp.
  (gdb) pwd
  Working directory C:\tmp.

After:

  Reading symbols from ./outputs/gdb.rocm/simple/simple...
  (gdb) start
  Temporary breakpoint 1 at 0x1400010ec: file C:/rocgdb/src/gdb/testsuite/gdb.rocm/simple.cpp, line 33.
  Starting program: C:/msys2/home/alves/gdb/build-testsuite/outputs/gdb.rocm/simple/simple

  Thread 1 hit Temporary breakpoint 1, main () at C:/rocgdb/src/gdb/testsuite/gdb.rocm/simple.cpp:33
  33        hipError_t error = hipMalloc (&result_ptr, sizeof (int));

  (gdb) info sources
  C:/msys2/home/alves/gdb/build-testsuite/outputs/gdb.rocm/simple/simple:

  C:/rocgdb/src/gdb/testsuite/gdb.rocm/simple.cpp, C:/Program Files (x86)/Windows Kits/10/Include/10.0.22621.0/ucrt/corecrt_stdio_config.h,
  C:/Program Files/AMD/ROCm/7.1/include/hip/hip_runtime_api.h, C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.39.33519/include/stdint.h,
  C:/Program Files (x86)/Windows Kits/10/Include/10.0.22621.0/ucrt/stdio.h, C:/Program Files/AMD/ROCm/7.1/lib/clang/21/include/__stddef_nullptr_t.h,
  C:/Program Files/AMD/ROCm/7.1/lib/clang/21/include/__stddef_size_t.h, C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.39.33519/include/cstdlib,
  C:/Program Files (x86)/Windows Kits/10/Include/10.0.22621.0/ucrt/stdlib.h, C:/Program Files (x86)/Windows Kits/10/Include/10.0.22621.0/ucrt/corecrt_search.h,
  C:/Program Files (x86)/Windows Kits/10/Include/10.0.22621.0/ucrt/corecrt_malloc.h,
  ...

  (gdb) info inferiors
    Num  Description       Connection           Executable
  * 1    process 19920     1 (native)           C:/msys2/home/alves/gdb/build-testsuite/outputs/gdb.rocm/simple/simple

  (gdb) info shared
  From                To                  Syms Read   Shared Object Library
  0x00007ffd22f91000  0x00007ffd231a69a8  Yes (*)     C:/Windows/SYSTEM32/ntdll.dll
  0x00007ffd21af1000  0x00007ffd21bb2520  Yes (*)     C:/Windows/System32/kernel32.dll
  0x00007ffd20091000  0x00007ffd20432548  Yes (*)     C:/Windows/System32/KernelBase.dll
  0x00007ffcc89c1000  0x00007ffcc9b145bc  Yes (*)     C:/Windows/SYSTEM32/amdhip64_7.dll
  0x00007ffd22911000  0x00007ffd22d829b0  Yes (*)     C:/Windows/System32/setupapi.dll
  0x00007ffd216c1000  0x00007ffd217653f0  Yes (*)     C:/Windows/System32/msvcrt.dll
  0x00007ffd22431000  0x00007ffd224de5c8  Yes (*)     C:/Windows/System32/advapi32.dll
  0x00007ffd21a41000  0x00007ffd21ae52b8  Yes (*)     C:/Windows/System32/sechost.dll
  0x00007ffd20651000  0x00007ffd20676af0  Yes (*)     C:/Windows/System32/bcrypt.dll
  0x00007ffd21f31000  0x00007ffd22045860  Yes (*)     C:/Windows/System32/rpcrt4.dll
  0x00007ffd22d91000  0x00007ffd22f40e50  Yes (*)     C:/Windows/System32/user32.dll
  0x00007ffd20781000  0x00007ffd207a43d8  Yes (*)     C:/Windows/System32/win32u.dll
  0x00007ffd228d1000  0x00007ffd228f73d8  Yes (*)     C:/Windows/System32/gdi32.dll
  0x00007ffd207b1000  0x00007ffd208d09c8  Yes (*)     C:/Windows/System32/gdi32full.dll
  0x00007ffd20b91000  0x00007ffd20c28410  Yes (*)     C:/Windows/System32/msvcp_win.dll
  0x00007ffd20471000  0x00007ffd2057f410  Yes (*)     C:/Windows/System32/ucrtbase.dll
  0x00007ffd21541000  0x00007ffd215afbc8  Yes (*)     C:/Windows/System32/ws2_32.dll
  0x00007ffd223f1000  0x00007ffd2241fc40  Yes (*)     C:/Windows/System32/imm32.dll
  (*): Shared library is missing debugging information.

  (gdb) detach
  Detaching from program: C:/msys2/home/alves/gdb/build-testsuite/outputs/gdb.rocm/simple/simple, process 7816
  [Inferior 1 (process 7816) detached]

  (gdb) cd /tmp
  Working directory C:/tmp.
  (gdb) pwd
  Working directory C:/tmp.

Reviewed-By: Eli Zaretskii <eliz@gnu.org>
Reviewed-By: Andrew Burgess <aburgess@redhat.com>
Change-Id: I6366c41d200938259330e60bc1e98b576f787518
2026-07-06 22:13:07 +01:00

188 lines
6.4 KiB
C++

/* Path manipulation routines for GDB and gdbserver.
Copyright (C) 1986-2026 Free Software Foundation, Inc.
This file is part of GDB.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#ifndef GDBSUPPORT_PATHSTUFF_H
#define GDBSUPPORT_PATHSTUFF_H
#include "gdbsupport/byte-vector.h"
#include "gdbsupport/array-view.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <array>
/* Path utilities. */
/* String containing the current directory (what getwd would return). */
extern char *current_directory;
/* Normalize backslashes to forward slashes in the null-terminated
PATH string. Returns PATH. */
extern char *normalize_slashes (char *path);
/* Return the real path of FILENAME, expanding all the symbolic links.
Contrary to "gdb_abspath", this function does not use
CURRENT_DIRECTORY for path expansion. Instead, it relies on the
current working directory (CWD) of GDB or gdbserver. */
extern gdb::unique_xmalloc_ptr<char> gdb_realpath (const char *filename);
/* Return a copy of FILENAME, with its directory prefix canonicalized
by gdb_realpath. */
extern std::string gdb_realpath_keepfile (const char *filename);
/* Return PATH in absolute form, performing tilde-expansion if necessary.
PATH cannot be NULL or the empty string.
This does not resolve symlinks however, use gdb_realpath for that.
Contrary to "gdb_realpath", this function uses CWD for the path
expansion. This may lead to scenarios the current working
directory is different than CWD.
If CWD is NULL, this function returns a copy of PATH. */
extern std::string gdb_abspath (const char *path,
const char *cwd = current_directory);
/* Overload of gdb_abspath which takes std::string. */
static inline std::string
gdb_abspath (const std::string &path)
{
return gdb_abspath (path.c_str ());
}
/* Overload of gdb_abspath which takes gdb::unique_xmalloc_ptr<char>. */
static inline std::string
gdb_abspath (const gdb::unique_xmalloc_ptr<char> &path)
{
return gdb_abspath (path.get ());
}
/* If the path in CHILD is a child of the path in PARENT, return a
pointer to the first component in the CHILD's pathname below the
PARENT. Otherwise, return NULL. */
extern const char *child_path (const char *parent, const char *child);
/* Join elements in PATHS into a single path.
The first element can be absolute or relative. Only a single directory
separator will be placed between elements of PATHS, if one element ends
with a directory separator, or an element starts with a directory
separator, then these will be collapsed into a single separator. */
extern std::string path_join (gdb::array_view<const char *> paths);
/* Same as the above, but accept paths as distinct parameters. */
template<typename ...Args>
std::string
path_join (Args... paths)
{
/* It doesn't make sense to join less than two paths. */
static_assert (sizeof... (Args) >= 2);
std::array<const char *, sizeof... (Args)> path_array
{ paths... };
return path_join (gdb::array_view<const char *> (path_array));
}
/* Return whether PATH contains a directory separator character. */
extern bool contains_dir_separator (const char *path);
/* Get the usual user cache directory for the current platform.
On Linux, it follows the XDG Base Directory specification: use
$XDG_CACHE_HOME/gdb if the XDG_CACHE_HOME environment variable is
defined, otherwise $HOME/.cache.
On macOS, it follows the local convention and uses
~/Library/Caches/gdb.
The return value is absolute and tilde-expanded. Return an empty
string if neither XDG_CACHE_HOME (on Linux) or HOME are defined. */
extern std::string get_standard_cache_dir ();
/* Get the usual temporary directory for the current platform.
On Windows, this is the TMP or TEMP environment variable.
On the rest, this is the TMPDIR environment variable, if defined, else /tmp.
Throw an exception on error. */
extern std::string get_standard_temp_dir ();
/* Get the usual user config directory for the current platform.
On Linux, it follows the XDG Base Directory specification: use
$XDG_CONFIG_HOME/gdb if the XDG_CONFIG_HOME environment variable is
defined, otherwise $HOME/.config.
On macOS, it follows the local convention and uses
~/Library/Preferences/gdb.
The return value is absolute and tilde-expanded. Return an empty
string if neither XDG_CONFIG_HOME (on Linux) or HOME are defined. */
extern std::string get_standard_config_dir ();
/* Look for FILENAME in the standard configuration directory as returned by
GET_STANDARD_CONFIG_DIR and return the path to the file. No check is
performed that the file actually exists or not.
If FILENAME begins with a '.' then the path returned will remove the
leading '.' character, for example passing '.gdbinit' could return the
path '/home/username/.config/gdb/gdbinit'. */
extern std::string get_standard_config_filename (const char *filename);
/* Look for a file called NAME in either the standard config directory or
in the users home directory. If a suitable file is found then *BUF will
be filled with the contents of a call to 'stat' on the found file,
otherwise *BUF is undefined after this call.
If NAME starts with a '.' character then, when looking in the standard
config directory the file searched for has the '.' removed. For
example, if NAME is '.gdbinit' then on a Linux target GDB might look for
'~/.config/gdb/gdbinit' and then '~/.gdbinit'. */
extern std::string find_gdb_home_config_file (const char *name,
struct stat *buf);
/* Return the file name of the user's shell. Normally this comes from
the SHELL environment variable. */
extern const char *get_shell ();
/* Make a filename suitable to pass to mkstemp based on F (e.g.
/tmp/foo -> /tmp/foo-XXXXXX). */
extern gdb::char_vector make_temp_filename (const std::string &f);
#endif /* GDBSUPPORT_PATHSTUFF_H */