GDB Debug Multi-process and multithreaded commands

Source: Internet
Author: User

 GDB Debug Multi-process and multithreaded commandsSource: http://blog.csdn.net/pbymw8iwm/article/details/78767971. By default, GDB will only debug the main process when debugging a multi-process program. But GDB (>v7.0) supports multi-process and simultaneous debugging, in other words, GDB can debug multiple programs at the same time. You only need to set Follow-fork-mode (default: Parent) and Detach-on-fork (default: On).
Follow-fork-mode detach-on-fork Description Parent on only debugging main process (GDB default)
Child on only debugging sub-processes
Parent off simultaneously debug two processes, GDB with main process, sub-process block at fork position
Child off simultaneously debug two processes, GDB and subprocess, main process block at Fork position Set method: Set Follow-fork-mode [Parent|child] s ET detach-on-fork [On|off]

Querying the process being debugged: info inferiors
Switch Debug process: inferior <infer number>
Add a new debug process: Add-inferior [-copies n] [-exec executable], you can use the file executable to assign to the inferior executable.
Others: Remove-inferiors infno, detach inferior

2. GDB supports debugging multi-threading by default, with the main thread, the child thread block in the Create thread.
Query Thread: INFO threads
Toggle Debug Thread: Thread <thread number>

Routines:
#include <stdio.h>
#include <pthread.h>

void Processa ();
void Processb ();
void * Processaworker (void *arg);

int main (int argc, const char *argv[])
{
int pid;

PID = fork ();

if (pid! = 0)
Processa ();
Else
PROCESSB ();

return 0;
}

void Processa ()
{
pid_t pid = Getpid ();
Char prefix[] = "Processa:";
Char tprefix[] = "thread";
int tstatus;
pthread_t pt;

printf ("%s%lu%s\n", prefix, PID, "Step1");

Tstatus = pthread_create (&pt, NULL, processaworker, NULL);
if (tstatus! = 0)
{
printf ("Processa:can not create new thread.");
}

Processaworker (NULL);
Sleep (1);
}

void * Processaworker (void *arg)
{
pid_t pid = Getpid ();
pthread_t tid = pthread_self ();
Char prefix[] = "Processa:";
Char tprefix[] = "thread";

printf ("%s%lu%s%lu%s\n", prefix, PID, Tprefix, Tid, "Step2");
printf ("%s%lu%s%lu%s\n", prefix, PID, Tprefix, Tid, "step3");

return NULL;
}

void Processb ()
{
pid_t pid = Getpid ();
Char prefix[] = "PROCESSB:";
printf ("%s%lu%s\n", prefix, PID, "Step1");
printf ("%s%lu%s\n", prefix, PID, "Step2");
printf ("%s%lu%s\n", prefix, PID, "Step3");

}

Output:
[Email protected] c-lab]$/testprocessa:802 Step1
processb:803 Step1
processb:803 Step2
processb:803 Step3
processa:802 Thread 3077555904 Step2
processa:802 Thread 3077555904 step3
processa:802 Thread 3077553008 Step2
processa:802 Thread 3077553008 step3
Debugging:
1. Debug the main process, block child process.
(GDB) Set Detach-on-fork off
(GDB) Show detach-on-fork
Whether GDB would detach the child of a fork is off.
(GDB) Catch fork
Catchpoint 1 (fork)
(GDB) R
[Thread debugging using libthread_db enabled]

Catchpoint 1 (forked process 3475), 0x00110424 in __kernel_vsyscall ()
Missing separate Debuginfos, Use:debuginfo-install glibc-2.12-1.47.el6.i686
(GDB) Break test.c:14
Breakpoint 2 at 0x8048546:file test.c, line 14.
(GDB) cont
[New Process 3475]
[Thread debugging using libthread_db enabled]

Breakpoint 2, Main (argc=1, argv=0xbffff364) at test.c:14
Missing separate Debuginfos, Use:debuginfo-install glibc-2.12-1.47.el6.i686
(GDB) Info inferiors
Num Description Executable
2 Process 3475/home/cnwuwil/labs/c-lab/test
* 1 Process 3472/home/cnwuwil/labs/c-lab/test

2. Switch to Child process:
(GDB) Inferior 2
[Switching to inferior 2 [process 3475] (/home/cnwuwil/labs/c-lab/test)]
[Switching to Thread 2 (thread 0xb7fe86c0 (LWP 3475)])
#0 0x00110424 in?? ()
(GDB) Info inferiors
Num Description Executable
* 2 Process 3475/home/cnwuwil/labs/c-lab/test
1 Process 3472/home/cnwuwil/labs/c-lab/test
(gdb) Inferior 1
[Switching to inferior 1 [process 3472] (/home/cnwuwil/labs/c-lab/test)]
[Switching to Thread 1 (thread 0xb7fe86c0 (LWP 3472)])
#0 Main (argc=1, argv=0xbffff364) at test.c:14
(GDB) Info inferiors
Num Description Executable
2 Process 3475/home/cnwuwil/labs/c-lab/test
* 1 Process 3472/home/cnwuwil/labs/c-lab/test

3. Set a breakpoint to continue debugging the main process, and the main process to produce two sub-threads:
(GDB) Break test.c:50
Breakpoint 3 at 0x804867d:file test.c, line 50. (2 locations)
(GDB) cont
processa:3472 Step1
[New Thread 0xb7fe7b70 (LWP 3562)]
processa:3472 Thread 3086911168 Step2

Breakpoint 3, Processaworker (arg=0x0) at test.c:50
(GDB) Info inferiors
Num Description Executable
2 Process 3475/home/cnwuwil/labs/c-lab/test
* 1 Process 3472/home/cnwuwil/labs/c-lab/test
(GDB) Info threads
3 Thread 0xb7fe7b70 (LWP 3562) 0x00110424 in __kernel_vsyscall ()
2 Thread 0xb7fe86c0 (LWP 3475) 0x00110424 in?? ()
* 1 Thread 0xb7fe86c0 (LWP 3472) Processaworker (arg=0x0) at test.c:50

4. Switch to the child thread in the main process, note: Thread 2 is the child process that was created earlier
(GDB) Thread 3
[Switching to Thread 3 (thread 0xb7fe7b70 (LWP 3562)]) #0 0x00110424 in __kernel_vsyscall ()
(GDB) cont
processa:3472 Thread 3086911168 step3
processa:3472 Thread 3086908272 Step2
[Switching to Thread 0xb7fe7b70 (LWP 3562)]

Breakpoint 3, Processaworker (arg=0x0) at test.c:50
(GDB) Info threads
* 3 Thread 0xb7fe7b70 (LWP 3562) Processaworker (arg=0x0) at test.c:50
2 Thread 0xb7fe86c0 (LWP 3475) 0x00110424 in?? ()
1 Thread 0xb7fe86c0 (LWP 3472) 0x00110424 in __kernel_vsyscall ()
(GDB) Thread 1


GDB Debug Multi-process and multithreaded commands

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.