在程式出錯時,我們通過全局變量錯誤号errno和perror函數能夠很快的定位到錯誤原因。
Linux錯誤代碼及其含義
C Name Value Description 含義
Success 0 Success 成功
EPERM 1 Operation not permitted 操作不允許
ENOENT 2 No such file or directory 沒有這樣的檔案或目錄
ESRCH 3 No such process 沒有這樣的過程
EINTR 4 Interrupted system call 系統調用被中斷
EIO 5 I/O error I/O錯誤
ENXIO 6 No such device or address 沒有這樣的裝置或位址
E2BIG 7 Arg list too long 參數清單太長
ENOEXEC 8 Exec format error 執行格式錯誤
EBADF 9 Bad file number 壞的檔案描述符
ECHILD 10 No child processes 沒有子程序
EAGAIN 11 Try again 資源暫時不可用
ENOMEM 12 Out of memory 記憶體溢出
EACCES 13 Permission denied 拒絕許可
EFAULT 14 Bad address 錯誤的位址
ENOTBLK 15 Block device required 塊裝置請求
EBUSY 16 Device or resource busy 裝置或資源忙
EEXIST 17 File exists 檔案存在
EXDEV 18 Cross-device link 無效的交叉連結
ENODEV 19 No such device 裝置不存在
ENOTDIR 20 Not a directory 不是一個目錄
EISDIR 21 Is a directory 是一個目錄
EINVAL 22 Invalid argument 無效的參數
ENFILE* 23 File table overflow 打開太多的檔案系統
EMFILE 24 Too many open files 打開的檔案過多
ENOTTY 25 Not a tty device 不是tty裝置
ETXTBSY 26 Text file busy 文本檔案忙
EFBIG 27 File too large 檔案太大
ENOSPC 28 No space left on device 裝置上沒有空間
ESPIPE 29 Illegal seek 非法移位
EROFS 30 Read-only file system 隻讀檔案系統
EMLINK 31 Too many links 太多的連結
EPIPE 32 Broken pipe 管道破裂
EDOM 33 Math argument out of domain 數值結果超出範圍
ERANGE 34 Math result not representable 數值結果不具代表性
EDEADLK 35 Resource deadlock would occur 資源死鎖錯誤
ENAMETOOLONG 36 Filename too long 檔案名太長
ENOLCK 37 No record locks available 沒有可用鎖
ENOSYS 38 Function not implemented 功能沒有實作
ENOTEMPTY 39 Directory not empty 目錄不空
ELOOP 40 Too many symbolic links encountered 符号連結層次太多
EWOULDBLOCK 41 Same as EAGAIN 和EAGAIN一樣
ENOMSG 42 No message of desired type 沒有期望類型的消息
EIDRM 43 Identifier removed 辨別符删除
ECHRNG 44 Channel number out of range 頻道數目超出範圍
EL2NSYNC 45 Level 2 not synchronized 2級不同步
EL3HLT 46 Level 3 halted 3級中斷
EL3RST 47 Level 3 reset 3級複位
ELNRNG 48 Link number out of range 連結數超出範圍
EUNATCH 49 Protocol driver not attached 協定驅動程式沒有連接配接
ENOCSI 50 No CSI structure available 沒有可用CSI結構
EL2HLT 51 Level 2 halted 2級中斷
EBADE 52 Invalid exchange 無效的交換
EBADR 53 Invalid request descriptor 請求描述符無效
EXFULL 54 Exchange full 交換全
ENOANO 55 No anode 沒有陽極
EBADRQC 56 Invalid request code 無效的請求代碼
EBADSLT 57 Invalid slot 無效的槽
EDEADLOCK 58 Same as EDEADLK 和EDEADLK一樣
EBFONT 59 Bad font file format 錯誤的字型檔案格式
ENOSTR 60 Device not a stream 裝置不是字元流
ENODATA 61 No data available 無可用資料
ETIME 62 Timer expired 計時器過期
ENOSR 63 Out of streams resources 流資源溢出
ENONET 64 Machine is not on the network 機器不上網
ENOPKG 65 Package not installed 沒有安裝軟體包
EREMOTE 66 Object is remote 對象是遠端的
ENOLINK 67 Link has been severed 聯系被切斷
EADV 68 Advertise error 廣告的錯誤
ESRMNT 69 Srmount error srmount錯誤
ECOMM 70 Communication error on send 發送時的通訊錯誤
EPROTO 71 Protocol error 協定錯誤
EMULTIHOP 72 Multihop attempted 多跳嘗試
EDOTDOT 73 RFS specific error RFS特定的錯誤
EBADMSG 74 Not a data message 非資料消息
EOVERFLOW 75 Value too large for defined data type 值太大,對于定義資料類型
ENOTUNIQ 76 Name not unique on network 名不是唯一的網絡
EBADFD 77 File descriptor in bad state 檔案描述符在壞狀态
EREMCHG 78 Remote address changed 遠端位址改變了
ELIBACC 79 Cannot access a needed shared library 無法通路必要的共享庫
ELIBBAD 80 Accessing a corrupted shared library 通路損壞的共享庫
ELIBSCN 81 A .lib section in an .out is corrupted 庫段. out損壞
ELIBMAX 82 Linking in too many shared libraries 試圖連結太多的共享庫
ELIBEXEC 83 Cannot exec a shared library directly 不能直接執行一個共享庫
EILSEQ 84 Illegal byte sequence 無效的或不完整的多位元組或寬字元
ERESTART 85 Interrupted system call should be restarted 應該重新啟動中斷的系統調用
ESTRPIPE 86 Streams pipe error 流管錯誤
EUSERS 87 Too many users 使用者太多
ENOTSOCK 88 Socket operation on non-socket 套接字操作在非套接字上
EDESTADDRREQ 89 Destination address required 需要目标位址
EMSGSIZE 90 Message too long 消息太長
EPROTOTYPE 91 Protocol wrong type for socket socket協定類型錯誤
ENOPROTOOPT 92 Protocol not available 協定不可用
EPROTONOSUPPORT 93 Protocol not supported 不支援的協定
ESOCKTNOSUPPORT 94 Socket type not supported 套接字類型不受支援
EOPNOTSUPP 95 Operation not supported on transport 不支援的操作
EPFNOSUPPORT 96 Protocol family not supported 不支援的協定族
EAFNOSUPPORT 97 Address family not supported by protocol 協定不支援的位址
EADDRINUSE 98 Address already in use 位址已在使用
EADDRNOTAVAIL 99 Cannot assign requested address 無法配置設定請求的位址
ENETDOWN 100 Network is down 網絡癱瘓
ENETUNREACH 101 Network is unreachable 網絡不可達
ENETRESET 102 Network dropped 網絡連接配接丢失
ECONNABORTED 103 Software caused connection 軟體導緻連接配接中斷
ECONNRESET 104 Connection reset by 連接配接被重置
ENOBUFS 105 No buffer space available 沒有可用的緩沖空間
EISCONN 106 Transport endpoint 傳輸端點已經連接配接
ENOTCONN 107 Transport endpoint 傳輸終點沒有連接配接
ESHUTDOWN 108 Cannot send after transport 傳輸後無法發送
ETOOMANYREFS 109 Too many references 太多的參考
ETIMEDOUT 110 Connection timed 連接配接逾時
ECONNREFUSED 111 Connection refused 拒絕連接配接
EHOSTDOWN 112 Host is down 主機已關閉
EHOSTUNREACH 113 No route to host 沒有主機的路由
EALREADY 114 Operation already 已運作
EINPROGRESS 115 Operation now in 正在運作
ESTALE 116 Stale NFS file handle 陳舊的NFS檔案句柄
EUCLEAN 117 Structure needs cleaning 結構需要清洗
ENOTNAM 118 Not a XENIX-named 不是XENIX命名的
ENAVAIL 119 No XENIX semaphores 沒有XENIX信号量
EISNAM 120 Is a named type file 是一個命名的檔案類型
EREMOTEIO 121 Remote I/O error 遠端輸入/輸出錯誤
EDQUOT 122 Quota exceeded 超出磁盤配額
ENOMEDIUM 123 No medium found 沒有磁盤被發現
EMEDIUMTYPE 124 Wrong medium type 錯誤的媒體類型
ECANCELED 125 Operation Canceled 取消操作
ENOKEY 126 Required key not available 所需鍵不可用
EKEYEXPIRED 127 Key has expired 關鍵已過期
EKEYREVOKED 128 Key has been revoked 關鍵被撤銷
EKEYREJECTED 129 Key was rejected by service 關鍵被拒絕服務
EOWNERDEAD 130 Owner died 所有者死亡
ENOTRECOVERABLE 131 State not recoverable 狀态不可恢複
ERFKILL 132 Operation not possible due to RF-kill 由于RF-kill而無法操作
暫時記錄這麼多,133-255全是Unknown error
二、錯誤處理函數
許多不同的函數庫都在失敗時設定全局變量errno的值來指明失敗原因。值得注意的是函數報告出錯後必須立刻檢查errno變量,因為它有可能被下一個函數調用所覆寫。
有兩個非常有用的函數可以用來報告出現的錯誤:
#include <string.>
char *strerror(int errnum);
該函數把錯誤代碼映射為一個字元串,對錯誤類型進行說明。
#include <stdio.h>
void perror(const char *s);
該函數也把errno變量中報告的錯誤應為到一個字元串,并把它輸出到标準錯誤輸出流。
---------------------
檢視錯誤代碼errno是調試程式的一個重要方法。當linuc C api函數發生異常時,一般會将errno變量(需include errno.h)賦一個整數值,不同的值表示不同的含義,可以通過檢視該值推測出錯的原因。在實際程式設計中用這一招解決了不少原本看來莫名其妙的問題。比較麻煩的是每次都要去linux源代碼裡面查找錯誤代碼的含義,現在把它貼出來,以後需要查時就來這裡看了。
1-34号錯誤号是在核心源碼的include/asm-generic/errno-base.h定義
35-132則是在include/asm-generic/errno.h中定義
剩下還有一些更大的錯誤号是留給核心級别的,如系統調用等,使用者程式一般是看不見的這些号的,Ubuntu9.10中/usr/src/linux-headers-2.6.31-21-generic/include/linux/errno.h
#ifndef _ASM_GENERIC_ERRNO_BASE_H
#define _ASM_GENERIC_ERRNO_BASE_H
#define EPERM 1 /* Operation not permitted */
#define ENOENT 2 /* No such file or directory */
#define ESRCH 3 /* No such process */
#define EINTR 4 /* Interrupted system call */
#define EIO 5 /* I/O error */
#define ENXIO 6 /* No such device or address */
#define E2BIG 7 /* Argument list too long */
#define ENOEXEC 8 /* Exec format error */
#define EBADF 9 /* Bad file number */
#define ECHILD 10 /* No child processes */
#define EAGAIN 11 /* Try again */
#define ENOMEM 12 /* Out of memory */
#define EACCES 13 /* Permission denied */
#define EFAULT 14 /* Bad address */
#define ENOTBLK 15 /* Block device required */
#define EBUSY 16 /* Device or resource busy */
#define EEXIST 17 /* File exists */
#define EXDEV 18 /* Cross-device link */
#define ENODEV 19 /* No such device */
#define ENOTDIR 20 /* Not a directory */
#define EISDIR 21 /* Is a directory */
#define EINVAL 22 /* Invalid argument */
#define ENFILE 23 /* File table overflow */
#define EMFILE 24 /* Too many open files */
#define ENOTTY 25 /* Not a typewriter */
#define ETXTBSY 26 /* Text file busy */
#define EFBIG 27 /* File too large */
#define ENOSPC 28 /* No space left on device */
#define ESPIPE 29 /* Illegal seek */
#define EROFS 30 /* Read-only file system */
#define EMLINK 31 /* Too many links */
#define EPIPE 32 /* Broken pipe */
#define EDOM 33 /* Math argument out of domain of func */
#define ERANGE 34 /* Math result not representable */
#endif
#include <asm-generic/errno-base.h>
#define EDEADLK 35 /* Resource deadlock would occur */
#define ENAMETOOLONG 36 /* File name too long */
#define ENOLCK 37 /* No record locks available */
#define ENOSYS 38 /* Function not implemented */
#define ENOTEMPTY 39 /* Directory not empty */
#define ELOOP 40 /* Too many symbolic links encountered */
#define EWOULDBLOCK EAGAIN /* Operation would block */
#define ENOMSG 42 /* No message of desired type */
#define EIDRM 43 /* Identifier removed */
#define ECHRNG 44 /* Channel number out of range */
#define EL2NSYNC 45 /* Level 2 not synchronized */
#define EL3HLT 46 /* Level 3 halted */
#define EL3RST 47 /* Level 3 reset */
#define ELNRNG 48 /* Link number out of range */
#define EUNATCH 49 /* Protocol driver not attached */
#define ENOCSI 50 /* No CSI structure available */
#define EL2HLT 51 /* Level 2 halted */
#define EBADE 52 /* Invalid exchange */
#define EBADR 53 /* Invalid request descriptor */
#define EXFULL 54 /* Exchange full */
#define ENOANO 55 /* No anode */
#define EBADRQC 56 /* Invalid request code */
#define EBADSLT 57 /* Invalid slot */
#define EDEADLOCK EDEADLK
#define EBFONT 59 /* Bad font file format */
#define ENOSTR 60 /* Device not a stream */
#define ENODATA 61 /* No data available */
#define ETIME 62 /* Timer expired */
#define ENOSR 63 /* Out of streams resources */
#define ENONET 64 /* Machine is not on the network */
#define ENOPKG 65 /* Package not installed */
#define EREMOTE 66 /* Object is remote */
#define ENOLINK 67 /* Link has been severed */
#define EADV 68 /* Advertise error */
#define ESRMNT 69 /* Srmount error */
#define ECOMM 70 /* Communication error on send */
#define EPROTO 71 /* Protocol error */
#define EMULTIHOP 72 /* Multihop attempted */
#define EDOTDOT 73 /* RFS specific error */
#define EBADMSG 74 /* Not a data message */
#define EOVERFLOW 75 /* Value too large for defined data type */
#define ENOTUNIQ 76 /* Name not unique on network */
#define EBADFD 77 /* File descriptor in bad state */
#define EREMCHG 78 /* Remote address changed */
#define ELIBACC 79 /* Can not access a needed shared library */
#define ELIBBAD 80 /* Accessing a corrupted shared library */
#define ELIBSCN 81 /* .lib section in a.out corrupted */
#define ELIBMAX 82 /* Attempting to link in too many shared libraries */
#define ELIBEXEC 83 /* Cannot exec a shared library directly */
#define EILSEQ 84 /* Illegal byte sequence */
#define ERESTART 85 /* Interrupted system call should be restarted */
#define ESTRPIPE 86 /* Streams pipe error */
#define EUSERS 87 /* Too many users */
#define ENOTSOCK 88 /* Socket operation on non-socket */
#define EDESTADDRREQ 89 /* Destination address required */
#define EMSGSIZE 90 /* Message too long */
#define EPROTOTYPE 91 /* Protocol wrong type for socket */
#define ENOPROTOOPT 92 /* Protocol not available */
#define EPROTONOSUPPORT 93 /* Protocol not supported */
#define ESOCKTNOSUPPORT 94 /* Socket type not supported */
#define EOPNOTSUPP 95 /* Operation not supported on transport endpoint */
#define EPFNOSUPPORT 96 /* Protocol family not supported */
#define EAFNOSUPPORT 97 /* Address family not supported by protocol */
#define EADDRINUSE 98 /* Address already in use */
#define EADDRNOTAVAIL 99 /* Cannot assign requested address */
#define ENETDOWN 100 /* Network is down */
#define ENETUNREACH 101 /* Network is unreachable */
#define ENETRESET 102 /* Network dropped connection because of reset */
#define ECONNABORTED 103 /* Software caused connection abort */
#define ECONNRESET 104 /* Connection reset by peer */
#define ENOBUFS 105 /* No buffer space available */
#define EISCONN 106 /* Transport endpoint is already connected */
#define ENOTCONN 107 /* Transport endpoint is not connected */
#define ESHUTDOWN 108 /* Cannot send after transport endpoint shutdown */
#define ETOOMANYREFS 109 /* Too many references: cannot splice */
#define ETIMEDOUT 110 /* Connection timed out */
#define ECONNREFUSED 111 /* Connection refused */
#define EHOSTDOWN 112 /* Host is down */
#define EHOSTUNREACH 113 /* No route to host */
#define EALREADY 114 /* Operation already in progress */
#define EINPROGRESS 115 /* Operation now in progress */
#define ESTALE 116 /* Stale NFS file handle */
#define EUCLEAN 117 /* Structure needs cleaning */
#define ENOTNAM 118 /* Not a XENIX named type file */
#define ENAVAIL 119 /* No XENIX semaphores available */
#define EISNAM 120 /* Is a named type file */
#define EREMOTEIO 121 /* Remote I/O error */
#define EDQUOT 122 /* Quota exceeded */
#define ENOMEDIUM 123 /* No medium found */
#define EMEDIUMTYPE 124 /* Wrong medium type */
#define ECANCELED 125 /* Operation Canceled */
#define ENOKEY 126 /* Required key not available */
#define EKEYEXPIRED 127 /* Key has expired */
#define EKEYREVOKED 128 /* Key has been revoked */
#define EKEYREJECTED 129 /* Key was rejected by service */
/* for robust mutexes */
#define EOWNERDEAD 130 /* Owner died */
#define ENOTRECOVERABLE 131 /* State not recoverable */
#define ERFKILL 132 /* Operation not possible due to RF-kill */
#ifdef __KERNEL__
/*
* These should never be seen by user programs. To return one of ERESTART*
* codes, signal_pending() MUST be set. Note that ptrace can observe these
* at syscall exit tracing, but they will never be left for the debugged user
* process to see.
*/
#define ERESTARTSYS 512
#define ERESTARTNOINTR 513
#define ERESTARTNOHAND 514 /* restart if no handler.. */
#define ENOIOCTLCMD 515 /* No ioctl command */
#define ERESTART_RESTARTBLOCK 516 /* restart by calling sys_restart_syscall */
/* Defined for the NFSv3 protocol */
#define EBADHANDLE 521 /* Illegal NFS file handle */
#define ENOTSYNC 522 /* Update synchronization mismatch */
#define EBADCOOKIE 523 /* Cookie is stale */
#define ENOTSUPP 524 /* Operation is not supported */
#define ETOOSMALL 525 /* Buffer or request is too small */
#define ESERVERFAULT 526 /* An untranslatable error occurred */
#define EBADTYPE 527 /* Type not supported by server */
#define EJUKEBOX 528 /* Request initiated, but will not complete before timeout */
#define EIOCBQUEUED 529 /* iocb queued, will get completion event */
#define EIOCBRETRY 530 /* iocb queued, will trigger a retry */
#endif