Thursday, July 7, 2011

Segmentation Fault

A segmentation fault or bus error occurs when the hardware notifies a operating system about a memory access violation. On receiving the notification the OS sends a signal to the process which caused the exception. Now its upto the process receiving the exception to decide how it would like to handle this exception. By default the process receiving the signal dumps its state in to a core file and terminates but the default signal handler can be overridden to customize how the signal is handled.

There are many ways to get a segmentation fault, at least in the lower-level languages such as C(++). A common way to get a segmentation fault is to dereference a null pointer:
int *p = NULL;
*p = 1;
Segmentation fault also happens when you try to write to a portion of memory that was marked as read-only:
char *str = "Foo"; // Compiler marks the constant string as read-only
*str = 'b';              // Which means this is illegal and results in a segfault
Accessing Dangling pointer also causes segmentation faults like here:
char *p = NULL;
{
    char c;
    p = &c;
}
// Now p is dangling
The pointer p dangles because it points to character variable c that ceased to exist after the block ended. And when you try to dereference dangling pointer (like *p='A'), you would probably get a segmentation fault.


Another reason for segmentation fault is to recurse without a base case, which causes a stack overflow:
int main() { main(); }
Segmentation fault also happens when accessing a region of memory already freed

Whenever segmentation faults happens, a core is generated by the process in the current directory. By default, most of the Linux distros disable the creation of core. So if you dont find the core file run the following command to enable the creation of core files.
$ ulimit -c unlimited
 Generally core files are created as core.<pid>, pid is the pid of the process that created the core.

Running the above command will set ulimit to unlimited on that terminal. So when you start a new terminal it would not be set. So to make it default in all the new terminals add it into ~/.bashrc.

But to make sense out of the core dump you must compile the program with debugging symbols. Lets take an example program to understand how to use the core dump to the fullest.
$ cat hello.c
#include <stdio.h>
int
main ()
{
        int *p = NULL;
        *p = 2;
        return 0;
}
 Now enable debugging symbols while compiling
$ gcc -g -O0 hello.c -o hello
Now when you run this program, you will receive the following error
$ ./hello
Segmentation fault (core dumped)
You will find that a core file is created in the current directory.

Now run the core with the debugger (on most of the distros gdb is provided if not install it)
$ gdb hello core.5123
It will display the line in the program that caused the error. There are many other options that you can use with gdb to debug your issue, we will visit that in future posts.

Some of the best practices to follow while programming in C/C++ is to 
  • Always check for NULL before dereferencing the pointers. Example

             if (ptr == NULL)
                       //return with error
  • Also check for NULL after the call to dynamic memory allocation functions like malloc, calloc, new etc. Example

            ptr = (int *) malloc (10 * sizeof (int));
            if (ptr == NULL)
                       //return with error
References
* http://collectd.org/wiki/index.php/Core_file
* http://stackoverflow.com/questions/2346806/what-is-segmentation-fault
* http://en.wikipedia.org/wiki/Segmentation_fault

Monday, June 27, 2011

SSH: "Remote host Identification has changed" error


SSH is the most common and widely used method for remote login. It is better the Telnet because it provides authentication mechanism to secure your machine (If you dont want to enter your password each time when you login, checkout the previous post on passwordless-ssh-login-howto). But, sometimes one may find that the machine that we used to login sometime back is refusing to authenticate. If the error that you found is
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that the RSA host key has just been changed.
The fingerprint for the RSA key sent by the remote host is
9b:8e:77:08:7c:8f:2d:f7:0b:4e:dc:d3:98:29:dd:ec.
Please contact your system administrator.
Add correct host key in /root/.ssh/known_hosts to get rid of this message.
Offending key in /root/.ssh/known_hosts:8
RSA host key for 192.168.1.8 has changed and you have requested strict checking.
Host key verification failed.
Just delete the file ~/.ssh/known-hosts.
> rm ~/.ssh/known-hosts
 Thats it, now try running ssh at the terminal it will succeed.

Thursday, June 16, 2011

Ubuntu: Creating a bootable Pendrive

 In this post, I will demonstrate how to create a bootable pendrive from an .iso image on ubuntu. Creating a bootable pendrive is very easy in ubuntu. If you are using ubuntu 10.10, then goto
System->Administrator->Startup Disk Creator
Select Startup Disk Creator it will prompt for password if you are not root, enter the password and it starts. It looks like this

Startup Disk Creator


Once you have started the Startup Disk Creator follow these steps to create a bootable Pendrive:

1. click on  "other" button and select the location where you have stored the .iso file.


2. Then if you want to erase all the data on the pendrive then press on "Erase Disk" button and wait for it complete. Skip this step if already empty.


3. Select "Make Startup Disk" button.







Thats it. Once it completes you have a bootable pendrive which you may use it for
  • liveCD
  • installing a fresh distro installation

I have tested this for creating a bootable pendrive from ubuntu-11.04.iso on ubuntu-10.10. If you happen to use it for other distros then do post your results

Friday, June 3, 2011

mencoder: Converting .ogv to .avi

recordMyDesktop produces videos in .ogv format. Youtube and many other websites fail to play the .ogv video. Use mencoder to convert the .ogv video. mencoder is a simple movie encoder, designed to encode MPlayer-playable movies to other  MPlayer-playable formats.

In ubuntu, install the mencoder using
>sudo apt-get install mencoder
once installed, use the following command to covert the .ogv file to .avi file.
>mencoder video.ogv -ovc xvid -oac mp3lame -xvidencopts pass=1 -o video.avi
 There are many more options to mencoder, read the man page for more info.

gtk-recordMyDesktop

The Chinese proverb "A picture is worth thousand words" fits perfectly here, so I will be demonstrating by showing a video.


The front end makes use of the commandline options to perform the task, which means that the things we achieve through the gui can also be done through the commandline.

Prev 

Simple video capture

Simple video capture:
We start by exploring the recordMyDesktop through command line. If you just cant wait to capture your first video, then follow the command to start recordMyDesktop from the command line
>recordmydesktop --no-sound -o my-first-video.ogv
 When you enter this command you will see



                                           
From the screenshot, you can see that the recordMyDesktop records the desktop session untill a ctrl+c is pressed to stop it. On receiving the signal, it stops capturing the video and starts encoding the video. Once it completes, we have successfully captured the video. By default the video is saved in the home directory.

By default recordMyDesktop captures the whole screen, but we can pass options to configure it according to our needs. For more information check the man page.

Prev Next 

RecordMyDesktop

Intro:

recordMyDesktop is an open source free software used to record desktop session on GNU/Linux. It is a very handy application to have on Linux and one can find lot of videos on net which make use of recordMyDesktop.  The project was started by John Varouhakis in 2006 and he continued to be the maintainer of the project for a long time. The project has not seen an active traffic for last year or two. The last release was  0.3.8.1. The project is hosted on sourceforge. Here is the link to download the source code.
 
Like many other linux softwares out there, recordMyDesktop also comes in two flavors: the typical of linux commandline and two graphical frontends, gtk-recordMyDesktop and qt-recordMyDesktop. Most of the Linux destro's have recordMyDesktop pre-installed in them. To install it on ubuntu, use
> sudo apt-get install gtk-recordmydesktop 
gtk version of recordMyDesktop
One last important nugget is recordMyDesktop supports only .ogv theora for video encoding, and .ogv vorbis for audio. If you want to convert your screencasts to other formats there are many good Linux tools for this and also you can find very helpful information here.

Next