Monday, November 29, 2010

Playing with GDB: Installing GDB and debugging remote application on PPC target

Building Application for PPC Target:

I used MPC8377 as target and built my application using Application Development Kit (ADK) for MPC 8377 that comes with Mentor Embedded Linux (MEL). 
You can use any other cross toolchain to build your application. Another easily available is the one provided by CodeSourcery.


On Host Machine:

If you have Linux(Ubuntu) machine then you might already  GDB for native (x86) application debugging. But for cross debugging for any non-x86 architecture, you need to get GDB source, configure it for that particular architecture and install it by following below instructions:

  • Download latest source of GDB "gdb-7.2.tar.gz" from "http://ftp.gnu.org/gnu/gdb/".
  • Untar "gdb-7.2.tar.gz" file.
  • Open terminal and change directory to location where you extracted the "gdb-7.2.tar.gz" file.
  • Run "./configure --target=powerpc-linux".
  • Run "make".
  • Run "make install".
Now you have got GDB configured for PPC on your host machine.

On Target Side:

GDB is a remote serial protocol in which you need some application on target side that understands GDB protocol. In terms of GDB, this application is called Debugging stub. For PPC with embedded linux, we have gdbserver that provides a replacement of GDB stub.

  • Boot kernel on your PPC target
  • Note ip address assigned to target by dhcp (In case you did not assigned static ip using bootargs).
  • Copy your out file (elf file build with debug info) to target system. You can transfer files easily by mounting any local folder. You can also use scp for remote copy.
  • Run gdbserver on target by giving command "gdbserver:12345 outfilename". Here 12345 is port number used for communication and outfilename is name of .elf file.
Below are output messages
root@mpc8377e-rdb:~# gdbserver :12345 HelloWorld
Process HelloWorld created; pid = 1623
Listening on port 12345



To learn more about gdbserver read http://davis.lbl.gov/Manuals/GDB/gdb_17.html#SEC138

Now you have gdbserver running on your target platform. This gdbserver understands GDB protocol via TCP/IP.

Now come back to host machine and give following command:
powerpc-linux-gdb outfilename

In my case it was
taimoor@taimoor-ubuntu:~$ powerpc-linux-gdb /home/taimoor/TestApps8377/HelloWorld_8377/Debug/HelloWorld_8377
GNU gdb (GDB) 7.2
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "--host=i686-pc-linux-gnu --target=powerpc-linux".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/taimoor/HelloWorld_8377/Debug/HelloWorld_8377...done.
(gdb)

Now you have debugger running on your host machine.Now give the following command to connect to gdbserver running on target.

(gdb) target remote [target ip]:12345

Here targetip is IP of target platform. you can check targetip using ifconfig command on target's terminal. If you have given some static IP then use that one.

Now you can use gdb commands like "b main" to break in main function and then 'c' to continue.

Monday, November 8, 2010

Debugging Run time Exceptions

In Visual Studio, one can enable option to stop on any type of exception.

For example to debug any access violation exception, go to Debug->Exceptions and check the box for Win32 Exceptions->c0000005 Access violation as shown below:

Monday, November 1, 2010

Why C?

For me C is a language that gives you a lot of freedom. Using C, a programmer can access almost every device on his machine. You can even combine C and Assembly to get the best results.
C programmer vs other High level language ones is like a difference between a driver and engineer. Both can drive the car but engineer knows how every part of car is working whereas driver only knows how to use different parts to drive the car.

  • C can address and manipulate memory by direct address. A program can obtain  the memory address of any object (both data objects and functions) and manipulate without restriction the contents of the memory specified by the address. This capability is good to have because it allows flexibility. However, you have no protection from the program overwriting critical parts of the operating system when you are programming a PC using DOS.
  •  C has a powerful library of functions. This library of functions enables programmers to perform I/O, work with strings (which are arrays of characters), and perform many other tasks.
  • Speed of the resulting application. C source code can be optimized much more than higher-level languages because the language set is relatively small and very efficient (other reasons too, discussed later). It is about as close as you can get to programming in assembly language, without programming in assembly language (if you don't know what assembly is just Google it). Heck you can even use assembly and C together!

  • That leads to a second advantage that C has which is its application in Firmware programming (hardware). That is due to its ability to use/work with assembly and communicate directly with controllers, processors and other devices.

  • C is a building bock for many other currently known languages. Look up the history of C and you will find that it has been around for some time (as programming languages go anyway). Take a look at Python for example a fully Object-Oriented High-Level programming language. It is written in C (perhaps C++ too). That tells you if you ever want to know what is going on under the hood in other languages; understanding C and how it works is essential.

  • That leads to the final advantage that I will touch on (there are no doubt others and disadvantages as well); C is a compiled language versus an interpreted language. Explained simply, this means that the code is compacted into executable instruction (in the case of windows anyway) rather than being "translated" on the fly at run time. This feature also lends heavily to the speed of C programs.