如何在CentOS 6上安装GDB
Zuoxiaojuan
・8 分钟阅读
GDB是CC 、ObjaveC、PASCAL、FORTRAN、GO、D、OpenCL C,ada 和Muldia 2的调试器。
CentOS基于RHEL(Red Hat Enterprise Linux)。RHEL的主要目标之一是成为一个稳定的服务器操作系统,这意味着新版本的软件包并不总是可用。
在撰写本文时,CentOS 6提供了GDB v7.2。然而,GDB团队最近发布了v7.91的代码。
官方建议运行不同的Linux发行版以使用更新版本的GDB。这并不理想,幸运的是,可以在CentOS 6上安装较新版本的GDB,因为GDB是调试器而不是系统核心组件,所以,使用较新版本是相当安全的。
本文介绍如何在CentOS 6.上同时安装支持和不支持的GDB版本。
我还会解释如何设置GDB,以便在使用C 标准库,如字符串,标准模板库,vector时更容易读取调试信息,这个特性被称为漂亮打印。
登录你的VPS,并设置你的用户帐户
登录你的VPS这可以通过在控制面板中单击"视图控制台或者使用SSH客户机来完成。
(a)作为root登录。
(b)创建自己的用户帐户,设置密码。
adduser <username> passwd <username>
(c)授予用户sudo访问权限。
visudo After the line"root ALL=(ALL) ALL" Add the line"<username> ALL=(ALL) ALL" --- If you aren't familiar with vi, go to the line"root ALL=(ALL) ALL". --- Hit"o" to create a new line after that line and enter insert mode. --- Type"<username> ALL=(ALL) ALL". --- Hit ESC. --- Type"ZZ" to save.
(d)以root身份注销,然后使用用户帐户重新登录,不用root身份来实际登录会更安全。使用sudo是一个更好的实践。
如果你想安装官方支持的(较旧)版本
安装GDB 。
sudo yum install gdb
检查已安装的版本,并查看其位置。
gdb --version May say: GNU gdb (GDB) Red Hat Enterprise Linux (7.2-75.el6) which gdb /usr/bin/gdb
如果你想从源代码安装一个新版本的GDB
安装一个C编译器,如GCC ,从源代码构建GDB不需要C编译器,但是需要它来演示GDB的漂亮打印功能。你可以通过执行本文中的步骤来构建来自源代码的最新版本的GCC 如何在CentOS 6上安装GCC ,或者你可以通过运行以下命令安装Centos6官方支持的GCC版本:
sudo yum install gcc gcc-c++
另外安装需要的软件包。
sudo yum install wget tar gzip ncurses-devel texinfo svn python-devel
确定要从源构建的GDB版本,访问GDB ftp站点以查看可下载的版本。
获取你想要的GDB版本的源,本文的其余部分是为v7.9.1编写的,将下载到
~/sourceInstallations/gdb-7.9.1/
中-你必须将适当的版本号替换为较新版本。mkdir ~/sourceInstallations cd ~/sourceInstallations wget ftp://ftp.gnu.org/gnu/gdb/gdb-7.9.1.tar.gz . tar -zxvf gdb-7.9.1.tar.gz
构建GDB,如果这正确完成,你将看到的最后一行将显示"success "。看到一些看似错误的消息快速滚动是很正常的。这些是安全的。
mkdir gdb-7.9.1.build cd gdb-7.9.1.build ../gdb-7.9.1/configure --with-python=yes && make && sudo make install && echo success --- If your VPS has multiple cores, you can speed up the build by changing the middle part --- of this line from"&& make &&" to"&& make -j <number of cores> &&". --- You can see the number of cores your VPS has by running"nproc" --- The parameter"--with-python=yes" is necessary for the pretty printing feature
安装C 漂亮打印。
cd ~/ svn co svn://gcc.gnu.org/svn/gcc/trunk/libstdc++-v3/python gdb_printers Create a file ~/.gdbinit of: python import sys sys.path.insert(0, '/home/<yourUserName>/gdb_printers/') from libstdcxx.v6.printers import register_libstdcxx_printers end --- One way to create this file is to run"vi ~/.gdbinit", hitting"i" to enter insert mode, --- typing the above file, hitting ESC, and hitting"ZZ" to save.
检查已安装的版本及其位置。
gdb --version May say: GNU gdb (GDB) 7.9.1 which gdb /usr/local/bin/gdb
可以选择运行GDB并查看漂亮打印。
mkdir ~/gdbExample cd ~/gdbExample Create a file gdbExample.cpp of: #include <string> #include <vector> using namespace std; int main() { string foo ="bar"; vector<string> vec; vec.push_back("foo"); vec.push_back("bar"); vec.push_back("foobar"); } --- One way to create this file is to run"vi gdbExample.cpp", hitting"i" to enter insert mode, --- typing the above file, hitting ESC, and hitting"ZZ" to save. g++ -ggdb gdbExample.cpp -o gdbExample Start GDB traditionally, by running"gdb ./gdbExample". Or, start GDB by using its terminal user interface (basically a text mode GUI), by running"gdb --tui ./gdbExample". Enter"break main" to set a breakpoint at the beginning of function main() -- and it will say: Breakpoint 1 at 0x<someAddress>: file gdbExample.cpp, line 6. Enter"run" to start the program, which will immediately hit the breakpoint you just set -- and it will say: Starting program: /home/<yourUserName>/gdbExample/gdbExample Breakpoint 1, main () at gdbExample.cpp:6 6 string foo ="bar"; Enter"next" and hit enter four times, and gdb will move up to just before executing: 10 vec.push_back("foobar"); Enter"print foo" and gdb will show: $1 ="bar" Enter"print vec" and gdb will show: $2 = std::vector of length 2, capacity 2 = {"foo","bar"} --- Remember, line 10 hasn't executed yet to add"foobar" to the vector Enter"quit" and"y" to quit anyway.
可选择回收硬盘空间。你的
~/sourcecinstallations
,保存目录可能是明智的,因为在某些时候可能需要使用可选配置选项,此外,构建进程会生成日志,以便日后出现问题时可以查看和处理日志。但是在运行之前的sudo make install- 重要:不要删除目录会占用大约386MB
~/gdb_printers/
目录!此目录的内容每次运行GDB时都会加载,它们没有被编译成GDB本身。cd ~/ rm -rf sourceInstallations --- Again, if you can spare the space, you may someday be happy to have left it there.