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