Mandalika's scratchpad [ Work blog @Oracle | My Music Compositions ]

Old Posts: 09.04  10.04  11.04  12.04  01.05  02.05  03.05  04.05  05.05  06.05  07.05  08.05  09.05  10.05  11.05  12.05  01.06  02.06  03.06  04.06  05.06  06.06  07.06  08.06  09.06  10.06  11.06  12.06  01.07  02.07  03.07  04.07  05.07  06.07  08.07  09.07  10.07  11.07  12.07  01.08  02.08  03.08  04.08  05.08  06.08  07.08  08.08  09.08  10.08  11.08  12.08  01.09  02.09  03.09  04.09  05.09  06.09  07.09  08.09  09.09  10.09  11.09  12.09  01.10  02.10  03.10  04.10  05.10  06.10  07.10  08.10  09.10  10.10  11.10  12.10  01.11  02.11  03.11  04.11  05.11  07.11  08.11  09.11  10.11  11.11  12.11  01.12  02.12  03.12  04.12  05.12  06.12  07.12  08.12  09.12  10.12  11.12  12.12  01.13  02.13  03.13  04.13  05.13  06.13  07.13  08.13  09.13  10.13  11.13  12.13  01.14  02.14  03.14  04.14  05.14  06.14  07.14  09.14  10.14  11.14  12.14  01.15  02.15  03.15  04.15  06.15  09.15  12.15  01.16  03.16  04.16  05.16  06.16  07.16  08.16  09.16  12.16  01.17  02.17  03.17  04.17  06.17  07.17  08.17  09.17  10.17  12.17  01.18  02.18  03.18  04.18  05.18  06.18  07.18  08.18  09.18  11.18  12.18  01.19  02.19  05.19  06.19  08.19  10.19  11.19  05.20  10.20  11.20  12.20 

Wednesday, March 23, 2005
Mixed-Programming and External Linkage

article was published on Sun Developer's web site at:

Apparently I got the idea to submit this article from my blog post:

Sunday, March 13, 2005
Sun Studio: Investigating memory leaks with Collector/Analyzer

Sun Studio compiler collection bundles a set of tools for collecting and analyzing performance data from an application. Collector & Analyzer, a pair of tools from Sun Studio suite that we use to collect and analyze the performance data. Both tools can be used from the command line or from a graphical user interface.

These two tools help answering the following questions:But this blog post mainly concentrates on finding memory leaks from the following simple C program

% more mem.c
#include <stdlib.h>
#include <stdio.h>

void allocate() {
int *x;
char *y;

x = (int *) malloc(sizeof(int) * 100);
y = (char *) malloc (sizeof(char) * 200);

printf("\nAddress of x = %u, y = %u", x, y);

x = (int *) malloc (sizeof(int) * 25);
y = (char *) malloc (sizeof(char) * 25);

printf("\nNew address of x = %u, y = %u\n", x, y);
free (y);

int main() {
return (0);

From the code, it is clear that the program is requesting memory allocations four times (a total of 725 bytes); but releasing only 25 bytes from virtual memory. The following steps explain how to find this 700 byte leak with Collector/Analyzer.

  1. Prepare the program for data collection

    To see source code in annotated source and disassembly, and source lines in the line analysis, the source file must be compiled with -g compiler option to generate debug symbol information
    % CC -g -o mem mem.c

  2. Collect the data

    We can collect run-time data that is useful for later analysis by running the executable under collect utility. To see the entire list of arguments that collect accepts, type collect without any arguments on the command line. Since we were interested in memory leaks, we have to turn the heap tracing on with -H option. (By default, heap tracing is off). Also we have to supply an experiment name to hold the data with -o option
    % which collect

    % collect -H on -o mem
    Creating experiment database ...

    Address of x = 134743584, y = 134743992
    New address of x = 134744200, y = 134614672

    It creates the experiment and archives the binary files describing each load object referenced in the loadobjects file
  3. Finally invoke the performance analyzer, er_print (command line tool) or analyzer (graphical tool)
    % er_print
    (er_print) source allocate
    Source file: ./mem.c
    Object file: ./mem
    Load Object: ./mem

    Incl. Incl. Excl. Incl.
    Bytes Leaks User CPU User CPU
    Leaked sec. sec.
    1. #include <stdlib.h>
    2. #include <stdio.h>
    0 0 0. 0. 4. void allocate() {

    0 0 0. 0. 5. int *x;
    0 0 0. 0. 6. char *y;
    400 1 0. 0. 8. x = (int *) malloc(sizeof(int) * 100);
    200 1 0. 0. 9. y = (char *) malloc (sizeof(char) * 200);
    0 0 0. 0. 11. printf("\nAddress of x = %u, y = %u", x, y);
    100 1 0. 0. 13. x = (int *) malloc (sizeof(int) * 25);
    0 0 0. 0. 14. y = (char *) malloc (sizeof(char) * 25);
    0 0 0. 0. 16. printf("\nNew address of x = %u, y = %u\n", x, y);
    0 0 0. 0. 17. free (y);
    0 0 0. 0. 18. }
    0 0 0. 0. 20. int main() {

    ## 700 3 0. 0. 21. allocate();
    0 0 0. 0. 22. return (0);
    0 0 0. 0. 23. }

    source allocate shows the annotated source with line numbers and number of bytes leaked from each line since we collected the data with heap tracing on (otherwise it shows the total CPU time spent in each line, by default)

    From the annotated source, we can see that 700 bytes were leaked after the execution of line:
    ## 700     3      0.        0.              21.         allocate();
    . And from the source of allocate(), the following lines were the culprits:

    400 1 0. 0. 8. x = (int *) malloc(sizeof(int) * 100);
    200 1 0. 0. 9. y = (char *) malloc (sizeof(char) * 200);
    100 1 0. 0. 13. x = (int *) malloc (sizeof(int) * 25);

    0 0 0. 0. 14. y = (char *) malloc (sizeof(char) * 25);
    statement was clean, since we have 0 0 0. 0. 17. free (y); at the end of function allocate().

    We can also get much useful information with the help of leaks and allocs commands that relate to memory allocations and deallocations. Brief description of those commands are as follows:
    • leaks

      Display a list of memory leaks, aggregated by common call stack. Each entry presents the total number of leaks and the total bytes leaked for the given call stack. The list is sorted by the number of bytes leaked.

    • allocs

      Display a list of memory allocations, aggregated by common call stack. Each entry presents the number of allocations and the total bytes allocated for the given call stack. The list is sorted by the number of bytes allocated.
Suggested Reading:

Saturday, March 12, 2005
Solaris 10: Installing MPlayer

MPlayer - Introduction:
MPlayer is a movie player for UNIX/Linux and plays most MPEG, VOB, AVI, OGG/OGM, ASF/WMA/WMV, QT/MOV/MP4, RM etc., files, supported by many native, XAnim, RealPlayer, and Win32 DLL codecs. You can watch VideoCD, SVCD, DVD, RealMedia, and MPEG-4 (DivX) movies too

GNU tools: gmake (make), gcc (compiler), gas (assembler), libiconv etc.,

  1. On Solaris SPARC, we need the GNU C/C++ Compiler; it does not matter if GNU C/C++ compiler is configured with or without the GNU assembler
  2. On Solaris x86, we need the GNU assembler and the GNU C/C++ compiler, configured to use the GNU assembler. The MPlayer code on the x86 platform makes heavy use of MMX, SSE and 3DNOW! instructions that cannot be compiled using Sun's assembler /usr/ccs/bin/as
  3. Error message from configure on a Solaris x86 system using GCC without GNU assembler
    % configure
    Checking assembler (/usr/ccs/bin/as) ... , failed
    Please upgrade(downgrade) binutils to 2.10.1...
  4. Typical error we get when building with a GNU C compiler that does not use GNU assembler (gas):

    % gmake
    gcc -c -Iloader -Ilibvo -O4 -march=i686 -mcpu=i686 -pipe -ffast-math
    -fomit-frame-pointer -I/usr/local/include -o mplayer.o mplayer.c
    Assembler: mplayer.c
    "(stdin)", line 3567 : Illegal mnemonic
    "(stdin)", line 3567 : Syntax error
    ... more "Illegal mnemonic" and "Syntax error" errors ...

Building MPlayer
  1. Download the latest source from and extract the files
  2. Check the PATH and make sure that GNU tools are available in PATH
  3. ./configure --prefix=<installation directory for mplayer> --as=gas in the directory where you extracted the files. This is where you see the file "configure" and "src" directory

    This step creates a bunch of Makefile(s) in different folders with the information that we supplied during "configure"
  4. gmake

    It compiles the code and creates the executables and shared objects or static libraries (depending on options supplied to configure)
  5. Finally, gmake install

    This command installs the mplayer, dependencies in appropriate places. You may have to use the Sun audio driver with the -ao sun option for sound

    eg., Playing an mpeg-1 file called Matrix.mpg
    mplayer -ao sun Matrix.mpg

Suggested Reading:
  1. MPlayer's documentation &
  2. MPlayer documentation for Solaris port:

Sunday, March 06, 2005
Solaris: Setting up a DHCP client

If the machine is connected to the network during the installation of the OS (Solaris in this case), the operating system takes care of setting up the dhcp (Dynamic Host Configuration Protocol) client for us. But if the machine is not hooked up to a network during the installation, we may have to set it up on our own once the machine is ready to use the network services. Infact it is a very simple process to setup and run dhcp client, which in turn takes care of getting the dynamic IP, DNS etc., from a DHCP server

All we need to do is to create couple of files for the ethernet interface.
  1. To get the interface name for ethernet, type dmesg | grep ether. Let's assume that it is elxl0
  2. Create an empty file called /etc/hostname.<ether interface> eg., touch /etc/hostname.elxl0

    Creating this file ensures that the interface gets plumbed. By plumbing, it implements the TCP/IP stack ie., the OS sets up the streams needed for TCP/IP to use the device and makes it ready for the DHCP software to do its stuff

    Behind the scenes: During the boot process, the OS reads all the /etc/hostname.* files and plumbs the devices. Once plumbed, the devices are configured by reading the /etc/hosts and the /etc/netmasks file
  3. Next step is to create /etc/dhcp.<ether interface>. eg., touch /etc/dhcp.elxl0
    This file can be empty if you want to accept the defaults, but may also contain one or both of the following directives:
    • wait time, and
    • primary

    By default, ifconfig will wait for 30 sec for the DHCP server to respond and then the boot process continues, while the interface gets configured in the background. Specifying the wait directive tells ifconfig not to return until the DHCP has responded. The primary directive indicates to ifconfig that the current interface is the primary one, if you have more than one interface under DHCP control. If you have only one interface under DHCP control, then it is automatically the primary one; so primary is redundant, although it's permissible
It is important to note that the dhcp client daemon, dhcpagent will be started only if /etc/dhcp.interface file exists. With these files in place, subsequent reboots will place the ethernet interface under DHCP control and the machine will be ready to access network services

Acknowledgements & suggested reading:
  1. Setting up a Solaris DHCP client by Rich Teer
  2. Configuring network interface cards by Lance Spitznet
  3. Solaris DHCP client at Sun Solaris documentation web site

Friday, March 04, 2005
UNIX/Linux: Activating Comcast High Speed Internet Account

Fact: Comcast doesn't support any OS except Windows & Mac. It doesn't mean that Unix/Linux users can not access internet with Comcast services; but it means Comcast refuses to trouble shoot any issues on *nix platform ie., *nix users are on their own to fix any of the issues

In general, Comcast technician leaves a CD-ROM to activate the Comcast account, once the installation is done. The activation wizard of the CD takes care of registering the modem, activating the account for us. Unfortunately it works only on Windows and Mac platforms

However Comcast is kind enough to provide web interface for activating the account (some people refer it as "registering the modem"; Comcast refers it as "Activating the ISP"). Comcast uses DHCP (Dynamic Host Configuration Protocol) for IP assignment. So, if we have a DHCP client running on the machine, it contacts the Comcast's DHCP server(s) and obtains a valid IP, DNS info etc., ie., As soon as the cable modem installation is done, our machine will be on the network (to verify, simply try arp <any well known hostname>); but we can't actually access any of the public web sites due to the pending registration. Since we are in the network of Comcast, we will be able to access few web sites of Comcast, with the help of Comcast's proxy server. URLs for Comcast's primary proxy servers are as follows:

  1. serves Oregon & Washington regions
  2. serves California except Sacramento
  3. serves Texas
  4. serves Utah, Colorado, Wyoming, Montana & Sacramento (CA)
  5. serves Indiana, Illinois & Michigan
  6. serves Ohio, Connecticut & Pennsylvania

Activation steps are as follows:

  1. Configure your browser to use the Comcast's proxy server for your geographic region (see above for the list)
    1. HTTP & SSL proxies must be configured to use the proxy server of your region
    2. 8000 is the port for both HTTP & SSL proxies
    3. Fill out "No Proxy for:" field with the name of the proxy server for your region without the string "https://sas." from the actual URL

  2. Now try to access the proxy server URL for your region. Hopefully you should be able to see a page with two fields: Account Number and Registration Code. Account Number is the Subscriber Number and Registration Code is the number (ID) that you may find under "Service Name and Address" field of the invoice you received from Comcast.
  3. As you proceed to the next step, you will be also be prompted to setup your Comcast e-mail account
  4. Once the device (modem) is registered, the web page shows a link to restart the cable modem. Go ahead and restart the modem. If you are using a router, you need to restart the modem as well. To restart the router: turn the power off, wait for a min and then turn it back on.
  5. Now your account is activated; but you still can not access public web sites due to Comcast's proxy server configuration. Since we do not need it anymore, go back to web browser's proxy configuration window and choose "Direct connection to the internet"
  6. If you can not access internet even at this point, try to restart the network or the machine depending on your expertise level. Even if it didn't help, try calling Comcast support center and find out the the proxy server, that you need to use in your region

The above steps are equally applicable for other platforms as well; but Unix, Linux were explicitly added to the title for Google feed

Thanks to:
James Dean Palmer

Thursday, March 03, 2005
Obfuscated "C" code

Found this interesting piece of C code somewhere on internet. Of course it compiles and prints out 3.141 on console; And it is not so hard to guess how the code in F_OO() gets executed
Govinda% more fun.c
#include <stdio.h>

#define _ F-->00 || F-OO--;
long F=00,OO=00;

void F_OO() {

printf("%1.3f\n", 4.*-F/OO/OO);

Govinda % cc -o fun fun.c
Govinda % ./fun


This page is powered by Blogger. Isn't yours?