linux程序调试及调试工具使用笔记

本文详细介绍了Linux环境下几个常用的程序调试工具,包括YAMD(用于查找动态分配错误)、memwatch(内存泄漏检测工具)、strace和ltrace(系统调用跟踪工具)、gdb(GNU调试器)以及libtool(通用库支持脚本)。通过实例演示了如何获取、构建、安装和使用这些工具进行程序调试。
摘要由CSDN通过智能技术生成

title - Linux程序调试及调试工具使用笔记

0.参考资料
[1].http://www.ibm.com/developerworks/cn/linux/sdk/l-debug/
[2]. http://dsec.pku.edu.cn/~yuhj/wiki/gdb.html
[3].http://www.gnu.org/software/libtool/manual/libtool.html

1.YAMD - http://www.cs.hmc.edu/~nate/yamd/

YAMD is Yet Another Malloc Debugger. It's a package for finding dynamic allocation
related bugs in C and C++. It currently runs on Linux/x86 and DJGPP. The current
version is 0.32.

1.1. 获取源码&构建&安装
http://www.cs.hmc.edu/~nate/yamd/yamd-0.32.tar.gz
$tar xzf yamd-0.32.tar.gz
$cd yamd-0.32
~/yamd-0.32$make
~/yamd-0.32$sudo make install

1.2. 示例
// testyamd.c
#include <stdlib.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
    char *ptr1;
    char *ptr2;

    ptr1 = malloc(512);
    ptr2 = malloc(512);

    ptr2 = ptr1;
    free(ptr1);
    free(ptr2);

    return 0;
}

// 编译
gcc -g testyamd.c -o testyamd

// 运行
run-yamd ./testyamd

2. memwatch - http://www.linkdata.se/sourcecode/memwatch/

A memory leak detection tool. Basically, you add a header file to your souce code
files, and compile with MEMWATCH defined or not. The header file MEMWATCH.H
contains detailed instructions. This is a list of some of the features present
in version 2.71:
- ANSI C
- Logging to file or user function using TRACE() macro
- Fault tolerant, can repair it’s own data structures
- Detects double-frees and erroneous free’s
- Detects unfreed memory
- Detects overflow and underflow to memory buffers
- Can set maximum allowed memory to allocate, to stress-test app
- ASSERT(expr) and VERIFY(expr) macros
- Can detect wild pointer writes
- Support for OS specific address validation to avoid GP’s (segmentation faults)
- Collects allocation statistics on application, module or line level
- Rudimentary support for threads (see FAQ for details)

2.1. 获取源码
http://www.linkdata.se/downloads/sourcecode/memwatch/memwatch-2.71.tar.gz
$tar xzf memwatch-2.71.tar.gz

2.2. 运行自带示例
1)编辑test.c,注释掉最后一行
2)$make
3)$./a.out
4)$less memwatch.log

3. strace, truss, ltrace
[TBD]

4. gdb - http://www.gnu.org/software/gdb/

GDB, the GNU Project debugger, allows you to see what is going on `inside'
another program while it executes -- or what another program was doing at the
moment it crashed.

GDB can do four main kinds of things (plus other things in support of these)
to help you catch bugs in the act:

  * Start your program, specifying anything that might affect its behavior.
  * Make your program stop on specified conditions.
  * Examine what has happened, when your program has stopped.
  * Change things in your program, so you can experiment with correcting the
    effects of one bug and go on to learn about another.

The program being debugged can be written in Ada, C, C++, Objective-C, Pascal
(and many other languages). Those programs might be executing on the same machine
as GDB (native) or on another machine (remote). GDB can run on most popular UNIX
and Microsoft Windows variants.

5. libtool

GNU libtool is a generic library support script. Libtool hides the complexity of
using shared libraries behind a consistent, portable interface.

5.1. 调试(注意)
编译pidgin-2.7.9 后,直接调试:
$~/opensource/pidgin-2.7.9/pidgin$gdb ./pidgin
gdb会给出如下提示:
"/home/zhoubo/opensource/pidgin-2.7.9/pidgin/pidgin": not in executable format: 文件格式无法识别

正确调试方式([3]3.4):
~/opensource/pidgin-2.7.9/pidgin$libtool --mode=execute vimgdb ./pidgin

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值