CSE Roadmap

Core tier · about 3 hours · 7 parts

Part 4 of 7 · 2 min read

Finding a failure

Think of a delivery that did not arrive. The tracking number shows every stop, and the last stop shows where it went wrong. The analogy stops in one place: a parcel stays at the last stop, and a program continues after a failed call unless it checks the result.

In plain words: When a command fails with a vague message, trace only the calls that matter and look for -1. For file problems use -e trace=openat,stat,access or the group -e trace=file. For a process that cannot start, -e trace=execve. Follow children with -f, since a shell starts programs as separate processes.

A worked example. The command cat /nonexistent prints cat: /nonexistent: No such file or directory. The trace shows the exact call:

$ strace -e trace=openat cat /nonexistent
openat(AT_FDCWD, "/nonexistent", O_RDONLY) = -1 ENOENT (No such file or directory)
cat: /nonexistent: No such file or directory
+++ exited with 1 +++

The program called openat on the path as typed and received ENOENT, and then exited with status 1 (Level 1). This confirms the cause at the system-call level, and not only from the message. For the case of Level 10, page 7 (Permission denied on a file that looks readable), the trace would show = -1 EACCES, which sends the investigation to the mode bits and directories of the path, in place of the existence of the file.

With -f, a shell command line shows each child:

$ strace -f -e trace=execve sh -c 'ls >/dev/null'
execve("/usr/bin/sh", ["sh", "-c", "ls >/dev/null"], ...) = 0
[pid   933] execve("/usr/bin/ls", ["ls"], ...) = 0

The shell first starts itself, then a second process (933) starts ls. Without -f the second execve would be missed.

The key lines. Start with the error name, then find which path or argument was used. The wrong path, the wrong working directory and a missing permission are the three common causes.

errno

errno is the error number that a failed system call leaves for the program. strace prints it as a name (such as ENOENT) and its text, which makes the failure precise.

Example: = -1 EACCES (Permission denied) means the kernel refused the access.

Check yourself: 1. How do you trace only file-related calls? 2. Why is -f needed for a shell command line? 3. Which error name sends you to the permission checks?