blob: ec923b18814ba70f71906f87ad6293f50bc119c9 [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. */
sewardj7989d0c2002-05-28 11:00:01 +0000140#define VG_N_THREADS 20
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. */
144#define VG_N_THREAD_KEYS 10
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
194
195/* ---------------------------------------------------------------------
196 Now the basic types are set up, we can haul in the kernel-interface
197 definitions.
198 ------------------------------------------------------------------ */
199
200#include "./vg_kerneliface.h"
201
202
203/* ---------------------------------------------------------------------
204 Command-line-settable options
205 ------------------------------------------------------------------ */
206
207#define VG_CLO_SMC_NONE 0
208#define VG_CLO_SMC_SOME 1
209#define VG_CLO_SMC_ALL 2
210
211#define VG_CLO_MAX_SFILES 10
212
sewardj97ced732002-03-25 00:07:36 +0000213/* Shall we V-check addrs (they are always A checked too): default: YES */
214extern Bool VG_(clo_check_addrVs);
sewardjde4a1d02002-03-22 01:27:54 +0000215/* Enquire about whether to attach to GDB at errors? default: NO */
216extern Bool VG_(clo_GDB_attach);
217/* Sanity-check level: 0 = none, 1 (default), > 1 = expensive. */
218extern Int VG_(sanity_level);
219/* Verbosity level: 0 = silent, 1 (default), > 1 = more verbose. */
220extern Int VG_(clo_verbosity);
221/* Automatically attempt to demangle C++ names? default: YES */
222extern Bool VG_(clo_demangle);
223/* Do leak check at exit? default: NO */
224extern Bool VG_(clo_leak_check);
225/* In leak check, show reachable-but-not-freed blocks? default: NO */
226extern Bool VG_(clo_show_reachable);
227/* How closely should we compare ExeContexts in leak records? default: 2 */
228extern Int VG_(clo_leak_resolution);
229/* Round malloc sizes upwards to integral number of words? default:
230 NO */
231extern Bool VG_(clo_sloppy_malloc);
232/* Allow loads from partially-valid addresses? default: YES */
233extern Bool VG_(clo_partial_loads_ok);
234/* Simulate child processes? default: NO */
235extern Bool VG_(clo_trace_children);
236/* The file id on which we send all messages. default: 2 (stderr). */
237extern Int VG_(clo_logfile_fd);
238/* Max volume of the freed blocks queue. */
239extern Int VG_(clo_freelist_vol);
240/* Assume accesses immediately below %esp are due to gcc-2.96 bugs.
241 default: NO */
242extern Bool VG_(clo_workaround_gcc296_bugs);
243
244/* The number of suppression files specified. */
245extern Int VG_(clo_n_suppressions);
246/* The names of the suppression files. */
247extern Char* VG_(clo_suppressions)[VG_CLO_MAX_SFILES];
248
249/* Single stepping? default: NO */
250extern Bool VG_(clo_single_step);
251/* Code improvement? default: YES */
252extern Bool VG_(clo_optimise);
253/* Memory-check instrumentation? default: YES */
254extern Bool VG_(clo_instrument);
255/* DEBUG: clean up instrumented code? default: YES */
256extern Bool VG_(clo_cleanup);
njn4f9c9342002-04-29 16:03:24 +0000257/* Cache simulation instrumentation? default: NO */
258extern Bool VG_(clo_cachesim);
sewardjde4a1d02002-03-22 01:27:54 +0000259/* SMC write checks? default: SOME (1,2,4 byte movs to mem) */
260extern Int VG_(clo_smc_check);
261/* DEBUG: print system calls? default: NO */
262extern Bool VG_(clo_trace_syscalls);
263/* DEBUG: print signal details? default: NO */
264extern Bool VG_(clo_trace_signals);
265/* DEBUG: print symtab details? default: NO */
266extern Bool VG_(clo_trace_symtab);
267/* DEBUG: print malloc details? default: NO */
268extern Bool VG_(clo_trace_malloc);
sewardj8937c812002-04-12 20:12:20 +0000269/* DEBUG: print thread scheduling events? default: NO */
270extern Bool VG_(clo_trace_sched);
sewardj45b4b372002-04-16 22:50:32 +0000271/* DEBUG: print pthread (mutex etc) events? default: 0 (none), 1
272 (some), 2 (all) */
273extern Int VG_(clo_trace_pthread_level);
sewardjde4a1d02002-03-22 01:27:54 +0000274/* Stop after this many basic blocks. default: Infinity. */
275extern ULong VG_(clo_stop_after);
276/* Display gory details for the k'th most popular error. default:
277 Infinity. */
278extern Int VG_(clo_dump_error);
279/* Number of parents of a backtrace. Default: 8. */
280extern Int VG_(clo_backtrace_size);
sewardj3984b852002-05-12 03:00:17 +0000281/* Engage miscellaneous wierd hacks needed for some progs. */
sewardj8d365b52002-05-12 10:52:16 +0000282extern Char* VG_(clo_weird_hacks);
sewardjde4a1d02002-03-22 01:27:54 +0000283
284
285/* ---------------------------------------------------------------------
286 Debugging and profiling stuff
287 ------------------------------------------------------------------ */
288
289/* No, really. I _am_ that strange. */
290#define OINK(nnn) VG_(message)(Vg_DebugMsg, "OINK %d",nnn)
291
292/* Tools for building messages from multiple parts. */
293typedef
294 enum { Vg_UserMsg, Vg_DebugMsg, Vg_DebugExtraMsg }
295 VgMsgKind;
296
297extern void VG_(start_msg) ( VgMsgKind kind );
298extern void VG_(add_to_msg) ( Char* format, ... );
299extern void VG_(end_msg) ( void );
300
301/* Send a simple, single-part message. */
302extern void VG_(message) ( VgMsgKind kind, Char* format, ... );
303
304/* Create a logfile into which messages can be dumped. */
305extern void VG_(startup_logging) ( void );
306extern void VG_(shutdown_logging) ( void );
307
308
309/* Profiling stuff */
310#ifdef VG_PROFILE
311
312#define VGP_M_STACK 10
313
sewardj671ff542002-05-07 09:25:30 +0000314#define VGP_M_CCS 26 /* == the # of elems in VGP_LIST */
sewardjde4a1d02002-03-22 01:27:54 +0000315#define VGP_LIST \
sewardj671ff542002-05-07 09:25:30 +0000316 VGP_PAIR(VgpUnc=0, "unclassified"), \
317 VGP_PAIR(VgpRun, "running"), \
318 VGP_PAIR(VgpSched, "scheduler"), \
sewardjde4a1d02002-03-22 01:27:54 +0000319 VGP_PAIR(VgpMalloc, "low-lev malloc/free"), \
320 VGP_PAIR(VgpCliMalloc, "client malloc/free"), \
321 VGP_PAIR(VgpTranslate, "translate-main"), \
322 VGP_PAIR(VgpToUCode, "to-ucode"), \
323 VGP_PAIR(VgpFromUcode, "from-ucode"), \
324 VGP_PAIR(VgpImprove, "improve"), \
325 VGP_PAIR(VgpInstrument, "instrument"), \
326 VGP_PAIR(VgpCleanup, "cleanup"), \
327 VGP_PAIR(VgpRegAlloc, "reg-alloc"), \
328 VGP_PAIR(VgpDoLRU, "do-lru"), \
329 VGP_PAIR(VgpSlowFindT, "slow-search-transtab"), \
330 VGP_PAIR(VgpInitAudit, "init-mem-audit"), \
331 VGP_PAIR(VgpExeContext, "exe-context"), \
332 VGP_PAIR(VgpReadSyms, "read-syms"), \
333 VGP_PAIR(VgpAddToT, "add-to-transtab"), \
334 VGP_PAIR(VgpSARP, "set-addr-range-perms"), \
335 VGP_PAIR(VgpSyscall, "syscall wrapper"), \
njn4f9c9342002-04-29 16:03:24 +0000336 VGP_PAIR(VgpCacheInstrument, "cache instrument"), \
337 VGP_PAIR(VgpCacheGetBBCC,"cache get BBCC"), \
338 VGP_PAIR(VgpCacheSimulate, "cache simulate"), \
339 VGP_PAIR(VgpCacheDump, "cache stats dump"), \
sewardjde4a1d02002-03-22 01:27:54 +0000340 VGP_PAIR(VgpSpare1, "spare 1"), \
341 VGP_PAIR(VgpSpare2, "spare 2")
342
343#define VGP_PAIR(enumname,str) enumname
344typedef enum { VGP_LIST } VgpCC;
345#undef VGP_PAIR
346
347extern void VGP_(init_profiling) ( void );
348extern void VGP_(done_profiling) ( void );
349extern void VGP_(pushcc) ( VgpCC );
350extern void VGP_(popcc) ( void );
351
352#define VGP_PUSHCC(cc) VGP_(pushcc)(cc)
353#define VGP_POPCC VGP_(popcc)()
354
355#else
356
357#define VGP_PUSHCC(cc) /* */
358#define VGP_POPCC /* */
359
360#endif /* VG_PROFILE */
361
362
363/* ---------------------------------------------------------------------
364 Exports of vg_malloc2.c
365 ------------------------------------------------------------------ */
366
367/* Allocation arenas.
368 SYMTAB is for Valgrind's symbol table storage.
369 CLIENT is for the client's mallocs/frees.
370 DEMANGLE is for the C++ demangler.
371 EXECTXT is for storing ExeContexts.
372 ERRCTXT is for storing ErrContexts.
373 PRIVATE is for Valgrind general stuff.
374 TRANSIENT is for very short-term use. It should be empty
375 in between uses.
376 When adding a new arena, remember also to add it
377 to ensure_mm_init().
378*/
379typedef Int ArenaId;
380
381#define VG_N_ARENAS 7
382
383#define VG_AR_PRIVATE 0 /* :: ArenaId */
384#define VG_AR_SYMTAB 1 /* :: ArenaId */
385#define VG_AR_CLIENT 2 /* :: ArenaId */
386#define VG_AR_DEMANGLE 3 /* :: ArenaId */
387#define VG_AR_EXECTXT 4 /* :: ArenaId */
388#define VG_AR_ERRCTXT 5 /* :: ArenaId */
389#define VG_AR_TRANSIENT 6 /* :: ArenaId */
390
391extern void* VG_(malloc) ( ArenaId arena, Int nbytes );
392extern void VG_(free) ( ArenaId arena, void* ptr );
393extern void* VG_(calloc) ( ArenaId arena, Int nmemb, Int nbytes );
394extern void* VG_(realloc) ( ArenaId arena, void* ptr, Int size );
395extern void* VG_(malloc_aligned) ( ArenaId aid, Int req_alignB,
396 Int req_pszB );
397
398extern void VG_(mallocSanityCheckArena) ( ArenaId arena );
399extern void VG_(mallocSanityCheckAll) ( void );
400
401extern void VG_(show_all_arena_stats) ( void );
402extern Bool VG_(is_empty_arena) ( ArenaId aid );
403
404
405/* The red-zone size for the client. This can be arbitrary, but
406 unfortunately must be set at compile time. */
407#define VG_AR_CLIENT_REDZONE_SZW 4
408
409#define VG_AR_CLIENT_REDZONE_SZB \
410 (VG_AR_CLIENT_REDZONE_SZW * VKI_BYTES_PER_WORD)
411
412
413/* ---------------------------------------------------------------------
sewardj2e93c502002-04-12 11:12:52 +0000414 Exports of vg_clientfuns.c
415 ------------------------------------------------------------------ */
416
417/* This doesn't export code or data that valgrind.so needs to link
418 against. However, the scheduler does need to know the following
419 request codes. A few, publically-visible, request codes are also
420 defined in valgrind.h. */
421
422#define VG_USERREQ__MALLOC 0x2001
423#define VG_USERREQ__BUILTIN_NEW 0x2002
424#define VG_USERREQ__BUILTIN_VEC_NEW 0x2003
425
426#define VG_USERREQ__FREE 0x2004
427#define VG_USERREQ__BUILTIN_DELETE 0x2005
428#define VG_USERREQ__BUILTIN_VEC_DELETE 0x2006
429
430#define VG_USERREQ__CALLOC 0x2007
431#define VG_USERREQ__REALLOC 0x2008
432#define VG_USERREQ__MEMALIGN 0x2009
433
434
sewardj20917d82002-05-28 01:36:45 +0000435/* (Fn, Arg): Create a new thread and run Fn applied to Arg in it. Fn
436 MUST NOT return -- ever. Eventually it will do either __QUIT or
437 __WAIT_JOINER. */
438#define VG_USERREQ__APPLY_IN_NEW_THREAD 0x3001
439
440/* ( no-args ): calling thread disappears from the system forever.
441 Reclaim resources. */
442#define VG_USERREQ__QUIT 0x3002
443
444/* ( void* ): calling thread waits for joiner and returns the void* to
445 it. */
446#define VG_USERREQ__WAIT_JOINER 0x3003
447
448/* ( ThreadId, void** ): wait to join a thread. */
449#define VG_USERREQ__PTHREAD_JOIN 0x3004
450
451/* Set cancellation state and type for this thread. */
452#define VG_USERREQ__SET_CANCELSTATE 0x3005
453#define VG_USERREQ__SET_CANCELTYPE 0x3006
454
455/* ( no-args ): Test if we are at a cancellation point. */
456#define VG_USERREQ__TESTCANCEL 0x3007
457
458/* ( ThreadId, &thread_exit_wrapper is the only allowable arg ): call
459 with this arg to indicate that a cancel is now pending for the
460 specified thread. */
461#define VG_USERREQ__SET_CANCELPEND 0x3008
462
463/* Set/get detach state for this thread. */
464#define VG_USERREQ__SET_OR_GET_DETACH 0x3009
465
466#define VG_USERREQ__PTHREAD_GET_THREADID 0x300B
467#define VG_USERREQ__PTHREAD_MUTEX_LOCK 0x300C
468#define VG_USERREQ__PTHREAD_MUTEX_TRYLOCK 0x300D
469#define VG_USERREQ__PTHREAD_MUTEX_UNLOCK 0x300E
470#define VG_USERREQ__PTHREAD_COND_WAIT 0x300F
471#define VG_USERREQ__PTHREAD_COND_TIMEDWAIT 0x3010
472#define VG_USERREQ__PTHREAD_COND_SIGNAL 0x3011
473#define VG_USERREQ__PTHREAD_COND_BROADCAST 0x3012
474#define VG_USERREQ__PTHREAD_KEY_CREATE 0x3013
475#define VG_USERREQ__PTHREAD_KEY_DELETE 0x3014
476#define VG_USERREQ__PTHREAD_SETSPECIFIC 0x3015
477#define VG_USERREQ__PTHREAD_GETSPECIFIC 0x3016
478#define VG_USERREQ__READ_MILLISECOND_TIMER 0x3017
479#define VG_USERREQ__PTHREAD_SIGMASK 0x3018
480#define VG_USERREQ__SIGWAIT 0x3019
481#define VG_USERREQ__PTHREAD_KILL 0x301A
482#define VG_USERREQ__PTHREAD_YIELD 0x301B
sewardj2e93c502002-04-12 11:12:52 +0000483
sewardj8ad94e12002-05-29 00:10:20 +0000484#define VG_USERREQ__CLEANUP_PUSH 0x3020
485#define VG_USERREQ__CLEANUP_POP 0x3021
sewardj870497a2002-05-29 01:06:47 +0000486#define VG_USERREQ__GET_KEY_D_AND_S 0x3022
sewardj8ad94e12002-05-29 00:10:20 +0000487
sewardjef037c72002-05-30 00:40:03 +0000488#define VG_USERREQ__NUKE_OTHER_THREADS 0x3023
489
sewardj4dced352002-06-04 22:54:20 +0000490
sewardj45b4b372002-04-16 22:50:32 +0000491/* Cosmetic ... */
492#define VG_USERREQ__GET_PTHREAD_TRACE_LEVEL 0x3101
sewardj4dced352002-06-04 22:54:20 +0000493/* Log a pthread error from client-space. Cosmetic. */
494#define VG_USERREQ__PTHREAD_ERROR 0x3102
sewardj45b4b372002-04-16 22:50:32 +0000495
sewardj54cacf02002-04-12 23:24:59 +0000496/*
497In vg_constants.h:
498#define VG_USERREQ__SIGNAL_RETURNS 0x4001
sewardj54cacf02002-04-12 23:24:59 +0000499*/
500
501
sewardj2e93c502002-04-12 11:12:52 +0000502/* ---------------------------------------------------------------------
503 Constants pertaining to the simulated CPU state, VG_(baseBlock),
504 which need to go here to avoid ugly circularities.
505 ------------------------------------------------------------------ */
506
507/* How big is the saved FPU state? */
508#define VG_SIZE_OF_FPUSTATE 108
509/* ... and in words ... */
510#define VG_SIZE_OF_FPUSTATE_W ((VG_SIZE_OF_FPUSTATE+3)/4)
511
512
513/* ---------------------------------------------------------------------
514 Exports of vg_scheduler.c
515 ------------------------------------------------------------------ */
516
517/* ThreadIds are simply indices into the vg_threads[] array. */
518typedef
519 UInt
520 ThreadId;
521
sewardj6072c362002-04-19 14:40:57 +0000522/* Special magic value for an invalid ThreadId. It corresponds to
523 LinuxThreads using zero as the initial value for
524 pthread_mutex_t.__m_owner and pthread_cond_t.__c_waiting. */
525#define VG_INVALID_THREADID ((ThreadId)(0))
sewardj2e93c502002-04-12 11:12:52 +0000526
527typedef
528 enum {
529 VgTs_Empty, /* this slot is not in use */
530 VgTs_Runnable, /* waiting to be scheduled */
531 VgTs_WaitJoiner, /* waiting for someone to do join on me */
532 VgTs_WaitJoinee, /* waiting for the thread I did join on */
533 VgTs_WaitFD, /* waiting for I/O completion on a fd */
534 VgTs_WaitMX, /* waiting on a mutex */
sewardj3b5d8862002-04-20 13:53:23 +0000535 VgTs_WaitCV, /* waiting on a condition variable */
sewardjb48e5002002-05-13 00:16:03 +0000536 VgTs_WaitSIG, /* waiting due to sigwait() */
sewardj2e93c502002-04-12 11:12:52 +0000537 VgTs_Sleeping /* sleeping for a while */
538 }
539 ThreadStatus;
sewardj8ad94e12002-05-29 00:10:20 +0000540
541/* An entry in a threads's cleanup stack. */
542typedef
543 struct {
544 void (*fn)(void*);
545 void* arg;
546 }
547 CleanupEntry;
sewardj2e93c502002-04-12 11:12:52 +0000548
549typedef
550 struct {
sewardj6072c362002-04-19 14:40:57 +0000551 /* ThreadId == 0 (and hence vg_threads[0]) is NEVER USED.
552 The thread identity is simply the index in vg_threads[].
553 ThreadId == 1 is the root thread and has the special property
sewardj1e8cdc92002-04-18 11:37:52 +0000554 that we don't try and allocate or deallocate its stack. For
555 convenience of generating error message, we also put the
556 ThreadId in this tid field, but be aware that it should
sewardj604ec3c2002-04-18 22:38:41 +0000557 ALWAYS == the index in vg_threads[]. */
sewardj1e8cdc92002-04-18 11:37:52 +0000558 ThreadId tid;
sewardj2e93c502002-04-12 11:12:52 +0000559
sewardj5f07b662002-04-23 16:52:51 +0000560 /* Current scheduling status.
561
562 Complications: whenever this is set to VgTs_WaitMX, you
563 should also set .m_edx to whatever the required return value
564 is for pthread_mutex_lock / pthread_cond_timedwait for when
565 the mutex finally gets unblocked. */
sewardj2e93c502002-04-12 11:12:52 +0000566 ThreadStatus status;
567
sewardj3b5d8862002-04-20 13:53:23 +0000568 /* When .status == WaitMX, points to the mutex I am waiting for.
569 When .status == WaitCV, points to the mutex associated with
570 the condition variable indicated by the .associated_cv field.
571 In all other cases, should be NULL. */
572 void* /* pthread_mutex_t* */ associated_mx;
573
574 /* When .status == WaitCV, points to the condition variable I am
575 waiting for. In all other cases, should be NULL. */
576 void* /* pthread_cond_t* */ associated_cv;
sewardj2e93c502002-04-12 11:12:52 +0000577
sewardj5f07b662002-04-23 16:52:51 +0000578 /* If VgTs_Sleeping, this is when we should wake up, measured in
579 milliseconds as supplied by VG_(read_millisecond_counter).
580
581 If VgTs_WaitCV, this indicates the time at which
582 pthread_cond_timedwait should wake up. If == 0xFFFFFFFF,
583 this means infinitely far in the future, viz,
584 pthread_cond_wait. */
585 UInt awaken_at;
sewardj2e93c502002-04-12 11:12:52 +0000586
sewardj20917d82002-05-28 01:36:45 +0000587 /* If VgTs_WaitJoiner, return value, as generated by joinees. */
588 void* joinee_retval;
589
590 /* If VgTs_WaitJoinee, place to copy the return value to, and
591 the identity of the thread we're waiting for. */
592 void** joiner_thread_return;
593 ThreadId joiner_jee_tid;
594
sewardj8ad94e12002-05-29 00:10:20 +0000595 /* Whether or not detached. */
596 Bool detached;
597
sewardj20917d82002-05-28 01:36:45 +0000598 /* Cancelability state and type. */
599 Bool cancel_st; /* False==PTH_CANCEL_DISABLE; True==.._ENABLE */
600 Bool cancel_ty; /* False==PTH_CANC_ASYNCH; True==..._DEFERRED */
601
602 /* Pointer to fn to call to do cancellation. Indicates whether
603 or not cancellation is pending. If NULL, not pending. Else
604 should be &thread_exit_wrapper(), indicating that
605 cancallation is pending. */
606 void (*cancel_pend)(void*);
607
sewardj8ad94e12002-05-29 00:10:20 +0000608 /* The cleanup stack. */
609 Int custack_used;
610 CleanupEntry custack[VG_N_CLEANUPSTACK];
sewardj2e93c502002-04-12 11:12:52 +0000611
sewardj5f07b662002-04-23 16:52:51 +0000612 /* thread-specific data */
613 void* specifics[VG_N_THREAD_KEYS];
614
sewardjb48e5002002-05-13 00:16:03 +0000615 /* This thread's blocked-signals mask. Semantics is that for a
616 signal to be delivered to this thread, the signal must not be
617 blocked by either the process-wide signal mask nor by this
618 one. So, if this thread is prepared to handle any signal that
619 the process as a whole is prepared to handle, this mask should
620 be made empty -- and that it is its default, starting
621 state. */
622 vki_ksigset_t sig_mask;
623
624 /* When not VgTs_WaitSIG, has no meaning. When VgTs_WaitSIG,
625 is the set of signals for which we are sigwait()ing. */
626 vki_ksigset_t sigs_waited_for;
627
sewardj2e93c502002-04-12 11:12:52 +0000628 /* Stacks. When a thread slot is freed, we don't deallocate its
629 stack; we just leave it lying around for the next use of the
630 slot. If the next use of the slot requires a larger stack,
631 only then is the old one deallocated and a new one
632 allocated.
633
634 For the main thread (threadid == 0), this mechanism doesn't
635 apply. We don't know the size of the stack since we didn't
636 allocate it, and furthermore we never reallocate it. */
637
638 /* The allocated size of this thread's stack (permanently zero
639 if this is ThreadId == 0, since we didn't allocate its stack) */
640 UInt stack_size;
641
642 /* Address of the lowest word in this thread's stack. NULL means
643 not allocated yet.
644 */
645 Addr stack_base;
646
sewardj1e8cdc92002-04-18 11:37:52 +0000647 /* Address of the highest legitimate word in this stack. This is
648 used for error messages only -- not critical for execution
649 correctness. Is is set for all stacks, specifically including
650 ThreadId == 0 (the main thread). */
651 Addr stack_highest_word;
652
sewardj2e93c502002-04-12 11:12:52 +0000653 /* Saved machine context. */
654 UInt m_eax;
655 UInt m_ebx;
656 UInt m_ecx;
657 UInt m_edx;
658 UInt m_esi;
659 UInt m_edi;
660 UInt m_ebp;
661 UInt m_esp;
662 UInt m_eflags;
663 UInt m_eip;
664 UInt m_fpu[VG_SIZE_OF_FPUSTATE_W];
665
666 UInt sh_eax;
667 UInt sh_ebx;
668 UInt sh_ecx;
669 UInt sh_edx;
670 UInt sh_esi;
671 UInt sh_edi;
672 UInt sh_ebp;
673 UInt sh_esp;
674 UInt sh_eflags;
675 }
676 ThreadState;
677
678
sewardj018f7622002-05-15 21:13:39 +0000679/* The thread table. */
680extern ThreadState VG_(threads)[VG_N_THREADS];
681
682/* Check that tid is in range and denotes a non-Empty thread. */
sewardjb48e5002002-05-13 00:16:03 +0000683extern Bool VG_(is_valid_tid) ( ThreadId tid );
684
sewardj018f7622002-05-15 21:13:39 +0000685/* Check that tid is in range. */
686extern Bool VG_(is_valid_or_empty_tid) ( ThreadId tid );
687
sewardj2e93c502002-04-12 11:12:52 +0000688/* Copy the specified thread's state into VG_(baseBlock) in
689 preparation for running it. */
690extern void VG_(load_thread_state)( ThreadId );
691
692/* Save the specified thread's state back in VG_(baseBlock), and fill
693 VG_(baseBlock) with junk, for sanity-check reasons. */
694extern void VG_(save_thread_state)( ThreadId );
695
sewardj1e8cdc92002-04-18 11:37:52 +0000696/* And for the currently running one, if valid. */
697extern ThreadState* VG_(get_current_thread_state) ( void );
sewardj2e93c502002-04-12 11:12:52 +0000698
sewardj1e8cdc92002-04-18 11:37:52 +0000699/* Similarly ... */
700extern ThreadId VG_(get_current_tid) ( void );
701
702/* Which thread is this address in the stack of, if any? Used for
703 error message generation. */
704extern ThreadId VG_(identify_stack_addr)( Addr a );
705
sewardjccef2e62002-05-29 19:26:32 +0000706/* Nuke all threads except tid. */
707extern void VG_(nuke_all_threads_except) ( ThreadId me );
708
sewardj2e93c502002-04-12 11:12:52 +0000709
710/* Return codes from the scheduler. */
711typedef
sewardj7e87e382002-05-03 19:09:05 +0000712 enum {
713 VgSrc_Deadlock, /* no runnable threads and no prospect of any
714 even if we wait for a long time */
715 VgSrc_ExitSyscall, /* client called exit(). This is the normal
716 route out. */
717 VgSrc_BbsDone /* In a debugging run, the specified number of
718 bbs has been completed. */
719 }
sewardj2e93c502002-04-12 11:12:52 +0000720 VgSchedReturnCode;
721
sewardj7e87e382002-05-03 19:09:05 +0000722
sewardj2e93c502002-04-12 11:12:52 +0000723/* The scheduler. */
724extern VgSchedReturnCode VG_(scheduler) ( void );
725
726extern void VG_(scheduler_init) ( void );
727
sewardj15a43e12002-04-17 19:35:12 +0000728extern void VG_(pp_sched_status) ( void );
sewardj2e93c502002-04-12 11:12:52 +0000729
730/* vg_oursignalhandler() might longjmp(). Here's the jmp_buf. */
731extern jmp_buf VG_(scheduler_jmpbuf);
732/* ... and if so, here's the signal which caused it to do so. */
733extern Int VG_(longjmpd_on_signal);
734
735
sewardja1679dd2002-05-10 22:31:40 +0000736/* Possible places where the main stack might be based. We check that
737 the initial stack, which we can't move, is allocated here.
738 VG_(scheduler_init) checks this. Andrea Archelangi's 2.4 kernels
739 have been rumoured to start stacks at 0x80000000, so that too is
740 considered.
sewardj2e93c502002-04-12 11:12:52 +0000741*/
sewardja1679dd2002-05-10 22:31:40 +0000742#define VG_STARTUP_STACK_BASE_1 (Addr)0xC0000000
743#define VG_STARTUP_STACK_BASE_2 (Addr)0x80000000
744#define VG_STARTUP_STACK_SMALLERTHAN 0x100000 /* 1024k */
745
746#define VG_STACK_MATCHES_BASE(zzstack, zzbase) \
747 ( \
748 ((zzstack) & ((zzbase) - VG_STARTUP_STACK_SMALLERTHAN)) \
749 == \
750 ((zzbase) - VG_STARTUP_STACK_SMALLERTHAN) \
751 )
sewardj2e93c502002-04-12 11:12:52 +0000752
753
754/* The red-zone size which we put at the bottom (highest address) of
755 thread stacks, for paranoia reasons. This can be arbitrary, and
756 doesn't really need to be set at compile time. */
757#define VG_AR_CLIENT_STACKBASE_REDZONE_SZW 4
758
759#define VG_AR_CLIENT_STACKBASE_REDZONE_SZB \
760 (VG_AR_CLIENT_STACKBASE_REDZONE_SZW * VKI_BYTES_PER_WORD)
761
762
sewardj018f7622002-05-15 21:13:39 +0000763/* Write a value to the client's %EDX (request return value register)
764 and set the shadow to indicate it is defined. */
765#define SET_EDX(zztid, zzval) \
766 do { VG_(threads)[zztid].m_edx = (zzval); \
767 VG_(threads)[zztid].sh_edx = VGM_WORD_VALID; \
768 } while (0)
769
770#define SET_EAX(zztid, zzval) \
771 do { VG_(threads)[zztid].m_eax = (zzval); \
772 VG_(threads)[zztid].sh_eax = VGM_WORD_VALID; \
773 } while (0)
774
sewardj2e93c502002-04-12 11:12:52 +0000775
776/* ---------------------------------------------------------------------
sewardjde4a1d02002-03-22 01:27:54 +0000777 Exports of vg_signals.c
778 ------------------------------------------------------------------ */
779
sewardjde4a1d02002-03-22 01:27:54 +0000780extern void VG_(sigstartup_actions) ( void );
781
sewardjb48e5002002-05-13 00:16:03 +0000782extern Bool VG_(deliver_signals) ( void );
sewardjde4a1d02002-03-22 01:27:54 +0000783extern void VG_(unblock_host_signal) ( Int sigNo );
sewardj018f7622002-05-15 21:13:39 +0000784extern void VG_(handle_SCSS_change) ( Bool force_update );
785
sewardjde4a1d02002-03-22 01:27:54 +0000786
787/* Fake system calls for signal handling. */
sewardj2342c972002-05-22 23:34:20 +0000788extern void VG_(do__NR_sigaltstack) ( ThreadId tid );
sewardj2e93c502002-04-12 11:12:52 +0000789extern void VG_(do__NR_sigaction) ( ThreadId tid );
sewardj018f7622002-05-15 21:13:39 +0000790extern void VG_(do__NR_sigprocmask) ( ThreadId tid,
791 Int how,
792 vki_ksigset_t* set,
793 vki_ksigset_t* oldset );
794extern void VG_(do_pthread_sigmask_SCSS_upd) ( ThreadId tid,
795 Int how,
796 vki_ksigset_t* set,
797 vki_ksigset_t* oldset );
798extern void VG_(send_signal_to_thread) ( ThreadId thread,
799 Int signo );
sewardjde4a1d02002-03-22 01:27:54 +0000800
sewardj2e93c502002-04-12 11:12:52 +0000801/* Modify the current thread's state once we have detected it is
802 returning from a signal handler. */
sewardj77e466c2002-04-14 02:29:29 +0000803extern Bool VG_(signal_returns) ( ThreadId );
sewardjde4a1d02002-03-22 01:27:54 +0000804
sewardj2e93c502002-04-12 11:12:52 +0000805/* Handy utilities to block/restore all host signals. */
806extern void VG_(block_all_host_signals)
807 ( /* OUT */ vki_ksigset_t* saved_mask );
sewardj018f7622002-05-15 21:13:39 +0000808extern void VG_(restore_all_host_signals)
sewardj2e93c502002-04-12 11:12:52 +0000809 ( /* IN */ vki_ksigset_t* saved_mask );
sewardjde4a1d02002-03-22 01:27:54 +0000810
811/* ---------------------------------------------------------------------
812 Exports of vg_mylibc.c
813 ------------------------------------------------------------------ */
814
815
sewardjfbe18b92002-05-10 00:46:59 +0000816#if !defined(NULL)
817# define NULL ((void*)0)
818#endif
sewardjde4a1d02002-03-22 01:27:54 +0000819
820extern void VG_(exit)( Int status )
821 __attribute__ ((__noreturn__));
822
823extern void VG_(printf) ( const char *format, ... );
824/* too noisy ... __attribute__ ((format (printf, 1, 2))) ; */
825
826extern void VG_(sprintf) ( Char* buf, Char *format, ... );
827
828extern void VG_(vprintf) ( void(*send)(Char),
829 const Char *format, va_list vargs );
830
831extern Bool VG_(isspace) ( Char c );
832
833extern Int VG_(strlen) ( const Char* str );
834
835extern Long VG_(atoll) ( Char* str );
sewardja70ca3f2002-05-30 01:22:20 +0000836extern Long VG_(atoll36) ( Char* str );
sewardjde4a1d02002-03-22 01:27:54 +0000837
838extern Char* VG_(strcat) ( Char* dest, const Char* src );
839extern Char* VG_(strncat) ( Char* dest, const Char* src, Int n );
840extern Char* VG_(strpbrk) ( const Char* s, const Char* accept );
841
842extern Char* VG_(strcpy) ( Char* dest, const Char* src );
843
844extern Int VG_(strcmp) ( const Char* s1, const Char* s2 );
845extern Int VG_(strcmp_ws) ( const Char* s1, const Char* s2 );
846
847extern Int VG_(strncmp) ( const Char* s1, const Char* s2, Int nmax );
848extern Int VG_(strncmp_ws) ( const Char* s1, const Char* s2, Int nmax );
849
850extern Char* VG_(strstr) ( const Char* haystack, Char* needle );
851extern Char* VG_(strchr) ( const Char* s, Char c );
852extern Char* VG_(strdup) ( ArenaId aid, const Char* s);
853
854extern Char* VG_(getenv) ( Char* name );
855extern Int VG_(getpid) ( void );
sewardj5f07b662002-04-23 16:52:51 +0000856
857extern void VG_(start_rdtsc_calibration) ( void );
858extern void VG_(end_rdtsc_calibration) ( void );
859extern UInt VG_(read_millisecond_timer) ( void );
sewardjde4a1d02002-03-22 01:27:54 +0000860
861
862extern Char VG_(toupper) ( Char c );
863
864extern void VG_(strncpy_safely) ( Char* dest, const Char* src, Int ndest );
865
866extern void VG_(strncpy) ( Char* dest, const Char* src, Int ndest );
867
868extern Bool VG_(stringMatch) ( Char* pat, Char* str );
869
870
sewardj3e1eb1f2002-05-18 13:14:17 +0000871#define VG__STRING(__str) #__str
sewardjde4a1d02002-03-22 01:27:54 +0000872
873/* Asserts are permanently enabled. Hurrah! */
874#define vg_assert(expr) \
875 ((void) ((expr) ? 0 : \
sewardj3e1eb1f2002-05-18 13:14:17 +0000876 (VG_(assert_fail) (VG__STRING(expr), \
sewardjde4a1d02002-03-22 01:27:54 +0000877 __FILE__, __LINE__, \
878 __PRETTY_FUNCTION__), 0)))
879
880extern void VG_(assert_fail) ( Char* expr, Char* file,
881 Int line, Char* fn )
882 __attribute__ ((__noreturn__));
883
njn4f9c9342002-04-29 16:03:24 +0000884/* Reading and writing files. */
sewardjde4a1d02002-03-22 01:27:54 +0000885extern Int VG_(open_read) ( Char* pathname );
njn4f9c9342002-04-29 16:03:24 +0000886extern Int VG_(open_write) ( Char* pathname );
887extern Int VG_(create_and_write) ( Char* pathname );
sewardjde4a1d02002-03-22 01:27:54 +0000888extern void VG_(close) ( Int fd );
889extern Int VG_(read) ( Int fd, void* buf, Int count);
890extern Int VG_(write) ( Int fd, void* buf, Int count);
sewardjb3586202002-05-09 17:38:13 +0000891extern Int VG_(stat) ( Char* file_name, struct vki_stat* buf );
sewardjde4a1d02002-03-22 01:27:54 +0000892
sewardj2e93c502002-04-12 11:12:52 +0000893extern Int VG_(fcntl) ( Int fd, Int cmd, Int arg );
894
895extern Int VG_(select)( Int n,
896 vki_fd_set* readfds,
897 vki_fd_set* writefds,
898 vki_fd_set* exceptfds,
899 struct vki_timeval * timeout );
900extern Int VG_(nanosleep)( const struct vki_timespec *req,
901 struct vki_timespec *rem );
902
903
sewardjde4a1d02002-03-22 01:27:54 +0000904/* mmap-ery ... */
905extern void* VG_(mmap)( void* start, UInt length,
906 UInt prot, UInt flags, UInt fd, UInt offset );
907
sewardj2e93c502002-04-12 11:12:52 +0000908extern Int VG_(munmap)( void* start, Int length );
sewardjde4a1d02002-03-22 01:27:54 +0000909
sewardjb3586202002-05-09 17:38:13 +0000910extern void* VG_(brk) ( void* end_data_segment );
911
sewardjde4a1d02002-03-22 01:27:54 +0000912
913/* Print a (panic) message, and abort. */
914extern void VG_(panic) ( Char* str )
915 __attribute__ ((__noreturn__));
916
917/* Get memory by anonymous mmap. */
sewardje6a25242002-04-21 22:03:07 +0000918extern void* VG_(get_memory_from_mmap) ( Int nBytes );
919
920/* Crude stand-in for the glibc system() call. */
921extern Int VG_(system) ( Char* cmd );
922
sewardjde4a1d02002-03-22 01:27:54 +0000923
924/* Signal stuff. Note that these use the vk_ (kernel) structure
925 definitions, which are different in places from those that glibc
926 defines. Since we're operating right at the kernel interface,
927 glibc's view of the world is entirely irrelevant. */
sewardj018f7622002-05-15 21:13:39 +0000928
929/* --- Signal set ops --- */
sewardjb48e5002002-05-13 00:16:03 +0000930extern Int VG_(ksigfillset)( vki_ksigset_t* set );
931extern Int VG_(ksigemptyset)( vki_ksigset_t* set );
sewardjde4a1d02002-03-22 01:27:54 +0000932
sewardj018f7622002-05-15 21:13:39 +0000933extern Bool VG_(kisfullsigset)( vki_ksigset_t* set );
934extern Bool VG_(kisemptysigset)( vki_ksigset_t* set );
935
936extern Int VG_(ksigaddset)( vki_ksigset_t* set, Int signum );
937extern Int VG_(ksigdelset)( vki_ksigset_t* set, Int signum );
938extern Int VG_(ksigismember) ( vki_ksigset_t* set, Int signum );
939
sewardjb48e5002002-05-13 00:16:03 +0000940extern void VG_(ksigaddset_from_set)( vki_ksigset_t* dst,
941 vki_ksigset_t* src );
942extern void VG_(ksigdelset_from_set)( vki_ksigset_t* dst,
943 vki_ksigset_t* src );
944
sewardj018f7622002-05-15 21:13:39 +0000945/* --- Mess with the kernel's sig state --- */
946extern Int VG_(ksigprocmask)( Int how, const vki_ksigset_t* set,
947 vki_ksigset_t* oldset );
948extern Int VG_(ksigaction) ( Int signum,
949 const vki_ksigaction* act,
950 vki_ksigaction* oldact );
sewardjde4a1d02002-03-22 01:27:54 +0000951
952extern Int VG_(ksignal)(Int signum, void (*sighandler)(Int));
953
954extern Int VG_(ksigaltstack)( const vki_kstack_t* ss, vki_kstack_t* oss );
955
sewardj018f7622002-05-15 21:13:39 +0000956extern Int VG_(kill)( Int pid, Int signo );
sewardjde4a1d02002-03-22 01:27:54 +0000957
958
959/* ---------------------------------------------------------------------
960 Definitions for the JITter (vg_translate.c, vg_to_ucode.c,
961 vg_from_ucode.c).
962 ------------------------------------------------------------------ */
963
964/* Tags which describe what operands are. */
965typedef
966 enum { TempReg=0, ArchReg=1, RealReg=2,
967 SpillNo=3, Literal=4, Lit16=5,
968 NoValue=6 }
969 Tag;
970
971
972/* Microinstruction opcodes. */
973typedef
974 enum {
975 NOP,
976 GET,
977 PUT,
978 LOAD,
979 STORE,
980 MOV,
981 CMOV, /* Used for cmpxchg and cmov */
982 WIDEN,
983 JMP,
984
985 /* Read/write the %EFLAGS register into a TempReg. */
986 GETF, PUTF,
987
988 ADD, ADC, AND, OR, XOR, SUB, SBB,
989 SHL, SHR, SAR, ROL, ROR, RCL, RCR,
990 NOT, NEG, INC, DEC, BSWAP,
991 CC2VAL,
992
993 /* Not strictly needed, but useful for making better
994 translations of address calculations. */
995 LEA1, /* reg2 := const + reg1 */
996 LEA2, /* reg3 := const + reg1 + reg2 * 1,2,4 or 8 */
997
998 /* not for translating x86 calls -- only to call helpers */
999 CALLM_S, CALLM_E, /* Mark start and end of push/pop sequences
1000 for CALLM. */
1001 PUSH, POP, CLEAR, /* Add/remove/zap args for helpers. */
1002 CALLM, /* call to a machine-code helper */
1003
1004 /* Hack for translating string (REP-) insns. Jump to literal if
1005 TempReg/RealReg is zero. */
1006 JIFZ,
1007
1008 /* FPU ops which read/write mem or don't touch mem at all. */
1009 FPU_R,
1010 FPU_W,
1011 FPU,
1012
1013 /* Advance the simulated %eip by some small (< 128) number. */
1014 INCEIP,
1015
1016 /* uinstrs which are not needed for mere translation of x86 code,
1017 only for instrumentation of it. */
1018 LOADV,
1019 STOREV,
1020 GETV,
1021 PUTV,
1022 TESTV,
1023 SETV,
1024 /* Get/set the v-bit (and it is only one bit) for the simulated
1025 %eflags register. */
1026 GETVF,
1027 PUTVF,
1028
1029 /* Do a unary or binary tag op. Only for post-instrumented
1030 code. For TAG1, first and only arg is a TempReg, and is both
1031 arg and result reg. For TAG2, first arg is src, second is
1032 dst, in the normal way; both are TempRegs. In both cases,
1033 3rd arg is a RiCHelper with a Lit16 tag. This indicates
1034 which tag op to do. */
1035 TAG1,
1036 TAG2
1037 }
1038 Opcode;
1039
1040
1041/* Condition codes, observing the Intel encoding. CondAlways is an
1042 extra. */
1043typedef
1044 enum {
1045 CondO = 0, /* overflow */
1046 CondNO = 1, /* no overflow */
1047 CondB = 2, /* below */
1048 CondNB = 3, /* not below */
1049 CondZ = 4, /* zero */
1050 CondNZ = 5, /* not zero */
1051 CondBE = 6, /* below or equal */
1052 CondNBE = 7, /* not below or equal */
1053 CondS = 8, /* negative */
1054 ConsNS = 9, /* not negative */
1055 CondP = 10, /* parity even */
1056 CondNP = 11, /* not parity even */
1057 CondL = 12, /* jump less */
1058 CondNL = 13, /* not less */
1059 CondLE = 14, /* less or equal */
1060 CondNLE = 15, /* not less or equal */
1061 CondAlways = 16 /* Jump always */
1062 }
1063 Condcode;
1064
1065
sewardj2e93c502002-04-12 11:12:52 +00001066/* Descriptions of additional properties of *unconditional* jumps. */
1067typedef
1068 enum {
1069 JmpBoring=0, /* boring unconditional jump */
1070 JmpCall=1, /* jump due to an x86 call insn */
1071 JmpRet=2, /* jump due to an x86 ret insn */
1072 JmpSyscall=3, /* do a system call, then jump */
1073 JmpClientReq=4 /* do a client request, then jump */
1074 }
1075 JmpKind;
1076
1077
sewardjde4a1d02002-03-22 01:27:54 +00001078/* Flags. User-level code can only read/write O(verflow), S(ign),
1079 Z(ero), A(ux-carry), C(arry), P(arity), and may also write
1080 D(irection). That's a total of 7 flags. A FlagSet is a bitset,
1081 thusly:
1082 76543210
1083 DOSZACP
1084 and bit 7 must always be zero since it is unused.
1085*/
1086typedef UChar FlagSet;
1087
1088#define FlagD (1<<6)
1089#define FlagO (1<<5)
1090#define FlagS (1<<4)
1091#define FlagZ (1<<3)
1092#define FlagA (1<<2)
1093#define FlagC (1<<1)
1094#define FlagP (1<<0)
1095
1096#define FlagsOSZACP (FlagO | FlagS | FlagZ | FlagA | FlagC | FlagP)
1097#define FlagsOSZAP (FlagO | FlagS | FlagZ | FlagA | FlagP)
1098#define FlagsOSZCP (FlagO | FlagS | FlagZ | FlagC | FlagP)
1099#define FlagsOSACP (FlagO | FlagS | FlagA | FlagC | FlagP)
1100#define FlagsSZACP ( FlagS | FlagZ | FlagA | FlagC | FlagP)
1101#define FlagsSZAP ( FlagS | FlagZ | FlagA | FlagP)
sewardj4a7456e2002-03-24 13:52:19 +00001102#define FlagsZCP ( FlagZ | FlagC | FlagP)
sewardjde4a1d02002-03-22 01:27:54 +00001103#define FlagsOC (FlagO | FlagC )
sewardj4d0ab1f2002-03-24 10:00:09 +00001104#define FlagsAC ( FlagA | FlagC )
sewardjde4a1d02002-03-22 01:27:54 +00001105
1106#define FlagsALL (FlagsOSZACP | FlagD)
1107#define FlagsEmpty (FlagSet)0
1108
1109#define VG_IS_FLAG_SUBSET(set1,set2) \
1110 (( ((FlagSet)set1) & ((FlagSet)set2) ) == ((FlagSet)set1) )
1111
1112#define VG_UNION_FLAG_SETS(set1,set2) \
1113 ( ((FlagSet)set1) | ((FlagSet)set2) )
1114
1115
1116
1117/* A Micro (u)-instruction. */
1118typedef
1119 struct {
1120 /* word 1 */
1121 UInt lit32; /* 32-bit literal */
1122
1123 /* word 2 */
1124 UShort val1; /* first operand */
1125 UShort val2; /* second operand */
1126
1127 /* word 3 */
1128 UShort val3; /* third operand */
1129 UChar opcode; /* opcode */
1130 UChar size; /* data transfer size */
1131
1132 /* word 4 */
1133 FlagSet flags_r; /* :: FlagSet */
1134 FlagSet flags_w; /* :: FlagSet */
1135 UChar tag1:4; /* first operand tag */
1136 UChar tag2:4; /* second operand tag */
1137 UChar tag3:4; /* third operand tag */
1138 UChar extra4b:4; /* Spare field, used by WIDEN for src
1139 -size, and by LEA2 for scale
njn4f9c9342002-04-29 16:03:24 +00001140 (1,2,4 or 8), and by unconditional JMPs for
1141 orig x86 instr size if --cachesim=yes */
1142
sewardjde4a1d02002-03-22 01:27:54 +00001143
1144 /* word 5 */
1145 UChar cond; /* condition, for jumps */
1146 Bool smc_check:1; /* do a smc test, if writes memory. */
1147 Bool signed_widen:1; /* signed or unsigned WIDEN ? */
sewardj2e93c502002-04-12 11:12:52 +00001148 JmpKind jmpkind:3; /* additional properties of unconditional JMP */
sewardjde4a1d02002-03-22 01:27:54 +00001149 }
1150 UInstr;
1151
1152
1153/* Expandable arrays of uinstrs. */
1154typedef
1155 struct {
1156 Int used;
1157 Int size;
1158 UInstr* instrs;
1159 Int nextTemp;
1160 }
1161 UCodeBlock;
1162
1163/* Refer to `the last instruction stuffed in', including as an
1164 lvalue. */
1165#define LAST_UINSTR(cb) (cb)->instrs[(cb)->used-1]
1166
1167/* An invalid temporary number :-) */
1168#define INVALID_TEMPREG 999999999
1169
1170
1171/* ---------------------------------------------------------------------
1172 Exports of vg_demangle.c
1173 ------------------------------------------------------------------ */
1174
1175extern void VG_(demangle) ( Char* orig, Char* result, Int result_size );
1176
1177
1178/* ---------------------------------------------------------------------
1179 Exports of vg_from_ucode.c
1180 ------------------------------------------------------------------ */
1181
1182extern UChar* VG_(emit_code) ( UCodeBlock* cb, Int* nbytes );
1183
1184
1185/* ---------------------------------------------------------------------
1186 Exports of vg_to_ucode.c
1187 ------------------------------------------------------------------ */
1188
1189extern Int VG_(disBB) ( UCodeBlock* cb, Addr eip0 );
1190extern Char* VG_(nameOfIntReg) ( Int size, Int reg );
1191extern Char VG_(nameOfIntSize) ( Int size );
1192extern UInt VG_(extend_s_8to32) ( UInt x );
1193extern Int VG_(getNewTemp) ( UCodeBlock* cb );
1194extern Int VG_(getNewShadow) ( UCodeBlock* cb );
1195
1196#define SHADOW(tempreg) ((tempreg)+1)
1197
1198
1199/* ---------------------------------------------------------------------
1200 Exports of vg_translate.c
1201 ------------------------------------------------------------------ */
1202
sewardj1e8cdc92002-04-18 11:37:52 +00001203extern void VG_(translate) ( ThreadState* tst,
1204 Addr orig_addr,
sewardjde4a1d02002-03-22 01:27:54 +00001205 UInt* orig_size,
1206 Addr* trans_addr,
1207 UInt* trans_size );
1208
1209extern void VG_(emptyUInstr) ( UInstr* u );
1210extern void VG_(newUInstr0) ( UCodeBlock* cb, Opcode opcode, Int sz );
1211extern void VG_(newUInstr1) ( UCodeBlock* cb, Opcode opcode, Int sz,
1212 Tag tag1, UInt val1 );
1213extern void VG_(newUInstr2) ( UCodeBlock* cb, Opcode opcode, Int sz,
1214 Tag tag1, UInt val1,
1215 Tag tag2, UInt val2 );
1216extern void VG_(newUInstr3) ( UCodeBlock* cb, Opcode opcode, Int sz,
1217 Tag tag1, UInt val1,
1218 Tag tag2, UInt val2,
1219 Tag tag3, UInt val3 );
1220extern void VG_(setFlagRW) ( UInstr* u,
1221 FlagSet fr, FlagSet fw );
1222
1223extern void VG_(setLiteralField) ( UCodeBlock* cb, UInt lit32 );
1224extern Bool VG_(anyFlagUse) ( UInstr* u );
1225
1226
1227
1228extern void VG_(ppUInstr) ( Int instrNo, UInstr* u );
1229extern void VG_(ppUCodeBlock) ( UCodeBlock* cb, Char* title );
1230
njn4f9c9342002-04-29 16:03:24 +00001231extern UCodeBlock* VG_(allocCodeBlock) ( void );
1232extern void VG_(freeCodeBlock) ( UCodeBlock* cb );
1233extern void VG_(copyUInstr) ( UCodeBlock* cb, UInstr* instr );
1234
sewardjde4a1d02002-03-22 01:27:54 +00001235extern Char* VG_(nameCondcode) ( Condcode cond );
1236extern Bool VG_(saneUInstr) ( Bool beforeRA, UInstr* u );
1237extern Bool VG_(saneUCodeBlock) ( UCodeBlock* cb );
1238extern Char* VG_(nameUOpcode) ( Bool upper, Opcode opc );
1239extern Int VG_(rankToRealRegNo) ( Int rank );
1240
1241extern void* VG_(jitmalloc) ( Int nbytes );
1242extern void VG_(jitfree) ( void* ptr );
1243
1244
1245/* ---------------------------------------------------------------------
1246 Exports of vg_execontext.c.
1247 ------------------------------------------------------------------ */
1248
1249/* Records the PC and a bit of the call chain. The first 4 %eip
1250 values are used in comparisons do remove duplicate errors, and for
1251 comparing against suppression specifications. The rest are purely
1252 informational (but often important). */
1253
1254typedef
1255 struct _ExeContextRec {
1256 struct _ExeContextRec * next;
1257 /* The size of this array is VG_(clo_backtrace_size); at least
1258 2, at most VG_DEEPEST_BACKTRACE. [0] is the current %eip,
1259 [1] is its caller, [2] is the caller of [1], etc. */
1260 Addr eips[0];
1261 }
1262 ExeContext;
1263
1264
1265/* Initialise the ExeContext storage mechanism. */
1266extern void VG_(init_ExeContext_storage) ( void );
1267
1268/* Print stats (informational only). */
1269extern void VG_(show_ExeContext_stats) ( void );
1270
1271
1272/* Take a snapshot of the client's stack. Search our collection of
1273 ExeContexts to see if we already have it, and if not, allocate a
1274 new one. Either way, return a pointer to the context. */
sewardj8c824512002-04-14 04:16:48 +00001275extern ExeContext* VG_(get_ExeContext) ( Bool skip_top_frame,
1276 Addr eip, Addr ebp );
sewardjde4a1d02002-03-22 01:27:54 +00001277
1278/* Print an ExeContext. */
1279extern void VG_(pp_ExeContext) ( ExeContext* );
1280
1281/* Compare two ExeContexts, just comparing the top two callers. */
1282extern Bool VG_(eq_ExeContext_top2) ( ExeContext* e1, ExeContext* e2 );
1283
1284/* Compare two ExeContexts, just comparing the top four callers. */
1285extern Bool VG_(eq_ExeContext_top4) ( ExeContext* e1, ExeContext* e2 );
1286
1287/* Compare two ExeContexts, comparing all callers. */
1288extern Bool VG_(eq_ExeContext_all) ( ExeContext* e1, ExeContext* e2 );
1289
1290
1291
1292/* ---------------------------------------------------------------------
1293 Exports of vg_errcontext.c.
1294 ------------------------------------------------------------------ */
1295
1296extern void VG_(load_suppressions) ( void );
1297extern void VG_(show_all_errors) ( void );
1298extern void VG_(record_value_error) ( Int size );
sewardjaabd5ad2002-04-19 15:43:37 +00001299extern void VG_(record_free_error) ( ThreadState* tst, Addr a );
1300extern void VG_(record_freemismatch_error) ( ThreadState* tst, Addr a );
sewardjde4a1d02002-03-22 01:27:54 +00001301extern void VG_(record_address_error) ( Addr a, Int size,
1302 Bool isWrite );
sewardj1e8cdc92002-04-18 11:37:52 +00001303
1304extern void VG_(record_jump_error) ( ThreadState* tst, Addr a );
sewardj8c824512002-04-14 04:16:48 +00001305
1306extern void VG_(record_param_err) ( ThreadState* tst,
1307 Addr a,
sewardjde4a1d02002-03-22 01:27:54 +00001308 Bool isWriteLack,
1309 Char* msg );
sewardj8c824512002-04-14 04:16:48 +00001310extern void VG_(record_user_err) ( ThreadState* tst,
1311 Addr a, Bool isWriteLack );
sewardj4dced352002-06-04 22:54:20 +00001312extern void VG_(record_pthread_err) ( ThreadId tid, Char* msg );
1313
sewardjde4a1d02002-03-22 01:27:54 +00001314
1315
1316/* The classification of a faulting address. */
1317typedef
sewardjb581a132002-05-08 00:32:50 +00001318 enum { Undescribed, /* as-yet unclassified */
1319 Stack,
1320 Unknown, /* classification yielded nothing useful */
1321 Freed, Mallocd,
1322 UserG, UserS }
sewardjde4a1d02002-03-22 01:27:54 +00001323 AddrKind;
1324
1325/* Records info about a faulting address. */
1326typedef
1327 struct {
1328 /* ALL */
1329 AddrKind akind;
1330 /* Freed, Mallocd */
1331 Int blksize;
1332 /* Freed, Mallocd */
1333 Int rwoffset;
1334 /* Freed, Mallocd */
1335 ExeContext* lastchange;
sewardj1e8cdc92002-04-18 11:37:52 +00001336 /* Stack */
1337 ThreadId stack_tid;
sewardjb581a132002-05-08 00:32:50 +00001338 /* True if is just-below %esp -- could be a gcc bug. */
1339 Bool maybe_gcc;
sewardjde4a1d02002-03-22 01:27:54 +00001340 }
1341 AddrInfo;
1342
1343
1344/* ---------------------------------------------------------------------
1345 Exports of vg_clientperms.c
1346 ------------------------------------------------------------------ */
1347
1348extern Bool VG_(client_perm_maybe_describe)( Addr a, AddrInfo* ai );
1349
sewardj8c824512002-04-14 04:16:48 +00001350extern UInt VG_(handle_client_request) ( ThreadState* tst, UInt* arg_block );
sewardjde4a1d02002-03-22 01:27:54 +00001351
1352extern void VG_(delete_client_stack_blocks_following_ESP_change) ( void );
1353
1354extern void VG_(show_client_block_stats) ( void );
1355
1356
1357/* ---------------------------------------------------------------------
1358 Exports of vg_procselfmaps.c
1359 ------------------------------------------------------------------ */
1360
1361extern
1362void VG_(read_procselfmaps) (
1363 void (*record_mapping)( Addr, UInt, Char, Char, Char, UInt, UChar* )
1364);
1365
1366
1367/* ---------------------------------------------------------------------
1368 Exports of vg_symtab2.c
1369 ------------------------------------------------------------------ */
1370
1371/* We assume the executable is loaded here ... can't really find
1372 out. There is a hacky sanity check in vg_init_memory_audit()
1373 which should trip up most stupidities.
1374*/
1375#define VG_ASSUMED_EXE_BASE (Addr)0x8048000
1376
1377extern void VG_(read_symbols) ( void );
1378extern void VG_(mini_stack_dump) ( ExeContext* ec );
1379extern void VG_(what_obj_and_fun_is_this)
1380 ( Addr a,
1381 Char* obj_buf, Int n_obj_buf,
1382 Char* fun_buf, Int n_fun_buf );
njn4f9c9342002-04-29 16:03:24 +00001383extern Bool VG_(what_line_is_this) ( Addr a,
1384 UChar* filename, Int n_filename,
1385 UInt* lineno );
1386extern Bool VG_(what_fn_is_this) ( Bool no_demangle, Addr a,
1387 Char* fn_name, Int n_fn_name);
sewardjde4a1d02002-03-22 01:27:54 +00001388
sewardj18d75132002-05-16 11:06:21 +00001389extern Bool VG_(symtab_notify_munmap) ( Addr start, UInt length );
sewardjde4a1d02002-03-22 01:27:54 +00001390
1391
1392/* ---------------------------------------------------------------------
1393 Exports of vg_clientmalloc.c
1394 ------------------------------------------------------------------ */
1395
sewardjde4a1d02002-03-22 01:27:54 +00001396typedef
1397 enum {
1398 Vg_AllocMalloc = 0,
sewardj2e93c502002-04-12 11:12:52 +00001399 Vg_AllocNew = 1,
sewardjde4a1d02002-03-22 01:27:54 +00001400 Vg_AllocNewVec = 2
1401 }
1402 VgAllocKind;
1403
1404/* Description of a malloc'd chunk. */
1405typedef
1406 struct _ShadowChunk {
1407 struct _ShadowChunk* next;
1408 ExeContext* where; /* where malloc'd/free'd */
1409 UInt size : 30; /* size requested. */
1410 VgAllocKind allockind : 2; /* which wrapper did the allocation */
1411 Addr data; /* ptr to actual block. */
1412 }
1413 ShadowChunk;
1414
1415extern void VG_(clientmalloc_done) ( void );
1416extern void VG_(describe_addr) ( Addr a, AddrInfo* ai );
1417extern ShadowChunk** VG_(get_malloc_shadows) ( /*OUT*/ UInt* n_shadows );
1418
sewardj2e93c502002-04-12 11:12:52 +00001419/* These are called from the scheduler, when it intercepts a user
1420 request. */
sewardj8c824512002-04-14 04:16:48 +00001421extern void* VG_(client_malloc) ( ThreadState* tst,
1422 UInt size, VgAllocKind kind );
1423extern void* VG_(client_memalign) ( ThreadState* tst,
1424 UInt align, UInt size );
1425extern void VG_(client_free) ( ThreadState* tst,
1426 void* ptrV, VgAllocKind kind );
1427extern void* VG_(client_calloc) ( ThreadState* tst,
1428 UInt nmemb, UInt size1 );
1429extern void* VG_(client_realloc) ( ThreadState* tst,
1430 void* ptrV, UInt size_new );
sewardjde4a1d02002-03-22 01:27:54 +00001431
1432
1433/* ---------------------------------------------------------------------
1434 Exports of vg_main.c
1435 ------------------------------------------------------------------ */
1436
sewardjde4a1d02002-03-22 01:27:54 +00001437/* A structure used as an intermediary when passing the simulated
1438 CPU's state to some assembly fragments, particularly system calls.
1439 Stuff is copied from baseBlock to here, the assembly magic runs,
1440 and then the inverse copy is done. */
1441
1442extern UInt VG_(m_state_static) [8 /* int regs, in Intel order */
1443 + 1 /* %eflags */
1444 + 1 /* %eip */
1445 + VG_SIZE_OF_FPUSTATE_W /* FPU state */
1446 ];
1447
1448/* Handy fns for doing the copy back and forth. */
1449extern void VG_(copy_baseBlock_to_m_state_static) ( void );
1450extern void VG_(copy_m_state_static_to_baseBlock) ( void );
1451
sewardjde4a1d02002-03-22 01:27:54 +00001452/* Called when some unhandleable client behaviour is detected.
1453 Prints a msg and aborts. */
1454extern void VG_(unimplemented) ( Char* msg );
sewardjcfc39b22002-05-08 01:58:18 +00001455extern void VG_(nvidia_moan) ( void );
sewardjde4a1d02002-03-22 01:27:54 +00001456
1457/* The stack on which Valgrind runs. We can't use the same stack as the
1458 simulatee -- that's an important design decision. */
1459extern UInt VG_(stack)[10000];
1460
1461/* Similarly, we have to ask for signals to be delivered on an
1462 alternative stack, since it is possible, although unlikely, that
1463 we'll have to run client code from inside the Valgrind-installed
1464 signal handler. If this happens it will be done by
1465 vg_deliver_signal_immediately(). */
1466extern UInt VG_(sigstack)[10000];
1467
sewardjde4a1d02002-03-22 01:27:54 +00001468/* Holds client's %esp at the point we gained control. From this the
1469 client's argc, argv and envp are deduced. */
1470extern Addr VG_(esp_at_startup);
1471extern Int VG_(client_argc);
1472extern Char** VG_(client_argv);
1473extern Char** VG_(client_envp);
1474
1475/* Remove valgrind.so from a LD_PRELOAD=... string so child processes
sewardj3e1eb1f2002-05-18 13:14:17 +00001476 don't get traced into. Also mess up $libdir/valgrind so that our
1477 libpthread.so disappears from view. */
1478void VG_(mash_LD_PRELOAD_and_LD_LIBRARY_PATH) ( Char* ld_preload_str,
1479 Char* ld_library_path_str );
sewardjde4a1d02002-03-22 01:27:54 +00001480
1481/* Something of a function looking for a home ... start up GDB. This
1482 is called from VG_(swizzle_esp_then_start_GDB) and so runs on the
1483 *client's* stack. This is necessary to give GDB the illusion that
1484 the client program really was running on the real cpu. */
1485extern void VG_(start_GDB_whilst_on_client_stack) ( void );
1486
1487/* Spew out vast amounts of junk during JITting? */
1488extern Bool VG_(disassemble);
1489
1490/* 64-bit counter for the number of basic blocks done. */
1491extern ULong VG_(bbs_done);
1492/* 64-bit counter for the number of bbs to go before a debug exit. */
1493extern ULong VG_(bbs_to_go);
1494
1495/* Counts downwards in vg_run_innerloop. */
1496extern UInt VG_(dispatch_ctr);
1497
sewardjde4a1d02002-03-22 01:27:54 +00001498/* Is the client running on the simulated CPU or the real one? */
1499extern Bool VG_(running_on_simd_CPU); /* Initially False */
1500
1501/* The current LRU epoch. */
1502extern UInt VG_(current_epoch);
1503
sewardj7e87e382002-05-03 19:09:05 +00001504/* This is the ThreadId of the last thread the scheduler ran. */
1505extern ThreadId VG_(last_run_tid);
1506
sewardjde4a1d02002-03-22 01:27:54 +00001507
1508/* --- Counters, for informational purposes only. --- */
1509
1510/* Number of lookups which miss the fast tt helper. */
1511extern UInt VG_(tt_fast_misses);
1512
1513/* Counts for LRU informational messages. */
1514
1515/* Number and total o/t size of new translations this epoch. */
1516extern UInt VG_(this_epoch_in_count);
1517extern UInt VG_(this_epoch_in_osize);
1518extern UInt VG_(this_epoch_in_tsize);
1519/* Number and total o/t size of discarded translations this epoch. */
1520extern UInt VG_(this_epoch_out_count);
1521extern UInt VG_(this_epoch_out_osize);
1522extern UInt VG_(this_epoch_out_tsize);
1523/* Number and total o/t size of translations overall. */
1524extern UInt VG_(overall_in_count);
1525extern UInt VG_(overall_in_osize);
1526extern UInt VG_(overall_in_tsize);
1527/* Number and total o/t size of discards overall. */
1528extern UInt VG_(overall_out_count);
1529extern UInt VG_(overall_out_osize);
1530extern UInt VG_(overall_out_tsize);
1531
1532/* The number of LRU-clearings of TT/TC. */
1533extern UInt VG_(number_of_lrus);
1534
1535/* Counts pertaining to the register allocator. */
1536
1537/* total number of uinstrs input to reg-alloc */
1538extern UInt VG_(uinstrs_prealloc);
1539
1540/* total number of uinstrs added due to spill code */
1541extern UInt VG_(uinstrs_spill);
1542
1543/* number of bbs requiring spill code */
1544extern UInt VG_(translations_needing_spill);
1545
1546/* total of register ranks over all translations */
1547extern UInt VG_(total_reg_rank);
1548
sewardjde4a1d02002-03-22 01:27:54 +00001549/* Counts pertaining to internal sanity checking. */
1550extern UInt VG_(sanity_fast_count);
1551extern UInt VG_(sanity_slow_count);
1552
sewardj2e93c502002-04-12 11:12:52 +00001553/* Counts pertaining to the scheduler. */
1554extern UInt VG_(num_scheduling_events_MINOR);
1555extern UInt VG_(num_scheduling_events_MAJOR);
1556
sewardjde4a1d02002-03-22 01:27:54 +00001557
1558/* ---------------------------------------------------------------------
1559 Exports of vg_memory.c
1560 ------------------------------------------------------------------ */
1561
1562extern void VGM_(init_memory_audit) ( void );
1563extern Addr VGM_(curr_dataseg_end);
1564extern void VG_(show_reg_tags) ( void );
1565extern void VG_(detect_memory_leaks) ( void );
1566extern void VG_(done_prof_mem) ( void );
1567
1568/* Set permissions for an address range. Not speed-critical. */
1569extern void VGM_(make_noaccess) ( Addr a, UInt len );
1570extern void VGM_(make_writable) ( Addr a, UInt len );
1571extern void VGM_(make_readable) ( Addr a, UInt len );
1572/* Use with care! (read: use for shmat only) */
1573extern void VGM_(make_readwritable) ( Addr a, UInt len );
1574extern void VGM_(copy_address_range_perms) ( Addr src, Addr dst,
1575 UInt len );
1576
1577/* Check permissions for an address range. Not speed-critical. */
1578extern Bool VGM_(check_writable) ( Addr a, UInt len, Addr* bad_addr );
1579extern Bool VGM_(check_readable) ( Addr a, UInt len, Addr* bad_addr );
1580extern Bool VGM_(check_readable_asciiz) ( Addr a, Addr* bad_addr );
1581
sewardj0c3b53f2002-05-01 01:58:35 +00001582/* Sanity checks which may be done at any time. The scheduler decides
1583 when. */
1584extern void VG_(do_sanity_checks) ( Bool force_expensive );
sewardjde4a1d02002-03-22 01:27:54 +00001585/* Very cheap ... */
1586extern Bool VG_(first_and_last_secondaries_look_plausible) ( void );
1587
1588/* These functions are called from generated code. */
1589extern void VG_(helperc_STOREV4) ( UInt, Addr );
1590extern void VG_(helperc_STOREV2) ( UInt, Addr );
1591extern void VG_(helperc_STOREV1) ( UInt, Addr );
1592
1593extern UInt VG_(helperc_LOADV1) ( Addr );
1594extern UInt VG_(helperc_LOADV2) ( Addr );
1595extern UInt VG_(helperc_LOADV4) ( Addr );
1596
1597extern void VGM_(handle_esp_assignment) ( Addr new_espA );
1598extern void VGM_(fpu_write_check) ( Addr addr, Int size );
1599extern void VGM_(fpu_read_check) ( Addr addr, Int size );
1600
1601/* Safely (avoiding SIGSEGV / SIGBUS) scan the entire valid address
1602 space and pass the addresses and values of all addressible,
1603 defined, aligned words to notify_word. This is the basis for the
1604 leak detector. Returns the number of calls made to notify_word. */
1605UInt VG_(scan_all_valid_memory) ( void (*notify_word)( Addr, UInt ) );
1606
1607/* Is this address within some small distance below %ESP? Used only
1608 for the --workaround-gcc296-bugs kludge. */
sewardj8c824512002-04-14 04:16:48 +00001609extern Bool VG_(is_just_below_ESP)( Addr esp, Addr aa );
sewardjde4a1d02002-03-22 01:27:54 +00001610
1611/* Nasty kludgery to deal with applications which switch stacks,
1612 like netscape. */
sewardjde4a1d02002-03-22 01:27:54 +00001613#define VG_PLAUSIBLE_STACK_SIZE 8000000
1614
sewardjc3bd5f52002-05-01 03:24:23 +00001615/* Needed by the pthreads implementation. */
1616#define VGM_WORD_VALID 0
1617#define VGM_WORD_INVALID 0xFFFFFFFF
1618
sewardjde4a1d02002-03-22 01:27:54 +00001619
1620/* ---------------------------------------------------------------------
1621 Exports of vg_syscall_mem.c
1622 ------------------------------------------------------------------ */
1623
sewardj2e93c502002-04-12 11:12:52 +00001624extern void VG_(perform_assumed_nonblocking_syscall) ( ThreadId tid );
sewardjde4a1d02002-03-22 01:27:54 +00001625
sewardj2e93c502002-04-12 11:12:52 +00001626extern void VG_(check_known_blocking_syscall) ( ThreadId tid,
1627 Int syscallno,
1628 Int* /*IN*/ res );
sewardjde4a1d02002-03-22 01:27:54 +00001629
1630extern Bool VG_(is_kerror) ( Int res );
1631
sewardj018f7622002-05-15 21:13:39 +00001632#define KERNEL_DO_SYSCALL(thread_id, result_lvalue) \
1633 VG_(load_thread_state)(thread_id); \
1634 VG_(copy_baseBlock_to_m_state_static)(); \
1635 VG_(do_syscall)(); \
1636 VG_(copy_m_state_static_to_baseBlock)(); \
1637 VG_(save_thread_state)(thread_id); \
1638 VG_(threads)[thread_id].sh_eax = VGM_WORD_VALID; \
1639 result_lvalue = VG_(threads)[thread_id].m_eax;
sewardjde4a1d02002-03-22 01:27:54 +00001640
1641
1642/* ---------------------------------------------------------------------
1643 Exports of vg_transtab.c
1644 ------------------------------------------------------------------ */
1645
1646/* An entry in the translation table (TT). */
1647typedef
1648 struct {
1649 /* +0 */ Addr orig_addr;
1650 /* +4 */ Addr trans_addr;
1651 /* +8 */ UInt mru_epoch;
1652 /* +12 */ UShort orig_size;
1653 /* +14 */ UShort trans_size;
1654 }
1655 TTEntry;
1656
1657/* The number of basic blocks in an epoch (one age-step). */
1658#define VG_BBS_PER_EPOCH 20000
1659
1660extern void VG_(get_tt_tc_used) ( UInt* tt_used, UInt* tc_used );
1661extern void VG_(maybe_do_lru_pass) ( void );
1662extern void VG_(flush_transtab) ( void );
1663extern Addr VG_(copy_to_transcache) ( Addr trans_addr, Int trans_size );
1664extern void VG_(add_to_trans_tab) ( TTEntry* tte );
sewardj18d75132002-05-16 11:06:21 +00001665extern void VG_(invalidate_translations) ( Addr start, UInt range );
sewardjde4a1d02002-03-22 01:27:54 +00001666
sewardj18d75132002-05-16 11:06:21 +00001667extern void VG_(init_tt_tc) ( void );
sewardjde4a1d02002-03-22 01:27:54 +00001668
1669extern void VG_(sanity_check_tc_tt) ( void );
1670extern Addr VG_(search_transtab) ( Addr original_addr );
1671
1672extern void VG_(invalidate_tt_fast)( void );
1673
1674
1675/* ---------------------------------------------------------------------
1676 Exports of vg_vtagops.c
1677 ------------------------------------------------------------------ */
1678
1679/* Lists the names of value-tag operations used in instrumented
1680 code. These are the third argument to TAG1 and TAG2 uinsns. */
1681
1682typedef
1683 enum {
1684 /* Unary. */
1685 VgT_PCast40, VgT_PCast20, VgT_PCast10,
1686 VgT_PCast01, VgT_PCast02, VgT_PCast04,
1687
1688 VgT_PCast14, VgT_PCast12, VgT_PCast11,
1689
1690 VgT_Left4, VgT_Left2, VgT_Left1,
1691
1692 VgT_SWiden14, VgT_SWiden24, VgT_SWiden12,
1693 VgT_ZWiden14, VgT_ZWiden24, VgT_ZWiden12,
1694
1695 /* Binary; 1st is rd; 2nd is rd+wr */
1696 VgT_UifU4, VgT_UifU2, VgT_UifU1, VgT_UifU0,
1697 VgT_DifD4, VgT_DifD2, VgT_DifD1,
1698
1699 VgT_ImproveAND4_TQ, VgT_ImproveAND2_TQ, VgT_ImproveAND1_TQ,
1700 VgT_ImproveOR4_TQ, VgT_ImproveOR2_TQ, VgT_ImproveOR1_TQ,
1701 VgT_DebugFn
1702 }
1703 VgTagOp;
1704
1705extern Char* VG_(nameOfTagOp) ( VgTagOp );
1706extern UInt VG_(DebugFn) ( UInt a1, UInt a2 );
1707
1708
1709/* ---------------------------------------------------------------------
1710 Exports of vg_syscall.S
1711 ------------------------------------------------------------------ */
1712
1713extern void VG_(do_syscall) ( void );
1714
1715
1716/* ---------------------------------------------------------------------
1717 Exports of vg_startup.S
1718 ------------------------------------------------------------------ */
1719
sewardjde4a1d02002-03-22 01:27:54 +00001720extern void VG_(switch_to_real_CPU) ( void );
1721
sewardj35805422002-04-21 13:05:34 +00001722extern void VG_(swizzle_esp_then_start_GDB) ( Addr m_eip_at_error,
1723 Addr m_esp_at_error,
1724 Addr m_ebp_at_error );
sewardjde4a1d02002-03-22 01:27:54 +00001725
1726
1727/* ---------------------------------------------------------------------
1728 Exports of vg_dispatch.S
1729 ------------------------------------------------------------------ */
1730
sewardj2e93c502002-04-12 11:12:52 +00001731/* Run a thread for a (very short) while, until some event happens
1732 which means we need to defer to the scheduler. */
1733extern UInt VG_(run_innerloop) ( void );
sewardjde4a1d02002-03-22 01:27:54 +00001734
1735
1736/* ---------------------------------------------------------------------
1737 Exports of vg_helpers.S
1738 ------------------------------------------------------------------ */
1739
sewardjde4a1d02002-03-22 01:27:54 +00001740/* Mul, div, etc, -- we don't codegen these directly. */
1741extern void VG_(helper_idiv_64_32);
1742extern void VG_(helper_div_64_32);
1743extern void VG_(helper_idiv_32_16);
1744extern void VG_(helper_div_32_16);
1745extern void VG_(helper_idiv_16_8);
1746extern void VG_(helper_div_16_8);
1747
1748extern void VG_(helper_imul_32_64);
1749extern void VG_(helper_mul_32_64);
1750extern void VG_(helper_imul_16_32);
1751extern void VG_(helper_mul_16_32);
1752extern void VG_(helper_imul_8_16);
1753extern void VG_(helper_mul_8_16);
1754
1755extern void VG_(helper_CLD);
1756extern void VG_(helper_STD);
1757extern void VG_(helper_get_dirflag);
1758
sewardj7d78e782002-06-02 00:04:00 +00001759extern void VG_(helper_CLC);
1760extern void VG_(helper_STC);
1761
sewardjde4a1d02002-03-22 01:27:54 +00001762extern void VG_(helper_shldl);
1763extern void VG_(helper_shldw);
1764extern void VG_(helper_shrdl);
1765extern void VG_(helper_shrdw);
1766
1767extern void VG_(helper_RDTSC);
1768extern void VG_(helper_CPUID);
1769
sewardjde4a1d02002-03-22 01:27:54 +00001770extern void VG_(helper_bsf);
1771extern void VG_(helper_bsr);
1772
1773extern void VG_(helper_fstsw_AX);
1774extern void VG_(helper_SAHF);
sewardj4d0ab1f2002-03-24 10:00:09 +00001775extern void VG_(helper_DAS);
sewardjfe8a1662002-03-24 11:54:07 +00001776extern void VG_(helper_DAA);
sewardjde4a1d02002-03-22 01:27:54 +00001777
1778extern void VG_(helper_value_check4_fail);
1779extern void VG_(helper_value_check2_fail);
1780extern void VG_(helper_value_check1_fail);
1781extern void VG_(helper_value_check0_fail);
1782
sewardj20917d82002-05-28 01:36:45 +00001783/* NOT A FUNCTION; this is a bogus RETURN ADDRESS. */
sewardj54cacf02002-04-12 23:24:59 +00001784extern void VG_(signalreturn_bogusRA)( void );
sewardj20917d82002-05-28 01:36:45 +00001785
sewardj54cacf02002-04-12 23:24:59 +00001786
njn4f9c9342002-04-29 16:03:24 +00001787/* ---------------------------------------------------------------------
1788 Exports of vg_cachesim.c
1789 ------------------------------------------------------------------ */
1790
1791extern UCodeBlock* VG_(cachesim_instrument)(UCodeBlock* cb_in, Addr orig_addr);
1792
1793typedef struct _iCC iCC;
1794typedef struct _idCC idCC;
1795
1796extern void VG_(init_cachesim) ( void );
1797extern void VG_(show_cachesim_results)( Int client_argc, Char** client_argv );
1798
1799extern void VG_(cachesim_log_non_mem_instr)( iCC* cc );
1800extern void VG_(cachesim_log_mem_instr) ( idCC* cc, Addr data_addr );
sewardjde4a1d02002-03-22 01:27:54 +00001801
sewardj18d75132002-05-16 11:06:21 +00001802extern void VG_(cachesim_notify_discard) ( TTEntry* tte );
1803
1804
sewardjde4a1d02002-03-22 01:27:54 +00001805/* ---------------------------------------------------------------------
1806 The state of the simulated CPU.
1807 ------------------------------------------------------------------ */
1808
1809/* This is the Intel register encoding. */
1810#define R_EAX 0
1811#define R_ECX 1
1812#define R_EDX 2
1813#define R_EBX 3
1814#define R_ESP 4
1815#define R_EBP 5
1816#define R_ESI 6
1817#define R_EDI 7
1818
1819#define R_AL (0+R_EAX)
1820#define R_CL (0+R_ECX)
1821#define R_DL (0+R_EDX)
1822#define R_BL (0+R_EBX)
1823#define R_AH (4+R_EAX)
1824#define R_CH (4+R_ECX)
1825#define R_DH (4+R_EDX)
1826#define R_BH (4+R_EBX)
1827
1828
1829/* ---------------------------------------------------------------------
1830 Offsets into baseBlock for everything which needs to referred to
1831 from generated code. The order of these decls does not imply
1832 what the order of the actual offsets is. The latter is important
1833 and is set up in vg_main.c.
1834 ------------------------------------------------------------------ */
1835
1836/* An array of words. In generated code, %ebp always points to the
1837 start of this array. Useful stuff, like the simulated CPU state,
1838 and the addresses of helper functions, can then be found by
1839 indexing off %ebp. The following declares variables which, at
1840 startup time, are given values denoting offsets into baseBlock.
1841 These offsets are in *words* from the start of baseBlock. */
1842
1843#define VG_BASEBLOCK_WORDS 200
1844
1845extern UInt VG_(baseBlock)[VG_BASEBLOCK_WORDS];
1846
1847
1848/* -----------------------------------------------------
1849 Read-write parts of baseBlock.
1850 -------------------------------------------------- */
1851
1852/* State of the simulated CPU. */
1853extern Int VGOFF_(m_eax);
1854extern Int VGOFF_(m_ecx);
1855extern Int VGOFF_(m_edx);
1856extern Int VGOFF_(m_ebx);
1857extern Int VGOFF_(m_esp);
1858extern Int VGOFF_(m_ebp);
1859extern Int VGOFF_(m_esi);
1860extern Int VGOFF_(m_edi);
1861extern Int VGOFF_(m_eflags);
1862extern Int VGOFF_(m_fpustate);
1863extern Int VGOFF_(m_eip);
1864
1865/* Reg-alloc spill area (VG_MAX_SPILLSLOTS words long). */
1866extern Int VGOFF_(spillslots);
1867
1868/* Records the valid bits for the 8 integer regs & flags reg. */
1869extern Int VGOFF_(sh_eax);
1870extern Int VGOFF_(sh_ecx);
1871extern Int VGOFF_(sh_edx);
1872extern Int VGOFF_(sh_ebx);
1873extern Int VGOFF_(sh_esp);
1874extern Int VGOFF_(sh_ebp);
1875extern Int VGOFF_(sh_esi);
1876extern Int VGOFF_(sh_edi);
1877extern Int VGOFF_(sh_eflags);
1878
1879
1880/* -----------------------------------------------------
1881 Read-only parts of baseBlock.
1882 -------------------------------------------------- */
1883
1884/* Offsets of addresses of helper functions. A "helper" function is
1885 one which is called from generated code. */
1886
1887extern Int VGOFF_(helper_idiv_64_32);
1888extern Int VGOFF_(helper_div_64_32);
1889extern Int VGOFF_(helper_idiv_32_16);
1890extern Int VGOFF_(helper_div_32_16);
1891extern Int VGOFF_(helper_idiv_16_8);
1892extern Int VGOFF_(helper_div_16_8);
1893
1894extern Int VGOFF_(helper_imul_32_64);
1895extern Int VGOFF_(helper_mul_32_64);
1896extern Int VGOFF_(helper_imul_16_32);
1897extern Int VGOFF_(helper_mul_16_32);
1898extern Int VGOFF_(helper_imul_8_16);
1899extern Int VGOFF_(helper_mul_8_16);
1900
1901extern Int VGOFF_(helper_CLD);
1902extern Int VGOFF_(helper_STD);
1903extern Int VGOFF_(helper_get_dirflag);
1904
sewardj7d78e782002-06-02 00:04:00 +00001905extern Int VGOFF_(helper_CLC);
1906extern Int VGOFF_(helper_STC);
1907
sewardjde4a1d02002-03-22 01:27:54 +00001908extern Int VGOFF_(helper_shldl);
1909extern Int VGOFF_(helper_shldw);
1910extern Int VGOFF_(helper_shrdl);
1911extern Int VGOFF_(helper_shrdw);
1912
1913extern Int VGOFF_(helper_RDTSC);
1914extern Int VGOFF_(helper_CPUID);
1915
sewardjde4a1d02002-03-22 01:27:54 +00001916extern Int VGOFF_(helper_bsf);
1917extern Int VGOFF_(helper_bsr);
1918
1919extern Int VGOFF_(helper_fstsw_AX);
1920extern Int VGOFF_(helper_SAHF);
sewardj4d0ab1f2002-03-24 10:00:09 +00001921extern Int VGOFF_(helper_DAS);
sewardjfe8a1662002-03-24 11:54:07 +00001922extern Int VGOFF_(helper_DAA);
sewardjde4a1d02002-03-22 01:27:54 +00001923
1924extern Int VGOFF_(helper_value_check4_fail);
1925extern Int VGOFF_(helper_value_check2_fail);
1926extern Int VGOFF_(helper_value_check1_fail);
1927extern Int VGOFF_(helper_value_check0_fail);
1928
sewardjde4a1d02002-03-22 01:27:54 +00001929extern Int VGOFF_(helperc_STOREV4); /* :: UInt -> Addr -> void */
1930extern Int VGOFF_(helperc_STOREV2); /* :: UInt -> Addr -> void */
1931extern Int VGOFF_(helperc_STOREV1); /* :: UInt -> Addr -> void */
1932
1933extern Int VGOFF_(helperc_LOADV4); /* :: Addr -> UInt -> void */
1934extern Int VGOFF_(helperc_LOADV2); /* :: Addr -> UInt -> void */
1935extern Int VGOFF_(helperc_LOADV1); /* :: Addr -> UInt -> void */
1936
1937extern Int VGOFF_(handle_esp_assignment); /* :: Addr -> void */
1938extern Int VGOFF_(fpu_write_check); /* :: Addr -> Int -> void */
1939extern Int VGOFF_(fpu_read_check); /* :: Addr -> Int -> void */
1940
njn4f9c9342002-04-29 16:03:24 +00001941extern Int VGOFF_(cachesim_log_non_mem_instr);
1942extern Int VGOFF_(cachesim_log_mem_instr);
sewardjde4a1d02002-03-22 01:27:54 +00001943
1944#endif /* ndef __VG_INCLUDE_H */
1945
sewardj3b2736a2002-03-24 12:18:35 +00001946
1947/* ---------------------------------------------------------------------
1948 Finally - autoconf-generated settings
1949 ------------------------------------------------------------------ */
1950
1951#include "config.h"
1952
sewardjde4a1d02002-03-22 01:27:54 +00001953/*--------------------------------------------------------------------*/
1954/*--- end vg_include.h ---*/
1955/*--------------------------------------------------------------------*/