Monday, March 5, 2012

QEMU: Debugging your program

Lesson Number 2:
=============
This lesson assumes that reader is done with first lesson and has compiled and run HelloWorld application on QEMU's ARM emulation.

Debugging your program:
=================

To debug your program, you first need to build your program with debug information. For this gcc has -g switch. So your HelloWorld program needs rebuilding with -g compiler switch. Complete command will be:

arm-none-eabi-gcc -g3 -o HelloWorldARM HelloWorld.c -T {TOOLCHAIN_INSTALLATION}/arm-none-eabi/lib/generic-hosted.ld 


Running Program in QEMU with GDBStub:
===========================

Next step is to run this new HelloWorld ELF file on QEMU but we need to enable GDB stub in QEMU and we need to tell it to wait for GDB connection before execution of program. For this QEMU provides two switches
  • -s : QEMU will listen on port localhost:1234 for a connection by GDB
  • -S : QEMU will start as if you set a breakpoint at time zero, and you will need to use the GDB command "continue" to actually begin the simulation.
so command to launch QEMU will be:

 ./arm-softmmu/qemu-system-arm -kernel {PATH_OF_ YOUR_HELLOWORLD_ELF}-semihosting -nographic -s -S




Debug Session with Command-line GDB:
==========================

1) Go to your Sourcery toolchain for arm-none-eabi installation directory. It will be [Toolchain_installation_directory]/bin
2) Start GDB debug session using these commands:

  • ./arm-none-eabi-gdb [PATH_OF_YOUR_PROGRAM_WITH_DEBUG_INFO]
  • target remote localhost:1234
  • b main
  • c 
First command will start gdb and it will print details of GDB and a message of symbol loading.
Second command will ask GDB to connect to GDB server listening on port 1234 of localhost.
Third command will place a breakpoint at main function.
Fourth command will resume (continue) the debug session and it will then hit the breakpoint at main function.
You can then step through the code using "n" i.e. step over or "s" i.e. step into commands at GDB terminal.

Example Command line Debugsession:
========================

taimoor@pkl-tmirza-ubuntu:~/$ ./arm-none-eabi-gdb /home/taimoor/Lab_Preparation/Qemu_Stuff/Example_Programs/helloDashT

GNU gdb (Sourcery CodeBench 2011.09-60) 7.2.50.20100908-cvs
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=arm-none-eabi".
For bug reporting instructions, please see:
<https://support.codesourcery.com/GNUToolchain/>...
Reading symbols from /home/taimoor/Personal/@UCP/Lab_Preparation/Qemu_Stuff/Example_Programs/helloDashT...done.
GDB can't read core files on this machine.

(gdb) target remote localhost:1234
Remote debugging using localhost:1234
__cs3_reset_generic () at /opt/codesourcery/arm-none-eabi/lib/src/generic-reset.S:34
34        b    __cs3_start_asm_sim

(gdb) b main
Breakpoint 1 at 0x274: file HelloWorld.c, line 5.

(gdb) c
Continuing.

Breakpoint 1, main () at HelloWorld.c:5
5        int i=0;
(gdb)


Debug Session with CDT/Cross GDB:
=========================

1) Start CDT
2) Go to Run->Debug Configurations...
3) Create a new debug configuration of type "C/C++ Remote Application"
4) Click Browse Button adjacent to C/C++ Application box and select your HelloWorld Application.
5) No need to specify any project.
6) Click New button next to Connection combo box and create a Local connection. Give any name to connection e.g. I used "Test" and Finish that dialog.

Your configuration should look like this:



7) Go to Debugger tab of your configuration.
8) Click Browse button next to GDB Debugger box and select "arm-none-eabi-gdb" from your toolchain's bin directory.
9) Click Apply to save your configuration and then click Debug button.
10) Your debug session will start and breakpoint in main will get hit.
11) You can step through the code using F6.

Example CDT Debug session:
===================


I modified my HelloWorld program and added a loop in it.

Enjoy Cross Debugging.

QEMU Step by Step

Lesson Number 1:
============


How to install Qemu:
=============

1) Go to http://wiki.qemu.org/Download and install qemu source code. You can use wget http://wiki.qemu.org/download/qemu-1.0.1.tar.gz

2) Unzip downloaded QEMU source via UI or using following command:
tar -xvzf qemu-1.0.1.tar.gz

3) Goto directory where you unzipped QEMU sources and configure your QEMU using following commands:

  • ./configure 
    • If you do not give any target list to ./configure, it will configure qemu for all supported targets. But you can configure QEMU for a particular target using --target-list=[LIST_OF_Targets].
    • You can obtain list of supported targets by running ./configure without any parameter and then scrolling up and see "target list"
    • For x86 run this command as ./configure i386-softmmu
  • make
    • This will build QEMU for configured list of targets.
  • make install
    • I don't prefer installation and normally does not use this command. 

Getting the Toolchain:
==============

The best toolchain available is CodeSourcery's. You need to go to https://sourcery.mentor.com/GNUToolchain/ and install whatever toolchain you need.

I installed arm-none-eabi to use it for baremetal application development.

Writing a Hello World Program:
====================

1) Open any editor and type simple helloworld program. On Ubuntu you can use cat aswell:

$ cat > HelloWorld.c
#include <stdio.h>

int main()
{

     printf("Hello from ARM processor"\n");
     return 0;
}

2) Goto your toolchain installation directory and build your application using following command:

arm-none-eabi-gcc -o HelloWorldARM HelloWorld.c -T {TOOLCHAIN_INSTALLATION}/arm-none-eabi/lib/generic-hosted.ld

3) This will build your HelloWorld program and generate an ELF.

4) You can check the type of generated built file using following command:
  • file ./HelloWorldARM
This will give following:
./HelloWorldARM: ELF 32-bit LSB executable, ARM, version 1 (SYSV), statically linked, not stripped

Running program QEMU's ARM CPU Emulator:
=================================

 1) Goto your QEMU directory and run following command:

  • ./arm-linux-user/qemu-arm {PATH_OF _YOUR_HELLOWORLD_ELF}

Running program in QEMU's Machine Emulator:
===============================

1) You can also run your HelloWorld program using QEMU's ARM machine emulation.
  • ./arm-softmmu/qemu-system-arm -kernel {PATH_OF _YOUR_HELLOWORLD_ELF}-semihosting -nographic

Ubuntu Login screen not appearing

Had a hard time today when rebooted my Ubuntu machine after more than 3 months and its login screen stopped appearing. Only pinkish background screen was coming.

After some investigations, found out that problem was because of zlib which I installed for Qemu building.
I had installed zlib from source so I had to do following steps:

1) Switched to console using Alt+Ctrl+F2 and logged in text mode
2) Went to directory where zlib was installed (cd zlib/installed/directory)
3) make uninstall
4) Rebooted my PC.

WOW....

Thursday, November 10, 2011

Playing with Pidgin

Automatically Joining channels:

On the buddy list, select Buddies->Add Chat. Use this dialog to add the chat to your buddy list. Once the chat appears in your list, right-click on it and select Auto-Join. The channel will auto-join whenever you sign onto your IRC account.

Setting Real Name:

Go to the Accounts menu. Select your IRC account and click Edit Account. Click the Advanced tab. Enter a name in the Real Name field.

Monday, October 17, 2011

Purchasing a new CNG cylinder


1) There are many brands of cylinders available in market. OGRA (Oil and Gas Regulatory Authority) of Pakistan has list of approved CNG cylinders at http://www.ogra.org.pk/images/data/downloads/1275560046.pdf. Among these cylinders, all Italian cylinders are considered better than others. But I could not find any new Italian cylinder in the market. Faber, which is regarded as best, comes with all company fitted CNG Suzuki cars. It is also light in weight than all other cylinders.

2) I read at http://tribune.com.pk/story/239492/ogra-bans-cng-cylinder-import-from-12-suppliers/ that OGRA has banned import of Faber and EKC cylinders. I sent a query at Pakwheels to know about ban on
EKC as almost all CNG workshops are selling it.
Here is link to my post http://www.pakwheels.com/forums/cng/176639-ekc-dubai-banned-ogra. I also tried calling OGRA representatives but could not find any information from them. I even called at their Islamabad office and sent an email to their chairman and his team but none of them bothered to reply.

3) Finally, I got below reply from OGRA regarding the email I sent to all their officials.


4) The capacity of CNG cylinder is measured in water liters. Cylinder installed my car was Delamine (Italian brand) with capacity of 28.5L. Its CNG capacity was 4.5kg CNG. I planned to change it with 50L cylinder that can contain 8kg CNG.

5) I tested my car's cylinder from HDIP Lahore and put an add to sell it on back of my car. I also placed an add on OLX which I think is a very good place to sell your stuff. But I did not have much time so I decided to sell it to some dealer.

6) I got list of CNG centers in Lahore from http://www.pakcng.com/CNGdealers.asp?City=Lahore. I called them one by one but all of them asked me to visit their shop. So I decided to visit Bhawalpur road (road of Miani grave yard).

7) I also visited this workshop as it was recommended by a pak wheeler http://www.pakwheels.com/forums/cng/173518-best-workshop-cng-lahore-johar-town. I got the best rate from Bahawalpur road dealers. My plan was to go to Noor CNG a.k.a Butt CNG but he refused to purchase second hand cylinder. So I went to Gasline, had some negotiations with him and finally purchased new 50L E.K.C in 9k with exchange of my current cylinder.

Purchasing a new Suzuki car

I purchased new Suzuki Mehran 2011 for my Aunt last month. Below are the things I learned during this experience.
  1.    Get a list of Suzuki’s authorized dealers from PAK Suzuki’s website http://www.paksuzuki.com.pk/Automobile/Pages/DealersNew.aspx.
  2.  If you are planning to purchase car with own, do not contact just one dealer. Contact all of them because own is a variable amount and it totally depends on your negotiations with dealer.
  3. Price of car = Base price of car + freight charges + Own
  4. Call any of these dealers, negotiate with him and ask him to confirm car for you. After this you can visit him any time and pay token money after looking at the car. They don’t allow to take test drive of car ( I don’t know why). According to them test drive is allowed only for newly launched brands.
  5. Regarding payment, you need to make pay order of base price against name of Pak Suzuki pvt. Ltd.
  6. While taking the vehicle, do check everything including following:
a.       Front lights (low, high and dippers)
b.      Indicators (right, left and hazard light)
c.       Horn
d.       Wipers (water thrower)
e.      AC
f.        Petrol to CNG conversion
g.       Tools
h.      All doors are closing properly and locks are also working
i.     Keep the engine running for some time and check the temperature needle.