blob: 3b8358fd944e2bbaa6c623c80131adc4cae43c4c [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/* ---------------------------------------------------------------------
42 Build options and table sizes. You should be able to change these
43 options or sizes, recompile, and still have a working system.
44 ------------------------------------------------------------------ */
45
46#include "vg_constants.h"
47
48
49/* Set to 1 to enable time profiling. Since this uses SIGPROF, we
50 don't want this permanently enabled -- only for profiling
51 builds. */
52#if 0
53# define VG_PROFILE
54#endif
55
56
57/* Total number of integer registers available for allocation. That's
58 all of them except %esp, %edi and %ebp. %edi is a general spare
59 temporary. %ebp permanently points at VG_(baseBlock). Note that
60 it's important that this tie in with what rankToRealRegNo() says.
61 DO NOT CHANGE THIS VALUE FROM 5. ! */
62#define VG_MAX_REALREGS 5
63
64/* Total number of spill slots available for allocation, if a TempReg
65 doesn't make it into a RealReg. Just bomb the entire system if
66 this value is too small; we don't expect it will ever get
67 particularly high. */
68#define VG_MAX_SPILLSLOTS 24
69
70
71/* Constants for the slow translation lookup cache. */
72#define VG_TRANSTAB_SLOW_BITS 11
73#define VG_TRANSTAB_SLOW_SIZE (1 << VG_TRANSTAB_SLOW_BITS)
74#define VG_TRANSTAB_SLOW_MASK ((VG_TRANSTAB_SLOW_SIZE) - 1)
75
76/* Size of a buffer used for creating messages. */
77#define M_VG_MSGBUF 10000
78
79/* Size of a smallish table used to read /proc/self/map entries. */
sewardjebc82332002-04-24 14:44:23 +000080#define M_PROCMAP_BUF 50000
sewardjde4a1d02002-03-22 01:27:54 +000081
82/* Max length of pathname to a .so/executable file. */
83#define M_VG_LIBNAMESTR 100
84
85/* Max length of a text fragment used to construct error messages. */
86#define M_VG_ERRTXT 512
87
88/* Max length of the string copied from env var VG_ARGS at startup. */
89#define M_VG_CMDLINE_STRLEN 1000
90
91/* Max number of options for Valgrind which we can handle. */
92#define M_VG_CMDLINE_OPTS 100
93
94/* After this many different unsuppressed errors have been observed,
95 be more conservative about collecting new ones. */
96#define M_VG_COLLECT_ERRORS_SLOWLY_AFTER 50
97
98/* After this many different unsuppressed errors have been observed,
99 stop collecting errors at all, and tell the user their program is
100 evidently a steaming pile of camel dung. */
sewardj1bebcbf2002-04-24 21:24:18 +0000101#define M_VG_COLLECT_NO_ERRORS_AFTER_SHOWN 300
sewardjf2537be2002-04-24 21:03:47 +0000102
103/* After this many total errors have been observed, stop collecting
104 errors at all. Counterpart to M_VG_COLLECT_NO_ERRORS_AFTER_SHOWN. */
sewardj1bebcbf2002-04-24 21:24:18 +0000105#define M_VG_COLLECT_NO_ERRORS_AFTER_FOUND 30000
sewardjde4a1d02002-03-22 01:27:54 +0000106
107/* These many bytes below %ESP are considered addressible if we're
108 doing the --workaround-gcc296-bugs hack. */
sewardjb581a132002-05-08 00:32:50 +0000109#define VG_GCC296_BUG_STACK_SLOP 1024
sewardjde4a1d02002-03-22 01:27:54 +0000110
111/* The maximum number of calls we're prepared to save in a
112 backtrace. */
113#define VG_DEEPEST_BACKTRACE 50
114
115/* Number of lists in which we keep track of malloc'd but not free'd
116 blocks. Should be prime. */
117#define VG_N_MALLOCLISTS 997
118
119/* Number of lists in which we keep track of ExeContexts. Should be
120 prime. */
121#define VG_N_EC_LISTS /*997*/ 4999
122
sewardj2e93c502002-04-12 11:12:52 +0000123/* Defines the thread-scheduling timeslice, in terms of the number of
124 basic blocks we attempt to run each thread for. Smaller values
125 give finer interleaving but much increased scheduling overheads. */
sewardjd8091eb2002-04-24 02:19:36 +0000126#define VG_SCHEDULING_QUANTUM 20000
sewardj2e93c502002-04-12 11:12:52 +0000127
128/* The maximum number of pthreads that we support. This is
129 deliberately not very high since our implementation of some of the
sewardj5f07b662002-04-23 16:52:51 +0000130 scheduler algorithms is surely O(N) in the number of threads, since
131 that's simple, at least. And (in practice) we hope that most
sewardj2e93c502002-04-12 11:12:52 +0000132 programs do not need many threads. */
sewardj7989d0c2002-05-28 11:00:01 +0000133#define VG_N_THREADS 20
sewardj5f07b662002-04-23 16:52:51 +0000134
135/* Maximum number of pthread keys available. Again, we start low until
136 the need for a higher number presents itself. */
137#define VG_N_THREAD_KEYS 10
sewardj2e93c502002-04-12 11:12:52 +0000138
139/* Number of file descriptors that can simultaneously be waited on for
140 I/O to complete. Perhaps this should be the same as VG_N_THREADS
141 (surely a thread can't wait on more than one fd at once?. Who
142 knows.) */
143#define VG_N_WAITING_FDS 10
144
sewardjbf290b92002-05-01 02:28:01 +0000145/* Stack size for a thread. We try and check that they do not go
146 beyond it. */
147#define VG_PTHREAD_STACK_SIZE 65536
148
sewardj20917d82002-05-28 01:36:45 +0000149/* Number of entries in the semaphore-remapping table. */
150#define VG_N_SEMAPHORES 50
151
152/* Number of entries in the rwlock-remapping table. */
153#define VG_N_RWLOCKS 50
154
sewardjde4a1d02002-03-22 01:27:54 +0000155
156/* ---------------------------------------------------------------------
157 Basic types
158 ------------------------------------------------------------------ */
159
160typedef unsigned char UChar;
161typedef unsigned short UShort;
162typedef unsigned int UInt;
163typedef unsigned long long int ULong;
164
165typedef signed char Char;
166typedef signed short Short;
167typedef signed int Int;
168typedef signed long long int Long;
169
170typedef unsigned int Addr;
171
172typedef unsigned char Bool;
173#define False ((Bool)0)
174#define True ((Bool)1)
175
176#define mycat_wrk(aaa,bbb) aaa##bbb
177#define mycat(aaa,bbb) mycat_wrk(aaa,bbb)
178
179/* Just pray that gcc's constant folding works properly ... */
180#define BITS(bit7,bit6,bit5,bit4,bit3,bit2,bit1,bit0) \
181 ( ((bit7) << 7) | ((bit6) << 6) | ((bit5) << 5) | ((bit4) << 4) \
182 | ((bit3) << 3) | ((bit2) << 2) | ((bit1) << 1) | (bit0))
183
184
185/* ---------------------------------------------------------------------
186 Now the basic types are set up, we can haul in the kernel-interface
187 definitions.
188 ------------------------------------------------------------------ */
189
190#include "./vg_kerneliface.h"
191
192
193/* ---------------------------------------------------------------------
194 Command-line-settable options
195 ------------------------------------------------------------------ */
196
197#define VG_CLO_SMC_NONE 0
198#define VG_CLO_SMC_SOME 1
199#define VG_CLO_SMC_ALL 2
200
201#define VG_CLO_MAX_SFILES 10
202
sewardj97ced732002-03-25 00:07:36 +0000203/* Shall we V-check addrs (they are always A checked too): default: YES */
204extern Bool VG_(clo_check_addrVs);
sewardjde4a1d02002-03-22 01:27:54 +0000205/* Enquire about whether to attach to GDB at errors? default: NO */
206extern Bool VG_(clo_GDB_attach);
207/* Sanity-check level: 0 = none, 1 (default), > 1 = expensive. */
208extern Int VG_(sanity_level);
209/* Verbosity level: 0 = silent, 1 (default), > 1 = more verbose. */
210extern Int VG_(clo_verbosity);
211/* Automatically attempt to demangle C++ names? default: YES */
212extern Bool VG_(clo_demangle);
213/* Do leak check at exit? default: NO */
214extern Bool VG_(clo_leak_check);
215/* In leak check, show reachable-but-not-freed blocks? default: NO */
216extern Bool VG_(clo_show_reachable);
217/* How closely should we compare ExeContexts in leak records? default: 2 */
218extern Int VG_(clo_leak_resolution);
219/* Round malloc sizes upwards to integral number of words? default:
220 NO */
221extern Bool VG_(clo_sloppy_malloc);
222/* Allow loads from partially-valid addresses? default: YES */
223extern Bool VG_(clo_partial_loads_ok);
224/* Simulate child processes? default: NO */
225extern Bool VG_(clo_trace_children);
226/* The file id on which we send all messages. default: 2 (stderr). */
227extern Int VG_(clo_logfile_fd);
228/* Max volume of the freed blocks queue. */
229extern Int VG_(clo_freelist_vol);
230/* Assume accesses immediately below %esp are due to gcc-2.96 bugs.
231 default: NO */
232extern Bool VG_(clo_workaround_gcc296_bugs);
233
234/* The number of suppression files specified. */
235extern Int VG_(clo_n_suppressions);
236/* The names of the suppression files. */
237extern Char* VG_(clo_suppressions)[VG_CLO_MAX_SFILES];
238
239/* Single stepping? default: NO */
240extern Bool VG_(clo_single_step);
241/* Code improvement? default: YES */
242extern Bool VG_(clo_optimise);
243/* Memory-check instrumentation? default: YES */
244extern Bool VG_(clo_instrument);
245/* DEBUG: clean up instrumented code? default: YES */
246extern Bool VG_(clo_cleanup);
njn4f9c9342002-04-29 16:03:24 +0000247/* Cache simulation instrumentation? default: NO */
248extern Bool VG_(clo_cachesim);
sewardjde4a1d02002-03-22 01:27:54 +0000249/* SMC write checks? default: SOME (1,2,4 byte movs to mem) */
250extern Int VG_(clo_smc_check);
251/* DEBUG: print system calls? default: NO */
252extern Bool VG_(clo_trace_syscalls);
253/* DEBUG: print signal details? default: NO */
254extern Bool VG_(clo_trace_signals);
255/* DEBUG: print symtab details? default: NO */
256extern Bool VG_(clo_trace_symtab);
257/* DEBUG: print malloc details? default: NO */
258extern Bool VG_(clo_trace_malloc);
sewardj8937c812002-04-12 20:12:20 +0000259/* DEBUG: print thread scheduling events? default: NO */
260extern Bool VG_(clo_trace_sched);
sewardj45b4b372002-04-16 22:50:32 +0000261/* DEBUG: print pthread (mutex etc) events? default: 0 (none), 1
262 (some), 2 (all) */
263extern Int VG_(clo_trace_pthread_level);
sewardjde4a1d02002-03-22 01:27:54 +0000264/* Stop after this many basic blocks. default: Infinity. */
265extern ULong VG_(clo_stop_after);
266/* Display gory details for the k'th most popular error. default:
267 Infinity. */
268extern Int VG_(clo_dump_error);
269/* Number of parents of a backtrace. Default: 8. */
270extern Int VG_(clo_backtrace_size);
sewardj3984b852002-05-12 03:00:17 +0000271/* Engage miscellaneous wierd hacks needed for some progs. */
sewardj8d365b52002-05-12 10:52:16 +0000272extern Char* VG_(clo_weird_hacks);
sewardjde4a1d02002-03-22 01:27:54 +0000273
274
275/* ---------------------------------------------------------------------
276 Debugging and profiling stuff
277 ------------------------------------------------------------------ */
278
279/* No, really. I _am_ that strange. */
280#define OINK(nnn) VG_(message)(Vg_DebugMsg, "OINK %d",nnn)
281
282/* Tools for building messages from multiple parts. */
283typedef
284 enum { Vg_UserMsg, Vg_DebugMsg, Vg_DebugExtraMsg }
285 VgMsgKind;
286
287extern void VG_(start_msg) ( VgMsgKind kind );
288extern void VG_(add_to_msg) ( Char* format, ... );
289extern void VG_(end_msg) ( void );
290
291/* Send a simple, single-part message. */
292extern void VG_(message) ( VgMsgKind kind, Char* format, ... );
293
294/* Create a logfile into which messages can be dumped. */
295extern void VG_(startup_logging) ( void );
296extern void VG_(shutdown_logging) ( void );
297
298
299/* Profiling stuff */
300#ifdef VG_PROFILE
301
302#define VGP_M_STACK 10
303
sewardj671ff542002-05-07 09:25:30 +0000304#define VGP_M_CCS 26 /* == the # of elems in VGP_LIST */
sewardjde4a1d02002-03-22 01:27:54 +0000305#define VGP_LIST \
sewardj671ff542002-05-07 09:25:30 +0000306 VGP_PAIR(VgpUnc=0, "unclassified"), \
307 VGP_PAIR(VgpRun, "running"), \
308 VGP_PAIR(VgpSched, "scheduler"), \
sewardjde4a1d02002-03-22 01:27:54 +0000309 VGP_PAIR(VgpMalloc, "low-lev malloc/free"), \
310 VGP_PAIR(VgpCliMalloc, "client malloc/free"), \
311 VGP_PAIR(VgpTranslate, "translate-main"), \
312 VGP_PAIR(VgpToUCode, "to-ucode"), \
313 VGP_PAIR(VgpFromUcode, "from-ucode"), \
314 VGP_PAIR(VgpImprove, "improve"), \
315 VGP_PAIR(VgpInstrument, "instrument"), \
316 VGP_PAIR(VgpCleanup, "cleanup"), \
317 VGP_PAIR(VgpRegAlloc, "reg-alloc"), \
318 VGP_PAIR(VgpDoLRU, "do-lru"), \
319 VGP_PAIR(VgpSlowFindT, "slow-search-transtab"), \
320 VGP_PAIR(VgpInitAudit, "init-mem-audit"), \
321 VGP_PAIR(VgpExeContext, "exe-context"), \
322 VGP_PAIR(VgpReadSyms, "read-syms"), \
323 VGP_PAIR(VgpAddToT, "add-to-transtab"), \
324 VGP_PAIR(VgpSARP, "set-addr-range-perms"), \
325 VGP_PAIR(VgpSyscall, "syscall wrapper"), \
njn4f9c9342002-04-29 16:03:24 +0000326 VGP_PAIR(VgpCacheInstrument, "cache instrument"), \
327 VGP_PAIR(VgpCacheGetBBCC,"cache get BBCC"), \
328 VGP_PAIR(VgpCacheSimulate, "cache simulate"), \
329 VGP_PAIR(VgpCacheDump, "cache stats dump"), \
sewardjde4a1d02002-03-22 01:27:54 +0000330 VGP_PAIR(VgpSpare1, "spare 1"), \
331 VGP_PAIR(VgpSpare2, "spare 2")
332
333#define VGP_PAIR(enumname,str) enumname
334typedef enum { VGP_LIST } VgpCC;
335#undef VGP_PAIR
336
337extern void VGP_(init_profiling) ( void );
338extern void VGP_(done_profiling) ( void );
339extern void VGP_(pushcc) ( VgpCC );
340extern void VGP_(popcc) ( void );
341
342#define VGP_PUSHCC(cc) VGP_(pushcc)(cc)
343#define VGP_POPCC VGP_(popcc)()
344
345#else
346
347#define VGP_PUSHCC(cc) /* */
348#define VGP_POPCC /* */
349
350#endif /* VG_PROFILE */
351
352
353/* ---------------------------------------------------------------------
354 Exports of vg_malloc2.c
355 ------------------------------------------------------------------ */
356
357/* Allocation arenas.
358 SYMTAB is for Valgrind's symbol table storage.
359 CLIENT is for the client's mallocs/frees.
360 DEMANGLE is for the C++ demangler.
361 EXECTXT is for storing ExeContexts.
362 ERRCTXT is for storing ErrContexts.
363 PRIVATE is for Valgrind general stuff.
364 TRANSIENT is for very short-term use. It should be empty
365 in between uses.
366 When adding a new arena, remember also to add it
367 to ensure_mm_init().
368*/
369typedef Int ArenaId;
370
371#define VG_N_ARENAS 7
372
373#define VG_AR_PRIVATE 0 /* :: ArenaId */
374#define VG_AR_SYMTAB 1 /* :: ArenaId */
375#define VG_AR_CLIENT 2 /* :: ArenaId */
376#define VG_AR_DEMANGLE 3 /* :: ArenaId */
377#define VG_AR_EXECTXT 4 /* :: ArenaId */
378#define VG_AR_ERRCTXT 5 /* :: ArenaId */
379#define VG_AR_TRANSIENT 6 /* :: ArenaId */
380
381extern void* VG_(malloc) ( ArenaId arena, Int nbytes );
382extern void VG_(free) ( ArenaId arena, void* ptr );
383extern void* VG_(calloc) ( ArenaId arena, Int nmemb, Int nbytes );
384extern void* VG_(realloc) ( ArenaId arena, void* ptr, Int size );
385extern void* VG_(malloc_aligned) ( ArenaId aid, Int req_alignB,
386 Int req_pszB );
387
388extern void VG_(mallocSanityCheckArena) ( ArenaId arena );
389extern void VG_(mallocSanityCheckAll) ( void );
390
391extern void VG_(show_all_arena_stats) ( void );
392extern Bool VG_(is_empty_arena) ( ArenaId aid );
393
394
395/* The red-zone size for the client. This can be arbitrary, but
396 unfortunately must be set at compile time. */
397#define VG_AR_CLIENT_REDZONE_SZW 4
398
399#define VG_AR_CLIENT_REDZONE_SZB \
400 (VG_AR_CLIENT_REDZONE_SZW * VKI_BYTES_PER_WORD)
401
402
403/* ---------------------------------------------------------------------
sewardj2e93c502002-04-12 11:12:52 +0000404 Exports of vg_clientfuns.c
405 ------------------------------------------------------------------ */
406
407/* This doesn't export code or data that valgrind.so needs to link
408 against. However, the scheduler does need to know the following
409 request codes. A few, publically-visible, request codes are also
410 defined in valgrind.h. */
411
412#define VG_USERREQ__MALLOC 0x2001
413#define VG_USERREQ__BUILTIN_NEW 0x2002
414#define VG_USERREQ__BUILTIN_VEC_NEW 0x2003
415
416#define VG_USERREQ__FREE 0x2004
417#define VG_USERREQ__BUILTIN_DELETE 0x2005
418#define VG_USERREQ__BUILTIN_VEC_DELETE 0x2006
419
420#define VG_USERREQ__CALLOC 0x2007
421#define VG_USERREQ__REALLOC 0x2008
422#define VG_USERREQ__MEMALIGN 0x2009
423
424
sewardj20917d82002-05-28 01:36:45 +0000425/* (Fn, Arg): Create a new thread and run Fn applied to Arg in it. Fn
426 MUST NOT return -- ever. Eventually it will do either __QUIT or
427 __WAIT_JOINER. */
428#define VG_USERREQ__APPLY_IN_NEW_THREAD 0x3001
429
430/* ( no-args ): calling thread disappears from the system forever.
431 Reclaim resources. */
432#define VG_USERREQ__QUIT 0x3002
433
434/* ( void* ): calling thread waits for joiner and returns the void* to
435 it. */
436#define VG_USERREQ__WAIT_JOINER 0x3003
437
438/* ( ThreadId, void** ): wait to join a thread. */
439#define VG_USERREQ__PTHREAD_JOIN 0x3004
440
441/* Set cancellation state and type for this thread. */
442#define VG_USERREQ__SET_CANCELSTATE 0x3005
443#define VG_USERREQ__SET_CANCELTYPE 0x3006
444
445/* ( no-args ): Test if we are at a cancellation point. */
446#define VG_USERREQ__TESTCANCEL 0x3007
447
448/* ( ThreadId, &thread_exit_wrapper is the only allowable arg ): call
449 with this arg to indicate that a cancel is now pending for the
450 specified thread. */
451#define VG_USERREQ__SET_CANCELPEND 0x3008
452
453/* Set/get detach state for this thread. */
454#define VG_USERREQ__SET_OR_GET_DETACH 0x3009
455
456#define VG_USERREQ__PTHREAD_GET_THREADID 0x300B
457#define VG_USERREQ__PTHREAD_MUTEX_LOCK 0x300C
458#define VG_USERREQ__PTHREAD_MUTEX_TRYLOCK 0x300D
459#define VG_USERREQ__PTHREAD_MUTEX_UNLOCK 0x300E
460#define VG_USERREQ__PTHREAD_COND_WAIT 0x300F
461#define VG_USERREQ__PTHREAD_COND_TIMEDWAIT 0x3010
462#define VG_USERREQ__PTHREAD_COND_SIGNAL 0x3011
463#define VG_USERREQ__PTHREAD_COND_BROADCAST 0x3012
464#define VG_USERREQ__PTHREAD_KEY_CREATE 0x3013
465#define VG_USERREQ__PTHREAD_KEY_DELETE 0x3014
466#define VG_USERREQ__PTHREAD_SETSPECIFIC 0x3015
467#define VG_USERREQ__PTHREAD_GETSPECIFIC 0x3016
468#define VG_USERREQ__READ_MILLISECOND_TIMER 0x3017
469#define VG_USERREQ__PTHREAD_SIGMASK 0x3018
470#define VG_USERREQ__SIGWAIT 0x3019
471#define VG_USERREQ__PTHREAD_KILL 0x301A
472#define VG_USERREQ__PTHREAD_YIELD 0x301B
sewardj2e93c502002-04-12 11:12:52 +0000473
sewardj45b4b372002-04-16 22:50:32 +0000474/* Cosmetic ... */
475#define VG_USERREQ__GET_PTHREAD_TRACE_LEVEL 0x3101
476
sewardj54cacf02002-04-12 23:24:59 +0000477/*
478In vg_constants.h:
479#define VG_USERREQ__SIGNAL_RETURNS 0x4001
sewardj54cacf02002-04-12 23:24:59 +0000480*/
481
482
sewardj2e93c502002-04-12 11:12:52 +0000483/* ---------------------------------------------------------------------
484 Constants pertaining to the simulated CPU state, VG_(baseBlock),
485 which need to go here to avoid ugly circularities.
486 ------------------------------------------------------------------ */
487
488/* How big is the saved FPU state? */
489#define VG_SIZE_OF_FPUSTATE 108
490/* ... and in words ... */
491#define VG_SIZE_OF_FPUSTATE_W ((VG_SIZE_OF_FPUSTATE+3)/4)
492
493
494/* ---------------------------------------------------------------------
495 Exports of vg_scheduler.c
496 ------------------------------------------------------------------ */
497
498/* ThreadIds are simply indices into the vg_threads[] array. */
499typedef
500 UInt
501 ThreadId;
502
sewardj6072c362002-04-19 14:40:57 +0000503/* Special magic value for an invalid ThreadId. It corresponds to
504 LinuxThreads using zero as the initial value for
505 pthread_mutex_t.__m_owner and pthread_cond_t.__c_waiting. */
506#define VG_INVALID_THREADID ((ThreadId)(0))
sewardj2e93c502002-04-12 11:12:52 +0000507
508typedef
509 enum {
510 VgTs_Empty, /* this slot is not in use */
511 VgTs_Runnable, /* waiting to be scheduled */
512 VgTs_WaitJoiner, /* waiting for someone to do join on me */
513 VgTs_WaitJoinee, /* waiting for the thread I did join on */
514 VgTs_WaitFD, /* waiting for I/O completion on a fd */
515 VgTs_WaitMX, /* waiting on a mutex */
sewardj3b5d8862002-04-20 13:53:23 +0000516 VgTs_WaitCV, /* waiting on a condition variable */
sewardjb48e5002002-05-13 00:16:03 +0000517 VgTs_WaitSIG, /* waiting due to sigwait() */
sewardj2e93c502002-04-12 11:12:52 +0000518 VgTs_Sleeping /* sleeping for a while */
519 }
520 ThreadStatus;
521
522typedef
523 struct {
sewardj6072c362002-04-19 14:40:57 +0000524 /* ThreadId == 0 (and hence vg_threads[0]) is NEVER USED.
525 The thread identity is simply the index in vg_threads[].
526 ThreadId == 1 is the root thread and has the special property
sewardj1e8cdc92002-04-18 11:37:52 +0000527 that we don't try and allocate or deallocate its stack. For
528 convenience of generating error message, we also put the
529 ThreadId in this tid field, but be aware that it should
sewardj604ec3c2002-04-18 22:38:41 +0000530 ALWAYS == the index in vg_threads[]. */
sewardj1e8cdc92002-04-18 11:37:52 +0000531 ThreadId tid;
sewardj2e93c502002-04-12 11:12:52 +0000532
sewardj5f07b662002-04-23 16:52:51 +0000533 /* Current scheduling status.
534
535 Complications: whenever this is set to VgTs_WaitMX, you
536 should also set .m_edx to whatever the required return value
537 is for pthread_mutex_lock / pthread_cond_timedwait for when
538 the mutex finally gets unblocked. */
sewardj2e93c502002-04-12 11:12:52 +0000539 ThreadStatus status;
540
sewardj3b5d8862002-04-20 13:53:23 +0000541 /* When .status == WaitMX, points to the mutex I am waiting for.
542 When .status == WaitCV, points to the mutex associated with
543 the condition variable indicated by the .associated_cv field.
544 In all other cases, should be NULL. */
545 void* /* pthread_mutex_t* */ associated_mx;
546
547 /* When .status == WaitCV, points to the condition variable I am
548 waiting for. In all other cases, should be NULL. */
549 void* /* pthread_cond_t* */ associated_cv;
sewardj2e93c502002-04-12 11:12:52 +0000550
sewardj5f07b662002-04-23 16:52:51 +0000551 /* If VgTs_Sleeping, this is when we should wake up, measured in
552 milliseconds as supplied by VG_(read_millisecond_counter).
553
554 If VgTs_WaitCV, this indicates the time at which
555 pthread_cond_timedwait should wake up. If == 0xFFFFFFFF,
556 this means infinitely far in the future, viz,
557 pthread_cond_wait. */
558 UInt awaken_at;
sewardj2e93c502002-04-12 11:12:52 +0000559
sewardj20917d82002-05-28 01:36:45 +0000560 /* If VgTs_WaitJoiner, return value, as generated by joinees. */
561 void* joinee_retval;
562
563 /* If VgTs_WaitJoinee, place to copy the return value to, and
564 the identity of the thread we're waiting for. */
565 void** joiner_thread_return;
566 ThreadId joiner_jee_tid;
567
568 /* Cancelability state and type. */
569 Bool cancel_st; /* False==PTH_CANCEL_DISABLE; True==.._ENABLE */
570 Bool cancel_ty; /* False==PTH_CANC_ASYNCH; True==..._DEFERRED */
571
572 /* Pointer to fn to call to do cancellation. Indicates whether
573 or not cancellation is pending. If NULL, not pending. Else
574 should be &thread_exit_wrapper(), indicating that
575 cancallation is pending. */
576 void (*cancel_pend)(void*);
577
578 /* Whether or not detached. */
579 Bool detached;
sewardj2e93c502002-04-12 11:12:52 +0000580
sewardj5f07b662002-04-23 16:52:51 +0000581 /* thread-specific data */
582 void* specifics[VG_N_THREAD_KEYS];
583
sewardjb48e5002002-05-13 00:16:03 +0000584 /* This thread's blocked-signals mask. Semantics is that for a
585 signal to be delivered to this thread, the signal must not be
586 blocked by either the process-wide signal mask nor by this
587 one. So, if this thread is prepared to handle any signal that
588 the process as a whole is prepared to handle, this mask should
589 be made empty -- and that it is its default, starting
590 state. */
591 vki_ksigset_t sig_mask;
592
593 /* When not VgTs_WaitSIG, has no meaning. When VgTs_WaitSIG,
594 is the set of signals for which we are sigwait()ing. */
595 vki_ksigset_t sigs_waited_for;
596
sewardj2e93c502002-04-12 11:12:52 +0000597 /* Stacks. When a thread slot is freed, we don't deallocate its
598 stack; we just leave it lying around for the next use of the
599 slot. If the next use of the slot requires a larger stack,
600 only then is the old one deallocated and a new one
601 allocated.
602
603 For the main thread (threadid == 0), this mechanism doesn't
604 apply. We don't know the size of the stack since we didn't
605 allocate it, and furthermore we never reallocate it. */
606
607 /* The allocated size of this thread's stack (permanently zero
608 if this is ThreadId == 0, since we didn't allocate its stack) */
609 UInt stack_size;
610
611 /* Address of the lowest word in this thread's stack. NULL means
612 not allocated yet.
613 */
614 Addr stack_base;
615
sewardj1e8cdc92002-04-18 11:37:52 +0000616 /* Address of the highest legitimate word in this stack. This is
617 used for error messages only -- not critical for execution
618 correctness. Is is set for all stacks, specifically including
619 ThreadId == 0 (the main thread). */
620 Addr stack_highest_word;
621
sewardj2e93c502002-04-12 11:12:52 +0000622 /* Saved machine context. */
623 UInt m_eax;
624 UInt m_ebx;
625 UInt m_ecx;
626 UInt m_edx;
627 UInt m_esi;
628 UInt m_edi;
629 UInt m_ebp;
630 UInt m_esp;
631 UInt m_eflags;
632 UInt m_eip;
633 UInt m_fpu[VG_SIZE_OF_FPUSTATE_W];
634
635 UInt sh_eax;
636 UInt sh_ebx;
637 UInt sh_ecx;
638 UInt sh_edx;
639 UInt sh_esi;
640 UInt sh_edi;
641 UInt sh_ebp;
642 UInt sh_esp;
643 UInt sh_eflags;
644 }
645 ThreadState;
646
647
sewardj018f7622002-05-15 21:13:39 +0000648/* The thread table. */
649extern ThreadState VG_(threads)[VG_N_THREADS];
650
651/* Check that tid is in range and denotes a non-Empty thread. */
sewardjb48e5002002-05-13 00:16:03 +0000652extern Bool VG_(is_valid_tid) ( ThreadId tid );
653
sewardj018f7622002-05-15 21:13:39 +0000654/* Check that tid is in range. */
655extern Bool VG_(is_valid_or_empty_tid) ( ThreadId tid );
656
sewardj2e93c502002-04-12 11:12:52 +0000657/* Copy the specified thread's state into VG_(baseBlock) in
658 preparation for running it. */
659extern void VG_(load_thread_state)( ThreadId );
660
661/* Save the specified thread's state back in VG_(baseBlock), and fill
662 VG_(baseBlock) with junk, for sanity-check reasons. */
663extern void VG_(save_thread_state)( ThreadId );
664
sewardj1e8cdc92002-04-18 11:37:52 +0000665/* And for the currently running one, if valid. */
666extern ThreadState* VG_(get_current_thread_state) ( void );
sewardj2e93c502002-04-12 11:12:52 +0000667
sewardj1e8cdc92002-04-18 11:37:52 +0000668/* Similarly ... */
669extern ThreadId VG_(get_current_tid) ( void );
670
671/* Which thread is this address in the stack of, if any? Used for
672 error message generation. */
673extern ThreadId VG_(identify_stack_addr)( Addr a );
674
sewardj2e93c502002-04-12 11:12:52 +0000675
676/* Return codes from the scheduler. */
677typedef
sewardj7e87e382002-05-03 19:09:05 +0000678 enum {
679 VgSrc_Deadlock, /* no runnable threads and no prospect of any
680 even if we wait for a long time */
681 VgSrc_ExitSyscall, /* client called exit(). This is the normal
682 route out. */
683 VgSrc_BbsDone /* In a debugging run, the specified number of
684 bbs has been completed. */
685 }
sewardj2e93c502002-04-12 11:12:52 +0000686 VgSchedReturnCode;
687
sewardj7e87e382002-05-03 19:09:05 +0000688
sewardj2e93c502002-04-12 11:12:52 +0000689/* The scheduler. */
690extern VgSchedReturnCode VG_(scheduler) ( void );
691
692extern void VG_(scheduler_init) ( void );
693
sewardj15a43e12002-04-17 19:35:12 +0000694extern void VG_(pp_sched_status) ( void );
sewardj2e93c502002-04-12 11:12:52 +0000695
696/* vg_oursignalhandler() might longjmp(). Here's the jmp_buf. */
697extern jmp_buf VG_(scheduler_jmpbuf);
698/* ... and if so, here's the signal which caused it to do so. */
699extern Int VG_(longjmpd_on_signal);
700
701
sewardja1679dd2002-05-10 22:31:40 +0000702/* Possible places where the main stack might be based. We check that
703 the initial stack, which we can't move, is allocated here.
704 VG_(scheduler_init) checks this. Andrea Archelangi's 2.4 kernels
705 have been rumoured to start stacks at 0x80000000, so that too is
706 considered.
sewardj2e93c502002-04-12 11:12:52 +0000707*/
sewardja1679dd2002-05-10 22:31:40 +0000708#define VG_STARTUP_STACK_BASE_1 (Addr)0xC0000000
709#define VG_STARTUP_STACK_BASE_2 (Addr)0x80000000
710#define VG_STARTUP_STACK_SMALLERTHAN 0x100000 /* 1024k */
711
712#define VG_STACK_MATCHES_BASE(zzstack, zzbase) \
713 ( \
714 ((zzstack) & ((zzbase) - VG_STARTUP_STACK_SMALLERTHAN)) \
715 == \
716 ((zzbase) - VG_STARTUP_STACK_SMALLERTHAN) \
717 )
sewardj2e93c502002-04-12 11:12:52 +0000718
719
720/* The red-zone size which we put at the bottom (highest address) of
721 thread stacks, for paranoia reasons. This can be arbitrary, and
722 doesn't really need to be set at compile time. */
723#define VG_AR_CLIENT_STACKBASE_REDZONE_SZW 4
724
725#define VG_AR_CLIENT_STACKBASE_REDZONE_SZB \
726 (VG_AR_CLIENT_STACKBASE_REDZONE_SZW * VKI_BYTES_PER_WORD)
727
728
sewardj018f7622002-05-15 21:13:39 +0000729/* Write a value to the client's %EDX (request return value register)
730 and set the shadow to indicate it is defined. */
731#define SET_EDX(zztid, zzval) \
732 do { VG_(threads)[zztid].m_edx = (zzval); \
733 VG_(threads)[zztid].sh_edx = VGM_WORD_VALID; \
734 } while (0)
735
736#define SET_EAX(zztid, zzval) \
737 do { VG_(threads)[zztid].m_eax = (zzval); \
738 VG_(threads)[zztid].sh_eax = VGM_WORD_VALID; \
739 } while (0)
740
sewardj2e93c502002-04-12 11:12:52 +0000741
742/* ---------------------------------------------------------------------
sewardjde4a1d02002-03-22 01:27:54 +0000743 Exports of vg_signals.c
744 ------------------------------------------------------------------ */
745
sewardjde4a1d02002-03-22 01:27:54 +0000746extern void VG_(sigstartup_actions) ( void );
747
sewardjb48e5002002-05-13 00:16:03 +0000748extern Bool VG_(deliver_signals) ( void );
sewardjde4a1d02002-03-22 01:27:54 +0000749extern void VG_(unblock_host_signal) ( Int sigNo );
sewardj018f7622002-05-15 21:13:39 +0000750extern void VG_(handle_SCSS_change) ( Bool force_update );
751
sewardjde4a1d02002-03-22 01:27:54 +0000752
753/* Fake system calls for signal handling. */
sewardj2342c972002-05-22 23:34:20 +0000754extern void VG_(do__NR_sigaltstack) ( ThreadId tid );
sewardj2e93c502002-04-12 11:12:52 +0000755extern void VG_(do__NR_sigaction) ( ThreadId tid );
sewardj018f7622002-05-15 21:13:39 +0000756extern void VG_(do__NR_sigprocmask) ( ThreadId tid,
757 Int how,
758 vki_ksigset_t* set,
759 vki_ksigset_t* oldset );
760extern void VG_(do_pthread_sigmask_SCSS_upd) ( ThreadId tid,
761 Int how,
762 vki_ksigset_t* set,
763 vki_ksigset_t* oldset );
764extern void VG_(send_signal_to_thread) ( ThreadId thread,
765 Int signo );
sewardjde4a1d02002-03-22 01:27:54 +0000766
sewardj2e93c502002-04-12 11:12:52 +0000767/* Modify the current thread's state once we have detected it is
768 returning from a signal handler. */
sewardj77e466c2002-04-14 02:29:29 +0000769extern Bool VG_(signal_returns) ( ThreadId );
sewardjde4a1d02002-03-22 01:27:54 +0000770
sewardj2e93c502002-04-12 11:12:52 +0000771/* Handy utilities to block/restore all host signals. */
772extern void VG_(block_all_host_signals)
773 ( /* OUT */ vki_ksigset_t* saved_mask );
sewardj018f7622002-05-15 21:13:39 +0000774extern void VG_(restore_all_host_signals)
sewardj2e93c502002-04-12 11:12:52 +0000775 ( /* IN */ vki_ksigset_t* saved_mask );
sewardjde4a1d02002-03-22 01:27:54 +0000776
777/* ---------------------------------------------------------------------
778 Exports of vg_mylibc.c
779 ------------------------------------------------------------------ */
780
781
sewardjfbe18b92002-05-10 00:46:59 +0000782#if !defined(NULL)
783# define NULL ((void*)0)
784#endif
sewardjde4a1d02002-03-22 01:27:54 +0000785
786extern void VG_(exit)( Int status )
787 __attribute__ ((__noreturn__));
788
789extern void VG_(printf) ( const char *format, ... );
790/* too noisy ... __attribute__ ((format (printf, 1, 2))) ; */
791
792extern void VG_(sprintf) ( Char* buf, Char *format, ... );
793
794extern void VG_(vprintf) ( void(*send)(Char),
795 const Char *format, va_list vargs );
796
797extern Bool VG_(isspace) ( Char c );
798
799extern Int VG_(strlen) ( const Char* str );
800
801extern Long VG_(atoll) ( Char* str );
802
803extern Char* VG_(strcat) ( Char* dest, const Char* src );
804extern Char* VG_(strncat) ( Char* dest, const Char* src, Int n );
805extern Char* VG_(strpbrk) ( const Char* s, const Char* accept );
806
807extern Char* VG_(strcpy) ( Char* dest, const Char* src );
808
809extern Int VG_(strcmp) ( const Char* s1, const Char* s2 );
810extern Int VG_(strcmp_ws) ( const Char* s1, const Char* s2 );
811
812extern Int VG_(strncmp) ( const Char* s1, const Char* s2, Int nmax );
813extern Int VG_(strncmp_ws) ( const Char* s1, const Char* s2, Int nmax );
814
815extern Char* VG_(strstr) ( const Char* haystack, Char* needle );
816extern Char* VG_(strchr) ( const Char* s, Char c );
817extern Char* VG_(strdup) ( ArenaId aid, const Char* s);
818
819extern Char* VG_(getenv) ( Char* name );
820extern Int VG_(getpid) ( void );
sewardj5f07b662002-04-23 16:52:51 +0000821
822extern void VG_(start_rdtsc_calibration) ( void );
823extern void VG_(end_rdtsc_calibration) ( void );
824extern UInt VG_(read_millisecond_timer) ( void );
sewardjde4a1d02002-03-22 01:27:54 +0000825
826
827extern Char VG_(toupper) ( Char c );
828
829extern void VG_(strncpy_safely) ( Char* dest, const Char* src, Int ndest );
830
831extern void VG_(strncpy) ( Char* dest, const Char* src, Int ndest );
832
833extern Bool VG_(stringMatch) ( Char* pat, Char* str );
834
835
sewardj3e1eb1f2002-05-18 13:14:17 +0000836#define VG__STRING(__str) #__str
sewardjde4a1d02002-03-22 01:27:54 +0000837
838/* Asserts are permanently enabled. Hurrah! */
839#define vg_assert(expr) \
840 ((void) ((expr) ? 0 : \
sewardj3e1eb1f2002-05-18 13:14:17 +0000841 (VG_(assert_fail) (VG__STRING(expr), \
sewardjde4a1d02002-03-22 01:27:54 +0000842 __FILE__, __LINE__, \
843 __PRETTY_FUNCTION__), 0)))
844
845extern void VG_(assert_fail) ( Char* expr, Char* file,
846 Int line, Char* fn )
847 __attribute__ ((__noreturn__));
848
njn4f9c9342002-04-29 16:03:24 +0000849/* Reading and writing files. */
sewardjde4a1d02002-03-22 01:27:54 +0000850extern Int VG_(open_read) ( Char* pathname );
njn4f9c9342002-04-29 16:03:24 +0000851extern Int VG_(open_write) ( Char* pathname );
852extern Int VG_(create_and_write) ( Char* pathname );
sewardjde4a1d02002-03-22 01:27:54 +0000853extern void VG_(close) ( Int fd );
854extern Int VG_(read) ( Int fd, void* buf, Int count);
855extern Int VG_(write) ( Int fd, void* buf, Int count);
sewardjb3586202002-05-09 17:38:13 +0000856extern Int VG_(stat) ( Char* file_name, struct vki_stat* buf );
sewardjde4a1d02002-03-22 01:27:54 +0000857
sewardj2e93c502002-04-12 11:12:52 +0000858extern Int VG_(fcntl) ( Int fd, Int cmd, Int arg );
859
860extern Int VG_(select)( Int n,
861 vki_fd_set* readfds,
862 vki_fd_set* writefds,
863 vki_fd_set* exceptfds,
864 struct vki_timeval * timeout );
865extern Int VG_(nanosleep)( const struct vki_timespec *req,
866 struct vki_timespec *rem );
867
868
sewardjde4a1d02002-03-22 01:27:54 +0000869/* mmap-ery ... */
870extern void* VG_(mmap)( void* start, UInt length,
871 UInt prot, UInt flags, UInt fd, UInt offset );
872
sewardj2e93c502002-04-12 11:12:52 +0000873extern Int VG_(munmap)( void* start, Int length );
sewardjde4a1d02002-03-22 01:27:54 +0000874
sewardjb3586202002-05-09 17:38:13 +0000875extern void* VG_(brk) ( void* end_data_segment );
876
sewardjde4a1d02002-03-22 01:27:54 +0000877
878/* Print a (panic) message, and abort. */
879extern void VG_(panic) ( Char* str )
880 __attribute__ ((__noreturn__));
881
882/* Get memory by anonymous mmap. */
sewardje6a25242002-04-21 22:03:07 +0000883extern void* VG_(get_memory_from_mmap) ( Int nBytes );
884
885/* Crude stand-in for the glibc system() call. */
886extern Int VG_(system) ( Char* cmd );
887
sewardjde4a1d02002-03-22 01:27:54 +0000888
889/* Signal stuff. Note that these use the vk_ (kernel) structure
890 definitions, which are different in places from those that glibc
891 defines. Since we're operating right at the kernel interface,
892 glibc's view of the world is entirely irrelevant. */
sewardj018f7622002-05-15 21:13:39 +0000893
894/* --- Signal set ops --- */
sewardjb48e5002002-05-13 00:16:03 +0000895extern Int VG_(ksigfillset)( vki_ksigset_t* set );
896extern Int VG_(ksigemptyset)( vki_ksigset_t* set );
sewardjde4a1d02002-03-22 01:27:54 +0000897
sewardj018f7622002-05-15 21:13:39 +0000898extern Bool VG_(kisfullsigset)( vki_ksigset_t* set );
899extern Bool VG_(kisemptysigset)( vki_ksigset_t* set );
900
901extern Int VG_(ksigaddset)( vki_ksigset_t* set, Int signum );
902extern Int VG_(ksigdelset)( vki_ksigset_t* set, Int signum );
903extern Int VG_(ksigismember) ( vki_ksigset_t* set, Int signum );
904
sewardjb48e5002002-05-13 00:16:03 +0000905extern void VG_(ksigaddset_from_set)( vki_ksigset_t* dst,
906 vki_ksigset_t* src );
907extern void VG_(ksigdelset_from_set)( vki_ksigset_t* dst,
908 vki_ksigset_t* src );
909
sewardj018f7622002-05-15 21:13:39 +0000910/* --- Mess with the kernel's sig state --- */
911extern Int VG_(ksigprocmask)( Int how, const vki_ksigset_t* set,
912 vki_ksigset_t* oldset );
913extern Int VG_(ksigaction) ( Int signum,
914 const vki_ksigaction* act,
915 vki_ksigaction* oldact );
sewardjde4a1d02002-03-22 01:27:54 +0000916
917extern Int VG_(ksignal)(Int signum, void (*sighandler)(Int));
918
919extern Int VG_(ksigaltstack)( const vki_kstack_t* ss, vki_kstack_t* oss );
920
sewardj018f7622002-05-15 21:13:39 +0000921extern Int VG_(kill)( Int pid, Int signo );
sewardjde4a1d02002-03-22 01:27:54 +0000922
923
924/* ---------------------------------------------------------------------
925 Definitions for the JITter (vg_translate.c, vg_to_ucode.c,
926 vg_from_ucode.c).
927 ------------------------------------------------------------------ */
928
929/* Tags which describe what operands are. */
930typedef
931 enum { TempReg=0, ArchReg=1, RealReg=2,
932 SpillNo=3, Literal=4, Lit16=5,
933 NoValue=6 }
934 Tag;
935
936
937/* Microinstruction opcodes. */
938typedef
939 enum {
940 NOP,
941 GET,
942 PUT,
943 LOAD,
944 STORE,
945 MOV,
946 CMOV, /* Used for cmpxchg and cmov */
947 WIDEN,
948 JMP,
949
950 /* Read/write the %EFLAGS register into a TempReg. */
951 GETF, PUTF,
952
953 ADD, ADC, AND, OR, XOR, SUB, SBB,
954 SHL, SHR, SAR, ROL, ROR, RCL, RCR,
955 NOT, NEG, INC, DEC, BSWAP,
956 CC2VAL,
957
958 /* Not strictly needed, but useful for making better
959 translations of address calculations. */
960 LEA1, /* reg2 := const + reg1 */
961 LEA2, /* reg3 := const + reg1 + reg2 * 1,2,4 or 8 */
962
963 /* not for translating x86 calls -- only to call helpers */
964 CALLM_S, CALLM_E, /* Mark start and end of push/pop sequences
965 for CALLM. */
966 PUSH, POP, CLEAR, /* Add/remove/zap args for helpers. */
967 CALLM, /* call to a machine-code helper */
968
969 /* Hack for translating string (REP-) insns. Jump to literal if
970 TempReg/RealReg is zero. */
971 JIFZ,
972
973 /* FPU ops which read/write mem or don't touch mem at all. */
974 FPU_R,
975 FPU_W,
976 FPU,
977
978 /* Advance the simulated %eip by some small (< 128) number. */
979 INCEIP,
980
981 /* uinstrs which are not needed for mere translation of x86 code,
982 only for instrumentation of it. */
983 LOADV,
984 STOREV,
985 GETV,
986 PUTV,
987 TESTV,
988 SETV,
989 /* Get/set the v-bit (and it is only one bit) for the simulated
990 %eflags register. */
991 GETVF,
992 PUTVF,
993
994 /* Do a unary or binary tag op. Only for post-instrumented
995 code. For TAG1, first and only arg is a TempReg, and is both
996 arg and result reg. For TAG2, first arg is src, second is
997 dst, in the normal way; both are TempRegs. In both cases,
998 3rd arg is a RiCHelper with a Lit16 tag. This indicates
999 which tag op to do. */
1000 TAG1,
1001 TAG2
1002 }
1003 Opcode;
1004
1005
1006/* Condition codes, observing the Intel encoding. CondAlways is an
1007 extra. */
1008typedef
1009 enum {
1010 CondO = 0, /* overflow */
1011 CondNO = 1, /* no overflow */
1012 CondB = 2, /* below */
1013 CondNB = 3, /* not below */
1014 CondZ = 4, /* zero */
1015 CondNZ = 5, /* not zero */
1016 CondBE = 6, /* below or equal */
1017 CondNBE = 7, /* not below or equal */
1018 CondS = 8, /* negative */
1019 ConsNS = 9, /* not negative */
1020 CondP = 10, /* parity even */
1021 CondNP = 11, /* not parity even */
1022 CondL = 12, /* jump less */
1023 CondNL = 13, /* not less */
1024 CondLE = 14, /* less or equal */
1025 CondNLE = 15, /* not less or equal */
1026 CondAlways = 16 /* Jump always */
1027 }
1028 Condcode;
1029
1030
sewardj2e93c502002-04-12 11:12:52 +00001031/* Descriptions of additional properties of *unconditional* jumps. */
1032typedef
1033 enum {
1034 JmpBoring=0, /* boring unconditional jump */
1035 JmpCall=1, /* jump due to an x86 call insn */
1036 JmpRet=2, /* jump due to an x86 ret insn */
1037 JmpSyscall=3, /* do a system call, then jump */
1038 JmpClientReq=4 /* do a client request, then jump */
1039 }
1040 JmpKind;
1041
1042
sewardjde4a1d02002-03-22 01:27:54 +00001043/* Flags. User-level code can only read/write O(verflow), S(ign),
1044 Z(ero), A(ux-carry), C(arry), P(arity), and may also write
1045 D(irection). That's a total of 7 flags. A FlagSet is a bitset,
1046 thusly:
1047 76543210
1048 DOSZACP
1049 and bit 7 must always be zero since it is unused.
1050*/
1051typedef UChar FlagSet;
1052
1053#define FlagD (1<<6)
1054#define FlagO (1<<5)
1055#define FlagS (1<<4)
1056#define FlagZ (1<<3)
1057#define FlagA (1<<2)
1058#define FlagC (1<<1)
1059#define FlagP (1<<0)
1060
1061#define FlagsOSZACP (FlagO | FlagS | FlagZ | FlagA | FlagC | FlagP)
1062#define FlagsOSZAP (FlagO | FlagS | FlagZ | FlagA | FlagP)
1063#define FlagsOSZCP (FlagO | FlagS | FlagZ | FlagC | FlagP)
1064#define FlagsOSACP (FlagO | FlagS | FlagA | FlagC | FlagP)
1065#define FlagsSZACP ( FlagS | FlagZ | FlagA | FlagC | FlagP)
1066#define FlagsSZAP ( FlagS | FlagZ | FlagA | FlagP)
sewardj4a7456e2002-03-24 13:52:19 +00001067#define FlagsZCP ( FlagZ | FlagC | FlagP)
sewardjde4a1d02002-03-22 01:27:54 +00001068#define FlagsOC (FlagO | FlagC )
sewardj4d0ab1f2002-03-24 10:00:09 +00001069#define FlagsAC ( FlagA | FlagC )
sewardjde4a1d02002-03-22 01:27:54 +00001070
1071#define FlagsALL (FlagsOSZACP | FlagD)
1072#define FlagsEmpty (FlagSet)0
1073
1074#define VG_IS_FLAG_SUBSET(set1,set2) \
1075 (( ((FlagSet)set1) & ((FlagSet)set2) ) == ((FlagSet)set1) )
1076
1077#define VG_UNION_FLAG_SETS(set1,set2) \
1078 ( ((FlagSet)set1) | ((FlagSet)set2) )
1079
1080
1081
1082/* A Micro (u)-instruction. */
1083typedef
1084 struct {
1085 /* word 1 */
1086 UInt lit32; /* 32-bit literal */
1087
1088 /* word 2 */
1089 UShort val1; /* first operand */
1090 UShort val2; /* second operand */
1091
1092 /* word 3 */
1093 UShort val3; /* third operand */
1094 UChar opcode; /* opcode */
1095 UChar size; /* data transfer size */
1096
1097 /* word 4 */
1098 FlagSet flags_r; /* :: FlagSet */
1099 FlagSet flags_w; /* :: FlagSet */
1100 UChar tag1:4; /* first operand tag */
1101 UChar tag2:4; /* second operand tag */
1102 UChar tag3:4; /* third operand tag */
1103 UChar extra4b:4; /* Spare field, used by WIDEN for src
1104 -size, and by LEA2 for scale
njn4f9c9342002-04-29 16:03:24 +00001105 (1,2,4 or 8), and by unconditional JMPs for
1106 orig x86 instr size if --cachesim=yes */
1107
sewardjde4a1d02002-03-22 01:27:54 +00001108
1109 /* word 5 */
1110 UChar cond; /* condition, for jumps */
1111 Bool smc_check:1; /* do a smc test, if writes memory. */
1112 Bool signed_widen:1; /* signed or unsigned WIDEN ? */
sewardj2e93c502002-04-12 11:12:52 +00001113 JmpKind jmpkind:3; /* additional properties of unconditional JMP */
sewardjde4a1d02002-03-22 01:27:54 +00001114 }
1115 UInstr;
1116
1117
1118/* Expandable arrays of uinstrs. */
1119typedef
1120 struct {
1121 Int used;
1122 Int size;
1123 UInstr* instrs;
1124 Int nextTemp;
1125 }
1126 UCodeBlock;
1127
1128/* Refer to `the last instruction stuffed in', including as an
1129 lvalue. */
1130#define LAST_UINSTR(cb) (cb)->instrs[(cb)->used-1]
1131
1132/* An invalid temporary number :-) */
1133#define INVALID_TEMPREG 999999999
1134
1135
1136/* ---------------------------------------------------------------------
1137 Exports of vg_demangle.c
1138 ------------------------------------------------------------------ */
1139
1140extern void VG_(demangle) ( Char* orig, Char* result, Int result_size );
1141
1142
1143/* ---------------------------------------------------------------------
1144 Exports of vg_from_ucode.c
1145 ------------------------------------------------------------------ */
1146
1147extern UChar* VG_(emit_code) ( UCodeBlock* cb, Int* nbytes );
1148
1149
1150/* ---------------------------------------------------------------------
1151 Exports of vg_to_ucode.c
1152 ------------------------------------------------------------------ */
1153
1154extern Int VG_(disBB) ( UCodeBlock* cb, Addr eip0 );
1155extern Char* VG_(nameOfIntReg) ( Int size, Int reg );
1156extern Char VG_(nameOfIntSize) ( Int size );
1157extern UInt VG_(extend_s_8to32) ( UInt x );
1158extern Int VG_(getNewTemp) ( UCodeBlock* cb );
1159extern Int VG_(getNewShadow) ( UCodeBlock* cb );
1160
1161#define SHADOW(tempreg) ((tempreg)+1)
1162
1163
1164/* ---------------------------------------------------------------------
1165 Exports of vg_translate.c
1166 ------------------------------------------------------------------ */
1167
sewardj1e8cdc92002-04-18 11:37:52 +00001168extern void VG_(translate) ( ThreadState* tst,
1169 Addr orig_addr,
sewardjde4a1d02002-03-22 01:27:54 +00001170 UInt* orig_size,
1171 Addr* trans_addr,
1172 UInt* trans_size );
1173
1174extern void VG_(emptyUInstr) ( UInstr* u );
1175extern void VG_(newUInstr0) ( UCodeBlock* cb, Opcode opcode, Int sz );
1176extern void VG_(newUInstr1) ( UCodeBlock* cb, Opcode opcode, Int sz,
1177 Tag tag1, UInt val1 );
1178extern void VG_(newUInstr2) ( UCodeBlock* cb, Opcode opcode, Int sz,
1179 Tag tag1, UInt val1,
1180 Tag tag2, UInt val2 );
1181extern void VG_(newUInstr3) ( UCodeBlock* cb, Opcode opcode, Int sz,
1182 Tag tag1, UInt val1,
1183 Tag tag2, UInt val2,
1184 Tag tag3, UInt val3 );
1185extern void VG_(setFlagRW) ( UInstr* u,
1186 FlagSet fr, FlagSet fw );
1187
1188extern void VG_(setLiteralField) ( UCodeBlock* cb, UInt lit32 );
1189extern Bool VG_(anyFlagUse) ( UInstr* u );
1190
1191
1192
1193extern void VG_(ppUInstr) ( Int instrNo, UInstr* u );
1194extern void VG_(ppUCodeBlock) ( UCodeBlock* cb, Char* title );
1195
njn4f9c9342002-04-29 16:03:24 +00001196extern UCodeBlock* VG_(allocCodeBlock) ( void );
1197extern void VG_(freeCodeBlock) ( UCodeBlock* cb );
1198extern void VG_(copyUInstr) ( UCodeBlock* cb, UInstr* instr );
1199
sewardjde4a1d02002-03-22 01:27:54 +00001200extern Char* VG_(nameCondcode) ( Condcode cond );
1201extern Bool VG_(saneUInstr) ( Bool beforeRA, UInstr* u );
1202extern Bool VG_(saneUCodeBlock) ( UCodeBlock* cb );
1203extern Char* VG_(nameUOpcode) ( Bool upper, Opcode opc );
1204extern Int VG_(rankToRealRegNo) ( Int rank );
1205
1206extern void* VG_(jitmalloc) ( Int nbytes );
1207extern void VG_(jitfree) ( void* ptr );
1208
1209
1210/* ---------------------------------------------------------------------
1211 Exports of vg_execontext.c.
1212 ------------------------------------------------------------------ */
1213
1214/* Records the PC and a bit of the call chain. The first 4 %eip
1215 values are used in comparisons do remove duplicate errors, and for
1216 comparing against suppression specifications. The rest are purely
1217 informational (but often important). */
1218
1219typedef
1220 struct _ExeContextRec {
1221 struct _ExeContextRec * next;
1222 /* The size of this array is VG_(clo_backtrace_size); at least
1223 2, at most VG_DEEPEST_BACKTRACE. [0] is the current %eip,
1224 [1] is its caller, [2] is the caller of [1], etc. */
1225 Addr eips[0];
1226 }
1227 ExeContext;
1228
1229
1230/* Initialise the ExeContext storage mechanism. */
1231extern void VG_(init_ExeContext_storage) ( void );
1232
1233/* Print stats (informational only). */
1234extern void VG_(show_ExeContext_stats) ( void );
1235
1236
1237/* Take a snapshot of the client's stack. Search our collection of
1238 ExeContexts to see if we already have it, and if not, allocate a
1239 new one. Either way, return a pointer to the context. */
sewardj8c824512002-04-14 04:16:48 +00001240extern ExeContext* VG_(get_ExeContext) ( Bool skip_top_frame,
1241 Addr eip, Addr ebp );
sewardjde4a1d02002-03-22 01:27:54 +00001242
1243/* Print an ExeContext. */
1244extern void VG_(pp_ExeContext) ( ExeContext* );
1245
1246/* Compare two ExeContexts, just comparing the top two callers. */
1247extern Bool VG_(eq_ExeContext_top2) ( ExeContext* e1, ExeContext* e2 );
1248
1249/* Compare two ExeContexts, just comparing the top four callers. */
1250extern Bool VG_(eq_ExeContext_top4) ( ExeContext* e1, ExeContext* e2 );
1251
1252/* Compare two ExeContexts, comparing all callers. */
1253extern Bool VG_(eq_ExeContext_all) ( ExeContext* e1, ExeContext* e2 );
1254
1255
1256
1257/* ---------------------------------------------------------------------
1258 Exports of vg_errcontext.c.
1259 ------------------------------------------------------------------ */
1260
1261extern void VG_(load_suppressions) ( void );
1262extern void VG_(show_all_errors) ( void );
1263extern void VG_(record_value_error) ( Int size );
sewardjaabd5ad2002-04-19 15:43:37 +00001264extern void VG_(record_free_error) ( ThreadState* tst, Addr a );
1265extern void VG_(record_freemismatch_error) ( ThreadState* tst, Addr a );
sewardjde4a1d02002-03-22 01:27:54 +00001266extern void VG_(record_address_error) ( Addr a, Int size,
1267 Bool isWrite );
sewardj1e8cdc92002-04-18 11:37:52 +00001268
1269extern void VG_(record_jump_error) ( ThreadState* tst, Addr a );
sewardj8c824512002-04-14 04:16:48 +00001270
1271extern void VG_(record_param_err) ( ThreadState* tst,
1272 Addr a,
sewardjde4a1d02002-03-22 01:27:54 +00001273 Bool isWriteLack,
1274 Char* msg );
sewardj8c824512002-04-14 04:16:48 +00001275extern void VG_(record_user_err) ( ThreadState* tst,
1276 Addr a, Bool isWriteLack );
sewardjde4a1d02002-03-22 01:27:54 +00001277
1278
1279/* The classification of a faulting address. */
1280typedef
sewardjb581a132002-05-08 00:32:50 +00001281 enum { Undescribed, /* as-yet unclassified */
1282 Stack,
1283 Unknown, /* classification yielded nothing useful */
1284 Freed, Mallocd,
1285 UserG, UserS }
sewardjde4a1d02002-03-22 01:27:54 +00001286 AddrKind;
1287
1288/* Records info about a faulting address. */
1289typedef
1290 struct {
1291 /* ALL */
1292 AddrKind akind;
1293 /* Freed, Mallocd */
1294 Int blksize;
1295 /* Freed, Mallocd */
1296 Int rwoffset;
1297 /* Freed, Mallocd */
1298 ExeContext* lastchange;
sewardj1e8cdc92002-04-18 11:37:52 +00001299 /* Stack */
1300 ThreadId stack_tid;
sewardjb581a132002-05-08 00:32:50 +00001301 /* True if is just-below %esp -- could be a gcc bug. */
1302 Bool maybe_gcc;
sewardjde4a1d02002-03-22 01:27:54 +00001303 }
1304 AddrInfo;
1305
1306
1307/* ---------------------------------------------------------------------
1308 Exports of vg_clientperms.c
1309 ------------------------------------------------------------------ */
1310
1311extern Bool VG_(client_perm_maybe_describe)( Addr a, AddrInfo* ai );
1312
sewardj8c824512002-04-14 04:16:48 +00001313extern UInt VG_(handle_client_request) ( ThreadState* tst, UInt* arg_block );
sewardjde4a1d02002-03-22 01:27:54 +00001314
1315extern void VG_(delete_client_stack_blocks_following_ESP_change) ( void );
1316
1317extern void VG_(show_client_block_stats) ( void );
1318
1319
1320/* ---------------------------------------------------------------------
1321 Exports of vg_procselfmaps.c
1322 ------------------------------------------------------------------ */
1323
1324extern
1325void VG_(read_procselfmaps) (
1326 void (*record_mapping)( Addr, UInt, Char, Char, Char, UInt, UChar* )
1327);
1328
1329
1330/* ---------------------------------------------------------------------
1331 Exports of vg_symtab2.c
1332 ------------------------------------------------------------------ */
1333
1334/* We assume the executable is loaded here ... can't really find
1335 out. There is a hacky sanity check in vg_init_memory_audit()
1336 which should trip up most stupidities.
1337*/
1338#define VG_ASSUMED_EXE_BASE (Addr)0x8048000
1339
1340extern void VG_(read_symbols) ( void );
1341extern void VG_(mini_stack_dump) ( ExeContext* ec );
1342extern void VG_(what_obj_and_fun_is_this)
1343 ( Addr a,
1344 Char* obj_buf, Int n_obj_buf,
1345 Char* fun_buf, Int n_fun_buf );
njn4f9c9342002-04-29 16:03:24 +00001346extern Bool VG_(what_line_is_this) ( Addr a,
1347 UChar* filename, Int n_filename,
1348 UInt* lineno );
1349extern Bool VG_(what_fn_is_this) ( Bool no_demangle, Addr a,
1350 Char* fn_name, Int n_fn_name);
sewardjde4a1d02002-03-22 01:27:54 +00001351
sewardj18d75132002-05-16 11:06:21 +00001352extern Bool VG_(symtab_notify_munmap) ( Addr start, UInt length );
sewardjde4a1d02002-03-22 01:27:54 +00001353
1354
1355/* ---------------------------------------------------------------------
1356 Exports of vg_clientmalloc.c
1357 ------------------------------------------------------------------ */
1358
sewardjde4a1d02002-03-22 01:27:54 +00001359typedef
1360 enum {
1361 Vg_AllocMalloc = 0,
sewardj2e93c502002-04-12 11:12:52 +00001362 Vg_AllocNew = 1,
sewardjde4a1d02002-03-22 01:27:54 +00001363 Vg_AllocNewVec = 2
1364 }
1365 VgAllocKind;
1366
1367/* Description of a malloc'd chunk. */
1368typedef
1369 struct _ShadowChunk {
1370 struct _ShadowChunk* next;
1371 ExeContext* where; /* where malloc'd/free'd */
1372 UInt size : 30; /* size requested. */
1373 VgAllocKind allockind : 2; /* which wrapper did the allocation */
1374 Addr data; /* ptr to actual block. */
1375 }
1376 ShadowChunk;
1377
1378extern void VG_(clientmalloc_done) ( void );
1379extern void VG_(describe_addr) ( Addr a, AddrInfo* ai );
1380extern ShadowChunk** VG_(get_malloc_shadows) ( /*OUT*/ UInt* n_shadows );
1381
sewardj2e93c502002-04-12 11:12:52 +00001382/* These are called from the scheduler, when it intercepts a user
1383 request. */
sewardj8c824512002-04-14 04:16:48 +00001384extern void* VG_(client_malloc) ( ThreadState* tst,
1385 UInt size, VgAllocKind kind );
1386extern void* VG_(client_memalign) ( ThreadState* tst,
1387 UInt align, UInt size );
1388extern void VG_(client_free) ( ThreadState* tst,
1389 void* ptrV, VgAllocKind kind );
1390extern void* VG_(client_calloc) ( ThreadState* tst,
1391 UInt nmemb, UInt size1 );
1392extern void* VG_(client_realloc) ( ThreadState* tst,
1393 void* ptrV, UInt size_new );
sewardjde4a1d02002-03-22 01:27:54 +00001394
1395
1396/* ---------------------------------------------------------------------
1397 Exports of vg_main.c
1398 ------------------------------------------------------------------ */
1399
sewardjde4a1d02002-03-22 01:27:54 +00001400/* A structure used as an intermediary when passing the simulated
1401 CPU's state to some assembly fragments, particularly system calls.
1402 Stuff is copied from baseBlock to here, the assembly magic runs,
1403 and then the inverse copy is done. */
1404
1405extern UInt VG_(m_state_static) [8 /* int regs, in Intel order */
1406 + 1 /* %eflags */
1407 + 1 /* %eip */
1408 + VG_SIZE_OF_FPUSTATE_W /* FPU state */
1409 ];
1410
1411/* Handy fns for doing the copy back and forth. */
1412extern void VG_(copy_baseBlock_to_m_state_static) ( void );
1413extern void VG_(copy_m_state_static_to_baseBlock) ( void );
1414
sewardjde4a1d02002-03-22 01:27:54 +00001415/* Called when some unhandleable client behaviour is detected.
1416 Prints a msg and aborts. */
1417extern void VG_(unimplemented) ( Char* msg );
sewardjcfc39b22002-05-08 01:58:18 +00001418extern void VG_(nvidia_moan) ( void );
sewardjde4a1d02002-03-22 01:27:54 +00001419
1420/* The stack on which Valgrind runs. We can't use the same stack as the
1421 simulatee -- that's an important design decision. */
1422extern UInt VG_(stack)[10000];
1423
1424/* Similarly, we have to ask for signals to be delivered on an
1425 alternative stack, since it is possible, although unlikely, that
1426 we'll have to run client code from inside the Valgrind-installed
1427 signal handler. If this happens it will be done by
1428 vg_deliver_signal_immediately(). */
1429extern UInt VG_(sigstack)[10000];
1430
sewardjde4a1d02002-03-22 01:27:54 +00001431/* Holds client's %esp at the point we gained control. From this the
1432 client's argc, argv and envp are deduced. */
1433extern Addr VG_(esp_at_startup);
1434extern Int VG_(client_argc);
1435extern Char** VG_(client_argv);
1436extern Char** VG_(client_envp);
1437
1438/* Remove valgrind.so from a LD_PRELOAD=... string so child processes
sewardj3e1eb1f2002-05-18 13:14:17 +00001439 don't get traced into. Also mess up $libdir/valgrind so that our
1440 libpthread.so disappears from view. */
1441void VG_(mash_LD_PRELOAD_and_LD_LIBRARY_PATH) ( Char* ld_preload_str,
1442 Char* ld_library_path_str );
sewardjde4a1d02002-03-22 01:27:54 +00001443
1444/* Something of a function looking for a home ... start up GDB. This
1445 is called from VG_(swizzle_esp_then_start_GDB) and so runs on the
1446 *client's* stack. This is necessary to give GDB the illusion that
1447 the client program really was running on the real cpu. */
1448extern void VG_(start_GDB_whilst_on_client_stack) ( void );
1449
1450/* Spew out vast amounts of junk during JITting? */
1451extern Bool VG_(disassemble);
1452
1453/* 64-bit counter for the number of basic blocks done. */
1454extern ULong VG_(bbs_done);
1455/* 64-bit counter for the number of bbs to go before a debug exit. */
1456extern ULong VG_(bbs_to_go);
1457
1458/* Counts downwards in vg_run_innerloop. */
1459extern UInt VG_(dispatch_ctr);
1460
sewardjde4a1d02002-03-22 01:27:54 +00001461/* Is the client running on the simulated CPU or the real one? */
1462extern Bool VG_(running_on_simd_CPU); /* Initially False */
1463
1464/* The current LRU epoch. */
1465extern UInt VG_(current_epoch);
1466
sewardj7e87e382002-05-03 19:09:05 +00001467/* This is the ThreadId of the last thread the scheduler ran. */
1468extern ThreadId VG_(last_run_tid);
1469
sewardjde4a1d02002-03-22 01:27:54 +00001470
1471/* --- Counters, for informational purposes only. --- */
1472
1473/* Number of lookups which miss the fast tt helper. */
1474extern UInt VG_(tt_fast_misses);
1475
1476/* Counts for LRU informational messages. */
1477
1478/* Number and total o/t size of new translations this epoch. */
1479extern UInt VG_(this_epoch_in_count);
1480extern UInt VG_(this_epoch_in_osize);
1481extern UInt VG_(this_epoch_in_tsize);
1482/* Number and total o/t size of discarded translations this epoch. */
1483extern UInt VG_(this_epoch_out_count);
1484extern UInt VG_(this_epoch_out_osize);
1485extern UInt VG_(this_epoch_out_tsize);
1486/* Number and total o/t size of translations overall. */
1487extern UInt VG_(overall_in_count);
1488extern UInt VG_(overall_in_osize);
1489extern UInt VG_(overall_in_tsize);
1490/* Number and total o/t size of discards overall. */
1491extern UInt VG_(overall_out_count);
1492extern UInt VG_(overall_out_osize);
1493extern UInt VG_(overall_out_tsize);
1494
1495/* The number of LRU-clearings of TT/TC. */
1496extern UInt VG_(number_of_lrus);
1497
1498/* Counts pertaining to the register allocator. */
1499
1500/* total number of uinstrs input to reg-alloc */
1501extern UInt VG_(uinstrs_prealloc);
1502
1503/* total number of uinstrs added due to spill code */
1504extern UInt VG_(uinstrs_spill);
1505
1506/* number of bbs requiring spill code */
1507extern UInt VG_(translations_needing_spill);
1508
1509/* total of register ranks over all translations */
1510extern UInt VG_(total_reg_rank);
1511
sewardjde4a1d02002-03-22 01:27:54 +00001512/* Counts pertaining to internal sanity checking. */
1513extern UInt VG_(sanity_fast_count);
1514extern UInt VG_(sanity_slow_count);
1515
sewardj2e93c502002-04-12 11:12:52 +00001516/* Counts pertaining to the scheduler. */
1517extern UInt VG_(num_scheduling_events_MINOR);
1518extern UInt VG_(num_scheduling_events_MAJOR);
1519
sewardjde4a1d02002-03-22 01:27:54 +00001520
1521/* ---------------------------------------------------------------------
1522 Exports of vg_memory.c
1523 ------------------------------------------------------------------ */
1524
1525extern void VGM_(init_memory_audit) ( void );
1526extern Addr VGM_(curr_dataseg_end);
1527extern void VG_(show_reg_tags) ( void );
1528extern void VG_(detect_memory_leaks) ( void );
1529extern void VG_(done_prof_mem) ( void );
1530
1531/* Set permissions for an address range. Not speed-critical. */
1532extern void VGM_(make_noaccess) ( Addr a, UInt len );
1533extern void VGM_(make_writable) ( Addr a, UInt len );
1534extern void VGM_(make_readable) ( Addr a, UInt len );
1535/* Use with care! (read: use for shmat only) */
1536extern void VGM_(make_readwritable) ( Addr a, UInt len );
1537extern void VGM_(copy_address_range_perms) ( Addr src, Addr dst,
1538 UInt len );
1539
1540/* Check permissions for an address range. Not speed-critical. */
1541extern Bool VGM_(check_writable) ( Addr a, UInt len, Addr* bad_addr );
1542extern Bool VGM_(check_readable) ( Addr a, UInt len, Addr* bad_addr );
1543extern Bool VGM_(check_readable_asciiz) ( Addr a, Addr* bad_addr );
1544
sewardj0c3b53f2002-05-01 01:58:35 +00001545/* Sanity checks which may be done at any time. The scheduler decides
1546 when. */
1547extern void VG_(do_sanity_checks) ( Bool force_expensive );
sewardjde4a1d02002-03-22 01:27:54 +00001548/* Very cheap ... */
1549extern Bool VG_(first_and_last_secondaries_look_plausible) ( void );
1550
1551/* These functions are called from generated code. */
1552extern void VG_(helperc_STOREV4) ( UInt, Addr );
1553extern void VG_(helperc_STOREV2) ( UInt, Addr );
1554extern void VG_(helperc_STOREV1) ( UInt, Addr );
1555
1556extern UInt VG_(helperc_LOADV1) ( Addr );
1557extern UInt VG_(helperc_LOADV2) ( Addr );
1558extern UInt VG_(helperc_LOADV4) ( Addr );
1559
1560extern void VGM_(handle_esp_assignment) ( Addr new_espA );
1561extern void VGM_(fpu_write_check) ( Addr addr, Int size );
1562extern void VGM_(fpu_read_check) ( Addr addr, Int size );
1563
1564/* Safely (avoiding SIGSEGV / SIGBUS) scan the entire valid address
1565 space and pass the addresses and values of all addressible,
1566 defined, aligned words to notify_word. This is the basis for the
1567 leak detector. Returns the number of calls made to notify_word. */
1568UInt VG_(scan_all_valid_memory) ( void (*notify_word)( Addr, UInt ) );
1569
1570/* Is this address within some small distance below %ESP? Used only
1571 for the --workaround-gcc296-bugs kludge. */
sewardj8c824512002-04-14 04:16:48 +00001572extern Bool VG_(is_just_below_ESP)( Addr esp, Addr aa );
sewardjde4a1d02002-03-22 01:27:54 +00001573
1574/* Nasty kludgery to deal with applications which switch stacks,
1575 like netscape. */
sewardjde4a1d02002-03-22 01:27:54 +00001576#define VG_PLAUSIBLE_STACK_SIZE 8000000
1577
sewardjc3bd5f52002-05-01 03:24:23 +00001578/* Needed by the pthreads implementation. */
1579#define VGM_WORD_VALID 0
1580#define VGM_WORD_INVALID 0xFFFFFFFF
1581
sewardjde4a1d02002-03-22 01:27:54 +00001582
1583/* ---------------------------------------------------------------------
1584 Exports of vg_syscall_mem.c
1585 ------------------------------------------------------------------ */
1586
sewardj2e93c502002-04-12 11:12:52 +00001587extern void VG_(perform_assumed_nonblocking_syscall) ( ThreadId tid );
sewardjde4a1d02002-03-22 01:27:54 +00001588
sewardj2e93c502002-04-12 11:12:52 +00001589extern void VG_(check_known_blocking_syscall) ( ThreadId tid,
1590 Int syscallno,
1591 Int* /*IN*/ res );
sewardjde4a1d02002-03-22 01:27:54 +00001592
1593extern Bool VG_(is_kerror) ( Int res );
1594
sewardj018f7622002-05-15 21:13:39 +00001595#define KERNEL_DO_SYSCALL(thread_id, result_lvalue) \
1596 VG_(load_thread_state)(thread_id); \
1597 VG_(copy_baseBlock_to_m_state_static)(); \
1598 VG_(do_syscall)(); \
1599 VG_(copy_m_state_static_to_baseBlock)(); \
1600 VG_(save_thread_state)(thread_id); \
1601 VG_(threads)[thread_id].sh_eax = VGM_WORD_VALID; \
1602 result_lvalue = VG_(threads)[thread_id].m_eax;
sewardjde4a1d02002-03-22 01:27:54 +00001603
1604
1605/* ---------------------------------------------------------------------
1606 Exports of vg_transtab.c
1607 ------------------------------------------------------------------ */
1608
1609/* An entry in the translation table (TT). */
1610typedef
1611 struct {
1612 /* +0 */ Addr orig_addr;
1613 /* +4 */ Addr trans_addr;
1614 /* +8 */ UInt mru_epoch;
1615 /* +12 */ UShort orig_size;
1616 /* +14 */ UShort trans_size;
1617 }
1618 TTEntry;
1619
1620/* The number of basic blocks in an epoch (one age-step). */
1621#define VG_BBS_PER_EPOCH 20000
1622
1623extern void VG_(get_tt_tc_used) ( UInt* tt_used, UInt* tc_used );
1624extern void VG_(maybe_do_lru_pass) ( void );
1625extern void VG_(flush_transtab) ( void );
1626extern Addr VG_(copy_to_transcache) ( Addr trans_addr, Int trans_size );
1627extern void VG_(add_to_trans_tab) ( TTEntry* tte );
sewardj18d75132002-05-16 11:06:21 +00001628extern void VG_(invalidate_translations) ( Addr start, UInt range );
sewardjde4a1d02002-03-22 01:27:54 +00001629
sewardj18d75132002-05-16 11:06:21 +00001630extern void VG_(init_tt_tc) ( void );
sewardjde4a1d02002-03-22 01:27:54 +00001631
1632extern void VG_(sanity_check_tc_tt) ( void );
1633extern Addr VG_(search_transtab) ( Addr original_addr );
1634
1635extern void VG_(invalidate_tt_fast)( void );
1636
1637
1638/* ---------------------------------------------------------------------
1639 Exports of vg_vtagops.c
1640 ------------------------------------------------------------------ */
1641
1642/* Lists the names of value-tag operations used in instrumented
1643 code. These are the third argument to TAG1 and TAG2 uinsns. */
1644
1645typedef
1646 enum {
1647 /* Unary. */
1648 VgT_PCast40, VgT_PCast20, VgT_PCast10,
1649 VgT_PCast01, VgT_PCast02, VgT_PCast04,
1650
1651 VgT_PCast14, VgT_PCast12, VgT_PCast11,
1652
1653 VgT_Left4, VgT_Left2, VgT_Left1,
1654
1655 VgT_SWiden14, VgT_SWiden24, VgT_SWiden12,
1656 VgT_ZWiden14, VgT_ZWiden24, VgT_ZWiden12,
1657
1658 /* Binary; 1st is rd; 2nd is rd+wr */
1659 VgT_UifU4, VgT_UifU2, VgT_UifU1, VgT_UifU0,
1660 VgT_DifD4, VgT_DifD2, VgT_DifD1,
1661
1662 VgT_ImproveAND4_TQ, VgT_ImproveAND2_TQ, VgT_ImproveAND1_TQ,
1663 VgT_ImproveOR4_TQ, VgT_ImproveOR2_TQ, VgT_ImproveOR1_TQ,
1664 VgT_DebugFn
1665 }
1666 VgTagOp;
1667
1668extern Char* VG_(nameOfTagOp) ( VgTagOp );
1669extern UInt VG_(DebugFn) ( UInt a1, UInt a2 );
1670
1671
1672/* ---------------------------------------------------------------------
1673 Exports of vg_syscall.S
1674 ------------------------------------------------------------------ */
1675
1676extern void VG_(do_syscall) ( void );
1677
1678
1679/* ---------------------------------------------------------------------
1680 Exports of vg_startup.S
1681 ------------------------------------------------------------------ */
1682
sewardjde4a1d02002-03-22 01:27:54 +00001683extern void VG_(switch_to_real_CPU) ( void );
1684
sewardj35805422002-04-21 13:05:34 +00001685extern void VG_(swizzle_esp_then_start_GDB) ( Addr m_eip_at_error,
1686 Addr m_esp_at_error,
1687 Addr m_ebp_at_error );
sewardjde4a1d02002-03-22 01:27:54 +00001688
1689
1690/* ---------------------------------------------------------------------
1691 Exports of vg_dispatch.S
1692 ------------------------------------------------------------------ */
1693
sewardj2e93c502002-04-12 11:12:52 +00001694/* Run a thread for a (very short) while, until some event happens
1695 which means we need to defer to the scheduler. */
1696extern UInt VG_(run_innerloop) ( void );
sewardjde4a1d02002-03-22 01:27:54 +00001697
1698
1699/* ---------------------------------------------------------------------
1700 Exports of vg_helpers.S
1701 ------------------------------------------------------------------ */
1702
sewardjde4a1d02002-03-22 01:27:54 +00001703/* Mul, div, etc, -- we don't codegen these directly. */
1704extern void VG_(helper_idiv_64_32);
1705extern void VG_(helper_div_64_32);
1706extern void VG_(helper_idiv_32_16);
1707extern void VG_(helper_div_32_16);
1708extern void VG_(helper_idiv_16_8);
1709extern void VG_(helper_div_16_8);
1710
1711extern void VG_(helper_imul_32_64);
1712extern void VG_(helper_mul_32_64);
1713extern void VG_(helper_imul_16_32);
1714extern void VG_(helper_mul_16_32);
1715extern void VG_(helper_imul_8_16);
1716extern void VG_(helper_mul_8_16);
1717
1718extern void VG_(helper_CLD);
1719extern void VG_(helper_STD);
1720extern void VG_(helper_get_dirflag);
1721
1722extern void VG_(helper_shldl);
1723extern void VG_(helper_shldw);
1724extern void VG_(helper_shrdl);
1725extern void VG_(helper_shrdw);
1726
1727extern void VG_(helper_RDTSC);
1728extern void VG_(helper_CPUID);
1729
sewardjde4a1d02002-03-22 01:27:54 +00001730extern void VG_(helper_bsf);
1731extern void VG_(helper_bsr);
1732
1733extern void VG_(helper_fstsw_AX);
1734extern void VG_(helper_SAHF);
sewardj4d0ab1f2002-03-24 10:00:09 +00001735extern void VG_(helper_DAS);
sewardjfe8a1662002-03-24 11:54:07 +00001736extern void VG_(helper_DAA);
sewardjde4a1d02002-03-22 01:27:54 +00001737
1738extern void VG_(helper_value_check4_fail);
1739extern void VG_(helper_value_check2_fail);
1740extern void VG_(helper_value_check1_fail);
1741extern void VG_(helper_value_check0_fail);
1742
sewardj20917d82002-05-28 01:36:45 +00001743/* NOT A FUNCTION; this is a bogus RETURN ADDRESS. */
sewardj54cacf02002-04-12 23:24:59 +00001744extern void VG_(signalreturn_bogusRA)( void );
sewardj20917d82002-05-28 01:36:45 +00001745
sewardj54cacf02002-04-12 23:24:59 +00001746
njn4f9c9342002-04-29 16:03:24 +00001747/* ---------------------------------------------------------------------
1748 Exports of vg_cachesim.c
1749 ------------------------------------------------------------------ */
1750
1751extern UCodeBlock* VG_(cachesim_instrument)(UCodeBlock* cb_in, Addr orig_addr);
1752
1753typedef struct _iCC iCC;
1754typedef struct _idCC idCC;
1755
1756extern void VG_(init_cachesim) ( void );
1757extern void VG_(show_cachesim_results)( Int client_argc, Char** client_argv );
1758
1759extern void VG_(cachesim_log_non_mem_instr)( iCC* cc );
1760extern void VG_(cachesim_log_mem_instr) ( idCC* cc, Addr data_addr );
sewardjde4a1d02002-03-22 01:27:54 +00001761
sewardj18d75132002-05-16 11:06:21 +00001762extern void VG_(cachesim_notify_discard) ( TTEntry* tte );
1763
1764
sewardjde4a1d02002-03-22 01:27:54 +00001765/* ---------------------------------------------------------------------
1766 The state of the simulated CPU.
1767 ------------------------------------------------------------------ */
1768
1769/* This is the Intel register encoding. */
1770#define R_EAX 0
1771#define R_ECX 1
1772#define R_EDX 2
1773#define R_EBX 3
1774#define R_ESP 4
1775#define R_EBP 5
1776#define R_ESI 6
1777#define R_EDI 7
1778
1779#define R_AL (0+R_EAX)
1780#define R_CL (0+R_ECX)
1781#define R_DL (0+R_EDX)
1782#define R_BL (0+R_EBX)
1783#define R_AH (4+R_EAX)
1784#define R_CH (4+R_ECX)
1785#define R_DH (4+R_EDX)
1786#define R_BH (4+R_EBX)
1787
1788
1789/* ---------------------------------------------------------------------
1790 Offsets into baseBlock for everything which needs to referred to
1791 from generated code. The order of these decls does not imply
1792 what the order of the actual offsets is. The latter is important
1793 and is set up in vg_main.c.
1794 ------------------------------------------------------------------ */
1795
1796/* An array of words. In generated code, %ebp always points to the
1797 start of this array. Useful stuff, like the simulated CPU state,
1798 and the addresses of helper functions, can then be found by
1799 indexing off %ebp. The following declares variables which, at
1800 startup time, are given values denoting offsets into baseBlock.
1801 These offsets are in *words* from the start of baseBlock. */
1802
1803#define VG_BASEBLOCK_WORDS 200
1804
1805extern UInt VG_(baseBlock)[VG_BASEBLOCK_WORDS];
1806
1807
1808/* -----------------------------------------------------
1809 Read-write parts of baseBlock.
1810 -------------------------------------------------- */
1811
1812/* State of the simulated CPU. */
1813extern Int VGOFF_(m_eax);
1814extern Int VGOFF_(m_ecx);
1815extern Int VGOFF_(m_edx);
1816extern Int VGOFF_(m_ebx);
1817extern Int VGOFF_(m_esp);
1818extern Int VGOFF_(m_ebp);
1819extern Int VGOFF_(m_esi);
1820extern Int VGOFF_(m_edi);
1821extern Int VGOFF_(m_eflags);
1822extern Int VGOFF_(m_fpustate);
1823extern Int VGOFF_(m_eip);
1824
1825/* Reg-alloc spill area (VG_MAX_SPILLSLOTS words long). */
1826extern Int VGOFF_(spillslots);
1827
1828/* Records the valid bits for the 8 integer regs & flags reg. */
1829extern Int VGOFF_(sh_eax);
1830extern Int VGOFF_(sh_ecx);
1831extern Int VGOFF_(sh_edx);
1832extern Int VGOFF_(sh_ebx);
1833extern Int VGOFF_(sh_esp);
1834extern Int VGOFF_(sh_ebp);
1835extern Int VGOFF_(sh_esi);
1836extern Int VGOFF_(sh_edi);
1837extern Int VGOFF_(sh_eflags);
1838
1839
1840/* -----------------------------------------------------
1841 Read-only parts of baseBlock.
1842 -------------------------------------------------- */
1843
1844/* Offsets of addresses of helper functions. A "helper" function is
1845 one which is called from generated code. */
1846
1847extern Int VGOFF_(helper_idiv_64_32);
1848extern Int VGOFF_(helper_div_64_32);
1849extern Int VGOFF_(helper_idiv_32_16);
1850extern Int VGOFF_(helper_div_32_16);
1851extern Int VGOFF_(helper_idiv_16_8);
1852extern Int VGOFF_(helper_div_16_8);
1853
1854extern Int VGOFF_(helper_imul_32_64);
1855extern Int VGOFF_(helper_mul_32_64);
1856extern Int VGOFF_(helper_imul_16_32);
1857extern Int VGOFF_(helper_mul_16_32);
1858extern Int VGOFF_(helper_imul_8_16);
1859extern Int VGOFF_(helper_mul_8_16);
1860
1861extern Int VGOFF_(helper_CLD);
1862extern Int VGOFF_(helper_STD);
1863extern Int VGOFF_(helper_get_dirflag);
1864
1865extern Int VGOFF_(helper_shldl);
1866extern Int VGOFF_(helper_shldw);
1867extern Int VGOFF_(helper_shrdl);
1868extern Int VGOFF_(helper_shrdw);
1869
1870extern Int VGOFF_(helper_RDTSC);
1871extern Int VGOFF_(helper_CPUID);
1872
sewardjde4a1d02002-03-22 01:27:54 +00001873extern Int VGOFF_(helper_bsf);
1874extern Int VGOFF_(helper_bsr);
1875
1876extern Int VGOFF_(helper_fstsw_AX);
1877extern Int VGOFF_(helper_SAHF);
sewardj4d0ab1f2002-03-24 10:00:09 +00001878extern Int VGOFF_(helper_DAS);
sewardjfe8a1662002-03-24 11:54:07 +00001879extern Int VGOFF_(helper_DAA);
sewardjde4a1d02002-03-22 01:27:54 +00001880
1881extern Int VGOFF_(helper_value_check4_fail);
1882extern Int VGOFF_(helper_value_check2_fail);
1883extern Int VGOFF_(helper_value_check1_fail);
1884extern Int VGOFF_(helper_value_check0_fail);
1885
sewardjde4a1d02002-03-22 01:27:54 +00001886extern Int VGOFF_(helperc_STOREV4); /* :: UInt -> Addr -> void */
1887extern Int VGOFF_(helperc_STOREV2); /* :: UInt -> Addr -> void */
1888extern Int VGOFF_(helperc_STOREV1); /* :: UInt -> Addr -> void */
1889
1890extern Int VGOFF_(helperc_LOADV4); /* :: Addr -> UInt -> void */
1891extern Int VGOFF_(helperc_LOADV2); /* :: Addr -> UInt -> void */
1892extern Int VGOFF_(helperc_LOADV1); /* :: Addr -> UInt -> void */
1893
1894extern Int VGOFF_(handle_esp_assignment); /* :: Addr -> void */
1895extern Int VGOFF_(fpu_write_check); /* :: Addr -> Int -> void */
1896extern Int VGOFF_(fpu_read_check); /* :: Addr -> Int -> void */
1897
njn4f9c9342002-04-29 16:03:24 +00001898extern Int VGOFF_(cachesim_log_non_mem_instr);
1899extern Int VGOFF_(cachesim_log_mem_instr);
sewardjde4a1d02002-03-22 01:27:54 +00001900
1901#endif /* ndef __VG_INCLUDE_H */
1902
sewardj3b2736a2002-03-24 12:18:35 +00001903
1904/* ---------------------------------------------------------------------
1905 Finally - autoconf-generated settings
1906 ------------------------------------------------------------------ */
1907
1908#include "config.h"
1909
sewardjde4a1d02002-03-22 01:27:54 +00001910/*--------------------------------------------------------------------*/
1911/*--- end vg_include.h ---*/
1912/*--------------------------------------------------------------------*/