blob: ff589f1c83881a79e26e66017335f42af217afda [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"), \
njn37cea302002-09-30 11:24:00 +0000183 VGP_PAIR(VgpCoreCheapSanity, "core-cheap-sanity"), \
184 VGP_PAIR(VgpCoreExpensiveSanity, "core-expensive-sanity"), \
njn25e49d8e72002-09-23 09:36:25 +0000185 /* These ones depend on the skin */ \
186 VGP_PAIR(VgpPreCloInit, "pre-clo-init"), \
187 VGP_PAIR(VgpPostCloInit, "post-clo-init"), \
188 VGP_PAIR(VgpInstrument, "instrument"), \
189 VGP_PAIR(VgpSkinSysWrap, "skin-syscall-wrapper"), \
njn37cea302002-09-30 11:24:00 +0000190 VGP_PAIR(VgpSkinCheapSanity, "skin-cheap-sanity"), \
191 VGP_PAIR(VgpSkinExpensiveSanity, "skin-expensive-sanity"), \
njn25e49d8e72002-09-23 09:36:25 +0000192 VGP_PAIR(VgpFini, "fini")
193
194#define VGP_PAIR(n,name) n
195typedef enum { VGP_CORE_LIST } VgpCoreCC;
196#undef VGP_PAIR
197
198/* When registering skin profiling events, ensure that the 'n' value is in
199 * the range (VgpFini+1..) */
200extern void VGP_(register_profile_event) ( Int n, Char* name );
201
202extern void VGP_(pushcc) ( UInt cc );
203extern void VGP_(popcc) ( UInt cc );
204
205/* Define them only if they haven't already been defined by vg_profile.c */
206#ifndef VGP_PUSHCC
207# define VGP_PUSHCC(x)
208#endif
209#ifndef VGP_POPCC
210# define VGP_POPCC(x)
211#endif
212
213
214/*====================================================================*/
215/*=== Useful stuff to call from generated code ===*/
216/*====================================================================*/
217
218/* ------------------------------------------------------------------ */
219/* General stuff */
220
221/* Get the simulated %esp */
222extern Addr VG_(get_stack_pointer) ( void );
223
njnd5bb0a52002-09-27 10:24:48 +0000224/* Detect if an address is within Valgrind's stack or Valgrind's
225 m_state_static; useful for memory leak detectors to tell if a block
226 is used by Valgrind (and thus can be ignored). */
njn25e49d8e72002-09-23 09:36:25 +0000227extern Bool VG_(within_stack)(Addr a);
njn25e49d8e72002-09-23 09:36:25 +0000228extern Bool VG_(within_m_state_static)(Addr a);
229
230/* Check if an address is 4-byte aligned */
231#define IS_ALIGNED4_ADDR(aaa_p) (0 == (((UInt)(aaa_p)) & 3))
232
233
234/* ------------------------------------------------------------------ */
235/* Thread-related stuff */
236
237/* Special magic value for an invalid ThreadId. It corresponds to
238 LinuxThreads using zero as the initial value for
239 pthread_mutex_t.__m_owner and pthread_cond_t.__c_waiting. */
240#define VG_INVALID_THREADID ((ThreadId)(0))
241
242/* ThreadIds are simply indices into the vg_threads[] array. */
243typedef
244 UInt
245 ThreadId;
246
njnd5bb0a52002-09-27 10:24:48 +0000247/* struct _ThreadState defined elsewhere; ThreadState is abstract as its
248 definition is not important for skins. */
njn25e49d8e72002-09-23 09:36:25 +0000249typedef
250 struct _ThreadState
251 ThreadState;
252
253extern ThreadId VG_(get_current_tid_1_if_root) ( void );
254extern ThreadState* VG_(get_ThreadState) ( ThreadId tid );
255
256
257/*====================================================================*/
258/*=== Valgrind's version of libc ===*/
259/*====================================================================*/
260
261/* Valgrind doesn't use libc at all, for good reasons (trust us). So here
262 are its own versions of C library functions, but with VG_ prefixes. Note
263 that the types of some are slightly different to the real ones. Some
264 extra useful functions are provided too; descriptions of how they work
265 are given below. */
266
267#if !defined(NULL)
268# define NULL ((void*)0)
269#endif
270
271
272/* ------------------------------------------------------------------ */
273/* stdio.h
274 *
275 * Note that they all output to the file descriptor given by the
276 * --logfile-fd=N argument, which defaults to 2 (stderr). Hence no
277 * need for VG_(fprintf)().
njn25e49d8e72002-09-23 09:36:25 +0000278 */
279extern void VG_(printf) ( const char *format, ... );
280/* too noisy ... __attribute__ ((format (printf, 1, 2))) ; */
281extern void VG_(sprintf) ( Char* buf, Char *format, ... );
282extern void VG_(vprintf) ( void(*send)(Char),
283 const Char *format, va_list vargs );
284
285/* ------------------------------------------------------------------ */
286/* stdlib.h */
287
288extern void* VG_(malloc) ( Int nbytes );
njnd5bb0a52002-09-27 10:24:48 +0000289extern void VG_(free) ( void* p );
290extern void* VG_(calloc) ( Int n, Int nbytes );
291extern void* VG_(realloc) ( void* p, Int size );
292extern void* VG_(malloc_aligned) ( Int align_bytes, Int nbytes );
njn25e49d8e72002-09-23 09:36:25 +0000293
294extern void VG_(print_malloc_stats) ( void );
295
296
297extern void VG_(exit)( Int status )
298 __attribute__ ((__noreturn__));
njne427a662002-10-02 11:08:25 +0000299/* Prints a panic message (a constant string), appends newline and bug
300 reporting info, aborts. */
301__attribute__ ((__noreturn__))
302extern void VG_(skin_panic) ( Char* str );
njn25e49d8e72002-09-23 09:36:25 +0000303
njnd5bb0a52002-09-27 10:24:48 +0000304/* Looks up VG_(client_envp) */
njn25e49d8e72002-09-23 09:36:25 +0000305extern Char* VG_(getenv) ( Char* name );
306
307/* Crude stand-in for the glibc system() call. */
308extern Int VG_(system) ( Char* cmd );
309
njnd5bb0a52002-09-27 10:24:48 +0000310extern Long VG_(atoll) ( Char* str );
njn25e49d8e72002-09-23 09:36:25 +0000311
312/* Like atoll(), but converts a number of base 2..36 */
313extern Long VG_(atoll36) ( UInt base, Char* str );
314
315
316/* ------------------------------------------------------------------ */
njnd5bb0a52002-09-27 10:24:48 +0000317/* ctype.h */
njn25e49d8e72002-09-23 09:36:25 +0000318extern Bool VG_(isspace) ( Char c );
319extern Bool VG_(isdigit) ( Char c );
320extern Char VG_(toupper) ( Char c );
321
322
323/* ------------------------------------------------------------------ */
324/* string.h */
325extern Int VG_(strlen) ( const Char* str );
326extern Char* VG_(strcat) ( Char* dest, const Char* src );
327extern Char* VG_(strncat) ( Char* dest, const Char* src, Int n );
328extern Char* VG_(strpbrk) ( const Char* s, const Char* accept );
329extern Char* VG_(strcpy) ( Char* dest, const Char* src );
330extern Char* VG_(strncpy) ( Char* dest, const Char* src, Int ndest );
331extern Int VG_(strcmp) ( const Char* s1, const Char* s2 );
332extern Int VG_(strncmp) ( const Char* s1, const Char* s2, Int nmax );
333extern Char* VG_(strstr) ( const Char* haystack, Char* needle );
334extern Char* VG_(strchr) ( const Char* s, Char c );
335extern Char* VG_(strdup) ( const Char* s);
336
njnd5bb0a52002-09-27 10:24:48 +0000337/* Like strcmp() and strncmp(), but stop comparing at any whitespace. */
njn25e49d8e72002-09-23 09:36:25 +0000338extern Int VG_(strcmp_ws) ( const Char* s1, const Char* s2 );
njn25e49d8e72002-09-23 09:36:25 +0000339extern Int VG_(strncmp_ws) ( const Char* s1, const Char* s2, Int nmax );
340
njnd5bb0a52002-09-27 10:24:48 +0000341/* Like strncpy(), but if 'src' is longer than 'ndest' inserts a '\0' as the
342 last character. */
njn25e49d8e72002-09-23 09:36:25 +0000343extern void VG_(strncpy_safely) ( Char* dest, const Char* src, Int ndest );
344
345/* Mini-regexp function. Searches for 'pat' in 'str'. Supports
346 * meta-symbols '*' and '?'. '\' escapes meta-symbols. */
njn4ba5a792002-09-30 10:23:54 +0000347extern Bool VG_(string_match) ( Char* pat, Char* str );
njn25e49d8e72002-09-23 09:36:25 +0000348
349
350/* ------------------------------------------------------------------ */
351/* math.h */
njnd5bb0a52002-09-27 10:24:48 +0000352/* Returns the base-2 logarithm of x. */
njn25e49d8e72002-09-23 09:36:25 +0000353extern Int VG_(log2) ( Int x );
354
355
356/* ------------------------------------------------------------------ */
njnd5bb0a52002-09-27 10:24:48 +0000357/* unistd.h, fcntl.h, sys/stat.h */
358extern Int VG_(getpid) ( void );
359
360extern Int VG_(open) ( const Char* pathname, Int flags, Int mode );
361extern Int VG_(read) ( Int fd, void* buf, Int count);
362extern Int VG_(write) ( Int fd, void* buf, Int count);
363extern void VG_(close) ( Int fd );
364
365extern Int VG_(stat) ( Char* file_name, struct vki_stat* buf );
njn25e49d8e72002-09-23 09:36:25 +0000366
367
368/* ------------------------------------------------------------------ */
369/* assert.h */
370/* Asserts permanently enabled -- no turning off with NDEBUG. Hurrah! */
371#define VG__STRING(__str) #__str
372
njne427a662002-10-02 11:08:25 +0000373#define sk_assert(expr) \
njn25e49d8e72002-09-23 09:36:25 +0000374 ((void) ((expr) ? 0 : \
njne427a662002-10-02 11:08:25 +0000375 (VG_(skin_assert_fail) (VG__STRING(expr), \
376 __FILE__, __LINE__, \
377 __PRETTY_FUNCTION__), 0)))
njn25e49d8e72002-09-23 09:36:25 +0000378
njne427a662002-10-02 11:08:25 +0000379__attribute__ ((__noreturn__))
380extern void VG_(skin_assert_fail) ( Char* expr, Char* file,
381 Int line, Char* fn );
njn25e49d8e72002-09-23 09:36:25 +0000382
383
384/* ------------------------------------------------------------------ */
njnd5bb0a52002-09-27 10:24:48 +0000385/* system/mman.h */
njn25e49d8e72002-09-23 09:36:25 +0000386extern void* VG_(mmap)( void* start, UInt length,
387 UInt prot, UInt flags, UInt fd, UInt offset );
388extern Int VG_(munmap)( void* start, Int length );
389
390/* Get memory by anonymous mmap. */
391extern void* VG_(get_memory_from_mmap) ( Int nBytes, Char* who );
392
393
394/* ------------------------------------------------------------------ */
395/* signal.h.
396
397 Note that these use the vk_ (kernel) structure
398 definitions, which are different in places from those that glibc
399 defines -- hence the 'k' prefix. Since we're operating right at the
400 kernel interface, glibc's view of the world is entirely irrelevant. */
401
402/* --- Signal set ops --- */
njnd5bb0a52002-09-27 10:24:48 +0000403extern Int VG_(ksigfillset) ( vki_ksigset_t* set );
404extern Int VG_(ksigemptyset) ( vki_ksigset_t* set );
njn25e49d8e72002-09-23 09:36:25 +0000405
njnd5bb0a52002-09-27 10:24:48 +0000406extern Bool VG_(kisfullsigset) ( vki_ksigset_t* set );
407extern Bool VG_(kisemptysigset) ( vki_ksigset_t* set );
njn25e49d8e72002-09-23 09:36:25 +0000408
njnd5bb0a52002-09-27 10:24:48 +0000409extern Int VG_(ksigaddset) ( vki_ksigset_t* set, Int signum );
410extern Int VG_(ksigdelset) ( vki_ksigset_t* set, Int signum );
njn25e49d8e72002-09-23 09:36:25 +0000411extern Int VG_(ksigismember) ( vki_ksigset_t* set, Int signum );
412
njnd5bb0a52002-09-27 10:24:48 +0000413extern void VG_(ksigaddset_from_set) ( vki_ksigset_t* dst, vki_ksigset_t* src );
414extern void VG_(ksigdelset_from_set) ( vki_ksigset_t* dst, vki_ksigset_t* src );
njn25e49d8e72002-09-23 09:36:25 +0000415
416/* --- Mess with the kernel's sig state --- */
njnd5bb0a52002-09-27 10:24:48 +0000417extern Int VG_(ksigprocmask) ( Int how, const vki_ksigset_t* set,
njn25e49d8e72002-09-23 09:36:25 +0000418 vki_ksigset_t* oldset );
njnd5bb0a52002-09-27 10:24:48 +0000419extern Int VG_(ksigaction) ( Int signum,
420 const vki_ksigaction* act,
421 vki_ksigaction* oldact );
njn25e49d8e72002-09-23 09:36:25 +0000422
njnd5bb0a52002-09-27 10:24:48 +0000423extern Int VG_(ksignal) ( Int signum, void (*sighandler)(Int) );
424extern Int VG_(ksigaltstack) ( const vki_kstack_t* ss, vki_kstack_t* oss );
njn25e49d8e72002-09-23 09:36:25 +0000425
sewardjdcaf3122002-09-30 23:12:33 +0000426extern Int VG_(kkill) ( Int pid, Int signo );
427extern Int VG_(ksigpending) ( vki_ksigset_t* set );
njn25e49d8e72002-09-23 09:36:25 +0000428
429
430/*====================================================================*/
431/*=== UCode definition ===*/
432/*====================================================================*/
433
sewardje1042472002-09-30 12:33:11 +0000434/* Tags which describe what operands are. Must fit into 4 bits, which
435 they clearly do. */
njn25e49d8e72002-09-23 09:36:25 +0000436typedef
sewardje1042472002-09-30 12:33:11 +0000437enum { TempReg =0, /* virtual temp-reg */
438 ArchReg =1, /* simulated integer reg */
439 ArchRegS =2, /* simulated segment reg */
440 RealReg =3, /* real machine's real reg */
441 SpillNo =4, /* spill slot location */
442 Literal =5, /* literal; .lit32 field has actual value */
443 Lit16 =6, /* literal; .val[123] field has actual value */
444 NoValue =7 /* operand not in use */
445 }
446 Tag;
njn25e49d8e72002-09-23 09:36:25 +0000447
njnd5bb0a52002-09-27 10:24:48 +0000448/* Invalid register numbers (can't be negative) */
njn25e49d8e72002-09-23 09:36:25 +0000449#define INVALID_TEMPREG 999999999
450#define INVALID_REALREG 999999999
451
452/* Microinstruction opcodes. */
453typedef
454 enum {
njnd5bb0a52002-09-27 10:24:48 +0000455 NOP, /* Null op */
njn25e49d8e72002-09-23 09:36:25 +0000456
njnd5bb0a52002-09-27 10:24:48 +0000457 /* Moving values around */
458 GET, PUT, /* simulated register <--> TempReg */
459 GETF, PUTF, /* simulated %eflags <--> TempReg */
460 LOAD, STORE, /* memory <--> TempReg */
461 MOV, /* TempReg <--> TempReg */
462 CMOV, /* Used for cmpxchg and cmov */
njn25e49d8e72002-09-23 09:36:25 +0000463
njnd5bb0a52002-09-27 10:24:48 +0000464 /* Arithmetic/logical ops */
465 ADD, ADC, SUB, SBB, /* Add/subtract (w/wo carry) */
466 AND, OR, XOR, NOT, /* Boolean ops */
467 SHL, SHR, SAR, ROL, ROR, RCL, RCR, /* Shift/rotate (w/wo carry) */
468 NEG, /* Negate */
469 INC, DEC, /* Increment/decrement */
470 BSWAP, /* Big-endian <--> little-endian */
471 CC2VAL, /* Condition code --> 0 or 1 */
472 WIDEN, /* Signed or unsigned widening */
njn25e49d8e72002-09-23 09:36:25 +0000473
njnd5bb0a52002-09-27 10:24:48 +0000474 /* Conditional or unconditional jump */
475 JMP,
476
477 /* FPU ops */
478 FPU, /* Doesn't touch memory */
479 FPU_R, FPU_W, /* Reads/writes memory */
480
481 /* Not strictly needed, but improve address calculation translations. */
njn25e49d8e72002-09-23 09:36:25 +0000482 LEA1, /* reg2 := const + reg1 */
483 LEA2, /* reg3 := const + reg1 + reg2 * 1,2,4 or 8 */
484
njnd5bb0a52002-09-27 10:24:48 +0000485 /* Hack for x86 REP insns. Jump to literal if TempReg/RealReg is zero. */
486 JIFZ,
njn25e49d8e72002-09-23 09:36:25 +0000487
njnd5bb0a52002-09-27 10:24:48 +0000488 /* Advance the simulated %eip by some small (< 128) number. */
489 INCEIP,
njn25e49d8e72002-09-23 09:36:25 +0000490
sewardje1042472002-09-30 12:33:11 +0000491 /* Dealing with segment registers */
492 GETSEG, PUTSEG, /* simulated segment register <--> TempReg */
493 USESEG, /* (LDT/GDT index, virtual addr) --> linear addr */
494
njnd5bb0a52002-09-27 10:24:48 +0000495 /* Not for translating x86 calls -- only to call helpers */
496 CALLM_S, CALLM_E, /* Mark start/end of CALLM push/pop sequence */
497 PUSH, POP, CLEAR, /* Add/remove/zap args for helpers */
498 CALLM, /* Call assembly-code helper */
499
500 /* Not for translating x86 calls -- only to call C helper functions of
501 up to three arguments (or two if the functions has a return value).
502 Arguments and return value must be word-sized. More arguments can
503 be faked with global variables (eg. use VG_(set_global_var)()).
504
505 Seven possibilities: 'arg[123]' show where args go, 'ret' shows
506 where return value goes (if present).
njn25e49d8e72002-09-23 09:36:25 +0000507
508 CCALL(-, -, - ) void f(void)
509 CCALL(arg1, -, - ) void f(UInt arg1)
510 CCALL(arg1, arg2, - ) void f(UInt arg1, UInt arg2)
511 CCALL(arg1, arg2, arg3) void f(UInt arg1, UInt arg2, UInt arg3)
512 CCALL(-, -, ret ) UInt f(UInt)
513 CCALL(arg1, -, ret ) UInt f(UInt arg1)
njnd5bb0a52002-09-27 10:24:48 +0000514 CCALL(arg1, arg2, ret ) UInt f(UInt arg1, UInt arg2) */
njn25e49d8e72002-09-23 09:36:25 +0000515 CCALL,
516
njnd5bb0a52002-09-27 10:24:48 +0000517 /* This opcode makes it easy for skins that extend UCode to do this to
518 avoid opcode overlap:
njn25e49d8e72002-09-23 09:36:25 +0000519
njnd5bb0a52002-09-27 10:24:48 +0000520 enum { EU_OP1 = DUMMY_FINAL_UOPCODE + 1, ... }
njn25e49d8e72002-09-23 09:36:25 +0000521
522 WARNING: Do not add new opcodes after this one! They can be added
523 before, though. */
524 DUMMY_FINAL_UOPCODE
525 }
526 Opcode;
527
528
njnd5bb0a52002-09-27 10:24:48 +0000529/* Condition codes, using the Intel encoding. CondAlways is an extra. */
njn25e49d8e72002-09-23 09:36:25 +0000530typedef
531 enum {
532 CondO = 0, /* overflow */
533 CondNO = 1, /* no overflow */
534 CondB = 2, /* below */
535 CondNB = 3, /* not below */
536 CondZ = 4, /* zero */
537 CondNZ = 5, /* not zero */
538 CondBE = 6, /* below or equal */
539 CondNBE = 7, /* not below or equal */
540 CondS = 8, /* negative */
sewardje1042472002-09-30 12:33:11 +0000541 CondNS = 9, /* not negative */
njn25e49d8e72002-09-23 09:36:25 +0000542 CondP = 10, /* parity even */
543 CondNP = 11, /* not parity even */
544 CondL = 12, /* jump less */
545 CondNL = 13, /* not less */
546 CondLE = 14, /* less or equal */
547 CondNLE = 15, /* not less or equal */
548 CondAlways = 16 /* Jump always */
549 }
550 Condcode;
551
552
553/* Descriptions of additional properties of *unconditional* jumps. */
554typedef
555 enum {
556 JmpBoring=0, /* boring unconditional jump */
557 JmpCall=1, /* jump due to an x86 call insn */
558 JmpRet=2, /* jump due to an x86 ret insn */
559 JmpSyscall=3, /* do a system call, then jump */
560 JmpClientReq=4 /* do a client request, then jump */
561 }
562 JmpKind;
563
564
565/* Flags. User-level code can only read/write O(verflow), S(ign),
566 Z(ero), A(ux-carry), C(arry), P(arity), and may also write
567 D(irection). That's a total of 7 flags. A FlagSet is a bitset,
568 thusly:
569 76543210
570 DOSZACP
571 and bit 7 must always be zero since it is unused.
572*/
573typedef UChar FlagSet;
574
575#define FlagD (1<<6)
576#define FlagO (1<<5)
577#define FlagS (1<<4)
578#define FlagZ (1<<3)
579#define FlagA (1<<2)
580#define FlagC (1<<1)
581#define FlagP (1<<0)
582
583#define FlagsOSZACP (FlagO | FlagS | FlagZ | FlagA | FlagC | FlagP)
584#define FlagsOSZAP (FlagO | FlagS | FlagZ | FlagA | FlagP)
585#define FlagsOSZCP (FlagO | FlagS | FlagZ | FlagC | FlagP)
586#define FlagsOSACP (FlagO | FlagS | FlagA | FlagC | FlagP)
587#define FlagsSZACP ( FlagS | FlagZ | FlagA | FlagC | FlagP)
588#define FlagsSZAP ( FlagS | FlagZ | FlagA | FlagP)
589#define FlagsZCP ( FlagZ | FlagC | FlagP)
590#define FlagsOC (FlagO | FlagC )
591#define FlagsAC ( FlagA | FlagC )
592
593#define FlagsALL (FlagsOSZACP | FlagD)
594#define FlagsEmpty (FlagSet)0
595
596
597/* Liveness of general purpose registers, useful for code generation.
598 Reg rank order 0..N-1 corresponds to bits 0..N-1, ie. first
599 reg's liveness in bit 0, last reg's in bit N-1. Note that
600 these rankings don't match the Intel register ordering. */
601typedef UInt RRegSet;
602
603#define ALL_RREGS_DEAD 0 /* 0000...00b */
604#define ALL_RREGS_LIVE (1 << (VG_MAX_REALREGS-1)) /* 0011...11b */
605#define UNIT_RREGSET(rank) (1 << (rank))
606
607#define IS_RREG_LIVE(rank,rregs_live) (rregs_live & UNIT_RREGSET(rank))
608#define SET_RREG_LIVENESS(rank,rregs_live,b) \
609 do { RRegSet unit = UNIT_RREGSET(rank); \
610 if (b) rregs_live |= unit; \
611 else rregs_live &= ~unit; \
612 } while(0)
613
614
615/* A Micro (u)-instruction. */
616typedef
617 struct {
618 /* word 1 */
619 UInt lit32; /* 32-bit literal */
620
621 /* word 2 */
622 UShort val1; /* first operand */
623 UShort val2; /* second operand */
624
625 /* word 3 */
626 UShort val3; /* third operand */
627 UChar opcode; /* opcode */
628 UChar size; /* data transfer size */
629
630 /* word 4 */
631 FlagSet flags_r; /* :: FlagSet */
632 FlagSet flags_w; /* :: FlagSet */
633 UChar tag1:4; /* first operand tag */
634 UChar tag2:4; /* second operand tag */
635 UChar tag3:4; /* third operand tag */
636 UChar extra4b:4; /* Spare field, used by WIDEN for src
637 -size, and by LEA2 for scale (1,2,4 or 8),
638 and by JMPs for original x86 instr size */
639
640 /* word 5 */
641 UChar cond; /* condition, for jumps */
642 Bool signed_widen:1; /* signed or unsigned WIDEN ? */
643 JmpKind jmpkind:3; /* additional properties of unconditional JMP */
644
645 /* Additional properties for UInstrs that call C functions:
646 - CCALL
647 - PUT (when %ESP is the target)
648 - possibly skin-specific UInstrs
649 */
650 UChar argc:2; /* Number of args, max 3 */
651 UChar regparms_n:2; /* Number of args passed in registers */
652 Bool has_ret_val:1; /* Function has return value? */
653
654 /* RealReg liveness; only sensical after reg alloc and liveness
655 analysis done. This info is a little bit arch-specific --
656 VG_MAX_REALREGS can vary on different architectures. Note that
657 to use this information requires converting between register ranks
njn4ba5a792002-09-30 10:23:54 +0000658 and the Intel register numbers, using VG_(realreg_to_rank)()
659 and/or VG_(rank_to_realreg)() */
njn25e49d8e72002-09-23 09:36:25 +0000660 RRegSet regs_live_after:VG_MAX_REALREGS;
661 }
662 UInstr;
663
664
665/* Expandable arrays of uinstrs. */
666typedef
667 struct {
668 Int used;
669 Int size;
670 UInstr* instrs;
671 Int nextTemp;
672 }
673 UCodeBlock;
674
675
676/*====================================================================*/
677/*=== Instrumenting UCode ===*/
678/*====================================================================*/
679
680/* A structure for communicating TempReg and RealReg uses of UInstrs. */
681typedef
682 struct {
683 Int num;
684 Bool isWrite;
685 }
686 RegUse;
687
688/* Find what this instruction does to its regs. Tag indicates whether we're
689 * considering TempRegs (pre-reg-alloc) or RealRegs (post-reg-alloc).
690 * Useful for analysis/optimisation passes. */
njn4ba5a792002-09-30 10:23:54 +0000691extern Int VG_(get_reg_usage) ( UInstr* u, Tag tag, RegUse* arr );
njn25e49d8e72002-09-23 09:36:25 +0000692
693
694/* ------------------------------------------------------------------ */
njnd5bb0a52002-09-27 10:24:48 +0000695/* Used to register helper functions to be called from generated code. A
696 limited number of compact helpers can be registered; the code generated
697 to call them is slightly shorter -- so register the mostly frequently
698 called helpers as compact. */
njn25e49d8e72002-09-23 09:36:25 +0000699extern void VG_(register_compact_helper) ( Addr a );
700extern void VG_(register_noncompact_helper) ( Addr a );
701
702
703/* ------------------------------------------------------------------ */
704/* Virtual register allocation */
705
706/* Get a new virtual register */
njn4ba5a792002-09-30 10:23:54 +0000707extern Int VG_(get_new_temp) ( UCodeBlock* cb );
njn25e49d8e72002-09-23 09:36:25 +0000708
709/* Get a new virtual shadow register */
njn4ba5a792002-09-30 10:23:54 +0000710extern Int VG_(get_new_shadow) ( UCodeBlock* cb );
njn25e49d8e72002-09-23 09:36:25 +0000711
712/* Get a virtual register's corresponding virtual shadow register */
713#define SHADOW(tempreg) ((tempreg)+1)
714
715
716/* ------------------------------------------------------------------ */
717/* Low-level UInstr builders */
njn4ba5a792002-09-30 10:23:54 +0000718extern void VG_(new_NOP) ( UInstr* u );
719extern void VG_(new_UInstr0) ( UCodeBlock* cb, Opcode opcode, Int sz );
720extern void VG_(new_UInstr1) ( UCodeBlock* cb, Opcode opcode, Int sz,
njn25e49d8e72002-09-23 09:36:25 +0000721 Tag tag1, UInt val1 );
njn4ba5a792002-09-30 10:23:54 +0000722extern void VG_(new_UInstr2) ( UCodeBlock* cb, Opcode opcode, Int sz,
njn25e49d8e72002-09-23 09:36:25 +0000723 Tag tag1, UInt val1,
724 Tag tag2, UInt val2 );
njn4ba5a792002-09-30 10:23:54 +0000725extern void VG_(new_UInstr3) ( UCodeBlock* cb, Opcode opcode, Int sz,
njn25e49d8e72002-09-23 09:36:25 +0000726 Tag tag1, UInt val1,
727 Tag tag2, UInt val2,
728 Tag tag3, UInt val3 );
njn25e49d8e72002-09-23 09:36:25 +0000729
njn4ba5a792002-09-30 10:23:54 +0000730extern void VG_(set_flag_RW) ( UInstr* u, FlagSet fr, FlagSet fw );
731extern void VG_(set_lit_field) ( UCodeBlock* cb, UInt lit32 );
732extern void VG_(set_ccall_fields) ( UCodeBlock* cb, Addr fn, UChar argc,
733 UChar regparms_n, Bool has_ret_val );
njn25e49d8e72002-09-23 09:36:25 +0000734
njn4ba5a792002-09-30 10:23:54 +0000735extern void VG_(copy_UInstr) ( UCodeBlock* cb, UInstr* instr );
736
737extern Bool VG_(any_flag_use)( UInstr* u );
njn25e49d8e72002-09-23 09:36:25 +0000738
739/* Refer to `the last instruction stuffed in' (can be lvalue). */
740#define LAST_UINSTR(cb) (cb)->instrs[(cb)->used-1]
741
742
743/* ------------------------------------------------------------------ */
744/* Higher-level UInstr sequence builders */
njn4ba5a792002-09-30 10:23:54 +0000745extern void VG_(call_helper_0_0) ( UCodeBlock* cb, Addr f);
746extern void VG_(call_helper_1_0) ( UCodeBlock* cb, Addr f, UInt arg1,
747 UInt regparms_n);
748extern void VG_(call_helper_2_0) ( UCodeBlock* cb, Addr f, UInt arg1, UInt arg2,
749 UInt regparms_n);
njn25e49d8e72002-09-23 09:36:25 +0000750
751/* One way around the 3-arg C function limit is to pass args via global
752 * variables... ugly, but it works. */
njn4ba5a792002-09-30 10:23:54 +0000753extern void VG_(set_global_var) ( UCodeBlock* cb, Addr globvar_ptr, UInt val);
njn25e49d8e72002-09-23 09:36:25 +0000754
755/* ------------------------------------------------------------------ */
njnd5bb0a52002-09-27 10:24:48 +0000756/* Allocating/freeing basic blocks of UCode */
njn4ba5a792002-09-30 10:23:54 +0000757extern UCodeBlock* VG_(alloc_UCodeBlock) ( void );
758extern void VG_(free_UCodeBlock) ( UCodeBlock* cb );
njnd5bb0a52002-09-27 10:24:48 +0000759
760/* ------------------------------------------------------------------ */
761/* UCode pretty/ugly printing. Probably only useful to call from a skin
njn25e49d8e72002-09-23 09:36:25 +0000762 if VG_(needs).extended_UCode == True. */
763
764/* When True, all generated code is/should be printed. */
765extern Bool VG_(print_codegen);
766
njn4ba5a792002-09-30 10:23:54 +0000767/* Pretty/ugly printing functions */
768extern void VG_(pp_UCodeBlock) ( UCodeBlock* cb, Char* title );
769extern void VG_(pp_UInstr) ( Int instrNo, UInstr* u );
770extern void VG_(pp_UInstr_regs) ( Int instrNo, UInstr* u );
771extern void VG_(up_UInstr) ( Int instrNo, UInstr* u );
772extern Char* VG_(name_UOpcode) ( Bool upper, Opcode opc );
773extern void VG_(pp_UOperand) ( UInstr* u, Int operandNo,
774 Int sz, Bool parens );
njn25e49d8e72002-09-23 09:36:25 +0000775
njn25e49d8e72002-09-23 09:36:25 +0000776
777/*====================================================================*/
njnd5bb0a52002-09-27 10:24:48 +0000778/*=== Generating x86 code from UCode ===*/
njn25e49d8e72002-09-23 09:36:25 +0000779/*====================================================================*/
780
njnd5bb0a52002-09-27 10:24:48 +0000781/* All this only necessary for skins with VG_(needs).extends_UCode == True. */
njn25e49d8e72002-09-23 09:36:25 +0000782
sewardje1042472002-09-30 12:33:11 +0000783/* This is the Intel register encoding -- integer regs. */
njn25e49d8e72002-09-23 09:36:25 +0000784#define R_EAX 0
785#define R_ECX 1
786#define R_EDX 2
787#define R_EBX 3
788#define R_ESP 4
789#define R_EBP 5
790#define R_ESI 6
791#define R_EDI 7
792
793#define R_AL (0+R_EAX)
794#define R_CL (0+R_ECX)
795#define R_DL (0+R_EDX)
796#define R_BL (0+R_EBX)
797#define R_AH (4+R_EAX)
798#define R_CH (4+R_ECX)
799#define R_DH (4+R_EDX)
800#define R_BH (4+R_EBX)
801
sewardje1042472002-09-30 12:33:11 +0000802/* This is the Intel register encoding -- segment regs. */
803#define R_ES 0
804#define R_CS 1
805#define R_SS 2
806#define R_DS 3
807#define R_FS 4
808#define R_GS 5
809
njn25e49d8e72002-09-23 09:36:25 +0000810/* For pretty printing x86 code */
sewardje1042472002-09-30 12:33:11 +0000811extern Char* VG_(name_of_seg_reg) ( Int sreg );
njn4ba5a792002-09-30 10:23:54 +0000812extern Char* VG_(name_of_int_reg) ( Int size, Int reg );
813extern Char VG_(name_of_int_size) ( Int size );
njn25e49d8e72002-09-23 09:36:25 +0000814
815/* Randomly useful things */
816extern UInt VG_(extend_s_8to32) ( UInt x );
817
818/* Code emitters */
njn4ba5a792002-09-30 10:23:54 +0000819extern void VG_(emitB) ( UInt b );
820extern void VG_(emitW) ( UInt w );
821extern void VG_(emitL) ( UInt l );
822extern void VG_(new_emit) ( void );
njn25e49d8e72002-09-23 09:36:25 +0000823
824/* Finding offsets */
njn4ba5a792002-09-30 10:23:54 +0000825extern Int VG_(helper_offset) ( Addr a );
826extern Int VG_(shadow_reg_offset) ( Int arch );
827extern Int VG_(shadow_flags_offset) ( void );
njn25e49d8e72002-09-23 09:36:25 +0000828
njnd5bb0a52002-09-27 10:24:48 +0000829/* Convert reg ranks <-> Intel register ordering, for using register
830 liveness information. */
njn4ba5a792002-09-30 10:23:54 +0000831extern Int VG_(realreg_to_rank) ( Int realreg );
832extern Int VG_(rank_to_realreg) ( Int rank );
njn25e49d8e72002-09-23 09:36:25 +0000833
njnd5bb0a52002-09-27 10:24:48 +0000834/* Call a subroutine. Does no argument passing, stack manipulations, etc. */
njn4ba5a792002-09-30 10:23:54 +0000835extern void VG_(synth_call) ( Bool ensure_shortform, Int word_offset );
njn25e49d8e72002-09-23 09:36:25 +0000836
njnd5bb0a52002-09-27 10:24:48 +0000837/* For calling C functions -- saves caller save regs, pushes args, calls,
838 clears the stack, restores caller save regs. `fn' must be registered in
839 the baseBlock first. Acceptable tags are RealReg and Literal. Optimises
840 things, eg. by not preserving non-live caller-save registers.
njn25e49d8e72002-09-23 09:36:25 +0000841
njnd5bb0a52002-09-27 10:24:48 +0000842 WARNING: a UInstr should *not* be translated with synth_ccall() followed
843 by some other x86 assembly code; this will invalidate the results of
844 vg_realreg_liveness_analysis() and everything will fall over. */
njn4ba5a792002-09-30 10:23:54 +0000845extern void VG_(synth_ccall) ( Addr fn, Int argc, Int regparms_n, UInt argv[],
846 Tag tagv[], Int ret_reg,
847 RRegSet regs_live_before,
848 RRegSet regs_live_after );
njn25e49d8e72002-09-23 09:36:25 +0000849
850/* Addressing modes */
njn4ba5a792002-09-30 10:23:54 +0000851extern void VG_(emit_amode_offregmem_reg)( Int off, Int regmem, Int reg );
852extern void VG_(emit_amode_ereg_greg) ( Int e_reg, Int g_reg );
njn25e49d8e72002-09-23 09:36:25 +0000853
854/* v-size (4, or 2 with OSO) insn emitters */
njn4ba5a792002-09-30 10:23:54 +0000855extern void VG_(emit_movv_offregmem_reg) ( Int sz, Int off, Int areg, Int reg );
856extern void VG_(emit_movv_reg_offregmem) ( Int sz, Int reg, Int off, Int areg );
857extern void VG_(emit_movv_reg_reg) ( Int sz, Int reg1, Int reg2 );
858extern void VG_(emit_nonshiftopv_lit_reg)( Int sz, Opcode opc, UInt lit,
859 Int reg );
860extern void VG_(emit_shiftopv_lit_reg) ( Int sz, Opcode opc, UInt lit,
861 Int reg );
862extern void VG_(emit_nonshiftopv_reg_reg)( Int sz, Opcode opc,
863 Int reg1, Int reg2 );
864extern void VG_(emit_movv_lit_reg) ( Int sz, UInt lit, Int reg );
865extern void VG_(emit_unaryopv_reg) ( Int sz, Opcode opc, Int reg );
866extern void VG_(emit_pushv_reg) ( Int sz, Int reg );
867extern void VG_(emit_popv_reg) ( Int sz, Int reg );
njn25e49d8e72002-09-23 09:36:25 +0000868
njn4ba5a792002-09-30 10:23:54 +0000869extern void VG_(emit_pushl_lit32) ( UInt int32 );
870extern void VG_(emit_pushl_lit8) ( Int lit8 );
871extern void VG_(emit_cmpl_zero_reg) ( Int reg );
872extern void VG_(emit_swapl_reg_EAX) ( Int reg );
873extern void VG_(emit_movv_lit_offregmem) ( Int sz, UInt lit, Int off,
874 Int memreg );
njn25e49d8e72002-09-23 09:36:25 +0000875
876/* b-size (1 byte) instruction emitters */
njn4ba5a792002-09-30 10:23:54 +0000877extern void VG_(emit_movb_lit_offregmem) ( UInt lit, Int off, Int memreg );
878extern void VG_(emit_movb_reg_offregmem) ( Int reg, Int off, Int areg );
879extern void VG_(emit_unaryopb_reg) ( Opcode opc, Int reg );
880extern void VG_(emit_testb_lit_reg) ( UInt lit, Int reg );
njn25e49d8e72002-09-23 09:36:25 +0000881
882/* zero-extended load emitters */
njn4ba5a792002-09-30 10:23:54 +0000883extern void VG_(emit_movzbl_offregmem_reg) ( Int off, Int regmem, Int reg );
884extern void VG_(emit_movzwl_offregmem_reg) ( Int off, Int areg, Int reg );
njn25e49d8e72002-09-23 09:36:25 +0000885
886/* misc instruction emitters */
njn4ba5a792002-09-30 10:23:54 +0000887extern void VG_(emit_call_reg) ( Int reg );
888extern void VG_(emit_add_lit_to_esp) ( Int lit );
889extern void VG_(emit_jcondshort_delta) ( Condcode cond, Int delta );
890extern void VG_(emit_pushal) ( void );
891extern void VG_(emit_popal) ( void );
892extern void VG_(emit_AMD_prefetch_reg) ( Int reg );
njn25e49d8e72002-09-23 09:36:25 +0000893
894
895/*====================================================================*/
896/*=== Execution contexts ===*/
897/*====================================================================*/
898
899/* Generic resolution type used in a few different ways, such as deciding
900 how closely to compare two errors for equality. */
901typedef
902 enum { Vg_LowRes, Vg_MedRes, Vg_HighRes }
903 VgRes;
904
905typedef
906 struct _ExeContext
907 ExeContext;
908
njnd5bb0a52002-09-27 10:24:48 +0000909/* Compare two ExeContexts. Number of callers considered depends on `res':
910 Vg_LowRes: 2
911 Vg_MedRes: 4
912 Vg_HighRes: all */
njn25e49d8e72002-09-23 09:36:25 +0000913extern Bool VG_(eq_ExeContext) ( VgRes res,
914 ExeContext* e1, ExeContext* e2 );
915
916/* Print an ExeContext. */
917extern void VG_(pp_ExeContext) ( ExeContext* );
918
919/* Take a snapshot of the client's stack. Search our collection of
920 ExeContexts to see if we already have it, and if not, allocate a
921 new one. Either way, return a pointer to the context. */
922extern ExeContext* VG_(get_ExeContext) ( ThreadState *tst );
923
924
925/*====================================================================*/
926/*=== Error reporting ===*/
927/*====================================================================*/
928
929/* ------------------------------------------------------------------ */
930/* Suppressions describe errors which we want to suppress, ie, not
931 show the user, usually because it is caused by a problem in a library
932 which we can't fix, replace or work around. Suppressions are read from
njnd5bb0a52002-09-27 10:24:48 +0000933 a file at startup time. This gives flexibility so that new
njn25e49d8e72002-09-23 09:36:25 +0000934 suppressions can be added to the file as and when needed.
935*/
936
937typedef
938 Int /* Do not make this unsigned! */
939 SuppKind;
940
941/* An extensible (via the 'extra' field) suppression record. This holds
942 the suppression details of interest to a skin. Skins can use a normal
943 enum (with element values in the normal range (0..)) for `skind'.
944
945 If VG_(needs).report_errors==True, for each suppression read in by core
946 SKN_(recognised_suppression)() and SKN_(read_extra_suppression_info) will
947 be called. The `skind' field is filled in by the value returned in the
948 argument of the first function; the second function can fill in the
949 `string' and `extra' fields if it wants.
950*/
951typedef
952 struct {
953 /* What kind of suppression. Must use the range (0..) */
954 SuppKind skind;
955 /* String -- use is optional. NULL by default. */
956 Char* string;
957 /* Anything else -- use is optional. NULL by default. */
958 void* extra;
959 }
960 SkinSupp;
961
962
963/* ------------------------------------------------------------------ */
964/* Error records contain enough info to generate an error report. The idea
965 is that (typically) the same few points in the program generate thousands
njnd5bb0a52002-09-27 10:24:48 +0000966 of errors, and we don't want to spew out a fresh error message for each
967 one. Instead, we use these structures to common up duplicates.
njn25e49d8e72002-09-23 09:36:25 +0000968*/
969
970typedef
971 Int /* Do not make this unsigned! */
972 ErrorKind;
973
974/* An extensible (via the 'extra' field) error record. This holds
975 the error details of interest to a skin. Skins can use a normal
976 enum (with element values in the normal range (0..)) for `ekind'.
977
978 When errors are found and recorded with VG_(maybe_record_error)(), all
979 the skin must do is pass in the four parameters; core will
980 allocate/initialise the error record.
981*/
982typedef
983 struct {
984 /* Used by ALL. Must be in the range (0..) */
985 Int ekind;
986 /* Used frequently */
987 Addr addr;
988 /* Used frequently */
989 Char* string;
njnd5bb0a52002-09-27 10:24:48 +0000990 /* For any skin-specific extras */
njn25e49d8e72002-09-23 09:36:25 +0000991 void* extra;
992 }
993 SkinError;
994
995
996/* ------------------------------------------------------------------ */
njnd5bb0a52002-09-27 10:24:48 +0000997/* Call this when an error occurs. It will be recorded if it hasn't been
njn25e49d8e72002-09-23 09:36:25 +0000998 seen before. If it has, the existing error record will have its count
999 incremented.
1000
1001 If the error occurs in generated code, 'tst' should be NULL. If the
1002 error occurs in non-generated code, 'tst' should be non-NULL. The
1003 `extra' field can be stack-allocated; it will be copied (using
1004 SKN_(dup_extra_and_update)()) if needed. But it won't be copied
1005 if it's NULL.
njnd5bb0a52002-09-27 10:24:48 +00001006
1007 If no 'a', 's' or 'extra' of interest needs to be recorded, just use
1008 NULL for them.
njn25e49d8e72002-09-23 09:36:25 +00001009*/
1010extern void VG_(maybe_record_error) ( ThreadState* tst, ErrorKind ekind,
1011 Addr a, Char* s, void* extra );
1012
1013/* Gets a non-blank, non-comment line of at most nBuf chars from fd.
1014 Skips leading spaces on the line. Returns True if EOF was hit instead.
1015 Useful for reading in extra skin-specific suppression lines.
1016*/
njn4ba5a792002-09-30 10:23:54 +00001017extern Bool VG_(get_line) ( Int fd, Char* buf, Int nBuf );
njn25e49d8e72002-09-23 09:36:25 +00001018
1019
1020/*====================================================================*/
1021/*=== Obtaining debug information ===*/
1022/*====================================================================*/
1023
1024/* Get the file/function/line number of the instruction at address 'a'.
1025 For these four, if debug info for the address is found, it copies the
1026 info into the buffer/UInt and returns True. If not, it returns False and
1027 nothing is copied. VG_(get_fnname) always demangles C++ function names.
1028*/
1029extern Bool VG_(get_filename) ( Addr a, Char* filename, Int n_filename );
1030extern Bool VG_(get_fnname) ( Addr a, Char* fnname, Int n_fnname );
1031extern Bool VG_(get_linenum) ( Addr a, UInt* linenum );
1032
1033/* This one is more efficient if getting both filename and line number,
1034 because the two lookups are done together. */
1035extern Bool VG_(get_filename_linenum)
1036 ( Addr a, Char* filename, Int n_filename,
1037 UInt* linenum );
1038
1039/* Succeeds only if we find from debug info that 'a' is the address of the
1040 first instruction in a function -- as opposed to VG_(get_fnname) which
1041 succeeds if we find from debug info that 'a' is the address of any
1042 instruction in a function. Use this to instrument the start of
njnd5bb0a52002-09-27 10:24:48 +00001043 a particular function. Nb: if an executable/shared object is stripped
njn25e49d8e72002-09-23 09:36:25 +00001044 of its symbols, this function will not be able to recognise function
1045 entry points within it. */
1046extern Bool VG_(get_fnname_if_entry) ( Addr a, Char* filename, Int n_filename );
1047
1048/* Succeeds if the address is within a shared object or the main executable.
1049 It doesn't matter if debug info is present or not. */
1050extern Bool VG_(get_objname) ( Addr a, Char* objname, Int n_objname );
1051
1052
1053/*====================================================================*/
1054/*=== Shadow chunks and block-finding ===*/
1055/*====================================================================*/
1056
1057typedef
1058 enum {
1059 Vg_AllocMalloc = 0,
1060 Vg_AllocNew = 1,
1061 Vg_AllocNewVec = 2
1062 }
1063 VgAllocKind;
1064
1065/* Description of a malloc'd chunk. skin_extra[] part can be used by
1066 the skin; size of array is given by VG_(needs).sizeof_shadow_chunk. */
1067typedef
1068 struct _ShadowChunk {
1069 struct _ShadowChunk* next;
1070 UInt size : 30; /* size requested */
1071 VgAllocKind allockind : 2; /* which wrapper did the allocation */
1072 Addr data; /* ptr to actual block */
1073 UInt skin_extra[0]; /* extra skin-specific info */
1074 }
1075 ShadowChunk;
1076
1077/* Use this to free blocks if VG_(needs).alternative_free == True.
1078 It frees the ShadowChunk and the malloc'd block it points to. */
njn4ba5a792002-09-30 10:23:54 +00001079extern void VG_(free_ShadowChunk) ( ShadowChunk* sc );
njn25e49d8e72002-09-23 09:36:25 +00001080
1081/* Makes an array of pointers to all the shadow chunks of malloc'd blocks */
1082extern ShadowChunk** VG_(get_malloc_shadows) ( /*OUT*/ UInt* n_shadows );
1083
1084/* Determines if address 'a' is within the bounds of the block at start.
1085 Allows a little 'slop' round the edges. */
1086extern Bool VG_(addr_is_in_block) ( Addr a, Addr start, UInt size );
1087
1088/* Searches through currently malloc'd blocks until a matching one is found.
1089 Returns NULL if none match. Extra arguments can be implicitly passed to
njnd5bb0a52002-09-27 10:24:48 +00001090 p using nested functions; see memcheck/mc_errcontext.c for an example. */
njn25e49d8e72002-09-23 09:36:25 +00001091extern ShadowChunk* VG_(any_matching_mallocd_ShadowChunks)
1092 ( Bool (*p) ( ShadowChunk* ));
1093
1094/* Searches through all thread's stacks to see if any match. Returns
1095 * VG_INVALID_THREADID if none match. */
1096extern ThreadId VG_(any_matching_thread_stack)
1097 ( Bool (*p) ( Addr stack_min, Addr stack_max ));
1098
1099/*====================================================================*/
1100/*=== Skin-specific stuff ===*/
1101/*====================================================================*/
1102
1103/* Skin-specific settings.
1104 *
1105 * If new fields are added to this type, update:
njnd5bb0a52002-09-27 10:24:48 +00001106 * - vg_main.c:initialisation of VG_(needs)
njn25e49d8e72002-09-23 09:36:25 +00001107 * - vg_main.c:sanity_check_needs()
1108 *
1109 * If the name of this type or any of its fields change, update:
1110 * - dependent comments (just search for "VG_(needs)").
1111 */
1112typedef
1113 struct {
njne427a662002-10-02 11:08:25 +00001114 /* Name and description used in the startup message. `name' also
njnd5bb0a52002-09-27 10:24:48 +00001115 determines the string used for identifying suppressions in a
njne427a662002-10-02 11:08:25 +00001116 suppression file as belonging to this skin. `bug_reports_to'
1117 (should be an email address) is printed if an `sk_assert' assertion
1118 fails or VG_(skin_panic) is called. */
njn25e49d8e72002-09-23 09:36:25 +00001119 Char* name;
1120 Char* description;
njne427a662002-10-02 11:08:25 +00001121 Char* bug_reports_to;
njn25e49d8e72002-09-23 09:36:25 +00001122
njnd5bb0a52002-09-27 10:24:48 +00001123 /* Booleans that decide core behaviour, but don't require extra
1124 operations to be defined if `True' */
1125
1126 /* Should __libc_freeres() be run? Bugs in it can crash the skin. */
1127 Bool run_libc_freeres;
njn25e49d8e72002-09-23 09:36:25 +00001128
1129 /* Want to have errors detected by Valgrind's core reported? Includes:
1130 - pthread API errors (many; eg. unlocking a non-locked mutex)
1131 - silly arguments to malloc() et al (eg. negative size)
1132 - invalid file descriptors to blocking syscalls read() and write()
1133 - bad signal numbers passed to sigaction()
1134 - attempt to install signal handler for SIGKILL or SIGSTOP */
1135 Bool core_errors;
njn25e49d8e72002-09-23 09:36:25 +00001136
1137 /* Booleans that indicate extra operations are defined; if these are
1138 True, the corresponding template functions (given below) must be
1139 defined. A lot like being a member of a type class. */
1140
njnd5bb0a52002-09-27 10:24:48 +00001141 /* Want to report errors from the skin? This implies use of
1142 suppressions, too. */
1143 Bool skin_errors;
1144
njn25e49d8e72002-09-23 09:36:25 +00001145 /* Is information kept about specific individual basic blocks? (Eg. for
njnd5bb0a52002-09-27 10:24:48 +00001146 cachegrind there are cost-centres for every instruction, stored at a
njn25e49d8e72002-09-23 09:36:25 +00001147 basic block level.) If so, it sometimes has to be discarded, because
1148 .so mmap/munmap-ping or self-modifying code (informed by the
1149 DISCARD_TRANSLATIONS user request) can cause one instruction address
njnd5bb0a52002-09-27 10:24:48 +00001150 to be used for more than one instruction in one program run... */
njn25e49d8e72002-09-23 09:36:25 +00001151 Bool basic_block_discards;
1152
njnd5bb0a52002-09-27 10:24:48 +00001153 /* Skin maintains information about each register? */
njn25e49d8e72002-09-23 09:36:25 +00001154 Bool shadow_regs;
1155
1156 /* Skin defines its own command line options? */
1157 Bool command_line_options;
1158 /* Skin defines its own client requests? */
1159 Bool client_requests;
1160
1161 /* Skin defines its own UInstrs? */
1162 Bool extended_UCode;
1163
1164 /* Skin does stuff before and/or after system calls? */
1165 Bool syscall_wrapper;
1166
1167 /* Size, in words, of extra info about malloc'd blocks recorded by
1168 skin. Be careful to get this right or you'll get seg faults! */
1169 UInt sizeof_shadow_block;
1170
1171 /* Skin does free()s itself? */
1172 Bool alternative_free;
1173
1174 /* Are skin-state sanity checks performed? */
1175 Bool sanity_checks;
1176 }
1177 VgNeeds;
1178
1179extern VgNeeds VG_(needs);
1180
1181
1182/* ------------------------------------------------------------------ */
1183/* Core events to track */
1184
1185/* Part of the core from which this call was made. Useful for determining
njnd5bb0a52002-09-27 10:24:48 +00001186 what kind of error message should be emitted. */
njn25e49d8e72002-09-23 09:36:25 +00001187typedef
1188 enum { Vg_CorePThread, Vg_CoreSignal, Vg_CoreSysCall, Vg_CoreTranslate }
1189 CorePart;
1190
1191/* Events happening in core to track. To be notified, assign a function
njnd5bb0a52002-09-27 10:24:48 +00001192 to the function pointer. To ignore an event, don't do anything
1193 (default assignment is to NULL in which case the call is skipped). */
njn25e49d8e72002-09-23 09:36:25 +00001194typedef
1195 struct {
1196 /* Memory events */
1197 void (*new_mem_startup)( Addr a, UInt len, Bool rr, Bool ww, Bool xx );
1198 void (*new_mem_heap) ( Addr a, UInt len, Bool is_inited );
1199 void (*new_mem_stack) ( Addr a, UInt len );
1200 void (*new_mem_stack_aligned) ( Addr a, UInt len );
1201 void (*new_mem_stack_signal) ( Addr a, UInt len );
1202 void (*new_mem_brk) ( Addr a, UInt len );
1203 void (*new_mem_mmap) ( Addr a, UInt len,
1204 Bool nn, Bool rr, Bool ww, Bool xx );
1205
1206 void (*copy_mem_heap) ( Addr from, Addr to, UInt len );
1207 void (*copy_mem_remap) ( Addr from, Addr to, UInt len );
1208 void (*change_mem_mprotect) ( Addr a, UInt len,
1209 Bool nn, Bool rr, Bool ww, Bool xx );
1210
njnd5bb0a52002-09-27 10:24:48 +00001211 /* Used on redzones around malloc'd blocks and at end of stack */
njn25e49d8e72002-09-23 09:36:25 +00001212 void (*ban_mem_heap) ( Addr a, UInt len );
1213 void (*ban_mem_stack) ( Addr a, UInt len );
1214
1215 void (*die_mem_heap) ( Addr a, UInt len );
1216 void (*die_mem_stack) ( Addr a, UInt len );
1217 void (*die_mem_stack_aligned) ( Addr a, UInt len );
1218 void (*die_mem_stack_signal) ( Addr a, UInt len );
1219 void (*die_mem_brk) ( Addr a, UInt len );
1220 void (*die_mem_munmap) ( Addr a, UInt len );
1221
1222 void (*bad_free) ( ThreadState* tst, Addr a );
1223 void (*mismatched_free) ( ThreadState* tst, Addr a );
1224
1225 void (*pre_mem_read) ( CorePart part, ThreadState* tst,
1226 Char* s, Addr a, UInt size );
1227 void (*pre_mem_read_asciiz) ( CorePart part, ThreadState* tst,
1228 Char* s, Addr a );
1229 void (*pre_mem_write) ( CorePart part, ThreadState* tst,
1230 Char* s, Addr a, UInt size );
1231 /* Not implemented yet -- have to add in lots of places, which is a
1232 pain. Won't bother unless/until there's a need. */
1233 /* void (*post_mem_read) ( ThreadState* tst, Char* s,
1234 Addr a, UInt size ); */
1235 void (*post_mem_write) ( Addr a, UInt size );
1236
1237
njnd5bb0a52002-09-27 10:24:48 +00001238 /* Scheduler events (not exhaustive) */
njn25e49d8e72002-09-23 09:36:25 +00001239 void (*thread_run) ( ThreadId tid );
1240
1241
njnd5bb0a52002-09-27 10:24:48 +00001242 /* Mutex events (not exhaustive) */
njn25e49d8e72002-09-23 09:36:25 +00001243 void (*post_mutex_lock) ( ThreadId tid,
1244 void* /*pthread_mutex_t* */ mutex );
1245 void (*post_mutex_unlock) ( ThreadId tid,
1246 void* /*pthread_mutex_t* */ mutex );
njn25e49d8e72002-09-23 09:36:25 +00001247
njnd5bb0a52002-09-27 10:24:48 +00001248
1249 /* Others... thread, condition variable, signal events... */
njn25e49d8e72002-09-23 09:36:25 +00001250 /* ... */
1251 }
1252 VgTrackEvents;
1253
1254/* Declare the struct instance */
1255extern VgTrackEvents VG_(track_events);
1256
1257
1258/* ------------------------------------------------------------------ */
1259/* Template functions */
1260
1261/* These are the parameterised functions in the core. The default definitions
njnd5bb0a52002-09-27 10:24:48 +00001262 are overridden by LD_PRELOADed skin version. At the very least, a skin
1263 must define the fundamental template functions. Depending on what needs
1264 are set, extra template functions will be used too. Functions are
1265 grouped under the needs that govern their use. */
njn25e49d8e72002-09-23 09:36:25 +00001266
1267
1268/* ------------------------------------------------------------------ */
1269/* Fundamental template functions */
1270
1271/* Initialise skin. Must do the following:
1272 - initialise the 'needs' struct
1273 - register any helpers called by generated code
1274
1275 May do the following:
1276 - indicate events to track by initialising part or all of the 'track'
1277 struct
1278 - register any skin-specific profiling events
1279 - any other skin-specific initialisation
1280*/
1281extern void SK_(pre_clo_init) ( VgNeeds* needs, VgTrackEvents* track );
1282
njnd5bb0a52002-09-27 10:24:48 +00001283/* Do initialisation that can only be done after command line processing. */
njn25e49d8e72002-09-23 09:36:25 +00001284extern void SK_(post_clo_init)( void );
1285
1286/* Instrument a basic block. Must be a true function, ie. the same input
1287 always results in the same output, because basic blocks can be
1288 retranslated. Unless you're doing something really strange...
1289 'orig_addr' is the address of the first instruction in the block. */
1290extern UCodeBlock* SK_(instrument) ( UCodeBlock* cb, Addr orig_addr );
1291
1292/* Finish up, print out any results, etc. */
1293extern void SK_(fini) ( void );
1294
1295
1296/* ------------------------------------------------------------------ */
1297/* VG_(needs).report_errors */
1298
1299/* Identify if two errors are equal, or equal enough. `res' indicates how
1300 close is "close enough". `res' should be passed on as necessary, eg. if
1301 the SkinError's extra field contains an ExeContext, `res' should be
1302 passed to VG_(eq_ExeContext)() if the ExeContexts are considered. Other
1303 than that, probably don't worry about it unless you have lots of very
1304 similar errors occurring.
1305 */
1306extern Bool SK_(eq_SkinError) ( VgRes res,
1307 SkinError* e1, SkinError* e2 );
1308
1309/* Print error context. The passed function pp_ExeContext() can be (and
1310 probably should be) used to print the location of the error. */
1311extern void SK_(pp_SkinError) ( SkinError* ec, void (*pp_ExeContext)(void) );
1312
1313/* Copy the ec->extra part and replace ec->extra with the new copy. This is
1314 necessary to move from a temporary stack copy to a permanent heap one.
1315
1316 Then fill in any details that could be postponed until after the decision
1317 whether to ignore the error (ie. details not affecting the result of
1318 SK_(eq_SkinError)()). This saves time when errors are ignored.
1319
1320 Yuk.
1321*/
1322extern void SK_(dup_extra_and_update)(SkinError* ec);
1323
1324/* Return value indicates recognition. If recognised, type goes in `skind'. */
1325extern Bool SK_(recognised_suppression) ( Char* name, SuppKind *skind );
1326
1327/* Read any extra info for this suppression kind. For filling up the
1328 `string' and `extra' fields in a `SkinSupp' struct if necessary. */
1329extern Bool SK_(read_extra_suppression_info) ( Int fd, Char* buf,
njnd5bb0a52002-09-27 10:24:48 +00001330 Int nBuf, SkinSupp *s );
njn25e49d8e72002-09-23 09:36:25 +00001331
1332/* This should just check the kinds match and maybe some stuff in the
1333 'extra' field if appropriate */
1334extern Bool SK_(error_matches_suppression)(SkinError* ec, SkinSupp* su);
1335
1336
1337/* ------------------------------------------------------------------ */
1338/* VG_(needs).basic_block_discards */
1339
njnd5bb0a52002-09-27 10:24:48 +00001340/* Should discard any information that pertains to specific basic blocks
1341 or instructions within the address range given. */
njn25e49d8e72002-09-23 09:36:25 +00001342extern void SK_(discard_basic_block_info) ( Addr a, UInt size );
1343
1344
1345/* ------------------------------------------------------------------ */
1346/* VG_(needs).shadow_regs */
1347
1348/* Valid values for general registers and EFLAGS register, for initialising
1349 and updating registers when written in certain places in core. */
1350extern void SK_(written_shadow_regs_values) ( UInt* gen_reg, UInt* eflags );
1351
1352
1353/* ------------------------------------------------------------------ */
1354/* VG_(needs).command_line_options */
1355
njnd5bb0a52002-09-27 10:24:48 +00001356/* Return True if option was recognised. Presumably sets some state to
1357 record the option as well. */
1358extern Bool SK_(process_cmd_line_option) ( Char* argv );
njn25e49d8e72002-09-23 09:36:25 +00001359
1360/* Print out command line usage for skin options */
1361extern Char* SK_(usage) ( void );
1362
1363
1364/* ------------------------------------------------------------------ */
1365/* VG_(needs).client_requests */
1366
1367extern UInt SK_(handle_client_request) ( ThreadState* tst, UInt* arg_block );
1368
1369
1370/* ------------------------------------------------------------------ */
1371/* VG_(needs).extends_UCode */
1372
njn4ba5a792002-09-30 10:23:54 +00001373/* Useful to use in VG_(get_Xreg_usage)() */
njnd5bb0a52002-09-27 10:24:48 +00001374#define VG_UINSTR_READS_REG(ono) \
njn25e49d8e72002-09-23 09:36:25 +00001375 { if (mycat(u->tag,ono) == tag) \
1376 { arr[n].num = mycat(u->val,ono); \
1377 arr[n].isWrite = False; \
1378 n++; \
1379 } \
1380 }
njnd5bb0a52002-09-27 10:24:48 +00001381#define VG_UINSTR_WRITES_REG(ono) \
1382 { if (mycat(u->tag,ono) == tag) \
1383 { arr[n].num = mycat(u->val,ono); \
1384 arr[n].isWrite = True; \
1385 n++; \
1386 } \
njn25e49d8e72002-09-23 09:36:25 +00001387 }
1388
njn4ba5a792002-09-30 10:23:54 +00001389/* 'X' prefix indicates eXtended UCode. */
1390extern Int SK_(get_Xreg_usage) ( UInstr* u, Tag tag, RegUse* arr );
1391extern void SK_(emit_XUInstr) ( UInstr* u, RRegSet regs_live_before );
1392extern Bool SK_(sane_XUInstr) ( Bool beforeRA, Bool beforeLiveness,
njn25e49d8e72002-09-23 09:36:25 +00001393 UInstr* u );
njn4ba5a792002-09-30 10:23:54 +00001394extern Char* SK_(name_XUOpcode) ( Opcode opc );
1395extern void SK_(pp_XUInstr) ( UInstr* u );
njn25e49d8e72002-09-23 09:36:25 +00001396
1397
1398/* ------------------------------------------------------------------ */
1399/* VG_(needs).syscall_wrapper */
1400
1401/* If either of the pre_ functions malloc() something to return, the
1402 * corresponding post_ function had better free() it!
1403 */
1404extern void* SK_( pre_syscall) ( ThreadId tid, UInt syscallno,
1405 Bool is_blocking );
1406extern void SK_(post_syscall) ( ThreadId tid, UInt syscallno,
1407 void* pre_result, Int res,
1408 Bool is_blocking );
1409
njnd5bb0a52002-09-27 10:24:48 +00001410
njn25e49d8e72002-09-23 09:36:25 +00001411/* ------------------------------------------------------------------ */
njnd5bb0a52002-09-27 10:24:48 +00001412/* VG_(needs).sizeof_shadow_chunk (if > 0) */
njn25e49d8e72002-09-23 09:36:25 +00001413
1414extern void SK_(complete_shadow_chunk) ( ShadowChunk* sc, ThreadState* tst );
1415
1416
1417/* ------------------------------------------------------------------ */
1418/* VG_(needs).alternative_free */
1419
1420extern void SK_(alt_free) ( ShadowChunk* sc, ThreadState* tst );
1421
njnd5bb0a52002-09-27 10:24:48 +00001422
njn25e49d8e72002-09-23 09:36:25 +00001423/* ---------------------------------------------------------------------
1424 VG_(needs).sanity_checks */
1425
njnd5bb0a52002-09-27 10:24:48 +00001426/* Can be useful for ensuring a skin's correctness. SK_(cheap_sanity_check)
1427 is called very frequently; SK_(expensive_sanity_check) is called less
1428 frequently and can be more involved. */
njn25e49d8e72002-09-23 09:36:25 +00001429extern Bool SK_(cheap_sanity_check) ( void );
1430extern Bool SK_(expensive_sanity_check) ( void );
1431
1432
1433#endif /* NDEF __VG_SKIN_H */
1434
1435/*--------------------------------------------------------------------*/
1436/*--- end vg_skin.h ---*/
1437/*--------------------------------------------------------------------*/
1438