October 20, 2010


Filed under: C, Commands, GNU/Linux, Tools — Tags: , , , , — shijitht @ 1:18 pm

Valgrind is a collection of tools to check the correctness of a program. The main tool in it is memcheck. It reports memory leak, out of bound writes, improperly initialized variables etc. This provides a report which pin points the correct location of the error. So this is a good tool to debug programs with unpredictable behavior and crash.


Inorder to see the exact line number of error, compile the code with -g option and reports could be misleading if optimization above level 1 are used(-O1). The -g option compiles the code with debugging symbols enabled, this helps valgrind to locate line number.
Use this program prog.c

void fun()
    char *a = (char *)malloc(10 * sizeof(char));
    a[10] = 'a';
    return 0;

prog.c has two major errors,
1. a[10]  = ‘a’;
a[10]  is out of the allocated region. Writing to this region could produce mysterious behavior. This is called heap block overrun.
2. 10 byte block pointed by a is never freed. So on return to main, that block remains inaccessible and unusable. This is a serious memory leak.

Lets use valgrind to detect these errors,
Compile the code with -g option

$ cc -g prog.c

Generate report

$ valgrind –leak-check=yes   ./a.out
can use 2>&1 to redirect report to a file( $ valgrind –leak-check=yes  ./a.out > report   2>&1 )

Analyzing report

Various error messages and summaries can be found. error messages are generated in case of out of bound writes, here a[10].
The corresponding report is
==4836== Invalid write of size 4
==4836==    at 0x80483FF: fun(prog.c:6)
==4836==    by 0x8048411: main (prog.c:11)
==4836==  Address 0x419a050 is 0 bytes after a block of size 40 alloc’d
==4836==    at 0x4024F20: malloc (vg_replace_malloc.c:236)
==4836==    by 0x80483F5: fun(prog.c:5)
==4836==    by 0x8048411: main (prog.c:11)
4836 is the process id. First line shows, error is due to an invalid write of size 4. Below it is a complete stack trace. The error happened at line 6 of  prog.c. Read stack trace from bottom to up. Started from main, then a function call to fun, malloc and error at last. Error shows the address we tried to write is beyond the allocated 40 byte block. This information is quite useful to make the code correct.

The Leak summery show the memory leaks.
==4836== LEAK SUMMARY:
==4836==    definitely lost: 40 bytes in 1 blocks
==4836==    indirectly lost: 0 bytes in 0 blocks
==4836==      possibly lost: 0 bytes in 0 blocks
==4836==    still reachable: 0 bytes in 0 blocks
==4836==         suppressed: 0 bytes in 0 blocks
Second line shows the 40 byte block lost in function fun. Report includes other types of leaks also.

Valgrind checks these errors and leaks in runtime like a virtual machine executing each instruction of a code. So it is time consuming for large code. But the report generated is very much useful and can be used to correct mistakes which are otherwise very difficult to detect.


August 22, 2010

Brief note about using Git

Filed under: Commands, Tools — Tags: , , — shijitht @ 7:51 pm

This is a short note including everyday commands. You can find more details like installing git and more commands at http://help.github.com/.
Don’t forget $ man git.

Create a repo graphically and do the following to get started.

The first section is to set up your name and email as a committer.

“git commit” allows to commit locally with  comment specified using -m option.
Here $ git commit -m “first commit” will make your local repo a step ahead of remote repo.
Check status using $ git status .
At first we need to connect to the remote repo. Origin is the remote repo-name used here, I suggest github or whatever name say your real repo name.
Now push using git push. Here master branch pushed to origin(remote repo-name).
To start tracking/add a new file → $ git add <file name> .
To commit to the remote repo → $ git push <repo name>  <branch name>.

Default branch choice is master.
To see branches → $ git branch .
Add -a option if you wish to see remote branches also.
To add a new branch for parallel developing → $ git branch <branch name> .
Move to the new branch → $ git checkout <branch name> .
To push a new branch →
$ git push <remote repo name>  <local branch name> :<remote branch name>.

If you use both branch names same, the ” <local branch name>: ” is optional.
To delete a branch locally → $ git branch -d .
To delete remote branch → $ git push <repo name>   :<remote branch name> .
i.e. nothing pushed( local section blank ).
To add a tag → $ git tag <tag name>
To update remote repo → $ git push   – – tags
To remove tags → $ git push <repo name>  :<tag name> .
Creating a folder demands adding files under it and use git rm in case of file deletion. So better keep file names distinct.
Use git clone/fetch to create a local copy of a repo.
To remove a repo do it graphically.

Useful links :

August 21, 2010

GitHub key update delay

Filed under: Commands — Tags: , , , , — shijitht @ 10:35 pm

Changing the current rsa public key or adding a new key wont work as soon as you do it. The usual delay I get is about 10-30 min. So don’t get frustrated and change keys continuously. Keep patience and wait. Do
$ ssh git@github.com   in intervals until confirmation. The success message is

ERROR: Hi shijith! You've successfully authenticated, but GitHub does not
provide shell access

August 9, 2010


Filed under: Commands — Tags: , , , — shijitht @ 12:03 pm
df (1)               - report file system disk space usage

df is used for checking file system size, used size and available size. By default it reports in numbers of 1k block. Two useful options are

            Use 1024-byte block units when writing space figures.
            Display in human readable format, Kb, Mb, or Gb.

Use man to find more.

August 5, 2010

Enabling root in Ubuntu !

Filed under: Commands — Tags: , , , — shijitht @ 11:15 pm

If you notice, you will find that Ubuntu never asks for a root password while installing. The thing is, root account disabled as default. This is not true in almost every case. So how can we enable it ?. sudo can do the trick. It means ” Super User DO “.If you are not a power user, you don’t need to activate root account. If you are ever asked to type a root privileged command, you could use  sudo. To run using sudo
$ sudo  < command to execute >
This will prompt for a user password.

If you are someone having lots of fun in terminal, activate the master.
To enable
$ sudo passwd
This will prompt for new root password and once you confirm it, you can start using root account. There is another option, type  $ sudo -i  for a root console.

Disable root by
$ sudo passwd -l root

For more administrative task use visudo, a privileged command which can change the power of users and groups.

vim configuration

Filed under: Commands — Tags: , — shijitht @ 6:43 pm

If you are a vim editor fan and a C programmer, you could get frustrated by typing commands for line number, C indentation and syntax. To automate this, install vim-runtime package. This includes rules for language-specific syntax highlighting and indentation, color schemes etc.

Create a file .exrc in home directory. $vim  ~/.exrc
Add these lines to the file
set nu                 ” for line number
set cindent         ” for C indentation i.e. correct spacing for better understanding
syntax on           ” for coloring according C syntax

A lot more options could be added for user related customizations.

July 31, 2010

su using ssh

Filed under: Commands — Tags: , , — shijitht @ 10:19 am

Use ssh-keygen to create privet and public keys for the user say guest.
Now change to root and add a new file in root home directory.
Put public key of guest into it.
Now we can login as root from guest without typing root password.
$ ssh -l root localhost
Make sure ssh service is on.
Pretty handy if you need permission from administrator for each root access.

July 29, 2010

Unmount a busy device

Filed under: Commands — Tags: — shijitht @ 9:50 pm

problem : #umount device will show an error message device is busy
#fuser -m device will show the processes currently accessing the device.Kill those device (kill -9 if needed) to free the device and umount it.
Try #umount -l device for lazy unmount.

December 24, 2009


Filed under: Commands — Tags: — shijitht @ 1:11 pm

Mail will sent mail to any user.

$mail username/address(Address like email)
The temporary mail messages are in the file
/var/mail/username and it’s contents deleted when
the user uses the message.
The message used with command mail
This will show the mail messages one after the other (Subject only).
When we press ENTER the messages will be shown in detail one after
the other.
we can switch to any message using it’s number

The options are
&d — will delete the message
&dt — delete and show the next message
&s file name — store the message in the file specified
&q — quit the mailbox

If we quit the mailbox without deleting/storing messages, it will be stored in spool.
CTR+D can be used to exit or go to the next argument.

Blog at WordPress.com.