Nginx Source Analysis--gdb debugging

Source: Internet
Author: User
Tags sigint signal

Debug with Gdb[i]Nginx[ii] and the use of GDB debugging other programs are no different, but Nginx can be daemon program, can also be run in multiple processes, so the use of GDB debugging and usually a bit different. Of course, we can choose to set Nginx to non-daemon mode and run it as a single process, which needs to be set up as follows:
Daemon off;
Master_process off;

This is the first case:
This setup of Nginx under GDB debugging is very common, the process can be [III] is this:
Execute command:
[Email protected]:/usr/local/nginx/sbin$ sudo gdb./nginx
The current directory is in the/usr/local/nginx/sbin, the directory has the execution of the program Nginx, the last command is to use GDB to start debugging Nginx, into the GDB command line, directly input R can execute Nginx:
(GDB) R
Starting program:/usr/local/nginx/sbin/nginx
SRC/CORE/NGX_CONF_FILE.C 1163:1 million
===============================
Ngx_timer_resolution 1000000
Because Nginx before the form of operation, the keyboard input is nginx takeover, at this time cannot input GDB command, so you need to press CTRL + C fallback to the GDB command line mode, and Nginx is interrupted in the __kernel_vsyscall call, Enter the BT command to view the call stack information:
(GDB) R
Starting program:/usr/local/nginx/sbin/nginx
SRC/CORE/NGX_CONF_FILE.C 1163:1 million
===============================
Ngx_timer_resolution 1000000
^c
Program received signal SIGINT, Interrupt.
0xb7f29430 in __kernel_vsyscall ()
(GDB) bt
#0 0xb7f29430 in __kernel_vsyscall ()
#1 0xb7e081a8 in epoll_wait () from/lib/tls/i686/cmov/libc.so.6
#2 0x08073ea3 in Ngx_epoll_process_events (Cycle=0x860ad60, timer=4294967295,
flags=0) at src/event/modules/ngx_epoll_module.c:405
#3 0x080668ee in Ngx_process_events_and_timers (CYCLE=0X860AD60)
At src/event/ngx_event.c:253
#4 0x08071618 in Ngx_single_process_cycle (CYCLE=0X860AD60)
At src/os/unix/ngx_process_cycle.c:283
#5 0x0804a926 in Main (Argc=1, argv=0xbfd2ae44) at src/core/nginx.c:372
(GDB)
OK, to set a breakpoint for Nginx, it is very simple, such as the function Ngx_process_events_and_timers set a breakpoint, and then enter the command C to continue to execute Nginx:
#4 0x08071618 in Ngx_single_process_cycle (CYCLE=0X860AD60)
At src/os/unix/ngx_process_cycle.c:283
#5 0x0804a926 in Main (Argc=1, argv=0xbfd2ae44) at src/core/nginx.c:372
(GDB) B ngx_process_events_and_timers
Breakpoint 1 at 0x80667d6:file src/event/ngx_event.c, line 200.
(GDB) C
Continuing.
Breakpoint 1, ngx_process_events_and_timers (CYCLE=0X860AD60)
At src/event/ngx_event.c:200
200 {
(GDB)
The result of my here GdB immediately prompt breakpoint interrupt, this is because just tuned to this function, perhaps in you that does not immediately interrupt, so GDB has been parked behind continuing, at this time we have to take the initiative to trigger the event (or wait, time may be long, According to your nginx settings, so that Nginx calls this function, such as the use of the browser to access the Nginx monitoring site, so that nginx events occur.
After breaking the breakpoint, we debug and trace with the GDB General Command S, c, and so on, which need not say more:
200 {
(GDB) s
__cyg_profile_func_enter (this=0x80667d0, call=0x8071618)
At src/core/my_debug.c:65
Warning:source file is more recent than executable.
My_debug_print ("enter\n%p\n%p\n", call, this);
(GDB)
66}
(GDB) C
Continuing.
Ngx_timer_resolution 1000000
Second case:
When in daemon mode (that is, configuration options: daemon on;) A single process to run Nginx, the above method does not work, the reason is very simple, when nginx in the daemon mode of operation, the startup process Nginx will fork several times, and those parent process has already been directly exited , so what you see is this:
[Email protected]:/usr/local/nginx/sbin$ sudo gdb./nginx
GNU gdb 6.8-debian
Copyright (C) Free Software Foundation, Inc.
License gplv3+: GNU GPL version 3 or later This was free software:you was free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "Show copying"
and "Show warranty" for details.
This GDB is configured as "I486-linux-gnu" ...
(GDB) R
Starting program:/usr/local/nginx/sbin/nginx
SRC/CORE/NGX_CONF_FILE.C 1163:1 million
===============================
Program exited normally.
(GDB)
The fact that Nginx did not exit is the parent (or grandfather) process, and GDB does not follow the fork:
[Email protected]:/usr/local/nginx/sbin$ pidof Nginx
7908
GDB considers this and therefore provides the appropriate Follow-fork-mode options:
The usage is:
Set Follow-fork-mode [Parent|child]
Description
Parent:fork continues to debug the parent process after the child process is not affected.
Child:fork after the child process is debugged, the parent process is unaffected.
When this option is set, the other tuning operations are the same as those described earlier, with examples such as:
[Email protected]:/usr/local/nginx/sbin$ sudo gdb./nginx-q
(GDB) R
Starting program:/usr/local/nginx/sbin/nginx
SRC/CORE/NGX_CONF_FILE.C 1163:1 million
===============================
Program exited normally.
(gdb) Shell pidof Nginx
9723
(gdb) Shell kill-9 9723
(GDB) Set Follow-fork-mode child
(GDB) B ngx_process_events_and_timers
Breakpoint 1 at 0x80667d6:file src/event/ngx_event.c, line 200.
(GDB) R
Starting program:/usr/local/nginx/sbin/nginx
SRC/CORE/NGX_CONF_FILE.C 1163:1 million
===============================
[Switching to process 9739]
Breakpoint 1, ngx_process_events_and_timers (CYCLE=0X8FF0D60)
At src/event/ngx_event.c:200
200 {
(GDB) s
[Tcsetpgrp failed in terminal_inferior:no such process]
__cyg_profile_func_enter (this=0x80667d0, call=0x8071618)
At src/core/my_debug.c:65
Warning:source file is more recent than executable.
My_debug_print ("enter\n%p\n%p\n", call, this);
(GDB) C
Continuing.
Above we set breakpoints first, so that after executing the R command, Nginx will automatically be interrupted to return to the GDB command line, if not, you will not be able to return to the GDB command line, that is, press CTRL + C has no effect, because Nginx is running in daemon mode, the input output has been redirected, Nginx did not receive this CTRL + C input. Of course, there is no other way, such as another shell window, using the KILL command to send an SIGINT signal to the NGINX process, which is the same as pressing CTRL + C, as follows (assuming 9897 is the Nginx process ID number):
[Email protected]:~$ sudo kill-2 9897
Second case:
The third situation is that no matter the Nginx operation mode, it is universal, that is, using GdB's Attach, Detach command.
This assumes the relevant configuration of the current Nginx:
daemon on;
Master_process on;
Worker_processes 1;
That is, daemon mode, multi-process operation Nginx.
[Email protected]:/usr/local/nginx/sbin$ sudo gdb-q
(GDB) shell./nginx
SRC/CORE/NGX_CONF_FILE.C 1163:1 million
===============================
(gdb) Shell pidof Nginx
10246 10245
(GDB)
You can see a total of two nginx processes, where 10245 is the master process, and 10246 is the worker process. We want to debug worker 10246 as follows:
(gdb) Attach 10246
Attaching to Program:/usr/local/nginx/sbin/nginx, Process 10246
Reading symbols From/lib/tls/i686/cmov/libcrypt.so.1...done.
Loaded symbols For/lib/tls/i686/cmov/libcrypt.so.1
Reading symbols From/lib/libpcre.so.3...done.
Loaded symbols for/lib/libpcre.so.3
Reading symbols From/usr/lib/libz.so.1...done.
Loaded symbols For/usr/lib/libz.so.1
Reading symbols From/lib/tls/i686/cmov/libc.so.6...done.
Loaded symbols for/lib/tls/i686/cmov/libc.so.6
Reading symbols From/lib/ld-linux.so.2...done.
Loaded symbols for/lib/ld-linux.so.2
Reading symbols From/lib/tls/i686/cmov/libnss_compat.so.2...done.
Loaded symbols for/lib/tls/i686/cmov/libnss_compat.so.2
Reading symbols From/lib/tls/i686/cmov/libnsl.so.1...done.
Loaded symbols For/lib/tls/i686/cmov/libnsl.so.1
Reading symbols From/lib/tls/i686/cmov/libnss_nis.so.2...done.
Loaded symbols for/lib/tls/i686/cmov/libnss_nis.so.2
Reading symbols From/lib/tls/i686/cmov/libnss_files.so.2...done.
Loaded symbols for/lib/tls/i686/cmov/libnss_files.so.2
0xb8016430 in __kernel_vsyscall ()
(GDB) B ngx_process_events_and_timers
Breakpoint 3 at 0x80667d6:file src/event/ngx_event.c, line 200.
(GDB) C
Continuing.
Breakpoint 3, Ngx_process_events_and_timers (cycle=0x8f43d68) at src/event/ngx_event.c:200
200 {
(GDB) Detach
Detaching from Program:/usr/local/nginx/sbin/nginx, Process 10246
(GDB)
Overall, the third method is the best.


[i] http://www.gnu.org/software/gdb/[ii] Nginx was compiled with the-G option turned on. [III] means that the following method is just one of many methods.

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.