Thursday, February 5, 2009

Traps in printf

I was debugging this segmentation fault that we were seeing at a customer. It wasn't caught because in never hit the code path in dev/QA. It would happen when logging was enabled. GNU debugger seemed to indicate that the logging library was using the vnsprintf call which uses va_list (variable argument list) as input. I spent close to 2 hrs trying to debug this before realizing the error. So lets see if you can try to figure it out. The same problem exists in printf as well:

printf(" some randmon long %l and string %s \n", 4, "hello");

compile the above and it will throw segv.

Thing is, its easy to assume that %l is a specifier for long. Its not. Its a length specifier not type specifier. You have use %ld. In absense of that printf will assumen that the first arguement ( 4) is a string and throw segv.

Lesson: variable arugement is very powerful. but be aware of the traps.

Monday, February 2, 2009

The case of missing core dumps

Have you had situations when your program seg faulted and yet did not create a core file. Well run the following commad :
> ulimit -a
if you see "core file size  0" then there's your answer.
To enable run the follwoing :
> ulimit -c unlimited

viola.


Tuesday, January 27, 2009

configuring your shell env

How often did you have to write your .bashrc file all over again because you changed your job.  I end up writing/copying it every time and its a bit tired. So here I aim add template bashrc for reuse.
-------------------------------------------------------
#!/bin/sh
# .bashrc

# User specific aliases and functions

# Source global definitions
if [ -f /etc/bashrc ]; then
  . /etc/bashrc
fi

# use vim style editing of commandline
set -o vi

#make sure cp always creates a backup file
alias cp='cp --backup'

# have history igonre rm. save you from accidently doing !r and deleting stuff you
# did not intend to delete
export HISTIGNORE="rm*:&"

export PATH=/usr/local/ant/1.6.5/bin:~/scripts/:$PATH
export JAVA_HOME=/usr/local/jdk/1.5.0_09/

export PATH=/usr/local/jdk/1.5.0_09/bin:$PATH

export CVSROOT=:pserver:${LOGNAME}@10.1.1.40:/CVSrep

# if you are mysql user, this sets your mysql prompt
export MYSQL_PS1="\\d@\\h>"


unalias vi
alias vi='~/bin/bin/vim'

# hack for weird promts when using patch vim for vimshell
if [[ $TERM == "screen" ]]; then
PROMPT_COMMAND=''
fi