Error codes and logging
Error returns
Raw syscalls return errors as a negative number in RAX, between -4095 and -1. Check the sign of RAX after every call that can fail, and negate it to get the positive errno:
syscall cmp rax, 0 jge ok ; no error neg rax ; rax = errno ; handle the error ok:
errno values
Common errnos: 2 ENOENT (no such file), 5 EIO, 9 EBADF (bad descriptor), 13 EACCES, 17 EEXIST, 22 EINVAL. The list is in errno.h. Your program maps them to a message table and prints the text.
Writing a log
A log is a file opened with O_APPEND (0x400), written with sys_write, and closed with sys_close. Write one line per event: timestamp, severity, message, newline. Opening and closing per line is slow but simple; keeping the descriptor open is the production version.
This complete program opens a file that does not exist, gets the negative error, turns it into the positive errno and reports it:
default rel %define SYS_OPEN 2 %define SYS_WRITE 1 %define SYS_EXIT 60 %define O_RDONLY 0 section .data missing db "missing.txt", 0 msg db "errno 2", 10 ok db "no error", 10 section .text global _start _start: mov rax, SYS_OPEN mov rdi, missing mov rsi, O_RDONLY xor rdx, rdx syscall cmp rax, 0 jge fine neg rax ; rax = errno cmp rax, 2 jne fine mov rax, SYS_WRITE mov rdi, 1 mov rsi, msg mov rdx, 8 syscall jmp done fine: mov rax, SYS_WRITE mov rdi, 1 mov rsi, ok mov rdx, 9 syscall done: mov rax, SYS_EXIT xor rdi, rdi syscall
nasm -f elf64 -o err.o err.asm
ld -o err err.o
./err
echo $?
errno 2
0
The first snippet is a fragment: it shows the sign test and the negate. The complete program was assembled with NASM 3.01, linked with GNU ld 2.45.1, and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
How does a raw syscall report an error?
Answer
A negative value in RAX
The kernel returns -errno, so a negative RAX means failure.
-
Which range holds the error returns?
Answer
-4095 to -1
The kernel reserves the top of the negative range for errors.
-
What does
neg raxdo after a failed call?Answer
It turns -errno into the positive errno
Negating a negative error gives the positive number errno.h names.
-
Which errno is ENOENT?
Answer
2
ENOENT is 2, no such file or directory.
-
Which errno means a bad file descriptor?
Answer
EBADF, 9
EBADF is 9, and close or read on an invalid descriptor returns it.
-
Which open flag appends to an existing file?
Answer
O_APPEND, 0x400
O_APPEND moves the write position to the end for every write.
-
What does this program print?
default rel %define SYS_OPEN 2 %define SYS_WRITE 1 %define SYS_EXIT 60 %define O_RDONLY 0 section .data missing db "missing.txt", 0 msg db "errno 2", 10 ok db "no error", 10 section .text global _start _start: mov rax, SYS_OPEN mov rdi, missing mov rsi, O_RDONLY xor rdx, rdx syscall cmp rax, 0 jge fine neg rax cmp rax, 2 jne fine mov rax, SYS_WRITE mov rdi, 1 mov rsi, msg mov rdx, 8 syscall jmp done fine: mov rax, SYS_WRITE mov rdi, 1 mov rsi, ok mov rdx, 9 syscall done: mov rax, SYS_EXIT xor rdi, rdi syscallAnswer
errno 2
The file is absent, so open returns -2 and the program prints the errno message.
-
What exit status does that program return?
Answer
0
The program reports the error and still exits with 0.
-
When should a descriptor stay open for logging?
Answer
When the program writes many lines and the open cost matters
One open with repeated writes is the production form; open per line is simple but slow.
-
Which check must come right after a call that can fail?
Answer
The sign of RAX
A negative RAX is the error, and it must be tested before the value is used.
Article author: Arthur Isaev