如何在python/pybinder中捕获核心文件



Context:我有一个python脚本,调用C++中实现的接口,并由pybinder公开。

据我所知,拥有核心文件需要:

  • 将核心文件大小设置为无限制,默认为0
  • (可选(设置核心模式文件中的核心文件路径,或apport命令

这是我的程序

# Enable process to corefile size.
resource.setrlimit(resource.RLIMIT_CORE, (resource.RLIM_INFINITY, resource.RLIM_INFINITY))
# Core pattern file indicates the destination of corefile, which could be a command to calculate destination (if starting with '|'), or a path. 
f = open("/proc/sys/kernel/core_pattern", "w")
f.write(r"kernel.core_pattern=/tmp/test_coredump/corefile.%e.%p")
f.close()
# Print to make sure it's correct.
f = open("/proc/sys/kernel/core_pattern", "r")
print(f.readlines())
f.close()
print("main function invoked")
obj: coredump_pybind.CoredumpObject
obj = coredump_pybind.GetObject()
obj.Run()
print("main function finishes, begin destructing")
# CoredumpObject is implemented in C++ and intentionally segfaults to test the script

我确实得到了segfault

['kernel.core_pattern=/tmp/test_coredump/corefile.%e.%pn']
main function invoked
WARNING: Logging before InitGoogleLogging() is written to STDERR
I20220329 00:07:21.942946 3537696 coredump.cc:10] construction
I20220329 00:07:21.942981 3537696 coredump_pybind.cc:10] CoredumpObject
I20220329 00:07:21.943009 3537696 coredump_pybind.cc:14] ~CoredumpObject
I20220329 00:07:21.943020 3537696 coredump.cc:18] destruction
Segmentation fault

我所期望的是,我可以在/tmp/test_coredump/中找到核心文件(它是预先创建的(,但在那里什么也找不到。

错误发生在

f.write(r"kernel.core_pattern=/tmp/test_coredump/corefile.%e.%p")

应该是

f.write(r"/tmp/test_coredump/corefile.%e.%p")

最新更新