博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
用户态调测工具(二):perror和man
阅读量:2512 次
发布时间:2019-05-11

本文共 2974 字,大约阅读时间需要 9 分钟。

errno

linux中输入man perror 命令,如下

#include 
void perror(const char *s);#include
const char * const sys_errlist[];int sys_nerr; int errno; /* Not really declared this way; see errno(3) */

errno定义在errno.h中,在系统调用或库函数调用失败,值就会改变。errno值不同,对应不同的错误信息。

打印errno值含义

#include 
/* for strerror */#include
#include
int main(int argc, char ** argv) { int i = 0; for(i = 0; i < 256; i++) printf("errno.%02d is: %s\n", i, strerror(i)); return 0;}

errno.00 is: Success

errno.01 is: Operation not permitted
errno.02 is: No such file or directory
errno.03 is: No such process
errno.04 is: Interrupted system call
errno.05 is: Input/output error
errno.130 is: Owner died
errno.131 is: State not recoverable
errno.132 is: Operation not possible due to RF-kill
errno.133 is: Memory page has hardware error
以上错误不用全记住,出现错误后看英文意思或者搜一下即可。

errno使用注意:

系统调用执行正常, errno值不会被清零;
errno(包括perror函数)必须在出错的调用后面执行,中间不能插入新的系统调用(任何系统调用,print也不行),否则errno会被重新置位。
建议以一下方式:
errno = 0
系统调用
Perror(“xxx”); 或者 调用strerror(i)

Perror 函数

The perror() function produces a message on standard error describing the last error encountered during a call to a system or library function.

Perror()提供错误的系统调用或者库函数的错误描述。

用法:

#include 
void perror(const char *s);

先打印s指向的字符串,然后输出errno所对应的错误提示信息。

Strerror函数

The strerror() function returns a pointer to a string that describes the error code passed in the argument errnum, possibly using the LC_MESSAGES part of the current locale to select the appropriate language. (For example, if errnum is EINVAL, the returned description will be “Invalid argument”.)

用法:

#include 
char *strerror(int errnum);

实例:

#include 
/* for strerror */#include
#include
#include
#include
#include
int main(int argc, char ** argv){ errno = 0; open("/xx", 'r'); perror("open"); printf("%s\n", strerror(errno)); return 0;}

执行结果:

./test
open: Permission denied
Permission denied
错误原因,因为ubuntu系统普通用户在根目录没有创建文件权限,所有会报错Permission denied
很方便就能看出原因

man命令

man是一个查看、学习系统调用的非常重要的工具。

系统调用出错,并通过perror()打出错误信息后,一旦错误不了解,或者认为系统已满足(比如open报错Permission denied,但是你使用root用户,认为自己应该有打开的权限时),通常可以查man资料解决。

man的使用:

1. 自己系统man:因个人环境不同,man版本不同,显示内容可能不是最新。
2. 网站
man的查看:
man显示包括
1. SYNOPSIS:函数头文件、参数
2. DESCRIPTION:功能说明,参数说明
3. RETURN VALUE:返回值
4. ERRORS:所有错误返回值和导致错误原因
5. VERSIONS:哪个内核和glibc版本开始支持
6. CONFORMING TO、NOTES、BUGS:其他特殊情况说明,非常重要,一旦出现错误并且自己认为不应该出现错误的情况,可能就是在你系统存在BUGS或ERRORS 、NOTES没读透。

比如EACCES

EACCES The requested access to the file is not allowed, or search per-
mission is denied for one of the directories in the path prefix
of pathname, or the file did not exist yet and write access to
the parent directory is not allowed.
就可以解析出,出现此种错误,可能原因
1. 没文件权限
2. 没查找目录权限
3. 文件不存在
4. 没写权限(当你想以写权限打开时)
所以查看man介绍,你会发现更多可能,加深对这个系统调用的理解。
其他:本人曾负责xattr系统调用,涉及异常出错问题,查看man文档多遍,帮助我对这个系统调用正常和异常场景分析更加透彻,并向ltp社区提交个人构造的用例:-D。
这里写图片描述

你可能感兴趣的文章
Hadoop平台相关技术
查看>>
Android中热修复框架AndFix原理解析及案例使用
查看>>
python3安装scrapy
查看>>
python正则表达式入门一
查看>>
python正则表达式入门二
查看>>
scrapy运行
查看>>
XPATH入门
查看>>
python爬虫 CSS选择器
查看>>
正常关闭java程序
查看>>
查看linux核心数
查看>>
数据结构与算法三: 数组
查看>>
Activiti工作流会签二 启动流程
查看>>
Activiti工作流会签三 撤销,审批,驳回
查看>>
Oauth2方式实现单点登录
查看>>
CountDownLatch源码解析加流程图详解--AQS类注释翻译
查看>>
ES相关度评分
查看>>
我们一起做一个可以商用的springboot脚手架
查看>>
idea在搭建ssm框架时mybatis整合问题 无法找到mapper
查看>>
互斥锁 synchronized分析
查看>>
win10 Docke安装mysql8.0
查看>>