]> git.cworth.org Git - apitrace/blob - os_posix.cpp
bf8d1908b5003f70a6cba035c241cc438d527931
[apitrace] / os_posix.cpp
1 /**************************************************************************
2  *
3  * Copyright 2010-2011 VMware, Inc.
4  * All Rights Reserved.
5  *
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:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
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
22  * THE SOFTWARE.
23  *
24  **************************************************************************/
25
26
27 #include <string.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30
31 #include <unistd.h>
32 #include <sys/time.h>
33 #include <pthread.h>
34
35 #ifdef __APPLE__
36 #include <mach-o/dyld.h>
37 #endif
38
39 #include "os.hpp"
40
41
42 namespace OS {
43
44
45 static pthread_mutex_t 
46 mutex = PTHREAD_MUTEX_INITIALIZER;
47
48
49 void
50 AcquireMutex(void)
51 {
52     pthread_mutex_lock(&mutex);
53 }
54
55
56 void
57 ReleaseMutex(void)
58 {
59     pthread_mutex_unlock(&mutex);
60 }
61
62
63 bool
64 GetProcessName(char *str, size_t size)
65 {
66     char szProcessPath[PATH_MAX + 1];
67     char *lpProcessName;
68
69     // http://stackoverflow.com/questions/1023306/finding-current-executables-path-without-proc-self-exe
70 #ifdef __APPLE__
71     uint32_t len = sizeof szProcessPath;
72     if (_NSGetExecutablePath(szProcessPath, &len) != 0) {
73         *str = 0;
74         return false;
75     }
76 #else
77     ssize_t len;
78     len = readlink("/proc/self/exe", szProcessPath, sizeof(szProcessPath) - 1);
79     if (len == -1) {
80         // /proc/self/exe is not available on setuid processes, so fallback to
81         // /proc/self/cmdline.
82         int fd = open("/proc/self/cmdline", O_RDONLY);
83         if (fd >= 0) {
84             len = read(fd, szProcessPath, sizeof(szProcessPath) - 1);
85             close(fd);
86         }
87     }
88     if (len <= 0) {
89         snprintf(str, size, "%i", (int)getpid());
90         return true;
91     }
92 #endif
93     szProcessPath[len] = 0;
94
95     lpProcessName = strrchr(szProcessPath, '/');
96     lpProcessName = lpProcessName ? lpProcessName + 1 : szProcessPath;
97
98     strncpy(str, lpProcessName, size);
99     if (size)
100         str[size - 1] = 0;
101
102     return true;
103 }
104
105 bool
106 GetCurrentDir(char *str, size_t size)
107 {
108     char *ret;
109     ret = getcwd(str, size);
110     str[size - 1] = 0;
111     return ret ? true : false;
112 }
113
114 void
115 DebugMessage(const char *format, ...)
116 {
117     va_list ap;
118     va_start(ap, format);
119     fflush(stdout);
120     vfprintf(stderr, format, ap);
121     va_end(ap);
122 }
123
124 long long GetTime(void)
125 {
126     struct timeval tv;
127     gettimeofday(&tv, NULL);
128     return tv.tv_usec + tv.tv_sec*1000000LL;
129 }
130
131 void
132 Abort(void)
133 {
134     exit(0);
135 }
136
137
138 } /* namespace OS */
139