blob: 7264581a743a5b2679abdfdc1e10e9df7a616d56 [file] [log] [blame]
sewardjde4a1d02002-03-22 01:27:54 +00001
2/*--------------------------------------------------------------------*/
3/*--- A header file for all parts of Valgrind. ---*/
4/*--- Include no other! ---*/
5/*--- vg_include.h ---*/
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#ifndef __VG_INCLUDE_H
34#define __VG_INCLUDE_H
35
36
37#include <stdarg.h> /* ANSI varargs stuff */
38#include <setjmp.h> /* for jmp_buf */
39
40
41/* ---------------------------------------------------------------------
sewardj2d94c112002-06-03 01:25:54 +000042 Where to send bug reports to.
43 ------------------------------------------------------------------ */
44
45#define VG_EMAIL_ADDR "jseward@acm.org"
46
47
48/* ---------------------------------------------------------------------
sewardjde4a1d02002-03-22 01:27:54 +000049 Build options and table sizes. You should be able to change these
50 options or sizes, recompile, and still have a working system.
51 ------------------------------------------------------------------ */
52
53#include "vg_constants.h"
54
55
56/* Set to 1 to enable time profiling. Since this uses SIGPROF, we
57 don't want this permanently enabled -- only for profiling
58 builds. */
59#if 0
60# define VG_PROFILE
61#endif
62
63
64/* Total number of integer registers available for allocation. That's
65 all of them except %esp, %edi and %ebp. %edi is a general spare
66 temporary. %ebp permanently points at VG_(baseBlock). Note that
67 it's important that this tie in with what rankToRealRegNo() says.
68 DO NOT CHANGE THIS VALUE FROM 5. ! */
69#define VG_MAX_REALREGS 5
70
71/* Total number of spill slots available for allocation, if a TempReg
72 doesn't make it into a RealReg. Just bomb the entire system if
73 this value is too small; we don't expect it will ever get
74 particularly high. */
75#define VG_MAX_SPILLSLOTS 24
76
77
78/* Constants for the slow translation lookup cache. */
79#define VG_TRANSTAB_SLOW_BITS 11
80#define VG_TRANSTAB_SLOW_SIZE (1 << VG_TRANSTAB_SLOW_BITS)
81#define VG_TRANSTAB_SLOW_MASK ((VG_TRANSTAB_SLOW_SIZE) - 1)
82
83/* Size of a buffer used for creating messages. */
84#define M_VG_MSGBUF 10000
85
86/* Size of a smallish table used to read /proc/self/map entries. */
sewardjebc82332002-04-24 14:44:23 +000087#define M_PROCMAP_BUF 50000
sewardjde4a1d02002-03-22 01:27:54 +000088
89/* Max length of pathname to a .so/executable file. */
90#define M_VG_LIBNAMESTR 100
91
92/* Max length of a text fragment used to construct error messages. */
93#define M_VG_ERRTXT 512
94
95/* Max length of the string copied from env var VG_ARGS at startup. */
96#define M_VG_CMDLINE_STRLEN 1000
97
98/* Max number of options for Valgrind which we can handle. */
99#define M_VG_CMDLINE_OPTS 100
100
101/* After this many different unsuppressed errors have been observed,
102 be more conservative about collecting new ones. */
103#define M_VG_COLLECT_ERRORS_SLOWLY_AFTER 50
104
105/* After this many different unsuppressed errors have been observed,
106 stop collecting errors at all, and tell the user their program is
107 evidently a steaming pile of camel dung. */
sewardj1bebcbf2002-04-24 21:24:18 +0000108#define M_VG_COLLECT_NO_ERRORS_AFTER_SHOWN 300
sewardjf2537be2002-04-24 21:03:47 +0000109
110/* After this many total errors have been observed, stop collecting
111 errors at all. Counterpart to M_VG_COLLECT_NO_ERRORS_AFTER_SHOWN. */
sewardj1bebcbf2002-04-24 21:24:18 +0000112#define M_VG_COLLECT_NO_ERRORS_AFTER_FOUND 30000
sewardjde4a1d02002-03-22 01:27:54 +0000113
114/* These many bytes below %ESP are considered addressible if we're
115 doing the --workaround-gcc296-bugs hack. */
sewardjb581a132002-05-08 00:32:50 +0000116#define VG_GCC296_BUG_STACK_SLOP 1024
sewardjde4a1d02002-03-22 01:27:54 +0000117
118/* The maximum number of calls we're prepared to save in a
119 backtrace. */
120#define VG_DEEPEST_BACKTRACE 50
121
122/* Number of lists in which we keep track of malloc'd but not free'd
123 blocks. Should be prime. */
124#define VG_N_MALLOCLISTS 997
125
126/* Number of lists in which we keep track of ExeContexts. Should be
127 prime. */
128#define VG_N_EC_LISTS /*997*/ 4999
129
sewardj2e93c502002-04-12 11:12:52 +0000130/* Defines the thread-scheduling timeslice, in terms of the number of
131 basic blocks we attempt to run each thread for. Smaller values
132 give finer interleaving but much increased scheduling overheads. */
sewardj4505b9e2002-05-28 11:27:31 +0000133#define VG_SCHEDULING_QUANTUM 50000
sewardj2e93c502002-04-12 11:12:52 +0000134
135/* The maximum number of pthreads that we support. This is
136 deliberately not very high since our implementation of some of the
sewardj5f07b662002-04-23 16:52:51 +0000137 scheduler algorithms is surely O(N) in the number of threads, since
138 that's simple, at least. And (in practice) we hope that most
sewardj2e93c502002-04-12 11:12:52 +0000139 programs do not need many threads. */
sewardj8208ae82002-06-17 14:25:00 +0000140#define VG_N_THREADS 50
sewardj5f07b662002-04-23 16:52:51 +0000141
142/* Maximum number of pthread keys available. Again, we start low until
143 the need for a higher number presents itself. */
sewardj8208ae82002-06-17 14:25:00 +0000144#define VG_N_THREAD_KEYS 50
sewardj2e93c502002-04-12 11:12:52 +0000145
146/* Number of file descriptors that can simultaneously be waited on for
147 I/O to complete. Perhaps this should be the same as VG_N_THREADS
148 (surely a thread can't wait on more than one fd at once?. Who
149 knows.) */
150#define VG_N_WAITING_FDS 10
151
sewardjbf290b92002-05-01 02:28:01 +0000152/* Stack size for a thread. We try and check that they do not go
153 beyond it. */
sewardjf0b06452002-06-04 08:38:04 +0000154#define VG_PTHREAD_STACK_SIZE (1 << 20)
sewardjbf290b92002-05-01 02:28:01 +0000155
sewardj20917d82002-05-28 01:36:45 +0000156/* Number of entries in the semaphore-remapping table. */
157#define VG_N_SEMAPHORES 50
158
159/* Number of entries in the rwlock-remapping table. */
160#define VG_N_RWLOCKS 50
161
sewardj8ad94e12002-05-29 00:10:20 +0000162/* Number of entries in each thread's cleanup stack. */
163#define VG_N_CLEANUPSTACK 8
164
sewardjde4a1d02002-03-22 01:27:54 +0000165
166/* ---------------------------------------------------------------------
167 Basic types
168 ------------------------------------------------------------------ */
169
170typedef unsigned char UChar;
171typedef unsigned short UShort;
172typedef unsigned int UInt;
173typedef unsigned long long int ULong;
174
175typedef signed char Char;
176typedef signed short Short;
177typedef signed int Int;
178typedef signed long long int Long;
179
180typedef unsigned int Addr;
181
182typedef unsigned char Bool;
183#define False ((Bool)0)
184#define True ((Bool)1)
185
186#define mycat_wrk(aaa,bbb) aaa##bbb
187#define mycat(aaa,bbb) mycat_wrk(aaa,bbb)
188
189/* Just pray that gcc's constant folding works properly ... */
190#define BITS(bit7,bit6,bit5,bit4,bit3,bit2,bit1,bit0) \
191 ( ((bit7) << 7) | ((bit6) << 6) | ((bit5) << 5) | ((bit4) << 4) \
192 | ((bit3) << 3) | ((bit2) << 2) | ((bit1) << 1) | (bit0))
193
njn7cf0bd32002-06-08 13:36:03 +0000194/* For cache simulation */
195typedef struct {
196 int size; /* bytes */
197 int assoc;
198 int line_size; /* bytes */
199} cache_t;
200
201#define UNDEFINED_CACHE ((cache_t) { -1, -1, -1 })
sewardjde4a1d02002-03-22 01:27:54 +0000202
203/* ---------------------------------------------------------------------
204 Now the basic types are set up, we can haul in the kernel-interface
205 definitions.
206 ------------------------------------------------------------------ */
207
208#include "./vg_kerneliface.h"
209
210
211/* ---------------------------------------------------------------------
212 Command-line-settable options
213 ------------------------------------------------------------------ */
214
215#define VG_CLO_SMC_NONE 0
216#define VG_CLO_SMC_SOME 1
217#define VG_CLO_SMC_ALL 2
218
219#define VG_CLO_MAX_SFILES 10
220
sewardj72f98ff2002-06-13 17:23:38 +0000221/* Should we stop collecting errors if too many appear? default: YES */
sewardj2e432902002-06-13 20:44:00 +0000222extern Bool VG_(clo_error_limit);
sewardj97ced732002-03-25 00:07:36 +0000223/* Shall we V-check addrs (they are always A checked too): default: YES */
sewardj72f98ff2002-06-13 17:23:38 +0000224extern Bool VG_(clo_check_addrVs);
sewardjde4a1d02002-03-22 01:27:54 +0000225/* Enquire about whether to attach to GDB at errors? default: NO */
226extern Bool VG_(clo_GDB_attach);
227/* Sanity-check level: 0 = none, 1 (default), > 1 = expensive. */
228extern Int VG_(sanity_level);
229/* Verbosity level: 0 = silent, 1 (default), > 1 = more verbose. */
230extern Int VG_(clo_verbosity);
231/* Automatically attempt to demangle C++ names? default: YES */
232extern Bool VG_(clo_demangle);
233/* Do leak check at exit? default: NO */
234extern Bool VG_(clo_leak_check);
235/* In leak check, show reachable-but-not-freed blocks? default: NO */
236extern Bool VG_(clo_show_reachable);
237/* How closely should we compare ExeContexts in leak records? default: 2 */
238extern Int VG_(clo_leak_resolution);
239/* Round malloc sizes upwards to integral number of words? default:
240 NO */
241extern Bool VG_(clo_sloppy_malloc);
sewardj246d4662002-06-14 10:17:05 +0000242/* Minimum alignment in functions that don't specify alignment explicitly.
243 default: 0, i.e. use default of the machine (== 4) */
244extern Int VG_(clo_alignment);
sewardjde4a1d02002-03-22 01:27:54 +0000245/* Allow loads from partially-valid addresses? default: YES */
246extern Bool VG_(clo_partial_loads_ok);
247/* Simulate child processes? default: NO */
248extern Bool VG_(clo_trace_children);
249/* The file id on which we send all messages. default: 2 (stderr). */
250extern Int VG_(clo_logfile_fd);
251/* Max volume of the freed blocks queue. */
252extern Int VG_(clo_freelist_vol);
253/* Assume accesses immediately below %esp are due to gcc-2.96 bugs.
254 default: NO */
255extern Bool VG_(clo_workaround_gcc296_bugs);
256
257/* The number of suppression files specified. */
258extern Int VG_(clo_n_suppressions);
259/* The names of the suppression files. */
260extern Char* VG_(clo_suppressions)[VG_CLO_MAX_SFILES];
261
262/* Single stepping? default: NO */
263extern Bool VG_(clo_single_step);
264/* Code improvement? default: YES */
265extern Bool VG_(clo_optimise);
266/* Memory-check instrumentation? default: YES */
267extern Bool VG_(clo_instrument);
268/* DEBUG: clean up instrumented code? default: YES */
269extern Bool VG_(clo_cleanup);
njn4f9c9342002-04-29 16:03:24 +0000270/* Cache simulation instrumentation? default: NO */
271extern Bool VG_(clo_cachesim);
njn7cf0bd32002-06-08 13:36:03 +0000272/* I1 cache configuration. default: undefined */
273extern cache_t VG_(clo_I1_cache);
274/* D1 cache configuration. default: undefined */
275extern cache_t VG_(clo_D1_cache);
276/* L2 cache configuration. default: undefined */
277extern cache_t VG_(clo_L2_cache);
sewardjde4a1d02002-03-22 01:27:54 +0000278/* SMC write checks? default: SOME (1,2,4 byte movs to mem) */
279extern Int VG_(clo_smc_check);
280/* DEBUG: print system calls? default: NO */
281extern Bool VG_(clo_trace_syscalls);
282/* DEBUG: print signal details? default: NO */
283extern Bool VG_(clo_trace_signals);
284/* DEBUG: print symtab details? default: NO */
285extern Bool VG_(clo_trace_symtab);
286/* DEBUG: print malloc details? default: NO */
287extern Bool VG_(clo_trace_malloc);
sewardj8937c812002-04-12 20:12:20 +0000288/* DEBUG: print thread scheduling events? default: NO */
289extern Bool VG_(clo_trace_sched);
sewardj45b4b372002-04-16 22:50:32 +0000290/* DEBUG: print pthread (mutex etc) events? default: 0 (none), 1
291 (some), 2 (all) */
292extern Int VG_(clo_trace_pthread_level);
sewardjde4a1d02002-03-22 01:27:54 +0000293/* Stop after this many basic blocks. default: Infinity. */
294extern ULong VG_(clo_stop_after);
295/* Display gory details for the k'th most popular error. default:
296 Infinity. */
297extern Int VG_(clo_dump_error);
298/* Number of parents of a backtrace. Default: 8. */
299extern Int VG_(clo_backtrace_size);
sewardj3984b852002-05-12 03:00:17 +0000300/* Engage miscellaneous wierd hacks needed for some progs. */
sewardj8d365b52002-05-12 10:52:16 +0000301extern Char* VG_(clo_weird_hacks);
sewardjde4a1d02002-03-22 01:27:54 +0000302
303
304/* ---------------------------------------------------------------------
305 Debugging and profiling stuff
306 ------------------------------------------------------------------ */
307
308/* No, really. I _am_ that strange. */
309#define OINK(nnn) VG_(message)(Vg_DebugMsg, "OINK %d",nnn)
310
311/* Tools for building messages from multiple parts. */
312typedef
313 enum { Vg_UserMsg, Vg_DebugMsg, Vg_DebugExtraMsg }
314 VgMsgKind;
315
316extern void VG_(start_msg) ( VgMsgKind kind );
317extern void VG_(add_to_msg) ( Char* format, ... );
318extern void VG_(end_msg) ( void );
319
320/* Send a simple, single-part message. */
321extern void VG_(message) ( VgMsgKind kind, Char* format, ... );
322
323/* Create a logfile into which messages can be dumped. */
324extern void VG_(startup_logging) ( void );
325extern void VG_(shutdown_logging) ( void );
326
327
328/* Profiling stuff */
329#ifdef VG_PROFILE
330
331#define VGP_M_STACK 10
332
sewardj671ff542002-05-07 09:25:30 +0000333#define VGP_M_CCS 26 /* == the # of elems in VGP_LIST */
sewardjde4a1d02002-03-22 01:27:54 +0000334#define VGP_LIST \
sewardj671ff542002-05-07 09:25:30 +0000335 VGP_PAIR(VgpUnc=0, "unclassified"), \
336 VGP_PAIR(VgpRun, "running"), \
337 VGP_PAIR(VgpSched, "scheduler"), \
sewardjde4a1d02002-03-22 01:27:54 +0000338 VGP_PAIR(VgpMalloc, "low-lev malloc/free"), \
339 VGP_PAIR(VgpCliMalloc, "client malloc/free"), \
340 VGP_PAIR(VgpTranslate, "translate-main"), \
341 VGP_PAIR(VgpToUCode, "to-ucode"), \
342 VGP_PAIR(VgpFromUcode, "from-ucode"), \
343 VGP_PAIR(VgpImprove, "improve"), \
344 VGP_PAIR(VgpInstrument, "instrument"), \
345 VGP_PAIR(VgpCleanup, "cleanup"), \
346 VGP_PAIR(VgpRegAlloc, "reg-alloc"), \
347 VGP_PAIR(VgpDoLRU, "do-lru"), \
348 VGP_PAIR(VgpSlowFindT, "slow-search-transtab"), \
349 VGP_PAIR(VgpInitAudit, "init-mem-audit"), \
350 VGP_PAIR(VgpExeContext, "exe-context"), \
351 VGP_PAIR(VgpReadSyms, "read-syms"), \
352 VGP_PAIR(VgpAddToT, "add-to-transtab"), \
353 VGP_PAIR(VgpSARP, "set-addr-range-perms"), \
354 VGP_PAIR(VgpSyscall, "syscall wrapper"), \
njn4f9c9342002-04-29 16:03:24 +0000355 VGP_PAIR(VgpCacheInstrument, "cache instrument"), \
356 VGP_PAIR(VgpCacheGetBBCC,"cache get BBCC"), \
357 VGP_PAIR(VgpCacheSimulate, "cache simulate"), \
358 VGP_PAIR(VgpCacheDump, "cache stats dump"), \
sewardjde4a1d02002-03-22 01:27:54 +0000359 VGP_PAIR(VgpSpare1, "spare 1"), \
360 VGP_PAIR(VgpSpare2, "spare 2")
361
362#define VGP_PAIR(enumname,str) enumname
363typedef enum { VGP_LIST } VgpCC;
364#undef VGP_PAIR
365
366extern void VGP_(init_profiling) ( void );
367extern void VGP_(done_profiling) ( void );
368extern void VGP_(pushcc) ( VgpCC );
369extern void VGP_(popcc) ( void );
370
371#define VGP_PUSHCC(cc) VGP_(pushcc)(cc)
372#define VGP_POPCC VGP_(popcc)()
373
374#else
375
376#define VGP_PUSHCC(cc) /* */
377#define VGP_POPCC /* */
378
379#endif /* VG_PROFILE */
380
381
382/* ---------------------------------------------------------------------
383 Exports of vg_malloc2.c
384 ------------------------------------------------------------------ */
385
386/* Allocation arenas.
387 SYMTAB is for Valgrind's symbol table storage.
388 CLIENT is for the client's mallocs/frees.
389 DEMANGLE is for the C++ demangler.
390 EXECTXT is for storing ExeContexts.
391 ERRCTXT is for storing ErrContexts.
392 PRIVATE is for Valgrind general stuff.
393 TRANSIENT is for very short-term use. It should be empty
394 in between uses.
395 When adding a new arena, remember also to add it
396 to ensure_mm_init().
397*/
398typedef Int ArenaId;
399
400#define VG_N_ARENAS 7
401
402#define VG_AR_PRIVATE 0 /* :: ArenaId */
403#define VG_AR_SYMTAB 1 /* :: ArenaId */
404#define VG_AR_CLIENT 2 /* :: ArenaId */
405#define VG_AR_DEMANGLE 3 /* :: ArenaId */
406#define VG_AR_EXECTXT 4 /* :: ArenaId */
407#define VG_AR_ERRCTXT 5 /* :: ArenaId */
408#define VG_AR_TRANSIENT 6 /* :: ArenaId */
409
410extern void* VG_(malloc) ( ArenaId arena, Int nbytes );
411extern void VG_(free) ( ArenaId arena, void* ptr );
412extern void* VG_(calloc) ( ArenaId arena, Int nmemb, Int nbytes );
413extern void* VG_(realloc) ( ArenaId arena, void* ptr, Int size );
414extern void* VG_(malloc_aligned) ( ArenaId aid, Int req_alignB,
415 Int req_pszB );
416
417extern void VG_(mallocSanityCheckArena) ( ArenaId arena );
418extern void VG_(mallocSanityCheckAll) ( void );
419
420extern void VG_(show_all_arena_stats) ( void );
421extern Bool VG_(is_empty_arena) ( ArenaId aid );
422
423
424/* The red-zone size for the client. This can be arbitrary, but
425 unfortunately must be set at compile time. */
426#define VG_AR_CLIENT_REDZONE_SZW 4
427
428#define VG_AR_CLIENT_REDZONE_SZB \
429 (VG_AR_CLIENT_REDZONE_SZW * VKI_BYTES_PER_WORD)
430
431
432/* ---------------------------------------------------------------------
sewardj2e93c502002-04-12 11:12:52 +0000433 Exports of vg_clientfuns.c
434 ------------------------------------------------------------------ */
435
436/* This doesn't export code or data that valgrind.so needs to link
437 against. However, the scheduler does need to know the following
438 request codes. A few, publically-visible, request codes are also
439 defined in valgrind.h. */
440
441#define VG_USERREQ__MALLOC 0x2001
442#define VG_USERREQ__BUILTIN_NEW 0x2002
443#define VG_USERREQ__BUILTIN_VEC_NEW 0x2003
444
445#define VG_USERREQ__FREE 0x2004
446#define VG_USERREQ__BUILTIN_DELETE 0x2005
447#define VG_USERREQ__BUILTIN_VEC_DELETE 0x2006
448
449#define VG_USERREQ__CALLOC 0x2007
450#define VG_USERREQ__REALLOC 0x2008
451#define VG_USERREQ__MEMALIGN 0x2009
452
453
sewardj20917d82002-05-28 01:36:45 +0000454/* (Fn, Arg): Create a new thread and run Fn applied to Arg in it. Fn
455 MUST NOT return -- ever. Eventually it will do either __QUIT or
456 __WAIT_JOINER. */
457#define VG_USERREQ__APPLY_IN_NEW_THREAD 0x3001
458
459/* ( no-args ): calling thread disappears from the system forever.
460 Reclaim resources. */
461#define VG_USERREQ__QUIT 0x3002
462
463/* ( void* ): calling thread waits for joiner and returns the void* to
464 it. */
465#define VG_USERREQ__WAIT_JOINER 0x3003
466
467/* ( ThreadId, void** ): wait to join a thread. */
468#define VG_USERREQ__PTHREAD_JOIN 0x3004
469
470/* Set cancellation state and type for this thread. */
471#define VG_USERREQ__SET_CANCELSTATE 0x3005
472#define VG_USERREQ__SET_CANCELTYPE 0x3006
473
474/* ( no-args ): Test if we are at a cancellation point. */
475#define VG_USERREQ__TESTCANCEL 0x3007
476
477/* ( ThreadId, &thread_exit_wrapper is the only allowable arg ): call
478 with this arg to indicate that a cancel is now pending for the
479 specified thread. */
480#define VG_USERREQ__SET_CANCELPEND 0x3008
481
482/* Set/get detach state for this thread. */
483#define VG_USERREQ__SET_OR_GET_DETACH 0x3009
484
485#define VG_USERREQ__PTHREAD_GET_THREADID 0x300B
486#define VG_USERREQ__PTHREAD_MUTEX_LOCK 0x300C
487#define VG_USERREQ__PTHREAD_MUTEX_TRYLOCK 0x300D
488#define VG_USERREQ__PTHREAD_MUTEX_UNLOCK 0x300E
489#define VG_USERREQ__PTHREAD_COND_WAIT 0x300F
490#define VG_USERREQ__PTHREAD_COND_TIMEDWAIT 0x3010
491#define VG_USERREQ__PTHREAD_COND_SIGNAL 0x3011
492#define VG_USERREQ__PTHREAD_COND_BROADCAST 0x3012
493#define VG_USERREQ__PTHREAD_KEY_CREATE 0x3013
494#define VG_USERREQ__PTHREAD_KEY_DELETE 0x3014
495#define VG_USERREQ__PTHREAD_SETSPECIFIC 0x3015
496#define VG_USERREQ__PTHREAD_GETSPECIFIC 0x3016
497#define VG_USERREQ__READ_MILLISECOND_TIMER 0x3017
498#define VG_USERREQ__PTHREAD_SIGMASK 0x3018
499#define VG_USERREQ__SIGWAIT 0x3019
500#define VG_USERREQ__PTHREAD_KILL 0x301A
501#define VG_USERREQ__PTHREAD_YIELD 0x301B
sewardj2e93c502002-04-12 11:12:52 +0000502
sewardj8ad94e12002-05-29 00:10:20 +0000503#define VG_USERREQ__CLEANUP_PUSH 0x3020
504#define VG_USERREQ__CLEANUP_POP 0x3021
sewardj870497a2002-05-29 01:06:47 +0000505#define VG_USERREQ__GET_KEY_D_AND_S 0x3022
sewardj8ad94e12002-05-29 00:10:20 +0000506
sewardjef037c72002-05-30 00:40:03 +0000507#define VG_USERREQ__NUKE_OTHER_THREADS 0x3023
sewardjefbfcdf2002-06-19 17:35:45 +0000508
509/* Ask how many signal handler returns have happened to this
510 thread. */
sewardj9a2224b2002-06-19 10:17:40 +0000511#define VG_USERREQ__GET_N_SIGS_RETURNED 0x3024
sewardjef037c72002-05-30 00:40:03 +0000512
sewardj4dced352002-06-04 22:54:20 +0000513
sewardjefbfcdf2002-06-19 17:35:45 +0000514
515
sewardj45b4b372002-04-16 22:50:32 +0000516/* Cosmetic ... */
517#define VG_USERREQ__GET_PTHREAD_TRACE_LEVEL 0x3101
sewardj4dced352002-06-04 22:54:20 +0000518/* Log a pthread error from client-space. Cosmetic. */
519#define VG_USERREQ__PTHREAD_ERROR 0x3102
sewardj45b4b372002-04-16 22:50:32 +0000520
sewardj54cacf02002-04-12 23:24:59 +0000521/*
522In vg_constants.h:
523#define VG_USERREQ__SIGNAL_RETURNS 0x4001
sewardj54cacf02002-04-12 23:24:59 +0000524*/
525
526
sewardj2e93c502002-04-12 11:12:52 +0000527/* ---------------------------------------------------------------------
528 Constants pertaining to the simulated CPU state, VG_(baseBlock),
529 which need to go here to avoid ugly circularities.
530 ------------------------------------------------------------------ */
531
532/* How big is the saved FPU state? */
533#define VG_SIZE_OF_FPUSTATE 108
534/* ... and in words ... */
535#define VG_SIZE_OF_FPUSTATE_W ((VG_SIZE_OF_FPUSTATE+3)/4)
536
537
538/* ---------------------------------------------------------------------
539 Exports of vg_scheduler.c
540 ------------------------------------------------------------------ */
541
542/* ThreadIds are simply indices into the vg_threads[] array. */
543typedef
544 UInt
545 ThreadId;
546
sewardj6072c362002-04-19 14:40:57 +0000547/* Special magic value for an invalid ThreadId. It corresponds to
548 LinuxThreads using zero as the initial value for
549 pthread_mutex_t.__m_owner and pthread_cond_t.__c_waiting. */
550#define VG_INVALID_THREADID ((ThreadId)(0))
sewardj2e93c502002-04-12 11:12:52 +0000551
552typedef
553 enum {
554 VgTs_Empty, /* this slot is not in use */
555 VgTs_Runnable, /* waiting to be scheduled */
556 VgTs_WaitJoiner, /* waiting for someone to do join on me */
557 VgTs_WaitJoinee, /* waiting for the thread I did join on */
558 VgTs_WaitFD, /* waiting for I/O completion on a fd */
559 VgTs_WaitMX, /* waiting on a mutex */
sewardj3b5d8862002-04-20 13:53:23 +0000560 VgTs_WaitCV, /* waiting on a condition variable */
sewardjb48e5002002-05-13 00:16:03 +0000561 VgTs_WaitSIG, /* waiting due to sigwait() */
sewardj2e93c502002-04-12 11:12:52 +0000562 VgTs_Sleeping /* sleeping for a while */
563 }
564 ThreadStatus;
sewardj8ad94e12002-05-29 00:10:20 +0000565
566/* An entry in a threads's cleanup stack. */
567typedef
568 struct {
569 void (*fn)(void*);
570 void* arg;
571 }
572 CleanupEntry;
sewardj2e93c502002-04-12 11:12:52 +0000573
574typedef
575 struct {
sewardj6072c362002-04-19 14:40:57 +0000576 /* ThreadId == 0 (and hence vg_threads[0]) is NEVER USED.
577 The thread identity is simply the index in vg_threads[].
578 ThreadId == 1 is the root thread and has the special property
sewardj1e8cdc92002-04-18 11:37:52 +0000579 that we don't try and allocate or deallocate its stack. For
580 convenience of generating error message, we also put the
581 ThreadId in this tid field, but be aware that it should
sewardj604ec3c2002-04-18 22:38:41 +0000582 ALWAYS == the index in vg_threads[]. */
sewardj1e8cdc92002-04-18 11:37:52 +0000583 ThreadId tid;
sewardj2e93c502002-04-12 11:12:52 +0000584
sewardj5f07b662002-04-23 16:52:51 +0000585 /* Current scheduling status.
586
587 Complications: whenever this is set to VgTs_WaitMX, you
588 should also set .m_edx to whatever the required return value
589 is for pthread_mutex_lock / pthread_cond_timedwait for when
590 the mutex finally gets unblocked. */
sewardj2e93c502002-04-12 11:12:52 +0000591 ThreadStatus status;
592
sewardj3b5d8862002-04-20 13:53:23 +0000593 /* When .status == WaitMX, points to the mutex I am waiting for.
594 When .status == WaitCV, points to the mutex associated with
595 the condition variable indicated by the .associated_cv field.
596 In all other cases, should be NULL. */
597 void* /* pthread_mutex_t* */ associated_mx;
598
599 /* When .status == WaitCV, points to the condition variable I am
600 waiting for. In all other cases, should be NULL. */
601 void* /* pthread_cond_t* */ associated_cv;
sewardj2e93c502002-04-12 11:12:52 +0000602
sewardj5f07b662002-04-23 16:52:51 +0000603 /* If VgTs_Sleeping, this is when we should wake up, measured in
604 milliseconds as supplied by VG_(read_millisecond_counter).
605
606 If VgTs_WaitCV, this indicates the time at which
607 pthread_cond_timedwait should wake up. If == 0xFFFFFFFF,
608 this means infinitely far in the future, viz,
609 pthread_cond_wait. */
610 UInt awaken_at;
sewardj2e93c502002-04-12 11:12:52 +0000611
sewardj20917d82002-05-28 01:36:45 +0000612 /* If VgTs_WaitJoiner, return value, as generated by joinees. */
613 void* joinee_retval;
614
615 /* If VgTs_WaitJoinee, place to copy the return value to, and
616 the identity of the thread we're waiting for. */
617 void** joiner_thread_return;
618 ThreadId joiner_jee_tid;
619
sewardj8ad94e12002-05-29 00:10:20 +0000620 /* Whether or not detached. */
621 Bool detached;
622
sewardj20917d82002-05-28 01:36:45 +0000623 /* Cancelability state and type. */
624 Bool cancel_st; /* False==PTH_CANCEL_DISABLE; True==.._ENABLE */
625 Bool cancel_ty; /* False==PTH_CANC_ASYNCH; True==..._DEFERRED */
626
627 /* Pointer to fn to call to do cancellation. Indicates whether
628 or not cancellation is pending. If NULL, not pending. Else
629 should be &thread_exit_wrapper(), indicating that
630 cancallation is pending. */
631 void (*cancel_pend)(void*);
632
sewardj8ad94e12002-05-29 00:10:20 +0000633 /* The cleanup stack. */
634 Int custack_used;
635 CleanupEntry custack[VG_N_CLEANUPSTACK];
sewardj2e93c502002-04-12 11:12:52 +0000636
sewardj5f07b662002-04-23 16:52:51 +0000637 /* thread-specific data */
638 void* specifics[VG_N_THREAD_KEYS];
639
sewardjb48e5002002-05-13 00:16:03 +0000640 /* This thread's blocked-signals mask. Semantics is that for a
641 signal to be delivered to this thread, the signal must not be
642 blocked by either the process-wide signal mask nor by this
643 one. So, if this thread is prepared to handle any signal that
644 the process as a whole is prepared to handle, this mask should
645 be made empty -- and that it is its default, starting
646 state. */
647 vki_ksigset_t sig_mask;
648
649 /* When not VgTs_WaitSIG, has no meaning. When VgTs_WaitSIG,
650 is the set of signals for which we are sigwait()ing. */
651 vki_ksigset_t sigs_waited_for;
652
sewardj9a2224b2002-06-19 10:17:40 +0000653 /* Counts the number of times a signal handler for this thread
654 has returned. This makes it easy to implement pause(), by
655 polling this value, of course interspersed with nanosleeps,
656 and waiting till it changes. */
657 UInt n_signals_returned;
658
sewardj2e93c502002-04-12 11:12:52 +0000659 /* Stacks. When a thread slot is freed, we don't deallocate its
660 stack; we just leave it lying around for the next use of the
661 slot. If the next use of the slot requires a larger stack,
662 only then is the old one deallocated and a new one
663 allocated.
664
665 For the main thread (threadid == 0), this mechanism doesn't
666 apply. We don't know the size of the stack since we didn't
667 allocate it, and furthermore we never reallocate it. */
668
669 /* The allocated size of this thread's stack (permanently zero
670 if this is ThreadId == 0, since we didn't allocate its stack) */
671 UInt stack_size;
672
673 /* Address of the lowest word in this thread's stack. NULL means
674 not allocated yet.
675 */
676 Addr stack_base;
677
sewardj1e8cdc92002-04-18 11:37:52 +0000678 /* Address of the highest legitimate word in this stack. This is
679 used for error messages only -- not critical for execution
680 correctness. Is is set for all stacks, specifically including
681 ThreadId == 0 (the main thread). */
682 Addr stack_highest_word;
683
sewardj2e93c502002-04-12 11:12:52 +0000684 /* Saved machine context. */
685 UInt m_eax;
686 UInt m_ebx;
687 UInt m_ecx;
688 UInt m_edx;
689 UInt m_esi;
690 UInt m_edi;
691 UInt m_ebp;
692 UInt m_esp;
693 UInt m_eflags;
694 UInt m_eip;
695 UInt m_fpu[VG_SIZE_OF_FPUSTATE_W];
696
697 UInt sh_eax;
698 UInt sh_ebx;
699 UInt sh_ecx;
700 UInt sh_edx;
701 UInt sh_esi;
702 UInt sh_edi;
703 UInt sh_ebp;
704 UInt sh_esp;
705 UInt sh_eflags;
706 }
707 ThreadState;
708
709
sewardj018f7622002-05-15 21:13:39 +0000710/* The thread table. */
711extern ThreadState VG_(threads)[VG_N_THREADS];
712
713/* Check that tid is in range and denotes a non-Empty thread. */
sewardjb48e5002002-05-13 00:16:03 +0000714extern Bool VG_(is_valid_tid) ( ThreadId tid );
715
sewardj018f7622002-05-15 21:13:39 +0000716/* Check that tid is in range. */
717extern Bool VG_(is_valid_or_empty_tid) ( ThreadId tid );
718
sewardj2e93c502002-04-12 11:12:52 +0000719/* Copy the specified thread's state into VG_(baseBlock) in
720 preparation for running it. */
721extern void VG_(load_thread_state)( ThreadId );
722
723/* Save the specified thread's state back in VG_(baseBlock), and fill
724 VG_(baseBlock) with junk, for sanity-check reasons. */
725extern void VG_(save_thread_state)( ThreadId );
726
sewardj1e8cdc92002-04-18 11:37:52 +0000727/* And for the currently running one, if valid. */
728extern ThreadState* VG_(get_current_thread_state) ( void );
sewardj2e93c502002-04-12 11:12:52 +0000729
sewardj1e8cdc92002-04-18 11:37:52 +0000730/* Similarly ... */
731extern ThreadId VG_(get_current_tid) ( void );
732
733/* Which thread is this address in the stack of, if any? Used for
734 error message generation. */
735extern ThreadId VG_(identify_stack_addr)( Addr a );
736
sewardjccef2e62002-05-29 19:26:32 +0000737/* Nuke all threads except tid. */
738extern void VG_(nuke_all_threads_except) ( ThreadId me );
739
sewardj2e93c502002-04-12 11:12:52 +0000740
741/* Return codes from the scheduler. */
742typedef
sewardj7e87e382002-05-03 19:09:05 +0000743 enum {
744 VgSrc_Deadlock, /* no runnable threads and no prospect of any
745 even if we wait for a long time */
746 VgSrc_ExitSyscall, /* client called exit(). This is the normal
747 route out. */
748 VgSrc_BbsDone /* In a debugging run, the specified number of
749 bbs has been completed. */
750 }
sewardj2e93c502002-04-12 11:12:52 +0000751 VgSchedReturnCode;
752
sewardj7e87e382002-05-03 19:09:05 +0000753
sewardj2e93c502002-04-12 11:12:52 +0000754/* The scheduler. */
755extern VgSchedReturnCode VG_(scheduler) ( void );
756
757extern void VG_(scheduler_init) ( void );
758
sewardj15a43e12002-04-17 19:35:12 +0000759extern void VG_(pp_sched_status) ( void );
sewardj2e93c502002-04-12 11:12:52 +0000760
761/* vg_oursignalhandler() might longjmp(). Here's the jmp_buf. */
762extern jmp_buf VG_(scheduler_jmpbuf);
763/* ... and if so, here's the signal which caused it to do so. */
764extern Int VG_(longjmpd_on_signal);
765
766
sewardja1679dd2002-05-10 22:31:40 +0000767/* Possible places where the main stack might be based. We check that
768 the initial stack, which we can't move, is allocated here.
769 VG_(scheduler_init) checks this. Andrea Archelangi's 2.4 kernels
770 have been rumoured to start stacks at 0x80000000, so that too is
771 considered.
sewardj2e93c502002-04-12 11:12:52 +0000772*/
sewardja1679dd2002-05-10 22:31:40 +0000773#define VG_STARTUP_STACK_BASE_1 (Addr)0xC0000000
774#define VG_STARTUP_STACK_BASE_2 (Addr)0x80000000
775#define VG_STARTUP_STACK_SMALLERTHAN 0x100000 /* 1024k */
776
777#define VG_STACK_MATCHES_BASE(zzstack, zzbase) \
778 ( \
779 ((zzstack) & ((zzbase) - VG_STARTUP_STACK_SMALLERTHAN)) \
780 == \
781 ((zzbase) - VG_STARTUP_STACK_SMALLERTHAN) \
782 )
sewardj2e93c502002-04-12 11:12:52 +0000783
784
785/* The red-zone size which we put at the bottom (highest address) of
786 thread stacks, for paranoia reasons. This can be arbitrary, and
787 doesn't really need to be set at compile time. */
788#define VG_AR_CLIENT_STACKBASE_REDZONE_SZW 4
789
790#define VG_AR_CLIENT_STACKBASE_REDZONE_SZB \
791 (VG_AR_CLIENT_STACKBASE_REDZONE_SZW * VKI_BYTES_PER_WORD)
792
793
sewardj018f7622002-05-15 21:13:39 +0000794/* Write a value to the client's %EDX (request return value register)
795 and set the shadow to indicate it is defined. */
796#define SET_EDX(zztid, zzval) \
797 do { VG_(threads)[zztid].m_edx = (zzval); \
798 VG_(threads)[zztid].sh_edx = VGM_WORD_VALID; \
799 } while (0)
800
801#define SET_EAX(zztid, zzval) \
802 do { VG_(threads)[zztid].m_eax = (zzval); \
803 VG_(threads)[zztid].sh_eax = VGM_WORD_VALID; \
804 } while (0)
805
sewardj2e93c502002-04-12 11:12:52 +0000806
807/* ---------------------------------------------------------------------
sewardjde4a1d02002-03-22 01:27:54 +0000808 Exports of vg_signals.c
809 ------------------------------------------------------------------ */
810
sewardjde4a1d02002-03-22 01:27:54 +0000811extern void VG_(sigstartup_actions) ( void );
812
sewardjb48e5002002-05-13 00:16:03 +0000813extern Bool VG_(deliver_signals) ( void );
sewardjde4a1d02002-03-22 01:27:54 +0000814extern void VG_(unblock_host_signal) ( Int sigNo );
sewardj018f7622002-05-15 21:13:39 +0000815extern void VG_(handle_SCSS_change) ( Bool force_update );
816
sewardjde4a1d02002-03-22 01:27:54 +0000817
818/* Fake system calls for signal handling. */
sewardj2342c972002-05-22 23:34:20 +0000819extern void VG_(do__NR_sigaltstack) ( ThreadId tid );
sewardj2e93c502002-04-12 11:12:52 +0000820extern void VG_(do__NR_sigaction) ( ThreadId tid );
sewardj018f7622002-05-15 21:13:39 +0000821extern void VG_(do__NR_sigprocmask) ( ThreadId tid,
822 Int how,
823 vki_ksigset_t* set,
824 vki_ksigset_t* oldset );
825extern void VG_(do_pthread_sigmask_SCSS_upd) ( ThreadId tid,
826 Int how,
827 vki_ksigset_t* set,
828 vki_ksigset_t* oldset );
829extern void VG_(send_signal_to_thread) ( ThreadId thread,
830 Int signo );
sewardjde4a1d02002-03-22 01:27:54 +0000831
sewardjefbfcdf2002-06-19 17:35:45 +0000832extern void VG_(do_sigpending) ( ThreadId tid, vki_ksigset_t* set );
833
834
sewardj2e93c502002-04-12 11:12:52 +0000835/* Modify the current thread's state once we have detected it is
836 returning from a signal handler. */
sewardj77e466c2002-04-14 02:29:29 +0000837extern Bool VG_(signal_returns) ( ThreadId );
sewardjde4a1d02002-03-22 01:27:54 +0000838
sewardj2e93c502002-04-12 11:12:52 +0000839/* Handy utilities to block/restore all host signals. */
840extern void VG_(block_all_host_signals)
841 ( /* OUT */ vki_ksigset_t* saved_mask );
sewardj018f7622002-05-15 21:13:39 +0000842extern void VG_(restore_all_host_signals)
sewardj2e93c502002-04-12 11:12:52 +0000843 ( /* IN */ vki_ksigset_t* saved_mask );
sewardjde4a1d02002-03-22 01:27:54 +0000844
845/* ---------------------------------------------------------------------
846 Exports of vg_mylibc.c
847 ------------------------------------------------------------------ */
848
849
sewardjfbe18b92002-05-10 00:46:59 +0000850#if !defined(NULL)
851# define NULL ((void*)0)
852#endif
sewardjde4a1d02002-03-22 01:27:54 +0000853
854extern void VG_(exit)( Int status )
855 __attribute__ ((__noreturn__));
856
857extern void VG_(printf) ( const char *format, ... );
858/* too noisy ... __attribute__ ((format (printf, 1, 2))) ; */
859
860extern void VG_(sprintf) ( Char* buf, Char *format, ... );
861
862extern void VG_(vprintf) ( void(*send)(Char),
863 const Char *format, va_list vargs );
864
865extern Bool VG_(isspace) ( Char c );
njn7cf0bd32002-06-08 13:36:03 +0000866extern Bool VG_(isdigit) ( Char c );
sewardjde4a1d02002-03-22 01:27:54 +0000867
868extern Int VG_(strlen) ( const Char* str );
869
870extern Long VG_(atoll) ( Char* str );
sewardja70ca3f2002-05-30 01:22:20 +0000871extern Long VG_(atoll36) ( Char* str );
sewardjde4a1d02002-03-22 01:27:54 +0000872
873extern Char* VG_(strcat) ( Char* dest, const Char* src );
874extern Char* VG_(strncat) ( Char* dest, const Char* src, Int n );
875extern Char* VG_(strpbrk) ( const Char* s, const Char* accept );
876
877extern Char* VG_(strcpy) ( Char* dest, const Char* src );
878
879extern Int VG_(strcmp) ( const Char* s1, const Char* s2 );
880extern Int VG_(strcmp_ws) ( const Char* s1, const Char* s2 );
881
882extern Int VG_(strncmp) ( const Char* s1, const Char* s2, Int nmax );
883extern Int VG_(strncmp_ws) ( const Char* s1, const Char* s2, Int nmax );
884
885extern Char* VG_(strstr) ( const Char* haystack, Char* needle );
886extern Char* VG_(strchr) ( const Char* s, Char c );
887extern Char* VG_(strdup) ( ArenaId aid, const Char* s);
888
889extern Char* VG_(getenv) ( Char* name );
890extern Int VG_(getpid) ( void );
sewardj5f07b662002-04-23 16:52:51 +0000891
892extern void VG_(start_rdtsc_calibration) ( void );
893extern void VG_(end_rdtsc_calibration) ( void );
894extern UInt VG_(read_millisecond_timer) ( void );
sewardjde4a1d02002-03-22 01:27:54 +0000895
896
897extern Char VG_(toupper) ( Char c );
898
899extern void VG_(strncpy_safely) ( Char* dest, const Char* src, Int ndest );
900
901extern void VG_(strncpy) ( Char* dest, const Char* src, Int ndest );
902
903extern Bool VG_(stringMatch) ( Char* pat, Char* str );
904
905
sewardj3e1eb1f2002-05-18 13:14:17 +0000906#define VG__STRING(__str) #__str
sewardjde4a1d02002-03-22 01:27:54 +0000907
908/* Asserts are permanently enabled. Hurrah! */
909#define vg_assert(expr) \
910 ((void) ((expr) ? 0 : \
sewardj3e1eb1f2002-05-18 13:14:17 +0000911 (VG_(assert_fail) (VG__STRING(expr), \
sewardjde4a1d02002-03-22 01:27:54 +0000912 __FILE__, __LINE__, \
913 __PRETTY_FUNCTION__), 0)))
914
915extern void VG_(assert_fail) ( Char* expr, Char* file,
916 Int line, Char* fn )
917 __attribute__ ((__noreturn__));
918
njn4f9c9342002-04-29 16:03:24 +0000919/* Reading and writing files. */
sewardjde4a1d02002-03-22 01:27:54 +0000920extern Int VG_(open_read) ( Char* pathname );
njn4f9c9342002-04-29 16:03:24 +0000921extern Int VG_(open_write) ( Char* pathname );
922extern Int VG_(create_and_write) ( Char* pathname );
sewardjde4a1d02002-03-22 01:27:54 +0000923extern void VG_(close) ( Int fd );
924extern Int VG_(read) ( Int fd, void* buf, Int count);
925extern Int VG_(write) ( Int fd, void* buf, Int count);
sewardjb3586202002-05-09 17:38:13 +0000926extern Int VG_(stat) ( Char* file_name, struct vki_stat* buf );
sewardjde4a1d02002-03-22 01:27:54 +0000927
sewardj2e93c502002-04-12 11:12:52 +0000928extern Int VG_(fcntl) ( Int fd, Int cmd, Int arg );
929
930extern Int VG_(select)( Int n,
931 vki_fd_set* readfds,
932 vki_fd_set* writefds,
933 vki_fd_set* exceptfds,
934 struct vki_timeval * timeout );
935extern Int VG_(nanosleep)( const struct vki_timespec *req,
936 struct vki_timespec *rem );
937
938
sewardjde4a1d02002-03-22 01:27:54 +0000939/* mmap-ery ... */
940extern void* VG_(mmap)( void* start, UInt length,
941 UInt prot, UInt flags, UInt fd, UInt offset );
942
sewardj2e93c502002-04-12 11:12:52 +0000943extern Int VG_(munmap)( void* start, Int length );
sewardjde4a1d02002-03-22 01:27:54 +0000944
sewardjb3586202002-05-09 17:38:13 +0000945extern void* VG_(brk) ( void* end_data_segment );
946
sewardjde4a1d02002-03-22 01:27:54 +0000947
948/* Print a (panic) message, and abort. */
949extern void VG_(panic) ( Char* str )
950 __attribute__ ((__noreturn__));
951
952/* Get memory by anonymous mmap. */
sewardje9047952002-06-05 20:28:33 +0000953extern void* VG_(get_memory_from_mmap) ( Int nBytes, Char* who );
sewardje6a25242002-04-21 22:03:07 +0000954
955/* Crude stand-in for the glibc system() call. */
956extern Int VG_(system) ( Char* cmd );
957
sewardjde4a1d02002-03-22 01:27:54 +0000958
959/* Signal stuff. Note that these use the vk_ (kernel) structure
960 definitions, which are different in places from those that glibc
961 defines. Since we're operating right at the kernel interface,
962 glibc's view of the world is entirely irrelevant. */
sewardj018f7622002-05-15 21:13:39 +0000963
964/* --- Signal set ops --- */
sewardjb48e5002002-05-13 00:16:03 +0000965extern Int VG_(ksigfillset)( vki_ksigset_t* set );
966extern Int VG_(ksigemptyset)( vki_ksigset_t* set );
sewardjde4a1d02002-03-22 01:27:54 +0000967
sewardj018f7622002-05-15 21:13:39 +0000968extern Bool VG_(kisfullsigset)( vki_ksigset_t* set );
969extern Bool VG_(kisemptysigset)( vki_ksigset_t* set );
970
971extern Int VG_(ksigaddset)( vki_ksigset_t* set, Int signum );
972extern Int VG_(ksigdelset)( vki_ksigset_t* set, Int signum );
973extern Int VG_(ksigismember) ( vki_ksigset_t* set, Int signum );
974
sewardjb48e5002002-05-13 00:16:03 +0000975extern void VG_(ksigaddset_from_set)( vki_ksigset_t* dst,
976 vki_ksigset_t* src );
977extern void VG_(ksigdelset_from_set)( vki_ksigset_t* dst,
978 vki_ksigset_t* src );
979
sewardj018f7622002-05-15 21:13:39 +0000980/* --- Mess with the kernel's sig state --- */
981extern Int VG_(ksigprocmask)( Int how, const vki_ksigset_t* set,
982 vki_ksigset_t* oldset );
983extern Int VG_(ksigaction) ( Int signum,
984 const vki_ksigaction* act,
985 vki_ksigaction* oldact );
sewardjde4a1d02002-03-22 01:27:54 +0000986
987extern Int VG_(ksignal)(Int signum, void (*sighandler)(Int));
988
989extern Int VG_(ksigaltstack)( const vki_kstack_t* ss, vki_kstack_t* oss );
990
sewardj018f7622002-05-15 21:13:39 +0000991extern Int VG_(kill)( Int pid, Int signo );
sewardjefbfcdf2002-06-19 17:35:45 +0000992extern Int VG_(sigpending) ( vki_ksigset_t* set );
sewardjde4a1d02002-03-22 01:27:54 +0000993
994
995/* ---------------------------------------------------------------------
996 Definitions for the JITter (vg_translate.c, vg_to_ucode.c,
997 vg_from_ucode.c).
998 ------------------------------------------------------------------ */
999
1000/* Tags which describe what operands are. */
1001typedef
1002 enum { TempReg=0, ArchReg=1, RealReg=2,
1003 SpillNo=3, Literal=4, Lit16=5,
1004 NoValue=6 }
1005 Tag;
1006
1007
1008/* Microinstruction opcodes. */
1009typedef
1010 enum {
1011 NOP,
1012 GET,
1013 PUT,
1014 LOAD,
1015 STORE,
1016 MOV,
1017 CMOV, /* Used for cmpxchg and cmov */
1018 WIDEN,
1019 JMP,
1020
1021 /* Read/write the %EFLAGS register into a TempReg. */
1022 GETF, PUTF,
1023
1024 ADD, ADC, AND, OR, XOR, SUB, SBB,
1025 SHL, SHR, SAR, ROL, ROR, RCL, RCR,
1026 NOT, NEG, INC, DEC, BSWAP,
1027 CC2VAL,
1028
1029 /* Not strictly needed, but useful for making better
1030 translations of address calculations. */
1031 LEA1, /* reg2 := const + reg1 */
1032 LEA2, /* reg3 := const + reg1 + reg2 * 1,2,4 or 8 */
1033
1034 /* not for translating x86 calls -- only to call helpers */
1035 CALLM_S, CALLM_E, /* Mark start and end of push/pop sequences
1036 for CALLM. */
1037 PUSH, POP, CLEAR, /* Add/remove/zap args for helpers. */
1038 CALLM, /* call to a machine-code helper */
1039
1040 /* Hack for translating string (REP-) insns. Jump to literal if
1041 TempReg/RealReg is zero. */
1042 JIFZ,
1043
1044 /* FPU ops which read/write mem or don't touch mem at all. */
1045 FPU_R,
1046 FPU_W,
1047 FPU,
1048
1049 /* Advance the simulated %eip by some small (< 128) number. */
1050 INCEIP,
1051
1052 /* uinstrs which are not needed for mere translation of x86 code,
1053 only for instrumentation of it. */
1054 LOADV,
1055 STOREV,
1056 GETV,
1057 PUTV,
1058 TESTV,
1059 SETV,
1060 /* Get/set the v-bit (and it is only one bit) for the simulated
1061 %eflags register. */
1062 GETVF,
1063 PUTVF,
1064
1065 /* Do a unary or binary tag op. Only for post-instrumented
1066 code. For TAG1, first and only arg is a TempReg, and is both
1067 arg and result reg. For TAG2, first arg is src, second is
1068 dst, in the normal way; both are TempRegs. In both cases,
1069 3rd arg is a RiCHelper with a Lit16 tag. This indicates
1070 which tag op to do. */
1071 TAG1,
1072 TAG2
1073 }
1074 Opcode;
1075
1076
1077/* Condition codes, observing the Intel encoding. CondAlways is an
1078 extra. */
1079typedef
1080 enum {
1081 CondO = 0, /* overflow */
1082 CondNO = 1, /* no overflow */
1083 CondB = 2, /* below */
1084 CondNB = 3, /* not below */
1085 CondZ = 4, /* zero */
1086 CondNZ = 5, /* not zero */
1087 CondBE = 6, /* below or equal */
1088 CondNBE = 7, /* not below or equal */
1089 CondS = 8, /* negative */
1090 ConsNS = 9, /* not negative */
1091 CondP = 10, /* parity even */
1092 CondNP = 11, /* not parity even */
1093 CondL = 12, /* jump less */
1094 CondNL = 13, /* not less */
1095 CondLE = 14, /* less or equal */
1096 CondNLE = 15, /* not less or equal */
1097 CondAlways = 16 /* Jump always */
1098 }
1099 Condcode;
1100
1101
sewardj2e93c502002-04-12 11:12:52 +00001102/* Descriptions of additional properties of *unconditional* jumps. */
1103typedef
1104 enum {
1105 JmpBoring=0, /* boring unconditional jump */
1106 JmpCall=1, /* jump due to an x86 call insn */
1107 JmpRet=2, /* jump due to an x86 ret insn */
1108 JmpSyscall=3, /* do a system call, then jump */
1109 JmpClientReq=4 /* do a client request, then jump */
1110 }
1111 JmpKind;
1112
1113
sewardjde4a1d02002-03-22 01:27:54 +00001114/* Flags. User-level code can only read/write O(verflow), S(ign),
1115 Z(ero), A(ux-carry), C(arry), P(arity), and may also write
1116 D(irection). That's a total of 7 flags. A FlagSet is a bitset,
1117 thusly:
1118 76543210
1119 DOSZACP
1120 and bit 7 must always be zero since it is unused.
1121*/
1122typedef UChar FlagSet;
1123
1124#define FlagD (1<<6)
1125#define FlagO (1<<5)
1126#define FlagS (1<<4)
1127#define FlagZ (1<<3)
1128#define FlagA (1<<2)
1129#define FlagC (1<<1)
1130#define FlagP (1<<0)
1131
1132#define FlagsOSZACP (FlagO | FlagS | FlagZ | FlagA | FlagC | FlagP)
1133#define FlagsOSZAP (FlagO | FlagS | FlagZ | FlagA | FlagP)
1134#define FlagsOSZCP (FlagO | FlagS | FlagZ | FlagC | FlagP)
1135#define FlagsOSACP (FlagO | FlagS | FlagA | FlagC | FlagP)
1136#define FlagsSZACP ( FlagS | FlagZ | FlagA | FlagC | FlagP)
1137#define FlagsSZAP ( FlagS | FlagZ | FlagA | FlagP)
sewardj4a7456e2002-03-24 13:52:19 +00001138#define FlagsZCP ( FlagZ | FlagC | FlagP)
sewardjde4a1d02002-03-22 01:27:54 +00001139#define FlagsOC (FlagO | FlagC )
sewardj4d0ab1f2002-03-24 10:00:09 +00001140#define FlagsAC ( FlagA | FlagC )
sewardjde4a1d02002-03-22 01:27:54 +00001141
1142#define FlagsALL (FlagsOSZACP | FlagD)
1143#define FlagsEmpty (FlagSet)0
1144
1145#define VG_IS_FLAG_SUBSET(set1,set2) \
1146 (( ((FlagSet)set1) & ((FlagSet)set2) ) == ((FlagSet)set1) )
1147
1148#define VG_UNION_FLAG_SETS(set1,set2) \
1149 ( ((FlagSet)set1) | ((FlagSet)set2) )
1150
1151
1152
1153/* A Micro (u)-instruction. */
1154typedef
1155 struct {
1156 /* word 1 */
1157 UInt lit32; /* 32-bit literal */
1158
1159 /* word 2 */
1160 UShort val1; /* first operand */
1161 UShort val2; /* second operand */
1162
1163 /* word 3 */
1164 UShort val3; /* third operand */
1165 UChar opcode; /* opcode */
1166 UChar size; /* data transfer size */
1167
1168 /* word 4 */
1169 FlagSet flags_r; /* :: FlagSet */
1170 FlagSet flags_w; /* :: FlagSet */
1171 UChar tag1:4; /* first operand tag */
1172 UChar tag2:4; /* second operand tag */
1173 UChar tag3:4; /* third operand tag */
1174 UChar extra4b:4; /* Spare field, used by WIDEN for src
1175 -size, and by LEA2 for scale
njn4f9c9342002-04-29 16:03:24 +00001176 (1,2,4 or 8), and by unconditional JMPs for
1177 orig x86 instr size if --cachesim=yes */
1178
sewardjde4a1d02002-03-22 01:27:54 +00001179
1180 /* word 5 */
1181 UChar cond; /* condition, for jumps */
1182 Bool smc_check:1; /* do a smc test, if writes memory. */
1183 Bool signed_widen:1; /* signed or unsigned WIDEN ? */
sewardj2e93c502002-04-12 11:12:52 +00001184 JmpKind jmpkind:3; /* additional properties of unconditional JMP */
sewardjde4a1d02002-03-22 01:27:54 +00001185 }
1186 UInstr;
1187
1188
1189/* Expandable arrays of uinstrs. */
1190typedef
1191 struct {
1192 Int used;
1193 Int size;
1194 UInstr* instrs;
1195 Int nextTemp;
1196 }
1197 UCodeBlock;
1198
1199/* Refer to `the last instruction stuffed in', including as an
1200 lvalue. */
1201#define LAST_UINSTR(cb) (cb)->instrs[(cb)->used-1]
1202
1203/* An invalid temporary number :-) */
1204#define INVALID_TEMPREG 999999999
1205
1206
1207/* ---------------------------------------------------------------------
1208 Exports of vg_demangle.c
1209 ------------------------------------------------------------------ */
1210
1211extern void VG_(demangle) ( Char* orig, Char* result, Int result_size );
1212
1213
1214/* ---------------------------------------------------------------------
1215 Exports of vg_from_ucode.c
1216 ------------------------------------------------------------------ */
1217
1218extern UChar* VG_(emit_code) ( UCodeBlock* cb, Int* nbytes );
1219
1220
1221/* ---------------------------------------------------------------------
1222 Exports of vg_to_ucode.c
1223 ------------------------------------------------------------------ */
1224
1225extern Int VG_(disBB) ( UCodeBlock* cb, Addr eip0 );
1226extern Char* VG_(nameOfIntReg) ( Int size, Int reg );
1227extern Char VG_(nameOfIntSize) ( Int size );
1228extern UInt VG_(extend_s_8to32) ( UInt x );
1229extern Int VG_(getNewTemp) ( UCodeBlock* cb );
1230extern Int VG_(getNewShadow) ( UCodeBlock* cb );
1231
1232#define SHADOW(tempreg) ((tempreg)+1)
1233
1234
1235/* ---------------------------------------------------------------------
1236 Exports of vg_translate.c
1237 ------------------------------------------------------------------ */
1238
sewardj1e8cdc92002-04-18 11:37:52 +00001239extern void VG_(translate) ( ThreadState* tst,
1240 Addr orig_addr,
sewardjde4a1d02002-03-22 01:27:54 +00001241 UInt* orig_size,
1242 Addr* trans_addr,
1243 UInt* trans_size );
1244
1245extern void VG_(emptyUInstr) ( UInstr* u );
1246extern void VG_(newUInstr0) ( UCodeBlock* cb, Opcode opcode, Int sz );
1247extern void VG_(newUInstr1) ( UCodeBlock* cb, Opcode opcode, Int sz,
1248 Tag tag1, UInt val1 );
1249extern void VG_(newUInstr2) ( UCodeBlock* cb, Opcode opcode, Int sz,
1250 Tag tag1, UInt val1,
1251 Tag tag2, UInt val2 );
1252extern void VG_(newUInstr3) ( UCodeBlock* cb, Opcode opcode, Int sz,
1253 Tag tag1, UInt val1,
1254 Tag tag2, UInt val2,
1255 Tag tag3, UInt val3 );
1256extern void VG_(setFlagRW) ( UInstr* u,
1257 FlagSet fr, FlagSet fw );
1258
1259extern void VG_(setLiteralField) ( UCodeBlock* cb, UInt lit32 );
1260extern Bool VG_(anyFlagUse) ( UInstr* u );
1261
1262
1263
1264extern void VG_(ppUInstr) ( Int instrNo, UInstr* u );
1265extern void VG_(ppUCodeBlock) ( UCodeBlock* cb, Char* title );
1266
njn4f9c9342002-04-29 16:03:24 +00001267extern UCodeBlock* VG_(allocCodeBlock) ( void );
1268extern void VG_(freeCodeBlock) ( UCodeBlock* cb );
1269extern void VG_(copyUInstr) ( UCodeBlock* cb, UInstr* instr );
1270
sewardjde4a1d02002-03-22 01:27:54 +00001271extern Char* VG_(nameCondcode) ( Condcode cond );
1272extern Bool VG_(saneUInstr) ( Bool beforeRA, UInstr* u );
1273extern Bool VG_(saneUCodeBlock) ( UCodeBlock* cb );
1274extern Char* VG_(nameUOpcode) ( Bool upper, Opcode opc );
1275extern Int VG_(rankToRealRegNo) ( Int rank );
1276
1277extern void* VG_(jitmalloc) ( Int nbytes );
1278extern void VG_(jitfree) ( void* ptr );
1279
1280
1281/* ---------------------------------------------------------------------
1282 Exports of vg_execontext.c.
1283 ------------------------------------------------------------------ */
1284
1285/* Records the PC and a bit of the call chain. The first 4 %eip
1286 values are used in comparisons do remove duplicate errors, and for
1287 comparing against suppression specifications. The rest are purely
1288 informational (but often important). */
1289
1290typedef
1291 struct _ExeContextRec {
1292 struct _ExeContextRec * next;
1293 /* The size of this array is VG_(clo_backtrace_size); at least
1294 2, at most VG_DEEPEST_BACKTRACE. [0] is the current %eip,
1295 [1] is its caller, [2] is the caller of [1], etc. */
1296 Addr eips[0];
1297 }
1298 ExeContext;
1299
1300
1301/* Initialise the ExeContext storage mechanism. */
1302extern void VG_(init_ExeContext_storage) ( void );
1303
1304/* Print stats (informational only). */
1305extern void VG_(show_ExeContext_stats) ( void );
1306
1307
1308/* Take a snapshot of the client's stack. Search our collection of
1309 ExeContexts to see if we already have it, and if not, allocate a
1310 new one. Either way, return a pointer to the context. */
sewardj8c824512002-04-14 04:16:48 +00001311extern ExeContext* VG_(get_ExeContext) ( Bool skip_top_frame,
1312 Addr eip, Addr ebp );
sewardjde4a1d02002-03-22 01:27:54 +00001313
1314/* Print an ExeContext. */
1315extern void VG_(pp_ExeContext) ( ExeContext* );
1316
1317/* Compare two ExeContexts, just comparing the top two callers. */
1318extern Bool VG_(eq_ExeContext_top2) ( ExeContext* e1, ExeContext* e2 );
1319
1320/* Compare two ExeContexts, just comparing the top four callers. */
1321extern Bool VG_(eq_ExeContext_top4) ( ExeContext* e1, ExeContext* e2 );
1322
1323/* Compare two ExeContexts, comparing all callers. */
1324extern Bool VG_(eq_ExeContext_all) ( ExeContext* e1, ExeContext* e2 );
1325
1326
1327
1328/* ---------------------------------------------------------------------
1329 Exports of vg_errcontext.c.
1330 ------------------------------------------------------------------ */
1331
1332extern void VG_(load_suppressions) ( void );
1333extern void VG_(show_all_errors) ( void );
1334extern void VG_(record_value_error) ( Int size );
sewardjaabd5ad2002-04-19 15:43:37 +00001335extern void VG_(record_free_error) ( ThreadState* tst, Addr a );
1336extern void VG_(record_freemismatch_error) ( ThreadState* tst, Addr a );
sewardjde4a1d02002-03-22 01:27:54 +00001337extern void VG_(record_address_error) ( Addr a, Int size,
1338 Bool isWrite );
sewardj1e8cdc92002-04-18 11:37:52 +00001339
1340extern void VG_(record_jump_error) ( ThreadState* tst, Addr a );
sewardj8c824512002-04-14 04:16:48 +00001341
1342extern void VG_(record_param_err) ( ThreadState* tst,
1343 Addr a,
sewardjde4a1d02002-03-22 01:27:54 +00001344 Bool isWriteLack,
1345 Char* msg );
sewardj8c824512002-04-14 04:16:48 +00001346extern void VG_(record_user_err) ( ThreadState* tst,
1347 Addr a, Bool isWriteLack );
sewardj4dced352002-06-04 22:54:20 +00001348extern void VG_(record_pthread_err) ( ThreadId tid, Char* msg );
1349
sewardjde4a1d02002-03-22 01:27:54 +00001350
1351
1352/* The classification of a faulting address. */
1353typedef
sewardjb581a132002-05-08 00:32:50 +00001354 enum { Undescribed, /* as-yet unclassified */
1355 Stack,
1356 Unknown, /* classification yielded nothing useful */
1357 Freed, Mallocd,
1358 UserG, UserS }
sewardjde4a1d02002-03-22 01:27:54 +00001359 AddrKind;
1360
1361/* Records info about a faulting address. */
1362typedef
1363 struct {
1364 /* ALL */
1365 AddrKind akind;
1366 /* Freed, Mallocd */
1367 Int blksize;
1368 /* Freed, Mallocd */
1369 Int rwoffset;
1370 /* Freed, Mallocd */
1371 ExeContext* lastchange;
sewardj1e8cdc92002-04-18 11:37:52 +00001372 /* Stack */
1373 ThreadId stack_tid;
sewardjb581a132002-05-08 00:32:50 +00001374 /* True if is just-below %esp -- could be a gcc bug. */
1375 Bool maybe_gcc;
sewardjde4a1d02002-03-22 01:27:54 +00001376 }
1377 AddrInfo;
1378
1379
1380/* ---------------------------------------------------------------------
1381 Exports of vg_clientperms.c
1382 ------------------------------------------------------------------ */
1383
1384extern Bool VG_(client_perm_maybe_describe)( Addr a, AddrInfo* ai );
1385
sewardj8c824512002-04-14 04:16:48 +00001386extern UInt VG_(handle_client_request) ( ThreadState* tst, UInt* arg_block );
sewardjde4a1d02002-03-22 01:27:54 +00001387
1388extern void VG_(delete_client_stack_blocks_following_ESP_change) ( void );
1389
1390extern void VG_(show_client_block_stats) ( void );
1391
1392
1393/* ---------------------------------------------------------------------
1394 Exports of vg_procselfmaps.c
1395 ------------------------------------------------------------------ */
1396
1397extern
1398void VG_(read_procselfmaps) (
1399 void (*record_mapping)( Addr, UInt, Char, Char, Char, UInt, UChar* )
1400);
1401
1402
1403/* ---------------------------------------------------------------------
1404 Exports of vg_symtab2.c
1405 ------------------------------------------------------------------ */
1406
1407/* We assume the executable is loaded here ... can't really find
1408 out. There is a hacky sanity check in vg_init_memory_audit()
1409 which should trip up most stupidities.
1410*/
1411#define VG_ASSUMED_EXE_BASE (Addr)0x8048000
1412
1413extern void VG_(read_symbols) ( void );
1414extern void VG_(mini_stack_dump) ( ExeContext* ec );
1415extern void VG_(what_obj_and_fun_is_this)
1416 ( Addr a,
1417 Char* obj_buf, Int n_obj_buf,
1418 Char* fun_buf, Int n_fun_buf );
njn4f9c9342002-04-29 16:03:24 +00001419extern Bool VG_(what_line_is_this) ( Addr a,
1420 UChar* filename, Int n_filename,
1421 UInt* lineno );
1422extern Bool VG_(what_fn_is_this) ( Bool no_demangle, Addr a,
1423 Char* fn_name, Int n_fn_name);
sewardjde4a1d02002-03-22 01:27:54 +00001424
sewardj18d75132002-05-16 11:06:21 +00001425extern Bool VG_(symtab_notify_munmap) ( Addr start, UInt length );
sewardjde4a1d02002-03-22 01:27:54 +00001426
1427
1428/* ---------------------------------------------------------------------
1429 Exports of vg_clientmalloc.c
1430 ------------------------------------------------------------------ */
1431
sewardjde4a1d02002-03-22 01:27:54 +00001432typedef
1433 enum {
1434 Vg_AllocMalloc = 0,
sewardj2e93c502002-04-12 11:12:52 +00001435 Vg_AllocNew = 1,
sewardjde4a1d02002-03-22 01:27:54 +00001436 Vg_AllocNewVec = 2
1437 }
1438 VgAllocKind;
1439
1440/* Description of a malloc'd chunk. */
1441typedef
1442 struct _ShadowChunk {
1443 struct _ShadowChunk* next;
1444 ExeContext* where; /* where malloc'd/free'd */
1445 UInt size : 30; /* size requested. */
1446 VgAllocKind allockind : 2; /* which wrapper did the allocation */
1447 Addr data; /* ptr to actual block. */
1448 }
1449 ShadowChunk;
1450
1451extern void VG_(clientmalloc_done) ( void );
1452extern void VG_(describe_addr) ( Addr a, AddrInfo* ai );
1453extern ShadowChunk** VG_(get_malloc_shadows) ( /*OUT*/ UInt* n_shadows );
1454
sewardj2e93c502002-04-12 11:12:52 +00001455/* These are called from the scheduler, when it intercepts a user
1456 request. */
sewardj8c824512002-04-14 04:16:48 +00001457extern void* VG_(client_malloc) ( ThreadState* tst,
1458 UInt size, VgAllocKind kind );
1459extern void* VG_(client_memalign) ( ThreadState* tst,
1460 UInt align, UInt size );
1461extern void VG_(client_free) ( ThreadState* tst,
1462 void* ptrV, VgAllocKind kind );
1463extern void* VG_(client_calloc) ( ThreadState* tst,
1464 UInt nmemb, UInt size1 );
1465extern void* VG_(client_realloc) ( ThreadState* tst,
1466 void* ptrV, UInt size_new );
sewardjde4a1d02002-03-22 01:27:54 +00001467
1468
1469/* ---------------------------------------------------------------------
1470 Exports of vg_main.c
1471 ------------------------------------------------------------------ */
1472
sewardjde4a1d02002-03-22 01:27:54 +00001473/* A structure used as an intermediary when passing the simulated
1474 CPU's state to some assembly fragments, particularly system calls.
1475 Stuff is copied from baseBlock to here, the assembly magic runs,
1476 and then the inverse copy is done. */
1477
1478extern UInt VG_(m_state_static) [8 /* int regs, in Intel order */
1479 + 1 /* %eflags */
1480 + 1 /* %eip */
1481 + VG_SIZE_OF_FPUSTATE_W /* FPU state */
1482 ];
1483
1484/* Handy fns for doing the copy back and forth. */
1485extern void VG_(copy_baseBlock_to_m_state_static) ( void );
1486extern void VG_(copy_m_state_static_to_baseBlock) ( void );
1487
sewardjde4a1d02002-03-22 01:27:54 +00001488/* Called when some unhandleable client behaviour is detected.
1489 Prints a msg and aborts. */
1490extern void VG_(unimplemented) ( Char* msg );
sewardjcfc39b22002-05-08 01:58:18 +00001491extern void VG_(nvidia_moan) ( void );
sewardjde4a1d02002-03-22 01:27:54 +00001492
1493/* The stack on which Valgrind runs. We can't use the same stack as the
1494 simulatee -- that's an important design decision. */
1495extern UInt VG_(stack)[10000];
1496
1497/* Similarly, we have to ask for signals to be delivered on an
1498 alternative stack, since it is possible, although unlikely, that
1499 we'll have to run client code from inside the Valgrind-installed
1500 signal handler. If this happens it will be done by
1501 vg_deliver_signal_immediately(). */
1502extern UInt VG_(sigstack)[10000];
1503
sewardjde4a1d02002-03-22 01:27:54 +00001504/* Holds client's %esp at the point we gained control. From this the
1505 client's argc, argv and envp are deduced. */
1506extern Addr VG_(esp_at_startup);
1507extern Int VG_(client_argc);
1508extern Char** VG_(client_argv);
1509extern Char** VG_(client_envp);
1510
1511/* Remove valgrind.so from a LD_PRELOAD=... string so child processes
sewardj3e1eb1f2002-05-18 13:14:17 +00001512 don't get traced into. Also mess up $libdir/valgrind so that our
1513 libpthread.so disappears from view. */
1514void VG_(mash_LD_PRELOAD_and_LD_LIBRARY_PATH) ( Char* ld_preload_str,
1515 Char* ld_library_path_str );
sewardjde4a1d02002-03-22 01:27:54 +00001516
1517/* Something of a function looking for a home ... start up GDB. This
1518 is called from VG_(swizzle_esp_then_start_GDB) and so runs on the
1519 *client's* stack. This is necessary to give GDB the illusion that
1520 the client program really was running on the real cpu. */
1521extern void VG_(start_GDB_whilst_on_client_stack) ( void );
1522
1523/* Spew out vast amounts of junk during JITting? */
1524extern Bool VG_(disassemble);
1525
1526/* 64-bit counter for the number of basic blocks done. */
1527extern ULong VG_(bbs_done);
1528/* 64-bit counter for the number of bbs to go before a debug exit. */
1529extern ULong VG_(bbs_to_go);
1530
1531/* Counts downwards in vg_run_innerloop. */
1532extern UInt VG_(dispatch_ctr);
1533
sewardjde4a1d02002-03-22 01:27:54 +00001534/* Is the client running on the simulated CPU or the real one? */
1535extern Bool VG_(running_on_simd_CPU); /* Initially False */
1536
1537/* The current LRU epoch. */
1538extern UInt VG_(current_epoch);
1539
sewardj7e87e382002-05-03 19:09:05 +00001540/* This is the ThreadId of the last thread the scheduler ran. */
1541extern ThreadId VG_(last_run_tid);
1542
sewardjde4a1d02002-03-22 01:27:54 +00001543
1544/* --- Counters, for informational purposes only. --- */
1545
1546/* Number of lookups which miss the fast tt helper. */
1547extern UInt VG_(tt_fast_misses);
1548
1549/* Counts for LRU informational messages. */
1550
1551/* Number and total o/t size of new translations this epoch. */
1552extern UInt VG_(this_epoch_in_count);
1553extern UInt VG_(this_epoch_in_osize);
1554extern UInt VG_(this_epoch_in_tsize);
1555/* Number and total o/t size of discarded translations this epoch. */
1556extern UInt VG_(this_epoch_out_count);
1557extern UInt VG_(this_epoch_out_osize);
1558extern UInt VG_(this_epoch_out_tsize);
1559/* Number and total o/t size of translations overall. */
1560extern UInt VG_(overall_in_count);
1561extern UInt VG_(overall_in_osize);
1562extern UInt VG_(overall_in_tsize);
1563/* Number and total o/t size of discards overall. */
1564extern UInt VG_(overall_out_count);
1565extern UInt VG_(overall_out_osize);
1566extern UInt VG_(overall_out_tsize);
1567
1568/* The number of LRU-clearings of TT/TC. */
1569extern UInt VG_(number_of_lrus);
1570
1571/* Counts pertaining to the register allocator. */
1572
1573/* total number of uinstrs input to reg-alloc */
1574extern UInt VG_(uinstrs_prealloc);
1575
1576/* total number of uinstrs added due to spill code */
1577extern UInt VG_(uinstrs_spill);
1578
1579/* number of bbs requiring spill code */
1580extern UInt VG_(translations_needing_spill);
1581
1582/* total of register ranks over all translations */
1583extern UInt VG_(total_reg_rank);
1584
sewardjde4a1d02002-03-22 01:27:54 +00001585/* Counts pertaining to internal sanity checking. */
1586extern UInt VG_(sanity_fast_count);
1587extern UInt VG_(sanity_slow_count);
1588
sewardj2e93c502002-04-12 11:12:52 +00001589/* Counts pertaining to the scheduler. */
1590extern UInt VG_(num_scheduling_events_MINOR);
1591extern UInt VG_(num_scheduling_events_MAJOR);
1592
sewardjde4a1d02002-03-22 01:27:54 +00001593
1594/* ---------------------------------------------------------------------
1595 Exports of vg_memory.c
1596 ------------------------------------------------------------------ */
1597
1598extern void VGM_(init_memory_audit) ( void );
1599extern Addr VGM_(curr_dataseg_end);
1600extern void VG_(show_reg_tags) ( void );
1601extern void VG_(detect_memory_leaks) ( void );
1602extern void VG_(done_prof_mem) ( void );
1603
1604/* Set permissions for an address range. Not speed-critical. */
1605extern void VGM_(make_noaccess) ( Addr a, UInt len );
1606extern void VGM_(make_writable) ( Addr a, UInt len );
1607extern void VGM_(make_readable) ( Addr a, UInt len );
1608/* Use with care! (read: use for shmat only) */
1609extern void VGM_(make_readwritable) ( Addr a, UInt len );
1610extern void VGM_(copy_address_range_perms) ( Addr src, Addr dst,
1611 UInt len );
1612
1613/* Check permissions for an address range. Not speed-critical. */
1614extern Bool VGM_(check_writable) ( Addr a, UInt len, Addr* bad_addr );
1615extern Bool VGM_(check_readable) ( Addr a, UInt len, Addr* bad_addr );
1616extern Bool VGM_(check_readable_asciiz) ( Addr a, Addr* bad_addr );
1617
sewardj0c3b53f2002-05-01 01:58:35 +00001618/* Sanity checks which may be done at any time. The scheduler decides
1619 when. */
1620extern void VG_(do_sanity_checks) ( Bool force_expensive );
sewardjde4a1d02002-03-22 01:27:54 +00001621/* Very cheap ... */
1622extern Bool VG_(first_and_last_secondaries_look_plausible) ( void );
1623
1624/* These functions are called from generated code. */
1625extern void VG_(helperc_STOREV4) ( UInt, Addr );
1626extern void VG_(helperc_STOREV2) ( UInt, Addr );
1627extern void VG_(helperc_STOREV1) ( UInt, Addr );
1628
1629extern UInt VG_(helperc_LOADV1) ( Addr );
1630extern UInt VG_(helperc_LOADV2) ( Addr );
1631extern UInt VG_(helperc_LOADV4) ( Addr );
1632
1633extern void VGM_(handle_esp_assignment) ( Addr new_espA );
1634extern void VGM_(fpu_write_check) ( Addr addr, Int size );
1635extern void VGM_(fpu_read_check) ( Addr addr, Int size );
1636
1637/* Safely (avoiding SIGSEGV / SIGBUS) scan the entire valid address
1638 space and pass the addresses and values of all addressible,
1639 defined, aligned words to notify_word. This is the basis for the
1640 leak detector. Returns the number of calls made to notify_word. */
1641UInt VG_(scan_all_valid_memory) ( void (*notify_word)( Addr, UInt ) );
1642
1643/* Is this address within some small distance below %ESP? Used only
1644 for the --workaround-gcc296-bugs kludge. */
sewardj8c824512002-04-14 04:16:48 +00001645extern Bool VG_(is_just_below_ESP)( Addr esp, Addr aa );
sewardjde4a1d02002-03-22 01:27:54 +00001646
1647/* Nasty kludgery to deal with applications which switch stacks,
1648 like netscape. */
sewardjde4a1d02002-03-22 01:27:54 +00001649#define VG_PLAUSIBLE_STACK_SIZE 8000000
1650
sewardjc3bd5f52002-05-01 03:24:23 +00001651/* Needed by the pthreads implementation. */
1652#define VGM_WORD_VALID 0
1653#define VGM_WORD_INVALID 0xFFFFFFFF
1654
sewardjde4a1d02002-03-22 01:27:54 +00001655
1656/* ---------------------------------------------------------------------
1657 Exports of vg_syscall_mem.c
1658 ------------------------------------------------------------------ */
1659
sewardj2e93c502002-04-12 11:12:52 +00001660extern void VG_(perform_assumed_nonblocking_syscall) ( ThreadId tid );
sewardjde4a1d02002-03-22 01:27:54 +00001661
sewardj2e93c502002-04-12 11:12:52 +00001662extern void VG_(check_known_blocking_syscall) ( ThreadId tid,
1663 Int syscallno,
1664 Int* /*IN*/ res );
sewardjde4a1d02002-03-22 01:27:54 +00001665
1666extern Bool VG_(is_kerror) ( Int res );
1667
sewardj018f7622002-05-15 21:13:39 +00001668#define KERNEL_DO_SYSCALL(thread_id, result_lvalue) \
1669 VG_(load_thread_state)(thread_id); \
1670 VG_(copy_baseBlock_to_m_state_static)(); \
1671 VG_(do_syscall)(); \
1672 VG_(copy_m_state_static_to_baseBlock)(); \
1673 VG_(save_thread_state)(thread_id); \
1674 VG_(threads)[thread_id].sh_eax = VGM_WORD_VALID; \
1675 result_lvalue = VG_(threads)[thread_id].m_eax;
sewardjde4a1d02002-03-22 01:27:54 +00001676
1677
1678/* ---------------------------------------------------------------------
1679 Exports of vg_transtab.c
1680 ------------------------------------------------------------------ */
1681
1682/* An entry in the translation table (TT). */
1683typedef
1684 struct {
1685 /* +0 */ Addr orig_addr;
1686 /* +4 */ Addr trans_addr;
1687 /* +8 */ UInt mru_epoch;
1688 /* +12 */ UShort orig_size;
1689 /* +14 */ UShort trans_size;
1690 }
1691 TTEntry;
1692
1693/* The number of basic blocks in an epoch (one age-step). */
1694#define VG_BBS_PER_EPOCH 20000
1695
1696extern void VG_(get_tt_tc_used) ( UInt* tt_used, UInt* tc_used );
1697extern void VG_(maybe_do_lru_pass) ( void );
1698extern void VG_(flush_transtab) ( void );
1699extern Addr VG_(copy_to_transcache) ( Addr trans_addr, Int trans_size );
1700extern void VG_(add_to_trans_tab) ( TTEntry* tte );
sewardj18d75132002-05-16 11:06:21 +00001701extern void VG_(invalidate_translations) ( Addr start, UInt range );
sewardjde4a1d02002-03-22 01:27:54 +00001702
sewardj18d75132002-05-16 11:06:21 +00001703extern void VG_(init_tt_tc) ( void );
sewardjde4a1d02002-03-22 01:27:54 +00001704
1705extern void VG_(sanity_check_tc_tt) ( void );
1706extern Addr VG_(search_transtab) ( Addr original_addr );
1707
1708extern void VG_(invalidate_tt_fast)( void );
1709
1710
1711/* ---------------------------------------------------------------------
1712 Exports of vg_vtagops.c
1713 ------------------------------------------------------------------ */
1714
1715/* Lists the names of value-tag operations used in instrumented
1716 code. These are the third argument to TAG1 and TAG2 uinsns. */
1717
1718typedef
1719 enum {
1720 /* Unary. */
1721 VgT_PCast40, VgT_PCast20, VgT_PCast10,
1722 VgT_PCast01, VgT_PCast02, VgT_PCast04,
1723
1724 VgT_PCast14, VgT_PCast12, VgT_PCast11,
1725
1726 VgT_Left4, VgT_Left2, VgT_Left1,
1727
1728 VgT_SWiden14, VgT_SWiden24, VgT_SWiden12,
1729 VgT_ZWiden14, VgT_ZWiden24, VgT_ZWiden12,
1730
1731 /* Binary; 1st is rd; 2nd is rd+wr */
1732 VgT_UifU4, VgT_UifU2, VgT_UifU1, VgT_UifU0,
1733 VgT_DifD4, VgT_DifD2, VgT_DifD1,
1734
1735 VgT_ImproveAND4_TQ, VgT_ImproveAND2_TQ, VgT_ImproveAND1_TQ,
1736 VgT_ImproveOR4_TQ, VgT_ImproveOR2_TQ, VgT_ImproveOR1_TQ,
1737 VgT_DebugFn
1738 }
1739 VgTagOp;
1740
1741extern Char* VG_(nameOfTagOp) ( VgTagOp );
1742extern UInt VG_(DebugFn) ( UInt a1, UInt a2 );
1743
1744
1745/* ---------------------------------------------------------------------
1746 Exports of vg_syscall.S
1747 ------------------------------------------------------------------ */
1748
1749extern void VG_(do_syscall) ( void );
1750
1751
1752/* ---------------------------------------------------------------------
1753 Exports of vg_startup.S
1754 ------------------------------------------------------------------ */
1755
sewardjde4a1d02002-03-22 01:27:54 +00001756extern void VG_(switch_to_real_CPU) ( void );
1757
sewardj35805422002-04-21 13:05:34 +00001758extern void VG_(swizzle_esp_then_start_GDB) ( Addr m_eip_at_error,
1759 Addr m_esp_at_error,
1760 Addr m_ebp_at_error );
sewardjde4a1d02002-03-22 01:27:54 +00001761
1762
1763/* ---------------------------------------------------------------------
1764 Exports of vg_dispatch.S
1765 ------------------------------------------------------------------ */
1766
sewardj2e93c502002-04-12 11:12:52 +00001767/* Run a thread for a (very short) while, until some event happens
1768 which means we need to defer to the scheduler. */
1769extern UInt VG_(run_innerloop) ( void );
sewardjde4a1d02002-03-22 01:27:54 +00001770
1771
1772/* ---------------------------------------------------------------------
1773 Exports of vg_helpers.S
1774 ------------------------------------------------------------------ */
1775
sewardjde4a1d02002-03-22 01:27:54 +00001776/* Mul, div, etc, -- we don't codegen these directly. */
1777extern void VG_(helper_idiv_64_32);
1778extern void VG_(helper_div_64_32);
1779extern void VG_(helper_idiv_32_16);
1780extern void VG_(helper_div_32_16);
1781extern void VG_(helper_idiv_16_8);
1782extern void VG_(helper_div_16_8);
1783
1784extern void VG_(helper_imul_32_64);
1785extern void VG_(helper_mul_32_64);
1786extern void VG_(helper_imul_16_32);
1787extern void VG_(helper_mul_16_32);
1788extern void VG_(helper_imul_8_16);
1789extern void VG_(helper_mul_8_16);
1790
1791extern void VG_(helper_CLD);
1792extern void VG_(helper_STD);
1793extern void VG_(helper_get_dirflag);
1794
sewardj7d78e782002-06-02 00:04:00 +00001795extern void VG_(helper_CLC);
1796extern void VG_(helper_STC);
1797
sewardjde4a1d02002-03-22 01:27:54 +00001798extern void VG_(helper_shldl);
1799extern void VG_(helper_shldw);
1800extern void VG_(helper_shrdl);
1801extern void VG_(helper_shrdw);
1802
1803extern void VG_(helper_RDTSC);
1804extern void VG_(helper_CPUID);
1805
sewardjde4a1d02002-03-22 01:27:54 +00001806extern void VG_(helper_bsf);
1807extern void VG_(helper_bsr);
1808
1809extern void VG_(helper_fstsw_AX);
1810extern void VG_(helper_SAHF);
sewardj4d0ab1f2002-03-24 10:00:09 +00001811extern void VG_(helper_DAS);
sewardjfe8a1662002-03-24 11:54:07 +00001812extern void VG_(helper_DAA);
sewardjde4a1d02002-03-22 01:27:54 +00001813
1814extern void VG_(helper_value_check4_fail);
1815extern void VG_(helper_value_check2_fail);
1816extern void VG_(helper_value_check1_fail);
1817extern void VG_(helper_value_check0_fail);
1818
sewardj20917d82002-05-28 01:36:45 +00001819/* NOT A FUNCTION; this is a bogus RETURN ADDRESS. */
sewardj54cacf02002-04-12 23:24:59 +00001820extern void VG_(signalreturn_bogusRA)( void );
sewardj20917d82002-05-28 01:36:45 +00001821
sewardj54cacf02002-04-12 23:24:59 +00001822
njn4f9c9342002-04-29 16:03:24 +00001823/* ---------------------------------------------------------------------
1824 Exports of vg_cachesim.c
1825 ------------------------------------------------------------------ */
1826
sewardj07133bf2002-06-13 10:25:56 +00001827extern Int VG_(log2) ( Int x );
njn7cf0bd32002-06-08 13:36:03 +00001828
sewardj07133bf2002-06-13 10:25:56 +00001829extern UCodeBlock* VG_(cachesim_instrument) ( UCodeBlock* cb_in,
1830 Addr orig_addr );
njn4f9c9342002-04-29 16:03:24 +00001831
1832typedef struct _iCC iCC;
1833typedef struct _idCC idCC;
1834
njn7cf0bd32002-06-08 13:36:03 +00001835extern void VG_(init_cachesim) ( void );
1836extern void VG_(do_cachesim_results)( Int client_argc, Char** client_argv );
njn4f9c9342002-04-29 16:03:24 +00001837
1838extern void VG_(cachesim_log_non_mem_instr)( iCC* cc );
1839extern void VG_(cachesim_log_mem_instr) ( idCC* cc, Addr data_addr );
sewardjde4a1d02002-03-22 01:27:54 +00001840
sewardj18d75132002-05-16 11:06:21 +00001841extern void VG_(cachesim_notify_discard) ( TTEntry* tte );
1842
1843
sewardjde4a1d02002-03-22 01:27:54 +00001844/* ---------------------------------------------------------------------
1845 The state of the simulated CPU.
1846 ------------------------------------------------------------------ */
1847
1848/* This is the Intel register encoding. */
1849#define R_EAX 0
1850#define R_ECX 1
1851#define R_EDX 2
1852#define R_EBX 3
1853#define R_ESP 4
1854#define R_EBP 5
1855#define R_ESI 6
1856#define R_EDI 7
1857
1858#define R_AL (0+R_EAX)
1859#define R_CL (0+R_ECX)
1860#define R_DL (0+R_EDX)
1861#define R_BL (0+R_EBX)
1862#define R_AH (4+R_EAX)
1863#define R_CH (4+R_ECX)
1864#define R_DH (4+R_EDX)
1865#define R_BH (4+R_EBX)
1866
1867
1868/* ---------------------------------------------------------------------
1869 Offsets into baseBlock for everything which needs to referred to
1870 from generated code. The order of these decls does not imply
1871 what the order of the actual offsets is. The latter is important
1872 and is set up in vg_main.c.
1873 ------------------------------------------------------------------ */
1874
1875/* An array of words. In generated code, %ebp always points to the
1876 start of this array. Useful stuff, like the simulated CPU state,
1877 and the addresses of helper functions, can then be found by
1878 indexing off %ebp. The following declares variables which, at
1879 startup time, are given values denoting offsets into baseBlock.
1880 These offsets are in *words* from the start of baseBlock. */
1881
1882#define VG_BASEBLOCK_WORDS 200
1883
1884extern UInt VG_(baseBlock)[VG_BASEBLOCK_WORDS];
1885
1886
1887/* -----------------------------------------------------
1888 Read-write parts of baseBlock.
1889 -------------------------------------------------- */
1890
1891/* State of the simulated CPU. */
1892extern Int VGOFF_(m_eax);
1893extern Int VGOFF_(m_ecx);
1894extern Int VGOFF_(m_edx);
1895extern Int VGOFF_(m_ebx);
1896extern Int VGOFF_(m_esp);
1897extern Int VGOFF_(m_ebp);
1898extern Int VGOFF_(m_esi);
1899extern Int VGOFF_(m_edi);
1900extern Int VGOFF_(m_eflags);
1901extern Int VGOFF_(m_fpustate);
1902extern Int VGOFF_(m_eip);
1903
1904/* Reg-alloc spill area (VG_MAX_SPILLSLOTS words long). */
1905extern Int VGOFF_(spillslots);
1906
1907/* Records the valid bits for the 8 integer regs & flags reg. */
1908extern Int VGOFF_(sh_eax);
1909extern Int VGOFF_(sh_ecx);
1910extern Int VGOFF_(sh_edx);
1911extern Int VGOFF_(sh_ebx);
1912extern Int VGOFF_(sh_esp);
1913extern Int VGOFF_(sh_ebp);
1914extern Int VGOFF_(sh_esi);
1915extern Int VGOFF_(sh_edi);
1916extern Int VGOFF_(sh_eflags);
1917
1918
1919/* -----------------------------------------------------
1920 Read-only parts of baseBlock.
1921 -------------------------------------------------- */
1922
1923/* Offsets of addresses of helper functions. A "helper" function is
1924 one which is called from generated code. */
1925
1926extern Int VGOFF_(helper_idiv_64_32);
1927extern Int VGOFF_(helper_div_64_32);
1928extern Int VGOFF_(helper_idiv_32_16);
1929extern Int VGOFF_(helper_div_32_16);
1930extern Int VGOFF_(helper_idiv_16_8);
1931extern Int VGOFF_(helper_div_16_8);
1932
1933extern Int VGOFF_(helper_imul_32_64);
1934extern Int VGOFF_(helper_mul_32_64);
1935extern Int VGOFF_(helper_imul_16_32);
1936extern Int VGOFF_(helper_mul_16_32);
1937extern Int VGOFF_(helper_imul_8_16);
1938extern Int VGOFF_(helper_mul_8_16);
1939
1940extern Int VGOFF_(helper_CLD);
1941extern Int VGOFF_(helper_STD);
1942extern Int VGOFF_(helper_get_dirflag);
1943
sewardj7d78e782002-06-02 00:04:00 +00001944extern Int VGOFF_(helper_CLC);
1945extern Int VGOFF_(helper_STC);
1946
sewardjde4a1d02002-03-22 01:27:54 +00001947extern Int VGOFF_(helper_shldl);
1948extern Int VGOFF_(helper_shldw);
1949extern Int VGOFF_(helper_shrdl);
1950extern Int VGOFF_(helper_shrdw);
1951
1952extern Int VGOFF_(helper_RDTSC);
1953extern Int VGOFF_(helper_CPUID);
1954
sewardjde4a1d02002-03-22 01:27:54 +00001955extern Int VGOFF_(helper_bsf);
1956extern Int VGOFF_(helper_bsr);
1957
1958extern Int VGOFF_(helper_fstsw_AX);
1959extern Int VGOFF_(helper_SAHF);
sewardj4d0ab1f2002-03-24 10:00:09 +00001960extern Int VGOFF_(helper_DAS);
sewardjfe8a1662002-03-24 11:54:07 +00001961extern Int VGOFF_(helper_DAA);
sewardjde4a1d02002-03-22 01:27:54 +00001962
1963extern Int VGOFF_(helper_value_check4_fail);
1964extern Int VGOFF_(helper_value_check2_fail);
1965extern Int VGOFF_(helper_value_check1_fail);
1966extern Int VGOFF_(helper_value_check0_fail);
1967
sewardjde4a1d02002-03-22 01:27:54 +00001968extern Int VGOFF_(helperc_STOREV4); /* :: UInt -> Addr -> void */
1969extern Int VGOFF_(helperc_STOREV2); /* :: UInt -> Addr -> void */
1970extern Int VGOFF_(helperc_STOREV1); /* :: UInt -> Addr -> void */
1971
1972extern Int VGOFF_(helperc_LOADV4); /* :: Addr -> UInt -> void */
1973extern Int VGOFF_(helperc_LOADV2); /* :: Addr -> UInt -> void */
1974extern Int VGOFF_(helperc_LOADV1); /* :: Addr -> UInt -> void */
1975
1976extern Int VGOFF_(handle_esp_assignment); /* :: Addr -> void */
1977extern Int VGOFF_(fpu_write_check); /* :: Addr -> Int -> void */
1978extern Int VGOFF_(fpu_read_check); /* :: Addr -> Int -> void */
1979
njn4f9c9342002-04-29 16:03:24 +00001980extern Int VGOFF_(cachesim_log_non_mem_instr);
1981extern Int VGOFF_(cachesim_log_mem_instr);
sewardjde4a1d02002-03-22 01:27:54 +00001982
1983#endif /* ndef __VG_INCLUDE_H */
1984
sewardj3b2736a2002-03-24 12:18:35 +00001985
1986/* ---------------------------------------------------------------------
1987 Finally - autoconf-generated settings
1988 ------------------------------------------------------------------ */
1989
1990#include "config.h"
1991
sewardjde4a1d02002-03-22 01:27:54 +00001992/*--------------------------------------------------------------------*/
1993/*--- end vg_include.h ---*/
1994/*--------------------------------------------------------------------*/