haoningabc 发表于 2013-1-26 12:34:13

gdb的helloworld

想起东软的1个linux老师,就讲了两天基本就把这书讲完了,
刚买了本书,一定要一星期内看完
《debug hacks》
源码在http://www.oreilly.co.jp/books/9784873114040/
日语,google翻译吧
奶奶的幸好哥我练过,要不被你成那啥呀~
先 ulimit -c 内核转储文件的大小限制
ulimit -c 1073741842
或开机启动
vim /etc/profile
ulimit -S -c unlimited > /dev/null 2>&1
再故意写个错代码
这个意思似乎是0是系统的,不能访问
test.c
#include <stdio.h>int main(){    int *a=NULL;    *a=0*1;    return 0;}
gcc -p -g test.c
./a.out
生成core*
gdb -c core.1234 ./a.out
l 6
Quit

------------------
在固定位置/var/core/生成内核转储
cat /etc/sysctl.conf
kernel.core_pattern=/var/core/%t-%e-%p-%c.corekernel.core_uses_pid=0
sysctl -p
cat /proc/sys/kernel/core_pattern


参考http://wenku.baidu.com/view/6518f4d376eeaeaad1f330e0.html
http://dsec.pku.edu.cn/~yuhj/wiki/gdb.html

#include <stdio.h>int globi=0;int func(int n){    globi=1;    int sum=0,i;    for(i=0; i<n; i++)    {      sum+=i;    }    return sum;}main(){    int i;    long result = 0;    for(i=1; i<=100; i++)    {      result += i;    }    printf("result = %d \n", result );    printf("result = %d \n", func(250) );}
cc -g tst.c -o tst
gdb tst
b 16   ------break 16行
info b -------info break
r --------run
n --------next
c --------continue
p i ----打印变量i
p sum
bt   --------查看函数堆栈。
set variable i=99   
c
q -----Quit
shell ls -------运行shell命令
make----

show args
set args

pwd
cd

info terminal
tty /dev/ttyb

watch globi ---------好像得是全局变量才能用
r

gdb <program> PID
调试运行的程序两种方法:
1、在UNIX下用ps查看正在运行的程序的PID(进程ID),然后用gdb <program> PID格式挂接正在运行的程序。
2、先用gdb <program>关联上源代码,并进行gdb,在gdb中用attach命令来挂接进程的PID。并用detach来取消挂接的进程

info program ------是否在运行

在gdb中,我们可以有以下几种暂停方式:断点(BreakPoint)、观
察点(Watch Point)、捕捉点(Catch Point)、信号(Signals)、
线程停止(Thread Stops)。如果要恢复程序运行,可以使用c或是
continue命令
页: [1]
查看完整版本: gdb的helloworld