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