linux下调试thread 类_linux下GDB调试

linux下GDB是一个非常强大的调试工具,但是他不像vs一样具有强大的图形界面,基本都靠命令来进行调试,对于新手来说也算是个坎。下面就跟大家一起探究一下gdb这个强大的调试工具。

1.开启core

1.1 查看core是否开启

$ulimit -c

0

0:表示关闭,不会生成core文件;否则此值说明core文件的最大限制;

1.2 打开开启core

打开/etc/profile

$sudo gedit /etc/profile

在文件末尾加上:

ulimit -S -c unlimited > /dev/null 2>&1

wq!保存退出。然后让设置立即生效:

$source /etc/profile

然后输入命令:

$ulimit -c

unlimited

core文件已经开启。unlimited为不限制core文件大小。

1.3 设置保存core文件的路径和命名方式

$sudo sysctl -w kernel.core_pattern=/tmp/core-%e.%p.%h.%t

/tmp目录为core文件的保存路径,core文件命名格式为:

%%:相当于%

%p:相当于

%u:相当于

%g:相当于

%s:相当于导致dump的信号的数字

%t:相当于dump的时间

%e:相当于执行文件的名称

%h:相当于hostname

2.编译

2.1 先准备一份简单的源码:

#include

#include

#include

#include

#include

void printinfo(const char* pstr)

{

printf("%s\n", pstr);

}

void* threadproc1(void* arg)

{

int i = 0;

while(i < 20)//

{

printf("I am threadproc1, tid:%u, i:%d\n", pthread_self(), i++);

sleep(1);

}

return NULL;

}

void* threadproc2(void* arg)

{

int i = 0;

while(i < 20)//

{

printf("I am threadproc2, tid:%u, i:%d\n", pthread_self(), i++);

sleep(1);

}

return NULL;

}

int main(int narg, const char** args)

{

const char* pstr = args[1];

printinfo(pstr);

pthread_t tid1,tid2;

pthread_create(&tid1,NULL,threadproc1,NULL);//创建线程1

pthread_create(&tid2,NULL,threadproc2,NULL);//创建线程2

pthread_join(tid1,NULL);//等待线程1

printf("thread1 joined\n");

pthread_join(tid2,NULL);//等待线程2

printf("thread2 joined\n");

free((void*)pstr);

char* pend = "main end\n";

printf(pend);

free(pend);

return 0;

}

2.2 编译源码

将主程序命名为gdbtest.cpp,使用g++进行编译:

$g++ -g -c gdbtest.cpp

$g++ -o gdbtest gdbtest.o -lpthread

注意,编译时需要添加-g选项,通知编译器生成gdb调试信息。代码编译完成后,将在当前目录下生成gdbtest可执行程序。

3 gdb调试

3.1 gdb调试常用命令说明

1.gdb $(filename)

启动gdb调试,$(filename)也可以使用file命令指定;

2.file $(filename)

设置gdb调试的文件名

3. set args $(arg1) $(arg2) $(arg3) ...

设置main函数输入参数

4.b/break linenum

设置断点,linenum为行号

5.r/run $(arg

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值