You should add the path "c:\mingw\bin" in your environment variable. This way whenever you type gdb on command prompt, it would search from the above path.

And the same for valgrind?

Till date Valgrind does not run on Windows platform.

Answer from Mantosh Kumar on Stack Overflow
🌐
GNU Project
sourceware.org › gdb › current › onlinedocs › gdb.html › Source-Path.html
Source Path (Debugging with GDB)
When searching for source files on MS-DOS and MS-Windows, where absolute paths start with a drive letter (e.g. C:/project/foo.c), GDB will remove the drive letter from the file name before appending it to a search directory from source path; for instance if the executable references the source file C:/project/foo.c and source path is set to D:/mnt/cross, then GDB will search in the following locations for the source file:
🌐
GNU Project
sourceware.org › gdb › current › onlinedocs › gdb.html › Working-Directory.html
Working Directory (Debugging with GDB)
If you work on a system where GDB supports the info proc command (see Process Information), you can use the info proc command to find out the current working directory of the debuggee.
🌐
Medium
medium.com › @pamirghimire › debugging-with-gdb-on-windows-using-visual-studio-code-81ba70b562f3
Debugging with GDB on Windows using Visual Studio Code | by Pamir Ghimire | Medium
June 6, 2020 - So MinGW is the GCC port for Windows ... installation comes GDB, a classic debugger for C/C++ [2]. You can find it at path\\to\\MinGW\\bin\\gdb.exe`. So now you have a C/C++ compiler and a debugger....
🌐
OGG
rpg.hamsterrepublic.com › ohrrpgce › GDB_on_Windows
GDB on Windows
MinGW distributes a Windows version of gdb. You can get the latest mingw installer here which can in turn install gdb. After installing MinGW, run the "MinGW Installation Manager" (which for me was located in C:\MinGW\libexec\mingw-get\guimain.exe ) and then make sure that the mingw32-gdb bin package is installed. (These instructions are for mingw32, not the mingw-w64 fork. But gdb from mingw-w64 should work too.)
🌐
Kansas State University
textbooks.cs.ksu.edu › cis308 › 0-chapter › 0_5-debugging › tele.html
Debugging C programs :: CIS 308 Textbook
February 16, 2023 - Click Start, type Environment variables, and select “Edit the system environment variables”). Click “Environment Variables..”, then find Path under System variables and click “Edit…”. Look to see if the location of your gdb.exe ...
🌐
GNU
ftp.gnu.org › old-gnu › Manuals › gdb › html_node › gdb_48.html
Debugging with GDB - Source Path
When you start GDB, its source path includes only `cdir' and `cwd', in that order. To add other directories, use the directory command. ... Add directory dirname to the front of the source path. Several directory names may be given to this command, separated by `:' (`;' on MS-DOS and MS-Windows, where `:' usually appears as part of absolute file names) or whitespace.
Find elsewhere
🌐
GitHub
github.com › microsoft › vscode-cpptools › issues › 3076
miDebuggerPath value when gdb is in Windows PATH · Issue #3076 · microsoft/vscode-cpptools
January 23, 2019 - You switched accounts on another tab or window. Reload to refresh your session. ... debuggerfixedCheck the Milestone for the release in which the fix is or will be available.Check the Milestone for the release in which the fix is or will be available. ... Building and debugging any C program using MINGW64, when miDebuggerPath in launch.json is unspecified or set to "gdb", results in the error below. System PATH variable has gdb.exe.
Published   Jan 23, 2019
🌐
Super User
superuser.com › questions › 1396155 › where-does-gdb-get-installed-to-when-using-wsl-in-vs-code
windows subsystem for linux - Where does gdb get installed to when using WSL in VS Code? - Super User
What would my path be? ... This bug report might be relevant for your question. ... Try C:/dev/GDB/gdb.exe. ... He’s referring to gdb, the GNU Debugger. He only misspelled it every time except once. // It most certainly won’t be at C:\dev when he used WSL to install it. ... "C:\Users\jamie\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu18.04onWindows_79rhkp1fndgsc\LocalState\rootfs\usr\bin\gdb" ... Find the answer to your question by asking.
🌐
Dzyoba
alex.dzyoba.com › blog › gdb-source-path
How to point GDB to your sources | There is no magic here
You can direct GDB to the new source path right in the debug session with directory <dir> command: (gdb) list 6 ./Programs/python.c: No such file or directory. (gdb) directory /usr/src/python Source directories searched: /usr/src/python:$cdir:$cwd (gdb) list 6 #ifdef __FreeBSD__ 7 #include <fenv.h> 8 #endif 9 10 #ifdef MS_WINDOWS 11 int 12 wmain(int argc, wchar_t **argv) 13 { 14 return Py_Main(argc, argv); 15 }
🌐
VisualGDB
visualgdb.com › gdbreference › commands › set_solib-search-path
GDB Command Reference - set solib-search-path command
The default value for the solib-search-path variable is "." that corresponds to the working directory of GDB (directory where GDB was launched unless changed using the cd command).
Top answer
1 of 4
33

Use the info source command to get info for the current stack frame.

Here is an example of its output:

(gdb) info source
Current source file is /build/gtk+2.0-LJ3oCC/gtk+2.0-2.24.30/modules/input/gtkimcontextxim.c
Located in /home/sashoalm/Desktop/compile/gtk+2.0-2.24.30/modules/input/gtkimcontextxim.c
Contains 1870 lines.
Source language is c.
Producer is GNU C11 5.3.1 20160225 -mtune=generic -march=i686 -g -g -O2 -O2 -fstack-protector-strong -fPIC -fstack-protector-strong.
Compiled with DWARF 2 debugging format.
Does not include preprocessor macro info.
2 of 4
3

Excellent answer from Ciro Santill. However, the script needed a small correction to work with my gdb 8.0.1.

I also changed it to copy the text to the clipboard so I can use it in vim straight away. It works nicely with file_line.vim plugin. This is an example of the clipboard content produced by the script:

/home/ops1/projects/test01/main.cpp:5

The script is below:

import pyperclip

class Clippath (gdb.Command):
    """print absolute path"""
    def __init__(self):
        super(Clippath, self).__init__("clippath", gdb.COMMAND_USER)

    def invoke(self, arg, from_tty):
        symtabline = gdb.selected_frame().find_sal()
        pyperclip.copy(symtabline.symtab.fullname() + ":" + str(symtabline.line))

Clippath()

Here are the steps to make it all work:

  1. Install pyperclip python library sudo zypper in python3-pyperclip
  2. Save the script above to a file, say file-path.py and copy it to ~/.gdb
  3. Update ~/.gdbinit with adding the following lines: source ~/.gdb/file-path.py
  4. Now you can copy the path and line to the clipboard with clippath in gdb

And read more about GDB Python API - link

🌐
Desy
zeuthen.desy.de › unix › unixguide › infohtml › gdb › Source-Path.html
Source Path - Debugging with GDB
When you start gdb, its source path includes only `cdir' and `cwd', in that order. To add other directories, use the directory command. The search path is used to find both program source files and gdb script files (read using the `-command' option and `source' command).
🌐
GNU Project
sourceware.org › gdb › current › onlinedocs › gdb.html › Environment.html
Environment (Debugging with GDB)
Add directory to the front of the PATH environment variable (the search path for executables) that will be passed to your program. The value of PATH used by GDB does not change. You may specify several directory names, separated by whitespace or by a system-dependent separator character (‘:’ on Unix, ‘;’ on MS-DOS and MS-Windows).
🌐
VisualGDB
visualgdb.com › documentation › gdbsession
GDB Session window – VisualGDB Documentation
You can then use the search button to quickly find the relevant commands and understand how GDB responded to them. Source File List button displays all source files known to GDB at the moment. This list is derived from the debug symbols stored in the debugged executable and is independent from the contents of Solution Explorer. Use this button to troubleshoot problems with debug symbols, as it will show the exact paths of source files reported by GDB (i.e.
🌐
Apple Developer
developer.apple.com › library › archive › documentation › DeveloperTools › gdb › gdb › gdb_8.html
Debugging with gdb - Examining Source Files
When you start GDB, its source path includes only `cdir' and `cwd', in that order. To add other directories, use the directory command. ... Add directory dirname to the front of the source path. Several directory names may be given to this command, separated by `:' (`;' on MS-DOS and MS-Windows, where `:' usually appears as part of absolute file names) or whitespace.
🌐
Visual Studio Code
code.visualstudio.com › docs › cpp › config-mingw
Using GCC with MinGW
November 3, 2021 - The toolchain includes g++ and gdb. UCRT on Windows machines is only included in Windows 10 or later. If you are using another version of Windows, run the following command that does not use UCRT: pacman -S --needed base-devel mingw-w64-x86_64-toolchain · When adding the MinGW-w64 destination folder to your list of environment variables, the default path will then be: C:\msys64\mingw64\bin.