|
1 | 1 | /* |
2 | 2 | * sumain.c |
3 | 3 | * |
4 | | - * Copyright (C) 2015-2018 Kristofer Berggren |
| 4 | + * Copyright (C) 2015-2022 Kristofer Berggren |
5 | 5 | * All rights reserved. |
6 | 6 | * |
7 | 7 | * stackusage is distributed under the BSD 3-Clause license, see LICENSE for details. |
|
50 | 50 | #define SU_LOG_WARN SU_LOG("%s (pid %d): %s:%d warning\n", \ |
51 | 51 | su_name, getpid(), __FUNCTION__, __LINE__) |
52 | 52 |
|
| 53 | +#if defined(__APPLE__) |
| 54 | +#define DYLD_INTERPOSE(_newfun, _orgfun) \ |
| 55 | +__attribute__((used)) static struct{ const void *newfun; const void *orgfun; } _interpose_##_orgfun \ |
| 56 | +__attribute__ ((section ("__DATA,__interpose"))) = { (const void *)(unsigned long)&_newfun, \ |
| 57 | + (const void *)(unsigned long)&_orgfun } |
| 58 | +#endif |
| 59 | + |
53 | 60 |
|
54 | 61 | /* ----------- Types --------------------------------------------- */ |
55 | 62 | typedef struct |
@@ -168,20 +175,32 @@ void signal_handler(int num) |
168 | 175 | } |
169 | 176 |
|
170 | 177 |
|
| 178 | +#if defined(__APPLE__) |
| 179 | +int pthread_create_wrap(pthread_t *thread, const pthread_attr_t *attr, |
| 180 | + void *(*start_routine) (void *), void *arg); |
| 181 | + |
| 182 | +int pthread_create_wrap(pthread_t *thread, const pthread_attr_t *attr, |
| 183 | + void *(*start_routine) (void *), void *arg) |
| 184 | +#else |
171 | 185 | int pthread_create(pthread_t *thread, const pthread_attr_t *attr, |
172 | 186 | void *(*start_routine) (void *), void *arg) |
| 187 | +#endif |
173 | 188 | { |
174 | 189 | int rv = -1; |
175 | 190 | su_threadstart_t *tstart = NULL; |
176 | 191 |
|
177 | 192 | if(real_pthread_create == NULL) |
178 | 193 | { |
| 194 | +#if defined(__APPLE__) |
| 195 | + real_pthread_create = pthread_create; |
| 196 | +#else |
179 | 197 | /* Get function ptr to real pthread_create */ |
180 | 198 | real_pthread_create = dlsym(RTLD_NEXT, "pthread_create"); |
181 | 199 | if(real_pthread_create == NULL) |
182 | 200 | { |
183 | 201 | SU_LOG_ERR; |
184 | 202 | } |
| 203 | +#endif |
185 | 204 |
|
186 | 205 | /* Initialize thread key with callback at thread termination */ |
187 | 206 | pthread_key_create(&threadkey, su_thread_fini); |
@@ -225,6 +244,9 @@ int pthread_create(pthread_t *thread, const pthread_attr_t *attr, |
225 | 244 |
|
226 | 245 | return rv; |
227 | 246 | } |
| 247 | +#if defined(__APPLE__) |
| 248 | +DYLD_INTERPOSE(pthread_create_wrap, pthread_create); |
| 249 | +#endif |
228 | 250 |
|
229 | 251 |
|
230 | 252 | /* ----------- Local Functions ----------------------------------- */ |
|
0 commit comments