Log inStart free
LESSON 1.15 · 6 STAGES · 2 CONTROLS · 3 CHECKSPRO · PREVIEW

System calls

Watch a program ask the kernel for a file, and see what strace prints.


Pro lesson. Stage 1 is free to watch. Pro opens all 6 stages, 2 controls and 3 checks, in this lesson and every other.

Go Pro · $8.25/mo
EXPLAINER · 1.15 · SYSCALL BAYKERNEL MODE · RAX 257 · 1 CALL
01
Stage 1 of 6 · Enter kernel
os.open makes a system call: call number 257 and its arguments go into registers. The CPU switches to kernel mode.
Explain it simply

The top band is user space, where your program runs. It can't reach the disk or the screen by itself. So os.open asks the kernel, the part of the system that can. It puts 257, the number for openat, in the register rax, and a pointer to notes.txt in rsi. Then the CPU switches to kernel mode, the bottom band.

The program is at line 3, os.open of notes.txt. The registers hold rax 257, rdi -100, rsi a pointer to the path, and rdx 0x80000. The CPU is in kernel mode. The syscall table row 257, openat, is lit. strace has printed the first half of the line.

code, tables and boundarybytes and datacall number, registers and pointerserrorresult and output
boundaryuser spacekernel spaceprogrammemorysyscall tablefd table · pid 5340file notes.txtterminalstrace output1import os2try:3 fd = os.open("notes.txt", os.O_RDONLY)4except OSError as e:5 os.write(2, f"{e.strerror}\n".encode())6 raise SystemExit(1)7while data := os.read(fd, 64):8 os.write(1, data.upper())9os.close(fd)path'notes.txt'stracea second processptrace on pid 5340stops1system calls1programstoppedCPU ▼ kernel moderax257rdi-100rsi&pathrdx0x800000read1write3close231exit_group257openat0→ terminal1→ terminal2→ terminalmode rw-r--r-- (644)hello\nopenat(AT_FDCWD, "notes.txt", O_RDONLY|O_CLOEXEC ▌boundaryuser spacekernel spaceprogrammemorysyscall tablefd table · pid 5340file notes.txtterminalstrace output1import os2try:3 fd = os.open("notes.txt", os.O_RDONLY)4except OSError as e:5 os.write(2, f"{e.strerror}\n".encode())6 raise SystemExit(1)7while data := os.read(fd, 64):8 os.write(1, data.upper())9os.close(fd)path'notes.txt'stracea second processptrace on pid 5340stops1system calls1programstoppedCPU ▼ kernel moderax257rdi-100rsi&pathrdx0x800000read1write3close231exit_group257openat0→ terminal1→ terminal2→ terminalmode rw-r--r-- (644)hello\nopenat(AT_FDCWD, "notes.txt", O_RDONLY|O_CLOEXEC ▌
Toy: Linux x86-64 · python3 + strace 6.8 lines · Python's start-up calls not drawn · pid is a stand-in
STAGE 1 / 6
MATH · STAGE 1HOVER A SYMBOL OR A SHAPE
handler=table[rax]\sy{handler}{param}{\text{handler}} = \sy{table}{param}{\text{table}}[\sy{rax}{param}{\text{rax}}]
table[257]=openat\text{table}[257] = \texttt{openat}

The kernel uses the number in rax to pick the handler.

Common mix-ups

MYTHstrace shows every line that the program runs.

The fix, and 1 more mix-up with the diagram to show them, come with Pro.

Predict, then reveal
3 CHECKS · PRO
Q01With a read size of 4 bytes, how many read calls does the program make?

Make your guess, then test it in the diagram. All 3 checks come with Pro.

PRO LESSON · YOU WATCHED STAGE 1 OF 6
Unlock the full explainer
  • 5 more stages and 2 live controls
  • 2 mix-up cards and 3 predict-then-reveal checks
  • Watch it as a narrated video, in three voices
  • Every Pro lesson and capstone · 656 in total

$99 a year, or $15 a month. Cancel any time.