核心转储文件名获取线程名称,而不是带有core_pattern%e%p.core的可执行文件名称

我最近开始通过使用pthread_setname_np()在我的应用程序中设置一些线程名称。 这样做后,如果在一个命名线程中发生崩溃,则核心转储文件名将获得线程名称,而不是带有core_pattern%e的可执行文件名。%p.core

根据核心手册页 ,core_pattern中的%e标志应该被扩展为可执行文件名称。 它没有说任何有关线程名称

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

这是pthread_setname_np()或core_pattern中的错误

什么是Linux中的核心转储文件? 它提供了什么信息?

如何在GDB中打印最后收到的信号?

Linux:窥探一个信号,而不是为随后的核心转储废弃寄存器?

核心文件中的_kernel_vsyscall()

如何在linux中的cryptoAPI中添加更多的algorithm

我在Linux CentOS 6.7上运行。

Apache MINA – 坚持SSL连接

linux:我如何生成一个核心文件

Linux(MIPS):查看核心转储时暂时“更改”注册内容

如果我只需要堆栈回溯,核心转储的最小大小是多less?

在Linux内核中,头包含错误

生成核心的可执行文件名称可以使用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

所以我最终解决了这个问题,通过将核心转储管道输送到一个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压缩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.*',] def processcore(self): (dirname,basename) = os.path.split(self.corefile) # Eg 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,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

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

我也有同样的问题。 而且我也以同样的方式解决了这个问题。 我得到可执行文件名使用/ proc / pid / exe

src_file_path = os.readlink("/proc/%s/exe" %pid) exec_filename = os.path.basename(src_file_path)

相关文章

HashMap是Java中最常用的集合类框架,也是Java语言中非常典型...
在EffectiveJava中的第 36条中建议 用 EnumSet 替代位字段,...
介绍 注解是JDK1.5版本开始引入的一个特性,用于对代码进行说...
介绍 LinkedList同时实现了List接口和Deque接口,也就是说它...
介绍 TreeSet和TreeMap在Java里有着相同的实现,前者仅仅是对...
HashMap为什么线程不安全 put的不安全 由于多线程对HashMap进...