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 | /* |
njn | c953984 | 2002-10-02 13:26:35 +0000 | [diff] [blame] | 9 | This file is part of Valgrind, an extensible x86 protected-mode |
| 10 | emulator for monitoring program execution on x86-Unixes. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 11 | |
nethercote | bb1c991 | 2004-01-04 16:43:23 +0000 | [diff] [blame] | 12 | Copyright (C) 2000-2004 Julian Seward |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 13 | jseward@acm.org |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 14 | |
| 15 | This program is free software; you can redistribute it and/or |
| 16 | modify it under the terms of the GNU General Public License as |
| 17 | published by the Free Software Foundation; either version 2 of the |
| 18 | License, or (at your option) any later version. |
| 19 | |
| 20 | This program is distributed in the hope that it will be useful, but |
| 21 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 23 | General Public License for more details. |
| 24 | |
| 25 | You should have received a copy of the GNU General Public License |
| 26 | along with this program; if not, write to the Free Software |
| 27 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 28 | 02111-1307, USA. |
| 29 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 30 | The GNU General Public License is contained in the file COPYING. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 31 | */ |
| 32 | |
nethercote | f1e5e15 | 2004-09-01 23:58:16 +0000 | [diff] [blame] | 33 | #include "core.h" |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 34 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 35 | /* --------------------------------------------------------------------- |
| 36 | Wrappers around system calls, and other stuff, to do with signals. |
| 37 | ------------------------------------------------------------------ */ |
| 38 | |
| 39 | /* sigemptyset, sigfullset, sigaddset and sigdelset return 0 on |
| 40 | success and -1 on error. |
| 41 | */ |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 42 | Int VG_(sigfillset)( vki_sigset_t* set ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 43 | { |
| 44 | Int i; |
| 45 | if (set == NULL) |
| 46 | return -1; |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 47 | for (i = 0; i < _VKI_NSIG_WORDS; i++) |
nethercote | acac2fd | 2004-11-04 13:49:28 +0000 | [diff] [blame] | 48 | set->sig[i] = ~(UWord)0x0; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 49 | return 0; |
| 50 | } |
| 51 | |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 52 | Int VG_(sigemptyset)( vki_sigset_t* set ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 53 | { |
| 54 | Int i; |
| 55 | if (set == NULL) |
| 56 | return -1; |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 57 | for (i = 0; i < _VKI_NSIG_WORDS; i++) |
| 58 | set->sig[i] = 0x0; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 59 | return 0; |
| 60 | } |
| 61 | |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 62 | Bool VG_(isemptysigset)( vki_sigset_t* set ) |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 63 | { |
| 64 | Int i; |
| 65 | vg_assert(set != NULL); |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 66 | for (i = 0; i < _VKI_NSIG_WORDS; i++) |
| 67 | if (set->sig[i] != 0x0) return False; |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 68 | return True; |
| 69 | } |
| 70 | |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 71 | Bool VG_(isfullsigset)( vki_sigset_t* set ) |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 72 | { |
| 73 | Int i; |
| 74 | vg_assert(set != NULL); |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 75 | for (i = 0; i < _VKI_NSIG_WORDS; i++) |
nethercote | 93246cf | 2004-11-04 18:56:47 +0000 | [diff] [blame] | 76 | if (set->sig[i] != ~(UWord)0x0) return False; |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 77 | return True; |
| 78 | } |
| 79 | |
| 80 | |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 81 | Int VG_(sigaddset)( vki_sigset_t* set, Int signum ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 82 | { |
| 83 | if (set == NULL) |
| 84 | return -1; |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 85 | if (signum < 1 || signum > _VKI_NSIG) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 86 | return -1; |
| 87 | signum--; |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 88 | set->sig[signum / _VKI_NSIG_BPW] |= (1 << (signum % _VKI_NSIG_BPW)); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 89 | return 0; |
| 90 | } |
| 91 | |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 92 | Int VG_(sigdelset)( vki_sigset_t* set, Int signum ) |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 93 | { |
| 94 | if (set == NULL) |
| 95 | return -1; |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 96 | if (signum < 1 || signum > _VKI_NSIG) |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 97 | return -1; |
| 98 | signum--; |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 99 | set->sig[signum / _VKI_NSIG_BPW] &= ~(1 << (signum % _VKI_NSIG_BPW)); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 100 | return 0; |
| 101 | } |
| 102 | |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 103 | Int VG_(sigismember) ( vki_sigset_t* set, Int signum ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 104 | { |
| 105 | if (set == NULL) |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 106 | return 0; |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 107 | if (signum < 1 || signum > _VKI_NSIG) |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 108 | return 0; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 109 | signum--; |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 110 | if (1 & ((set->sig[signum / _VKI_NSIG_BPW]) >> (signum % _VKI_NSIG_BPW))) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 111 | return 1; |
| 112 | else |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 117 | /* Add all signals in src to dst. */ |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 118 | void VG_(sigaddset_from_set)( vki_sigset_t* dst, vki_sigset_t* src ) |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 119 | { |
| 120 | Int i; |
| 121 | vg_assert(dst != NULL && src != NULL); |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 122 | for (i = 0; i < _VKI_NSIG_WORDS; i++) |
| 123 | dst->sig[i] |= src->sig[i]; |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | /* Remove all signals in src from dst. */ |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 127 | void VG_(sigdelset_from_set)( vki_sigset_t* dst, vki_sigset_t* src ) |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 128 | { |
| 129 | Int i; |
| 130 | vg_assert(dst != NULL && src != NULL); |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 131 | for (i = 0; i < _VKI_NSIG_WORDS; i++) |
| 132 | dst->sig[i] &= ~(src->sig[i]); |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 136 | /* The functions sigaction, sigprocmask, sigpending and sigsuspend |
| 137 | return 0 on success and -1 on error. |
| 138 | */ |
nethercote | 50397c2 | 2004-11-04 18:03:06 +0000 | [diff] [blame] | 139 | Int VG_(sigprocmask)( Int how, const vki_sigset_t* set, vki_sigset_t* oldset) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 140 | { |
| 141 | Int res |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 142 | = VG_(do_syscall)(__NR_rt_sigprocmask, |
nethercote | 50397c2 | 2004-11-04 18:03:06 +0000 | [diff] [blame] | 143 | how, (UWord)set, (UWord)oldset, |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 144 | _VKI_NSIG_WORDS * sizeof(UWord)); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 145 | return VG_(is_kerror)(res) ? -1 : 0; |
| 146 | } |
| 147 | |
| 148 | |
nethercote | 50397c2 | 2004-11-04 18:03:06 +0000 | [diff] [blame] | 149 | Int VG_(sigaction) ( Int signum, const struct vki_sigaction* act, |
| 150 | struct vki_sigaction* oldact) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 151 | { |
| 152 | Int res |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 153 | = VG_(do_syscall)(__NR_rt_sigaction, |
nethercote | 50397c2 | 2004-11-04 18:03:06 +0000 | [diff] [blame] | 154 | signum, (UWord)act, (UWord)oldact, |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 155 | _VKI_NSIG_WORDS * sizeof(UWord)); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 156 | /* VG_(printf)("res = %d\n",res); */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 157 | return VG_(is_kerror)(res) ? -1 : 0; |
| 158 | } |
| 159 | |
| 160 | |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 161 | Int VG_(sigaltstack)( const vki_stack_t* ss, vki_stack_t* oss ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 162 | { |
nethercote | fbd6ffd | 2004-11-04 17:10:24 +0000 | [diff] [blame] | 163 | Int res = VG_(do_syscall)(__NR_sigaltstack, (UWord)ss, (UWord)oss); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 164 | return VG_(is_kerror)(res) ? -1 : 0; |
| 165 | } |
| 166 | |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 167 | Int VG_(sigtimedwait)( const vki_sigset_t *set, vki_siginfo_t *info, |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 168 | const struct vki_timespec *timeout ) |
| 169 | { |
| 170 | Int res = VG_(do_syscall)(__NR_rt_sigtimedwait, set, info, timeout, sizeof(*set)); |
| 171 | |
| 172 | return VG_(is_kerror)(res) ? -1 : res; |
| 173 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 174 | |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 175 | Int VG_(signal)(Int signum, void (*sighandler)(Int)) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 176 | { |
| 177 | Int res; |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 178 | struct vki_sigaction sa; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 179 | sa.ksa_handler = sighandler; |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 180 | sa.sa_flags = VKI_SA_ONSTACK | VKI_SA_RESTART; |
| 181 | sa.sa_restorer = NULL; |
| 182 | res = VG_(sigemptyset)( &sa.sa_mask ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 183 | vg_assert(res == 0); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 184 | res = VG_(do_syscall)(__NR_rt_sigaction, |
nethercote | 50397c2 | 2004-11-04 18:03:06 +0000 | [diff] [blame] | 185 | signum, (UWord)&sa, (UWord)NULL, |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 186 | _VKI_NSIG_WORDS * sizeof(UWord)); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 187 | return VG_(is_kerror)(res) ? -1 : 0; |
| 188 | } |
| 189 | |
| 190 | |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 191 | Int VG_(kill)( Int pid, Int signo ) |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 192 | { |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 193 | Int res = VG_(do_syscall)(__NR_kill, pid, signo); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 194 | return VG_(is_kerror)(res) ? -1 : 0; |
| 195 | } |
| 196 | |
| 197 | |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame^] | 198 | Int VG_(tkill)( ThreadId tid, Int signo ) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 199 | { |
| 200 | Int ret = -VKI_ENOSYS; |
| 201 | |
thughes | 848907a | 2004-10-18 18:56:25 +0000 | [diff] [blame] | 202 | #ifdef __NR_tgkill |
| 203 | ret = VG_(do_syscall)(__NR_tgkill, VG_(main_pid), tid, signo); |
| 204 | #endif /* __NR_tgkill */ |
| 205 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 206 | #ifdef __NR_tkill |
thughes | 848907a | 2004-10-18 18:56:25 +0000 | [diff] [blame] | 207 | if (ret == -VKI_ENOSYS) |
| 208 | ret = VG_(do_syscall)(__NR_tkill, tid, signo); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 209 | #endif /* __NR_tkill */ |
| 210 | |
| 211 | if (ret == -VKI_ENOSYS) |
| 212 | ret = VG_(do_syscall)(__NR_kill, tid, signo); |
| 213 | |
| 214 | return VG_(is_kerror)(ret) ? -1 : 0; |
| 215 | } |
| 216 | |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 217 | Int VG_(sigpending) ( vki_sigset_t* set ) |
sewardj | efbfcdf | 2002-06-19 17:35:45 +0000 | [diff] [blame] | 218 | { |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame^] | 219 | // Nb: AMD64/Linux doesn't have __NR_sigpending; it only provides |
| 220 | // __NR_rt_sigpending. This function will have to be abstracted in some |
| 221 | // way to account for this. In the meantime, the easy option is to forget |
| 222 | // about it for AMD64 until it's needed. |
| 223 | #ifdef __amd64__ |
| 224 | I_die_here; |
| 225 | #else |
nethercote | cf51cc4 | 2004-11-04 17:02:42 +0000 | [diff] [blame] | 226 | Int res = VG_(do_syscall)(__NR_sigpending, (UWord)set); |
sewardj | efbfcdf | 2002-06-19 17:35:45 +0000 | [diff] [blame] | 227 | return VG_(is_kerror)(res) ? -1 : 0; |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame^] | 228 | #endif |
sewardj | efbfcdf | 2002-06-19 17:35:45 +0000 | [diff] [blame] | 229 | } |
| 230 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 231 | Int VG_(waitpid)(Int pid, Int *status, Int options) |
| 232 | { |
| 233 | Int ret = VG_(do_syscall)(__NR_wait4, pid, status, options, NULL); |
| 234 | |
| 235 | return VG_(is_kerror)(ret) ? -1 : ret; |
| 236 | } |
| 237 | |
| 238 | Int VG_(gettid)(void) |
| 239 | { |
| 240 | Int ret; |
| 241 | |
| 242 | ret = VG_(do_syscall)(__NR_gettid); |
| 243 | |
thughes | 8ef6d96 | 2004-10-16 10:46:01 +0000 | [diff] [blame] | 244 | if (ret == -VKI_ENOSYS) { |
| 245 | Char pid[16]; |
| 246 | |
| 247 | /* |
| 248 | * The gettid system call does not exist. The obvious assumption |
| 249 | * to make at this point would be that we are running on an older |
| 250 | * system where the getpid system call actually returns the ID of |
| 251 | * the current thread. |
| 252 | * |
| 253 | * Unfortunately it seems that there are some systems with a kernel |
| 254 | * where getpid has been changed to return the ID of the thread group |
| 255 | * leader but where the gettid system call has not yet been added. |
| 256 | * |
| 257 | * So instead of calling getpid here we use readlink to see where |
| 258 | * the /proc/self link is pointing... |
| 259 | */ |
| 260 | if ((ret = VG_(do_syscall)(__NR_readlink, "/proc/self", pid, sizeof(pid))) >= 0) { |
| 261 | pid[ret] = '\0'; |
| 262 | ret = VG_(atoll)(pid); |
| 263 | } |
| 264 | } |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 265 | |
| 266 | return ret; |
| 267 | } |
| 268 | |
| 269 | |
sewardj | efbfcdf | 2002-06-19 17:35:45 +0000 | [diff] [blame] | 270 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 271 | /* --------------------------------------------------------------------- |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 272 | mmap/munmap, exit, fcntl |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 273 | ------------------------------------------------------------------ */ |
| 274 | |
nethercote | cf51cc4 | 2004-11-04 17:02:42 +0000 | [diff] [blame] | 275 | static Int munmap_inner(void *start, SizeT length) |
fitzhardinge | b50068f | 2004-02-24 23:42:55 +0000 | [diff] [blame] | 276 | { |
nethercote | cf51cc4 | 2004-11-04 17:02:42 +0000 | [diff] [blame] | 277 | return VG_(do_syscall)(__NR_munmap, (UWord)start, length ); |
fitzhardinge | b50068f | 2004-02-24 23:42:55 +0000 | [diff] [blame] | 278 | } |
| 279 | |
nethercote | 5b9fafd | 2004-11-04 18:39:22 +0000 | [diff] [blame] | 280 | static Addr mmap_inner(void *start, SizeT length, UInt prot, UInt flags, |
| 281 | UInt fd, OffT offset) |
fitzhardinge | b50068f | 2004-02-24 23:42:55 +0000 | [diff] [blame] | 282 | { |
nethercote | 6456ab1 | 2004-10-18 14:47:48 +0000 | [diff] [blame] | 283 | Int ret; |
| 284 | |
| 285 | PLATFORM_DO_MMAP(ret, start, length, prot, |
| 286 | flags & ~(VKI_MAP_NOSYMS|VKI_MAP_CLIENT), |
| 287 | fd, offset); |
| 288 | return ret; |
fitzhardinge | b50068f | 2004-02-24 23:42:55 +0000 | [diff] [blame] | 289 | } |
| 290 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 291 | /* Returns -1 on failure. */ |
nethercote | 8b5f40c | 2004-11-02 13:29:50 +0000 | [diff] [blame] | 292 | void* VG_(mmap)( void* start, SizeT length, |
nethercote | 5b9fafd | 2004-11-04 18:39:22 +0000 | [diff] [blame] | 293 | UInt prot, UInt flags, UInt sf_flags, UInt fd, OffT offset) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 294 | { |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 295 | Addr res; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 296 | |
| 297 | if (!(flags & VKI_MAP_FIXED)) { |
| 298 | start = (void *)VG_(find_map_space)((Addr)start, length, !!(flags & VKI_MAP_CLIENT)); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 299 | |
| 300 | flags |= VKI_MAP_FIXED; |
| 301 | } |
nethercote | b4250ae | 2004-07-10 16:50:09 +0000 | [diff] [blame] | 302 | if (start == 0) |
| 303 | return (void *)-1; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 304 | |
fitzhardinge | b50068f | 2004-02-24 23:42:55 +0000 | [diff] [blame] | 305 | res = mmap_inner(start, length, prot, flags, fd, offset); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 306 | |
nethercote | b4250ae | 2004-07-10 16:50:09 +0000 | [diff] [blame] | 307 | // Check it ended up in the right place. |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 308 | if (!VG_(is_kerror)(res)) { |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 309 | if (flags & VKI_MAP_CLIENT) { |
nethercote | 0060050 | 2004-09-11 23:27:09 +0000 | [diff] [blame] | 310 | vg_assert(VG_(client_base) <= res && res+length <= VG_(client_end)); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 311 | } else { |
nethercote | fb583e2 | 2004-09-07 23:15:37 +0000 | [diff] [blame] | 312 | vg_assert(VG_(valgrind_base) <= res && res+length-1 <= VG_(valgrind_last)); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 313 | } |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 314 | |
nethercote | b4250ae | 2004-07-10 16:50:09 +0000 | [diff] [blame] | 315 | sf_flags |= SF_MMAP; |
| 316 | if ( flags & VKI_MAP_FIXED) sf_flags |= SF_FIXED; |
| 317 | if ( flags & VKI_MAP_SHARED) sf_flags |= SF_SHARED; |
| 318 | if (!(flags & VKI_MAP_ANONYMOUS)) sf_flags |= SF_FILE; |
| 319 | if (!(flags & VKI_MAP_CLIENT)) sf_flags |= SF_VALGRIND; |
| 320 | if ( flags & VKI_MAP_NOSYMS) sf_flags |= SF_NOSYMS; |
| 321 | |
| 322 | VG_(map_fd_segment)(res, length, prot, sf_flags, fd, offset, NULL); |
| 323 | } |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 324 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 325 | return VG_(is_kerror)(res) ? ((void*)(-1)) : (void*)res; |
| 326 | } |
| 327 | |
| 328 | /* Returns -1 on failure. */ |
nethercote | 8b5f40c | 2004-11-02 13:29:50 +0000 | [diff] [blame] | 329 | Int VG_(munmap)( void* start, SizeT length ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 330 | { |
fitzhardinge | b50068f | 2004-02-24 23:42:55 +0000 | [diff] [blame] | 331 | Int res = munmap_inner(start, length); |
fitzhardinge | 920d04e | 2003-12-20 18:19:50 +0000 | [diff] [blame] | 332 | if (!VG_(is_kerror)(res)) |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 333 | VG_(unmap_range)((Addr)start, length); |
| 334 | return VG_(is_kerror)(res) ? -1 : 0; |
| 335 | } |
| 336 | |
nethercote | 8b5f40c | 2004-11-02 13:29:50 +0000 | [diff] [blame] | 337 | Int VG_(mprotect)( void *start, SizeT length, UInt prot ) |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 338 | { |
nethercote | 8b5f40c | 2004-11-02 13:29:50 +0000 | [diff] [blame] | 339 | Int res = VG_(do_syscall)(__NR_mprotect, (UWord)start, length, prot ); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 340 | if (!VG_(is_kerror)(res)) |
| 341 | VG_(mprotect_range)((Addr)start, length, prot); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 342 | return VG_(is_kerror)(res) ? -1 : 0; |
| 343 | } |
| 344 | |
| 345 | void VG_(exit)( Int status ) |
| 346 | { |
nethercote | cf51cc4 | 2004-11-04 17:02:42 +0000 | [diff] [blame] | 347 | (void)VG_(do_syscall)(__NR_exit_group, status ); |
| 348 | (void)VG_(do_syscall)(__NR_exit, status ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 349 | /* Why are we still alive here? */ |
| 350 | /*NOTREACHED*/ |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 351 | *(volatile Int *)0 = 'x'; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 352 | vg_assert(2+2 == 5); |
| 353 | } |
| 354 | |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 355 | /* Returns -1 on error. */ |
| 356 | Int VG_(fcntl) ( Int fd, Int cmd, Int arg ) |
| 357 | { |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 358 | Int res = VG_(do_syscall)(__NR_fcntl, fd, cmd, arg); |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 359 | return VG_(is_kerror)(res) ? -1 : res; |
| 360 | } |
| 361 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 362 | Int VG_(poll)( struct vki_pollfd *ufds, UInt nfds, Int timeout) |
| 363 | { |
| 364 | Int res = VG_(do_syscall)(__NR_poll, ufds, nfds, timeout); |
| 365 | |
| 366 | return res; |
| 367 | } |
| 368 | |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 369 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 370 | /* --------------------------------------------------------------------- |
| 371 | printf implementation. The key function, vg_vprintf(), emits chars |
| 372 | into a caller-supplied function. Distantly derived from: |
| 373 | |
| 374 | vprintf replacement for Checker. |
| 375 | Copyright 1993, 1994, 1995 Tristan Gingold |
| 376 | Written September 1993 Tristan Gingold |
| 377 | Tristan Gingold, 8 rue Parmentier, F-91120 PALAISEAU, FRANCE |
| 378 | |
| 379 | (Checker itself was GPL'd.) |
| 380 | ------------------------------------------------------------------ */ |
| 381 | |
| 382 | |
| 383 | /* Some flags. */ |
| 384 | #define VG_MSG_SIGNED 1 /* The value is signed. */ |
| 385 | #define VG_MSG_ZJUSTIFY 2 /* Must justify with '0'. */ |
| 386 | #define VG_MSG_LJUSTIFY 4 /* Must justify on the left. */ |
sewardj | 78e3cd9 | 2002-10-22 04:45:48 +0000 | [diff] [blame] | 387 | #define VG_MSG_PAREN 8 /* Parenthesize if present (for %y) */ |
njn | 607adfc | 2003-09-30 14:15:44 +0000 | [diff] [blame] | 388 | #define VG_MSG_COMMA 16 /* Add commas to numbers (for %d, %u) */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 389 | |
| 390 | /* Copy a string into the buffer. */ |
sewardj | 78e3cd9 | 2002-10-22 04:45:48 +0000 | [diff] [blame] | 391 | static UInt |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 392 | myvprintf_str ( void(*send)(Char), Int flags, Int width, Char* str, |
| 393 | Bool capitalise ) |
| 394 | { |
| 395 | # define MAYBE_TOUPPER(ch) (capitalise ? VG_(toupper)(ch) : (ch)) |
sewardj | 78e3cd9 | 2002-10-22 04:45:48 +0000 | [diff] [blame] | 396 | UInt ret = 0; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 397 | Int i, extra; |
| 398 | Int len = VG_(strlen)(str); |
| 399 | |
| 400 | if (width == 0) { |
sewardj | 78e3cd9 | 2002-10-22 04:45:48 +0000 | [diff] [blame] | 401 | ret += len; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 402 | for (i = 0; i < len; i++) |
| 403 | send(MAYBE_TOUPPER(str[i])); |
sewardj | 78e3cd9 | 2002-10-22 04:45:48 +0000 | [diff] [blame] | 404 | return ret; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | if (len > width) { |
sewardj | 78e3cd9 | 2002-10-22 04:45:48 +0000 | [diff] [blame] | 408 | ret += width; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 409 | for (i = 0; i < width; i++) |
| 410 | send(MAYBE_TOUPPER(str[i])); |
sewardj | 78e3cd9 | 2002-10-22 04:45:48 +0000 | [diff] [blame] | 411 | return ret; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | extra = width - len; |
| 415 | if (flags & VG_MSG_LJUSTIFY) { |
sewardj | 78e3cd9 | 2002-10-22 04:45:48 +0000 | [diff] [blame] | 416 | ret += extra; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 417 | for (i = 0; i < extra; i++) |
| 418 | send(' '); |
| 419 | } |
sewardj | 78e3cd9 | 2002-10-22 04:45:48 +0000 | [diff] [blame] | 420 | ret += len; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 421 | for (i = 0; i < len; i++) |
| 422 | send(MAYBE_TOUPPER(str[i])); |
| 423 | if (!(flags & VG_MSG_LJUSTIFY)) { |
sewardj | 78e3cd9 | 2002-10-22 04:45:48 +0000 | [diff] [blame] | 424 | ret += extra; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 425 | for (i = 0; i < extra; i++) |
| 426 | send(' '); |
| 427 | } |
| 428 | |
| 429 | # undef MAYBE_TOUPPER |
sewardj | 78e3cd9 | 2002-10-22 04:45:48 +0000 | [diff] [blame] | 430 | |
| 431 | return ret; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | /* Write P into the buffer according to these args: |
| 435 | * If SIGN is true, p is a signed. |
| 436 | * BASE is the base. |
| 437 | * If WITH_ZERO is true, '0' must be added. |
| 438 | * WIDTH is the width of the field. |
| 439 | */ |
sewardj | 78e3cd9 | 2002-10-22 04:45:48 +0000 | [diff] [blame] | 440 | static UInt |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 441 | myvprintf_int64 ( void(*send)(Char), Int flags, Int base, Int width, ULong p) |
| 442 | { |
| 443 | Char buf[40]; |
| 444 | Int ind = 0; |
njn | 607adfc | 2003-09-30 14:15:44 +0000 | [diff] [blame] | 445 | Int i, nc = 0; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 446 | Bool neg = False; |
| 447 | Char *digits = "0123456789ABCDEF"; |
sewardj | 78e3cd9 | 2002-10-22 04:45:48 +0000 | [diff] [blame] | 448 | UInt ret = 0; |
| 449 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 450 | if (base < 2 || base > 16) |
sewardj | 78e3cd9 | 2002-10-22 04:45:48 +0000 | [diff] [blame] | 451 | return ret; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 452 | |
| 453 | if ((flags & VG_MSG_SIGNED) && (Long)p < 0) { |
| 454 | p = - (Long)p; |
| 455 | neg = True; |
| 456 | } |
| 457 | |
| 458 | if (p == 0) |
| 459 | buf[ind++] = '0'; |
| 460 | else { |
| 461 | while (p > 0) { |
njn | 607adfc | 2003-09-30 14:15:44 +0000 | [diff] [blame] | 462 | if (flags & VG_MSG_COMMA && 10 == base && |
| 463 | 0 == (ind-nc) % 3 && 0 != ind) |
| 464 | { |
| 465 | buf[ind++] = ','; |
| 466 | nc++; |
| 467 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 468 | buf[ind++] = digits[p % base]; |
| 469 | p /= base; |
njn | 607adfc | 2003-09-30 14:15:44 +0000 | [diff] [blame] | 470 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | if (neg) |
| 474 | buf[ind++] = '-'; |
| 475 | |
| 476 | if (width > 0 && !(flags & VG_MSG_LJUSTIFY)) { |
| 477 | for(; ind < width; ind++) { |
| 478 | vg_assert(ind < 39); |
| 479 | buf[ind] = (flags & VG_MSG_ZJUSTIFY) ? '0': ' '; |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | /* Reverse copy to buffer. */ |
sewardj | 78e3cd9 | 2002-10-22 04:45:48 +0000 | [diff] [blame] | 484 | ret += ind; |
njn | 607adfc | 2003-09-30 14:15:44 +0000 | [diff] [blame] | 485 | for (i = ind -1; i >= 0; i--) { |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 486 | send(buf[i]); |
njn | 607adfc | 2003-09-30 14:15:44 +0000 | [diff] [blame] | 487 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 488 | if (width > 0 && (flags & VG_MSG_LJUSTIFY)) { |
sewardj | 78e3cd9 | 2002-10-22 04:45:48 +0000 | [diff] [blame] | 489 | for(; ind < width; ind++) { |
| 490 | ret++; |
njn | 607adfc | 2003-09-30 14:15:44 +0000 | [diff] [blame] | 491 | send(' '); // Never pad with zeroes on RHS -- changes the value! |
sewardj | 78e3cd9 | 2002-10-22 04:45:48 +0000 | [diff] [blame] | 492 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 493 | } |
sewardj | 78e3cd9 | 2002-10-22 04:45:48 +0000 | [diff] [blame] | 494 | return ret; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | |
| 498 | /* A simple vprintf(). */ |
sewardj | 78e3cd9 | 2002-10-22 04:45:48 +0000 | [diff] [blame] | 499 | UInt |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 500 | VG_(vprintf) ( void(*send)(Char), const Char *format, va_list vargs ) |
| 501 | { |
sewardj | 78e3cd9 | 2002-10-22 04:45:48 +0000 | [diff] [blame] | 502 | UInt ret = 0; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 503 | int i; |
| 504 | int flags; |
| 505 | int width; |
| 506 | Bool is_long; |
| 507 | |
| 508 | /* We assume that vargs has already been initialised by the |
| 509 | caller, using va_start, and that the caller will similarly |
| 510 | clean up with va_end. |
| 511 | */ |
| 512 | |
| 513 | for (i = 0; format[i] != 0; i++) { |
| 514 | if (format[i] != '%') { |
| 515 | send(format[i]); |
sewardj | 78e3cd9 | 2002-10-22 04:45:48 +0000 | [diff] [blame] | 516 | ret++; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 517 | continue; |
| 518 | } |
| 519 | i++; |
| 520 | /* A '%' has been found. Ignore a trailing %. */ |
| 521 | if (format[i] == 0) |
| 522 | break; |
| 523 | if (format[i] == '%') { |
| 524 | /* `%%' is replaced by `%'. */ |
| 525 | send('%'); |
sewardj | 78e3cd9 | 2002-10-22 04:45:48 +0000 | [diff] [blame] | 526 | ret++; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 527 | continue; |
| 528 | } |
| 529 | flags = 0; |
| 530 | is_long = False; |
| 531 | width = 0; /* length of the field. */ |
sewardj | 78e3cd9 | 2002-10-22 04:45:48 +0000 | [diff] [blame] | 532 | if (format[i] == '(') { |
| 533 | flags |= VG_MSG_PAREN; |
| 534 | i++; |
| 535 | } |
njn | 607adfc | 2003-09-30 14:15:44 +0000 | [diff] [blame] | 536 | /* If ',' follows '%', commas will be inserted. */ |
| 537 | if (format[i] == ',') { |
| 538 | flags |= VG_MSG_COMMA; |
| 539 | i++; |
| 540 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 541 | /* If '-' follows '%', justify on the left. */ |
| 542 | if (format[i] == '-') { |
| 543 | flags |= VG_MSG_LJUSTIFY; |
| 544 | i++; |
| 545 | } |
| 546 | /* If '0' follows '%', pads will be inserted. */ |
| 547 | if (format[i] == '0') { |
| 548 | flags |= VG_MSG_ZJUSTIFY; |
| 549 | i++; |
| 550 | } |
| 551 | /* Compute the field length. */ |
| 552 | while (format[i] >= '0' && format[i] <= '9') { |
| 553 | width *= 10; |
| 554 | width += format[i++] - '0'; |
| 555 | } |
| 556 | while (format[i] == 'l') { |
| 557 | i++; |
| 558 | is_long = True; |
| 559 | } |
| 560 | |
| 561 | switch (format[i]) { |
| 562 | case 'd': /* %d */ |
| 563 | flags |= VG_MSG_SIGNED; |
| 564 | if (is_long) |
sewardj | 78e3cd9 | 2002-10-22 04:45:48 +0000 | [diff] [blame] | 565 | ret += myvprintf_int64(send, flags, 10, width, |
| 566 | (ULong)(va_arg (vargs, Long))); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 567 | else |
sewardj | 78e3cd9 | 2002-10-22 04:45:48 +0000 | [diff] [blame] | 568 | ret += myvprintf_int64(send, flags, 10, width, |
| 569 | (ULong)(va_arg (vargs, Int))); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 570 | break; |
| 571 | case 'u': /* %u */ |
| 572 | if (is_long) |
sewardj | 78e3cd9 | 2002-10-22 04:45:48 +0000 | [diff] [blame] | 573 | ret += myvprintf_int64(send, flags, 10, width, |
| 574 | (ULong)(va_arg (vargs, ULong))); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 575 | else |
sewardj | 78e3cd9 | 2002-10-22 04:45:48 +0000 | [diff] [blame] | 576 | ret += myvprintf_int64(send, flags, 10, width, |
| 577 | (ULong)(va_arg (vargs, UInt))); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 578 | break; |
| 579 | case 'p': /* %p */ |
sewardj | 78e3cd9 | 2002-10-22 04:45:48 +0000 | [diff] [blame] | 580 | ret += 2; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 581 | send('0'); |
| 582 | send('x'); |
sewardj | 78e3cd9 | 2002-10-22 04:45:48 +0000 | [diff] [blame] | 583 | ret += myvprintf_int64(send, flags, 16, width, |
nethercote | 50397c2 | 2004-11-04 18:03:06 +0000 | [diff] [blame] | 584 | (ULong)((UWord)va_arg (vargs, void *))); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 585 | break; |
| 586 | case 'x': /* %x */ |
| 587 | if (is_long) |
sewardj | 78e3cd9 | 2002-10-22 04:45:48 +0000 | [diff] [blame] | 588 | ret += myvprintf_int64(send, flags, 16, width, |
| 589 | (ULong)(va_arg (vargs, ULong))); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 590 | else |
sewardj | 78e3cd9 | 2002-10-22 04:45:48 +0000 | [diff] [blame] | 591 | ret += myvprintf_int64(send, flags, 16, width, |
| 592 | (ULong)(va_arg (vargs, UInt))); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 593 | break; |
| 594 | case 'c': /* %c */ |
sewardj | 78e3cd9 | 2002-10-22 04:45:48 +0000 | [diff] [blame] | 595 | ret++; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 596 | send(va_arg (vargs, int)); |
| 597 | break; |
| 598 | case 's': case 'S': { /* %s */ |
| 599 | char *str = va_arg (vargs, char *); |
| 600 | if (str == (char*) 0) str = "(null)"; |
sewardj | 78e3cd9 | 2002-10-22 04:45:48 +0000 | [diff] [blame] | 601 | ret += myvprintf_str(send, flags, width, str, format[i]=='S'); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 602 | break; |
sewardj | 78e3cd9 | 2002-10-22 04:45:48 +0000 | [diff] [blame] | 603 | } |
| 604 | case 'y': { /* %y - print symbol */ |
| 605 | Char buf[100]; |
| 606 | Char *cp = buf; |
| 607 | Addr a = va_arg(vargs, Addr); |
| 608 | |
| 609 | if (flags & VG_MSG_PAREN) |
| 610 | *cp++ = '('; |
sewardj | 6e008cb | 2002-12-15 13:11:39 +0000 | [diff] [blame] | 611 | if (VG_(get_fnname_w_offset)(a, cp, sizeof(buf)-4)) { |
sewardj | 78e3cd9 | 2002-10-22 04:45:48 +0000 | [diff] [blame] | 612 | if (flags & VG_MSG_PAREN) { |
| 613 | cp += VG_(strlen)(cp); |
| 614 | *cp++ = ')'; |
| 615 | *cp = '\0'; |
| 616 | } |
| 617 | ret += myvprintf_str(send, flags, width, buf, 0); |
| 618 | } |
| 619 | |
| 620 | break; |
| 621 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 622 | default: |
| 623 | break; |
| 624 | } |
| 625 | } |
sewardj | 78e3cd9 | 2002-10-22 04:45:48 +0000 | [diff] [blame] | 626 | return ret; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | |
| 630 | /* A general replacement for printf(). Note that only low-level |
| 631 | debugging info should be sent via here. The official route is to |
| 632 | to use vg_message(). This interface is deprecated. |
| 633 | */ |
| 634 | static char myprintf_buf[100]; |
| 635 | static int n_myprintf_buf; |
| 636 | |
| 637 | static void add_to_myprintf_buf ( Char c ) |
| 638 | { |
| 639 | if (n_myprintf_buf >= 100-10 /*paranoia*/ ) { |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 640 | if (VG_(clo_log_fd) >= 0) { |
sewardj | 570f890 | 2002-11-03 11:44:36 +0000 | [diff] [blame] | 641 | VG_(send_bytes_to_logging_sink)( |
| 642 | myprintf_buf, VG_(strlen)(myprintf_buf) ); |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 643 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 644 | n_myprintf_buf = 0; |
| 645 | myprintf_buf[n_myprintf_buf] = 0; |
| 646 | } |
| 647 | myprintf_buf[n_myprintf_buf++] = c; |
| 648 | myprintf_buf[n_myprintf_buf] = 0; |
| 649 | } |
| 650 | |
sewardj | 78e3cd9 | 2002-10-22 04:45:48 +0000 | [diff] [blame] | 651 | UInt VG_(printf) ( const char *format, ... ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 652 | { |
sewardj | 78e3cd9 | 2002-10-22 04:45:48 +0000 | [diff] [blame] | 653 | UInt ret; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 654 | va_list vargs; |
| 655 | va_start(vargs,format); |
| 656 | |
| 657 | n_myprintf_buf = 0; |
| 658 | myprintf_buf[n_myprintf_buf] = 0; |
sewardj | 78e3cd9 | 2002-10-22 04:45:48 +0000 | [diff] [blame] | 659 | ret = VG_(vprintf) ( add_to_myprintf_buf, format, vargs ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 660 | |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 661 | if (n_myprintf_buf > 0 && VG_(clo_log_fd) >= 0) { |
sewardj | 570f890 | 2002-11-03 11:44:36 +0000 | [diff] [blame] | 662 | VG_(send_bytes_to_logging_sink)( myprintf_buf, n_myprintf_buf ); |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 663 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 664 | |
| 665 | va_end(vargs); |
sewardj | 78e3cd9 | 2002-10-22 04:45:48 +0000 | [diff] [blame] | 666 | |
| 667 | return ret; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 668 | } |
| 669 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 670 | /* A general replacement for sprintf(). */ |
thughes | 4ad52d0 | 2004-06-27 17:37:21 +0000 | [diff] [blame] | 671 | |
| 672 | static Char *vg_sprintf_ptr; |
| 673 | |
| 674 | static void add_to_vg_sprintf_buf ( Char c ) |
| 675 | { |
| 676 | *vg_sprintf_ptr++ = c; |
| 677 | } |
| 678 | |
sewardj | 78e3cd9 | 2002-10-22 04:45:48 +0000 | [diff] [blame] | 679 | UInt VG_(sprintf) ( Char* buf, Char *format, ... ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 680 | { |
sewardj | 05bcdcb | 2003-05-18 10:05:38 +0000 | [diff] [blame] | 681 | Int ret; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 682 | va_list vargs; |
thughes | 4ad52d0 | 2004-06-27 17:37:21 +0000 | [diff] [blame] | 683 | |
| 684 | vg_sprintf_ptr = buf; |
| 685 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 686 | va_start(vargs,format); |
| 687 | |
sewardj | 78e3cd9 | 2002-10-22 04:45:48 +0000 | [diff] [blame] | 688 | ret = VG_(vprintf) ( add_to_vg_sprintf_buf, format, vargs ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 689 | add_to_vg_sprintf_buf(0); |
| 690 | |
| 691 | va_end(vargs); |
sewardj | 78e3cd9 | 2002-10-22 04:45:48 +0000 | [diff] [blame] | 692 | |
| 693 | vg_assert(VG_(strlen)(buf) == ret); |
| 694 | return ret; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | |
| 698 | /* --------------------------------------------------------------------- |
| 699 | Misc str* functions. |
| 700 | ------------------------------------------------------------------ */ |
| 701 | |
| 702 | Bool VG_(isspace) ( Char c ) |
| 703 | { |
| 704 | return (c == ' ' || c == '\n' || c == '\t' || c == 0); |
| 705 | } |
| 706 | |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 707 | Bool VG_(isdigit) ( Char c ) |
| 708 | { |
| 709 | return (c >= '0' && c <= '9'); |
| 710 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 711 | |
| 712 | Int VG_(strlen) ( const Char* str ) |
| 713 | { |
| 714 | Int i = 0; |
| 715 | while (str[i] != 0) i++; |
| 716 | return i; |
| 717 | } |
| 718 | |
| 719 | |
| 720 | Long VG_(atoll) ( Char* str ) |
| 721 | { |
| 722 | Bool neg = False; |
| 723 | Long n = 0; |
| 724 | if (*str == '-') { str++; neg = True; }; |
| 725 | while (*str >= '0' && *str <= '9') { |
| 726 | n = 10*n + (Long)(*str - '0'); |
| 727 | str++; |
| 728 | } |
| 729 | if (neg) n = -n; |
| 730 | return n; |
| 731 | } |
| 732 | |
| 733 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 734 | Long VG_(atoll16) ( Char* str ) |
sewardj | a70ca3f | 2002-05-30 01:22:20 +0000 | [diff] [blame] | 735 | { |
| 736 | Bool neg = False; |
| 737 | Long n = 0; |
| 738 | if (*str == '-') { str++; neg = True; }; |
| 739 | while (True) { |
| 740 | if (*str >= '0' && *str <= '9') { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 741 | n = 16*n + (Long)(*str - '0'); |
sewardj | a70ca3f | 2002-05-30 01:22:20 +0000 | [diff] [blame] | 742 | } |
| 743 | else |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 744 | if (*str >= 'A' && *str <= 'F') { |
| 745 | n = 16*n + (Long)((*str - 'A') + 10); |
sewardj | a70ca3f | 2002-05-30 01:22:20 +0000 | [diff] [blame] | 746 | } |
| 747 | else |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 748 | if (*str >= 'a' && *str <= 'f') { |
| 749 | n = 16*n + (Long)((*str - 'a') + 10); |
| 750 | } |
| 751 | else { |
| 752 | break; |
| 753 | } |
| 754 | str++; |
| 755 | } |
| 756 | if (neg) n = -n; |
| 757 | return n; |
| 758 | } |
| 759 | |
| 760 | Long VG_(atoll36) ( UInt base, Char* str ) |
| 761 | { |
| 762 | Bool neg = False; |
| 763 | Long n = 0; |
| 764 | vg_assert(base >= 2 && base <= 36); |
| 765 | if (*str == '-') { str++; neg = True; }; |
| 766 | while (True) { |
sewardj | 05bcdcb | 2003-05-18 10:05:38 +0000 | [diff] [blame] | 767 | if (*str >= '0' |
| 768 | && *str <= (Char)('9' - (10 - base))) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 769 | n = base*n + (Long)(*str - '0'); |
| 770 | } |
| 771 | else |
sewardj | 05bcdcb | 2003-05-18 10:05:38 +0000 | [diff] [blame] | 772 | if (base > 10 && *str >= 'A' |
| 773 | && *str <= (Char)('Z' - (36 - base))) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 774 | n = base*n + (Long)((*str - 'A') + 10); |
| 775 | } |
| 776 | else |
sewardj | 05bcdcb | 2003-05-18 10:05:38 +0000 | [diff] [blame] | 777 | if (base > 10 && *str >= 'a' |
| 778 | && *str <= (Char)('z' - (36 - base))) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 779 | n = base*n + (Long)((*str - 'a') + 10); |
sewardj | a70ca3f | 2002-05-30 01:22:20 +0000 | [diff] [blame] | 780 | } |
| 781 | else { |
| 782 | break; |
| 783 | } |
| 784 | str++; |
| 785 | } |
| 786 | if (neg) n = -n; |
| 787 | return n; |
| 788 | } |
| 789 | |
| 790 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 791 | Char* VG_(strcat) ( Char* dest, const Char* src ) |
| 792 | { |
| 793 | Char* dest_orig = dest; |
| 794 | while (*dest) dest++; |
| 795 | while (*src) *dest++ = *src++; |
| 796 | *dest = 0; |
| 797 | return dest_orig; |
| 798 | } |
| 799 | |
| 800 | |
| 801 | Char* VG_(strncat) ( Char* dest, const Char* src, Int n ) |
| 802 | { |
| 803 | Char* dest_orig = dest; |
| 804 | while (*dest) dest++; |
| 805 | while (*src && n > 0) { *dest++ = *src++; n--; } |
| 806 | *dest = 0; |
| 807 | return dest_orig; |
| 808 | } |
| 809 | |
| 810 | |
| 811 | Char* VG_(strpbrk) ( const Char* s, const Char* accept ) |
| 812 | { |
| 813 | const Char* a; |
| 814 | while (*s) { |
| 815 | a = accept; |
| 816 | while (*a) |
| 817 | if (*a++ == *s) |
| 818 | return (Char *) s; |
| 819 | s++; |
| 820 | } |
| 821 | return NULL; |
| 822 | } |
| 823 | |
| 824 | |
| 825 | Char* VG_(strcpy) ( Char* dest, const Char* src ) |
| 826 | { |
| 827 | Char* dest_orig = dest; |
| 828 | while (*src) *dest++ = *src++; |
| 829 | *dest = 0; |
| 830 | return dest_orig; |
| 831 | } |
| 832 | |
| 833 | |
| 834 | /* Copy bytes, not overrunning the end of dest and always ensuring |
| 835 | zero termination. */ |
| 836 | void VG_(strncpy_safely) ( Char* dest, const Char* src, Int ndest ) |
| 837 | { |
| 838 | Int i; |
| 839 | vg_assert(ndest > 0); |
| 840 | i = 0; |
| 841 | dest[i] = 0; |
| 842 | while (True) { |
| 843 | if (src[i] == 0) return; |
| 844 | if (i >= ndest-1) return; |
| 845 | dest[i] = src[i]; |
| 846 | i++; |
| 847 | dest[i] = 0; |
| 848 | } |
| 849 | } |
| 850 | |
| 851 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 852 | Char* VG_(strncpy) ( Char* dest, const Char* src, Int ndest ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 853 | { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 854 | Int i = 0; |
| 855 | while (True) { |
| 856 | if (i >= ndest) return dest; /* reached limit */ |
| 857 | dest[i] = src[i]; |
| 858 | if (src[i++] == 0) { |
| 859 | /* reached NUL; pad rest with zeroes as required */ |
| 860 | while (i < ndest) dest[i++] = 0; |
| 861 | return dest; |
| 862 | } |
| 863 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | |
| 867 | Int VG_(strcmp) ( const Char* s1, const Char* s2 ) |
| 868 | { |
| 869 | while (True) { |
| 870 | if (*s1 == 0 && *s2 == 0) return 0; |
| 871 | if (*s1 == 0) return -1; |
| 872 | if (*s2 == 0) return 1; |
| 873 | |
| 874 | if (*(UChar*)s1 < *(UChar*)s2) return -1; |
| 875 | if (*(UChar*)s1 > *(UChar*)s2) return 1; |
| 876 | |
| 877 | s1++; s2++; |
| 878 | } |
| 879 | } |
| 880 | |
| 881 | |
| 882 | Int VG_(strcmp_ws) ( const Char* s1, const Char* s2 ) |
| 883 | { |
| 884 | while (True) { |
| 885 | if (VG_(isspace)(*s1) && VG_(isspace)(*s2)) return 0; |
| 886 | if (VG_(isspace)(*s1)) return -1; |
| 887 | if (VG_(isspace)(*s2)) return 1; |
| 888 | |
| 889 | if (*(UChar*)s1 < *(UChar*)s2) return -1; |
| 890 | if (*(UChar*)s1 > *(UChar*)s2) return 1; |
| 891 | |
| 892 | s1++; s2++; |
| 893 | } |
| 894 | } |
| 895 | |
| 896 | |
| 897 | Int VG_(strncmp) ( const Char* s1, const Char* s2, Int nmax ) |
| 898 | { |
| 899 | Int n = 0; |
| 900 | while (True) { |
| 901 | if (n >= nmax) return 0; |
| 902 | if (*s1 == 0 && *s2 == 0) return 0; |
| 903 | if (*s1 == 0) return -1; |
| 904 | if (*s2 == 0) return 1; |
| 905 | |
| 906 | if (*(UChar*)s1 < *(UChar*)s2) return -1; |
| 907 | if (*(UChar*)s1 > *(UChar*)s2) return 1; |
| 908 | |
| 909 | s1++; s2++; n++; |
| 910 | } |
| 911 | } |
| 912 | |
| 913 | |
| 914 | Int VG_(strncmp_ws) ( const Char* s1, const Char* s2, Int nmax ) |
| 915 | { |
| 916 | Int n = 0; |
| 917 | while (True) { |
| 918 | if (n >= nmax) return 0; |
| 919 | if (VG_(isspace)(*s1) && VG_(isspace)(*s2)) return 0; |
| 920 | if (VG_(isspace)(*s1)) return -1; |
| 921 | if (VG_(isspace)(*s2)) return 1; |
| 922 | |
| 923 | if (*(UChar*)s1 < *(UChar*)s2) return -1; |
| 924 | if (*(UChar*)s1 > *(UChar*)s2) return 1; |
| 925 | |
| 926 | s1++; s2++; n++; |
| 927 | } |
| 928 | } |
| 929 | |
| 930 | |
| 931 | Char* VG_(strstr) ( const Char* haystack, Char* needle ) |
| 932 | { |
sewardj | 3984b85 | 2002-05-12 03:00:17 +0000 | [diff] [blame] | 933 | Int n; |
| 934 | if (haystack == NULL) |
| 935 | return NULL; |
| 936 | n = VG_(strlen)(needle); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 937 | while (True) { |
| 938 | if (haystack[0] == 0) |
| 939 | return NULL; |
| 940 | if (VG_(strncmp)(haystack, needle, n) == 0) |
| 941 | return (Char*)haystack; |
| 942 | haystack++; |
| 943 | } |
| 944 | } |
| 945 | |
| 946 | |
| 947 | Char* VG_(strchr) ( const Char* s, Char c ) |
| 948 | { |
| 949 | while (True) { |
| 950 | if (*s == c) return (Char*)s; |
| 951 | if (*s == 0) return NULL; |
| 952 | s++; |
| 953 | } |
| 954 | } |
| 955 | |
| 956 | |
thughes | c0b63de | 2004-06-13 09:55:22 +0000 | [diff] [blame] | 957 | Char* VG_(strrchr) ( const Char* s, Char c ) |
| 958 | { |
| 959 | Int n = VG_(strlen)(s); |
| 960 | while (--n > 0) { |
| 961 | if (s[n] == c) return (Char*)s + n; |
| 962 | } |
| 963 | return NULL; |
| 964 | } |
| 965 | |
| 966 | |
sewardj | 7ab2aca | 2002-10-20 19:40:32 +0000 | [diff] [blame] | 967 | void* VG_(memcpy) ( void *dest, const void *src, Int sz ) |
| 968 | { |
| 969 | const Char *s = (const Char *)src; |
| 970 | Char *d = (Char *)dest; |
| 971 | vg_assert(sz >= 0); |
| 972 | |
| 973 | while (sz--) |
| 974 | *d++ = *s++; |
| 975 | |
| 976 | return dest; |
| 977 | } |
| 978 | |
| 979 | |
| 980 | void* VG_(memset) ( void *dest, Int c, Int sz ) |
| 981 | { |
| 982 | Char *d = (Char *)dest; |
| 983 | vg_assert(sz >= 0); |
| 984 | |
| 985 | while (sz--) |
| 986 | *d++ = c; |
| 987 | |
| 988 | return dest; |
| 989 | } |
| 990 | |
njn | 6d68e79 | 2003-02-24 10:49:08 +0000 | [diff] [blame] | 991 | Int VG_(memcmp) ( const void* s1, const void* s2, Int n ) |
| 992 | { |
| 993 | Int res; |
njn | fa4690b | 2003-02-24 21:43:05 +0000 | [diff] [blame] | 994 | UChar a0; |
| 995 | UChar b0; |
njn | 6d68e79 | 2003-02-24 10:49:08 +0000 | [diff] [blame] | 996 | vg_assert(n >= 0); |
| 997 | |
| 998 | while (n != 0) { |
njn | fa4690b | 2003-02-24 21:43:05 +0000 | [diff] [blame] | 999 | a0 = ((UChar *) s1)[0]; |
| 1000 | b0 = ((UChar *) s2)[0]; |
njn | 6d68e79 | 2003-02-24 10:49:08 +0000 | [diff] [blame] | 1001 | s1 += 1; |
| 1002 | s2 += 1; |
| 1003 | res = a0 - b0; |
| 1004 | if (res != 0) |
| 1005 | return res; |
| 1006 | n -= 1; |
| 1007 | } |
| 1008 | return 0; |
| 1009 | } |
sewardj | 7ab2aca | 2002-10-20 19:40:32 +0000 | [diff] [blame] | 1010 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1011 | Char VG_(toupper) ( Char c ) |
| 1012 | { |
| 1013 | if (c >= 'a' && c <= 'z') |
| 1014 | return c + ('A' - 'a'); |
| 1015 | else |
| 1016 | return c; |
| 1017 | } |
| 1018 | |
| 1019 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1020 | /* Inline just for the wrapper VG_(strdup) below */ |
| 1021 | __inline__ Char* VG_(arena_strdup) ( ArenaId aid, const Char* s ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1022 | { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1023 | Int i; |
fitzhardinge | a772847 | 2003-12-16 01:48:38 +0000 | [diff] [blame] | 1024 | Int len; |
| 1025 | Char* res; |
| 1026 | |
| 1027 | if (s == NULL) |
| 1028 | return NULL; |
| 1029 | |
| 1030 | len = VG_(strlen)(s) + 1; |
| 1031 | res = VG_(arena_malloc) (aid, len); |
| 1032 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1033 | for (i = 0; i < len; i++) |
| 1034 | res[i] = s[i]; |
| 1035 | return res; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1036 | } |
| 1037 | |
nethercote | 7cc9c23 | 2004-01-21 15:08:04 +0000 | [diff] [blame] | 1038 | /* Wrapper to avoid exposing tools to ArenaId's */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1039 | Char* VG_(strdup) ( const Char* s ) |
| 1040 | { |
nethercote | 60f5b82 | 2004-01-26 17:24:42 +0000 | [diff] [blame] | 1041 | return VG_(arena_strdup) ( VG_AR_TOOL, s ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1042 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1043 | |
| 1044 | /* --------------------------------------------------------------------- |
| 1045 | A simple string matching routine, purloined from Hugs98. |
| 1046 | `*' matches any sequence of zero or more characters |
| 1047 | `?' matches any single character exactly |
| 1048 | `\c' matches the character c only (ignoring special chars) |
| 1049 | c matches the character c only |
| 1050 | ------------------------------------------------------------------ */ |
| 1051 | |
| 1052 | /* Keep track of recursion depth. */ |
| 1053 | static Int recDepth; |
| 1054 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1055 | static Bool string_match_wrk ( const Char* pat, const Char* str ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1056 | { |
sewardj | e54b69d | 2003-07-06 01:14:42 +0000 | [diff] [blame] | 1057 | vg_assert(recDepth >= 0 && recDepth < 500); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1058 | recDepth++; |
| 1059 | for (;;) { |
| 1060 | switch (*pat) { |
| 1061 | case '\0' : return (*str=='\0'); |
| 1062 | case '*' : do { |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 1063 | if (string_match_wrk(pat+1,str)) { |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1064 | recDepth--; |
| 1065 | return True; |
| 1066 | } |
| 1067 | } while (*str++); |
| 1068 | recDepth--; |
| 1069 | return False; |
| 1070 | case '?' : if (*str++=='\0') { |
| 1071 | recDepth--; |
| 1072 | return False; |
| 1073 | } |
| 1074 | pat++; |
| 1075 | break; |
| 1076 | case '\\' : if (*++pat == '\0') { |
| 1077 | recDepth--; |
| 1078 | return False; /* spurious trailing \ in pattern */ |
| 1079 | } |
| 1080 | /* falls through to ... */ |
| 1081 | default : if (*pat++ != *str++) { |
| 1082 | recDepth--; |
| 1083 | return False; |
| 1084 | } |
| 1085 | break; |
| 1086 | } |
| 1087 | } |
| 1088 | } |
| 1089 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1090 | Bool VG_(string_match) ( const Char* pat, const Char* str ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1091 | { |
| 1092 | Bool b; |
| 1093 | recDepth = 0; |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 1094 | b = string_match_wrk ( pat, str ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1095 | /* |
| 1096 | VG_(printf)("%s %s %s\n", |
| 1097 | b?"TRUE ":"FALSE", pat, str); |
| 1098 | */ |
| 1099 | return b; |
| 1100 | } |
| 1101 | |
| 1102 | |
| 1103 | /* --------------------------------------------------------------------- |
| 1104 | Assertery. |
| 1105 | ------------------------------------------------------------------ */ |
| 1106 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1107 | /* Fake up an ExeContext which is of our actual real CPU state, so we |
| 1108 | can print a stack trace. This isn't terribly useful in the case |
| 1109 | where we were killed by a signal, since we just get a backtrace |
| 1110 | into the signal handler. Also, it could be somewhat risky if we |
| 1111 | actully got the panic/exception within the execontext/stack |
| 1112 | dump/symtab code. But it's better than nothing. */ |
| 1113 | static inline ExeContext *get_real_execontext(Addr ret) |
| 1114 | { |
| 1115 | ExeContext *ec; |
nethercote | 856e587 | 2004-10-25 20:44:09 +0000 | [diff] [blame] | 1116 | Addr sp, fp; |
nethercote | 759dda3 | 2004-08-07 18:16:56 +0000 | [diff] [blame] | 1117 | Addr stacktop, sigstack_low, sigstack_high; |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1118 | |
nethercote | 856e587 | 2004-10-25 20:44:09 +0000 | [diff] [blame] | 1119 | ARCH_GET_REAL_STACK_PTR(sp); |
| 1120 | ARCH_GET_REAL_FRAME_PTR(fp); |
nethercote | 820bd8c | 2004-09-07 23:04:49 +0000 | [diff] [blame] | 1121 | stacktop = VG_(valgrind_last); |
nethercote | 759dda3 | 2004-08-07 18:16:56 +0000 | [diff] [blame] | 1122 | VG_(get_sigstack_bounds)( &sigstack_low, &sigstack_high ); |
nethercote | 856e587 | 2004-10-25 20:44:09 +0000 | [diff] [blame] | 1123 | if (sp >= sigstack_low && sp < sigstack_high) |
nethercote | 759dda3 | 2004-08-07 18:16:56 +0000 | [diff] [blame] | 1124 | stacktop = sigstack_high; |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1125 | |
nethercote | 856e587 | 2004-10-25 20:44:09 +0000 | [diff] [blame] | 1126 | ec = VG_(get_ExeContext2)(ret, fp, sp, stacktop); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1127 | |
| 1128 | return ec; |
| 1129 | } |
| 1130 | |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1131 | __attribute__ ((noreturn)) |
thughes | 5876d55 | 2004-09-26 18:44:06 +0000 | [diff] [blame] | 1132 | static void report_and_quit ( const Char* report, ExeContext *ec ) |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1133 | { |
thughes | 5876d55 | 2004-09-26 18:44:06 +0000 | [diff] [blame] | 1134 | if (ec == NULL) |
| 1135 | ec = get_real_execontext((Addr)__builtin_return_address(0)); |
| 1136 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1137 | VG_(pp_ExeContext)(ec); |
| 1138 | |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1139 | VG_(pp_sched_status)(); |
sewardj | 366ee1e | 2003-04-26 22:36:42 +0000 | [diff] [blame] | 1140 | VG_(printf)("\n"); |
| 1141 | VG_(printf)("Note: see also the FAQ.txt in the source distribution.\n"); |
| 1142 | VG_(printf)("It contains workarounds to several common problems.\n"); |
| 1143 | VG_(printf)("\n"); |
| 1144 | VG_(printf)("If that doesn't help, please report this bug to: %s\n\n", |
| 1145 | report); |
| 1146 | VG_(printf)("In the bug report, send all the above text, the valgrind\n"); |
sewardj | 96330bd | 2003-04-27 23:46:21 +0000 | [diff] [blame] | 1147 | VG_(printf)("version, and what Linux distro you are using. Thanks.\n\n"); |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1148 | VG_(exit)(1); |
| 1149 | } |
| 1150 | |
| 1151 | __attribute__ ((noreturn)) |
daywalker | 3222e0a | 2003-09-18 01:39:50 +0000 | [diff] [blame] | 1152 | static void assert_fail ( const Char* expr, const Char* name, const Char* report, |
| 1153 | const Char* file, Int line, const Char* fn ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1154 | { |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1155 | static Bool entered = False; |
| 1156 | if (entered) |
| 1157 | VG_(exit)(2); |
| 1158 | entered = True; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1159 | VG_(printf)("\n%s: %s:%d (%s): Assertion `%s' failed.\n", |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1160 | name, file, line, fn, expr ); |
thughes | 5876d55 | 2004-09-26 18:44:06 +0000 | [diff] [blame] | 1161 | report_and_quit(report, NULL); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1162 | } |
| 1163 | |
njn | 94065fd | 2004-11-22 19:26:27 +0000 | [diff] [blame] | 1164 | void VG_(tool_assert_fail) ( const Char* expr, const Char* file, Int line, const Char* fn ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1165 | { |
njn | d04b7c6 | 2002-10-03 14:05:52 +0000 | [diff] [blame] | 1166 | assert_fail(expr, VG_(details).name, VG_(details).bug_reports_to, |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1167 | file, line, fn); |
| 1168 | } |
| 1169 | |
daywalker | 3222e0a | 2003-09-18 01:39:50 +0000 | [diff] [blame] | 1170 | void VG_(core_assert_fail) ( const Char* expr, const Char* file, Int line, const Char* fn ) |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1171 | { |
nethercote | 421281e | 2003-11-20 16:20:55 +0000 | [diff] [blame] | 1172 | assert_fail(expr, "valgrind", VG_BUGS_TO, file, line, fn); |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1173 | } |
| 1174 | |
| 1175 | __attribute__ ((noreturn)) |
thughes | 671ce38 | 2004-09-27 18:57:08 +0000 | [diff] [blame] | 1176 | static void panic ( Char* name, Char* report, Char* str, ExeContext *ec ) |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1177 | { |
| 1178 | VG_(printf)("\n%s: the `impossible' happened:\n %s\n", name, str); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1179 | VG_(printf)("Basic block ctr is approximately %llu\n", VG_(bbs_done) ); |
thughes | 671ce38 | 2004-09-27 18:57:08 +0000 | [diff] [blame] | 1180 | report_and_quit(report, ec); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1181 | } |
| 1182 | |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1183 | void VG_(core_panic) ( Char* str ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1184 | { |
thughes | 5876d55 | 2004-09-26 18:44:06 +0000 | [diff] [blame] | 1185 | panic("valgrind", VG_BUGS_TO, str, NULL); |
| 1186 | } |
| 1187 | |
| 1188 | void VG_(core_panic_at) ( Char* str, ExeContext *ec ) |
| 1189 | { |
| 1190 | panic("valgrind", VG_BUGS_TO, str, ec); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1191 | } |
| 1192 | |
njn | 6799325 | 2004-11-22 18:02:32 +0000 | [diff] [blame] | 1193 | void VG_(tool_panic) ( Char* str ) |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1194 | { |
thughes | 5876d55 | 2004-09-26 18:44:06 +0000 | [diff] [blame] | 1195 | panic(VG_(details).name, VG_(details).bug_reports_to, str, NULL); |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1196 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1197 | |
njn | 13f0293 | 2003-04-30 20:23:58 +0000 | [diff] [blame] | 1198 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1199 | /* --------------------------------------------------------------------- |
| 1200 | Primitive support for reading files. |
| 1201 | ------------------------------------------------------------------ */ |
| 1202 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1203 | static inline Bool fd_exists(Int fd) |
| 1204 | { |
| 1205 | struct vki_stat st; |
| 1206 | |
| 1207 | return VG_(fstat)(fd, &st) == 0; |
| 1208 | } |
| 1209 | |
| 1210 | /* Move an fd into the Valgrind-safe range */ |
| 1211 | Int VG_(safe_fd)(Int oldfd) |
| 1212 | { |
| 1213 | Int newfd; |
| 1214 | |
thughes | ad1c956 | 2004-06-26 11:27:52 +0000 | [diff] [blame] | 1215 | vg_assert(VG_(fd_hard_limit) != -1); |
fitzhardinge | b791a19 | 2003-12-18 07:22:44 +0000 | [diff] [blame] | 1216 | |
thughes | ad1c956 | 2004-06-26 11:27:52 +0000 | [diff] [blame] | 1217 | newfd = VG_(fcntl)(oldfd, VKI_F_DUPFD, VG_(fd_hard_limit)); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1218 | if (newfd != -1) |
| 1219 | VG_(close)(oldfd); |
| 1220 | |
| 1221 | VG_(fcntl)(newfd, VKI_F_SETFD, VKI_FD_CLOEXEC); |
| 1222 | |
thughes | ad1c956 | 2004-06-26 11:27:52 +0000 | [diff] [blame] | 1223 | vg_assert(newfd >= VG_(fd_hard_limit)); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1224 | return newfd; |
| 1225 | } |
| 1226 | |
| 1227 | |
| 1228 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1229 | /* Returns -1 on failure. */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1230 | Int VG_(open) ( const Char* pathname, Int flags, Int mode ) |
| 1231 | { |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1232 | Int fd; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1233 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1234 | /* (old comment, not sure if it still applies NJN 2002-sep-09) */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1235 | /* This gets a segmentation fault if pathname isn't a valid file. |
| 1236 | I don't know why. It seems like the call to open is getting |
| 1237 | intercepted and messed with by glibc ... */ |
| 1238 | /* fd = open( pathname, O_RDONLY ); */ |
| 1239 | /* ... so we go direct to the horse's mouth, which seems to work |
| 1240 | ok: */ |
nethercote | cf51cc4 | 2004-11-04 17:02:42 +0000 | [diff] [blame] | 1241 | fd = VG_(do_syscall)(__NR_open, (UWord)pathname, flags, mode); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1242 | /* VG_(printf)("result = %d\n", fd); */ |
jsgf | f3c3f1a | 2003-10-14 22:13:28 +0000 | [diff] [blame] | 1243 | /* return -ve error code */ |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1244 | return fd; |
| 1245 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1246 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1247 | Int VG_(pipe) ( Int fd[2] ) |
| 1248 | { |
| 1249 | Int ret = VG_(do_syscall)(__NR_pipe, fd); |
| 1250 | return VG_(is_kerror)(ret) ? -1 : 0; |
| 1251 | } |
| 1252 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1253 | void VG_(close) ( Int fd ) |
| 1254 | { |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1255 | VG_(do_syscall)(__NR_close, fd); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1256 | } |
| 1257 | |
| 1258 | |
| 1259 | Int VG_(read) ( Int fd, void* buf, Int count) |
| 1260 | { |
| 1261 | Int res; |
| 1262 | /* res = read( fd, buf, count ); */ |
nethercote | cf51cc4 | 2004-11-04 17:02:42 +0000 | [diff] [blame] | 1263 | res = VG_(do_syscall)(__NR_read, fd, (UWord)buf, count); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1264 | /* return -ERRNO on error */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1265 | return res; |
| 1266 | } |
| 1267 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1268 | Int VG_(write) ( Int fd, const void* buf, Int count) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1269 | { |
| 1270 | Int res; |
| 1271 | /* res = write( fd, buf, count ); */ |
nethercote | cf51cc4 | 2004-11-04 17:02:42 +0000 | [diff] [blame] | 1272 | res = VG_(do_syscall)(__NR_write, fd, (UWord)buf, count); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1273 | /* return -ERRNO on error */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1274 | return res; |
| 1275 | } |
| 1276 | |
nethercote | 5b9fafd | 2004-11-04 18:39:22 +0000 | [diff] [blame] | 1277 | OffT VG_(lseek) ( Int fd, OffT offset, Int whence) |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 1278 | { |
| 1279 | Int res; |
| 1280 | /* res = lseek( fd, offset, whence ); */ |
nethercote | 5b9fafd | 2004-11-04 18:39:22 +0000 | [diff] [blame] | 1281 | res = VG_(do_syscall)(__NR_lseek, fd, offset, whence); |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 1282 | if (VG_(is_kerror)(res)) res = -1; |
| 1283 | return res; |
| 1284 | } |
| 1285 | |
sewardj | b358620 | 2002-05-09 17:38:13 +0000 | [diff] [blame] | 1286 | Int VG_(stat) ( Char* file_name, struct vki_stat* buf ) |
| 1287 | { |
| 1288 | Int res; |
nethercote | cf51cc4 | 2004-11-04 17:02:42 +0000 | [diff] [blame] | 1289 | res = VG_(do_syscall)(__NR_stat, (UWord)file_name, (UWord)buf); |
fitzhardinge | e1c06d8 | 2003-10-30 07:21:44 +0000 | [diff] [blame] | 1290 | return res; /* return -ve error */ |
njn | 4155712 | 2002-10-14 09:25:37 +0000 | [diff] [blame] | 1291 | } |
| 1292 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1293 | Int VG_(fstat) ( Int fd, struct vki_stat* buf ) |
| 1294 | { |
| 1295 | Int res; |
nethercote | cf51cc4 | 2004-11-04 17:02:42 +0000 | [diff] [blame] | 1296 | res = VG_(do_syscall)(__NR_fstat, fd, (UWord)buf); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1297 | return VG_(is_kerror)(res) ? (-1) : 0; |
| 1298 | } |
| 1299 | |
| 1300 | Int VG_(dup2) ( Int oldfd, Int newfd ) |
| 1301 | { |
| 1302 | Int res; |
nethercote | cf51cc4 | 2004-11-04 17:02:42 +0000 | [diff] [blame] | 1303 | res = VG_(do_syscall)(__NR_dup2, oldfd, newfd); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1304 | return VG_(is_kerror)(res) ? (-1) : res; |
| 1305 | } |
| 1306 | |
njn | 4155712 | 2002-10-14 09:25:37 +0000 | [diff] [blame] | 1307 | Int VG_(rename) ( Char* old_name, Char* new_name ) |
| 1308 | { |
| 1309 | Int res; |
nethercote | cf51cc4 | 2004-11-04 17:02:42 +0000 | [diff] [blame] | 1310 | res = VG_(do_syscall)(__NR_rename, (UWord)old_name, (UWord)new_name); |
njn | 4155712 | 2002-10-14 09:25:37 +0000 | [diff] [blame] | 1311 | return VG_(is_kerror)(res) ? (-1) : 0; |
sewardj | b358620 | 2002-05-09 17:38:13 +0000 | [diff] [blame] | 1312 | } |
| 1313 | |
njn | 4aca2d2 | 2002-10-04 10:29:38 +0000 | [diff] [blame] | 1314 | Int VG_(unlink) ( Char* file_name ) |
| 1315 | { |
| 1316 | Int res; |
nethercote | cf51cc4 | 2004-11-04 17:02:42 +0000 | [diff] [blame] | 1317 | res = VG_(do_syscall)(__NR_unlink, (UWord)file_name); |
njn | 4155712 | 2002-10-14 09:25:37 +0000 | [diff] [blame] | 1318 | return VG_(is_kerror)(res) ? (-1) : 0; |
njn | 4aca2d2 | 2002-10-04 10:29:38 +0000 | [diff] [blame] | 1319 | } |
| 1320 | |
njn | 13f0293 | 2003-04-30 20:23:58 +0000 | [diff] [blame] | 1321 | /* Nb: we do not allow the Linux extension which malloc()s memory for the |
| 1322 | buffer if buf==NULL, because we don't want Linux calling malloc() */ |
nethercote | 928a5f7 | 2004-11-03 18:10:37 +0000 | [diff] [blame] | 1323 | Char* VG_(getcwd) ( Char* buf, SizeT size ) |
njn | 13f0293 | 2003-04-30 20:23:58 +0000 | [diff] [blame] | 1324 | { |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame^] | 1325 | Word res; |
njn | 13f0293 | 2003-04-30 20:23:58 +0000 | [diff] [blame] | 1326 | vg_assert(buf != NULL); |
nethercote | cf51cc4 | 2004-11-04 17:02:42 +0000 | [diff] [blame] | 1327 | res = VG_(do_syscall)(__NR_getcwd, (UWord)buf, size); |
njn | 13f0293 | 2003-04-30 20:23:58 +0000 | [diff] [blame] | 1328 | return VG_(is_kerror)(res) ? ((Char*)NULL) : (Char*)res; |
| 1329 | } |
| 1330 | |
njn | 99ccf08 | 2003-09-30 13:51:23 +0000 | [diff] [blame] | 1331 | /* Alternative version that does allocate the memory. Easier to use. */ |
| 1332 | Bool VG_(getcwd_alloc) ( Char** out ) |
| 1333 | { |
nethercote | 928a5f7 | 2004-11-03 18:10:37 +0000 | [diff] [blame] | 1334 | SizeT size = 4; |
njn | 99ccf08 | 2003-09-30 13:51:23 +0000 | [diff] [blame] | 1335 | |
| 1336 | *out = NULL; |
| 1337 | while (True) { |
| 1338 | *out = VG_(malloc)(size); |
| 1339 | if (NULL == VG_(getcwd)(*out, size)) { |
| 1340 | VG_(free)(*out); |
| 1341 | if (size > 65535) |
| 1342 | return False; |
| 1343 | size *= 2; |
| 1344 | } else { |
| 1345 | return True; |
| 1346 | } |
| 1347 | } |
| 1348 | } |
| 1349 | |
njn | 13f0293 | 2003-04-30 20:23:58 +0000 | [diff] [blame] | 1350 | |
| 1351 | /* --------------------------------------------------------------------- |
| 1352 | Misc functions looking for a proper home. |
| 1353 | ------------------------------------------------------------------ */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1354 | |
nethercote | ff9721d | 2004-01-26 17:10:01 +0000 | [diff] [blame] | 1355 | /* clone the environment */ |
nethercote | 85cdd34 | 2004-08-01 22:36:40 +0000 | [diff] [blame] | 1356 | static Char **env_clone ( Char **oldenv ) |
nethercote | ff9721d | 2004-01-26 17:10:01 +0000 | [diff] [blame] | 1357 | { |
| 1358 | Char **oldenvp; |
| 1359 | Char **newenvp; |
| 1360 | Char **newenv; |
| 1361 | Int envlen; |
| 1362 | |
| 1363 | for (oldenvp = oldenv; oldenvp && *oldenvp; oldenvp++); |
| 1364 | |
| 1365 | envlen = oldenvp - oldenv + 1; |
| 1366 | |
| 1367 | newenv = VG_(arena_malloc)(VG_AR_CORE, envlen * sizeof(Char **)); |
| 1368 | |
| 1369 | oldenvp = oldenv; |
| 1370 | newenvp = newenv; |
| 1371 | |
| 1372 | while (oldenvp && *oldenvp) { |
| 1373 | *newenvp++ = *oldenvp++; |
| 1374 | } |
| 1375 | |
| 1376 | *newenvp = *oldenvp; |
| 1377 | |
| 1378 | return newenv; |
| 1379 | } |
| 1380 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1381 | void VG_(env_unsetenv) ( Char **env, const Char *varname ) |
| 1382 | { |
| 1383 | Char **from; |
| 1384 | Char **to = NULL; |
| 1385 | Int len = VG_(strlen)(varname); |
| 1386 | |
| 1387 | for(from = to = env; from && *from; from++) { |
| 1388 | if (!(VG_(strncmp)(varname, *from, len) == 0 && (*from)[len] == '=')) { |
| 1389 | *to = *from; |
| 1390 | to++; |
| 1391 | } |
| 1392 | } |
| 1393 | *to = *from; |
| 1394 | } |
| 1395 | |
| 1396 | /* set the environment; returns the old env if a new one was allocated */ |
| 1397 | Char **VG_(env_setenv) ( Char ***envp, const Char* varname, const Char *val ) |
| 1398 | { |
| 1399 | Char **env = (*envp); |
| 1400 | Char **cpp; |
| 1401 | Int len = VG_(strlen)(varname); |
| 1402 | Char *valstr = VG_(arena_malloc)(VG_AR_CORE, len + VG_(strlen)(val) + 2); |
| 1403 | Char **oldenv = NULL; |
| 1404 | |
| 1405 | VG_(sprintf)(valstr, "%s=%s", varname, val); |
| 1406 | |
| 1407 | for(cpp = env; cpp && *cpp; cpp++) { |
| 1408 | if (VG_(strncmp)(varname, *cpp, len) == 0 && (*cpp)[len] == '=') { |
| 1409 | *cpp = valstr; |
| 1410 | return oldenv; |
| 1411 | } |
| 1412 | } |
| 1413 | |
| 1414 | if (env == NULL) { |
| 1415 | env = VG_(arena_malloc)(VG_AR_CORE, sizeof(Char **) * 2); |
| 1416 | env[0] = valstr; |
| 1417 | env[1] = NULL; |
| 1418 | |
| 1419 | *envp = env; |
| 1420 | |
| 1421 | } else { |
| 1422 | Int envlen = (cpp-env) + 2; |
| 1423 | Char **newenv = VG_(arena_malloc)(VG_AR_CORE, envlen * sizeof(Char **)); |
| 1424 | |
| 1425 | for(cpp = newenv; *env; ) |
| 1426 | *cpp++ = *env++; |
| 1427 | *cpp++ = valstr; |
| 1428 | *cpp++ = NULL; |
| 1429 | |
| 1430 | oldenv = *envp; |
| 1431 | |
| 1432 | *envp = newenv; |
| 1433 | } |
| 1434 | |
| 1435 | return oldenv; |
| 1436 | } |
| 1437 | |
nethercote | 85cdd34 | 2004-08-01 22:36:40 +0000 | [diff] [blame] | 1438 | /* We do getenv without libc's help by snooping around in |
| 1439 | VG_(client_envp) as determined at startup time. */ |
| 1440 | Char *VG_(getenv)(Char *varname) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1441 | { |
| 1442 | Int i, n; |
| 1443 | n = VG_(strlen)(varname); |
nethercote | 85cdd34 | 2004-08-01 22:36:40 +0000 | [diff] [blame] | 1444 | for (i = 0; VG_(client_envp)[i] != NULL; i++) { |
| 1445 | Char* s = VG_(client_envp)[i]; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1446 | if (VG_(strncmp)(varname, s, n) == 0 && s[n] == '=') { |
| 1447 | return & s[n+1]; |
| 1448 | } |
| 1449 | } |
| 1450 | return NULL; |
| 1451 | } |
| 1452 | |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 1453 | /* Support for getrlimit. */ |
| 1454 | Int VG_(getrlimit) (Int resource, struct vki_rlimit *rlim) |
| 1455 | { |
| 1456 | Int res; |
| 1457 | /* res = getrlimit( resource, rlim ); */ |
nethercote | cf51cc4 | 2004-11-04 17:02:42 +0000 | [diff] [blame] | 1458 | res = VG_(do_syscall)(__NR_getrlimit, resource, (UWord)rlim); |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 1459 | if(VG_(is_kerror)(res)) res = -1; |
| 1460 | return res; |
| 1461 | } |
| 1462 | |
| 1463 | |
fitzhardinge | f0046f2 | 2003-12-18 02:39:22 +0000 | [diff] [blame] | 1464 | /* Support for setrlimit. */ |
| 1465 | Int VG_(setrlimit) (Int resource, struct vki_rlimit *rlim) |
| 1466 | { |
| 1467 | Int res; |
| 1468 | /* res = setrlimit( resource, rlim ); */ |
nethercote | cf51cc4 | 2004-11-04 17:02:42 +0000 | [diff] [blame] | 1469 | res = VG_(do_syscall)(__NR_setrlimit, resource, (UWord)rlim); |
fitzhardinge | f0046f2 | 2003-12-18 02:39:22 +0000 | [diff] [blame] | 1470 | if(VG_(is_kerror)(res)) res = -1; |
| 1471 | return res; |
| 1472 | } |
| 1473 | |
| 1474 | |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 1475 | /* Support for getdents. */ |
| 1476 | Int VG_(getdents) (UInt fd, struct vki_dirent *dirp, UInt count) |
| 1477 | { |
| 1478 | Int res; |
| 1479 | /* res = getdents( fd, dirp, count ); */ |
nethercote | cf51cc4 | 2004-11-04 17:02:42 +0000 | [diff] [blame] | 1480 | res = VG_(do_syscall)(__NR_getdents, fd, (UWord)dirp, count); |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 1481 | if (VG_(is_kerror)(res)) res = -1; |
| 1482 | return res; |
| 1483 | } |
| 1484 | |
| 1485 | /* Support for a readlink. */ |
| 1486 | Int VG_(readlink) (Char* path, Char* buf, UInt bufsiz) |
| 1487 | { |
| 1488 | Int res; |
| 1489 | /* res = readlink( path, buf, bufsiz ); */ |
nethercote | cf51cc4 | 2004-11-04 17:02:42 +0000 | [diff] [blame] | 1490 | res = VG_(do_syscall)(__NR_readlink, (UWord)path, (UWord)buf, bufsiz); |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 1491 | if (VG_(is_kerror)(res)) res = -1; |
| 1492 | return res; |
| 1493 | } |
| 1494 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1495 | /* You'd be amazed how many places need to know the current pid. */ |
| 1496 | Int VG_(getpid) ( void ) |
| 1497 | { |
| 1498 | Int res; |
| 1499 | /* res = getpid(); */ |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1500 | res = VG_(do_syscall)(__NR_getpid); |
| 1501 | return res; |
| 1502 | } |
| 1503 | |
| 1504 | Int VG_(getpgrp) ( void ) |
| 1505 | { |
| 1506 | Int res; |
| 1507 | /* res = getpgid(); */ |
| 1508 | res = VG_(do_syscall)(__NR_getpgrp); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1509 | return res; |
| 1510 | } |
| 1511 | |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 1512 | Int VG_(getppid) ( void ) |
| 1513 | { |
| 1514 | Int res; |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1515 | res = VG_(do_syscall)(__NR_getppid); |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 1516 | return res; |
| 1517 | } |
| 1518 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1519 | Int VG_(setpgid) ( Int pid, Int pgrp ) |
| 1520 | { |
| 1521 | return VG_(do_syscall)(__NR_setpgid, pid, pgrp); |
| 1522 | } |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 1523 | |
nethercote | 60a96c5 | 2004-08-03 13:08:31 +0000 | [diff] [blame] | 1524 | /* Walk through a colon-separated environment variable, and remove the |
| 1525 | entries which match remove_pattern. It slides everything down over |
| 1526 | the removed entries, and pads the remaining space with '\0'. It |
| 1527 | modifies the entries in place (in the client address space), but it |
| 1528 | shouldn't matter too much, since we only do this just before an |
| 1529 | execve(). |
| 1530 | |
| 1531 | This is also careful to mop up any excess ':'s, since empty strings |
| 1532 | delimited by ':' are considered to be '.' in a path. |
| 1533 | */ |
| 1534 | static void mash_colon_env(Char *varp, const Char *remove_pattern) |
| 1535 | { |
| 1536 | Char *const start = varp; |
| 1537 | Char *entry_start = varp; |
| 1538 | Char *output = varp; |
| 1539 | |
| 1540 | if (varp == NULL) |
| 1541 | return; |
| 1542 | |
| 1543 | while(*varp) { |
| 1544 | if (*varp == ':') { |
| 1545 | Char prev; |
| 1546 | Bool match; |
| 1547 | |
| 1548 | /* This is a bit subtle: we want to match against the entry |
| 1549 | we just copied, because it may have overlapped with |
| 1550 | itself, junking the original. */ |
| 1551 | |
| 1552 | prev = *output; |
| 1553 | *output = '\0'; |
| 1554 | |
| 1555 | match = VG_(string_match)(remove_pattern, entry_start); |
| 1556 | |
| 1557 | *output = prev; |
| 1558 | |
| 1559 | if (match) { |
| 1560 | output = entry_start; |
| 1561 | varp++; /* skip ':' after removed entry */ |
| 1562 | } else |
| 1563 | entry_start = output+1; /* entry starts after ':' */ |
| 1564 | } |
| 1565 | |
| 1566 | *output++ = *varp++; |
| 1567 | } |
| 1568 | |
| 1569 | /* match against the last entry */ |
| 1570 | if (VG_(string_match)(remove_pattern, entry_start)) { |
| 1571 | output = entry_start; |
| 1572 | if (output > start) { |
| 1573 | /* remove trailing ':' */ |
| 1574 | output--; |
| 1575 | vg_assert(*output == ':'); |
| 1576 | } |
| 1577 | } |
| 1578 | |
| 1579 | /* pad out the left-overs with '\0' */ |
| 1580 | while(output < varp) |
| 1581 | *output++ = '\0'; |
| 1582 | } |
| 1583 | |
| 1584 | |
| 1585 | // Removes all the Valgrind-added stuff from the passed environment. Used |
| 1586 | // when starting child processes, so they don't see that added stuff. |
| 1587 | void VG_(env_remove_valgrind_env_stuff)(Char** envp) |
| 1588 | { |
| 1589 | Int i; |
| 1590 | Char* ld_preload_str = NULL; |
| 1591 | Char* ld_library_path_str = NULL; |
| 1592 | Char* buf; |
| 1593 | |
| 1594 | // Find LD_* variables |
| 1595 | for (i = 0; envp[i] != NULL; i++) { |
| 1596 | if (VG_(strncmp)(envp[i], "LD_PRELOAD=", 11) == 0) |
| 1597 | ld_preload_str = &envp[i][11]; |
| 1598 | if (VG_(strncmp)(envp[i], "LD_LIBRARY_PATH=", 16) == 0) |
| 1599 | ld_library_path_str = &envp[i][16]; |
| 1600 | } |
| 1601 | |
| 1602 | buf = VG_(arena_malloc)(VG_AR_CORE, VG_(strlen)(VG_(libdir)) + 20); |
| 1603 | |
| 1604 | // Remove Valgrind-specific entries from LD_*. |
| 1605 | VG_(sprintf)(buf, "%s*/vg_inject.so", VG_(libdir)); |
| 1606 | mash_colon_env(ld_preload_str, buf); |
| 1607 | VG_(sprintf)(buf, "%s*/vgpreload_*.so", VG_(libdir)); |
| 1608 | mash_colon_env(ld_preload_str, buf); |
| 1609 | VG_(sprintf)(buf, "%s*", VG_(libdir)); |
| 1610 | mash_colon_env(ld_library_path_str, buf); |
| 1611 | |
| 1612 | // Remove VALGRIND_CLO variable. |
| 1613 | VG_(env_unsetenv)(envp, VALGRINDCLO); |
| 1614 | |
| 1615 | // XXX if variable becomes empty, remove it completely? |
| 1616 | |
| 1617 | VG_(arena_free)(VG_AR_CORE, buf); |
| 1618 | } |
| 1619 | |
sewardj | e6a2524 | 2002-04-21 22:03:07 +0000 | [diff] [blame] | 1620 | /* Return -1 if error, else 0. NOTE does not indicate return code of |
| 1621 | child! */ |
| 1622 | Int VG_(system) ( Char* cmd ) |
| 1623 | { |
| 1624 | Int pid, res; |
sewardj | e6a2524 | 2002-04-21 22:03:07 +0000 | [diff] [blame] | 1625 | if (cmd == NULL) |
| 1626 | return 1; |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1627 | pid = VG_(do_syscall)(__NR_fork); |
sewardj | e6a2524 | 2002-04-21 22:03:07 +0000 | [diff] [blame] | 1628 | if (VG_(is_kerror)(pid)) |
| 1629 | return -1; |
| 1630 | if (pid == 0) { |
| 1631 | /* child */ |
nethercote | ff9721d | 2004-01-26 17:10:01 +0000 | [diff] [blame] | 1632 | static Char** envp = NULL; |
sewardj | e6a2524 | 2002-04-21 22:03:07 +0000 | [diff] [blame] | 1633 | Char* argv[4]; |
nethercote | ff9721d | 2004-01-26 17:10:01 +0000 | [diff] [blame] | 1634 | |
fitzhardinge | b50068f | 2004-02-24 23:42:55 +0000 | [diff] [blame] | 1635 | /* restore the DATA rlimit for the child */ |
| 1636 | VG_(setrlimit)(VKI_RLIMIT_DATA, &VG_(client_rlimit_data)); |
| 1637 | |
nethercote | 60a96c5 | 2004-08-03 13:08:31 +0000 | [diff] [blame] | 1638 | envp = env_clone(VG_(client_envp)); |
| 1639 | VG_(env_remove_valgrind_env_stuff)( envp ); |
nethercote | ff9721d | 2004-01-26 17:10:01 +0000 | [diff] [blame] | 1640 | |
sewardj | e6a2524 | 2002-04-21 22:03:07 +0000 | [diff] [blame] | 1641 | argv[0] = "/bin/sh"; |
| 1642 | argv[1] = "-c"; |
| 1643 | argv[2] = cmd; |
| 1644 | argv[3] = 0; |
nethercote | ff9721d | 2004-01-26 17:10:01 +0000 | [diff] [blame] | 1645 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1646 | (void)VG_(do_syscall)(__NR_execve, |
nethercote | 50397c2 | 2004-11-04 18:03:06 +0000 | [diff] [blame] | 1647 | (UWord)"/bin/sh", (UWord)argv, (UWord)envp); |
nethercote | ff9721d | 2004-01-26 17:10:01 +0000 | [diff] [blame] | 1648 | |
sewardj | e6a2524 | 2002-04-21 22:03:07 +0000 | [diff] [blame] | 1649 | /* If we're still alive here, execve failed. */ |
fitzhardinge | abab839 | 2004-03-02 21:38:51 +0000 | [diff] [blame] | 1650 | VG_(exit)(1); |
sewardj | e6a2524 | 2002-04-21 22:03:07 +0000 | [diff] [blame] | 1651 | } else { |
| 1652 | /* parent */ |
fitzhardinge | abab839 | 2004-03-02 21:38:51 +0000 | [diff] [blame] | 1653 | res = VG_(waitpid)(pid, NULL, 0); |
sewardj | e6a2524 | 2002-04-21 22:03:07 +0000 | [diff] [blame] | 1654 | if (VG_(is_kerror)(res)) { |
| 1655 | return -1; |
| 1656 | } else { |
fitzhardinge | abab839 | 2004-03-02 21:38:51 +0000 | [diff] [blame] | 1657 | return 0; |
sewardj | e6a2524 | 2002-04-21 22:03:07 +0000 | [diff] [blame] | 1658 | } |
| 1659 | } |
| 1660 | } |
| 1661 | |
| 1662 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1663 | /* --------------------------------------------------------------------- |
fitzhardinge | 6687169 | 2004-01-25 03:32:58 +0000 | [diff] [blame] | 1664 | Support for a millisecond-granularity timer. |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1665 | ------------------------------------------------------------------ */ |
| 1666 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1667 | UInt VG_(read_millisecond_timer) ( void ) |
| 1668 | { |
fitzhardinge | 426f9e6 | 2004-01-25 03:44:18 +0000 | [diff] [blame] | 1669 | static ULong base = 0; |
fitzhardinge | 6687169 | 2004-01-25 03:32:58 +0000 | [diff] [blame] | 1670 | struct vki_timeval tv_now; |
| 1671 | ULong now; |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1672 | Int res; |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1673 | |
nethercote | fbd6ffd | 2004-11-04 17:10:24 +0000 | [diff] [blame] | 1674 | res = VG_(do_syscall)(__NR_gettimeofday, (UWord)&tv_now, (UWord)NULL); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1675 | |
fitzhardinge | 6687169 | 2004-01-25 03:32:58 +0000 | [diff] [blame] | 1676 | now = tv_now.tv_sec * 1000000ULL + tv_now.tv_usec; |
| 1677 | |
| 1678 | if (base == 0) |
| 1679 | base = now; |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1680 | |
fitzhardinge | 6687169 | 2004-01-25 03:32:58 +0000 | [diff] [blame] | 1681 | return (now - base) / 1000; |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1682 | } |
| 1683 | |
| 1684 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1685 | /* --------------------------------------------------------------------- |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1686 | Primitive support for bagging memory via mmap. |
| 1687 | ------------------------------------------------------------------ */ |
| 1688 | |
nethercote | 8b5f40c | 2004-11-02 13:29:50 +0000 | [diff] [blame] | 1689 | void* VG_(get_memory_from_mmap) ( SizeT nBytes, Char* who ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1690 | { |
nethercote | 8b5f40c | 2004-11-02 13:29:50 +0000 | [diff] [blame] | 1691 | static SizeT tot_alloc = 0; |
sewardj | 7c2020b | 2002-09-30 22:56:03 +0000 | [diff] [blame] | 1692 | void* p; |
nethercote | c314eba | 2004-07-15 12:59:41 +0000 | [diff] [blame] | 1693 | p = VG_(mmap)(0, nBytes, |
| 1694 | VKI_PROT_READ|VKI_PROT_WRITE|VKI_PROT_EXEC, |
| 1695 | VKI_MAP_PRIVATE|VKI_MAP_ANONYMOUS, 0, -1, 0); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1696 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1697 | if (p != ((void*)(-1))) { |
nethercote | fb583e2 | 2004-09-07 23:15:37 +0000 | [diff] [blame] | 1698 | vg_assert((void*)VG_(valgrind_base) <= p && p <= (void*)VG_(valgrind_last)); |
nethercote | 50397c2 | 2004-11-04 18:03:06 +0000 | [diff] [blame] | 1699 | tot_alloc += nBytes; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1700 | if (0) |
sewardj | e904795 | 2002-06-05 20:28:33 +0000 | [diff] [blame] | 1701 | VG_(printf)( |
nethercote | 8b5f40c | 2004-11-02 13:29:50 +0000 | [diff] [blame] | 1702 | "get_memory_from_mmap: %llu tot, %llu req = %p .. %p, caller %s\n", |
| 1703 | (ULong)tot_alloc, (ULong)nBytes, p, ((char*)p) + nBytes - 1, who ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1704 | return p; |
| 1705 | } |
sewardj | 7c2020b | 2002-09-30 22:56:03 +0000 | [diff] [blame] | 1706 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1707 | VG_(printf)("\n"); |
nethercote | 8b5f40c | 2004-11-02 13:29:50 +0000 | [diff] [blame] | 1708 | VG_(printf)("VG_(get_memory_from_mmap): %s's request for %llu bytes failed.\n", |
| 1709 | who, (ULong)nBytes); |
| 1710 | VG_(printf)("VG_(get_memory_from_mmap): %llu bytes already allocated.\n", |
| 1711 | (ULong)tot_alloc); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1712 | VG_(printf)("\n"); |
nethercote | e757d6e | 2004-07-10 16:17:52 +0000 | [diff] [blame] | 1713 | VG_(printf)("Sorry. You could try using a tool that uses less memory;\n"); |
| 1714 | VG_(printf)("eg. addrcheck instead of memcheck.\n"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1715 | VG_(printf)("\n"); |
| 1716 | VG_(exit)(1); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1717 | } |
| 1718 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1719 | /* --------------------------------------------------------------------- |
| 1720 | Generally useful... |
| 1721 | ------------------------------------------------------------------ */ |
| 1722 | |
| 1723 | Int VG_(log2) ( Int x ) |
| 1724 | { |
| 1725 | Int i; |
| 1726 | /* Any more than 32 and we overflow anyway... */ |
| 1727 | for (i = 0; i < 32; i++) { |
| 1728 | if (1 << i == x) return i; |
| 1729 | } |
| 1730 | return -1; |
| 1731 | } |
| 1732 | |
| 1733 | |
njn | d3b0c5f | 2003-09-30 14:43:54 +0000 | [diff] [blame] | 1734 | // Generic shell sort. Like stdlib.h's qsort(). |
nethercote | 928a5f7 | 2004-11-03 18:10:37 +0000 | [diff] [blame] | 1735 | void VG_(ssort)( void* base, SizeT nmemb, SizeT size, |
njn | d3b0c5f | 2003-09-30 14:43:54 +0000 | [diff] [blame] | 1736 | Int (*compar)(void*, void*) ) |
| 1737 | { |
| 1738 | Int incs[14] = { 1, 4, 13, 40, 121, 364, 1093, 3280, |
| 1739 | 9841, 29524, 88573, 265720, |
| 1740 | 797161, 2391484 }; |
| 1741 | Int lo = 0; |
| 1742 | Int hi = nmemb-1; |
| 1743 | Int i, j, h, bigN, hp; |
| 1744 | |
| 1745 | bigN = hi - lo + 1; if (bigN < 2) return; |
| 1746 | hp = 0; while (hp < 14 && incs[hp] < bigN) hp++; hp--; |
| 1747 | vg_assert(0 <= hp && hp < 14); |
| 1748 | |
| 1749 | #define SORT \ |
| 1750 | for ( ; hp >= 0; hp--) { \ |
| 1751 | h = incs[hp]; \ |
njn | 92c5436 | 2003-10-15 14:00:35 +0000 | [diff] [blame] | 1752 | for (i = lo + h; i <= hi; i++) { \ |
njn | d3b0c5f | 2003-09-30 14:43:54 +0000 | [diff] [blame] | 1753 | ASSIGN(v,0, a,i); \ |
| 1754 | j = i; \ |
njn | 92c5436 | 2003-10-15 14:00:35 +0000 | [diff] [blame] | 1755 | while (COMPAR(a,(j-h), v,0) > 0) { \ |
njn | d3b0c5f | 2003-09-30 14:43:54 +0000 | [diff] [blame] | 1756 | ASSIGN(a,j, a,(j-h)); \ |
| 1757 | j = j - h; \ |
| 1758 | if (j <= (lo + h - 1)) break; \ |
| 1759 | } \ |
| 1760 | ASSIGN(a,j, v,0); \ |
njn | d3b0c5f | 2003-09-30 14:43:54 +0000 | [diff] [blame] | 1761 | } \ |
| 1762 | } |
| 1763 | |
| 1764 | // Specialised cases |
nethercote | 928a5f7 | 2004-11-03 18:10:37 +0000 | [diff] [blame] | 1765 | if (sizeof(ULong) == size) { |
njn | d3b0c5f | 2003-09-30 14:43:54 +0000 | [diff] [blame] | 1766 | |
| 1767 | #define ASSIGN(dst, dsti, src, srci) \ |
| 1768 | (dst)[(dsti)] = (src)[(srci)]; |
| 1769 | |
| 1770 | #define COMPAR(dst, dsti, src, srci) \ |
| 1771 | compar( (void*)(& (dst)[(dsti)]), (void*)(& (src)[(srci)]) ) |
| 1772 | |
nethercote | 928a5f7 | 2004-11-03 18:10:37 +0000 | [diff] [blame] | 1773 | ULong* a = (ULong*)base; |
| 1774 | ULong v[1]; |
| 1775 | |
| 1776 | SORT; |
| 1777 | |
| 1778 | } else if (sizeof(UInt) == size) { |
| 1779 | |
njn | d3b0c5f | 2003-09-30 14:43:54 +0000 | [diff] [blame] | 1780 | UInt* a = (UInt*)base; |
| 1781 | UInt v[1]; |
| 1782 | |
| 1783 | SORT; |
| 1784 | |
| 1785 | } else if (sizeof(UShort) == size) { |
| 1786 | UShort* a = (UShort*)base; |
| 1787 | UShort v[1]; |
| 1788 | |
| 1789 | SORT; |
| 1790 | |
| 1791 | } else if (sizeof(UChar) == size) { |
| 1792 | UChar* a = (UChar*)base; |
| 1793 | UChar v[1]; |
| 1794 | |
| 1795 | SORT; |
| 1796 | |
| 1797 | #undef ASSIGN |
| 1798 | #undef COMPAR |
| 1799 | |
| 1800 | // General case |
| 1801 | } else { |
njn | 92c5436 | 2003-10-15 14:00:35 +0000 | [diff] [blame] | 1802 | char* a = base; |
| 1803 | char v[size]; // will be at least 'size' bytes |
njn | d3b0c5f | 2003-09-30 14:43:54 +0000 | [diff] [blame] | 1804 | |
| 1805 | #define ASSIGN(dst, dsti, src, srci) \ |
njn | 92c5436 | 2003-10-15 14:00:35 +0000 | [diff] [blame] | 1806 | VG_(memcpy)( &dst[size*(dsti)], &src[size*(srci)], size ); |
njn | d3b0c5f | 2003-09-30 14:43:54 +0000 | [diff] [blame] | 1807 | |
| 1808 | #define COMPAR(dst, dsti, src, srci) \ |
njn | 92c5436 | 2003-10-15 14:00:35 +0000 | [diff] [blame] | 1809 | compar( &dst[size*(dsti)], &src[size*(srci)] ) |
njn | d3b0c5f | 2003-09-30 14:43:54 +0000 | [diff] [blame] | 1810 | |
| 1811 | SORT; |
| 1812 | |
| 1813 | #undef ASSIGN |
| 1814 | #undef COMPAR |
| 1815 | } |
| 1816 | #undef SORT |
| 1817 | } |
| 1818 | |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 1819 | /* --------------------------------------------------------------------- |
| 1820 | Gruesome hackery for connecting to a logging server over the network. |
| 1821 | This is all very Linux-kernel specific. |
| 1822 | ------------------------------------------------------------------ */ |
| 1823 | |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 1824 | static |
| 1825 | Int parse_inet_addr_and_port ( UChar* str, UInt* ip_addr, UShort* port ); |
| 1826 | |
| 1827 | static |
| 1828 | Int my_socket ( Int domain, Int type, Int protocol ); |
| 1829 | |
| 1830 | static |
| 1831 | Int my_connect ( Int sockfd, struct vki_sockaddr_in* serv_addr, |
| 1832 | Int addrlen ); |
| 1833 | |
| 1834 | static |
| 1835 | UInt my_htonl ( UInt x ) |
| 1836 | { |
| 1837 | return |
| 1838 | (((x >> 24) & 0xFF) << 0) | (((x >> 16) & 0xFF) << 8) |
| 1839 | | (((x >> 8) & 0xFF) << 16) | (((x >> 0) & 0xFF) << 24); |
| 1840 | } |
| 1841 | |
| 1842 | static |
| 1843 | UShort my_htons ( UShort x ) |
| 1844 | { |
| 1845 | return |
| 1846 | (((x >> 8) & 0xFF) << 0) | (((x >> 0) & 0xFF) << 8); |
| 1847 | } |
| 1848 | |
| 1849 | |
| 1850 | /* The main function. |
| 1851 | |
| 1852 | Supplied string contains either an ip address "192.168.0.1" or |
| 1853 | an ip address and port pair, "192.168.0.1:1500". Parse these, |
| 1854 | and return: |
| 1855 | -1 if there is a parse error |
| 1856 | -2 if no parse error, but specified host:port cannot be opened |
| 1857 | the relevant file (socket) descriptor, otherwise. |
sewardj | 4f094a7 | 2002-11-05 23:37:35 +0000 | [diff] [blame] | 1858 | is used. |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 1859 | */ |
| 1860 | Int VG_(connect_via_socket)( UChar* str ) |
| 1861 | { |
| 1862 | Int sd, res; |
| 1863 | struct vki_sockaddr_in servAddr; |
| 1864 | UInt ip = 0; |
sewardj | 4f094a7 | 2002-11-05 23:37:35 +0000 | [diff] [blame] | 1865 | UShort port = VG_CLO_DEFAULT_LOGPORT; |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 1866 | Bool ok = parse_inet_addr_and_port(str, &ip, &port); |
| 1867 | if (!ok) |
| 1868 | return -1; |
| 1869 | |
| 1870 | if (0) |
| 1871 | VG_(printf)("ip = %d.%d.%d.%d, port %d\n", |
| 1872 | (ip >> 24) & 0xFF, (ip >> 16) & 0xFF, |
| 1873 | (ip >> 8) & 0xFF, ip & 0xFF, |
| 1874 | (UInt)port ); |
| 1875 | |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 1876 | servAddr.sin_family = VKI_AF_INET; |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 1877 | servAddr.sin_addr.s_addr = my_htonl(ip); |
| 1878 | servAddr.sin_port = my_htons(port); |
| 1879 | |
| 1880 | /* create socket */ |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 1881 | sd = my_socket(VKI_AF_INET, VKI_SOCK_STREAM, 0 /* IPPROTO_IP ? */); |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 1882 | if (sd < 0) { |
| 1883 | /* this shouldn't happen ... nevertheless */ |
| 1884 | return -2; |
| 1885 | } |
| 1886 | |
| 1887 | /* connect to server */ |
| 1888 | res = my_connect(sd, (struct vki_sockaddr_in *) &servAddr, |
| 1889 | sizeof(servAddr)); |
| 1890 | if (res < 0) { |
| 1891 | /* connection failed */ |
| 1892 | return -2; |
| 1893 | } |
| 1894 | |
| 1895 | return sd; |
| 1896 | } |
| 1897 | |
| 1898 | |
| 1899 | /* Let d = one or more digits. Accept either: |
| 1900 | d.d.d.d or d.d.d.d:d |
| 1901 | */ |
| 1902 | Int parse_inet_addr_and_port ( UChar* str, UInt* ip_addr, UShort* port ) |
| 1903 | { |
| 1904 | # define GET_CH ((*str) ? (*str++) : 0) |
| 1905 | UInt ipa, i, j, c, any; |
| 1906 | ipa = 0; |
| 1907 | for (i = 0; i < 4; i++) { |
| 1908 | j = 0; |
| 1909 | any = 0; |
| 1910 | while (1) { |
| 1911 | c = GET_CH; |
| 1912 | if (c < '0' || c > '9') break; |
| 1913 | j = 10 * j + (int)(c - '0'); |
| 1914 | any = 1; |
| 1915 | } |
| 1916 | if (any == 0 || j > 255) goto syntaxerr; |
| 1917 | ipa = (ipa << 8) + j; |
| 1918 | if (i <= 2 && c != '.') goto syntaxerr; |
| 1919 | } |
| 1920 | if (c == 0 || c == ':') |
| 1921 | *ip_addr = ipa; |
| 1922 | if (c == 0) goto ok; |
| 1923 | if (c != ':') goto syntaxerr; |
| 1924 | j = 0; |
| 1925 | any = 0; |
| 1926 | while (1) { |
| 1927 | c = GET_CH; |
| 1928 | if (c < '0' || c > '9') break; |
| 1929 | j = j * 10 + (int)(c - '0'); |
| 1930 | any = 1; |
| 1931 | if (j > 65535) goto syntaxerr; |
| 1932 | } |
| 1933 | if (any == 0 || c != 0) goto syntaxerr; |
sewardj | d222067 | 2002-11-13 19:41:41 +0000 | [diff] [blame] | 1934 | if (j < 1024) goto syntaxerr; |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 1935 | *port = (UShort)j; |
| 1936 | ok: |
| 1937 | return 1; |
| 1938 | syntaxerr: |
| 1939 | return 0; |
| 1940 | # undef GET_CH |
| 1941 | } |
| 1942 | |
| 1943 | |
| 1944 | static |
| 1945 | Int my_socket ( Int domain, Int type, Int protocol ) |
| 1946 | { |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame^] | 1947 | // AMD64/Linux doesn't define __NR_socketcall... see comment above |
| 1948 | // VG_(sigpending)() for more details. |
| 1949 | #ifdef __amd64__ |
| 1950 | I_die_here; |
| 1951 | #else |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 1952 | Int res; |
nethercote | bf77968 | 2004-11-04 17:24:57 +0000 | [diff] [blame] | 1953 | UWord args[3]; |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 1954 | args[0] = domain; |
| 1955 | args[1] = type; |
| 1956 | args[2] = protocol; |
nethercote | cf51cc4 | 2004-11-04 17:02:42 +0000 | [diff] [blame] | 1957 | res = VG_(do_syscall)(__NR_socketcall, VKI_SYS_SOCKET, (UWord)&args); |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 1958 | if (VG_(is_kerror)(res)) |
| 1959 | res = -1; |
| 1960 | return res; |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame^] | 1961 | #endif |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 1962 | } |
| 1963 | |
| 1964 | static |
| 1965 | Int my_connect ( Int sockfd, struct vki_sockaddr_in* serv_addr, |
| 1966 | Int addrlen ) |
| 1967 | { |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame^] | 1968 | // AMD64/Linux doesn't define __NR_socketcall... see comment above |
| 1969 | // VG_(sigpending)() for more details. |
| 1970 | #ifdef __amd64__ |
| 1971 | I_die_here; |
| 1972 | #else |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 1973 | Int res; |
nethercote | bf77968 | 2004-11-04 17:24:57 +0000 | [diff] [blame] | 1974 | UWord args[3]; |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 1975 | args[0] = sockfd; |
nethercote | bf77968 | 2004-11-04 17:24:57 +0000 | [diff] [blame] | 1976 | args[1] = (UWord)serv_addr; |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 1977 | args[2] = addrlen; |
nethercote | cf51cc4 | 2004-11-04 17:02:42 +0000 | [diff] [blame] | 1978 | res = VG_(do_syscall)(__NR_socketcall, VKI_SYS_CONNECT, (UWord)&args); |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 1979 | if (VG_(is_kerror)(res)) |
| 1980 | res = -1; |
| 1981 | return res; |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame^] | 1982 | #endif |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 1983 | } |
| 1984 | |
| 1985 | Int VG_(write_socket)( Int sd, void *msg, Int count ) |
| 1986 | { |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame^] | 1987 | // AMD64/Linux doesn't define __NR_socketcall... see comment above |
| 1988 | // VG_(sigpending)() for more details. |
| 1989 | #ifdef __amd64__ |
| 1990 | I_die_here; |
| 1991 | #else |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 1992 | /* This is actually send(). */ |
sewardj | 570f890 | 2002-11-03 11:44:36 +0000 | [diff] [blame] | 1993 | |
| 1994 | /* Requests not to send SIGPIPE on errors on stream oriented |
| 1995 | sockets when the other end breaks the connection. The EPIPE |
| 1996 | error is still returned. */ |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 1997 | Int flags = VKI_MSG_NOSIGNAL; |
sewardj | 570f890 | 2002-11-03 11:44:36 +0000 | [diff] [blame] | 1998 | |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 1999 | Int res; |
nethercote | bf77968 | 2004-11-04 17:24:57 +0000 | [diff] [blame] | 2000 | UWord args[4]; |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 2001 | args[0] = sd; |
nethercote | bf77968 | 2004-11-04 17:24:57 +0000 | [diff] [blame] | 2002 | args[1] = (UWord)msg; |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 2003 | args[2] = count; |
| 2004 | args[3] = flags; |
nethercote | cf51cc4 | 2004-11-04 17:02:42 +0000 | [diff] [blame] | 2005 | res = VG_(do_syscall)(__NR_socketcall, VKI_SYS_SEND, (UWord)&args); |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 2006 | if (VG_(is_kerror)(res)) |
| 2007 | res = -1; |
| 2008 | return res; |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame^] | 2009 | #endif |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 2010 | } |
| 2011 | |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 2012 | Int VG_(getsockname) ( Int sd, struct vki_sockaddr *name, Int *namelen) |
| 2013 | { |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame^] | 2014 | // AMD64/Linux doesn't define __NR_socketcall... see comment above |
| 2015 | // VG_(sigpending)() for more details. |
| 2016 | #ifdef __amd64__ |
| 2017 | I_die_here; |
| 2018 | #else |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 2019 | Int res; |
nethercote | bf77968 | 2004-11-04 17:24:57 +0000 | [diff] [blame] | 2020 | UWord args[3]; |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 2021 | args[0] = sd; |
nethercote | bf77968 | 2004-11-04 17:24:57 +0000 | [diff] [blame] | 2022 | args[1] = (UWord)name; |
| 2023 | args[2] = (UWord)namelen; |
nethercote | cf51cc4 | 2004-11-04 17:02:42 +0000 | [diff] [blame] | 2024 | res = VG_(do_syscall)(__NR_socketcall, VKI_SYS_GETSOCKNAME, (UWord)&args); |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 2025 | if(VG_(is_kerror)(res)) |
| 2026 | res = -1; |
| 2027 | return res; |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame^] | 2028 | #endif |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 2029 | } |
| 2030 | |
| 2031 | Int VG_(getpeername) ( Int sd, struct vki_sockaddr *name, Int *namelen) |
| 2032 | { |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame^] | 2033 | // AMD64/Linux doesn't define __NR_socketcall... see comment above |
| 2034 | // VG_(sigpending)() for more details. |
| 2035 | #ifdef __amd64__ |
| 2036 | I_die_here; |
| 2037 | #else |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 2038 | Int res; |
nethercote | bf77968 | 2004-11-04 17:24:57 +0000 | [diff] [blame] | 2039 | UWord args[3]; |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 2040 | args[0] = sd; |
nethercote | bf77968 | 2004-11-04 17:24:57 +0000 | [diff] [blame] | 2041 | args[1] = (UWord)name; |
| 2042 | args[2] = (UWord)namelen; |
nethercote | cf51cc4 | 2004-11-04 17:02:42 +0000 | [diff] [blame] | 2043 | res = VG_(do_syscall)(__NR_socketcall, VKI_SYS_GETPEERNAME, (UWord)&args); |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 2044 | if(VG_(is_kerror)(res)) |
| 2045 | res = -1; |
| 2046 | return res; |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame^] | 2047 | #endif |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 2048 | } |
| 2049 | |
| 2050 | Int VG_(getsockopt) ( Int sd, Int level, Int optname, void *optval, |
| 2051 | Int *optlen) |
| 2052 | { |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame^] | 2053 | // AMD64/Linux doesn't define __NR_socketcall... see comment above |
| 2054 | // VG_(sigpending)() for more details. |
| 2055 | #ifdef __amd64__ |
| 2056 | I_die_here; |
| 2057 | #else |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 2058 | Int res; |
nethercote | bf77968 | 2004-11-04 17:24:57 +0000 | [diff] [blame] | 2059 | UWord args[5]; |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 2060 | args[0] = sd; |
nethercote | bf77968 | 2004-11-04 17:24:57 +0000 | [diff] [blame] | 2061 | args[1] = level; |
| 2062 | args[2] = optname; |
| 2063 | args[3] = (UWord)optval; |
| 2064 | args[4] = (UWord)optlen; |
nethercote | cf51cc4 | 2004-11-04 17:02:42 +0000 | [diff] [blame] | 2065 | res = VG_(do_syscall)(__NR_socketcall, VKI_SYS_GETSOCKOPT, (UWord)&args); |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 2066 | if(VG_(is_kerror)(res)) |
| 2067 | res = -1; |
| 2068 | return res; |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame^] | 2069 | #endif |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 2070 | } |
| 2071 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2072 | /*--------------------------------------------------------------------*/ |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame^] | 2073 | /*--- end ---*/ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2074 | /*--------------------------------------------------------------------*/ |