当前位置:网站首页>【gdb调试工具】| 如何在多线程、多进程以及正在运行的程序下调试
【gdb调试工具】| 如何在多线程、多进程以及正在运行的程序下调试
2022-06-24 07:48:00 【Jxiepc】
1、gdb基本命令
- bread【b】:设置断点,且可设置多个;
如:b 20 => 设置断点在20行;
- run【r】:运行程序,若遇到断点会停止;
- backtrace【bt】:查看堆栈信息;
- next【n】:执行当前语句,不会进入内部;
- step【s】:单步执行,会进入程序内部;
- print【p】:显示变量值;
如:p name => 显示变量name的值;
- continue【c】:继续程序的运行,直到遇到下一个断点;
- set var name=value:设置变量的值;
需要在编译时加上-g参数表示gdb;
2、生成core文件
当程序挂掉后,系统不会缺省生成core文件;
ulimit -a:查看系统参数;
ulimit -c unlimited:将core文件的大小设为无限制;
后运行程序,若错误,即可生成core文件;
gdb 程序名 core文件名:即可进入查看;




测试源码
void test() {
int *a;
if(a == 10) {
cout << "***" << endl;
}
}
3、调式正在运行的程序
通过获取当前程序的进程【ps -aux】查看;
在使用【gdb 程序名 -p 进程号】;


测试程序
#include <unistd.h>
#include <iostream>
int main(int argc, char* argv[])
{
int i=10000;
for(;i>=0; --i) {
sleep(1);
std::cout << i << std::endl;
}
return 0;
}
4、调试多进程程序
缺省调试的是父进程;
在gdb中通过以下选项来选择父子进程:
- set follow-fork-mode parent;
- set follow-fork-mode child;
设置调试模式【set detach-on-fork [on/off]】:
- 表示当前进程,其他进程继续运行,则用on(缺省);
- 表示当前进程,其他进程被挂起,则用off;
info inferiors:查看调试的进程;
inferior 进程id:切换当前调试的进程;




测试程序
#include <unistd.h>
#include <iostream>
int main(int argc, char* argv[])
{
pid_t pid;
pid = fork();
if(pid == 0) {
// 子进程
std::cout << "子进程:" << std::endl;
for(int i=0; i<10; i++) {
sleep(1);
std::cout << "子:" << i << std::endl;
}
}else if(pid > 0) {
std::cout << "父进程:" << std::endl;
for(int i=0; i<10; i++) {
sleep(1);
std::cout << "父:" << i << std::endl;
}
}else {
std::cout << "fork error" << std::endl;
exit(-1);
}
return 0;
}
5、调试多线程程序
【查看进程、线程】:
ps aux | grep 筛选名称:查看当前运行的进程;
ps -aL | grep 筛选名称:查看当前运行的轻量级进程;
pstree -p 主线程id:查看主线程和新线程的关系;
【gdb中】:
info threads:查看线程;
thread 线程id:切换线程;
set scheduler-locking on:只允许当前线程;
set scheduler-locking off:运行全部的线程;
thread apply 线程id cmd:指定某线程某gdb命令;
thread apply all cmd:全部的线程执行某gdb命令;




边栏推荐
猜你喜欢

Leetcode -- wrong set

linux(centos7.9)安装部署mysql-cluster 7.6

【LeetCode】415. 字符串相加
![[noi Simulation Competition] send (tree DP)](/img/5b/3beb9f5fdad00b6d5dc789e88c6e98.png)
[noi Simulation Competition] send (tree DP)
![[Niuke] convert string to integer](/img/56/3e491b3d0eea0d89a04d0b871501d7.png)
[Niuke] convert string to integer

MySQL | store notes of Master Kong MySQL from introduction to advanced

Redis实现全局唯一ID

【牛客】把字符串转换成整数

JS to find and update the specified value in the object through the key

The list of open source summer winners has been publicized, and the field of basic software has become a hot application this year
随机推荐
[e325: attention] VIM editing error
On the routing tree of gin
Change SSH port number
What is SRE? A detailed explanation of SRE operation and maintenance system
2022-06-23:给定一个非负数组,任意选择数字,使累加和最大且为7的倍数,返回最大累加和。 n比较大,10的5次方。 来自美团。3.26笔试。
[use picgo+ Tencent cloud object to store cos as a map bed]
Camera projection matrix calculation
2020 China's provinces and cities, three-level linkage data, data agencies (data from the official website of the National Bureau of Statistics)
Applet cloud data, data request a method to collect data
解决:模型训练时loss出现nan
目标检测系列——Fast R-CNN
[noi simulation] pendulum (linear algebra, Du Jiao sieve)
Transplantation of xuantie e906 -- fanwai 0: Construction of xuantie c906 simulation environment
用VNC Viewer的方式远程连接无需显示屏的树莓派
怎么把mdf和ldf文件导入MySQL workbench中
216. combined summation III enumeration method
Huawei Router: GRE Technology
从华为WeAutomate数字机器人论坛,看政企领域的“政务新智理”
Database to query the quantity of books lent in this month. If it is higher than 10, it will display "more than 10 books lent in this month". Otherwise, it will display "less than 10 books lent in thi
[Niuke] convert string to integer