Segfault 之后:有没有办法检查指针是否仍然有效? [英] After Segfault: Is there a way, to check if pointer is still valid?

查看:21
本文介绍了Segfault 之后:有没有办法检查指针是否仍然有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我计划创建一个日志记录/跟踪机制,它将字符串文字的地址 (const char*) 写入环形缓冲区.这些字符串位于只读数据段中,由预处理器使用 __function____file__ 创建.

I plan to create a logging/tracing mechanism, which writes the address (const char*) of string literals to a ring-buffer. These strings are in the read-only data-segment and are created by the preprocessor with __function__ or __file__.

问题:如果所有指针都有效,是否有可能在 Segfault 之后分析此环形缓冲区内容?有效"是指它们指向映射的内存区域,取消引用不会导致分段错误.

The Question: Is it possible, to analyze this ring-buffer content after a Segfault, if all pointers are valid? With "valid" I mean that they point to a mapped memory area and dereferencing won't cause a segmentation fault.

我正在使用 Linux 2.6.3x 和 GCC 4.4.x.

I'm working with Linux 2.6.3x and GCC 4.4.x.

最好的问候,

查理

推荐答案

当然,如果您依赖的堆栈或其他内存已损坏,则可能会出现问题,但任何代码都是如此.

Of course if the stack or other memory that you rely upon has been corrupted then there could be problems, but that is true for any code.

假设你所依赖的堆栈或其他内存没有问题,并假设你没有调用任何像 malloc() 这样的函数,这些函数不是 异步信号安全,并假设您不尝试从信号处理程序返回,那么从信号处理程序中读取或写入缓冲区应该没有问题.

Assuming that that there is no problem with the stack or other memory that you rely upon, and assuming that you do not call any functions like malloc() that are not async-signal safe, and assuming that you do not attempt to return from your signal handler, then there should be no problem reading or writing your buffer from within your signal handler.

如果您尝试测试特定地址是否有效,您可以使用系统调用,例如 mincore() 并检查错误结果.

If you are trying to test whether a particular address is valid, you could use a system call such as mincore() and check for an error result.

这篇关于Segfault 之后:有没有办法检查指针是否仍然有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆