binutils-gdb/gdb/filesystem.c
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

141 lines
4 KiB
C

/* Handle different target file systems for GDB, the GNU Debugger.
Copyright (C) 2010-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/>. */
#include "filesystem.h"
#include "filenames.h"
#include "gdbarch.h"
#include "cli/cli-cmds.h"
#include "inferior.h"
/* Same as HAVE_DOS_BASED_FILE_SYSTEM, but usable as an rvalue. */
#ifdef HAVE_DOS_BASED_FILE_SYSTEM
# define DOS_BASED_FILE_SYSTEM 1
#else
# define DOS_BASED_FILE_SYSTEM 0
#endif
const char file_system_kind_auto[] = "auto";
const char file_system_kind_unix[] = "unix";
const char file_system_kind_dos_based[] = "dos-based";
const char *const target_file_system_kinds[] =
{
file_system_kind_auto,
file_system_kind_unix,
file_system_kind_dos_based,
NULL
};
const char *target_file_system_kind = file_system_kind_auto;
const char *
effective_target_file_system_kind (void)
{
if (target_file_system_kind == file_system_kind_auto)
{
if (gdbarch_has_dos_based_file_system (current_inferior ()->arch ()))
return file_system_kind_dos_based;
else
return file_system_kind_unix;
}
else
return target_file_system_kind;
}
const char *
target_lbasename (const char *kind, const char *name)
{
if (kind == file_system_kind_dos_based)
return dos_lbasename (name);
else
return unix_lbasename (name);
}
/* See filesystem.h. */
bool
should_normalize_slashes ()
{
if (DOS_BASED_FILE_SYSTEM)
return true;
else
{
const char *fskind = effective_target_file_system_kind ();
return fskind == file_system_kind_dos_based;
}
}
/* See filesystem.h. */
char *
gdb_getcwd (char *buf, size_t size)
{
char *cwd = getcwd (buf, size);
/* Note this is returning a host path, so we only check
DOS_BASED_FILE_SYSTEM without considering the target filesystem
kind. */
if (cwd != nullptr && DOS_BASED_FILE_SYSTEM)
{
/* If CWD is non-NULL, then it's always a NULL-terminated
string. */
normalize_slashes (cwd);
}
return cwd;
}
static void
show_target_file_system_kind_command (struct ui_file *file,
int from_tty,
struct cmd_list_element *c,
const char *value)
{
if (target_file_system_kind == file_system_kind_auto)
gdb_printf (file, _("\
The assumed file system kind for target reported file names \
is \"%s\" (currently \"%s\").\n"),
value,
effective_target_file_system_kind ());
else
gdb_printf (file, _("\
The assumed file system kind for target reported file names \
is \"%s\".\n"),
value);
}
INIT_GDB_FILE (filesystem)
{
add_setshow_enum_cmd ("target-file-system-kind",
class_files,
target_file_system_kinds,
&target_file_system_kind, _("\
Set assumed file system kind for target reported file names."), _("\
Show assumed file system kind for target reported file names."),
_("\
If `unix', target file names (e.g., loaded shared library file names)\n\
starting the forward slash (`/') character are considered absolute,\n\
and the directory separator character is the forward slash (`/'). If\n\
`dos-based', target file names starting with a drive letter followed\n\
by a colon (e.g., `c:'), are also considered absolute, and the\n\
backslash (`\\') is also considered a directory separator. Set to\n\
`auto' (which is the default), to let GDB decide, based on its\n\
knowledge of the target operating system."),
NULL, /* setfunc */
show_target_file_system_kind_command,
&setlist, &showlist);
}