Tuesday, December 2, 2008

LD_DEBUG Linux

The dynamic library loader used in linux (part of glibc) has some neat tricks. One of these is that you can set an environment variable called

LD_DEBUG

to show how symbols (variables and functions, for example) are resolved for a dynamic executable. This can sometimes help resolve obscure bugs where your application isn't doing what you expect (assuming it is caused by symbols being resolved differently to what you were expecting).

This is very useful if you get segmentation violations or aborts for a program - this can sometimes be caused by linking against the wrong version of a library. This is also a really good way to understand what happens when you run any program! It has some self-documentation - for the impatient, you can do

$ LD_DEBUG=help /path/to/some/dynamic/executable

eg

$ LD_DEBUG=help ls

prints out:

Valid options for the LD_DEBUG environment variable are:

libs display library search paths
reloc display relocation processing
files display progress for input file
symbols display symbol table processing
bindings display information about symbol binding
versions display version dependencies
all all previous options combined
statistics display relocation statistics
help display this help message and exit


To direct the debugging output into a file instead of standard output a
filename can be specified using the LD_DEBUG_OUTPUT environment variable.

As a quick example of what it does:

$ LD_DEBUG=all ls 2>&1 > /dev/null | less

13442:
13442: file=librt.so.1; needed by ls
13442: find library=librt.so.1; searching
13442: search cache=/etc/ld.so.cache
13442: trying file=/lib/librt.so.1
13442:
13442: file=librt.so.1; generating link map
13442: dynamic: 0x400263ec base: 0x40020000 size: 0x00010d14
13442: entry: 0x400219c0 phdr: 0x40020034 phnum: 6
13442:
13442:
13442: file=libc.so.6; needed by ls
13442: find library=libc.so.6; searching
13442: search cache=/etc/ld.so.cache
13442: trying file=/lib/libc.so.6
13442:
13442: file=libc.so.6; generating link map
13442: dynamic: 0x40146ce4 base: 0x40031000 size: 0x0011ab00
13442: entry: 0x4004a184 phdr: 0x40031034 phnum: 6
13442:
...
13442: checking for version `GLIBC_2.2' in file /lib/librt.so.1 required by file ls
13442: checking for version `GLIBC_2.1' in file /lib/libc.so.6 required by file ls
13442: checking for version `GLIBC_2.2.3' in file /lib/libc.so.6 required by file ls
...
13442: relocation processing: /lib/libpthread.so.0 (lazy)
13442: symbol=_errno; lookup in file=ls
13442: symbol=_errno; lookup in file=/lib/librt.so.1
13442: symbol=_errno; lookup in file=/lib/libc.so.6
13442: symbol=_errno; lookup in file=/lib/libpthread.so.0
13442: symbol=_errno; lookup in file=/lib/ld-linux.so.2
13442: binding file /lib/libpthread.so.0 to /lib/libc.so.6: normal symbol `_errno' [GLIBC_2.0]
13442: symbol=_h_errno; lookup in file=ls
13442: symbol=_h_errno; lookup in file=/lib/librt.so.1
13442: symbol=_h_errno; lookup in file=/lib/libc.so.6
13442: symbol=_h_errno; lookup in file=/lib/libpthread.so.0
13442: symbol=_h_errno; lookup in file=/lib/ld-linux.so.2
13442: binding file /lib/libpthread.so.0 to /lib/libc.so.6: normal symbol `_h_errno' [GLIBC_2.0]
...

In other words, every single function and external variable in the standard library that ls(1) uses must be located each time it is run (kind of obvious, really).

$ ldd /bin/ls
librt.so.1 => /lib/librt.so.1 (0x40020000)
libc.so.6 => /lib/libc.so.6 (0x40031000)
libpthread.so.0 => /lib/libpthread.so.0 (0x4014c000)
/lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)

see ld.so(8) for environment variables.

Friday, November 21, 2008

wireless network setup on linux

I tried setting up wireless network on my linux laptop..partitioned with windows too ! Initially, I struggled and then after "Googling", I finally setup the network and it was actually great. It's because the network access was not pretty good on windows.....so on Linux, it was great experience !

After few days, I made again a mistake to change the channel of the ESSID [wireless network ID on linux] and network stopped working on Linux :(

Now the problem was to identify the wireless networks available because I felt, Linux didn't have good gui interface to show the available wireless networks...so tried finding out ways to do so....and again "Googling" helped me. Here are some of the tricks/tools which helps for the same....it's for sure for me.....and for all who face troubles !

$ iwconfig

Show the various ethernet and other interfaces, associated wireless network.

$ iwlist eth1 scan

It shows all the wireless networks available to connect. 'eth1' is the interface
identified from the previous command [associated to wireless network]

$ dhclient

A very useful utility for dhcp clients tracing.

Wednesday, October 15, 2008

Counting number of 1s in a number...

A simple and powerful algorithm....for calculating number of set bits....

#include

int CountSetbits(int num )
{
int iCount = 0;
while ( num )
{
num &= (num-1) ;
iCount++;
}

return iCount;

}

int main()
{
int num = 7;
printf("Number of set bits in a number %d = %d\n", num, CountSetbits(num));
}

Thursday, September 18, 2008

Learning Python.....

The dir() Function

The built-in function dir() is used to find out which names a module defines. It returns a sorted list of strings:
I have been listening quite often about python......not from colleagues but on internet...and went through some comparative study between python/perl/ruby. Many a times, I found python is on an edge to other scripting language and I had it in mind that I'll give it a try provided I get chance to do something interesting in a new language. Today, I found myself with good enough time and an activity hence I thought to start python....long pending language to dig upon -

Seems like very interesting and initially I would have got carried away if I wouldn't have found every single thing I stuck at....Google helped [as usual] me in finding whatever I wish to know about python....obviously credit goes to python users world wide who keep putting things here and there....which google fetch smartly for us.

For others, Few trips and tricks I can give is as follows -

-> As usual, interpreter based scripting, hence all the scripts need to have following at the first line of script

#! /usr/bin/env python

-> You can import in-built rich library of functions e.g. -

python>>> import sys
python>>> import os

-> You can find very helpful [help;] doing following -

python>>> import fibo, sys
python>>> dir(fibo)
['__name__', 'fib', 'fib2']
python>>> dir(sys)
['__displayhook__', '__doc__', '__excepthook__', '__name__', '__stderr__',
'__stdin__', '__stdout__', '_getframe', 'api_version', 'argv',
'builtin_module_names', 'byteorder', 'callstats', 'copyright',
'displayhook', 'exc_clear', 'exc_info', 'exc_type', 'excepthook',
'exec_prefix', 'executable', 'exit', 'getdefaultencoding', 'getdlopenflags',
'getrecursionlimit', 'getrefcount', 'hexversion', 'maxint', 'maxunicode',
'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache',
'platform', 'prefix', 'ps1', 'ps2', 'setcheckinterval', 'setdlopenflags',
'setprofile', 'setrecursionlimit', 'settrace', 'stderr', 'stdin', 'stdout',
'version', 'version_info', 'warnoptions']

Rest will follow soon, happy python[ing]

Thursday, September 11, 2008

LINUX Boot process :: initrd-ramdisk

While booting linux on ARM based platform, I suffered enough and linux boot hasn't get going across panic......It's panic, which has stopped me...and I am not sure what's wrong and where. I am comparatively a newbie so google is helping me a lot !

Found an interesting article....hope it gives me some clues to move further....

What's an initial RAM disk?

The initial RAM disk (initrd) is an initial root file system that is mounted prior to when the real root file system is available. The initrd is bound to the kernel and loaded as part of the kernel boot procedure. The kernel then mounts this initrd as part of the two-stage boot process to load the modules to make the real file systems available and get at the real root file system.

The initrd contains a minimal set of directories and executables to achieve this, such as the insmod tool to install kernel modules into the kernel.

In the case of desktop or server Linux systems, the initrd is a transient file system. Its lifetime is short, only serving as a bridge to the real root file system. In embedded systems with no mutable storage, the initrd is the permanent root file system. This article explores both of these contexts.

Further Read Available here.

Monday, September 8, 2008

Building Linux for arm arch.

I started building linux on Arm architecture and while configuring linux, I had a bad time pointing to a correct qt installation. The problem was that while first try, the environment had a qt installation which was not good one [damn, sysadmin who doesn't care at times, what's lying where], referenced for build. Now everytime, it was trying to pick up the library and never able to create the conf application, which launches "xconfig" GUI window. I tried searching a number of Makefiles, modules which are referencing QTDIR but couldn't get one. As I was a starter atleast for this, I suffered....and couldn't find the solution fast.

Anyways, After giving some more try, I can see that in fact, while building, environment generates .tmp_qtcheck file inside scripts/kconfig. This file is as follows -

KC_QT_CFLAHS=-I${QTDIR}/include
KC_QT_LIBS=-L/usr/qt-3.1/lib.................
KC_QT_MOC=/usr/lib64/qt-3.1/bin/moc

This need to be removed, if you want to provide new QTDIR to the linux build process. Hope it helps.

Thursday, September 4, 2008

LAMP

Linux - Apache - MySql - Php

This has become a hot technology quickly for website developments. I started playing with it again after I made some experimentation 2-3 years ago. I see a problem....in human behaviour.....in me now..huhhhh. I use to know/understand every bit of [so called] LAMP those days and had a good setup on my windows PC. Also I made some experimentation with mambo and everything seemed to work fine. I wanted to take it to next step, creating my own site with some basic contents in the website but then as usual, I got carried away...There were few other important things in life than creating a website....fooh..

I started this exercise again on linux pc....everything remains same but I don't remember much from my last work...so a ton of rework. I start to hate my forgetfulness now. I would like to sharpen my memory.

I would be posting the setup for the LAMP on my Linux machine. Already there are a lot of stuff on web so I wouldn't like to create alltogether new guide but would point to useful ones. A part of my work would talk about issues or level of understanding concerning the problems/issues, I faced while working on LAMP installation.

Till then......WAIT !