Linux How to get error description by error number

2020-05-06 04:31发布

In linux (specifically I have Ubuntu 14) if some program terminates with an error, I can get numerical error code via $? variable

$ ./failing_app
$ echo $?

However the number itself does not tell me much, how do I get error name and description?

There is a list of errors in $ man errno but it gives only names, not numerical values.

I've search the google and results are quite strange: e.g. I have a toy Rust program in which I'm trying to access array element out of bounds. The program panics and $? is 101, however it is said to correspond to Network unreachable error, which doesn't make any sense.

标签: linux ubuntu
3条回答
走好不送
2楼-- · 2020-05-06 05:15

This link provides a list for errors with the numbers. Hope this is what you are looking for. http://www.thegeekstuff.com/2010/10/linux-error-codes/

Also, on my system I see the file /usr/include/asm-generic/errno-base.h has the definitions of the errors as well as error numbers. This would be a better reference than the link.

查看更多
狗以群分
3楼-- · 2020-05-06 05:20

The exit status of a program ($? in the shell) is unrelated to the C errno.

In a C program, normally the exit status comes from the argument to exit or the return value of main. The convention is that 0 means a successful exit (or true for the shell) and other values are a failure (ir false).

However if a program died from receiving a signal, the shell sets $? to 128 plus the signal number. For example, on a segmentation fault (SIGSEGV, which is 11), $? would be 139.

To list signal numbers, I run kill -l.

查看更多
Melony?
4楼-- · 2020-05-06 05:24

There is no single central defining authority. Each program assigns its own semantics to error codes. The good ones have documentation in the manual page; for examples, see e.g. the grep and xargs manual pages from GNU.

The exit(3) Linux manual page also notes that "BSD has attempted to standardize exit codes". The BSD sysexits(3) manual page is actually good recommended reading, bus as indicated, hardly more than a nudge for a limited number of error scenarios.

The kernel has a much more detailed and well-documented set of possible errors and their causes, but this is obviously limited to system calls, and does not address application-level errors at all. For Linux, see the errno(3) manual page.

The Advanced Bash Scripting Guide has a section about common conventions; http://tldp.org/LDP/abs/html/exitcodes.html#EXITCODESREF -- but like the ABS in general, it is an unholy mixture of standards, conventions, the author's personal opinion, guesswork, and lies. (None of the exit codes in the table are "reserved", even though it says so, and the text which refers to the table emphasizes this incorrect fact.)

查看更多
登录 后发表回答