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开发者_StackOverflow社区 of this file?
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.
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
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:
- Install pyperclip python library sudo zypper in python3-pyperclip
- Save the script above to a file, say file-path.py and copy it to ~/.gdb
- Update ~/.gdbinit with adding the following lines: source ~/.gdb/file-path.py
- Now you can copy the path and line to the clipboard with
clippath
in gdb
And read more about GDB Python API - link
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.
精彩评论