blob: c4ba60310b51b005bae6ee0140900629b800d930 [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. */
80#define M_PROCMAP_BUF 20000
81
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. */
101#define M_VG_COLLECT_NO_ERRORS_AFTER 500
102
103/* These many bytes below %ESP are considered addressible if we're
104 doing the --workaround-gcc296-bugs hack. */
105#define VG_GCC296_BUG_STACK_SLOP 256
106
107/* The maximum number of calls we're prepared to save in a
108 backtrace. */
109#define VG_DEEPEST_BACKTRACE 50
110
111/* Number of lists in which we keep track of malloc'd but not free'd
112 blocks. Should be prime. */
113#define VG_N_MALLOCLISTS 997
114
115/* Number of lists in which we keep track of ExeContexts. Should be
116 prime. */
117#define VG_N_EC_LISTS /*997*/ 4999
118
sewardj2e93c502002-04-12 11:12:52 +0000119/* Defines the thread-scheduling timeslice, in terms of the number of
120 basic blocks we attempt to run each thread for. Smaller values
121 give finer interleaving but much increased scheduling overheads. */
122#define VG_SCHEDULING_QUANTUM 10000
123
124/* The maximum number of pthreads that we support. This is
125 deliberately not very high since our implementation of some of the
126 scheduler algorithms is surely O(N^2) in the number of threads,
127 since that's simple, at least. And (in practice) we hope that most
128 programs do not need many threads. */
129#define VG_N_THREADS 20
130
131/* Number of file descriptors that can simultaneously be waited on for
132 I/O to complete. Perhaps this should be the same as VG_N_THREADS
133 (surely a thread can't wait on more than one fd at once?. Who
134 knows.) */
135#define VG_N_WAITING_FDS 10
136
137/* Maximum number of mutexes allowed. */
138#define VG_N_MUTEXES 10
139
sewardjde4a1d02002-03-22 01:27:54 +0000140
141/* ---------------------------------------------------------------------
142 Basic types
143 ------------------------------------------------------------------ */
144
145typedef unsigned char UChar;
146typedef unsigned short UShort;
147typedef unsigned int UInt;
148typedef unsigned long long int ULong;
149
150typedef signed char Char;
151typedef signed short Short;
152typedef signed int Int;
153typedef signed long long int Long;
154
155typedef unsigned int Addr;
156
157typedef unsigned char Bool;
158#define False ((Bool)0)
159#define True ((Bool)1)
160
161#define mycat_wrk(aaa,bbb) aaa##bbb
162#define mycat(aaa,bbb) mycat_wrk(aaa,bbb)
163
164/* Just pray that gcc's constant folding works properly ... */
165#define BITS(bit7,bit6,bit5,bit4,bit3,bit2,bit1,bit0) \
166 ( ((bit7) << 7) | ((bit6) << 6) | ((bit5) << 5) | ((bit4) << 4) \
167 | ((bit3) << 3) | ((bit2) << 2) | ((bit1) << 1) | (bit0))
168
169
170/* ---------------------------------------------------------------------
171 Now the basic types are set up, we can haul in the kernel-interface
172 definitions.
173 ------------------------------------------------------------------ */
174
175#include "./vg_kerneliface.h"
176
177
178/* ---------------------------------------------------------------------
179 Command-line-settable options
180 ------------------------------------------------------------------ */
181
182#define VG_CLO_SMC_NONE 0
183#define VG_CLO_SMC_SOME 1
184#define VG_CLO_SMC_ALL 2
185
186#define VG_CLO_MAX_SFILES 10
187
sewardj97ced732002-03-25 00:07:36 +0000188/* Shall we V-check addrs (they are always A checked too): default: YES */
189extern Bool VG_(clo_check_addrVs);
sewardjde4a1d02002-03-22 01:27:54 +0000190/* Enquire about whether to attach to GDB at errors? default: NO */
191extern Bool VG_(clo_GDB_attach);
192/* Sanity-check level: 0 = none, 1 (default), > 1 = expensive. */
193extern Int VG_(sanity_level);
194/* Verbosity level: 0 = silent, 1 (default), > 1 = more verbose. */
195extern Int VG_(clo_verbosity);
196/* Automatically attempt to demangle C++ names? default: YES */
197extern Bool VG_(clo_demangle);
198/* Do leak check at exit? default: NO */
199extern Bool VG_(clo_leak_check);
200/* In leak check, show reachable-but-not-freed blocks? default: NO */
201extern Bool VG_(clo_show_reachable);
202/* How closely should we compare ExeContexts in leak records? default: 2 */
203extern Int VG_(clo_leak_resolution);
204/* Round malloc sizes upwards to integral number of words? default:
205 NO */
206extern Bool VG_(clo_sloppy_malloc);
207/* Allow loads from partially-valid addresses? default: YES */
208extern Bool VG_(clo_partial_loads_ok);
209/* Simulate child processes? default: NO */
210extern Bool VG_(clo_trace_children);
211/* The file id on which we send all messages. default: 2 (stderr). */
212extern Int VG_(clo_logfile_fd);
213/* Max volume of the freed blocks queue. */
214extern Int VG_(clo_freelist_vol);
215/* Assume accesses immediately below %esp are due to gcc-2.96 bugs.
216 default: NO */
217extern Bool VG_(clo_workaround_gcc296_bugs);
218
219/* The number of suppression files specified. */
220extern Int VG_(clo_n_suppressions);
221/* The names of the suppression files. */
222extern Char* VG_(clo_suppressions)[VG_CLO_MAX_SFILES];
223
224/* Single stepping? default: NO */
225extern Bool VG_(clo_single_step);
226/* Code improvement? default: YES */
227extern Bool VG_(clo_optimise);
228/* Memory-check instrumentation? default: YES */
229extern Bool VG_(clo_instrument);
230/* DEBUG: clean up instrumented code? default: YES */
231extern Bool VG_(clo_cleanup);
sewardjde4a1d02002-03-22 01:27:54 +0000232/* SMC write checks? default: SOME (1,2,4 byte movs to mem) */
233extern Int VG_(clo_smc_check);
234/* DEBUG: print system calls? default: NO */
235extern Bool VG_(clo_trace_syscalls);
236/* DEBUG: print signal details? default: NO */
237extern Bool VG_(clo_trace_signals);
238/* DEBUG: print symtab details? default: NO */
239extern Bool VG_(clo_trace_symtab);
240/* DEBUG: print malloc details? default: NO */
241extern Bool VG_(clo_trace_malloc);
sewardj8937c812002-04-12 20:12:20 +0000242/* DEBUG: print thread scheduling events? default: NO */
243extern Bool VG_(clo_trace_sched);
sewardj45b4b372002-04-16 22:50:32 +0000244/* DEBUG: print pthread (mutex etc) events? default: 0 (none), 1
245 (some), 2 (all) */
246extern Int VG_(clo_trace_pthread_level);
sewardjde4a1d02002-03-22 01:27:54 +0000247/* Stop after this many basic blocks. default: Infinity. */
248extern ULong VG_(clo_stop_after);
249/* Display gory details for the k'th most popular error. default:
250 Infinity. */
251extern Int VG_(clo_dump_error);
252/* Number of parents of a backtrace. Default: 8. */
253extern Int VG_(clo_backtrace_size);
254
255
256/* ---------------------------------------------------------------------
257 Debugging and profiling stuff
258 ------------------------------------------------------------------ */
259
260/* No, really. I _am_ that strange. */
261#define OINK(nnn) VG_(message)(Vg_DebugMsg, "OINK %d",nnn)
262
263/* Tools for building messages from multiple parts. */
264typedef
265 enum { Vg_UserMsg, Vg_DebugMsg, Vg_DebugExtraMsg }
266 VgMsgKind;
267
268extern void VG_(start_msg) ( VgMsgKind kind );
269extern void VG_(add_to_msg) ( Char* format, ... );
270extern void VG_(end_msg) ( void );
271
272/* Send a simple, single-part message. */
273extern void VG_(message) ( VgMsgKind kind, Char* format, ... );
274
275/* Create a logfile into which messages can be dumped. */
276extern void VG_(startup_logging) ( void );
277extern void VG_(shutdown_logging) ( void );
278
279
280/* Profiling stuff */
281#ifdef VG_PROFILE
282
283#define VGP_M_STACK 10
284
285#define VGP_M_CCS 20 /* == the # of elems in VGP_LIST */
286#define VGP_LIST \
287 VGP_PAIR(VgpRun=0, "running"), \
288 VGP_PAIR(VgpMalloc, "low-lev malloc/free"), \
289 VGP_PAIR(VgpCliMalloc, "client malloc/free"), \
290 VGP_PAIR(VgpTranslate, "translate-main"), \
291 VGP_PAIR(VgpToUCode, "to-ucode"), \
292 VGP_PAIR(VgpFromUcode, "from-ucode"), \
293 VGP_PAIR(VgpImprove, "improve"), \
294 VGP_PAIR(VgpInstrument, "instrument"), \
295 VGP_PAIR(VgpCleanup, "cleanup"), \
296 VGP_PAIR(VgpRegAlloc, "reg-alloc"), \
297 VGP_PAIR(VgpDoLRU, "do-lru"), \
298 VGP_PAIR(VgpSlowFindT, "slow-search-transtab"), \
299 VGP_PAIR(VgpInitAudit, "init-mem-audit"), \
300 VGP_PAIR(VgpExeContext, "exe-context"), \
301 VGP_PAIR(VgpReadSyms, "read-syms"), \
302 VGP_PAIR(VgpAddToT, "add-to-transtab"), \
303 VGP_PAIR(VgpSARP, "set-addr-range-perms"), \
304 VGP_PAIR(VgpSyscall, "syscall wrapper"), \
305 VGP_PAIR(VgpSpare1, "spare 1"), \
306 VGP_PAIR(VgpSpare2, "spare 2")
307
308#define VGP_PAIR(enumname,str) enumname
309typedef enum { VGP_LIST } VgpCC;
310#undef VGP_PAIR
311
312extern void VGP_(init_profiling) ( void );
313extern void VGP_(done_profiling) ( void );
314extern void VGP_(pushcc) ( VgpCC );
315extern void VGP_(popcc) ( void );
316
317#define VGP_PUSHCC(cc) VGP_(pushcc)(cc)
318#define VGP_POPCC VGP_(popcc)()
319
320#else
321
322#define VGP_PUSHCC(cc) /* */
323#define VGP_POPCC /* */
324
325#endif /* VG_PROFILE */
326
327
328/* ---------------------------------------------------------------------
329 Exports of vg_malloc2.c
330 ------------------------------------------------------------------ */
331
332/* Allocation arenas.
333 SYMTAB is for Valgrind's symbol table storage.
334 CLIENT is for the client's mallocs/frees.
335 DEMANGLE is for the C++ demangler.
336 EXECTXT is for storing ExeContexts.
337 ERRCTXT is for storing ErrContexts.
338 PRIVATE is for Valgrind general stuff.
339 TRANSIENT is for very short-term use. It should be empty
340 in between uses.
341 When adding a new arena, remember also to add it
342 to ensure_mm_init().
343*/
344typedef Int ArenaId;
345
346#define VG_N_ARENAS 7
347
348#define VG_AR_PRIVATE 0 /* :: ArenaId */
349#define VG_AR_SYMTAB 1 /* :: ArenaId */
350#define VG_AR_CLIENT 2 /* :: ArenaId */
351#define VG_AR_DEMANGLE 3 /* :: ArenaId */
352#define VG_AR_EXECTXT 4 /* :: ArenaId */
353#define VG_AR_ERRCTXT 5 /* :: ArenaId */
354#define VG_AR_TRANSIENT 6 /* :: ArenaId */
355
356extern void* VG_(malloc) ( ArenaId arena, Int nbytes );
357extern void VG_(free) ( ArenaId arena, void* ptr );
358extern void* VG_(calloc) ( ArenaId arena, Int nmemb, Int nbytes );
359extern void* VG_(realloc) ( ArenaId arena, void* ptr, Int size );
360extern void* VG_(malloc_aligned) ( ArenaId aid, Int req_alignB,
361 Int req_pszB );
362
363extern void VG_(mallocSanityCheckArena) ( ArenaId arena );
364extern void VG_(mallocSanityCheckAll) ( void );
365
366extern void VG_(show_all_arena_stats) ( void );
367extern Bool VG_(is_empty_arena) ( ArenaId aid );
368
369
370/* The red-zone size for the client. This can be arbitrary, but
371 unfortunately must be set at compile time. */
372#define VG_AR_CLIENT_REDZONE_SZW 4
373
374#define VG_AR_CLIENT_REDZONE_SZB \
375 (VG_AR_CLIENT_REDZONE_SZW * VKI_BYTES_PER_WORD)
376
377
378/* ---------------------------------------------------------------------
sewardj2e93c502002-04-12 11:12:52 +0000379 Exports of vg_clientfuns.c
380 ------------------------------------------------------------------ */
381
382/* This doesn't export code or data that valgrind.so needs to link
383 against. However, the scheduler does need to know the following
384 request codes. A few, publically-visible, request codes are also
385 defined in valgrind.h. */
386
387#define VG_USERREQ__MALLOC 0x2001
388#define VG_USERREQ__BUILTIN_NEW 0x2002
389#define VG_USERREQ__BUILTIN_VEC_NEW 0x2003
390
391#define VG_USERREQ__FREE 0x2004
392#define VG_USERREQ__BUILTIN_DELETE 0x2005
393#define VG_USERREQ__BUILTIN_VEC_DELETE 0x2006
394
395#define VG_USERREQ__CALLOC 0x2007
396#define VG_USERREQ__REALLOC 0x2008
397#define VG_USERREQ__MEMALIGN 0x2009
398
399
400#define VG_USERREQ__PTHREAD_CREATE 0x3001
sewardjbc5b99f2002-04-13 00:08:51 +0000401#define VG_USERREQ__PTHREAD_JOIN 0x3002
402#define VG_USERREQ__PTHREAD_GET_THREADID 0x3003
403#define VG_USERREQ__PTHREAD_MUTEX_INIT 0x3004
404#define VG_USERREQ__PTHREAD_MUTEX_LOCK 0x3005
405#define VG_USERREQ__PTHREAD_MUTEX_UNLOCK 0x3006
406#define VG_USERREQ__PTHREAD_MUTEX_DESTROY 0x3007
407#define VG_USERREQ__PTHREAD_CANCEL 0x3008
sewardj2e93c502002-04-12 11:12:52 +0000408
sewardj45b4b372002-04-16 22:50:32 +0000409/* Cosmetic ... */
410#define VG_USERREQ__GET_PTHREAD_TRACE_LEVEL 0x3101
411
sewardj54cacf02002-04-12 23:24:59 +0000412/*
413In vg_constants.h:
414#define VG_USERREQ__SIGNAL_RETURNS 0x4001
sewardjbc5b99f2002-04-13 00:08:51 +0000415#define VG_USERREQ__PTHREAD_RETURNS 0x4002
416#define VG_USERREQ__SHUTDOWN_VALGRIND 0x4003
sewardj54cacf02002-04-12 23:24:59 +0000417*/
418
419
sewardj2e93c502002-04-12 11:12:52 +0000420/* ---------------------------------------------------------------------
421 Constants pertaining to the simulated CPU state, VG_(baseBlock),
422 which need to go here to avoid ugly circularities.
423 ------------------------------------------------------------------ */
424
425/* How big is the saved FPU state? */
426#define VG_SIZE_OF_FPUSTATE 108
427/* ... and in words ... */
428#define VG_SIZE_OF_FPUSTATE_W ((VG_SIZE_OF_FPUSTATE+3)/4)
429
430
431/* ---------------------------------------------------------------------
432 Exports of vg_scheduler.c
433 ------------------------------------------------------------------ */
434
435/* ThreadIds are simply indices into the vg_threads[] array. */
436typedef
437 UInt
438 ThreadId;
439
440/* MutexIds are simply indices into the vg_mutexes[] array. */
441typedef
442 UInt
443 MutexId;
444
445
446#define VG_INVALID_THREADID ((ThreadId)(-1))
447
448typedef
449 enum {
450 VgTs_Empty, /* this slot is not in use */
451 VgTs_Runnable, /* waiting to be scheduled */
452 VgTs_WaitJoiner, /* waiting for someone to do join on me */
453 VgTs_WaitJoinee, /* waiting for the thread I did join on */
454 VgTs_WaitFD, /* waiting for I/O completion on a fd */
455 VgTs_WaitMX, /* waiting on a mutex */
456 VgTs_Sleeping /* sleeping for a while */
457 }
458 ThreadStatus;
459
460typedef
461 struct {
462 /* The thread identity is simply the index in vg_threads[].
463 ThreadId == 0 is the root thread and has the special property
464 that we don't try and allocate or deallocate its stack. */
465
466 /* Current scheduling status. */
467 ThreadStatus status;
468
469 /* Identity of joiner (thread who called join on me), or
470 VG_INVALID_THREADID if no one asked to join yet. */
471 ThreadId joiner;
472
473 /* Identity of mutex we are waiting on, if .status == WaitMX. */
474 MutexId waited_on_mid;
475
476 /* If VgTs_Sleeping, this is when we should wake up. */
477 ULong awaken_at;
478
479 /* return value */
480 void* retval;
481
482 /* Stacks. When a thread slot is freed, we don't deallocate its
483 stack; we just leave it lying around for the next use of the
484 slot. If the next use of the slot requires a larger stack,
485 only then is the old one deallocated and a new one
486 allocated.
487
488 For the main thread (threadid == 0), this mechanism doesn't
489 apply. We don't know the size of the stack since we didn't
490 allocate it, and furthermore we never reallocate it. */
491
492 /* The allocated size of this thread's stack (permanently zero
493 if this is ThreadId == 0, since we didn't allocate its stack) */
494 UInt stack_size;
495
496 /* Address of the lowest word in this thread's stack. NULL means
497 not allocated yet.
498 */
499 Addr stack_base;
500
501 /* Saved machine context. */
502 UInt m_eax;
503 UInt m_ebx;
504 UInt m_ecx;
505 UInt m_edx;
506 UInt m_esi;
507 UInt m_edi;
508 UInt m_ebp;
509 UInt m_esp;
510 UInt m_eflags;
511 UInt m_eip;
512 UInt m_fpu[VG_SIZE_OF_FPUSTATE_W];
513
514 UInt sh_eax;
515 UInt sh_ebx;
516 UInt sh_ecx;
517 UInt sh_edx;
518 UInt sh_esi;
519 UInt sh_edi;
520 UInt sh_ebp;
521 UInt sh_esp;
522 UInt sh_eflags;
523 }
524 ThreadState;
525
526
527/* Copy the specified thread's state into VG_(baseBlock) in
528 preparation for running it. */
529extern void VG_(load_thread_state)( ThreadId );
530
531/* Save the specified thread's state back in VG_(baseBlock), and fill
532 VG_(baseBlock) with junk, for sanity-check reasons. */
533extern void VG_(save_thread_state)( ThreadId );
534
535/* Get the thread state block for the specified thread. */
536extern ThreadState* VG_(get_thread_state)( ThreadId );
537
538
539/* Create, and add to TT/TC, the translation of a client basic
540 block. */
541extern void VG_(create_translation_for) ( Addr orig_addr );
542
543/* Return codes from the scheduler. */
544typedef
545 enum { VgSrc_Deadlock, VgSrc_Shutdown, VgSrc_BbsDone }
546 VgSchedReturnCode;
547
548/* The scheduler. */
549extern VgSchedReturnCode VG_(scheduler) ( void );
550
551extern void VG_(scheduler_init) ( void );
552
sewardj15a43e12002-04-17 19:35:12 +0000553extern void VG_(pp_sched_status) ( void );
sewardj2e93c502002-04-12 11:12:52 +0000554
555/* vg_oursignalhandler() might longjmp(). Here's the jmp_buf. */
556extern jmp_buf VG_(scheduler_jmpbuf);
557/* ... and if so, here's the signal which caused it to do so. */
558extern Int VG_(longjmpd_on_signal);
559
560
561/* We check that the initial stack, which we can't move, is allocated
562 here. VG_(scheduler_init) checks this.
563*/
564#define VG_STARTUP_STACK_MASK (Addr)0xBFFF8000
565
566
567/* The red-zone size which we put at the bottom (highest address) of
568 thread stacks, for paranoia reasons. This can be arbitrary, and
569 doesn't really need to be set at compile time. */
570#define VG_AR_CLIENT_STACKBASE_REDZONE_SZW 4
571
572#define VG_AR_CLIENT_STACKBASE_REDZONE_SZB \
573 (VG_AR_CLIENT_STACKBASE_REDZONE_SZW * VKI_BYTES_PER_WORD)
574
575
576
577/* ---------------------------------------------------------------------
sewardjde4a1d02002-03-22 01:27:54 +0000578 Exports of vg_signals.c
579 ------------------------------------------------------------------ */
580
sewardjde4a1d02002-03-22 01:27:54 +0000581extern void VG_(sigstartup_actions) ( void );
582
sewardj2e93c502002-04-12 11:12:52 +0000583extern void VG_(deliver_signals) ( ThreadId );
sewardjde4a1d02002-03-22 01:27:54 +0000584extern void VG_(unblock_host_signal) ( Int sigNo );
585
586
587/* Fake system calls for signal handling. */
sewardj2e93c502002-04-12 11:12:52 +0000588extern void VG_(do__NR_sigaction) ( ThreadId tid );
sewardjde4a1d02002-03-22 01:27:54 +0000589extern void VG_(do__NR_sigprocmask) ( Int how, vki_ksigset_t* set );
590
sewardj2e93c502002-04-12 11:12:52 +0000591/* Modify the current thread's state once we have detected it is
592 returning from a signal handler. */
sewardj77e466c2002-04-14 02:29:29 +0000593extern Bool VG_(signal_returns) ( ThreadId );
sewardjde4a1d02002-03-22 01:27:54 +0000594
sewardj2e93c502002-04-12 11:12:52 +0000595/* Handy utilities to block/restore all host signals. */
596extern void VG_(block_all_host_signals)
597 ( /* OUT */ vki_ksigset_t* saved_mask );
598extern void VG_(restore_host_signals)
599 ( /* IN */ vki_ksigset_t* saved_mask );
sewardjde4a1d02002-03-22 01:27:54 +0000600
601/* ---------------------------------------------------------------------
602 Exports of vg_mylibc.c
603 ------------------------------------------------------------------ */
604
605
606#define NULL ((void*)0)
607
608extern void VG_(exit)( Int status )
609 __attribute__ ((__noreturn__));
610
611extern void VG_(printf) ( const char *format, ... );
612/* too noisy ... __attribute__ ((format (printf, 1, 2))) ; */
613
614extern void VG_(sprintf) ( Char* buf, Char *format, ... );
615
616extern void VG_(vprintf) ( void(*send)(Char),
617 const Char *format, va_list vargs );
618
619extern Bool VG_(isspace) ( Char c );
620
621extern Int VG_(strlen) ( const Char* str );
622
623extern Long VG_(atoll) ( Char* str );
624
625extern Char* VG_(strcat) ( Char* dest, const Char* src );
626extern Char* VG_(strncat) ( Char* dest, const Char* src, Int n );
627extern Char* VG_(strpbrk) ( const Char* s, const Char* accept );
628
629extern Char* VG_(strcpy) ( Char* dest, const Char* src );
630
631extern Int VG_(strcmp) ( const Char* s1, const Char* s2 );
632extern Int VG_(strcmp_ws) ( const Char* s1, const Char* s2 );
633
634extern Int VG_(strncmp) ( const Char* s1, const Char* s2, Int nmax );
635extern Int VG_(strncmp_ws) ( const Char* s1, const Char* s2, Int nmax );
636
637extern Char* VG_(strstr) ( const Char* haystack, Char* needle );
638extern Char* VG_(strchr) ( const Char* s, Char c );
639extern Char* VG_(strdup) ( ArenaId aid, const Char* s);
640
641extern Char* VG_(getenv) ( Char* name );
642extern Int VG_(getpid) ( void );
sewardj2e93c502002-04-12 11:12:52 +0000643extern ULong VG_(read_microsecond_timer)( void );
sewardjde4a1d02002-03-22 01:27:54 +0000644
645
646extern Char VG_(toupper) ( Char c );
647
648extern void VG_(strncpy_safely) ( Char* dest, const Char* src, Int ndest );
649
650extern void VG_(strncpy) ( Char* dest, const Char* src, Int ndest );
651
652extern Bool VG_(stringMatch) ( Char* pat, Char* str );
653
654
655#define __STRING(x) #x
656
657/* Asserts are permanently enabled. Hurrah! */
658#define vg_assert(expr) \
659 ((void) ((expr) ? 0 : \
660 (VG_(assert_fail) (__STRING(expr), \
661 __FILE__, __LINE__, \
662 __PRETTY_FUNCTION__), 0)))
663
664extern void VG_(assert_fail) ( Char* expr, Char* file,
665 Int line, Char* fn )
666 __attribute__ ((__noreturn__));
667
sewardjde4a1d02002-03-22 01:27:54 +0000668/* Reading files. */
669extern Int VG_(open_read) ( Char* pathname );
670extern void VG_(close) ( Int fd );
671extern Int VG_(read) ( Int fd, void* buf, Int count);
672extern Int VG_(write) ( Int fd, void* buf, Int count);
673
sewardj2e93c502002-04-12 11:12:52 +0000674extern Int VG_(fcntl) ( Int fd, Int cmd, Int arg );
675
676extern Int VG_(select)( Int n,
677 vki_fd_set* readfds,
678 vki_fd_set* writefds,
679 vki_fd_set* exceptfds,
680 struct vki_timeval * timeout );
681extern Int VG_(nanosleep)( const struct vki_timespec *req,
682 struct vki_timespec *rem );
683
684
sewardjde4a1d02002-03-22 01:27:54 +0000685/* mmap-ery ... */
686extern void* VG_(mmap)( void* start, UInt length,
687 UInt prot, UInt flags, UInt fd, UInt offset );
688
sewardj2e93c502002-04-12 11:12:52 +0000689extern Int VG_(munmap)( void* start, Int length );
sewardjde4a1d02002-03-22 01:27:54 +0000690
691
692/* Print a (panic) message, and abort. */
693extern void VG_(panic) ( Char* str )
694 __attribute__ ((__noreturn__));
695
696/* Get memory by anonymous mmap. */
697void* VG_(get_memory_from_mmap) ( Int nBytes );
698
699/* Signal stuff. Note that these use the vk_ (kernel) structure
700 definitions, which are different in places from those that glibc
701 defines. Since we're operating right at the kernel interface,
702 glibc's view of the world is entirely irrelevant. */
703extern Int VG_(ksigfillset)( vki_ksigset_t* set );
704extern Int VG_(ksigemptyset)( vki_ksigset_t* set );
705extern Int VG_(ksigaddset)( vki_ksigset_t* set, Int signum );
706
707extern Int VG_(ksigprocmask)( Int how, const vki_ksigset_t* set,
708 vki_ksigset_t* oldset );
709extern Int VG_(ksigaction) ( Int signum,
710 const vki_ksigaction* act,
711 vki_ksigaction* oldact );
712extern Int VG_(ksigismember) ( vki_ksigset_t* set, Int signum );
713
714extern Int VG_(ksignal)(Int signum, void (*sighandler)(Int));
715
716extern Int VG_(ksigaltstack)( const vki_kstack_t* ss, vki_kstack_t* oss );
717
718
719
720/* ---------------------------------------------------------------------
721 Definitions for the JITter (vg_translate.c, vg_to_ucode.c,
722 vg_from_ucode.c).
723 ------------------------------------------------------------------ */
724
725/* Tags which describe what operands are. */
726typedef
727 enum { TempReg=0, ArchReg=1, RealReg=2,
728 SpillNo=3, Literal=4, Lit16=5,
729 NoValue=6 }
730 Tag;
731
732
733/* Microinstruction opcodes. */
734typedef
735 enum {
736 NOP,
737 GET,
738 PUT,
739 LOAD,
740 STORE,
741 MOV,
742 CMOV, /* Used for cmpxchg and cmov */
743 WIDEN,
744 JMP,
745
746 /* Read/write the %EFLAGS register into a TempReg. */
747 GETF, PUTF,
748
749 ADD, ADC, AND, OR, XOR, SUB, SBB,
750 SHL, SHR, SAR, ROL, ROR, RCL, RCR,
751 NOT, NEG, INC, DEC, BSWAP,
752 CC2VAL,
753
754 /* Not strictly needed, but useful for making better
755 translations of address calculations. */
756 LEA1, /* reg2 := const + reg1 */
757 LEA2, /* reg3 := const + reg1 + reg2 * 1,2,4 or 8 */
758
759 /* not for translating x86 calls -- only to call helpers */
760 CALLM_S, CALLM_E, /* Mark start and end of push/pop sequences
761 for CALLM. */
762 PUSH, POP, CLEAR, /* Add/remove/zap args for helpers. */
763 CALLM, /* call to a machine-code helper */
764
765 /* Hack for translating string (REP-) insns. Jump to literal if
766 TempReg/RealReg is zero. */
767 JIFZ,
768
769 /* FPU ops which read/write mem or don't touch mem at all. */
770 FPU_R,
771 FPU_W,
772 FPU,
773
774 /* Advance the simulated %eip by some small (< 128) number. */
775 INCEIP,
776
777 /* uinstrs which are not needed for mere translation of x86 code,
778 only for instrumentation of it. */
779 LOADV,
780 STOREV,
781 GETV,
782 PUTV,
783 TESTV,
784 SETV,
785 /* Get/set the v-bit (and it is only one bit) for the simulated
786 %eflags register. */
787 GETVF,
788 PUTVF,
789
790 /* Do a unary or binary tag op. Only for post-instrumented
791 code. For TAG1, first and only arg is a TempReg, and is both
792 arg and result reg. For TAG2, first arg is src, second is
793 dst, in the normal way; both are TempRegs. In both cases,
794 3rd arg is a RiCHelper with a Lit16 tag. This indicates
795 which tag op to do. */
796 TAG1,
797 TAG2
798 }
799 Opcode;
800
801
802/* Condition codes, observing the Intel encoding. CondAlways is an
803 extra. */
804typedef
805 enum {
806 CondO = 0, /* overflow */
807 CondNO = 1, /* no overflow */
808 CondB = 2, /* below */
809 CondNB = 3, /* not below */
810 CondZ = 4, /* zero */
811 CondNZ = 5, /* not zero */
812 CondBE = 6, /* below or equal */
813 CondNBE = 7, /* not below or equal */
814 CondS = 8, /* negative */
815 ConsNS = 9, /* not negative */
816 CondP = 10, /* parity even */
817 CondNP = 11, /* not parity even */
818 CondL = 12, /* jump less */
819 CondNL = 13, /* not less */
820 CondLE = 14, /* less or equal */
821 CondNLE = 15, /* not less or equal */
822 CondAlways = 16 /* Jump always */
823 }
824 Condcode;
825
826
sewardj2e93c502002-04-12 11:12:52 +0000827/* Descriptions of additional properties of *unconditional* jumps. */
828typedef
829 enum {
830 JmpBoring=0, /* boring unconditional jump */
831 JmpCall=1, /* jump due to an x86 call insn */
832 JmpRet=2, /* jump due to an x86 ret insn */
833 JmpSyscall=3, /* do a system call, then jump */
834 JmpClientReq=4 /* do a client request, then jump */
835 }
836 JmpKind;
837
838
sewardjde4a1d02002-03-22 01:27:54 +0000839/* Flags. User-level code can only read/write O(verflow), S(ign),
840 Z(ero), A(ux-carry), C(arry), P(arity), and may also write
841 D(irection). That's a total of 7 flags. A FlagSet is a bitset,
842 thusly:
843 76543210
844 DOSZACP
845 and bit 7 must always be zero since it is unused.
846*/
847typedef UChar FlagSet;
848
849#define FlagD (1<<6)
850#define FlagO (1<<5)
851#define FlagS (1<<4)
852#define FlagZ (1<<3)
853#define FlagA (1<<2)
854#define FlagC (1<<1)
855#define FlagP (1<<0)
856
857#define FlagsOSZACP (FlagO | FlagS | FlagZ | FlagA | FlagC | FlagP)
858#define FlagsOSZAP (FlagO | FlagS | FlagZ | FlagA | FlagP)
859#define FlagsOSZCP (FlagO | FlagS | FlagZ | FlagC | FlagP)
860#define FlagsOSACP (FlagO | FlagS | FlagA | FlagC | FlagP)
861#define FlagsSZACP ( FlagS | FlagZ | FlagA | FlagC | FlagP)
862#define FlagsSZAP ( FlagS | FlagZ | FlagA | FlagP)
sewardj4a7456e2002-03-24 13:52:19 +0000863#define FlagsZCP ( FlagZ | FlagC | FlagP)
sewardjde4a1d02002-03-22 01:27:54 +0000864#define FlagsOC (FlagO | FlagC )
sewardj4d0ab1f2002-03-24 10:00:09 +0000865#define FlagsAC ( FlagA | FlagC )
sewardjde4a1d02002-03-22 01:27:54 +0000866
867#define FlagsALL (FlagsOSZACP | FlagD)
868#define FlagsEmpty (FlagSet)0
869
870#define VG_IS_FLAG_SUBSET(set1,set2) \
871 (( ((FlagSet)set1) & ((FlagSet)set2) ) == ((FlagSet)set1) )
872
873#define VG_UNION_FLAG_SETS(set1,set2) \
874 ( ((FlagSet)set1) | ((FlagSet)set2) )
875
876
877
878/* A Micro (u)-instruction. */
879typedef
880 struct {
881 /* word 1 */
882 UInt lit32; /* 32-bit literal */
883
884 /* word 2 */
885 UShort val1; /* first operand */
886 UShort val2; /* second operand */
887
888 /* word 3 */
889 UShort val3; /* third operand */
890 UChar opcode; /* opcode */
891 UChar size; /* data transfer size */
892
893 /* word 4 */
894 FlagSet flags_r; /* :: FlagSet */
895 FlagSet flags_w; /* :: FlagSet */
896 UChar tag1:4; /* first operand tag */
897 UChar tag2:4; /* second operand tag */
898 UChar tag3:4; /* third operand tag */
899 UChar extra4b:4; /* Spare field, used by WIDEN for src
900 -size, and by LEA2 for scale
901 (1,2,4 or 8) */
902
903 /* word 5 */
904 UChar cond; /* condition, for jumps */
905 Bool smc_check:1; /* do a smc test, if writes memory. */
906 Bool signed_widen:1; /* signed or unsigned WIDEN ? */
sewardj2e93c502002-04-12 11:12:52 +0000907 JmpKind jmpkind:3; /* additional properties of unconditional JMP */
sewardjde4a1d02002-03-22 01:27:54 +0000908 }
909 UInstr;
910
911
912/* Expandable arrays of uinstrs. */
913typedef
914 struct {
915 Int used;
916 Int size;
917 UInstr* instrs;
918 Int nextTemp;
919 }
920 UCodeBlock;
921
922/* Refer to `the last instruction stuffed in', including as an
923 lvalue. */
924#define LAST_UINSTR(cb) (cb)->instrs[(cb)->used-1]
925
926/* An invalid temporary number :-) */
927#define INVALID_TEMPREG 999999999
928
929
930/* ---------------------------------------------------------------------
931 Exports of vg_demangle.c
932 ------------------------------------------------------------------ */
933
934extern void VG_(demangle) ( Char* orig, Char* result, Int result_size );
935
936
937/* ---------------------------------------------------------------------
938 Exports of vg_from_ucode.c
939 ------------------------------------------------------------------ */
940
941extern UChar* VG_(emit_code) ( UCodeBlock* cb, Int* nbytes );
942
943
944/* ---------------------------------------------------------------------
945 Exports of vg_to_ucode.c
946 ------------------------------------------------------------------ */
947
948extern Int VG_(disBB) ( UCodeBlock* cb, Addr eip0 );
949extern Char* VG_(nameOfIntReg) ( Int size, Int reg );
950extern Char VG_(nameOfIntSize) ( Int size );
951extern UInt VG_(extend_s_8to32) ( UInt x );
952extern Int VG_(getNewTemp) ( UCodeBlock* cb );
953extern Int VG_(getNewShadow) ( UCodeBlock* cb );
954
955#define SHADOW(tempreg) ((tempreg)+1)
956
957
958/* ---------------------------------------------------------------------
959 Exports of vg_translate.c
960 ------------------------------------------------------------------ */
961
962extern void VG_(translate) ( Addr orig_addr,
963 UInt* orig_size,
964 Addr* trans_addr,
965 UInt* trans_size );
966
967extern void VG_(emptyUInstr) ( UInstr* u );
968extern void VG_(newUInstr0) ( UCodeBlock* cb, Opcode opcode, Int sz );
969extern void VG_(newUInstr1) ( UCodeBlock* cb, Opcode opcode, Int sz,
970 Tag tag1, UInt val1 );
971extern void VG_(newUInstr2) ( UCodeBlock* cb, Opcode opcode, Int sz,
972 Tag tag1, UInt val1,
973 Tag tag2, UInt val2 );
974extern void VG_(newUInstr3) ( UCodeBlock* cb, Opcode opcode, Int sz,
975 Tag tag1, UInt val1,
976 Tag tag2, UInt val2,
977 Tag tag3, UInt val3 );
978extern void VG_(setFlagRW) ( UInstr* u,
979 FlagSet fr, FlagSet fw );
980
981extern void VG_(setLiteralField) ( UCodeBlock* cb, UInt lit32 );
982extern Bool VG_(anyFlagUse) ( UInstr* u );
983
984
985
986extern void VG_(ppUInstr) ( Int instrNo, UInstr* u );
987extern void VG_(ppUCodeBlock) ( UCodeBlock* cb, Char* title );
988
989extern Char* VG_(nameCondcode) ( Condcode cond );
990extern Bool VG_(saneUInstr) ( Bool beforeRA, UInstr* u );
991extern Bool VG_(saneUCodeBlock) ( UCodeBlock* cb );
992extern Char* VG_(nameUOpcode) ( Bool upper, Opcode opc );
993extern Int VG_(rankToRealRegNo) ( Int rank );
994
995extern void* VG_(jitmalloc) ( Int nbytes );
996extern void VG_(jitfree) ( void* ptr );
997
998
999/* ---------------------------------------------------------------------
1000 Exports of vg_execontext.c.
1001 ------------------------------------------------------------------ */
1002
1003/* Records the PC and a bit of the call chain. The first 4 %eip
1004 values are used in comparisons do remove duplicate errors, and for
1005 comparing against suppression specifications. The rest are purely
1006 informational (but often important). */
1007
1008typedef
1009 struct _ExeContextRec {
1010 struct _ExeContextRec * next;
1011 /* The size of this array is VG_(clo_backtrace_size); at least
1012 2, at most VG_DEEPEST_BACKTRACE. [0] is the current %eip,
1013 [1] is its caller, [2] is the caller of [1], etc. */
1014 Addr eips[0];
1015 }
1016 ExeContext;
1017
1018
1019/* Initialise the ExeContext storage mechanism. */
1020extern void VG_(init_ExeContext_storage) ( void );
1021
1022/* Print stats (informational only). */
1023extern void VG_(show_ExeContext_stats) ( void );
1024
1025
1026/* Take a snapshot of the client's stack. Search our collection of
1027 ExeContexts to see if we already have it, and if not, allocate a
1028 new one. Either way, return a pointer to the context. */
sewardj8c824512002-04-14 04:16:48 +00001029extern ExeContext* VG_(get_ExeContext) ( Bool skip_top_frame,
1030 Addr eip, Addr ebp );
sewardjde4a1d02002-03-22 01:27:54 +00001031
1032/* Print an ExeContext. */
1033extern void VG_(pp_ExeContext) ( ExeContext* );
1034
1035/* Compare two ExeContexts, just comparing the top two callers. */
1036extern Bool VG_(eq_ExeContext_top2) ( ExeContext* e1, ExeContext* e2 );
1037
1038/* Compare two ExeContexts, just comparing the top four callers. */
1039extern Bool VG_(eq_ExeContext_top4) ( ExeContext* e1, ExeContext* e2 );
1040
1041/* Compare two ExeContexts, comparing all callers. */
1042extern Bool VG_(eq_ExeContext_all) ( ExeContext* e1, ExeContext* e2 );
1043
1044
1045
1046/* ---------------------------------------------------------------------
1047 Exports of vg_errcontext.c.
1048 ------------------------------------------------------------------ */
1049
1050extern void VG_(load_suppressions) ( void );
1051extern void VG_(show_all_errors) ( void );
1052extern void VG_(record_value_error) ( Int size );
1053extern void VG_(record_free_error) ( Addr a );
1054extern void VG_(record_freemismatch_error) ( Addr a );
1055extern void VG_(record_address_error) ( Addr a, Int size,
1056 Bool isWrite );
1057extern void VG_(record_jump_error) ( Addr a );
sewardj8c824512002-04-14 04:16:48 +00001058
1059extern void VG_(record_param_err) ( ThreadState* tst,
1060 Addr a,
sewardjde4a1d02002-03-22 01:27:54 +00001061 Bool isWriteLack,
1062 Char* msg );
sewardj8c824512002-04-14 04:16:48 +00001063extern void VG_(record_user_err) ( ThreadState* tst,
1064 Addr a, Bool isWriteLack );
sewardjde4a1d02002-03-22 01:27:54 +00001065
1066
1067/* The classification of a faulting address. */
1068typedef
1069 enum { Stack, Unknown, Freed, Mallocd, UserG, UserS }
1070 AddrKind;
1071
1072/* Records info about a faulting address. */
1073typedef
1074 struct {
1075 /* ALL */
1076 AddrKind akind;
1077 /* Freed, Mallocd */
1078 Int blksize;
1079 /* Freed, Mallocd */
1080 Int rwoffset;
1081 /* Freed, Mallocd */
1082 ExeContext* lastchange;
1083 }
1084 AddrInfo;
1085
1086
1087/* ---------------------------------------------------------------------
1088 Exports of vg_clientperms.c
1089 ------------------------------------------------------------------ */
1090
1091extern Bool VG_(client_perm_maybe_describe)( Addr a, AddrInfo* ai );
1092
sewardj8c824512002-04-14 04:16:48 +00001093extern UInt VG_(handle_client_request) ( ThreadState* tst, UInt* arg_block );
sewardjde4a1d02002-03-22 01:27:54 +00001094
1095extern void VG_(delete_client_stack_blocks_following_ESP_change) ( void );
1096
1097extern void VG_(show_client_block_stats) ( void );
1098
1099
1100/* ---------------------------------------------------------------------
1101 Exports of vg_procselfmaps.c
1102 ------------------------------------------------------------------ */
1103
1104extern
1105void VG_(read_procselfmaps) (
1106 void (*record_mapping)( Addr, UInt, Char, Char, Char, UInt, UChar* )
1107);
1108
1109
1110/* ---------------------------------------------------------------------
1111 Exports of vg_symtab2.c
1112 ------------------------------------------------------------------ */
1113
1114/* We assume the executable is loaded here ... can't really find
1115 out. There is a hacky sanity check in vg_init_memory_audit()
1116 which should trip up most stupidities.
1117*/
1118#define VG_ASSUMED_EXE_BASE (Addr)0x8048000
1119
1120extern void VG_(read_symbols) ( void );
1121extern void VG_(mini_stack_dump) ( ExeContext* ec );
1122extern void VG_(what_obj_and_fun_is_this)
1123 ( Addr a,
1124 Char* obj_buf, Int n_obj_buf,
1125 Char* fun_buf, Int n_fun_buf );
1126
1127extern void VG_(symtab_notify_munmap) ( Addr start, UInt length );
1128
1129
1130/* ---------------------------------------------------------------------
1131 Exports of vg_clientmalloc.c
1132 ------------------------------------------------------------------ */
1133
sewardjde4a1d02002-03-22 01:27:54 +00001134typedef
1135 enum {
1136 Vg_AllocMalloc = 0,
sewardj2e93c502002-04-12 11:12:52 +00001137 Vg_AllocNew = 1,
sewardjde4a1d02002-03-22 01:27:54 +00001138 Vg_AllocNewVec = 2
1139 }
1140 VgAllocKind;
1141
1142/* Description of a malloc'd chunk. */
1143typedef
1144 struct _ShadowChunk {
1145 struct _ShadowChunk* next;
1146 ExeContext* where; /* where malloc'd/free'd */
1147 UInt size : 30; /* size requested. */
1148 VgAllocKind allockind : 2; /* which wrapper did the allocation */
1149 Addr data; /* ptr to actual block. */
1150 }
1151 ShadowChunk;
1152
1153extern void VG_(clientmalloc_done) ( void );
1154extern void VG_(describe_addr) ( Addr a, AddrInfo* ai );
1155extern ShadowChunk** VG_(get_malloc_shadows) ( /*OUT*/ UInt* n_shadows );
1156
sewardj2e93c502002-04-12 11:12:52 +00001157/* These are called from the scheduler, when it intercepts a user
1158 request. */
sewardj8c824512002-04-14 04:16:48 +00001159extern void* VG_(client_malloc) ( ThreadState* tst,
1160 UInt size, VgAllocKind kind );
1161extern void* VG_(client_memalign) ( ThreadState* tst,
1162 UInt align, UInt size );
1163extern void VG_(client_free) ( ThreadState* tst,
1164 void* ptrV, VgAllocKind kind );
1165extern void* VG_(client_calloc) ( ThreadState* tst,
1166 UInt nmemb, UInt size1 );
1167extern void* VG_(client_realloc) ( ThreadState* tst,
1168 void* ptrV, UInt size_new );
sewardjde4a1d02002-03-22 01:27:54 +00001169
1170
1171/* ---------------------------------------------------------------------
1172 Exports of vg_main.c
1173 ------------------------------------------------------------------ */
1174
sewardjde4a1d02002-03-22 01:27:54 +00001175/* A structure used as an intermediary when passing the simulated
1176 CPU's state to some assembly fragments, particularly system calls.
1177 Stuff is copied from baseBlock to here, the assembly magic runs,
1178 and then the inverse copy is done. */
1179
1180extern UInt VG_(m_state_static) [8 /* int regs, in Intel order */
1181 + 1 /* %eflags */
1182 + 1 /* %eip */
1183 + VG_SIZE_OF_FPUSTATE_W /* FPU state */
1184 ];
1185
1186/* Handy fns for doing the copy back and forth. */
1187extern void VG_(copy_baseBlock_to_m_state_static) ( void );
1188extern void VG_(copy_m_state_static_to_baseBlock) ( void );
1189
sewardjde4a1d02002-03-22 01:27:54 +00001190/* Called when some unhandleable client behaviour is detected.
1191 Prints a msg and aborts. */
1192extern void VG_(unimplemented) ( Char* msg );
1193
1194/* The stack on which Valgrind runs. We can't use the same stack as the
1195 simulatee -- that's an important design decision. */
1196extern UInt VG_(stack)[10000];
1197
1198/* Similarly, we have to ask for signals to be delivered on an
1199 alternative stack, since it is possible, although unlikely, that
1200 we'll have to run client code from inside the Valgrind-installed
1201 signal handler. If this happens it will be done by
1202 vg_deliver_signal_immediately(). */
1203extern UInt VG_(sigstack)[10000];
1204
sewardjde4a1d02002-03-22 01:27:54 +00001205/* Holds client's %esp at the point we gained control. From this the
1206 client's argc, argv and envp are deduced. */
1207extern Addr VG_(esp_at_startup);
1208extern Int VG_(client_argc);
1209extern Char** VG_(client_argv);
1210extern Char** VG_(client_envp);
1211
1212/* Remove valgrind.so from a LD_PRELOAD=... string so child processes
1213 don't get traced into. */
1214extern void VG_(mash_LD_PRELOAD_string)( Char* ld_preload_str );
1215
1216/* Something of a function looking for a home ... start up GDB. This
1217 is called from VG_(swizzle_esp_then_start_GDB) and so runs on the
1218 *client's* stack. This is necessary to give GDB the illusion that
1219 the client program really was running on the real cpu. */
1220extern void VG_(start_GDB_whilst_on_client_stack) ( void );
1221
1222/* Spew out vast amounts of junk during JITting? */
1223extern Bool VG_(disassemble);
1224
1225/* 64-bit counter for the number of basic blocks done. */
1226extern ULong VG_(bbs_done);
1227/* 64-bit counter for the number of bbs to go before a debug exit. */
1228extern ULong VG_(bbs_to_go);
1229
1230/* Counts downwards in vg_run_innerloop. */
1231extern UInt VG_(dispatch_ctr);
1232
sewardjde4a1d02002-03-22 01:27:54 +00001233/* Is the client running on the simulated CPU or the real one? */
1234extern Bool VG_(running_on_simd_CPU); /* Initially False */
1235
1236/* The current LRU epoch. */
1237extern UInt VG_(current_epoch);
1238
1239
1240/* --- Counters, for informational purposes only. --- */
1241
1242/* Number of lookups which miss the fast tt helper. */
1243extern UInt VG_(tt_fast_misses);
1244
1245/* Counts for LRU informational messages. */
1246
1247/* Number and total o/t size of new translations this epoch. */
1248extern UInt VG_(this_epoch_in_count);
1249extern UInt VG_(this_epoch_in_osize);
1250extern UInt VG_(this_epoch_in_tsize);
1251/* Number and total o/t size of discarded translations this epoch. */
1252extern UInt VG_(this_epoch_out_count);
1253extern UInt VG_(this_epoch_out_osize);
1254extern UInt VG_(this_epoch_out_tsize);
1255/* Number and total o/t size of translations overall. */
1256extern UInt VG_(overall_in_count);
1257extern UInt VG_(overall_in_osize);
1258extern UInt VG_(overall_in_tsize);
1259/* Number and total o/t size of discards overall. */
1260extern UInt VG_(overall_out_count);
1261extern UInt VG_(overall_out_osize);
1262extern UInt VG_(overall_out_tsize);
1263
1264/* The number of LRU-clearings of TT/TC. */
1265extern UInt VG_(number_of_lrus);
1266
1267/* Counts pertaining to the register allocator. */
1268
1269/* total number of uinstrs input to reg-alloc */
1270extern UInt VG_(uinstrs_prealloc);
1271
1272/* total number of uinstrs added due to spill code */
1273extern UInt VG_(uinstrs_spill);
1274
1275/* number of bbs requiring spill code */
1276extern UInt VG_(translations_needing_spill);
1277
1278/* total of register ranks over all translations */
1279extern UInt VG_(total_reg_rank);
1280
1281/* Counts pertaining to the self-modifying-code detection machinery. */
1282
1283/* Total number of writes checked. */
1284//extern UInt VG_(smc_total_check4s);
1285
1286/* Number of writes which the fast smc check couldn't show were
1287 harmless. */
1288extern UInt VG_(smc_cache_passed);
1289
1290/* Numnber of writes which really did write on original code. */
1291extern UInt VG_(smc_fancy_passed);
1292
1293/* Number of translations discarded as a result. */
1294//extern UInt VG_(smc_discard_count);
1295
1296/* Counts pertaining to internal sanity checking. */
1297extern UInt VG_(sanity_fast_count);
1298extern UInt VG_(sanity_slow_count);
1299
sewardj2e93c502002-04-12 11:12:52 +00001300/* Counts pertaining to the scheduler. */
1301extern UInt VG_(num_scheduling_events_MINOR);
1302extern UInt VG_(num_scheduling_events_MAJOR);
1303
sewardjde4a1d02002-03-22 01:27:54 +00001304
1305/* ---------------------------------------------------------------------
1306 Exports of vg_memory.c
1307 ------------------------------------------------------------------ */
1308
1309extern void VGM_(init_memory_audit) ( void );
1310extern Addr VGM_(curr_dataseg_end);
1311extern void VG_(show_reg_tags) ( void );
1312extern void VG_(detect_memory_leaks) ( void );
1313extern void VG_(done_prof_mem) ( void );
1314
1315/* Set permissions for an address range. Not speed-critical. */
1316extern void VGM_(make_noaccess) ( Addr a, UInt len );
1317extern void VGM_(make_writable) ( Addr a, UInt len );
1318extern void VGM_(make_readable) ( Addr a, UInt len );
1319/* Use with care! (read: use for shmat only) */
1320extern void VGM_(make_readwritable) ( Addr a, UInt len );
1321extern void VGM_(copy_address_range_perms) ( Addr src, Addr dst,
1322 UInt len );
1323
1324/* Check permissions for an address range. Not speed-critical. */
1325extern Bool VGM_(check_writable) ( Addr a, UInt len, Addr* bad_addr );
1326extern Bool VGM_(check_readable) ( Addr a, UInt len, Addr* bad_addr );
1327extern Bool VGM_(check_readable_asciiz) ( Addr a, Addr* bad_addr );
1328
1329/* Sanity checks which may be done at any time. Doing them at
1330 signal-delivery time turns out to be convenient. */
sewardj2e93c502002-04-12 11:12:52 +00001331extern void VG_(do_sanity_checks) ( ThreadId tid, Bool force_expensive );
sewardjde4a1d02002-03-22 01:27:54 +00001332/* Very cheap ... */
1333extern Bool VG_(first_and_last_secondaries_look_plausible) ( void );
1334
1335/* These functions are called from generated code. */
1336extern void VG_(helperc_STOREV4) ( UInt, Addr );
1337extern void VG_(helperc_STOREV2) ( UInt, Addr );
1338extern void VG_(helperc_STOREV1) ( UInt, Addr );
1339
1340extern UInt VG_(helperc_LOADV1) ( Addr );
1341extern UInt VG_(helperc_LOADV2) ( Addr );
1342extern UInt VG_(helperc_LOADV4) ( Addr );
1343
1344extern void VGM_(handle_esp_assignment) ( Addr new_espA );
1345extern void VGM_(fpu_write_check) ( Addr addr, Int size );
1346extern void VGM_(fpu_read_check) ( Addr addr, Int size );
1347
1348/* Safely (avoiding SIGSEGV / SIGBUS) scan the entire valid address
1349 space and pass the addresses and values of all addressible,
1350 defined, aligned words to notify_word. This is the basis for the
1351 leak detector. Returns the number of calls made to notify_word. */
1352UInt VG_(scan_all_valid_memory) ( void (*notify_word)( Addr, UInt ) );
1353
1354/* Is this address within some small distance below %ESP? Used only
1355 for the --workaround-gcc296-bugs kludge. */
sewardj8c824512002-04-14 04:16:48 +00001356extern Bool VG_(is_just_below_ESP)( Addr esp, Addr aa );
sewardjde4a1d02002-03-22 01:27:54 +00001357
1358/* Nasty kludgery to deal with applications which switch stacks,
1359 like netscape. */
1360#define VG_STACK_STARTS_AT 0xC0000000
1361#define VG_PLAUSIBLE_STACK_SIZE 8000000
1362
1363extern Bool VG_(is_plausible_stack_addr) ( Addr );
1364
1365
1366/* ---------------------------------------------------------------------
1367 Exports of vg_syscall_mem.c
1368 ------------------------------------------------------------------ */
1369
sewardj2e93c502002-04-12 11:12:52 +00001370extern void VG_(perform_assumed_nonblocking_syscall) ( ThreadId tid );
sewardjde4a1d02002-03-22 01:27:54 +00001371
sewardj2e93c502002-04-12 11:12:52 +00001372extern void VG_(check_known_blocking_syscall) ( ThreadId tid,
1373 Int syscallno,
1374 Int* /*IN*/ res );
sewardjde4a1d02002-03-22 01:27:54 +00001375
1376extern Bool VG_(is_kerror) ( Int res );
1377
sewardj2e93c502002-04-12 11:12:52 +00001378#define KERNEL_DO_SYSCALL(thread_id, result_lvalue) \
1379 VG_(load_thread_state)(thread_id); \
sewardjde4a1d02002-03-22 01:27:54 +00001380 VG_(copy_baseBlock_to_m_state_static)(); \
1381 VG_(do_syscall)(); \
1382 VG_(copy_m_state_static_to_baseBlock)(); \
sewardj2e93c502002-04-12 11:12:52 +00001383 VG_(save_thread_state)(thread_id); \
1384 result_lvalue = VG_(get_thread_state)(thread_id)->m_eax;
sewardjde4a1d02002-03-22 01:27:54 +00001385
1386
1387/* ---------------------------------------------------------------------
1388 Exports of vg_transtab.c
1389 ------------------------------------------------------------------ */
1390
1391/* An entry in the translation table (TT). */
1392typedef
1393 struct {
1394 /* +0 */ Addr orig_addr;
1395 /* +4 */ Addr trans_addr;
1396 /* +8 */ UInt mru_epoch;
1397 /* +12 */ UShort orig_size;
1398 /* +14 */ UShort trans_size;
1399 }
1400 TTEntry;
1401
1402/* The number of basic blocks in an epoch (one age-step). */
1403#define VG_BBS_PER_EPOCH 20000
1404
1405extern void VG_(get_tt_tc_used) ( UInt* tt_used, UInt* tc_used );
1406extern void VG_(maybe_do_lru_pass) ( void );
1407extern void VG_(flush_transtab) ( void );
1408extern Addr VG_(copy_to_transcache) ( Addr trans_addr, Int trans_size );
1409extern void VG_(add_to_trans_tab) ( TTEntry* tte );
1410
1411extern void VG_(smc_mark_original) ( Addr original_addr,
1412 Int original_len );
1413
1414extern void VG_(init_transtab_and_SMC) ( void );
1415
1416extern void VG_(sanity_check_tc_tt) ( void );
1417extern Addr VG_(search_transtab) ( Addr original_addr );
1418
1419extern void VG_(invalidate_tt_fast)( void );
1420
1421
1422/* ---------------------------------------------------------------------
1423 Exports of vg_vtagops.c
1424 ------------------------------------------------------------------ */
1425
1426/* Lists the names of value-tag operations used in instrumented
1427 code. These are the third argument to TAG1 and TAG2 uinsns. */
1428
1429typedef
1430 enum {
1431 /* Unary. */
1432 VgT_PCast40, VgT_PCast20, VgT_PCast10,
1433 VgT_PCast01, VgT_PCast02, VgT_PCast04,
1434
1435 VgT_PCast14, VgT_PCast12, VgT_PCast11,
1436
1437 VgT_Left4, VgT_Left2, VgT_Left1,
1438
1439 VgT_SWiden14, VgT_SWiden24, VgT_SWiden12,
1440 VgT_ZWiden14, VgT_ZWiden24, VgT_ZWiden12,
1441
1442 /* Binary; 1st is rd; 2nd is rd+wr */
1443 VgT_UifU4, VgT_UifU2, VgT_UifU1, VgT_UifU0,
1444 VgT_DifD4, VgT_DifD2, VgT_DifD1,
1445
1446 VgT_ImproveAND4_TQ, VgT_ImproveAND2_TQ, VgT_ImproveAND1_TQ,
1447 VgT_ImproveOR4_TQ, VgT_ImproveOR2_TQ, VgT_ImproveOR1_TQ,
1448 VgT_DebugFn
1449 }
1450 VgTagOp;
1451
1452extern Char* VG_(nameOfTagOp) ( VgTagOp );
1453extern UInt VG_(DebugFn) ( UInt a1, UInt a2 );
1454
1455
1456/* ---------------------------------------------------------------------
1457 Exports of vg_syscall.S
1458 ------------------------------------------------------------------ */
1459
1460extern void VG_(do_syscall) ( void );
1461
1462
1463/* ---------------------------------------------------------------------
1464 Exports of vg_startup.S
1465 ------------------------------------------------------------------ */
1466
1467extern void VG_(shutdown);
1468extern void VG_(switch_to_real_CPU) ( void );
1469
1470extern void VG_(swizzle_esp_then_start_GDB) ( void );
1471
1472
1473/* ---------------------------------------------------------------------
1474 Exports of vg_dispatch.S
1475 ------------------------------------------------------------------ */
1476
sewardj2e93c502002-04-12 11:12:52 +00001477/* Run a thread for a (very short) while, until some event happens
1478 which means we need to defer to the scheduler. */
1479extern UInt VG_(run_innerloop) ( void );
sewardjde4a1d02002-03-22 01:27:54 +00001480
1481
1482/* ---------------------------------------------------------------------
1483 Exports of vg_helpers.S
1484 ------------------------------------------------------------------ */
1485
sewardjde4a1d02002-03-22 01:27:54 +00001486/* SMC fast checks. */
1487extern void VG_(helper_smc_check4);
1488
1489/* Mul, div, etc, -- we don't codegen these directly. */
1490extern void VG_(helper_idiv_64_32);
1491extern void VG_(helper_div_64_32);
1492extern void VG_(helper_idiv_32_16);
1493extern void VG_(helper_div_32_16);
1494extern void VG_(helper_idiv_16_8);
1495extern void VG_(helper_div_16_8);
1496
1497extern void VG_(helper_imul_32_64);
1498extern void VG_(helper_mul_32_64);
1499extern void VG_(helper_imul_16_32);
1500extern void VG_(helper_mul_16_32);
1501extern void VG_(helper_imul_8_16);
1502extern void VG_(helper_mul_8_16);
1503
1504extern void VG_(helper_CLD);
1505extern void VG_(helper_STD);
1506extern void VG_(helper_get_dirflag);
1507
1508extern void VG_(helper_shldl);
1509extern void VG_(helper_shldw);
1510extern void VG_(helper_shrdl);
1511extern void VG_(helper_shrdw);
1512
1513extern void VG_(helper_RDTSC);
1514extern void VG_(helper_CPUID);
1515
sewardjde4a1d02002-03-22 01:27:54 +00001516extern void VG_(helper_bsf);
1517extern void VG_(helper_bsr);
1518
1519extern void VG_(helper_fstsw_AX);
1520extern void VG_(helper_SAHF);
sewardj4d0ab1f2002-03-24 10:00:09 +00001521extern void VG_(helper_DAS);
sewardjfe8a1662002-03-24 11:54:07 +00001522extern void VG_(helper_DAA);
sewardjde4a1d02002-03-22 01:27:54 +00001523
1524extern void VG_(helper_value_check4_fail);
1525extern void VG_(helper_value_check2_fail);
1526extern void VG_(helper_value_check1_fail);
1527extern void VG_(helper_value_check0_fail);
1528
sewardjbc5b99f2002-04-13 00:08:51 +00001529/* NOT FUNCTIONS; these are bogus RETURN ADDRESS. */
sewardj54cacf02002-04-12 23:24:59 +00001530extern void VG_(signalreturn_bogusRA)( void );
sewardjbc5b99f2002-04-13 00:08:51 +00001531extern void VG_(pthreadreturn_bogusRA)( void );
sewardj54cacf02002-04-12 23:24:59 +00001532
sewardjde4a1d02002-03-22 01:27:54 +00001533
1534/* ---------------------------------------------------------------------
1535 The state of the simulated CPU.
1536 ------------------------------------------------------------------ */
1537
1538/* This is the Intel register encoding. */
1539#define R_EAX 0
1540#define R_ECX 1
1541#define R_EDX 2
1542#define R_EBX 3
1543#define R_ESP 4
1544#define R_EBP 5
1545#define R_ESI 6
1546#define R_EDI 7
1547
1548#define R_AL (0+R_EAX)
1549#define R_CL (0+R_ECX)
1550#define R_DL (0+R_EDX)
1551#define R_BL (0+R_EBX)
1552#define R_AH (4+R_EAX)
1553#define R_CH (4+R_ECX)
1554#define R_DH (4+R_EDX)
1555#define R_BH (4+R_EBX)
1556
1557
1558/* ---------------------------------------------------------------------
1559 Offsets into baseBlock for everything which needs to referred to
1560 from generated code. The order of these decls does not imply
1561 what the order of the actual offsets is. The latter is important
1562 and is set up in vg_main.c.
1563 ------------------------------------------------------------------ */
1564
1565/* An array of words. In generated code, %ebp always points to the
1566 start of this array. Useful stuff, like the simulated CPU state,
1567 and the addresses of helper functions, can then be found by
1568 indexing off %ebp. The following declares variables which, at
1569 startup time, are given values denoting offsets into baseBlock.
1570 These offsets are in *words* from the start of baseBlock. */
1571
1572#define VG_BASEBLOCK_WORDS 200
1573
1574extern UInt VG_(baseBlock)[VG_BASEBLOCK_WORDS];
1575
1576
1577/* -----------------------------------------------------
1578 Read-write parts of baseBlock.
1579 -------------------------------------------------- */
1580
1581/* State of the simulated CPU. */
1582extern Int VGOFF_(m_eax);
1583extern Int VGOFF_(m_ecx);
1584extern Int VGOFF_(m_edx);
1585extern Int VGOFF_(m_ebx);
1586extern Int VGOFF_(m_esp);
1587extern Int VGOFF_(m_ebp);
1588extern Int VGOFF_(m_esi);
1589extern Int VGOFF_(m_edi);
1590extern Int VGOFF_(m_eflags);
1591extern Int VGOFF_(m_fpustate);
1592extern Int VGOFF_(m_eip);
1593
1594/* Reg-alloc spill area (VG_MAX_SPILLSLOTS words long). */
1595extern Int VGOFF_(spillslots);
1596
1597/* Records the valid bits for the 8 integer regs & flags reg. */
1598extern Int VGOFF_(sh_eax);
1599extern Int VGOFF_(sh_ecx);
1600extern Int VGOFF_(sh_edx);
1601extern Int VGOFF_(sh_ebx);
1602extern Int VGOFF_(sh_esp);
1603extern Int VGOFF_(sh_ebp);
1604extern Int VGOFF_(sh_esi);
1605extern Int VGOFF_(sh_edi);
1606extern Int VGOFF_(sh_eflags);
1607
1608
1609/* -----------------------------------------------------
1610 Read-only parts of baseBlock.
1611 -------------------------------------------------- */
1612
1613/* Offsets of addresses of helper functions. A "helper" function is
1614 one which is called from generated code. */
1615
1616extern Int VGOFF_(helper_idiv_64_32);
1617extern Int VGOFF_(helper_div_64_32);
1618extern Int VGOFF_(helper_idiv_32_16);
1619extern Int VGOFF_(helper_div_32_16);
1620extern Int VGOFF_(helper_idiv_16_8);
1621extern Int VGOFF_(helper_div_16_8);
1622
1623extern Int VGOFF_(helper_imul_32_64);
1624extern Int VGOFF_(helper_mul_32_64);
1625extern Int VGOFF_(helper_imul_16_32);
1626extern Int VGOFF_(helper_mul_16_32);
1627extern Int VGOFF_(helper_imul_8_16);
1628extern Int VGOFF_(helper_mul_8_16);
1629
1630extern Int VGOFF_(helper_CLD);
1631extern Int VGOFF_(helper_STD);
1632extern Int VGOFF_(helper_get_dirflag);
1633
1634extern Int VGOFF_(helper_shldl);
1635extern Int VGOFF_(helper_shldw);
1636extern Int VGOFF_(helper_shrdl);
1637extern Int VGOFF_(helper_shrdw);
1638
1639extern Int VGOFF_(helper_RDTSC);
1640extern Int VGOFF_(helper_CPUID);
1641
sewardjde4a1d02002-03-22 01:27:54 +00001642extern Int VGOFF_(helper_bsf);
1643extern Int VGOFF_(helper_bsr);
1644
1645extern Int VGOFF_(helper_fstsw_AX);
1646extern Int VGOFF_(helper_SAHF);
sewardj4d0ab1f2002-03-24 10:00:09 +00001647extern Int VGOFF_(helper_DAS);
sewardjfe8a1662002-03-24 11:54:07 +00001648extern Int VGOFF_(helper_DAA);
sewardjde4a1d02002-03-22 01:27:54 +00001649
1650extern Int VGOFF_(helper_value_check4_fail);
1651extern Int VGOFF_(helper_value_check2_fail);
1652extern Int VGOFF_(helper_value_check1_fail);
1653extern Int VGOFF_(helper_value_check0_fail);
1654
sewardjde4a1d02002-03-22 01:27:54 +00001655extern Int VGOFF_(helperc_STOREV4); /* :: UInt -> Addr -> void */
1656extern Int VGOFF_(helperc_STOREV2); /* :: UInt -> Addr -> void */
1657extern Int VGOFF_(helperc_STOREV1); /* :: UInt -> Addr -> void */
1658
1659extern Int VGOFF_(helperc_LOADV4); /* :: Addr -> UInt -> void */
1660extern Int VGOFF_(helperc_LOADV2); /* :: Addr -> UInt -> void */
1661extern Int VGOFF_(helperc_LOADV1); /* :: Addr -> UInt -> void */
1662
1663extern Int VGOFF_(handle_esp_assignment); /* :: Addr -> void */
1664extern Int VGOFF_(fpu_write_check); /* :: Addr -> Int -> void */
1665extern Int VGOFF_(fpu_read_check); /* :: Addr -> Int -> void */
1666
sewardjde4a1d02002-03-22 01:27:54 +00001667
1668
1669#endif /* ndef __VG_INCLUDE_H */
1670
sewardj3b2736a2002-03-24 12:18:35 +00001671
1672/* ---------------------------------------------------------------------
1673 Finally - autoconf-generated settings
1674 ------------------------------------------------------------------ */
1675
1676#include "config.h"
1677
sewardjde4a1d02002-03-22 01:27:54 +00001678/*--------------------------------------------------------------------*/
1679/*--- end vg_include.h ---*/
1680/*--------------------------------------------------------------------*/