blob: 3693d91241c345f881c3aa018dce6d748551fc53 [file] [log] [blame]
sewardjde4a1d02002-03-22 01:27:54 +00001
2/*--------------------------------------------------------------------*/
3/*--- Reimplementation of some C library stuff, to avoid depending ---*/
4/*--- on libc.so. ---*/
5/*--- vg_mylibc.c ---*/
6/*--------------------------------------------------------------------*/
7
8/*
njnc9539842002-10-02 13:26:35 +00009 This file is part of Valgrind, an extensible x86 protected-mode
10 emulator for monitoring program execution on x86-Unixes.
sewardjde4a1d02002-03-22 01:27:54 +000011
njn0e1b5142003-04-15 14:58:06 +000012 Copyright (C) 2000-2003 Julian Seward
sewardjde4a1d02002-03-22 01:27:54 +000013 jseward@acm.org
sewardjde4a1d02002-03-22 01:27:54 +000014
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
njn25e49d8e72002-09-23 09:36:25 +000030 The GNU General Public License is contained in the file COPYING.
sewardjde4a1d02002-03-22 01:27:54 +000031*/
32
33#include "vg_include.h"
34
sewardjde4a1d02002-03-22 01:27:54 +000035/* ---------------------------------------------------------------------
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*/
42Int VG_(ksigfillset)( vki_ksigset_t* set )
43{
44 Int i;
45 if (set == NULL)
46 return -1;
47 for (i = 0; i < VKI_KNSIG_WORDS; i++)
48 set->ws[i] = 0xFFFFFFFF;
49 return 0;
50}
51
52Int VG_(ksigemptyset)( vki_ksigset_t* set )
53{
54 Int i;
55 if (set == NULL)
56 return -1;
57 for (i = 0; i < VKI_KNSIG_WORDS; i++)
58 set->ws[i] = 0x0;
59 return 0;
60}
61
sewardjb48e5002002-05-13 00:16:03 +000062Bool VG_(kisemptysigset)( vki_ksigset_t* set )
63{
64 Int i;
65 vg_assert(set != NULL);
66 for (i = 0; i < VKI_KNSIG_WORDS; i++)
67 if (set->ws[i] != 0x0) return False;
68 return True;
69}
70
sewardj018f7622002-05-15 21:13:39 +000071Bool VG_(kisfullsigset)( vki_ksigset_t* set )
72{
73 Int i;
74 vg_assert(set != NULL);
75 for (i = 0; i < VKI_KNSIG_WORDS; i++)
sewardj05bcdcb2003-05-18 10:05:38 +000076 if (set->ws[i] != (UInt)(~0x0)) return False;
sewardj018f7622002-05-15 21:13:39 +000077 return True;
78}
79
80
sewardjde4a1d02002-03-22 01:27:54 +000081Int VG_(ksigaddset)( vki_ksigset_t* set, Int signum )
82{
83 if (set == NULL)
84 return -1;
njn25e49d8e72002-09-23 09:36:25 +000085 if (signum < 1 || signum > VKI_KNSIG)
sewardjde4a1d02002-03-22 01:27:54 +000086 return -1;
87 signum--;
88 set->ws[signum / VKI_KNSIG_BPW] |= (1 << (signum % VKI_KNSIG_BPW));
89 return 0;
90}
91
sewardj018f7622002-05-15 21:13:39 +000092Int VG_(ksigdelset)( vki_ksigset_t* set, Int signum )
93{
94 if (set == NULL)
95 return -1;
njn25e49d8e72002-09-23 09:36:25 +000096 if (signum < 1 || signum > VKI_KNSIG)
sewardj018f7622002-05-15 21:13:39 +000097 return -1;
98 signum--;
99 set->ws[signum / VKI_KNSIG_BPW] &= ~(1 << (signum % VKI_KNSIG_BPW));
100 return 0;
101}
102
sewardjde4a1d02002-03-22 01:27:54 +0000103Int VG_(ksigismember) ( vki_ksigset_t* set, Int signum )
104{
105 if (set == NULL)
sewardjb48e5002002-05-13 00:16:03 +0000106 return 0;
njn25e49d8e72002-09-23 09:36:25 +0000107 if (signum < 1 || signum > VKI_KNSIG)
sewardjb48e5002002-05-13 00:16:03 +0000108 return 0;
sewardjde4a1d02002-03-22 01:27:54 +0000109 signum--;
110 if (1 & ((set->ws[signum / VKI_KNSIG_BPW]) >> (signum % VKI_KNSIG_BPW)))
111 return 1;
112 else
113 return 0;
114}
115
116
sewardjb48e5002002-05-13 00:16:03 +0000117/* Add all signals in src to dst. */
118void VG_(ksigaddset_from_set)( vki_ksigset_t* dst, vki_ksigset_t* src )
119{
120 Int i;
121 vg_assert(dst != NULL && src != NULL);
122 for (i = 0; i < VKI_KNSIG_WORDS; i++)
123 dst->ws[i] |= src->ws[i];
124}
125
126/* Remove all signals in src from dst. */
127void VG_(ksigdelset_from_set)( vki_ksigset_t* dst, vki_ksigset_t* src )
128{
129 Int i;
130 vg_assert(dst != NULL && src != NULL);
131 for (i = 0; i < VKI_KNSIG_WORDS; i++)
132 dst->ws[i] &= ~(src->ws[i]);
133}
134
135
sewardjde4a1d02002-03-22 01:27:54 +0000136/* The functions sigaction, sigprocmask, sigpending and sigsuspend
137 return 0 on success and -1 on error.
138*/
139Int VG_(ksigprocmask)( Int how,
140 const vki_ksigset_t* set,
141 vki_ksigset_t* oldset)
142{
143 Int res
jsgf855d93d2003-10-13 22:26:55 +0000144 = VG_(do_syscall)(__NR_rt_sigprocmask,
145 how, (UInt)set, (UInt)oldset,
146 VKI_KNSIG_WORDS * VKI_BYTES_PER_WORD);
sewardjde4a1d02002-03-22 01:27:54 +0000147 return VG_(is_kerror)(res) ? -1 : 0;
148}
149
150
151Int VG_(ksigaction) ( Int signum,
152 const vki_ksigaction* act,
153 vki_ksigaction* oldact)
154{
155 Int res
jsgf855d93d2003-10-13 22:26:55 +0000156 = VG_(do_syscall)(__NR_rt_sigaction,
157 signum, (UInt)act, (UInt)oldact,
158 VKI_KNSIG_WORDS * VKI_BYTES_PER_WORD);
sewardj018f7622002-05-15 21:13:39 +0000159 /* VG_(printf)("res = %d\n",res); */
sewardjde4a1d02002-03-22 01:27:54 +0000160 return VG_(is_kerror)(res) ? -1 : 0;
161}
162
163
164Int VG_(ksigaltstack)( const vki_kstack_t* ss, vki_kstack_t* oss )
165{
166 Int res
jsgf855d93d2003-10-13 22:26:55 +0000167 = VG_(do_syscall)(__NR_sigaltstack, (UInt)ss, (UInt)oss);
sewardjde4a1d02002-03-22 01:27:54 +0000168 return VG_(is_kerror)(res) ? -1 : 0;
169}
170
jsgf855d93d2003-10-13 22:26:55 +0000171Int VG_(ksigtimedwait)( const vki_ksigset_t *set, vki_ksiginfo_t *info,
172 const struct vki_timespec *timeout )
173{
174 Int res = VG_(do_syscall)(__NR_rt_sigtimedwait, set, info, timeout, sizeof(*set));
175
176 return VG_(is_kerror)(res) ? -1 : res;
177}
sewardjde4a1d02002-03-22 01:27:54 +0000178
179Int VG_(ksignal)(Int signum, void (*sighandler)(Int))
180{
181 Int res;
182 vki_ksigaction sa;
183 sa.ksa_handler = sighandler;
184 sa.ksa_flags = VKI_SA_ONSTACK | VKI_SA_RESTART;
185 sa.ksa_restorer = NULL;
186 res = VG_(ksigemptyset)( &sa.ksa_mask );
187 vg_assert(res == 0);
jsgf855d93d2003-10-13 22:26:55 +0000188 res = VG_(do_syscall)(__NR_rt_sigaction,
189 signum, (UInt)(&sa), (UInt)NULL,
190 VKI_KNSIG_WORDS * VKI_BYTES_PER_WORD);
sewardjde4a1d02002-03-22 01:27:54 +0000191 return VG_(is_kerror)(res) ? -1 : 0;
192}
193
194
sewardjdcaf3122002-09-30 23:12:33 +0000195Int VG_(kkill)( Int pid, Int signo )
sewardj018f7622002-05-15 21:13:39 +0000196{
jsgf855d93d2003-10-13 22:26:55 +0000197 Int res = VG_(do_syscall)(__NR_kill, pid, signo);
sewardj018f7622002-05-15 21:13:39 +0000198 return VG_(is_kerror)(res) ? -1 : 0;
199}
200
201
jsgf855d93d2003-10-13 22:26:55 +0000202Int VG_(ktkill)( Int tid, Int signo )
203{
204 Int ret = -VKI_ENOSYS;
205
206#ifdef __NR_tkill
207 ret = VG_(do_syscall)(__NR_tkill, tid, signo);
208#endif /* __NR_tkill */
209
210 if (ret == -VKI_ENOSYS)
211 ret = VG_(do_syscall)(__NR_kill, tid, signo);
212
213 return VG_(is_kerror)(ret) ? -1 : 0;
214}
215
sewardjdcaf3122002-09-30 23:12:33 +0000216Int VG_(ksigpending) ( vki_ksigset_t* set )
sewardjefbfcdf2002-06-19 17:35:45 +0000217{
jsgf855d93d2003-10-13 22:26:55 +0000218 Int res = VG_(do_syscall)(__NR_sigpending, (UInt)set);
sewardjefbfcdf2002-06-19 17:35:45 +0000219 return VG_(is_kerror)(res) ? -1 : 0;
220}
221
jsgf855d93d2003-10-13 22:26:55 +0000222Int VG_(waitpid)(Int pid, Int *status, Int options)
223{
224 Int ret = VG_(do_syscall)(__NR_wait4, pid, status, options, NULL);
225
226 return VG_(is_kerror)(ret) ? -1 : ret;
227}
228
229Int VG_(gettid)(void)
230{
231 Int ret;
232
233 ret = VG_(do_syscall)(__NR_gettid);
234
235 if (ret == -VKI_ENOSYS)
236 ret = VG_(do_syscall)(__NR_getpid);
237
238 return ret;
239}
240
241
sewardjefbfcdf2002-06-19 17:35:45 +0000242
sewardjde4a1d02002-03-22 01:27:54 +0000243/* ---------------------------------------------------------------------
sewardj2e93c502002-04-12 11:12:52 +0000244 mmap/munmap, exit, fcntl
sewardjde4a1d02002-03-22 01:27:54 +0000245 ------------------------------------------------------------------ */
246
247/* Returns -1 on failure. */
248void* VG_(mmap)( void* start, UInt length,
249 UInt prot, UInt flags, UInt fd, UInt offset)
250{
251 Int res;
252 UInt args[6];
253 args[0] = (UInt)start;
254 args[1] = length;
255 args[2] = prot;
256 args[3] = flags;
257 args[4] = fd;
258 args[5] = offset;
jsgf855d93d2003-10-13 22:26:55 +0000259 res = VG_(do_syscall)(__NR_mmap, (UInt)(&(args[0])) );
sewardjde4a1d02002-03-22 01:27:54 +0000260 return VG_(is_kerror)(res) ? ((void*)(-1)) : (void*)res;
261}
262
263/* Returns -1 on failure. */
264Int VG_(munmap)( void* start, Int length )
265{
jsgf855d93d2003-10-13 22:26:55 +0000266 Int res = VG_(do_syscall)(__NR_munmap, (UInt)start, (UInt)length );
sewardjde4a1d02002-03-22 01:27:54 +0000267 return VG_(is_kerror)(res) ? -1 : 0;
268}
269
270void VG_(exit)( Int status )
271{
jsgf855d93d2003-10-13 22:26:55 +0000272 (void)VG_(do_syscall)(__NR_exit_group, (UInt)status );
273 (void)VG_(do_syscall)(__NR_exit, (UInt)status );
sewardjde4a1d02002-03-22 01:27:54 +0000274 /* Why are we still alive here? */
275 /*NOTREACHED*/
jsgf855d93d2003-10-13 22:26:55 +0000276 *(volatile Int *)0 = 'x';
sewardjde4a1d02002-03-22 01:27:54 +0000277 vg_assert(2+2 == 5);
278}
279
sewardj2e93c502002-04-12 11:12:52 +0000280/* Returns -1 on error. */
281Int VG_(fcntl) ( Int fd, Int cmd, Int arg )
282{
jsgf855d93d2003-10-13 22:26:55 +0000283 Int res = VG_(do_syscall)(__NR_fcntl, fd, cmd, arg);
sewardj2e93c502002-04-12 11:12:52 +0000284 return VG_(is_kerror)(res) ? -1 : res;
285}
286
287/* Returns -1 on error. */
288Int VG_(select)( Int n,
289 vki_fd_set* readfds,
290 vki_fd_set* writefds,
291 vki_fd_set* exceptfds,
292 struct vki_timeval * timeout )
293{
294 Int res;
295 UInt args[5];
296 args[0] = n;
297 args[1] = (UInt)readfds;
298 args[2] = (UInt)writefds;
299 args[3] = (UInt)exceptfds;
300 args[4] = (UInt)timeout;
jsgf855d93d2003-10-13 22:26:55 +0000301 res = VG_(do_syscall)(__NR_select, (UInt)(&(args[0])) );
sewardj2e93c502002-04-12 11:12:52 +0000302 return VG_(is_kerror)(res) ? -1 : res;
sewardj2e93c502002-04-12 11:12:52 +0000303}
304
jsgf855d93d2003-10-13 22:26:55 +0000305Int VG_(poll)( struct vki_pollfd *ufds, UInt nfds, Int timeout)
306{
307 Int res = VG_(do_syscall)(__NR_poll, ufds, nfds, timeout);
308
309 return res;
310}
311
sewardj5f07b662002-04-23 16:52:51 +0000312/* Returns -1 on error, 0 if ok, 1 if interrupted. */
sewardj2e93c502002-04-12 11:12:52 +0000313Int VG_(nanosleep)( const struct vki_timespec *req,
314 struct vki_timespec *rem )
315{
316 Int res;
jsgf855d93d2003-10-13 22:26:55 +0000317 res = VG_(do_syscall)(__NR_nanosleep, (UInt)req, (UInt)rem);
sewardj2e93c502002-04-12 11:12:52 +0000318 if (res == -VKI_EINVAL) return -1;
sewardj5f07b662002-04-23 16:52:51 +0000319 if (res == -VKI_EINTR) return 1;
sewardj2e93c502002-04-12 11:12:52 +0000320 return 0;
321}
322
sewardjb3586202002-05-09 17:38:13 +0000323void* VG_(brk) ( void* end_data_segment )
324{
325 Int res;
jsgf855d93d2003-10-13 22:26:55 +0000326 res = VG_(do_syscall)(__NR_brk, (UInt)end_data_segment);
sewardjb3586202002-05-09 17:38:13 +0000327 return (void*)( VG_(is_kerror)(res) ? -1 : res );
328}
329
sewardj2e93c502002-04-12 11:12:52 +0000330
sewardjde4a1d02002-03-22 01:27:54 +0000331/* ---------------------------------------------------------------------
332 printf implementation. The key function, vg_vprintf(), emits chars
333 into a caller-supplied function. Distantly derived from:
334
335 vprintf replacement for Checker.
336 Copyright 1993, 1994, 1995 Tristan Gingold
337 Written September 1993 Tristan Gingold
338 Tristan Gingold, 8 rue Parmentier, F-91120 PALAISEAU, FRANCE
339
340 (Checker itself was GPL'd.)
341 ------------------------------------------------------------------ */
342
343
344/* Some flags. */
345#define VG_MSG_SIGNED 1 /* The value is signed. */
346#define VG_MSG_ZJUSTIFY 2 /* Must justify with '0'. */
347#define VG_MSG_LJUSTIFY 4 /* Must justify on the left. */
sewardj78e3cd92002-10-22 04:45:48 +0000348#define VG_MSG_PAREN 8 /* Parenthesize if present (for %y) */
njn607adfc2003-09-30 14:15:44 +0000349#define VG_MSG_COMMA 16 /* Add commas to numbers (for %d, %u) */
sewardjde4a1d02002-03-22 01:27:54 +0000350
351/* Copy a string into the buffer. */
sewardj78e3cd92002-10-22 04:45:48 +0000352static UInt
sewardjde4a1d02002-03-22 01:27:54 +0000353myvprintf_str ( void(*send)(Char), Int flags, Int width, Char* str,
354 Bool capitalise )
355{
356# define MAYBE_TOUPPER(ch) (capitalise ? VG_(toupper)(ch) : (ch))
sewardj78e3cd92002-10-22 04:45:48 +0000357 UInt ret = 0;
sewardjde4a1d02002-03-22 01:27:54 +0000358 Int i, extra;
359 Int len = VG_(strlen)(str);
360
361 if (width == 0) {
sewardj78e3cd92002-10-22 04:45:48 +0000362 ret += len;
sewardjde4a1d02002-03-22 01:27:54 +0000363 for (i = 0; i < len; i++)
364 send(MAYBE_TOUPPER(str[i]));
sewardj78e3cd92002-10-22 04:45:48 +0000365 return ret;
sewardjde4a1d02002-03-22 01:27:54 +0000366 }
367
368 if (len > width) {
sewardj78e3cd92002-10-22 04:45:48 +0000369 ret += width;
sewardjde4a1d02002-03-22 01:27:54 +0000370 for (i = 0; i < width; i++)
371 send(MAYBE_TOUPPER(str[i]));
sewardj78e3cd92002-10-22 04:45:48 +0000372 return ret;
sewardjde4a1d02002-03-22 01:27:54 +0000373 }
374
375 extra = width - len;
376 if (flags & VG_MSG_LJUSTIFY) {
sewardj78e3cd92002-10-22 04:45:48 +0000377 ret += extra;
sewardjde4a1d02002-03-22 01:27:54 +0000378 for (i = 0; i < extra; i++)
379 send(' ');
380 }
sewardj78e3cd92002-10-22 04:45:48 +0000381 ret += len;
sewardjde4a1d02002-03-22 01:27:54 +0000382 for (i = 0; i < len; i++)
383 send(MAYBE_TOUPPER(str[i]));
384 if (!(flags & VG_MSG_LJUSTIFY)) {
sewardj78e3cd92002-10-22 04:45:48 +0000385 ret += extra;
sewardjde4a1d02002-03-22 01:27:54 +0000386 for (i = 0; i < extra; i++)
387 send(' ');
388 }
389
390# undef MAYBE_TOUPPER
sewardj78e3cd92002-10-22 04:45:48 +0000391
392 return ret;
sewardjde4a1d02002-03-22 01:27:54 +0000393}
394
395/* Write P into the buffer according to these args:
396 * If SIGN is true, p is a signed.
397 * BASE is the base.
398 * If WITH_ZERO is true, '0' must be added.
399 * WIDTH is the width of the field.
400 */
sewardj78e3cd92002-10-22 04:45:48 +0000401static UInt
sewardjde4a1d02002-03-22 01:27:54 +0000402myvprintf_int64 ( void(*send)(Char), Int flags, Int base, Int width, ULong p)
403{
404 Char buf[40];
405 Int ind = 0;
njn607adfc2003-09-30 14:15:44 +0000406 Int i, nc = 0;
sewardjde4a1d02002-03-22 01:27:54 +0000407 Bool neg = False;
408 Char *digits = "0123456789ABCDEF";
sewardj78e3cd92002-10-22 04:45:48 +0000409 UInt ret = 0;
410
sewardjde4a1d02002-03-22 01:27:54 +0000411 if (base < 2 || base > 16)
sewardj78e3cd92002-10-22 04:45:48 +0000412 return ret;
sewardjde4a1d02002-03-22 01:27:54 +0000413
414 if ((flags & VG_MSG_SIGNED) && (Long)p < 0) {
415 p = - (Long)p;
416 neg = True;
417 }
418
419 if (p == 0)
420 buf[ind++] = '0';
421 else {
422 while (p > 0) {
njn607adfc2003-09-30 14:15:44 +0000423 if (flags & VG_MSG_COMMA && 10 == base &&
424 0 == (ind-nc) % 3 && 0 != ind)
425 {
426 buf[ind++] = ',';
427 nc++;
428 }
sewardjde4a1d02002-03-22 01:27:54 +0000429 buf[ind++] = digits[p % base];
430 p /= base;
njn607adfc2003-09-30 14:15:44 +0000431 }
sewardjde4a1d02002-03-22 01:27:54 +0000432 }
433
434 if (neg)
435 buf[ind++] = '-';
436
437 if (width > 0 && !(flags & VG_MSG_LJUSTIFY)) {
438 for(; ind < width; ind++) {
439 vg_assert(ind < 39);
440 buf[ind] = (flags & VG_MSG_ZJUSTIFY) ? '0': ' ';
441 }
442 }
443
444 /* Reverse copy to buffer. */
sewardj78e3cd92002-10-22 04:45:48 +0000445 ret += ind;
njn607adfc2003-09-30 14:15:44 +0000446 for (i = ind -1; i >= 0; i--) {
sewardjde4a1d02002-03-22 01:27:54 +0000447 send(buf[i]);
njn607adfc2003-09-30 14:15:44 +0000448 }
sewardjde4a1d02002-03-22 01:27:54 +0000449 if (width > 0 && (flags & VG_MSG_LJUSTIFY)) {
sewardj78e3cd92002-10-22 04:45:48 +0000450 for(; ind < width; ind++) {
451 ret++;
njn607adfc2003-09-30 14:15:44 +0000452 send(' '); // Never pad with zeroes on RHS -- changes the value!
sewardj78e3cd92002-10-22 04:45:48 +0000453 }
sewardjde4a1d02002-03-22 01:27:54 +0000454 }
sewardj78e3cd92002-10-22 04:45:48 +0000455 return ret;
sewardjde4a1d02002-03-22 01:27:54 +0000456}
457
458
459/* A simple vprintf(). */
sewardj78e3cd92002-10-22 04:45:48 +0000460UInt
sewardjde4a1d02002-03-22 01:27:54 +0000461VG_(vprintf) ( void(*send)(Char), const Char *format, va_list vargs )
462{
sewardj78e3cd92002-10-22 04:45:48 +0000463 UInt ret = 0;
sewardjde4a1d02002-03-22 01:27:54 +0000464 int i;
465 int flags;
466 int width;
467 Bool is_long;
468
469 /* We assume that vargs has already been initialised by the
470 caller, using va_start, and that the caller will similarly
471 clean up with va_end.
472 */
473
474 for (i = 0; format[i] != 0; i++) {
475 if (format[i] != '%') {
476 send(format[i]);
sewardj78e3cd92002-10-22 04:45:48 +0000477 ret++;
sewardjde4a1d02002-03-22 01:27:54 +0000478 continue;
479 }
480 i++;
481 /* A '%' has been found. Ignore a trailing %. */
482 if (format[i] == 0)
483 break;
484 if (format[i] == '%') {
485 /* `%%' is replaced by `%'. */
486 send('%');
sewardj78e3cd92002-10-22 04:45:48 +0000487 ret++;
sewardjde4a1d02002-03-22 01:27:54 +0000488 continue;
489 }
490 flags = 0;
491 is_long = False;
492 width = 0; /* length of the field. */
sewardj78e3cd92002-10-22 04:45:48 +0000493 if (format[i] == '(') {
494 flags |= VG_MSG_PAREN;
495 i++;
496 }
njn607adfc2003-09-30 14:15:44 +0000497 /* If ',' follows '%', commas will be inserted. */
498 if (format[i] == ',') {
499 flags |= VG_MSG_COMMA;
500 i++;
501 }
sewardjde4a1d02002-03-22 01:27:54 +0000502 /* If '-' follows '%', justify on the left. */
503 if (format[i] == '-') {
504 flags |= VG_MSG_LJUSTIFY;
505 i++;
506 }
507 /* If '0' follows '%', pads will be inserted. */
508 if (format[i] == '0') {
509 flags |= VG_MSG_ZJUSTIFY;
510 i++;
511 }
512 /* Compute the field length. */
513 while (format[i] >= '0' && format[i] <= '9') {
514 width *= 10;
515 width += format[i++] - '0';
516 }
517 while (format[i] == 'l') {
518 i++;
519 is_long = True;
520 }
521
522 switch (format[i]) {
523 case 'd': /* %d */
524 flags |= VG_MSG_SIGNED;
525 if (is_long)
sewardj78e3cd92002-10-22 04:45:48 +0000526 ret += myvprintf_int64(send, flags, 10, width,
527 (ULong)(va_arg (vargs, Long)));
sewardjde4a1d02002-03-22 01:27:54 +0000528 else
sewardj78e3cd92002-10-22 04:45:48 +0000529 ret += myvprintf_int64(send, flags, 10, width,
530 (ULong)(va_arg (vargs, Int)));
sewardjde4a1d02002-03-22 01:27:54 +0000531 break;
532 case 'u': /* %u */
533 if (is_long)
sewardj78e3cd92002-10-22 04:45:48 +0000534 ret += myvprintf_int64(send, flags, 10, width,
535 (ULong)(va_arg (vargs, ULong)));
sewardjde4a1d02002-03-22 01:27:54 +0000536 else
sewardj78e3cd92002-10-22 04:45:48 +0000537 ret += myvprintf_int64(send, flags, 10, width,
538 (ULong)(va_arg (vargs, UInt)));
sewardjde4a1d02002-03-22 01:27:54 +0000539 break;
540 case 'p': /* %p */
sewardj78e3cd92002-10-22 04:45:48 +0000541 ret += 2;
sewardjde4a1d02002-03-22 01:27:54 +0000542 send('0');
543 send('x');
sewardj78e3cd92002-10-22 04:45:48 +0000544 ret += myvprintf_int64(send, flags, 16, width,
545 (ULong)((UInt)va_arg (vargs, void *)));
sewardjde4a1d02002-03-22 01:27:54 +0000546 break;
547 case 'x': /* %x */
548 if (is_long)
sewardj78e3cd92002-10-22 04:45:48 +0000549 ret += myvprintf_int64(send, flags, 16, width,
550 (ULong)(va_arg (vargs, ULong)));
sewardjde4a1d02002-03-22 01:27:54 +0000551 else
sewardj78e3cd92002-10-22 04:45:48 +0000552 ret += myvprintf_int64(send, flags, 16, width,
553 (ULong)(va_arg (vargs, UInt)));
sewardjde4a1d02002-03-22 01:27:54 +0000554 break;
555 case 'c': /* %c */
sewardj78e3cd92002-10-22 04:45:48 +0000556 ret++;
sewardjde4a1d02002-03-22 01:27:54 +0000557 send(va_arg (vargs, int));
558 break;
559 case 's': case 'S': { /* %s */
560 char *str = va_arg (vargs, char *);
561 if (str == (char*) 0) str = "(null)";
sewardj78e3cd92002-10-22 04:45:48 +0000562 ret += myvprintf_str(send, flags, width, str, format[i]=='S');
sewardjde4a1d02002-03-22 01:27:54 +0000563 break;
sewardj78e3cd92002-10-22 04:45:48 +0000564 }
565 case 'y': { /* %y - print symbol */
566 Char buf[100];
567 Char *cp = buf;
568 Addr a = va_arg(vargs, Addr);
569
570 if (flags & VG_MSG_PAREN)
571 *cp++ = '(';
sewardj6e008cb2002-12-15 13:11:39 +0000572 if (VG_(get_fnname_w_offset)(a, cp, sizeof(buf)-4)) {
sewardj78e3cd92002-10-22 04:45:48 +0000573 if (flags & VG_MSG_PAREN) {
574 cp += VG_(strlen)(cp);
575 *cp++ = ')';
576 *cp = '\0';
577 }
578 ret += myvprintf_str(send, flags, width, buf, 0);
579 }
580
581 break;
582 }
sewardjde4a1d02002-03-22 01:27:54 +0000583 default:
584 break;
585 }
586 }
sewardj78e3cd92002-10-22 04:45:48 +0000587 return ret;
sewardjde4a1d02002-03-22 01:27:54 +0000588}
589
590
591/* A general replacement for printf(). Note that only low-level
592 debugging info should be sent via here. The official route is to
593 to use vg_message(). This interface is deprecated.
594*/
595static char myprintf_buf[100];
596static int n_myprintf_buf;
597
598static void add_to_myprintf_buf ( Char c )
599{
600 if (n_myprintf_buf >= 100-10 /*paranoia*/ ) {
sewardj73cf3bc2002-11-03 03:20:15 +0000601 if (VG_(clo_logfile_fd) >= 0) {
sewardj570f8902002-11-03 11:44:36 +0000602 VG_(send_bytes_to_logging_sink)(
603 myprintf_buf, VG_(strlen)(myprintf_buf) );
sewardj73cf3bc2002-11-03 03:20:15 +0000604 }
sewardjde4a1d02002-03-22 01:27:54 +0000605 n_myprintf_buf = 0;
606 myprintf_buf[n_myprintf_buf] = 0;
607 }
608 myprintf_buf[n_myprintf_buf++] = c;
609 myprintf_buf[n_myprintf_buf] = 0;
610}
611
sewardj78e3cd92002-10-22 04:45:48 +0000612UInt VG_(printf) ( const char *format, ... )
sewardjde4a1d02002-03-22 01:27:54 +0000613{
sewardj78e3cd92002-10-22 04:45:48 +0000614 UInt ret;
sewardjde4a1d02002-03-22 01:27:54 +0000615 va_list vargs;
616 va_start(vargs,format);
617
618 n_myprintf_buf = 0;
619 myprintf_buf[n_myprintf_buf] = 0;
sewardj78e3cd92002-10-22 04:45:48 +0000620 ret = VG_(vprintf) ( add_to_myprintf_buf, format, vargs );
sewardjde4a1d02002-03-22 01:27:54 +0000621
sewardj73cf3bc2002-11-03 03:20:15 +0000622 if (n_myprintf_buf > 0 && VG_(clo_logfile_fd) >= 0) {
sewardj570f8902002-11-03 11:44:36 +0000623 VG_(send_bytes_to_logging_sink)( myprintf_buf, n_myprintf_buf );
sewardj73cf3bc2002-11-03 03:20:15 +0000624 }
sewardjde4a1d02002-03-22 01:27:54 +0000625
626 va_end(vargs);
sewardj78e3cd92002-10-22 04:45:48 +0000627
628 return ret;
sewardjde4a1d02002-03-22 01:27:54 +0000629}
630
631
632/* A general replacement for sprintf(). */
sewardj78e3cd92002-10-22 04:45:48 +0000633UInt VG_(sprintf) ( Char* buf, Char *format, ... )
sewardjde4a1d02002-03-22 01:27:54 +0000634{
sewardj05bcdcb2003-05-18 10:05:38 +0000635 Int ret;
sewardjde4a1d02002-03-22 01:27:54 +0000636 va_list vargs;
sewardj1771e172002-11-13 22:06:35 +0000637 Char *ptr = buf;
638 static void add_to_vg_sprintf_buf ( Char c )
639 {
640 *ptr++ = c;
641 }
642
sewardjde4a1d02002-03-22 01:27:54 +0000643 va_start(vargs,format);
644
sewardj78e3cd92002-10-22 04:45:48 +0000645 ret = VG_(vprintf) ( add_to_vg_sprintf_buf, format, vargs );
sewardjde4a1d02002-03-22 01:27:54 +0000646 add_to_vg_sprintf_buf(0);
647
648 va_end(vargs);
sewardj78e3cd92002-10-22 04:45:48 +0000649
650 vg_assert(VG_(strlen)(buf) == ret);
651 return ret;
sewardjde4a1d02002-03-22 01:27:54 +0000652}
653
654
655/* ---------------------------------------------------------------------
656 Misc str* functions.
657 ------------------------------------------------------------------ */
658
659Bool VG_(isspace) ( Char c )
660{
661 return (c == ' ' || c == '\n' || c == '\t' || c == 0);
662}
663
njn7cf0bd32002-06-08 13:36:03 +0000664Bool VG_(isdigit) ( Char c )
665{
666 return (c >= '0' && c <= '9');
667}
sewardjde4a1d02002-03-22 01:27:54 +0000668
669Int VG_(strlen) ( const Char* str )
670{
671 Int i = 0;
672 while (str[i] != 0) i++;
673 return i;
674}
675
676
677Long VG_(atoll) ( Char* str )
678{
679 Bool neg = False;
680 Long n = 0;
681 if (*str == '-') { str++; neg = True; };
682 while (*str >= '0' && *str <= '9') {
683 n = 10*n + (Long)(*str - '0');
684 str++;
685 }
686 if (neg) n = -n;
687 return n;
688}
689
690
njn25e49d8e72002-09-23 09:36:25 +0000691Long VG_(atoll16) ( Char* str )
sewardja70ca3f2002-05-30 01:22:20 +0000692{
693 Bool neg = False;
694 Long n = 0;
695 if (*str == '-') { str++; neg = True; };
696 while (True) {
697 if (*str >= '0' && *str <= '9') {
njn25e49d8e72002-09-23 09:36:25 +0000698 n = 16*n + (Long)(*str - '0');
sewardja70ca3f2002-05-30 01:22:20 +0000699 }
700 else
njn25e49d8e72002-09-23 09:36:25 +0000701 if (*str >= 'A' && *str <= 'F') {
702 n = 16*n + (Long)((*str - 'A') + 10);
sewardja70ca3f2002-05-30 01:22:20 +0000703 }
704 else
njn25e49d8e72002-09-23 09:36:25 +0000705 if (*str >= 'a' && *str <= 'f') {
706 n = 16*n + (Long)((*str - 'a') + 10);
707 }
708 else {
709 break;
710 }
711 str++;
712 }
713 if (neg) n = -n;
714 return n;
715}
716
717Long VG_(atoll36) ( UInt base, Char* str )
718{
719 Bool neg = False;
720 Long n = 0;
721 vg_assert(base >= 2 && base <= 36);
722 if (*str == '-') { str++; neg = True; };
723 while (True) {
sewardj05bcdcb2003-05-18 10:05:38 +0000724 if (*str >= '0'
725 && *str <= (Char)('9' - (10 - base))) {
njn25e49d8e72002-09-23 09:36:25 +0000726 n = base*n + (Long)(*str - '0');
727 }
728 else
sewardj05bcdcb2003-05-18 10:05:38 +0000729 if (base > 10 && *str >= 'A'
730 && *str <= (Char)('Z' - (36 - base))) {
njn25e49d8e72002-09-23 09:36:25 +0000731 n = base*n + (Long)((*str - 'A') + 10);
732 }
733 else
sewardj05bcdcb2003-05-18 10:05:38 +0000734 if (base > 10 && *str >= 'a'
735 && *str <= (Char)('z' - (36 - base))) {
njn25e49d8e72002-09-23 09:36:25 +0000736 n = base*n + (Long)((*str - 'a') + 10);
sewardja70ca3f2002-05-30 01:22:20 +0000737 }
738 else {
739 break;
740 }
741 str++;
742 }
743 if (neg) n = -n;
744 return n;
745}
746
747
sewardjde4a1d02002-03-22 01:27:54 +0000748Char* VG_(strcat) ( Char* dest, const Char* src )
749{
750 Char* dest_orig = dest;
751 while (*dest) dest++;
752 while (*src) *dest++ = *src++;
753 *dest = 0;
754 return dest_orig;
755}
756
757
758Char* VG_(strncat) ( Char* dest, const Char* src, Int n )
759{
760 Char* dest_orig = dest;
761 while (*dest) dest++;
762 while (*src && n > 0) { *dest++ = *src++; n--; }
763 *dest = 0;
764 return dest_orig;
765}
766
767
768Char* VG_(strpbrk) ( const Char* s, const Char* accept )
769{
770 const Char* a;
771 while (*s) {
772 a = accept;
773 while (*a)
774 if (*a++ == *s)
775 return (Char *) s;
776 s++;
777 }
778 return NULL;
779}
780
781
782Char* VG_(strcpy) ( Char* dest, const Char* src )
783{
784 Char* dest_orig = dest;
785 while (*src) *dest++ = *src++;
786 *dest = 0;
787 return dest_orig;
788}
789
790
791/* Copy bytes, not overrunning the end of dest and always ensuring
792 zero termination. */
793void VG_(strncpy_safely) ( Char* dest, const Char* src, Int ndest )
794{
795 Int i;
796 vg_assert(ndest > 0);
797 i = 0;
798 dest[i] = 0;
799 while (True) {
800 if (src[i] == 0) return;
801 if (i >= ndest-1) return;
802 dest[i] = src[i];
803 i++;
804 dest[i] = 0;
805 }
806}
807
808
njn25e49d8e72002-09-23 09:36:25 +0000809Char* VG_(strncpy) ( Char* dest, const Char* src, Int ndest )
sewardjde4a1d02002-03-22 01:27:54 +0000810{
njn25e49d8e72002-09-23 09:36:25 +0000811 Int i = 0;
812 while (True) {
813 if (i >= ndest) return dest; /* reached limit */
814 dest[i] = src[i];
815 if (src[i++] == 0) {
816 /* reached NUL; pad rest with zeroes as required */
817 while (i < ndest) dest[i++] = 0;
818 return dest;
819 }
820 }
sewardjde4a1d02002-03-22 01:27:54 +0000821}
822
823
824Int VG_(strcmp) ( const Char* s1, const Char* s2 )
825{
826 while (True) {
827 if (*s1 == 0 && *s2 == 0) return 0;
828 if (*s1 == 0) return -1;
829 if (*s2 == 0) return 1;
830
831 if (*(UChar*)s1 < *(UChar*)s2) return -1;
832 if (*(UChar*)s1 > *(UChar*)s2) return 1;
833
834 s1++; s2++;
835 }
836}
837
838
839Int VG_(strcmp_ws) ( const Char* s1, const Char* s2 )
840{
841 while (True) {
842 if (VG_(isspace)(*s1) && VG_(isspace)(*s2)) return 0;
843 if (VG_(isspace)(*s1)) return -1;
844 if (VG_(isspace)(*s2)) return 1;
845
846 if (*(UChar*)s1 < *(UChar*)s2) return -1;
847 if (*(UChar*)s1 > *(UChar*)s2) return 1;
848
849 s1++; s2++;
850 }
851}
852
853
854Int VG_(strncmp) ( const Char* s1, const Char* s2, Int nmax )
855{
856 Int n = 0;
857 while (True) {
858 if (n >= nmax) return 0;
859 if (*s1 == 0 && *s2 == 0) return 0;
860 if (*s1 == 0) return -1;
861 if (*s2 == 0) return 1;
862
863 if (*(UChar*)s1 < *(UChar*)s2) return -1;
864 if (*(UChar*)s1 > *(UChar*)s2) return 1;
865
866 s1++; s2++; n++;
867 }
868}
869
870
871Int VG_(strncmp_ws) ( const Char* s1, const Char* s2, Int nmax )
872{
873 Int n = 0;
874 while (True) {
875 if (n >= nmax) return 0;
876 if (VG_(isspace)(*s1) && VG_(isspace)(*s2)) return 0;
877 if (VG_(isspace)(*s1)) return -1;
878 if (VG_(isspace)(*s2)) return 1;
879
880 if (*(UChar*)s1 < *(UChar*)s2) return -1;
881 if (*(UChar*)s1 > *(UChar*)s2) return 1;
882
883 s1++; s2++; n++;
884 }
885}
886
887
888Char* VG_(strstr) ( const Char* haystack, Char* needle )
889{
sewardj3984b852002-05-12 03:00:17 +0000890 Int n;
891 if (haystack == NULL)
892 return NULL;
893 n = VG_(strlen)(needle);
sewardjde4a1d02002-03-22 01:27:54 +0000894 while (True) {
895 if (haystack[0] == 0)
896 return NULL;
897 if (VG_(strncmp)(haystack, needle, n) == 0)
898 return (Char*)haystack;
899 haystack++;
900 }
901}
902
903
904Char* VG_(strchr) ( const Char* s, Char c )
905{
906 while (True) {
907 if (*s == c) return (Char*)s;
908 if (*s == 0) return NULL;
909 s++;
910 }
911}
912
913
sewardj7ab2aca2002-10-20 19:40:32 +0000914void* VG_(memcpy) ( void *dest, const void *src, Int sz )
915{
916 const Char *s = (const Char *)src;
917 Char *d = (Char *)dest;
918 vg_assert(sz >= 0);
919
920 while (sz--)
921 *d++ = *s++;
922
923 return dest;
924}
925
926
927void* VG_(memset) ( void *dest, Int c, Int sz )
928{
929 Char *d = (Char *)dest;
930 vg_assert(sz >= 0);
931
932 while (sz--)
933 *d++ = c;
934
935 return dest;
936}
937
njn6d68e792003-02-24 10:49:08 +0000938Int VG_(memcmp) ( const void* s1, const void* s2, Int n )
939{
940 Int res;
njnfa4690b2003-02-24 21:43:05 +0000941 UChar a0;
942 UChar b0;
njn6d68e792003-02-24 10:49:08 +0000943 vg_assert(n >= 0);
944
945 while (n != 0) {
njnfa4690b2003-02-24 21:43:05 +0000946 a0 = ((UChar *) s1)[0];
947 b0 = ((UChar *) s2)[0];
njn6d68e792003-02-24 10:49:08 +0000948 s1 += 1;
949 s2 += 1;
950 res = a0 - b0;
951 if (res != 0)
952 return res;
953 n -= 1;
954 }
955 return 0;
956}
sewardj7ab2aca2002-10-20 19:40:32 +0000957
sewardjde4a1d02002-03-22 01:27:54 +0000958Char VG_(toupper) ( Char c )
959{
960 if (c >= 'a' && c <= 'z')
961 return c + ('A' - 'a');
962 else
963 return c;
964}
965
966
njn25e49d8e72002-09-23 09:36:25 +0000967/* Inline just for the wrapper VG_(strdup) below */
968__inline__ Char* VG_(arena_strdup) ( ArenaId aid, const Char* s )
sewardjde4a1d02002-03-22 01:27:54 +0000969{
njn25e49d8e72002-09-23 09:36:25 +0000970 Int i;
971 Int len = VG_(strlen)(s) + 1;
972 Char* res = VG_(arena_malloc) (aid, len);
973 for (i = 0; i < len; i++)
974 res[i] = s[i];
975 return res;
sewardjde4a1d02002-03-22 01:27:54 +0000976}
977
njn25e49d8e72002-09-23 09:36:25 +0000978/* Wrapper to avoid exposing skins to ArenaId's */
979Char* VG_(strdup) ( const Char* s )
980{
981 return VG_(arena_strdup) ( VG_AR_SKIN, s );
982}
sewardjde4a1d02002-03-22 01:27:54 +0000983
984/* ---------------------------------------------------------------------
985 A simple string matching routine, purloined from Hugs98.
986 `*' matches any sequence of zero or more characters
987 `?' matches any single character exactly
988 `\c' matches the character c only (ignoring special chars)
989 c matches the character c only
990 ------------------------------------------------------------------ */
991
992/* Keep track of recursion depth. */
993static Int recDepth;
994
njn4ba5a792002-09-30 10:23:54 +0000995static Bool string_match_wrk ( Char* pat, Char* str )
sewardjde4a1d02002-03-22 01:27:54 +0000996{
sewardje54b69d2003-07-06 01:14:42 +0000997 vg_assert(recDepth >= 0 && recDepth < 500);
sewardjde4a1d02002-03-22 01:27:54 +0000998 recDepth++;
999 for (;;) {
1000 switch (*pat) {
1001 case '\0' : return (*str=='\0');
1002 case '*' : do {
njn4ba5a792002-09-30 10:23:54 +00001003 if (string_match_wrk(pat+1,str)) {
sewardjde4a1d02002-03-22 01:27:54 +00001004 recDepth--;
1005 return True;
1006 }
1007 } while (*str++);
1008 recDepth--;
1009 return False;
1010 case '?' : if (*str++=='\0') {
1011 recDepth--;
1012 return False;
1013 }
1014 pat++;
1015 break;
1016 case '\\' : if (*++pat == '\0') {
1017 recDepth--;
1018 return False; /* spurious trailing \ in pattern */
1019 }
1020 /* falls through to ... */
1021 default : if (*pat++ != *str++) {
1022 recDepth--;
1023 return False;
1024 }
1025 break;
1026 }
1027 }
1028}
1029
njn4ba5a792002-09-30 10:23:54 +00001030Bool VG_(string_match) ( Char* pat, Char* str )
sewardjde4a1d02002-03-22 01:27:54 +00001031{
1032 Bool b;
1033 recDepth = 0;
njn4ba5a792002-09-30 10:23:54 +00001034 b = string_match_wrk ( pat, str );
sewardjde4a1d02002-03-22 01:27:54 +00001035 /*
1036 VG_(printf)("%s %s %s\n",
1037 b?"TRUE ":"FALSE", pat, str);
1038 */
1039 return b;
1040}
1041
1042
1043/* ---------------------------------------------------------------------
1044 Assertery.
1045 ------------------------------------------------------------------ */
1046
jsgf855d93d2003-10-13 22:26:55 +00001047/* Fake up an ExeContext which is of our actual real CPU state, so we
1048 can print a stack trace. This isn't terribly useful in the case
1049 where we were killed by a signal, since we just get a backtrace
1050 into the signal handler. Also, it could be somewhat risky if we
1051 actully got the panic/exception within the execontext/stack
1052 dump/symtab code. But it's better than nothing. */
1053static inline ExeContext *get_real_execontext(Addr ret)
1054{
1055 ExeContext *ec;
fitzhardingea09a1b52003-11-07 23:09:48 +00001056 Addr esp, ebp;
jsgf855d93d2003-10-13 22:26:55 +00001057 Addr stacktop;
jsgf855d93d2003-10-13 22:26:55 +00001058
fitzhardingea09a1b52003-11-07 23:09:48 +00001059 asm("movl %%ebp, %0; movl %%esp, %1" : "=r" (ebp), "=r" (esp));
jsgf855d93d2003-10-13 22:26:55 +00001060 stacktop = (Addr)&VG_(stack)[VG_STACK_SIZE_W];
1061 if (esp >= (Addr)&VG_(sigstack)[0] && esp < (Addr)&VG_(sigstack)[VG_STACK_SIZE_W])
1062 stacktop = (Addr)&VG_(sigstack)[VG_STACK_SIZE_W];
1063
1064 ec = VG_(get_ExeContext2)(ret, ebp, esp, stacktop);
1065
1066 return ec;
1067}
1068
njne427a662002-10-02 11:08:25 +00001069__attribute__ ((noreturn))
daywalker3222e0a2003-09-18 01:39:50 +00001070static void report_and_quit ( const Char* report )
njne427a662002-10-02 11:08:25 +00001071{
jsgf855d93d2003-10-13 22:26:55 +00001072 ExeContext *ec = get_real_execontext((Addr)__builtin_return_address(0));
1073 VG_(pp_ExeContext)(ec);
1074
njne427a662002-10-02 11:08:25 +00001075 VG_(pp_sched_status)();
sewardj366ee1e2003-04-26 22:36:42 +00001076 VG_(printf)("\n");
1077 VG_(printf)("Note: see also the FAQ.txt in the source distribution.\n");
1078 VG_(printf)("It contains workarounds to several common problems.\n");
1079 VG_(printf)("\n");
1080 VG_(printf)("If that doesn't help, please report this bug to: %s\n\n",
1081 report);
1082 VG_(printf)("In the bug report, send all the above text, the valgrind\n");
sewardj96330bd2003-04-27 23:46:21 +00001083 VG_(printf)("version, and what Linux distro you are using. Thanks.\n\n");
njne427a662002-10-02 11:08:25 +00001084 VG_(shutdown_logging)();
1085 VG_(exit)(1);
1086}
1087
1088__attribute__ ((noreturn))
daywalker3222e0a2003-09-18 01:39:50 +00001089static void assert_fail ( const Char* expr, const Char* name, const Char* report,
1090 const Char* file, Int line, const Char* fn )
sewardjde4a1d02002-03-22 01:27:54 +00001091{
sewardj018f7622002-05-15 21:13:39 +00001092 static Bool entered = False;
1093 if (entered)
1094 VG_(exit)(2);
1095 entered = True;
sewardjde4a1d02002-03-22 01:27:54 +00001096 VG_(printf)("\n%s: %s:%d (%s): Assertion `%s' failed.\n",
njne427a662002-10-02 11:08:25 +00001097 name, file, line, fn, expr );
1098 report_and_quit(report);
sewardjde4a1d02002-03-22 01:27:54 +00001099}
1100
daywalker3222e0a2003-09-18 01:39:50 +00001101void VG_(skin_assert_fail) ( const Char* expr, const Char* file, Int line, const Char* fn )
sewardjde4a1d02002-03-22 01:27:54 +00001102{
njnd04b7c62002-10-03 14:05:52 +00001103 assert_fail(expr, VG_(details).name, VG_(details).bug_reports_to,
njne427a662002-10-02 11:08:25 +00001104 file, line, fn);
1105}
1106
daywalker3222e0a2003-09-18 01:39:50 +00001107void VG_(core_assert_fail) ( const Char* expr, const Char* file, Int line, const Char* fn )
njne427a662002-10-02 11:08:25 +00001108{
1109 assert_fail(expr, "valgrind", VG_EMAIL_ADDR, file, line, fn);
1110}
1111
1112__attribute__ ((noreturn))
1113static void panic ( Char* name, Char* report, Char* str )
1114{
1115 VG_(printf)("\n%s: the `impossible' happened:\n %s\n", name, str);
sewardjde4a1d02002-03-22 01:27:54 +00001116 VG_(printf)("Basic block ctr is approximately %llu\n", VG_(bbs_done) );
njne427a662002-10-02 11:08:25 +00001117 report_and_quit(report);
sewardjde4a1d02002-03-22 01:27:54 +00001118}
1119
njne427a662002-10-02 11:08:25 +00001120void VG_(core_panic) ( Char* str )
njn25e49d8e72002-09-23 09:36:25 +00001121{
njne427a662002-10-02 11:08:25 +00001122 panic("valgrind", VG_EMAIL_ADDR, str);
njn25e49d8e72002-09-23 09:36:25 +00001123}
1124
njne427a662002-10-02 11:08:25 +00001125void VG_(skin_panic) ( Char* str )
1126{
njnd04b7c62002-10-03 14:05:52 +00001127 panic(VG_(details).name, VG_(details).bug_reports_to, str);
njne427a662002-10-02 11:08:25 +00001128}
sewardjde4a1d02002-03-22 01:27:54 +00001129
njn13f02932003-04-30 20:23:58 +00001130
sewardjde4a1d02002-03-22 01:27:54 +00001131/* ---------------------------------------------------------------------
1132 Primitive support for reading files.
1133 ------------------------------------------------------------------ */
1134
jsgf855d93d2003-10-13 22:26:55 +00001135static inline Bool fd_exists(Int fd)
1136{
1137 struct vki_stat st;
1138
1139 return VG_(fstat)(fd, &st) == 0;
1140}
1141
1142/* Move an fd into the Valgrind-safe range */
1143Int VG_(safe_fd)(Int oldfd)
1144{
1145 Int newfd;
1146
1147 newfd = VG_(fcntl)(oldfd, VKI_F_DUPFD, VG_MAX_FD+1);
1148 if (newfd != -1)
1149 VG_(close)(oldfd);
1150
1151 VG_(fcntl)(newfd, VKI_F_SETFD, VKI_FD_CLOEXEC);
1152
1153 vg_assert(newfd > VG_MAX_FD);
1154 return newfd;
1155}
1156
1157
1158
sewardjde4a1d02002-03-22 01:27:54 +00001159/* Returns -1 on failure. */
njn25e49d8e72002-09-23 09:36:25 +00001160Int VG_(open) ( const Char* pathname, Int flags, Int mode )
1161{
sewardjde4a1d02002-03-22 01:27:54 +00001162 Int fd;
sewardjde4a1d02002-03-22 01:27:54 +00001163
njn25e49d8e72002-09-23 09:36:25 +00001164 /* (old comment, not sure if it still applies NJN 2002-sep-09) */
sewardjde4a1d02002-03-22 01:27:54 +00001165 /* This gets a segmentation fault if pathname isn't a valid file.
1166 I don't know why. It seems like the call to open is getting
1167 intercepted and messed with by glibc ... */
1168 /* fd = open( pathname, O_RDONLY ); */
1169 /* ... so we go direct to the horse's mouth, which seems to work
1170 ok: */
jsgf855d93d2003-10-13 22:26:55 +00001171 fd = VG_(do_syscall)(__NR_open, (UInt)pathname, flags, mode);
njn4f9c9342002-04-29 16:03:24 +00001172 /* VG_(printf)("result = %d\n", fd); */
jsgff3c3f1a2003-10-14 22:13:28 +00001173 /* return -ve error code */
njn4f9c9342002-04-29 16:03:24 +00001174 return fd;
1175}
sewardjde4a1d02002-03-22 01:27:54 +00001176
jsgf855d93d2003-10-13 22:26:55 +00001177Int VG_(pipe) ( Int fd[2] )
1178{
1179 Int ret = VG_(do_syscall)(__NR_pipe, fd);
1180 return VG_(is_kerror)(ret) ? -1 : 0;
1181}
1182
sewardjde4a1d02002-03-22 01:27:54 +00001183void VG_(close) ( Int fd )
1184{
jsgf855d93d2003-10-13 22:26:55 +00001185 VG_(do_syscall)(__NR_close, fd);
sewardjde4a1d02002-03-22 01:27:54 +00001186}
1187
1188
1189Int VG_(read) ( Int fd, void* buf, Int count)
1190{
1191 Int res;
1192 /* res = read( fd, buf, count ); */
jsgf855d93d2003-10-13 22:26:55 +00001193 res = VG_(do_syscall)(__NR_read, fd, (UInt)buf, count);
1194 /* return -ERRNO on error */
sewardjde4a1d02002-03-22 01:27:54 +00001195 return res;
1196}
1197
jsgf855d93d2003-10-13 22:26:55 +00001198Int VG_(write) ( Int fd, const void* buf, Int count)
sewardjde4a1d02002-03-22 01:27:54 +00001199{
1200 Int res;
1201 /* res = write( fd, buf, count ); */
jsgf855d93d2003-10-13 22:26:55 +00001202 res = VG_(do_syscall)(__NR_write, fd, (UInt)buf, count);
1203 /* return -ERRNO on error */
sewardjde4a1d02002-03-22 01:27:54 +00001204 return res;
1205}
1206
rjwalshf5f536f2003-11-17 17:45:00 +00001207Int VG_(lseek) ( Int fd, Long offset, Int whence)
1208{
1209 Int res;
1210 /* res = lseek( fd, offset, whence ); */
1211 res = VG_(do_syscall)(__NR_lseek, fd, (UInt)offset, whence);
1212 if (VG_(is_kerror)(res)) res = -1;
1213 return res;
1214}
1215
sewardjb3586202002-05-09 17:38:13 +00001216Int VG_(stat) ( Char* file_name, struct vki_stat* buf )
1217{
1218 Int res;
jsgf855d93d2003-10-13 22:26:55 +00001219 res = VG_(do_syscall)(__NR_stat, (UInt)file_name, (UInt)buf);
fitzhardingee1c06d82003-10-30 07:21:44 +00001220 return res; /* return -ve error */
njn41557122002-10-14 09:25:37 +00001221}
1222
jsgf855d93d2003-10-13 22:26:55 +00001223Int VG_(fstat) ( Int fd, struct vki_stat* buf )
1224{
1225 Int res;
1226 res = VG_(do_syscall)(__NR_fstat, (UInt)fd, (UInt)buf);
1227 return VG_(is_kerror)(res) ? (-1) : 0;
1228}
1229
1230Int VG_(dup2) ( Int oldfd, Int newfd )
1231{
1232 Int res;
1233 res = VG_(do_syscall)(__NR_dup2, (UInt)oldfd, (UInt)newfd);
1234 return VG_(is_kerror)(res) ? (-1) : res;
1235}
1236
njn41557122002-10-14 09:25:37 +00001237Int VG_(rename) ( Char* old_name, Char* new_name )
1238{
1239 Int res;
jsgf855d93d2003-10-13 22:26:55 +00001240 res = VG_(do_syscall)(__NR_rename, (UInt)old_name, (UInt)new_name);
njn41557122002-10-14 09:25:37 +00001241 return VG_(is_kerror)(res) ? (-1) : 0;
sewardjb3586202002-05-09 17:38:13 +00001242}
1243
njn4aca2d22002-10-04 10:29:38 +00001244Int VG_(unlink) ( Char* file_name )
1245{
1246 Int res;
jsgf855d93d2003-10-13 22:26:55 +00001247 res = VG_(do_syscall)(__NR_unlink, (UInt)file_name);
njn41557122002-10-14 09:25:37 +00001248 return VG_(is_kerror)(res) ? (-1) : 0;
njn4aca2d22002-10-04 10:29:38 +00001249}
1250
njn13f02932003-04-30 20:23:58 +00001251/* Nb: we do not allow the Linux extension which malloc()s memory for the
1252 buffer if buf==NULL, because we don't want Linux calling malloc() */
1253Char* VG_(getcwd) ( Char* buf, Int size )
1254{
1255 Int res;
1256 vg_assert(buf != NULL);
jsgf855d93d2003-10-13 22:26:55 +00001257 res = VG_(do_syscall)(__NR_getcwd, (UInt)buf, (UInt)size);
njn13f02932003-04-30 20:23:58 +00001258 return VG_(is_kerror)(res) ? ((Char*)NULL) : (Char*)res;
1259}
1260
njn99ccf082003-09-30 13:51:23 +00001261/* Alternative version that does allocate the memory. Easier to use. */
1262Bool VG_(getcwd_alloc) ( Char** out )
1263{
1264 UInt size = 4;
1265
1266 *out = NULL;
1267 while (True) {
1268 *out = VG_(malloc)(size);
1269 if (NULL == VG_(getcwd)(*out, size)) {
1270 VG_(free)(*out);
1271 if (size > 65535)
1272 return False;
1273 size *= 2;
1274 } else {
1275 return True;
1276 }
1277 }
1278}
1279
njn13f02932003-04-30 20:23:58 +00001280
1281/* ---------------------------------------------------------------------
1282 Misc functions looking for a proper home.
1283 ------------------------------------------------------------------ */
sewardjde4a1d02002-03-22 01:27:54 +00001284
1285/* We do getenv without libc's help by snooping around in
njn25e49d8e72002-09-23 09:36:25 +00001286 VG_(client_envp) as determined at startup time. */
sewardjde4a1d02002-03-22 01:27:54 +00001287Char* VG_(getenv) ( Char* varname )
1288{
1289 Int i, n;
1290 n = VG_(strlen)(varname);
1291 for (i = 0; VG_(client_envp)[i] != NULL; i++) {
1292 Char* s = VG_(client_envp)[i];
1293 if (VG_(strncmp)(varname, s, n) == 0 && s[n] == '=') {
1294 return & s[n+1];
1295 }
1296 }
1297 return NULL;
1298}
1299
sewardj4cf05692002-10-27 20:28:29 +00001300
rjwalshf5f536f2003-11-17 17:45:00 +00001301/* Support for getrlimit. */
1302Int VG_(getrlimit) (Int resource, struct vki_rlimit *rlim)
1303{
1304 Int res;
1305 /* res = getrlimit( resource, rlim ); */
1306 res = VG_(do_syscall)(__NR_getrlimit, (UInt)resource, (UInt)rlim);
1307 if(VG_(is_kerror)(res)) res = -1;
1308 return res;
1309}
1310
1311
1312/* Support for getdents. */
1313Int VG_(getdents) (UInt fd, struct vki_dirent *dirp, UInt count)
1314{
1315 Int res;
1316 /* res = getdents( fd, dirp, count ); */
1317 res = VG_(do_syscall)(__NR_getdents, fd, (UInt)dirp, count);
1318 if (VG_(is_kerror)(res)) res = -1;
1319 return res;
1320}
1321
1322/* Support for a readlink. */
1323Int VG_(readlink) (Char* path, Char* buf, UInt bufsiz)
1324{
1325 Int res;
1326 /* res = readlink( path, buf, bufsiz ); */
1327 res = VG_(do_syscall)(__NR_readlink, (UInt)path, (UInt)buf, bufsiz);
1328 if (VG_(is_kerror)(res)) res = -1;
1329 return res;
1330}
1331
sewardjde4a1d02002-03-22 01:27:54 +00001332/* You'd be amazed how many places need to know the current pid. */
1333Int VG_(getpid) ( void )
1334{
1335 Int res;
1336 /* res = getpid(); */
jsgf855d93d2003-10-13 22:26:55 +00001337 res = VG_(do_syscall)(__NR_getpid);
1338 return res;
1339}
1340
1341Int VG_(getpgrp) ( void )
1342{
1343 Int res;
1344 /* res = getpgid(); */
1345 res = VG_(do_syscall)(__NR_getpgrp);
sewardjde4a1d02002-03-22 01:27:54 +00001346 return res;
1347}
1348
sewardj4cf05692002-10-27 20:28:29 +00001349Int VG_(getppid) ( void )
1350{
1351 Int res;
jsgf855d93d2003-10-13 22:26:55 +00001352 res = VG_(do_syscall)(__NR_getppid);
sewardj4cf05692002-10-27 20:28:29 +00001353 return res;
1354}
1355
jsgf855d93d2003-10-13 22:26:55 +00001356Int VG_(setpgid) ( Int pid, Int pgrp )
1357{
1358 return VG_(do_syscall)(__NR_setpgid, pid, pgrp);
1359}
sewardj4cf05692002-10-27 20:28:29 +00001360
sewardje6a25242002-04-21 22:03:07 +00001361/* Return -1 if error, else 0. NOTE does not indicate return code of
1362 child! */
1363Int VG_(system) ( Char* cmd )
1364{
1365 Int pid, res;
1366 void* environ[1] = { NULL };
1367 if (cmd == NULL)
1368 return 1;
jsgf855d93d2003-10-13 22:26:55 +00001369 pid = VG_(do_syscall)(__NR_fork);
sewardje6a25242002-04-21 22:03:07 +00001370 if (VG_(is_kerror)(pid))
1371 return -1;
1372 if (pid == 0) {
1373 /* child */
1374 Char* argv[4];
1375 argv[0] = "/bin/sh";
1376 argv[1] = "-c";
1377 argv[2] = cmd;
1378 argv[3] = 0;
jsgf855d93d2003-10-13 22:26:55 +00001379 (void)VG_(do_syscall)(__NR_execve,
1380 (UInt)"/bin/sh", (UInt)argv, (UInt)&environ);
sewardje6a25242002-04-21 22:03:07 +00001381 /* If we're still alive here, execve failed. */
1382 return -1;
1383 } else {
1384 /* parent */
jsgf855d93d2003-10-13 22:26:55 +00001385 res = VG_(do_syscall)(__NR_waitpid, pid, (UInt)NULL, 0);
sewardje6a25242002-04-21 22:03:07 +00001386 if (VG_(is_kerror)(res)) {
1387 return -1;
1388 } else {
1389 return 0;
1390 }
1391 }
1392}
1393
1394
sewardjde4a1d02002-03-22 01:27:54 +00001395/* ---------------------------------------------------------------------
sewardj5f07b662002-04-23 16:52:51 +00001396 Support for a millisecond-granularity counter using RDTSC.
1397 ------------------------------------------------------------------ */
1398
1399static __inline__ ULong do_rdtsc_insn ( void )
1400{
1401 ULong x;
1402 __asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
1403 return x;
1404}
1405
1406/* 0 = pre-calibration, 1 = calibration, 2 = running */
1407static Int rdtsc_calibration_state = 0;
1408static ULong rdtsc_ticks_per_millisecond = 0; /* invalid value */
1409
1410static struct vki_timeval rdtsc_cal_start_timeval;
1411static struct vki_timeval rdtsc_cal_end_timeval;
1412
1413static ULong rdtsc_cal_start_raw;
1414static ULong rdtsc_cal_end_raw;
1415
1416UInt VG_(read_millisecond_timer) ( void )
1417{
1418 ULong rdtsc_now;
njne7442cf2003-09-30 14:03:21 +00001419 // If called before rdtsc setup completed (eg. from SK_(pre_clo_init)())
1420 // just return 0.
1421 if (rdtsc_calibration_state < 2) return 0;
sewardj5f07b662002-04-23 16:52:51 +00001422 rdtsc_now = do_rdtsc_insn();
1423 vg_assert(rdtsc_now > rdtsc_cal_end_raw);
1424 rdtsc_now -= rdtsc_cal_end_raw;
1425 rdtsc_now /= rdtsc_ticks_per_millisecond;
1426 return (UInt)rdtsc_now;
1427}
1428
1429
1430void VG_(start_rdtsc_calibration) ( void )
1431{
1432 Int res;
1433 vg_assert(rdtsc_calibration_state == 0);
1434 rdtsc_calibration_state = 1;
1435 rdtsc_cal_start_raw = do_rdtsc_insn();
jsgf855d93d2003-10-13 22:26:55 +00001436 res = VG_(do_syscall)(__NR_gettimeofday, (UInt)&rdtsc_cal_start_timeval,
1437 (UInt)NULL);
sewardj5f07b662002-04-23 16:52:51 +00001438 vg_assert(!VG_(is_kerror)(res));
1439}
1440
1441void VG_(end_rdtsc_calibration) ( void )
1442{
1443 Int res, loops;
1444 ULong cpu_clock_MHZ;
1445 ULong cal_clock_ticks;
1446 ULong cal_wallclock_microseconds;
1447 ULong wallclock_start_microseconds;
1448 ULong wallclock_end_microseconds;
1449 struct vki_timespec req;
1450 struct vki_timespec rem;
1451
1452 vg_assert(rdtsc_calibration_state == 1);
1453 rdtsc_calibration_state = 2;
1454
1455 /* Try and delay for 20 milliseconds, so that we can at least have
1456 some minimum level of accuracy. */
1457 req.tv_sec = 0;
1458 req.tv_nsec = 20 * 1000 * 1000;
1459 loops = 0;
1460 while (True) {
1461 res = VG_(nanosleep)(&req, &rem);
1462 vg_assert(res == 0 /*ok*/ || res == 1 /*interrupted*/);
1463 if (res == 0)
1464 break;
1465 if (rem.tv_sec == 0 && rem.tv_nsec == 0)
1466 break;
1467 req = rem;
1468 loops++;
1469 if (loops > 100)
njne427a662002-10-02 11:08:25 +00001470 VG_(core_panic)("calibration nanosleep loop failed?!");
sewardj5f07b662002-04-23 16:52:51 +00001471 }
1472
1473 /* Now read both timers, and do the Math. */
1474 rdtsc_cal_end_raw = do_rdtsc_insn();
jsgf855d93d2003-10-13 22:26:55 +00001475 res = VG_(do_syscall)(__NR_gettimeofday, (UInt)&rdtsc_cal_end_timeval,
1476 (UInt)NULL);
sewardj5f07b662002-04-23 16:52:51 +00001477
1478 vg_assert(rdtsc_cal_end_raw > rdtsc_cal_start_raw);
1479 cal_clock_ticks = rdtsc_cal_end_raw - rdtsc_cal_start_raw;
1480
1481 wallclock_start_microseconds
1482 = (1000000ULL * (ULong)(rdtsc_cal_start_timeval.tv_sec))
1483 + (ULong)(rdtsc_cal_start_timeval.tv_usec);
1484 wallclock_end_microseconds
1485 = (1000000ULL * (ULong)(rdtsc_cal_end_timeval.tv_sec))
1486 + (ULong)(rdtsc_cal_end_timeval.tv_usec);
1487 vg_assert(wallclock_end_microseconds > wallclock_start_microseconds);
1488 cal_wallclock_microseconds
1489 = wallclock_end_microseconds - wallclock_start_microseconds;
1490
1491 /* Since we just nanoslept for 20 ms ... */
1492 vg_assert(cal_wallclock_microseconds >= 20000);
1493
1494 /* Now we know (roughly) that cal_clock_ticks on RDTSC take
1495 cal_wallclock_microseconds elapsed time. Calculate the RDTSC
1496 ticks-per-millisecond value. */
1497 if (0)
1498 VG_(printf)("%lld ticks in %lld microseconds\n",
1499 cal_clock_ticks, cal_wallclock_microseconds );
1500
1501 rdtsc_ticks_per_millisecond
1502 = cal_clock_ticks / (cal_wallclock_microseconds / 1000ULL);
1503 cpu_clock_MHZ
1504 = (1000ULL * rdtsc_ticks_per_millisecond) / 1000000ULL;
1505 if (VG_(clo_verbosity) >= 1)
1506 VG_(message)(Vg_UserMsg, "Estimated CPU clock rate is %d MHz",
1507 (UInt)cpu_clock_MHZ);
sewardj09c55fd2002-06-13 11:37:41 +00001508 if (cpu_clock_MHZ < 50 || cpu_clock_MHZ > 10000)
njne427a662002-10-02 11:08:25 +00001509 VG_(core_panic)("end_rdtsc_calibration: "
sewardj09c55fd2002-06-13 11:37:41 +00001510 "estimated CPU MHz outside range 50 .. 10000");
sewardj5f07b662002-04-23 16:52:51 +00001511 /* Paranoia about division by zero later. */
1512 vg_assert(rdtsc_ticks_per_millisecond != 0);
1513 if (0)
1514 VG_(printf)("ticks per millisecond %llu\n",
1515 rdtsc_ticks_per_millisecond);
1516}
1517
1518
1519
1520/* ---------------------------------------------------------------------
sewardjde4a1d02002-03-22 01:27:54 +00001521 Primitive support for bagging memory via mmap.
1522 ------------------------------------------------------------------ */
1523
sewardje9047952002-06-05 20:28:33 +00001524void* VG_(get_memory_from_mmap) ( Int nBytes, Char* who )
sewardjde4a1d02002-03-22 01:27:54 +00001525{
1526 static UInt tot_alloc = 0;
sewardj7c2020b2002-09-30 22:56:03 +00001527 void* p;
1528 p = VG_(mmap)( 0, nBytes,
1529 VKI_PROT_READ|VKI_PROT_WRITE|VKI_PROT_EXEC,
1530 VKI_MAP_PRIVATE|VKI_MAP_ANONYMOUS, -1, 0 );
sewardjde4a1d02002-03-22 01:27:54 +00001531 if (p != ((void*)(-1))) {
1532 tot_alloc += (UInt)nBytes;
1533 if (0)
sewardje9047952002-06-05 20:28:33 +00001534 VG_(printf)(
1535 "get_memory_from_mmap: %d tot, %d req = %p .. %p, caller %s\n",
1536 tot_alloc, nBytes, p, ((char*)p) + nBytes - 1, who );
sewardjde4a1d02002-03-22 01:27:54 +00001537 return p;
1538 }
sewardj7c2020b2002-09-30 22:56:03 +00001539
njn25e49d8e72002-09-23 09:36:25 +00001540 VG_(printf)("\n");
daywalkerb1db1232003-09-26 00:26:07 +00001541 VG_(printf)("VG_(get_memory_from_mmap): %s's request for %d bytes failed.\n",
1542 who, nBytes);
njn25e49d8e72002-09-23 09:36:25 +00001543 VG_(printf)("VG_(get_memory_from_mmap): %d bytes already allocated.\n",
1544 tot_alloc);
1545 VG_(printf)("\n");
1546 VG_(printf)("This may mean that you have run out of swap space,\n");
1547 VG_(printf)("since running programs on valgrind increases their memory\n");
1548 VG_(printf)("usage at least 3 times. You might want to use 'top'\n");
1549 VG_(printf)("to determine whether you really have run out of swap.\n");
1550 VG_(printf)("If so, you may be able to work around it by adding a\n");
1551 VG_(printf)("temporary swap file -- this is easier than finding a\n");
1552 VG_(printf)("new swap partition. Go ask your sysadmin(s) [politely!]\n");
1553 VG_(printf)("\n");
1554 VG_(printf)("VG_(get_memory_from_mmap): out of memory! Fatal! Bye!\n");
1555 VG_(printf)("\n");
1556 VG_(exit)(1);
sewardjde4a1d02002-03-22 01:27:54 +00001557}
1558
njn25e49d8e72002-09-23 09:36:25 +00001559/* ---------------------------------------------------------------------
1560 Generally useful...
1561 ------------------------------------------------------------------ */
1562
1563Int VG_(log2) ( Int x )
1564{
1565 Int i;
1566 /* Any more than 32 and we overflow anyway... */
1567 for (i = 0; i < 32; i++) {
1568 if (1 << i == x) return i;
1569 }
1570 return -1;
1571}
1572
1573
njnd3b0c5f2003-09-30 14:43:54 +00001574// Generic shell sort. Like stdlib.h's qsort().
1575void VG_(ssort)( void* base, UInt nmemb, UInt size,
1576 Int (*compar)(void*, void*) )
1577{
1578 Int incs[14] = { 1, 4, 13, 40, 121, 364, 1093, 3280,
1579 9841, 29524, 88573, 265720,
1580 797161, 2391484 };
1581 Int lo = 0;
1582 Int hi = nmemb-1;
1583 Int i, j, h, bigN, hp;
1584
1585 bigN = hi - lo + 1; if (bigN < 2) return;
1586 hp = 0; while (hp < 14 && incs[hp] < bigN) hp++; hp--;
1587 vg_assert(0 <= hp && hp < 14);
1588
1589 #define SORT \
1590 for ( ; hp >= 0; hp--) { \
1591 h = incs[hp]; \
njn92c54362003-10-15 14:00:35 +00001592 for (i = lo + h; i <= hi; i++) { \
njnd3b0c5f2003-09-30 14:43:54 +00001593 ASSIGN(v,0, a,i); \
1594 j = i; \
njn92c54362003-10-15 14:00:35 +00001595 while (COMPAR(a,(j-h), v,0) > 0) { \
njnd3b0c5f2003-09-30 14:43:54 +00001596 ASSIGN(a,j, a,(j-h)); \
1597 j = j - h; \
1598 if (j <= (lo + h - 1)) break; \
1599 } \
1600 ASSIGN(a,j, v,0); \
njnd3b0c5f2003-09-30 14:43:54 +00001601 } \
1602 }
1603
1604 // Specialised cases
1605 if (sizeof(UInt) == size) {
1606
1607 #define ASSIGN(dst, dsti, src, srci) \
1608 (dst)[(dsti)] = (src)[(srci)];
1609
1610 #define COMPAR(dst, dsti, src, srci) \
1611 compar( (void*)(& (dst)[(dsti)]), (void*)(& (src)[(srci)]) )
1612
1613 UInt* a = (UInt*)base;
1614 UInt v[1];
1615
1616 SORT;
1617
1618 } else if (sizeof(UShort) == size) {
1619 UShort* a = (UShort*)base;
1620 UShort v[1];
1621
1622 SORT;
1623
1624 } else if (sizeof(UChar) == size) {
1625 UChar* a = (UChar*)base;
1626 UChar v[1];
1627
1628 SORT;
1629
1630 #undef ASSIGN
1631 #undef COMPAR
1632
1633 // General case
1634 } else {
njn92c54362003-10-15 14:00:35 +00001635 char* a = base;
1636 char v[size]; // will be at least 'size' bytes
njnd3b0c5f2003-09-30 14:43:54 +00001637
1638 #define ASSIGN(dst, dsti, src, srci) \
njn92c54362003-10-15 14:00:35 +00001639 VG_(memcpy)( &dst[size*(dsti)], &src[size*(srci)], size );
njnd3b0c5f2003-09-30 14:43:54 +00001640
1641 #define COMPAR(dst, dsti, src, srci) \
njn92c54362003-10-15 14:00:35 +00001642 compar( &dst[size*(dsti)], &src[size*(srci)] )
njnd3b0c5f2003-09-30 14:43:54 +00001643
1644 SORT;
1645
1646 #undef ASSIGN
1647 #undef COMPAR
1648 }
1649 #undef SORT
1650}
1651
sewardj73cf3bc2002-11-03 03:20:15 +00001652/* ---------------------------------------------------------------------
1653 Gruesome hackery for connecting to a logging server over the network.
1654 This is all very Linux-kernel specific.
1655 ------------------------------------------------------------------ */
1656
1657/* Various needed constants from the kernel iface (2.4),
1658 /usr/src/linux-2.4.9-31 */
1659
1660/* kernel, ./include/linux/net.h */
rjwalshf5f536f2003-11-17 17:45:00 +00001661#define SYS_SOCKET 1 /* sys_socket(2) */
1662#define SYS_CONNECT 3 /* sys_connect(2) */
1663#define SYS_GETSOCKNAME 6 /* sys_getsockname(2) */
1664#define SYS_GETPEERNAME 7 /* sys_getpeername(2) */
1665#define SYS_SEND 9 /* sys_send(2) */
1666#define SYS_GETSOCKOPT 15 /* sys_getsockopt(2) */
sewardj73cf3bc2002-11-03 03:20:15 +00001667
1668typedef UInt __u32;
1669
1670/* Internet address. */
1671struct vki_in_addr {
1672 __u32 s_addr;
1673};
1674
1675/* kernel, include/linux/socket.h */
sewardj73cf3bc2002-11-03 03:20:15 +00001676#define AF_INET 2 /* Internet IP Protocol */
sewardj570f8902002-11-03 11:44:36 +00001677#define MSG_NOSIGNAL 0x4000 /* Do not generate SIGPIPE */
sewardj73cf3bc2002-11-03 03:20:15 +00001678
1679/* kernel, ./include/asm-i386/socket.h */
1680#define SOCK_STREAM 1 /* stream (connection) socket */
1681
1682/* kernel, /usr/src/linux-2.4.9-31/linux/include/in.h */
1683/* Structure describing an Internet (IP) socket address. */
1684#define __SOCK_SIZE__ 16 /* sizeof(struct sockaddr) */
1685struct vki_sockaddr_in {
1686 vki_sa_family_t sin_family; /* Address family */
1687 unsigned short int sin_port; /* Port number */
1688 struct vki_in_addr sin_addr; /* Internet address */
1689
1690 /* Pad to size of `struct sockaddr'. */
1691 unsigned char __pad[__SOCK_SIZE__ - sizeof(short int) -
1692 sizeof(unsigned short int) -
1693 sizeof(struct vki_in_addr)];
1694};
1695
1696
1697static
1698Int parse_inet_addr_and_port ( UChar* str, UInt* ip_addr, UShort* port );
1699
1700static
1701Int my_socket ( Int domain, Int type, Int protocol );
1702
1703static
1704Int my_connect ( Int sockfd, struct vki_sockaddr_in* serv_addr,
1705 Int addrlen );
1706
1707static
1708UInt my_htonl ( UInt x )
1709{
1710 return
1711 (((x >> 24) & 0xFF) << 0) | (((x >> 16) & 0xFF) << 8)
1712 | (((x >> 8) & 0xFF) << 16) | (((x >> 0) & 0xFF) << 24);
1713}
1714
1715static
1716UShort my_htons ( UShort x )
1717{
1718 return
1719 (((x >> 8) & 0xFF) << 0) | (((x >> 0) & 0xFF) << 8);
1720}
1721
1722
1723/* The main function.
1724
1725 Supplied string contains either an ip address "192.168.0.1" or
1726 an ip address and port pair, "192.168.0.1:1500". Parse these,
1727 and return:
1728 -1 if there is a parse error
1729 -2 if no parse error, but specified host:port cannot be opened
1730 the relevant file (socket) descriptor, otherwise.
sewardj4f094a72002-11-05 23:37:35 +00001731 is used.
sewardj73cf3bc2002-11-03 03:20:15 +00001732*/
1733Int VG_(connect_via_socket)( UChar* str )
1734{
1735 Int sd, res;
1736 struct vki_sockaddr_in servAddr;
1737 UInt ip = 0;
sewardj4f094a72002-11-05 23:37:35 +00001738 UShort port = VG_CLO_DEFAULT_LOGPORT;
sewardj73cf3bc2002-11-03 03:20:15 +00001739 Bool ok = parse_inet_addr_and_port(str, &ip, &port);
1740 if (!ok)
1741 return -1;
1742
1743 if (0)
1744 VG_(printf)("ip = %d.%d.%d.%d, port %d\n",
1745 (ip >> 24) & 0xFF, (ip >> 16) & 0xFF,
1746 (ip >> 8) & 0xFF, ip & 0xFF,
1747 (UInt)port );
1748
1749 servAddr.sin_family = AF_INET;
1750 servAddr.sin_addr.s_addr = my_htonl(ip);
1751 servAddr.sin_port = my_htons(port);
1752
1753 /* create socket */
1754 sd = my_socket(AF_INET, SOCK_STREAM, 0 /* IPPROTO_IP ? */);
1755 if (sd < 0) {
1756 /* this shouldn't happen ... nevertheless */
1757 return -2;
1758 }
1759
1760 /* connect to server */
1761 res = my_connect(sd, (struct vki_sockaddr_in *) &servAddr,
1762 sizeof(servAddr));
1763 if (res < 0) {
1764 /* connection failed */
1765 return -2;
1766 }
1767
1768 return sd;
1769}
1770
1771
1772/* Let d = one or more digits. Accept either:
1773 d.d.d.d or d.d.d.d:d
1774*/
1775Int parse_inet_addr_and_port ( UChar* str, UInt* ip_addr, UShort* port )
1776{
1777# define GET_CH ((*str) ? (*str++) : 0)
1778 UInt ipa, i, j, c, any;
1779 ipa = 0;
1780 for (i = 0; i < 4; i++) {
1781 j = 0;
1782 any = 0;
1783 while (1) {
1784 c = GET_CH;
1785 if (c < '0' || c > '9') break;
1786 j = 10 * j + (int)(c - '0');
1787 any = 1;
1788 }
1789 if (any == 0 || j > 255) goto syntaxerr;
1790 ipa = (ipa << 8) + j;
1791 if (i <= 2 && c != '.') goto syntaxerr;
1792 }
1793 if (c == 0 || c == ':')
1794 *ip_addr = ipa;
1795 if (c == 0) goto ok;
1796 if (c != ':') goto syntaxerr;
1797 j = 0;
1798 any = 0;
1799 while (1) {
1800 c = GET_CH;
1801 if (c < '0' || c > '9') break;
1802 j = j * 10 + (int)(c - '0');
1803 any = 1;
1804 if (j > 65535) goto syntaxerr;
1805 }
1806 if (any == 0 || c != 0) goto syntaxerr;
sewardjd2220672002-11-13 19:41:41 +00001807 if (j < 1024) goto syntaxerr;
sewardj73cf3bc2002-11-03 03:20:15 +00001808 *port = (UShort)j;
1809 ok:
1810 return 1;
1811 syntaxerr:
1812 return 0;
1813# undef GET_CH
1814}
1815
1816
1817static
1818Int my_socket ( Int domain, Int type, Int protocol )
1819{
1820 Int res;
1821 UInt args[3];
1822 args[0] = domain;
1823 args[1] = type;
1824 args[2] = protocol;
jsgf855d93d2003-10-13 22:26:55 +00001825 res = VG_(do_syscall)(__NR_socketcall, SYS_SOCKET, (UInt)&args);
sewardj73cf3bc2002-11-03 03:20:15 +00001826 if (VG_(is_kerror)(res))
1827 res = -1;
1828 return res;
1829}
1830
1831static
1832Int my_connect ( Int sockfd, struct vki_sockaddr_in* serv_addr,
1833 Int addrlen )
1834{
1835 Int res;
1836 UInt args[3];
1837 args[0] = sockfd;
1838 args[1] = (UInt)serv_addr;
1839 args[2] = addrlen;
jsgf855d93d2003-10-13 22:26:55 +00001840 res = VG_(do_syscall)(__NR_socketcall, SYS_CONNECT, (UInt)&args);
sewardj73cf3bc2002-11-03 03:20:15 +00001841 if (VG_(is_kerror)(res))
1842 res = -1;
1843 return res;
1844}
1845
1846Int VG_(write_socket)( Int sd, void *msg, Int count )
1847{
1848 /* This is actually send(). */
sewardj570f8902002-11-03 11:44:36 +00001849
1850 /* Requests not to send SIGPIPE on errors on stream oriented
1851 sockets when the other end breaks the connection. The EPIPE
1852 error is still returned. */
1853 Int flags = MSG_NOSIGNAL;
1854
sewardj73cf3bc2002-11-03 03:20:15 +00001855 Int res;
1856 UInt args[4];
1857 args[0] = sd;
1858 args[1] = (UInt)msg;
1859 args[2] = count;
1860 args[3] = flags;
jsgf855d93d2003-10-13 22:26:55 +00001861 res = VG_(do_syscall)(__NR_socketcall, SYS_SEND, (UInt)&args);
sewardj73cf3bc2002-11-03 03:20:15 +00001862 if (VG_(is_kerror)(res))
1863 res = -1;
1864 return res;
1865}
1866
rjwalshf5f536f2003-11-17 17:45:00 +00001867Int VG_(getsockname) ( Int sd, struct vki_sockaddr *name, Int *namelen)
1868{
1869 Int res;
1870 UInt args[3];
1871 args[0] = sd;
1872 args[1] = (UInt)name;
1873 args[2] = (UInt)namelen;
1874 res = VG_(do_syscall)(__NR_socketcall, SYS_GETSOCKNAME, (UInt)&args);
1875 if(VG_(is_kerror)(res))
1876 res = -1;
1877 return res;
1878}
1879
1880Int VG_(getpeername) ( Int sd, struct vki_sockaddr *name, Int *namelen)
1881{
1882 Int res;
1883 UInt args[3];
1884 args[0] = sd;
1885 args[1] = (UInt)name;
1886 args[2] = (UInt)namelen;
1887 res = VG_(do_syscall)(__NR_socketcall, SYS_GETPEERNAME, (UInt)&args);
1888 if(VG_(is_kerror)(res))
1889 res = -1;
1890 return res;
1891}
1892
1893Int VG_(getsockopt) ( Int sd, Int level, Int optname, void *optval,
1894 Int *optlen)
1895{
1896 Int res;
1897 UInt args[5];
1898 args[0] = sd;
1899 args[1] = (UInt)level;
1900 args[2] = (UInt)optname;
1901 args[3] = (UInt)optval;
1902 args[4] = (UInt)optlen;
1903 res = VG_(do_syscall)(__NR_socketcall, SYS_GETSOCKOPT, (UInt)&args);
1904 if(VG_(is_kerror)(res))
1905 res = -1;
1906 return res;
1907}
1908
sewardjde4a1d02002-03-22 01:27:54 +00001909
1910/*--------------------------------------------------------------------*/
1911/*--- end vg_mylibc.c ---*/
1912/*--------------------------------------------------------------------*/