When I was reading an interesting blog about prstat behavior, I read about projects in Solaris. After reading about projects, I came to know that they are mainly used for controlling resources used by process[es]. Suppose, if we want to control the CPU/disc/memory usages, then we create a project [using projadd, projmod] and in /etc/project, we have to modify resource control field. Then we can create/attach processes into this project [using newtask]. Once the process is created/attached in this project, all resource constraints will apply to that process. Suppose if the resource constraint name is process.max-file-descriptor and the value is 10, a process in that project can not have more that 10 file descriptors. Moreover, using prstat -J command, we can clearly see the resources used for projects. I felt this is a very good way to control resources used by specific processes, but I don’t know if the similar thing exists on other platforms like Linux. Some good links that I have referred are here & here.
Make vim to remember file cursor position : solaris
February 21, 2007 at 6:21 pm (IDE, solaris)
Some time back, I installed vim in Solaris after taking the package from sunfreeware site. I have configured colors also. But one main annoying thing is that it does not remember the cursor position of the same file opened previously. i.e. When I open a file and move to line no:1000 , quit and open the same file again, cursor will be placed at line no:0. After reading a few web pages [not with much help], I have read vim documentation [:help viminfo]. There I found that by typing `” [backquote & doublequote], cursor will be moved to the line where we left last time. But I dont want to type `” everytime I open a file. I want to automate this. After searching for `” pattern in my Linux vim config files, I found the following code snippet in /etc/vimrc in my SuSE.
if has(“autocmd”)
autocmd BufReadPost *
\ if line(“‘\”") > 0 && line(“‘\”") <= line(“$”) |
\ exe “normal g`\”" |
\ endif
endif
After I have added the above code in my solaris .vimrc, things began working properly. Actually I have sent a mail to vim mailing list and started working on this problem. When I checked my mails after solving the problem, I saw few good replies from vim mailing list pointing to these links tip#80 , yahoo_groups .
nm & dump in AIX
January 30, 2007 at 12:54 pm (AIX, C/C++)
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.
Useful non-crypto modules in OpenSSL crypto library
December 26, 2006 at 2:58 am (C/C++)
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??
November 23, 2006 at 2:42 pm (C/C++, Linux, solaris)
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
November 13, 2006 at 2:20 pm (C/C++, solaris)
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?
November 11, 2006 at 10:16 am (C/C++, Linux)
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
October 28, 2006 at 1:34 pm (Linux, solaris)
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
October 23, 2006 at 12:29 pm (C/C++, Linux)
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
October 13, 2006 at 11:52 am (C/C++, Linux, solaris)
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.