如何调试 Apache 模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1962692/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Debug an Apache Module
提问by Willi Ballenthin
I've been writing an Apache module recently. It's been interesting to work with the memory pool paradigm, but I'm clearly not doing something right. I've got a segfault, and I cannot seem to find it. My current debug cycle involves ap_rprintfs and a make script that rebuilds and reloads Apache.
我最近一直在写一个 Apache 模块。使用内存池范例很有趣,但我显然没有做正确的事情。我有一个段错误,我似乎找不到它。我当前的调试周期涉及 ap_rprintfs 和重建和重新加载 Apache 的 make 脚本。
What kind of tools are available for working in this type of environment?
有哪些工具可以在这种环境中工作?
回答by Marko Kevac
You should use GNU Debugger (gdb). Start Apache via command gdb bin/httpdand than r -Xinside gdb. When segfault occurs, you will be able to see where it occurred with command bt.
您应该使用 GNU 调试器 (gdb)。通过命令gdb bin/httpd而不是r -X在 gdb 中启动 Apache 。当发生段错误时,您将能够使用 command 看到它发生的位置bt。
回答by PixelCloudSt
I documented my own experience debugging the mod_deflate Apache module using ddd (a gdb front end) Here. The contents of that post are below:
我证明我自己的经验调试使用DDD(一GDB前端)的mod_deflate模块Apache模块这里。该帖子的内容如下:
This guide documents the steps required to debug an Apache 2.2.16 module. The module being debugged in this example is the deflate module (mod_deflate.c) as well as the zlib library which is uses to compress data with. Both the zlib library and deflate module, in this example, contain custom code with which we wish to step through.
本指南记录了调试 Apache 2.2.16 模块所需的步骤。本例中调试的模块是 deflate 模块 (mod_deflate.c) 以及用于压缩数据的 zlib 库。在本例中,zlib 库和 deflate 模块都包含我们希望逐步执行的自定义代码。
- Download and compile the Apache source distribution. Also make sure you don't already have an Apache installation on your system. You can download the Apache source from here.
- 下载并编译 Apache 源代码发行版。还要确保您的系统上还没有安装 Apache。您可以从这里下载 Apache 源代码。
$ EXTRA_CFLAGS="-g" ./configure --prefix=/ap --with-included-apr --enable-mods-shared=all
$ make
$ make install
$ EXTRA_CFLAGS="-g" ./configure --prefix=/ap --with-included-apr --enable-mods-shared=all
$ make
$ 安装
Notes: EXTRA_CFLAGS="-g" tells the compiler to include debug symbols. --prefix=/ap Places the install in /ap . --with-included-apr removes the possibility of version or compile-option mismatches with APR and APR-util code (may not be necessary, but doesn't hurt). --enable-mods-shared=all allows for the ability to alter a module, and then reload it. If this option is not used, the module code is compiled into the main Apache binary.
注意: EXTRA_CFLAGS="-g" 告诉编译器包含调试符号。--prefix=/ap 将安装放在 /ap 中。--with-included-apr 消除了版本或编译选项与 APR 和 APR-util 代码不匹配的可能性(可能不是必需的,但不会造成伤害)。--enable-mods-shared=all 允许修改模块,然后重新加载它。如果未使用此选项,则模块代码将编译到主 Apache 二进制文件中。
- Update the Apache configuration file at /ap/config/httpd.conf.
- 更新位于 /ap/config/httpd.conf 的 Apache 配置文件。
Make sure the line LoadModule deflate_module modules/mod_deflate.so (or something similar) is present. Add the line AddOutputFilterByType DEFLATE text/html text/plain text/xml (or something similar).
确保存在 LoadModule deflate_module modules/mod_deflate.so(或类似的东西)这一行。添加行 AddOutputFilterByType DEFLATE text/html text/plain text/xml (或类似的东西)。
- Compile the zlib library (with the default compile flags in this case).
- 编译 zlib 库(在这种情况下使用默认编译标志)。
$ CFLAGS="-g" ./configure --prefix=bin
$ CFLAGS="-g" ./configure --prefix=bin
In the Makefile remove the -03 option so that the code is not optimized.
在 Makefile 中删除 -03 选项,以便不优化代码。
$ make test
$ make install
$进行测试
$ 安装
Notes: By default zlib builds a static library. EXTRA_CFLAGS=-g tells the compiler to include debug symbols. --prefix=/ap Places the install in bin.
注意:默认情况下,zlib 构建一个静态库。EXTRA_CFLAGS=-g 告诉编译器包含调试符号。--prefix=/ap 将安装放在 bin 中。
- Compile and install mod_deflate.
- 编译并安装 mod_deflate。
$ /ap/bin/apxs -I/mydir/zlib/bin/include/ -L/mydir/zlib/bin/lib/ -c mod_deflate.c -lahaz -g
$ cp .libs/mod_deflate.so
$ /ap/modules/mod_deflate.so
$ /ap/bin/apachectl -k stop
$ /ap/bin/apachectl -k start
$ /ap/bin/apxs -I/mydir/zlib/bin/include/ -L/mydir/zlib/bin/lib/ -c mod_deflate.c -lahaz -g
$ cp .libs/mod_deflate.so
$ /ap/modules/mod_deflate.so
$ /ap/bin/apachectl -k 停止
$ /ap/bin/apachectl -k 开始
Notes: -g tells the compiler to include debug symbols.
注意: -g 告诉编译器包含调试符号。
- Start debugging
- 开始调试
$ ddd /ap/bin/httpd
(gdb) r -X
$ ddd /ap/bin/httpd
(gdb) r -X
ctrl-c to return to the gdb prompt
ctrl-c 返回 gdb 提示符
File->Open Source and select either the mod_deflate.c or aha363_zlib.c
File->Open Source 并选择 mod_deflate.c 或 aha363_zlib.c
Set breakpoint visually or via the gdb command (i.e. (gdb) b aha363_zlib.c )
可视化或通过 gdb 命令设置断点(即 (gdb) b aha363_zlib.c )
Notes: From The Apache Modules Book – Application Development with Apache pg 328 “.. we use the -X option to prevent Apache from detaching itself, forking children, and going into daemon mode… [Apache] is blocked while waiting for incoming connections. All modules are loaded, and the configuration is active. If we leave it there, the webserver is basically up and running and will service incoming request. We can interrupt it with Ctrl-c to return to the debugger.”
注释:来自 The Apache Modules Book – Application Development with Apache pg 328 “.. 我们使用 -X 选项来防止 Apache 分离自身、分叉子进程和进入守护进程模式...... [Apache] 在等待传入连接时被阻止。所有模块都已加载,并且配置处于活动状态。如果我们把它留在那里,网络服务器基本上已经启动并运行,并将为传入的请求提供服务。我们可以使用 Ctrl-c 中断它以返回调试器。”
That should be all that is required to get the Apache module code ready to debug.
这应该是让 Apache 模块代码准备好调试所需的全部内容。

