blob: 638ee825bdef255886d62462cf2c663a27cbd5bb [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/*
njnc9539842002-10-02 13:26:35 +00008 This file is part of Valgrind, an extensible x86 protected-mode
9 emulator for monitoring program execution on x86-Unixes.
njn25e49d8e72002-09-23 09:36:25 +000010
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
njn211b6ad2003-02-03 12:33:31 +000062 If you increase 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)
njn211b6ad2003-02-03 12:33:31 +000068
69 You can decrease it, and performance will drop because more spills will
70 occur. If you decrease it too much, everything will fall over.
njn25e49d8e72002-09-23 09:36:25 +000071
72 Do not change this unless you really know what you are doing! */
73#define VG_MAX_REALREGS 6
74
75
76/*====================================================================*/
njn1a1dd8b2002-09-27 10:42:20 +000077/*=== Basic types, useful macros ===*/
njn25e49d8e72002-09-23 09:36:25 +000078/*====================================================================*/
79
njn25e49d8e72002-09-23 09:36:25 +000080typedef unsigned char UChar;
81typedef unsigned short UShort;
82typedef unsigned int UInt;
83typedef unsigned long long int ULong;
84
85typedef signed char Char;
86typedef signed short Short;
87typedef signed int Int;
88typedef signed long long int Long;
89
90typedef unsigned int Addr;
91
92typedef unsigned char Bool;
93#define False ((Bool)0)
94#define True ((Bool)1)
95
96
njn1a1dd8b2002-09-27 10:42:20 +000097#define mycat_wrk(aaa,bbb) aaa##bbb
98#define mycat(aaa,bbb) mycat_wrk(aaa,bbb)
99
100/* No, really. I _am_ that strange. */
101#define OINK(nnn) VG_(message)(Vg_DebugMsg, "OINK %d",nnn)
102
njn25e49d8e72002-09-23 09:36:25 +0000103/* ---------------------------------------------------------------------
104 Now the basic types are set up, we can haul in the kernel-interface
105 definitions.
106 ------------------------------------------------------------------ */
107
njn2574bb4762002-09-24 11:23:50 +0000108#include "../coregrind/vg_kerneliface.h"
njn25e49d8e72002-09-23 09:36:25 +0000109
110
111/*====================================================================*/
njn27f1a382002-11-08 15:48:16 +0000112/*=== Core/skin interface version ===*/
113/*====================================================================*/
114
115/* The major version number indicates binary-incompatible changes to the
116 interface; if the core and skin major versions don't match, Valgrind
117 will abort. The minor version indicates binary-compatible changes.
njn27f1a382002-11-08 15:48:16 +0000118*/
njn27f1a382002-11-08 15:48:16 +0000119#define VG_CORE_INTERFACE_MAJOR_VERSION 1
njn563f96f2003-02-03 11:17:46 +0000120#define VG_CORE_INTERFACE_MINOR_VERSION 2
njn27f1a382002-11-08 15:48:16 +0000121
122extern const Int VG_(skin_interface_major_version);
123extern const Int VG_(skin_interface_minor_version);
124
njn563f96f2003-02-03 11:17:46 +0000125/* Every skin must include this macro somewhere, exactly once. */
njn27f1a382002-11-08 15:48:16 +0000126#define VG_DETERMINE_INTERFACE_VERSION \
127const Int VG_(skin_interface_major_version) = VG_CORE_INTERFACE_MAJOR_VERSION; \
128const Int VG_(skin_interface_minor_version) = VG_CORE_INTERFACE_MINOR_VERSION;
129
njn211b6ad2003-02-03 12:33:31 +0000130
njn27f1a382002-11-08 15:48:16 +0000131/*====================================================================*/
njn25e49d8e72002-09-23 09:36:25 +0000132/*=== Command-line options ===*/
133/*====================================================================*/
134
135/* Verbosity level: 0 = silent, 1 (default), > 1 = more verbose. */
136extern Int VG_(clo_verbosity);
137
138/* Profile? */
139extern Bool VG_(clo_profile);
140
njn25e49d8e72002-09-23 09:36:25 +0000141/* Call this if a recognised option was bad for some reason.
142 Note: don't use it just because an option was unrecognised -- return 'False'
143 from SKN_(process_cmd_line_option) to indicate that. */
144extern void VG_(bad_option) ( Char* opt );
145
146/* Client args */
147extern Int VG_(client_argc);
148extern Char** VG_(client_argv);
149
njnd5bb0a52002-09-27 10:24:48 +0000150/* Client environment. Can be inspected with VG_(getenv)() */
njn25e49d8e72002-09-23 09:36:25 +0000151extern Char** VG_(client_envp);
152
153
154/*====================================================================*/
155/*=== Printing messages for the user ===*/
156/*====================================================================*/
157
158/* Print a message prefixed by "??<pid>?? "; '?' depends on the VgMsgKind.
159 Should be used for all user output. */
160
161typedef
162 enum { Vg_UserMsg, /* '?' == '=' */
163 Vg_DebugMsg, /* '?' == '-' */
164 Vg_DebugExtraMsg /* '?' == '+' */
165 }
166 VgMsgKind;
167
168/* Functions for building a message from multiple parts. */
169extern void VG_(start_msg) ( VgMsgKind kind );
170extern void VG_(add_to_msg) ( Char* format, ... );
171/* Ends and prints the message. Appends a newline. */
172extern void VG_(end_msg) ( void );
173
njnd5bb0a52002-09-27 10:24:48 +0000174/* Send a single-part message. Appends a newline. */
njn25e49d8e72002-09-23 09:36:25 +0000175extern void VG_(message) ( VgMsgKind kind, Char* format, ... );
176
177
178/*====================================================================*/
179/*=== Profiling ===*/
180/*====================================================================*/
181
182/* Nb: VGP_(register_profile_event)() relies on VgpUnc being the first one */
183#define VGP_CORE_LIST \
184 /* These ones depend on the core */ \
185 VGP_PAIR(VgpUnc, "unclassified"), \
186 VGP_PAIR(VgpRun, "running"), \
187 VGP_PAIR(VgpSched, "scheduler"), \
188 VGP_PAIR(VgpMalloc, "low-lev malloc/free"), \
189 VGP_PAIR(VgpCliMalloc, "client malloc/free"), \
190 VGP_PAIR(VgpStack, "adjust-stack"), \
191 VGP_PAIR(VgpTranslate, "translate-main"), \
192 VGP_PAIR(VgpToUCode, "to-ucode"), \
193 VGP_PAIR(VgpFromUcode, "from-ucode"), \
194 VGP_PAIR(VgpImprove, "improve"), \
195 VGP_PAIR(VgpRegAlloc, "reg-alloc"), \
196 VGP_PAIR(VgpLiveness, "liveness-analysis"), \
197 VGP_PAIR(VgpDoLRU, "do-lru"), \
198 VGP_PAIR(VgpSlowFindT, "slow-search-transtab"), \
199 VGP_PAIR(VgpInitMem, "init-memory"), \
200 VGP_PAIR(VgpExeContext, "exe-context"), \
201 VGP_PAIR(VgpReadSyms, "read-syms"), \
202 VGP_PAIR(VgpSearchSyms, "search-syms"), \
203 VGP_PAIR(VgpAddToT, "add-to-transtab"), \
204 VGP_PAIR(VgpCoreSysWrap, "core-syscall-wrapper"), \
205 VGP_PAIR(VgpDemangle, "demangle"), \
njn37cea302002-09-30 11:24:00 +0000206 VGP_PAIR(VgpCoreCheapSanity, "core-cheap-sanity"), \
207 VGP_PAIR(VgpCoreExpensiveSanity, "core-expensive-sanity"), \
njn25e49d8e72002-09-23 09:36:25 +0000208 /* These ones depend on the skin */ \
209 VGP_PAIR(VgpPreCloInit, "pre-clo-init"), \
210 VGP_PAIR(VgpPostCloInit, "post-clo-init"), \
211 VGP_PAIR(VgpInstrument, "instrument"), \
212 VGP_PAIR(VgpSkinSysWrap, "skin-syscall-wrapper"), \
njn37cea302002-09-30 11:24:00 +0000213 VGP_PAIR(VgpSkinCheapSanity, "skin-cheap-sanity"), \
214 VGP_PAIR(VgpSkinExpensiveSanity, "skin-expensive-sanity"), \
njn25e49d8e72002-09-23 09:36:25 +0000215 VGP_PAIR(VgpFini, "fini")
216
217#define VGP_PAIR(n,name) n
218typedef enum { VGP_CORE_LIST } VgpCoreCC;
219#undef VGP_PAIR
220
221/* When registering skin profiling events, ensure that the 'n' value is in
222 * the range (VgpFini+1..) */
223extern void VGP_(register_profile_event) ( Int n, Char* name );
224
225extern void VGP_(pushcc) ( UInt cc );
226extern void VGP_(popcc) ( UInt cc );
227
228/* Define them only if they haven't already been defined by vg_profile.c */
229#ifndef VGP_PUSHCC
230# define VGP_PUSHCC(x)
231#endif
232#ifndef VGP_POPCC
233# define VGP_POPCC(x)
234#endif
235
236
237/*====================================================================*/
238/*=== Useful stuff to call from generated code ===*/
239/*====================================================================*/
240
241/* ------------------------------------------------------------------ */
242/* General stuff */
243
njn41557122002-10-14 09:25:37 +0000244/* 64-bit counter for the number of basic blocks done. */
245extern ULong VG_(bbs_done);
246
njn25e49d8e72002-09-23 09:36:25 +0000247/* Get the simulated %esp */
248extern Addr VG_(get_stack_pointer) ( void );
249
njnd5bb0a52002-09-27 10:24:48 +0000250/* Detect if an address is within Valgrind's stack or Valgrind's
251 m_state_static; useful for memory leak detectors to tell if a block
252 is used by Valgrind (and thus can be ignored). */
njn25e49d8e72002-09-23 09:36:25 +0000253extern Bool VG_(within_stack)(Addr a);
njn25e49d8e72002-09-23 09:36:25 +0000254extern Bool VG_(within_m_state_static)(Addr a);
255
256/* Check if an address is 4-byte aligned */
257#define IS_ALIGNED4_ADDR(aaa_p) (0 == (((UInt)(aaa_p)) & 3))
258
259
260/* ------------------------------------------------------------------ */
261/* Thread-related stuff */
262
263/* Special magic value for an invalid ThreadId. It corresponds to
264 LinuxThreads using zero as the initial value for
265 pthread_mutex_t.__m_owner and pthread_cond_t.__c_waiting. */
266#define VG_INVALID_THREADID ((ThreadId)(0))
267
268/* ThreadIds are simply indices into the vg_threads[] array. */
269typedef
270 UInt
271 ThreadId;
272
njnd5bb0a52002-09-27 10:24:48 +0000273/* struct _ThreadState defined elsewhere; ThreadState is abstract as its
274 definition is not important for skins. */
njn25e49d8e72002-09-23 09:36:25 +0000275typedef
276 struct _ThreadState
277 ThreadState;
278
sewardj7ab2aca2002-10-20 19:40:32 +0000279extern ThreadId VG_(get_current_tid) ( void );
sewardjb52a1b02002-10-23 21:38:22 +0000280extern ThreadId VG_(get_current_or_recent_tid) ( void );
sewardj7ab2aca2002-10-20 19:40:32 +0000281extern ThreadId VG_(get_tid_from_ThreadState) ( ThreadState* );
njn25e49d8e72002-09-23 09:36:25 +0000282extern ThreadState* VG_(get_ThreadState) ( ThreadId tid );
283
284
285/*====================================================================*/
286/*=== Valgrind's version of libc ===*/
287/*====================================================================*/
288
289/* Valgrind doesn't use libc at all, for good reasons (trust us). So here
290 are its own versions of C library functions, but with VG_ prefixes. Note
291 that the types of some are slightly different to the real ones. Some
292 extra useful functions are provided too; descriptions of how they work
293 are given below. */
294
295#if !defined(NULL)
296# define NULL ((void*)0)
297#endif
298
299
300/* ------------------------------------------------------------------ */
301/* stdio.h
302 *
303 * Note that they all output to the file descriptor given by the
304 * --logfile-fd=N argument, which defaults to 2 (stderr). Hence no
305 * need for VG_(fprintf)().
njn25e49d8e72002-09-23 09:36:25 +0000306 */
sewardj78e3cd92002-10-22 04:45:48 +0000307extern UInt VG_(printf) ( const char *format, ... );
njn25e49d8e72002-09-23 09:36:25 +0000308/* too noisy ... __attribute__ ((format (printf, 1, 2))) ; */
sewardj78e3cd92002-10-22 04:45:48 +0000309extern UInt VG_(sprintf) ( Char* buf, Char *format, ... );
310extern UInt VG_(vprintf) ( void(*send)(Char),
njn25e49d8e72002-09-23 09:36:25 +0000311 const Char *format, va_list vargs );
312
njn41557122002-10-14 09:25:37 +0000313extern Int VG_(rename) ( Char* old_name, Char* new_name );
314
njn25e49d8e72002-09-23 09:36:25 +0000315/* ------------------------------------------------------------------ */
316/* stdlib.h */
317
318extern void* VG_(malloc) ( Int nbytes );
njnd5bb0a52002-09-27 10:24:48 +0000319extern void VG_(free) ( void* p );
320extern void* VG_(calloc) ( Int n, Int nbytes );
321extern void* VG_(realloc) ( void* p, Int size );
322extern void* VG_(malloc_aligned) ( Int align_bytes, Int nbytes );
njn25e49d8e72002-09-23 09:36:25 +0000323
324extern void VG_(print_malloc_stats) ( void );
325
326
327extern void VG_(exit)( Int status )
328 __attribute__ ((__noreturn__));
njne427a662002-10-02 11:08:25 +0000329/* Prints a panic message (a constant string), appends newline and bug
330 reporting info, aborts. */
331__attribute__ ((__noreturn__))
332extern void VG_(skin_panic) ( Char* str );
njn25e49d8e72002-09-23 09:36:25 +0000333
njnd5bb0a52002-09-27 10:24:48 +0000334/* Looks up VG_(client_envp) */
njn25e49d8e72002-09-23 09:36:25 +0000335extern Char* VG_(getenv) ( Char* name );
336
337/* Crude stand-in for the glibc system() call. */
338extern Int VG_(system) ( Char* cmd );
339
njnd5bb0a52002-09-27 10:24:48 +0000340extern Long VG_(atoll) ( Char* str );
njn25e49d8e72002-09-23 09:36:25 +0000341
342/* Like atoll(), but converts a number of base 2..36 */
343extern Long VG_(atoll36) ( UInt base, Char* str );
344
345
346/* ------------------------------------------------------------------ */
njnd5bb0a52002-09-27 10:24:48 +0000347/* ctype.h */
njn25e49d8e72002-09-23 09:36:25 +0000348extern Bool VG_(isspace) ( Char c );
349extern Bool VG_(isdigit) ( Char c );
350extern Char VG_(toupper) ( Char c );
351
352
353/* ------------------------------------------------------------------ */
354/* string.h */
355extern Int VG_(strlen) ( const Char* str );
356extern Char* VG_(strcat) ( Char* dest, const Char* src );
357extern Char* VG_(strncat) ( Char* dest, const Char* src, Int n );
358extern Char* VG_(strpbrk) ( const Char* s, const Char* accept );
359extern Char* VG_(strcpy) ( Char* dest, const Char* src );
360extern Char* VG_(strncpy) ( Char* dest, const Char* src, Int ndest );
361extern Int VG_(strcmp) ( const Char* s1, const Char* s2 );
362extern Int VG_(strncmp) ( const Char* s1, const Char* s2, Int nmax );
363extern Char* VG_(strstr) ( const Char* haystack, Char* needle );
364extern Char* VG_(strchr) ( const Char* s, Char c );
365extern Char* VG_(strdup) ( const Char* s);
sewardj7ab2aca2002-10-20 19:40:32 +0000366extern void* VG_(memcpy) ( void *d, const void *s, Int sz );
367extern void* VG_(memset) ( void *s, Int c, Int sz );
njn25e49d8e72002-09-23 09:36:25 +0000368
njnd5bb0a52002-09-27 10:24:48 +0000369/* Like strcmp() and strncmp(), but stop comparing at any whitespace. */
njn25e49d8e72002-09-23 09:36:25 +0000370extern Int VG_(strcmp_ws) ( const Char* s1, const Char* s2 );
njn25e49d8e72002-09-23 09:36:25 +0000371extern Int VG_(strncmp_ws) ( const Char* s1, const Char* s2, Int nmax );
372
njnd5bb0a52002-09-27 10:24:48 +0000373/* Like strncpy(), but if 'src' is longer than 'ndest' inserts a '\0' as the
374 last character. */
njn25e49d8e72002-09-23 09:36:25 +0000375extern void VG_(strncpy_safely) ( Char* dest, const Char* src, Int ndest );
376
377/* Mini-regexp function. Searches for 'pat' in 'str'. Supports
378 * meta-symbols '*' and '?'. '\' escapes meta-symbols. */
njn4ba5a792002-09-30 10:23:54 +0000379extern Bool VG_(string_match) ( Char* pat, Char* str );
njn25e49d8e72002-09-23 09:36:25 +0000380
381
382/* ------------------------------------------------------------------ */
383/* math.h */
njnd5bb0a52002-09-27 10:24:48 +0000384/* Returns the base-2 logarithm of x. */
njn25e49d8e72002-09-23 09:36:25 +0000385extern Int VG_(log2) ( Int x );
386
387
388/* ------------------------------------------------------------------ */
njnd5bb0a52002-09-27 10:24:48 +0000389/* unistd.h, fcntl.h, sys/stat.h */
sewardj4cf05692002-10-27 20:28:29 +0000390extern Int VG_(getpid) ( void );
391extern Int VG_(getppid) ( void );
njnd5bb0a52002-09-27 10:24:48 +0000392
njn4aca2d22002-10-04 10:29:38 +0000393extern Int VG_(open) ( const Char* pathname, Int flags, Int mode );
394extern Int VG_(read) ( Int fd, void* buf, Int count);
395extern Int VG_(write) ( Int fd, void* buf, Int count);
396extern void VG_(close) ( Int fd );
njnd5bb0a52002-09-27 10:24:48 +0000397
njn41557122002-10-14 09:25:37 +0000398/* Nb: VG_(rename)() declared in stdio.h section above */
njn4aca2d22002-10-04 10:29:38 +0000399extern Int VG_(unlink) ( Char* file_name );
400extern Int VG_(stat) ( Char* file_name, struct vki_stat* buf );
njn25e49d8e72002-09-23 09:36:25 +0000401
402
403/* ------------------------------------------------------------------ */
404/* assert.h */
405/* Asserts permanently enabled -- no turning off with NDEBUG. Hurrah! */
406#define VG__STRING(__str) #__str
407
njne427a662002-10-02 11:08:25 +0000408#define sk_assert(expr) \
njn25e49d8e72002-09-23 09:36:25 +0000409 ((void) ((expr) ? 0 : \
njne427a662002-10-02 11:08:25 +0000410 (VG_(skin_assert_fail) (VG__STRING(expr), \
411 __FILE__, __LINE__, \
412 __PRETTY_FUNCTION__), 0)))
njn25e49d8e72002-09-23 09:36:25 +0000413
njne427a662002-10-02 11:08:25 +0000414__attribute__ ((__noreturn__))
415extern void VG_(skin_assert_fail) ( Char* expr, Char* file,
416 Int line, Char* fn );
njn25e49d8e72002-09-23 09:36:25 +0000417
418
419/* ------------------------------------------------------------------ */
njnd5bb0a52002-09-27 10:24:48 +0000420/* system/mman.h */
njn25e49d8e72002-09-23 09:36:25 +0000421extern void* VG_(mmap)( void* start, UInt length,
422 UInt prot, UInt flags, UInt fd, UInt offset );
423extern Int VG_(munmap)( void* start, Int length );
424
425/* Get memory by anonymous mmap. */
426extern void* VG_(get_memory_from_mmap) ( Int nBytes, Char* who );
427
428
429/* ------------------------------------------------------------------ */
430/* signal.h.
431
432 Note that these use the vk_ (kernel) structure
433 definitions, which are different in places from those that glibc
434 defines -- hence the 'k' prefix. Since we're operating right at the
435 kernel interface, glibc's view of the world is entirely irrelevant. */
436
437/* --- Signal set ops --- */
njnd5bb0a52002-09-27 10:24:48 +0000438extern Int VG_(ksigfillset) ( vki_ksigset_t* set );
439extern Int VG_(ksigemptyset) ( vki_ksigset_t* set );
njn25e49d8e72002-09-23 09:36:25 +0000440
njnd5bb0a52002-09-27 10:24:48 +0000441extern Bool VG_(kisfullsigset) ( vki_ksigset_t* set );
442extern Bool VG_(kisemptysigset) ( vki_ksigset_t* set );
njn25e49d8e72002-09-23 09:36:25 +0000443
njnd5bb0a52002-09-27 10:24:48 +0000444extern Int VG_(ksigaddset) ( vki_ksigset_t* set, Int signum );
445extern Int VG_(ksigdelset) ( vki_ksigset_t* set, Int signum );
njn25e49d8e72002-09-23 09:36:25 +0000446extern Int VG_(ksigismember) ( vki_ksigset_t* set, Int signum );
447
njnd5bb0a52002-09-27 10:24:48 +0000448extern void VG_(ksigaddset_from_set) ( vki_ksigset_t* dst, vki_ksigset_t* src );
449extern void VG_(ksigdelset_from_set) ( vki_ksigset_t* dst, vki_ksigset_t* src );
njn25e49d8e72002-09-23 09:36:25 +0000450
451/* --- Mess with the kernel's sig state --- */
njnd5bb0a52002-09-27 10:24:48 +0000452extern Int VG_(ksigprocmask) ( Int how, const vki_ksigset_t* set,
njn25e49d8e72002-09-23 09:36:25 +0000453 vki_ksigset_t* oldset );
njnd5bb0a52002-09-27 10:24:48 +0000454extern Int VG_(ksigaction) ( Int signum,
455 const vki_ksigaction* act,
456 vki_ksigaction* oldact );
njn25e49d8e72002-09-23 09:36:25 +0000457
njnd5bb0a52002-09-27 10:24:48 +0000458extern Int VG_(ksignal) ( Int signum, void (*sighandler)(Int) );
459extern Int VG_(ksigaltstack) ( const vki_kstack_t* ss, vki_kstack_t* oss );
njn25e49d8e72002-09-23 09:36:25 +0000460
sewardjdcaf3122002-09-30 23:12:33 +0000461extern Int VG_(kkill) ( Int pid, Int signo );
462extern Int VG_(ksigpending) ( vki_ksigset_t* set );
njn25e49d8e72002-09-23 09:36:25 +0000463
464
465/*====================================================================*/
466/*=== UCode definition ===*/
467/*====================================================================*/
468
sewardje1042472002-09-30 12:33:11 +0000469/* Tags which describe what operands are. Must fit into 4 bits, which
470 they clearly do. */
njn25e49d8e72002-09-23 09:36:25 +0000471typedef
sewardje1042472002-09-30 12:33:11 +0000472enum { TempReg =0, /* virtual temp-reg */
473 ArchReg =1, /* simulated integer reg */
474 ArchRegS =2, /* simulated segment reg */
475 RealReg =3, /* real machine's real reg */
476 SpillNo =4, /* spill slot location */
477 Literal =5, /* literal; .lit32 field has actual value */
478 Lit16 =6, /* literal; .val[123] field has actual value */
479 NoValue =7 /* operand not in use */
480 }
481 Tag;
njn25e49d8e72002-09-23 09:36:25 +0000482
njnd5bb0a52002-09-27 10:24:48 +0000483/* Invalid register numbers (can't be negative) */
njn25e49d8e72002-09-23 09:36:25 +0000484#define INVALID_TEMPREG 999999999
485#define INVALID_REALREG 999999999
486
487/* Microinstruction opcodes. */
488typedef
489 enum {
njnd5bb0a52002-09-27 10:24:48 +0000490 NOP, /* Null op */
njn25e49d8e72002-09-23 09:36:25 +0000491
sewardj7a5ebcf2002-11-13 22:42:13 +0000492 LOCK, /* Indicate the existance of a LOCK prefix (functionally NOP) */
493
njnd5bb0a52002-09-27 10:24:48 +0000494 /* Moving values around */
495 GET, PUT, /* simulated register <--> TempReg */
496 GETF, PUTF, /* simulated %eflags <--> TempReg */
497 LOAD, STORE, /* memory <--> TempReg */
498 MOV, /* TempReg <--> TempReg */
499 CMOV, /* Used for cmpxchg and cmov */
njn25e49d8e72002-09-23 09:36:25 +0000500
njnd5bb0a52002-09-27 10:24:48 +0000501 /* Arithmetic/logical ops */
502 ADD, ADC, SUB, SBB, /* Add/subtract (w/wo carry) */
503 AND, OR, XOR, NOT, /* Boolean ops */
504 SHL, SHR, SAR, ROL, ROR, RCL, RCR, /* Shift/rotate (w/wo carry) */
505 NEG, /* Negate */
506 INC, DEC, /* Increment/decrement */
507 BSWAP, /* Big-endian <--> little-endian */
508 CC2VAL, /* Condition code --> 0 or 1 */
509 WIDEN, /* Signed or unsigned widening */
njn25e49d8e72002-09-23 09:36:25 +0000510
njnd5bb0a52002-09-27 10:24:48 +0000511 /* Conditional or unconditional jump */
512 JMP,
513
514 /* FPU ops */
515 FPU, /* Doesn't touch memory */
516 FPU_R, FPU_W, /* Reads/writes memory */
517
518 /* Not strictly needed, but improve address calculation translations. */
njn25e49d8e72002-09-23 09:36:25 +0000519 LEA1, /* reg2 := const + reg1 */
520 LEA2, /* reg3 := const + reg1 + reg2 * 1,2,4 or 8 */
521
njnd5bb0a52002-09-27 10:24:48 +0000522 /* Hack for x86 REP insns. Jump to literal if TempReg/RealReg is zero. */
523 JIFZ,
njn25e49d8e72002-09-23 09:36:25 +0000524
njnd5bb0a52002-09-27 10:24:48 +0000525 /* Advance the simulated %eip by some small (< 128) number. */
526 INCEIP,
njn25e49d8e72002-09-23 09:36:25 +0000527
sewardje1042472002-09-30 12:33:11 +0000528 /* Dealing with segment registers */
529 GETSEG, PUTSEG, /* simulated segment register <--> TempReg */
530 USESEG, /* (LDT/GDT index, virtual addr) --> linear addr */
531
njnd5bb0a52002-09-27 10:24:48 +0000532 /* Not for translating x86 calls -- only to call helpers */
533 CALLM_S, CALLM_E, /* Mark start/end of CALLM push/pop sequence */
534 PUSH, POP, CLEAR, /* Add/remove/zap args for helpers */
535 CALLM, /* Call assembly-code helper */
536
537 /* Not for translating x86 calls -- only to call C helper functions of
538 up to three arguments (or two if the functions has a return value).
539 Arguments and return value must be word-sized. More arguments can
540 be faked with global variables (eg. use VG_(set_global_var)()).
541
542 Seven possibilities: 'arg[123]' show where args go, 'ret' shows
543 where return value goes (if present).
njn25e49d8e72002-09-23 09:36:25 +0000544
545 CCALL(-, -, - ) void f(void)
546 CCALL(arg1, -, - ) void f(UInt arg1)
547 CCALL(arg1, arg2, - ) void f(UInt arg1, UInt arg2)
548 CCALL(arg1, arg2, arg3) void f(UInt arg1, UInt arg2, UInt arg3)
549 CCALL(-, -, ret ) UInt f(UInt)
550 CCALL(arg1, -, ret ) UInt f(UInt arg1)
njnd5bb0a52002-09-27 10:24:48 +0000551 CCALL(arg1, arg2, ret ) UInt f(UInt arg1, UInt arg2) */
njn25e49d8e72002-09-23 09:36:25 +0000552 CCALL,
553
njnd5bb0a52002-09-27 10:24:48 +0000554 /* This opcode makes it easy for skins that extend UCode to do this to
555 avoid opcode overlap:
njn25e49d8e72002-09-23 09:36:25 +0000556
njnd5bb0a52002-09-27 10:24:48 +0000557 enum { EU_OP1 = DUMMY_FINAL_UOPCODE + 1, ... }
njn25e49d8e72002-09-23 09:36:25 +0000558
559 WARNING: Do not add new opcodes after this one! They can be added
560 before, though. */
561 DUMMY_FINAL_UOPCODE
562 }
563 Opcode;
564
565
njnd5bb0a52002-09-27 10:24:48 +0000566/* Condition codes, using the Intel encoding. CondAlways is an extra. */
njn25e49d8e72002-09-23 09:36:25 +0000567typedef
568 enum {
569 CondO = 0, /* overflow */
570 CondNO = 1, /* no overflow */
571 CondB = 2, /* below */
572 CondNB = 3, /* not below */
573 CondZ = 4, /* zero */
574 CondNZ = 5, /* not zero */
575 CondBE = 6, /* below or equal */
576 CondNBE = 7, /* not below or equal */
577 CondS = 8, /* negative */
sewardje1042472002-09-30 12:33:11 +0000578 CondNS = 9, /* not negative */
njn25e49d8e72002-09-23 09:36:25 +0000579 CondP = 10, /* parity even */
580 CondNP = 11, /* not parity even */
581 CondL = 12, /* jump less */
582 CondNL = 13, /* not less */
583 CondLE = 14, /* less or equal */
584 CondNLE = 15, /* not less or equal */
585 CondAlways = 16 /* Jump always */
586 }
587 Condcode;
588
589
590/* Descriptions of additional properties of *unconditional* jumps. */
591typedef
592 enum {
593 JmpBoring=0, /* boring unconditional jump */
594 JmpCall=1, /* jump due to an x86 call insn */
595 JmpRet=2, /* jump due to an x86 ret insn */
596 JmpSyscall=3, /* do a system call, then jump */
597 JmpClientReq=4 /* do a client request, then jump */
598 }
599 JmpKind;
600
601
602/* Flags. User-level code can only read/write O(verflow), S(ign),
603 Z(ero), A(ux-carry), C(arry), P(arity), and may also write
604 D(irection). That's a total of 7 flags. A FlagSet is a bitset,
605 thusly:
606 76543210
607 DOSZACP
608 and bit 7 must always be zero since it is unused.
sewardj2370f3b2002-11-30 15:01:01 +0000609
610 Note: these Flag? values are **not** the positions in the actual
611 %eflags register. */
612
njn25e49d8e72002-09-23 09:36:25 +0000613typedef UChar FlagSet;
614
615#define FlagD (1<<6)
616#define FlagO (1<<5)
617#define FlagS (1<<4)
618#define FlagZ (1<<3)
619#define FlagA (1<<2)
620#define FlagC (1<<1)
621#define FlagP (1<<0)
622
623#define FlagsOSZACP (FlagO | FlagS | FlagZ | FlagA | FlagC | FlagP)
624#define FlagsOSZAP (FlagO | FlagS | FlagZ | FlagA | FlagP)
625#define FlagsOSZCP (FlagO | FlagS | FlagZ | FlagC | FlagP)
626#define FlagsOSACP (FlagO | FlagS | FlagA | FlagC | FlagP)
627#define FlagsSZACP ( FlagS | FlagZ | FlagA | FlagC | FlagP)
628#define FlagsSZAP ( FlagS | FlagZ | FlagA | FlagP)
629#define FlagsZCP ( FlagZ | FlagC | FlagP)
630#define FlagsOC (FlagO | FlagC )
631#define FlagsAC ( FlagA | FlagC )
632
633#define FlagsALL (FlagsOSZACP | FlagD)
634#define FlagsEmpty (FlagSet)0
635
636
sewardj2370f3b2002-11-30 15:01:01 +0000637/* flag positions in eflags */
638#define EFlagC (1 << 0) /* carry */
639#define EFlagP (1 << 2) /* parity */
sewardjfa492d42002-12-08 18:20:01 +0000640#define EFlagA (1 << 4) /* aux carry */
sewardj2370f3b2002-11-30 15:01:01 +0000641#define EFlagZ (1 << 6) /* zero */
642#define EFlagS (1 << 7) /* sign */
sewardjfa492d42002-12-08 18:20:01 +0000643#define EFlagD (1 << 10) /* direction */
sewardj2370f3b2002-11-30 15:01:01 +0000644#define EFlagO (1 << 11) /* overflow */
645
njn25e49d8e72002-09-23 09:36:25 +0000646/* Liveness of general purpose registers, useful for code generation.
647 Reg rank order 0..N-1 corresponds to bits 0..N-1, ie. first
648 reg's liveness in bit 0, last reg's in bit N-1. Note that
649 these rankings don't match the Intel register ordering. */
650typedef UInt RRegSet;
651
652#define ALL_RREGS_DEAD 0 /* 0000...00b */
njne7c258c2002-10-03 08:57:01 +0000653#define ALL_RREGS_LIVE ((1 << VG_MAX_REALREGS)-1) /* 0011...11b */
njn25e49d8e72002-09-23 09:36:25 +0000654#define UNIT_RREGSET(rank) (1 << (rank))
655
656#define IS_RREG_LIVE(rank,rregs_live) (rregs_live & UNIT_RREGSET(rank))
657#define SET_RREG_LIVENESS(rank,rregs_live,b) \
658 do { RRegSet unit = UNIT_RREGSET(rank); \
659 if (b) rregs_live |= unit; \
660 else rregs_live &= ~unit; \
661 } while(0)
662
663
664/* A Micro (u)-instruction. */
665typedef
666 struct {
667 /* word 1 */
668 UInt lit32; /* 32-bit literal */
669
670 /* word 2 */
671 UShort val1; /* first operand */
672 UShort val2; /* second operand */
673
674 /* word 3 */
675 UShort val3; /* third operand */
676 UChar opcode; /* opcode */
677 UChar size; /* data transfer size */
678
679 /* word 4 */
680 FlagSet flags_r; /* :: FlagSet */
681 FlagSet flags_w; /* :: FlagSet */
682 UChar tag1:4; /* first operand tag */
683 UChar tag2:4; /* second operand tag */
684 UChar tag3:4; /* third operand tag */
685 UChar extra4b:4; /* Spare field, used by WIDEN for src
686 -size, and by LEA2 for scale (1,2,4 or 8),
687 and by JMPs for original x86 instr size */
688
689 /* word 5 */
690 UChar cond; /* condition, for jumps */
691 Bool signed_widen:1; /* signed or unsigned WIDEN ? */
692 JmpKind jmpkind:3; /* additional properties of unconditional JMP */
693
694 /* Additional properties for UInstrs that call C functions:
695 - CCALL
696 - PUT (when %ESP is the target)
697 - possibly skin-specific UInstrs
698 */
699 UChar argc:2; /* Number of args, max 3 */
700 UChar regparms_n:2; /* Number of args passed in registers */
701 Bool has_ret_val:1; /* Function has return value? */
702
703 /* RealReg liveness; only sensical after reg alloc and liveness
704 analysis done. This info is a little bit arch-specific --
705 VG_MAX_REALREGS can vary on different architectures. Note that
706 to use this information requires converting between register ranks
njn4ba5a792002-09-30 10:23:54 +0000707 and the Intel register numbers, using VG_(realreg_to_rank)()
708 and/or VG_(rank_to_realreg)() */
njn25e49d8e72002-09-23 09:36:25 +0000709 RRegSet regs_live_after:VG_MAX_REALREGS;
710 }
711 UInstr;
712
713
njn25e49d8e72002-09-23 09:36:25 +0000714typedef
njn810086f2002-11-14 12:42:47 +0000715 struct _UCodeBlock
njn25e49d8e72002-09-23 09:36:25 +0000716 UCodeBlock;
717
njn810086f2002-11-14 12:42:47 +0000718extern Int VG_(get_num_instrs) (UCodeBlock* cb);
719extern Int VG_(get_num_temps) (UCodeBlock* cb);
njn25e49d8e72002-09-23 09:36:25 +0000720
njn810086f2002-11-14 12:42:47 +0000721extern UInstr* VG_(get_instr) (UCodeBlock* cb, Int i);
722extern UInstr* VG_(get_last_instr) (UCodeBlock* cb);
723
njn211b6ad2003-02-03 12:33:31 +0000724
njn25e49d8e72002-09-23 09:36:25 +0000725/*====================================================================*/
726/*=== Instrumenting UCode ===*/
727/*====================================================================*/
728
njn810086f2002-11-14 12:42:47 +0000729/* Find what this instruction does to its regs. `tag' indicates whether we're
730 considering TempRegs (pre-reg-alloc) or RealRegs (post-reg-alloc).
731 `regs' is filled with the affected register numbers, `isWrites' parallels
732 it and indicates if the reg is read or written. If a reg is read and
733 written, it will appear twice in `regs'. `regs' and `isWrites' must be
734 able to fit 3 elements.
njn25e49d8e72002-09-23 09:36:25 +0000735
njn810086f2002-11-14 12:42:47 +0000736 Useful for analysis/optimisation passes. */
737extern Int VG_(get_reg_usage) ( UInstr* u, Tag tag, Int* regs, Bool* isWrites );
njn25e49d8e72002-09-23 09:36:25 +0000738
739
njnd5bb0a52002-09-27 10:24:48 +0000740/* Used to register helper functions to be called from generated code. A
741 limited number of compact helpers can be registered; the code generated
742 to call them is slightly shorter -- so register the mostly frequently
743 called helpers as compact. */
njn25e49d8e72002-09-23 09:36:25 +0000744extern void VG_(register_compact_helper) ( Addr a );
745extern void VG_(register_noncompact_helper) ( Addr a );
746
747
748/* ------------------------------------------------------------------ */
749/* Virtual register allocation */
750
751/* Get a new virtual register */
njn4ba5a792002-09-30 10:23:54 +0000752extern Int VG_(get_new_temp) ( UCodeBlock* cb );
njn25e49d8e72002-09-23 09:36:25 +0000753
754/* Get a new virtual shadow register */
njn4ba5a792002-09-30 10:23:54 +0000755extern Int VG_(get_new_shadow) ( UCodeBlock* cb );
njn25e49d8e72002-09-23 09:36:25 +0000756
757/* Get a virtual register's corresponding virtual shadow register */
758#define SHADOW(tempreg) ((tempreg)+1)
759
760
761/* ------------------------------------------------------------------ */
762/* Low-level UInstr builders */
njn4ba5a792002-09-30 10:23:54 +0000763extern void VG_(new_NOP) ( UInstr* u );
764extern void VG_(new_UInstr0) ( UCodeBlock* cb, Opcode opcode, Int sz );
765extern void VG_(new_UInstr1) ( UCodeBlock* cb, Opcode opcode, Int sz,
njn25e49d8e72002-09-23 09:36:25 +0000766 Tag tag1, UInt val1 );
njn4ba5a792002-09-30 10:23:54 +0000767extern void VG_(new_UInstr2) ( UCodeBlock* cb, Opcode opcode, Int sz,
njn25e49d8e72002-09-23 09:36:25 +0000768 Tag tag1, UInt val1,
769 Tag tag2, UInt val2 );
njn4ba5a792002-09-30 10:23:54 +0000770extern void VG_(new_UInstr3) ( UCodeBlock* cb, Opcode opcode, Int sz,
njn25e49d8e72002-09-23 09:36:25 +0000771 Tag tag1, UInt val1,
772 Tag tag2, UInt val2,
773 Tag tag3, UInt val3 );
njn25e49d8e72002-09-23 09:36:25 +0000774
njn810086f2002-11-14 12:42:47 +0000775/* Set read/write/undefined flags. Undefined flags are treaten as written,
776 but it's worth keeping them logically distinct. */
777extern void VG_(set_flag_fields) ( UCodeBlock* cb, FlagSet fr, FlagSet fw,
778 FlagSet fu);
njn4ba5a792002-09-30 10:23:54 +0000779extern void VG_(set_lit_field) ( UCodeBlock* cb, UInt lit32 );
780extern void VG_(set_ccall_fields) ( UCodeBlock* cb, Addr fn, UChar argc,
781 UChar regparms_n, Bool has_ret_val );
njn810086f2002-11-14 12:42:47 +0000782extern void VG_(set_cond_field) ( UCodeBlock* cb, Condcode code );
njn25e49d8e72002-09-23 09:36:25 +0000783
njn4ba5a792002-09-30 10:23:54 +0000784extern void VG_(copy_UInstr) ( UCodeBlock* cb, UInstr* instr );
785
786extern Bool VG_(any_flag_use)( UInstr* u );
njn25e49d8e72002-09-23 09:36:25 +0000787
njnac6c1762002-10-04 14:34:15 +0000788/* Macro versions of the above; just shorter to type. */
789#define uInstr0 VG_(new_UInstr0)
790#define uInstr1 VG_(new_UInstr1)
791#define uInstr2 VG_(new_UInstr2)
792#define uInstr3 VG_(new_UInstr3)
793#define uLiteral VG_(set_lit_field)
794#define uCCall VG_(set_ccall_fields)
njn810086f2002-11-14 12:42:47 +0000795#define uCond VG_(set_cond_field)
796#define uFlagsRWU VG_(set_flag_fields)
njnac6c1762002-10-04 14:34:15 +0000797#define newTemp VG_(get_new_temp)
798#define newShadow VG_(get_new_shadow)
799
njn25e49d8e72002-09-23 09:36:25 +0000800/* Refer to `the last instruction stuffed in' (can be lvalue). */
801#define LAST_UINSTR(cb) (cb)->instrs[(cb)->used-1]
802
803
804/* ------------------------------------------------------------------ */
805/* Higher-level UInstr sequence builders */
njn4ba5a792002-09-30 10:23:54 +0000806extern void VG_(call_helper_0_0) ( UCodeBlock* cb, Addr f);
807extern void VG_(call_helper_1_0) ( UCodeBlock* cb, Addr f, UInt arg1,
808 UInt regparms_n);
809extern void VG_(call_helper_2_0) ( UCodeBlock* cb, Addr f, UInt arg1, UInt arg2,
810 UInt regparms_n);
njn25e49d8e72002-09-23 09:36:25 +0000811
812/* One way around the 3-arg C function limit is to pass args via global
813 * variables... ugly, but it works. */
njn4ba5a792002-09-30 10:23:54 +0000814extern void VG_(set_global_var) ( UCodeBlock* cb, Addr globvar_ptr, UInt val);
njn25e49d8e72002-09-23 09:36:25 +0000815
816/* ------------------------------------------------------------------ */
njnd5bb0a52002-09-27 10:24:48 +0000817/* Allocating/freeing basic blocks of UCode */
njn810086f2002-11-14 12:42:47 +0000818extern UCodeBlock* VG_(setup_UCodeBlock) ( UCodeBlock* cb );
njn4ba5a792002-09-30 10:23:54 +0000819extern void VG_(free_UCodeBlock) ( UCodeBlock* cb );
njnd5bb0a52002-09-27 10:24:48 +0000820
821/* ------------------------------------------------------------------ */
822/* UCode pretty/ugly printing. Probably only useful to call from a skin
njn25e49d8e72002-09-23 09:36:25 +0000823 if VG_(needs).extended_UCode == True. */
824
825/* When True, all generated code is/should be printed. */
826extern Bool VG_(print_codegen);
827
njn4ba5a792002-09-30 10:23:54 +0000828/* Pretty/ugly printing functions */
829extern void VG_(pp_UCodeBlock) ( UCodeBlock* cb, Char* title );
830extern void VG_(pp_UInstr) ( Int instrNo, UInstr* u );
831extern void VG_(pp_UInstr_regs) ( Int instrNo, UInstr* u );
832extern void VG_(up_UInstr) ( Int instrNo, UInstr* u );
833extern Char* VG_(name_UOpcode) ( Bool upper, Opcode opc );
njn563f96f2003-02-03 11:17:46 +0000834extern Char* VG_(name_UCondcode) ( Condcode cond );
njn4ba5a792002-09-30 10:23:54 +0000835extern void VG_(pp_UOperand) ( UInstr* u, Int operandNo,
836 Int sz, Bool parens );
njn25e49d8e72002-09-23 09:36:25 +0000837
njnb93d1782003-02-03 12:03:22 +0000838/* ------------------------------------------------------------------ */
839/* Accessing shadow archregs */
840extern UInt VG_(get_shadow_archreg) ( UInt archreg );
841extern void VG_(set_shadow_archreg) ( UInt archreg, UInt val );
842extern Addr VG_(shadow_archreg_address) ( UInt archreg );
njn25e49d8e72002-09-23 09:36:25 +0000843
njn211b6ad2003-02-03 12:33:31 +0000844
845/* ------------------------------------------------------------------ */
846/* Offsets of addresses of helper functions. A "helper" function is one
847 which is called from generated code via CALLM. */
848
849extern Int VGOFF_(helper_idiv_64_32);
850extern Int VGOFF_(helper_div_64_32);
851extern Int VGOFF_(helper_idiv_32_16);
852extern Int VGOFF_(helper_div_32_16);
853extern Int VGOFF_(helper_idiv_16_8);
854extern Int VGOFF_(helper_div_16_8);
855
856extern Int VGOFF_(helper_imul_32_64);
857extern Int VGOFF_(helper_mul_32_64);
858extern Int VGOFF_(helper_imul_16_32);
859extern Int VGOFF_(helper_mul_16_32);
860extern Int VGOFF_(helper_imul_8_16);
861extern Int VGOFF_(helper_mul_8_16);
862
863extern Int VGOFF_(helper_CLD);
864extern Int VGOFF_(helper_STD);
865extern Int VGOFF_(helper_get_dirflag);
866
867extern Int VGOFF_(helper_CLC);
868extern Int VGOFF_(helper_STC);
869
870extern Int VGOFF_(helper_shldl);
871extern Int VGOFF_(helper_shldw);
872extern Int VGOFF_(helper_shrdl);
873extern Int VGOFF_(helper_shrdw);
874
875extern Int VGOFF_(helper_RDTSC);
876extern Int VGOFF_(helper_CPUID);
877
878extern Int VGOFF_(helper_bsf);
879extern Int VGOFF_(helper_bsr);
880
881extern Int VGOFF_(helper_fstsw_AX);
882extern Int VGOFF_(helper_SAHF);
883extern Int VGOFF_(helper_DAS);
884extern Int VGOFF_(helper_DAA);
885
886
njn25e49d8e72002-09-23 09:36:25 +0000887/*====================================================================*/
njnd5bb0a52002-09-27 10:24:48 +0000888/*=== Generating x86 code from UCode ===*/
njn25e49d8e72002-09-23 09:36:25 +0000889/*====================================================================*/
890
njnd5bb0a52002-09-27 10:24:48 +0000891/* All this only necessary for skins with VG_(needs).extends_UCode == True. */
njn25e49d8e72002-09-23 09:36:25 +0000892
sewardje1042472002-09-30 12:33:11 +0000893/* This is the Intel register encoding -- integer regs. */
njn25e49d8e72002-09-23 09:36:25 +0000894#define R_EAX 0
895#define R_ECX 1
896#define R_EDX 2
897#define R_EBX 3
898#define R_ESP 4
899#define R_EBP 5
900#define R_ESI 6
901#define R_EDI 7
902
903#define R_AL (0+R_EAX)
904#define R_CL (0+R_ECX)
905#define R_DL (0+R_EDX)
906#define R_BL (0+R_EBX)
907#define R_AH (4+R_EAX)
908#define R_CH (4+R_ECX)
909#define R_DH (4+R_EDX)
910#define R_BH (4+R_EBX)
911
sewardje1042472002-09-30 12:33:11 +0000912/* This is the Intel register encoding -- segment regs. */
913#define R_ES 0
914#define R_CS 1
915#define R_SS 2
916#define R_DS 3
917#define R_FS 4
918#define R_GS 5
919
njn25e49d8e72002-09-23 09:36:25 +0000920/* For pretty printing x86 code */
sewardje1042472002-09-30 12:33:11 +0000921extern Char* VG_(name_of_seg_reg) ( Int sreg );
njn4ba5a792002-09-30 10:23:54 +0000922extern Char* VG_(name_of_int_reg) ( Int size, Int reg );
923extern Char VG_(name_of_int_size) ( Int size );
njn25e49d8e72002-09-23 09:36:25 +0000924
njnac6c1762002-10-04 14:34:15 +0000925/* Shorter macros for convenience */
926#define nameIReg VG_(name_of_int_reg)
927#define nameISize VG_(name_of_int_size)
928#define nameSReg VG_(name_of_seg_reg)
929
njn25e49d8e72002-09-23 09:36:25 +0000930/* Randomly useful things */
931extern UInt VG_(extend_s_8to32) ( UInt x );
932
933/* Code emitters */
njn4ba5a792002-09-30 10:23:54 +0000934extern void VG_(emitB) ( UInt b );
935extern void VG_(emitW) ( UInt w );
936extern void VG_(emitL) ( UInt l );
sewardjfa492d42002-12-08 18:20:01 +0000937extern void VG_(new_emit) ( Bool upd_cc, FlagSet uses_flags, FlagSet sets_flags );
njn25e49d8e72002-09-23 09:36:25 +0000938
939/* Finding offsets */
njn4ba5a792002-09-30 10:23:54 +0000940extern Int VG_(helper_offset) ( Addr a );
941extern Int VG_(shadow_reg_offset) ( Int arch );
942extern Int VG_(shadow_flags_offset) ( void );
njn25e49d8e72002-09-23 09:36:25 +0000943
njnd5bb0a52002-09-27 10:24:48 +0000944/* Convert reg ranks <-> Intel register ordering, for using register
945 liveness information. */
njn4ba5a792002-09-30 10:23:54 +0000946extern Int VG_(realreg_to_rank) ( Int realreg );
947extern Int VG_(rank_to_realreg) ( Int rank );
njn25e49d8e72002-09-23 09:36:25 +0000948
njnd5bb0a52002-09-27 10:24:48 +0000949/* Call a subroutine. Does no argument passing, stack manipulations, etc. */
sewardjfa492d42002-12-08 18:20:01 +0000950extern void VG_(synth_call) ( Bool ensure_shortform, Int word_offset,
951 Bool upd_cc, FlagSet use_flags, FlagSet set_flags );
njn25e49d8e72002-09-23 09:36:25 +0000952
njnd5bb0a52002-09-27 10:24:48 +0000953/* For calling C functions -- saves caller save regs, pushes args, calls,
954 clears the stack, restores caller save regs. `fn' must be registered in
955 the baseBlock first. Acceptable tags are RealReg and Literal. Optimises
956 things, eg. by not preserving non-live caller-save registers.
njn25e49d8e72002-09-23 09:36:25 +0000957
njnd5bb0a52002-09-27 10:24:48 +0000958 WARNING: a UInstr should *not* be translated with synth_ccall() followed
959 by some other x86 assembly code; this will invalidate the results of
960 vg_realreg_liveness_analysis() and everything will fall over. */
njn4ba5a792002-09-30 10:23:54 +0000961extern void VG_(synth_ccall) ( Addr fn, Int argc, Int regparms_n, UInt argv[],
962 Tag tagv[], Int ret_reg,
963 RRegSet regs_live_before,
964 RRegSet regs_live_after );
njn25e49d8e72002-09-23 09:36:25 +0000965
966/* Addressing modes */
njn4ba5a792002-09-30 10:23:54 +0000967extern void VG_(emit_amode_offregmem_reg)( Int off, Int regmem, Int reg );
968extern void VG_(emit_amode_ereg_greg) ( Int e_reg, Int g_reg );
njn25e49d8e72002-09-23 09:36:25 +0000969
970/* v-size (4, or 2 with OSO) insn emitters */
njn4ba5a792002-09-30 10:23:54 +0000971extern void VG_(emit_movv_offregmem_reg) ( Int sz, Int off, Int areg, Int reg );
972extern void VG_(emit_movv_reg_offregmem) ( Int sz, Int reg, Int off, Int areg );
973extern void VG_(emit_movv_reg_reg) ( Int sz, Int reg1, Int reg2 );
sewardjfa492d42002-12-08 18:20:01 +0000974extern void VG_(emit_nonshiftopv_lit_reg)( Bool upd_cc, Int sz, Opcode opc, UInt lit,
njn4ba5a792002-09-30 10:23:54 +0000975 Int reg );
sewardjfa492d42002-12-08 18:20:01 +0000976extern void VG_(emit_shiftopv_lit_reg) ( Bool upd_cc, Int sz, Opcode opc, UInt lit,
njn4ba5a792002-09-30 10:23:54 +0000977 Int reg );
sewardjfa492d42002-12-08 18:20:01 +0000978extern void VG_(emit_nonshiftopv_reg_reg)( Bool upd_cc, Int sz, Opcode opc,
njn4ba5a792002-09-30 10:23:54 +0000979 Int reg1, Int reg2 );
980extern void VG_(emit_movv_lit_reg) ( Int sz, UInt lit, Int reg );
sewardjfa492d42002-12-08 18:20:01 +0000981extern void VG_(emit_unaryopv_reg) ( Bool upd_cc, Int sz, Opcode opc, Int reg );
njn4ba5a792002-09-30 10:23:54 +0000982extern void VG_(emit_pushv_reg) ( Int sz, Int reg );
983extern void VG_(emit_popv_reg) ( Int sz, Int reg );
njn25e49d8e72002-09-23 09:36:25 +0000984
njn4ba5a792002-09-30 10:23:54 +0000985extern void VG_(emit_pushl_lit32) ( UInt int32 );
986extern void VG_(emit_pushl_lit8) ( Int lit8 );
sewardjfa492d42002-12-08 18:20:01 +0000987extern void VG_(emit_cmpl_zero_reg) ( Bool upd_cc, Int reg );
njn4ba5a792002-09-30 10:23:54 +0000988extern void VG_(emit_swapl_reg_EAX) ( Int reg );
989extern void VG_(emit_movv_lit_offregmem) ( Int sz, UInt lit, Int off,
990 Int memreg );
njn25e49d8e72002-09-23 09:36:25 +0000991
992/* b-size (1 byte) instruction emitters */
njn4ba5a792002-09-30 10:23:54 +0000993extern void VG_(emit_movb_lit_offregmem) ( UInt lit, Int off, Int memreg );
994extern void VG_(emit_movb_reg_offregmem) ( Int reg, Int off, Int areg );
sewardjfa492d42002-12-08 18:20:01 +0000995extern void VG_(emit_unaryopb_reg) ( Bool upd_cc, Opcode opc, Int reg );
996extern void VG_(emit_testb_lit_reg) ( Bool upd_cc, UInt lit, Int reg );
njn25e49d8e72002-09-23 09:36:25 +0000997
998/* zero-extended load emitters */
njn4ba5a792002-09-30 10:23:54 +0000999extern void VG_(emit_movzbl_offregmem_reg) ( Int off, Int regmem, Int reg );
1000extern void VG_(emit_movzwl_offregmem_reg) ( Int off, Int areg, Int reg );
njn25e49d8e72002-09-23 09:36:25 +00001001
1002/* misc instruction emitters */
njn4ba5a792002-09-30 10:23:54 +00001003extern void VG_(emit_call_reg) ( Int reg );
1004extern void VG_(emit_add_lit_to_esp) ( Int lit );
njn4ba5a792002-09-30 10:23:54 +00001005extern void VG_(emit_pushal) ( void );
1006extern void VG_(emit_popal) ( void );
1007extern void VG_(emit_AMD_prefetch_reg) ( Int reg );
njn25e49d8e72002-09-23 09:36:25 +00001008
sewardja2113f92002-12-12 23:42:48 +00001009/* jump emitters */
1010extern void VG_(init_target) ( Int *tgt );
1011
1012extern void VG_(target_back) ( Int *tgt );
1013extern void VG_(target_forward) ( Int *tgt );
1014extern void VG_(emit_target_delta) ( Int *tgt );
1015
1016extern void VG_(emit_jcondshort_delta) ( Bool simd_cc, Condcode cond, Int delta );
1017extern void VG_(emit_jcondshort_target)( Bool simd_cc, Condcode cond, Int *tgt );
1018
njn25e49d8e72002-09-23 09:36:25 +00001019
1020/*====================================================================*/
1021/*=== Execution contexts ===*/
1022/*====================================================================*/
1023
1024/* Generic resolution type used in a few different ways, such as deciding
1025 how closely to compare two errors for equality. */
1026typedef
1027 enum { Vg_LowRes, Vg_MedRes, Vg_HighRes }
1028 VgRes;
1029
1030typedef
1031 struct _ExeContext
1032 ExeContext;
1033
njnd5bb0a52002-09-27 10:24:48 +00001034/* Compare two ExeContexts. Number of callers considered depends on `res':
1035 Vg_LowRes: 2
1036 Vg_MedRes: 4
1037 Vg_HighRes: all */
njn25e49d8e72002-09-23 09:36:25 +00001038extern Bool VG_(eq_ExeContext) ( VgRes res,
1039 ExeContext* e1, ExeContext* e2 );
1040
1041/* Print an ExeContext. */
1042extern void VG_(pp_ExeContext) ( ExeContext* );
1043
1044/* Take a snapshot of the client's stack. Search our collection of
1045 ExeContexts to see if we already have it, and if not, allocate a
1046 new one. Either way, return a pointer to the context. */
1047extern ExeContext* VG_(get_ExeContext) ( ThreadState *tst );
1048
sewardj499e3de2002-11-13 22:22:25 +00001049/* Just grab the client's EIP, as a much smaller and cheaper
1050 indication of where they are. */
1051extern Addr VG_(get_EIP)( ThreadState *tst );
njn25e49d8e72002-09-23 09:36:25 +00001052
njn211b6ad2003-02-03 12:33:31 +00001053
njn25e49d8e72002-09-23 09:36:25 +00001054/*====================================================================*/
1055/*=== Error reporting ===*/
1056/*====================================================================*/
1057
1058/* ------------------------------------------------------------------ */
1059/* Suppressions describe errors which we want to suppress, ie, not
1060 show the user, usually because it is caused by a problem in a library
1061 which we can't fix, replace or work around. Suppressions are read from
njnd5bb0a52002-09-27 10:24:48 +00001062 a file at startup time. This gives flexibility so that new
njn25e49d8e72002-09-23 09:36:25 +00001063 suppressions can be added to the file as and when needed.
1064*/
1065
1066typedef
1067 Int /* Do not make this unsigned! */
1068 SuppKind;
1069
njn810086f2002-11-14 12:42:47 +00001070/* The skin-relevant parts of a suppression are:
1071 kind: what kind of suppression; must be in the range (0..)
1072 string: use is optional. NULL by default.
1073 extra: use is optional. NULL by default. void* so it's extensible.
njn25e49d8e72002-09-23 09:36:25 +00001074*/
1075typedef
njn810086f2002-11-14 12:42:47 +00001076 struct _Supp
1077 Supp;
1078
1079/* Useful in SK_(error_matches_suppression)() */
1080SuppKind VG_(get_supp_kind) ( Supp* su );
1081Char* VG_(get_supp_string) ( Supp* su );
1082void* VG_(get_supp_extra) ( Supp* su );
1083
1084/* Must be used in VG_(recognised_suppression)() */
1085void VG_(set_supp_kind) ( Supp* su, SuppKind suppkind );
1086/* May be used in VG_(read_extra_suppression_info)() */
1087void VG_(set_supp_string) ( Supp* su, Char* string );
1088void VG_(set_supp_extra) ( Supp* su, void* extra );
njn25e49d8e72002-09-23 09:36:25 +00001089
1090
1091/* ------------------------------------------------------------------ */
1092/* Error records contain enough info to generate an error report. The idea
1093 is that (typically) the same few points in the program generate thousands
njnd5bb0a52002-09-27 10:24:48 +00001094 of errors, and we don't want to spew out a fresh error message for each
1095 one. Instead, we use these structures to common up duplicates.
njn25e49d8e72002-09-23 09:36:25 +00001096*/
1097
1098typedef
1099 Int /* Do not make this unsigned! */
1100 ErrorKind;
1101
njn810086f2002-11-14 12:42:47 +00001102/* The skin-relevant parts of an Error are:
1103 kind: what kind of error; must be in the range (0..)
1104 addr: use is optional. 0 by default.
1105 string: use is optional. NULL by default.
1106 extra: use is optional. NULL by default. void* so it's extensible.
njn25e49d8e72002-09-23 09:36:25 +00001107*/
1108typedef
njn810086f2002-11-14 12:42:47 +00001109 struct _Error
1110 Error;
njn25e49d8e72002-09-23 09:36:25 +00001111
njn810086f2002-11-14 12:42:47 +00001112/* Useful in SK_(error_matches_suppression)(), SK_(pp_SkinError)(), etc */
njnb877d492003-01-28 20:40:57 +00001113ExeContext* VG_(get_error_where) ( Error* err );
1114SuppKind VG_(get_error_kind) ( Error* err );
1115Addr VG_(get_error_address) ( Error* err );
1116Char* VG_(get_error_string) ( Error* err );
1117void* VG_(get_error_extra) ( Error* err );
njn25e49d8e72002-09-23 09:36:25 +00001118
njnd5bb0a52002-09-27 10:24:48 +00001119/* Call this when an error occurs. It will be recorded if it hasn't been
njn25e49d8e72002-09-23 09:36:25 +00001120 seen before. If it has, the existing error record will have its count
1121 incremented.
1122
1123 If the error occurs in generated code, 'tst' should be NULL. If the
1124 error occurs in non-generated code, 'tst' should be non-NULL. The
1125 `extra' field can be stack-allocated; it will be copied (using
1126 SKN_(dup_extra_and_update)()) if needed. But it won't be copied
1127 if it's NULL.
njnd5bb0a52002-09-27 10:24:48 +00001128
1129 If no 'a', 's' or 'extra' of interest needs to be recorded, just use
1130 NULL for them.
njn25e49d8e72002-09-23 09:36:25 +00001131*/
1132extern void VG_(maybe_record_error) ( ThreadState* tst, ErrorKind ekind,
1133 Addr a, Char* s, void* extra );
1134
1135/* Gets a non-blank, non-comment line of at most nBuf chars from fd.
1136 Skips leading spaces on the line. Returns True if EOF was hit instead.
1137 Useful for reading in extra skin-specific suppression lines.
1138*/
njn4ba5a792002-09-30 10:23:54 +00001139extern Bool VG_(get_line) ( Int fd, Char* buf, Int nBuf );
njn25e49d8e72002-09-23 09:36:25 +00001140
1141
1142/*====================================================================*/
1143/*=== Obtaining debug information ===*/
1144/*====================================================================*/
1145
sewardj6e008cb2002-12-15 13:11:39 +00001146/* Get the file/function/line number of the instruction at address
1147 'a'. For these four, if debug info for the address is found, it
1148 copies the info into the buffer/UInt and returns True. If not, it
1149 returns False and nothing is copied. VG_(get_fnname) always
1150 demangles C++ function names. VG_(get_fnname_w_offset) is the
1151 same, except it appends "+N" to symbol names to indicate offsets.
njn25e49d8e72002-09-23 09:36:25 +00001152*/
1153extern Bool VG_(get_filename) ( Addr a, Char* filename, Int n_filename );
1154extern Bool VG_(get_fnname) ( Addr a, Char* fnname, Int n_fnname );
1155extern Bool VG_(get_linenum) ( Addr a, UInt* linenum );
sewardj6e008cb2002-12-15 13:11:39 +00001156extern Bool VG_(get_fnname_w_offset)
1157 ( Addr a, Char* fnname, Int n_fnname );
njn25e49d8e72002-09-23 09:36:25 +00001158
1159/* This one is more efficient if getting both filename and line number,
1160 because the two lookups are done together. */
1161extern Bool VG_(get_filename_linenum)
1162 ( Addr a, Char* filename, Int n_filename,
1163 UInt* linenum );
1164
1165/* Succeeds only if we find from debug info that 'a' is the address of the
1166 first instruction in a function -- as opposed to VG_(get_fnname) which
1167 succeeds if we find from debug info that 'a' is the address of any
1168 instruction in a function. Use this to instrument the start of
njnd5bb0a52002-09-27 10:24:48 +00001169 a particular function. Nb: if an executable/shared object is stripped
njn25e49d8e72002-09-23 09:36:25 +00001170 of its symbols, this function will not be able to recognise function
1171 entry points within it. */
1172extern Bool VG_(get_fnname_if_entry) ( Addr a, Char* filename, Int n_filename );
1173
1174/* Succeeds if the address is within a shared object or the main executable.
1175 It doesn't matter if debug info is present or not. */
1176extern Bool VG_(get_objname) ( Addr a, Char* objname, Int n_objname );
1177
sewardj47104382002-10-20 18:35:48 +00001178/* A way to get information about what segments are mapped */
1179typedef struct _SegInfo SegInfo;
1180
njnb877d492003-01-28 20:40:57 +00001181/* Returns NULL if the SegInfo isn't found. It doesn't matter if debug info
1182 is present or not. */
1183extern SegInfo* VG_(get_obj) ( Addr a );
1184
1185extern const SegInfo* VG_(next_seginfo) ( const SegInfo *seg );
1186extern Addr VG_(seg_start) ( const SegInfo *seg );
1187extern UInt VG_(seg_size) ( const SegInfo *seg );
1188extern const UChar* VG_(seg_filename) ( const SegInfo *seg );
1189extern UInt VG_(seg_sym_offset)( const SegInfo *seg );
sewardj47104382002-10-20 18:35:48 +00001190
1191typedef
1192 enum {
1193 Vg_SectUnknown,
1194 Vg_SectText,
sewardj8fe15a32002-10-20 19:29:21 +00001195 Vg_SectData,
1196 Vg_SectBSS,
sewardj47104382002-10-20 18:35:48 +00001197 Vg_SectGOT,
sewardj8fe15a32002-10-20 19:29:21 +00001198 Vg_SectPLT,
sewardj47104382002-10-20 18:35:48 +00001199 }
1200 VgSectKind;
1201
1202extern VgSectKind VG_(seg_sect_kind)(Addr);
1203
njn25e49d8e72002-09-23 09:36:25 +00001204
1205/*====================================================================*/
1206/*=== Shadow chunks and block-finding ===*/
1207/*====================================================================*/
1208
njn810086f2002-11-14 12:42:47 +00001209/* The skin-relevant parts of a ShadowChunk are:
1210 size: size of the block in bytes
1211 addr: addr of the block
1212 extra: anything extra kept by the skin; size is determined by
1213 VG_(needs).sizeof_shadow_chunk
1214*/
njn25e49d8e72002-09-23 09:36:25 +00001215typedef
njn810086f2002-11-14 12:42:47 +00001216 struct _ShadowChunk
njn25e49d8e72002-09-23 09:36:25 +00001217 ShadowChunk;
1218
njn810086f2002-11-14 12:42:47 +00001219extern UInt VG_(get_sc_size) ( ShadowChunk* sc );
1220extern Addr VG_(get_sc_data) ( ShadowChunk* sc );
1221/* Gets the ith word of the `extra' field. */
1222extern UInt VG_(get_sc_extra) ( ShadowChunk* sc, UInt i );
1223/* Sets the ith word of the `extra' field to `word'. */
1224extern void VG_(set_sc_extra) ( ShadowChunk* sc, UInt i, UInt word );
1225
1226/* These two should only be used if the `alternative_free' need is set, once
1227 we reach the point where the block would have been free'd. */
1228extern ShadowChunk* VG_(get_sc_next) ( ShadowChunk* sc );
1229extern void VG_(set_sc_next) ( ShadowChunk* sc, ShadowChunk* next );
1230
1231
njn25e49d8e72002-09-23 09:36:25 +00001232/* Use this to free blocks if VG_(needs).alternative_free == True.
1233 It frees the ShadowChunk and the malloc'd block it points to. */
njn4ba5a792002-09-30 10:23:54 +00001234extern void VG_(free_ShadowChunk) ( ShadowChunk* sc );
njn25e49d8e72002-09-23 09:36:25 +00001235
1236/* Makes an array of pointers to all the shadow chunks of malloc'd blocks */
1237extern ShadowChunk** VG_(get_malloc_shadows) ( /*OUT*/ UInt* n_shadows );
1238
1239/* Determines if address 'a' is within the bounds of the block at start.
1240 Allows a little 'slop' round the edges. */
1241extern Bool VG_(addr_is_in_block) ( Addr a, Addr start, UInt size );
1242
1243/* Searches through currently malloc'd blocks until a matching one is found.
1244 Returns NULL if none match. Extra arguments can be implicitly passed to
njnd5bb0a52002-09-27 10:24:48 +00001245 p using nested functions; see memcheck/mc_errcontext.c for an example. */
njn25e49d8e72002-09-23 09:36:25 +00001246extern ShadowChunk* VG_(any_matching_mallocd_ShadowChunks)
1247 ( Bool (*p) ( ShadowChunk* ));
1248
1249/* Searches through all thread's stacks to see if any match. Returns
1250 * VG_INVALID_THREADID if none match. */
1251extern ThreadId VG_(any_matching_thread_stack)
1252 ( Bool (*p) ( Addr stack_min, Addr stack_max ));
1253
sewardja4495682002-10-21 07:29:59 +00001254/* Do memory leak detection. */
1255extern void VG_(generic_detect_memory_leaks) (
1256 Bool is_valid_64k_chunk ( UInt ),
1257 Bool is_valid_address ( Addr ),
1258 ExeContext* get_where ( ShadowChunk* ),
1259 VgRes leak_resolution,
sewardj99aac972002-12-26 01:53:45 +00001260 Bool show_reachable,
1261 UInt /*CoreErrorKind*/ leakSupp
sewardja4495682002-10-21 07:29:59 +00001262 );
1263
1264
njn25e49d8e72002-09-23 09:36:25 +00001265/*====================================================================*/
1266/*=== Skin-specific stuff ===*/
1267/*====================================================================*/
1268
njnd04b7c62002-10-03 14:05:52 +00001269/* ------------------------------------------------------------------ */
1270/* Details */
njnd04b7c62002-10-03 14:05:52 +00001271
njn120281f2003-02-03 12:20:07 +00001272/* Default value for avg_translations_sizeB (in bytes), indicating typical
1273 code expansion of about 6:1. */
1274#define VG_DEFAULT_TRANS_SIZEB 100
1275
njn810086f2002-11-14 12:42:47 +00001276/* Information used in the startup message. `name' also determines the
1277 string used for identifying suppressions in a suppression file as
1278 belonging to this skin. `version' can be NULL, in which case (not
1279 surprisingly) no version info is printed; this mechanism is designed for
1280 skins distributed with Valgrind that share a version number with
1281 Valgrind. Other skins not distributed as part of Valgrind should
sewardjc0d8f682002-11-30 00:49:43 +00001282 probably have their own version number. */
1283extern void VG_(details_name) ( Char* name );
1284extern void VG_(details_version) ( Char* version );
1285extern void VG_(details_description) ( Char* description );
1286extern void VG_(details_copyright_author) ( Char* copyright_author );
1287
1288/* Average size of a translation, in bytes, so that the translation
njn120281f2003-02-03 12:20:07 +00001289 storage machinery can allocate memory appropriately. Not critical,
1290 setting is optional. */
1291extern void VG_(details_avg_translation_sizeB) ( UInt size );
njnd04b7c62002-10-03 14:05:52 +00001292
njn810086f2002-11-14 12:42:47 +00001293/* String printed if an `sk_assert' assertion fails or VG_(skin_panic)
1294 is called. Should probably be an email address. */
1295extern void VG_(details_bug_reports_to) ( Char* bug_reports_to );
njnd04b7c62002-10-03 14:05:52 +00001296
1297/* ------------------------------------------------------------------ */
1298/* Needs */
1299
njn810086f2002-11-14 12:42:47 +00001300/* Booleans that decide core behaviour, but don't require extra
1301 operations to be defined if `True' */
njnd5bb0a52002-09-27 10:24:48 +00001302
njn810086f2002-11-14 12:42:47 +00001303/* Should __libc_freeres() be run? Bugs in it can crash the skin. */
1304extern void VG_(needs_libc_freeres) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001305
njn810086f2002-11-14 12:42:47 +00001306/* Want to have errors detected by Valgrind's core reported? Includes:
1307 - pthread API errors (many; eg. unlocking a non-locked mutex)
1308 - silly arguments to malloc() et al (eg. negative size)
1309 - invalid file descriptors to blocking syscalls read() and write()
1310 - bad signal numbers passed to sigaction()
1311 - attempt to install signal handler for SIGKILL or SIGSTOP */
1312extern void VG_(needs_core_errors) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001313
njn810086f2002-11-14 12:42:47 +00001314/* Booleans that indicate extra operations are defined; if these are True,
1315 the corresponding template functions (given below) must be defined. A
1316 lot like being a member of a type class. */
njn25e49d8e72002-09-23 09:36:25 +00001317
njn810086f2002-11-14 12:42:47 +00001318/* Want to report errors from skin? This implies use of suppressions, too. */
1319extern void VG_(needs_skin_errors) ( void );
njnd5bb0a52002-09-27 10:24:48 +00001320
njn810086f2002-11-14 12:42:47 +00001321/* Is information kept about specific individual basic blocks? (Eg. for
1322 cachegrind there are cost-centres for every instruction, stored at a
1323 basic block level.) If so, it sometimes has to be discarded, because
1324 .so mmap/munmap-ping or self-modifying code (informed by the
1325 DISCARD_TRANSLATIONS user request) can cause one instruction address
1326 to be used for more than one instruction in one program run... */
1327extern void VG_(needs_basic_block_discards) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001328
njn810086f2002-11-14 12:42:47 +00001329/* Skin maintains information about each register? */
1330extern void VG_(needs_shadow_regs) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001331
njn810086f2002-11-14 12:42:47 +00001332/* Skin defines its own command line options? */
1333extern void VG_(needs_command_line_options) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001334
njn810086f2002-11-14 12:42:47 +00001335/* Skin defines its own client requests? */
1336extern void VG_(needs_client_requests) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001337
njn810086f2002-11-14 12:42:47 +00001338/* Skin defines its own UInstrs? */
1339extern void VG_(needs_extended_UCode) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001340
njn810086f2002-11-14 12:42:47 +00001341/* Skin does stuff before and/or after system calls? */
1342extern void VG_(needs_syscall_wrapper) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001343
njn810086f2002-11-14 12:42:47 +00001344/* Size, in words, of extra info about malloc'd blocks recorded by
1345 skin. Be careful to get this right or you'll get seg faults! */
1346extern void VG_(needs_sizeof_shadow_block) ( Int size );
njn25e49d8e72002-09-23 09:36:25 +00001347
njn810086f2002-11-14 12:42:47 +00001348/* Skin does free()s itself? Useful if a skin needs to keep track of
1349 blocks in some way after they're free'd.
1350 WARNING: don't forget to call VG_(free_ShadowChunk)() for each block
1351 eventually! */
1352extern void VG_(needs_alternative_free) ( void );
sewardj8fe15a32002-10-20 19:29:21 +00001353
njn810086f2002-11-14 12:42:47 +00001354/* Are skin-state sanity checks performed? */
1355extern void VG_(needs_sanity_checks) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001356
njn810086f2002-11-14 12:42:47 +00001357/* Do we need to see data symbols? */
1358extern void VG_(needs_data_syms) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001359
1360/* ------------------------------------------------------------------ */
1361/* Core events to track */
1362
1363/* Part of the core from which this call was made. Useful for determining
njnd5bb0a52002-09-27 10:24:48 +00001364 what kind of error message should be emitted. */
njn25e49d8e72002-09-23 09:36:25 +00001365typedef
1366 enum { Vg_CorePThread, Vg_CoreSignal, Vg_CoreSysCall, Vg_CoreTranslate }
1367 CorePart;
1368
njn810086f2002-11-14 12:42:47 +00001369#define EV extern void
njn25e49d8e72002-09-23 09:36:25 +00001370
njn810086f2002-11-14 12:42:47 +00001371/* Events happening in core to track. To be notified, pass a callback
1372 function to the appropriate function. To ignore an event, don't do
1373 anything (default is for events to be ignored). */
1374
1375/* Memory events */
1376
1377EV VG_(track_new_mem_startup) ( void (*f)(Addr a, UInt len,
1378 Bool rr, Bool ww, Bool xx) );
1379EV VG_(track_new_mem_heap) ( void (*f)(Addr a, UInt len, Bool is_inited) );
1380EV VG_(track_new_mem_stack) ( void (*f)(Addr a, UInt len) );
1381EV VG_(track_new_mem_stack_aligned) ( void (*f)(Addr a, UInt len) );
1382EV VG_(track_new_mem_stack_signal) ( void (*f)(Addr a, UInt len) );
1383EV VG_(track_new_mem_brk) ( void (*f)(Addr a, UInt len) );
1384EV VG_(track_new_mem_mmap) ( void (*f)(Addr a, UInt len,
1385 Bool rr, Bool ww, Bool xx) );
1386
1387EV VG_(track_copy_mem_heap) ( void (*f)(Addr from, Addr to, UInt len) );
1388EV VG_(track_copy_mem_remap) ( void (*f)(Addr from, Addr to, UInt len) );
1389EV VG_(track_change_mem_mprotect) ( void (*f)(Addr a, UInt len,
1390 Bool rr, Bool ww, Bool xx) );
njn25e49d8e72002-09-23 09:36:25 +00001391
njn810086f2002-11-14 12:42:47 +00001392/* Used on redzones around malloc'd blocks and at end of stack */
1393EV VG_(track_ban_mem_heap) ( void (*f)(Addr a, UInt len) );
1394EV VG_(track_ban_mem_stack) ( void (*f)(Addr a, UInt len) );
njn25e49d8e72002-09-23 09:36:25 +00001395
njn810086f2002-11-14 12:42:47 +00001396EV VG_(track_die_mem_heap) ( void (*f)(Addr a, UInt len) );
1397EV VG_(track_die_mem_stack) ( void (*f)(Addr a, UInt len) );
1398EV VG_(track_die_mem_stack_aligned) ( void (*f)(Addr a, UInt len) );
1399EV VG_(track_die_mem_stack_signal) ( void (*f)(Addr a, UInt len) );
1400EV VG_(track_die_mem_brk) ( void (*f)(Addr a, UInt len) );
1401EV VG_(track_die_mem_munmap) ( void (*f)(Addr a, UInt len) );
njn25e49d8e72002-09-23 09:36:25 +00001402
njn810086f2002-11-14 12:42:47 +00001403EV VG_(track_bad_free) ( void (*f)(ThreadState* tst, Addr a) );
1404EV VG_(track_mismatched_free) ( void (*f)(ThreadState* tst, Addr a) );
njn25e49d8e72002-09-23 09:36:25 +00001405
njn810086f2002-11-14 12:42:47 +00001406EV VG_(track_pre_mem_read) ( void (*f)(CorePart part, ThreadState* tst,
1407 Char* s, Addr a, UInt size) );
1408EV VG_(track_pre_mem_read_asciiz) ( void (*f)(CorePart part, ThreadState* tst,
1409 Char* s, Addr a) );
1410EV VG_(track_pre_mem_write) ( void (*f)(CorePart part, ThreadState* tst,
1411 Char* s, Addr a, UInt size) );
1412/* Not implemented yet -- have to add in lots of places, which is a
1413 pain. Won't bother unless/until there's a need. */
1414/* EV VG_(track_post_mem_read) ( void (*f)(ThreadState* tst, Char* s,
1415 Addr a, UInt size) ); */
1416EV VG_(track_post_mem_write) ( void (*f)(Addr a, UInt size) );
njn25e49d8e72002-09-23 09:36:25 +00001417
1418
njn810086f2002-11-14 12:42:47 +00001419/* Scheduler events (not exhaustive) */
njn25e49d8e72002-09-23 09:36:25 +00001420
njn810086f2002-11-14 12:42:47 +00001421EV VG_(track_thread_run) ( void (*f)(ThreadId tid) );
njn25e49d8e72002-09-23 09:36:25 +00001422
njn810086f2002-11-14 12:42:47 +00001423/* Thread events (not exhaustive) */
sewardjdca84112002-11-13 22:29:34 +00001424
njn810086f2002-11-14 12:42:47 +00001425/* Called during thread create, before the new thread has run any
1426 instructions (or touched any memory). */
1427EV VG_(track_post_thread_create)( void (*f)(ThreadId tid, ThreadId child) );
1428/* Called once the joinee thread is terminated and the joining thread is
1429 about to resume. */
1430EV VG_(track_post_thread_join) ( void (*f)(ThreadId joiner, ThreadId joinee) );
njn25e49d8e72002-09-23 09:36:25 +00001431
njnd5bb0a52002-09-27 10:24:48 +00001432
njn810086f2002-11-14 12:42:47 +00001433/* Mutex events (not exhaustive) */
njn25e49d8e72002-09-23 09:36:25 +00001434
njn810086f2002-11-14 12:42:47 +00001435/* Called before a thread can block while waiting for a mutex (called
1436 regardless of whether the thread will block or not). */
1437EV VG_(track_pre_mutex_lock) ( void (*f)(ThreadId tid,
1438 void* /*pthread_mutex_t* */ mutex) );
1439/* Called once the thread actually holds the mutex (always paired with
1440 pre_mutex_lock). */
1441EV VG_(track_post_mutex_lock) ( void (*f)(ThreadId tid,
1442 void* /*pthread_mutex_t* */ mutex) );
1443/* Called after a thread has released a mutex (no need for a corresponding
1444 pre_mutex_unlock, because unlocking can't block). */
1445EV VG_(track_post_mutex_unlock) ( void (*f)(ThreadId tid,
1446 void* /*pthread_mutex_t* */ mutex) );
njn25e49d8e72002-09-23 09:36:25 +00001447
njn810086f2002-11-14 12:42:47 +00001448/* Others... condition variable, signal events... */
1449/* ... */
1450
1451#undef EV
njn25e49d8e72002-09-23 09:36:25 +00001452
1453/* ------------------------------------------------------------------ */
1454/* Template functions */
1455
1456/* These are the parameterised functions in the core. The default definitions
njnd5bb0a52002-09-27 10:24:48 +00001457 are overridden by LD_PRELOADed skin version. At the very least, a skin
1458 must define the fundamental template functions. Depending on what needs
1459 are set, extra template functions will be used too. Functions are
1460 grouped under the needs that govern their use. */
njn25e49d8e72002-09-23 09:36:25 +00001461
1462
1463/* ------------------------------------------------------------------ */
1464/* Fundamental template functions */
1465
1466/* Initialise skin. Must do the following:
njn810086f2002-11-14 12:42:47 +00001467 - initialise the `details' struct, via the VG_(details_*)() functions
njn25e49d8e72002-09-23 09:36:25 +00001468 - register any helpers called by generated code
1469
1470 May do the following:
njn810086f2002-11-14 12:42:47 +00001471 - initialise the `needs' struct to indicate certain requirements, via
1472 the VG_(needs_*)() functions
1473 - initialise the `track' struct to indicate core events of interest, via
1474 the VG_(track_*)() functions
njn25e49d8e72002-09-23 09:36:25 +00001475 - register any skin-specific profiling events
1476 - any other skin-specific initialisation
1477*/
njn810086f2002-11-14 12:42:47 +00001478extern void SK_(pre_clo_init) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001479
njnd5bb0a52002-09-27 10:24:48 +00001480/* Do initialisation that can only be done after command line processing. */
njn25e49d8e72002-09-23 09:36:25 +00001481extern void SK_(post_clo_init)( void );
1482
1483/* Instrument a basic block. Must be a true function, ie. the same input
1484 always results in the same output, because basic blocks can be
1485 retranslated. Unless you're doing something really strange...
1486 'orig_addr' is the address of the first instruction in the block. */
1487extern UCodeBlock* SK_(instrument) ( UCodeBlock* cb, Addr orig_addr );
1488
1489/* Finish up, print out any results, etc. */
1490extern void SK_(fini) ( void );
1491
1492
1493/* ------------------------------------------------------------------ */
1494/* VG_(needs).report_errors */
1495
1496/* Identify if two errors are equal, or equal enough. `res' indicates how
1497 close is "close enough". `res' should be passed on as necessary, eg. if
njn810086f2002-11-14 12:42:47 +00001498 the Error's `extra' part contains an ExeContext, `res' should be
njn25e49d8e72002-09-23 09:36:25 +00001499 passed to VG_(eq_ExeContext)() if the ExeContexts are considered. Other
1500 than that, probably don't worry about it unless you have lots of very
1501 similar errors occurring.
1502 */
njn810086f2002-11-14 12:42:47 +00001503extern Bool SK_(eq_SkinError) ( VgRes res, Error* e1, Error* e2 );
njn25e49d8e72002-09-23 09:36:25 +00001504
1505/* Print error context. The passed function pp_ExeContext() can be (and
1506 probably should be) used to print the location of the error. */
njn810086f2002-11-14 12:42:47 +00001507extern void SK_(pp_SkinError) ( Error* err, void (*pp_ExeContext)(void) );
njn25e49d8e72002-09-23 09:36:25 +00001508
njn810086f2002-11-14 12:42:47 +00001509/* Should copy the `extra' part which the core uses to override the old
1510 version. This is necessary to move from a temporary stack copy to a
1511 permanent heap one.
njn25e49d8e72002-09-23 09:36:25 +00001512
njn810086f2002-11-14 12:42:47 +00001513 Then should fill in any details that could be postponed until after the
1514 decision whether to ignore the error (ie. details not affecting the
1515 result of SK_(eq_SkinError)()). This saves time when errors are ignored.
njn25e49d8e72002-09-23 09:36:25 +00001516
1517 Yuk.
1518*/
njn810086f2002-11-14 12:42:47 +00001519extern void* SK_(dup_extra_and_update) ( Error* err );
njn25e49d8e72002-09-23 09:36:25 +00001520
njn810086f2002-11-14 12:42:47 +00001521/* Return value indicates recognition. If recognised, must set skind using
1522 VG_(set_supp_kind)(). */
1523extern Bool SK_(recognised_suppression) ( Char* name, Supp* su );
njn25e49d8e72002-09-23 09:36:25 +00001524
njn810086f2002-11-14 12:42:47 +00001525/* Read any extra info for this suppression kind. Most likely for filling
1526 in the `extra' and `string' parts (with VG_(set_supp_{extra,string})())
1527 of a suppression if necessary. Should return False if a syntax error
1528 occurred, True otherwise. */
1529extern Bool SK_(read_extra_suppression_info) ( Int fd, Char* buf, Int nBuf,
1530 Supp* su );
njn25e49d8e72002-09-23 09:36:25 +00001531
1532/* This should just check the kinds match and maybe some stuff in the
njn810086f2002-11-14 12:42:47 +00001533 `string' and `extra' field if appropriate (using VG_(get_supp_*)() to
1534 get the relevant suppression parts). */
1535extern Bool SK_(error_matches_suppression)(Error* err, Supp* su);
njn25e49d8e72002-09-23 09:36:25 +00001536
1537
1538/* ------------------------------------------------------------------ */
1539/* VG_(needs).basic_block_discards */
1540
njnd5bb0a52002-09-27 10:24:48 +00001541/* Should discard any information that pertains to specific basic blocks
1542 or instructions within the address range given. */
njn25e49d8e72002-09-23 09:36:25 +00001543extern void SK_(discard_basic_block_info) ( Addr a, UInt size );
1544
1545
1546/* ------------------------------------------------------------------ */
1547/* VG_(needs).shadow_regs */
1548
1549/* Valid values for general registers and EFLAGS register, for initialising
1550 and updating registers when written in certain places in core. */
1551extern void SK_(written_shadow_regs_values) ( UInt* gen_reg, UInt* eflags );
1552
1553
1554/* ------------------------------------------------------------------ */
1555/* VG_(needs).command_line_options */
1556
njnd5bb0a52002-09-27 10:24:48 +00001557/* Return True if option was recognised. Presumably sets some state to
1558 record the option as well. */
1559extern Bool SK_(process_cmd_line_option) ( Char* argv );
njn25e49d8e72002-09-23 09:36:25 +00001560
1561/* Print out command line usage for skin options */
1562extern Char* SK_(usage) ( void );
1563
1564
1565/* ------------------------------------------------------------------ */
1566/* VG_(needs).client_requests */
1567
sewardj34042512002-10-22 04:14:35 +00001568extern Bool SK_(handle_client_request) ( ThreadState* tst, UInt* arg_block, UInt *ret );
njn25e49d8e72002-09-23 09:36:25 +00001569
1570
1571/* ------------------------------------------------------------------ */
1572/* VG_(needs).extends_UCode */
1573
njn4ba5a792002-09-30 10:23:54 +00001574/* Useful to use in VG_(get_Xreg_usage)() */
njn810086f2002-11-14 12:42:47 +00001575#define VG_UINSTR_READS_REG(ono,regs,isWrites) \
njn25e49d8e72002-09-23 09:36:25 +00001576 { if (mycat(u->tag,ono) == tag) \
njn810086f2002-11-14 12:42:47 +00001577 { regs[n] = mycat(u->val,ono); \
1578 isWrites[n] = False; \
njn25e49d8e72002-09-23 09:36:25 +00001579 n++; \
1580 } \
1581 }
njn810086f2002-11-14 12:42:47 +00001582#define VG_UINSTR_WRITES_REG(ono,regs,isWrites) \
njnd5bb0a52002-09-27 10:24:48 +00001583 { if (mycat(u->tag,ono) == tag) \
njn810086f2002-11-14 12:42:47 +00001584 { regs[n] = mycat(u->val,ono); \
1585 isWrites[n] = True; \
njnd5bb0a52002-09-27 10:24:48 +00001586 n++; \
1587 } \
njn25e49d8e72002-09-23 09:36:25 +00001588 }
1589
njn4ba5a792002-09-30 10:23:54 +00001590/* 'X' prefix indicates eXtended UCode. */
njn810086f2002-11-14 12:42:47 +00001591extern Int SK_(get_Xreg_usage) ( UInstr* u, Tag tag, Int* regs,
1592 Bool* isWrites );
njn4ba5a792002-09-30 10:23:54 +00001593extern void SK_(emit_XUInstr) ( UInstr* u, RRegSet regs_live_before );
1594extern Bool SK_(sane_XUInstr) ( Bool beforeRA, Bool beforeLiveness,
njn25e49d8e72002-09-23 09:36:25 +00001595 UInstr* u );
njn4ba5a792002-09-30 10:23:54 +00001596extern Char* SK_(name_XUOpcode) ( Opcode opc );
1597extern void SK_(pp_XUInstr) ( UInstr* u );
njn25e49d8e72002-09-23 09:36:25 +00001598
1599
1600/* ------------------------------------------------------------------ */
1601/* VG_(needs).syscall_wrapper */
1602
1603/* If either of the pre_ functions malloc() something to return, the
1604 * corresponding post_ function had better free() it!
1605 */
1606extern void* SK_( pre_syscall) ( ThreadId tid, UInt syscallno,
1607 Bool is_blocking );
1608extern void SK_(post_syscall) ( ThreadId tid, UInt syscallno,
1609 void* pre_result, Int res,
1610 Bool is_blocking );
1611
njnd5bb0a52002-09-27 10:24:48 +00001612
njn25e49d8e72002-09-23 09:36:25 +00001613/* ------------------------------------------------------------------ */
njnd5bb0a52002-09-27 10:24:48 +00001614/* VG_(needs).sizeof_shadow_chunk (if > 0) */
njn25e49d8e72002-09-23 09:36:25 +00001615
njn810086f2002-11-14 12:42:47 +00001616/* Must fill in the `extra' part, using VG_(set_sc_extra)(). */
njn25e49d8e72002-09-23 09:36:25 +00001617extern void SK_(complete_shadow_chunk) ( ShadowChunk* sc, ThreadState* tst );
1618
1619
1620/* ------------------------------------------------------------------ */
1621/* VG_(needs).alternative_free */
1622
njn810086f2002-11-14 12:42:47 +00001623/* If this need is set, when a dynamic block would normally be free'd, this
1624 is called instead. The block is contained inside the ShadowChunk; use
1625 the VG_(get_sc_*)() functions to access it. */
njn25e49d8e72002-09-23 09:36:25 +00001626extern void SK_(alt_free) ( ShadowChunk* sc, ThreadState* tst );
1627
njnd5bb0a52002-09-27 10:24:48 +00001628
njn25e49d8e72002-09-23 09:36:25 +00001629/* ---------------------------------------------------------------------
1630 VG_(needs).sanity_checks */
1631
njnd5bb0a52002-09-27 10:24:48 +00001632/* Can be useful for ensuring a skin's correctness. SK_(cheap_sanity_check)
1633 is called very frequently; SK_(expensive_sanity_check) is called less
1634 frequently and can be more involved. */
njn25e49d8e72002-09-23 09:36:25 +00001635extern Bool SK_(cheap_sanity_check) ( void );
1636extern Bool SK_(expensive_sanity_check) ( void );
1637
1638
1639#endif /* NDEF __VG_SKIN_H */
1640
1641/*--------------------------------------------------------------------*/
1642/*--- end vg_skin.h ---*/
1643/*--------------------------------------------------------------------*/
1644