如何调试 Apache 模块
我最近一直在写一个 Apache 模块。使用内存池范例很有趣,但我显然没有做正确的事情。我遇到了段错误,但我似乎无法找到它。我当前的调试周期涉及 ap_rprintfs 和一个用于重建和重新加载 Apache 的 make 脚本。
什么样的工具可用于在这种类型的环境中工作?
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.
What kind of tools are available for working in this type of environment?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该使用 GNU 调试器 (gdb)。通过命令
gdb bin/httpd
和 gdb 内的r -X
启动 Apache。当发生段错误时,您将能够使用命令bt
查看发生的位置。You should use GNU Debugger (gdb). Start Apache via command
gdb bin/httpd
and thanr -X
inside gdb. When segfault occurs, you will be able to see where it occurred with commandbt
.我记录了我自己使用 ddd(gdb 前端)调试 mod_deflate Apache 模块的经验 此处 。该帖子的内容如下:
本指南记录了调试 Apache 2.2.16 模块所需的步骤。本示例中正在调试的模块是 deflate 模块 (mod_deflate.c) 以及用于压缩数据的 zlib 库。在本例中,zlib 库和 deflate 模块都包含我们希望单步执行的自定义代码。
注意:
EXTRA_CFLAGS="-g" 告诉编译器包含调试符号。
--prefix=/ap 将安装放置在 /ap 中。
--with-included-apr 消除了版本或编译选项与 APR 和 APR-util 代码不匹配的可能性(可能不是必需的,但不会造成伤害)。
--enable-mods-shared=all 允许更改模块,然后重新加载它。如果不使用此选项,模块代码将编译到主 Apache 二进制文件中。
确保 LoadModule deflate_module module/mod_deflate.so (或类似的内容)行存在。
添加行 AddOutputFilterByType DEFLATE text/html text/plain text/xml (或类似的内容)。
在 Makefile 中删除 -03 选项,这样代码就不会被优化。
注意:
默认情况下 zlib 构建一个静态库。
EXTRA_CFLAGS=-g 告诉编译器包含调试符号。
--prefix=/ap 将安装放入 bin 中。
注意:
-g 告诉编译器包含调试符号。
ctrl-c 返回 gdb 提示符
File->Open Source 并选择 mod_deflate.c 或 aha363_zlib.c
直观地或通过 gdb 命令设置断点(即(gdb) b aha363_zlib.c )
注释:摘自 The Apache Modules Book – Application Development with Apache pg 328 “.. 我们使用 -X 选项来防止 Apache 自行分离,
分叉子进程,并进入守护进程模式……[Apache] 在等待传入连接时被阻止。所有模块均已加载,并且
配置已激活。如果我们将其留在那里,网络服务器基本上已启动并正在运行,并将为传入的请求提供服务。我们可以中断它
Ctrl-c 返回调试器。”
这应该就是准备好调试 Apache 模块代码所需的全部内容。
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:
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.
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.
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).
In the Makefile remove the -03 option so that the code is not optimized.
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.
Notes:
-g tells the compiler to include debug symbols.
ctrl-c to return to the gdb prompt
File->Open Source and select either the mod_deflate.c or aha363_zlib.c
Set breakpoint visually or via the gdb command (i.e. (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.”
That should be all that is required to get the Apache module code ready to debug.