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