My Tech notes: May 2008

Unix Documentation

Free Online Unix Training Materials

Lists many links to free Unix training materials.

Pointers and Arrays Materials

Pointers and Arrays materials Explained for C beginners

C FAQ and General Questions C Interview Questions

Powered By

Free XML Skins for Blogger

Powered by Blogger

Saturday, May 31, 2008

Kernel (for 2.6) debugging- kgdb

Kernel (for 2.6) debugging- kgdb &

kgdb is a kernel patch that, once applied, allows for the use of the familiar gdb interface for source-level debugging of a running Linux kernel.

The kgdb patch supplies the kernel with a debugging stub. This stub uses gdb remote serial/Ethernet protocol to communicate with gdb (host machine). This patch is applied to the kernel on the machine that will run the gdb session (host machine).

The gdbmod for module debugging, gdbmod version of gdb is required to debug modules that are loaded dynamically on to the testing machine.

Configuring kgdb

Download kgdb and gdbmod patch for your kernel version from

Copy the patch to the kernel source directory on the development machine, and apply,

  1. Unzip the kgdb patch
    mkdir ${BASE_DIR}/patch-kgdb
    cd ${BASE_DIR}/patch-kgdb
    tar -jxvf linux-2.6.7-kgdb-2.2.tar.bz2
  2. patch -p1 < ${BASE_DIR}/patch-kgdb/core-lite.patch
  3. patch -p1 < ${BASE_DIR}/patch-kgdb/i386-lite.patch
  4. patch -p1 < ${BASE_DIR}/patch-kgdb/8250.patch
  5. patch -p1 < ${BASE_DIR}/patch-kgdb/eth.patch
  6. patch -p1 < ${BASE_DIR}/patch-kgdb/i386.patch
  7. patch -p1 < ${BASE_DIR}/patch-kgdb/core.patch

This kernel will be for target board, the patch applied here are for i386, for ARM patch download latest patch from for kernel

Apply the patch for arm board as

  1. patch -p1 < ${BASE_DIR}/patch-kgdb/core-lite.patch
  2. patch -p1 < ${BASE_DIR}/patch-kgdb/arm-lite.patch
  3. patch -p1 < ${BASE_DIR}/patch-kgdb/8250.patch
  4. patch -p1 < ${BASE_DIR}/patch-kgdb/eth.patch
  5. patch -p1 < ${BASE_DIR}/patch-kgdb/core.patch

The patched kernel must now be recompiled.

        $ make menuconfig

From the main menu navigate to and select kernel hacking. You should now see an option for Remote (serial) debugging with gdb. Make sure this option is selected and then exit.

        $ make clean 
        $ make bzImage

The newly compiled kernel image (e.g., bzImage) is copied to the target machine for booting up.

Compiling the kernel on the development machine

  1. In the ${BASE_DIR}/linux-2.6.7/Makefile, set the EXTRAVERSION = -kgdb
  2. make mrproper
  3. make menuconfig
    Select the options appropriate for the target machine Hardware.
    Select the options pertaining to kgdb under "Kernel hacking" .
    Remote (serial) debugging with gdb
  4. make clean
  5. make bzImage
  6. Transfer the built kernel to the Target machine from the Development machine.

    Copy the Kernel image from ${BASE_DIR}/linux-2.6.7/arch/i386/boot/bzImage to the target machine as /boot/vmlinuz-2.6.7-kgdb
    Copy the Map file from ${BASE_DIR}/linux-2.6.7/ to the target machine as /boot/
    Also create links,
    ln -s /boot/vmlinuz-2.6.7-kgdb /boot/vmlinuz
    ln -s /boot/ /boot/

  1. Edit the /boot/grub/grub.conf file in the target machine to have the kgdb enabled kernel entry.
    Sample is shown below :-

    # Sample grub.conf which will by default boot the kgdb enabled kernel

    title RH
    root (hd0,0)
    kernel /boot/vmlinuz ro root=/dev/sda1
    initrd /boot/initrd

    title Linux-2.6.7-kgdb
    root (hd0,0)
    kernel /boot/vmlinuz-2.6.7-kgdb ro root=/dev/sda1 rootfstype=ext3 kgdbwait kgdb8250=0,115200
  2. Using kgdb ethernet interface

Add following line in the grub entry :

kgdboe=@,@ (that's kgdboe=@LOCAL-IP/,@REMOTE-IP/)

# Sample grub.conf which will by default boot the kgdb enabled kernel

title Linux-

root (hd0,0)

kernel /boot/vmlinuz- ro root=/dev/hda1 kgdboe=@,@


Then for starting the debug session give following command on gdb.

(gdb) ./vmlinux

(gdb) target remote udp:HOSTNAME:6443

Starting the debug session

  1. After booting the target machine will wait for the host development machine to connect, by displaying the message:-

    Waiting for connection from remote gdb...
  2. For version 2.6 this message will not appear, it will just wait after printing message "booting linux, uncompressing linux", can connect to this form host machine,
  3. cd ${BASE_DIR}/linux-2.6.7
  4. For setting a debug session with baud rate of 115200 on /dev/ttyS0 , run as "root" user:-

    gdb ./vmlinux
    GNU gdb Red Hat Linux (6.0post-0.20040223.17rh)
    Copyright 2004 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 "i386-redhat-linux-gnu"...Using host libthread_db library "/lib/tls/".

    (gdb) shell echo -e "\003" > /dev/ttyS0
    (gdb) set remotebaud 115200
    (gdb) target remote /dev/ttyS0
    Remote debugging using /dev/ttyS0
    breakpoint () at kernel/kgdb.c:1212
    1212 atomic_set(&kgdb_setting_breakpoint, 0);
    warning: shared library handler failed to enable breakpoint
  5. For further commands refer


Gdb quick ref guide.

Simple how to

Remote debugging

Wednesday, May 28, 2008

Kernel (for 2.6) debugging or module debugging

Kernel (for 2.6) debugging or module debugging

kgdb is a kernel patch that, once applied, allows for the use of the familiar gdb interface for source-level debugging of a running Linux kernel.

The kgdb patch supplies the kernel with a debugging stub. This stub uses gdb remote serial/Ethernet protocol to communicate with gdb (host machine). This patch is applied to the kernel on the machine that will run the gdb session (host machine).

The gdbmod for module debugging, gdbmod version of gdb is required to debug modules that are loaded dynamically on to the testing machine.

Download kgdb and gdbmod patch from:


Gdb quick ref guide.

Simple how to

Tuesday, May 27, 2008

c language links

Monday, May 26, 2008

Insight debugger making it work under windows for remote debugging- ARM target

Insight debugger another option

This tool looks promising as can run from windows environment.

Have more options than DDD, like setting up remote target through GUI.

Getting started

Compile Insight to run on windows environment, Insight can be compiled for windows or linux environment,

For compilation under windows environment one needs either cygwin or minGw. I have used cygwin. Packages needed to compile insight are

· gcc,

· binutils

· Insight

· NewLib

Download cygwin from

This downloads setup.exe to your system. Save it and run setup, it will ask for packages to download, select the appropriate tools or install default packages for now.

Note: While upgrading remove all reference to cygwin1.dll from cygwin directory and keep only latest one reference which will be available in x:\cygwin\bin, where x is dir in which cygwin is installed.

Check this link for installation details

There are two ways to get insight work for you on windows machine,

1. Do it by yourself approach

This requires downloading of insight and compile on cygwin,

Compile and build steps are

·         $insight 6.8/configure --exec-prefix=/home/Bhagwat/insight 6.8/build/insight --host=i686-pc-cygwin --target= arm-elf >configure.out 

This will generate makefile, now install insight to create insight.exe

·         $make –w all install LIBGCC2_DEBUG_FLAGS=”g1 o1” LANGUAGES=”c c++” >make.out

Note: These steps assume TCL/TK installed properly, as insight is TCL/TK based GUI.

2. Check for pre-compiled binary of insight

This option requires searching for correct version of insight, which is build with proper versions of other packages,

This can be downloaded from

GNUARM consists of pre-compiled binary distribution. This toolchain consists of the GNU binutils, compiler set (GCC) and Insight debugger. Newlib is used for the C library. The toolchain includes the C and C++ compilers.

Saturday, May 24, 2008


Friday, May 23, 2008

c++ text book and some good resources


Data Structures with C++ Using STL, 2nd edition
Ford and Topp, Prentice Hall

Accelerated C++
Koenig and Moo, Addison-Wesley

These books are recommended only. The course will follow primarily the order of the Ford and Topp book, which is available at the bookstore. Some of the lectures are based on material in Koenig and Moo. Students should have available a C++ reference book, such as those by Malik or Stroustrop, and use on-line reference material.

Web Resources

There are also two very good websites you should find helpful:
    The STL (standard template library) programmer's guide at SGI is a good resource for questions about vectors, lists, maps, etc.

    This is an organized summary of the most important FAQ's that have appeared on the C++ newsgroups. Both beginning and advanced students can benefit from it.

Feel free to search for and share other web resources.

Drop in Tutoring

In addition to the TA Office Hours, you may also want to check out:

Advising & Learning Assistance Center Drop in Tutoring

C++ Development toolset


You probably already have Cygwin on your laptop as part of the RCS default installation. What is it? It's a UNIX environment for the Windows Operating System. You can do all of your work for this class using Cygwin, g++, and a text editor such as Emacs (in other words, you don't need to use Visual Studio). Read more about Cygwin here:

Emacs on Windows

For those of you interested in running the Emacs text editor on your Windows OS, you can download the binaries and access documentation here:

Dev C++

Here's a free shareware (GNU General Public License) C++ development environment that can be used with the gcc compilers. The environment is similar to Visual Studio and is a good option for this class.

MinGW (Minimalistic GNU for Windows)

MinGW is a Windows native port of many of the GNU tool sets - including G++. This will allow you to run G++ (and other GNU tools) in Windows without needing to use Cygwin. These tools are released under Public Domain & GNU General Public License.

The main site contains links to the tool documentation (in the Documentation section) as well as a Wiki which contains lots of information (such as FAQs and How-To).

The downloads page on the main MinGW page is a bit out-of-date, so head on over to the MinGW SourceForge page to find the most recent downloads.

Quick and easy installation:

  • Go to the download page on the SourceForge protect page
  • Download 'MinGW 5.0.3' which is currently located under the 'Candidate' section. This installer will download and install the needed packages for you. Make sure that if you have a firewall you allow it to access the internet.
  • When you run the installer, you want to 'Download and Install' the packages, and you want to download them from the 'Current' releases (you will be prompted for this information)
  • When you are presented with the option to select packages to install, be sure to select the 'G++' package


  • If you want to use ./configure && make then you want to download and install MSYS as well.
  • If you are going to be using the jEdit commando file linked to below, make sure that the installation path dose not contain any spaces.

Insight (Stand-Alone GNU Debugger (GDB) Front-End)

Insight is a front-end for the GNU Debugger (GDB). This is covered by the GNU General Public License.

The website is unfortunately lacking when it comes to documentation, however, the tool is pretty easy to use. If you play around with it enough, you'll get the hang of it. This tool is released as source code that is compatible with *nix and Windows/Cygwin.

If you don't want to build it and you are a Windows user, our friends at MinGW have a Windows native version you can download. Go to the MinGW SourceForge download page. Under the 'Purposed' section, download 'insight.exe' under 'insight-6.3.50-20051116-cvs'


  • When compiling your projects, make sure to tell the compiler to generate the needed debugger information.

jEdit G++ Commando File

For those of you out there who use the jEdit text editor, this is a Console Commando file for running G++ (i.e. an easy to use GUI). This should be compatible with WinXP, *nix, and Mac. The download contains a readme with important information and notes. Please be sure to read this!


Wednesday, May 21, 2008

drag and drop data/files from windows to linux

copy from windows to linux in simple way

drag and drop data/files from windows to linux

I was struggling to get the files from windows to Linux machine.

well there are number of ways to do that, here I am sharing a very simple way -ie drag and drop data/files from windows to Linux, that can be used by non-techie person also.

1. To get files from windows X machine to Linux Y machine, you need to know IP address of Linux machine. Assuming that you have write permission on Linux machine.

2. Now you are ready with files to copy on Linux Y machine, open my-computer , go to address bar and type this is Linux Y machine's IP address. Press enter.

3. You will not see any data here, right click and you can see login as, login with user name and password. or same can be found at, go to file login as and login to linux Y machine.

4. once you are login select the folder where you want to drag and drop the files from windows machine.

5. Now Drag files from windows machine and drop to Linux machine.

And you are done..

Tuesday, May 20, 2008

Remote debugging for ARM target board

Remote debugging for ARM target board

With GDB one can both trace and modify code and data flow, and otherwise analyze the behavior of code, without explicitly changing any of it.

Rather than run a full-blown instance of GDB on the target platform, you can use GDBserver, a program that lets you run GDB on a different machine than the one on which your program is running. The advantage of using GDBserver is that it needs just a fraction of the target resources that GDB consumes, because it implements only the low-level functionality of the debugger -- namely setting breakpoints and accessing the target processor registers and read/write application memory. GDBserver takes control of the application being debugged, and then waits for instructions from a remote instance of GDB running on a development workstation.

Remote target needs to have debugging stub (gdbserver),
The gdbserver is also referred to as the 'stub' and must be cross-compiled for
that target.
You also have to use a version of gdb on the host that was built
with the correct target in mind.  Executing your gdb with the -v flag will give
you information on how it was built.
before you begin, you need a communication interface between the PC running GDB
and the target platform: either a serial link or, preferably, an Ethernet network
connection. You also need a cross-target toolchain: a GNU C compiler (together with
a C run-time library and binary utilities, a.k.a. binutils) that runs on the
development workstation and generates executables for the target processor. You will
build two sets of binaries from the GDB source code:
cross-target -- where the host is the development workstation and the target is the
target processor
native -- where both host and target are the target processor
First download the GDB source code compressed archive, and unpack it
The GDB source package uses the GNU build system, where generating binaries is
usually just a few commands away (./configure ; make ; make install)

Configure gdbserver for ARM target board


To remote debug need a gdb built for the correct target. To do this, download the latest gdb release (6.8)

    $ tar –xvzf .gz 
    $ cd gdb-6.8 
    $ ./configure --host=i386 --target=arm-linux --build=i686 
    $ make
    $ make install 

Cross target GDB ie arm-linux-gdb will be available in /home/bhagwat/arm-tools/bin/arm-linux-gdb/bin


Then, to build 'the stub' aka gdbserver for the arm-linux platform

    $ cd gdb-6.8/gdb/gdbserver 
    $ export CC=arm-linux-gcc   // if required
    $ export LD=arm-linux-ld    // if required 
    $ ./configure --build=i686 --host=arm-linux --target=arm-linux
    $ make

Copy gdbserver on target platform

gdbserver requires library, copy this from standard path to target boards /lib path.

Start with Remote Debugging

Start with simple hello.c example program

$ vi hello.c 
int main ( void)
   printf ( ”Hello , World ! \ n” ) ;
   return 0;

To build this program, you must use a cross compiler that is configured to generate binaries for the ARM Linux target (arm-linux-gcc).

Compile using arm-linux-gcc

$ arm-linux-gcc -g -Wall -o hello hello.c

Copy the program to board.

Telnet into the target and start gdbserver:

 $ gdbserver :4000 hello

The port number (4000) is arbitrary. Be sure to include the full path to your executable.

On the host, cd to the hello.c program directory and type:

 $ /home/bhagwat/arm-tools/bin/arm-linux-gdb/bin/arm-linux-gdb hello
 $ target remote ds:4000
 $ break main
 $ continue

Where "ds" is the name or IP address of your target board.

If you want to have some comfort, use ddd:

 $ ddd --gdb --debugger /home/bhagwat/arm-tools/bin/arm-linux-gdb/bin/arm-linux-gdb hello

Keep me updating.

Happy remote debugging.

Sunday, May 18, 2008

count the number of ones in a 32

/*no. of 1's in no. of 1's steps*/
int count(unsigned long int n)
int count=0;
return count ;

/* This is a table which will contain number of ones
corrosponding to number value */
static int bits_in_char [256] ;

int bitcount (unsigned int n)
// works only for 32-bit ints

return bits_in_char [n & 0xffu]
+ bits_in_char [(n >> 8) & 0xffu]
+ bits_in_char [(n >> 16) & 0xffu]
+ bits_in_char [(n >> 24) & 0xffu] ;

Friday, May 16, 2008

Deadlock...(RDBMS)- a Joke


Boss said to secretary: For a week we will go abroad,
so make arrangement.
Secretary make call to Husband: For a week my boss and
I will be going abroad, you look after yourself.
Husband make call to secret lover: My wife is going
abroad for a week, so lets spend the week together.
Secret lover make call to small boy whom she is giving
private tuition: I have work for a week, so you need
not come for class.
Small boy make call to his grandfather: Grandpa, for a
week I don't have class 'coz my teacher is busy. Lets
spend the week together.
Grandpa(the 1st boss ;) ) make call to his secretary: This week I am
spending my time with my grandson. We cannot attend
that meeting.
Secretary make call to her husband: This week my boss
has some work, we cancelled our trip.
Husband make call to secret lover: We cannot spend
this week together, my wife has cancelled her trip.
Secret lover make call to small boy whom she is giving
private tuition: This week we will have class as usual.
Small boy make call to his grandfather: Grandpa, my
teacher said this week I have to attend class. Sorry I
can't give you company.
Grandpa make call to his secretary: Don't worry this
week we will attend that meeting, so make arrangement .

HOW IS IT ????
this is called dead lock.

Thursday, May 15, 2008

Quantum computers take step toward practicality

In computing, a logic gate is built to accept a set of inputs and, depending on their properties, provide a specific output. In the binary logic found in today's electrical computers, a certain gate will yield a "1" only if all of its inputs are "1"s. Otherwise it will yield a "0." Similarly, a quantum photonic gate would work by detecting the properties of input photons from two light beams, called "control" and "signal," and then producing an output based on those, such as by flipping the polarization of one of the input photons.
clipped from

Computers based on the powerful properties of quantum mechanics have the potential to revolutionize information technology and security, but for decades they have remained more theoretical than practical, and difficult to scale up. That is changing, however, as demonstrated in a report this week in the journal Science.

In the paper, engineers and physicists from Stanford and the University of California at Santa Barbara demonstrate a potential progenitor of an essential component of quantum computers, "a logic gate" that enables interaction between just two particles of light.

The key advance is a solid state device that can reliably produce an interaction between the light particles, called photons.
"We have demonstrated a system composed of a single quantum dot in a cavity that can be used to realize such a gate, and we demonstrated that two photons can be made to interact with each other via this system,"
Logic from light

 blog it

Tuesday, May 13, 2008

program which produces its own source code as its output

FILE *fd;
int c;

fd= fopen("./file.c","r");
while ( (c=fgetc(fd)) != EOF)
printf("%c", c);


Monday, May 12, 2008

How to reverse a String without using C functions

How to reverse a String without using C functions ?
the above is slightly wrong
this is the corrected one..
char * rev_string (char * str)
char temp;
int i , j;
for (i = 0 ; str[i]!= '\0' ; i++);
for(j = 0 ; j < i ; j++ , i--)
temp = str[j];
str[j] = str[i];
str[i] = temp;
return str;
void reverse(char *);
void main()
char str[]="Hello";
printf("Reverse String is %s",str);
void reverse(char *p)
char *q=p;
 blog it

Friday, May 9, 2008

internal bootloader for ARM

Tips on i-boot for ARM926EJS.

I got chance to work on internal booltloader here are my notes on i-boot compilation.

Download doc file here.


Thursday, May 8, 2008

Learning ARM linux commands for creating library

ARM Linux commands starts with arm-linux-* for example compiling source code for ctime.c be

$ arm-linux-gcc -c -o ctimer.o ctimer.c

This linux command generates ctimer.o as output.

I was searching for the ARM-Linux command which will give me library.

$ arm-linux-ar cr libctimer.a ctimer.o

This command gives libctimer.a library to use in ARM linux platform.
Simple usage will be, say this timer library want to use in test.c
Compile test.c as

$ arm-linux-gcc -L. -o tester -I. test.c -lctimer

here . indicates library path, where the library is stored.

And your exe tester is ready.

Wednesday, May 7, 2008

linux from scratch

Enjoy reading linux from scratch book.

linux from scratch.rar

Monday, May 5, 2008

politely and anonymously ask your friends to stop sending spam

Wondered how you can politely and anonymously ask your friends to stop sending spam,
well stopForwarding.Us
enables you to send politely and anonymous message to your friend on behalf of you,
Below is example mail message which sends polite mail.

One of your friends has sent you this message from StopForwarding.Us, a website that allows individuals to anonymously email their friends and politely ask that they stop the habit of sending forwarded emails or FWDs.

Please do not forward chain letters, urban myths presented as truth, potentially offensive jokes, videos or photos without being asked or first receiving permission. If you find something that is funny and it is clean and you genuinely think the recipient will enjoy it then foward it to that person only (not in an email blast to all your friends and family) and include a personal note about why you enjoyed it and why you think they will too. Avoid sending forwards to friends or relatives that you've grown distant with. It can be frustrating for the recpient when the only correspondance you have with someone is via impersonal, unwanted email.

For more tips on email etiquette, visit StopForwarding.Us/etiq.html

Thursday, May 1, 2008

debug macro in kernel - dbg() macro

Help ksymoops out

mkdir /var/log/ksymoops

Every time a module is loaded /proc/ksymsand /proc/modules is copied there.

-k /var/log/ksymoops/.ksyms

-l /var/log/ksymoops/.modules

dbg() macro

#if !defined(CONFIG_FOO_MODULE)
#define MY_NAME "foo_driver"
#define MY_NAME THIS_MODULE->name

#define dbg(fmt, arg...) do {
if (debug)
printk(KERN_DEBUG "%s: "__FUNCTION__": "
fmt "\n" , MY_NAME , ## arg);
} while (0)

static int debug;
MODULE_PARM(debug, "i");

Using dbg()

dbg ("Added slot %d to the list",

shows up as:
hotplug_pci: pci_hp_register: Added slot 2 to the list

dbg ("");

shows up as:
hotplug_pci: pci_hp_register:

Other printk() macros

#define err(fmt, arg...)
printk(KERN_ERR "%s: " fmt "\n" , MY_NAME , ## arg)

#define info(fmt, arg...)
printk(KERN_INFO "%s: " fmt "\n" , MY_NAME , ## arg)

#define warn(fmt, arg...)
printk(KERN_WARNING "%s: " fmt "\n" , MY_NAME , ## arg)