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

时间:2021-11-23 01:00:41

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