Wednesday, January 15, 2014

Find the line where segmentation fault occurs


With using gcc, new users generally gets a segmentation fault while doing operations with pointers in C. As gcc is the most robust compiler of C, it doesn't allow to do anything wrong with memory references. Your TurboC++ compiler may allow invalid memory references in many cases but gcc may not. But, don't be scared of it. By using one of the best debuggers 'gdb' will find the line where core dump- segmentation fault occurs.


There are six reasons for occurrences of segmentation fault in C program. Out of all, generation of SIGSEGV is most frequent event which leads segmentation fault. Consider following example:

    #include<stdio.h>
    int main()
    {
       int x,*y,*z;
       x=10;
       *y=12;
       *z=x+*y;

       printf(”Addition is %d”,*z);
       return(0);
    }

This program will work fine in TurboC++ compiler but gcc will produce segmentation fault. As, it is a run time error the line where return fault has occurred, won't be displayed. So, what to do..?
Let see find it. First compile the program as,

gcc myprogram.c -g

The program will be compiled successfully and it will generate a.out file which is executable. We may use -o option also to give another name to output file. The -g option here gives ability to output file to get debugged. Now, the debugger will be enabled for output file a.out . Use gdb (GNU debugger) to debug program.

gdb a.out

This will start debugging and gdb> prompt will be shown. Use 'break' instruction to insert breakpoint in the program. Such as,

gdb> break 1

It will insert breakpoint at line no.1 in the program. Now, run the program by 'run' command.

gdb> run

This will execute your program till first breakpoint then after, we may step through our program by 'next' command. The 'next' command will execute the program line by line.
Wherever you find the segmentation fault, the program will stop on that line it will also show the line number. The above program will stop the execution due to segmentation fault on line no.6. It is invalid memory reference which is not allowed by gcc!

Friday, April 26, 2013

Use valgrind and find memory errors

The segmentation fault is the most frequently occurred runtime error in the C/C++ program compiled using gcc/g++. I have seen that students use TurboC++ compiler because it doesn't show the error like “segmentation fault”. Today's engineering students does not take any efforts to analyze why such kinds of problems occurs?
A program use memory space allocated to it which uses the stack and heap area. The memory is allocated here using runtime. For this we use the malloc function or new operator. Now, when the following situations occur, the program will show the “segmentation fault”.
- Memory is not released using delete/free.
- Using the array index which is not in the specified range.
- The uninitialized pointer is referenced in the program.
- Read only memory is attempted to be used for writing.
- Already freed pointer is dereferenced.

In order to find the reason of such different kinds of memory problem, we may use the valgrind which is a memory checker utility provided by the Linux system. It is used along with GDB. It is not possible for GDB find the memory errors in one stroke, so valgrind can be used. It is useful in many scenarios of C/C++ programming. Few of these are discussed in this blog post. Valgrind is used to notify the user with all the errors as given above. Generally, memory leakage problems can be easily detected by the valgrind. As the gcc is very robust compiler, the system tool is very much useful in the programming.

In order to download the valgrind latest version, use the following command on debian based Linux systems such as Ubuntu/Mint.

sudo apt-get install valgrind

or use following the install it on Redhat based Linux.

sudo yum install valgrind

As C/C++ don't have any automatic garbage collector, valgrind can be used the identify the garbages in the program. Lets write a simple program in C++.

#include<iostream>
using namespace std;
int main()
{
     int *x;
     x = new int(10);
     return 0;
}

This program is creating an array of 10 numbers using pointers. So, the memory of 10 locations is allocated to variable x using new operator. Now, compile the program by enabling the debugger to it using following command.

g++ -g prog.cpp -o prog

It will create the executable file named prog. Now use valgrind to check for the memory problems.

valgrind --tool=memcheck --leak-check=yes ./prog

This uses the tool “memcheck” by enabling the checking of the memory leakage ability.

==3550== Memcheck, a memory error detector
==3550== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==3550== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==3550== Command: ./prog
==3550==
==3550==
==3550== HEAP SUMMARY:
==3550== in use at exit: 4 bytes in 1 blocks
==3550== total heap usage: 1 allocs, 0 frees, 4 bytes allocated
==3550==
==3550== 4 bytes in 1 blocks are definitely lost in loss record 1 of 1
==3550== at 0x402B733: operator new(unsigned int) (in /usr/lib/valgrind/vgpreload _memcheck -x86-linux.so)
==3550== by 0x80485B0: main (prog.cpp:6)
==3550==
==3550== LEAK SUMMARY:
==3550== definitely lost: 4 bytes in 1 blocks
==3550== indirectly lost: 0 bytes in 0 blocks
==3550== possibly lost: 0 bytes in 0 blocks
==3550== still reachable: 0 bytes in 0 blocks
==3550== suppressed: 0 bytes in 0 blocks
==3550==
==3550== For counts of detected and suppressed errors, rerun with: -v
==3550== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

Here, 3550 is the process ID of your program. It has shown that the program has definitely lost 4 bytes in 1 block. This is shown because that the memory allocated is not freed. After adding 'delete x' statement at the end of the program, we will get this message using valgrind.

All heap blocks were freed -- no leaks are possible

Lets use the variable x[15] from the given array. It is not possible to use this variable from the array! I will write the following statement in the program.

x[15] = 34;

Now, valgrind shows the following statements:

==3608== Invalid write of size 4
==3608== at 0x80485C2: main (prog.cpp:7)
==3608== Address 0x4330064 is not stack'd, malloc'd or (recently) free'd

It means the line number 7 of prog.cpp has “invalid write of size 4 bytes”! We will easily identify that the memory is used is some wrong way in the program. Remember it is not a syntactical mistake!

Now when we use the uninitialized data in the program such as,

if (x[1]==1)
    x[1] = 0;

As the array is not initialized, the x[1] won't contain any value in it. When you compile the program and analyze it by valgrind, you will get the following output.

==3661== Invalid read of size 4
==3661== at 0x80485C2: main (prog.cpp:7)
==3661== Address 0x433002c is 0 bytes after a block of size 4 alloc'd
==3661== at 0x402B733: operator new(unsigned int) (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==3661== by 0x80485B0: main (prog.cpp:6)

This is the error for uninitialized data in the program. You will identify that the statement on line number 6 has some uninitialized data variable in it.
I think using valgrind improves the quality of software development. A software developer must use it in order to solve the memory leakage problems. It has many features, you may use 'man valgrind' to see all these features of it.
Enjoy good programming! 
 
 

Saturday, April 6, 2013

Swapping Techniques

Swapping refer to exchanging the contents of the variables. It is most important process in the programming. Generally, the sorting techniques use the swapping operations. When the information is very vast, we need some efficient sorting techniques to improve the speed of processing. You should use the swapping technique which has least time and space complexity in order to improve the speed of processing of the algorithms.
The generalized swapping technique that most software developers use it is have a temporary variable. For example:

if,
x = 12
y = 15

t = x
x = y
y = t

This will swap the contents of variables x and y. But, this technique wastes the extra memory in variable 't'. We can eliminate this by using the concept of addition and subtraction to swap the contents. For example:

x = x + y
y = x - y
x = x - y

Here, we are not using the temporary variable but three mathematical operations are required. This can be optimized by introducing the bitwise operation technique to swap the contents. The XOR operation will do the task for us. Let's see how this technique work.

You might be knowing the truth table of XOR operation.

x   y   x XOR y
0   0      0
0   1      1
1   0      1
1   1      0

The following operation will do the swapping.

x = x XOR y
y = x XOR y
x = x XOR y

Lets demonstrate it by simple example.

Let x = 12 and y = 15
means in digital or binary format, x = 1100 and y = 1111

so,

x = x XOR y = 1100 XOR 1111 = '0011'
y = x XOR y = 0011 XOR 1111 = '1100'
x = X XOR y = 0011 XOR 1100 = '1111'

So the final value of x and y are 1111 (15) and 1100 (12) respectively!

You may check this technique in C/C++/Java program by following operations.

int x = 12, y = 15;
x = x ^ y;  /* The ^ called as XOR operator */
y = x ^ y;
x = x ^ y;

These operations can be combined using assignment operator also. Such as,

x^=y^=x^=y;

Done.

tushar@tusharkute.com

Wednesday, December 5, 2012

DDD: Best Visual Debugger

Debugging is the most important activity in the software and program development. In order to remove or identify the logical errors in the program; debugging is done. Linux uses the utility of ‘gdb’ (GNU Debugger) to debug the programs. It has a strong instruction set but lacking in visual GUI debugging utility. 
 
DDD or Data Display Debugger is a visual debugger made available under GNU public license uses the gdb activities with GUI at foreground! Not only gdb but ddd has in-built command-line debuggers such as DBX, JDB, HP Wildebeest Debugger (WDB), XDB, the Perl debugger, the Bash debugger, the Python debugger, and the GNU Make debugger etc. So, by this visual debugger you can debug programs of C, C++, Java, Shell, Perl, Python etc.
 
Here I am giving a small tutorial to show how to use ddd to debug the C programs. The advanced tutorials will be given afterwards.
 
Let’s start… As I told ddd is available under GPL, so you can download the debugger using following command directly by debian version Linux.
 
sudo apt-get install ddd
 
or you may go to ‘Ubuntu Software Centre’ and search for ‘ddd’ as shown in the picture 1 below. You may get information about it and install from there.
[Note: If images are not visible, click on it to view large] 
 
Picture 1: DDD entry in Ubuntu Software Centre
For example, you have written a C program code named ‘basic.c’. Compile it using gcc in the following fashion:
 
gcc –g –o basic basic.c
 
here, -o suggests the name of the output file that you will create after compilation of ‘basic.c’ and –g suggests that you are giving ability to debug this program. If the –g is missing your program can’t be debugged by gdb or ddd. After compilation you will get an executable file named ‘basic’ (without extension). Note: you may give any name to your executable file at the time of compilation after option –o.
 
Your program will be compiled successfully as shown in the picture 2 below.
 
Picture 2: Compilation of program for debugging
Now, open your ‘ddd’ by simply typing ‘ddd’ on the terminal.
 
Open your program ‘basic.’c by using ‘File’ menu -> open program option. Your program will be opened as shown in the picture 3 below.
 
Picture 3: The ddd windows
Your program will be visible in ‘Program Window’. Below of it, you may find ‘Assembly Code Window’, where the assembly equivalent code is shown. And below of this window, your gdb command line debugger is shown. You may instruct program directly by using gdb also! 
 
In order to set a breakpoint inside the program move your mouse on to respective line and press right click. You may get options as shown in the picture 4 below.
Picture 4 : Setting a breakpoint
The any number of breakpoints can be set in a single program. It is used for executing the program till that point. From that line onwards, you may execute program step-by-step. The breakpoints in the function can be set by typing the command ‘b main’ in the ‘gdb window’ also! Or ‘b 12’ can also be written. Here 12 is the line number in the program.
Now, go to ‘Program’ menu and click on the ‘Run’. Your program will get executed till the first breakpoint set. See the picture 5 below:
Picture 5: Watch variable values by F6
The green arrow shows the line which is in execution currently. Red dot shows the breakpoint. Now, from here onwards you may execute step-by-step just by pressing function key ‘F6’. The program execution will be processed step-by-step as you go on pressing ‘F6’. You may find arrow is traversing with the lines of execution. When you move the mouse on any variable used in the program, you may get the value of the variable on current execution. In the picture 5, you may see the values inside array ‘a’ is shown by the ‘ddd’ at mouse position. By this way you can check the contents of the variable at any point in the program.
 
You may check the content by typing the command in the ‘gdb window’ as shown in the picture 6 below. It has shown the contents of a[i] by typing ‘print a[i]’ in the gdb window.
Picture 6: Use gdb window.
The features of ‘ddd’ are not limited with this. It has a rich set of applications. Just find the options given in the ‘ddd’s menu. You will learn a lot!

Sunday, April 29, 2012

Installing netbeans on Linux


NetBeans 6.5 IDE
NetBeans is one of the best integrated development environment (IDE) for developing with Java, JavaScript, PHP, Python, Groovy, C, C++, Scala, Clojure, as well as object oriented modeling and designs. It is open source and freely available IDE. It comes in a shell package on the internet. Many times we get confused about the installation of NetBeans for Linux OS.
In order to install Java oriented NetBeans, we must have installed JVM on our computer. Following are the steps to install offline NetBeans on the Linux system:

To install NetBeans IDE:

1.  Download the IDE setup file (extension .sh) from http://netbeans.org/downloads/

2.   Open the terminal and navigate to the directory that contains the installer file with .sh extention.

3.   Change the installer file's permissions to make the binary executable by typing from a command prompt:

        $ chmod +x executable_shell_file_name

        This will make your executable_shell_file_name executable. By
typing ‘ ls –a ‘ command, you can check the rwx permissions of the
file.

4.   Launch the installer by typing:

        $ ./executable_shell_file_name

5.   The installer searches for installed JDKs and prompts you to specify which the installer should use. You can also specify which JDK the installer should use from the command line. For example:

  $ ./ executable_shell_file_name -is:javahome path_to_your_jdk

6.   The installer will ask the directory to install. Choose it and press next.
  
Launching NetBeans IDE

To start the IDE:

    Navigate to the bin subdirectory of your installation.
    Execute the launcher script by typing ./netbeans.   

Depending on the Linux distro that you are using, you can find the installed file in the ‘applications’.

Saturday, April 7, 2012

Creating 'root' in Ubuntu


First time, when I used the Ubuntu operating system I found that the setup does not ask for the ‘root’ or its password! It accepts any user name that we want and then we may give any password to it. After installation it shows all accounts that we have created at the startup screen. I was wondered about the ‘root’ and its password also. It was my mistake that I have not searched the process to create the root on internet. I thought the main admin will be our root. But, I know that there is no any Linux distro without ‘root’.

Last month, when I was writing the RPC (Remote Procedure Call) programs on my Ubuntu system, it was not working and refusing the connections again and again. The same program I got executed on Redhat enterprise linux in my college! I tried lot of things and searched on the internet also but I did not find any solution to run the RPC on Ubuntu system. Finally, I mailed this to Prof. Chitrakant Banchhor in SCOE, Pune. He has given me the solution to create the root on Ubuntu. Then, my RPC worked under Ubuntu! We need to have admin (root) rights to run any RPC program under any Ubuntu or Debian distributions!

Here is the process to create it:

1.   Open the terminal in any administrator.
2.   Type:                sudo passwd root
3.   It will ask for your current admin password. After typing it, it will ask to type new root password twice!
4.   After typing it, your root will get established!
5.   We may switch to root temporarily by
su
<typing password>     
6.  In order to login to root, we need to choose the option ‘other’ at user login screen. Type ‘root’ at login name and then password.