]> git.cworth.org Git - apitrace/blob - os_posix.cpp
Make sure that the traces are always complete.
[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 #include <signal.h>
37
38 #ifdef __APPLE__
39 #include <mach-o/dyld.h>
40 #endif
41
42 #include "os.hpp"
43
44
45 namespace OS {
46
47
48 static pthread_mutex_t 
49 mutex = PTHREAD_MUTEX_INITIALIZER;
50
51
52 void
53 AcquireMutex(void)
54 {
55     pthread_mutex_lock(&mutex);
56 }
57
58
59 void
60 ReleaseMutex(void)
61 {
62     pthread_mutex_unlock(&mutex);
63 }
64
65
66 bool
67 GetProcessName(char *str, size_t size)
68 {
69     char szProcessPath[PATH_MAX + 1];
70     char *lpProcessName;
71
72     // http://stackoverflow.com/questions/1023306/finding-current-executables-path-without-proc-self-exe
73 #ifdef __APPLE__
74     uint32_t len = sizeof szProcessPath;
75     if (_NSGetExecutablePath(szProcessPath, &len) != 0) {
76         *str = 0;
77         return false;
78     }
79 #else
80     ssize_t len;
81     len = readlink("/proc/self/exe", szProcessPath, sizeof(szProcessPath) - 1);
82     if (len == -1) {
83         // /proc/self/exe is not available on setuid processes, so fallback to
84         // /proc/self/cmdline.
85         int fd = open("/proc/self/cmdline", O_RDONLY);
86         if (fd >= 0) {
87             len = read(fd, szProcessPath, sizeof(szProcessPath) - 1);
88             close(fd);
89         }
90     }
91     if (len <= 0) {
92         snprintf(str, size, "%i", (int)getpid());
93         return true;
94     }
95 #endif
96     szProcessPath[len] = 0;
97
98     lpProcessName = strrchr(szProcessPath, '/');
99     lpProcessName = lpProcessName ? lpProcessName + 1 : szProcessPath;
100
101     strncpy(str, lpProcessName, size);
102     if (size)
103         str[size - 1] = 0;
104
105     return true;
106 }
107
108 bool
109 GetCurrentDir(char *str, size_t size)
110 {
111     char *ret;
112     ret = getcwd(str, size);
113     str[size - 1] = 0;
114     return ret ? true : false;
115 }
116
117 void
118 DebugMessage(const char *format, ...)
119 {
120     va_list ap;
121     va_start(ap, format);
122     fflush(stdout);
123     vfprintf(stderr, format, ap);
124     va_end(ap);
125 }
126
127 long long GetTime(void)
128 {
129     struct timeval tv;
130     gettimeofday(&tv, NULL);
131     return tv.tv_usec + tv.tv_sec*1000000LL;
132 }
133
134 void
135 Abort(void)
136 {
137     exit(0);
138 }
139
140
141 void
142 CatchInterrupts(void (*func)(int))
143 {
144     struct sigaction new_action, old_action;
145
146     new_action.sa_handler = func;
147     sigemptyset(&new_action.sa_mask);
148     new_action.sa_flags = 0;
149
150 #define SET_IF_NOT_IGNORED(sig)                \
151     do {                                       \
152         sigaction(sig, NULL, &old_action);     \
153         if (old_action.sa_handler != SIG_IGN)  \
154             sigaction(sig, &new_action, NULL); \
155     } while (0)
156
157     SET_IF_NOT_IGNORED(SIGINT);
158     SET_IF_NOT_IGNORED(SIGHUP);
159     SET_IF_NOT_IGNORED(SIGTERM);
160
161 #undef SET_IF_NOT_IGNORED
162 }
163
164 } /* namespace OS */
165