如何在Linux用户空间应用程序中测试地址是否可读 [英] How to test if an address is readable in linux userspace app

查看:179
本文介绍了如何在Linux用户空间应用程序中测试地址是否可读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于调试目的,我需要测试一个指针以查看其是否指向有效的可读页面.目前,我正在解析/proc/[pid]/maps来查看地址是否映射正确,但这似乎有点麻烦.有没有更好的办法?谢谢.

For debugging purposes I need to test a pointer to see if it points to a valid readable page. Currently I am parsing /proc/[pid]/maps to see if the address is mapped ok, but this seems a bit long-winded. Is there a better way? Thanks.

推荐答案

典型的方法是使用write()系统调用从页面读取(写入虚拟pipe()文件描述符).如果传递给write()的缓冲区不可读,它会返回-1errno == EFAULT而不是出错.

The canonical way is to use the write() system call to read from the page (writing to a dummy pipe() file descriptor). Instead of faulting, it will return -1 with errno == EFAULT if the buffer passed to write() is unreadable.

这篇关于如何在Linux用户空间应用程序中测试地址是否可读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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