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