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

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

问题描述

我打算创建一个记录/追踪机制,它将字符串文本的地址( const char * )写入环形缓冲区。这些字符串位于只读数据段中,由预处理器使用 __ function __ __ file __ 创建。 p>

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



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



祝好,

Charly

解决方案

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



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



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


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__.

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.

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

Best regards,

Charly

解决方案

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.

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.

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天全站免登陆