Linux core dump filename获取线程名,而不是core_模式为%e.%p.core的可执行文件名
我最近开始使用pthread_setname_np()在应用程序中设置一些线程名称。执行此操作后,如果其中一个命名线程内发生崩溃,则core转储文件名将获取线程名称,而不是core_模式为%e.%p.core的可执行文件名称 根据,core_模式中的%e标志应该扩展为可执行文件名。它没有说任何关于线程名称的内容 我想要的是可执行文件名,而不是线程名,因为我有其他自动脚本(不是由我维护的),它们依赖于以应用程序名开头的核心文件名 这是pthread_setname_np()或core_模式中的错误吗Linux core dump filename获取线程名,而不是core_模式为%e.%p.core的可执行文件名,linux,pthreads,centos6,core,Linux,Pthreads,Centos6,Core,我最近开始使用pthread_setname_np()在应用程序中设置一些线程名称。执行此操作后,如果其中一个命名线程内发生崩溃,则core转储文件名将获取线程名称,而不是core_模式为%e.%p.core的可执行文件名称 根据,core_模式中的%e标志应该扩展为可执行文件名。它没有说任何关于线程名称的内容 我想要的是可执行文件名,而不是线程名,因为我有其他自动脚本(不是由我维护的),它们依赖于以应用程序名开头的核心文件名 这是pthread_setname_np()或core_模式中的错误
我在Linux CentOS 6.7上运行。因此,我通过将核心转储文件传输到Python脚本,然后根据线程名regex模式到可执行文件名的硬编码映射重命名核心文件名,从而解决了这个问题 下面是如何将核心管道连接到脚本:
/sbin/sysctl -q -w "kernel.core_pattern=|/opt/mydirectory/bin/core_helper.py --corefile /opt/mydirectory/coredumps/%e.%p.core"
/sbin/sysctl -q -w "kernel.core_pipe_limit=8"
下面是core_helper.py中一个类的片段。作为奖励,如果您给核心文件名一个.gz扩展名,它将用gzip压缩coredump
class CoredumpHelperConfig:
def __init__(self, corefile):
self.corefile = corefile
# Work-around: Linux is putting the thread name into the
# core filename instead of the executable. Revert the thread name to
# executable name by using this mapping.
# The order is important -- the first match will be used.
threadNameToExecutableMapping = [# pattern , replace
(r'fooThread.*', r'foo'),
(r'barThread.*', r'foo'),
]
def processCore(self):
(dirname, basename) = os.path.split(self.corefile)
# E.g. fooThread0.21495.core (no compression) or fooThread0.21495.core.gz (compression requested)
match = re.match(r'^(\w+)\.(\d+)\.(core(\.gz)?)$', basename)
assert match
(threadName, pid, ext, compression) = match.groups()
# Work-around for thread name problem
execName = threadName
for (pattern, replace) in CoredumpHelperConfig.threadNameToExecutableMapping:
match = re.match(pattern, threadName)
if match:
execName = re.sub(pattern, replace, threadName)
break
self.corefile = os.path.join(dirname, '.'.join([execName, pid, ext]))
# Pipe the contents of the core into corefile, optionally compressing it
core = open(self.corefile, 'w')
coreProcessApp = "tee"
if(compression):
coreProcessApp = "gzip"
p = subprocess.Popen(coreProcessApp, shell=True, stdin=sys.stdin, stdout=core, stderr=core)
core.close()
return True
我将把它作为如何编写文件其余部分的练习留给读者。生成核心的可执行文件名可以使用gdb检索。 下面是它的打印:
gdb -batch -ex "core corefile" | grep "Core was generated" | cut -d\` -f2 | cut -d"'" -f1 | awk '{print $1}'
或者最好使用pid%p和/proc来获取它。例如:
$ sleep 900 &
[1] 2615
$ readlink /proc/$(pidof sleep)/exe
/bin/sleep
$ basename $(readlink /proc/$(pidof sleep)/exe)
sleep
我也有同样的问题。我也用同样的方法工作过。 我使用/proc/pid/exe获取可执行文件名
src_file_path = os.readlink("/proc/%s/exe" %pid)
exec_filename = os.path.basename(src_file_path)
看起来这家伙也有同样的问题: