当前位置:网站首页>[GDB debugging tool] | how to debug under multithreading, multiprocessing and running programs

[GDB debugging tool] | how to debug under multithreading, multiprocessing and running programs

2022-06-24 09:23:00 Jxiepc

1、gdb Basic commands

- bread【b】: To set breakpoints , And multiple... Can be set ;
	 Such as :b 20 =>  Set breakpoints in 20 That's ok ;
- run【r】: Run the program , If a breakpoint is encountered, it will stop ;
- backtrace【bt】: View stack information ;
- next【n】: Execute the current statement , Won't go inside ;
- step【s】: Step by step , Will enter the program ;
- print【p】: Display variable values ;
	 Such as :p name =>  Show variable name Value ;
- continue【c】: Continue running the program , Until the next breakpoint ;
- set var name=value: Set the value of the variable ;

 It needs to be added at compile time -g parameter gdb;

2、 Generate core file

 When the program hangs up , The system will not generate by default core file ;

ulimit -a: Check system parameters ;
ulimit -c unlimited: take core The size of the file is set to unlimited ;
	 Run the program after , If wrong , It can generate core file ;
gdb  The program name  core file name : You can go to view ;

 Insert picture description here
 Insert picture description here
 Insert picture description here
 Insert picture description here
Test source

void test() {
    
	int *a;
	if(a == 10) {
    
		cout << "***" << endl;
	}
}

3、 Mode the running program

 By getting the process of the current program 【ps -aux】 see ;
 In the use of 【gdb  The program name  -p  Process number 】;

 Insert picture description here
 Insert picture description here
The test program

#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、 Debugging multiprocess programs

 By default, the parent process is debugged ;
 stay gdb Select the parent-child process through the following options in :
	- set follow-fork-mode parent;
	- set follow-fork-mode child;
 Set debug mode 【set detach-on-fork [on/off]】:
	-  Represents the current process , Other processes continue to run , Then use on( default );
	-  Represents the current process , Other processes are suspended , Then use off;

info inferiors: View the process of debugging ;
inferior  process id: Toggle the current debugging process ;

 Insert picture description here
 Insert picture description here
 Insert picture description here

 Insert picture description here
The test program

#include <unistd.h>
#include <iostream>
 
 
int main(int argc, char* argv[])
{
    
    pid_t pid;

    pid = fork();

    if(pid == 0) {
     //  Subprocesses 
        std::cout << " Subprocesses :" << std::endl;

        for(int i=0; i<10; i++) {
    
            sleep(1);
            std::cout << " Son :" << i << std::endl;
        }
    }else if(pid > 0) {
    
        std::cout << " The parent process :" << std::endl;

        for(int i=0; i<10; i++) {
    
            sleep(1);
            std::cout << " Father :" << i << std::endl;
        }
    }else {
    
        std::cout << "fork error" << std::endl;
        exit(-1);
    }
    return 0;
}

5、 Debugging multithreaded programs

【 Check the process 、 Threads 】:
ps aux | grep  Screen names : View the currently running process ;
ps -aL | grep  Screen names : View the currently running lightweight processes ;
pstree -p  The main thread id: View the relationship between the main thread and the new thread ;

【gdb in 】:
info threads: Look at the thread ;
thread  Threads id: Switching thread ;
set scheduler-locking on: Only the current thread is allowed ;
set scheduler-locking off: Run all threads ;
thread apply  Threads id cmd: Specify a thread gdb command ;
thread apply all cmd: All threads execute a certain gdb command ;

 Insert picture description here

 Insert picture description here

 Insert picture description here
 Insert picture description here

原网站

版权声明
本文为[Jxiepc]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206240748035330.html