4. gdb and valgrind examples

If valgrind is not installed: sudo apt install -y valgrind
$ cat p1-null.c
int main( void)
{
  int *x = 0;

  return *x;
}

$ gcc -g -o p1-null p1-null.c

$ ./p1-null
Segmentation fault (core dumped)

$ gdb -q ./p1-null
Reading symbols from ./p1-null...done.
(gdb) run
Starting program: /home/perry/src/OSTEP/RP/14-vm-api/p1-null

Program received signal SIGSEGV, Segmentation fault.
0x000055555555460a in main () at p1-null.c:5
5         return *x;
(gdb) print x
$1 = (int *) 0x0
(gdb) quit
$ valgrind --leak-check=yes ./p1-null
==28028== Memcheck, a memory error detector
==28028== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==28028== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==28028== Command: ./p1-null
==28028== Invalid read of size 4
==28028==    at 0x10860A: main (p1-null.c:5)
==28028==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
==28028== Process terminating with default action of signal 11 (SIGSEGV)
==28028==  Access not within mapped region at address 0x0
==28028==    at 0x10860A: main (p1-null.c:5)
...