1 /**************************************************************************
3 * Copyright 2010-2011 VMware, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 **************************************************************************/
36 #include <mach-o/dyld.h>
45 static pthread_mutex_t
46 mutex = PTHREAD_MUTEX_INITIALIZER;
52 pthread_mutex_lock(&mutex);
59 pthread_mutex_unlock(&mutex);
64 GetProcessName(char *str, size_t size)
66 char szProcessPath[PATH_MAX + 1];
69 // http://stackoverflow.com/questions/1023306/finding-current-executables-path-without-proc-self-exe
71 uint32_t len = sizeof szProcessPath;
72 if (_NSGetExecutablePath(szProcessPath, &len) != 0) {
78 len = readlink("/proc/self/exe", szProcessPath, sizeof(szProcessPath) - 1);
80 snprintf(str, size, "%i", (int)getpid());
84 szProcessPath[len] = 0;
86 lpProcessName = strrchr(szProcessPath, '/');
87 lpProcessName = lpProcessName ? lpProcessName + 1 : szProcessPath;
89 strncpy(str, lpProcessName, size);
97 GetCurrentDir(char *str, size_t size)
100 ret = getcwd(str, size);
102 return ret ? true : false;
106 DebugMessage(const char *format, ...)
109 va_start(ap, format);
111 vfprintf(stderr, format, ap);
115 long long GetTime(void)
118 gettimeofday(&tv, NULL);
119 return tv.tv_usec + tv.tv_sec*1000000LL;