开发者

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

开发者 https://www.devze.com 2023-03-19 16:05 出处:网络
I have a simple C++ program: #include <iostream> int main() { std::cout << "Hello\\n";

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


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.


I am not familiar with scratchbox, but if it is similar to busybox, I assume it is comparing its called name (argv[0]) with common Unix utilities/functionalities that it provides. If none match, it passes control to your code.

0

精彩评论

暂无评论...
验证码 换一张
取 消