Outside a debugger this program just says Segmentation fault. Inside GDB, a crash is a stop: the program is frozen at the moment it went wrong.
Reading a crash: signal, backtrace and frames
Level 12 of 13 · interactive GDB exercise · runs in your browser
When a program dies, use the signal, the backtrace and frame selection to find the line of your own code that caused it and the data it was working on.
No breakpoint needed. Type run and GDB catches it. Then backtrace shows how the program got there.
The crash is often inside a library function. up walks out to your code, where the bad value came from.
Run the program until it crashes, then find out which user's record caused it.
You work in a real GDB session on the program below. GDB has loaded the program. Nothing is running yet.
#include <stdio.h>
#include <string.h>
struct user {
const char *name;
const char *email;
};
struct user users[3] = {
{"ada", "[email protected]"},
{"bob", NULL},
{"cy", "[email protected]"},
};
int email_length(const struct user *u) {
return (int)strlen(u->email);
}
int main(void) {
int total = 0;
for (int i = 0; i < 3; i++) {
total += email_length(&users[i]);
}
printf("total = %d\n", total);
return 0;
}
main.c, compiled with gcc -g -O0 -std=c11 -Wall -fno-omit-frame-pointer -fno-stack-protector -no-pie -fno-pie for 32-bit x86 Linux.
runbacktraceframe <n>, up, downprint <expr>info argsinfo localsx/s <addr>print $_siginfo._sifields._sigfault.si_addrprint u right after the crash and getting No symbol "u" in current context. You are in strlen's frame. Select email_length with up first.continue after the crash. The signal is delivered and the program dies. Type run to crash it again and inspect it this time.