Posts Tagged ‘gdb’

Debugging NGinx

April 22, 2010

NGinx is a lightweight, high performance web server & reverse proxy. It’s fast becoming popular & is touted to take on Apache as a preferred browser. It uses far lesser Operational memory than Apache, plus it consumes lot lesser CPU Cycles. All this is mostly due to its architecture. Credit has to be given to Igor Sysoev who’s single-handedly done this !

Nginx

NGinx Webserver

Off late, I have been tinkering NGinx code. But as any programmer would vouch, debugging is a very much integral part of programming. There are no direct manuals available for the same. For any beginner this new web server along with new environment can be daunting. Here’s a low-down on how to use gdb to debug NGinx.

First of, make sure you take care of these –

  1. Install all the dependencies that NGinx requires.
  2. Compile NGinx from source. The standard NGinx binaries (got from apt-get or yum or other package managers) haven’t been built with debugging on.
  3. Make sure you check out all the available options that are available at compile time. Check here for that.
  4. If all is well, NGinx would have compiled fine. It would have installed under ‘/usr/local/nginx’.
  5. Go there and open ‘conf/nginx.conf’. You might see “worker_processes <some_number>”, edit it to “worker_processes 1”. Save & close the config.
  6. Now start the server by typing ‘/usr/local/nginx/sbin/nginx’. nginx starts with one main process + 1 child process (as we specified in config).

Now the web server is started & is running. It should serve requests. Test it by typing any local url in your browser. The next points, should be typed as is in command-line.

  1. gdb
  2. file /usr/local/nginx/sbin/nginx
  3. After this if you want to put breakpoint at any place then put – b ngx_http_<your_filename>_module.c:19
  4. Next open another terminal session & type “ps -ef | grep nginx”, this tells you that there are 2 nginx processes running. One Master, One Child. Get the pid of the child process.
  5. Come back to your gdb prompt & type – attache <pid_of_child_process>
  6. Type continue, to resume normal execution.
  7. Goto the browser & navigate to the url location where your module would get activated.
  8. You should break at the file where breakpoint you mentioned.

Enjoy debugging 🙂

PS: One thing I couldn’t figure out was that NGinx does not seem to server requests from master process. Why is that? Is it a design decision?