nm & dump in AIX

In Solaris, Linux and HP-UX, I have used nm to see exported symbols in my shared library. So I have started using the same command in AIX also. But, in the command output, all my exported symbols are prefixed with dot [.]. Moreover, there are lot many symbols in ” T ” [Global Text] section of the output. Even, basic string functions like strlen, memcpy are also under ” T “. I thought there might be a bug in my makefiles and tried to fix them. After debugging this for 6 hours, I thought of trying other commands also. Finally I have tried dump [dump -HTv] and I got the output exactly what I have expected. Functions like strlen,memcpy are under undef section. So, in AIX, it is better to use dump for seeing symbols in the shared library. As I dont have free time, I have not investigated why nm was showing symbols in a different manner.

Advertisements

Useful non-crypto modules in OpenSSL crypto library

In C/C++, for secure networking/cryptography, I feel OpenSSL is the best choice. Other than cryptography/secure networking, there are many other good modules in OpenSSL that can be used in various applications. Few of them are BIO, BN, ASN1. BIO is an abstraction layer over I/O. By using BIO, we can do Socket I/O, File I/O, in-memory I/O etc. Moreover, BIO can be used as a filter also; ex: we write plain text to BIO, we get the encrypted text when reading from BIO. BN is a BIGNUM library. It is a library for handling very big numbers. For creating very big primes, arithemetic operations on very big numbers, we can use BN module. ASN1 is the abstract syntax notation module. Using this module, we can use many ASN1 functions. As I know, these 3 are very good non-crypto modules. If you consider cryptography or ssl, OpenSSL has everything. Moreover, it comes with a very flexible license.

Gmake going in loops??

From many days, I have observed that our build sometimes goes into recursive loops and does not get out of the loop for many hours/days. I found the reason for this problem. It is about the modification time of makefile & C/C++ files. I use tar.gz which preserve file modification times and transfer that tar.gz to other systems. If those systems have different timezone, it might happen that the makefile/c/c++ files are modified in the future. Since gmake considers file modification times when running makefile, it goes in recursive loop. Simple solution is to touch the makefile/c/c++ files with future modification time using touch command.

Solaris:Make a shared library which can be executable

After many days, I got some free time to continue the work that I have done here.
In solaris, I know, by writing asm code, we can make a shared library work as an executable. So, I thought of writing a simple tool which can make necessary changes to shared library to act as an executable. First I have read cc man page to find out how to set an entry point. I found that we have to pass -e option to linker. So, I have written a test program [download] and compiled it to create a shared library. When executed it [$./libtest.so] , it gave segmentation fault and failed. It was setting entry point correctly. I have used elfdump to check whether it is setting entry point properly or not. We can see entry point with elfdump -p <executable> [e_entry value]. We can search for that entry point in ‘elfdump -s <executable> | grep <e_entry value>’. For reading the section headers, I have used the example given in elf man page. Then, I have executed that section header print program on that shared library. Then I have observed that the created shared library was not having ‘.interp’ section. ‘.interp’ contains interpreter value. After loading the file, system handles the control to interpreter, if ‘.interp’ section exists. So, at this stage, I thought of writing a program using elf/gelf library to add the interp section. But, when I read the man pages again, I saw -I option to linker to set the interp value. [By default, for executables, .interp section is created and /usr/lib/ld.so value is set. By default, For shared libraries, it wont create any interp section]. So, I have used -I option to compile my program into library. Now, entry point is correct and .interp section is also there. When executed my shared library [$./libtest.so], it executed normally without giving segmentation fault.

One more problem here is to get the command line arguments. I think, we have to write some assembly code to do that. If you want that feature, you can refer to this and this. you can download my programs here.

Do we need __PRETTY_FUNCTION__ like macros in other compilers?

You might know that GCC in addition to __func__ macro, it supports __FUNCTION__ and __PRETTY_FUNCTION__. __func__ is the predefined-macro which contains the function name in a string. It is defined in C specs, But, __FUNCTION__ and __PRETY_FUNCTION__ are extensions by GCC. __FUNCTION__ macro is same as __func__. __PRETTY_FUNCTION__ macro gives the full function declaration including parameters and return values. I tried to look for other alternatives on SUNWspro compilers and HP-C/C++ but could not find any. Then I questioned myself ‘do we need this macro while we can always get sufficient debug information with __LINE__ , __FILE__, __func__??’.  Answer is ‘No’. We can always get sufficient debug information with the macros defined in specs. More over, if we use GCC extensions in code, portability to other platforms is a big pain. BTW, VS.net compiler also has a similar pre-defined macro for getting decorated function name.

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/libmemleaks.so ./test.

By mistake, I was explicitly loading /lib/libmemusage.so instead of libc.so. Initially I got so many compilation errors in my libmemleaks.so 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 memusage.sh file. I have copied that into my linux machine and modified a line and executed it [sh memusage.sh ./test]. Then, I got the same multi colored table. Then I came to know, this memusage.so library is written for getting memory stats. memusage.sh, internally executes like LD_PRELOAD= /lib/libmemusage.so <prog.name>. 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 memusage.sh program. Using memusage.sh, we can find memory stats of any program [including java programs]. Execute sh memusage.sh –help for command help.

Normally, linux distributions dont distribute memusage.sh. 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.

« Older entries Newer entries »