Category: C


Everybody knows that Valgrind is the best open source tool to detect memory bugs on Linux OS.

Recently, I updated valgrind through emerge and a problem began to occurred:

==23796== Memcheck, a memory error detector
==23796== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
==23796== Using Valgrind-3.5.0 and LibVEX; rerun with -h for copyright info
==23796== Command: /bin/echo
==23796== 

valgrind:  Fatal error at startup: a function redirection
valgrind:  which is mandatory for this platform-tool combination
valgrind:  cannot be set up.  Details of the redirection are:
valgrind:  
valgrind:  A must-be-redirected function
valgrind:  whose name matches the pattern:      strlen
valgrind:  in an object with soname matching:   ld-linux-x86-64.so.2
valgrind:  was not found whilst processing
valgrind:  symbols from the object with soname: ld-linux-x86-64.so.2
valgrind:  
valgrind:  Possible fixes: (1, short term): install glibc's debuginfo
valgrind:  package on this machine.  (2, longer term): ask the packagers
valgrind:  for your Linux distribution to please in future ship a non-
valgrind:  stripped ld.so (or whatever the dynamic linker .so is called)
valgrind:  that exports the above-named function using the standard
valgrind:  calling conventions for this platform.
valgrind:  
valgrind:  Cannot continue -- exiting now.  Sorry.

How do you see, the glibc package has been stripped. There is a bug opened to this. But, for a while, I need of the tool and I can’t wait for a solution…

Here comes a little trick to permit valgrind operates again over the glibc. Just add to your make.conf the line bellow:

FEATURES="splitdebug"

Just a little explanation about stripping on Gentoo:

There are two ways to stop stripping from interfering with debugging and useful backtraces. The first is to tell Portage to not strip binaries at all, by adding nostrip to FEATURES. This will leave the installed files exactly as gcc created them, with all the debug information and symbol tables, which increases the disk space occupied by executables and libraries. To avoid this problem, in Portage version 2.0.54-r1 and the 2.1 series, it’s possible to use the splitdebug FEATURE instead.
With splitdebug enabled, Portage will still strip the binaries installed in the system. But before doing that, all the useful debug information is copied to a “.debug” file, which is then installed inside /usr/lib/debug (the complete name of the file would be given by appending to that the path where the file is actually installed). The path to that file is then saved in the original file inside an ELF section called “.gnu_debuglink”, so that gdb knows which file to load the symbols from.

After modify your make.conf, re-emerge glibc:

emerge glibc

And now, be happy: :)

==21886== Memcheck, a memory error detector
==21886== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
==21886== Using Valgrind-3.5.0 and LibVEX; rerun with -h for copyright info
==21886== Command: /bin/echo
==21886==
==21886== HEAP SUMMARY:
==21886==     in use at exit: 0 bytes in 0 blocks
==21886==   total heap usage: 30 allocs, 30 frees, 3,681 bytes allocated
==21886==
==21886== All heap blocks were freed -- no leaks are possible
==21886==
==21886== For counts of detected and suppressed errors, rerun with: -v
==21886== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 4 from 4)

Changing process name

A simple trick to change a process name in C programming is to do it:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>

int main(int argc, char **argv) {
    int st = 0;
    int pid = fork();

    /* copy new name to child process */
    if(pid == 0) {
        printf("Check my pid: %d\n", getpid());
        strcpy(argv[0], "new_name");
        sleep(20);
        exit(0);
    }

    wait(&st);
    return 0;
}

To see it in action, firstly, compile the program:

gcc -Wall -o test test.c

Now, execute it in background mode:

./test &

Then, check the name of the process through the given pid:

ps -auxww | grep pid

Yeah!!! The key is argv[0] !!! ;)

You can include arguments too, just do it:

strcpy(argv[0],"newprocess -a -x -y");

Now, use your imagination… =]

Follow

Get every new post delivered to your Inbox.