Linux how to debug multi-process program

  

In the Linux system operation, often encounter multi-process debugging problems, in fact, multi-process debugging is not as complicated as imagined, the following small series will introduce you how to debug Linux Process procedures, interested friends may wish to understand.

debugging

multi-process:

(1) follow-fork-mode

set follow-fork-mode [parent |  Child] ———— After fork, choose to debug the parent process or child process

(parent: continue to debug the parent process after fork; child: fork after debugging the parent process. The default is to debug the parent after fork Process)

set detach-on-fork [on |  Off] ———— Indicates whether gdb disconnects debugging of a process after fork, or is controlled by gdb

(on: disconnects the process specified by follow-fork-mode; Off: gdb will control the parent process and child process, the process specified by follow-fork-mode will be debugged, and the other process will be placed in a pause state)

(2)attach pid

gdb under ubuntu To use the attach pid permission is not enough, you need to switch to root, or you can add permissions to gdb with sudo chmod +s /usr/bin/gdb.

Using attach requires adding a sleep() statement at the beginning of the parent-child process code to let the process sleep, then running the process in the background, getting the child process pid through ps, and finally attach pid into the debug child process.

eg:

$./test & //Make the process test run in the background

$ps -ef |  Grep test //View process pid

The result looks like this:

XXX 12345 23456 ——————————&mdash ;————— . /test //parent process

XXX 12346 12345 ———————————————— /test //child process, the first number here is the child process pid

$ gdb -q test

(gdb)attach 12346 //attach to the child process

Added sleep() can be a statement like this:

while(pause) //pause is a flag variable

sleep(1);

Debugging in gdb You only need to set pause=0 to make the condition unsatisfiable and execute the subsequent code.

(3) gdb wrapper

When the parent process forks the child process, the child process will immediately call the exec function to execute the new code. In this case, you can also use gdb wrapper. The advantage is that you don't have to add extra code.

Put a process in the background under Linux:

(1). /test &

(2)“. /test” Then press “ctrl+z” to put the process back into the background with the number returned by “bg %num”

Pull the process back to the foreground under Linux:

Use “fg%num”

The above is the Linux multi-process program debugging method introduced, multi-process debugging we usually use gdb, when you use the gdb debugging tool.

Copyright © Windows knowledge All Rights Reserved