blob: 8f99dcf414c441eddc0d8f3eb711c71dab664965 [file] [log] [blame]
njn25e49d8e72002-09-23 09:36:25 +00001
2/*--------------------------------------------------------------------*/
3/*--- The only header your skin will ever need to #include... ---*/
4/*--- vg_skin.h ---*/
5/*--------------------------------------------------------------------*/
6
7/*
8 This file is part of Valgrind, an x86 protected-mode emulator
9 designed for debugging and profiling binaries on x86-Unixes.
10
11 Copyright (C) 2000-2002 Julian Seward
12 jseward@acm.org
13
14 This program is free software; you can redistribute it and/or
15 modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation; either version 2 of the
17 License, or (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful, but
20 WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
27 02111-1307, USA.
28
29 The GNU General Public License is contained in the file COPYING.
30*/
31
32#ifndef __VG_SKIN_H
33#define __VG_SKIN_H
34
35#include <stdarg.h> /* ANSI varargs stuff */
36#include <setjmp.h> /* for jmp_buf */
37
38#include "vg_constants_skin.h"
39
40
41/*====================================================================*/
42/*=== Build options and table sizes. ===*/
43/*====================================================================*/
44
45/* You should be able to change these options or sizes, recompile, and
46 still have a working system. */
47
48/* The maximum number of pthreads that we support. This is
49 deliberately not very high since our implementation of some of the
50 scheduler algorithms is surely O(N) in the number of threads, since
51 that's simple, at least. And (in practice) we hope that most
52 programs do not need many threads. */
53#define VG_N_THREADS 50
54
55/* Maximum number of pthread keys available. Again, we start low until
56 the need for a higher number presents itself. */
57#define VG_N_THREAD_KEYS 50
58
59/* Total number of integer registers available for allocation -- all of
60 them except %esp, %ebp. %ebp permanently points at VG_(baseBlock).
61
62 If you change this you'll have to also change at least these:
njn4ba5a792002-09-30 10:23:54 +000063 - VG_(rank_to_realreg)()
64 - VG_(realreg_to_rank)()
njn25e49d8e72002-09-23 09:36:25 +000065 - ppRegsLiveness()
66 - the RegsLive type (maybe -- RegsLive type must have more than
67 VG_MAX_REALREGS bits)
68
69 Do not change this unless you really know what you are doing! */
70#define VG_MAX_REALREGS 6
71
72
73/*====================================================================*/
njn1a1dd8b2002-09-27 10:42:20 +000074/*=== Basic types, useful macros ===*/
njn25e49d8e72002-09-23 09:36:25 +000075/*====================================================================*/
76
njn25e49d8e72002-09-23 09:36:25 +000077typedef unsigned char UChar;
78typedef unsigned short UShort;
79typedef unsigned int UInt;
80typedef unsigned long long int ULong;
81
82typedef signed char Char;
83typedef signed short Short;
84typedef signed int Int;
85typedef signed long long int Long;
86
87typedef unsigned int Addr;
88
89typedef unsigned char Bool;
90#define False ((Bool)0)
91#define True ((Bool)1)
92
93
njn1a1dd8b2002-09-27 10:42:20 +000094#define mycat_wrk(aaa,bbb) aaa##bbb
95#define mycat(aaa,bbb) mycat_wrk(aaa,bbb)
96
97/* No, really. I _am_ that strange. */
98#define OINK(nnn) VG_(message)(Vg_DebugMsg, "OINK %d",nnn)
99
njn25e49d8e72002-09-23 09:36:25 +0000100/* ---------------------------------------------------------------------
101 Now the basic types are set up, we can haul in the kernel-interface
102 definitions.
103 ------------------------------------------------------------------ */
104
njn2574bb4762002-09-24 11:23:50 +0000105#include "../coregrind/vg_kerneliface.h"
njn25e49d8e72002-09-23 09:36:25 +0000106
107
108/*====================================================================*/
109/*=== Command-line options ===*/
110/*====================================================================*/
111
112/* Verbosity level: 0 = silent, 1 (default), > 1 = more verbose. */
113extern Int VG_(clo_verbosity);
114
115/* Profile? */
116extern Bool VG_(clo_profile);
117
njn25e49d8e72002-09-23 09:36:25 +0000118/* Call this if a recognised option was bad for some reason.
119 Note: don't use it just because an option was unrecognised -- return 'False'
120 from SKN_(process_cmd_line_option) to indicate that. */
121extern void VG_(bad_option) ( Char* opt );
122
123/* Client args */
124extern Int VG_(client_argc);
125extern Char** VG_(client_argv);
126
njnd5bb0a52002-09-27 10:24:48 +0000127/* Client environment. Can be inspected with VG_(getenv)() */
njn25e49d8e72002-09-23 09:36:25 +0000128extern Char** VG_(client_envp);
129
130
131/*====================================================================*/
132/*=== Printing messages for the user ===*/
133/*====================================================================*/
134
135/* Print a message prefixed by "??<pid>?? "; '?' depends on the VgMsgKind.
136 Should be used for all user output. */
137
138typedef
139 enum { Vg_UserMsg, /* '?' == '=' */
140 Vg_DebugMsg, /* '?' == '-' */
141 Vg_DebugExtraMsg /* '?' == '+' */
142 }
143 VgMsgKind;
144
145/* Functions for building a message from multiple parts. */
146extern void VG_(start_msg) ( VgMsgKind kind );
147extern void VG_(add_to_msg) ( Char* format, ... );
148/* Ends and prints the message. Appends a newline. */
149extern void VG_(end_msg) ( void );
150
njnd5bb0a52002-09-27 10:24:48 +0000151/* Send a single-part message. Appends a newline. */
njn25e49d8e72002-09-23 09:36:25 +0000152extern void VG_(message) ( VgMsgKind kind, Char* format, ... );
153
154
155/*====================================================================*/
156/*=== Profiling ===*/
157/*====================================================================*/
158
159/* Nb: VGP_(register_profile_event)() relies on VgpUnc being the first one */
160#define VGP_CORE_LIST \
161 /* These ones depend on the core */ \
162 VGP_PAIR(VgpUnc, "unclassified"), \
163 VGP_PAIR(VgpRun, "running"), \
164 VGP_PAIR(VgpSched, "scheduler"), \
165 VGP_PAIR(VgpMalloc, "low-lev malloc/free"), \
166 VGP_PAIR(VgpCliMalloc, "client malloc/free"), \
167 VGP_PAIR(VgpStack, "adjust-stack"), \
168 VGP_PAIR(VgpTranslate, "translate-main"), \
169 VGP_PAIR(VgpToUCode, "to-ucode"), \
170 VGP_PAIR(VgpFromUcode, "from-ucode"), \
171 VGP_PAIR(VgpImprove, "improve"), \
172 VGP_PAIR(VgpRegAlloc, "reg-alloc"), \
173 VGP_PAIR(VgpLiveness, "liveness-analysis"), \
174 VGP_PAIR(VgpDoLRU, "do-lru"), \
175 VGP_PAIR(VgpSlowFindT, "slow-search-transtab"), \
176 VGP_PAIR(VgpInitMem, "init-memory"), \
177 VGP_PAIR(VgpExeContext, "exe-context"), \
178 VGP_PAIR(VgpReadSyms, "read-syms"), \
179 VGP_PAIR(VgpSearchSyms, "search-syms"), \
180 VGP_PAIR(VgpAddToT, "add-to-transtab"), \
181 VGP_PAIR(VgpCoreSysWrap, "core-syscall-wrapper"), \
182 VGP_PAIR(VgpDemangle, "demangle"), \
183 /* These ones depend on the skin */ \
184 VGP_PAIR(VgpPreCloInit, "pre-clo-init"), \
185 VGP_PAIR(VgpPostCloInit, "post-clo-init"), \
186 VGP_PAIR(VgpInstrument, "instrument"), \
187 VGP_PAIR(VgpSkinSysWrap, "skin-syscall-wrapper"), \
188 VGP_PAIR(VgpFini, "fini")
189
190#define VGP_PAIR(n,name) n
191typedef enum { VGP_CORE_LIST } VgpCoreCC;
192#undef VGP_PAIR
193
194/* When registering skin profiling events, ensure that the 'n' value is in
195 * the range (VgpFini+1..) */
196extern void VGP_(register_profile_event) ( Int n, Char* name );
197
198extern void VGP_(pushcc) ( UInt cc );
199extern void VGP_(popcc) ( UInt cc );
200
201/* Define them only if they haven't already been defined by vg_profile.c */
202#ifndef VGP_PUSHCC
203# define VGP_PUSHCC(x)
204#endif
205#ifndef VGP_POPCC
206# define VGP_POPCC(x)
207#endif
208
209
210/*====================================================================*/
211/*=== Useful stuff to call from generated code ===*/
212/*====================================================================*/
213
214/* ------------------------------------------------------------------ */
215/* General stuff */
216
217/* Get the simulated %esp */
218extern Addr VG_(get_stack_pointer) ( void );
219
njnd5bb0a52002-09-27 10:24:48 +0000220/* Detect if an address is within Valgrind's stack or Valgrind's
221 m_state_static; useful for memory leak detectors to tell if a block
222 is used by Valgrind (and thus can be ignored). */
njn25e49d8e72002-09-23 09:36:25 +0000223extern Bool VG_(within_stack)(Addr a);
njn25e49d8e72002-09-23 09:36:25 +0000224extern Bool VG_(within_m_state_static)(Addr a);
225
226/* Check if an address is 4-byte aligned */
227#define IS_ALIGNED4_ADDR(aaa_p) (0 == (((UInt)(aaa_p)) & 3))
228
229
230/* ------------------------------------------------------------------ */
231/* Thread-related stuff */
232
233/* Special magic value for an invalid ThreadId. It corresponds to
234 LinuxThreads using zero as the initial value for
235 pthread_mutex_t.__m_owner and pthread_cond_t.__c_waiting. */
236#define VG_INVALID_THREADID ((ThreadId)(0))
237
238/* ThreadIds are simply indices into the vg_threads[] array. */
239typedef
240 UInt
241 ThreadId;
242
njnd5bb0a52002-09-27 10:24:48 +0000243/* struct _ThreadState defined elsewhere; ThreadState is abstract as its
244 definition is not important for skins. */
njn25e49d8e72002-09-23 09:36:25 +0000245typedef
246 struct _ThreadState
247 ThreadState;
248
249extern ThreadId VG_(get_current_tid_1_if_root) ( void );
250extern ThreadState* VG_(get_ThreadState) ( ThreadId tid );
251
252
253/*====================================================================*/
254/*=== Valgrind's version of libc ===*/
255/*====================================================================*/
256
257/* Valgrind doesn't use libc at all, for good reasons (trust us). So here
258 are its own versions of C library functions, but with VG_ prefixes. Note
259 that the types of some are slightly different to the real ones. Some
260 extra useful functions are provided too; descriptions of how they work
261 are given below. */
262
263#if !defined(NULL)
264# define NULL ((void*)0)
265#endif
266
267
268/* ------------------------------------------------------------------ */
269/* stdio.h
270 *
271 * Note that they all output to the file descriptor given by the
272 * --logfile-fd=N argument, which defaults to 2 (stderr). Hence no
273 * need for VG_(fprintf)().
njn25e49d8e72002-09-23 09:36:25 +0000274 */
275extern void VG_(printf) ( const char *format, ... );
276/* too noisy ... __attribute__ ((format (printf, 1, 2))) ; */
277extern void VG_(sprintf) ( Char* buf, Char *format, ... );
278extern void VG_(vprintf) ( void(*send)(Char),
279 const Char *format, va_list vargs );
280
281/* ------------------------------------------------------------------ */
282/* stdlib.h */
283
284extern void* VG_(malloc) ( Int nbytes );
njnd5bb0a52002-09-27 10:24:48 +0000285extern void VG_(free) ( void* p );
286extern void* VG_(calloc) ( Int n, Int nbytes );
287extern void* VG_(realloc) ( void* p, Int size );
288extern void* VG_(malloc_aligned) ( Int align_bytes, Int nbytes );
njn25e49d8e72002-09-23 09:36:25 +0000289
290extern void VG_(print_malloc_stats) ( void );
291
292
293extern void VG_(exit)( Int status )
294 __attribute__ ((__noreturn__));
njnd5bb0a52002-09-27 10:24:48 +0000295/* Prints a panic message (a constant string), appends newline, aborts. */
njn25e49d8e72002-09-23 09:36:25 +0000296extern void VG_(panic) ( Char* str )
297 __attribute__ ((__noreturn__));
298
njnd5bb0a52002-09-27 10:24:48 +0000299/* Looks up VG_(client_envp) */
njn25e49d8e72002-09-23 09:36:25 +0000300extern Char* VG_(getenv) ( Char* name );
301
302/* Crude stand-in for the glibc system() call. */
303extern Int VG_(system) ( Char* cmd );
304
njnd5bb0a52002-09-27 10:24:48 +0000305extern Long VG_(atoll) ( Char* str );
njn25e49d8e72002-09-23 09:36:25 +0000306
307/* Like atoll(), but converts a number of base 2..36 */
308extern Long VG_(atoll36) ( UInt base, Char* str );
309
310
311/* ------------------------------------------------------------------ */
njnd5bb0a52002-09-27 10:24:48 +0000312/* ctype.h */
njn25e49d8e72002-09-23 09:36:25 +0000313extern Bool VG_(isspace) ( Char c );
314extern Bool VG_(isdigit) ( Char c );
315extern Char VG_(toupper) ( Char c );
316
317
318/* ------------------------------------------------------------------ */
319/* string.h */
320extern Int VG_(strlen) ( const Char* str );
321extern Char* VG_(strcat) ( Char* dest, const Char* src );
322extern Char* VG_(strncat) ( Char* dest, const Char* src, Int n );
323extern Char* VG_(strpbrk) ( const Char* s, const Char* accept );
324extern Char* VG_(strcpy) ( Char* dest, const Char* src );
325extern Char* VG_(strncpy) ( Char* dest, const Char* src, Int ndest );
326extern Int VG_(strcmp) ( const Char* s1, const Char* s2 );
327extern Int VG_(strncmp) ( const Char* s1, const Char* s2, Int nmax );
328extern Char* VG_(strstr) ( const Char* haystack, Char* needle );
329extern Char* VG_(strchr) ( const Char* s, Char c );
330extern Char* VG_(strdup) ( const Char* s);
331
njnd5bb0a52002-09-27 10:24:48 +0000332/* Like strcmp() and strncmp(), but stop comparing at any whitespace. */
njn25e49d8e72002-09-23 09:36:25 +0000333extern Int VG_(strcmp_ws) ( const Char* s1, const Char* s2 );
njn25e49d8e72002-09-23 09:36:25 +0000334extern Int VG_(strncmp_ws) ( const Char* s1, const Char* s2, Int nmax );
335
njnd5bb0a52002-09-27 10:24:48 +0000336/* Like strncpy(), but if 'src' is longer than 'ndest' inserts a '\0' as the
337 last character. */
njn25e49d8e72002-09-23 09:36:25 +0000338extern void VG_(strncpy_safely) ( Char* dest, const Char* src, Int ndest );
339
340/* Mini-regexp function. Searches for 'pat' in 'str'. Supports
341 * meta-symbols '*' and '?'. '\' escapes meta-symbols. */
njn4ba5a792002-09-30 10:23:54 +0000342extern Bool VG_(string_match) ( Char* pat, Char* str );
njn25e49d8e72002-09-23 09:36:25 +0000343
344
345/* ------------------------------------------------------------------ */
346/* math.h */
njnd5bb0a52002-09-27 10:24:48 +0000347/* Returns the base-2 logarithm of x. */
njn25e49d8e72002-09-23 09:36:25 +0000348extern Int VG_(log2) ( Int x );
349
350
351/* ------------------------------------------------------------------ */
njnd5bb0a52002-09-27 10:24:48 +0000352/* unistd.h, fcntl.h, sys/stat.h */
353extern Int VG_(getpid) ( void );
354
355extern Int VG_(open) ( const Char* pathname, Int flags, Int mode );
356extern Int VG_(read) ( Int fd, void* buf, Int count);
357extern Int VG_(write) ( Int fd, void* buf, Int count);
358extern void VG_(close) ( Int fd );
359
360extern Int VG_(stat) ( Char* file_name, struct vki_stat* buf );
njn25e49d8e72002-09-23 09:36:25 +0000361
362
363/* ------------------------------------------------------------------ */
364/* assert.h */
365/* Asserts permanently enabled -- no turning off with NDEBUG. Hurrah! */
366#define VG__STRING(__str) #__str
367
368#define vg_assert(expr) \
369 ((void) ((expr) ? 0 : \
370 (VG_(assert_fail) (VG__STRING(expr), \
371 __FILE__, __LINE__, \
372 __PRETTY_FUNCTION__), 0)))
373
374extern void VG_(assert_fail) ( Char* expr, Char* file,
375 Int line, Char* fn )
376 __attribute__ ((__noreturn__));
377
378
379/* ------------------------------------------------------------------ */
njnd5bb0a52002-09-27 10:24:48 +0000380/* system/mman.h */
njn25e49d8e72002-09-23 09:36:25 +0000381extern void* VG_(mmap)( void* start, UInt length,
382 UInt prot, UInt flags, UInt fd, UInt offset );
383extern Int VG_(munmap)( void* start, Int length );
384
385/* Get memory by anonymous mmap. */
386extern void* VG_(get_memory_from_mmap) ( Int nBytes, Char* who );
387
388
389/* ------------------------------------------------------------------ */
390/* signal.h.
391
392 Note that these use the vk_ (kernel) structure
393 definitions, which are different in places from those that glibc
394 defines -- hence the 'k' prefix. Since we're operating right at the
395 kernel interface, glibc's view of the world is entirely irrelevant. */
396
397/* --- Signal set ops --- */
njnd5bb0a52002-09-27 10:24:48 +0000398extern Int VG_(ksigfillset) ( vki_ksigset_t* set );
399extern Int VG_(ksigemptyset) ( vki_ksigset_t* set );
njn25e49d8e72002-09-23 09:36:25 +0000400
njnd5bb0a52002-09-27 10:24:48 +0000401extern Bool VG_(kisfullsigset) ( vki_ksigset_t* set );
402extern Bool VG_(kisemptysigset) ( vki_ksigset_t* set );
njn25e49d8e72002-09-23 09:36:25 +0000403
njnd5bb0a52002-09-27 10:24:48 +0000404extern Int VG_(ksigaddset) ( vki_ksigset_t* set, Int signum );
405extern Int VG_(ksigdelset) ( vki_ksigset_t* set, Int signum );
njn25e49d8e72002-09-23 09:36:25 +0000406extern Int VG_(ksigismember) ( vki_ksigset_t* set, Int signum );
407
njnd5bb0a52002-09-27 10:24:48 +0000408extern void VG_(ksigaddset_from_set) ( vki_ksigset_t* dst, vki_ksigset_t* src );
409extern void VG_(ksigdelset_from_set) ( vki_ksigset_t* dst, vki_ksigset_t* src );
njn25e49d8e72002-09-23 09:36:25 +0000410
411/* --- Mess with the kernel's sig state --- */
njnd5bb0a52002-09-27 10:24:48 +0000412extern Int VG_(ksigprocmask) ( Int how, const vki_ksigset_t* set,
njn25e49d8e72002-09-23 09:36:25 +0000413 vki_ksigset_t* oldset );
njnd5bb0a52002-09-27 10:24:48 +0000414extern Int VG_(ksigaction) ( Int signum,
415 const vki_ksigaction* act,
416 vki_ksigaction* oldact );
njn25e49d8e72002-09-23 09:36:25 +0000417
njnd5bb0a52002-09-27 10:24:48 +0000418extern Int VG_(ksignal) ( Int signum, void (*sighandler)(Int) );
419extern Int VG_(ksigaltstack) ( const vki_kstack_t* ss, vki_kstack_t* oss );
njn25e49d8e72002-09-23 09:36:25 +0000420
njnd5bb0a52002-09-27 10:24:48 +0000421extern Int VG_(kill) ( Int pid, Int signo );
422extern Int VG_(sigpending) ( vki_ksigset_t* set );
njn25e49d8e72002-09-23 09:36:25 +0000423
424
425/*====================================================================*/
426/*=== UCode definition ===*/
427/*====================================================================*/
428
429/* Tags which describe what operands are. */
430typedef
431 enum { TempReg=0, ArchReg=1, RealReg=2,
432 SpillNo=3, Literal=4, Lit16=5,
433 NoValue=6 }
434 Tag;
435
njnd5bb0a52002-09-27 10:24:48 +0000436/* Invalid register numbers (can't be negative) */
njn25e49d8e72002-09-23 09:36:25 +0000437#define INVALID_TEMPREG 999999999
438#define INVALID_REALREG 999999999
439
440/* Microinstruction opcodes. */
441typedef
442 enum {
njnd5bb0a52002-09-27 10:24:48 +0000443 NOP, /* Null op */
njn25e49d8e72002-09-23 09:36:25 +0000444
njnd5bb0a52002-09-27 10:24:48 +0000445 /* Moving values around */
446 GET, PUT, /* simulated register <--> TempReg */
447 GETF, PUTF, /* simulated %eflags <--> TempReg */
448 LOAD, STORE, /* memory <--> TempReg */
449 MOV, /* TempReg <--> TempReg */
450 CMOV, /* Used for cmpxchg and cmov */
njn25e49d8e72002-09-23 09:36:25 +0000451
njnd5bb0a52002-09-27 10:24:48 +0000452 /* Arithmetic/logical ops */
453 ADD, ADC, SUB, SBB, /* Add/subtract (w/wo carry) */
454 AND, OR, XOR, NOT, /* Boolean ops */
455 SHL, SHR, SAR, ROL, ROR, RCL, RCR, /* Shift/rotate (w/wo carry) */
456 NEG, /* Negate */
457 INC, DEC, /* Increment/decrement */
458 BSWAP, /* Big-endian <--> little-endian */
459 CC2VAL, /* Condition code --> 0 or 1 */
460 WIDEN, /* Signed or unsigned widening */
njn25e49d8e72002-09-23 09:36:25 +0000461
njnd5bb0a52002-09-27 10:24:48 +0000462 /* Conditional or unconditional jump */
463 JMP,
464
465 /* FPU ops */
466 FPU, /* Doesn't touch memory */
467 FPU_R, FPU_W, /* Reads/writes memory */
468
469 /* Not strictly needed, but improve address calculation translations. */
njn25e49d8e72002-09-23 09:36:25 +0000470 LEA1, /* reg2 := const + reg1 */
471 LEA2, /* reg3 := const + reg1 + reg2 * 1,2,4 or 8 */
472
njnd5bb0a52002-09-27 10:24:48 +0000473 /* Hack for x86 REP insns. Jump to literal if TempReg/RealReg is zero. */
474 JIFZ,
njn25e49d8e72002-09-23 09:36:25 +0000475
njnd5bb0a52002-09-27 10:24:48 +0000476 /* Advance the simulated %eip by some small (< 128) number. */
477 INCEIP,
njn25e49d8e72002-09-23 09:36:25 +0000478
njnd5bb0a52002-09-27 10:24:48 +0000479 /* Not for translating x86 calls -- only to call helpers */
480 CALLM_S, CALLM_E, /* Mark start/end of CALLM push/pop sequence */
481 PUSH, POP, CLEAR, /* Add/remove/zap args for helpers */
482 CALLM, /* Call assembly-code helper */
483
484 /* Not for translating x86 calls -- only to call C helper functions of
485 up to three arguments (or two if the functions has a return value).
486 Arguments and return value must be word-sized. More arguments can
487 be faked with global variables (eg. use VG_(set_global_var)()).
488
489 Seven possibilities: 'arg[123]' show where args go, 'ret' shows
490 where return value goes (if present).
njn25e49d8e72002-09-23 09:36:25 +0000491
492 CCALL(-, -, - ) void f(void)
493 CCALL(arg1, -, - ) void f(UInt arg1)
494 CCALL(arg1, arg2, - ) void f(UInt arg1, UInt arg2)
495 CCALL(arg1, arg2, arg3) void f(UInt arg1, UInt arg2, UInt arg3)
496 CCALL(-, -, ret ) UInt f(UInt)
497 CCALL(arg1, -, ret ) UInt f(UInt arg1)
njnd5bb0a52002-09-27 10:24:48 +0000498 CCALL(arg1, arg2, ret ) UInt f(UInt arg1, UInt arg2) */
njn25e49d8e72002-09-23 09:36:25 +0000499 CCALL,
500
njnd5bb0a52002-09-27 10:24:48 +0000501 /* This opcode makes it easy for skins that extend UCode to do this to
502 avoid opcode overlap:
njn25e49d8e72002-09-23 09:36:25 +0000503
njnd5bb0a52002-09-27 10:24:48 +0000504 enum { EU_OP1 = DUMMY_FINAL_UOPCODE + 1, ... }
njn25e49d8e72002-09-23 09:36:25 +0000505
506 WARNING: Do not add new opcodes after this one! They can be added
507 before, though. */
508 DUMMY_FINAL_UOPCODE
509 }
510 Opcode;
511
512
njnd5bb0a52002-09-27 10:24:48 +0000513/* Condition codes, using the Intel encoding. CondAlways is an extra. */
njn25e49d8e72002-09-23 09:36:25 +0000514typedef
515 enum {
516 CondO = 0, /* overflow */
517 CondNO = 1, /* no overflow */
518 CondB = 2, /* below */
519 CondNB = 3, /* not below */
520 CondZ = 4, /* zero */
521 CondNZ = 5, /* not zero */
522 CondBE = 6, /* below or equal */
523 CondNBE = 7, /* not below or equal */
524 CondS = 8, /* negative */
525 ConsNS = 9, /* not negative */
526 CondP = 10, /* parity even */
527 CondNP = 11, /* not parity even */
528 CondL = 12, /* jump less */
529 CondNL = 13, /* not less */
530 CondLE = 14, /* less or equal */
531 CondNLE = 15, /* not less or equal */
532 CondAlways = 16 /* Jump always */
533 }
534 Condcode;
535
536
537/* Descriptions of additional properties of *unconditional* jumps. */
538typedef
539 enum {
540 JmpBoring=0, /* boring unconditional jump */
541 JmpCall=1, /* jump due to an x86 call insn */
542 JmpRet=2, /* jump due to an x86 ret insn */
543 JmpSyscall=3, /* do a system call, then jump */
544 JmpClientReq=4 /* do a client request, then jump */
545 }
546 JmpKind;
547
548
549/* Flags. User-level code can only read/write O(verflow), S(ign),
550 Z(ero), A(ux-carry), C(arry), P(arity), and may also write
551 D(irection). That's a total of 7 flags. A FlagSet is a bitset,
552 thusly:
553 76543210
554 DOSZACP
555 and bit 7 must always be zero since it is unused.
556*/
557typedef UChar FlagSet;
558
559#define FlagD (1<<6)
560#define FlagO (1<<5)
561#define FlagS (1<<4)
562#define FlagZ (1<<3)
563#define FlagA (1<<2)
564#define FlagC (1<<1)
565#define FlagP (1<<0)
566
567#define FlagsOSZACP (FlagO | FlagS | FlagZ | FlagA | FlagC | FlagP)
568#define FlagsOSZAP (FlagO | FlagS | FlagZ | FlagA | FlagP)
569#define FlagsOSZCP (FlagO | FlagS | FlagZ | FlagC | FlagP)
570#define FlagsOSACP (FlagO | FlagS | FlagA | FlagC | FlagP)
571#define FlagsSZACP ( FlagS | FlagZ | FlagA | FlagC | FlagP)
572#define FlagsSZAP ( FlagS | FlagZ | FlagA | FlagP)
573#define FlagsZCP ( FlagZ | FlagC | FlagP)
574#define FlagsOC (FlagO | FlagC )
575#define FlagsAC ( FlagA | FlagC )
576
577#define FlagsALL (FlagsOSZACP | FlagD)
578#define FlagsEmpty (FlagSet)0
579
580
581/* Liveness of general purpose registers, useful for code generation.
582 Reg rank order 0..N-1 corresponds to bits 0..N-1, ie. first
583 reg's liveness in bit 0, last reg's in bit N-1. Note that
584 these rankings don't match the Intel register ordering. */
585typedef UInt RRegSet;
586
587#define ALL_RREGS_DEAD 0 /* 0000...00b */
588#define ALL_RREGS_LIVE (1 << (VG_MAX_REALREGS-1)) /* 0011...11b */
589#define UNIT_RREGSET(rank) (1 << (rank))
590
591#define IS_RREG_LIVE(rank,rregs_live) (rregs_live & UNIT_RREGSET(rank))
592#define SET_RREG_LIVENESS(rank,rregs_live,b) \
593 do { RRegSet unit = UNIT_RREGSET(rank); \
594 if (b) rregs_live |= unit; \
595 else rregs_live &= ~unit; \
596 } while(0)
597
598
599/* A Micro (u)-instruction. */
600typedef
601 struct {
602 /* word 1 */
603 UInt lit32; /* 32-bit literal */
604
605 /* word 2 */
606 UShort val1; /* first operand */
607 UShort val2; /* second operand */
608
609 /* word 3 */
610 UShort val3; /* third operand */
611 UChar opcode; /* opcode */
612 UChar size; /* data transfer size */
613
614 /* word 4 */
615 FlagSet flags_r; /* :: FlagSet */
616 FlagSet flags_w; /* :: FlagSet */
617 UChar tag1:4; /* first operand tag */
618 UChar tag2:4; /* second operand tag */
619 UChar tag3:4; /* third operand tag */
620 UChar extra4b:4; /* Spare field, used by WIDEN for src
621 -size, and by LEA2 for scale (1,2,4 or 8),
622 and by JMPs for original x86 instr size */
623
624 /* word 5 */
625 UChar cond; /* condition, for jumps */
626 Bool signed_widen:1; /* signed or unsigned WIDEN ? */
627 JmpKind jmpkind:3; /* additional properties of unconditional JMP */
628
629 /* Additional properties for UInstrs that call C functions:
630 - CCALL
631 - PUT (when %ESP is the target)
632 - possibly skin-specific UInstrs
633 */
634 UChar argc:2; /* Number of args, max 3 */
635 UChar regparms_n:2; /* Number of args passed in registers */
636 Bool has_ret_val:1; /* Function has return value? */
637
638 /* RealReg liveness; only sensical after reg alloc and liveness
639 analysis done. This info is a little bit arch-specific --
640 VG_MAX_REALREGS can vary on different architectures. Note that
641 to use this information requires converting between register ranks
njn4ba5a792002-09-30 10:23:54 +0000642 and the Intel register numbers, using VG_(realreg_to_rank)()
643 and/or VG_(rank_to_realreg)() */
njn25e49d8e72002-09-23 09:36:25 +0000644 RRegSet regs_live_after:VG_MAX_REALREGS;
645 }
646 UInstr;
647
648
649/* Expandable arrays of uinstrs. */
650typedef
651 struct {
652 Int used;
653 Int size;
654 UInstr* instrs;
655 Int nextTemp;
656 }
657 UCodeBlock;
658
659
660/*====================================================================*/
661/*=== Instrumenting UCode ===*/
662/*====================================================================*/
663
664/* A structure for communicating TempReg and RealReg uses of UInstrs. */
665typedef
666 struct {
667 Int num;
668 Bool isWrite;
669 }
670 RegUse;
671
672/* Find what this instruction does to its regs. Tag indicates whether we're
673 * considering TempRegs (pre-reg-alloc) or RealRegs (post-reg-alloc).
674 * Useful for analysis/optimisation passes. */
njn4ba5a792002-09-30 10:23:54 +0000675extern Int VG_(get_reg_usage) ( UInstr* u, Tag tag, RegUse* arr );
njn25e49d8e72002-09-23 09:36:25 +0000676
677
678/* ------------------------------------------------------------------ */
njnd5bb0a52002-09-27 10:24:48 +0000679/* Used to register helper functions to be called from generated code. A
680 limited number of compact helpers can be registered; the code generated
681 to call them is slightly shorter -- so register the mostly frequently
682 called helpers as compact. */
njn25e49d8e72002-09-23 09:36:25 +0000683extern void VG_(register_compact_helper) ( Addr a );
684extern void VG_(register_noncompact_helper) ( Addr a );
685
686
687/* ------------------------------------------------------------------ */
688/* Virtual register allocation */
689
690/* Get a new virtual register */
njn4ba5a792002-09-30 10:23:54 +0000691extern Int VG_(get_new_temp) ( UCodeBlock* cb );
njn25e49d8e72002-09-23 09:36:25 +0000692
693/* Get a new virtual shadow register */
njn4ba5a792002-09-30 10:23:54 +0000694extern Int VG_(get_new_shadow) ( UCodeBlock* cb );
njn25e49d8e72002-09-23 09:36:25 +0000695
696/* Get a virtual register's corresponding virtual shadow register */
697#define SHADOW(tempreg) ((tempreg)+1)
698
699
700/* ------------------------------------------------------------------ */
701/* Low-level UInstr builders */
njn4ba5a792002-09-30 10:23:54 +0000702extern void VG_(new_NOP) ( UInstr* u );
703extern void VG_(new_UInstr0) ( UCodeBlock* cb, Opcode opcode, Int sz );
704extern void VG_(new_UInstr1) ( UCodeBlock* cb, Opcode opcode, Int sz,
njn25e49d8e72002-09-23 09:36:25 +0000705 Tag tag1, UInt val1 );
njn4ba5a792002-09-30 10:23:54 +0000706extern void VG_(new_UInstr2) ( UCodeBlock* cb, Opcode opcode, Int sz,
njn25e49d8e72002-09-23 09:36:25 +0000707 Tag tag1, UInt val1,
708 Tag tag2, UInt val2 );
njn4ba5a792002-09-30 10:23:54 +0000709extern void VG_(new_UInstr3) ( UCodeBlock* cb, Opcode opcode, Int sz,
njn25e49d8e72002-09-23 09:36:25 +0000710 Tag tag1, UInt val1,
711 Tag tag2, UInt val2,
712 Tag tag3, UInt val3 );
njn25e49d8e72002-09-23 09:36:25 +0000713
njn4ba5a792002-09-30 10:23:54 +0000714extern void VG_(set_flag_RW) ( UInstr* u, FlagSet fr, FlagSet fw );
715extern void VG_(set_lit_field) ( UCodeBlock* cb, UInt lit32 );
716extern void VG_(set_ccall_fields) ( UCodeBlock* cb, Addr fn, UChar argc,
717 UChar regparms_n, Bool has_ret_val );
njn25e49d8e72002-09-23 09:36:25 +0000718
njn4ba5a792002-09-30 10:23:54 +0000719extern void VG_(copy_UInstr) ( UCodeBlock* cb, UInstr* instr );
720
721extern Bool VG_(any_flag_use)( UInstr* u );
njn25e49d8e72002-09-23 09:36:25 +0000722
723/* Refer to `the last instruction stuffed in' (can be lvalue). */
724#define LAST_UINSTR(cb) (cb)->instrs[(cb)->used-1]
725
726
727/* ------------------------------------------------------------------ */
728/* Higher-level UInstr sequence builders */
njn4ba5a792002-09-30 10:23:54 +0000729extern void VG_(call_helper_0_0) ( UCodeBlock* cb, Addr f);
730extern void VG_(call_helper_1_0) ( UCodeBlock* cb, Addr f, UInt arg1,
731 UInt regparms_n);
732extern void VG_(call_helper_2_0) ( UCodeBlock* cb, Addr f, UInt arg1, UInt arg2,
733 UInt regparms_n);
njn25e49d8e72002-09-23 09:36:25 +0000734
735/* One way around the 3-arg C function limit is to pass args via global
736 * variables... ugly, but it works. */
njn4ba5a792002-09-30 10:23:54 +0000737extern void VG_(set_global_var) ( UCodeBlock* cb, Addr globvar_ptr, UInt val);
njn25e49d8e72002-09-23 09:36:25 +0000738
739/* ------------------------------------------------------------------ */
njnd5bb0a52002-09-27 10:24:48 +0000740/* Allocating/freeing basic blocks of UCode */
njn4ba5a792002-09-30 10:23:54 +0000741extern UCodeBlock* VG_(alloc_UCodeBlock) ( void );
742extern void VG_(free_UCodeBlock) ( UCodeBlock* cb );
njnd5bb0a52002-09-27 10:24:48 +0000743
744/* ------------------------------------------------------------------ */
745/* UCode pretty/ugly printing. Probably only useful to call from a skin
njn25e49d8e72002-09-23 09:36:25 +0000746 if VG_(needs).extended_UCode == True. */
747
748/* When True, all generated code is/should be printed. */
749extern Bool VG_(print_codegen);
750
njn4ba5a792002-09-30 10:23:54 +0000751/* Pretty/ugly printing functions */
752extern void VG_(pp_UCodeBlock) ( UCodeBlock* cb, Char* title );
753extern void VG_(pp_UInstr) ( Int instrNo, UInstr* u );
754extern void VG_(pp_UInstr_regs) ( Int instrNo, UInstr* u );
755extern void VG_(up_UInstr) ( Int instrNo, UInstr* u );
756extern Char* VG_(name_UOpcode) ( Bool upper, Opcode opc );
757extern void VG_(pp_UOperand) ( UInstr* u, Int operandNo,
758 Int sz, Bool parens );
njn25e49d8e72002-09-23 09:36:25 +0000759
njn25e49d8e72002-09-23 09:36:25 +0000760
761/*====================================================================*/
njnd5bb0a52002-09-27 10:24:48 +0000762/*=== Generating x86 code from UCode ===*/
njn25e49d8e72002-09-23 09:36:25 +0000763/*====================================================================*/
764
njnd5bb0a52002-09-27 10:24:48 +0000765/* All this only necessary for skins with VG_(needs).extends_UCode == True. */
njn25e49d8e72002-09-23 09:36:25 +0000766
767/* This is the Intel register encoding. */
768#define R_EAX 0
769#define R_ECX 1
770#define R_EDX 2
771#define R_EBX 3
772#define R_ESP 4
773#define R_EBP 5
774#define R_ESI 6
775#define R_EDI 7
776
777#define R_AL (0+R_EAX)
778#define R_CL (0+R_ECX)
779#define R_DL (0+R_EDX)
780#define R_BL (0+R_EBX)
781#define R_AH (4+R_EAX)
782#define R_CH (4+R_ECX)
783#define R_DH (4+R_EDX)
784#define R_BH (4+R_EBX)
785
786/* For pretty printing x86 code */
njn4ba5a792002-09-30 10:23:54 +0000787extern Char* VG_(name_of_int_reg) ( Int size, Int reg );
788extern Char VG_(name_of_int_size) ( Int size );
njn25e49d8e72002-09-23 09:36:25 +0000789
790/* Randomly useful things */
791extern UInt VG_(extend_s_8to32) ( UInt x );
792
793/* Code emitters */
njn4ba5a792002-09-30 10:23:54 +0000794extern void VG_(emitB) ( UInt b );
795extern void VG_(emitW) ( UInt w );
796extern void VG_(emitL) ( UInt l );
797extern void VG_(new_emit) ( void );
njn25e49d8e72002-09-23 09:36:25 +0000798
799/* Finding offsets */
njn4ba5a792002-09-30 10:23:54 +0000800extern Int VG_(helper_offset) ( Addr a );
801extern Int VG_(shadow_reg_offset) ( Int arch );
802extern Int VG_(shadow_flags_offset) ( void );
njn25e49d8e72002-09-23 09:36:25 +0000803
njnd5bb0a52002-09-27 10:24:48 +0000804/* Convert reg ranks <-> Intel register ordering, for using register
805 liveness information. */
njn4ba5a792002-09-30 10:23:54 +0000806extern Int VG_(realreg_to_rank) ( Int realreg );
807extern Int VG_(rank_to_realreg) ( Int rank );
njn25e49d8e72002-09-23 09:36:25 +0000808
njnd5bb0a52002-09-27 10:24:48 +0000809/* Call a subroutine. Does no argument passing, stack manipulations, etc. */
njn4ba5a792002-09-30 10:23:54 +0000810extern void VG_(synth_call) ( Bool ensure_shortform, Int word_offset );
njn25e49d8e72002-09-23 09:36:25 +0000811
njnd5bb0a52002-09-27 10:24:48 +0000812/* For calling C functions -- saves caller save regs, pushes args, calls,
813 clears the stack, restores caller save regs. `fn' must be registered in
814 the baseBlock first. Acceptable tags are RealReg and Literal. Optimises
815 things, eg. by not preserving non-live caller-save registers.
njn25e49d8e72002-09-23 09:36:25 +0000816
njnd5bb0a52002-09-27 10:24:48 +0000817 WARNING: a UInstr should *not* be translated with synth_ccall() followed
818 by some other x86 assembly code; this will invalidate the results of
819 vg_realreg_liveness_analysis() and everything will fall over. */
njn4ba5a792002-09-30 10:23:54 +0000820extern void VG_(synth_ccall) ( Addr fn, Int argc, Int regparms_n, UInt argv[],
821 Tag tagv[], Int ret_reg,
822 RRegSet regs_live_before,
823 RRegSet regs_live_after );
njn25e49d8e72002-09-23 09:36:25 +0000824
825/* Addressing modes */
njn4ba5a792002-09-30 10:23:54 +0000826extern void VG_(emit_amode_offregmem_reg)( Int off, Int regmem, Int reg );
827extern void VG_(emit_amode_ereg_greg) ( Int e_reg, Int g_reg );
njn25e49d8e72002-09-23 09:36:25 +0000828
829/* v-size (4, or 2 with OSO) insn emitters */
njn4ba5a792002-09-30 10:23:54 +0000830extern void VG_(emit_movv_offregmem_reg) ( Int sz, Int off, Int areg, Int reg );
831extern void VG_(emit_movv_reg_offregmem) ( Int sz, Int reg, Int off, Int areg );
832extern void VG_(emit_movv_reg_reg) ( Int sz, Int reg1, Int reg2 );
833extern void VG_(emit_nonshiftopv_lit_reg)( Int sz, Opcode opc, UInt lit,
834 Int reg );
835extern void VG_(emit_shiftopv_lit_reg) ( Int sz, Opcode opc, UInt lit,
836 Int reg );
837extern void VG_(emit_nonshiftopv_reg_reg)( Int sz, Opcode opc,
838 Int reg1, Int reg2 );
839extern void VG_(emit_movv_lit_reg) ( Int sz, UInt lit, Int reg );
840extern void VG_(emit_unaryopv_reg) ( Int sz, Opcode opc, Int reg );
841extern void VG_(emit_pushv_reg) ( Int sz, Int reg );
842extern void VG_(emit_popv_reg) ( Int sz, Int reg );
njn25e49d8e72002-09-23 09:36:25 +0000843
njn4ba5a792002-09-30 10:23:54 +0000844extern void VG_(emit_pushl_lit32) ( UInt int32 );
845extern void VG_(emit_pushl_lit8) ( Int lit8 );
846extern void VG_(emit_cmpl_zero_reg) ( Int reg );
847extern void VG_(emit_swapl_reg_EAX) ( Int reg );
848extern void VG_(emit_movv_lit_offregmem) ( Int sz, UInt lit, Int off,
849 Int memreg );
njn25e49d8e72002-09-23 09:36:25 +0000850
851/* b-size (1 byte) instruction emitters */
njn4ba5a792002-09-30 10:23:54 +0000852extern void VG_(emit_movb_lit_offregmem) ( UInt lit, Int off, Int memreg );
853extern void VG_(emit_movb_reg_offregmem) ( Int reg, Int off, Int areg );
854extern void VG_(emit_unaryopb_reg) ( Opcode opc, Int reg );
855extern void VG_(emit_testb_lit_reg) ( UInt lit, Int reg );
njn25e49d8e72002-09-23 09:36:25 +0000856
857/* zero-extended load emitters */
njn4ba5a792002-09-30 10:23:54 +0000858extern void VG_(emit_movzbl_offregmem_reg) ( Int off, Int regmem, Int reg );
859extern void VG_(emit_movzwl_offregmem_reg) ( Int off, Int areg, Int reg );
njn25e49d8e72002-09-23 09:36:25 +0000860
861/* misc instruction emitters */
njn4ba5a792002-09-30 10:23:54 +0000862extern void VG_(emit_call_reg) ( Int reg );
863extern void VG_(emit_add_lit_to_esp) ( Int lit );
864extern void VG_(emit_jcondshort_delta) ( Condcode cond, Int delta );
865extern void VG_(emit_pushal) ( void );
866extern void VG_(emit_popal) ( void );
867extern void VG_(emit_AMD_prefetch_reg) ( Int reg );
njn25e49d8e72002-09-23 09:36:25 +0000868
869
870/*====================================================================*/
871/*=== Execution contexts ===*/
872/*====================================================================*/
873
874/* Generic resolution type used in a few different ways, such as deciding
875 how closely to compare two errors for equality. */
876typedef
877 enum { Vg_LowRes, Vg_MedRes, Vg_HighRes }
878 VgRes;
879
880typedef
881 struct _ExeContext
882 ExeContext;
883
njnd5bb0a52002-09-27 10:24:48 +0000884/* Compare two ExeContexts. Number of callers considered depends on `res':
885 Vg_LowRes: 2
886 Vg_MedRes: 4
887 Vg_HighRes: all */
njn25e49d8e72002-09-23 09:36:25 +0000888extern Bool VG_(eq_ExeContext) ( VgRes res,
889 ExeContext* e1, ExeContext* e2 );
890
891/* Print an ExeContext. */
892extern void VG_(pp_ExeContext) ( ExeContext* );
893
894/* Take a snapshot of the client's stack. Search our collection of
895 ExeContexts to see if we already have it, and if not, allocate a
896 new one. Either way, return a pointer to the context. */
897extern ExeContext* VG_(get_ExeContext) ( ThreadState *tst );
898
899
900/*====================================================================*/
901/*=== Error reporting ===*/
902/*====================================================================*/
903
904/* ------------------------------------------------------------------ */
905/* Suppressions describe errors which we want to suppress, ie, not
906 show the user, usually because it is caused by a problem in a library
907 which we can't fix, replace or work around. Suppressions are read from
njnd5bb0a52002-09-27 10:24:48 +0000908 a file at startup time. This gives flexibility so that new
njn25e49d8e72002-09-23 09:36:25 +0000909 suppressions can be added to the file as and when needed.
910*/
911
912typedef
913 Int /* Do not make this unsigned! */
914 SuppKind;
915
916/* An extensible (via the 'extra' field) suppression record. This holds
917 the suppression details of interest to a skin. Skins can use a normal
918 enum (with element values in the normal range (0..)) for `skind'.
919
920 If VG_(needs).report_errors==True, for each suppression read in by core
921 SKN_(recognised_suppression)() and SKN_(read_extra_suppression_info) will
922 be called. The `skind' field is filled in by the value returned in the
923 argument of the first function; the second function can fill in the
924 `string' and `extra' fields if it wants.
925*/
926typedef
927 struct {
928 /* What kind of suppression. Must use the range (0..) */
929 SuppKind skind;
930 /* String -- use is optional. NULL by default. */
931 Char* string;
932 /* Anything else -- use is optional. NULL by default. */
933 void* extra;
934 }
935 SkinSupp;
936
937
938/* ------------------------------------------------------------------ */
939/* Error records contain enough info to generate an error report. The idea
940 is that (typically) the same few points in the program generate thousands
njnd5bb0a52002-09-27 10:24:48 +0000941 of errors, and we don't want to spew out a fresh error message for each
942 one. Instead, we use these structures to common up duplicates.
njn25e49d8e72002-09-23 09:36:25 +0000943*/
944
945typedef
946 Int /* Do not make this unsigned! */
947 ErrorKind;
948
949/* An extensible (via the 'extra' field) error record. This holds
950 the error details of interest to a skin. Skins can use a normal
951 enum (with element values in the normal range (0..)) for `ekind'.
952
953 When errors are found and recorded with VG_(maybe_record_error)(), all
954 the skin must do is pass in the four parameters; core will
955 allocate/initialise the error record.
956*/
957typedef
958 struct {
959 /* Used by ALL. Must be in the range (0..) */
960 Int ekind;
961 /* Used frequently */
962 Addr addr;
963 /* Used frequently */
964 Char* string;
njnd5bb0a52002-09-27 10:24:48 +0000965 /* For any skin-specific extras */
njn25e49d8e72002-09-23 09:36:25 +0000966 void* extra;
967 }
968 SkinError;
969
970
971/* ------------------------------------------------------------------ */
njnd5bb0a52002-09-27 10:24:48 +0000972/* Call this when an error occurs. It will be recorded if it hasn't been
njn25e49d8e72002-09-23 09:36:25 +0000973 seen before. If it has, the existing error record will have its count
974 incremented.
975
976 If the error occurs in generated code, 'tst' should be NULL. If the
977 error occurs in non-generated code, 'tst' should be non-NULL. The
978 `extra' field can be stack-allocated; it will be copied (using
979 SKN_(dup_extra_and_update)()) if needed. But it won't be copied
980 if it's NULL.
njnd5bb0a52002-09-27 10:24:48 +0000981
982 If no 'a', 's' or 'extra' of interest needs to be recorded, just use
983 NULL for them.
njn25e49d8e72002-09-23 09:36:25 +0000984*/
985extern void VG_(maybe_record_error) ( ThreadState* tst, ErrorKind ekind,
986 Addr a, Char* s, void* extra );
987
988/* Gets a non-blank, non-comment line of at most nBuf chars from fd.
989 Skips leading spaces on the line. Returns True if EOF was hit instead.
990 Useful for reading in extra skin-specific suppression lines.
991*/
njn4ba5a792002-09-30 10:23:54 +0000992extern Bool VG_(get_line) ( Int fd, Char* buf, Int nBuf );
njn25e49d8e72002-09-23 09:36:25 +0000993
994
995/*====================================================================*/
996/*=== Obtaining debug information ===*/
997/*====================================================================*/
998
999/* Get the file/function/line number of the instruction at address 'a'.
1000 For these four, if debug info for the address is found, it copies the
1001 info into the buffer/UInt and returns True. If not, it returns False and
1002 nothing is copied. VG_(get_fnname) always demangles C++ function names.
1003*/
1004extern Bool VG_(get_filename) ( Addr a, Char* filename, Int n_filename );
1005extern Bool VG_(get_fnname) ( Addr a, Char* fnname, Int n_fnname );
1006extern Bool VG_(get_linenum) ( Addr a, UInt* linenum );
1007
1008/* This one is more efficient if getting both filename and line number,
1009 because the two lookups are done together. */
1010extern Bool VG_(get_filename_linenum)
1011 ( Addr a, Char* filename, Int n_filename,
1012 UInt* linenum );
1013
1014/* Succeeds only if we find from debug info that 'a' is the address of the
1015 first instruction in a function -- as opposed to VG_(get_fnname) which
1016 succeeds if we find from debug info that 'a' is the address of any
1017 instruction in a function. Use this to instrument the start of
njnd5bb0a52002-09-27 10:24:48 +00001018 a particular function. Nb: if an executable/shared object is stripped
njn25e49d8e72002-09-23 09:36:25 +00001019 of its symbols, this function will not be able to recognise function
1020 entry points within it. */
1021extern Bool VG_(get_fnname_if_entry) ( Addr a, Char* filename, Int n_filename );
1022
1023/* Succeeds if the address is within a shared object or the main executable.
1024 It doesn't matter if debug info is present or not. */
1025extern Bool VG_(get_objname) ( Addr a, Char* objname, Int n_objname );
1026
1027
1028/*====================================================================*/
1029/*=== Shadow chunks and block-finding ===*/
1030/*====================================================================*/
1031
1032typedef
1033 enum {
1034 Vg_AllocMalloc = 0,
1035 Vg_AllocNew = 1,
1036 Vg_AllocNewVec = 2
1037 }
1038 VgAllocKind;
1039
1040/* Description of a malloc'd chunk. skin_extra[] part can be used by
1041 the skin; size of array is given by VG_(needs).sizeof_shadow_chunk. */
1042typedef
1043 struct _ShadowChunk {
1044 struct _ShadowChunk* next;
1045 UInt size : 30; /* size requested */
1046 VgAllocKind allockind : 2; /* which wrapper did the allocation */
1047 Addr data; /* ptr to actual block */
1048 UInt skin_extra[0]; /* extra skin-specific info */
1049 }
1050 ShadowChunk;
1051
1052/* Use this to free blocks if VG_(needs).alternative_free == True.
1053 It frees the ShadowChunk and the malloc'd block it points to. */
njn4ba5a792002-09-30 10:23:54 +00001054extern void VG_(free_ShadowChunk) ( ShadowChunk* sc );
njn25e49d8e72002-09-23 09:36:25 +00001055
1056/* Makes an array of pointers to all the shadow chunks of malloc'd blocks */
1057extern ShadowChunk** VG_(get_malloc_shadows) ( /*OUT*/ UInt* n_shadows );
1058
1059/* Determines if address 'a' is within the bounds of the block at start.
1060 Allows a little 'slop' round the edges. */
1061extern Bool VG_(addr_is_in_block) ( Addr a, Addr start, UInt size );
1062
1063/* Searches through currently malloc'd blocks until a matching one is found.
1064 Returns NULL if none match. Extra arguments can be implicitly passed to
njnd5bb0a52002-09-27 10:24:48 +00001065 p using nested functions; see memcheck/mc_errcontext.c for an example. */
njn25e49d8e72002-09-23 09:36:25 +00001066extern ShadowChunk* VG_(any_matching_mallocd_ShadowChunks)
1067 ( Bool (*p) ( ShadowChunk* ));
1068
1069/* Searches through all thread's stacks to see if any match. Returns
1070 * VG_INVALID_THREADID if none match. */
1071extern ThreadId VG_(any_matching_thread_stack)
1072 ( Bool (*p) ( Addr stack_min, Addr stack_max ));
1073
1074/*====================================================================*/
1075/*=== Skin-specific stuff ===*/
1076/*====================================================================*/
1077
1078/* Skin-specific settings.
1079 *
1080 * If new fields are added to this type, update:
njnd5bb0a52002-09-27 10:24:48 +00001081 * - vg_main.c:initialisation of VG_(needs)
njn25e49d8e72002-09-23 09:36:25 +00001082 * - vg_main.c:sanity_check_needs()
1083 *
1084 * If the name of this type or any of its fields change, update:
1085 * - dependent comments (just search for "VG_(needs)").
1086 */
1087typedef
1088 struct {
njnd5bb0a52002-09-27 10:24:48 +00001089 /* Name and description used in the startup message. 'name' also
1090 determines the string used for identifying suppressions in a
1091 suppression file as belonging to this skin. */
njn25e49d8e72002-09-23 09:36:25 +00001092 Char* name;
1093 Char* description;
1094
njnd5bb0a52002-09-27 10:24:48 +00001095 /* Booleans that decide core behaviour, but don't require extra
1096 operations to be defined if `True' */
1097
1098 /* Should __libc_freeres() be run? Bugs in it can crash the skin. */
1099 Bool run_libc_freeres;
njn25e49d8e72002-09-23 09:36:25 +00001100
1101 /* Want to have errors detected by Valgrind's core reported? Includes:
1102 - pthread API errors (many; eg. unlocking a non-locked mutex)
1103 - silly arguments to malloc() et al (eg. negative size)
1104 - invalid file descriptors to blocking syscalls read() and write()
1105 - bad signal numbers passed to sigaction()
1106 - attempt to install signal handler for SIGKILL or SIGSTOP */
1107 Bool core_errors;
njn25e49d8e72002-09-23 09:36:25 +00001108
1109 /* Booleans that indicate extra operations are defined; if these are
1110 True, the corresponding template functions (given below) must be
1111 defined. A lot like being a member of a type class. */
1112
njnd5bb0a52002-09-27 10:24:48 +00001113 /* Want to report errors from the skin? This implies use of
1114 suppressions, too. */
1115 Bool skin_errors;
1116
njn25e49d8e72002-09-23 09:36:25 +00001117 /* Is information kept about specific individual basic blocks? (Eg. for
njnd5bb0a52002-09-27 10:24:48 +00001118 cachegrind there are cost-centres for every instruction, stored at a
njn25e49d8e72002-09-23 09:36:25 +00001119 basic block level.) If so, it sometimes has to be discarded, because
1120 .so mmap/munmap-ping or self-modifying code (informed by the
1121 DISCARD_TRANSLATIONS user request) can cause one instruction address
njnd5bb0a52002-09-27 10:24:48 +00001122 to be used for more than one instruction in one program run... */
njn25e49d8e72002-09-23 09:36:25 +00001123 Bool basic_block_discards;
1124
njnd5bb0a52002-09-27 10:24:48 +00001125 /* Skin maintains information about each register? */
njn25e49d8e72002-09-23 09:36:25 +00001126 Bool shadow_regs;
1127
1128 /* Skin defines its own command line options? */
1129 Bool command_line_options;
1130 /* Skin defines its own client requests? */
1131 Bool client_requests;
1132
1133 /* Skin defines its own UInstrs? */
1134 Bool extended_UCode;
1135
1136 /* Skin does stuff before and/or after system calls? */
1137 Bool syscall_wrapper;
1138
1139 /* Size, in words, of extra info about malloc'd blocks recorded by
1140 skin. Be careful to get this right or you'll get seg faults! */
1141 UInt sizeof_shadow_block;
1142
1143 /* Skin does free()s itself? */
1144 Bool alternative_free;
1145
1146 /* Are skin-state sanity checks performed? */
1147 Bool sanity_checks;
1148 }
1149 VgNeeds;
1150
1151extern VgNeeds VG_(needs);
1152
1153
1154/* ------------------------------------------------------------------ */
1155/* Core events to track */
1156
1157/* Part of the core from which this call was made. Useful for determining
njnd5bb0a52002-09-27 10:24:48 +00001158 what kind of error message should be emitted. */
njn25e49d8e72002-09-23 09:36:25 +00001159typedef
1160 enum { Vg_CorePThread, Vg_CoreSignal, Vg_CoreSysCall, Vg_CoreTranslate }
1161 CorePart;
1162
1163/* Events happening in core to track. To be notified, assign a function
njnd5bb0a52002-09-27 10:24:48 +00001164 to the function pointer. To ignore an event, don't do anything
1165 (default assignment is to NULL in which case the call is skipped). */
njn25e49d8e72002-09-23 09:36:25 +00001166typedef
1167 struct {
1168 /* Memory events */
1169 void (*new_mem_startup)( Addr a, UInt len, Bool rr, Bool ww, Bool xx );
1170 void (*new_mem_heap) ( Addr a, UInt len, Bool is_inited );
1171 void (*new_mem_stack) ( Addr a, UInt len );
1172 void (*new_mem_stack_aligned) ( Addr a, UInt len );
1173 void (*new_mem_stack_signal) ( Addr a, UInt len );
1174 void (*new_mem_brk) ( Addr a, UInt len );
1175 void (*new_mem_mmap) ( Addr a, UInt len,
1176 Bool nn, Bool rr, Bool ww, Bool xx );
1177
1178 void (*copy_mem_heap) ( Addr from, Addr to, UInt len );
1179 void (*copy_mem_remap) ( Addr from, Addr to, UInt len );
1180 void (*change_mem_mprotect) ( Addr a, UInt len,
1181 Bool nn, Bool rr, Bool ww, Bool xx );
1182
njnd5bb0a52002-09-27 10:24:48 +00001183 /* Used on redzones around malloc'd blocks and at end of stack */
njn25e49d8e72002-09-23 09:36:25 +00001184 void (*ban_mem_heap) ( Addr a, UInt len );
1185 void (*ban_mem_stack) ( Addr a, UInt len );
1186
1187 void (*die_mem_heap) ( Addr a, UInt len );
1188 void (*die_mem_stack) ( Addr a, UInt len );
1189 void (*die_mem_stack_aligned) ( Addr a, UInt len );
1190 void (*die_mem_stack_signal) ( Addr a, UInt len );
1191 void (*die_mem_brk) ( Addr a, UInt len );
1192 void (*die_mem_munmap) ( Addr a, UInt len );
1193
1194 void (*bad_free) ( ThreadState* tst, Addr a );
1195 void (*mismatched_free) ( ThreadState* tst, Addr a );
1196
1197 void (*pre_mem_read) ( CorePart part, ThreadState* tst,
1198 Char* s, Addr a, UInt size );
1199 void (*pre_mem_read_asciiz) ( CorePart part, ThreadState* tst,
1200 Char* s, Addr a );
1201 void (*pre_mem_write) ( CorePart part, ThreadState* tst,
1202 Char* s, Addr a, UInt size );
1203 /* Not implemented yet -- have to add in lots of places, which is a
1204 pain. Won't bother unless/until there's a need. */
1205 /* void (*post_mem_read) ( ThreadState* tst, Char* s,
1206 Addr a, UInt size ); */
1207 void (*post_mem_write) ( Addr a, UInt size );
1208
1209
njnd5bb0a52002-09-27 10:24:48 +00001210 /* Scheduler events (not exhaustive) */
njn25e49d8e72002-09-23 09:36:25 +00001211 void (*thread_run) ( ThreadId tid );
1212
1213
njnd5bb0a52002-09-27 10:24:48 +00001214 /* Mutex events (not exhaustive) */
njn25e49d8e72002-09-23 09:36:25 +00001215 void (*post_mutex_lock) ( ThreadId tid,
1216 void* /*pthread_mutex_t* */ mutex );
1217 void (*post_mutex_unlock) ( ThreadId tid,
1218 void* /*pthread_mutex_t* */ mutex );
njn25e49d8e72002-09-23 09:36:25 +00001219
njnd5bb0a52002-09-27 10:24:48 +00001220
1221 /* Others... thread, condition variable, signal events... */
njn25e49d8e72002-09-23 09:36:25 +00001222 /* ... */
1223 }
1224 VgTrackEvents;
1225
1226/* Declare the struct instance */
1227extern VgTrackEvents VG_(track_events);
1228
1229
1230/* ------------------------------------------------------------------ */
1231/* Template functions */
1232
1233/* These are the parameterised functions in the core. The default definitions
njnd5bb0a52002-09-27 10:24:48 +00001234 are overridden by LD_PRELOADed skin version. At the very least, a skin
1235 must define the fundamental template functions. Depending on what needs
1236 are set, extra template functions will be used too. Functions are
1237 grouped under the needs that govern their use. */
njn25e49d8e72002-09-23 09:36:25 +00001238
1239
1240/* ------------------------------------------------------------------ */
1241/* Fundamental template functions */
1242
1243/* Initialise skin. Must do the following:
1244 - initialise the 'needs' struct
1245 - register any helpers called by generated code
1246
1247 May do the following:
1248 - indicate events to track by initialising part or all of the 'track'
1249 struct
1250 - register any skin-specific profiling events
1251 - any other skin-specific initialisation
1252*/
1253extern void SK_(pre_clo_init) ( VgNeeds* needs, VgTrackEvents* track );
1254
njnd5bb0a52002-09-27 10:24:48 +00001255/* Do initialisation that can only be done after command line processing. */
njn25e49d8e72002-09-23 09:36:25 +00001256extern void SK_(post_clo_init)( void );
1257
1258/* Instrument a basic block. Must be a true function, ie. the same input
1259 always results in the same output, because basic blocks can be
1260 retranslated. Unless you're doing something really strange...
1261 'orig_addr' is the address of the first instruction in the block. */
1262extern UCodeBlock* SK_(instrument) ( UCodeBlock* cb, Addr orig_addr );
1263
1264/* Finish up, print out any results, etc. */
1265extern void SK_(fini) ( void );
1266
1267
1268/* ------------------------------------------------------------------ */
1269/* VG_(needs).report_errors */
1270
1271/* Identify if two errors are equal, or equal enough. `res' indicates how
1272 close is "close enough". `res' should be passed on as necessary, eg. if
1273 the SkinError's extra field contains an ExeContext, `res' should be
1274 passed to VG_(eq_ExeContext)() if the ExeContexts are considered. Other
1275 than that, probably don't worry about it unless you have lots of very
1276 similar errors occurring.
1277 */
1278extern Bool SK_(eq_SkinError) ( VgRes res,
1279 SkinError* e1, SkinError* e2 );
1280
1281/* Print error context. The passed function pp_ExeContext() can be (and
1282 probably should be) used to print the location of the error. */
1283extern void SK_(pp_SkinError) ( SkinError* ec, void (*pp_ExeContext)(void) );
1284
1285/* Copy the ec->extra part and replace ec->extra with the new copy. This is
1286 necessary to move from a temporary stack copy to a permanent heap one.
1287
1288 Then fill in any details that could be postponed until after the decision
1289 whether to ignore the error (ie. details not affecting the result of
1290 SK_(eq_SkinError)()). This saves time when errors are ignored.
1291
1292 Yuk.
1293*/
1294extern void SK_(dup_extra_and_update)(SkinError* ec);
1295
1296/* Return value indicates recognition. If recognised, type goes in `skind'. */
1297extern Bool SK_(recognised_suppression) ( Char* name, SuppKind *skind );
1298
1299/* Read any extra info for this suppression kind. For filling up the
1300 `string' and `extra' fields in a `SkinSupp' struct if necessary. */
1301extern Bool SK_(read_extra_suppression_info) ( Int fd, Char* buf,
njnd5bb0a52002-09-27 10:24:48 +00001302 Int nBuf, SkinSupp *s );
njn25e49d8e72002-09-23 09:36:25 +00001303
1304/* This should just check the kinds match and maybe some stuff in the
1305 'extra' field if appropriate */
1306extern Bool SK_(error_matches_suppression)(SkinError* ec, SkinSupp* su);
1307
1308
1309/* ------------------------------------------------------------------ */
1310/* VG_(needs).basic_block_discards */
1311
njnd5bb0a52002-09-27 10:24:48 +00001312/* Should discard any information that pertains to specific basic blocks
1313 or instructions within the address range given. */
njn25e49d8e72002-09-23 09:36:25 +00001314extern void SK_(discard_basic_block_info) ( Addr a, UInt size );
1315
1316
1317/* ------------------------------------------------------------------ */
1318/* VG_(needs).shadow_regs */
1319
1320/* Valid values for general registers and EFLAGS register, for initialising
1321 and updating registers when written in certain places in core. */
1322extern void SK_(written_shadow_regs_values) ( UInt* gen_reg, UInt* eflags );
1323
1324
1325/* ------------------------------------------------------------------ */
1326/* VG_(needs).command_line_options */
1327
njnd5bb0a52002-09-27 10:24:48 +00001328/* Return True if option was recognised. Presumably sets some state to
1329 record the option as well. */
1330extern Bool SK_(process_cmd_line_option) ( Char* argv );
njn25e49d8e72002-09-23 09:36:25 +00001331
1332/* Print out command line usage for skin options */
1333extern Char* SK_(usage) ( void );
1334
1335
1336/* ------------------------------------------------------------------ */
1337/* VG_(needs).client_requests */
1338
1339extern UInt SK_(handle_client_request) ( ThreadState* tst, UInt* arg_block );
1340
1341
1342/* ------------------------------------------------------------------ */
1343/* VG_(needs).extends_UCode */
1344
njn4ba5a792002-09-30 10:23:54 +00001345/* Useful to use in VG_(get_Xreg_usage)() */
njnd5bb0a52002-09-27 10:24:48 +00001346#define VG_UINSTR_READS_REG(ono) \
njn25e49d8e72002-09-23 09:36:25 +00001347 { if (mycat(u->tag,ono) == tag) \
1348 { arr[n].num = mycat(u->val,ono); \
1349 arr[n].isWrite = False; \
1350 n++; \
1351 } \
1352 }
njnd5bb0a52002-09-27 10:24:48 +00001353#define VG_UINSTR_WRITES_REG(ono) \
1354 { if (mycat(u->tag,ono) == tag) \
1355 { arr[n].num = mycat(u->val,ono); \
1356 arr[n].isWrite = True; \
1357 n++; \
1358 } \
njn25e49d8e72002-09-23 09:36:25 +00001359 }
1360
njn4ba5a792002-09-30 10:23:54 +00001361/* 'X' prefix indicates eXtended UCode. */
1362extern Int SK_(get_Xreg_usage) ( UInstr* u, Tag tag, RegUse* arr );
1363extern void SK_(emit_XUInstr) ( UInstr* u, RRegSet regs_live_before );
1364extern Bool SK_(sane_XUInstr) ( Bool beforeRA, Bool beforeLiveness,
njn25e49d8e72002-09-23 09:36:25 +00001365 UInstr* u );
njn4ba5a792002-09-30 10:23:54 +00001366extern Char* SK_(name_XUOpcode) ( Opcode opc );
1367extern void SK_(pp_XUInstr) ( UInstr* u );
njn25e49d8e72002-09-23 09:36:25 +00001368
1369
1370/* ------------------------------------------------------------------ */
1371/* VG_(needs).syscall_wrapper */
1372
1373/* If either of the pre_ functions malloc() something to return, the
1374 * corresponding post_ function had better free() it!
1375 */
1376extern void* SK_( pre_syscall) ( ThreadId tid, UInt syscallno,
1377 Bool is_blocking );
1378extern void SK_(post_syscall) ( ThreadId tid, UInt syscallno,
1379 void* pre_result, Int res,
1380 Bool is_blocking );
1381
njnd5bb0a52002-09-27 10:24:48 +00001382
njn25e49d8e72002-09-23 09:36:25 +00001383/* ------------------------------------------------------------------ */
njnd5bb0a52002-09-27 10:24:48 +00001384/* VG_(needs).sizeof_shadow_chunk (if > 0) */
njn25e49d8e72002-09-23 09:36:25 +00001385
1386extern void SK_(complete_shadow_chunk) ( ShadowChunk* sc, ThreadState* tst );
1387
1388
1389/* ------------------------------------------------------------------ */
1390/* VG_(needs).alternative_free */
1391
1392extern void SK_(alt_free) ( ShadowChunk* sc, ThreadState* tst );
1393
njnd5bb0a52002-09-27 10:24:48 +00001394
njn25e49d8e72002-09-23 09:36:25 +00001395/* ---------------------------------------------------------------------
1396 VG_(needs).sanity_checks */
1397
njnd5bb0a52002-09-27 10:24:48 +00001398/* Can be useful for ensuring a skin's correctness. SK_(cheap_sanity_check)
1399 is called very frequently; SK_(expensive_sanity_check) is called less
1400 frequently and can be more involved. */
njn25e49d8e72002-09-23 09:36:25 +00001401extern Bool SK_(cheap_sanity_check) ( void );
1402extern Bool SK_(expensive_sanity_check) ( void );
1403
1404
1405#endif /* NDEF __VG_SKIN_H */
1406
1407/*--------------------------------------------------------------------*/
1408/*--- end vg_skin.h ---*/
1409/*--------------------------------------------------------------------*/
1410