My experience with using GNU screen

In my B.Tech, we were taught about using screen command. But at that time, I did not realise the importance of that. Because, I used to open 1 connection to server, write code/compile/execute in that terminal only. But, now I open more than 5 connections. Last week, I read a small tutorial about screen and felt the importance of it. Screen is used for terminal multiplexing. We open only one terminal connection to server. In that terminal, we can use screen to create multiple virtual windows. Since we can detach/attach screens, we can also use it as remote desktop for linux.

In linux, screen is already installed. So, I have started using it directly. In my solaris machine, it is not installed. So, I have downloaded its package from sunfreeware site and installed it. When I started screen, I was not able to use backspace on command line. I could not find any clues on net. But, when I connected to that machine using ssh, that problem was solved. After that when I opened a file using vim in screen, I was not able to use arrow keys. Colors were also not according to default vim colorscheme. I have searched on internet.. but could not find any clues. I knew, it was the problem with TERM & TERMCAP. So, I have exited screen and started it again with TERM=screen screen. Then, I could use arrowkeys and colors were also fine. But at some places, vim was not showing code properly. Again.. I wasted lot of time for searching on the net. After getting frustrated with putty and terminal types, I connected to that solaris machine from my linux box and started screen with the same command [TERM=screen screen]. Luckily, then, everything in screen was working fine. Still, I dont know why I was getting problems if I directly connect to solaris from putty and use screen.

Seeing pre-processor macros in gdb

In gdb, we can see the source code while debugging. But, if there is any macro in the source code which is defined in some other file, then we have to search for that macro outside gdb [using fgrep] and see what it expands to. In complex codes like openssl, it is even difficult to find/understand macros. When I was searching for this kind of feature in gdb, I saw this link. Then, I came to know that this feature is already implemented. After further research on this topic, I read this link. In this link, it says, if we build our source code with -g3 -gdwarf-2 , it will include all the macros in the debug information. So, I have written a test program and built it with those options. After that, in gdb, I have tried $info macro <Macro.Name>, it gave the line no# where the macro is defined and full macro definition. In addition to just seeing macros, we can use ‘macro expand’ command to see what the macro expands to.

One interesting note: I tested a small program and it worked in my SuSE8.1, GCC3.2.2, GDB5.3. I have tried to test the same thing in Fedora-core3, GCC3.4.2, GDB6.1, it did not work. I think it is the problem with GDB [though I am not sure]. GDB needs to support these debugging formats. So, if we build GDB with proper flags, I think it will work.

I read that there are better debug formats than dwarf-2, one among them is stabs. But, once again, your debugger needs to support it.

Stack traces in C/C++ programs in solaris & linux

In Java, there are direct APIs for getting stack traces. But, for C, there are no popular APIs for doing the same. So, I was just searching for this feature in linux. Then I came across this link. It provides very clean and simple interface for getting stack traces. They even gave the sample program in the end of the article, demonstrating how to use it. Note: We have to compile the program with -rdynamic flag. In solaris, I know, with dtrace tool, we can do all these tricks. But, if I want to get/use the stack trace inside the C program, dtrace doesn’t help. After searching for “stack” in /usr/include/*.h contents, I found some functions in ucontext.h, which are relevant to what I want. Then, after seeing man page of one of their functions [man printstack], it is confirmed. I have written a small program to demonstrate stack traces in C program. You can download it here. For C++ programs, we may have to use c++filt [like ./a.out | c++filt] to get the correct functions names from mangled ones.

Finding memory leaks in solaris

I have started searching for a tool like memusage library in solaris. Then, I read about umem library. I felt, this tool is useful for finding memory leaks for a running program. We have to use the umem with mdb whose interface is very difficult to use. Then, after further exploration on this topic, I have opened sunstudio gui and started debugging with ‘memory checks’ on. Then, I found out that sunstudio is internally using dbx. So, I have noted dbx commands that are used for memory leaks.

dbx is more like gdb. first, we have to build the executable [CC test.c]. then start that executable using dbx [dbx ./a.out]. set ‘memory checks option on’ [check -memuse]. run the executable [run]. If there are leaks in our application, we will get a table like the following

Total Size Num of Leaked Blocks Block Address Allocation call stack
========== ====== =========== ====================
8 1 0x80688a8 func2 < func1 < func < main

This table is saying that, it has got a memory leak of 8 bytes in main>func>func1>func2 function. From this, we can know that memory allocated in function func2 is not freed. Now, we can guess where that allocated memory can be freed.

Enjoy! Happy leak free code!!

Magic with memusage library in linux

Today, I have started writing a tool for finding memory leaks in my programs. So, I have started with defining my own memory functions like malloc, realloc, free with exactly same signature as of the original memory functions. In these functions, I explicitly load original memory libraries, call the original functions and I keep note of how many times each of them is called. So, if I wanted to find memory leaks in a program test, instead of executing it like $./test, I would execute it like $LD_PRELOAD=$PWD/ ./test.

By mistake, I was explicitly loading /lib/ instead of Initially I got so many compilation errors in my program. So, I removed lot of code and compiled it again succesfully. But, when I executed it finally, I got a table with heap/stack size and no.of times malloc/calloc/free are called. The table is shown below.memory_report

Surprised!! I was not expecting this multi-colored table. I did not write any code for printing this table. In yahoo/google, I searched and did not get any references to this problem [no documentation, problem with open source??]. Then I have used google code search and searched for “Memory usage summary” and got a link to glibc-2.2.5/malloc/memusage.c. After seeing that file, in _fini function, I understood that, they are writing the memory usage report. _fini is the function in shared library which is called, when the system is unloading the library.

After that, I have download glibc 2.2.5 source code and started looking for the clues. In the same directory as glibc-2.2.5/malloc, I found a shell script file. I have copied that into my linux machine and modified a line and executed it [sh ./test]. Then, I got the same multi colored table. Then I came to know, this library is written for getting memory stats., internally executes like LD_PRELOAD= /lib/ <>. Even I thought of writing my memory leak checking tool with the same concept and at last I have completed my program. But, my program is not as colorful as program. Using, we can find memory stats of any program [including java programs]. Execute sh –help for command help.

Normally, linux distributions dont distribute You can see the same shell script here. you can download it from here [I have modified at 2 places. Replaced @SLIBDIR@ , @BINDIR@ with /lib, /bin. To get graphs, you need to have memusagestat executable in /bin]. Finally, I am very happy that I found this utility after researching on this for 3 1/2 days.

long and long long

I am sure, in java, size of long is 64 bits and there is not any datatype of long long. So, In C /C++ also, I used to think long is 64 bit and long long is bigger than 64 bit [128 bit]. But, recently I got a doubt regarding this. So, I have written a C program and compiled it on many systems. Size of long long is always 64bits. But size of long was varying based on compiler flags. In solaris, if we give cc longsize.c, by default ILP32 bit data model used. In this case, size of long is 32 bits. But, if we give cc -xarch=v9 longsize.c, code will be compiled for 64-bit SPARC machines and LP64 data model is used. In ILP32 bit model, size of integer, long and pointer is 32 bits. In LP64 data model, size of long and pointer is 64 bits. So, independent of machines and datamodels, if we want 64 bit data type, we have to use long long.

Then I have read in some article that long long is introduced recently to support very big numbers. we use %ld in printf to print long and %lld for long long.

Update: Recently, I have learnt that, in windows, size of long in both 32 bit and 64 bit builds is same and it is 32 bits. It follows LLP64 [long long and pointer are 64 bit] data model in 64 bit builds.

Multi line greps

    In linux/solaris, we can use fgrep/grep for pattern matching in files. But one limitation with these commands is they limit their pattern matching to one line. They don’t search for the pattern in multiple lines. Then I found an open source tool, pcregrep [available as sdk also]. pcregrep provides multi-line grep. In many latest versions of linux and solaris machines, I have found this command, but they are older versions [maybe stable versions]. Older versions did not have multi-line grep functionality. So, I have downloaded its latest source and built it.

One usecase: I have function names, then I need to find out their declarations from header files. Function could be declared over many lines. This is my usecase. In this scenario, I have used pcregrep.