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