linux - 核心转储文件名使用 core_pattern %e.%p.core 获取线程名称而不是可执行文件名称

标签 linux pthreads centos6 core

我最近开始使用 pthread_setname_np() 在我的应用程序中设置一些线程名称。执行此操作后,如果在其中一个命名线程中发生崩溃,则核心转储文件名将获取线程名称而不是具有 core_pattern %e.%p.core

的可执行文件名称

根据core man page ,core_pattern 中的 %e 标志应该扩展为可执行文件名称。它没有说明线程名称。

我想要可执行文件名称而不是线程名称,因为我有其他自动化脚本(不是我维护的)依赖于以应用程序名称开头的核心文件名。

这是 pthread_setname_np() 或 core_pattern 中的错误吗?

我在 Linux CentOS 6.7 上运行。

最佳答案

因此,我通过将核心转储管道传输到 Python 脚本来解决这个问题,然后根据线程名称正则表达式模式到可执行名称的硬编码映射重命名核心文件名。

以下是将核心通过管道传递给脚本的方法:

/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 压缩核心转储。

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

我将把它作为练习留给读者如何编写文件的其余部分。

关于linux - 核心转储文件名使用 core_pattern %e.%p.core 获取线程名称而不是可执行文件名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34801353/

相关文章:

c++ - 如何在不使用 pthread_join() 的情况下同步线程?

mysql - MariaDB 和 SqlYog - 无法创建数据库

linux - 根据文件名将平面文件系统移动、重命名到目录

linux - linux下如何获取进程的总内存使用量和执行时间?

linux - bash,检测 mp4 文件

mysqlfailover 命令给出错误 1045 : Access denied for user 'root' @'localhost' (using password: NO)

c - 使用 C 中的字符数组中的字符进行多线程读取/处理

c++ - 为什么这个非递归互斥量可以获取两次呢?

compiler-errors - gcc 汇编程序消息无法打开/tmp/ccqjY5HV.s 以读取没有这样的文件或目录

java - 什么设置JVM参数MaxNewSize的值?人体工程学?