python - 使用 pthread_setname_np 时出现分段错误
问题描述
我正在尝试使用 python 中的线程名称 libpthread.pthread_setname_np 函数来修补线程名称,就像这样
'''
libpthread = ctypes.CDLL("libpthread.so")
if not libpthread:
# no pthread library, not patching
return
if not hasattr(libpthread, "pthread_setname_np"):
# pthread library does not have pthread_setname_np function, not patching
return
pthread_setname_np = libpthread.pthread_setname_np
pthread_setname_np.argtypes = [ctypes.c_void_p, ctypes.c_char_p]
pthread_setname_np.restype = ctypes.c_int
orig_start = threading.Thread.start
def start(self):
"""Patched version of threading.Thread.start"""
orig_start(self)
try:
name = self.name
if name:
if hasattr(name, "encode"):
name = name.encode('ascii', 'replace')
ident = getattr(self, "ident", None)
if ident is not None:
pthread_setname_np(ident, name[:15])
except Exception: # pylint: disable=broad-except
pass # Don't care about failure to set name
'''
以下代码通过以下消息证明我存在分段错误。
libpthread-2.31.so 中的段错误 4
关于可能导致此问题的任何输入。
解决方案
推荐阅读
- sql-server - DateTime.TryParse 使用错误的 MSA 格式
- java - 如何设置 shapedrawable 的边框并将颜色填充为不同的颜色
- docker - 在本地使用 Docker 开发时避免不断的“停止、构建、启动”循环
- angular6 - 刷新页面导致 404 错误-Angular 6
- gradle - 在 bootJar 任务中指定 gradle 配置或自定义依赖项?
- c# - 控制生成的图片框
- sql - 如何插入新字段并切换其值
- javascript - 无法在javascript中使用条件隐藏div
- pimcore - Pimcore 5:在 areabrick 控制器中访问块可编辑项
- mysql - 试图从同一个表中获取匹配和不匹配的值