Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
nginx
GitHub Repository: nginx/nginx.org
Path: blob/main/xml/en/docs/nginx_dtrace_pid_provider.xml
1 views
1
<?xml version="1.0"?>
2
3
<!--
4
Copyright (C) Nginx, Inc.
5
-->
6
7
<!DOCTYPE article SYSTEM "../../../dtd/article.dtd">
8
9
<article name="Debugging nginx with DTrace pid provider"
10
link="/en/docs/nginx_dtrace_pid_provider.html"
11
lang="en"
12
rev="4"
13
toc="no">
14
15
<section>
16
17
<para>
18
This article assumes the reader has a general knowledge of nginx internals and
19
<link id="see_also">DTrace</link>.
20
</para>
21
22
<para>
23
Although nginx built with the <link doc="debugging_log.xml">--with-debug</link>
24
option already provides a lot of information about request processing,
25
it is sometimes desirable to trace particular parts of code path more
26
thoroughly and at the same time omit the rest of debugging output.
27
DTrace pid provider (available on Solaris, macOS) is a useful tool to
28
explore userland program’s internals, since it doesn’t require any code
29
changes and it can help with the task.
30
A simple DTrace script to trace and print nginx function calls
31
may look like this:
32
33
<programlisting>
34
#pragma D option flowindent
35
36
pid$target:nginx::entry {
37
}
38
39
pid$target:nginx::return {
40
}
41
</programlisting>
42
43
</para>
44
45
<para>
46
DTrace capabilities for function calls tracing provide only a limited amount
47
of useful information, though.
48
Real-time inspection of function arguments is typically more interesting,
49
but also a bit more complicated.
50
Examples below are intended to help the reader become more familiar with
51
DTrace and the process of analyzing nginx behavior using DTrace.
52
</para>
53
54
<para>
55
One of the common scenarios for using DTrace with nginx is the following:
56
attach to the nginx worker process to log request lines and request start times.
57
The corresponding function to attach is
58
<c-func>ngx_http_process_request</c-func>, and the argument in question
59
is a pointer to the <literal>ngx_http_request_t</literal> structure.
60
DTrace script for such request logging can be as simple as:
61
62
<programlisting>
63
pid$target::*ngx_http_process_request:entry
64
{
65
this->request = (ngx_http_request_t *)copyin(arg0, sizeof(ngx_http_request_t));
66
this->request_line = stringof(copyin((uintptr_t)this->request->request_line.data,
67
this->request->request_line.len));
68
printf("request line = %s\n", this->request_line);
69
printf("request start sec = %d\n", this->request->start_sec);
70
}
71
</programlisting>
72
73
</para>
74
75
<para>
76
It should be noted that in the example above DTrace requires some knowledge
77
about the <literal>ngx_http_request_t</literal> structure.
78
Unfortunately while it is possible to use a specific <literal>#include</literal>
79
directive in the DTrace script and then pass it to a C preprocessor
80
(with the <literal>-C</literal> flag), that doesn’t really work.
81
Due to a lot of cross dependencies, almost all nginx header files
82
have to be included.
83
In turn, based on <command>configure</command> script settings,
84
nginx headers will include PCRE,
85
OpenSSL and a variety of system header files.
86
While in theory all those header files related to a specific nginx build
87
might be included in DTrace script preprocessing and compilation, in reality
88
DTrace script most probably will fail to compile because of unknown syntax in
89
some header files.
90
</para>
91
92
<para>
93
The problem above can be solved by including only the relevant and
94
necessary structure and type definitions in the DTrace script.
95
DTrace has to know sizes of structures, types, and fields offsets.
96
Thus dependencies can be further reduced by manually optimizing
97
structure definitions for use with DTrace.
98
</para>
99
100
<para>
101
Let’s use DTrace script example above and see what structure definitions
102
it needs to work properly.
103
</para>
104
105
<para>
106
First of all <literal>objs/ngx_auto_config.h</literal> file generated by
107
configure should be included, because it defines a number of constants
108
affecting various <literal>#ifdef</literal>’s.
109
After that, some basic types and definitions
110
like <literal>ngx_str_t</literal>, <literal>ngx_table_elt_t</literal>,
111
<literal>ngx_uint_t</literal> etc. should be put at the beginning of the
112
DTrace script.
113
These definitions are compact, commonly used and unlikely to be
114
frequently changed.
115
</para>
116
117
<para>
118
Then there’s the <literal>ngx_http_request_t</literal> structure that
119
contains a lot of pointers to other structures.
120
Because these pointers are really irrelevant to this script, and because they
121
have the same size, it is possible to just replace them with void pointers.
122
Instead of changing definitions, it is better to add appropriate typedefs,
123
though:
124
125
<programlisting>
126
typedef ngx_http_upstream_t void;
127
typedef ngx_http_request_body_t void;
128
</programlisting>
129
130
Last but not least it is necessary to add definitions of two member structures
131
(<literal>ngx_http_headers_in_t</literal>,
132
<literal>ngx_http_headers_out_t</literal>),
133
declarations of callback functions and definitions of constants.
134
</para>
135
136
<para>
137
The final DTrace script can be downloaded from
138
<link url="http://nginx.org/download/trace_process_request.d">here</link>.
139
</para>
140
141
<para>
142
The following example shows the output of running this script:
143
144
<programlisting>
145
# dtrace -C -I ./objs -s trace_process_request.d -p 4848
146
dtrace: script 'trace_process_request.d' matched 1 probe
147
CPU ID FUNCTION:NAME
148
1 4 .XAbmO.ngx_http_process_request:entry request line = GET / HTTP/1.1
149
request start sec = 1349162898
150
151
0 4 .XAbmO.ngx_http_process_request:entry request line = GET /en/docs/nginx_dtrace_pid_provider.html HTTP/1.1
152
request start sec = 1349162899
153
</programlisting>
154
155
</para>
156
157
<para>Using similar techniques the reader should be able to trace other
158
nginx function calls.
159
</para>
160
161
</section>
162
163
164
<section id="see_also"
165
name="See also">
166
167
<para>
168
<list type="bullet">
169
170
<listitem>
171
<link url="http://docs.oracle.com/cd/E19253-01/817-6223/index.html">
172
Solaris Dynamic Tracing Guide</link>
173
</listitem>
174
175
<listitem>
176
<link url="http://dtrace.org/blogs/brendan/2011/02/09/dtrace-pid-provider/">
177
Introduction article on DTrace pid provider</link>
178
</listitem>
179
180
</list>
181
</para>
182
183
</section>
184
185
</article>
186
187