Friday, May 16, 2014

How to debug unknown exception in AIX system using dbx?

dbx is the equivalent of gdb debugger in Linux.
Let's assume you have an issue with an application which terminates with an exception in the AIX system.
You don't know where this exception is thrown in the code and the exception message is not very clear to you. The first thing to you need to know is where this exception gets thrown in the code so you can fix the issue. Here is the trick:

>dbx  myprogram

(dbx) stop in __ThrowV6  //break at any exception, __ThrowV6 is the exception handler. All exceptions will go to this exception handler. If you stop here and view the stack you will know exactly where the exception is being thrown.
(dbx) r -c arguments   //run the program with specific arguments


This trick above is handy if you don't know where to set a break point in the program since the exception can be thrown in many places in the code. Some code does handle the exception, but it would just print out a generic message which is useless. It is a good idea to handle the exception and print out the real message.

#include <exception>
using namespace std;
try
{
....
}
catch(exceptio& e)
{
cout << e.what() << endl; //this will print out the actually error causing the exception.
}

Thursday, May 15, 2014

How to share/access Linux folder from Windows machine?

Samba is a service that would allow you to share your Linux folder so that you can access it on your Windows machines.
Here is a quick tutorial on how to do it.

1) first you need to check if the samba server is running or not
/sbin/service --status-all  | grep smb

If it is not running yet, try to start it

sudo /sbin/service smb start

2) second check if this shared definition exists in your smb.conf file

[homes]
         comment = Home Directories
         browseable = no
         writable = yes
         valid users = %S

In order to access the symlinks in your samba share, you need to add the followings to your global section:

[global]

follow symlinks = yes
wide links = yes
unix extensions = no

If the above does not exist in the smb.conf file, add them to the file

sudo emacs -nw  /etc/samba/smb.conf

Your home directory should be shared by default.

Once you have finished modifying the file, restart the samba service

sudo /sbin/service smb restart

3) If you still cannot access the shared in Windows, check and make sure the user is already in the samba database.
It will prompt for the password, it is good to use the same password you used to log into windows.

sudo smbpasswd -a nquach

Once you have everything setup, you can access your linux share on Windows by using this path.

\\linuxmachine\username or \\LinuxHostName\nquach

Cheers

Monday, May 12, 2014

How to profile your code using valgrind?



There are various ways that you can profile the code. I will talk about Callgrind and Massif. You can get a lot more information from these two profilers, but I will just mention about my purposes for using them.
Use Callgrind to find out which methods get called the most, and use Massif to find out which methods use the most memory.

Callgrind records the call history and the program's calls graph. Here I am using it to find which methods get called the most.

Example:
>valgrind --tool=callgrind  --callgrind-out-file=callgrind_prof.out validaterpd -s -o consichk.txt -r coreapplication_OH452963239_v326.rpd -pAdmin123

Use kcachegrind to view the output from callgrind.

>kcachegrind callgrind_prof.out





Massif is a heap profiler. Here I am using it to find out the peak memory usage of my program.
I can also find out which method uses the most memory as well.
For details on now to interpret the results of your profile, please check this page

http://valgrind.org/docs/manual/ms-manual.html


Occasionally you may running into issues where valgrind fails to run complaining about the
VG_N_SEGMENTS segment being too low.

If you run into this error, you need to change the default value and recompile valgrind.
You can search for the cpp file defining this  VG_N_SEGMENTS value and change it.
For example, I have changed it from 30000 to 90000.



Example:

>valgrind --depth=200 --tool=massif  --massif-out-file=main_mem_prof.out validaterpd -s -o consichk.txt -r coreapplication_OH452963239_v326.rpd -pAdmin123

>ms_print main_mem_prof.out > main_depth200.txt


From the screenshot, you can tell that the peak is at snapshot 4 and the peak memory usage is around 4.2GB.

Friday, May 9, 2014

How to install software on your Linux machine?

This article will talk about how to install any software on your Linux machine, if you have the source code. I will not talk about how to install the software from the rpm binary file.

If you already have the RPM file, you can install the software by running

rpm -ivh  filename.rpm

If you want to deinstall from an application installed from a RPM package, you can run

rpm -e <applicationname/processname>
>rpm -e oracle-xe


Here are some of the advantages of installing software from the source code:

1. You cannot find the rpm binaries for your platform
2. Only the source code is provided by the website.
3. You cannot find the latest rpm binaries for the latest source code.

Most of the open source code project will tar and zip the source code in a file like *.bz2 or *.gz.

Here are the steps you need to do:

1. Download the source code. Let's say it is called valgrind-3.7.0.tar.bz2
2. Extract the file.

>tar xvjh valgrind-3.7.0.tar.bz2
or
>tar xzvh valgrind-3.7.0.tar.gz

Assuming the files get extracted into valgrind37 folder
3. cd to the source folder  valgrind37

>cd /valgrind37

4. Configure the software

>./configure  \-prefix=/valgrind37   //run the configuration script. Notice I use to prefix to specify where
I want to install the software. The directory must be an absolute path. If you don't use the prefig, it will install the software in the default location like /usr/bin/.

5. Build the source code
>make

6. Install the sofware
>make install

7. Clean the temp build files.
>make distclean    //you need to run this every time you run ./configure with different prefix values.

8. Set the environment
Most of the time, you only need to set these two environment variables to run any programs.
>export LD_LIBRARY_PATH=/vagrind37/lib/valgrind/:$LD_LIBRARY_PATH
>export PATH=/valgrind37/bin:$PATH