欢迎使用CSDN-markdown编辑器

简单的gdb调试

gdb是类unix下注明的调试工具,和gcc组成了程序的两把利刃。本文档主要阐述两个内容:

  1. 安装gdb
  2. 调试

安装

我们使用的环境是CentOS,使用RedHat的用户一样,同时假设已经安装好了gcc和g++。

[root$localhost ~]$ yum install -y gdb

安装完成后,使用gdb查看是否已经安装成功。

[root$localhost ~]$ gdb -v
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-90.el6)
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.

调试

调试之前,我们先得编写一个hello,world的C++代码。

简单的c++代码

#include "iostream"
int main(int argc, char** argv)
{
    std::cout<<"hello, world!"<<std::endl;
    return 0;
}

保存命名为main.cpp。

最简单的编译和运行

[root$localhost ~]$ g++ test.cpp
[root$localhost ~]$ ls
a.out  main.cpp

其中a.out是根据main.cpp自动生成的可执行文件。但是这样一个可执行文件不能进行gdb的调试,
如果强行调试,会出现如下结果:

[zoujing@localhost test]$ gdb a.out 
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-90.el6)
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/zoujing/test/a.out...(no debugging symbols found)...done.
(gdb) b main
Breakpoint 1 at 0x400818
(gdb) r
Starting program: /home/zoujing/test/a.out 

Breakpoint 1, 0x0000000000400818 in main ()
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.192.el6.x86_64 libgcc-4.4.7-17.el6.x86_64 libstdc++-4.4.7-17.el6.x86_64
(gdb)

带调试信息的编译和运行

[root$localhost ~]$ g++ -g test.cpp
[root$localhost ~]$ ls
a.out  main.cpp

a.out会替换之前的文件。这时,a.out里面会包含调试信息。如果比较之前的文件,通过readelf -h 查看a.out文件,我们可以看到两者的差异。
带有调试信息的输出:

[zoujing@localhost test]$ readelf a.out -h
ELF Header:
  Magic:   7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 
  Class:                             ELF64
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              EXEC (Executable file)
  Machine:                           Advanced Micro Devices X86-64
  Version:                           0x1
  Entry point address:               0x400730
  Start of program headers:          64 (bytes into file)
  Start of section headers:          31136 (bytes into file)
  Flags:                             0x0
  Size of this header:               64 (bytes)
  Size of program headers:           56 (bytes)
  Number of program headers:         8
  Size of section headers:           64 (bytes)
  Number of section headers:         37
  Section header string table index: 34

不带调试信息的输出:

[zoujing@localhost test]$ readelf -h a.release 
ELF Header:
  Magic:   7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 
  Class:                             ELF64
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              EXEC (Executable file)
  Machine:                           Advanced Micro Devices X86-64
  Version:                           0x1
  Entry point address:               0x400730
  Start of program headers:          64 (bytes into file)
  Start of section headers:          3648 (bytes into file)
  Flags:                             0x0
  Size of this header:               64 (bytes)
  Size of program headers:           56 (bytes)
  Number of program headers:         8
  Size of section headers:           64 (bytes)
  Number of section headers:         30
  Section header string table index: 27

我们可以很轻易的看出来,带调试信息的Number of section headers比不带调试信息的多了7个,同时Section header string table index比不带调试信息的多了7个。这多出来的就是debug的符号信息。可以了解readelf继续查看相关信息,在.symtab.symtab是符号表)里面会看到待调试的比不带调试的确实多了一部分信息。
下面的是具体的调试信息:

[zoujing@localhost test]$ gdb a.out 
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-90.el6)
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/zoujing/test/a.out...done.
(gdb) b main
Breakpoint 1 at 0x400823: file main.cpp, line 5.
(gdb) r
Starting program: /home/zoujing/test/a.out 

Breakpoint 1, main (argc=1, argv=0x7fffffffe918) at main.cpp:5
5       std::cout<<"hello, world!"<<std::endl;
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.192.el6.x86_64 libgcc-4.4.7-17.el6.x86_64 libstdc++-4.4.7-17.el6.x86_64
(gdb) l
1   #include "iostream"
2   
3   int main(int argc, char** argv)
4   {
5       std::cout<<"hello, world!"<<std::endl;
6       return 0;
7   }
8   
9   
(gdb) 

命令讲解

gdb file表示调试这个file文件。
b表示断点break。gdb支持函数断点,例如我们例子里面的main函数(不需要带参数)b main,也可以b + 行数,比如,我需要调试main函数里面的输出语言,我们可以b 5
l表示显示当前代码list。gdb支持显示任意行的代码,比如我想显示第5行的,我可以l 5

结语

gdb还有很多其他功能,和强大的gcc搭配,编译出的代码运行效率非常高,而且调试非常方便。

本项目是一个基于SSM(Spring+SpringMVC+MyBatis)后端框架与Vue.js前端框架开发的疫情居家办公系统。该系统旨在为居家办公的员工提供一个高效、便捷的工作环境,同时帮助企业更好地管理远程工作流程。项目包含了完整的数据库设计、前后端代码实现以及详细的文档说明,非常适合计算机相关专业的毕设学生和需要进行项目实战练习的Java学习者。 系统的核心功能包括用户管理、任务分配、进度跟踪、文件共享和在线沟通等。用户管理模块允许管理员创建和管理用户账户,分配不同的权限。任务分配模块使项目经理能够轻松地分配任务给团队成员,并设置截止日期。进度跟踪模块允许员工实时更新他们的工作状态,确保项目按计划进行。文件共享模块提供了一个安全的平台,让团队成员可以共享和协作处理文档。在线沟通模块则支持即时消息和视频会议,以增强团队之间的沟通效率。 技术栈方面,后端采用了Spring框架来管理业务逻辑,SpringMVC用于构建Web应用程序,MyBatis作为ORM框架简化数据库操作。前端则使用Vue.js来实现动态用户界面,搭配Vue Router进行页面导航,以及Vuex进行状态管理。数据库选用MySQL,确保数据的安全性和可靠性。 该项目不仅提供了一个完整的技术实现示例,还为开发者留下了扩展和改进的空间,可以根据实际需求添加新功能或优化现有功能。
本项目是一个基于SSM(Spring+SpringMVC+MyBatis)后端框架与Vue.js前端框架开发的网上球鞋竞拍系统。该项目旨在为球鞋爱好者提供一个便捷、高效的在线竞拍平台,用户可以在此平台上浏览、搜索、竞拍心仪的球鞋,并参与到各种有趣的竞拍活动中。 系统的主要功能包括用户注册登录、球鞋信息展示、竞拍活动创建与管理、实时竞拍以及交易安全保障等。用户可以通过注册账号后,浏览平台上发布的各类球鞋信息,包括品牌、型号、颜色、尺码以及当前竞拍状态等。系统支持用户创建和管理自己的竞拍活动,设定竞拍规则和时间,同时提供实时竞拍功能,确保公平、透明的交易过程。 在技术实现上,后端采用SSM框架进行开发,Spring负责业务逻辑层,SpringMVC处理Web请求,MyBatis进行数据库操作,保证了系统的稳定性和扩展性。前端则使用Vue.js框架,结合Axios进行数据请求,实现了前后端分离,提高了开发效率和用户体验。 数据库设计方面,系统采用了MySQL数据库,存储用户信息、球鞋信息、竞拍活动等数据,确保数据的安全性和完整性。此外,项目还包含了详细的文档资料,包括需求分析、系统设计、数据库设计以及测试报告等,为项目的实施和维护提供了有力的支持。 该项目不仅适合作为计算机相关专业学生的毕业设计题目,也适合Java学习者进行实战练习,通过在此基础上进行功能扩展和改进,可以进一步提升编程技能和项目管理能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值