Debugging/Single Stepping GCC (GNU C Compiler) with Eclipse

Debugging/Single Stepping GCC (GNU C Compiler) with Eclipse

I am doing the below steps in a Lubuntu running on VirtualBox. I usually do messy things on a virtual machine as it does not disturb my actual OS.

The GCC source code can be downloaded from the official page : http://ftpmirror.gnu.org/gcc/gcc-5.3.0/gcc-5.3.0.tar.bz2

Change directory to where the sources were downloaded. Extract the sources by running the following command in the terminal:

tar jxf gcc-5.3.0.tar.bz2

Install the prerequisites that are required for the gcc build.

cd gcc-5.3.0
./contrib/download_prerequisites

Some other prerequisites might be required to be installed.

sudo apt-get install build-essential gawk m4 gcc-multilib

Create an “objdir” outside the extracted GCC sources and change directory to it.

cd ..
mkdir objdir
cd objdir

Run the following commands to build the GCC (prefix is set so that it does not affect the already installed gcc version) :

$PWD/../gcc-5.3.0/configure --prefix=$HOME/gcc-5.3.0 --enable-languages=c --disable-multilib
make STAGE1_CXXFLAGS="-g -O0" all-stage1
sudo make install

If required install Eclipse with CDT with the following command:

sudo apt-get install eclipse eclipse-cdt eclipse-jdt

Create a new Eclipse project with “Makefile project with existing code”.

MakeFileWithExistingCode

Import the sources :

import

Open the “Debug” perspective:

Perspective

Create a “hello.c” file in your home folder which has to be compiled by the GCC which we have built.
/* Hello World program */
#include <stdio.h>
main()
{
printf(“Hello World”);
}

Create a .gdbinit file in the home folder with the following contents:

set follow-fork-mode child

Create the debug configuration as follows.

DebugConfig_1DebugConfig_2

Select the options related to “Non-Stop mode”, Force thread list..” and “Automatically debug forked…”DebugConfig_3

Begin the debug session by clicking on the bug symbol at the top. The execution stops at the main function of the gcc driver.

debug_1

Put a breakpoint in the actual c compiler which the gcc driver calls. The file is at gcc-5.3.0/gcc/main.c.

debug_2

Run the code further and it will hit the breakpoint that was set in the c compiler.  Here you are debugging/single stepping the gnu c compiler.

Mountain Lion OSX : Features

Mountain Lion OSX : Features

In this post I mention some of the features that I really like in the Mountain Lion OSX. I use a MacBook Pro from early 2011.Hence most of the stuff I mention here are trackpad relevant.

For the ones who are used to other operating systems there is a tendency to use the OSX as windows or Linux or any other OS.The below mentioned features make you use the OSX as a OSX.Some features are existing in the previous versions of OSX and some are new in the Mountain Lion OSX. The list is not exhaustive , I will keep adding new stuff regularly.

Launchpad :

Pinch the trackpad with the thumb and the other 3 fingers.The Launchpad comes up.Here you will find all the programs. You can do a two finger swipe to move between different pages of the Launchpad.

Mission Control:

You can have a birds-eye view of all the open windows by doing a 3 finger swipe upwards. If the three finger swipe does not work then check for the “System Preferences -> Trackpad -> More Gestures -> Mission Control”. The mission control can also be accessed using the key F3.

Scrolling:

The Mountain Lion OSX is as close as it gets to an IPhone experience on a Mac. And with every new version of OSX the gap will be smaller than the previous.So if you had used a previous version of the OSX the default scroll direction in the Mountain Lion OSX will not be natural. But if you have been a Iphone user then the default scroll direction would feel natural. Anyways you can change the scroll direction by going to “System Preferences -> Trackpad -> Scroll & Zoom -> Scroll Direction: Natural”. Unchecking this box will make you go back to the scroll direction of the previous OSXs.

Fullscreen for programs:

Its always pleasant to have the program content on the full screen rather than share space with the tool bar,address bar,Menu Bar. To make a full screen click on the opposite pointing arrows at the top right corner of the program window.To exit the full screen move the mouse pointer to the top of the screen and click on the inward pointing arrows.If you have multiple fullscreen programs open then you can browse through them by doing a three finger swipe on the trackpad.If the three finger swipe does not work then check for the “System Preferences -> Trackpad -> More Gestures -> Swipe between fullscreen apps”.

Window Resizing:

In Mountain Lion now you can resize the windows by dragging any of its edges. This was not the case in the previous OSXs where the resizing was only possible by dragging the lower right corner of the window.

Quick Look:

You can have a quick look of any files by selecting the file and then hitting the space key. This gives a quick preview of the selected file.

Finder background:

Although of only aesthetic relevance you can change the background of the finder window when in icon view. First select the icon view. Then go to “view -> show view options”. Here you get an option to select a background. You can make this to be default  for your entire Mac by clicking the “Use as default ” button.

Select files or folders in Finder using keyboard:

In the Finder window you can directly select a folder or a file by typing its name.

Spring loaded folders:

There is a easy way of moving files between folders. You just drag and drop into the folders. The spring loaded feature helps in moving files between nested folders. You just need to drag the file onto a folder and wait until it opens the inner folder and do the same until you get to the required folder.

Do and Undo delete :

To delete a file a folder the keyboard shortcut is Command + Delete. To undo a delete the keyboard shortcut is Command + z. The Mac remembers 4 levels of deletes. Hence Command + z can undo 4 previous deletes.

search your mac using spotlight :

A complete blog page can be dedicated to the spotlight feature of the OSX. But I will mention only certain favourites of mine.

First get the spotlight field active by using the keyboard shortcut Command + Space.

The spotlight can act as a calculator. just type for example 5 + 2. You will see the answer appear next to the calculator.

To search for files and file contents with exact phrases surround the phrase words with double quotes. For example type “Comfortably numb” and the spotlight will list all the entries having this exact phrase.

To search in file with a particular type use the keyword kind. For example to search the the word hello in pdf file type

hello kind:pdf

The spotlight also accepts boolean searches. To search for files having the words Hello and How. Type

Hello AND How.

Arch Linux and Mountain Lion:VirtualBox Part 3

Arch Linux and Mountain Lion:VirtualBox Part 3

Install Arch Linux on the virtual hard drive:

The steps below are the ones I followed by referring the beginners guide.

1)After the 12 steps mention in my other post,you will be presented with a shell prompt. I have an US keyboard hence I will skip the part which is required for other language keyboards.For other keyboards refer the part “Change the language” in the beginner’s guide.

2)I had an internet connection by default hence did not need to configure the same.To find out if there is an internet connection run the command:

 ping -c 3 www.google.com

If you don’t get the below error then you are doing fine else check the beginner’s guide.

ping: unknown host

3)Now we will partition and format the virtual hard drive.We will create 3 partitions Root,Home and BIOS.Run the following command

cgdisk /dev/sda

4)Now we are presented with a table which has no partition entries.

Create the Root partition first.

Choose New (or press N)
Press Enter for the first sector (2048)
Type in 10G 
Press Enter again for the default hex code (8300)
Press Enter again for a blank partition name.
This creates a new Root partition.

Now to create the home partition:
Press the down arrow a couple of times to move to the larger free space area.

Choose New (or press N)
Press Enter for the first sector
Type in the remaining 10G
Press Enter for the default hex code (8300)
Press Enter for a blank partition name.
This creates our home partition.

Now to create the BIOS partition:
There should be third entry in the table with 1007.0 KiB size.Press the up or down arrow and select this entry.

Choose New (or press N)
Press Enter for the first sector.
Press Enter again for selecting the remaining size.
Type EF02 for the partition type
Press Enter for a blank partition name.
This creates our BIOS partition.

5)Create a file system.Run the following commands.

mkfs.ext4 /dev/sda1
mkfs.ext4 /dev/sda2

6)Mount the partitions:Run the following commands.

mount /dev/sda1 /mnt
mkdir /mnt/home
mount /dev/sda2 /mnt/home

7)Install the base system.Run the following commands.

pacstrap -i /mnt base

8)Generate a FSTAB (File System Table).Run the following commands.

genfstab -U -p /mnt >> /mnt/etc/fstab
nano /mnt/etc/fstab

Press Control X followed by Y and enter to save the file and exit nano (The default command line editor).
9)Chroot(Change the root directory) and configure the base system.Run the following commands.

arch-chroot /mnt

10)Setting the Locale.
Run the following command:

nano /etc/locale.gen

Uncomment (Remove the # at the beginning of the line) the lines that are relevant.I only needed en_US.UTF-8 UTF-8.Press Control X followed by Y and enter to save the file and exit nano.
Run the following command:

locale-gen

Run the following command:

echo LANG=en_US.UTF-8 > /etc/locale.conf
export LANG=en_US.UTF-8

If you has set a different keyboard than the US layout then refer the “Console font and keymap” section in the beginner’s guide.

11)Set the Time Zone.
For Berlin, Germany I ran the following command:

ln -s /usr/share/zoneinfo/Europe/Berlin /etc/localtime

To find the allowed Zones run the following commands:

ls /usr/share/zoneinfo/

To get a subzone for a zone.Here Europe is taken as an example.

ls /usr/share/zoneinfo/Europe

12)Set the hardware clock to UTC:

hwclock --systohc --utc

13)Set the host name:I set it to oopra.

echo oopra > /etc/hostname

14)Configure the wired network:

systemctl enable dhcpcd.service

15)Set the root password and remember it.

passwd

16)Install Bootloader: We will install Grub(Grand Unified Bootloader) for our Arch Linux:

pacman -S grub
grub-install --recheck /dev/sda
grub-mkconfig -o /boot/grub/grub.cfg

17)Now the Arch Linux is installed in the Virtual hard drive.Shutdown the Linux system.
Run the command:

shutdown

18)Now in the virtualBox window select the “Settings tab” uncheck the CD/DVD ROM.

Screen Shot 2013-07-20 at 11.05.53 AM

19)Start the Arch Linux virtual machine again.
Now we are booting the Arch Linux that is installed in the virtual hard drive.
Again it presents us a shell prompt.Now its time to create another user.Until now we were running as a root user.But for our day-to-day work another user name has to be created.I created a new user oopra.

useradd -m -g users -s /bin/bash oopra

Add some for information about the user. In my case the user is oopra:

chfn oopra

Choose a new password for the new user.oopra in my case:

passwd oopra

20)Now the time has finally arrived to create a Graphical User Interface.
Run the following commands for installing the X window system:

pacman -S xorg-server xorg-server-utils xorg-xinit
pacman -S mesa

21)Install Guest additions:

pacman -S virtualbox-guest-utils
modprobe -a vboxguest vboxsf vboxvideo
nano /etc/modules-load.d/virtualbox.conf

Add the following lines in virtualbox.conf

vboxguest
vboxsf
vboxvideo

Press Control X followed by Y and enter to save the file and exit nano.

22) Test X Window System:
Install the default environment:

pacman -S xorg-twm xorg-xclock xterm

Remove the default xinitrc file.

rm ~/.xinitrc

23)Install SLIM for starting a desktop manager:

pacman -S slim

Enable SLIM:

systemctl enable slim.service

Create a .xinitrc file:

cp /etc/skel/.xinitrc ~/.xinitrc
nano ~/.xinitrc

The .xinitrc should look like:

#!/bin/sh

if [ -d /etc/X11/xinit/xinitrc.d ]; then
  for f in /etc/X11/xinit/xinitrc.d/*; do
    [ -x "$f" ] && . "$f"
  done
  unset f
fi
exec startxfce4

Press Control X followed by Y and enter to save the file and exit nano.

The .xinitrc file should be created for all users. The above step only creates the .xinitrc file for the root. For the other user I created (oopra in my case) the same file has to be created.

su oopra

Create a .xinitrc file:

cp /etc/skel/.xinitrc ~/.xinitrc
nano ~/.xinitrc

The .xinitrc should look like:

#!/bin/sh

if [ -d /etc/X11/xinit/xinitrc.d ]; then
  for f in /etc/X11/xinit/xinitrc.d/*; do
    [ -x "$f" ] && . "$f"
  done
  unset f
fi
exec startxfce4

Press Control X followed by Y and enter to save the file and exit nano.

 

Do not forget to exit.

exit

24)Install the desktop manager xfce4.

pacman -S xfce4

25)Now you have a basic Arch Linux with GUI support.Time to enable the sound :
Install Alsa running the following commands:

pacman -S alsa-utils

Unmute running the following command:

amixer sset Master unmute

Initialise Alsa control in Virtual Box running the following command:

alsactl init

26) Install flash plugins:

pacman -S flashplugin

27)Now you have a basic Arch Linux with GUI support.Now reboot using the following command:

reboot

Once it presents the login window. Login with the username and password that was created before. You can add anything more you want but running the

 pacman -S PackageNameYouWant

command.

Arch Linux and Mountain Lion:Why (Arch) Linux Part 1

Arch Linux and Mountain Lion:Why (Arch) Linux Part 1

My first choice for an OS is GNU/Linux. But the performance of the MacBook pro is irresistible. So I went ahead and bought a Mac.The question that will I be able to run GNU/Linux on Mac was on the back of my mind.As with other things I thought I will figure it out someway or another.

The MacBook Pro is an amazing machine.As the OS X is specifically designed for the hardware it runs on.It makes the OS X fast,efficient and pleasant for the user.The battery and the display are the other things that make the MacBook Pro a good buy.

All said I was still missing GNU/Linux.The mere possibility that I could configure it to exactly my requirements makes it a favourite.Added to this for me it is the best environment for software development.Hence not having GNU/Linux is not an option.And buying another machine was not an option either.After spending nearly 65,000 INR on the MacBook Pro,I was almost broke :-).

So I set out to install GNU/Linux on my Mac Book Pro.The first thing that came to my mind was to dual-boot with Linux Mint.The Mac boot loader Boot Camp is known to support dual-boot with Linux.But it was too restrictive for me.Also Apple does not officially state that it supports dual-boot with Linux.Hence the next step was to use rEFIt.But it was never smooth for me.Hence I uninstalled rEFIt.I also had problems upgrading the OS X to Mountain Lion after doing this.Although I am not certain my suspicions are towards the rEFIt.So I decided not to mess with the boot of the Mac.

So the other option is to use Linux on a Virtual Machine.Below are the steps I followed to install Arch Linux.I choose Arch Linux as it is the most customisable distro I know and the documentation is excellent.I had tried ubuntu as a virtual machine but it made my Mac horribly slow.You can refer the Wiki of the Arch Linux or follow the below steps as I did.Mine is a MacBook Pro 13-inch,Early 2011,connected to the internet with the ethernet cable.

Check my other post for installation of  VirtualBox on Mac and running Arch Linux as guest.

Mountain Lion:VirtualBox Arch Linux ISO image w/o burning CD/DVD Part 2

Mountain Lion:VirtualBox Arch Linux ISO image w/o burning CD/DVD Part 2

Install VirtualBox and Boot Arch Linux from the iso image:

1) Download VirtualBox disk image from here or the official link.Also download the Arch Linux ISO image for here.

2) Install VirtualBox by double clicking on the disk image and following window is displayed.Click on the VirtualBox.pkg and follow the instructions that are displayed. (For windows 8 users just click on the downloaded exe file and follow the below instructions)Image

3)Once installed, start the VirtualBox application from the “Applications folder” and click on the “New” button at the top left corner.

4) Give any name for your virtual machine.Select the type as “Linux” and the version as “Arch Linux”.

5) Set the RAM size to 1024MB.I found it to be of the right size so that the Mac OS X (or windows 8) can have the remaining 3 GB for itself.If you increase it above 1024 MB then the Mac OS X performance decreases considerably.

6)Select “Create a virtual Hard Drive now”,”VDI” hard drive type,Fixed Size of minimum 20GB in the next windows.

7)Now in the main window there should be an entry of the virtual machine that was created with the above steps.

Screen Shot 2013-07-20 at 12.10.58 PM

8)Select the newly created virtual machine and click on the settings button.

9)In the new window select the system tab.Rearrange the boot order so that the CD/DVD ROM is before the Hard Disk and check both of them. Let the chipset be “PIIX3” and in the extended features check “Hardware clock in UTC time” and “Enable absolute pointing device”.

Image

10)Next select the storage tab.Here you should find the already created VDI.Now right-click outside the VDI entry and add a “IDE controller”.After that right click on the newly created IDE controller and select “add CD/DVD device”.Here in the new window select “choose disk” and select the Arch Linux ISO image that was downloaded before.

Image

11) Now start the virtual machine and it should boot the Arch Linux from the ISO image.The Arch Linux presents a shell prompt instead of a GUI.This is because Arch Linux is very minimal and it allows you to choose and install the desktop manager you prefer.

12)The next step is to install the Arch Linux on the VDI virtual hard drive that was created.Check my other post here for the installation.