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