:(){ :|:& };:

When you can’t use gdb or valgrind, there is /lib/libSegFault.so.

Under certain conditions I can’t get gdb or valgrind to work on my binary and I don’t want
any delays from them. And I just wanted to see where the binary segfaulted so I remembered of
/lib/libSegFault.so.


LD_PRELOAD=/lib/libSegFault.so executable
Backtrace:
/lib/libSegFault.so(+0x206f)[0x11206f]
[0x9d1400]
executable[0x805994d]
executable[0x8059a0d]
executable[0x805d043]
executable[0x8057d59]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe6)[0xae5bd6]
executable[0x8048e81]

Now I use addr2line to translate the addresses to locations in binary:

addr2line -e executable 0x805994d 0x8059a0d 0x805d043 0x8057d59
sm.c:505
sm.c:564
tmr.c:104
main.c:155

Thats it .

Ruby: undefined method `innerHTML’

In ruby 1.8 seems that renamed/removed the innerHTML method in Mechanize:

main.rb:18: undefined method `innerHTML’ for # (NoMethodError)

it was somehow renamed to inner_html.

Things I want to implement using dehydra/Treehydra.

I’ve started using Dehydra just to get intimate with it. What I would like to use it for would be(in no particular order):

– Proper C++ RMI[1].
Dia[2] UML diagram generator.
– Stub Testing.


[1]: http://java.sun.com/javase/technologies/core/basic/rmi/index.jsp
[2]: http://projects.gnome.org/dia/

Dehydra and gcc-4.5 on Ubuntu Lucid Lynx

gcc-4.5 haven’t reached Ubuntu Lucid repositories just yet.
So I had to grab it from Debian. You can do this by
placing this line in your /etc/apt/sources.list,

deb http://ftp.debian.org/debian/ experimental main

After this, I updated my package list and downloaded dehydra source code from Mozilla, since
the Debian package still have some errors.

sudo apt-get update
sudo apt-get install gcc-4.5
sudo apt-get install gcc-4.5-plugin-dev
sudo apt-get install xulrunner-1.9.2-dev


hg clone http://hg.mozilla.org/rewriting-and-analysis/dehydra/
cd dehydra
CXX=g++-4.5 CC=gcc-4.5 ./configure --js-headers=/usr/include/xulrunner-1.9.2.3/ --js-libs=/usr/lib/xulrunner-1.9.2.3/
CXX=g++-4.5 CC=gcc-4.5 make
CXX=g++-4.5 CC=gcc-4.5 make check_dehydra

Now I can start using dehydra. I looked inside test/unit_test_harness.py to see how I can call gcc with plugins.
This is what I found this:
/usr/lib/gcc/i486-linux-gnu/4.5.0/cc1plus -fplugin=./gcc_dehydra.so -o /dev/null -fplugin-arg-gcc_dehydra-=./test/test_virtual_inheritance.js ./test/virtual_inheritance.cc
C* getC()
Analyzing compilation unit
Performing interprocedural optimizations
Assembling functions:
C* getC()class C : private A,private virtual B
t.bases[0].isVirtual: undefined

Execution times (seconds)
parser : 0.01 (100%) usr 0.00 ( 0%) sys 0.00 ( 0%) wall 288 kB (31%) ggc
TOTAL : 0.01 0.01 0.01 919 kB
Extra diagnostic checks enabled; compiler may run slowly.
Configure with --enable-checking=release to disable checks.
OK


[1]: https://bugzilla.mozilla.org/show_bug.cgi?id=535696
[2]: https://developer.mozilla.org/En/Dehydra/Installing_Dehydra#Usage

Obtain the call graph from C sources.

When I have to understand how a C program word from a bird-eye view I usually tend to draw on paper the flow of the program.
I looked around to see if there are any tools to do this automatically and found cflow[1].
The downside is that it doesn’t know about dot[2] format.
I looked around and found cflow2vcg[3] that does exactly that: turns a cflow graph in a dot graph. It’s not available in Ubuntus’ repositories
so you’ll have to compile it by hand.


cflow --cpp --format=posix --omit-arguments --level-indent='0=\t' --level-indent='1=\t' --level-indent=start='\t' YOUR_FILE.C > cflow.tmp
cflow2dot < cflow.dot > cflow.dot
dot -Tjpg -G"300,900" cflow.dot -o cflow.jpg
display cflow.jpg

The -G”300,900″ option works around a ‘dot’ segfault.

Stubing the libc functions.

I tried to use Mockpp[1] one day to test a scheduling queue.
The queue used gettimeofday to see when a packet expired.
This would prove imposible to test with stub-testing, unless I would wrap gettimeofday
in another class, and then change the code to use that class which would be difficult.

After some googling I found about the GNU ld “-wrap” parameter.

From `man 1 ld`:

--wrap symbol
Use a wrapper function for symbol. Any undefined reference to
symbol will be resolved to "__wrap_symbol".

Any undefined reference to "__real_symbol" will be
resolved to symbol.

So I put up a little test, made out of two files:
malloc_wrapper.c and test_ld_wrap.c

/* malloc_wrapper.c */
#include <stdio.h>
void *__real_malloc (int);

void * __wrap_malloc (size_t c)
{
	void *lptr = __real_malloc(size);
	printf("@ %s:[%p] + %p %#x\n", program_invocation_short_name, __builtin_return_address(0), lptr, size );
	return lptr;
}
/* test_ld_wrap.c */
#include <stdio.h>
int main()
{
    malloc(1);
}

Putting the whole thing together

gcc -c malloc_wrapper.c
gcc -c test_ld_wrap.c
gcc -Wl,-wrap,malloc test_ld_wrap.o malloc_wrapper.o

Running the test will yield:

~/mine> ./a.out
malloc called with 1

Yey, success!

I prefer this method on architectures where LD_PRELOAD is not available,
and this usually happens in the Embedded world.
You will have to recompile your application though.
This parameter can be used either for testing or for memory leak detection, when the C library doesn’t provide
support. ( see `man 3 mtrace` for GNU libc).

———–
[1] http://mockpp.sourceforge.net/

One good reason I ditched ruby for python

I had a small stuff to do test IPsec with lots of selectors. I thought I might be binding 50k ports and poll them for connections.
I though I’d go for Ruby since everyone praise it as intuitive. I’ve used it before at my testing work and I thought this might be a good
occasion to see how it evolved.

Soon I got in the limit of how many file descriptors select() can watch, that is 1024. That also crashed ruby1.8.7.
Further more, I wasn’t able to find official documentation about detecting EOF(end of file) while doing a recvfrom_nonblock().
You have to go around stackoverflow to find similar questions.

Then I though that python might do better. One search on google was enough to see that python implements epoll. Hooray..!!!

It seems to me that ruby is just some toy language, good for learning how to program OOP style, which should be dropped when doing
performance critical or reliable projects.

Comic 1

Indexed man pages

why not index ,somehow, the Unix man pages ?

I googled a bit and found swish.

It’s a neat tool, that can index mp3(id3), html/xml, mail, rtf, latex and (drum roll please) manual pages(nroff).
So I installed it on my ubuntu:

sudo apt-get install swish++

then made up a quick config.
NOTE: run as user to avoid any problems.

cd ~
cp /usr/share/doc/swish++/examples/swish++.conf.gz .
gunzip swish++.conf.gz
mkdir /tmp/tmpswish/
joe swish++.conf

Now, be sure to uncomment the lines

 95 IncludeMeta             author bugs caveats description diagnostics environment
 96 IncludeMeta             errors examples exit-status files history name notes
 97 IncludeMeta             options return-value see-also synopsis warnings
...
194 FilterFile *.bz2        bunzip2 -c %f > @%F
195 FilterFile *.gz         gunzip -c %f > /tmp/tmpswish/@%B
196 FilterFile *.Z          uncompress -c %f > @%F
...
222 IncludeFile Man *.[1-9n] *.[1-9][a-z]
index++   -e "man:*.gz" /usr/share/man
index++   -e "man:*" /tmp/tmpswish
rm -rf /tmp/tmpswish

now, let’s search for SO_REUSEADDR

user ~ > search++ SO_REUSEADDR
# results: 3
99 /tmp/tmpswish//socket.7 18753 socket.7
57 /tmp/tmpswish//ip.7 28366 ip - Linux IPv4 protocol implementation
6 /tmp/tmpswish//smb.conf.5 287447 smb.conf.5

All you have to do now, is
 man 7 socket