Local Debugging

   


Let's start by compiling and debugging helloworld solely on the host system, tbdev1. The script buildrootfilesystem generates helloworld.c in /root/cross/builds and cross-compiles it for the target system. Let's use that source code file and compile it for the x86 processor using tbdev1. Here are the steps to install gdb and compile and debug helloworld.c for the x86 on tbdev1:

  1. Install gdb on tbdev1:

     root@tbdev1[508]: apt-get install gdb 
  2. Change into the /root/cross/builds directory and compile helloworld.c with the debugging flag -g:

     root@tbdev1[509]: cd /root/cross/builds root@tbdev1[510]: gcc -g -o helloworld-i386-linux helloworld.c 
  3. You need to check a couple things. Using the file program, you can determine the executable's architecture and see whether it contains debugging symbol tables:

     root@tbdev1[512]: file helloworld-i386-linux helloworld-i386-linux: ELF 32-bit LSB executable, Intel 80386, version 1, dynamically  graphics/ccc.giflinked (uses shared libs), not stripped 

    stripped means the executable contains no debugging symbol tables, and not stripped means the executable contains debugging symbol tables.

    As you can see, this compiled version of helloworld is for the Intel 80386, x86, and is not stripped. You can later use the strip program to remove debugging symbol tables and then use file to see that helloworld doesn't contain debugging symbol tables.

  4. Execute helloworld, to check for proper compilation:

     root@tbdev1[516]: ./helloworld-i386-linux Hello world 1 times! Hello world 2 times! Hello world 3 times! Hello world 4 times! Hello world 5 times! Hello world 6 times! Hello world 7 times! Hello world 8 times! Hello world 9 times! 
  5. Use gdb to debug helloworld:

     root@tbdev1[521]: gdb helloworld-i386-linux GNU gdb 19990928 Copyright 1998 Free Software Foundation, Inc. GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions. Type "show copying" to see the conditions. There is absolutely no warranty for GDB.  Type "show warranty" for details. This GDB was configured as "i686-pc-linux-gnu"... (gdb) 

    NOTE

    Don't forget about the oldest debugging tool, the print statement. C programs can use printf while kernel modules use printk. You can use printk while debugging kernel module code because kernel drivers can't link to the standard C library.

  6. Debug helloworld. List the helloworld-i386-linux executable source code by using the list command, and then run the helloworld-i386-linux executable:

     (gdb) list 1       #include <stdio.h> 2 3       int main(void) 4       { 5               int i; 6 7               for (i = 1; i < 10; i++) 8               { 9                       printf("Hello world %d times!\n",i); 10              } (gdb) run Starting program: /root/cross/builds/helloworld-i386-linux Hello world 1 times! Hello world 2 times! Hello world 3 times! Hello world 4 times! Hello world 5 times! Hello world 6 times! Hello world 7 times! Hello world 8 times! Hello world 9 times! Program exited with code 025. (gdb) 

    Without breakpoints, helloworld-i386-linux executes to completion while printing Hello world and variable i's value to the screen.

  7. Set a breakpoint at line 9, by using the break command:

     (gdb) break 9 Breakpoint 1 at 0x80483f8: file helloworld.c, line 9. (gdb) 
  8. Start helloworld-i386-linux again, by using run:

     (gdb) run Starting program: /root/cross/builds/helloworld-i386-linux Breakpoint 1, main () at helloworld.c:9 9                       printf("Hello world %d times!\n",i); (gdb) 

    This means you're about to execute line 9.

  9. Execute the line by using the next command:

     (gdb) next Hello world 1 times! 7               for (i = 1; i < 10; i++) (gdb) 

    "Hello world 1 times!" is the output from line 9's printf statement.

  10. Use continue to continue. The program should break again at line 9. Examine the value of i by using the print command:

     (gdb) continue Continuing. Breakpoint 1, main () at helloworld.c:9 9                       printf("Hello world %d times!\n",i); (gdb) print i $1 = 2 (gdb) next Hello world 2 times! 7               for (i = 1; i < 10; i++) (gdb) 

    As expected, i = 2 because this is the second time through the for loop. You could continue like this, examining i eight more times. Instead, change i to 8 to speed things along. Use the set variable command, and then use continue:

     (gdb) set variable i = 8 (gdb) continue Continuing. Hello world 8 times! Breakpoint 1, main () at helloworld.c:9 9                       printf("Hello world %d times!\n",i); (gdb) 

    Use continue again, and helloworld continues to execute and then exits:

     (gdb) continue Continuing. Hello world 9 times! Program exited with code 025. (gdb) 

    NOTE

    You can use a watchpoint to stop program execution whenever a variable value changes. Watchpoints slow program execution but really help to determine what's going on in a program. You can use the backtrace command to examine stack frames. backtrace shows a nested list of calling functions.

  11. You are done debugging helloworld-i386-linux, so use the quit command:

     (gdb) quit root@tbdev1[525]: 

In this section, you compiled helloworld with the debugging flag -g. Using the file program, you determined helloworld's architecture and found that it contains debugging symbol tables. Then you executed helloworld within gdb, listed its source code, ran helloworld, set a breakpoint, examined a variable value, and set a variable. You did all this on the Project Trailblazer development workstation, tbdev1. In the next section, you will learn how to remotely debug a cross-compiled version of helloworld on Project Trailblazer's ARM target board over the network.


       
    Top


    Embedded LinuxR. Hardware, Software, and Interfacing
    Embedded LinuxR. Hardware, Software, and Interfacing
    ISBN: N/A
    EAN: N/A
    Year: 2001
    Pages: 103

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net