blob: 7fbf9283c188d7ce75b38eaa0b6b3fb18f329c12 [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
35
36
37/* ---------------------------------------------------------------------
38 Really Actually DO system calls.
39 ------------------------------------------------------------------ */
40
41/* Ripped off from /usr/include/asm/unistd.h. */
42
43static
44UInt vg_do_syscall0 ( UInt syscallno )
45{
46 UInt __res;
47 __asm__ volatile ("int $0x80"
48 : "=a" (__res)
49 : "0" (syscallno) );
50 return __res;
51}
52
53
54static
55UInt vg_do_syscall1 ( UInt syscallno, UInt arg1 )
56{
57 UInt __res;
58 __asm__ volatile ("int $0x80"
59 : "=a" (__res)
60 : "0" (syscallno),
61 "b" (arg1) );
62 return __res;
63}
64
65
66static
67UInt vg_do_syscall2 ( UInt syscallno,
68 UInt arg1, UInt arg2 )
69{
70 UInt __res;
71 __asm__ volatile ("int $0x80"
72 : "=a" (__res)
73 : "0" (syscallno),
74 "b" (arg1),
75 "c" (arg2) );
76 return __res;
77}
78
79
80static
81UInt vg_do_syscall3 ( UInt syscallno,
82 UInt arg1, UInt arg2, UInt arg3 )
83{
84 UInt __res;
85 __asm__ volatile ("int $0x80"
86 : "=a" (__res)
87 : "0" (syscallno),
88 "b" (arg1),
89 "c" (arg2),
90 "d" (arg3) );
91 return __res;
92}
93
94
95static
96UInt vg_do_syscall4 ( UInt syscallno,
97 UInt arg1, UInt arg2, UInt arg3, UInt arg4 )
98{
99 UInt __res;
100 __asm__ volatile ("int $0x80"
101 : "=a" (__res)
102 : "0" (syscallno),
103 "b" (arg1),
104 "c" (arg2),
105 "d" (arg3),
106 "S" (arg4) );
107 return __res;
108}
109
110
111#if 0
112static
113UInt vg_do_syscall5 ( UInt syscallno,
114 UInt arg1, UInt arg2, UInt arg3, UInt arg4,
115 UInt arg5 )
116{
117 UInt __res;
118 __asm__ volatile ("int $0x80"
119 : "=a" (__res)
120 : "0" (syscallno),
121 "b" (arg1),
122 "c" (arg2),
123 "d" (arg3),
124 "S" (arg4),
125 "D" (arg5) );
126 return __res;
127}
128#endif
129
130/* ---------------------------------------------------------------------
131 Wrappers around system calls, and other stuff, to do with signals.
132 ------------------------------------------------------------------ */
133
134/* sigemptyset, sigfullset, sigaddset and sigdelset return 0 on
135 success and -1 on error.
136*/
137Int VG_(ksigfillset)( vki_ksigset_t* set )
138{
139 Int i;
140 if (set == NULL)
141 return -1;
142 for (i = 0; i < VKI_KNSIG_WORDS; i++)
143 set->ws[i] = 0xFFFFFFFF;
144 return 0;
145}
146
147Int VG_(ksigemptyset)( vki_ksigset_t* set )
148{
149 Int i;
150 if (set == NULL)
151 return -1;
152 for (i = 0; i < VKI_KNSIG_WORDS; i++)
153 set->ws[i] = 0x0;
154 return 0;
155}
156
sewardjb48e5002002-05-13 00:16:03 +0000157Bool VG_(kisemptysigset)( vki_ksigset_t* set )
158{
159 Int i;
160 vg_assert(set != NULL);
161 for (i = 0; i < VKI_KNSIG_WORDS; i++)
162 if (set->ws[i] != 0x0) return False;
163 return True;
164}
165
sewardj018f7622002-05-15 21:13:39 +0000166Bool VG_(kisfullsigset)( vki_ksigset_t* set )
167{
168 Int i;
169 vg_assert(set != NULL);
170 for (i = 0; i < VKI_KNSIG_WORDS; i++)
sewardj05bcdcb2003-05-18 10:05:38 +0000171 if (set->ws[i] != (UInt)(~0x0)) return False;
sewardj018f7622002-05-15 21:13:39 +0000172 return True;
173}
174
175
sewardjde4a1d02002-03-22 01:27:54 +0000176Int VG_(ksigaddset)( vki_ksigset_t* set, Int signum )
177{
178 if (set == NULL)
179 return -1;
njn25e49d8e72002-09-23 09:36:25 +0000180 if (signum < 1 || signum > VKI_KNSIG)
sewardjde4a1d02002-03-22 01:27:54 +0000181 return -1;
182 signum--;
183 set->ws[signum / VKI_KNSIG_BPW] |= (1 << (signum % VKI_KNSIG_BPW));
184 return 0;
185}
186
sewardj018f7622002-05-15 21:13:39 +0000187Int VG_(ksigdelset)( vki_ksigset_t* set, Int signum )
188{
189 if (set == NULL)
190 return -1;
njn25e49d8e72002-09-23 09:36:25 +0000191 if (signum < 1 || signum > VKI_KNSIG)
sewardj018f7622002-05-15 21:13:39 +0000192 return -1;
193 signum--;
194 set->ws[signum / VKI_KNSIG_BPW] &= ~(1 << (signum % VKI_KNSIG_BPW));
195 return 0;
196}
197
sewardjde4a1d02002-03-22 01:27:54 +0000198Int VG_(ksigismember) ( vki_ksigset_t* set, Int signum )
199{
200 if (set == NULL)
sewardjb48e5002002-05-13 00:16:03 +0000201 return 0;
njn25e49d8e72002-09-23 09:36:25 +0000202 if (signum < 1 || signum > VKI_KNSIG)
sewardjb48e5002002-05-13 00:16:03 +0000203 return 0;
sewardjde4a1d02002-03-22 01:27:54 +0000204 signum--;
205 if (1 & ((set->ws[signum / VKI_KNSIG_BPW]) >> (signum % VKI_KNSIG_BPW)))
206 return 1;
207 else
208 return 0;
209}
210
211
sewardjb48e5002002-05-13 00:16:03 +0000212/* Add all signals in src to dst. */
213void VG_(ksigaddset_from_set)( vki_ksigset_t* dst, vki_ksigset_t* src )
214{
215 Int i;
216 vg_assert(dst != NULL && src != NULL);
217 for (i = 0; i < VKI_KNSIG_WORDS; i++)
218 dst->ws[i] |= src->ws[i];
219}
220
221/* Remove all signals in src from dst. */
222void VG_(ksigdelset_from_set)( vki_ksigset_t* dst, vki_ksigset_t* src )
223{
224 Int i;
225 vg_assert(dst != NULL && src != NULL);
226 for (i = 0; i < VKI_KNSIG_WORDS; i++)
227 dst->ws[i] &= ~(src->ws[i]);
228}
229
230
sewardjde4a1d02002-03-22 01:27:54 +0000231/* The functions sigaction, sigprocmask, sigpending and sigsuspend
232 return 0 on success and -1 on error.
233*/
234Int VG_(ksigprocmask)( Int how,
235 const vki_ksigset_t* set,
236 vki_ksigset_t* oldset)
237{
238 Int res
239 = vg_do_syscall4(__NR_rt_sigprocmask,
240 how, (UInt)set, (UInt)oldset,
241 VKI_KNSIG_WORDS * VKI_BYTES_PER_WORD);
242 return VG_(is_kerror)(res) ? -1 : 0;
243}
244
245
246Int VG_(ksigaction) ( Int signum,
247 const vki_ksigaction* act,
248 vki_ksigaction* oldact)
249{
250 Int res
251 = vg_do_syscall4(__NR_rt_sigaction,
252 signum, (UInt)act, (UInt)oldact,
253 VKI_KNSIG_WORDS * VKI_BYTES_PER_WORD);
sewardj018f7622002-05-15 21:13:39 +0000254 /* VG_(printf)("res = %d\n",res); */
sewardjde4a1d02002-03-22 01:27:54 +0000255 return VG_(is_kerror)(res) ? -1 : 0;
256}
257
258
259Int VG_(ksigaltstack)( const vki_kstack_t* ss, vki_kstack_t* oss )
260{
261 Int res
262 = vg_do_syscall2(__NR_sigaltstack, (UInt)ss, (UInt)oss);
263 return VG_(is_kerror)(res) ? -1 : 0;
264}
265
266
267Int VG_(ksignal)(Int signum, void (*sighandler)(Int))
268{
269 Int res;
270 vki_ksigaction sa;
271 sa.ksa_handler = sighandler;
272 sa.ksa_flags = VKI_SA_ONSTACK | VKI_SA_RESTART;
273 sa.ksa_restorer = NULL;
274 res = VG_(ksigemptyset)( &sa.ksa_mask );
275 vg_assert(res == 0);
276 res = vg_do_syscall4(__NR_rt_sigaction,
277 signum, (UInt)(&sa), (UInt)NULL,
278 VKI_KNSIG_WORDS * VKI_BYTES_PER_WORD);
279 return VG_(is_kerror)(res) ? -1 : 0;
280}
281
282
sewardjdcaf3122002-09-30 23:12:33 +0000283Int VG_(kkill)( Int pid, Int signo )
sewardj018f7622002-05-15 21:13:39 +0000284{
285 Int res = vg_do_syscall2(__NR_kill, pid, signo);
286 return VG_(is_kerror)(res) ? -1 : 0;
287}
288
289
sewardjdcaf3122002-09-30 23:12:33 +0000290Int VG_(ksigpending) ( vki_ksigset_t* set )
sewardjefbfcdf2002-06-19 17:35:45 +0000291{
292 Int res = vg_do_syscall1(__NR_sigpending, (UInt)set);
293 return VG_(is_kerror)(res) ? -1 : 0;
294}
295
296
sewardjde4a1d02002-03-22 01:27:54 +0000297/* ---------------------------------------------------------------------
sewardj2e93c502002-04-12 11:12:52 +0000298 mmap/munmap, exit, fcntl
sewardjde4a1d02002-03-22 01:27:54 +0000299 ------------------------------------------------------------------ */
300
301/* Returns -1 on failure. */
302void* VG_(mmap)( void* start, UInt length,
303 UInt prot, UInt flags, UInt fd, UInt offset)
304{
305 Int res;
306 UInt args[6];
307 args[0] = (UInt)start;
308 args[1] = length;
309 args[2] = prot;
310 args[3] = flags;
311 args[4] = fd;
312 args[5] = offset;
313 res = vg_do_syscall1(__NR_mmap, (UInt)(&(args[0])) );
314 return VG_(is_kerror)(res) ? ((void*)(-1)) : (void*)res;
315}
316
317/* Returns -1 on failure. */
318Int VG_(munmap)( void* start, Int length )
319{
320 Int res = vg_do_syscall2(__NR_munmap, (UInt)start, (UInt)length );
321 return VG_(is_kerror)(res) ? -1 : 0;
322}
323
324void VG_(exit)( Int status )
325{
326 (void)vg_do_syscall1(__NR_exit, (UInt)status );
327 /* Why are we still alive here? */
328 /*NOTREACHED*/
329 vg_assert(2+2 == 5);
330}
331
sewardj2e93c502002-04-12 11:12:52 +0000332/* Returns -1 on error. */
333Int VG_(fcntl) ( Int fd, Int cmd, Int arg )
334{
335 Int res = vg_do_syscall3(__NR_fcntl, fd, cmd, arg);
336 return VG_(is_kerror)(res) ? -1 : res;
337}
338
339/* Returns -1 on error. */
340Int VG_(select)( Int n,
341 vki_fd_set* readfds,
342 vki_fd_set* writefds,
343 vki_fd_set* exceptfds,
344 struct vki_timeval * timeout )
345{
346 Int res;
347 UInt args[5];
348 args[0] = n;
349 args[1] = (UInt)readfds;
350 args[2] = (UInt)writefds;
351 args[3] = (UInt)exceptfds;
352 args[4] = (UInt)timeout;
353 res = vg_do_syscall1(__NR_select, (UInt)(&(args[0])) );
354 return VG_(is_kerror)(res) ? -1 : res;
sewardj2e93c502002-04-12 11:12:52 +0000355}
356
sewardj5f07b662002-04-23 16:52:51 +0000357/* Returns -1 on error, 0 if ok, 1 if interrupted. */
sewardj2e93c502002-04-12 11:12:52 +0000358Int VG_(nanosleep)( const struct vki_timespec *req,
359 struct vki_timespec *rem )
360{
361 Int res;
362 res = vg_do_syscall2(__NR_nanosleep, (UInt)req, (UInt)rem);
363 if (res == -VKI_EINVAL) return -1;
sewardj5f07b662002-04-23 16:52:51 +0000364 if (res == -VKI_EINTR) return 1;
sewardj2e93c502002-04-12 11:12:52 +0000365 return 0;
366}
367
sewardjb3586202002-05-09 17:38:13 +0000368void* VG_(brk) ( void* end_data_segment )
369{
370 Int res;
371 res = vg_do_syscall1(__NR_brk, (UInt)end_data_segment);
372 return (void*)( VG_(is_kerror)(res) ? -1 : res );
373}
374
sewardj2e93c502002-04-12 11:12:52 +0000375
sewardjde4a1d02002-03-22 01:27:54 +0000376/* ---------------------------------------------------------------------
377 printf implementation. The key function, vg_vprintf(), emits chars
378 into a caller-supplied function. Distantly derived from:
379
380 vprintf replacement for Checker.
381 Copyright 1993, 1994, 1995 Tristan Gingold
382 Written September 1993 Tristan Gingold
383 Tristan Gingold, 8 rue Parmentier, F-91120 PALAISEAU, FRANCE
384
385 (Checker itself was GPL'd.)
386 ------------------------------------------------------------------ */
387
388
389/* Some flags. */
390#define VG_MSG_SIGNED 1 /* The value is signed. */
391#define VG_MSG_ZJUSTIFY 2 /* Must justify with '0'. */
392#define VG_MSG_LJUSTIFY 4 /* Must justify on the left. */
sewardj78e3cd92002-10-22 04:45:48 +0000393#define VG_MSG_PAREN 8 /* Parenthesize if present (for %y) */
sewardjde4a1d02002-03-22 01:27:54 +0000394
395/* Copy a string into the buffer. */
sewardj78e3cd92002-10-22 04:45:48 +0000396static UInt
sewardjde4a1d02002-03-22 01:27:54 +0000397myvprintf_str ( void(*send)(Char), Int flags, Int width, Char* str,
398 Bool capitalise )
399{
400# define MAYBE_TOUPPER(ch) (capitalise ? VG_(toupper)(ch) : (ch))
sewardj78e3cd92002-10-22 04:45:48 +0000401 UInt ret = 0;
sewardjde4a1d02002-03-22 01:27:54 +0000402 Int i, extra;
403 Int len = VG_(strlen)(str);
404
405 if (width == 0) {
sewardj78e3cd92002-10-22 04:45:48 +0000406 ret += len;
sewardjde4a1d02002-03-22 01:27:54 +0000407 for (i = 0; i < len; i++)
408 send(MAYBE_TOUPPER(str[i]));
sewardj78e3cd92002-10-22 04:45:48 +0000409 return ret;
sewardjde4a1d02002-03-22 01:27:54 +0000410 }
411
412 if (len > width) {
sewardj78e3cd92002-10-22 04:45:48 +0000413 ret += width;
sewardjde4a1d02002-03-22 01:27:54 +0000414 for (i = 0; i < width; i++)
415 send(MAYBE_TOUPPER(str[i]));
sewardj78e3cd92002-10-22 04:45:48 +0000416 return ret;
sewardjde4a1d02002-03-22 01:27:54 +0000417 }
418
419 extra = width - len;
420 if (flags & VG_MSG_LJUSTIFY) {
sewardj78e3cd92002-10-22 04:45:48 +0000421 ret += extra;
sewardjde4a1d02002-03-22 01:27:54 +0000422 for (i = 0; i < extra; i++)
423 send(' ');
424 }
sewardj78e3cd92002-10-22 04:45:48 +0000425 ret += len;
sewardjde4a1d02002-03-22 01:27:54 +0000426 for (i = 0; i < len; i++)
427 send(MAYBE_TOUPPER(str[i]));
428 if (!(flags & VG_MSG_LJUSTIFY)) {
sewardj78e3cd92002-10-22 04:45:48 +0000429 ret += extra;
sewardjde4a1d02002-03-22 01:27:54 +0000430 for (i = 0; i < extra; i++)
431 send(' ');
432 }
433
434# undef MAYBE_TOUPPER
sewardj78e3cd92002-10-22 04:45:48 +0000435
436 return ret;
sewardjde4a1d02002-03-22 01:27:54 +0000437}
438
439/* Write P into the buffer according to these args:
440 * If SIGN is true, p is a signed.
441 * BASE is the base.
442 * If WITH_ZERO is true, '0' must be added.
443 * WIDTH is the width of the field.
444 */
sewardj78e3cd92002-10-22 04:45:48 +0000445static UInt
sewardjde4a1d02002-03-22 01:27:54 +0000446myvprintf_int64 ( void(*send)(Char), Int flags, Int base, Int width, ULong p)
447{
448 Char buf[40];
449 Int ind = 0;
450 Int i;
451 Bool neg = False;
452 Char *digits = "0123456789ABCDEF";
sewardj78e3cd92002-10-22 04:45:48 +0000453 UInt ret = 0;
454
sewardjde4a1d02002-03-22 01:27:54 +0000455 if (base < 2 || base > 16)
sewardj78e3cd92002-10-22 04:45:48 +0000456 return ret;
sewardjde4a1d02002-03-22 01:27:54 +0000457
458 if ((flags & VG_MSG_SIGNED) && (Long)p < 0) {
459 p = - (Long)p;
460 neg = True;
461 }
462
463 if (p == 0)
464 buf[ind++] = '0';
465 else {
466 while (p > 0) {
467 buf[ind++] = digits[p % base];
468 p /= base;
469 }
470 }
471
472 if (neg)
473 buf[ind++] = '-';
474
475 if (width > 0 && !(flags & VG_MSG_LJUSTIFY)) {
476 for(; ind < width; ind++) {
477 vg_assert(ind < 39);
478 buf[ind] = (flags & VG_MSG_ZJUSTIFY) ? '0': ' ';
479 }
480 }
481
482 /* Reverse copy to buffer. */
sewardj78e3cd92002-10-22 04:45:48 +0000483 ret += ind;
sewardjde4a1d02002-03-22 01:27:54 +0000484 for (i = ind -1; i >= 0; i--)
485 send(buf[i]);
sewardjde4a1d02002-03-22 01:27:54 +0000486 if (width > 0 && (flags & VG_MSG_LJUSTIFY)) {
sewardj78e3cd92002-10-22 04:45:48 +0000487 for(; ind < width; ind++) {
488 ret++;
sewardjde4a1d02002-03-22 01:27:54 +0000489 send((flags & VG_MSG_ZJUSTIFY) ? '0': ' ');
sewardj78e3cd92002-10-22 04:45:48 +0000490 }
sewardjde4a1d02002-03-22 01:27:54 +0000491 }
sewardj78e3cd92002-10-22 04:45:48 +0000492 return ret;
sewardjde4a1d02002-03-22 01:27:54 +0000493}
494
495
496/* A simple vprintf(). */
sewardj78e3cd92002-10-22 04:45:48 +0000497UInt
sewardjde4a1d02002-03-22 01:27:54 +0000498VG_(vprintf) ( void(*send)(Char), const Char *format, va_list vargs )
499{
sewardj78e3cd92002-10-22 04:45:48 +0000500 UInt ret = 0;
sewardjde4a1d02002-03-22 01:27:54 +0000501 int i;
502 int flags;
503 int width;
504 Bool is_long;
505
506 /* We assume that vargs has already been initialised by the
507 caller, using va_start, and that the caller will similarly
508 clean up with va_end.
509 */
510
511 for (i = 0; format[i] != 0; i++) {
512 if (format[i] != '%') {
513 send(format[i]);
sewardj78e3cd92002-10-22 04:45:48 +0000514 ret++;
sewardjde4a1d02002-03-22 01:27:54 +0000515 continue;
516 }
517 i++;
518 /* A '%' has been found. Ignore a trailing %. */
519 if (format[i] == 0)
520 break;
521 if (format[i] == '%') {
522 /* `%%' is replaced by `%'. */
523 send('%');
sewardj78e3cd92002-10-22 04:45:48 +0000524 ret++;
sewardjde4a1d02002-03-22 01:27:54 +0000525 continue;
526 }
527 flags = 0;
528 is_long = False;
529 width = 0; /* length of the field. */
sewardj78e3cd92002-10-22 04:45:48 +0000530 if (format[i] == '(') {
531 flags |= VG_MSG_PAREN;
532 i++;
533 }
sewardjde4a1d02002-03-22 01:27:54 +0000534 /* If '-' follows '%', justify on the left. */
535 if (format[i] == '-') {
536 flags |= VG_MSG_LJUSTIFY;
537 i++;
538 }
539 /* If '0' follows '%', pads will be inserted. */
540 if (format[i] == '0') {
541 flags |= VG_MSG_ZJUSTIFY;
542 i++;
543 }
544 /* Compute the field length. */
545 while (format[i] >= '0' && format[i] <= '9') {
546 width *= 10;
547 width += format[i++] - '0';
548 }
549 while (format[i] == 'l') {
550 i++;
551 is_long = True;
552 }
553
554 switch (format[i]) {
555 case 'd': /* %d */
556 flags |= VG_MSG_SIGNED;
557 if (is_long)
sewardj78e3cd92002-10-22 04:45:48 +0000558 ret += myvprintf_int64(send, flags, 10, width,
559 (ULong)(va_arg (vargs, Long)));
sewardjde4a1d02002-03-22 01:27:54 +0000560 else
sewardj78e3cd92002-10-22 04:45:48 +0000561 ret += myvprintf_int64(send, flags, 10, width,
562 (ULong)(va_arg (vargs, Int)));
sewardjde4a1d02002-03-22 01:27:54 +0000563 break;
564 case 'u': /* %u */
565 if (is_long)
sewardj78e3cd92002-10-22 04:45:48 +0000566 ret += myvprintf_int64(send, flags, 10, width,
567 (ULong)(va_arg (vargs, ULong)));
sewardjde4a1d02002-03-22 01:27:54 +0000568 else
sewardj78e3cd92002-10-22 04:45:48 +0000569 ret += myvprintf_int64(send, flags, 10, width,
570 (ULong)(va_arg (vargs, UInt)));
sewardjde4a1d02002-03-22 01:27:54 +0000571 break;
572 case 'p': /* %p */
sewardj78e3cd92002-10-22 04:45:48 +0000573 ret += 2;
sewardjde4a1d02002-03-22 01:27:54 +0000574 send('0');
575 send('x');
sewardj78e3cd92002-10-22 04:45:48 +0000576 ret += myvprintf_int64(send, flags, 16, width,
577 (ULong)((UInt)va_arg (vargs, void *)));
sewardjde4a1d02002-03-22 01:27:54 +0000578 break;
579 case 'x': /* %x */
580 if (is_long)
sewardj78e3cd92002-10-22 04:45:48 +0000581 ret += myvprintf_int64(send, flags, 16, width,
582 (ULong)(va_arg (vargs, ULong)));
sewardjde4a1d02002-03-22 01:27:54 +0000583 else
sewardj78e3cd92002-10-22 04:45:48 +0000584 ret += myvprintf_int64(send, flags, 16, width,
585 (ULong)(va_arg (vargs, UInt)));
sewardjde4a1d02002-03-22 01:27:54 +0000586 break;
587 case 'c': /* %c */
sewardj78e3cd92002-10-22 04:45:48 +0000588 ret++;
sewardjde4a1d02002-03-22 01:27:54 +0000589 send(va_arg (vargs, int));
590 break;
591 case 's': case 'S': { /* %s */
592 char *str = va_arg (vargs, char *);
593 if (str == (char*) 0) str = "(null)";
sewardj78e3cd92002-10-22 04:45:48 +0000594 ret += myvprintf_str(send, flags, width, str, format[i]=='S');
sewardjde4a1d02002-03-22 01:27:54 +0000595 break;
sewardj78e3cd92002-10-22 04:45:48 +0000596 }
597 case 'y': { /* %y - print symbol */
598 Char buf[100];
599 Char *cp = buf;
600 Addr a = va_arg(vargs, Addr);
601
602 if (flags & VG_MSG_PAREN)
603 *cp++ = '(';
sewardj6e008cb2002-12-15 13:11:39 +0000604 if (VG_(get_fnname_w_offset)(a, cp, sizeof(buf)-4)) {
sewardj78e3cd92002-10-22 04:45:48 +0000605 if (flags & VG_MSG_PAREN) {
606 cp += VG_(strlen)(cp);
607 *cp++ = ')';
608 *cp = '\0';
609 }
610 ret += myvprintf_str(send, flags, width, buf, 0);
611 }
612
613 break;
614 }
sewardjde4a1d02002-03-22 01:27:54 +0000615 default:
616 break;
617 }
618 }
sewardj78e3cd92002-10-22 04:45:48 +0000619 return ret;
sewardjde4a1d02002-03-22 01:27:54 +0000620}
621
622
623/* A general replacement for printf(). Note that only low-level
624 debugging info should be sent via here. The official route is to
625 to use vg_message(). This interface is deprecated.
626*/
627static char myprintf_buf[100];
628static int n_myprintf_buf;
629
630static void add_to_myprintf_buf ( Char c )
631{
632 if (n_myprintf_buf >= 100-10 /*paranoia*/ ) {
sewardj73cf3bc2002-11-03 03:20:15 +0000633 if (VG_(clo_logfile_fd) >= 0) {
sewardj570f8902002-11-03 11:44:36 +0000634 VG_(send_bytes_to_logging_sink)(
635 myprintf_buf, VG_(strlen)(myprintf_buf) );
sewardj73cf3bc2002-11-03 03:20:15 +0000636 }
sewardjde4a1d02002-03-22 01:27:54 +0000637 n_myprintf_buf = 0;
638 myprintf_buf[n_myprintf_buf] = 0;
639 }
640 myprintf_buf[n_myprintf_buf++] = c;
641 myprintf_buf[n_myprintf_buf] = 0;
642}
643
sewardj78e3cd92002-10-22 04:45:48 +0000644UInt VG_(printf) ( const char *format, ... )
sewardjde4a1d02002-03-22 01:27:54 +0000645{
sewardj78e3cd92002-10-22 04:45:48 +0000646 UInt ret;
sewardjde4a1d02002-03-22 01:27:54 +0000647 va_list vargs;
648 va_start(vargs,format);
649
650 n_myprintf_buf = 0;
651 myprintf_buf[n_myprintf_buf] = 0;
sewardj78e3cd92002-10-22 04:45:48 +0000652 ret = VG_(vprintf) ( add_to_myprintf_buf, format, vargs );
sewardjde4a1d02002-03-22 01:27:54 +0000653
sewardj73cf3bc2002-11-03 03:20:15 +0000654 if (n_myprintf_buf > 0 && VG_(clo_logfile_fd) >= 0) {
sewardj570f8902002-11-03 11:44:36 +0000655 VG_(send_bytes_to_logging_sink)( myprintf_buf, n_myprintf_buf );
sewardj73cf3bc2002-11-03 03:20:15 +0000656 }
sewardjde4a1d02002-03-22 01:27:54 +0000657
658 va_end(vargs);
sewardj78e3cd92002-10-22 04:45:48 +0000659
660 return ret;
sewardjde4a1d02002-03-22 01:27:54 +0000661}
662
663
664/* A general replacement for sprintf(). */
sewardj78e3cd92002-10-22 04:45:48 +0000665UInt VG_(sprintf) ( Char* buf, Char *format, ... )
sewardjde4a1d02002-03-22 01:27:54 +0000666{
sewardj05bcdcb2003-05-18 10:05:38 +0000667 Int ret;
sewardjde4a1d02002-03-22 01:27:54 +0000668 va_list vargs;
sewardj1771e172002-11-13 22:06:35 +0000669 Char *ptr = buf;
670 static void add_to_vg_sprintf_buf ( Char c )
671 {
672 *ptr++ = c;
673 }
674
sewardjde4a1d02002-03-22 01:27:54 +0000675 va_start(vargs,format);
676
sewardj78e3cd92002-10-22 04:45:48 +0000677 ret = VG_(vprintf) ( add_to_vg_sprintf_buf, format, vargs );
sewardjde4a1d02002-03-22 01:27:54 +0000678 add_to_vg_sprintf_buf(0);
679
680 va_end(vargs);
sewardj78e3cd92002-10-22 04:45:48 +0000681
682 vg_assert(VG_(strlen)(buf) == ret);
683 return ret;
sewardjde4a1d02002-03-22 01:27:54 +0000684}
685
686
687/* ---------------------------------------------------------------------
688 Misc str* functions.
689 ------------------------------------------------------------------ */
690
691Bool VG_(isspace) ( Char c )
692{
693 return (c == ' ' || c == '\n' || c == '\t' || c == 0);
694}
695
njn7cf0bd32002-06-08 13:36:03 +0000696Bool VG_(isdigit) ( Char c )
697{
698 return (c >= '0' && c <= '9');
699}
sewardjde4a1d02002-03-22 01:27:54 +0000700
701Int VG_(strlen) ( const Char* str )
702{
703 Int i = 0;
704 while (str[i] != 0) i++;
705 return i;
706}
707
708
709Long VG_(atoll) ( Char* str )
710{
711 Bool neg = False;
712 Long n = 0;
713 if (*str == '-') { str++; neg = True; };
714 while (*str >= '0' && *str <= '9') {
715 n = 10*n + (Long)(*str - '0');
716 str++;
717 }
718 if (neg) n = -n;
719 return n;
720}
721
722
njn25e49d8e72002-09-23 09:36:25 +0000723Long VG_(atoll16) ( Char* str )
sewardja70ca3f2002-05-30 01:22:20 +0000724{
725 Bool neg = False;
726 Long n = 0;
727 if (*str == '-') { str++; neg = True; };
728 while (True) {
729 if (*str >= '0' && *str <= '9') {
njn25e49d8e72002-09-23 09:36:25 +0000730 n = 16*n + (Long)(*str - '0');
sewardja70ca3f2002-05-30 01:22:20 +0000731 }
732 else
njn25e49d8e72002-09-23 09:36:25 +0000733 if (*str >= 'A' && *str <= 'F') {
734 n = 16*n + (Long)((*str - 'A') + 10);
sewardja70ca3f2002-05-30 01:22:20 +0000735 }
736 else
njn25e49d8e72002-09-23 09:36:25 +0000737 if (*str >= 'a' && *str <= 'f') {
738 n = 16*n + (Long)((*str - 'a') + 10);
739 }
740 else {
741 break;
742 }
743 str++;
744 }
745 if (neg) n = -n;
746 return n;
747}
748
749Long VG_(atoll36) ( UInt base, Char* str )
750{
751 Bool neg = False;
752 Long n = 0;
753 vg_assert(base >= 2 && base <= 36);
754 if (*str == '-') { str++; neg = True; };
755 while (True) {
sewardj05bcdcb2003-05-18 10:05:38 +0000756 if (*str >= '0'
757 && *str <= (Char)('9' - (10 - base))) {
njn25e49d8e72002-09-23 09:36:25 +0000758 n = base*n + (Long)(*str - '0');
759 }
760 else
sewardj05bcdcb2003-05-18 10:05:38 +0000761 if (base > 10 && *str >= 'A'
762 && *str <= (Char)('Z' - (36 - base))) {
njn25e49d8e72002-09-23 09:36:25 +0000763 n = base*n + (Long)((*str - 'A') + 10);
764 }
765 else
sewardj05bcdcb2003-05-18 10:05:38 +0000766 if (base > 10 && *str >= 'a'
767 && *str <= (Char)('z' - (36 - base))) {
njn25e49d8e72002-09-23 09:36:25 +0000768 n = base*n + (Long)((*str - 'a') + 10);
sewardja70ca3f2002-05-30 01:22:20 +0000769 }
770 else {
771 break;
772 }
773 str++;
774 }
775 if (neg) n = -n;
776 return n;
777}
778
779
sewardjde4a1d02002-03-22 01:27:54 +0000780Char* VG_(strcat) ( Char* dest, const Char* src )
781{
782 Char* dest_orig = dest;
783 while (*dest) dest++;
784 while (*src) *dest++ = *src++;
785 *dest = 0;
786 return dest_orig;
787}
788
789
790Char* VG_(strncat) ( Char* dest, const Char* src, Int n )
791{
792 Char* dest_orig = dest;
793 while (*dest) dest++;
794 while (*src && n > 0) { *dest++ = *src++; n--; }
795 *dest = 0;
796 return dest_orig;
797}
798
799
800Char* VG_(strpbrk) ( const Char* s, const Char* accept )
801{
802 const Char* a;
803 while (*s) {
804 a = accept;
805 while (*a)
806 if (*a++ == *s)
807 return (Char *) s;
808 s++;
809 }
810 return NULL;
811}
812
813
814Char* VG_(strcpy) ( Char* dest, const Char* src )
815{
816 Char* dest_orig = dest;
817 while (*src) *dest++ = *src++;
818 *dest = 0;
819 return dest_orig;
820}
821
822
823/* Copy bytes, not overrunning the end of dest and always ensuring
824 zero termination. */
825void VG_(strncpy_safely) ( Char* dest, const Char* src, Int ndest )
826{
827 Int i;
828 vg_assert(ndest > 0);
829 i = 0;
830 dest[i] = 0;
831 while (True) {
832 if (src[i] == 0) return;
833 if (i >= ndest-1) return;
834 dest[i] = src[i];
835 i++;
836 dest[i] = 0;
837 }
838}
839
840
njn25e49d8e72002-09-23 09:36:25 +0000841Char* VG_(strncpy) ( Char* dest, const Char* src, Int ndest )
sewardjde4a1d02002-03-22 01:27:54 +0000842{
njn25e49d8e72002-09-23 09:36:25 +0000843 Int i = 0;
844 while (True) {
845 if (i >= ndest) return dest; /* reached limit */
846 dest[i] = src[i];
847 if (src[i++] == 0) {
848 /* reached NUL; pad rest with zeroes as required */
849 while (i < ndest) dest[i++] = 0;
850 return dest;
851 }
852 }
sewardjde4a1d02002-03-22 01:27:54 +0000853}
854
855
856Int VG_(strcmp) ( const Char* s1, const Char* s2 )
857{
858 while (True) {
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++;
867 }
868}
869
870
871Int VG_(strcmp_ws) ( const Char* s1, const Char* s2 )
872{
873 while (True) {
874 if (VG_(isspace)(*s1) && VG_(isspace)(*s2)) return 0;
875 if (VG_(isspace)(*s1)) return -1;
876 if (VG_(isspace)(*s2)) return 1;
877
878 if (*(UChar*)s1 < *(UChar*)s2) return -1;
879 if (*(UChar*)s1 > *(UChar*)s2) return 1;
880
881 s1++; s2++;
882 }
883}
884
885
886Int VG_(strncmp) ( const Char* s1, const Char* s2, Int nmax )
887{
888 Int n = 0;
889 while (True) {
890 if (n >= nmax) return 0;
891 if (*s1 == 0 && *s2 == 0) return 0;
892 if (*s1 == 0) return -1;
893 if (*s2 == 0) return 1;
894
895 if (*(UChar*)s1 < *(UChar*)s2) return -1;
896 if (*(UChar*)s1 > *(UChar*)s2) return 1;
897
898 s1++; s2++; n++;
899 }
900}
901
902
903Int VG_(strncmp_ws) ( const Char* s1, const Char* s2, Int nmax )
904{
905 Int n = 0;
906 while (True) {
907 if (n >= nmax) return 0;
908 if (VG_(isspace)(*s1) && VG_(isspace)(*s2)) return 0;
909 if (VG_(isspace)(*s1)) return -1;
910 if (VG_(isspace)(*s2)) return 1;
911
912 if (*(UChar*)s1 < *(UChar*)s2) return -1;
913 if (*(UChar*)s1 > *(UChar*)s2) return 1;
914
915 s1++; s2++; n++;
916 }
917}
918
919
920Char* VG_(strstr) ( const Char* haystack, Char* needle )
921{
sewardj3984b852002-05-12 03:00:17 +0000922 Int n;
923 if (haystack == NULL)
924 return NULL;
925 n = VG_(strlen)(needle);
sewardjde4a1d02002-03-22 01:27:54 +0000926 while (True) {
927 if (haystack[0] == 0)
928 return NULL;
929 if (VG_(strncmp)(haystack, needle, n) == 0)
930 return (Char*)haystack;
931 haystack++;
932 }
933}
934
935
936Char* VG_(strchr) ( const Char* s, Char c )
937{
938 while (True) {
939 if (*s == c) return (Char*)s;
940 if (*s == 0) return NULL;
941 s++;
942 }
943}
944
945
sewardj7ab2aca2002-10-20 19:40:32 +0000946void* VG_(memcpy) ( void *dest, const void *src, Int sz )
947{
948 const Char *s = (const Char *)src;
949 Char *d = (Char *)dest;
950 vg_assert(sz >= 0);
951
952 while (sz--)
953 *d++ = *s++;
954
955 return dest;
956}
957
958
959void* VG_(memset) ( void *dest, Int c, Int sz )
960{
961 Char *d = (Char *)dest;
962 vg_assert(sz >= 0);
963
964 while (sz--)
965 *d++ = c;
966
967 return dest;
968}
969
njn6d68e792003-02-24 10:49:08 +0000970Int VG_(memcmp) ( const void* s1, const void* s2, Int n )
971{
972 Int res;
njnfa4690b2003-02-24 21:43:05 +0000973 UChar a0;
974 UChar b0;
njn6d68e792003-02-24 10:49:08 +0000975 vg_assert(n >= 0);
976
977 while (n != 0) {
njnfa4690b2003-02-24 21:43:05 +0000978 a0 = ((UChar *) s1)[0];
979 b0 = ((UChar *) s2)[0];
njn6d68e792003-02-24 10:49:08 +0000980 s1 += 1;
981 s2 += 1;
982 res = a0 - b0;
983 if (res != 0)
984 return res;
985 n -= 1;
986 }
987 return 0;
988}
sewardj7ab2aca2002-10-20 19:40:32 +0000989
sewardjde4a1d02002-03-22 01:27:54 +0000990Char VG_(toupper) ( Char c )
991{
992 if (c >= 'a' && c <= 'z')
993 return c + ('A' - 'a');
994 else
995 return c;
996}
997
998
njn25e49d8e72002-09-23 09:36:25 +0000999/* Inline just for the wrapper VG_(strdup) below */
1000__inline__ Char* VG_(arena_strdup) ( ArenaId aid, const Char* s )
sewardjde4a1d02002-03-22 01:27:54 +00001001{
njn25e49d8e72002-09-23 09:36:25 +00001002 Int i;
1003 Int len = VG_(strlen)(s) + 1;
1004 Char* res = VG_(arena_malloc) (aid, len);
1005 for (i = 0; i < len; i++)
1006 res[i] = s[i];
1007 return res;
sewardjde4a1d02002-03-22 01:27:54 +00001008}
1009
njn25e49d8e72002-09-23 09:36:25 +00001010/* Wrapper to avoid exposing skins to ArenaId's */
1011Char* VG_(strdup) ( const Char* s )
1012{
1013 return VG_(arena_strdup) ( VG_AR_SKIN, s );
1014}
sewardjde4a1d02002-03-22 01:27:54 +00001015
1016/* ---------------------------------------------------------------------
1017 A simple string matching routine, purloined from Hugs98.
1018 `*' matches any sequence of zero or more characters
1019 `?' matches any single character exactly
1020 `\c' matches the character c only (ignoring special chars)
1021 c matches the character c only
1022 ------------------------------------------------------------------ */
1023
1024/* Keep track of recursion depth. */
1025static Int recDepth;
1026
njn4ba5a792002-09-30 10:23:54 +00001027static Bool string_match_wrk ( Char* pat, Char* str )
sewardjde4a1d02002-03-22 01:27:54 +00001028{
sewardje54b69d2003-07-06 01:14:42 +00001029 vg_assert(recDepth >= 0 && recDepth < 500);
sewardjde4a1d02002-03-22 01:27:54 +00001030 recDepth++;
1031 for (;;) {
1032 switch (*pat) {
1033 case '\0' : return (*str=='\0');
1034 case '*' : do {
njn4ba5a792002-09-30 10:23:54 +00001035 if (string_match_wrk(pat+1,str)) {
sewardjde4a1d02002-03-22 01:27:54 +00001036 recDepth--;
1037 return True;
1038 }
1039 } while (*str++);
1040 recDepth--;
1041 return False;
1042 case '?' : if (*str++=='\0') {
1043 recDepth--;
1044 return False;
1045 }
1046 pat++;
1047 break;
1048 case '\\' : if (*++pat == '\0') {
1049 recDepth--;
1050 return False; /* spurious trailing \ in pattern */
1051 }
1052 /* falls through to ... */
1053 default : if (*pat++ != *str++) {
1054 recDepth--;
1055 return False;
1056 }
1057 break;
1058 }
1059 }
1060}
1061
njn4ba5a792002-09-30 10:23:54 +00001062Bool VG_(string_match) ( Char* pat, Char* str )
sewardjde4a1d02002-03-22 01:27:54 +00001063{
1064 Bool b;
1065 recDepth = 0;
njn4ba5a792002-09-30 10:23:54 +00001066 b = string_match_wrk ( pat, str );
sewardjde4a1d02002-03-22 01:27:54 +00001067 /*
1068 VG_(printf)("%s %s %s\n",
1069 b?"TRUE ":"FALSE", pat, str);
1070 */
1071 return b;
1072}
1073
1074
1075/* ---------------------------------------------------------------------
1076 Assertery.
1077 ------------------------------------------------------------------ */
1078
njne427a662002-10-02 11:08:25 +00001079__attribute__ ((noreturn))
daywalker3222e0a2003-09-18 01:39:50 +00001080static void report_and_quit ( const Char* report )
njne427a662002-10-02 11:08:25 +00001081{
1082 VG_(pp_sched_status)();
sewardj366ee1e2003-04-26 22:36:42 +00001083 VG_(printf)("\n");
1084 VG_(printf)("Note: see also the FAQ.txt in the source distribution.\n");
1085 VG_(printf)("It contains workarounds to several common problems.\n");
1086 VG_(printf)("\n");
1087 VG_(printf)("If that doesn't help, please report this bug to: %s\n\n",
1088 report);
1089 VG_(printf)("In the bug report, send all the above text, the valgrind\n");
sewardj96330bd2003-04-27 23:46:21 +00001090 VG_(printf)("version, and what Linux distro you are using. Thanks.\n\n");
njne427a662002-10-02 11:08:25 +00001091 VG_(shutdown_logging)();
1092 VG_(exit)(1);
1093}
1094
1095__attribute__ ((noreturn))
daywalker3222e0a2003-09-18 01:39:50 +00001096static void assert_fail ( const Char* expr, const Char* name, const Char* report,
1097 const Char* file, Int line, const Char* fn )
sewardjde4a1d02002-03-22 01:27:54 +00001098{
sewardj018f7622002-05-15 21:13:39 +00001099 static Bool entered = False;
1100 if (entered)
1101 VG_(exit)(2);
1102 entered = True;
sewardjde4a1d02002-03-22 01:27:54 +00001103 VG_(printf)("\n%s: %s:%d (%s): Assertion `%s' failed.\n",
njne427a662002-10-02 11:08:25 +00001104 name, file, line, fn, expr );
1105 report_and_quit(report);
sewardjde4a1d02002-03-22 01:27:54 +00001106}
1107
daywalker3222e0a2003-09-18 01:39:50 +00001108void VG_(skin_assert_fail) ( const Char* expr, const Char* file, Int line, const Char* fn )
sewardjde4a1d02002-03-22 01:27:54 +00001109{
njnd04b7c62002-10-03 14:05:52 +00001110 assert_fail(expr, VG_(details).name, VG_(details).bug_reports_to,
njne427a662002-10-02 11:08:25 +00001111 file, line, fn);
1112}
1113
daywalker3222e0a2003-09-18 01:39:50 +00001114void VG_(core_assert_fail) ( const Char* expr, const Char* file, Int line, const Char* fn )
njne427a662002-10-02 11:08:25 +00001115{
1116 assert_fail(expr, "valgrind", VG_EMAIL_ADDR, file, line, fn);
1117}
1118
1119__attribute__ ((noreturn))
1120static void panic ( Char* name, Char* report, Char* str )
1121{
1122 VG_(printf)("\n%s: the `impossible' happened:\n %s\n", name, str);
sewardjde4a1d02002-03-22 01:27:54 +00001123 VG_(printf)("Basic block ctr is approximately %llu\n", VG_(bbs_done) );
njne427a662002-10-02 11:08:25 +00001124 report_and_quit(report);
sewardjde4a1d02002-03-22 01:27:54 +00001125}
1126
njne427a662002-10-02 11:08:25 +00001127void VG_(core_panic) ( Char* str )
njn25e49d8e72002-09-23 09:36:25 +00001128{
njne427a662002-10-02 11:08:25 +00001129 panic("valgrind", VG_EMAIL_ADDR, str);
njn25e49d8e72002-09-23 09:36:25 +00001130}
1131
njne427a662002-10-02 11:08:25 +00001132void VG_(skin_panic) ( Char* str )
1133{
njnd04b7c62002-10-03 14:05:52 +00001134 panic(VG_(details).name, VG_(details).bug_reports_to, str);
njne427a662002-10-02 11:08:25 +00001135}
sewardjde4a1d02002-03-22 01:27:54 +00001136
njn13f02932003-04-30 20:23:58 +00001137
sewardjde4a1d02002-03-22 01:27:54 +00001138/* ---------------------------------------------------------------------
1139 Primitive support for reading files.
1140 ------------------------------------------------------------------ */
1141
1142/* Returns -1 on failure. */
njn25e49d8e72002-09-23 09:36:25 +00001143Int VG_(open) ( const Char* pathname, Int flags, Int mode )
1144{
sewardjde4a1d02002-03-22 01:27:54 +00001145 Int fd;
sewardjde4a1d02002-03-22 01:27:54 +00001146
njn25e49d8e72002-09-23 09:36:25 +00001147 /* (old comment, not sure if it still applies NJN 2002-sep-09) */
sewardjde4a1d02002-03-22 01:27:54 +00001148 /* This gets a segmentation fault if pathname isn't a valid file.
1149 I don't know why. It seems like the call to open is getting
1150 intercepted and messed with by glibc ... */
1151 /* fd = open( pathname, O_RDONLY ); */
1152 /* ... so we go direct to the horse's mouth, which seems to work
1153 ok: */
njn25e49d8e72002-09-23 09:36:25 +00001154 fd = vg_do_syscall3(__NR_open, (UInt)pathname, flags, mode);
njn4f9c9342002-04-29 16:03:24 +00001155 /* VG_(printf)("result = %d\n", fd); */
njn13f02932003-04-30 20:23:58 +00001156 if (VG_(is_kerror)(fd)) fd = -1;
njn4f9c9342002-04-29 16:03:24 +00001157 return fd;
1158}
sewardjde4a1d02002-03-22 01:27:54 +00001159
1160void VG_(close) ( Int fd )
1161{
1162 vg_do_syscall1(__NR_close, fd);
1163}
1164
1165
1166Int VG_(read) ( Int fd, void* buf, Int count)
1167{
1168 Int res;
1169 /* res = read( fd, buf, count ); */
1170 res = vg_do_syscall3(__NR_read, fd, (UInt)buf, count);
1171 if (VG_(is_kerror)(res)) res = -1;
1172 return res;
1173}
1174
1175Int VG_(write) ( Int fd, void* buf, Int count)
1176{
1177 Int res;
1178 /* res = write( fd, buf, count ); */
1179 res = vg_do_syscall3(__NR_write, fd, (UInt)buf, count);
1180 if (VG_(is_kerror)(res)) res = -1;
1181 return res;
1182}
1183
sewardjb3586202002-05-09 17:38:13 +00001184Int VG_(stat) ( Char* file_name, struct vki_stat* buf )
1185{
1186 Int res;
1187 res = vg_do_syscall2(__NR_stat, (UInt)file_name, (UInt)buf);
njn41557122002-10-14 09:25:37 +00001188 return VG_(is_kerror)(res) ? (-1) : 0;
1189}
1190
1191Int VG_(rename) ( Char* old_name, Char* new_name )
1192{
1193 Int res;
1194 res = vg_do_syscall2(__NR_rename, (UInt)old_name, (UInt)new_name);
1195 return VG_(is_kerror)(res) ? (-1) : 0;
sewardjb3586202002-05-09 17:38:13 +00001196}
1197
njn4aca2d22002-10-04 10:29:38 +00001198Int VG_(unlink) ( Char* file_name )
1199{
1200 Int res;
1201 res = vg_do_syscall1(__NR_unlink, (UInt)file_name);
njn41557122002-10-14 09:25:37 +00001202 return VG_(is_kerror)(res) ? (-1) : 0;
njn4aca2d22002-10-04 10:29:38 +00001203}
1204
njn13f02932003-04-30 20:23:58 +00001205/* Nb: we do not allow the Linux extension which malloc()s memory for the
1206 buffer if buf==NULL, because we don't want Linux calling malloc() */
1207Char* VG_(getcwd) ( Char* buf, Int size )
1208{
1209 Int res;
1210 vg_assert(buf != NULL);
1211 res = vg_do_syscall2(__NR_getcwd, (UInt)buf, (UInt)size);
1212 return VG_(is_kerror)(res) ? ((Char*)NULL) : (Char*)res;
1213}
1214
1215
1216/* ---------------------------------------------------------------------
1217 Misc functions looking for a proper home.
1218 ------------------------------------------------------------------ */
sewardjde4a1d02002-03-22 01:27:54 +00001219
1220/* We do getenv without libc's help by snooping around in
njn25e49d8e72002-09-23 09:36:25 +00001221 VG_(client_envp) as determined at startup time. */
sewardjde4a1d02002-03-22 01:27:54 +00001222Char* VG_(getenv) ( Char* varname )
1223{
1224 Int i, n;
1225 n = VG_(strlen)(varname);
1226 for (i = 0; VG_(client_envp)[i] != NULL; i++) {
1227 Char* s = VG_(client_envp)[i];
1228 if (VG_(strncmp)(varname, s, n) == 0 && s[n] == '=') {
1229 return & s[n+1];
1230 }
1231 }
1232 return NULL;
1233}
1234
sewardj4cf05692002-10-27 20:28:29 +00001235
sewardjde4a1d02002-03-22 01:27:54 +00001236/* You'd be amazed how many places need to know the current pid. */
1237Int VG_(getpid) ( void )
1238{
1239 Int res;
1240 /* res = getpid(); */
1241 res = vg_do_syscall0(__NR_getpid);
1242 return res;
1243}
1244
sewardj4cf05692002-10-27 20:28:29 +00001245Int VG_(getppid) ( void )
1246{
1247 Int res;
1248 res = vg_do_syscall0(__NR_getppid);
1249 return res;
1250}
1251
1252
sewardje6a25242002-04-21 22:03:07 +00001253/* Return -1 if error, else 0. NOTE does not indicate return code of
1254 child! */
1255Int VG_(system) ( Char* cmd )
1256{
1257 Int pid, res;
1258 void* environ[1] = { NULL };
1259 if (cmd == NULL)
1260 return 1;
1261 pid = vg_do_syscall0(__NR_fork);
1262 if (VG_(is_kerror)(pid))
1263 return -1;
1264 if (pid == 0) {
1265 /* child */
1266 Char* argv[4];
1267 argv[0] = "/bin/sh";
1268 argv[1] = "-c";
1269 argv[2] = cmd;
1270 argv[3] = 0;
1271 (void)vg_do_syscall3(__NR_execve,
1272 (UInt)"/bin/sh", (UInt)argv, (UInt)&environ);
1273 /* If we're still alive here, execve failed. */
1274 return -1;
1275 } else {
1276 /* parent */
1277 res = vg_do_syscall3(__NR_waitpid, pid, (UInt)NULL, 0);
1278 if (VG_(is_kerror)(res)) {
1279 return -1;
1280 } else {
1281 return 0;
1282 }
1283 }
1284}
1285
1286
sewardjde4a1d02002-03-22 01:27:54 +00001287/* ---------------------------------------------------------------------
sewardj5f07b662002-04-23 16:52:51 +00001288 Support for a millisecond-granularity counter using RDTSC.
1289 ------------------------------------------------------------------ */
1290
1291static __inline__ ULong do_rdtsc_insn ( void )
1292{
1293 ULong x;
1294 __asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
1295 return x;
1296}
1297
1298/* 0 = pre-calibration, 1 = calibration, 2 = running */
1299static Int rdtsc_calibration_state = 0;
1300static ULong rdtsc_ticks_per_millisecond = 0; /* invalid value */
1301
1302static struct vki_timeval rdtsc_cal_start_timeval;
1303static struct vki_timeval rdtsc_cal_end_timeval;
1304
1305static ULong rdtsc_cal_start_raw;
1306static ULong rdtsc_cal_end_raw;
1307
1308UInt VG_(read_millisecond_timer) ( void )
1309{
1310 ULong rdtsc_now;
1311 vg_assert(rdtsc_calibration_state == 2);
1312 rdtsc_now = do_rdtsc_insn();
1313 vg_assert(rdtsc_now > rdtsc_cal_end_raw);
1314 rdtsc_now -= rdtsc_cal_end_raw;
1315 rdtsc_now /= rdtsc_ticks_per_millisecond;
1316 return (UInt)rdtsc_now;
1317}
1318
1319
1320void VG_(start_rdtsc_calibration) ( void )
1321{
1322 Int res;
1323 vg_assert(rdtsc_calibration_state == 0);
1324 rdtsc_calibration_state = 1;
1325 rdtsc_cal_start_raw = do_rdtsc_insn();
1326 res = vg_do_syscall2(__NR_gettimeofday, (UInt)&rdtsc_cal_start_timeval,
1327 (UInt)NULL);
1328 vg_assert(!VG_(is_kerror)(res));
1329}
1330
1331void VG_(end_rdtsc_calibration) ( void )
1332{
1333 Int res, loops;
1334 ULong cpu_clock_MHZ;
1335 ULong cal_clock_ticks;
1336 ULong cal_wallclock_microseconds;
1337 ULong wallclock_start_microseconds;
1338 ULong wallclock_end_microseconds;
1339 struct vki_timespec req;
1340 struct vki_timespec rem;
1341
1342 vg_assert(rdtsc_calibration_state == 1);
1343 rdtsc_calibration_state = 2;
1344
1345 /* Try and delay for 20 milliseconds, so that we can at least have
1346 some minimum level of accuracy. */
1347 req.tv_sec = 0;
1348 req.tv_nsec = 20 * 1000 * 1000;
1349 loops = 0;
1350 while (True) {
1351 res = VG_(nanosleep)(&req, &rem);
1352 vg_assert(res == 0 /*ok*/ || res == 1 /*interrupted*/);
1353 if (res == 0)
1354 break;
1355 if (rem.tv_sec == 0 && rem.tv_nsec == 0)
1356 break;
1357 req = rem;
1358 loops++;
1359 if (loops > 100)
njne427a662002-10-02 11:08:25 +00001360 VG_(core_panic)("calibration nanosleep loop failed?!");
sewardj5f07b662002-04-23 16:52:51 +00001361 }
1362
1363 /* Now read both timers, and do the Math. */
1364 rdtsc_cal_end_raw = do_rdtsc_insn();
1365 res = vg_do_syscall2(__NR_gettimeofday, (UInt)&rdtsc_cal_end_timeval,
1366 (UInt)NULL);
1367
1368 vg_assert(rdtsc_cal_end_raw > rdtsc_cal_start_raw);
1369 cal_clock_ticks = rdtsc_cal_end_raw - rdtsc_cal_start_raw;
1370
1371 wallclock_start_microseconds
1372 = (1000000ULL * (ULong)(rdtsc_cal_start_timeval.tv_sec))
1373 + (ULong)(rdtsc_cal_start_timeval.tv_usec);
1374 wallclock_end_microseconds
1375 = (1000000ULL * (ULong)(rdtsc_cal_end_timeval.tv_sec))
1376 + (ULong)(rdtsc_cal_end_timeval.tv_usec);
1377 vg_assert(wallclock_end_microseconds > wallclock_start_microseconds);
1378 cal_wallclock_microseconds
1379 = wallclock_end_microseconds - wallclock_start_microseconds;
1380
1381 /* Since we just nanoslept for 20 ms ... */
1382 vg_assert(cal_wallclock_microseconds >= 20000);
1383
1384 /* Now we know (roughly) that cal_clock_ticks on RDTSC take
1385 cal_wallclock_microseconds elapsed time. Calculate the RDTSC
1386 ticks-per-millisecond value. */
1387 if (0)
1388 VG_(printf)("%lld ticks in %lld microseconds\n",
1389 cal_clock_ticks, cal_wallclock_microseconds );
1390
1391 rdtsc_ticks_per_millisecond
1392 = cal_clock_ticks / (cal_wallclock_microseconds / 1000ULL);
1393 cpu_clock_MHZ
1394 = (1000ULL * rdtsc_ticks_per_millisecond) / 1000000ULL;
1395 if (VG_(clo_verbosity) >= 1)
1396 VG_(message)(Vg_UserMsg, "Estimated CPU clock rate is %d MHz",
1397 (UInt)cpu_clock_MHZ);
sewardj09c55fd2002-06-13 11:37:41 +00001398 if (cpu_clock_MHZ < 50 || cpu_clock_MHZ > 10000)
njne427a662002-10-02 11:08:25 +00001399 VG_(core_panic)("end_rdtsc_calibration: "
sewardj09c55fd2002-06-13 11:37:41 +00001400 "estimated CPU MHz outside range 50 .. 10000");
sewardj5f07b662002-04-23 16:52:51 +00001401 /* Paranoia about division by zero later. */
1402 vg_assert(rdtsc_ticks_per_millisecond != 0);
1403 if (0)
1404 VG_(printf)("ticks per millisecond %llu\n",
1405 rdtsc_ticks_per_millisecond);
1406}
1407
1408
1409
1410/* ---------------------------------------------------------------------
sewardjde4a1d02002-03-22 01:27:54 +00001411 Primitive support for bagging memory via mmap.
1412 ------------------------------------------------------------------ */
1413
sewardje9047952002-06-05 20:28:33 +00001414void* VG_(get_memory_from_mmap) ( Int nBytes, Char* who )
sewardjde4a1d02002-03-22 01:27:54 +00001415{
1416 static UInt tot_alloc = 0;
sewardj7c2020b2002-09-30 22:56:03 +00001417 void* p;
1418 p = VG_(mmap)( 0, nBytes,
1419 VKI_PROT_READ|VKI_PROT_WRITE|VKI_PROT_EXEC,
1420 VKI_MAP_PRIVATE|VKI_MAP_ANONYMOUS, -1, 0 );
sewardjde4a1d02002-03-22 01:27:54 +00001421 if (p != ((void*)(-1))) {
1422 tot_alloc += (UInt)nBytes;
1423 if (0)
sewardje9047952002-06-05 20:28:33 +00001424 VG_(printf)(
1425 "get_memory_from_mmap: %d tot, %d req = %p .. %p, caller %s\n",
1426 tot_alloc, nBytes, p, ((char*)p) + nBytes - 1, who );
sewardjde4a1d02002-03-22 01:27:54 +00001427 return p;
1428 }
sewardj7c2020b2002-09-30 22:56:03 +00001429
njn25e49d8e72002-09-23 09:36:25 +00001430 VG_(printf)("\n");
daywalkerb1db1232003-09-26 00:26:07 +00001431 VG_(printf)("VG_(get_memory_from_mmap): %s's request for %d bytes failed.\n",
1432 who, nBytes);
njn25e49d8e72002-09-23 09:36:25 +00001433 VG_(printf)("VG_(get_memory_from_mmap): %d bytes already allocated.\n",
1434 tot_alloc);
1435 VG_(printf)("\n");
1436 VG_(printf)("This may mean that you have run out of swap space,\n");
1437 VG_(printf)("since running programs on valgrind increases their memory\n");
1438 VG_(printf)("usage at least 3 times. You might want to use 'top'\n");
1439 VG_(printf)("to determine whether you really have run out of swap.\n");
1440 VG_(printf)("If so, you may be able to work around it by adding a\n");
1441 VG_(printf)("temporary swap file -- this is easier than finding a\n");
1442 VG_(printf)("new swap partition. Go ask your sysadmin(s) [politely!]\n");
1443 VG_(printf)("\n");
1444 VG_(printf)("VG_(get_memory_from_mmap): out of memory! Fatal! Bye!\n");
1445 VG_(printf)("\n");
1446 VG_(exit)(1);
sewardjde4a1d02002-03-22 01:27:54 +00001447}
1448
njn25e49d8e72002-09-23 09:36:25 +00001449/* ---------------------------------------------------------------------
1450 Generally useful...
1451 ------------------------------------------------------------------ */
1452
1453Int VG_(log2) ( Int x )
1454{
1455 Int i;
1456 /* Any more than 32 and we overflow anyway... */
1457 for (i = 0; i < 32; i++) {
1458 if (1 << i == x) return i;
1459 }
1460 return -1;
1461}
1462
1463
sewardj73cf3bc2002-11-03 03:20:15 +00001464/* ---------------------------------------------------------------------
1465 Gruesome hackery for connecting to a logging server over the network.
1466 This is all very Linux-kernel specific.
1467 ------------------------------------------------------------------ */
1468
1469/* Various needed constants from the kernel iface (2.4),
1470 /usr/src/linux-2.4.9-31 */
1471
1472/* kernel, ./include/linux/net.h */
1473#define SYS_SOCKET 1 /* sys_socket(2) */
1474#define SYS_CONNECT 3 /* sys_connect(2) */
1475#define SYS_SEND 9 /* sys_send(2) */
1476
1477typedef UInt __u32;
1478
1479/* Internet address. */
1480struct vki_in_addr {
1481 __u32 s_addr;
1482};
1483
1484/* kernel, include/linux/socket.h */
1485typedef unsigned short vki_sa_family_t;
1486#define AF_INET 2 /* Internet IP Protocol */
sewardj570f8902002-11-03 11:44:36 +00001487#define MSG_NOSIGNAL 0x4000 /* Do not generate SIGPIPE */
sewardj73cf3bc2002-11-03 03:20:15 +00001488
1489/* kernel, ./include/asm-i386/socket.h */
1490#define SOCK_STREAM 1 /* stream (connection) socket */
1491
1492/* kernel, /usr/src/linux-2.4.9-31/linux/include/in.h */
1493/* Structure describing an Internet (IP) socket address. */
1494#define __SOCK_SIZE__ 16 /* sizeof(struct sockaddr) */
1495struct vki_sockaddr_in {
1496 vki_sa_family_t sin_family; /* Address family */
1497 unsigned short int sin_port; /* Port number */
1498 struct vki_in_addr sin_addr; /* Internet address */
1499
1500 /* Pad to size of `struct sockaddr'. */
1501 unsigned char __pad[__SOCK_SIZE__ - sizeof(short int) -
1502 sizeof(unsigned short int) -
1503 sizeof(struct vki_in_addr)];
1504};
1505
1506
1507static
1508Int parse_inet_addr_and_port ( UChar* str, UInt* ip_addr, UShort* port );
1509
1510static
1511Int my_socket ( Int domain, Int type, Int protocol );
1512
1513static
1514Int my_connect ( Int sockfd, struct vki_sockaddr_in* serv_addr,
1515 Int addrlen );
1516
1517static
1518UInt my_htonl ( UInt x )
1519{
1520 return
1521 (((x >> 24) & 0xFF) << 0) | (((x >> 16) & 0xFF) << 8)
1522 | (((x >> 8) & 0xFF) << 16) | (((x >> 0) & 0xFF) << 24);
1523}
1524
1525static
1526UShort my_htons ( UShort x )
1527{
1528 return
1529 (((x >> 8) & 0xFF) << 0) | (((x >> 0) & 0xFF) << 8);
1530}
1531
1532
1533/* The main function.
1534
1535 Supplied string contains either an ip address "192.168.0.1" or
1536 an ip address and port pair, "192.168.0.1:1500". Parse these,
1537 and return:
1538 -1 if there is a parse error
1539 -2 if no parse error, but specified host:port cannot be opened
1540 the relevant file (socket) descriptor, otherwise.
sewardj4f094a72002-11-05 23:37:35 +00001541 is used.
sewardj73cf3bc2002-11-03 03:20:15 +00001542*/
1543Int VG_(connect_via_socket)( UChar* str )
1544{
1545 Int sd, res;
1546 struct vki_sockaddr_in servAddr;
1547 UInt ip = 0;
sewardj4f094a72002-11-05 23:37:35 +00001548 UShort port = VG_CLO_DEFAULT_LOGPORT;
sewardj73cf3bc2002-11-03 03:20:15 +00001549 Bool ok = parse_inet_addr_and_port(str, &ip, &port);
1550 if (!ok)
1551 return -1;
1552
1553 if (0)
1554 VG_(printf)("ip = %d.%d.%d.%d, port %d\n",
1555 (ip >> 24) & 0xFF, (ip >> 16) & 0xFF,
1556 (ip >> 8) & 0xFF, ip & 0xFF,
1557 (UInt)port );
1558
1559 servAddr.sin_family = AF_INET;
1560 servAddr.sin_addr.s_addr = my_htonl(ip);
1561 servAddr.sin_port = my_htons(port);
1562
1563 /* create socket */
1564 sd = my_socket(AF_INET, SOCK_STREAM, 0 /* IPPROTO_IP ? */);
1565 if (sd < 0) {
1566 /* this shouldn't happen ... nevertheless */
1567 return -2;
1568 }
1569
1570 /* connect to server */
1571 res = my_connect(sd, (struct vki_sockaddr_in *) &servAddr,
1572 sizeof(servAddr));
1573 if (res < 0) {
1574 /* connection failed */
1575 return -2;
1576 }
1577
1578 return sd;
1579}
1580
1581
1582/* Let d = one or more digits. Accept either:
1583 d.d.d.d or d.d.d.d:d
1584*/
1585Int parse_inet_addr_and_port ( UChar* str, UInt* ip_addr, UShort* port )
1586{
1587# define GET_CH ((*str) ? (*str++) : 0)
1588 UInt ipa, i, j, c, any;
1589 ipa = 0;
1590 for (i = 0; i < 4; i++) {
1591 j = 0;
1592 any = 0;
1593 while (1) {
1594 c = GET_CH;
1595 if (c < '0' || c > '9') break;
1596 j = 10 * j + (int)(c - '0');
1597 any = 1;
1598 }
1599 if (any == 0 || j > 255) goto syntaxerr;
1600 ipa = (ipa << 8) + j;
1601 if (i <= 2 && c != '.') goto syntaxerr;
1602 }
1603 if (c == 0 || c == ':')
1604 *ip_addr = ipa;
1605 if (c == 0) goto ok;
1606 if (c != ':') goto syntaxerr;
1607 j = 0;
1608 any = 0;
1609 while (1) {
1610 c = GET_CH;
1611 if (c < '0' || c > '9') break;
1612 j = j * 10 + (int)(c - '0');
1613 any = 1;
1614 if (j > 65535) goto syntaxerr;
1615 }
1616 if (any == 0 || c != 0) goto syntaxerr;
sewardjd2220672002-11-13 19:41:41 +00001617 if (j < 1024) goto syntaxerr;
sewardj73cf3bc2002-11-03 03:20:15 +00001618 *port = (UShort)j;
1619 ok:
1620 return 1;
1621 syntaxerr:
1622 return 0;
1623# undef GET_CH
1624}
1625
1626
1627static
1628Int my_socket ( Int domain, Int type, Int protocol )
1629{
1630 Int res;
1631 UInt args[3];
1632 args[0] = domain;
1633 args[1] = type;
1634 args[2] = protocol;
1635 res = vg_do_syscall2(__NR_socketcall, SYS_SOCKET, (UInt)&args);
1636 if (VG_(is_kerror)(res))
1637 res = -1;
1638 return res;
1639}
1640
1641static
1642Int my_connect ( Int sockfd, struct vki_sockaddr_in* serv_addr,
1643 Int addrlen )
1644{
1645 Int res;
1646 UInt args[3];
1647 args[0] = sockfd;
1648 args[1] = (UInt)serv_addr;
1649 args[2] = addrlen;
1650 res = vg_do_syscall2(__NR_socketcall, SYS_CONNECT, (UInt)&args);
1651 if (VG_(is_kerror)(res))
1652 res = -1;
1653 return res;
1654}
1655
1656Int VG_(write_socket)( Int sd, void *msg, Int count )
1657{
1658 /* This is actually send(). */
sewardj570f8902002-11-03 11:44:36 +00001659
1660 /* Requests not to send SIGPIPE on errors on stream oriented
1661 sockets when the other end breaks the connection. The EPIPE
1662 error is still returned. */
1663 Int flags = MSG_NOSIGNAL;
1664
sewardj73cf3bc2002-11-03 03:20:15 +00001665 Int res;
1666 UInt args[4];
1667 args[0] = sd;
1668 args[1] = (UInt)msg;
1669 args[2] = count;
1670 args[3] = flags;
1671 res = vg_do_syscall2(__NR_socketcall, SYS_SEND, (UInt)&args);
1672 if (VG_(is_kerror)(res))
1673 res = -1;
1674 return res;
1675}
1676
sewardjde4a1d02002-03-22 01:27:54 +00001677
1678/*--------------------------------------------------------------------*/
1679/*--- end vg_mylibc.c ---*/
1680/*--------------------------------------------------------------------*/