Glibc_error reporting, glibc_error

Source: Internet
Author: User
Tags mathematical functions

Glibc_error reporting, glibc_error

Many functions in the gnu c library detect and report error conditions. Our program needs to detect these error conditions. For example, when opening an input file, we need to determine whether the file is correctly opened. If it is not opened correctly, we need to print the error or take another correct method. To use this error reporting mechanism, we need to include the header file errno. h.

Check error:

Many library functions return a special value to show function running errors. Common special values include-1, null pointer, and EOF constant. However, the returned values only tell you that errors are generated, but do not tell you what the errors are. If you want to know what the error is, you have to rely on the error code. The error code is stored in the variable errno (declared in errno. h)

The errno variable contains the system error code. Its type is volatile. This type means that it can be suddenly changed by asynchronous threads, and the compiler never assumes its value. If you are writing a signal processing program, you should save the value of the change volume and restore its value.

The initial value of errno is 0. When an error occurs, errno cannot be 0. If there is no error, errno is not necessarily set to 0 (the database function does not modify the errno value when it runs successfully ). Therefore, do not judge whether an error occurs based on the errno value. The correct method is to document each function and mark the error type corresponding to the value of the error code. In this case, you can check errno to obtain the error code and then query the function documentation to obtain the error details. If you want to get the error code of a function in a library, you 'd better set errno to 0 again (maybe you want to save the following errno value and then restore it easily ).

Each error code has a symbolic name starting with E followed by uppercase letters or numbers. It is actually a macro defined in errno. h. Of course, not all macros are defined in an errno. h (for details, you can flip the header file by yourself. Note that there is not only one errno. h, multiple errno. h defines all macros together)

The value of the error code is generally positive and different, but there is also an exception: the error code of EWORLDBLOCK and EAGAIN is the same. Besides EWORLDBLOCK and EAGAIN, you can use the switch statement to determine the error code. But you should not rely on this. The only thing you can trust is the document.

Except for the GNU/Hurd system, EFAULT is returned when almost all system calls are passed into an invalid pointer. Therefore, glibc's function library instructions often omit the EFAULT explanation.

The macro names of most error codes are obviously easy to understand. If you really don't know what it is, you can check the manual or

~# man errno

Here are a few macros:

Macro: int EDOM domain error. It can be understood as a custom domain error. It is mainly used in mathematical functions. If a parameter value of a mathematical function is not in the definition field of the function, errno is set to EDOMMacro: int ERANGE range error, which is the opposite of the preceding EDOM. If EDOM is a defined field, ERANGER is a value field. It is also used in mathematical functions. If the return value of the mathematical function exceeds the agreed return value, the errno is set to ERANGEMacro: int EAGAIN resource temporarily unavailable. This kind of error may be random, and it will be good for you to run it again... EWOULDBLOCK is an alias of EAGAIN.

 

Error message:

We know the error code, but we always find it inconvenient to query the document. Fortunately, the library file provides us with the error message reporting function. These functions can report a descriptive error message. We can define the Message format for some message reporting functions.

The strerror and perror functions provide a standard error message for each error code. The variable program_invocation_short_name can easily obtain the program name and tell us which program has an error.

Several function prototypes:

#include <string.h>char * strerror(int errnum);char *strerror_r(int errnum, char *buf, size_t n);

Note: The strerror and strerror_r functions are similar. The difference lies in security. The official documents comment on strerror is MT-Unsafe race: strerror, while strerror_r is MT-Safe. Strerror returns a statically applied string buffer, which is shared by all threads. Strerror_r returns a private copy, which is not shared by other threads. Both functions may cause memory overflow (static applied buffer ). Although strerror_r can specify the string length, the length is char * buf. This function has two return values: one is returned using reurn and the other is char * buf. Return returns a static buffer.

 

#include<stdio.h>void perror(const char *message);

Note: perror prints the error message to the standard error output. If the input parameter is a null pointer, perror prints an error message based on errno. If the char * message is not empty, perror outputs the message as the prefix of the error message. Perror must be called immediately. Otherwise, the value of errno may change.

 

Char * program_invocation_name; // equivalent to argv [0] char * program_invocation_short_name // does not contain the directory name.

Note: The initialization of these two variables is performed by the glibc library before the main function is called. Therefore, in a non-GNU Library, these two variables are ineffective. In actual code, we need to define the _ GNU_SOURCE macro and tell the compiler to use the GNU Library.

The following two functions are widely used in the GNU project.

void error(int status, int errnum, const char *format, ...);void error_at_line(int status, int errnum, const char *fname, unsigned int lineno, const char *format, ...);

Note: The returned results of these two functions are related to status. If status is 0, the error message is formatted and printed normally. The global variable error_message_count also performs the auto-increment operation. The error message format is as follows: program_name: format_string: error_messager_for_errno \ n. If the status is not zero, the two functions call exit status, that is, exit with status (not returned ). Program_name: The function pointed to by the global variable error_print_progname determines the value of program_name. The error_at_line function is a bit special: there are two more parameters: fname and lineno. The error message format is as follows: program_name: fname: lineno format_string: error_mesage_for_errno \ n. If the global variable error_one_per_line is set to a non-zero value, only one error message is printed for each row.

In addition to the above error message functions, we also have the following: these functions are mainly used in the BSD system and are defined in the header file err. h, which is not recommended in the gnu system.

void warn(const char *format, ...)void vwarn(const char *format, va_list ap)void warnx(const char *format, ...)void vwarnx(const char *format, va_list ap)void err(int status, const char *format, ...)void verr(int status, const char *format, va_list ap)void errx(int status, const char *format, ...)void verrx(int status, const char *format, va_list ap)

You can view the manual by yourself. Just be a man !!!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.