16

Is there any strerror-like functionality currently in the kernel? I haven't been able to find one so my thought is no, but more importantly, has their been any discussion about this? I would think it could reduce troubleshooting time, since you wont have to look up error codes (which not everybody has memorized) and possibly make things a bit easier for system administrators and normal ever-day users (via dmesg).

I wanted to ask here before mailing the LKML. My thoughts were for a dual mechanism, one for the error name (e.g., EINVAL) and another for the description. Further, the %m glibc extension could be added to printk, except that it would have to read a error code since the glibc extension reads errno. Perhaps %m could print the error name while %M could print the error description?

Anyway, if it is added, it should be a .config option since it will bloat the text size. The size can be shrunk by just storing the error names (1 through 133 currently) in a single string with a null terminator between each string and just a slow strerror (forced to iterate through the string and count null-terminators), since the speed of this shouldn't matter. Internal errors 512-529 would have to be in a separate string. Then, a direct pointer to the null-terminated string could be returned with no need to copy anything. By my calculations, this would take roughly 1322 bytes for the error names and 3540 bytes for the descriptions (based upon what are now in comments after each error's #define and adding "no description" for those that are currently missing one).

Then, when config-disabled, the printk %m could just be interpreted as %d and %M could just print nothing (or some such).

Daniel Santos
  • 3,098
  • 26
  • 25
  • 2
    Try `man strace`. If you are interested in the actual error code, then you have alternate means to display it from user space. Sometime errors are expected such as with `stat`, etc. The kernel is in a position to provide more detailed error information outside the context of an `errno`. These are the things that `dmesg` is made of. – artless noise May 07 '13 at 20:50
  • @artless noise thanks for your comment and sorry for the late response. I haven't played with `strace` in a long time and I'd forgotten how useful it is, thanks! For this though, I'm mainly thinking about when you output an error code in a printk. I think I'll put something together and propose it to lkml, but after I'm done with my current project. – Daniel Santos May 29 '13 at 00:59
  • hmm, I had forgotten about this and I've since written this patch. I had to use `%de` however since using %m broke gcc's printf warnings. :( I guess I should go ahead and submit this. The only thing it currently lacks is that it fails to parse a few things in one arch (I think pa-risc, I forget now). – Daniel Santos Sep 03 '13 at 04:20

2 Answers2

7

No, there isn't. But you can see the list of the error codes in include/asm-generic/errno.h and in include/asm-generic/errno-base.h. There isn't too many (fewer than 200), so it is not too hard to learn them during the development.

peterh
  • 11,875
  • 18
  • 85
  • 108
  • 3
    If remembering 200 meaningless values is easy, then I don't want to know what hard is. – Dave Oct 08 '19 at 16:32
  • 1
    @Dave No! `errno-base.h` has only 34 values, `errno.h` has around 130. Note also, that in the practical development/stracing, only some of the error values come most commonly (`EACCESS`, `ENOENT`, and so), all the others are rare! You don't need to learn them all. I can remember around 5 from head, all the others I check in these headers. – peterh Oct 08 '19 at 19:07
3
char *get_error(int error){
    int er = abs(error);
    switch(er){
        case EPERM:
            return "Operation not permitted";
        case ENOENT:
            return "No such file or directory";
        case ESRCH:
            return "No such process";
        case EINTR:
            return "Interrupted system call";
        case EIO:
            return "I/O error";
        case ENXIO:
            return "No such device or address";
        case E2BIG:
            return "Argument list too long";
        case ENOEXEC:
            return "Exec format error";
        case EBADF:
            return "Bad file number";
        case ECHILD:
            return "No child processes";
        case EAGAIN:
            return "Try again Or Operation would block";
        case ENOMEM:
            return "Out of memory";
        case EACCES:
            return "Permission denied";
        case EFAULT:
            return "Bad address";
        case ENOTBLK:
            return "Block device required";
        case EBUSY:
            return "Device or resource busy";
        case EEXIST:
            return "File exists";
        case EXDEV:
            return "Cross-device link";
        case ENODEV:
            return "No such device";
        case ENOTDIR:
            return "Not a directory";
        case EISDIR:
            return "Is a directory";
        case EINVAL:
            return "Invalid argument";
        case ENFILE:
            return "File table overflow";
        case EMFILE:
            return "Too many open files";
        case ENOTTY:
            return "Not a typewriter";
        case ETXTBSY:
            return "Text file busy";
        case EFBIG:
            return "File too large";
        case ENOSPC:
            return "No space left on device";
        case ESPIPE:
            return "Illegal seek";
        case EROFS:
            return "Read-only file system";
        case EMLINK:
            return "Too many links";
        case EPIPE:
            return "Broken pipe";
        case EDOM:
            return "Math argument out of domain of func";
        case ERANGE:
            return "Math result not representable";

        case EDEADLK:
            return "Resource deadlock would occur";
        case ENAMETOOLONG:
            return "File name too long";
        case ENOLCK:
            return "No record locks available";
        case ENOSYS:
            return "Invalid system call number";
        case ENOTEMPTY:
            return "Directory not empty";
        case ELOOP:
            return "Too many symbolic links encountered";
        case ENOMSG:
            return "No message of desired type";
        case EIDRM:
            return "Identifier removed";
        case ECHRNG:
            return "Channel number out of range";
        case EL2NSYNC:
            return "Level 2 not synchronized";
        case EL3HLT:
            return "Level 3 halted";
        case EL3RST:
            return "Level 3 reset";
        case ELNRNG:
            return "Link number out of range";
        case EUNATCH:
            return "Protocol driver not attached";
        case ENOCSI:
            return "No CSI structure available";
        case EL2HLT:
            return "Level 2 halted";
        case EBADE:
            return "Invalid exchange";
        case EBADR:
            return "Invalid request descriptor";
        case EXFULL:
            return "Exchange full";
        case ENOANO:
            return "No anode";
        case EBADRQC:
            return "Invalid request code";
        case EBADSLT:
            return "Invalid slot";
        case EBFONT:
            return "Bad font file format";
        case ENOSTR:
            return "Device not a stream";
        case ENODATA:
            return "No data available";
        case ETIME:
            return "Timer expired";
        case ENOSR:
            return "Out of streams resources";
        case ENONET:
            return "Machine is not on the network";
        case ENOPKG:
            return "Package not installed";
        case EREMOTE:
            return "Object is remote";
        case ENOLINK:
            return "Link has been severed";
        case EADV:
            return "Advertise error";
        case ESRMNT:
            return "Srmount error";
        case ECOMM:
            return "Communication error on send";
        case EPROTO:
            return "Protocol error";
        case EMULTIHOP:
            return "Multihop attempted";
        case EDOTDOT:
            return "RFS specific error";
        case EBADMSG:
            return "Not a data message";
        case EOVERFLOW:
            return "Value too large for defined data type";
        case ENOTUNIQ:
            return "Name not unique on network";
        case EBADFD:
            return "File descriptor in bad state";
        case EREMCHG:
            return "Remote address changed";
        case ELIBACC:
            return "Can not access a needed shared library";
        case ELIBBAD:
            return "Accessing a corrupted shared library";
        case ELIBSCN:
            return ".lib section in a.out corrupted";
        case ELIBMAX:
            return "Attempting to link in too many shared libraries";
        case ELIBEXEC:
            return "Cannot exec a shared library directly";
        case EILSEQ:
            return "Illegal byte sequence";
        case ERESTART:
            return "Interrupted system call should be restarted";
        case ESTRPIPE:
            return "Streams pipe error";
        case EUSERS:
            return "Too many users";
        case ENOTSOCK:
            return "Socket operation on non-socket";
        case EDESTADDRREQ:
            return "Destination address required";
        case EMSGSIZE:
            return "Message too long";
        case EPROTOTYPE:
            return "Protocol wrong type for socket";
        case ENOPROTOOPT:
            return "Protocol not available";
        case EPROTONOSUPPORT:
            return "Protocol not supported";
        case ESOCKTNOSUPPORT:
            return "Socket type not supported";
        case EOPNOTSUPP:
            return "Operation not supported on transport endpoint";
        case EPFNOSUPPORT:
            return "Protocol family not supported";
        case EAFNOSUPPORT:
            return "Address family not supported by protocol";
        case EADDRINUSE:
            return "Address already in use";
        case EADDRNOTAVAIL:
            return "Cannot assign requested address";
        case ENETDOWN:
            return "Network is down";
        case ENETUNREACH:
            return "Network is unreachable";
        case ENETRESET:
            return "Network dropped connection because of reset";
        case ECONNABORTED:
            return "Software caused connection abort";
        case ECONNRESET:
            return "Connection reset by peer";
        case ENOBUFS:
            return "No buffer space available";
        case EISCONN:
            return "Transport endpoint is already connected";
        case ENOTCONN:
            return "Transport endpoint is not connected";
        case ESHUTDOWN:
            return "Cannot send after transport endpoint shutdown";
        case ETOOMANYREFS:
            return "Too many references: cannot splice";
        case ETIMEDOUT:
            return "Connection timed out";
        case ECONNREFUSED:
            return "Connection refused";
        case EHOSTDOWN:
            return "Host is down";
        case EHOSTUNREACH:
            return "No route to host";
        case EALREADY:
            return "Operation already in progress";
        case EINPROGRESS:
            return "Operation now in progress";
        case ESTALE:
            return "Stale file handle";
        case EUCLEAN:
            return "Structure needs cleaning";
        case ENOTNAM:
            return "Not a XENIX named type file";
        case ENAVAIL:
            return "No XENIX semaphores available";
        case EISNAM:
            return "Is a named type file";
        case EREMOTEIO:
            return "Remote I/O error";
        case EDQUOT:
            return "Quota exceeded";
        case ENOMEDIUM:
            return "No medium found";
        case EMEDIUMTYPE:
            return "Wrong medium type";
        case ECANCELED:
            return "Operation Canceled";
        case ENOKEY:
            return "Required key not available";
        case EKEYREVOKED:
            return "Key has been revoked";
        case EKEYREJECTED:
            return "Key was rejected by service";
        case EOWNERDEAD:
            return "Owner died";
        case ENOTRECOVERABLE:
            return "State not recoverable";
        case ERFKILL:
            return "Operation not possible due to RF-kill";
        case EHWPOISON:
            return "Memory page has hardware error";
    }
    return "Unknown Error Dash!";
}
EXPORT_SYMBOL(get_error);
Mahdi Mohammadi
  • 239
  • 2
  • 7