Debugging nginx with DTrace pid provider | english русский 简体中文 עברית 日本語 türkçe news about download security advisories documentation pgp keys faq links books support donation trac wiki nginx.com |
This article assumes the reader has a general knowledge of nginx internals and DTrace. Although nginx built with the --with-debug option already provides a lot of information about request processing, it is sometimes desirable to trace particular parts of code path more thoroughly and at the same time omit the rest of debugging output. DTrace pid provider (available on Solaris, Mac OS X) is a useful tool to explore userland program’s internals, since it doesn’t require any code changes and it can help with the task. A simple DTrace script to trace and print nginx function calls may look like this: #pragma D option flowindent pid$target:nginx::entry { } pid$target:nginx::return { }
DTrace capabilities for function calls tracing provide only a limited amount of useful information, though. Real-time inspection of function arguments is typically more interesting, but also a bit more complicated. Examples below are intended to help the reader become more familiar with DTrace and the process of analyzing nginx behavior using DTrace.
One of the common scenarios for using DTrace with nginx is the following:
attach to the nginx worker process to log request lines and request start times.
The corresponding function to attach is
pid$target::*ngx_http_process_request:entry { this->request = (ngx_http_request_t *)copyin(arg0, sizeof(ngx_http_request_t)); this->request_line = stringof(copyin((uintptr_t)this->request->request_line.data, this->request->request_line.len)); printf("request line = %s\n", this->request_line); printf("request start sec = %d\n", this->request->start_sec); }
It should be noted that in the example above DTrace requires some knowledge
about the The problem above can be solved by including only the relevant and necessary structure and type definitions in the DTrace script. DTrace has to know sizes of structures, types, and fields offsets. Thus dependencies can be further reduced by manually optimizing structure definitions for use with DTrace. Let’s use DTrace script example above and see what structure definitions it needs to work properly.
First of all
Then there’s the typedef ngx_http_upstream_t void; typedef ngx_http_request_body_t void;
Last but not least it is necessary to add definitions of two member structures
( The final DTrace script can be downloaded from here. The following example shows the output of running this script: # dtrace -C -I ./objs -s trace_process_request.d -p 4848 dtrace: script 'trace_process_request.d' matched 1 probe CPU ID FUNCTION:NAME 1 4 .XAbmO.ngx_http_process_request:entry request line = GET / HTTP/1.1 request start sec = 1349162898 0 4 .XAbmO.ngx_http_process_request:entry request line = GET /en/docs/nginx_dtrace_pid_provider.html HTTP/1.1 request start sec = 1349162899
Using similar techniques the reader should be able to trace other nginx function calls. See also
|