c++linuxgccltrace

Series of strcmp() calls at the start of simple C++ program


I have a simple C++ program:

#include <iostream>

int main()
{
        std::cout << "Hello\n";
        return 0;
}

I was playing with "scratchbox" - cross compilation platform that I use in order to compile applications for ARM platform. I run it on my Nokia N900 phone.

While investigating some performance problems I noticed several calls to strcmp() in the beginning. I used "ltrace" to trace the library calls and when I run it on a target (my cell phone) a see series of calls to strcmp() which is now making me curious - what is the purpose of these calls?

20:06 user@MaemoBox:~$ ltrace test
__libc_start_main(61732, 1, 0xbed66634, 345616, 345612 <unfinished ...>
__errno_location()                                                                                 = 0x4001d3b0
strrchr("test", '/')                                                                               = NULL
getuid()                                                                                           = 29999
bsearch("test", 0x00055966, 146, 1, 0x0000ed38 <unfinished ...>
strcmp("test", "mv")                                                                               = 7
strcmp("test", "sort")                                                                             = 1
strcmp("test", "tr")                                                                               = -13
strcmp("test", "sysctl")                                                                           = 1
strcmp("test", "test")                                                                             = 0
<... bsearch resumed> )                                                                            = 0x000559e2

Solution

  • Not sure, but could it be that test actually is the system test command, and not the program you wrote? This could happen for example if $PATH does not contain ., the current directory. And given that it is a small platform, all test, mv, etc., might actually be just one executable, which emulates the correct program by figuring out what it is called as? See Busybox for example.

    If that is the case, all the strcmp() are basically the executable trying to figure out what it is being run as. Solution: name your program something other than test, or run ltrace <path_to_your_executable>/test.

    The strrchr() call before the strcmp() calls suggests strongly that it is trying to extract the executable "basename", without the directory. In which case, running it as "./test" won't probably help, so you should rename it to something that's unique. (Just a guess, again.)

    I could be wrong, since there is theoretically, the platform can do anything when it runs an executable.