Friday Aug 27, 2010

Using DTrace to profile function flows in your C programs

Quite often, you may would have come across scenarios where you would want to prepare a trace of your own program and understand what is the execution flow of a particular function in your C program. In this example I will show you how exactly you can do that using DTrace.

Here is a simple C Program which implements two functions (add and sub).

#include <stdio.h>
int add(int a, int b);
int sub(int a, int b);
int main()
    int a;
    int b;
    int c;
    printf("\\nEnter A and B: ";);
    scanf("%d%d", &a, &b);
    c = add(a, b);
    printf ("\\n Val - %d", c);


int add(int a, int b)
    return (sub(a, b));


int sub(int a, int b)
    int c = a - b;
    return (c);

Now I would like to trace the function add in this program and see what functions, in turn, are invoked when add is executed.

DTrace Code:

    self->trace = 1;

    self->trace = 0;


I will my binary test (using -o flag) and I will be exacuting this
binary by ./test. In another terminal, I will execute this DTrace
script, which uses the pid provider.
Notice the probe description, the module part of it carries the name of
my program binary (this way only those function calls made in the
context of my program will be traced. If you leave the module part
blank in the probe description, all function calls (including system
calls) will be included in the trace).

Let us compile this application (I am using Sun compiler, you can use gcc as well)

kumar@sunsolaris:~/Desktop$ cc -o test myc.c

Now let us  run the DTrace script in another terminal window:

kumar@sunsolaris:~/Desktop$ dtrace -F -s myfunc.d `pgrep test` add

 And here is the output:

As you can see, the script tells me that add function calls sub. Just imagine your C application having 1000 functions, and the simplicity that you can bring to the whole debugging process if you can understand the flow with this ease.

Wednesday Feb 10, 2010

Fast Track to OpenSolaris minibook - Now available for download

EDIT: If you are having troubles downloading the PDF of the book from Scribd, you can download it from my personal server (follow this link).

Last month was pretty happening. I, in capacity of being leader for Mumbai OpenSolaris User Group (, spent last month working with the editorial team of Digit Magazine ( to come up with a minibook on OpenSolaris. Even though we got a very little time to finish a massive project like this, but with help of everyone, finally we could finish it in time and on the first day of this month (1/FEB/2010) 100,000 copies of the book- "Fast Track to OpenSolaris", along with a OpenSolaris CD with each copy, hit the newsstands all across the country! It took me 10 days to finish the book and it was the first time that I found myself writing a book of any sort.

Now Fast Track to OpenSolaris is available for download:

Fast Track to OpenSolaris

Digit and Mumbai OSUG are jointly running a online Quiz competition (OpenSolaris Geek Hunt) at  Participate in this quiz to test your OpenSolaris skills and even win a Acer Aspire One Netbook, 1 TB or 500 GB external hard disk and many more OpenSolaris goodies!

OpenSolaris Geek Hunt

I hope you all like the quiz and enjoy the book. Please feel free to drop me a line if you find any errors in the book. Also, please note that I wrote this book as a OpenSolaris community member and not as a Sun Microsystems Inc employee. So all the views and opinion and facts mentioned in this book are essentially mine and Sun should not be held responsible for mistakes and errors, if any! Now go ahead and geek out with OpenSolaris!

Friday Dec 11, 2009

Create a ZFS Storage pool on a Pen drive / USB Stick in OpenSolaris

You can do some crazy stuff with OpenSolaris and ZFS! Here is my crazy deal. Lets create a ZFS storage pool such that we have 2 Pen drives of 2 GB each and lets use it to save on big file of 3.5 GB (read store a nice DVD quality movie on two 2GB pendrives :P )

You will need two things for this:

1. Install OpenSolaris

2. Get two or more 2 GB (or of whatever size) pen drives / USB sticks (I do not have  2 pen drives with me so I will use one pen drive and one file to act like a pen drive. Process remains the same)

Step 1: Find out the device ID of your pen drive:

root@opensolaris:~# format -e
Searching for disks...done

       0. c7d0 <DEFAULT cyl 3271 alt 2 hd 255 sec 63>
       1. c9t0d0 <Generic-Flash Disk-8.07-1.87GB>
Specify disk (enter its number):

In this case, the disk 0 is my hard disk, and 1 with device ID c9t0d0 is the USB stick. If I had two pen drives I would have had one more entry. However, I am going to create a new file of 2 GB under the name disk1 and use it.

Step 2: Create a new zpool on these devices. If you are using many multiple pen drives then instead of /root/disk2 use the device ID of the devices.

root@opensolaris:~# zpool create randomZpool c9t0d0 /root/disk2
root@opensolaris:~# zpool status -v
  pool: randomZpool
 state: ONLINE
 scrub: none requested

    randomZpool    ONLINE       0     0     0
      c9t0d0       ONLINE       0     0     0
      /root/disk2  ONLINE       0     0     0

errors: No known data errors

  pool: rpool
 state: ONLINE
 scrub: none requested

    rpool       ONLINE       0     0     0
      c7d0s0    ONLINE       0     0     0

errors: No known data errors

 Step 3: Create a ZFS filesystem on the storage pool

root@opensolaris:~# zfs list | grep randomZpool
randomZpool               101K  3.77G    19K  /randomZpool
randomZpool/dummy          19K  3.77G    19K  /randomZpool/dummy

Now you can see, that I have a ZFS filesystem of 3.77GB under the name randomZpool/dummy. Now let us define a mount point for the same.

Step 4: Define a mountpoint for the ZFS Filesystem.

root@opensolaris:~# zfs set mountpoint=/export/movies randomZpool/dummy
root@opensolaris:~# df -h /export/movies
Filesystem            Size  Used Avail Use% Mounted on
randomZpool/dummy     3.8G   19K  3.8G   1% /export/movies

 Now you have the backup of your 3.5 GB movie file on two pen drives :)

How you can use this small arrangement to do much bigger (and crazier) stuff is limited only by your creativity ;)

Happy hacking to all!

Sunday Dec 06, 2009

FOSS.IN 09 - Abhishek was here!

FOSS.IN 2009 was pinnacle of geekfests and it was after a long time that I saw so many open source fanatics under one roof!! Thanks to Bangalore OpenSolaris User Group (specially Sriram Narayanan and Anil Gulecha) OpenSolaris platform was successful beyond expectations in making hackers heads turn!

Sriram explaining stuff about OpenSolaris to interested participants of FOSS.IN

Everyone was so impressed with our demos based on ZFS, DTrace and Crossbow that I literally lost the count of number of times I heard phrases like "That is so cooool!" Even though we managed to keep just a table and two laptops (one was Sriram's personal laptop and the other one was mine :P ) people kept flocking to us while we demonstrated our simple hacks on OpenSolaris 2009.06.

On the last day, thanks to Joe George from Sun IEC, Bangalore, we managed to get some OpenSolaris T-Shirts and it was a riot! We had around 50 T-shirts and we had kept a condition that anyone who wants a T-Shirt would have to compete an assignment based on either ZFS, DTrace, Crossbow or Zones and we gave different sets of assignments to every participants. People were allowed to use man pages and google and it became such an interesting game that we had a long queue of people waiting for their turn to try their hands out at it.Within 5 hours all our T-Shirts were gone!

Me giving our first winner the opensolaris t-shirt

Me giving a OpenSolaris T-Shirt to our first winner

The most interesting bit happened when one of the participants (who said that he is feeling really out of place because he was very new to the world of computers) walked into our booth and asked if OpenSolaris has got something to do with solar energy. After I had a 15 mins chat, he got so interested and excited that he said even he would like to try his hand at OpenSolaris and by the end of the day, he won a T-Shirt by completing his assignment of creating a ZFS snapshot, deleting the Download folder and rolling back!

One guy trying out his assignment to win Opensolaris t-shirt while others listen to Sriram

One of the participants using my laptop to complete his assignments while others listen are taking their assignments from Sriram

Keynote from Philip Tellis of Yahoo! in FOSS.IN 

A few GNU skeptics who walked to us commenting on the CDDL were sent back satisfied by examples like Belenix and Nexenta. Over all, it was a great event and we learned many new lessons too! I hope hackers who drool on cool technologies will continue to fall in love with OpenSolaris!

Shining new Sun Cluster 3.2 11/09 is now available

Sun Cluster is one of the best things Solaris has! Cluster engineering group just came out with the new version of Sun Cluster 3.2 11/09 which is now available! Some of the exciting new development Solaris Cluster 3.2 11/09 has is tons of improvements in infrastructure features, some new features in Scalable services and of course better support for MySQL replication in Geographic edition. New deployment options for Oracle DB and RAC is also a feature to look out for in this release.

 Read more about it on Sun Cluster Oasis -

Tuesday Apr 29, 2008

Terri's Tete a Tete with Dr. Hahn and Tim Cramer

OpenSolaris is creating lots of buzz everywhere! With so many people egarly waiting to know whats new with OpenSolaris, a small talk from the "inside men" was very much needed! Thanks to Terri Wischmann, who spoke to Dr. Stephen Hahn and Tim Cramer about whats happening at the OpenSolaris camp and got us an exciting inside scoop on it.

Both Dr. Hahn and Tim Cramer are no less than a celebrity in OpenSolaris community and now that Terri gets to interview them one on one and enjoy the OpenSolaris Bash at the CommunityOne, don't you agree that I have enough reason to feel jealous of her!!

Anyways... if you find yourself exactly in my position where you are missing CommunityOne because you can not be there in San Francisco on May 5th, then do what I am doing to be a part of celebration... Read Terri's Blog, hook on to the CommunityOne site, grab a copy of OpenSolaris latest release candidate and pray to god that you get to be there for the next CommunityOne!! :-)

Wednesday Apr 23, 2008

Much is happening on May 5th!!

May 5th is going to be such a happening date that you might get dazzled! First and foremost, of course, its because May 5th is Birthday of OpenSolaris!! But then OpenSolaris is coming out on the occasion of the CommunityOne which itself is one of the most important events in the OpenSource Calender! 

There is one place where you can catch up with everything which is going to happen on May 5th. Thanks to Terri Wischmann, who is soon going to write all about what is happening on May 5th and beyond... giving us the plug-point to all the buzz of May 5th. Visit her blog 'The Open Waters' where she will be updating (rather unleashing) everything about May 5th. So don't miss out on all the excitement and make sure you keep yourself updated on the most happening day of this year which would be... no points for guessing.. MAY 5th!

Its gonna be a festive day on May 5th!

May 5th is the most sought after day in this calender year! And do I need to tell you why is that?? Its Birthday of Opensolaris!! On May 5th, the much awaited Project Indiana will mature and would come out in open as the brand new OPEN SOLARIS!!

Me along with my folks at GLOSS are going to celebrate this in a big way!! We have long plans for this day... Even though we will be having exams going on (needless to say, \*exams\* are the most dreaded \*things\* in a student's life :-P) we have planned to celebrate the Opensolaris B'day in style! The agenda for the day is:

  • Put up 5 Laptops with placards which would read "Convert your CD
    into Souvenir"
  • Cut a Birthday Cake in name of Opensolaris 
  • Do a small presentation on new features of Opensolaris
  • Have Lunch together
  • Do a installation demo
  • At the end, invite students to come up and give a talk on what they
    feel about future of Opensolaris
  • Promise "Opensolaris" that we will work towards "Rearing it To
    become the Best Desktop OS"!
Everyone at GLOSS is excited about this! After all.. OpenSolaris is "OUR OWN" Operating System and we want to be nice "Uncle GLOSS" and we want to take good care of the new kid!!! :) Its all in the family you see :)


I am Abhishek and I work for Sun/Oracle! In this blog, I share my interest in systems, solaris, linux and other technologies :-)


« April 2014