]> git.cworth.org Git - apitrace/blob - common/os_posix.cpp
Include sys/syslimits.h for PATH_MAX on MacOSX.
[apitrace] / common / 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 <assert.h>
28 #include <string.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31
32 #include <unistd.h>
33 #include <sys/time.h>
34 #include <pthread.h>
35 #include <sys/stat.h>
36 #include <fcntl.h>
37 #include <signal.h>
38
39 #if defined(__linux__)
40 #include <linux/limits.h> // PATH_MAX
41 #endif
42
43 #ifdef __APPLE__
44 #include <sys/syslimits.h> // PATH_MAX
45 #include <mach-o/dyld.h>
46 #endif
47
48 #ifndef PATH_MAX
49 #warning PATH_MAX undefined
50 #define PATH_MAX 4096
51 #endif
52
53 #include "os.hpp"
54 #include "os_path.hpp"
55
56
57 namespace os {
58
59
60 static pthread_mutex_t 
61 mutex = PTHREAD_MUTEX_INITIALIZER;
62
63
64 void
65 acquireMutex(void)
66 {
67     pthread_mutex_lock(&mutex);
68 }
69
70
71 void
72 releaseMutex(void)
73 {
74     pthread_mutex_unlock(&mutex);
75 }
76
77
78 Path
79 getProcessName(void)
80 {
81     Path path;
82     size_t size = PATH_MAX;
83     char *buf = path.buf(size);
84
85     // http://stackoverflow.com/questions/1023306/finding-current-executables-path-without-proc-self-exe
86 #ifdef __APPLE__
87     uint32_t len = size;
88     if (_NSGetExecutablePath(buf, &len) != 0) {
89         *buf = 0;
90         return path;
91     }
92 #else
93     ssize_t len;
94     len = readlink("/proc/self/exe", buf, size - 1);
95     if (len == -1) {
96         // /proc/self/exe is not available on setuid processes, so fallback to
97         // /proc/self/cmdline.
98         int fd = open("/proc/self/cmdline", O_RDONLY);
99         if (fd >= 0) {
100             len = read(fd, buf, size - 1);
101             close(fd);
102         }
103     }
104     if (len <= 0) {
105         snprintf(buf, size, "%i", (int)getpid());
106         return path;
107     }
108 #endif
109     path.truncate(len);
110
111     return path;
112 }
113
114 Path
115 getCurrentDir(void)
116 {
117     Path path;
118     size_t size = PATH_MAX;
119     char *buf = path.buf(size);
120
121     getcwd(buf, size);
122     buf[size - 1] = 0;
123     
124     path.truncate();
125     return path;
126 }
127
128 void
129 log(const char *format, ...)
130 {
131     va_list ap;
132     va_start(ap, format);
133     fflush(stdout);
134     vfprintf(stderr, format, ap);
135     va_end(ap);
136 }
137
138 long long
139 getTime(void)
140 {
141     struct timeval tv;
142     gettimeofday(&tv, NULL);
143     return tv.tv_usec + tv.tv_sec*1000000LL;
144 }
145
146 void
147 abort(void)
148 {
149     exit(0);
150 }
151
152
153 static void (*gCallback)(void) = NULL;
154
155 #define NUM_SIGNALS 16
156
157 struct sigaction old_actions[NUM_SIGNALS];
158
159
160 /*
161  * See also:
162  * - http://sourceware.org/git/?p=glibc.git;a=blob;f=debug/segfault.c
163  * - http://ggi.cvs.sourceforge.net/viewvc/ggi/ggi-core/libgg/gg/cleanup.c?view=markup
164  */
165 static void
166 signalHandler(int sig, siginfo_t *info, void *context)
167 {
168     static int recursion_count = 0;
169
170     fprintf(stderr, "apitrace: warning: caught signal %i\n", sig);
171
172     if (recursion_count) {
173         fprintf(stderr, "apitrace: warning: recursion handling signal %i\n", sig);
174     } else {
175         if (gCallback) {
176             ++recursion_count;
177             gCallback();
178             --recursion_count;
179         }
180     }
181
182     struct sigaction *old_action;
183     if (sig >= NUM_SIGNALS) {
184         /* This should never happen */
185         fprintf(stderr, "error: unexpected signal %i\n", sig);
186         raise(SIGKILL);
187     }
188     old_action = &old_actions[sig];
189
190     if (old_action->sa_flags & SA_SIGINFO) {
191         // Handler is in sa_sigaction
192         old_action->sa_sigaction(sig, info, context);
193     } else {
194         if (old_action->sa_handler == SIG_DFL) {
195             fprintf(stderr, "apitrace: info: taking default action for signal %i\n", sig);
196
197 #if 1
198             struct sigaction dfl_action;
199             dfl_action.sa_handler = SIG_DFL;
200             sigemptyset (&dfl_action.sa_mask);
201             dfl_action.sa_flags = 0;
202             sigaction(sig, &dfl_action, NULL);
203
204             raise(sig);
205 #else
206             raise(SIGKILL);
207 #endif
208         } else if (old_action->sa_handler == SIG_IGN) {
209             /* ignore */
210         } else {
211             /* dispatch to handler */
212             old_action->sa_handler(sig);
213         }
214     }
215 }
216
217 void
218 setExceptionCallback(void (*callback)(void))
219 {
220     assert(!gCallback);
221     if (!gCallback) {
222         gCallback = callback;
223
224         struct sigaction new_action;
225         new_action.sa_sigaction = signalHandler;
226         sigemptyset(&new_action.sa_mask);
227         new_action.sa_flags = SA_SIGINFO | SA_RESTART;
228
229
230         for (int sig = 1; sig < NUM_SIGNALS; ++sig) {
231             // SIGKILL and SIGSTOP can't be handled
232             if (sig != SIGKILL && sig != SIGSTOP) {
233                 if (sigaction(sig,  NULL, &old_actions[sig]) >= 0) {
234                     sigaction(sig,  &new_action, NULL);
235                 }
236             }
237         }
238     }
239 }
240
241 void
242 resetExceptionCallback(void)
243 {
244     gCallback = NULL;
245 }
246
247 } /* namespace os */
248