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 | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 116 | |
| 117 | /* --------------------------------------------------------------------- |
| 118 | Pass pthread_ calls to Valgrind's request mechanism. |
| 119 | ------------------------------------------------------------------ */ |
| 120 | |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 121 | #include <pthread.h> |
| 122 | #include <stdio.h> |
| 123 | #include <errno.h> |
| 124 | |
| 125 | /* --------------------------------------------------- |
| 126 | THREAD ATTRIBUTES |
| 127 | ------------------------------------------------ */ |
| 128 | |
sewardj | 6af4b5d | 2002-04-16 04:40:49 +0000 | [diff] [blame] | 129 | int pthread_attr_init(pthread_attr_t *attr) |
| 130 | { |
| 131 | ignored("pthread_attr_init"); |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate) |
| 136 | { |
| 137 | ignored("pthread_attr_setdetachstate"); |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 142 | |
| 143 | /* --------------------------------------------------- |
| 144 | THREADs |
| 145 | ------------------------------------------------ */ |
| 146 | |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 147 | int pthread_equal(pthread_t thread1, pthread_t thread2) |
| 148 | { |
| 149 | return thread1 == thread2 ? 1 : 0; |
| 150 | } |
| 151 | |
| 152 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 153 | int |
| 154 | pthread_create (pthread_t *__restrict __thread, |
| 155 | __const pthread_attr_t *__restrict __attr, |
| 156 | void *(*__start_routine) (void *), |
| 157 | void *__restrict __arg) |
| 158 | { |
| 159 | int res; |
| 160 | ensure_valgrind("pthread_create"); |
| 161 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 162 | VG_USERREQ__PTHREAD_CREATE, |
| 163 | __thread, __attr, __start_routine, __arg); |
| 164 | return res; |
| 165 | } |
| 166 | |
| 167 | |
| 168 | |
| 169 | int |
| 170 | pthread_join (pthread_t __th, void **__thread_return) |
| 171 | { |
| 172 | int res; |
| 173 | ensure_valgrind("pthread_join"); |
| 174 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 175 | VG_USERREQ__PTHREAD_JOIN, |
| 176 | __th, __thread_return, 0, 0); |
| 177 | return res; |
| 178 | } |
| 179 | |
| 180 | |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame^] | 181 | void pthread_exit(void *retval) |
| 182 | { |
| 183 | int res; |
| 184 | ensure_valgrind("pthread_exit"); |
| 185 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 186 | VG_USERREQ__PTHREAD_EXIT, |
| 187 | retval, 0, 0, 0); |
| 188 | /* Doesn't return! */ |
| 189 | /* However, we have to fool gcc into knowing that. */ |
| 190 | barf("pthread_exit: still alive after request?!"); |
| 191 | } |
| 192 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 193 | |
| 194 | static int thread_specific_errno[VG_N_THREADS]; |
| 195 | |
| 196 | int* __errno_location ( void ) |
| 197 | { |
| 198 | int tid; |
| 199 | ensure_valgrind("__errno_location"); |
| 200 | VALGRIND_MAGIC_SEQUENCE(tid, 0 /* default */, |
| 201 | VG_USERREQ__PTHREAD_GET_THREADID, |
| 202 | 0, 0, 0, 0); |
| 203 | /* 'cos I'm paranoid ... */ |
| 204 | if (tid < 0 || tid >= VG_N_THREADS) |
| 205 | barf("__errno_location: invalid ThreadId"); |
| 206 | return & thread_specific_errno[tid]; |
| 207 | } |
| 208 | |
| 209 | |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 210 | /* --------------------------------------------------- |
| 211 | MUTEX ATTRIBUTES |
| 212 | ------------------------------------------------ */ |
| 213 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 214 | int pthread_mutexattr_init(pthread_mutexattr_t *attr) |
| 215 | { |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 216 | attr->__mutexkind = PTHREAD_MUTEX_ERRORCHECK_NP; |
sewardj | 8937c81 | 2002-04-12 20:12:20 +0000 | [diff] [blame] | 217 | return 0; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 218 | } |
| 219 | |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 220 | int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type) |
| 221 | { |
| 222 | switch (type) { |
| 223 | case PTHREAD_MUTEX_TIMED_NP: |
| 224 | case PTHREAD_MUTEX_RECURSIVE_NP: |
| 225 | case PTHREAD_MUTEX_ERRORCHECK_NP: |
| 226 | case PTHREAD_MUTEX_ADAPTIVE_NP: |
| 227 | attr->__mutexkind = type; |
| 228 | return 0; |
| 229 | default: |
| 230 | return EINVAL; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | int pthread_mutexattr_destroy(pthread_mutexattr_t *attr) |
| 235 | { |
| 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | |
| 240 | /* --------------------------------------------------- |
| 241 | MUTEXes |
| 242 | ------------------------------------------------ */ |
| 243 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 244 | int pthread_mutex_init(pthread_mutex_t *mutex, |
| 245 | const pthread_mutexattr_t *mutexattr) |
| 246 | { |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 247 | mutex->__m_count = 0; |
| 248 | mutex->__m_owner = (_pthread_descr)VG_INVALID_THREADID; |
| 249 | mutex->__m_kind = PTHREAD_MUTEX_ERRORCHECK_NP; |
| 250 | if (mutexattr) |
| 251 | mutex->__m_kind = mutexattr->__mutexkind; |
| 252 | return 0; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 253 | } |
| 254 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 255 | int pthread_mutex_lock(pthread_mutex_t *mutex) |
| 256 | { |
| 257 | int res; |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 258 | static int moans = 3; |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 259 | if (!(RUNNING_ON_VALGRIND) && moans-- > 0) { |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 260 | char* str = "pthread_mutex_lock-NOT-INSIDE-VALGRIND\n"; |
| 261 | write(2, str, strlen(str)); |
| 262 | return 0; |
| 263 | } else { |
| 264 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 265 | VG_USERREQ__PTHREAD_MUTEX_LOCK, |
| 266 | mutex, 0, 0, 0); |
| 267 | return res; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | int pthread_mutex_unlock(pthread_mutex_t *mutex) |
| 272 | { |
| 273 | int res; |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 274 | static int moans = 3; |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 275 | if (!(RUNNING_ON_VALGRIND) && moans-- > 0) { |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 276 | char* str = "pthread_mutex_unlock-NOT-INSIDE-VALGRIND\n"; |
| 277 | write(2, str, strlen(str)); |
| 278 | return 0; |
| 279 | } else { |
| 280 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 281 | VG_USERREQ__PTHREAD_MUTEX_UNLOCK, |
| 282 | mutex, 0, 0, 0); |
| 283 | return res; |
| 284 | } |
| 285 | } |
| 286 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 287 | int pthread_mutex_destroy(pthread_mutex_t *mutex) |
| 288 | { |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 289 | /* Valgrind doesn't hold any resources on behalf of the mutex, so no |
| 290 | need to involve it. */ |
| 291 | if (mutex->__m_count > 0) |
| 292 | return EBUSY; |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 293 | mutex->__m_count = 0; |
| 294 | mutex->__m_owner = (_pthread_descr)VG_INVALID_THREADID; |
| 295 | mutex->__m_kind = PTHREAD_MUTEX_ERRORCHECK_NP; |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 296 | return 0; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 300 | /* --------------------------------------------------- |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 301 | CONDITION VARIABLES |
| 302 | ------------------------------------------------ */ |
| 303 | |
| 304 | /* LinuxThreads supports no attributes for conditions. Hence ... */ |
| 305 | |
| 306 | int pthread_condattr_init(pthread_condattr_t *attr) |
| 307 | { |
| 308 | return 0; |
| 309 | } |
| 310 | |
| 311 | |
| 312 | int pthread_cond_init( pthread_cond_t *cond, |
| 313 | const pthread_condattr_t *cond_attr) |
| 314 | { |
| 315 | cond->__c_waiting = (_pthread_descr)VG_INVALID_THREADID; |
| 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | |
| 320 | /* --------------------------------------------------- |
| 321 | SCHEDULING |
| 322 | ------------------------------------------------ */ |
| 323 | |
| 324 | /* This is completely bogus. */ |
| 325 | int pthread_getschedparam(pthread_t target_thread, |
| 326 | int *policy, |
| 327 | struct sched_param *param) |
| 328 | { |
| 329 | if (policy) *policy = SCHED_OTHER; |
| 330 | if (param) param->__sched_priority = 0; /* who knows */ |
| 331 | return 0; |
| 332 | } |
| 333 | |
| 334 | int pthread_setschedparam(pthread_t target_thread, |
| 335 | int policy, |
| 336 | const struct sched_param *param) |
| 337 | { |
| 338 | ignored("pthread_setschedparam"); |
| 339 | return 0; |
| 340 | } |
| 341 | |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame^] | 342 | int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) |
| 343 | { |
| 344 | int res; |
| 345 | ensure_valgrind("pthread_cond_wait"); |
| 346 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 347 | VG_USERREQ__PTHREAD_COND_WAIT, |
| 348 | cond, mutex, 0, 0); |
| 349 | return res; |
| 350 | } |
| 351 | |
| 352 | int pthread_cond_signal(pthread_cond_t *cond) |
| 353 | { |
| 354 | int res; |
| 355 | ensure_valgrind("pthread_cond_signal"); |
| 356 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 357 | VG_USERREQ__PTHREAD_COND_SIGNAL, |
| 358 | cond, 0, 0, 0); |
| 359 | return res; |
| 360 | } |
| 361 | |
| 362 | int pthread_cond_broadcast(pthread_cond_t *cond) |
| 363 | { |
| 364 | int res; |
| 365 | ensure_valgrind("pthread_cond_broadcast"); |
| 366 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 367 | VG_USERREQ__PTHREAD_COND_BROADCAST, |
| 368 | cond, 0, 0, 0); |
| 369 | return res; |
| 370 | } |
| 371 | |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 372 | |
| 373 | /* --------------------------------------------------- |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 374 | CANCELLATION |
| 375 | ------------------------------------------------ */ |
| 376 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 377 | int pthread_setcanceltype(int type, int *oldtype) |
| 378 | { |
sewardj | 2a3d28c | 2002-04-14 13:27:00 +0000 | [diff] [blame] | 379 | ignored("pthread_setcanceltype"); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 380 | return 0; |
| 381 | } |
| 382 | |
| 383 | |
| 384 | int pthread_cancel(pthread_t thread) |
| 385 | { |
| 386 | int res; |
| 387 | ensure_valgrind("pthread_cancel"); |
| 388 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 389 | VG_USERREQ__PTHREAD_CANCEL, |
| 390 | thread, 0, 0, 0); |
| 391 | return res; |
| 392 | } |
| 393 | |
| 394 | |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 395 | /* --------------------------------------------------- |
| 396 | THREAD-SPECIFICs |
| 397 | ------------------------------------------------ */ |
sewardj | 5e5fa51 | 2002-04-14 13:13:05 +0000 | [diff] [blame] | 398 | |
| 399 | int pthread_key_create(pthread_key_t *key, |
| 400 | void (*destr_function) (void *)) |
| 401 | { |
sewardj | 2a3d28c | 2002-04-14 13:27:00 +0000 | [diff] [blame] | 402 | ignored("pthread_key_create"); |
sewardj | 5e5fa51 | 2002-04-14 13:13:05 +0000 | [diff] [blame] | 403 | return 0; |
| 404 | } |
| 405 | |
| 406 | int pthread_key_delete(pthread_key_t key) |
| 407 | { |
sewardj | 2a3d28c | 2002-04-14 13:27:00 +0000 | [diff] [blame] | 408 | ignored("pthread_key_delete"); |
sewardj | 5e5fa51 | 2002-04-14 13:13:05 +0000 | [diff] [blame] | 409 | return 0; |
| 410 | } |
| 411 | |
| 412 | int pthread_setspecific(pthread_key_t key, const void *pointer) |
| 413 | { |
sewardj | 2a3d28c | 2002-04-14 13:27:00 +0000 | [diff] [blame] | 414 | ignored("pthread_setspecific"); |
sewardj | 5e5fa51 | 2002-04-14 13:13:05 +0000 | [diff] [blame] | 415 | return 0; |
| 416 | } |
| 417 | |
| 418 | void * pthread_getspecific(pthread_key_t key) |
| 419 | { |
sewardj | 2a3d28c | 2002-04-14 13:27:00 +0000 | [diff] [blame] | 420 | ignored("pthread_setspecific"); |
sewardj | 5e5fa51 | 2002-04-14 13:13:05 +0000 | [diff] [blame] | 421 | return NULL; |
| 422 | } |
| 423 | |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 424 | |
| 425 | /* --------------------------------------------------- |
| 426 | MISC |
| 427 | ------------------------------------------------ */ |
| 428 | |
| 429 | /* What are these? Anybody know? I don't. */ |
| 430 | |
| 431 | void _pthread_cleanup_push_defer ( void ) |
| 432 | { |
| 433 | // char* str = "_pthread_cleanup_push_defer\n"; |
| 434 | // write(2, str, strlen(str)); |
| 435 | } |
| 436 | |
| 437 | void _pthread_cleanup_pop_restore ( void ) |
| 438 | { |
| 439 | // char* str = "_pthread_cleanup_pop_restore\n"; |
| 440 | // write(2, str, strlen(str)); |
| 441 | } |
| 442 | |
| 443 | |
| 444 | pthread_t pthread_self(void) |
| 445 | { |
| 446 | int tid; |
| 447 | ensure_valgrind("pthread_self"); |
| 448 | VALGRIND_MAGIC_SEQUENCE(tid, 0 /* default */, |
| 449 | VG_USERREQ__PTHREAD_GET_THREADID, |
| 450 | 0, 0, 0, 0); |
| 451 | if (tid < 0 || tid >= VG_N_THREADS) |
| 452 | barf("pthread_self: invalid ThreadId"); |
| 453 | return tid; |
| 454 | } |
| 455 | |
| 456 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 457 | /* --------------------------------------------------------------------- |
| 458 | These are here (I think) because they are deemed cancellation |
| 459 | points by POSIX. For the moment we'll simply pass the call along |
| 460 | to the corresponding thread-unaware (?) libc routine. |
| 461 | ------------------------------------------------------------------ */ |
| 462 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 463 | #include <stdlib.h> |
| 464 | #include <signal.h> |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 465 | #include <sys/types.h> |
| 466 | #include <sys/socket.h> |
| 467 | |
| 468 | extern |
| 469 | int __libc_sigaction |
| 470 | (int signum, |
| 471 | const struct sigaction *act, |
| 472 | struct sigaction *oldact); |
| 473 | int sigaction(int signum, |
| 474 | const struct sigaction *act, |
| 475 | struct sigaction *oldact) |
| 476 | { |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 477 | return __libc_sigaction(signum, act, oldact); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | |
| 481 | extern |
| 482 | int __libc_connect(int sockfd, |
| 483 | const struct sockaddr *serv_addr, |
| 484 | socklen_t addrlen); |
| 485 | int connect(int sockfd, |
| 486 | const struct sockaddr *serv_addr, |
| 487 | socklen_t addrlen) |
| 488 | { |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 489 | return __libc_connect(sockfd, serv_addr, addrlen); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | |
| 493 | extern |
| 494 | int __libc_fcntl(int fd, int cmd, long arg); |
| 495 | int fcntl(int fd, int cmd, long arg) |
| 496 | { |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 497 | return __libc_fcntl(fd, cmd, arg); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | |
| 501 | extern |
| 502 | ssize_t __libc_write(int fd, const void *buf, size_t count); |
| 503 | ssize_t write(int fd, const void *buf, size_t count) |
| 504 | { |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 505 | return __libc_write(fd, buf, count); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | |
| 509 | extern |
| 510 | ssize_t __libc_read(int fd, void *buf, size_t count); |
| 511 | ssize_t read(int fd, void *buf, size_t count) |
| 512 | { |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 513 | return __libc_read(fd, buf, count); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | |
| 517 | extern |
| 518 | int __libc_open(const char *pathname, int flags); |
| 519 | int open(const char *pathname, int flags) |
| 520 | { |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 521 | return __libc_open(pathname, flags); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | |
| 525 | extern |
| 526 | int __libc_close(int fd); |
| 527 | int close(int fd) |
| 528 | { |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 529 | return __libc_close(fd); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | |
| 533 | extern |
| 534 | int __libc_accept(int s, struct sockaddr *addr, socklen_t *addrlen); |
| 535 | int accept(int s, struct sockaddr *addr, socklen_t *addrlen) |
| 536 | { |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 537 | return __libc_accept(s, addr, addrlen); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | |
| 541 | extern |
| 542 | pid_t __libc_fork(void); |
| 543 | pid_t fork(void) |
| 544 | { |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 545 | return __libc_fork(); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | |
| 549 | extern |
| 550 | pid_t __libc_waitpid(pid_t pid, int *status, int options); |
| 551 | pid_t waitpid(pid_t pid, int *status, int options) |
| 552 | { |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 553 | return __libc_waitpid(pid, status, options); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | |
| 557 | extern |
| 558 | int __libc_nanosleep(const struct timespec *req, struct timespec *rem); |
| 559 | int nanosleep(const struct timespec *req, struct timespec *rem) |
| 560 | { |
| 561 | return __libc_nanosleep(req, rem); |
| 562 | } |
| 563 | |
| 564 | extern |
| 565 | int __libc_fsync(int fd); |
| 566 | int fsync(int fd) |
| 567 | { |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 568 | return __libc_fsync(fd); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 569 | } |
| 570 | |
sewardj | 70c7536 | 2002-04-13 04:18:32 +0000 | [diff] [blame] | 571 | extern |
| 572 | off_t __libc_lseek(int fildes, off_t offset, int whence); |
| 573 | off_t lseek(int fildes, off_t offset, int whence) |
| 574 | { |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 575 | return __libc_lseek(fildes, offset, whence); |
sewardj | 70c7536 | 2002-04-13 04:18:32 +0000 | [diff] [blame] | 576 | } |
| 577 | |
sewardj | 6af4b5d | 2002-04-16 04:40:49 +0000 | [diff] [blame] | 578 | extern |
| 579 | void __libc_longjmp(jmp_buf env, int val) __attribute((noreturn)); |
| 580 | void longjmp(jmp_buf env, int val) |
| 581 | { |
| 582 | __libc_longjmp(env, val); |
| 583 | } |
| 584 | |
| 585 | extern |
| 586 | int __libc_send(int s, const void *msg, size_t len, int flags); |
| 587 | int send(int s, const void *msg, size_t len, int flags) |
| 588 | { |
| 589 | return __libc_send(s, msg, len, flags); |
| 590 | } |
| 591 | |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 592 | extern |
| 593 | int __libc_recv(int s, void *buf, size_t len, int flags); |
| 594 | int recv(int s, void *buf, size_t len, int flags) |
| 595 | { |
| 596 | return __libc_recv(s, buf, len, flags); |
| 597 | } |
| 598 | |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 599 | |
sewardj | 70c7536 | 2002-04-13 04:18:32 +0000 | [diff] [blame] | 600 | /*--------------------------------------------------*/ |
| 601 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 602 | /* I've no idea what these are, but they get called quite a lot. |
| 603 | Anybody know? */ |
| 604 | |
| 605 | #undef _IO_flockfile |
| 606 | void _IO_flockfile ( _IO_FILE * file ) |
| 607 | { |
| 608 | // char* str = "_IO_flockfile\n"; |
| 609 | // write(2, str, strlen(str)); |
| 610 | // barf("_IO_flockfile"); |
| 611 | } |
| 612 | |
| 613 | #undef _IO_funlockfile |
| 614 | void _IO_funlockfile ( _IO_FILE * file ) |
| 615 | { |
| 616 | // char* str = "_IO_funlockfile\n"; |
| 617 | // write(2, str, strlen(str)); |
| 618 | //barf("_IO_funlockfile"); |
| 619 | } |
| 620 | |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 621 | /*--------------------------------------------------*/ |
| 622 | |
| 623 | #include "vg_kerneliface.h" |
| 624 | |
| 625 | static |
| 626 | __inline__ |
| 627 | int is_kerror ( int res ) |
| 628 | { |
| 629 | if (res >= -4095 && res <= -1) |
| 630 | return 1; |
| 631 | else |
| 632 | return 0; |
| 633 | } |
| 634 | |
| 635 | |
| 636 | static |
| 637 | int my_do_syscall1 ( int syscallno, int arg1 ) |
| 638 | { |
| 639 | int __res; |
| 640 | __asm__ volatile ("pushl %%ebx; movl %%edx,%%ebx ; int $0x80 ; popl %%ebx" |
| 641 | : "=a" (__res) |
| 642 | : "0" (syscallno), |
| 643 | "d" (arg1) ); |
| 644 | return __res; |
| 645 | } |
| 646 | |
| 647 | static |
| 648 | int my_do_syscall2 ( int syscallno, |
| 649 | int arg1, int arg2 ) |
| 650 | { |
| 651 | int __res; |
| 652 | __asm__ volatile ("pushl %%ebx; movl %%edx,%%ebx ; int $0x80 ; popl %%ebx" |
| 653 | : "=a" (__res) |
| 654 | : "0" (syscallno), |
| 655 | "d" (arg1), |
| 656 | "c" (arg2) ); |
| 657 | return __res; |
| 658 | } |
| 659 | |
| 660 | static |
| 661 | int do_syscall_select( int n, |
| 662 | vki_fd_set* readfds, |
| 663 | vki_fd_set* writefds, |
| 664 | vki_fd_set* exceptfds, |
| 665 | struct vki_timeval * timeout ) |
| 666 | { |
| 667 | int res; |
| 668 | int args[5]; |
| 669 | args[0] = n; |
| 670 | args[1] = (int)readfds; |
| 671 | args[2] = (int)writefds; |
| 672 | args[3] = (int)exceptfds; |
| 673 | args[4] = (int)timeout; |
| 674 | res = my_do_syscall1(__NR_select, (int)(&(args[0])) ); |
| 675 | if (is_kerror(res)) { |
| 676 | * (__errno_location()) = -res; |
| 677 | return -1; |
| 678 | } else { |
| 679 | return res; |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | |
| 684 | /* This is a wrapper round select(), which makes it thread-safe, |
| 685 | meaning that only this thread will block, rather than the entire |
| 686 | process. This wrapper in turn depends on nanosleep() not to block |
| 687 | the entire process, but I think (hope? suspect?) that POSIX |
| 688 | pthreads guarantees that to be the case. |
| 689 | |
| 690 | Basic idea is: modify the timeout parameter to select so that it |
| 691 | returns immediately. Poll like this until select returns non-zero, |
| 692 | indicating something interesting happened, or until our time is up. |
| 693 | Space out the polls with nanosleeps of say 20 milliseconds, which |
| 694 | is required to be nonblocking; this allows other threads to run. |
| 695 | */ |
| 696 | #include <assert.h> |
| 697 | |
| 698 | |
| 699 | int select ( int n, |
| 700 | fd_set *rfds, |
| 701 | fd_set *wfds, |
| 702 | fd_set *xfds, |
| 703 | struct timeval *timeout ) |
| 704 | { |
| 705 | int res; |
| 706 | fd_set rfds_copy; |
| 707 | fd_set wfds_copy; |
| 708 | fd_set xfds_copy; |
| 709 | struct vki_timeval t_now; |
| 710 | struct vki_timeval t_end; |
| 711 | struct vki_timeval zero_timeout; |
| 712 | struct vki_timespec nanosleep_interval; |
| 713 | |
| 714 | ensure_valgrind("select"); |
| 715 | |
| 716 | /* We assume that the kernel and libc data layouts are identical |
| 717 | for the following types. These asserts provide a crude |
| 718 | check. */ |
| 719 | if (sizeof(fd_set) != sizeof(vki_fd_set) |
| 720 | || sizeof(struct timeval) != sizeof(struct vki_timeval)) |
| 721 | barf("valgrind's hacky non-blocking select(): data sizes error"); |
| 722 | |
| 723 | /* If a zero timeout specified, this call is harmless. */ |
| 724 | if (timeout && timeout->tv_sec == 0 && timeout->tv_usec == 0) |
| 725 | return do_syscall_select( n, (vki_fd_set*)rfds, |
| 726 | (vki_fd_set*)wfds, |
| 727 | (vki_fd_set*)xfds, |
| 728 | (struct vki_timeval*)timeout); |
| 729 | |
| 730 | /* If a timeout was specified, set t_end to be the end wallclock |
| 731 | time. */ |
| 732 | if (timeout) { |
| 733 | res = my_do_syscall2(__NR_gettimeofday, (int)&t_now, (int)NULL); |
| 734 | assert(res == 0); |
| 735 | t_end = t_now; |
| 736 | t_end.tv_usec += timeout->tv_usec; |
| 737 | t_end.tv_sec += timeout->tv_sec; |
| 738 | if (t_end.tv_usec >= 1000000) { |
| 739 | t_end.tv_usec -= 1000000; |
| 740 | t_end.tv_sec += 1; |
| 741 | } |
| 742 | /* Stay sane ... */ |
| 743 | assert (t_end.tv_sec > t_now.tv_sec |
| 744 | || (t_end.tv_sec == t_now.tv_sec |
| 745 | && t_end.tv_usec >= t_now.tv_usec)); |
| 746 | } |
| 747 | |
| 748 | /* fprintf(stderr, "MY_SELECT: before loop\n"); */ |
| 749 | |
| 750 | /* Either timeout == NULL, meaning wait indefinitely, or timeout != |
| 751 | NULL, in which case t_end holds the end time. */ |
| 752 | while (1) { |
| 753 | if (timeout) { |
| 754 | res = my_do_syscall2(__NR_gettimeofday, (int)&t_now, (int)NULL); |
| 755 | assert(res == 0); |
| 756 | if (t_now.tv_sec > t_end.tv_sec |
| 757 | || (t_now.tv_sec == t_end.tv_sec |
| 758 | && t_now.tv_usec > t_end.tv_usec)) { |
| 759 | /* timeout; nothing interesting happened. */ |
| 760 | if (rfds) FD_ZERO(rfds); |
| 761 | if (wfds) FD_ZERO(wfds); |
| 762 | if (xfds) FD_ZERO(xfds); |
| 763 | return 0; |
| 764 | } |
| 765 | } |
| 766 | |
| 767 | /* These could be trashed each time round the loop, so restore |
| 768 | them each time. */ |
| 769 | if (rfds) rfds_copy = *rfds; |
| 770 | if (wfds) wfds_copy = *wfds; |
| 771 | if (xfds) xfds_copy = *xfds; |
| 772 | |
| 773 | zero_timeout.tv_sec = zero_timeout.tv_usec = 0; |
| 774 | |
| 775 | res = do_syscall_select( n, |
| 776 | rfds ? (vki_fd_set*)(&rfds_copy) : NULL, |
| 777 | wfds ? (vki_fd_set*)(&wfds_copy) : NULL, |
| 778 | xfds ? (vki_fd_set*)(&xfds_copy) : NULL, |
| 779 | & zero_timeout ); |
| 780 | if (res < 0) { |
| 781 | /* some kind of error (including EINTR); errno is set, so just |
| 782 | return. The sets are unspecified in this case. */ |
| 783 | return res; |
| 784 | } |
| 785 | if (res > 0) { |
| 786 | /* one or more fds is ready. Copy out resulting sets and |
| 787 | return. */ |
| 788 | if (rfds) *rfds = rfds_copy; |
| 789 | if (wfds) *wfds = wfds_copy; |
| 790 | if (xfds) *xfds = xfds_copy; |
| 791 | return res; |
| 792 | } |
| 793 | /* fprintf(stderr, "MY_SELECT: nanosleep\n"); */ |
| 794 | /* nanosleep and go round again */ |
| 795 | nanosleep_interval.tv_sec = 0; |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 796 | nanosleep_interval.tv_nsec = 20 * 1000 * 1000; /* 20 milliseconds */ |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 797 | /* It's critical here that valgrind's nanosleep implementation |
| 798 | is nonblocking. */ |
| 799 | (void)my_do_syscall2(__NR_nanosleep, |
| 800 | (int)(&nanosleep_interval), (int)NULL); |
| 801 | } |
| 802 | } |