sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1 | |
| 2 | /* This is a replacement for the standard libpthread.so. It is loaded |
| 3 | as part of the client's image (if required) and directs pthread |
| 4 | calls through to Valgrind's request mechanism. |
| 5 | |
| 6 | A couple of caveats. |
| 7 | |
| 8 | 1. Since it's a binary-compatible replacement for an existing library, |
| 9 | we must take care to used exactly the same data layouts, etc, as |
| 10 | the standard pthread.so does. |
| 11 | |
| 12 | 2. Since this runs as part of the client, there are no specific |
| 13 | restrictions on what headers etc we can include, so long as |
| 14 | this libpthread.so does not end up having dependencies on .so's |
| 15 | which the real one doesn't. |
| 16 | |
| 17 | Later ... it appears we cannot call file-related stuff in libc here, |
| 18 | perhaps fair enough. Be careful what you call from here. Even exit() |
| 19 | doesn't work (gives infinite recursion and then stack overflow); hence |
| 20 | myexit(). Also fprintf doesn't seem safe. |
| 21 | */ |
| 22 | |
| 23 | #include "valgrind.h" /* For the request-passing mechanism */ |
| 24 | #include "vg_include.h" /* For the VG_USERREQ__* constants */ |
| 25 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 26 | #include <unistd.h> |
| 27 | #include <string.h> |
| 28 | |
| 29 | |
| 30 | /* --------------------------------------------------------------------- |
| 31 | Helpers. We have to be pretty self-sufficient. |
| 32 | ------------------------------------------------------------------ */ |
| 33 | |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 34 | /* Extract from Valgrind the value of VG_(clo_trace_pthread_level). |
| 35 | Returns 0 (none) if not running on Valgrind. */ |
| 36 | static |
| 37 | int get_pt_trace_level ( void ) |
| 38 | { |
| 39 | int res; |
| 40 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 41 | VG_USERREQ__GET_PTHREAD_TRACE_LEVEL, |
| 42 | 0, 0, 0, 0); |
| 43 | return res; |
| 44 | } |
| 45 | |
| 46 | |
| 47 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 48 | static |
| 49 | void myexit ( int arg ) |
| 50 | { |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 51 | int __res; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 52 | __asm__ volatile ("movl %%ecx, %%ebx ; int $0x80" |
| 53 | : "=a" (__res) |
| 54 | : "0" (__NR_exit), |
| 55 | "c" (arg) ); |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 56 | /* We don't bother to mention the fact that this asm trashes %ebx, |
| 57 | since it won't return. If you ever do let it return ... fix |
| 58 | this! */ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | |
| 62 | /* Give up without using printf etc, since they seem to give |
| 63 | segfaults. */ |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 64 | static __inline__ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 65 | void ensure_valgrind ( char* caller ) |
| 66 | { |
| 67 | char* str; |
| 68 | int is_valgrind = RUNNING_ON_VALGRIND; |
| 69 | if (!is_valgrind) { |
| 70 | str = "\nvalgrind-ed process: vg_libpthread.so: " |
| 71 | "pthread call when\n"; |
| 72 | write(2, str, strlen(str)); |
| 73 | str = "not running on valgrind; aborting! " |
| 74 | "This is probably a bug in\n"; |
| 75 | write(2, str, strlen(str)); |
| 76 | str = "valgrind. Please report it to me at: " |
| 77 | "jseward@acm.org. Thanks.\n"; |
| 78 | write(2, str, strlen(str)); |
| 79 | str = "unexpectedly called function is: "; |
| 80 | write(2, str, strlen(str)); |
| 81 | write(2, caller, strlen(caller)); |
| 82 | str = "\n\n"; |
| 83 | write(2, str, strlen(str)); |
| 84 | myexit(1); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | |
| 89 | static |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 90 | __attribute__((noreturn)) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 91 | void barf ( char* str ) |
| 92 | { |
| 93 | char buf[100]; |
| 94 | buf[0] = 0; |
| 95 | strcat(buf, "\nvg_libpthread.so: "); |
| 96 | strcat(buf, str); |
| 97 | strcat(buf, "\n\n"); |
| 98 | write(2, buf, strlen(buf)); |
| 99 | myexit(1); |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 100 | /* We have to persuade gcc into believing this doesn't return. */ |
| 101 | while (1) { }; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | |
sewardj | 2a3d28c | 2002-04-14 13:27:00 +0000 | [diff] [blame] | 105 | static void ignored ( char* msg ) |
| 106 | { |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 107 | if (get_pt_trace_level() >= 1) { |
| 108 | char* ig = "vg_libpthread.so: IGNORED call to: "; |
| 109 | write(2, ig, strlen(ig)); |
| 110 | write(2, msg, strlen(msg)); |
| 111 | ig = "\n"; |
| 112 | write(2, ig, strlen(ig)); |
| 113 | } |
sewardj | 2a3d28c | 2002-04-14 13:27:00 +0000 | [diff] [blame] | 114 | } |
| 115 | |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 116 | static void kludged ( char* msg ) |
| 117 | { |
| 118 | if (get_pt_trace_level() >= 1) { |
| 119 | char* ig = "vg_libpthread.so: KLUDGED call to: "; |
| 120 | write(2, ig, strlen(ig)); |
| 121 | write(2, msg, strlen(msg)); |
| 122 | ig = "\n"; |
| 123 | write(2, ig, strlen(ig)); |
| 124 | } |
| 125 | } |
| 126 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 127 | |
| 128 | /* --------------------------------------------------------------------- |
| 129 | Pass pthread_ calls to Valgrind's request mechanism. |
| 130 | ------------------------------------------------------------------ */ |
| 131 | |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 132 | #include <pthread.h> |
| 133 | #include <stdio.h> |
| 134 | #include <errno.h> |
| 135 | |
| 136 | /* --------------------------------------------------- |
| 137 | THREAD ATTRIBUTES |
| 138 | ------------------------------------------------ */ |
| 139 | |
sewardj | 6af4b5d | 2002-04-16 04:40:49 +0000 | [diff] [blame] | 140 | int pthread_attr_init(pthread_attr_t *attr) |
| 141 | { |
| 142 | ignored("pthread_attr_init"); |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate) |
| 147 | { |
| 148 | ignored("pthread_attr_setdetachstate"); |
| 149 | return 0; |
| 150 | } |
| 151 | |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 152 | int pthread_attr_setinheritsched(pthread_attr_t *attr, int inherit) |
| 153 | { |
| 154 | ignored("pthread_attr_setinheritsched"); |
| 155 | return 0; |
| 156 | } |
sewardj | 6af4b5d | 2002-04-16 04:40:49 +0000 | [diff] [blame] | 157 | |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 158 | /* This is completely bogus. */ |
| 159 | int pthread_attr_getschedparam(const pthread_attr_t *attr, |
| 160 | struct sched_param *param) |
| 161 | { |
| 162 | kludged("pthread_attr_getschedparam"); |
| 163 | if (param) param->__sched_priority = 0; /* who knows */ |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | int pthread_attr_setschedparam(pthread_attr_t *attr, |
| 168 | const struct sched_param *param) |
| 169 | { |
| 170 | ignored("pthread_attr_setschedparam"); |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | int pthread_attr_destroy(pthread_attr_t *attr) |
| 175 | { |
| 176 | ignored("pthread_attr_destroy"); |
| 177 | return 0; |
| 178 | } |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 179 | |
| 180 | /* --------------------------------------------------- |
| 181 | THREADs |
| 182 | ------------------------------------------------ */ |
| 183 | |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 184 | int pthread_equal(pthread_t thread1, pthread_t thread2) |
| 185 | { |
| 186 | return thread1 == thread2 ? 1 : 0; |
| 187 | } |
| 188 | |
| 189 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 190 | int |
| 191 | pthread_create (pthread_t *__restrict __thread, |
| 192 | __const pthread_attr_t *__restrict __attr, |
| 193 | void *(*__start_routine) (void *), |
| 194 | void *__restrict __arg) |
| 195 | { |
| 196 | int res; |
| 197 | ensure_valgrind("pthread_create"); |
| 198 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 199 | VG_USERREQ__PTHREAD_CREATE, |
| 200 | __thread, __attr, __start_routine, __arg); |
| 201 | return res; |
| 202 | } |
| 203 | |
| 204 | |
| 205 | |
| 206 | int |
| 207 | pthread_join (pthread_t __th, void **__thread_return) |
| 208 | { |
| 209 | int res; |
| 210 | ensure_valgrind("pthread_join"); |
| 211 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 212 | VG_USERREQ__PTHREAD_JOIN, |
| 213 | __th, __thread_return, 0, 0); |
| 214 | return res; |
| 215 | } |
| 216 | |
| 217 | |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 218 | void pthread_exit(void *retval) |
| 219 | { |
| 220 | int res; |
| 221 | ensure_valgrind("pthread_exit"); |
| 222 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 223 | VG_USERREQ__PTHREAD_EXIT, |
| 224 | retval, 0, 0, 0); |
| 225 | /* Doesn't return! */ |
| 226 | /* However, we have to fool gcc into knowing that. */ |
| 227 | barf("pthread_exit: still alive after request?!"); |
| 228 | } |
| 229 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 230 | |
| 231 | static int thread_specific_errno[VG_N_THREADS]; |
| 232 | |
| 233 | int* __errno_location ( void ) |
| 234 | { |
| 235 | int tid; |
| 236 | ensure_valgrind("__errno_location"); |
| 237 | VALGRIND_MAGIC_SEQUENCE(tid, 0 /* default */, |
| 238 | VG_USERREQ__PTHREAD_GET_THREADID, |
| 239 | 0, 0, 0, 0); |
| 240 | /* 'cos I'm paranoid ... */ |
| 241 | if (tid < 0 || tid >= VG_N_THREADS) |
| 242 | barf("__errno_location: invalid ThreadId"); |
| 243 | return & thread_specific_errno[tid]; |
| 244 | } |
| 245 | |
| 246 | |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 247 | /* --------------------------------------------------- |
| 248 | MUTEX ATTRIBUTES |
| 249 | ------------------------------------------------ */ |
| 250 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 251 | int pthread_mutexattr_init(pthread_mutexattr_t *attr) |
| 252 | { |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 253 | attr->__mutexkind = PTHREAD_MUTEX_ERRORCHECK_NP; |
sewardj | 8937c81 | 2002-04-12 20:12:20 +0000 | [diff] [blame] | 254 | return 0; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 255 | } |
| 256 | |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 257 | int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type) |
| 258 | { |
| 259 | switch (type) { |
| 260 | case PTHREAD_MUTEX_TIMED_NP: |
| 261 | case PTHREAD_MUTEX_RECURSIVE_NP: |
| 262 | case PTHREAD_MUTEX_ERRORCHECK_NP: |
| 263 | case PTHREAD_MUTEX_ADAPTIVE_NP: |
| 264 | attr->__mutexkind = type; |
| 265 | return 0; |
| 266 | default: |
| 267 | return EINVAL; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | int pthread_mutexattr_destroy(pthread_mutexattr_t *attr) |
| 272 | { |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | |
| 277 | /* --------------------------------------------------- |
| 278 | MUTEXes |
| 279 | ------------------------------------------------ */ |
| 280 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 281 | int pthread_mutex_init(pthread_mutex_t *mutex, |
| 282 | const pthread_mutexattr_t *mutexattr) |
| 283 | { |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 284 | mutex->__m_count = 0; |
| 285 | mutex->__m_owner = (_pthread_descr)VG_INVALID_THREADID; |
| 286 | mutex->__m_kind = PTHREAD_MUTEX_ERRORCHECK_NP; |
| 287 | if (mutexattr) |
| 288 | mutex->__m_kind = mutexattr->__mutexkind; |
| 289 | return 0; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 290 | } |
| 291 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 292 | int pthread_mutex_lock(pthread_mutex_t *mutex) |
| 293 | { |
| 294 | int res; |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 295 | static int moans = 3; |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 296 | if (!(RUNNING_ON_VALGRIND) && moans-- > 0) { |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 297 | char* str = "pthread_mutex_lock-NOT-INSIDE-VALGRIND\n"; |
| 298 | write(2, str, strlen(str)); |
| 299 | return 0; |
| 300 | } else { |
| 301 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 302 | VG_USERREQ__PTHREAD_MUTEX_LOCK, |
| 303 | mutex, 0, 0, 0); |
| 304 | return res; |
| 305 | } |
| 306 | } |
| 307 | |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 308 | int pthread_mutex_trylock(pthread_mutex_t *mutex) |
| 309 | { |
| 310 | int res; |
| 311 | static int moans = 3; |
| 312 | if (!(RUNNING_ON_VALGRIND) && moans-- > 0) { |
| 313 | char* str = "pthread_mutex_trylock-NOT-INSIDE-VALGRIND\n"; |
| 314 | write(2, str, strlen(str)); |
| 315 | return 0; |
| 316 | } else { |
| 317 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 318 | VG_USERREQ__PTHREAD_MUTEX_TRYLOCK, |
| 319 | mutex, 0, 0, 0); |
| 320 | return res; |
| 321 | } |
| 322 | } |
| 323 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 324 | int pthread_mutex_unlock(pthread_mutex_t *mutex) |
| 325 | { |
| 326 | int res; |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 327 | static int moans = 3; |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 328 | if (!(RUNNING_ON_VALGRIND) && moans-- > 0) { |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 329 | char* str = "pthread_mutex_unlock-NOT-INSIDE-VALGRIND\n"; |
| 330 | write(2, str, strlen(str)); |
| 331 | return 0; |
| 332 | } else { |
| 333 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 334 | VG_USERREQ__PTHREAD_MUTEX_UNLOCK, |
| 335 | mutex, 0, 0, 0); |
| 336 | return res; |
| 337 | } |
| 338 | } |
| 339 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 340 | int pthread_mutex_destroy(pthread_mutex_t *mutex) |
| 341 | { |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 342 | /* Valgrind doesn't hold any resources on behalf of the mutex, so no |
| 343 | need to involve it. */ |
| 344 | if (mutex->__m_count > 0) |
| 345 | return EBUSY; |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 346 | mutex->__m_count = 0; |
| 347 | mutex->__m_owner = (_pthread_descr)VG_INVALID_THREADID; |
| 348 | mutex->__m_kind = PTHREAD_MUTEX_ERRORCHECK_NP; |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 349 | return 0; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 353 | /* --------------------------------------------------- |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 354 | CONDITION VARIABLES |
| 355 | ------------------------------------------------ */ |
| 356 | |
| 357 | /* LinuxThreads supports no attributes for conditions. Hence ... */ |
| 358 | |
| 359 | int pthread_condattr_init(pthread_condattr_t *attr) |
| 360 | { |
| 361 | return 0; |
| 362 | } |
| 363 | |
sewardj | 0738a59 | 2002-04-20 13:59:33 +0000 | [diff] [blame] | 364 | int pthread_condattr_destroy(pthread_condattr_t *attr) |
| 365 | { |
| 366 | return 0; |
| 367 | } |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 368 | |
| 369 | int pthread_cond_init( pthread_cond_t *cond, |
| 370 | const pthread_condattr_t *cond_attr) |
| 371 | { |
| 372 | cond->__c_waiting = (_pthread_descr)VG_INVALID_THREADID; |
| 373 | return 0; |
| 374 | } |
| 375 | |
| 376 | |
| 377 | /* --------------------------------------------------- |
| 378 | SCHEDULING |
| 379 | ------------------------------------------------ */ |
| 380 | |
| 381 | /* This is completely bogus. */ |
| 382 | int pthread_getschedparam(pthread_t target_thread, |
| 383 | int *policy, |
| 384 | struct sched_param *param) |
| 385 | { |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 386 | kludged("pthread_getschedparam"); |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 387 | if (policy) *policy = SCHED_OTHER; |
| 388 | if (param) param->__sched_priority = 0; /* who knows */ |
| 389 | return 0; |
| 390 | } |
| 391 | |
| 392 | int pthread_setschedparam(pthread_t target_thread, |
| 393 | int policy, |
| 394 | const struct sched_param *param) |
| 395 | { |
| 396 | ignored("pthread_setschedparam"); |
| 397 | return 0; |
| 398 | } |
| 399 | |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 400 | int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) |
| 401 | { |
| 402 | int res; |
| 403 | ensure_valgrind("pthread_cond_wait"); |
| 404 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 405 | VG_USERREQ__PTHREAD_COND_WAIT, |
| 406 | cond, mutex, 0, 0); |
| 407 | return res; |
| 408 | } |
| 409 | |
| 410 | int pthread_cond_signal(pthread_cond_t *cond) |
| 411 | { |
| 412 | int res; |
| 413 | ensure_valgrind("pthread_cond_signal"); |
| 414 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 415 | VG_USERREQ__PTHREAD_COND_SIGNAL, |
| 416 | cond, 0, 0, 0); |
| 417 | return res; |
| 418 | } |
| 419 | |
| 420 | int pthread_cond_broadcast(pthread_cond_t *cond) |
| 421 | { |
| 422 | int res; |
| 423 | ensure_valgrind("pthread_cond_broadcast"); |
| 424 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 425 | VG_USERREQ__PTHREAD_COND_BROADCAST, |
| 426 | cond, 0, 0, 0); |
| 427 | return res; |
| 428 | } |
| 429 | |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 430 | |
| 431 | /* --------------------------------------------------- |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 432 | CANCELLATION |
| 433 | ------------------------------------------------ */ |
| 434 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 435 | int pthread_setcanceltype(int type, int *oldtype) |
| 436 | { |
sewardj | 2a3d28c | 2002-04-14 13:27:00 +0000 | [diff] [blame] | 437 | ignored("pthread_setcanceltype"); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 438 | return 0; |
| 439 | } |
| 440 | |
| 441 | |
| 442 | int pthread_cancel(pthread_t thread) |
| 443 | { |
| 444 | int res; |
| 445 | ensure_valgrind("pthread_cancel"); |
| 446 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 447 | VG_USERREQ__PTHREAD_CANCEL, |
| 448 | thread, 0, 0, 0); |
| 449 | return res; |
| 450 | } |
| 451 | |
| 452 | |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 453 | /* --------------------------------------------------- |
| 454 | THREAD-SPECIFICs |
| 455 | ------------------------------------------------ */ |
sewardj | 5e5fa51 | 2002-04-14 13:13:05 +0000 | [diff] [blame] | 456 | |
| 457 | int pthread_key_create(pthread_key_t *key, |
| 458 | void (*destr_function) (void *)) |
| 459 | { |
sewardj | 2a3d28c | 2002-04-14 13:27:00 +0000 | [diff] [blame] | 460 | ignored("pthread_key_create"); |
sewardj | 5e5fa51 | 2002-04-14 13:13:05 +0000 | [diff] [blame] | 461 | return 0; |
| 462 | } |
| 463 | |
| 464 | int pthread_key_delete(pthread_key_t key) |
| 465 | { |
sewardj | 2a3d28c | 2002-04-14 13:27:00 +0000 | [diff] [blame] | 466 | ignored("pthread_key_delete"); |
sewardj | 5e5fa51 | 2002-04-14 13:13:05 +0000 | [diff] [blame] | 467 | return 0; |
| 468 | } |
| 469 | |
| 470 | int pthread_setspecific(pthread_key_t key, const void *pointer) |
| 471 | { |
sewardj | 2a3d28c | 2002-04-14 13:27:00 +0000 | [diff] [blame] | 472 | ignored("pthread_setspecific"); |
sewardj | 5e5fa51 | 2002-04-14 13:13:05 +0000 | [diff] [blame] | 473 | return 0; |
| 474 | } |
| 475 | |
| 476 | void * pthread_getspecific(pthread_key_t key) |
| 477 | { |
sewardj | 2a3d28c | 2002-04-14 13:27:00 +0000 | [diff] [blame] | 478 | ignored("pthread_setspecific"); |
sewardj | 5e5fa51 | 2002-04-14 13:13:05 +0000 | [diff] [blame] | 479 | return NULL; |
| 480 | } |
| 481 | |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 482 | |
| 483 | /* --------------------------------------------------- |
| 484 | MISC |
| 485 | ------------------------------------------------ */ |
| 486 | |
| 487 | /* What are these? Anybody know? I don't. */ |
| 488 | |
| 489 | void _pthread_cleanup_push_defer ( void ) |
| 490 | { |
| 491 | // char* str = "_pthread_cleanup_push_defer\n"; |
| 492 | // write(2, str, strlen(str)); |
| 493 | } |
| 494 | |
| 495 | void _pthread_cleanup_pop_restore ( void ) |
| 496 | { |
| 497 | // char* str = "_pthread_cleanup_pop_restore\n"; |
| 498 | // write(2, str, strlen(str)); |
| 499 | } |
| 500 | |
| 501 | |
| 502 | pthread_t pthread_self(void) |
| 503 | { |
| 504 | int tid; |
| 505 | ensure_valgrind("pthread_self"); |
| 506 | VALGRIND_MAGIC_SEQUENCE(tid, 0 /* default */, |
| 507 | VG_USERREQ__PTHREAD_GET_THREADID, |
| 508 | 0, 0, 0, 0); |
| 509 | if (tid < 0 || tid >= VG_N_THREADS) |
| 510 | barf("pthread_self: invalid ThreadId"); |
| 511 | return tid; |
| 512 | } |
| 513 | |
| 514 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 515 | /* --------------------------------------------------------------------- |
| 516 | These are here (I think) because they are deemed cancellation |
| 517 | points by POSIX. For the moment we'll simply pass the call along |
| 518 | to the corresponding thread-unaware (?) libc routine. |
| 519 | ------------------------------------------------------------------ */ |
| 520 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 521 | #include <stdlib.h> |
| 522 | #include <signal.h> |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 523 | #include <sys/types.h> |
| 524 | #include <sys/socket.h> |
| 525 | |
| 526 | extern |
| 527 | int __libc_sigaction |
| 528 | (int signum, |
| 529 | const struct sigaction *act, |
| 530 | struct sigaction *oldact); |
| 531 | int sigaction(int signum, |
| 532 | const struct sigaction *act, |
| 533 | struct sigaction *oldact) |
| 534 | { |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 535 | return __libc_sigaction(signum, act, oldact); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | |
| 539 | extern |
| 540 | int __libc_connect(int sockfd, |
| 541 | const struct sockaddr *serv_addr, |
| 542 | socklen_t addrlen); |
| 543 | int connect(int sockfd, |
| 544 | const struct sockaddr *serv_addr, |
| 545 | socklen_t addrlen) |
| 546 | { |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 547 | return __libc_connect(sockfd, serv_addr, addrlen); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | |
| 551 | extern |
| 552 | int __libc_fcntl(int fd, int cmd, long arg); |
| 553 | int fcntl(int fd, int cmd, long arg) |
| 554 | { |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 555 | return __libc_fcntl(fd, cmd, arg); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | |
| 559 | extern |
| 560 | ssize_t __libc_write(int fd, const void *buf, size_t count); |
| 561 | ssize_t write(int fd, const void *buf, size_t count) |
| 562 | { |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 563 | return __libc_write(fd, buf, count); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | |
| 567 | extern |
| 568 | ssize_t __libc_read(int fd, void *buf, size_t count); |
| 569 | ssize_t read(int fd, void *buf, size_t count) |
| 570 | { |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 571 | return __libc_read(fd, buf, count); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | |
| 575 | extern |
| 576 | int __libc_open(const char *pathname, int flags); |
| 577 | int open(const char *pathname, int flags) |
| 578 | { |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 579 | return __libc_open(pathname, flags); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | |
| 583 | extern |
| 584 | int __libc_close(int fd); |
| 585 | int close(int fd) |
| 586 | { |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 587 | return __libc_close(fd); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | |
| 591 | extern |
| 592 | int __libc_accept(int s, struct sockaddr *addr, socklen_t *addrlen); |
| 593 | int accept(int s, struct sockaddr *addr, socklen_t *addrlen) |
| 594 | { |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 595 | return __libc_accept(s, addr, addrlen); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | |
| 599 | extern |
| 600 | pid_t __libc_fork(void); |
| 601 | pid_t fork(void) |
| 602 | { |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 603 | return __libc_fork(); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | |
| 607 | extern |
| 608 | pid_t __libc_waitpid(pid_t pid, int *status, int options); |
| 609 | pid_t waitpid(pid_t pid, int *status, int options) |
| 610 | { |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 611 | return __libc_waitpid(pid, status, options); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | |
| 615 | extern |
| 616 | int __libc_nanosleep(const struct timespec *req, struct timespec *rem); |
| 617 | int nanosleep(const struct timespec *req, struct timespec *rem) |
| 618 | { |
| 619 | return __libc_nanosleep(req, rem); |
| 620 | } |
| 621 | |
| 622 | extern |
| 623 | int __libc_fsync(int fd); |
| 624 | int fsync(int fd) |
| 625 | { |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 626 | return __libc_fsync(fd); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 627 | } |
| 628 | |
sewardj | 70c7536 | 2002-04-13 04:18:32 +0000 | [diff] [blame] | 629 | extern |
| 630 | off_t __libc_lseek(int fildes, off_t offset, int whence); |
| 631 | off_t lseek(int fildes, off_t offset, int whence) |
| 632 | { |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 633 | return __libc_lseek(fildes, offset, whence); |
sewardj | 70c7536 | 2002-04-13 04:18:32 +0000 | [diff] [blame] | 634 | } |
| 635 | |
sewardj | 6af4b5d | 2002-04-16 04:40:49 +0000 | [diff] [blame] | 636 | extern |
| 637 | void __libc_longjmp(jmp_buf env, int val) __attribute((noreturn)); |
| 638 | void longjmp(jmp_buf env, int val) |
| 639 | { |
| 640 | __libc_longjmp(env, val); |
| 641 | } |
| 642 | |
| 643 | extern |
| 644 | int __libc_send(int s, const void *msg, size_t len, int flags); |
| 645 | int send(int s, const void *msg, size_t len, int flags) |
| 646 | { |
| 647 | return __libc_send(s, msg, len, flags); |
| 648 | } |
| 649 | |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 650 | extern |
| 651 | int __libc_recv(int s, void *buf, size_t len, int flags); |
| 652 | int recv(int s, void *buf, size_t len, int flags) |
| 653 | { |
| 654 | return __libc_recv(s, buf, len, flags); |
| 655 | } |
| 656 | |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 657 | |
sewardj | 70c7536 | 2002-04-13 04:18:32 +0000 | [diff] [blame] | 658 | /*--------------------------------------------------*/ |
| 659 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 660 | /* I've no idea what these are, but they get called quite a lot. |
| 661 | Anybody know? */ |
| 662 | |
| 663 | #undef _IO_flockfile |
| 664 | void _IO_flockfile ( _IO_FILE * file ) |
| 665 | { |
| 666 | // char* str = "_IO_flockfile\n"; |
| 667 | // write(2, str, strlen(str)); |
| 668 | // barf("_IO_flockfile"); |
| 669 | } |
| 670 | |
| 671 | #undef _IO_funlockfile |
| 672 | void _IO_funlockfile ( _IO_FILE * file ) |
| 673 | { |
| 674 | // char* str = "_IO_funlockfile\n"; |
| 675 | // write(2, str, strlen(str)); |
| 676 | //barf("_IO_funlockfile"); |
| 677 | } |
| 678 | |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 679 | /*--------------------------------------------------*/ |
| 680 | |
| 681 | #include "vg_kerneliface.h" |
| 682 | |
| 683 | static |
| 684 | __inline__ |
| 685 | int is_kerror ( int res ) |
| 686 | { |
| 687 | if (res >= -4095 && res <= -1) |
| 688 | return 1; |
| 689 | else |
| 690 | return 0; |
| 691 | } |
| 692 | |
| 693 | |
| 694 | static |
| 695 | int my_do_syscall1 ( int syscallno, int arg1 ) |
| 696 | { |
| 697 | int __res; |
| 698 | __asm__ volatile ("pushl %%ebx; movl %%edx,%%ebx ; int $0x80 ; popl %%ebx" |
| 699 | : "=a" (__res) |
| 700 | : "0" (syscallno), |
| 701 | "d" (arg1) ); |
| 702 | return __res; |
| 703 | } |
| 704 | |
| 705 | static |
| 706 | int my_do_syscall2 ( int syscallno, |
| 707 | int arg1, int arg2 ) |
| 708 | { |
| 709 | int __res; |
| 710 | __asm__ volatile ("pushl %%ebx; movl %%edx,%%ebx ; int $0x80 ; popl %%ebx" |
| 711 | : "=a" (__res) |
| 712 | : "0" (syscallno), |
| 713 | "d" (arg1), |
| 714 | "c" (arg2) ); |
| 715 | return __res; |
| 716 | } |
| 717 | |
| 718 | static |
| 719 | int do_syscall_select( int n, |
| 720 | vki_fd_set* readfds, |
| 721 | vki_fd_set* writefds, |
| 722 | vki_fd_set* exceptfds, |
| 723 | struct vki_timeval * timeout ) |
| 724 | { |
| 725 | int res; |
| 726 | int args[5]; |
| 727 | args[0] = n; |
| 728 | args[1] = (int)readfds; |
| 729 | args[2] = (int)writefds; |
| 730 | args[3] = (int)exceptfds; |
| 731 | args[4] = (int)timeout; |
| 732 | res = my_do_syscall1(__NR_select, (int)(&(args[0])) ); |
sewardj | 02535bc | 2002-04-21 01:08:26 +0000 | [diff] [blame^] | 733 | return res; |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | |
| 737 | /* This is a wrapper round select(), which makes it thread-safe, |
| 738 | meaning that only this thread will block, rather than the entire |
| 739 | process. This wrapper in turn depends on nanosleep() not to block |
| 740 | the entire process, but I think (hope? suspect?) that POSIX |
| 741 | pthreads guarantees that to be the case. |
| 742 | |
| 743 | Basic idea is: modify the timeout parameter to select so that it |
| 744 | returns immediately. Poll like this until select returns non-zero, |
| 745 | indicating something interesting happened, or until our time is up. |
| 746 | Space out the polls with nanosleeps of say 20 milliseconds, which |
| 747 | is required to be nonblocking; this allows other threads to run. |
sewardj | 02535bc | 2002-04-21 01:08:26 +0000 | [diff] [blame^] | 748 | |
| 749 | Assumes: |
| 750 | * (checked via assert) types fd_set and vki_fd_set are identical. |
| 751 | * (checked via assert) types timeval and vki_timeval are identical. |
| 752 | * (unchecked) libc error numbers (EINTR etc) are the negation of the |
| 753 | kernel's error numbers (VKI_EINTR etc). |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 754 | */ |
| 755 | #include <assert.h> |
| 756 | |
| 757 | |
| 758 | int select ( int n, |
| 759 | fd_set *rfds, |
| 760 | fd_set *wfds, |
| 761 | fd_set *xfds, |
| 762 | struct timeval *timeout ) |
| 763 | { |
| 764 | int res; |
| 765 | fd_set rfds_copy; |
| 766 | fd_set wfds_copy; |
| 767 | fd_set xfds_copy; |
| 768 | struct vki_timeval t_now; |
| 769 | struct vki_timeval t_end; |
| 770 | struct vki_timeval zero_timeout; |
| 771 | struct vki_timespec nanosleep_interval; |
sewardj | 02535bc | 2002-04-21 01:08:26 +0000 | [diff] [blame^] | 772 | int nanosleep_usec; |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 773 | |
| 774 | ensure_valgrind("select"); |
| 775 | |
| 776 | /* We assume that the kernel and libc data layouts are identical |
| 777 | for the following types. These asserts provide a crude |
| 778 | check. */ |
| 779 | if (sizeof(fd_set) != sizeof(vki_fd_set) |
| 780 | || sizeof(struct timeval) != sizeof(struct vki_timeval)) |
| 781 | barf("valgrind's hacky non-blocking select(): data sizes error"); |
| 782 | |
| 783 | /* If a zero timeout specified, this call is harmless. */ |
sewardj | 02535bc | 2002-04-21 01:08:26 +0000 | [diff] [blame^] | 784 | if (timeout && timeout->tv_sec == 0 && timeout->tv_usec == 0) { |
| 785 | res = do_syscall_select( n, (vki_fd_set*)rfds, |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 786 | (vki_fd_set*)wfds, |
| 787 | (vki_fd_set*)xfds, |
| 788 | (struct vki_timeval*)timeout); |
sewardj | 02535bc | 2002-04-21 01:08:26 +0000 | [diff] [blame^] | 789 | if (is_kerror(res)) { |
| 790 | * (__errno_location()) = -res; |
| 791 | return -1; |
| 792 | } else { |
| 793 | return res; |
| 794 | } |
| 795 | } |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 796 | |
| 797 | /* If a timeout was specified, set t_end to be the end wallclock |
| 798 | time. */ |
sewardj | 02535bc | 2002-04-21 01:08:26 +0000 | [diff] [blame^] | 799 | |
| 800 | /* Sleep in 50 millisecond intervals, unless the total duration is |
| 801 | one second or less, in which case sleep in 20 millisecond |
| 802 | intervals. */ |
| 803 | nanosleep_usec = 50 * 1000; |
| 804 | |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 805 | if (timeout) { |
| 806 | res = my_do_syscall2(__NR_gettimeofday, (int)&t_now, (int)NULL); |
| 807 | assert(res == 0); |
| 808 | t_end = t_now; |
| 809 | t_end.tv_usec += timeout->tv_usec; |
| 810 | t_end.tv_sec += timeout->tv_sec; |
| 811 | if (t_end.tv_usec >= 1000000) { |
| 812 | t_end.tv_usec -= 1000000; |
| 813 | t_end.tv_sec += 1; |
| 814 | } |
| 815 | /* Stay sane ... */ |
| 816 | assert (t_end.tv_sec > t_now.tv_sec |
| 817 | || (t_end.tv_sec == t_now.tv_sec |
| 818 | && t_end.tv_usec >= t_now.tv_usec)); |
sewardj | 02535bc | 2002-04-21 01:08:26 +0000 | [diff] [blame^] | 819 | if (timeout->tv_sec == 0) |
| 820 | nanosleep_usec = 20 * 1000; |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | /* fprintf(stderr, "MY_SELECT: before loop\n"); */ |
| 824 | |
| 825 | /* Either timeout == NULL, meaning wait indefinitely, or timeout != |
| 826 | NULL, in which case t_end holds the end time. */ |
| 827 | while (1) { |
| 828 | if (timeout) { |
| 829 | res = my_do_syscall2(__NR_gettimeofday, (int)&t_now, (int)NULL); |
| 830 | assert(res == 0); |
| 831 | if (t_now.tv_sec > t_end.tv_sec |
| 832 | || (t_now.tv_sec == t_end.tv_sec |
| 833 | && t_now.tv_usec > t_end.tv_usec)) { |
| 834 | /* timeout; nothing interesting happened. */ |
| 835 | if (rfds) FD_ZERO(rfds); |
| 836 | if (wfds) FD_ZERO(wfds); |
| 837 | if (xfds) FD_ZERO(xfds); |
| 838 | return 0; |
| 839 | } |
| 840 | } |
| 841 | |
| 842 | /* These could be trashed each time round the loop, so restore |
| 843 | them each time. */ |
| 844 | if (rfds) rfds_copy = *rfds; |
| 845 | if (wfds) wfds_copy = *wfds; |
| 846 | if (xfds) xfds_copy = *xfds; |
| 847 | |
| 848 | zero_timeout.tv_sec = zero_timeout.tv_usec = 0; |
| 849 | |
| 850 | res = do_syscall_select( n, |
| 851 | rfds ? (vki_fd_set*)(&rfds_copy) : NULL, |
| 852 | wfds ? (vki_fd_set*)(&wfds_copy) : NULL, |
| 853 | xfds ? (vki_fd_set*)(&xfds_copy) : NULL, |
| 854 | & zero_timeout ); |
sewardj | 02535bc | 2002-04-21 01:08:26 +0000 | [diff] [blame^] | 855 | if (is_kerror(res)) { |
| 856 | /* Some kind of error (including EINTR). Set errno and |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 857 | return. The sets are unspecified in this case. */ |
sewardj | 02535bc | 2002-04-21 01:08:26 +0000 | [diff] [blame^] | 858 | * (__errno_location()) = -res; |
| 859 | return -1; |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 860 | } |
| 861 | if (res > 0) { |
| 862 | /* one or more fds is ready. Copy out resulting sets and |
| 863 | return. */ |
| 864 | if (rfds) *rfds = rfds_copy; |
| 865 | if (wfds) *wfds = wfds_copy; |
| 866 | if (xfds) *xfds = xfds_copy; |
| 867 | return res; |
| 868 | } |
| 869 | /* fprintf(stderr, "MY_SELECT: nanosleep\n"); */ |
| 870 | /* nanosleep and go round again */ |
sewardj | 02535bc | 2002-04-21 01:08:26 +0000 | [diff] [blame^] | 871 | nanosleep_interval.tv_sec = 0; |
| 872 | nanosleep_interval.tv_nsec = 1000 * nanosleep_usec; |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 873 | /* It's critical here that valgrind's nanosleep implementation |
| 874 | is nonblocking. */ |
| 875 | (void)my_do_syscall2(__NR_nanosleep, |
| 876 | (int)(&nanosleep_interval), (int)NULL); |
| 877 | } |
| 878 | } |