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:


pid$1:test:$2:entry
{
    self->trace = 1;
}

pid$1::$2:return
/self->trace/
{
    self->trace = 0;
}

pid$1:::entry,
pid$1:::return
/self->trace/
{
}



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.



Tuesday Feb 16, 2010

What Sun means to me...

This might very well be my last personal blog entry in this blogroll... But I got some stuff to share which I would love to talk about... I saw many people in last few days who spoke about how much they love Sun as a company... How much the respect the people who worked with them here and how they lived there whole adult life working for Sun and still love every day of it.. It did not take me long to fall in love with Sun as well.. When I joined Sun in 2007 as the Campus Ambassador for my university, I was still a student... and very fresh to almost everything which was even remotely corporate... But then not even on the first day of my work anyone made me feel that I am novice.. In past three years, I have worked on many assignments, most of them were in areas like Marketing, and Administration, in which I have no formal training.. But not even once I felt I cannot do it... People with whom I worked, they had some magical power in them which could infuse confidence... There is so much to say about everyone that I worked with that I wouldn't know where to stop. So I put together some of the pictures I took over during these years.. and I have put them in a video with my favorite song (My Brilliant Feat by Colin Hay) in the background... Those who shared these moments with me, will know the context and the beauty and passion that is attached with it...



I may not have worked for Sun for decades, but I still love this company. And I will love it forever. I am glad that it is the first company I ever worked for and that wherever Sun guys would go, they will bring positive energy, passion and daredevil-like attitude in abundance.! There is no stopping them! Ever!

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 (http://hub.opensolaris.org/bin/view/User+Group+mmosug/), spent last month working with the editorial team of Digit Magazine (http://www.thinkdigit.com) 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: http://www.scribd.com/doc/26659478/Fast-Track-to-OpenSolaris

Fast Track to OpenSolaris

Digit and Mumbai OSUG are jointly running a online Quiz competition (OpenSolaris Geek Hunt) at http://www.thinkdigit.com/opensolaris  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!

Sunday Dec 20, 2009

What happens under the hood when you execute your C Programs?

I was a little curious to find out how my program gets executed. In order to find that out, I decided to write a simple c program which is here:



 #include <stdio.h>
int main()
{
    sleep(30);
    printf("Just testing an application!");
}



I have kept the sleep(30) in the beginning of the program so as to give me time to execute the DTrace script, about which I will be talking a little later.


Now i complied it with Sun Compiler and GCC



kumar@myosbox:~/Desktop/Demos$ suncc -o sunCCOutput simplec.c


kumar@myosbox:~/Desktop/Demos$ gcc -o gccOutput simplec.c



Now I wrote a simple DTrace script using the pid provider in order to figure out how printf is implemented (execution detail - functions which get invoked when printf function is called) in these compilers. The dtrace script is below:



pid$1::$2:entry
{
        self->trace = 1;
}

pid$1::$2:return
/self->trace/
{
        self->trace = 0;
}

pid$1:::entry,
pid$1:::return
/self->trace/
{
}



Now it was the time to execute this script and compare the outputs. I am going to redirect the output of this script into two text files (sunCCOutput.txt and gccOutput.txt).



kumar@myosbox:~/Desktop/Demos$ ./sunCCOutput 
kumar@myosbox:~/Desktop/Demos$ pfexec dtrace -F -s pid.d `pgrep sunCCOutput` printf > SunCCOutput.txt
dtrace: script 'pid.d' matched 6712 probes
kumar@myosbox:~/Desktop/Demos$ ./gccOutput
kumar@myosbox:~/Desktop/Demos$ pfexec dtrace -F -s pid.d `pgrep gccOutput` printf > gccOutput.txt
dtrace: script 'pid.d' matched 6714 probes



Now, the content of the file SunCCOutput.txt is as follows:



CPU FUNCTION                                 
1 -> printf
1 -> _setorientation
1 <- _setorientation
1 -> _ndoprnt
1 -> _findbuf
1 -> isatty
1 -> ___errno
1 <- ___errno
1 -> ioctl
1 <- ioctl
1 <- isatty
1 -> _setbufend
1 -> getxfdat
1 <- getxfdat
1 -> isseekable
1 -> ___errno
1 <- ___errno
1 -> fstat64
1 <- fstat64
1 -> ___errno
1 <- ___errno
1 <- isseekable
1 <- _setbufend
1 <- _findbuf
1 -> _realbufend
1 -> getxfdat
1 <- getxfdat
1 <- _realbufend
1 -> memcpy
1 <- memmove
1 <- memcpy
1 -> memchr
1 <- memchr
1 -> ferror
1 <- ferror
1 <- _ndoprnt
1 -> ferror
1 <- ferror


Contents of the file gccOutput.txt is as follows:



CPU FUNCTION                                 
1 -> printf
1 -> _setorientation
1 <- _setorientation
1 -> _ndoprnt
1 -> _findbuf
1 -> isatty
1 -> ___errno
1 <- ___errno
1 -> ioctl
1 <- ioctl
1 <- isatty
1 -> _setbufend
1 -> getxfdat
1 <- getxfdat
1 -> isseekable
1 -> ___errno
1 <- ___errno
1 -> fstat64
1 <- fstat64
1 -> ___errno
1 <- ___errno
1 <- isseekable
1 <- _setbufend
1 <- _findbuf
1 -> _realbufend
1 -> getxfdat
1 <- getxfdat
1 <- _realbufend
1 -> memcpy
1 <- memmove
1 <- memcpy
1 -> memchr
1 <- memchr
1 -> ferror
1 <- ferror
1 <- _ndoprnt
1 -> ferror
1 <- ferror

So you can see that there is no difference between gcc and suncc when if comes to implement the printf function. But its is so interesting to see that there are 19 function calls made everytime I use a printf in my c program! WOW! ;) I wonder if I could know it this easily if there was no Dtrace!

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


AVAILABLE DISK SELECTIONS:
       0. c7d0 <DEFAULT cyl 3271 alt 2 hd 255 sec 63>
          /pci@0,0/pci-ide@1,1/ide@0/cmdk@0,0
       1. c9t0d0 <Generic-Flash Disk-8.07-1.87GB>
          /pci@0,0/pci8086,265c@b/storage@1/disk@0,0
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
config:

    NAME           STATE     READ WRITE CKSUM
    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
config:

    NAME        STATE     READ WRITE CKSUM
    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


Foss.in


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 - http://blogs.sun.com/SC/entry/solaris_cluster_3_2_11


Wednesday Jul 08, 2009

OpenSolaris 2009.06 exclusive booklet in Indian Press

OpenSolaris has been a hot topic in India for quite some time now and in the month of July its going to be everywhere!! In the ongoing effort to take OpenSolaris to every hand in India, we have worked with Indian media and bundled OpenSolaris 2009.06 Live CD with 2 major IT magazines! Going even ahead of it we have created a 16 page exclusive booklet for OpenSolaris 2009.06 which has been bundled with Chip Magazine. So more than 120,000 readers in India would have a copy of something to give them a head start on OpenSolaris and a Live CD to play around with. :) A total of more than 250,000 Live Disks of OpenSolaris has hit Indian newsstands this month and I hope it would give a good push to our community in India.




Feel free to download the OpenSolaris booklet here and distribute if you want. It features new features of OpenSolaris 2009.06, Installation guide, guide to setup Web development and Java Development environment and develop an example project in each, and an interview of Glynn Foster . It also includes details on OpenSolaris communities and OSUM! Good work by the magazine team and our OpenSolaris people! I hope you enjoy reading it ;)

About

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

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today