非常教程

Nginx参考手册

指南 | Guides

Debugging nginx with DTrace pid provider

本文假定读者具有关于nginx内部和DTrace的一般知识。

尽管使用--with-debug选项构建的nginx已经提供了很多关于请求处理的信息,但有时候希望更全面地跟踪代码路径的特定部分,同时省略其余的调试输出。DTrace pid提供程序(可在Solaris,macOS上使用)是探索用户程序内部的有用工具,因为它不需要任何代码更改,并且可以帮助执行任务。跟踪和打印nginx函数调用的简单DTrace脚本可能如下所示:

#pragma D option flowindent

pid$target:nginx::entry {
}

pid$target:nginx::return {
}

但是,函数调用跟踪的DTrace功能仅提供有限的有用信息。实时检查函数参数通常更有趣,但也更复杂一些。下面的示例旨在帮助读者更熟悉DTrace以及使用DTrace分析nginx行为的过程。

与nginx一起使用DTrace的常见方案之一是:附加到nginx工作进程以记录请求行并请求开始时间。相应的附加函数是ngx_http_process_request(),并且所讨论的参数是指向ngx_http_request_t结构的指针。这种请求记录的DTrace脚本可以像下面这样简单:

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);
}

应该注意的是,在上面的例子中,DTrace需要一些关于ngx_http_process_request结构的知识。 不幸的是,虽然可以在DTrace脚本中使用特定的#include指令,然后将其传递给C预处理器(带有-C标志),但这并不起作用。 由于很多交叉依赖关系,几乎所有的nginx头文件都必须包含在内。 反过来,基于配置脚本设置,nginx头文件将包含PCRE,OpenSSL和各种系统头文件。 虽然理论上所有与特定nginx构建相关的头文件都可能包含在DTrace脚本预处理和编译中,但实际上,由于某些头文件中的未知语法,DTrace脚本很可能无法编译。

上面的问题可以通过在DTrace脚本中只包含相关的和必要的结构和类型定义来解决。DTrace必须知道结构,类型和字段偏移的大小。因此,通过手动优化用于DTrace的结构定义可以进一步降低依赖性。

让我们使用上面的DTrace脚本示例,看看它需要哪些结构定义才能正常工作。

首先应该包含由configure生成的objs / ngx_auto_config.h文件,因为它定义了一些影响各种#ifdef的常量。 之后,应该将一些基本类型和定义(如ngx_str_t,ngx_table_elt_t,ngx_uint_t等)放在DTrace脚本的开头。 这些定义是紧凑的,通常使用的,不太可能经常改变。

而且ngx_http_process_request_t是包含很多指向其他结构的指针的结构。因为这些指针与这个脚本真的无关,并且因为它们具有相同的大小,所以可以用void指针替换它们。不过,不要改变定义,但最好添加适当的typedef:

typedef ngx_http_upstream_t     void;
typedef ngx_http_request_body_t void;

最后但并非最不重要的是,需要添加两个成员结构(ngx_http_headers_in_tngx_http_headers_out_t)的定义,回调函数的声明和常量的定义。

最终的DTrace脚本可以从这里下载。

以下示例显示了运行此脚本的输出:

# 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

使用类似的技术,读者应该能够追踪其他nginx函数调用。

扩展内容

  • Solaris动态跟踪指南
  • 关于DTrace pid提供商的介绍性文章
Nginx

Nginx是一款轻量级的 Web 服务器/反向代理服务器及电子邮件代理服务器,可在 BSD-like 协议下发行。其特点是占有内存少,并发能力强。

主页 https://nginx.org/
源码 http://hg.nginx.org/nginx
发布版本 1.13.6

Nginx目录

1.指南 | Guides
2.核心 | Core
3.ngx_google_perftools_module
4.ngx_http_access_module
5.ngx_http_addition_module
6.ngx_http_api_module
7.ngx_http_auth_basic_module
8.ngx_http_auth_jwt_module
9.ngx_http_auth_request_module
10.ngx_http_autoindex_module
11.ngx_http_browser_module
12.ngx_http_charset_module
13.ngx_http_core_module
14.ngx_http_dav_module
15.ngx_http_empty_gif_module
16.ngx_http_f4f_module
17.ngx_http_fastcgi_module
18.ngx_http_flv_module
19.ngx_http_geoip_module
20.ngx_http_geo_module
21.ngx_http_gunzip_module
22.ngx_http_gzip_module
23.ngx_http_gzip_static_module
24.ngx_http_headers_module
25.ngx_http_hls_module
26.ngx_http_image_filter_module
27.ngx_http_index_module
28.ngx_http_js_module
29.ngx_http_keyval_module
30.ngx_http_limit_conn_module
31.ngx_http_limit_req_module
32.ngx_http_log_module
33.ngx_http_map_module
34.ngx_http_memcached_module
35.ngx_http_mirror_module
36.ngx_http_mp4_module
37.ngx_http_perl_module
38.ngx_http_proxy_module
39.ngx_http_random_index_module
40.ngx_http_realip_module
41.ngx_http_referer_module
42.ngx_http_rewrite_module
43.ngx_http_scgi_module
44.ngx_http_secure_link_module
45.ngx_http_session_log_module
46.ngx_http_slice_module
47.ngx_http_spdy_module
48.ngx_http_split_clients_module
49.ngx_http_ssi_module
50.ngx_http_ssl_module
51.ngx_http_status_module
52.ngx_http_stub_status_module
53.ngx_http_sub_module
54.ngx_http_upstream_conf_module
55.ngx_http_upstream_hc_module
56.ngx_http_upstream_module
57.ngx_http_userid_module
58.ngx_http_uwsgi_module
59.ngx_http_v2_module
60.ngx_http_xslt_module
61.ngx_mail_auth_http_module
62.ngx_mail_core_module
63.ngx_mail_imap_module
64.ngx_mail_pop3_module
65.ngx_mail_proxy_module
66.ngx_mail_smtp_module
67.ngx_mail_ssl_module
68.ngx_stream_access_module
69.ngx_stream_core_module
70.ngx_stream_geoip_module
71.ngx_stream_geo_module
72.ngx_stream_js_module
73.ngx_stream_limit_conn_module
74.ngx_stream_log_module
75.ngx_stream_map_module
76.ngx_stream_proxy_module
77.ngx_stream_realip_module
78.ngx_stream_return_module
79.ngx_stream_split_clients_module
80.ngx_stream_ssl_module
81.ngx_stream_ssl_preread_module
82.ngx_stream_upstream_hc_module
83.ngx_stream_upstream_module