Debug an Application#

  1. Create a new source C file and paste the following code in it. The code should print another message, but due to a bug, it doesn’t.

    #include <stdio.h>
    
    int len(char* s) {
      int l = 0;
      while (*s) s++;
      return l;
    }
    
    int rot13(int l) {
      if (l >= 'A' && l <= 'Z') l = (l - 'A' + 13) % 26 + 'A';
      if (l >= 'a' && l <= 'z') l = (l - 'a' + 13) % 26 + 'a';
      return l;
    }
    
    char* msg = "Jryy Qbar!!!\n";
    
    int main() {
      int i = 0;
      printf("The secret message is: ");
      while (i < len(msg)) printf("%c", rot13(msg[i++]));
    
      return 0;
    }
    
  2. Create a Makefile and add the following targets:

    • release: Compiles the program normally, e.g. gcc -Wall -o program program.c.

    • debug: Compiles the program with -g option (which includes debug symbols in the binary).

    Build and run the program with:

    make debug
    ./program
    
  3. You will notice that the output is not correct. Use gdb to locate the bug in the code. Specifically, we are asking you to not simply perform printf debugging where you insert code and recompile. Once you have the correct code, tar your sources and Makefile and upload it to canvas.

  4. In your report, provide 1)how you changed the code, 2)Makefile, 3)the message that should have been displayed and 4)how you found the bug.

  5. How does GDB know where functions and data are located in the executable when you are debugging? (3 lines max.)

    Possibly useful:

    • run objdump on an executable you compiled. Run objdump -help to see what options it offers. Experiment with the options to see what information you can get it to display.

    • gccintro