GDB debugging multi-process programs

Source: Internet
Author: User

GDB debugging multi-process programs

Programs often use Fork/exec to create multi-process programs. Multi-process program has its own independent address space, which is the primary attention of multi-process debugging. GDB is powerful and provides a lot of support for debugging multithreading.

Method 1: Debug multi-process the most soil method: Attach PID

Attach is a common way to debug a process, as long as there are executable programs and the corresponding PID, you can work. Of course, for the convenience of debugging, you can set the sleep period after the process starts, such as 30s, so that there is ample time to attach.

Method 2:set Follow-fork-mode Child + main breakpoint

When setting the set Follow-fork-mode CHILD,GDB executes the child process directly after the fork, knowing that the breakpoint has been hit and stopped. How do I set breakpoints for a child process? In the parent process, the address space of the child process cannot be known (only when the program is loaded). GDB provides a convenient mechanism: the breakpoint of the main function inherits the quilt process (after all, main is the entrance to any program).

Note: After the program stops in main, you can try to set a breakpoint. Whether the breakpoint is valid depends on whether GDB has loaded the address space of the target program.

Method 3:set Follow-fork-mode child + catch exec

The cache point is a special breakpoint. GDB can catch a lot of events, such asthrow/catch/exception/syscall/exec/fork/vfork等。其中和多进程关系最大的就是exec/fork事件。

举例:

GNU gdb Fedora (6.8-27.el5)Copyright (C) 2008 Free Software Foundation, Inc.(gdb) catchexecCatchpoint 1 (exec)(gdb) set follow-fork-mode child(gdb) r  -d ***Catchpoint 1 (exec‘d /****/binary), 0x0000003c68800a70 in _start ()   from /lib64/ld-linux-x86-64.so.2(gdb) bt#0  0x0000003c68800a70 in _start () from /lib64/ld-linux-x86-64.so.2#1  0x0000000000000003 in ?? ()#2  0x00007fff65c6e85a in ?? ()#3  0x00007fff65c6e85d in ?? ()#4  0x00007fff65c6e860 in ?? ()(gdb) b lib.cc:8720No symbol table is loaded.  Use the "file"command.(gdb) cContinuing(gdb) bt#0  0x0000003c68800a70 in _start () from /lib64/ld-linux-x86-64.so.2#1  0x0000000000000002 in ?? ()#2  0x00007fff1af7682a in ?? ()#3  0x0000000000000000 in ?? ()(gdb)  b lib.cc:8720Breakpoint 2 at 0x15f9694: file lib.cc, line 8720.(gdb) cContinuing.[Thread debugging usinglibthread_db enabled][Thread 0x40861940 (LWP 12602) exited][Switching to process 12630]0x0000003c6980d81c in vfork () from /lib64/libpthread.so.0Warning:Cannot insert breakpoint 2.Error accessing memory address 0x15f9694: Input/output error.(gdb) bt#0  0x0000003c6980d81c in vfork () from /lib64/libpthread.so.0#1  0x000000000040c3fb in ?? ()#2  0x00002adeab604000 in ?? ()#3  0x01000000004051ef in ?? ()#4  0x00007fffff4a42f0 in ?? ()#5  0x686365746e6f6972 in ?? ()#6  0x0000000d0000000c in ?? ()#7  0x0000000b0000000a in ?? ()#8  0x0000000000000000 in ?? ()(gdb) delete2  --此处当breakpoint无效时,必须删除,否则程序无法继续(gdb) cContinuing.[New process 12630]Executing newprogram: /****/binarywarning: Cannot initialize threaddebugging library: generic error[Switching to process 12630]Catchpoint 1 (exec‘d /****/binary), 0x0000003c68800a70 in _start ()   from /lib64/ld-linux-x86-64.so.2(gdb) bt#0  0x0000003c68800a70 in _start () from /lib64/ld-linux-x86-64.so.2#1  0x0000000000000009 in ?? ()Backtrace stopped: previous frame inner to thisframe (corrupt stack?)(gdb) b lib.cc:8720Breakpoint 4 at 0x15f9694: file lib.cc, line 8720.(gdb) b type.cc:32Breakpoint 5 at 0x1693050: file type.cc, line 32.(gdb) cContinuing.(gdb)  -- 和正常程序调试一样

说明:catch exec后,程序将在fork/vfork/exec处停下。并非每次停下后,设置断点都是有效的。如提供断点无效,需要删除,否则程序无法继续。要能够在新进程中设置断点,一定要等到新进程的地址空间被载入后,设置断点是才有效(exec将改变原程序的地址空间)。上述例子,主要想展示如何对新进程设置断点!

注意: 1) 程序地址非常重要 ( 代码和数据地址一样重要 ) 。使用 gdb 时,多多注意和利用地址信息。 2)On some systems, when a child process was spawned by vfork, you cannot debug the child or parent until an exec call Complet Es. 方法 4 info inferiors/inferiors inferiors

设置set detach-on-fork off/set follow-exec-mode new

If you choose to set ' detach-on-fork ' mode off, then GDB would retain control of all forked processes (including nested for KS). You can list the forked processes under the control's GDB info inferiors by using the command, and switch from one fork to another By using the inferior command.

The GDB used does not supportset detach-on-fork off/set follow-exec-mode new/info inferiors。不清楚。

GDB debugging multi-process programs

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.