Monday, July 2, 2012

32/64 bit shared library with 32/64 applications

mylib.c
========
#include <stdio.h>

int print_hello()
{
printf("Hello.\n");
return 0;
}

int print_world()
{
printf("World.\n");
return 0;
}

char *get_str()
{
return "Hello World";
}

$> xlc -c mylib.c -o mylib.o [-q64]
$> xlc -qmkshrobj -qexpfile=mylib.exp mylib.o [-q64]
$> cat mylib.exp [You can create this export file manually as well. By default all functions will be exported]
print_hello
print_world
get_str
$> xlc -qmkshrobj mylib.o [-q64]==> This will generate shr.o (mylib.o + mylib.exp)
$> [export OBJECT_MODE=64] ==> Though u compiled the code in 64 bit, ar (nm and strip etc) command does still work in 32 bit mode.
Hence, set the object mode to 64 bit, before calling the "ar" command.
$> ar -rv libfoo.a shr.o
This will generate the shared library (static linking) in AIX.

pgm.c
======
#include <stdio.h>

/* Note these are important to inform linker */
extern int print_hello();
extern int print_world();
extern char* get_str();

int main()
{
print_hello();
print_world();
printf("String from library: %s.\n", get_str());
}

$> xlc -o pgm pgm.c -L/home/harish/temp/ -lfoo [-q32|-q64]
$> ./pgm
Hello.
World.
String from library: Hello World.

NOTE: If you have compiled the application (pgm.c) with -q32 (as 32 bit),
then you would get error :
$> xlc -o pgm pgm.c -L/home/harish/temp/ -lfoo -q32
ld: 0711-317 ERROR: Undefined symbol: .print_hello
ld: 0711-317 ERROR: Undefined symbol: .print_world
ld: 0711-317 ERROR: Undefined symbol: .get_str
ld: 0711-345 Use the -bloadmap or -bnoquiet option to obtain more information.

[Use "-bnoquiet" option to find that ELF (XCOFF) format maintained by library
is different from what 32 bit application expects.]

$> xlc -o test test.c -L/perffs/ptx/harishse/sandbox/temp -lfoo -q32 -bnoquiet
(ld): halt 4
(ld): setfflag 4
(ld): savename test
(ld): filelist 6 1
(ld): i /lib/crt0.o
(ld): i test.o
(ld): lib /perffs/ptx/harishse/sandbox/temp/libfoo.a
(ld): lib /usr/vac/lib/libxlopt.a
(ld): lib /usr/vac/lib/libxl.a
(ld): lib /usr/lib/libc.a
LIBRARY: Shared object libc.a[shr.o]: 2874 symbols imported.
LIBRARY: Shared object libc.a[meth.o]: 2 symbols imported.
LIBRARY: Shared object libc.a[posix_aio.o]: 20 symbols imported.
LIBRARY: Shared object libc.a[aio.o]: 18 symbols imported.
LIBRARY: Shared object libc.a[pse.o]: 5 symbols imported.
LIBRARY: Shared object libc.a[dl.o]: 4 symbols imported.
LIBRARY: Shared object libc.a[pty.o]: 1 symbols imported.
FILELIST: Number of previously inserted files processed: 6
(ld): resolve
RESOLVE: 36 of 6085 symbols were kept.
(ld): addgl /usr/lib/glink.o
ADDGL: Glink code added for 6 symbols.
(ld): er full
ld: 0711-318 ERROR: Undefined symbols were found.
        The following symbols are in error:
 Symbol                    Inpndx  TY CL Source-File(Object-File) OR Import-File{Shared-object}
                              RLD: Address  Section  Rld-type Referencing Symbol
 ----------------------------------------------------------------------------------------------
 .print_hello              [26]    ER PR test.c(test.o)
                                   00000028 .text    R_RBR    [12]    .main
 .print_world              [28]    ER PR test.c(test.o)
                                   00000030 .text    R_RBR    [12]    .main
 .get_str                  [30]    ER PR test.c(test.o)
                                   00000038 .text    R_RBR    [12]    .main
ER: The return code is 8.

Monday, February 6, 2012

Kernel Debugging


[Ref: http://publib.boulder.ibm.com/infocenter/aix/v6r1/index.jsp?topic=%2Fcom.ibm.aix.kdb%2Fdoc%2Fkdb%2Fkdb.htm]
 
kdb command
This command is implemented as an ordinary user-space program and is typically used for
post-mortem analysis of a previously-crashed system by using a system dump file. The kdb command includes subcommands specific to the manipulation of system dumps.
 
KDB kernel debugger
- The KDB kernel debugger is integrated into the kernel and allows full control of the system while a debugging session is in progress. The KDB kernel debugger allows for traditional debugging tasks such as setting breakpoints and single-stepping through code.
- KDB needs to be enabled when the system boots.
- To check if the KDB is enabled or not, issue

$> bosdebug -L
Memory debugger           off
Memory sizes              0
Network memory sizes      0
Kernel debugger           off
Real Time Kernel          off
Backtracking fault log    on
Kernext Memory Tracking   off

- To create a kernel image with KDB enabled, issue
$> bosdebug -D 
This will turn on the kernel debugger. You need to bosboot and reboot in order to take this effect.
$> bosboot -a && reboot
[After of about 20-30 mins, you will have a KDB enabled image}
bosboot: Boot image is 49180 512 byte blocks.
- KDB enabled kernel image is ready. All you gotto do now is, reboot.
If you have HMC kinda machine (where you remotely try to reboot the machine), while booting, you can see Kernel Debugging is enabled.
$> reboot
 -------------------------------------------------------------------------------
                                Welcome to AIX.
                   boot image timestamp: 11:02:54 12/26/2011
                 The current time and date: 11:18:52 12/26/2011
        processor count: 1;  memory size: 2048MB;  kernel size: 28147575
         boot device: /vdevice/v-scsi@30000003/disk@8100000000000000:2
                       kernel debugger setting: enabled
-------------------------------------------------------------------------------
 


Tuesday, October 11, 2011

Sample vimrc file

For vimdiff colorschemes:
- Download github & molokai like below

curl -fLo ~/.vim/colors/molokai.vim --create-dirs https://raw.githubusercontent.com/tomasr/molokai/master/colors/molokai.vim
curl -fLo ~/.vim/colors/github.vim --create-dirs https://raw.githubusercontent.com/endel/vim-github-colorscheme/master/colors/github.vim
Then add the below in .vimrc file
if &diff
"   colorscheme github
    colorscheme molokai
endif
<or> ":colorscheme github" diff mode

syntax enable
set incsearch
set nocompatible
set showcmd
set hlsearch
set ai
set si
set ts=4 sw=4 smarttab
set nu
"set nowrap
set formatoptions+=r
set softtabstop=4
set smartindent
set autoindent
set showmatch
"set cursorline

source $HOME/.vim/plugin/taglist.vim
source $HOME/.vim/plugin/comments.vim

"highlight DiffAdd term=reverse cterm=bold ctermbg=white ctermfg=black
"highlight DiffChange term=reverse cterm=bold ctermbg=cyan ctermfg=black
"highlight DiffText term=reverse cterm=bold ctermbg=gray ctermfg=black
"highlight DiffDelete term=reverse cterm=bold ctermbg=cyan ctermfg=black

nnoremap <silent> <F1> :TlistToggle<CR>
"map <C-v><C-v> : vs<cr>
"map <C-q> : q<cr>

command CDC lcd %:p:h

hi LineNr   ctermfg=darkyellow
"hi Function ctermfg=cyan
hi Function ctermfg=yellow
"hi Type  ctermfg=cyan
hi Type  ctermfg=lightred
hi String ctermfg=darkgreen
"hi String ctermfg=150
hi Number ctermfg=brown
"hi Operator ctermfg=brown
hi Search ctermbg=yellow
hi Conditional ctermfg=green
hi Comment ctermfg=lightblue
"""
"if &term =~ "xterm\\|rxvt"
  " use an orange cursor in insert mode
  "let &t_SI = "\<Esc>]12;orange\x7"
  " use a red cursor otherwise
  "let &t_EI = "\<Esc>]12;red\x7"
  "silent !echo -ne "\033]12;red\007"
  "" reset cursor when vim exits
  "autocmd VimLeave * silent !echo -ne "\033]112\007"
  " use \003]12;gray\007 for gnome-terminal and rxvt up to version 9.21
"endif
highlight Cursor ctermfg=yellow ctermbg=black
"highlight iCursor guifg=white guibg=steelblue
"set guicursor=n-v-c:block-Cursor
"set guicursor+=i:ver100-iCursor
"set guicursor+=n-v-c:blinkon0
"set guicursor+=i:blinkwait10
"""

" Copyright abbreviations
iab cfn /*<CR>Function<CR><CR>Description<CR><CR>Parameter(s)<CR><CR>Return Value<CR>/<Up><Up><Up><Up><Up><Up>
iab todo // TODO Remove this
iab cpr Broadcom Proprietary and Confidential. Copyright © 2019 Broadcom.<CR>All Rights Reserved.<CR>The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.

iab testbin TOPDIR<TAB>= ../../..<CR>MAKEDIR<TAB>= $(TOPDIR)/make<CR><CR>include $(MAKEDIR)/Tools.make<CR>include $(MAKEDIR)/Targets.make<CR>include $(MAKEDIR)/Fabos.make<CR>include $(MAKEDIR)/Man.make<CR><CR>TESTBIN = testbin<CR>TESTBIN_SRCS = testbin.c<CR>TESTBIN_OBJS = $(TESTBIN_SRCS:.c=.o)<CR><CR>$(TESTBIN): $(TESTBIN_OBJS)<CR><TAB>$(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS)<CR>all:: $(TESTBIN)<CR><CR>clean::<CR><TAB>rm $(TESTBIN) $(TESTBIN_OBJS)


iab log_this #define LOG_THIS(format, ...) { \<CR>if (access("/harish", F_OK) != -1) { \<CR>char buf1[128] = {}; char buf2[256] = {}; \<CR>char tbuf[20] = {}; time_t now = time(0); struct tm *sTm = gmtime(&now); \<CR>strftime(tbuf, sizeof (tbuf), "%Y-%m-%d %H:%M:%S", sTm); \<CR>snprintf(buf1, sizeof (buf1), "[%s %s:%d]" format, tbuf, __func__, __LINE__, ##__VA_ARGS__); \<CR>snprintf(buf2, sizeof (buf2), "echo '%s' >> /harish_log", buf1); \<CR>system(buf2); } }


function! LoadCscope()
  let db = findfile("cscope.out", ".;")
  if (!empty(db))
    let path = strpart(db, 0, match(db, "/cscope.out$"))
    set nocscopeverbose " suppress 'duplicate connection' error
    exe "cs add " . db . " " . path
    set cscopeverbose
  " else add the database pointed to by environment variable
  elseif $CSCOPE_DB != ""
    cs add $CSCOPE_DB
  endif
endfunction
"au BufEnter /* call LoadCscope()

" http://cscope.sourceforge.net/cscope_maps.vim
nmap <C-\>ss :cs find s <C-R>=expand("<cword>")<CR><CR>
"nmap <C-\>g :cs find g <C-R>=expand("<cword>")<CR><CR>
nmap <C-\>cc :cs find c <C-R>=expand("<cword>")<CR><CR>
nmap <C-\>tt :cs find t <C-R>=expand("<cword>")<CR><CR>
"nmap <C-\>e :cs find e <C-R>=expand("<cword>")<CR><CR>
nmap <C-\>ff :cs find f <C-R>=expand("<cfile>")<CR><CR>
"nmap <C-\>i :cs find i ^<C-R>=expand("<cfile>")<CR>$<CR>
"nmap <C-\>d :cs find d <C-R>=expand("<cword>")<CR><CR>
nmap <C-\>s :cs find s
nmap <C-\>g :cs find g
nmap <C-\>c :cs find c
nmap <C-\>t :cs find t
nmap <C-\>e :cs find e
nmap <C-\>f :cs find f
nmap <C-\>i :cs find i
nmap <C-\>d :cs find d

" quick fix options
nmap qf :set cscopequickfix=s-,c-,d-,i-,t-,e-
nmap noqf :set cscopequickfix=""
nmap cw :cwindow

"function! AddCscope()
    "echom "Adding CSCOPE file.. please wait"
    "silent! exec "r!goto_base_ws"
    ":cs add cscope.files
"endfunction
"nnoremap <silent> <F3> :call AddCscope()<CR>
"nnoremap <F3> :! ~/bin/goto_base_ws && cd vobs/projects/springboard/fabos <CR>:cs add cscope.files<CR>

Thursday, June 9, 2011

To find out the location of the missed calls you got

http://site3.way2sms.com/jsp/LocateMobile.jsp

Thursday, March 24, 2011

Socket Programming - Part 1

Reference: http://www.tenouk.com/cnlinuxsockettutorials.html

Port Numbers
  • 16 bit integers
  • So a maximum port numbers are 2^16 = 65536 (0 to 65535)
  • Unique with a machine/IP address
  • Each service/application/daemon will have their own port number 
  • Required to make a connection (along with its host IP address)
Server & Client Port Numbers
  • Connection is defined by: (Server IP and Port number) + (Client IP and port number)
  • Server Port numbers are low numbers in the range 1 - 1023 (called as WELL KNOWN PORT NUMBER)
  • Accessible only by Administrators (roots, in linux)
  • Used for authentication
  • A server running on a well-known port lets the OS know what port it wants to listen on
  • Normally, client port numbers are higher number starting at 1024
  • Client normally simples lets the OS picks a new port that is not already in use.

Monday, March 7, 2011

Background and Foreground Process

1. When you run a command (process), you can make it to run as a background process using $<cmd> &
2. If you forget to make that command as background, and if you feel you should do it now, then
- Press CTRL+Z (Suspend that process)
- $> bg disown 1 (make that process to be a background
3. Now if you want to check the list of background processes, type $> jobs
4. If you want to bring the background process to foreground, then type
$> fg %1
Again to make it a background process,
$> bg %1

Monday, February 7, 2011

When properties screwed up

When Properties got screwed up in Visual Studio 2005/2008, in the cmd, type "devenv /resetskippkgs".
It will get you the default properties (color, font etc) for your editor. 

Wednesday, August 25, 2010

Late/Dynamic Binding in C++






Late Binding using Function Pointers (C)

Tuesday, August 24, 2010

Locks in Multi-processing/multi-thread programming

DeadLock:
deadlock is a situation wherein two or more competing actions are each waiting for the other to finish, and thus neither ever does.


LiveLock:

livelock is similar to a deadlock, except that the states of the processes involved in the livelock constantly change with regard to one another, none progressing. Livelock is a special case of resource starvation; the general definition only states that a specific process is not progressing.
A real-world example of livelock occurs when two people meet in a narrow corridor, and each tries to be polite by moving aside to let the other pass, but they end up swaying from side to side without making any progress because they both repeatedly move the same way at the same time.
Livelock is a risk with some algorithms that detect and recover from deadlock. If more than one process takes action, the deadlock detection algorithm can repeatedly trigger. This can be avoided by ensuring that only one process (chosen randomly or by priority) takes action.