'How to view the full path of a file in GDB?

When I stop at a break point in gdb, it just shows the filename.cpp. How can I view the full pathname of this file?



Solution 1:[1]

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.

Solution 2:[2]

In Python scripting

To learn Python scripting, or if you want to see just the full path and nothing else:

class Curpath(gdb.Command):
    """
Print absolute path of the current file.
"""
    def __init__(self):
        super().__init__('curpath', gdb.COMMAND_FILES)
    def invoke(self, argument, from_tty):
        gdb.write(gdb.selected_frame().find_sal().symtab.fullname() + os.linesep)
Curpath()

Usage:

curpath

Solution 3:[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

Solution 4:[4]

Use filename-display to control how file names are displayed in GDB. To display absolute filenames use the following command in the beginning of GDB session:

set filename-display absolute

See documentation. This option appeared since GDB 7.6.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 sashoalm
Solution 2 Ciro Santilli Путлер Капут 六四事
Solution 3
Solution 4 ks1322