]> git.cworth.org Git - apitrace/blob - os_posix.cpp
Handle sigsegv as well.
[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 struct Interrupts
142 {
143     Interrupts()
144         : set(false),
145           sig_int(NULL),
146           sig_hup(NULL),
147           sig_term(NULL),
148           sig_segv(NULL),
149           handler(NULL)
150     {}
151
152     bool set;
153     void (*sig_int)(int);
154     void (*sig_hup)(int);
155     void (*sig_term)(int);
156     void (*sig_segv)(int);
157
158     void (*handler)(int);
159 };
160 static Interrupts interrupts;
161
162 static void InterruptHandler(int sig)
163 {
164     if (interrupts.set && interrupts.handler) {
165         interrupts.handler(sig);
166     }
167     if (sig == SIGINT) {
168         if (!interrupts.sig_int) {
169             exit(sig);
170         }
171         interrupts.sig_int(sig);
172     } else if (sig == SIGHUP) {
173         if (!interrupts.sig_hup) {
174             exit(sig);
175         }
176         interrupts.sig_hup(sig);
177     } else if (sig == SIGTERM) {
178         if (!interrupts.sig_term) {
179             exit(sig);
180         }
181         interrupts.sig_term(sig);
182     } else if (sig == SIGSEGV) {
183         if (interrupts.sig_segv) {
184             interrupts.sig_segv(sig);
185         }
186     }
187 }
188
189 void
190 CatchInterrupts(void (*func)(int))
191 {
192     interrupts.handler = func;
193
194     if (!interrupts.set) {
195         struct sigaction new_action, old_action;
196         new_action.sa_handler = InterruptHandler;
197         sigemptyset(&new_action.sa_mask);
198         new_action.sa_flags = 0;
199 #define SET_IF_NOT_IGNORED(sig, old_handler)            \
200         do {                                            \
201             sigaction(sig, NULL, &old_action);          \
202             if (old_action.sa_handler != SIG_IGN) {     \
203                 old_handler = old_action.sa_handler;    \
204                 sigaction(sig, &new_action, NULL);      \
205             }                                           \
206         } while (0)
207
208         SET_IF_NOT_IGNORED(SIGINT, interrupts.sig_int);
209         SET_IF_NOT_IGNORED(SIGHUP, interrupts.sig_hup);
210         SET_IF_NOT_IGNORED(SIGTERM, interrupts.sig_term);
211         SET_IF_NOT_IGNORED(SIGSEGV, interrupts.sig_segv);
212
213         interrupts.set = true;
214 #undef SET_IF_NOT_IGNORED
215     }
216 }
217
218 } /* namespace OS */
219