Monday, 26 October 2009

Colored Printf - Linux

1. Use ncurses
2. ANSI Codes :
Printing color strings uses only printf, without any alternative libraries such as ncurses? Yes, we can do that in any unix based operating system that support ANSI codes, I am not sure whether it works on either windows or mac, have no chance to try at the moment.

By feeding some magic characters to printf, you can manipulate your fore color, background color and even attributes. Lets look at a simple example to print “Hello World” in Bright Red with background black.

 #define BRIGHT 1 
#define RED 31 
#define BG_BLACK 40 
printf("%c[%d;%d;%dmHello World", 0x1B, BRIGHT,RED,BG_BLACK); 

Okay, the code above print the string with bright red but the color setting will remain, to reset back the color to default, refers the code as bellow:

 printf("%c[%dm", 0x1B, 0); 

The fore color, background color and attributes codes are shown as bellow


How’s the color magic works? 0×1B is a special code that used to do all the color settings. 0×1B is hex code equivalent to decimal 27. With character(27) and a open square blacket “[”, initiate the setting. The rest of the values are (attribute);(fore color);(background color) and it ends the setting with ” m “. So entire thing will be look like this:

printf("%c[%d;%d;%dm",27,1,33,40);

With that the rest of your print line will be in bright yellow with background color black.

I have transparent background, What if I want my default background instead of any color?
Simple, ignore the background color, How?

printf("%c[%d;%dmHello World%c[%dm\n",27,1,33,27,0);

The line above will print bright yellow “Hello World” with default bg color.

Wednesday, 24 June 2009

Kinds Of Memory

There are roughly 5 kinds of memory:

1) Statically allocated
2) Automatically allocated (stack/registers)
3) Dynamically allocated (heap)
4) Read-only memory
5) Program memory


1) All global variables and all variables declared with the keyword static. ANSI C/C++ states that they will be initialized to zero before the program starts. These are the only variables that have a known value at program start, without the programmer explicitly initializing them. RAM.

2) All local variables. (Local constants might end up here too, though that depends on the type of system.) RAM.

3) Memory explicity allocated by the programmer with malloc or new. RAM.

4) All global constants. Strings end up here since they are a special case treated as constant character arrays. On a PC: RAM. Non-PC systems: non-volatile read-only memory.

6) The code itself, as well as constant numeric values written in the code (#defines etc). On a PC: RAM. Non-PC systems: non-volatile read-only memory.

Thursday, 3 July 2008

Recursive Find and Replace in Directory

Sed to our rescue :)

1. Generating the file argument using "find" command :

find ./ -type f -exec sed -i 's/string1/string2/' {} \;


Monday, 30 June 2008

Finding Out The Linux Distro and Version Info

Lot's of times we end up working on a system/server we don't know any thing about. So to be able to do more meaningful stuff, we need to find out what breed of Horses we are actually gonna race with.

1. The most basic way to do it would be to use "/PROC/" file system.
More specifically, the "version" file.

bash# cat /proc/version
Linux version 2.6.9-42.ELsmp (bhcompile@hs20-bc1-1.build.redhat.com) (gcc version 3.4.6 20060404 (Red Hat 3.4.6-2)) #1 SMP Wed Jul 12 23:27:17 EDT 2006


2. Use "LSB" as fodder (Linux Standard Base)

bash# lsb_release -a
LSB Version: :core-3.0-ia32:core-3.0-noarch:graphics-3.0-ia32:graphics-3.0-noarch
Distributor ID: RedHatEnterpriseAS
Description: Red Hat Enterprise Linux AS release 4 (Nahant Update 4)
Release: 4
Codename: NahantUpdate4

Currently, most of the major Linux distros (UBUNTU, RED HAT, Free BSD etc) comply with the LSB.

3. A not so popular way (which might not work on some distros like Free BSD) would be

bash# cat /etc/*-release
Red Hat Enterprise Linux AS release 4 (Nahant Update 4)

4. Again, using /etc , we can get

bash# cat /etc/issue
Red Hat Enterprise Linux AS release 4 (Nahant Update 4)
Kernel \r on an \m

5. Using the boot up messages is another nifty way to get this info :

bash# dmesg | head -1
Linux version 2.6.9-42.ELsmp (bhcompile@hs20-bc1-1.build.redhat.com) (gcc version 3.4.6 20060404 (Red Hat 3.4.6-2)) #1 SMP Wed Jul 12 23:27:17 EDT 2006



Well, am sure that there might be many more ways to find this info, and if you do know of them, do leave a comment and let me know about it. Each day I realize that there is much more learn/know, that I get even more keen to dwell further into this beast of a Linux.

Sunday, 13 January 2008

NFS Server Not Responding

Incase you are able to connect to the NFS server, and able to mount using , for example ,say :

mount -t nfs -o nolock 10.88.27.76:/home/jayant /mnt

And are further able to access the FS as well (able to view directories and files), but any attempt to try and execute or access any big file (like "insmod"ing a kernel object, or executing helloworld executable) results in the heinous "NFS server not responding" error,i.e., something like the following:

nfs: server 10.88.27.76 not responding, still trying

,then try to increase the rsize to the maximum permissible value (in my case, 8192 bytes)...This might help...So, instead of cmd1, try the following command to mount :

mount -t nfs -o nolock,rsize=8192 10.88.27.76:/home/jayant /mnt

Tuesday, 11 December 2007

ARM Linux Kernel Boot Requirements

Essentially, the boot loader should provide (as a minimum) the following:

1. Setup and initialise the RAM.
2. Initialise one serial port.
3. Detect the machine type.
4. Setup the kernel tagged list.
5. Call the kernel image.

For more information, refer the following link :

http://www.arm.linux.org.uk/developer/booting.php

Sunday, 18 November 2007

Debugging a Kernel object (ARM + linux)


1. Attach to the target and stop/break.

2. Load vmlinux (the statically linked executable file that contains the Linux kernel).

data.load.elf

A:CC jayant_amber......squashfsvmlinux /strippart 3. /nocode /noclear

3. Now, display the loaded modules (Linux > Display Modules).

4. Now from the TASK.MODule table, note down the .text sections start address (the red-circled address).



5. Well, you got it, we are now gonna use all the ELF info we can extract from the kernel object. Lets use the arm utilities to help us out. So, just do arm_v5t_le-readelf -a samdrv.ko > dump.dump.


6. Find the size of the .data section entry in the Sections Headers part from the dumped file.

7. Lets now calculate the address to relocate the .data section to. Subtract the size of the .data section from the magic number of the kernel object we wanna debug.

0xBF1BC920 0x3FD0 = 0xBF1B8950

8. Now load the kernel object, and use the following relocation code as well.

data.load.elf A:CCjayant_amber......Libsamdrv.ko /strippart 3. /gnu /nocode /noclear

/reloc .text at 0xBF044000 /reloc .rodata after .text /reloc .data at 0xBF1B8950

/reloc .bss after .data

9. Congratulations, you can now step through the kernel object and debug it.