sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1 | |
| 2 | /*--------------------------------------------------------------------*/ |
| 3 | /*--- Reimplementation of some C library stuff, to avoid depending ---*/ |
| 4 | /*--- on libc.so. ---*/ |
| 5 | /*--- vg_mylibc.c ---*/ |
| 6 | /*--------------------------------------------------------------------*/ |
| 7 | |
| 8 | /* |
| 9 | This file is part of Valgrind, an x86 protected-mode emulator |
| 10 | designed for debugging and profiling binaries on x86-Unixes. |
| 11 | |
| 12 | Copyright (C) 2000-2002 Julian Seward |
| 13 | jseward@acm.org |
| 14 | Julian_Seward@muraroa.demon.co.uk |
| 15 | |
| 16 | This program is free software; you can redistribute it and/or |
| 17 | modify it under the terms of the GNU General Public License as |
| 18 | published by the Free Software Foundation; either version 2 of the |
| 19 | License, or (at your option) any later version. |
| 20 | |
| 21 | This program is distributed in the hope that it will be useful, but |
| 22 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 24 | General Public License for more details. |
| 25 | |
| 26 | You should have received a copy of the GNU General Public License |
| 27 | along with this program; if not, write to the Free Software |
| 28 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 29 | 02111-1307, USA. |
| 30 | |
| 31 | The GNU General Public License is contained in the file LICENSE. |
| 32 | */ |
| 33 | |
| 34 | #include "vg_include.h" |
| 35 | |
| 36 | |
| 37 | |
| 38 | /* --------------------------------------------------------------------- |
| 39 | Really Actually DO system calls. |
| 40 | ------------------------------------------------------------------ */ |
| 41 | |
| 42 | /* Ripped off from /usr/include/asm/unistd.h. */ |
| 43 | |
| 44 | static |
| 45 | UInt vg_do_syscall0 ( UInt syscallno ) |
| 46 | { |
| 47 | UInt __res; |
| 48 | __asm__ volatile ("int $0x80" |
| 49 | : "=a" (__res) |
| 50 | : "0" (syscallno) ); |
| 51 | return __res; |
| 52 | } |
| 53 | |
| 54 | |
| 55 | static |
| 56 | UInt vg_do_syscall1 ( UInt syscallno, UInt arg1 ) |
| 57 | { |
| 58 | UInt __res; |
| 59 | __asm__ volatile ("int $0x80" |
| 60 | : "=a" (__res) |
| 61 | : "0" (syscallno), |
| 62 | "b" (arg1) ); |
| 63 | return __res; |
| 64 | } |
| 65 | |
| 66 | |
| 67 | static |
| 68 | UInt vg_do_syscall2 ( UInt syscallno, |
| 69 | UInt arg1, UInt arg2 ) |
| 70 | { |
| 71 | UInt __res; |
| 72 | __asm__ volatile ("int $0x80" |
| 73 | : "=a" (__res) |
| 74 | : "0" (syscallno), |
| 75 | "b" (arg1), |
| 76 | "c" (arg2) ); |
| 77 | return __res; |
| 78 | } |
| 79 | |
| 80 | |
| 81 | static |
| 82 | UInt vg_do_syscall3 ( UInt syscallno, |
| 83 | UInt arg1, UInt arg2, UInt arg3 ) |
| 84 | { |
| 85 | UInt __res; |
| 86 | __asm__ volatile ("int $0x80" |
| 87 | : "=a" (__res) |
| 88 | : "0" (syscallno), |
| 89 | "b" (arg1), |
| 90 | "c" (arg2), |
| 91 | "d" (arg3) ); |
| 92 | return __res; |
| 93 | } |
| 94 | |
| 95 | |
| 96 | static |
| 97 | UInt vg_do_syscall4 ( UInt syscallno, |
| 98 | UInt arg1, UInt arg2, UInt arg3, UInt arg4 ) |
| 99 | { |
| 100 | UInt __res; |
| 101 | __asm__ volatile ("int $0x80" |
| 102 | : "=a" (__res) |
| 103 | : "0" (syscallno), |
| 104 | "b" (arg1), |
| 105 | "c" (arg2), |
| 106 | "d" (arg3), |
| 107 | "S" (arg4) ); |
| 108 | return __res; |
| 109 | } |
| 110 | |
| 111 | |
| 112 | #if 0 |
| 113 | static |
| 114 | UInt vg_do_syscall5 ( UInt syscallno, |
| 115 | UInt arg1, UInt arg2, UInt arg3, UInt arg4, |
| 116 | UInt arg5 ) |
| 117 | { |
| 118 | UInt __res; |
| 119 | __asm__ volatile ("int $0x80" |
| 120 | : "=a" (__res) |
| 121 | : "0" (syscallno), |
| 122 | "b" (arg1), |
| 123 | "c" (arg2), |
| 124 | "d" (arg3), |
| 125 | "S" (arg4), |
| 126 | "D" (arg5) ); |
| 127 | return __res; |
| 128 | } |
| 129 | #endif |
| 130 | |
| 131 | /* --------------------------------------------------------------------- |
| 132 | Wrappers around system calls, and other stuff, to do with signals. |
| 133 | ------------------------------------------------------------------ */ |
| 134 | |
| 135 | /* sigemptyset, sigfullset, sigaddset and sigdelset return 0 on |
| 136 | success and -1 on error. |
| 137 | */ |
| 138 | Int VG_(ksigfillset)( vki_ksigset_t* set ) |
| 139 | { |
| 140 | Int i; |
| 141 | if (set == NULL) |
| 142 | return -1; |
| 143 | for (i = 0; i < VKI_KNSIG_WORDS; i++) |
| 144 | set->ws[i] = 0xFFFFFFFF; |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | Int VG_(ksigemptyset)( vki_ksigset_t* set ) |
| 149 | { |
| 150 | Int i; |
| 151 | if (set == NULL) |
| 152 | return -1; |
| 153 | for (i = 0; i < VKI_KNSIG_WORDS; i++) |
| 154 | set->ws[i] = 0x0; |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | Int VG_(ksigaddset)( vki_ksigset_t* set, Int signum ) |
| 159 | { |
| 160 | if (set == NULL) |
| 161 | return -1; |
| 162 | if (signum < 1 && signum > VKI_KNSIG) |
| 163 | return -1; |
| 164 | signum--; |
| 165 | set->ws[signum / VKI_KNSIG_BPW] |= (1 << (signum % VKI_KNSIG_BPW)); |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | Int VG_(ksigismember) ( vki_ksigset_t* set, Int signum ) |
| 170 | { |
| 171 | if (set == NULL) |
| 172 | return -1; |
| 173 | if (signum < 1 && signum > VKI_KNSIG) |
| 174 | return -1; |
| 175 | signum--; |
| 176 | if (1 & ((set->ws[signum / VKI_KNSIG_BPW]) >> (signum % VKI_KNSIG_BPW))) |
| 177 | return 1; |
| 178 | else |
| 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | |
| 183 | /* The functions sigaction, sigprocmask, sigpending and sigsuspend |
| 184 | return 0 on success and -1 on error. |
| 185 | */ |
| 186 | Int VG_(ksigprocmask)( Int how, |
| 187 | const vki_ksigset_t* set, |
| 188 | vki_ksigset_t* oldset) |
| 189 | { |
| 190 | Int res |
| 191 | = vg_do_syscall4(__NR_rt_sigprocmask, |
| 192 | how, (UInt)set, (UInt)oldset, |
| 193 | VKI_KNSIG_WORDS * VKI_BYTES_PER_WORD); |
| 194 | return VG_(is_kerror)(res) ? -1 : 0; |
| 195 | } |
| 196 | |
| 197 | |
| 198 | Int VG_(ksigaction) ( Int signum, |
| 199 | const vki_ksigaction* act, |
| 200 | vki_ksigaction* oldact) |
| 201 | { |
| 202 | Int res |
| 203 | = vg_do_syscall4(__NR_rt_sigaction, |
| 204 | signum, (UInt)act, (UInt)oldact, |
| 205 | VKI_KNSIG_WORDS * VKI_BYTES_PER_WORD); |
| 206 | return VG_(is_kerror)(res) ? -1 : 0; |
| 207 | } |
| 208 | |
| 209 | |
| 210 | Int VG_(ksigaltstack)( const vki_kstack_t* ss, vki_kstack_t* oss ) |
| 211 | { |
| 212 | Int res |
| 213 | = vg_do_syscall2(__NR_sigaltstack, (UInt)ss, (UInt)oss); |
| 214 | return VG_(is_kerror)(res) ? -1 : 0; |
| 215 | } |
| 216 | |
| 217 | |
| 218 | Int VG_(ksignal)(Int signum, void (*sighandler)(Int)) |
| 219 | { |
| 220 | Int res; |
| 221 | vki_ksigaction sa; |
| 222 | sa.ksa_handler = sighandler; |
| 223 | sa.ksa_flags = VKI_SA_ONSTACK | VKI_SA_RESTART; |
| 224 | sa.ksa_restorer = NULL; |
| 225 | res = VG_(ksigemptyset)( &sa.ksa_mask ); |
| 226 | vg_assert(res == 0); |
| 227 | res = vg_do_syscall4(__NR_rt_sigaction, |
| 228 | signum, (UInt)(&sa), (UInt)NULL, |
| 229 | VKI_KNSIG_WORDS * VKI_BYTES_PER_WORD); |
| 230 | return VG_(is_kerror)(res) ? -1 : 0; |
| 231 | } |
| 232 | |
| 233 | |
| 234 | /* --------------------------------------------------------------------- |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 235 | mmap/munmap, exit, fcntl |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 236 | ------------------------------------------------------------------ */ |
| 237 | |
| 238 | /* Returns -1 on failure. */ |
| 239 | void* VG_(mmap)( void* start, UInt length, |
| 240 | UInt prot, UInt flags, UInt fd, UInt offset) |
| 241 | { |
| 242 | Int res; |
| 243 | UInt args[6]; |
| 244 | args[0] = (UInt)start; |
| 245 | args[1] = length; |
| 246 | args[2] = prot; |
| 247 | args[3] = flags; |
| 248 | args[4] = fd; |
| 249 | args[5] = offset; |
| 250 | res = vg_do_syscall1(__NR_mmap, (UInt)(&(args[0])) ); |
| 251 | return VG_(is_kerror)(res) ? ((void*)(-1)) : (void*)res; |
| 252 | } |
| 253 | |
| 254 | /* Returns -1 on failure. */ |
| 255 | Int VG_(munmap)( void* start, Int length ) |
| 256 | { |
| 257 | Int res = vg_do_syscall2(__NR_munmap, (UInt)start, (UInt)length ); |
| 258 | return VG_(is_kerror)(res) ? -1 : 0; |
| 259 | } |
| 260 | |
| 261 | void VG_(exit)( Int status ) |
| 262 | { |
| 263 | (void)vg_do_syscall1(__NR_exit, (UInt)status ); |
| 264 | /* Why are we still alive here? */ |
| 265 | /*NOTREACHED*/ |
| 266 | vg_assert(2+2 == 5); |
| 267 | } |
| 268 | |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 269 | /* Returns -1 on error. */ |
| 270 | Int VG_(fcntl) ( Int fd, Int cmd, Int arg ) |
| 271 | { |
| 272 | Int res = vg_do_syscall3(__NR_fcntl, fd, cmd, arg); |
| 273 | return VG_(is_kerror)(res) ? -1 : res; |
| 274 | } |
| 275 | |
| 276 | /* Returns -1 on error. */ |
| 277 | Int VG_(select)( Int n, |
| 278 | vki_fd_set* readfds, |
| 279 | vki_fd_set* writefds, |
| 280 | vki_fd_set* exceptfds, |
| 281 | struct vki_timeval * timeout ) |
| 282 | { |
| 283 | Int res; |
| 284 | UInt args[5]; |
| 285 | args[0] = n; |
| 286 | args[1] = (UInt)readfds; |
| 287 | args[2] = (UInt)writefds; |
| 288 | args[3] = (UInt)exceptfds; |
| 289 | args[4] = (UInt)timeout; |
| 290 | res = vg_do_syscall1(__NR_select, (UInt)(&(args[0])) ); |
| 291 | return VG_(is_kerror)(res) ? -1 : res; |
| 292 | return res; |
| 293 | } |
| 294 | |
| 295 | /* Returns -1 on error, but 0 if ok or interrupted. */ |
| 296 | Int VG_(nanosleep)( const struct vki_timespec *req, |
| 297 | struct vki_timespec *rem ) |
| 298 | { |
| 299 | Int res; |
| 300 | res = vg_do_syscall2(__NR_nanosleep, (UInt)req, (UInt)rem); |
| 301 | if (res == -VKI_EINVAL) return -1; |
| 302 | return 0; |
| 303 | } |
| 304 | |
| 305 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 306 | /* --------------------------------------------------------------------- |
| 307 | printf implementation. The key function, vg_vprintf(), emits chars |
| 308 | into a caller-supplied function. Distantly derived from: |
| 309 | |
| 310 | vprintf replacement for Checker. |
| 311 | Copyright 1993, 1994, 1995 Tristan Gingold |
| 312 | Written September 1993 Tristan Gingold |
| 313 | Tristan Gingold, 8 rue Parmentier, F-91120 PALAISEAU, FRANCE |
| 314 | |
| 315 | (Checker itself was GPL'd.) |
| 316 | ------------------------------------------------------------------ */ |
| 317 | |
| 318 | |
| 319 | /* Some flags. */ |
| 320 | #define VG_MSG_SIGNED 1 /* The value is signed. */ |
| 321 | #define VG_MSG_ZJUSTIFY 2 /* Must justify with '0'. */ |
| 322 | #define VG_MSG_LJUSTIFY 4 /* Must justify on the left. */ |
| 323 | |
| 324 | |
| 325 | /* Copy a string into the buffer. */ |
| 326 | static void |
| 327 | myvprintf_str ( void(*send)(Char), Int flags, Int width, Char* str, |
| 328 | Bool capitalise ) |
| 329 | { |
| 330 | # define MAYBE_TOUPPER(ch) (capitalise ? VG_(toupper)(ch) : (ch)) |
| 331 | |
| 332 | Int i, extra; |
| 333 | Int len = VG_(strlen)(str); |
| 334 | |
| 335 | if (width == 0) { |
| 336 | for (i = 0; i < len; i++) |
| 337 | send(MAYBE_TOUPPER(str[i])); |
| 338 | return; |
| 339 | } |
| 340 | |
| 341 | if (len > width) { |
| 342 | for (i = 0; i < width; i++) |
| 343 | send(MAYBE_TOUPPER(str[i])); |
| 344 | return; |
| 345 | } |
| 346 | |
| 347 | extra = width - len; |
| 348 | if (flags & VG_MSG_LJUSTIFY) { |
| 349 | for (i = 0; i < extra; i++) |
| 350 | send(' '); |
| 351 | } |
| 352 | for (i = 0; i < len; i++) |
| 353 | send(MAYBE_TOUPPER(str[i])); |
| 354 | if (!(flags & VG_MSG_LJUSTIFY)) { |
| 355 | for (i = 0; i < extra; i++) |
| 356 | send(' '); |
| 357 | } |
| 358 | |
| 359 | # undef MAYBE_TOUPPER |
| 360 | } |
| 361 | |
| 362 | /* Write P into the buffer according to these args: |
| 363 | * If SIGN is true, p is a signed. |
| 364 | * BASE is the base. |
| 365 | * If WITH_ZERO is true, '0' must be added. |
| 366 | * WIDTH is the width of the field. |
| 367 | */ |
| 368 | static void |
| 369 | myvprintf_int64 ( void(*send)(Char), Int flags, Int base, Int width, ULong p) |
| 370 | { |
| 371 | Char buf[40]; |
| 372 | Int ind = 0; |
| 373 | Int i; |
| 374 | Bool neg = False; |
| 375 | Char *digits = "0123456789ABCDEF"; |
| 376 | |
| 377 | if (base < 2 || base > 16) |
| 378 | return; |
| 379 | |
| 380 | if ((flags & VG_MSG_SIGNED) && (Long)p < 0) { |
| 381 | p = - (Long)p; |
| 382 | neg = True; |
| 383 | } |
| 384 | |
| 385 | if (p == 0) |
| 386 | buf[ind++] = '0'; |
| 387 | else { |
| 388 | while (p > 0) { |
| 389 | buf[ind++] = digits[p % base]; |
| 390 | p /= base; |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | if (neg) |
| 395 | buf[ind++] = '-'; |
| 396 | |
| 397 | if (width > 0 && !(flags & VG_MSG_LJUSTIFY)) { |
| 398 | for(; ind < width; ind++) { |
| 399 | vg_assert(ind < 39); |
| 400 | buf[ind] = (flags & VG_MSG_ZJUSTIFY) ? '0': ' '; |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | /* Reverse copy to buffer. */ |
| 405 | for (i = ind -1; i >= 0; i--) |
| 406 | send(buf[i]); |
| 407 | |
| 408 | if (width > 0 && (flags & VG_MSG_LJUSTIFY)) { |
| 409 | for(; ind < width; ind++) |
| 410 | send((flags & VG_MSG_ZJUSTIFY) ? '0': ' '); |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | |
| 415 | /* A simple vprintf(). */ |
| 416 | void |
| 417 | VG_(vprintf) ( void(*send)(Char), const Char *format, va_list vargs ) |
| 418 | { |
| 419 | int i; |
| 420 | int flags; |
| 421 | int width; |
| 422 | Bool is_long; |
| 423 | |
| 424 | /* We assume that vargs has already been initialised by the |
| 425 | caller, using va_start, and that the caller will similarly |
| 426 | clean up with va_end. |
| 427 | */ |
| 428 | |
| 429 | for (i = 0; format[i] != 0; i++) { |
| 430 | if (format[i] != '%') { |
| 431 | send(format[i]); |
| 432 | continue; |
| 433 | } |
| 434 | i++; |
| 435 | /* A '%' has been found. Ignore a trailing %. */ |
| 436 | if (format[i] == 0) |
| 437 | break; |
| 438 | if (format[i] == '%') { |
| 439 | /* `%%' is replaced by `%'. */ |
| 440 | send('%'); |
| 441 | continue; |
| 442 | } |
| 443 | flags = 0; |
| 444 | is_long = False; |
| 445 | width = 0; /* length of the field. */ |
| 446 | /* If '-' follows '%', justify on the left. */ |
| 447 | if (format[i] == '-') { |
| 448 | flags |= VG_MSG_LJUSTIFY; |
| 449 | i++; |
| 450 | } |
| 451 | /* If '0' follows '%', pads will be inserted. */ |
| 452 | if (format[i] == '0') { |
| 453 | flags |= VG_MSG_ZJUSTIFY; |
| 454 | i++; |
| 455 | } |
| 456 | /* Compute the field length. */ |
| 457 | while (format[i] >= '0' && format[i] <= '9') { |
| 458 | width *= 10; |
| 459 | width += format[i++] - '0'; |
| 460 | } |
| 461 | while (format[i] == 'l') { |
| 462 | i++; |
| 463 | is_long = True; |
| 464 | } |
| 465 | |
| 466 | switch (format[i]) { |
| 467 | case 'd': /* %d */ |
| 468 | flags |= VG_MSG_SIGNED; |
| 469 | if (is_long) |
| 470 | myvprintf_int64(send, flags, 10, width, |
| 471 | (ULong)(va_arg (vargs, Long))); |
| 472 | else |
| 473 | myvprintf_int64(send, flags, 10, width, |
| 474 | (ULong)(va_arg (vargs, Int))); |
| 475 | break; |
| 476 | case 'u': /* %u */ |
| 477 | if (is_long) |
| 478 | myvprintf_int64(send, flags, 10, width, |
| 479 | (ULong)(va_arg (vargs, ULong))); |
| 480 | else |
| 481 | myvprintf_int64(send, flags, 10, width, |
| 482 | (ULong)(va_arg (vargs, UInt))); |
| 483 | break; |
| 484 | case 'p': /* %p */ |
| 485 | send('0'); |
| 486 | send('x'); |
| 487 | myvprintf_int64(send, flags, 16, width, |
| 488 | (ULong)((UInt)va_arg (vargs, void *))); |
| 489 | break; |
| 490 | case 'x': /* %x */ |
| 491 | if (is_long) |
| 492 | myvprintf_int64(send, flags, 16, width, |
| 493 | (ULong)(va_arg (vargs, ULong))); |
| 494 | else |
| 495 | myvprintf_int64(send, flags, 16, width, |
| 496 | (ULong)(va_arg (vargs, UInt))); |
| 497 | break; |
| 498 | case 'c': /* %c */ |
| 499 | send(va_arg (vargs, int)); |
| 500 | break; |
| 501 | case 's': case 'S': { /* %s */ |
| 502 | char *str = va_arg (vargs, char *); |
| 503 | if (str == (char*) 0) str = "(null)"; |
| 504 | myvprintf_str(send, flags, width, str, format[i]=='S'); |
| 505 | break; |
| 506 | } |
| 507 | default: |
| 508 | break; |
| 509 | } |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | |
| 514 | /* A general replacement for printf(). Note that only low-level |
| 515 | debugging info should be sent via here. The official route is to |
| 516 | to use vg_message(). This interface is deprecated. |
| 517 | */ |
| 518 | static char myprintf_buf[100]; |
| 519 | static int n_myprintf_buf; |
| 520 | |
| 521 | static void add_to_myprintf_buf ( Char c ) |
| 522 | { |
| 523 | if (n_myprintf_buf >= 100-10 /*paranoia*/ ) { |
| 524 | if (VG_(clo_logfile_fd) >= 0) |
| 525 | VG_(write) |
| 526 | (VG_(clo_logfile_fd), myprintf_buf, VG_(strlen)(myprintf_buf)); |
| 527 | n_myprintf_buf = 0; |
| 528 | myprintf_buf[n_myprintf_buf] = 0; |
| 529 | } |
| 530 | myprintf_buf[n_myprintf_buf++] = c; |
| 531 | myprintf_buf[n_myprintf_buf] = 0; |
| 532 | } |
| 533 | |
| 534 | void VG_(printf) ( const char *format, ... ) |
| 535 | { |
| 536 | va_list vargs; |
| 537 | va_start(vargs,format); |
| 538 | |
| 539 | n_myprintf_buf = 0; |
| 540 | myprintf_buf[n_myprintf_buf] = 0; |
| 541 | VG_(vprintf) ( add_to_myprintf_buf, format, vargs ); |
| 542 | |
| 543 | if (n_myprintf_buf > 0 && VG_(clo_logfile_fd) >= 0) |
| 544 | VG_(write) |
| 545 | ( VG_(clo_logfile_fd), myprintf_buf, VG_(strlen)(myprintf_buf)); |
| 546 | |
| 547 | va_end(vargs); |
| 548 | } |
| 549 | |
| 550 | |
| 551 | /* A general replacement for sprintf(). */ |
| 552 | static Char* vg_sprintf_ptr; |
| 553 | |
| 554 | static void add_to_vg_sprintf_buf ( Char c ) |
| 555 | { |
| 556 | *vg_sprintf_ptr++ = c; |
| 557 | } |
| 558 | |
| 559 | void VG_(sprintf) ( Char* buf, Char *format, ... ) |
| 560 | { |
| 561 | va_list vargs; |
| 562 | va_start(vargs,format); |
| 563 | |
| 564 | vg_sprintf_ptr = buf; |
| 565 | VG_(vprintf) ( add_to_vg_sprintf_buf, format, vargs ); |
| 566 | add_to_vg_sprintf_buf(0); |
| 567 | |
| 568 | va_end(vargs); |
| 569 | } |
| 570 | |
| 571 | |
| 572 | /* --------------------------------------------------------------------- |
| 573 | Misc str* functions. |
| 574 | ------------------------------------------------------------------ */ |
| 575 | |
| 576 | Bool VG_(isspace) ( Char c ) |
| 577 | { |
| 578 | return (c == ' ' || c == '\n' || c == '\t' || c == 0); |
| 579 | } |
| 580 | |
| 581 | |
| 582 | Int VG_(strlen) ( const Char* str ) |
| 583 | { |
| 584 | Int i = 0; |
| 585 | while (str[i] != 0) i++; |
| 586 | return i; |
| 587 | } |
| 588 | |
| 589 | |
| 590 | Long VG_(atoll) ( Char* str ) |
| 591 | { |
| 592 | Bool neg = False; |
| 593 | Long n = 0; |
| 594 | if (*str == '-') { str++; neg = True; }; |
| 595 | while (*str >= '0' && *str <= '9') { |
| 596 | n = 10*n + (Long)(*str - '0'); |
| 597 | str++; |
| 598 | } |
| 599 | if (neg) n = -n; |
| 600 | return n; |
| 601 | } |
| 602 | |
| 603 | |
| 604 | Char* VG_(strcat) ( Char* dest, const Char* src ) |
| 605 | { |
| 606 | Char* dest_orig = dest; |
| 607 | while (*dest) dest++; |
| 608 | while (*src) *dest++ = *src++; |
| 609 | *dest = 0; |
| 610 | return dest_orig; |
| 611 | } |
| 612 | |
| 613 | |
| 614 | Char* VG_(strncat) ( Char* dest, const Char* src, Int n ) |
| 615 | { |
| 616 | Char* dest_orig = dest; |
| 617 | while (*dest) dest++; |
| 618 | while (*src && n > 0) { *dest++ = *src++; n--; } |
| 619 | *dest = 0; |
| 620 | return dest_orig; |
| 621 | } |
| 622 | |
| 623 | |
| 624 | Char* VG_(strpbrk) ( const Char* s, const Char* accept ) |
| 625 | { |
| 626 | const Char* a; |
| 627 | while (*s) { |
| 628 | a = accept; |
| 629 | while (*a) |
| 630 | if (*a++ == *s) |
| 631 | return (Char *) s; |
| 632 | s++; |
| 633 | } |
| 634 | return NULL; |
| 635 | } |
| 636 | |
| 637 | |
| 638 | Char* VG_(strcpy) ( Char* dest, const Char* src ) |
| 639 | { |
| 640 | Char* dest_orig = dest; |
| 641 | while (*src) *dest++ = *src++; |
| 642 | *dest = 0; |
| 643 | return dest_orig; |
| 644 | } |
| 645 | |
| 646 | |
| 647 | /* Copy bytes, not overrunning the end of dest and always ensuring |
| 648 | zero termination. */ |
| 649 | void VG_(strncpy_safely) ( Char* dest, const Char* src, Int ndest ) |
| 650 | { |
| 651 | Int i; |
| 652 | vg_assert(ndest > 0); |
| 653 | i = 0; |
| 654 | dest[i] = 0; |
| 655 | while (True) { |
| 656 | if (src[i] == 0) return; |
| 657 | if (i >= ndest-1) return; |
| 658 | dest[i] = src[i]; |
| 659 | i++; |
| 660 | dest[i] = 0; |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | |
| 665 | void VG_(strncpy) ( Char* dest, const Char* src, Int ndest ) |
| 666 | { |
| 667 | VG_(strncpy_safely)( dest, src, ndest+1 ); |
| 668 | } |
| 669 | |
| 670 | |
| 671 | Int VG_(strcmp) ( const Char* s1, const Char* s2 ) |
| 672 | { |
| 673 | while (True) { |
| 674 | if (*s1 == 0 && *s2 == 0) return 0; |
| 675 | if (*s1 == 0) return -1; |
| 676 | if (*s2 == 0) return 1; |
| 677 | |
| 678 | if (*(UChar*)s1 < *(UChar*)s2) return -1; |
| 679 | if (*(UChar*)s1 > *(UChar*)s2) return 1; |
| 680 | |
| 681 | s1++; s2++; |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | |
| 686 | Int VG_(strcmp_ws) ( const Char* s1, const Char* s2 ) |
| 687 | { |
| 688 | while (True) { |
| 689 | if (VG_(isspace)(*s1) && VG_(isspace)(*s2)) return 0; |
| 690 | if (VG_(isspace)(*s1)) return -1; |
| 691 | if (VG_(isspace)(*s2)) return 1; |
| 692 | |
| 693 | if (*(UChar*)s1 < *(UChar*)s2) return -1; |
| 694 | if (*(UChar*)s1 > *(UChar*)s2) return 1; |
| 695 | |
| 696 | s1++; s2++; |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | |
| 701 | Int VG_(strncmp) ( const Char* s1, const Char* s2, Int nmax ) |
| 702 | { |
| 703 | Int n = 0; |
| 704 | while (True) { |
| 705 | if (n >= nmax) return 0; |
| 706 | if (*s1 == 0 && *s2 == 0) return 0; |
| 707 | if (*s1 == 0) return -1; |
| 708 | if (*s2 == 0) return 1; |
| 709 | |
| 710 | if (*(UChar*)s1 < *(UChar*)s2) return -1; |
| 711 | if (*(UChar*)s1 > *(UChar*)s2) return 1; |
| 712 | |
| 713 | s1++; s2++; n++; |
| 714 | } |
| 715 | } |
| 716 | |
| 717 | |
| 718 | Int VG_(strncmp_ws) ( const Char* s1, const Char* s2, Int nmax ) |
| 719 | { |
| 720 | Int n = 0; |
| 721 | while (True) { |
| 722 | if (n >= nmax) return 0; |
| 723 | if (VG_(isspace)(*s1) && VG_(isspace)(*s2)) return 0; |
| 724 | if (VG_(isspace)(*s1)) return -1; |
| 725 | if (VG_(isspace)(*s2)) return 1; |
| 726 | |
| 727 | if (*(UChar*)s1 < *(UChar*)s2) return -1; |
| 728 | if (*(UChar*)s1 > *(UChar*)s2) return 1; |
| 729 | |
| 730 | s1++; s2++; n++; |
| 731 | } |
| 732 | } |
| 733 | |
| 734 | |
| 735 | Char* VG_(strstr) ( const Char* haystack, Char* needle ) |
| 736 | { |
| 737 | Int n = VG_(strlen)(needle); |
| 738 | while (True) { |
| 739 | if (haystack[0] == 0) |
| 740 | return NULL; |
| 741 | if (VG_(strncmp)(haystack, needle, n) == 0) |
| 742 | return (Char*)haystack; |
| 743 | haystack++; |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | |
| 748 | Char* VG_(strchr) ( const Char* s, Char c ) |
| 749 | { |
| 750 | while (True) { |
| 751 | if (*s == c) return (Char*)s; |
| 752 | if (*s == 0) return NULL; |
| 753 | s++; |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | |
| 758 | Char VG_(toupper) ( Char c ) |
| 759 | { |
| 760 | if (c >= 'a' && c <= 'z') |
| 761 | return c + ('A' - 'a'); |
| 762 | else |
| 763 | return c; |
| 764 | } |
| 765 | |
| 766 | |
| 767 | Char* VG_(strdup) ( ArenaId aid, const Char* s ) |
| 768 | { |
| 769 | Int i; |
| 770 | Int len = VG_(strlen)(s) + 1; |
| 771 | Char* res = VG_(malloc) (aid, len); |
| 772 | for (i = 0; i < len; i++) |
| 773 | res[i] = s[i]; |
| 774 | return res; |
| 775 | } |
| 776 | |
| 777 | |
| 778 | /* --------------------------------------------------------------------- |
| 779 | A simple string matching routine, purloined from Hugs98. |
| 780 | `*' matches any sequence of zero or more characters |
| 781 | `?' matches any single character exactly |
| 782 | `\c' matches the character c only (ignoring special chars) |
| 783 | c matches the character c only |
| 784 | ------------------------------------------------------------------ */ |
| 785 | |
| 786 | /* Keep track of recursion depth. */ |
| 787 | static Int recDepth; |
| 788 | |
| 789 | static Bool stringMatch_wrk ( Char* pat, Char* str ) |
| 790 | { |
| 791 | vg_assert(recDepth >= 0 && recDepth < 500); |
| 792 | recDepth++; |
| 793 | for (;;) { |
| 794 | switch (*pat) { |
| 795 | case '\0' : return (*str=='\0'); |
| 796 | case '*' : do { |
| 797 | if (stringMatch_wrk(pat+1,str)) { |
| 798 | recDepth--; |
| 799 | return True; |
| 800 | } |
| 801 | } while (*str++); |
| 802 | recDepth--; |
| 803 | return False; |
| 804 | case '?' : if (*str++=='\0') { |
| 805 | recDepth--; |
| 806 | return False; |
| 807 | } |
| 808 | pat++; |
| 809 | break; |
| 810 | case '\\' : if (*++pat == '\0') { |
| 811 | recDepth--; |
| 812 | return False; /* spurious trailing \ in pattern */ |
| 813 | } |
| 814 | /* falls through to ... */ |
| 815 | default : if (*pat++ != *str++) { |
| 816 | recDepth--; |
| 817 | return False; |
| 818 | } |
| 819 | break; |
| 820 | } |
| 821 | } |
| 822 | } |
| 823 | |
| 824 | Bool VG_(stringMatch) ( Char* pat, Char* str ) |
| 825 | { |
| 826 | Bool b; |
| 827 | recDepth = 0; |
| 828 | b = stringMatch_wrk ( pat, str ); |
| 829 | /* |
| 830 | VG_(printf)("%s %s %s\n", |
| 831 | b?"TRUE ":"FALSE", pat, str); |
| 832 | */ |
| 833 | return b; |
| 834 | } |
| 835 | |
| 836 | |
| 837 | /* --------------------------------------------------------------------- |
| 838 | Assertery. |
| 839 | ------------------------------------------------------------------ */ |
| 840 | |
| 841 | #define EMAIL_ADDR "jseward@acm.org" |
| 842 | |
| 843 | void VG_(assert_fail) ( Char* expr, Char* file, Int line, Char* fn ) |
| 844 | { |
| 845 | VG_(printf)("\n%s: %s:%d (%s): Assertion `%s' failed.\n", |
| 846 | "valgrind", file, line, fn, expr ); |
| 847 | VG_(printf)("Please report this bug to me at: %s\n\n", EMAIL_ADDR); |
| 848 | VG_(shutdown_logging)(); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 849 | VG_(exit)(1); |
| 850 | } |
| 851 | |
| 852 | void VG_(panic) ( Char* str ) |
| 853 | { |
| 854 | VG_(printf)("\nvalgrind: the `impossible' happened:\n %s\n", str); |
| 855 | VG_(printf)("Basic block ctr is approximately %llu\n", VG_(bbs_done) ); |
| 856 | VG_(printf)("Please report this bug to me at: %s\n\n", EMAIL_ADDR); |
| 857 | VG_(shutdown_logging)(); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 858 | VG_(exit)(1); |
| 859 | } |
| 860 | |
| 861 | #undef EMAIL_ADDR |
| 862 | |
| 863 | |
| 864 | /* --------------------------------------------------------------------- |
| 865 | Primitive support for reading files. |
| 866 | ------------------------------------------------------------------ */ |
| 867 | |
| 868 | /* Returns -1 on failure. */ |
| 869 | Int VG_(open_read) ( Char* pathname ) |
| 870 | { |
| 871 | Int fd; |
| 872 | /* VG_(printf)("vg_open_read %s\n", pathname ); */ |
| 873 | |
| 874 | /* This gets a segmentation fault if pathname isn't a valid file. |
| 875 | I don't know why. It seems like the call to open is getting |
| 876 | intercepted and messed with by glibc ... */ |
| 877 | /* fd = open( pathname, O_RDONLY ); */ |
| 878 | /* ... so we go direct to the horse's mouth, which seems to work |
| 879 | ok: */ |
| 880 | const int O_RDONLY = 0; /* See /usr/include/bits/fcntl.h */ |
| 881 | fd = vg_do_syscall3(__NR_open, (UInt)pathname, O_RDONLY, 0); |
| 882 | /* VG_(printf)("result = %d\n", fd); */ |
| 883 | if (VG_(is_kerror)(fd)) fd = -1; |
| 884 | return fd; |
| 885 | } |
| 886 | |
| 887 | |
| 888 | void VG_(close) ( Int fd ) |
| 889 | { |
| 890 | vg_do_syscall1(__NR_close, fd); |
| 891 | } |
| 892 | |
| 893 | |
| 894 | Int VG_(read) ( Int fd, void* buf, Int count) |
| 895 | { |
| 896 | Int res; |
| 897 | /* res = read( fd, buf, count ); */ |
| 898 | res = vg_do_syscall3(__NR_read, fd, (UInt)buf, count); |
| 899 | if (VG_(is_kerror)(res)) res = -1; |
| 900 | return res; |
| 901 | } |
| 902 | |
| 903 | Int VG_(write) ( Int fd, void* buf, Int count) |
| 904 | { |
| 905 | Int res; |
| 906 | /* res = write( fd, buf, count ); */ |
| 907 | res = vg_do_syscall3(__NR_write, fd, (UInt)buf, count); |
| 908 | if (VG_(is_kerror)(res)) res = -1; |
| 909 | return res; |
| 910 | } |
| 911 | |
| 912 | /* Misc functions looking for a proper home. */ |
| 913 | |
| 914 | /* We do getenv without libc's help by snooping around in |
| 915 | VG_(client_env) as determined at startup time. */ |
| 916 | Char* VG_(getenv) ( Char* varname ) |
| 917 | { |
| 918 | Int i, n; |
| 919 | n = VG_(strlen)(varname); |
| 920 | for (i = 0; VG_(client_envp)[i] != NULL; i++) { |
| 921 | Char* s = VG_(client_envp)[i]; |
| 922 | if (VG_(strncmp)(varname, s, n) == 0 && s[n] == '=') { |
| 923 | return & s[n+1]; |
| 924 | } |
| 925 | } |
| 926 | return NULL; |
| 927 | } |
| 928 | |
| 929 | /* You'd be amazed how many places need to know the current pid. */ |
| 930 | Int VG_(getpid) ( void ) |
| 931 | { |
| 932 | Int res; |
| 933 | /* res = getpid(); */ |
| 934 | res = vg_do_syscall0(__NR_getpid); |
| 935 | return res; |
| 936 | } |
| 937 | |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 938 | /* Read a notional elapsed (wallclock-time) timer, giving a 64-bit |
| 939 | microseconds count. */ |
| 940 | ULong VG_(read_microsecond_timer)( void ) |
| 941 | { |
| 942 | Int res; |
| 943 | struct vki_timeval tv; |
| 944 | res = vg_do_syscall2(__NR_gettimeofday, (UInt)&tv, (UInt)NULL); |
| 945 | vg_assert(!VG_(is_kerror)(res)); |
| 946 | return (1000000ULL * (ULong)(tv.tv_sec)) + (ULong)(tv.tv_usec); |
| 947 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 948 | |
| 949 | /* --------------------------------------------------------------------- |
| 950 | Primitive support for bagging memory via mmap. |
| 951 | ------------------------------------------------------------------ */ |
| 952 | |
| 953 | void* VG_(get_memory_from_mmap) ( Int nBytes ) |
| 954 | { |
| 955 | static UInt tot_alloc = 0; |
| 956 | void* p = VG_(mmap)( 0, nBytes, |
| 957 | VKI_PROT_READ|VKI_PROT_WRITE|VKI_PROT_EXEC, |
| 958 | VKI_MAP_PRIVATE|VKI_MAP_ANONYMOUS, -1, 0 ); |
| 959 | if (p != ((void*)(-1))) { |
| 960 | tot_alloc += (UInt)nBytes; |
| 961 | if (0) |
| 962 | VG_(printf)("get_memory_from_mmap: %d tot, %d req\n", |
| 963 | tot_alloc, nBytes); |
| 964 | return p; |
| 965 | } |
| 966 | VG_(printf)("vg_get_memory_from_mmap failed on request of %d\n", |
| 967 | nBytes); |
| 968 | VG_(panic)("vg_get_memory_from_mmap: out of memory! Fatal! Bye!\n"); |
| 969 | } |
| 970 | |
| 971 | |
| 972 | /*--------------------------------------------------------------------*/ |
| 973 | /*--- end vg_mylibc.c ---*/ |
| 974 | /*--------------------------------------------------------------------*/ |