GDB Debugging--Start the debug program

Source: Internet
Author: User

First, start

>>gdb start

GDB debugs the debug symbols before debugging, that is, the compile time Add –g option, such as GCC file.c–g–o target

There are 3 ways to enable GDB, one to start the core, and one to attach a running process.

1. GDB <program>

2. GDB <program> Core

Using GDB to debug a running program and core file at the same time, the core is the file generated after core dump is executed illegally.

3. GDB <program> <PID>

If your program is a service program, you can specify the process ID at which the service program runs. GDB will automatically attach up and debug him. The program should be searched in the PATH environment variable.

>> Run the Debug program

1. Run Argv1 argv2

GDB runs with parameters

2. Run

Run with no parameters

3. Set args argv_a Argv_b

GDB startup program can reset parameters after execution

4. Run >./output

GDB redirects when you start a program

Second, GDB debug multi-process (attach)

Run GDB:
Gdb
(GDB) Attach xxxxx ---xxxxx process ID for the subprocess obtained with the PS command
(GDB) Stop ---This is important, you need to pause that subprocess first, then set some breakpoints and some watch
(GDB) Break PNs --in result = WIB (value, div); This line sets a breakpoint, you can use the List command to view the source code
Breakpoint 1 at 0x10808:file eg1.c, line 37.
(GDB) continue
Continuing.

Third, GDB debugging multithreading

1 . GDB can only track one process when debugging with GDB.

You can trace a parent or child process by setting up the GDB debugging tool with instructions before the fork function is called.

By default, GDB tracks the parent process.

The Set follow-fork-mode child command sets GDB to track sub-processes after the fork.

The set Follow-fork-mode parent setting tracks the parental process.

By default, GDB only debugs 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

Setting method: Set Follow-fork-mode [Parent|child] Set 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 Debugging--Start the debug program

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.