只是心态,抓住机会。
全部博文(1384)
分类: LINUX
2012-12-18 23:00:43
编写代码过程中少不了调试。在windows下面,我们有visual studio工具。在linux下面呢,实际上除了gdb工具之外,你没有别的选择。那么,怎么用gdb进行调试呢?我们可以一步一步来试试看。
调试的步骤基本如下所示,
(01) 首先,输入gdb test
(02) 进入到gdb的调试界面之后,输入list,即可看到test.c源文件
(03) 设置断点,输入 b main
(04) 启动test程序,输入run
(05) 程序在main开始的地方设置了断点,所以程序在printf处断住
(06) 这时候,可以单步跟踪。s单步可以进入到函数,而n单步则越过函数
(07) 如果希望从断点处继续运行程序,输入c
(08) 希望程序运行到函数结束,输入finish
(09) 查看断点信息,输入 info break
(10) 如果希望查看堆栈信息,输入bt
(11) 希望查看内存,输入 x/64xh + 内存地址
(12) 删除断点,则输入delete break + 断点序号
(13) 希望查看函数局部变量的数值,可以输入print + 变量名
(14)希望修改内存值,直接输入 print + *地址 = 数值
(15) 希望实时打印变量的数值,可以输入display + 变量名
(16) 查看函数的汇编代码,输入 disassemble + 函数名
(17) 退出调试输入quit即可
zhangbin@ubuntu:~$
zhangbin@ubuntu:~$ sudo su
[sudo] password for zhangbin:
Sorry, try again.
[sudo] password for zhangbin:
root@ubuntu:/home/zhangbin# apt-get install valgrind
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
libc6-dbg
Suggested packages:
kcachegrind alleyoop valkyrie
The following NEW packages will be installed:
libc6-dbg valgrind
0 upgraded, 2 newly installed, 0 to remove and 39 not upgraded.
Need to get 17.5 MB of archives.
After this operation, 63.3 MB of additional disk space will be used.
Do you want to continue [Y/n]? y
Get:1 precise-updates/main libc6-dbg i386 2.15-0ubuntu10.3 [2,575 kB]
Get:2 precise/main valgrind i386 1:3.7.0-0ubuntu3 [14.9 MB]
Fetched 17.5 MB in 50s (343 kB/s)
Selecting previously unselected package libc6-dbg.
(Reading database ... 324091 files and directories currently installed.)
Unpacking libc6-dbg (from .../libc6-dbg_2.15-0ubuntu10.3_i386.deb) ...
Selecting previously unselected package valgrind.
Unpacking valgrind (from .../valgrind_1%3a3.7.0-0ubuntu3_i386.deb) ...
Processing triggers for man-db ...
Setting up libc6-dbg (2.15-0ubuntu10.3) ...
Setting up valgrind (1:3.7.0-0ubuntu3) ...
root@ubuntu:/home/zhangbin#
zhangbin@ubuntu:~$
zhangbin@ubuntu:~$ sudo su
[sudo] password for zhangbin:
Sorry, try again.
[sudo] password for zhangbin:
root@ubuntu:/home/zhangbin# apt-get install valgrind
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
libc6-dbg
Suggested packages:
kcachegrind alleyoop valkyrie
The following NEW packages will be installed:
libc6-dbg valgrind
0 upgraded, 2 newly installed, 0 to remove and 39 not upgraded.
Need to get 17.5 MB of archives.
After this operation, 63.3 MB of additional disk space will be used.
Do you want to continue [Y/n]? y
Get:1 precise-updates/main libc6-dbg i386 2.15-0ubuntu10.3 [2,575 kB]
Get:2 precise/main valgrind i386 1:3.7.0-0ubuntu3 [14.9 MB]
Fetched 17.5 MB in 50s (343 kB/s)
Selecting previously unselected package libc6-dbg.
(Reading database ... 324091 files and directories currently installed.)
Unpacking libc6-dbg (from .../libc6-dbg_2.15-0ubuntu10.3_i386.deb) ...
Selecting previously unselected package valgrind.
Unpacking valgrind (from .../valgrind_1%3a3.7.0-0ubuntu3_i386.deb) ...
Processing triggers for man-db ...
Setting up libc6-dbg (2.15-0ubuntu10.3) ...
Setting up valgrind (1:3.7.0-0ubuntu3) ...
root@ubuntu:/home/zhangbin#
实时检测工具主要有 valgrind, Rational purify 等。
3.1 Valgrind
valgrind 是帮助程序员寻找程序里的 bug 和改进程序性能的工具。程序通过 valgrind 运行时,valgrind 收集各种有用的信息,通过这些信息可以找到程序中潜在的 bug 和性能瓶颈。
Valgrind 现在提供多个工具,其中最重要的是 Memcheck,Cachegrind,Massif 和 Callgrind。Valgrind 是在 Linux 系统下开发应用程序时用于调试内存问题的工具。它尤其擅长发现内存管理的问题,它可以检查程序运行时的内存泄漏问题。其中的 memecheck 工具可以用来寻找 c、c++ 程序中内存管理的错误。可以检查出下列几种内存操作上的错误: