blob: 1ff3f808d3fcddf84b3ad17627c2f20e6455be92 [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
njnf4ce3d32003-02-10 10:17:26 +0000292 additional useful functions are provided too; descriptions of how they
293 work are given below. */
njn25e49d8e72002-09-23 09:36:25 +0000294
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 );
njn6d68e792003-02-24 10:49:08 +0000368extern Int VG_(memcmp) ( const void* s1, const void* s2, Int n );
njn25e49d8e72002-09-23 09:36:25 +0000369
njnd5bb0a52002-09-27 10:24:48 +0000370/* Like strcmp() and strncmp(), but stop comparing at any whitespace. */
njn25e49d8e72002-09-23 09:36:25 +0000371extern Int VG_(strcmp_ws) ( const Char* s1, const Char* s2 );
njn25e49d8e72002-09-23 09:36:25 +0000372extern Int VG_(strncmp_ws) ( const Char* s1, const Char* s2, Int nmax );
373
njnd5bb0a52002-09-27 10:24:48 +0000374/* Like strncpy(), but if 'src' is longer than 'ndest' inserts a '\0' as the
375 last character. */
njn25e49d8e72002-09-23 09:36:25 +0000376extern void VG_(strncpy_safely) ( Char* dest, const Char* src, Int ndest );
377
378/* Mini-regexp function. Searches for 'pat' in 'str'. Supports
379 * meta-symbols '*' and '?'. '\' escapes meta-symbols. */
njn4ba5a792002-09-30 10:23:54 +0000380extern Bool VG_(string_match) ( Char* pat, Char* str );
njn25e49d8e72002-09-23 09:36:25 +0000381
382
383/* ------------------------------------------------------------------ */
384/* math.h */
njnd5bb0a52002-09-27 10:24:48 +0000385/* Returns the base-2 logarithm of x. */
njn25e49d8e72002-09-23 09:36:25 +0000386extern Int VG_(log2) ( Int x );
387
388
389/* ------------------------------------------------------------------ */
njnd5bb0a52002-09-27 10:24:48 +0000390/* unistd.h, fcntl.h, sys/stat.h */
sewardj4cf05692002-10-27 20:28:29 +0000391extern Int VG_(getpid) ( void );
392extern Int VG_(getppid) ( void );
njnd5bb0a52002-09-27 10:24:48 +0000393
njn4aca2d22002-10-04 10:29:38 +0000394extern Int VG_(open) ( const Char* pathname, Int flags, Int mode );
395extern Int VG_(read) ( Int fd, void* buf, Int count);
396extern Int VG_(write) ( Int fd, void* buf, Int count);
397extern void VG_(close) ( Int fd );
njnd5bb0a52002-09-27 10:24:48 +0000398
njn41557122002-10-14 09:25:37 +0000399/* Nb: VG_(rename)() declared in stdio.h section above */
njn4aca2d22002-10-04 10:29:38 +0000400extern Int VG_(unlink) ( Char* file_name );
401extern Int VG_(stat) ( Char* file_name, struct vki_stat* buf );
njn25e49d8e72002-09-23 09:36:25 +0000402
403
404/* ------------------------------------------------------------------ */
405/* assert.h */
406/* Asserts permanently enabled -- no turning off with NDEBUG. Hurrah! */
407#define VG__STRING(__str) #__str
408
njne427a662002-10-02 11:08:25 +0000409#define sk_assert(expr) \
njn25e49d8e72002-09-23 09:36:25 +0000410 ((void) ((expr) ? 0 : \
njne427a662002-10-02 11:08:25 +0000411 (VG_(skin_assert_fail) (VG__STRING(expr), \
412 __FILE__, __LINE__, \
413 __PRETTY_FUNCTION__), 0)))
njn25e49d8e72002-09-23 09:36:25 +0000414
njne427a662002-10-02 11:08:25 +0000415__attribute__ ((__noreturn__))
416extern void VG_(skin_assert_fail) ( Char* expr, Char* file,
417 Int line, Char* fn );
njn25e49d8e72002-09-23 09:36:25 +0000418
419
420/* ------------------------------------------------------------------ */
njnd5bb0a52002-09-27 10:24:48 +0000421/* system/mman.h */
njn25e49d8e72002-09-23 09:36:25 +0000422extern void* VG_(mmap)( void* start, UInt length,
423 UInt prot, UInt flags, UInt fd, UInt offset );
424extern Int VG_(munmap)( void* start, Int length );
425
426/* Get memory by anonymous mmap. */
427extern void* VG_(get_memory_from_mmap) ( Int nBytes, Char* who );
428
429
430/* ------------------------------------------------------------------ */
431/* signal.h.
432
433 Note that these use the vk_ (kernel) structure
434 definitions, which are different in places from those that glibc
435 defines -- hence the 'k' prefix. Since we're operating right at the
436 kernel interface, glibc's view of the world is entirely irrelevant. */
437
438/* --- Signal set ops --- */
njnd5bb0a52002-09-27 10:24:48 +0000439extern Int VG_(ksigfillset) ( vki_ksigset_t* set );
440extern Int VG_(ksigemptyset) ( vki_ksigset_t* set );
njn25e49d8e72002-09-23 09:36:25 +0000441
njnd5bb0a52002-09-27 10:24:48 +0000442extern Bool VG_(kisfullsigset) ( vki_ksigset_t* set );
443extern Bool VG_(kisemptysigset) ( vki_ksigset_t* set );
njn25e49d8e72002-09-23 09:36:25 +0000444
njnd5bb0a52002-09-27 10:24:48 +0000445extern Int VG_(ksigaddset) ( vki_ksigset_t* set, Int signum );
446extern Int VG_(ksigdelset) ( vki_ksigset_t* set, Int signum );
njn25e49d8e72002-09-23 09:36:25 +0000447extern Int VG_(ksigismember) ( vki_ksigset_t* set, Int signum );
448
njnd5bb0a52002-09-27 10:24:48 +0000449extern void VG_(ksigaddset_from_set) ( vki_ksigset_t* dst, vki_ksigset_t* src );
450extern void VG_(ksigdelset_from_set) ( vki_ksigset_t* dst, vki_ksigset_t* src );
njn25e49d8e72002-09-23 09:36:25 +0000451
452/* --- Mess with the kernel's sig state --- */
njnd5bb0a52002-09-27 10:24:48 +0000453extern Int VG_(ksigprocmask) ( Int how, const vki_ksigset_t* set,
njn25e49d8e72002-09-23 09:36:25 +0000454 vki_ksigset_t* oldset );
njnd5bb0a52002-09-27 10:24:48 +0000455extern Int VG_(ksigaction) ( Int signum,
456 const vki_ksigaction* act,
457 vki_ksigaction* oldact );
njn25e49d8e72002-09-23 09:36:25 +0000458
njnd5bb0a52002-09-27 10:24:48 +0000459extern Int VG_(ksignal) ( Int signum, void (*sighandler)(Int) );
460extern Int VG_(ksigaltstack) ( const vki_kstack_t* ss, vki_kstack_t* oss );
njn25e49d8e72002-09-23 09:36:25 +0000461
sewardjdcaf3122002-09-30 23:12:33 +0000462extern Int VG_(kkill) ( Int pid, Int signo );
463extern Int VG_(ksigpending) ( vki_ksigset_t* set );
njn25e49d8e72002-09-23 09:36:25 +0000464
465
466/*====================================================================*/
467/*=== UCode definition ===*/
468/*====================================================================*/
469
sewardje1042472002-09-30 12:33:11 +0000470/* Tags which describe what operands are. Must fit into 4 bits, which
471 they clearly do. */
njn25e49d8e72002-09-23 09:36:25 +0000472typedef
sewardje1042472002-09-30 12:33:11 +0000473enum { TempReg =0, /* virtual temp-reg */
474 ArchReg =1, /* simulated integer reg */
475 ArchRegS =2, /* simulated segment reg */
476 RealReg =3, /* real machine's real reg */
477 SpillNo =4, /* spill slot location */
478 Literal =5, /* literal; .lit32 field has actual value */
479 Lit16 =6, /* literal; .val[123] field has actual value */
480 NoValue =7 /* operand not in use */
481 }
482 Tag;
njn25e49d8e72002-09-23 09:36:25 +0000483
njnd5bb0a52002-09-27 10:24:48 +0000484/* Invalid register numbers (can't be negative) */
njn25e49d8e72002-09-23 09:36:25 +0000485#define INVALID_TEMPREG 999999999
486#define INVALID_REALREG 999999999
487
488/* Microinstruction opcodes. */
489typedef
490 enum {
njnd5bb0a52002-09-27 10:24:48 +0000491 NOP, /* Null op */
njn25e49d8e72002-09-23 09:36:25 +0000492
sewardj7a5ebcf2002-11-13 22:42:13 +0000493 LOCK, /* Indicate the existance of a LOCK prefix (functionally NOP) */
494
njnd5bb0a52002-09-27 10:24:48 +0000495 /* Moving values around */
496 GET, PUT, /* simulated register <--> TempReg */
497 GETF, PUTF, /* simulated %eflags <--> TempReg */
498 LOAD, STORE, /* memory <--> TempReg */
499 MOV, /* TempReg <--> TempReg */
500 CMOV, /* Used for cmpxchg and cmov */
njn25e49d8e72002-09-23 09:36:25 +0000501
njnd5bb0a52002-09-27 10:24:48 +0000502 /* Arithmetic/logical ops */
503 ADD, ADC, SUB, SBB, /* Add/subtract (w/wo carry) */
504 AND, OR, XOR, NOT, /* Boolean ops */
505 SHL, SHR, SAR, ROL, ROR, RCL, RCR, /* Shift/rotate (w/wo carry) */
506 NEG, /* Negate */
507 INC, DEC, /* Increment/decrement */
508 BSWAP, /* Big-endian <--> little-endian */
509 CC2VAL, /* Condition code --> 0 or 1 */
510 WIDEN, /* Signed or unsigned widening */
njn25e49d8e72002-09-23 09:36:25 +0000511
njnd5bb0a52002-09-27 10:24:48 +0000512 /* Conditional or unconditional jump */
513 JMP,
514
515 /* FPU ops */
516 FPU, /* Doesn't touch memory */
517 FPU_R, FPU_W, /* Reads/writes memory */
518
519 /* Not strictly needed, but improve address calculation translations. */
njn25e49d8e72002-09-23 09:36:25 +0000520 LEA1, /* reg2 := const + reg1 */
521 LEA2, /* reg3 := const + reg1 + reg2 * 1,2,4 or 8 */
522
njnd5bb0a52002-09-27 10:24:48 +0000523 /* Hack for x86 REP insns. Jump to literal if TempReg/RealReg is zero. */
524 JIFZ,
njn25e49d8e72002-09-23 09:36:25 +0000525
njnd5bb0a52002-09-27 10:24:48 +0000526 /* Advance the simulated %eip by some small (< 128) number. */
527 INCEIP,
njn25e49d8e72002-09-23 09:36:25 +0000528
sewardje1042472002-09-30 12:33:11 +0000529 /* Dealing with segment registers */
530 GETSEG, PUTSEG, /* simulated segment register <--> TempReg */
531 USESEG, /* (LDT/GDT index, virtual addr) --> linear addr */
532
njnd5bb0a52002-09-27 10:24:48 +0000533 /* Not for translating x86 calls -- only to call helpers */
534 CALLM_S, CALLM_E, /* Mark start/end of CALLM push/pop sequence */
535 PUSH, POP, CLEAR, /* Add/remove/zap args for helpers */
536 CALLM, /* Call assembly-code helper */
537
538 /* Not for translating x86 calls -- only to call C helper functions of
539 up to three arguments (or two if the functions has a return value).
540 Arguments and return value must be word-sized. More arguments can
541 be faked with global variables (eg. use VG_(set_global_var)()).
542
543 Seven possibilities: 'arg[123]' show where args go, 'ret' shows
544 where return value goes (if present).
njn25e49d8e72002-09-23 09:36:25 +0000545
546 CCALL(-, -, - ) void f(void)
547 CCALL(arg1, -, - ) void f(UInt arg1)
548 CCALL(arg1, arg2, - ) void f(UInt arg1, UInt arg2)
549 CCALL(arg1, arg2, arg3) void f(UInt arg1, UInt arg2, UInt arg3)
550 CCALL(-, -, ret ) UInt f(UInt)
551 CCALL(arg1, -, ret ) UInt f(UInt arg1)
njnd5bb0a52002-09-27 10:24:48 +0000552 CCALL(arg1, arg2, ret ) UInt f(UInt arg1, UInt arg2) */
njn25e49d8e72002-09-23 09:36:25 +0000553 CCALL,
554
njnd5bb0a52002-09-27 10:24:48 +0000555 /* This opcode makes it easy for skins that extend UCode to do this to
556 avoid opcode overlap:
njn25e49d8e72002-09-23 09:36:25 +0000557
njnd5bb0a52002-09-27 10:24:48 +0000558 enum { EU_OP1 = DUMMY_FINAL_UOPCODE + 1, ... }
njn25e49d8e72002-09-23 09:36:25 +0000559
560 WARNING: Do not add new opcodes after this one! They can be added
561 before, though. */
562 DUMMY_FINAL_UOPCODE
563 }
564 Opcode;
565
566
njnd5bb0a52002-09-27 10:24:48 +0000567/* Condition codes, using the Intel encoding. CondAlways is an extra. */
njn25e49d8e72002-09-23 09:36:25 +0000568typedef
569 enum {
570 CondO = 0, /* overflow */
571 CondNO = 1, /* no overflow */
572 CondB = 2, /* below */
573 CondNB = 3, /* not below */
574 CondZ = 4, /* zero */
575 CondNZ = 5, /* not zero */
576 CondBE = 6, /* below or equal */
577 CondNBE = 7, /* not below or equal */
578 CondS = 8, /* negative */
sewardje1042472002-09-30 12:33:11 +0000579 CondNS = 9, /* not negative */
njn25e49d8e72002-09-23 09:36:25 +0000580 CondP = 10, /* parity even */
581 CondNP = 11, /* not parity even */
582 CondL = 12, /* jump less */
583 CondNL = 13, /* not less */
584 CondLE = 14, /* less or equal */
585 CondNLE = 15, /* not less or equal */
586 CondAlways = 16 /* Jump always */
587 }
588 Condcode;
589
590
591/* Descriptions of additional properties of *unconditional* jumps. */
592typedef
593 enum {
594 JmpBoring=0, /* boring unconditional jump */
595 JmpCall=1, /* jump due to an x86 call insn */
596 JmpRet=2, /* jump due to an x86 ret insn */
597 JmpSyscall=3, /* do a system call, then jump */
598 JmpClientReq=4 /* do a client request, then jump */
599 }
600 JmpKind;
601
602
603/* Flags. User-level code can only read/write O(verflow), S(ign),
604 Z(ero), A(ux-carry), C(arry), P(arity), and may also write
605 D(irection). That's a total of 7 flags. A FlagSet is a bitset,
606 thusly:
607 76543210
608 DOSZACP
609 and bit 7 must always be zero since it is unused.
sewardj2370f3b2002-11-30 15:01:01 +0000610
611 Note: these Flag? values are **not** the positions in the actual
612 %eflags register. */
613
njn25e49d8e72002-09-23 09:36:25 +0000614typedef UChar FlagSet;
615
616#define FlagD (1<<6)
617#define FlagO (1<<5)
618#define FlagS (1<<4)
619#define FlagZ (1<<3)
620#define FlagA (1<<2)
621#define FlagC (1<<1)
622#define FlagP (1<<0)
623
624#define FlagsOSZACP (FlagO | FlagS | FlagZ | FlagA | FlagC | FlagP)
625#define FlagsOSZAP (FlagO | FlagS | FlagZ | FlagA | FlagP)
626#define FlagsOSZCP (FlagO | FlagS | FlagZ | FlagC | FlagP)
627#define FlagsOSACP (FlagO | FlagS | FlagA | FlagC | FlagP)
628#define FlagsSZACP ( FlagS | FlagZ | FlagA | FlagC | FlagP)
629#define FlagsSZAP ( FlagS | FlagZ | FlagA | FlagP)
630#define FlagsZCP ( FlagZ | FlagC | FlagP)
631#define FlagsOC (FlagO | FlagC )
632#define FlagsAC ( FlagA | FlagC )
633
634#define FlagsALL (FlagsOSZACP | FlagD)
635#define FlagsEmpty (FlagSet)0
636
637
sewardj2370f3b2002-11-30 15:01:01 +0000638/* flag positions in eflags */
639#define EFlagC (1 << 0) /* carry */
640#define EFlagP (1 << 2) /* parity */
sewardjfa492d42002-12-08 18:20:01 +0000641#define EFlagA (1 << 4) /* aux carry */
sewardj2370f3b2002-11-30 15:01:01 +0000642#define EFlagZ (1 << 6) /* zero */
643#define EFlagS (1 << 7) /* sign */
sewardjfa492d42002-12-08 18:20:01 +0000644#define EFlagD (1 << 10) /* direction */
sewardj2370f3b2002-11-30 15:01:01 +0000645#define EFlagO (1 << 11) /* overflow */
646
njn25e49d8e72002-09-23 09:36:25 +0000647/* Liveness of general purpose registers, useful for code generation.
648 Reg rank order 0..N-1 corresponds to bits 0..N-1, ie. first
649 reg's liveness in bit 0, last reg's in bit N-1. Note that
650 these rankings don't match the Intel register ordering. */
651typedef UInt RRegSet;
652
653#define ALL_RREGS_DEAD 0 /* 0000...00b */
njne7c258c2002-10-03 08:57:01 +0000654#define ALL_RREGS_LIVE ((1 << VG_MAX_REALREGS)-1) /* 0011...11b */
njn25e49d8e72002-09-23 09:36:25 +0000655#define UNIT_RREGSET(rank) (1 << (rank))
656
657#define IS_RREG_LIVE(rank,rregs_live) (rregs_live & UNIT_RREGSET(rank))
658#define SET_RREG_LIVENESS(rank,rregs_live,b) \
659 do { RRegSet unit = UNIT_RREGSET(rank); \
660 if (b) rregs_live |= unit; \
661 else rregs_live &= ~unit; \
662 } while(0)
663
664
665/* A Micro (u)-instruction. */
666typedef
667 struct {
668 /* word 1 */
669 UInt lit32; /* 32-bit literal */
670
671 /* word 2 */
672 UShort val1; /* first operand */
673 UShort val2; /* second operand */
674
675 /* word 3 */
676 UShort val3; /* third operand */
677 UChar opcode; /* opcode */
678 UChar size; /* data transfer size */
679
680 /* word 4 */
681 FlagSet flags_r; /* :: FlagSet */
682 FlagSet flags_w; /* :: FlagSet */
683 UChar tag1:4; /* first operand tag */
684 UChar tag2:4; /* second operand tag */
685 UChar tag3:4; /* third operand tag */
686 UChar extra4b:4; /* Spare field, used by WIDEN for src
687 -size, and by LEA2 for scale (1,2,4 or 8),
688 and by JMPs for original x86 instr size */
689
690 /* word 5 */
691 UChar cond; /* condition, for jumps */
692 Bool signed_widen:1; /* signed or unsigned WIDEN ? */
693 JmpKind jmpkind:3; /* additional properties of unconditional JMP */
694
695 /* Additional properties for UInstrs that call C functions:
696 - CCALL
697 - PUT (when %ESP is the target)
698 - possibly skin-specific UInstrs
699 */
700 UChar argc:2; /* Number of args, max 3 */
701 UChar regparms_n:2; /* Number of args passed in registers */
702 Bool has_ret_val:1; /* Function has return value? */
703
704 /* RealReg liveness; only sensical after reg alloc and liveness
705 analysis done. This info is a little bit arch-specific --
706 VG_MAX_REALREGS can vary on different architectures. Note that
707 to use this information requires converting between register ranks
njn4ba5a792002-09-30 10:23:54 +0000708 and the Intel register numbers, using VG_(realreg_to_rank)()
709 and/or VG_(rank_to_realreg)() */
njn25e49d8e72002-09-23 09:36:25 +0000710 RRegSet regs_live_after:VG_MAX_REALREGS;
711 }
712 UInstr;
713
714
njn25e49d8e72002-09-23 09:36:25 +0000715typedef
njn810086f2002-11-14 12:42:47 +0000716 struct _UCodeBlock
njn25e49d8e72002-09-23 09:36:25 +0000717 UCodeBlock;
718
njn810086f2002-11-14 12:42:47 +0000719extern Int VG_(get_num_instrs) (UCodeBlock* cb);
720extern Int VG_(get_num_temps) (UCodeBlock* cb);
njn25e49d8e72002-09-23 09:36:25 +0000721
njn810086f2002-11-14 12:42:47 +0000722extern UInstr* VG_(get_instr) (UCodeBlock* cb, Int i);
723extern UInstr* VG_(get_last_instr) (UCodeBlock* cb);
724
njn211b6ad2003-02-03 12:33:31 +0000725
njn25e49d8e72002-09-23 09:36:25 +0000726/*====================================================================*/
727/*=== Instrumenting UCode ===*/
728/*====================================================================*/
729
njnf4ce3d32003-02-10 10:17:26 +0000730/* Maximum number of registers read or written by a single UInstruction. */
731#define VG_MAX_REGS_USED 3
njn25e49d8e72002-09-23 09:36:25 +0000732
njnf4ce3d32003-02-10 10:17:26 +0000733/* Find what this instruction does to its regs, useful for
734 analysis/optimisation passes. `tag' indicates whether we're considering
735 TempRegs (pre-reg-alloc) or RealRegs (post-reg-alloc). `regs' is filled
736 with the affected register numbers, `isWrites' parallels it and indicates
737 if the reg is read or written. If a reg is read and written, it will
738 appear twice in `regs'. `regs' and `isWrites' must be able to fit
739 VG_MAX_REGS_USED elements. */
njn810086f2002-11-14 12:42:47 +0000740extern Int VG_(get_reg_usage) ( UInstr* u, Tag tag, Int* regs, Bool* isWrites );
njn25e49d8e72002-09-23 09:36:25 +0000741
742
njnd5bb0a52002-09-27 10:24:48 +0000743/* Used to register helper functions to be called from generated code. A
744 limited number of compact helpers can be registered; the code generated
745 to call them is slightly shorter -- so register the mostly frequently
746 called helpers as compact. */
njn25e49d8e72002-09-23 09:36:25 +0000747extern void VG_(register_compact_helper) ( Addr a );
748extern void VG_(register_noncompact_helper) ( Addr a );
749
750
751/* ------------------------------------------------------------------ */
752/* Virtual register allocation */
753
754/* Get a new virtual register */
njn4ba5a792002-09-30 10:23:54 +0000755extern Int VG_(get_new_temp) ( UCodeBlock* cb );
njn25e49d8e72002-09-23 09:36:25 +0000756
757/* Get a new virtual shadow register */
njn4ba5a792002-09-30 10:23:54 +0000758extern Int VG_(get_new_shadow) ( UCodeBlock* cb );
njn25e49d8e72002-09-23 09:36:25 +0000759
760/* Get a virtual register's corresponding virtual shadow register */
761#define SHADOW(tempreg) ((tempreg)+1)
762
763
764/* ------------------------------------------------------------------ */
765/* Low-level UInstr builders */
njn4ba5a792002-09-30 10:23:54 +0000766extern void VG_(new_NOP) ( UInstr* u );
767extern void VG_(new_UInstr0) ( UCodeBlock* cb, Opcode opcode, Int sz );
768extern void VG_(new_UInstr1) ( UCodeBlock* cb, Opcode opcode, Int sz,
njn25e49d8e72002-09-23 09:36:25 +0000769 Tag tag1, UInt val1 );
njn4ba5a792002-09-30 10:23:54 +0000770extern void VG_(new_UInstr2) ( UCodeBlock* cb, Opcode opcode, Int sz,
njn25e49d8e72002-09-23 09:36:25 +0000771 Tag tag1, UInt val1,
772 Tag tag2, UInt val2 );
njn4ba5a792002-09-30 10:23:54 +0000773extern void VG_(new_UInstr3) ( UCodeBlock* cb, Opcode opcode, Int sz,
njn25e49d8e72002-09-23 09:36:25 +0000774 Tag tag1, UInt val1,
775 Tag tag2, UInt val2,
776 Tag tag3, UInt val3 );
njn25e49d8e72002-09-23 09:36:25 +0000777
njn810086f2002-11-14 12:42:47 +0000778/* Set read/write/undefined flags. Undefined flags are treaten as written,
779 but it's worth keeping them logically distinct. */
780extern void VG_(set_flag_fields) ( UCodeBlock* cb, FlagSet fr, FlagSet fw,
781 FlagSet fu);
njn4ba5a792002-09-30 10:23:54 +0000782extern void VG_(set_lit_field) ( UCodeBlock* cb, UInt lit32 );
783extern void VG_(set_ccall_fields) ( UCodeBlock* cb, Addr fn, UChar argc,
784 UChar regparms_n, Bool has_ret_val );
njn810086f2002-11-14 12:42:47 +0000785extern void VG_(set_cond_field) ( UCodeBlock* cb, Condcode code );
njn25e49d8e72002-09-23 09:36:25 +0000786
njn4ba5a792002-09-30 10:23:54 +0000787extern void VG_(copy_UInstr) ( UCodeBlock* cb, UInstr* instr );
788
789extern Bool VG_(any_flag_use)( UInstr* u );
njn25e49d8e72002-09-23 09:36:25 +0000790
njnac6c1762002-10-04 14:34:15 +0000791/* Macro versions of the above; just shorter to type. */
792#define uInstr0 VG_(new_UInstr0)
793#define uInstr1 VG_(new_UInstr1)
794#define uInstr2 VG_(new_UInstr2)
795#define uInstr3 VG_(new_UInstr3)
796#define uLiteral VG_(set_lit_field)
797#define uCCall VG_(set_ccall_fields)
njn810086f2002-11-14 12:42:47 +0000798#define uCond VG_(set_cond_field)
799#define uFlagsRWU VG_(set_flag_fields)
njnac6c1762002-10-04 14:34:15 +0000800#define newTemp VG_(get_new_temp)
801#define newShadow VG_(get_new_shadow)
802
njn25e49d8e72002-09-23 09:36:25 +0000803/* Refer to `the last instruction stuffed in' (can be lvalue). */
804#define LAST_UINSTR(cb) (cb)->instrs[(cb)->used-1]
805
806
807/* ------------------------------------------------------------------ */
808/* Higher-level UInstr sequence builders */
njn4ba5a792002-09-30 10:23:54 +0000809extern void VG_(call_helper_0_0) ( UCodeBlock* cb, Addr f);
810extern void VG_(call_helper_1_0) ( UCodeBlock* cb, Addr f, UInt arg1,
811 UInt regparms_n);
812extern void VG_(call_helper_2_0) ( UCodeBlock* cb, Addr f, UInt arg1, UInt arg2,
813 UInt regparms_n);
njn25e49d8e72002-09-23 09:36:25 +0000814
815/* One way around the 3-arg C function limit is to pass args via global
njn6fefa1b2003-02-24 10:32:51 +0000816 * variables... ugly, but it works. This one puts a literal in there. */
njn4ba5a792002-09-30 10:23:54 +0000817extern void VG_(set_global_var) ( UCodeBlock* cb, Addr globvar_ptr, UInt val);
njn25e49d8e72002-09-23 09:36:25 +0000818
njn6fefa1b2003-02-24 10:32:51 +0000819/* This one puts the contents of a TempReg in the global variable. */
820extern void VG_(set_global_var_tempreg) ( UCodeBlock* cb, Addr globvar_ptr,
821 UInt t_val);
822
njn25e49d8e72002-09-23 09:36:25 +0000823/* ------------------------------------------------------------------ */
njnd5bb0a52002-09-27 10:24:48 +0000824/* Allocating/freeing basic blocks of UCode */
njn810086f2002-11-14 12:42:47 +0000825extern UCodeBlock* VG_(setup_UCodeBlock) ( UCodeBlock* cb );
njn4ba5a792002-09-30 10:23:54 +0000826extern void VG_(free_UCodeBlock) ( UCodeBlock* cb );
njnd5bb0a52002-09-27 10:24:48 +0000827
828/* ------------------------------------------------------------------ */
829/* UCode pretty/ugly printing. Probably only useful to call from a skin
njn25e49d8e72002-09-23 09:36:25 +0000830 if VG_(needs).extended_UCode == True. */
831
832/* When True, all generated code is/should be printed. */
833extern Bool VG_(print_codegen);
834
njn4ba5a792002-09-30 10:23:54 +0000835/* Pretty/ugly printing functions */
836extern void VG_(pp_UCodeBlock) ( UCodeBlock* cb, Char* title );
837extern void VG_(pp_UInstr) ( Int instrNo, UInstr* u );
838extern void VG_(pp_UInstr_regs) ( Int instrNo, UInstr* u );
839extern void VG_(up_UInstr) ( Int instrNo, UInstr* u );
840extern Char* VG_(name_UOpcode) ( Bool upper, Opcode opc );
njn563f96f2003-02-03 11:17:46 +0000841extern Char* VG_(name_UCondcode) ( Condcode cond );
njn4ba5a792002-09-30 10:23:54 +0000842extern void VG_(pp_UOperand) ( UInstr* u, Int operandNo,
843 Int sz, Bool parens );
njn25e49d8e72002-09-23 09:36:25 +0000844
njnb93d1782003-02-03 12:03:22 +0000845/* ------------------------------------------------------------------ */
njnf4ce3d32003-02-10 10:17:26 +0000846/* Accessing archregs and their shadows */
847extern UInt VG_(get_archreg) ( UInt archreg );
848extern UInt VG_(get_thread_archreg) ( ThreadId tid, UInt archreg );
849
njnb93d1782003-02-03 12:03:22 +0000850extern UInt VG_(get_shadow_archreg) ( UInt archreg );
851extern void VG_(set_shadow_archreg) ( UInt archreg, UInt val );
852extern Addr VG_(shadow_archreg_address) ( UInt archreg );
njn25e49d8e72002-09-23 09:36:25 +0000853
njnf4ce3d32003-02-10 10:17:26 +0000854extern UInt VG_(get_thread_shadow_archreg) ( ThreadId tid, UInt archreg );
855extern void VG_(set_thread_shadow_archreg) ( ThreadId tid, UInt archreg,
856 UInt val );
njn211b6ad2003-02-03 12:33:31 +0000857
858/* ------------------------------------------------------------------ */
859/* Offsets of addresses of helper functions. A "helper" function is one
860 which is called from generated code via CALLM. */
861
862extern Int VGOFF_(helper_idiv_64_32);
863extern Int VGOFF_(helper_div_64_32);
864extern Int VGOFF_(helper_idiv_32_16);
865extern Int VGOFF_(helper_div_32_16);
866extern Int VGOFF_(helper_idiv_16_8);
867extern Int VGOFF_(helper_div_16_8);
868
869extern Int VGOFF_(helper_imul_32_64);
870extern Int VGOFF_(helper_mul_32_64);
871extern Int VGOFF_(helper_imul_16_32);
872extern Int VGOFF_(helper_mul_16_32);
873extern Int VGOFF_(helper_imul_8_16);
874extern Int VGOFF_(helper_mul_8_16);
875
876extern Int VGOFF_(helper_CLD);
877extern Int VGOFF_(helper_STD);
878extern Int VGOFF_(helper_get_dirflag);
879
880extern Int VGOFF_(helper_CLC);
881extern Int VGOFF_(helper_STC);
882
883extern Int VGOFF_(helper_shldl);
884extern Int VGOFF_(helper_shldw);
885extern Int VGOFF_(helper_shrdl);
886extern Int VGOFF_(helper_shrdw);
887
888extern Int VGOFF_(helper_RDTSC);
889extern Int VGOFF_(helper_CPUID);
890
891extern Int VGOFF_(helper_bsf);
892extern Int VGOFF_(helper_bsr);
893
894extern Int VGOFF_(helper_fstsw_AX);
895extern Int VGOFF_(helper_SAHF);
896extern Int VGOFF_(helper_DAS);
897extern Int VGOFF_(helper_DAA);
898
899
njn25e49d8e72002-09-23 09:36:25 +0000900/*====================================================================*/
njnd5bb0a52002-09-27 10:24:48 +0000901/*=== Generating x86 code from UCode ===*/
njn25e49d8e72002-09-23 09:36:25 +0000902/*====================================================================*/
903
njnd5bb0a52002-09-27 10:24:48 +0000904/* All this only necessary for skins with VG_(needs).extends_UCode == True. */
njn25e49d8e72002-09-23 09:36:25 +0000905
sewardje1042472002-09-30 12:33:11 +0000906/* This is the Intel register encoding -- integer regs. */
njn25e49d8e72002-09-23 09:36:25 +0000907#define R_EAX 0
908#define R_ECX 1
909#define R_EDX 2
910#define R_EBX 3
911#define R_ESP 4
912#define R_EBP 5
913#define R_ESI 6
914#define R_EDI 7
915
916#define R_AL (0+R_EAX)
917#define R_CL (0+R_ECX)
918#define R_DL (0+R_EDX)
919#define R_BL (0+R_EBX)
920#define R_AH (4+R_EAX)
921#define R_CH (4+R_ECX)
922#define R_DH (4+R_EDX)
923#define R_BH (4+R_EBX)
924
sewardje1042472002-09-30 12:33:11 +0000925/* This is the Intel register encoding -- segment regs. */
926#define R_ES 0
927#define R_CS 1
928#define R_SS 2
929#define R_DS 3
930#define R_FS 4
931#define R_GS 5
932
njn25e49d8e72002-09-23 09:36:25 +0000933/* For pretty printing x86 code */
sewardje1042472002-09-30 12:33:11 +0000934extern Char* VG_(name_of_seg_reg) ( Int sreg );
njn4ba5a792002-09-30 10:23:54 +0000935extern Char* VG_(name_of_int_reg) ( Int size, Int reg );
936extern Char VG_(name_of_int_size) ( Int size );
njn25e49d8e72002-09-23 09:36:25 +0000937
njnac6c1762002-10-04 14:34:15 +0000938/* Shorter macros for convenience */
939#define nameIReg VG_(name_of_int_reg)
940#define nameISize VG_(name_of_int_size)
941#define nameSReg VG_(name_of_seg_reg)
942
njn25e49d8e72002-09-23 09:36:25 +0000943/* Randomly useful things */
944extern UInt VG_(extend_s_8to32) ( UInt x );
945
946/* Code emitters */
njn4ba5a792002-09-30 10:23:54 +0000947extern void VG_(emitB) ( UInt b );
948extern void VG_(emitW) ( UInt w );
949extern void VG_(emitL) ( UInt l );
sewardjfa492d42002-12-08 18:20:01 +0000950extern void VG_(new_emit) ( Bool upd_cc, FlagSet uses_flags, FlagSet sets_flags );
njn25e49d8e72002-09-23 09:36:25 +0000951
952/* Finding offsets */
njn4ba5a792002-09-30 10:23:54 +0000953extern Int VG_(helper_offset) ( Addr a );
954extern Int VG_(shadow_reg_offset) ( Int arch );
955extern Int VG_(shadow_flags_offset) ( void );
njn25e49d8e72002-09-23 09:36:25 +0000956
njnd5bb0a52002-09-27 10:24:48 +0000957/* Convert reg ranks <-> Intel register ordering, for using register
958 liveness information. */
njn4ba5a792002-09-30 10:23:54 +0000959extern Int VG_(realreg_to_rank) ( Int realreg );
960extern Int VG_(rank_to_realreg) ( Int rank );
njn25e49d8e72002-09-23 09:36:25 +0000961
njnd5bb0a52002-09-27 10:24:48 +0000962/* Call a subroutine. Does no argument passing, stack manipulations, etc. */
sewardjfa492d42002-12-08 18:20:01 +0000963extern void VG_(synth_call) ( Bool ensure_shortform, Int word_offset,
964 Bool upd_cc, FlagSet use_flags, FlagSet set_flags );
njn25e49d8e72002-09-23 09:36:25 +0000965
njnd5bb0a52002-09-27 10:24:48 +0000966/* For calling C functions -- saves caller save regs, pushes args, calls,
967 clears the stack, restores caller save regs. `fn' must be registered in
968 the baseBlock first. Acceptable tags are RealReg and Literal. Optimises
969 things, eg. by not preserving non-live caller-save registers.
njn25e49d8e72002-09-23 09:36:25 +0000970
njnd5bb0a52002-09-27 10:24:48 +0000971 WARNING: a UInstr should *not* be translated with synth_ccall() followed
972 by some other x86 assembly code; this will invalidate the results of
973 vg_realreg_liveness_analysis() and everything will fall over. */
njn4ba5a792002-09-30 10:23:54 +0000974extern void VG_(synth_ccall) ( Addr fn, Int argc, Int regparms_n, UInt argv[],
975 Tag tagv[], Int ret_reg,
976 RRegSet regs_live_before,
977 RRegSet regs_live_after );
njn25e49d8e72002-09-23 09:36:25 +0000978
979/* Addressing modes */
njn4ba5a792002-09-30 10:23:54 +0000980extern void VG_(emit_amode_offregmem_reg)( Int off, Int regmem, Int reg );
981extern void VG_(emit_amode_ereg_greg) ( Int e_reg, Int g_reg );
njn25e49d8e72002-09-23 09:36:25 +0000982
983/* v-size (4, or 2 with OSO) insn emitters */
njn4ba5a792002-09-30 10:23:54 +0000984extern void VG_(emit_movv_offregmem_reg) ( Int sz, Int off, Int areg, Int reg );
985extern void VG_(emit_movv_reg_offregmem) ( Int sz, Int reg, Int off, Int areg );
986extern void VG_(emit_movv_reg_reg) ( Int sz, Int reg1, Int reg2 );
sewardjfa492d42002-12-08 18:20:01 +0000987extern void VG_(emit_nonshiftopv_lit_reg)( Bool upd_cc, Int sz, Opcode opc, UInt lit,
njn4ba5a792002-09-30 10:23:54 +0000988 Int reg );
sewardjfa492d42002-12-08 18:20:01 +0000989extern void VG_(emit_shiftopv_lit_reg) ( Bool upd_cc, Int sz, Opcode opc, UInt lit,
njn4ba5a792002-09-30 10:23:54 +0000990 Int reg );
sewardjfa492d42002-12-08 18:20:01 +0000991extern void VG_(emit_nonshiftopv_reg_reg)( Bool upd_cc, Int sz, Opcode opc,
njn4ba5a792002-09-30 10:23:54 +0000992 Int reg1, Int reg2 );
993extern void VG_(emit_movv_lit_reg) ( Int sz, UInt lit, Int reg );
sewardjfa492d42002-12-08 18:20:01 +0000994extern void VG_(emit_unaryopv_reg) ( Bool upd_cc, Int sz, Opcode opc, Int reg );
njn4ba5a792002-09-30 10:23:54 +0000995extern void VG_(emit_pushv_reg) ( Int sz, Int reg );
996extern void VG_(emit_popv_reg) ( Int sz, Int reg );
njn25e49d8e72002-09-23 09:36:25 +0000997
njn4ba5a792002-09-30 10:23:54 +0000998extern void VG_(emit_pushl_lit32) ( UInt int32 );
999extern void VG_(emit_pushl_lit8) ( Int lit8 );
sewardjfa492d42002-12-08 18:20:01 +00001000extern void VG_(emit_cmpl_zero_reg) ( Bool upd_cc, Int reg );
njn4ba5a792002-09-30 10:23:54 +00001001extern void VG_(emit_swapl_reg_EAX) ( Int reg );
1002extern void VG_(emit_movv_lit_offregmem) ( Int sz, UInt lit, Int off,
1003 Int memreg );
njn25e49d8e72002-09-23 09:36:25 +00001004
1005/* b-size (1 byte) instruction emitters */
njn4ba5a792002-09-30 10:23:54 +00001006extern void VG_(emit_movb_lit_offregmem) ( UInt lit, Int off, Int memreg );
1007extern void VG_(emit_movb_reg_offregmem) ( Int reg, Int off, Int areg );
sewardjfa492d42002-12-08 18:20:01 +00001008extern void VG_(emit_unaryopb_reg) ( Bool upd_cc, Opcode opc, Int reg );
1009extern void VG_(emit_testb_lit_reg) ( Bool upd_cc, UInt lit, Int reg );
njn25e49d8e72002-09-23 09:36:25 +00001010
1011/* zero-extended load emitters */
njn4ba5a792002-09-30 10:23:54 +00001012extern void VG_(emit_movzbl_offregmem_reg) ( Int off, Int regmem, Int reg );
1013extern void VG_(emit_movzwl_offregmem_reg) ( Int off, Int areg, Int reg );
njn25e49d8e72002-09-23 09:36:25 +00001014
1015/* misc instruction emitters */
njn4ba5a792002-09-30 10:23:54 +00001016extern void VG_(emit_call_reg) ( Int reg );
1017extern void VG_(emit_add_lit_to_esp) ( Int lit );
njn4ba5a792002-09-30 10:23:54 +00001018extern void VG_(emit_pushal) ( void );
1019extern void VG_(emit_popal) ( void );
1020extern void VG_(emit_AMD_prefetch_reg) ( Int reg );
njn25e49d8e72002-09-23 09:36:25 +00001021
sewardja2113f92002-12-12 23:42:48 +00001022/* jump emitters */
1023extern void VG_(init_target) ( Int *tgt );
1024
1025extern void VG_(target_back) ( Int *tgt );
1026extern void VG_(target_forward) ( Int *tgt );
1027extern void VG_(emit_target_delta) ( Int *tgt );
1028
1029extern void VG_(emit_jcondshort_delta) ( Bool simd_cc, Condcode cond, Int delta );
1030extern void VG_(emit_jcondshort_target)( Bool simd_cc, Condcode cond, Int *tgt );
1031
njn25e49d8e72002-09-23 09:36:25 +00001032
1033/*====================================================================*/
1034/*=== Execution contexts ===*/
1035/*====================================================================*/
1036
1037/* Generic resolution type used in a few different ways, such as deciding
1038 how closely to compare two errors for equality. */
1039typedef
1040 enum { Vg_LowRes, Vg_MedRes, Vg_HighRes }
1041 VgRes;
1042
1043typedef
1044 struct _ExeContext
1045 ExeContext;
1046
njnd5bb0a52002-09-27 10:24:48 +00001047/* Compare two ExeContexts. Number of callers considered depends on `res':
1048 Vg_LowRes: 2
1049 Vg_MedRes: 4
1050 Vg_HighRes: all */
njn25e49d8e72002-09-23 09:36:25 +00001051extern Bool VG_(eq_ExeContext) ( VgRes res,
1052 ExeContext* e1, ExeContext* e2 );
1053
1054/* Print an ExeContext. */
1055extern void VG_(pp_ExeContext) ( ExeContext* );
1056
1057/* Take a snapshot of the client's stack. Search our collection of
1058 ExeContexts to see if we already have it, and if not, allocate a
1059 new one. Either way, return a pointer to the context. */
1060extern ExeContext* VG_(get_ExeContext) ( ThreadState *tst );
1061
sewardj499e3de2002-11-13 22:22:25 +00001062/* Just grab the client's EIP, as a much smaller and cheaper
1063 indication of where they are. */
1064extern Addr VG_(get_EIP)( ThreadState *tst );
njn25e49d8e72002-09-23 09:36:25 +00001065
njn211b6ad2003-02-03 12:33:31 +00001066
njn25e49d8e72002-09-23 09:36:25 +00001067/*====================================================================*/
1068/*=== Error reporting ===*/
1069/*====================================================================*/
1070
1071/* ------------------------------------------------------------------ */
1072/* Suppressions describe errors which we want to suppress, ie, not
1073 show the user, usually because it is caused by a problem in a library
1074 which we can't fix, replace or work around. Suppressions are read from
njnd5bb0a52002-09-27 10:24:48 +00001075 a file at startup time. This gives flexibility so that new
njn25e49d8e72002-09-23 09:36:25 +00001076 suppressions can be added to the file as and when needed.
1077*/
1078
1079typedef
1080 Int /* Do not make this unsigned! */
1081 SuppKind;
1082
njn810086f2002-11-14 12:42:47 +00001083/* The skin-relevant parts of a suppression are:
1084 kind: what kind of suppression; must be in the range (0..)
1085 string: use is optional. NULL by default.
1086 extra: use is optional. NULL by default. void* so it's extensible.
njn25e49d8e72002-09-23 09:36:25 +00001087*/
1088typedef
njn810086f2002-11-14 12:42:47 +00001089 struct _Supp
1090 Supp;
1091
1092/* Useful in SK_(error_matches_suppression)() */
1093SuppKind VG_(get_supp_kind) ( Supp* su );
1094Char* VG_(get_supp_string) ( Supp* su );
1095void* VG_(get_supp_extra) ( Supp* su );
1096
1097/* Must be used in VG_(recognised_suppression)() */
1098void VG_(set_supp_kind) ( Supp* su, SuppKind suppkind );
1099/* May be used in VG_(read_extra_suppression_info)() */
1100void VG_(set_supp_string) ( Supp* su, Char* string );
1101void VG_(set_supp_extra) ( Supp* su, void* extra );
njn25e49d8e72002-09-23 09:36:25 +00001102
1103
1104/* ------------------------------------------------------------------ */
1105/* Error records contain enough info to generate an error report. The idea
1106 is that (typically) the same few points in the program generate thousands
njnd5bb0a52002-09-27 10:24:48 +00001107 of errors, and we don't want to spew out a fresh error message for each
1108 one. Instead, we use these structures to common up duplicates.
njn25e49d8e72002-09-23 09:36:25 +00001109*/
1110
1111typedef
1112 Int /* Do not make this unsigned! */
1113 ErrorKind;
1114
njn810086f2002-11-14 12:42:47 +00001115/* The skin-relevant parts of an Error are:
1116 kind: what kind of error; must be in the range (0..)
1117 addr: use is optional. 0 by default.
1118 string: use is optional. NULL by default.
1119 extra: use is optional. NULL by default. void* so it's extensible.
njn25e49d8e72002-09-23 09:36:25 +00001120*/
1121typedef
njn810086f2002-11-14 12:42:47 +00001122 struct _Error
1123 Error;
njn25e49d8e72002-09-23 09:36:25 +00001124
njn810086f2002-11-14 12:42:47 +00001125/* Useful in SK_(error_matches_suppression)(), SK_(pp_SkinError)(), etc */
njnb877d492003-01-28 20:40:57 +00001126ExeContext* VG_(get_error_where) ( Error* err );
1127SuppKind VG_(get_error_kind) ( Error* err );
1128Addr VG_(get_error_address) ( Error* err );
1129Char* VG_(get_error_string) ( Error* err );
1130void* VG_(get_error_extra) ( Error* err );
njn25e49d8e72002-09-23 09:36:25 +00001131
njnd5bb0a52002-09-27 10:24:48 +00001132/* Call this when an error occurs. It will be recorded if it hasn't been
njn25e49d8e72002-09-23 09:36:25 +00001133 seen before. If it has, the existing error record will have its count
1134 incremented.
1135
1136 If the error occurs in generated code, 'tst' should be NULL. If the
1137 error occurs in non-generated code, 'tst' should be non-NULL. The
1138 `extra' field can be stack-allocated; it will be copied (using
1139 SKN_(dup_extra_and_update)()) if needed. But it won't be copied
1140 if it's NULL.
njnd5bb0a52002-09-27 10:24:48 +00001141
1142 If no 'a', 's' or 'extra' of interest needs to be recorded, just use
1143 NULL for them.
njn25e49d8e72002-09-23 09:36:25 +00001144*/
1145extern void VG_(maybe_record_error) ( ThreadState* tst, ErrorKind ekind,
1146 Addr a, Char* s, void* extra );
1147
1148/* Gets a non-blank, non-comment line of at most nBuf chars from fd.
1149 Skips leading spaces on the line. Returns True if EOF was hit instead.
1150 Useful for reading in extra skin-specific suppression lines.
1151*/
njn4ba5a792002-09-30 10:23:54 +00001152extern Bool VG_(get_line) ( Int fd, Char* buf, Int nBuf );
njn25e49d8e72002-09-23 09:36:25 +00001153
1154
1155/*====================================================================*/
1156/*=== Obtaining debug information ===*/
1157/*====================================================================*/
1158
sewardj6e008cb2002-12-15 13:11:39 +00001159/* Get the file/function/line number of the instruction at address
1160 'a'. For these four, if debug info for the address is found, it
1161 copies the info into the buffer/UInt and returns True. If not, it
1162 returns False and nothing is copied. VG_(get_fnname) always
1163 demangles C++ function names. VG_(get_fnname_w_offset) is the
1164 same, except it appends "+N" to symbol names to indicate offsets.
njn25e49d8e72002-09-23 09:36:25 +00001165*/
1166extern Bool VG_(get_filename) ( Addr a, Char* filename, Int n_filename );
1167extern Bool VG_(get_fnname) ( Addr a, Char* fnname, Int n_fnname );
1168extern Bool VG_(get_linenum) ( Addr a, UInt* linenum );
sewardj6e008cb2002-12-15 13:11:39 +00001169extern Bool VG_(get_fnname_w_offset)
1170 ( Addr a, Char* fnname, Int n_fnname );
njn25e49d8e72002-09-23 09:36:25 +00001171
1172/* This one is more efficient if getting both filename and line number,
1173 because the two lookups are done together. */
1174extern Bool VG_(get_filename_linenum)
1175 ( Addr a, Char* filename, Int n_filename,
1176 UInt* linenum );
1177
1178/* Succeeds only if we find from debug info that 'a' is the address of the
1179 first instruction in a function -- as opposed to VG_(get_fnname) which
1180 succeeds if we find from debug info that 'a' is the address of any
1181 instruction in a function. Use this to instrument the start of
njnd5bb0a52002-09-27 10:24:48 +00001182 a particular function. Nb: if an executable/shared object is stripped
njn25e49d8e72002-09-23 09:36:25 +00001183 of its symbols, this function will not be able to recognise function
1184 entry points within it. */
1185extern Bool VG_(get_fnname_if_entry) ( Addr a, Char* filename, Int n_filename );
1186
1187/* Succeeds if the address is within a shared object or the main executable.
1188 It doesn't matter if debug info is present or not. */
1189extern Bool VG_(get_objname) ( Addr a, Char* objname, Int n_objname );
1190
sewardj47104382002-10-20 18:35:48 +00001191/* A way to get information about what segments are mapped */
1192typedef struct _SegInfo SegInfo;
1193
njnb877d492003-01-28 20:40:57 +00001194/* Returns NULL if the SegInfo isn't found. It doesn't matter if debug info
1195 is present or not. */
1196extern SegInfo* VG_(get_obj) ( Addr a );
1197
1198extern const SegInfo* VG_(next_seginfo) ( const SegInfo *seg );
1199extern Addr VG_(seg_start) ( const SegInfo *seg );
1200extern UInt VG_(seg_size) ( const SegInfo *seg );
1201extern const UChar* VG_(seg_filename) ( const SegInfo *seg );
1202extern UInt VG_(seg_sym_offset)( const SegInfo *seg );
sewardj47104382002-10-20 18:35:48 +00001203
1204typedef
1205 enum {
1206 Vg_SectUnknown,
1207 Vg_SectText,
sewardj8fe15a32002-10-20 19:29:21 +00001208 Vg_SectData,
1209 Vg_SectBSS,
sewardj47104382002-10-20 18:35:48 +00001210 Vg_SectGOT,
sewardj8fe15a32002-10-20 19:29:21 +00001211 Vg_SectPLT,
sewardj47104382002-10-20 18:35:48 +00001212 }
1213 VgSectKind;
1214
1215extern VgSectKind VG_(seg_sect_kind)(Addr);
1216
njn25e49d8e72002-09-23 09:36:25 +00001217
1218/*====================================================================*/
1219/*=== Shadow chunks and block-finding ===*/
1220/*====================================================================*/
1221
njn810086f2002-11-14 12:42:47 +00001222/* The skin-relevant parts of a ShadowChunk are:
1223 size: size of the block in bytes
1224 addr: addr of the block
1225 extra: anything extra kept by the skin; size is determined by
1226 VG_(needs).sizeof_shadow_chunk
1227*/
njn25e49d8e72002-09-23 09:36:25 +00001228typedef
njn810086f2002-11-14 12:42:47 +00001229 struct _ShadowChunk
njn25e49d8e72002-09-23 09:36:25 +00001230 ShadowChunk;
1231
njn810086f2002-11-14 12:42:47 +00001232extern UInt VG_(get_sc_size) ( ShadowChunk* sc );
1233extern Addr VG_(get_sc_data) ( ShadowChunk* sc );
1234/* Gets the ith word of the `extra' field. */
1235extern UInt VG_(get_sc_extra) ( ShadowChunk* sc, UInt i );
1236/* Sets the ith word of the `extra' field to `word'. */
1237extern void VG_(set_sc_extra) ( ShadowChunk* sc, UInt i, UInt word );
1238
1239/* These two should only be used if the `alternative_free' need is set, once
1240 we reach the point where the block would have been free'd. */
1241extern ShadowChunk* VG_(get_sc_next) ( ShadowChunk* sc );
1242extern void VG_(set_sc_next) ( ShadowChunk* sc, ShadowChunk* next );
1243
1244
njn25e49d8e72002-09-23 09:36:25 +00001245/* Use this to free blocks if VG_(needs).alternative_free == True.
1246 It frees the ShadowChunk and the malloc'd block it points to. */
njn4ba5a792002-09-30 10:23:54 +00001247extern void VG_(free_ShadowChunk) ( ShadowChunk* sc );
njn25e49d8e72002-09-23 09:36:25 +00001248
1249/* Makes an array of pointers to all the shadow chunks of malloc'd blocks */
1250extern ShadowChunk** VG_(get_malloc_shadows) ( /*OUT*/ UInt* n_shadows );
1251
1252/* Determines if address 'a' is within the bounds of the block at start.
1253 Allows a little 'slop' round the edges. */
1254extern Bool VG_(addr_is_in_block) ( Addr a, Addr start, UInt size );
1255
1256/* Searches through currently malloc'd blocks until a matching one is found.
1257 Returns NULL if none match. Extra arguments can be implicitly passed to
njnd5bb0a52002-09-27 10:24:48 +00001258 p using nested functions; see memcheck/mc_errcontext.c for an example. */
njn25e49d8e72002-09-23 09:36:25 +00001259extern ShadowChunk* VG_(any_matching_mallocd_ShadowChunks)
1260 ( Bool (*p) ( ShadowChunk* ));
1261
1262/* Searches through all thread's stacks to see if any match. Returns
1263 * VG_INVALID_THREADID if none match. */
1264extern ThreadId VG_(any_matching_thread_stack)
1265 ( Bool (*p) ( Addr stack_min, Addr stack_max ));
1266
sewardja4495682002-10-21 07:29:59 +00001267/* Do memory leak detection. */
1268extern void VG_(generic_detect_memory_leaks) (
1269 Bool is_valid_64k_chunk ( UInt ),
1270 Bool is_valid_address ( Addr ),
1271 ExeContext* get_where ( ShadowChunk* ),
1272 VgRes leak_resolution,
sewardj99aac972002-12-26 01:53:45 +00001273 Bool show_reachable,
1274 UInt /*CoreErrorKind*/ leakSupp
sewardja4495682002-10-21 07:29:59 +00001275 );
1276
1277
njn25e49d8e72002-09-23 09:36:25 +00001278/*====================================================================*/
1279/*=== Skin-specific stuff ===*/
1280/*====================================================================*/
1281
njnd04b7c62002-10-03 14:05:52 +00001282/* ------------------------------------------------------------------ */
1283/* Details */
njnd04b7c62002-10-03 14:05:52 +00001284
njn120281f2003-02-03 12:20:07 +00001285/* Default value for avg_translations_sizeB (in bytes), indicating typical
1286 code expansion of about 6:1. */
1287#define VG_DEFAULT_TRANS_SIZEB 100
1288
njn810086f2002-11-14 12:42:47 +00001289/* Information used in the startup message. `name' also determines the
1290 string used for identifying suppressions in a suppression file as
1291 belonging to this skin. `version' can be NULL, in which case (not
1292 surprisingly) no version info is printed; this mechanism is designed for
1293 skins distributed with Valgrind that share a version number with
1294 Valgrind. Other skins not distributed as part of Valgrind should
sewardjc0d8f682002-11-30 00:49:43 +00001295 probably have their own version number. */
1296extern void VG_(details_name) ( Char* name );
1297extern void VG_(details_version) ( Char* version );
1298extern void VG_(details_description) ( Char* description );
1299extern void VG_(details_copyright_author) ( Char* copyright_author );
1300
1301/* Average size of a translation, in bytes, so that the translation
njn120281f2003-02-03 12:20:07 +00001302 storage machinery can allocate memory appropriately. Not critical,
1303 setting is optional. */
1304extern void VG_(details_avg_translation_sizeB) ( UInt size );
njnd04b7c62002-10-03 14:05:52 +00001305
njn810086f2002-11-14 12:42:47 +00001306/* String printed if an `sk_assert' assertion fails or VG_(skin_panic)
1307 is called. Should probably be an email address. */
1308extern void VG_(details_bug_reports_to) ( Char* bug_reports_to );
njnd04b7c62002-10-03 14:05:52 +00001309
1310/* ------------------------------------------------------------------ */
1311/* Needs */
1312
njn810086f2002-11-14 12:42:47 +00001313/* Booleans that decide core behaviour, but don't require extra
1314 operations to be defined if `True' */
njnd5bb0a52002-09-27 10:24:48 +00001315
njn810086f2002-11-14 12:42:47 +00001316/* Should __libc_freeres() be run? Bugs in it can crash the skin. */
1317extern void VG_(needs_libc_freeres) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001318
njn810086f2002-11-14 12:42:47 +00001319/* Want to have errors detected by Valgrind's core reported? Includes:
1320 - pthread API errors (many; eg. unlocking a non-locked mutex)
1321 - silly arguments to malloc() et al (eg. negative size)
1322 - invalid file descriptors to blocking syscalls read() and write()
1323 - bad signal numbers passed to sigaction()
1324 - attempt to install signal handler for SIGKILL or SIGSTOP */
1325extern void VG_(needs_core_errors) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001326
njn810086f2002-11-14 12:42:47 +00001327/* Booleans that indicate extra operations are defined; if these are True,
1328 the corresponding template functions (given below) must be defined. A
1329 lot like being a member of a type class. */
njn25e49d8e72002-09-23 09:36:25 +00001330
njn810086f2002-11-14 12:42:47 +00001331/* Want to report errors from skin? This implies use of suppressions, too. */
1332extern void VG_(needs_skin_errors) ( void );
njnd5bb0a52002-09-27 10:24:48 +00001333
njn810086f2002-11-14 12:42:47 +00001334/* Is information kept about specific individual basic blocks? (Eg. for
1335 cachegrind there are cost-centres for every instruction, stored at a
1336 basic block level.) If so, it sometimes has to be discarded, because
1337 .so mmap/munmap-ping or self-modifying code (informed by the
1338 DISCARD_TRANSLATIONS user request) can cause one instruction address
1339 to be used for more than one instruction in one program run... */
1340extern void VG_(needs_basic_block_discards) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001341
njn810086f2002-11-14 12:42:47 +00001342/* Skin maintains information about each register? */
1343extern void VG_(needs_shadow_regs) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001344
njn810086f2002-11-14 12:42:47 +00001345/* Skin defines its own command line options? */
1346extern void VG_(needs_command_line_options) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001347
njn810086f2002-11-14 12:42:47 +00001348/* Skin defines its own client requests? */
1349extern void VG_(needs_client_requests) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001350
njn810086f2002-11-14 12:42:47 +00001351/* Skin defines its own UInstrs? */
1352extern void VG_(needs_extended_UCode) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001353
njn810086f2002-11-14 12:42:47 +00001354/* Skin does stuff before and/or after system calls? */
1355extern void VG_(needs_syscall_wrapper) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001356
njn810086f2002-11-14 12:42:47 +00001357/* Size, in words, of extra info about malloc'd blocks recorded by
1358 skin. Be careful to get this right or you'll get seg faults! */
1359extern void VG_(needs_sizeof_shadow_block) ( Int size );
njn25e49d8e72002-09-23 09:36:25 +00001360
njn810086f2002-11-14 12:42:47 +00001361/* Skin does free()s itself? Useful if a skin needs to keep track of
1362 blocks in some way after they're free'd.
1363 WARNING: don't forget to call VG_(free_ShadowChunk)() for each block
1364 eventually! */
1365extern void VG_(needs_alternative_free) ( void );
sewardj8fe15a32002-10-20 19:29:21 +00001366
njn810086f2002-11-14 12:42:47 +00001367/* Are skin-state sanity checks performed? */
1368extern void VG_(needs_sanity_checks) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001369
njn810086f2002-11-14 12:42:47 +00001370/* Do we need to see data symbols? */
1371extern void VG_(needs_data_syms) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001372
1373/* ------------------------------------------------------------------ */
1374/* Core events to track */
1375
1376/* Part of the core from which this call was made. Useful for determining
njnd5bb0a52002-09-27 10:24:48 +00001377 what kind of error message should be emitted. */
njn25e49d8e72002-09-23 09:36:25 +00001378typedef
1379 enum { Vg_CorePThread, Vg_CoreSignal, Vg_CoreSysCall, Vg_CoreTranslate }
1380 CorePart;
1381
njn810086f2002-11-14 12:42:47 +00001382#define EV extern void
njn25e49d8e72002-09-23 09:36:25 +00001383
njn810086f2002-11-14 12:42:47 +00001384/* Events happening in core to track. To be notified, pass a callback
1385 function to the appropriate function. To ignore an event, don't do
1386 anything (default is for events to be ignored). */
1387
1388/* Memory events */
1389
1390EV VG_(track_new_mem_startup) ( void (*f)(Addr a, UInt len,
1391 Bool rr, Bool ww, Bool xx) );
1392EV VG_(track_new_mem_heap) ( void (*f)(Addr a, UInt len, Bool is_inited) );
1393EV VG_(track_new_mem_stack) ( void (*f)(Addr a, UInt len) );
1394EV VG_(track_new_mem_stack_aligned) ( void (*f)(Addr a, UInt len) );
1395EV VG_(track_new_mem_stack_signal) ( void (*f)(Addr a, UInt len) );
1396EV VG_(track_new_mem_brk) ( void (*f)(Addr a, UInt len) );
1397EV VG_(track_new_mem_mmap) ( void (*f)(Addr a, UInt len,
1398 Bool rr, Bool ww, Bool xx) );
1399
1400EV VG_(track_copy_mem_heap) ( void (*f)(Addr from, Addr to, UInt len) );
1401EV VG_(track_copy_mem_remap) ( void (*f)(Addr from, Addr to, UInt len) );
1402EV VG_(track_change_mem_mprotect) ( void (*f)(Addr a, UInt len,
1403 Bool rr, Bool ww, Bool xx) );
njn25e49d8e72002-09-23 09:36:25 +00001404
njn810086f2002-11-14 12:42:47 +00001405/* Used on redzones around malloc'd blocks and at end of stack */
1406EV VG_(track_ban_mem_heap) ( void (*f)(Addr a, UInt len) );
1407EV VG_(track_ban_mem_stack) ( void (*f)(Addr a, UInt len) );
njn25e49d8e72002-09-23 09:36:25 +00001408
njn810086f2002-11-14 12:42:47 +00001409EV VG_(track_die_mem_heap) ( void (*f)(Addr a, UInt len) );
1410EV VG_(track_die_mem_stack) ( void (*f)(Addr a, UInt len) );
1411EV VG_(track_die_mem_stack_aligned) ( void (*f)(Addr a, UInt len) );
1412EV VG_(track_die_mem_stack_signal) ( void (*f)(Addr a, UInt len) );
1413EV VG_(track_die_mem_brk) ( void (*f)(Addr a, UInt len) );
1414EV VG_(track_die_mem_munmap) ( void (*f)(Addr a, UInt len) );
njn25e49d8e72002-09-23 09:36:25 +00001415
njn810086f2002-11-14 12:42:47 +00001416EV VG_(track_bad_free) ( void (*f)(ThreadState* tst, Addr a) );
1417EV VG_(track_mismatched_free) ( void (*f)(ThreadState* tst, Addr a) );
njn25e49d8e72002-09-23 09:36:25 +00001418
njn810086f2002-11-14 12:42:47 +00001419EV VG_(track_pre_mem_read) ( void (*f)(CorePart part, ThreadState* tst,
1420 Char* s, Addr a, UInt size) );
1421EV VG_(track_pre_mem_read_asciiz) ( void (*f)(CorePart part, ThreadState* tst,
1422 Char* s, Addr a) );
1423EV VG_(track_pre_mem_write) ( void (*f)(CorePart part, ThreadState* tst,
1424 Char* s, Addr a, UInt size) );
1425/* Not implemented yet -- have to add in lots of places, which is a
1426 pain. Won't bother unless/until there's a need. */
1427/* EV VG_(track_post_mem_read) ( void (*f)(ThreadState* tst, Char* s,
1428 Addr a, UInt size) ); */
1429EV VG_(track_post_mem_write) ( void (*f)(Addr a, UInt size) );
njn25e49d8e72002-09-23 09:36:25 +00001430
1431
njn810086f2002-11-14 12:42:47 +00001432/* Scheduler events (not exhaustive) */
njn25e49d8e72002-09-23 09:36:25 +00001433
njn810086f2002-11-14 12:42:47 +00001434EV VG_(track_thread_run) ( void (*f)(ThreadId tid) );
njn25e49d8e72002-09-23 09:36:25 +00001435
njn810086f2002-11-14 12:42:47 +00001436/* Thread events (not exhaustive) */
sewardjdca84112002-11-13 22:29:34 +00001437
njn810086f2002-11-14 12:42:47 +00001438/* Called during thread create, before the new thread has run any
1439 instructions (or touched any memory). */
1440EV VG_(track_post_thread_create)( void (*f)(ThreadId tid, ThreadId child) );
1441/* Called once the joinee thread is terminated and the joining thread is
1442 about to resume. */
1443EV VG_(track_post_thread_join) ( void (*f)(ThreadId joiner, ThreadId joinee) );
njn25e49d8e72002-09-23 09:36:25 +00001444
njnd5bb0a52002-09-27 10:24:48 +00001445
njn810086f2002-11-14 12:42:47 +00001446/* Mutex events (not exhaustive) */
njn25e49d8e72002-09-23 09:36:25 +00001447
njn810086f2002-11-14 12:42:47 +00001448/* Called before a thread can block while waiting for a mutex (called
1449 regardless of whether the thread will block or not). */
1450EV VG_(track_pre_mutex_lock) ( void (*f)(ThreadId tid,
1451 void* /*pthread_mutex_t* */ mutex) );
1452/* Called once the thread actually holds the mutex (always paired with
1453 pre_mutex_lock). */
1454EV VG_(track_post_mutex_lock) ( void (*f)(ThreadId tid,
1455 void* /*pthread_mutex_t* */ mutex) );
1456/* Called after a thread has released a mutex (no need for a corresponding
1457 pre_mutex_unlock, because unlocking can't block). */
1458EV VG_(track_post_mutex_unlock) ( void (*f)(ThreadId tid,
1459 void* /*pthread_mutex_t* */ mutex) );
njn25e49d8e72002-09-23 09:36:25 +00001460
njn1045b0a2003-02-24 10:42:47 +00001461
1462/* Signal events (not exhaustive) */
1463
1464/* Called before a signal is delivered; `alt_stack' indicates if it is
1465 delivered on an alternative stack. */
1466EV VG_(track_pre_deliver_signal) ( void (*f)(ThreadId tid, Int sigNum,
1467 Bool alt_stack) );
1468/* Called after a signal is delivered. */
1469EV VG_(track_post_deliver_signal) ( void (*f)(ThreadId tid, Int sigNum ) );
1470
1471
1472/* Others... condition variables... */
njn810086f2002-11-14 12:42:47 +00001473/* ... */
1474
1475#undef EV
njn25e49d8e72002-09-23 09:36:25 +00001476
1477/* ------------------------------------------------------------------ */
1478/* Template functions */
1479
1480/* These are the parameterised functions in the core. The default definitions
njnd5bb0a52002-09-27 10:24:48 +00001481 are overridden by LD_PRELOADed skin version. At the very least, a skin
1482 must define the fundamental template functions. Depending on what needs
1483 are set, extra template functions will be used too. Functions are
1484 grouped under the needs that govern their use. */
njn25e49d8e72002-09-23 09:36:25 +00001485
1486
1487/* ------------------------------------------------------------------ */
1488/* Fundamental template functions */
1489
1490/* Initialise skin. Must do the following:
njn810086f2002-11-14 12:42:47 +00001491 - initialise the `details' struct, via the VG_(details_*)() functions
njn25e49d8e72002-09-23 09:36:25 +00001492 - register any helpers called by generated code
1493
1494 May do the following:
njn810086f2002-11-14 12:42:47 +00001495 - initialise the `needs' struct to indicate certain requirements, via
1496 the VG_(needs_*)() functions
1497 - initialise the `track' struct to indicate core events of interest, via
1498 the VG_(track_*)() functions
njn25e49d8e72002-09-23 09:36:25 +00001499 - register any skin-specific profiling events
1500 - any other skin-specific initialisation
1501*/
njn810086f2002-11-14 12:42:47 +00001502extern void SK_(pre_clo_init) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001503
njnd5bb0a52002-09-27 10:24:48 +00001504/* Do initialisation that can only be done after command line processing. */
njn25e49d8e72002-09-23 09:36:25 +00001505extern void SK_(post_clo_init)( void );
1506
1507/* Instrument a basic block. Must be a true function, ie. the same input
1508 always results in the same output, because basic blocks can be
1509 retranslated. Unless you're doing something really strange...
1510 'orig_addr' is the address of the first instruction in the block. */
1511extern UCodeBlock* SK_(instrument) ( UCodeBlock* cb, Addr orig_addr );
1512
1513/* Finish up, print out any results, etc. */
1514extern void SK_(fini) ( void );
1515
1516
1517/* ------------------------------------------------------------------ */
1518/* VG_(needs).report_errors */
1519
1520/* Identify if two errors are equal, or equal enough. `res' indicates how
1521 close is "close enough". `res' should be passed on as necessary, eg. if
njn810086f2002-11-14 12:42:47 +00001522 the Error's `extra' part contains an ExeContext, `res' should be
njn25e49d8e72002-09-23 09:36:25 +00001523 passed to VG_(eq_ExeContext)() if the ExeContexts are considered. Other
1524 than that, probably don't worry about it unless you have lots of very
1525 similar errors occurring.
1526 */
njn810086f2002-11-14 12:42:47 +00001527extern Bool SK_(eq_SkinError) ( VgRes res, Error* e1, Error* e2 );
njn25e49d8e72002-09-23 09:36:25 +00001528
1529/* Print error context. The passed function pp_ExeContext() can be (and
1530 probably should be) used to print the location of the error. */
njn810086f2002-11-14 12:42:47 +00001531extern void SK_(pp_SkinError) ( Error* err, void (*pp_ExeContext)(void) );
njn25e49d8e72002-09-23 09:36:25 +00001532
njn810086f2002-11-14 12:42:47 +00001533/* Should copy the `extra' part which the core uses to override the old
1534 version. This is necessary to move from a temporary stack copy to a
1535 permanent heap one.
njn25e49d8e72002-09-23 09:36:25 +00001536
njn810086f2002-11-14 12:42:47 +00001537 Then should fill in any details that could be postponed until after the
1538 decision whether to ignore the error (ie. details not affecting the
1539 result of SK_(eq_SkinError)()). This saves time when errors are ignored.
njn25e49d8e72002-09-23 09:36:25 +00001540
1541 Yuk.
1542*/
njn810086f2002-11-14 12:42:47 +00001543extern void* SK_(dup_extra_and_update) ( Error* err );
njn25e49d8e72002-09-23 09:36:25 +00001544
njn810086f2002-11-14 12:42:47 +00001545/* Return value indicates recognition. If recognised, must set skind using
1546 VG_(set_supp_kind)(). */
1547extern Bool SK_(recognised_suppression) ( Char* name, Supp* su );
njn25e49d8e72002-09-23 09:36:25 +00001548
njn810086f2002-11-14 12:42:47 +00001549/* Read any extra info for this suppression kind. Most likely for filling
1550 in the `extra' and `string' parts (with VG_(set_supp_{extra,string})())
1551 of a suppression if necessary. Should return False if a syntax error
1552 occurred, True otherwise. */
1553extern Bool SK_(read_extra_suppression_info) ( Int fd, Char* buf, Int nBuf,
1554 Supp* su );
njn25e49d8e72002-09-23 09:36:25 +00001555
1556/* This should just check the kinds match and maybe some stuff in the
njn810086f2002-11-14 12:42:47 +00001557 `string' and `extra' field if appropriate (using VG_(get_supp_*)() to
1558 get the relevant suppression parts). */
1559extern Bool SK_(error_matches_suppression)(Error* err, Supp* su);
njn25e49d8e72002-09-23 09:36:25 +00001560
1561
1562/* ------------------------------------------------------------------ */
1563/* VG_(needs).basic_block_discards */
1564
njnd5bb0a52002-09-27 10:24:48 +00001565/* Should discard any information that pertains to specific basic blocks
1566 or instructions within the address range given. */
njn25e49d8e72002-09-23 09:36:25 +00001567extern void SK_(discard_basic_block_info) ( Addr a, UInt size );
1568
1569
1570/* ------------------------------------------------------------------ */
1571/* VG_(needs).shadow_regs */
1572
1573/* Valid values for general registers and EFLAGS register, for initialising
1574 and updating registers when written in certain places in core. */
1575extern void SK_(written_shadow_regs_values) ( UInt* gen_reg, UInt* eflags );
1576
1577
1578/* ------------------------------------------------------------------ */
1579/* VG_(needs).command_line_options */
1580
njnd5bb0a52002-09-27 10:24:48 +00001581/* Return True if option was recognised. Presumably sets some state to
1582 record the option as well. */
1583extern Bool SK_(process_cmd_line_option) ( Char* argv );
njn25e49d8e72002-09-23 09:36:25 +00001584
1585/* Print out command line usage for skin options */
1586extern Char* SK_(usage) ( void );
1587
1588
1589/* ------------------------------------------------------------------ */
1590/* VG_(needs).client_requests */
1591
sewardj34042512002-10-22 04:14:35 +00001592extern Bool SK_(handle_client_request) ( ThreadState* tst, UInt* arg_block, UInt *ret );
njn25e49d8e72002-09-23 09:36:25 +00001593
1594
1595/* ------------------------------------------------------------------ */
1596/* VG_(needs).extends_UCode */
1597
njn4ba5a792002-09-30 10:23:54 +00001598/* Useful to use in VG_(get_Xreg_usage)() */
njn810086f2002-11-14 12:42:47 +00001599#define VG_UINSTR_READS_REG(ono,regs,isWrites) \
njn25e49d8e72002-09-23 09:36:25 +00001600 { if (mycat(u->tag,ono) == tag) \
njn810086f2002-11-14 12:42:47 +00001601 { regs[n] = mycat(u->val,ono); \
1602 isWrites[n] = False; \
njn25e49d8e72002-09-23 09:36:25 +00001603 n++; \
1604 } \
1605 }
njn810086f2002-11-14 12:42:47 +00001606#define VG_UINSTR_WRITES_REG(ono,regs,isWrites) \
njnd5bb0a52002-09-27 10:24:48 +00001607 { if (mycat(u->tag,ono) == tag) \
njn810086f2002-11-14 12:42:47 +00001608 { regs[n] = mycat(u->val,ono); \
1609 isWrites[n] = True; \
njnd5bb0a52002-09-27 10:24:48 +00001610 n++; \
1611 } \
njn25e49d8e72002-09-23 09:36:25 +00001612 }
1613
njn4ba5a792002-09-30 10:23:54 +00001614/* 'X' prefix indicates eXtended UCode. */
njn810086f2002-11-14 12:42:47 +00001615extern Int SK_(get_Xreg_usage) ( UInstr* u, Tag tag, Int* regs,
1616 Bool* isWrites );
njn4ba5a792002-09-30 10:23:54 +00001617extern void SK_(emit_XUInstr) ( UInstr* u, RRegSet regs_live_before );
1618extern Bool SK_(sane_XUInstr) ( Bool beforeRA, Bool beforeLiveness,
njn25e49d8e72002-09-23 09:36:25 +00001619 UInstr* u );
njn4ba5a792002-09-30 10:23:54 +00001620extern Char* SK_(name_XUOpcode) ( Opcode opc );
1621extern void SK_(pp_XUInstr) ( UInstr* u );
njn25e49d8e72002-09-23 09:36:25 +00001622
1623
1624/* ------------------------------------------------------------------ */
1625/* VG_(needs).syscall_wrapper */
1626
1627/* If either of the pre_ functions malloc() something to return, the
1628 * corresponding post_ function had better free() it!
1629 */
1630extern void* SK_( pre_syscall) ( ThreadId tid, UInt syscallno,
1631 Bool is_blocking );
1632extern void SK_(post_syscall) ( ThreadId tid, UInt syscallno,
1633 void* pre_result, Int res,
1634 Bool is_blocking );
1635
njnd5bb0a52002-09-27 10:24:48 +00001636
njn25e49d8e72002-09-23 09:36:25 +00001637/* ------------------------------------------------------------------ */
njnd5bb0a52002-09-27 10:24:48 +00001638/* VG_(needs).sizeof_shadow_chunk (if > 0) */
njn25e49d8e72002-09-23 09:36:25 +00001639
njn810086f2002-11-14 12:42:47 +00001640/* Must fill in the `extra' part, using VG_(set_sc_extra)(). */
njn25e49d8e72002-09-23 09:36:25 +00001641extern void SK_(complete_shadow_chunk) ( ShadowChunk* sc, ThreadState* tst );
1642
1643
1644/* ------------------------------------------------------------------ */
1645/* VG_(needs).alternative_free */
1646
njn810086f2002-11-14 12:42:47 +00001647/* If this need is set, when a dynamic block would normally be free'd, this
1648 is called instead. The block is contained inside the ShadowChunk; use
1649 the VG_(get_sc_*)() functions to access it. */
njn25e49d8e72002-09-23 09:36:25 +00001650extern void SK_(alt_free) ( ShadowChunk* sc, ThreadState* tst );
1651
njnd5bb0a52002-09-27 10:24:48 +00001652
njn25e49d8e72002-09-23 09:36:25 +00001653/* ---------------------------------------------------------------------
1654 VG_(needs).sanity_checks */
1655
njnd5bb0a52002-09-27 10:24:48 +00001656/* Can be useful for ensuring a skin's correctness. SK_(cheap_sanity_check)
1657 is called very frequently; SK_(expensive_sanity_check) is called less
1658 frequently and can be more involved. */
njn25e49d8e72002-09-23 09:36:25 +00001659extern Bool SK_(cheap_sanity_check) ( void );
1660extern Bool SK_(expensive_sanity_check) ( void );
1661
1662
1663#endif /* NDEF __VG_SKIN_H */
1664
1665/*--------------------------------------------------------------------*/
1666/*--- end vg_skin.h ---*/
1667/*--------------------------------------------------------------------*/
1668