]> git.cworth.org Git - apitrace/blob - os_posix.cpp
Recognize glFrameTerminatorGREMEDY as swapbuffer frame marker.
[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 #include <sys/stat.h>
35 #include <fcntl.h>
36
37 #ifdef __APPLE__
38 #include <mach-o/dyld.h>
39 #endif
40
41 #include "os.hpp"
42
43
44 namespace OS {
45
46
47 static pthread_mutex_t 
48 mutex = PTHREAD_MUTEX_INITIALIZER;
49
50
51 void
52 AcquireMutex(void)
53 {
54     pthread_mutex_lock(&mutex);
55 }
56
57
58 void
59 ReleaseMutex(void)
60 {
61     pthread_mutex_unlock(&mutex);
62 }
63
64
65 bool
66 GetProcessName(char *str, size_t size)
67 {
68     char szProcessPath[PATH_MAX + 1];
69     char *lpProcessName;
70
71     // http://stackoverflow.com/questions/1023306/finding-current-executables-path-without-proc-self-exe
72 #ifdef __APPLE__
73     uint32_t len = sizeof szProcessPath;
74     if (_NSGetExecutablePath(szProcessPath, &len) != 0) {
75         *str = 0;
76         return false;
77     }
78 #else
79     ssize_t len;
80     len = readlink("/proc/self/exe", szProcessPath, sizeof(szProcessPath) - 1);
81     if (len == -1) {
82         // /proc/self/exe is not available on setuid processes, so fallback to
83         // /proc/self/cmdline.
84         int fd = open("/proc/self/cmdline", O_RDONLY);
85         if (fd >= 0) {
86             len = read(fd, szProcessPath, sizeof(szProcessPath) - 1);
87             close(fd);
88         }
89     }
90     if (len <= 0) {
91         snprintf(str, size, "%i", (int)getpid());
92         return true;
93     }
94 #endif
95     szProcessPath[len] = 0;
96
97     lpProcessName = strrchr(szProcessPath, '/');
98     lpProcessName = lpProcessName ? lpProcessName + 1 : szProcessPath;
99
100     strncpy(str, lpProcessName, size);
101     if (size)
102         str[size - 1] = 0;
103
104     return true;
105 }
106
107 bool
108 GetCurrentDir(char *str, size_t size)
109 {
110     char *ret;
111     ret = getcwd(str, size);
112     str[size - 1] = 0;
113     return ret ? true : false;
114 }
115
116 void
117 DebugMessage(const char *format, ...)
118 {
119     va_list ap;
120     va_start(ap, format);
121     fflush(stdout);
122     vfprintf(stderr, format, ap);
123     va_end(ap);
124 }
125
126 long long GetTime(void)
127 {
128     struct timeval tv;
129     gettimeofday(&tv, NULL);
130     return tv.tv_usec + tv.tv_sec*1000000LL;
131 }
132
133 void
134 Abort(void)
135 {
136     exit(0);
137 }
138
139
140 } /* namespace OS */
141