Makefile and debugging

Lab  – Makefile and debugging

 

  1. Create a simple hello application
  2. Write a Makefile to build it
CC=gcc
CFLAGS=-g
LDFLAGS=-lpthread

.PHONY: clean all

all: test

x.o: x.c
            $(CC) $(CFLAGS) -c x.c

test: x.o
            $(CC) $(LDFLAGS) x.o -o test

clean:
            @rm -f *.o *~ test
  1. Change the debug level
  2. Change the compiler to arm compiler
  3. Add install target to the file and run make install to copy executable to root file system
  4. Add another file to your project with a simple add function and update the makefile
  5. Run the program on target using gdbserver
# gdbserver    0.0.0.0:8000      ./test
  1. Run gdb debugger on host and load the executable
# arm-none-linux-gnueabi-gdb    ./test
  1. Connect to target and debug the code
(gdb) target remote 192.168.24.30:8000
(gdb) b main
(gdb) c
(gdb) n
(gdb) info registers
(gdb) print i
(gdb) n
(gdb) n
(gdb) display i
(gdb) n
(gdb) n
(gdb) x/20w $sp
(gdb) c
  1. Strip the code and run it again on target
# arm-none-linux-gnueabi-strip –d ./test
  1. Change the code to access a wrong address
  2. Run the code to get an error
  3. Create a core dump
# ulimit -c unlimited
  1. Run the gdb debugger using the core dump and examine the error
  2. Write a fault handler to catch common errors and print useful information