blob: 8cb776ea12aca0798d2e43657ec6f18f3a138072 [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/*
9 This file is part of Valgrind, an x86 protected-mode emulator
10 designed for debugging and profiling binaries on x86-Unixes.
11
12 Copyright (C) 2000-2002 Julian Seward
13 jseward@acm.org
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
30 The GNU General Public License is contained in the file LICENSE.
31*/
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++)
171 if (set->ws[i] != ~0x0) return False;
172 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;
180 if (signum < 1 && signum > VKI_KNSIG)
181 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;
191 if (signum < 1 && signum > VKI_KNSIG)
192 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;
sewardjde4a1d02002-03-22 01:27:54 +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
sewardj018f7622002-05-15 21:13:39 +0000283Int VG_(kill)( Int pid, Int signo )
284{
285 Int res = vg_do_syscall2(__NR_kill, pid, signo);
286 return VG_(is_kerror)(res) ? -1 : 0;
287}
288
289
sewardjde4a1d02002-03-22 01:27:54 +0000290/* ---------------------------------------------------------------------
sewardj2e93c502002-04-12 11:12:52 +0000291 mmap/munmap, exit, fcntl
sewardjde4a1d02002-03-22 01:27:54 +0000292 ------------------------------------------------------------------ */
293
294/* Returns -1 on failure. */
295void* VG_(mmap)( void* start, UInt length,
296 UInt prot, UInt flags, UInt fd, UInt offset)
297{
298 Int res;
299 UInt args[6];
300 args[0] = (UInt)start;
301 args[1] = length;
302 args[2] = prot;
303 args[3] = flags;
304 args[4] = fd;
305 args[5] = offset;
306 res = vg_do_syscall1(__NR_mmap, (UInt)(&(args[0])) );
307 return VG_(is_kerror)(res) ? ((void*)(-1)) : (void*)res;
308}
309
310/* Returns -1 on failure. */
311Int VG_(munmap)( void* start, Int length )
312{
313 Int res = vg_do_syscall2(__NR_munmap, (UInt)start, (UInt)length );
314 return VG_(is_kerror)(res) ? -1 : 0;
315}
316
317void VG_(exit)( Int status )
318{
319 (void)vg_do_syscall1(__NR_exit, (UInt)status );
320 /* Why are we still alive here? */
321 /*NOTREACHED*/
322 vg_assert(2+2 == 5);
323}
324
sewardj2e93c502002-04-12 11:12:52 +0000325/* Returns -1 on error. */
326Int VG_(fcntl) ( Int fd, Int cmd, Int arg )
327{
328 Int res = vg_do_syscall3(__NR_fcntl, fd, cmd, arg);
329 return VG_(is_kerror)(res) ? -1 : res;
330}
331
332/* Returns -1 on error. */
333Int VG_(select)( Int n,
334 vki_fd_set* readfds,
335 vki_fd_set* writefds,
336 vki_fd_set* exceptfds,
337 struct vki_timeval * timeout )
338{
339 Int res;
340 UInt args[5];
341 args[0] = n;
342 args[1] = (UInt)readfds;
343 args[2] = (UInt)writefds;
344 args[3] = (UInt)exceptfds;
345 args[4] = (UInt)timeout;
346 res = vg_do_syscall1(__NR_select, (UInt)(&(args[0])) );
347 return VG_(is_kerror)(res) ? -1 : res;
sewardj2e93c502002-04-12 11:12:52 +0000348}
349
sewardj5f07b662002-04-23 16:52:51 +0000350/* Returns -1 on error, 0 if ok, 1 if interrupted. */
sewardj2e93c502002-04-12 11:12:52 +0000351Int VG_(nanosleep)( const struct vki_timespec *req,
352 struct vki_timespec *rem )
353{
354 Int res;
355 res = vg_do_syscall2(__NR_nanosleep, (UInt)req, (UInt)rem);
356 if (res == -VKI_EINVAL) return -1;
sewardj5f07b662002-04-23 16:52:51 +0000357 if (res == -VKI_EINTR) return 1;
sewardj2e93c502002-04-12 11:12:52 +0000358 return 0;
359}
360
sewardjb3586202002-05-09 17:38:13 +0000361void* VG_(brk) ( void* end_data_segment )
362{
363 Int res;
364 res = vg_do_syscall1(__NR_brk, (UInt)end_data_segment);
365 return (void*)( VG_(is_kerror)(res) ? -1 : res );
366}
367
sewardj2e93c502002-04-12 11:12:52 +0000368
sewardjde4a1d02002-03-22 01:27:54 +0000369/* ---------------------------------------------------------------------
370 printf implementation. The key function, vg_vprintf(), emits chars
371 into a caller-supplied function. Distantly derived from:
372
373 vprintf replacement for Checker.
374 Copyright 1993, 1994, 1995 Tristan Gingold
375 Written September 1993 Tristan Gingold
376 Tristan Gingold, 8 rue Parmentier, F-91120 PALAISEAU, FRANCE
377
378 (Checker itself was GPL'd.)
379 ------------------------------------------------------------------ */
380
381
382/* Some flags. */
383#define VG_MSG_SIGNED 1 /* The value is signed. */
384#define VG_MSG_ZJUSTIFY 2 /* Must justify with '0'. */
385#define VG_MSG_LJUSTIFY 4 /* Must justify on the left. */
386
387
388/* Copy a string into the buffer. */
389static void
390myvprintf_str ( void(*send)(Char), Int flags, Int width, Char* str,
391 Bool capitalise )
392{
393# define MAYBE_TOUPPER(ch) (capitalise ? VG_(toupper)(ch) : (ch))
394
395 Int i, extra;
396 Int len = VG_(strlen)(str);
397
398 if (width == 0) {
399 for (i = 0; i < len; i++)
400 send(MAYBE_TOUPPER(str[i]));
401 return;
402 }
403
404 if (len > width) {
405 for (i = 0; i < width; i++)
406 send(MAYBE_TOUPPER(str[i]));
407 return;
408 }
409
410 extra = width - len;
411 if (flags & VG_MSG_LJUSTIFY) {
412 for (i = 0; i < extra; i++)
413 send(' ');
414 }
415 for (i = 0; i < len; i++)
416 send(MAYBE_TOUPPER(str[i]));
417 if (!(flags & VG_MSG_LJUSTIFY)) {
418 for (i = 0; i < extra; i++)
419 send(' ');
420 }
421
422# undef MAYBE_TOUPPER
423}
424
425/* Write P into the buffer according to these args:
426 * If SIGN is true, p is a signed.
427 * BASE is the base.
428 * If WITH_ZERO is true, '0' must be added.
429 * WIDTH is the width of the field.
430 */
431static void
432myvprintf_int64 ( void(*send)(Char), Int flags, Int base, Int width, ULong p)
433{
434 Char buf[40];
435 Int ind = 0;
436 Int i;
437 Bool neg = False;
438 Char *digits = "0123456789ABCDEF";
439
440 if (base < 2 || base > 16)
441 return;
442
443 if ((flags & VG_MSG_SIGNED) && (Long)p < 0) {
444 p = - (Long)p;
445 neg = True;
446 }
447
448 if (p == 0)
449 buf[ind++] = '0';
450 else {
451 while (p > 0) {
452 buf[ind++] = digits[p % base];
453 p /= base;
454 }
455 }
456
457 if (neg)
458 buf[ind++] = '-';
459
460 if (width > 0 && !(flags & VG_MSG_LJUSTIFY)) {
461 for(; ind < width; ind++) {
462 vg_assert(ind < 39);
463 buf[ind] = (flags & VG_MSG_ZJUSTIFY) ? '0': ' ';
464 }
465 }
466
467 /* Reverse copy to buffer. */
468 for (i = ind -1; i >= 0; i--)
469 send(buf[i]);
470
471 if (width > 0 && (flags & VG_MSG_LJUSTIFY)) {
472 for(; ind < width; ind++)
473 send((flags & VG_MSG_ZJUSTIFY) ? '0': ' ');
474 }
475}
476
477
478/* A simple vprintf(). */
479void
480VG_(vprintf) ( void(*send)(Char), const Char *format, va_list vargs )
481{
482 int i;
483 int flags;
484 int width;
485 Bool is_long;
486
487 /* We assume that vargs has already been initialised by the
488 caller, using va_start, and that the caller will similarly
489 clean up with va_end.
490 */
491
492 for (i = 0; format[i] != 0; i++) {
493 if (format[i] != '%') {
494 send(format[i]);
495 continue;
496 }
497 i++;
498 /* A '%' has been found. Ignore a trailing %. */
499 if (format[i] == 0)
500 break;
501 if (format[i] == '%') {
502 /* `%%' is replaced by `%'. */
503 send('%');
504 continue;
505 }
506 flags = 0;
507 is_long = False;
508 width = 0; /* length of the field. */
509 /* If '-' follows '%', justify on the left. */
510 if (format[i] == '-') {
511 flags |= VG_MSG_LJUSTIFY;
512 i++;
513 }
514 /* If '0' follows '%', pads will be inserted. */
515 if (format[i] == '0') {
516 flags |= VG_MSG_ZJUSTIFY;
517 i++;
518 }
519 /* Compute the field length. */
520 while (format[i] >= '0' && format[i] <= '9') {
521 width *= 10;
522 width += format[i++] - '0';
523 }
524 while (format[i] == 'l') {
525 i++;
526 is_long = True;
527 }
528
529 switch (format[i]) {
530 case 'd': /* %d */
531 flags |= VG_MSG_SIGNED;
532 if (is_long)
533 myvprintf_int64(send, flags, 10, width,
534 (ULong)(va_arg (vargs, Long)));
535 else
536 myvprintf_int64(send, flags, 10, width,
537 (ULong)(va_arg (vargs, Int)));
538 break;
539 case 'u': /* %u */
540 if (is_long)
541 myvprintf_int64(send, flags, 10, width,
542 (ULong)(va_arg (vargs, ULong)));
543 else
544 myvprintf_int64(send, flags, 10, width,
545 (ULong)(va_arg (vargs, UInt)));
546 break;
547 case 'p': /* %p */
548 send('0');
549 send('x');
550 myvprintf_int64(send, flags, 16, width,
551 (ULong)((UInt)va_arg (vargs, void *)));
552 break;
553 case 'x': /* %x */
554 if (is_long)
555 myvprintf_int64(send, flags, 16, width,
556 (ULong)(va_arg (vargs, ULong)));
557 else
558 myvprintf_int64(send, flags, 16, width,
559 (ULong)(va_arg (vargs, UInt)));
560 break;
561 case 'c': /* %c */
562 send(va_arg (vargs, int));
563 break;
564 case 's': case 'S': { /* %s */
565 char *str = va_arg (vargs, char *);
566 if (str == (char*) 0) str = "(null)";
567 myvprintf_str(send, flags, width, str, format[i]=='S');
568 break;
569 }
570 default:
571 break;
572 }
573 }
574}
575
576
577/* A general replacement for printf(). Note that only low-level
578 debugging info should be sent via here. The official route is to
579 to use vg_message(). This interface is deprecated.
580*/
581static char myprintf_buf[100];
582static int n_myprintf_buf;
583
584static void add_to_myprintf_buf ( Char c )
585{
586 if (n_myprintf_buf >= 100-10 /*paranoia*/ ) {
587 if (VG_(clo_logfile_fd) >= 0)
588 VG_(write)
589 (VG_(clo_logfile_fd), myprintf_buf, VG_(strlen)(myprintf_buf));
590 n_myprintf_buf = 0;
591 myprintf_buf[n_myprintf_buf] = 0;
592 }
593 myprintf_buf[n_myprintf_buf++] = c;
594 myprintf_buf[n_myprintf_buf] = 0;
595}
596
597void VG_(printf) ( const char *format, ... )
598{
599 va_list vargs;
600 va_start(vargs,format);
601
602 n_myprintf_buf = 0;
603 myprintf_buf[n_myprintf_buf] = 0;
604 VG_(vprintf) ( add_to_myprintf_buf, format, vargs );
605
606 if (n_myprintf_buf > 0 && VG_(clo_logfile_fd) >= 0)
607 VG_(write)
608 ( VG_(clo_logfile_fd), myprintf_buf, VG_(strlen)(myprintf_buf));
609
610 va_end(vargs);
611}
612
613
614/* A general replacement for sprintf(). */
615static Char* vg_sprintf_ptr;
616
617static void add_to_vg_sprintf_buf ( Char c )
618{
619 *vg_sprintf_ptr++ = c;
620}
621
622void VG_(sprintf) ( Char* buf, Char *format, ... )
623{
624 va_list vargs;
625 va_start(vargs,format);
626
627 vg_sprintf_ptr = buf;
628 VG_(vprintf) ( add_to_vg_sprintf_buf, format, vargs );
629 add_to_vg_sprintf_buf(0);
630
631 va_end(vargs);
632}
633
634
635/* ---------------------------------------------------------------------
636 Misc str* functions.
637 ------------------------------------------------------------------ */
638
639Bool VG_(isspace) ( Char c )
640{
641 return (c == ' ' || c == '\n' || c == '\t' || c == 0);
642}
643
644
645Int VG_(strlen) ( const Char* str )
646{
647 Int i = 0;
648 while (str[i] != 0) i++;
649 return i;
650}
651
652
653Long VG_(atoll) ( Char* str )
654{
655 Bool neg = False;
656 Long n = 0;
657 if (*str == '-') { str++; neg = True; };
658 while (*str >= '0' && *str <= '9') {
659 n = 10*n + (Long)(*str - '0');
660 str++;
661 }
662 if (neg) n = -n;
663 return n;
664}
665
666
667Char* VG_(strcat) ( Char* dest, const Char* src )
668{
669 Char* dest_orig = dest;
670 while (*dest) dest++;
671 while (*src) *dest++ = *src++;
672 *dest = 0;
673 return dest_orig;
674}
675
676
677Char* VG_(strncat) ( Char* dest, const Char* src, Int n )
678{
679 Char* dest_orig = dest;
680 while (*dest) dest++;
681 while (*src && n > 0) { *dest++ = *src++; n--; }
682 *dest = 0;
683 return dest_orig;
684}
685
686
687Char* VG_(strpbrk) ( const Char* s, const Char* accept )
688{
689 const Char* a;
690 while (*s) {
691 a = accept;
692 while (*a)
693 if (*a++ == *s)
694 return (Char *) s;
695 s++;
696 }
697 return NULL;
698}
699
700
701Char* VG_(strcpy) ( Char* dest, const Char* src )
702{
703 Char* dest_orig = dest;
704 while (*src) *dest++ = *src++;
705 *dest = 0;
706 return dest_orig;
707}
708
709
710/* Copy bytes, not overrunning the end of dest and always ensuring
711 zero termination. */
712void VG_(strncpy_safely) ( Char* dest, const Char* src, Int ndest )
713{
714 Int i;
715 vg_assert(ndest > 0);
716 i = 0;
717 dest[i] = 0;
718 while (True) {
719 if (src[i] == 0) return;
720 if (i >= ndest-1) return;
721 dest[i] = src[i];
722 i++;
723 dest[i] = 0;
724 }
725}
726
727
728void VG_(strncpy) ( Char* dest, const Char* src, Int ndest )
729{
730 VG_(strncpy_safely)( dest, src, ndest+1 );
731}
732
733
734Int VG_(strcmp) ( const Char* s1, const Char* s2 )
735{
736 while (True) {
737 if (*s1 == 0 && *s2 == 0) return 0;
738 if (*s1 == 0) return -1;
739 if (*s2 == 0) return 1;
740
741 if (*(UChar*)s1 < *(UChar*)s2) return -1;
742 if (*(UChar*)s1 > *(UChar*)s2) return 1;
743
744 s1++; s2++;
745 }
746}
747
748
749Int VG_(strcmp_ws) ( const Char* s1, const Char* s2 )
750{
751 while (True) {
752 if (VG_(isspace)(*s1) && VG_(isspace)(*s2)) return 0;
753 if (VG_(isspace)(*s1)) return -1;
754 if (VG_(isspace)(*s2)) return 1;
755
756 if (*(UChar*)s1 < *(UChar*)s2) return -1;
757 if (*(UChar*)s1 > *(UChar*)s2) return 1;
758
759 s1++; s2++;
760 }
761}
762
763
764Int VG_(strncmp) ( const Char* s1, const Char* s2, Int nmax )
765{
766 Int n = 0;
767 while (True) {
768 if (n >= nmax) return 0;
769 if (*s1 == 0 && *s2 == 0) return 0;
770 if (*s1 == 0) return -1;
771 if (*s2 == 0) return 1;
772
773 if (*(UChar*)s1 < *(UChar*)s2) return -1;
774 if (*(UChar*)s1 > *(UChar*)s2) return 1;
775
776 s1++; s2++; n++;
777 }
778}
779
780
781Int VG_(strncmp_ws) ( const Char* s1, const Char* s2, Int nmax )
782{
783 Int n = 0;
784 while (True) {
785 if (n >= nmax) return 0;
786 if (VG_(isspace)(*s1) && VG_(isspace)(*s2)) return 0;
787 if (VG_(isspace)(*s1)) return -1;
788 if (VG_(isspace)(*s2)) return 1;
789
790 if (*(UChar*)s1 < *(UChar*)s2) return -1;
791 if (*(UChar*)s1 > *(UChar*)s2) return 1;
792
793 s1++; s2++; n++;
794 }
795}
796
797
798Char* VG_(strstr) ( const Char* haystack, Char* needle )
799{
sewardj3984b852002-05-12 03:00:17 +0000800 Int n;
801 if (haystack == NULL)
802 return NULL;
803 n = VG_(strlen)(needle);
sewardjde4a1d02002-03-22 01:27:54 +0000804 while (True) {
805 if (haystack[0] == 0)
806 return NULL;
807 if (VG_(strncmp)(haystack, needle, n) == 0)
808 return (Char*)haystack;
809 haystack++;
810 }
811}
812
813
814Char* VG_(strchr) ( const Char* s, Char c )
815{
816 while (True) {
817 if (*s == c) return (Char*)s;
818 if (*s == 0) return NULL;
819 s++;
820 }
821}
822
823
824Char VG_(toupper) ( Char c )
825{
826 if (c >= 'a' && c <= 'z')
827 return c + ('A' - 'a');
828 else
829 return c;
830}
831
832
833Char* VG_(strdup) ( ArenaId aid, const Char* s )
834{
835 Int i;
836 Int len = VG_(strlen)(s) + 1;
837 Char* res = VG_(malloc) (aid, len);
838 for (i = 0; i < len; i++)
839 res[i] = s[i];
840 return res;
841}
842
843
844/* ---------------------------------------------------------------------
845 A simple string matching routine, purloined from Hugs98.
846 `*' matches any sequence of zero or more characters
847 `?' matches any single character exactly
848 `\c' matches the character c only (ignoring special chars)
849 c matches the character c only
850 ------------------------------------------------------------------ */
851
852/* Keep track of recursion depth. */
853static Int recDepth;
854
855static Bool stringMatch_wrk ( Char* pat, Char* str )
856{
sewardjcad5c212002-05-16 23:16:13 +0000857 vg_assert(recDepth >= 0 && recDepth < 250);
sewardjde4a1d02002-03-22 01:27:54 +0000858 recDepth++;
859 for (;;) {
860 switch (*pat) {
861 case '\0' : return (*str=='\0');
862 case '*' : do {
863 if (stringMatch_wrk(pat+1,str)) {
864 recDepth--;
865 return True;
866 }
867 } while (*str++);
868 recDepth--;
869 return False;
870 case '?' : if (*str++=='\0') {
871 recDepth--;
872 return False;
873 }
874 pat++;
875 break;
876 case '\\' : if (*++pat == '\0') {
877 recDepth--;
878 return False; /* spurious trailing \ in pattern */
879 }
880 /* falls through to ... */
881 default : if (*pat++ != *str++) {
882 recDepth--;
883 return False;
884 }
885 break;
886 }
887 }
888}
889
890Bool VG_(stringMatch) ( Char* pat, Char* str )
891{
892 Bool b;
893 recDepth = 0;
894 b = stringMatch_wrk ( pat, str );
895 /*
896 VG_(printf)("%s %s %s\n",
897 b?"TRUE ":"FALSE", pat, str);
898 */
899 return b;
900}
901
902
903/* ---------------------------------------------------------------------
904 Assertery.
905 ------------------------------------------------------------------ */
906
907#define EMAIL_ADDR "jseward@acm.org"
908
909void VG_(assert_fail) ( Char* expr, Char* file, Int line, Char* fn )
910{
sewardj018f7622002-05-15 21:13:39 +0000911 static Bool entered = False;
912 if (entered)
913 VG_(exit)(2);
914 entered = True;
sewardjde4a1d02002-03-22 01:27:54 +0000915 VG_(printf)("\n%s: %s:%d (%s): Assertion `%s' failed.\n",
916 "valgrind", file, line, fn, expr );
sewardj15a43e12002-04-17 19:35:12 +0000917 VG_(pp_sched_status)();
sewardjde4a1d02002-03-22 01:27:54 +0000918 VG_(printf)("Please report this bug to me at: %s\n\n", EMAIL_ADDR);
919 VG_(shutdown_logging)();
sewardjde4a1d02002-03-22 01:27:54 +0000920 VG_(exit)(1);
921}
922
923void VG_(panic) ( Char* str )
924{
925 VG_(printf)("\nvalgrind: the `impossible' happened:\n %s\n", str);
926 VG_(printf)("Basic block ctr is approximately %llu\n", VG_(bbs_done) );
sewardj15a43e12002-04-17 19:35:12 +0000927 VG_(pp_sched_status)();
sewardjde4a1d02002-03-22 01:27:54 +0000928 VG_(printf)("Please report this bug to me at: %s\n\n", EMAIL_ADDR);
929 VG_(shutdown_logging)();
sewardjde4a1d02002-03-22 01:27:54 +0000930 VG_(exit)(1);
931}
932
933#undef EMAIL_ADDR
934
935
936/* ---------------------------------------------------------------------
937 Primitive support for reading files.
938 ------------------------------------------------------------------ */
939
940/* Returns -1 on failure. */
941Int VG_(open_read) ( Char* pathname )
942{
943 Int fd;
944 /* VG_(printf)("vg_open_read %s\n", pathname ); */
945
946 /* This gets a segmentation fault if pathname isn't a valid file.
947 I don't know why. It seems like the call to open is getting
948 intercepted and messed with by glibc ... */
949 /* fd = open( pathname, O_RDONLY ); */
950 /* ... so we go direct to the horse's mouth, which seems to work
951 ok: */
952 const int O_RDONLY = 0; /* See /usr/include/bits/fcntl.h */
953 fd = vg_do_syscall3(__NR_open, (UInt)pathname, O_RDONLY, 0);
954 /* VG_(printf)("result = %d\n", fd); */
955 if (VG_(is_kerror)(fd)) fd = -1;
956 return fd;
957}
njn4f9c9342002-04-29 16:03:24 +0000958
959/* Returns -1 on failure. */
960static Int VG_(chmod_u_rw) ( Int fd )
961{
962 Int res;
963 const int O_IRUSR_IWUSR = 000600; /* See /usr/include/cpio.h */
964 res = vg_do_syscall2(__NR_fchmod, fd, O_IRUSR_IWUSR);
965 if (VG_(is_kerror)(res)) res = -1;
966 return res;
967}
sewardjde4a1d02002-03-22 01:27:54 +0000968
njn4f9c9342002-04-29 16:03:24 +0000969/* Returns -1 on failure. */
970Int VG_(create_and_write) ( Char* pathname )
971{
972 Int fd;
973
974 const int O_CR_AND_WR_ONLY = 0101; /* See /usr/include/bits/fcntl.h */
975 fd = vg_do_syscall3(__NR_open, (UInt)pathname, O_CR_AND_WR_ONLY, 0);
976 /* VG_(printf)("result = %d\n", fd); */
977 if (VG_(is_kerror)(fd)) {
978 fd = -1;
979 } else {
980 VG_(chmod_u_rw)(fd);
981 if (VG_(is_kerror)(fd)) {
982 fd = -1;
983 }
984 }
985 return fd;
986}
987
988/* Returns -1 on failure. */
989Int VG_(open_write) ( Char* pathname )
990{
991 Int fd;
992
993 const int O_WRONLY_AND_TRUNC = 01001; /* See /usr/include/bits/fcntl.h */
994 fd = vg_do_syscall3(__NR_open, (UInt)pathname, O_WRONLY_AND_TRUNC, 0);
995 /* VG_(printf)("result = %d\n", fd); */
996 if (VG_(is_kerror)(fd)) {
997 fd = -1;
998 }
999 return fd;
1000}
sewardjde4a1d02002-03-22 01:27:54 +00001001
1002void VG_(close) ( Int fd )
1003{
1004 vg_do_syscall1(__NR_close, fd);
1005}
1006
1007
1008Int VG_(read) ( Int fd, void* buf, Int count)
1009{
1010 Int res;
1011 /* res = read( fd, buf, count ); */
1012 res = vg_do_syscall3(__NR_read, fd, (UInt)buf, count);
1013 if (VG_(is_kerror)(res)) res = -1;
1014 return res;
1015}
1016
1017Int VG_(write) ( Int fd, void* buf, Int count)
1018{
1019 Int res;
1020 /* res = write( fd, buf, count ); */
1021 res = vg_do_syscall3(__NR_write, fd, (UInt)buf, count);
1022 if (VG_(is_kerror)(res)) res = -1;
1023 return res;
1024}
1025
sewardjb3586202002-05-09 17:38:13 +00001026Int VG_(stat) ( Char* file_name, struct vki_stat* buf )
1027{
1028 Int res;
1029 res = vg_do_syscall2(__NR_stat, (UInt)file_name, (UInt)buf);
1030 return
1031 VG_(is_kerror)(res) ? (-1) : 0;
1032}
1033
sewardjde4a1d02002-03-22 01:27:54 +00001034/* Misc functions looking for a proper home. */
1035
1036/* We do getenv without libc's help by snooping around in
1037 VG_(client_env) as determined at startup time. */
1038Char* VG_(getenv) ( Char* varname )
1039{
1040 Int i, n;
1041 n = VG_(strlen)(varname);
1042 for (i = 0; VG_(client_envp)[i] != NULL; i++) {
1043 Char* s = VG_(client_envp)[i];
1044 if (VG_(strncmp)(varname, s, n) == 0 && s[n] == '=') {
1045 return & s[n+1];
1046 }
1047 }
1048 return NULL;
1049}
1050
1051/* You'd be amazed how many places need to know the current pid. */
1052Int VG_(getpid) ( void )
1053{
1054 Int res;
1055 /* res = getpid(); */
1056 res = vg_do_syscall0(__NR_getpid);
1057 return res;
1058}
1059
sewardje6a25242002-04-21 22:03:07 +00001060/* Return -1 if error, else 0. NOTE does not indicate return code of
1061 child! */
1062Int VG_(system) ( Char* cmd )
1063{
1064 Int pid, res;
1065 void* environ[1] = { NULL };
1066 if (cmd == NULL)
1067 return 1;
1068 pid = vg_do_syscall0(__NR_fork);
1069 if (VG_(is_kerror)(pid))
1070 return -1;
1071 if (pid == 0) {
1072 /* child */
1073 Char* argv[4];
1074 argv[0] = "/bin/sh";
1075 argv[1] = "-c";
1076 argv[2] = cmd;
1077 argv[3] = 0;
1078 (void)vg_do_syscall3(__NR_execve,
1079 (UInt)"/bin/sh", (UInt)argv, (UInt)&environ);
1080 /* If we're still alive here, execve failed. */
1081 return -1;
1082 } else {
1083 /* parent */
1084 res = vg_do_syscall3(__NR_waitpid, pid, (UInt)NULL, 0);
1085 if (VG_(is_kerror)(res)) {
1086 return -1;
1087 } else {
1088 return 0;
1089 }
1090 }
1091}
1092
1093
sewardjde4a1d02002-03-22 01:27:54 +00001094/* ---------------------------------------------------------------------
sewardj5f07b662002-04-23 16:52:51 +00001095 Support for a millisecond-granularity counter using RDTSC.
1096 ------------------------------------------------------------------ */
1097
1098static __inline__ ULong do_rdtsc_insn ( void )
1099{
1100 ULong x;
1101 __asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
1102 return x;
1103}
1104
1105/* 0 = pre-calibration, 1 = calibration, 2 = running */
1106static Int rdtsc_calibration_state = 0;
1107static ULong rdtsc_ticks_per_millisecond = 0; /* invalid value */
1108
1109static struct vki_timeval rdtsc_cal_start_timeval;
1110static struct vki_timeval rdtsc_cal_end_timeval;
1111
1112static ULong rdtsc_cal_start_raw;
1113static ULong rdtsc_cal_end_raw;
1114
1115UInt VG_(read_millisecond_timer) ( void )
1116{
1117 ULong rdtsc_now;
1118 vg_assert(rdtsc_calibration_state == 2);
1119 rdtsc_now = do_rdtsc_insn();
1120 vg_assert(rdtsc_now > rdtsc_cal_end_raw);
1121 rdtsc_now -= rdtsc_cal_end_raw;
1122 rdtsc_now /= rdtsc_ticks_per_millisecond;
1123 return (UInt)rdtsc_now;
1124}
1125
1126
1127void VG_(start_rdtsc_calibration) ( void )
1128{
1129 Int res;
1130 vg_assert(rdtsc_calibration_state == 0);
1131 rdtsc_calibration_state = 1;
1132 rdtsc_cal_start_raw = do_rdtsc_insn();
1133 res = vg_do_syscall2(__NR_gettimeofday, (UInt)&rdtsc_cal_start_timeval,
1134 (UInt)NULL);
1135 vg_assert(!VG_(is_kerror)(res));
1136}
1137
1138void VG_(end_rdtsc_calibration) ( void )
1139{
1140 Int res, loops;
1141 ULong cpu_clock_MHZ;
1142 ULong cal_clock_ticks;
1143 ULong cal_wallclock_microseconds;
1144 ULong wallclock_start_microseconds;
1145 ULong wallclock_end_microseconds;
1146 struct vki_timespec req;
1147 struct vki_timespec rem;
1148
1149 vg_assert(rdtsc_calibration_state == 1);
1150 rdtsc_calibration_state = 2;
1151
1152 /* Try and delay for 20 milliseconds, so that we can at least have
1153 some minimum level of accuracy. */
1154 req.tv_sec = 0;
1155 req.tv_nsec = 20 * 1000 * 1000;
1156 loops = 0;
1157 while (True) {
1158 res = VG_(nanosleep)(&req, &rem);
1159 vg_assert(res == 0 /*ok*/ || res == 1 /*interrupted*/);
1160 if (res == 0)
1161 break;
1162 if (rem.tv_sec == 0 && rem.tv_nsec == 0)
1163 break;
1164 req = rem;
1165 loops++;
1166 if (loops > 100)
1167 VG_(panic)("calibration nanosleep loop failed?!");
1168 }
1169
1170 /* Now read both timers, and do the Math. */
1171 rdtsc_cal_end_raw = do_rdtsc_insn();
1172 res = vg_do_syscall2(__NR_gettimeofday, (UInt)&rdtsc_cal_end_timeval,
1173 (UInt)NULL);
1174
1175 vg_assert(rdtsc_cal_end_raw > rdtsc_cal_start_raw);
1176 cal_clock_ticks = rdtsc_cal_end_raw - rdtsc_cal_start_raw;
1177
1178 wallclock_start_microseconds
1179 = (1000000ULL * (ULong)(rdtsc_cal_start_timeval.tv_sec))
1180 + (ULong)(rdtsc_cal_start_timeval.tv_usec);
1181 wallclock_end_microseconds
1182 = (1000000ULL * (ULong)(rdtsc_cal_end_timeval.tv_sec))
1183 + (ULong)(rdtsc_cal_end_timeval.tv_usec);
1184 vg_assert(wallclock_end_microseconds > wallclock_start_microseconds);
1185 cal_wallclock_microseconds
1186 = wallclock_end_microseconds - wallclock_start_microseconds;
1187
1188 /* Since we just nanoslept for 20 ms ... */
1189 vg_assert(cal_wallclock_microseconds >= 20000);
1190
1191 /* Now we know (roughly) that cal_clock_ticks on RDTSC take
1192 cal_wallclock_microseconds elapsed time. Calculate the RDTSC
1193 ticks-per-millisecond value. */
1194 if (0)
1195 VG_(printf)("%lld ticks in %lld microseconds\n",
1196 cal_clock_ticks, cal_wallclock_microseconds );
1197
1198 rdtsc_ticks_per_millisecond
1199 = cal_clock_ticks / (cal_wallclock_microseconds / 1000ULL);
1200 cpu_clock_MHZ
1201 = (1000ULL * rdtsc_ticks_per_millisecond) / 1000000ULL;
1202 if (VG_(clo_verbosity) >= 1)
1203 VG_(message)(Vg_UserMsg, "Estimated CPU clock rate is %d MHz",
1204 (UInt)cpu_clock_MHZ);
1205 if (cpu_clock_MHZ < 100 || cpu_clock_MHZ > 10000)
1206 VG_(panic)("end_rdtsc_calibration: "
1207 "estimated CPU MHz outside range 100 .. 10000");
1208 /* Paranoia about division by zero later. */
1209 vg_assert(rdtsc_ticks_per_millisecond != 0);
1210 if (0)
1211 VG_(printf)("ticks per millisecond %llu\n",
1212 rdtsc_ticks_per_millisecond);
1213}
1214
1215
1216
1217/* ---------------------------------------------------------------------
sewardjde4a1d02002-03-22 01:27:54 +00001218 Primitive support for bagging memory via mmap.
1219 ------------------------------------------------------------------ */
1220
1221void* VG_(get_memory_from_mmap) ( Int nBytes )
1222{
1223 static UInt tot_alloc = 0;
1224 void* p = VG_(mmap)( 0, nBytes,
1225 VKI_PROT_READ|VKI_PROT_WRITE|VKI_PROT_EXEC,
1226 VKI_MAP_PRIVATE|VKI_MAP_ANONYMOUS, -1, 0 );
1227 if (p != ((void*)(-1))) {
1228 tot_alloc += (UInt)nBytes;
1229 if (0)
1230 VG_(printf)("get_memory_from_mmap: %d tot, %d req\n",
1231 tot_alloc, nBytes);
1232 return p;
1233 }
1234 VG_(printf)("vg_get_memory_from_mmap failed on request of %d\n",
1235 nBytes);
1236 VG_(panic)("vg_get_memory_from_mmap: out of memory! Fatal! Bye!\n");
1237}
1238
1239
1240/*--------------------------------------------------------------------*/
1241/*--- end vg_mylibc.c ---*/
1242/*--------------------------------------------------------------------*/