]> git.cworth.org Git - apitrace/blob - common/os_posix.cpp
Add a path manipulating class.
[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     szProcessPath[len] = 0;
99     path.truncate(len);
100
101     return path;
102 }
103
104 Path
105 getCurrentDir(void)
106 {
107     Path path;
108     size_t size = PATH_MAX;
109     char *str = path.buf(size);
110     getcwd(str, size);
111     str[size - 1] = 0;
112     path.truncate();
113     return path;
114 }
115
116 void
117 log(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
127 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 static void (*gCallback)(void) = NULL;
142
143 #define NUM_SIGNALS 16
144
145 struct sigaction old_actions[NUM_SIGNALS];
146
147
148 /*
149  * See also:
150  * - http://sourceware.org/git/?p=glibc.git;a=blob;f=debug/segfault.c
151  * - http://ggi.cvs.sourceforge.net/viewvc/ggi/ggi-core/libgg/gg/cleanup.c?view=markup
152  */
153 static void
154 signalHandler(int sig, siginfo_t *info, void *context)
155 {
156     static int recursion_count = 0;
157
158     fprintf(stderr, "apitrace: warning: caught signal %i\n", sig);
159
160     if (recursion_count) {
161         fprintf(stderr, "apitrace: warning: recursion handling signal %i\n", sig);
162     } else {
163         if (gCallback) {
164             ++recursion_count;
165             gCallback();
166             --recursion_count;
167         }
168     }
169
170     struct sigaction *old_action;
171     if (sig >= NUM_SIGNALS) {
172         /* This should never happen */
173         fprintf(stderr, "error: unexpected signal %i\n", sig);
174         raise(SIGKILL);
175     }
176     old_action = &old_actions[sig];
177
178     if (old_action->sa_flags & SA_SIGINFO) {
179         // Handler is in sa_sigaction
180         old_action->sa_sigaction(sig, info, context);
181     } else {
182         if (old_action->sa_handler == SIG_DFL) {
183             fprintf(stderr, "apitrace: info: taking default action for signal %i\n", sig);
184
185 #if 1
186             struct sigaction dfl_action;
187             dfl_action.sa_handler = SIG_DFL;
188             sigemptyset (&dfl_action.sa_mask);
189             dfl_action.sa_flags = 0;
190             sigaction(sig, &dfl_action, NULL);
191
192             raise(sig);
193 #else
194             raise(SIGKILL);
195 #endif
196         } else if (old_action->sa_handler == SIG_IGN) {
197             /* ignore */
198         } else {
199             /* dispatch to handler */
200             old_action->sa_handler(sig);
201         }
202     }
203 }
204
205 void
206 setExceptionCallback(void (*callback)(void))
207 {
208     assert(!gCallback);
209     if (!gCallback) {
210         gCallback = callback;
211
212         struct sigaction new_action;
213         new_action.sa_sigaction = signalHandler;
214         sigemptyset(&new_action.sa_mask);
215         new_action.sa_flags = SA_SIGINFO | SA_RESTART;
216
217
218         for (int sig = 1; sig < NUM_SIGNALS; ++sig) {
219             // SIGKILL and SIGSTOP can't be handled
220             if (sig != SIGKILL && sig != SIGSTOP) {
221                 if (sigaction(sig,  NULL, &old_actions[sig]) >= 0) {
222                     sigaction(sig,  &new_action, NULL);
223                 }
224             }
225         }
226     }
227 }
228
229 void
230 resetExceptionCallback(void)
231 {
232     gCallback = NULL;
233 }
234
235 } /* namespace os */
236