blob: 6ad3ae664a01b8a63df58bd6de108aab4292d5ac [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
62 If you change this you'll have to also change at least these:
njn4ba5a792002-09-30 10:23:54 +000063 - VG_(rank_to_realreg)()
64 - VG_(realreg_to_rank)()
njn25e49d8e72002-09-23 09:36:25 +000065 - ppRegsLiveness()
66 - the RegsLive type (maybe -- RegsLive type must have more than
67 VG_MAX_REALREGS bits)
68
69 Do not change this unless you really know what you are doing! */
70#define VG_MAX_REALREGS 6
71
72
73/*====================================================================*/
njn1a1dd8b2002-09-27 10:42:20 +000074/*=== Basic types, useful macros ===*/
njn25e49d8e72002-09-23 09:36:25 +000075/*====================================================================*/
76
njn25e49d8e72002-09-23 09:36:25 +000077typedef unsigned char UChar;
78typedef unsigned short UShort;
79typedef unsigned int UInt;
80typedef unsigned long long int ULong;
81
82typedef signed char Char;
83typedef signed short Short;
84typedef signed int Int;
85typedef signed long long int Long;
86
87typedef unsigned int Addr;
88
89typedef unsigned char Bool;
90#define False ((Bool)0)
91#define True ((Bool)1)
92
93
njn1a1dd8b2002-09-27 10:42:20 +000094#define mycat_wrk(aaa,bbb) aaa##bbb
95#define mycat(aaa,bbb) mycat_wrk(aaa,bbb)
96
97/* No, really. I _am_ that strange. */
98#define OINK(nnn) VG_(message)(Vg_DebugMsg, "OINK %d",nnn)
99
njn25e49d8e72002-09-23 09:36:25 +0000100/* ---------------------------------------------------------------------
101 Now the basic types are set up, we can haul in the kernel-interface
102 definitions.
103 ------------------------------------------------------------------ */
104
njn2574bb4762002-09-24 11:23:50 +0000105#include "../coregrind/vg_kerneliface.h"
njn25e49d8e72002-09-23 09:36:25 +0000106
107
108/*====================================================================*/
njn27f1a382002-11-08 15:48:16 +0000109/*=== Core/skin interface version ===*/
110/*====================================================================*/
111
112/* The major version number indicates binary-incompatible changes to the
113 interface; if the core and skin major versions don't match, Valgrind
114 will abort. The minor version indicates binary-compatible changes.
njn27f1a382002-11-08 15:48:16 +0000115*/
njn27f1a382002-11-08 15:48:16 +0000116#define VG_CORE_INTERFACE_MAJOR_VERSION 1
njn563f96f2003-02-03 11:17:46 +0000117#define VG_CORE_INTERFACE_MINOR_VERSION 2
njn27f1a382002-11-08 15:48:16 +0000118
119extern const Int VG_(skin_interface_major_version);
120extern const Int VG_(skin_interface_minor_version);
121
njn563f96f2003-02-03 11:17:46 +0000122/* Every skin must include this macro somewhere, exactly once. */
njn27f1a382002-11-08 15:48:16 +0000123#define VG_DETERMINE_INTERFACE_VERSION \
124const Int VG_(skin_interface_major_version) = VG_CORE_INTERFACE_MAJOR_VERSION; \
125const Int VG_(skin_interface_minor_version) = VG_CORE_INTERFACE_MINOR_VERSION;
126
127/*====================================================================*/
njn25e49d8e72002-09-23 09:36:25 +0000128/*=== Command-line options ===*/
129/*====================================================================*/
130
131/* Verbosity level: 0 = silent, 1 (default), > 1 = more verbose. */
132extern Int VG_(clo_verbosity);
133
134/* Profile? */
135extern Bool VG_(clo_profile);
136
njn25e49d8e72002-09-23 09:36:25 +0000137/* Call this if a recognised option was bad for some reason.
138 Note: don't use it just because an option was unrecognised -- return 'False'
139 from SKN_(process_cmd_line_option) to indicate that. */
140extern void VG_(bad_option) ( Char* opt );
141
142/* Client args */
143extern Int VG_(client_argc);
144extern Char** VG_(client_argv);
145
njnd5bb0a52002-09-27 10:24:48 +0000146/* Client environment. Can be inspected with VG_(getenv)() */
njn25e49d8e72002-09-23 09:36:25 +0000147extern Char** VG_(client_envp);
148
149
150/*====================================================================*/
151/*=== Printing messages for the user ===*/
152/*====================================================================*/
153
154/* Print a message prefixed by "??<pid>?? "; '?' depends on the VgMsgKind.
155 Should be used for all user output. */
156
157typedef
158 enum { Vg_UserMsg, /* '?' == '=' */
159 Vg_DebugMsg, /* '?' == '-' */
160 Vg_DebugExtraMsg /* '?' == '+' */
161 }
162 VgMsgKind;
163
164/* Functions for building a message from multiple parts. */
165extern void VG_(start_msg) ( VgMsgKind kind );
166extern void VG_(add_to_msg) ( Char* format, ... );
167/* Ends and prints the message. Appends a newline. */
168extern void VG_(end_msg) ( void );
169
njnd5bb0a52002-09-27 10:24:48 +0000170/* Send a single-part message. Appends a newline. */
njn25e49d8e72002-09-23 09:36:25 +0000171extern void VG_(message) ( VgMsgKind kind, Char* format, ... );
172
173
174/*====================================================================*/
175/*=== Profiling ===*/
176/*====================================================================*/
177
178/* Nb: VGP_(register_profile_event)() relies on VgpUnc being the first one */
179#define VGP_CORE_LIST \
180 /* These ones depend on the core */ \
181 VGP_PAIR(VgpUnc, "unclassified"), \
182 VGP_PAIR(VgpRun, "running"), \
183 VGP_PAIR(VgpSched, "scheduler"), \
184 VGP_PAIR(VgpMalloc, "low-lev malloc/free"), \
185 VGP_PAIR(VgpCliMalloc, "client malloc/free"), \
186 VGP_PAIR(VgpStack, "adjust-stack"), \
187 VGP_PAIR(VgpTranslate, "translate-main"), \
188 VGP_PAIR(VgpToUCode, "to-ucode"), \
189 VGP_PAIR(VgpFromUcode, "from-ucode"), \
190 VGP_PAIR(VgpImprove, "improve"), \
191 VGP_PAIR(VgpRegAlloc, "reg-alloc"), \
192 VGP_PAIR(VgpLiveness, "liveness-analysis"), \
193 VGP_PAIR(VgpDoLRU, "do-lru"), \
194 VGP_PAIR(VgpSlowFindT, "slow-search-transtab"), \
195 VGP_PAIR(VgpInitMem, "init-memory"), \
196 VGP_PAIR(VgpExeContext, "exe-context"), \
197 VGP_PAIR(VgpReadSyms, "read-syms"), \
198 VGP_PAIR(VgpSearchSyms, "search-syms"), \
199 VGP_PAIR(VgpAddToT, "add-to-transtab"), \
200 VGP_PAIR(VgpCoreSysWrap, "core-syscall-wrapper"), \
201 VGP_PAIR(VgpDemangle, "demangle"), \
njn37cea302002-09-30 11:24:00 +0000202 VGP_PAIR(VgpCoreCheapSanity, "core-cheap-sanity"), \
203 VGP_PAIR(VgpCoreExpensiveSanity, "core-expensive-sanity"), \
njn25e49d8e72002-09-23 09:36:25 +0000204 /* These ones depend on the skin */ \
205 VGP_PAIR(VgpPreCloInit, "pre-clo-init"), \
206 VGP_PAIR(VgpPostCloInit, "post-clo-init"), \
207 VGP_PAIR(VgpInstrument, "instrument"), \
208 VGP_PAIR(VgpSkinSysWrap, "skin-syscall-wrapper"), \
njn37cea302002-09-30 11:24:00 +0000209 VGP_PAIR(VgpSkinCheapSanity, "skin-cheap-sanity"), \
210 VGP_PAIR(VgpSkinExpensiveSanity, "skin-expensive-sanity"), \
njn25e49d8e72002-09-23 09:36:25 +0000211 VGP_PAIR(VgpFini, "fini")
212
213#define VGP_PAIR(n,name) n
214typedef enum { VGP_CORE_LIST } VgpCoreCC;
215#undef VGP_PAIR
216
217/* When registering skin profiling events, ensure that the 'n' value is in
218 * the range (VgpFini+1..) */
219extern void VGP_(register_profile_event) ( Int n, Char* name );
220
221extern void VGP_(pushcc) ( UInt cc );
222extern void VGP_(popcc) ( UInt cc );
223
224/* Define them only if they haven't already been defined by vg_profile.c */
225#ifndef VGP_PUSHCC
226# define VGP_PUSHCC(x)
227#endif
228#ifndef VGP_POPCC
229# define VGP_POPCC(x)
230#endif
231
232
233/*====================================================================*/
234/*=== Useful stuff to call from generated code ===*/
235/*====================================================================*/
236
237/* ------------------------------------------------------------------ */
238/* General stuff */
239
njn41557122002-10-14 09:25:37 +0000240/* 64-bit counter for the number of basic blocks done. */
241extern ULong VG_(bbs_done);
242
njn25e49d8e72002-09-23 09:36:25 +0000243/* Get the simulated %esp */
244extern Addr VG_(get_stack_pointer) ( void );
245
njnd5bb0a52002-09-27 10:24:48 +0000246/* Detect if an address is within Valgrind's stack or Valgrind's
247 m_state_static; useful for memory leak detectors to tell if a block
248 is used by Valgrind (and thus can be ignored). */
njn25e49d8e72002-09-23 09:36:25 +0000249extern Bool VG_(within_stack)(Addr a);
njn25e49d8e72002-09-23 09:36:25 +0000250extern Bool VG_(within_m_state_static)(Addr a);
251
252/* Check if an address is 4-byte aligned */
253#define IS_ALIGNED4_ADDR(aaa_p) (0 == (((UInt)(aaa_p)) & 3))
254
255
256/* ------------------------------------------------------------------ */
257/* Thread-related stuff */
258
259/* Special magic value for an invalid ThreadId. It corresponds to
260 LinuxThreads using zero as the initial value for
261 pthread_mutex_t.__m_owner and pthread_cond_t.__c_waiting. */
262#define VG_INVALID_THREADID ((ThreadId)(0))
263
264/* ThreadIds are simply indices into the vg_threads[] array. */
265typedef
266 UInt
267 ThreadId;
268
njnd5bb0a52002-09-27 10:24:48 +0000269/* struct _ThreadState defined elsewhere; ThreadState is abstract as its
270 definition is not important for skins. */
njn25e49d8e72002-09-23 09:36:25 +0000271typedef
272 struct _ThreadState
273 ThreadState;
274
sewardj7ab2aca2002-10-20 19:40:32 +0000275extern ThreadId VG_(get_current_tid) ( void );
sewardjb52a1b02002-10-23 21:38:22 +0000276extern ThreadId VG_(get_current_or_recent_tid) ( void );
sewardj7ab2aca2002-10-20 19:40:32 +0000277extern ThreadId VG_(get_tid_from_ThreadState) ( ThreadState* );
njn25e49d8e72002-09-23 09:36:25 +0000278extern ThreadState* VG_(get_ThreadState) ( ThreadId tid );
279
280
281/*====================================================================*/
282/*=== Valgrind's version of libc ===*/
283/*====================================================================*/
284
285/* Valgrind doesn't use libc at all, for good reasons (trust us). So here
286 are its own versions of C library functions, but with VG_ prefixes. Note
287 that the types of some are slightly different to the real ones. Some
288 extra useful functions are provided too; descriptions of how they work
289 are given below. */
290
291#if !defined(NULL)
292# define NULL ((void*)0)
293#endif
294
295
296/* ------------------------------------------------------------------ */
297/* stdio.h
298 *
299 * Note that they all output to the file descriptor given by the
300 * --logfile-fd=N argument, which defaults to 2 (stderr). Hence no
301 * need for VG_(fprintf)().
njn25e49d8e72002-09-23 09:36:25 +0000302 */
sewardj78e3cd92002-10-22 04:45:48 +0000303extern UInt VG_(printf) ( const char *format, ... );
njn25e49d8e72002-09-23 09:36:25 +0000304/* too noisy ... __attribute__ ((format (printf, 1, 2))) ; */
sewardj78e3cd92002-10-22 04:45:48 +0000305extern UInt VG_(sprintf) ( Char* buf, Char *format, ... );
306extern UInt VG_(vprintf) ( void(*send)(Char),
njn25e49d8e72002-09-23 09:36:25 +0000307 const Char *format, va_list vargs );
308
njn41557122002-10-14 09:25:37 +0000309extern Int VG_(rename) ( Char* old_name, Char* new_name );
310
njn25e49d8e72002-09-23 09:36:25 +0000311/* ------------------------------------------------------------------ */
312/* stdlib.h */
313
314extern void* VG_(malloc) ( Int nbytes );
njnd5bb0a52002-09-27 10:24:48 +0000315extern void VG_(free) ( void* p );
316extern void* VG_(calloc) ( Int n, Int nbytes );
317extern void* VG_(realloc) ( void* p, Int size );
318extern void* VG_(malloc_aligned) ( Int align_bytes, Int nbytes );
njn25e49d8e72002-09-23 09:36:25 +0000319
320extern void VG_(print_malloc_stats) ( void );
321
322
323extern void VG_(exit)( Int status )
324 __attribute__ ((__noreturn__));
njne427a662002-10-02 11:08:25 +0000325/* Prints a panic message (a constant string), appends newline and bug
326 reporting info, aborts. */
327__attribute__ ((__noreturn__))
328extern void VG_(skin_panic) ( Char* str );
njn25e49d8e72002-09-23 09:36:25 +0000329
njnd5bb0a52002-09-27 10:24:48 +0000330/* Looks up VG_(client_envp) */
njn25e49d8e72002-09-23 09:36:25 +0000331extern Char* VG_(getenv) ( Char* name );
332
333/* Crude stand-in for the glibc system() call. */
334extern Int VG_(system) ( Char* cmd );
335
njnd5bb0a52002-09-27 10:24:48 +0000336extern Long VG_(atoll) ( Char* str );
njn25e49d8e72002-09-23 09:36:25 +0000337
338/* Like atoll(), but converts a number of base 2..36 */
339extern Long VG_(atoll36) ( UInt base, Char* str );
340
341
342/* ------------------------------------------------------------------ */
njnd5bb0a52002-09-27 10:24:48 +0000343/* ctype.h */
njn25e49d8e72002-09-23 09:36:25 +0000344extern Bool VG_(isspace) ( Char c );
345extern Bool VG_(isdigit) ( Char c );
346extern Char VG_(toupper) ( Char c );
347
348
349/* ------------------------------------------------------------------ */
350/* string.h */
351extern Int VG_(strlen) ( const Char* str );
352extern Char* VG_(strcat) ( Char* dest, const Char* src );
353extern Char* VG_(strncat) ( Char* dest, const Char* src, Int n );
354extern Char* VG_(strpbrk) ( const Char* s, const Char* accept );
355extern Char* VG_(strcpy) ( Char* dest, const Char* src );
356extern Char* VG_(strncpy) ( Char* dest, const Char* src, Int ndest );
357extern Int VG_(strcmp) ( const Char* s1, const Char* s2 );
358extern Int VG_(strncmp) ( const Char* s1, const Char* s2, Int nmax );
359extern Char* VG_(strstr) ( const Char* haystack, Char* needle );
360extern Char* VG_(strchr) ( const Char* s, Char c );
361extern Char* VG_(strdup) ( const Char* s);
sewardj7ab2aca2002-10-20 19:40:32 +0000362extern void* VG_(memcpy) ( void *d, const void *s, Int sz );
363extern void* VG_(memset) ( void *s, Int c, Int sz );
njn25e49d8e72002-09-23 09:36:25 +0000364
njnd5bb0a52002-09-27 10:24:48 +0000365/* Like strcmp() and strncmp(), but stop comparing at any whitespace. */
njn25e49d8e72002-09-23 09:36:25 +0000366extern Int VG_(strcmp_ws) ( const Char* s1, const Char* s2 );
njn25e49d8e72002-09-23 09:36:25 +0000367extern Int VG_(strncmp_ws) ( const Char* s1, const Char* s2, Int nmax );
368
njnd5bb0a52002-09-27 10:24:48 +0000369/* Like strncpy(), but if 'src' is longer than 'ndest' inserts a '\0' as the
370 last character. */
njn25e49d8e72002-09-23 09:36:25 +0000371extern void VG_(strncpy_safely) ( Char* dest, const Char* src, Int ndest );
372
373/* Mini-regexp function. Searches for 'pat' in 'str'. Supports
374 * meta-symbols '*' and '?'. '\' escapes meta-symbols. */
njn4ba5a792002-09-30 10:23:54 +0000375extern Bool VG_(string_match) ( Char* pat, Char* str );
njn25e49d8e72002-09-23 09:36:25 +0000376
377
378/* ------------------------------------------------------------------ */
379/* math.h */
njnd5bb0a52002-09-27 10:24:48 +0000380/* Returns the base-2 logarithm of x. */
njn25e49d8e72002-09-23 09:36:25 +0000381extern Int VG_(log2) ( Int x );
382
383
384/* ------------------------------------------------------------------ */
njnd5bb0a52002-09-27 10:24:48 +0000385/* unistd.h, fcntl.h, sys/stat.h */
sewardj4cf05692002-10-27 20:28:29 +0000386extern Int VG_(getpid) ( void );
387extern Int VG_(getppid) ( void );
njnd5bb0a52002-09-27 10:24:48 +0000388
njn4aca2d22002-10-04 10:29:38 +0000389extern Int VG_(open) ( const Char* pathname, Int flags, Int mode );
390extern Int VG_(read) ( Int fd, void* buf, Int count);
391extern Int VG_(write) ( Int fd, void* buf, Int count);
392extern void VG_(close) ( Int fd );
njnd5bb0a52002-09-27 10:24:48 +0000393
njn41557122002-10-14 09:25:37 +0000394/* Nb: VG_(rename)() declared in stdio.h section above */
njn4aca2d22002-10-04 10:29:38 +0000395extern Int VG_(unlink) ( Char* file_name );
396extern Int VG_(stat) ( Char* file_name, struct vki_stat* buf );
njn25e49d8e72002-09-23 09:36:25 +0000397
398
399/* ------------------------------------------------------------------ */
400/* assert.h */
401/* Asserts permanently enabled -- no turning off with NDEBUG. Hurrah! */
402#define VG__STRING(__str) #__str
403
njne427a662002-10-02 11:08:25 +0000404#define sk_assert(expr) \
njn25e49d8e72002-09-23 09:36:25 +0000405 ((void) ((expr) ? 0 : \
njne427a662002-10-02 11:08:25 +0000406 (VG_(skin_assert_fail) (VG__STRING(expr), \
407 __FILE__, __LINE__, \
408 __PRETTY_FUNCTION__), 0)))
njn25e49d8e72002-09-23 09:36:25 +0000409
njne427a662002-10-02 11:08:25 +0000410__attribute__ ((__noreturn__))
411extern void VG_(skin_assert_fail) ( Char* expr, Char* file,
412 Int line, Char* fn );
njn25e49d8e72002-09-23 09:36:25 +0000413
414
415/* ------------------------------------------------------------------ */
njnd5bb0a52002-09-27 10:24:48 +0000416/* system/mman.h */
njn25e49d8e72002-09-23 09:36:25 +0000417extern void* VG_(mmap)( void* start, UInt length,
418 UInt prot, UInt flags, UInt fd, UInt offset );
419extern Int VG_(munmap)( void* start, Int length );
420
421/* Get memory by anonymous mmap. */
422extern void* VG_(get_memory_from_mmap) ( Int nBytes, Char* who );
423
424
425/* ------------------------------------------------------------------ */
426/* signal.h.
427
428 Note that these use the vk_ (kernel) structure
429 definitions, which are different in places from those that glibc
430 defines -- hence the 'k' prefix. Since we're operating right at the
431 kernel interface, glibc's view of the world is entirely irrelevant. */
432
433/* --- Signal set ops --- */
njnd5bb0a52002-09-27 10:24:48 +0000434extern Int VG_(ksigfillset) ( vki_ksigset_t* set );
435extern Int VG_(ksigemptyset) ( vki_ksigset_t* set );
njn25e49d8e72002-09-23 09:36:25 +0000436
njnd5bb0a52002-09-27 10:24:48 +0000437extern Bool VG_(kisfullsigset) ( vki_ksigset_t* set );
438extern Bool VG_(kisemptysigset) ( vki_ksigset_t* set );
njn25e49d8e72002-09-23 09:36:25 +0000439
njnd5bb0a52002-09-27 10:24:48 +0000440extern Int VG_(ksigaddset) ( vki_ksigset_t* set, Int signum );
441extern Int VG_(ksigdelset) ( vki_ksigset_t* set, Int signum );
njn25e49d8e72002-09-23 09:36:25 +0000442extern Int VG_(ksigismember) ( vki_ksigset_t* set, Int signum );
443
njnd5bb0a52002-09-27 10:24:48 +0000444extern void VG_(ksigaddset_from_set) ( vki_ksigset_t* dst, vki_ksigset_t* src );
445extern void VG_(ksigdelset_from_set) ( vki_ksigset_t* dst, vki_ksigset_t* src );
njn25e49d8e72002-09-23 09:36:25 +0000446
447/* --- Mess with the kernel's sig state --- */
njnd5bb0a52002-09-27 10:24:48 +0000448extern Int VG_(ksigprocmask) ( Int how, const vki_ksigset_t* set,
njn25e49d8e72002-09-23 09:36:25 +0000449 vki_ksigset_t* oldset );
njnd5bb0a52002-09-27 10:24:48 +0000450extern Int VG_(ksigaction) ( Int signum,
451 const vki_ksigaction* act,
452 vki_ksigaction* oldact );
njn25e49d8e72002-09-23 09:36:25 +0000453
njnd5bb0a52002-09-27 10:24:48 +0000454extern Int VG_(ksignal) ( Int signum, void (*sighandler)(Int) );
455extern Int VG_(ksigaltstack) ( const vki_kstack_t* ss, vki_kstack_t* oss );
njn25e49d8e72002-09-23 09:36:25 +0000456
sewardjdcaf3122002-09-30 23:12:33 +0000457extern Int VG_(kkill) ( Int pid, Int signo );
458extern Int VG_(ksigpending) ( vki_ksigset_t* set );
njn25e49d8e72002-09-23 09:36:25 +0000459
460
461/*====================================================================*/
462/*=== UCode definition ===*/
463/*====================================================================*/
464
sewardje1042472002-09-30 12:33:11 +0000465/* Tags which describe what operands are. Must fit into 4 bits, which
466 they clearly do. */
njn25e49d8e72002-09-23 09:36:25 +0000467typedef
sewardje1042472002-09-30 12:33:11 +0000468enum { TempReg =0, /* virtual temp-reg */
469 ArchReg =1, /* simulated integer reg */
470 ArchRegS =2, /* simulated segment reg */
471 RealReg =3, /* real machine's real reg */
472 SpillNo =4, /* spill slot location */
473 Literal =5, /* literal; .lit32 field has actual value */
474 Lit16 =6, /* literal; .val[123] field has actual value */
475 NoValue =7 /* operand not in use */
476 }
477 Tag;
njn25e49d8e72002-09-23 09:36:25 +0000478
njnd5bb0a52002-09-27 10:24:48 +0000479/* Invalid register numbers (can't be negative) */
njn25e49d8e72002-09-23 09:36:25 +0000480#define INVALID_TEMPREG 999999999
481#define INVALID_REALREG 999999999
482
483/* Microinstruction opcodes. */
484typedef
485 enum {
njnd5bb0a52002-09-27 10:24:48 +0000486 NOP, /* Null op */
njn25e49d8e72002-09-23 09:36:25 +0000487
sewardj7a5ebcf2002-11-13 22:42:13 +0000488 LOCK, /* Indicate the existance of a LOCK prefix (functionally NOP) */
489
njnd5bb0a52002-09-27 10:24:48 +0000490 /* Moving values around */
491 GET, PUT, /* simulated register <--> TempReg */
492 GETF, PUTF, /* simulated %eflags <--> TempReg */
493 LOAD, STORE, /* memory <--> TempReg */
494 MOV, /* TempReg <--> TempReg */
495 CMOV, /* Used for cmpxchg and cmov */
njn25e49d8e72002-09-23 09:36:25 +0000496
njnd5bb0a52002-09-27 10:24:48 +0000497 /* Arithmetic/logical ops */
498 ADD, ADC, SUB, SBB, /* Add/subtract (w/wo carry) */
499 AND, OR, XOR, NOT, /* Boolean ops */
500 SHL, SHR, SAR, ROL, ROR, RCL, RCR, /* Shift/rotate (w/wo carry) */
501 NEG, /* Negate */
502 INC, DEC, /* Increment/decrement */
503 BSWAP, /* Big-endian <--> little-endian */
504 CC2VAL, /* Condition code --> 0 or 1 */
505 WIDEN, /* Signed or unsigned widening */
njn25e49d8e72002-09-23 09:36:25 +0000506
njnd5bb0a52002-09-27 10:24:48 +0000507 /* Conditional or unconditional jump */
508 JMP,
509
510 /* FPU ops */
511 FPU, /* Doesn't touch memory */
512 FPU_R, FPU_W, /* Reads/writes memory */
513
514 /* Not strictly needed, but improve address calculation translations. */
njn25e49d8e72002-09-23 09:36:25 +0000515 LEA1, /* reg2 := const + reg1 */
516 LEA2, /* reg3 := const + reg1 + reg2 * 1,2,4 or 8 */
517
njnd5bb0a52002-09-27 10:24:48 +0000518 /* Hack for x86 REP insns. Jump to literal if TempReg/RealReg is zero. */
519 JIFZ,
njn25e49d8e72002-09-23 09:36:25 +0000520
njnd5bb0a52002-09-27 10:24:48 +0000521 /* Advance the simulated %eip by some small (< 128) number. */
522 INCEIP,
njn25e49d8e72002-09-23 09:36:25 +0000523
sewardje1042472002-09-30 12:33:11 +0000524 /* Dealing with segment registers */
525 GETSEG, PUTSEG, /* simulated segment register <--> TempReg */
526 USESEG, /* (LDT/GDT index, virtual addr) --> linear addr */
527
njnd5bb0a52002-09-27 10:24:48 +0000528 /* Not for translating x86 calls -- only to call helpers */
529 CALLM_S, CALLM_E, /* Mark start/end of CALLM push/pop sequence */
530 PUSH, POP, CLEAR, /* Add/remove/zap args for helpers */
531 CALLM, /* Call assembly-code helper */
532
533 /* Not for translating x86 calls -- only to call C helper functions of
534 up to three arguments (or two if the functions has a return value).
535 Arguments and return value must be word-sized. More arguments can
536 be faked with global variables (eg. use VG_(set_global_var)()).
537
538 Seven possibilities: 'arg[123]' show where args go, 'ret' shows
539 where return value goes (if present).
njn25e49d8e72002-09-23 09:36:25 +0000540
541 CCALL(-, -, - ) void f(void)
542 CCALL(arg1, -, - ) void f(UInt arg1)
543 CCALL(arg1, arg2, - ) void f(UInt arg1, UInt arg2)
544 CCALL(arg1, arg2, arg3) void f(UInt arg1, UInt arg2, UInt arg3)
545 CCALL(-, -, ret ) UInt f(UInt)
546 CCALL(arg1, -, ret ) UInt f(UInt arg1)
njnd5bb0a52002-09-27 10:24:48 +0000547 CCALL(arg1, arg2, ret ) UInt f(UInt arg1, UInt arg2) */
njn25e49d8e72002-09-23 09:36:25 +0000548 CCALL,
549
njnd5bb0a52002-09-27 10:24:48 +0000550 /* This opcode makes it easy for skins that extend UCode to do this to
551 avoid opcode overlap:
njn25e49d8e72002-09-23 09:36:25 +0000552
njnd5bb0a52002-09-27 10:24:48 +0000553 enum { EU_OP1 = DUMMY_FINAL_UOPCODE + 1, ... }
njn25e49d8e72002-09-23 09:36:25 +0000554
555 WARNING: Do not add new opcodes after this one! They can be added
556 before, though. */
557 DUMMY_FINAL_UOPCODE
558 }
559 Opcode;
560
561
njnd5bb0a52002-09-27 10:24:48 +0000562/* Condition codes, using the Intel encoding. CondAlways is an extra. */
njn25e49d8e72002-09-23 09:36:25 +0000563typedef
564 enum {
565 CondO = 0, /* overflow */
566 CondNO = 1, /* no overflow */
567 CondB = 2, /* below */
568 CondNB = 3, /* not below */
569 CondZ = 4, /* zero */
570 CondNZ = 5, /* not zero */
571 CondBE = 6, /* below or equal */
572 CondNBE = 7, /* not below or equal */
573 CondS = 8, /* negative */
sewardje1042472002-09-30 12:33:11 +0000574 CondNS = 9, /* not negative */
njn25e49d8e72002-09-23 09:36:25 +0000575 CondP = 10, /* parity even */
576 CondNP = 11, /* not parity even */
577 CondL = 12, /* jump less */
578 CondNL = 13, /* not less */
579 CondLE = 14, /* less or equal */
580 CondNLE = 15, /* not less or equal */
581 CondAlways = 16 /* Jump always */
582 }
583 Condcode;
584
585
586/* Descriptions of additional properties of *unconditional* jumps. */
587typedef
588 enum {
589 JmpBoring=0, /* boring unconditional jump */
590 JmpCall=1, /* jump due to an x86 call insn */
591 JmpRet=2, /* jump due to an x86 ret insn */
592 JmpSyscall=3, /* do a system call, then jump */
593 JmpClientReq=4 /* do a client request, then jump */
594 }
595 JmpKind;
596
597
598/* Flags. User-level code can only read/write O(verflow), S(ign),
599 Z(ero), A(ux-carry), C(arry), P(arity), and may also write
600 D(irection). That's a total of 7 flags. A FlagSet is a bitset,
601 thusly:
602 76543210
603 DOSZACP
604 and bit 7 must always be zero since it is unused.
sewardj2370f3b2002-11-30 15:01:01 +0000605
606 Note: these Flag? values are **not** the positions in the actual
607 %eflags register. */
608
njn25e49d8e72002-09-23 09:36:25 +0000609typedef UChar FlagSet;
610
611#define FlagD (1<<6)
612#define FlagO (1<<5)
613#define FlagS (1<<4)
614#define FlagZ (1<<3)
615#define FlagA (1<<2)
616#define FlagC (1<<1)
617#define FlagP (1<<0)
618
619#define FlagsOSZACP (FlagO | FlagS | FlagZ | FlagA | FlagC | FlagP)
620#define FlagsOSZAP (FlagO | FlagS | FlagZ | FlagA | FlagP)
621#define FlagsOSZCP (FlagO | FlagS | FlagZ | FlagC | FlagP)
622#define FlagsOSACP (FlagO | FlagS | FlagA | FlagC | FlagP)
623#define FlagsSZACP ( FlagS | FlagZ | FlagA | FlagC | FlagP)
624#define FlagsSZAP ( FlagS | FlagZ | FlagA | FlagP)
625#define FlagsZCP ( FlagZ | FlagC | FlagP)
626#define FlagsOC (FlagO | FlagC )
627#define FlagsAC ( FlagA | FlagC )
628
629#define FlagsALL (FlagsOSZACP | FlagD)
630#define FlagsEmpty (FlagSet)0
631
632
sewardj2370f3b2002-11-30 15:01:01 +0000633/* flag positions in eflags */
634#define EFlagC (1 << 0) /* carry */
635#define EFlagP (1 << 2) /* parity */
sewardjfa492d42002-12-08 18:20:01 +0000636#define EFlagA (1 << 4) /* aux carry */
sewardj2370f3b2002-11-30 15:01:01 +0000637#define EFlagZ (1 << 6) /* zero */
638#define EFlagS (1 << 7) /* sign */
sewardjfa492d42002-12-08 18:20:01 +0000639#define EFlagD (1 << 10) /* direction */
sewardj2370f3b2002-11-30 15:01:01 +0000640#define EFlagO (1 << 11) /* overflow */
641
njn25e49d8e72002-09-23 09:36:25 +0000642/* Liveness of general purpose registers, useful for code generation.
643 Reg rank order 0..N-1 corresponds to bits 0..N-1, ie. first
644 reg's liveness in bit 0, last reg's in bit N-1. Note that
645 these rankings don't match the Intel register ordering. */
646typedef UInt RRegSet;
647
648#define ALL_RREGS_DEAD 0 /* 0000...00b */
njne7c258c2002-10-03 08:57:01 +0000649#define ALL_RREGS_LIVE ((1 << VG_MAX_REALREGS)-1) /* 0011...11b */
njn25e49d8e72002-09-23 09:36:25 +0000650#define UNIT_RREGSET(rank) (1 << (rank))
651
652#define IS_RREG_LIVE(rank,rregs_live) (rregs_live & UNIT_RREGSET(rank))
653#define SET_RREG_LIVENESS(rank,rregs_live,b) \
654 do { RRegSet unit = UNIT_RREGSET(rank); \
655 if (b) rregs_live |= unit; \
656 else rregs_live &= ~unit; \
657 } while(0)
658
659
660/* A Micro (u)-instruction. */
661typedef
662 struct {
663 /* word 1 */
664 UInt lit32; /* 32-bit literal */
665
666 /* word 2 */
667 UShort val1; /* first operand */
668 UShort val2; /* second operand */
669
670 /* word 3 */
671 UShort val3; /* third operand */
672 UChar opcode; /* opcode */
673 UChar size; /* data transfer size */
674
675 /* word 4 */
676 FlagSet flags_r; /* :: FlagSet */
677 FlagSet flags_w; /* :: FlagSet */
678 UChar tag1:4; /* first operand tag */
679 UChar tag2:4; /* second operand tag */
680 UChar tag3:4; /* third operand tag */
681 UChar extra4b:4; /* Spare field, used by WIDEN for src
682 -size, and by LEA2 for scale (1,2,4 or 8),
683 and by JMPs for original x86 instr size */
684
685 /* word 5 */
686 UChar cond; /* condition, for jumps */
687 Bool signed_widen:1; /* signed or unsigned WIDEN ? */
688 JmpKind jmpkind:3; /* additional properties of unconditional JMP */
689
690 /* Additional properties for UInstrs that call C functions:
691 - CCALL
692 - PUT (when %ESP is the target)
693 - possibly skin-specific UInstrs
694 */
695 UChar argc:2; /* Number of args, max 3 */
696 UChar regparms_n:2; /* Number of args passed in registers */
697 Bool has_ret_val:1; /* Function has return value? */
698
699 /* RealReg liveness; only sensical after reg alloc and liveness
700 analysis done. This info is a little bit arch-specific --
701 VG_MAX_REALREGS can vary on different architectures. Note that
702 to use this information requires converting between register ranks
njn4ba5a792002-09-30 10:23:54 +0000703 and the Intel register numbers, using VG_(realreg_to_rank)()
704 and/or VG_(rank_to_realreg)() */
njn25e49d8e72002-09-23 09:36:25 +0000705 RRegSet regs_live_after:VG_MAX_REALREGS;
706 }
707 UInstr;
708
709
njn25e49d8e72002-09-23 09:36:25 +0000710typedef
njn810086f2002-11-14 12:42:47 +0000711 struct _UCodeBlock
njn25e49d8e72002-09-23 09:36:25 +0000712 UCodeBlock;
713
njn810086f2002-11-14 12:42:47 +0000714extern Int VG_(get_num_instrs) (UCodeBlock* cb);
715extern Int VG_(get_num_temps) (UCodeBlock* cb);
njn25e49d8e72002-09-23 09:36:25 +0000716
njn810086f2002-11-14 12:42:47 +0000717extern UInstr* VG_(get_instr) (UCodeBlock* cb, Int i);
718extern UInstr* VG_(get_last_instr) (UCodeBlock* cb);
719
njn25e49d8e72002-09-23 09:36:25 +0000720/*====================================================================*/
721/*=== Instrumenting UCode ===*/
722/*====================================================================*/
723
njn810086f2002-11-14 12:42:47 +0000724/* Find what this instruction does to its regs. `tag' indicates whether we're
725 considering TempRegs (pre-reg-alloc) or RealRegs (post-reg-alloc).
726 `regs' is filled with the affected register numbers, `isWrites' parallels
727 it and indicates if the reg is read or written. If a reg is read and
728 written, it will appear twice in `regs'. `regs' and `isWrites' must be
729 able to fit 3 elements.
njn25e49d8e72002-09-23 09:36:25 +0000730
njn810086f2002-11-14 12:42:47 +0000731 Useful for analysis/optimisation passes. */
732extern Int VG_(get_reg_usage) ( UInstr* u, Tag tag, Int* regs, Bool* isWrites );
njn25e49d8e72002-09-23 09:36:25 +0000733
734
njnd5bb0a52002-09-27 10:24:48 +0000735/* Used to register helper functions to be called from generated code. A
736 limited number of compact helpers can be registered; the code generated
737 to call them is slightly shorter -- so register the mostly frequently
738 called helpers as compact. */
njn25e49d8e72002-09-23 09:36:25 +0000739extern void VG_(register_compact_helper) ( Addr a );
740extern void VG_(register_noncompact_helper) ( Addr a );
741
742
743/* ------------------------------------------------------------------ */
744/* Virtual register allocation */
745
746/* Get a new virtual register */
njn4ba5a792002-09-30 10:23:54 +0000747extern Int VG_(get_new_temp) ( UCodeBlock* cb );
njn25e49d8e72002-09-23 09:36:25 +0000748
749/* Get a new virtual shadow register */
njn4ba5a792002-09-30 10:23:54 +0000750extern Int VG_(get_new_shadow) ( UCodeBlock* cb );
njn25e49d8e72002-09-23 09:36:25 +0000751
752/* Get a virtual register's corresponding virtual shadow register */
753#define SHADOW(tempreg) ((tempreg)+1)
754
755
756/* ------------------------------------------------------------------ */
757/* Low-level UInstr builders */
njn4ba5a792002-09-30 10:23:54 +0000758extern void VG_(new_NOP) ( UInstr* u );
759extern void VG_(new_UInstr0) ( UCodeBlock* cb, Opcode opcode, Int sz );
760extern void VG_(new_UInstr1) ( UCodeBlock* cb, Opcode opcode, Int sz,
njn25e49d8e72002-09-23 09:36:25 +0000761 Tag tag1, UInt val1 );
njn4ba5a792002-09-30 10:23:54 +0000762extern void VG_(new_UInstr2) ( UCodeBlock* cb, Opcode opcode, Int sz,
njn25e49d8e72002-09-23 09:36:25 +0000763 Tag tag1, UInt val1,
764 Tag tag2, UInt val2 );
njn4ba5a792002-09-30 10:23:54 +0000765extern void VG_(new_UInstr3) ( UCodeBlock* cb, Opcode opcode, Int sz,
njn25e49d8e72002-09-23 09:36:25 +0000766 Tag tag1, UInt val1,
767 Tag tag2, UInt val2,
768 Tag tag3, UInt val3 );
njn25e49d8e72002-09-23 09:36:25 +0000769
njn810086f2002-11-14 12:42:47 +0000770/* Set read/write/undefined flags. Undefined flags are treaten as written,
771 but it's worth keeping them logically distinct. */
772extern void VG_(set_flag_fields) ( UCodeBlock* cb, FlagSet fr, FlagSet fw,
773 FlagSet fu);
njn4ba5a792002-09-30 10:23:54 +0000774extern void VG_(set_lit_field) ( UCodeBlock* cb, UInt lit32 );
775extern void VG_(set_ccall_fields) ( UCodeBlock* cb, Addr fn, UChar argc,
776 UChar regparms_n, Bool has_ret_val );
njn810086f2002-11-14 12:42:47 +0000777extern void VG_(set_cond_field) ( UCodeBlock* cb, Condcode code );
njn25e49d8e72002-09-23 09:36:25 +0000778
njn4ba5a792002-09-30 10:23:54 +0000779extern void VG_(copy_UInstr) ( UCodeBlock* cb, UInstr* instr );
780
781extern Bool VG_(any_flag_use)( UInstr* u );
njn25e49d8e72002-09-23 09:36:25 +0000782
njnac6c1762002-10-04 14:34:15 +0000783/* Macro versions of the above; just shorter to type. */
784#define uInstr0 VG_(new_UInstr0)
785#define uInstr1 VG_(new_UInstr1)
786#define uInstr2 VG_(new_UInstr2)
787#define uInstr3 VG_(new_UInstr3)
788#define uLiteral VG_(set_lit_field)
789#define uCCall VG_(set_ccall_fields)
njn810086f2002-11-14 12:42:47 +0000790#define uCond VG_(set_cond_field)
791#define uFlagsRWU VG_(set_flag_fields)
njnac6c1762002-10-04 14:34:15 +0000792#define newTemp VG_(get_new_temp)
793#define newShadow VG_(get_new_shadow)
794
njn25e49d8e72002-09-23 09:36:25 +0000795/* Refer to `the last instruction stuffed in' (can be lvalue). */
796#define LAST_UINSTR(cb) (cb)->instrs[(cb)->used-1]
797
798
799/* ------------------------------------------------------------------ */
800/* Higher-level UInstr sequence builders */
njn4ba5a792002-09-30 10:23:54 +0000801extern void VG_(call_helper_0_0) ( UCodeBlock* cb, Addr f);
802extern void VG_(call_helper_1_0) ( UCodeBlock* cb, Addr f, UInt arg1,
803 UInt regparms_n);
804extern void VG_(call_helper_2_0) ( UCodeBlock* cb, Addr f, UInt arg1, UInt arg2,
805 UInt regparms_n);
njn25e49d8e72002-09-23 09:36:25 +0000806
807/* One way around the 3-arg C function limit is to pass args via global
808 * variables... ugly, but it works. */
njn4ba5a792002-09-30 10:23:54 +0000809extern void VG_(set_global_var) ( UCodeBlock* cb, Addr globvar_ptr, UInt val);
njn25e49d8e72002-09-23 09:36:25 +0000810
811/* ------------------------------------------------------------------ */
njnd5bb0a52002-09-27 10:24:48 +0000812/* Allocating/freeing basic blocks of UCode */
njn810086f2002-11-14 12:42:47 +0000813extern UCodeBlock* VG_(setup_UCodeBlock) ( UCodeBlock* cb );
njn4ba5a792002-09-30 10:23:54 +0000814extern void VG_(free_UCodeBlock) ( UCodeBlock* cb );
njnd5bb0a52002-09-27 10:24:48 +0000815
816/* ------------------------------------------------------------------ */
817/* UCode pretty/ugly printing. Probably only useful to call from a skin
njn25e49d8e72002-09-23 09:36:25 +0000818 if VG_(needs).extended_UCode == True. */
819
820/* When True, all generated code is/should be printed. */
821extern Bool VG_(print_codegen);
822
njn4ba5a792002-09-30 10:23:54 +0000823/* Pretty/ugly printing functions */
824extern void VG_(pp_UCodeBlock) ( UCodeBlock* cb, Char* title );
825extern void VG_(pp_UInstr) ( Int instrNo, UInstr* u );
826extern void VG_(pp_UInstr_regs) ( Int instrNo, UInstr* u );
827extern void VG_(up_UInstr) ( Int instrNo, UInstr* u );
828extern Char* VG_(name_UOpcode) ( Bool upper, Opcode opc );
njn563f96f2003-02-03 11:17:46 +0000829extern Char* VG_(name_UCondcode) ( Condcode cond );
njn4ba5a792002-09-30 10:23:54 +0000830extern void VG_(pp_UOperand) ( UInstr* u, Int operandNo,
831 Int sz, Bool parens );
njn25e49d8e72002-09-23 09:36:25 +0000832
njnb93d1782003-02-03 12:03:22 +0000833/* ------------------------------------------------------------------ */
834/* Accessing shadow archregs */
835extern UInt VG_(get_shadow_archreg) ( UInt archreg );
836extern void VG_(set_shadow_archreg) ( UInt archreg, UInt val );
837extern Addr VG_(shadow_archreg_address) ( UInt archreg );
njn25e49d8e72002-09-23 09:36:25 +0000838
839/*====================================================================*/
njnd5bb0a52002-09-27 10:24:48 +0000840/*=== Generating x86 code from UCode ===*/
njn25e49d8e72002-09-23 09:36:25 +0000841/*====================================================================*/
842
njnd5bb0a52002-09-27 10:24:48 +0000843/* All this only necessary for skins with VG_(needs).extends_UCode == True. */
njn25e49d8e72002-09-23 09:36:25 +0000844
sewardje1042472002-09-30 12:33:11 +0000845/* This is the Intel register encoding -- integer regs. */
njn25e49d8e72002-09-23 09:36:25 +0000846#define R_EAX 0
847#define R_ECX 1
848#define R_EDX 2
849#define R_EBX 3
850#define R_ESP 4
851#define R_EBP 5
852#define R_ESI 6
853#define R_EDI 7
854
855#define R_AL (0+R_EAX)
856#define R_CL (0+R_ECX)
857#define R_DL (0+R_EDX)
858#define R_BL (0+R_EBX)
859#define R_AH (4+R_EAX)
860#define R_CH (4+R_ECX)
861#define R_DH (4+R_EDX)
862#define R_BH (4+R_EBX)
863
sewardje1042472002-09-30 12:33:11 +0000864/* This is the Intel register encoding -- segment regs. */
865#define R_ES 0
866#define R_CS 1
867#define R_SS 2
868#define R_DS 3
869#define R_FS 4
870#define R_GS 5
871
njn25e49d8e72002-09-23 09:36:25 +0000872/* For pretty printing x86 code */
sewardje1042472002-09-30 12:33:11 +0000873extern Char* VG_(name_of_seg_reg) ( Int sreg );
njn4ba5a792002-09-30 10:23:54 +0000874extern Char* VG_(name_of_int_reg) ( Int size, Int reg );
875extern Char VG_(name_of_int_size) ( Int size );
njn25e49d8e72002-09-23 09:36:25 +0000876
njnac6c1762002-10-04 14:34:15 +0000877/* Shorter macros for convenience */
878#define nameIReg VG_(name_of_int_reg)
879#define nameISize VG_(name_of_int_size)
880#define nameSReg VG_(name_of_seg_reg)
881
njn25e49d8e72002-09-23 09:36:25 +0000882/* Randomly useful things */
883extern UInt VG_(extend_s_8to32) ( UInt x );
884
885/* Code emitters */
njn4ba5a792002-09-30 10:23:54 +0000886extern void VG_(emitB) ( UInt b );
887extern void VG_(emitW) ( UInt w );
888extern void VG_(emitL) ( UInt l );
sewardjfa492d42002-12-08 18:20:01 +0000889extern void VG_(new_emit) ( Bool upd_cc, FlagSet uses_flags, FlagSet sets_flags );
njn25e49d8e72002-09-23 09:36:25 +0000890
891/* Finding offsets */
njn4ba5a792002-09-30 10:23:54 +0000892extern Int VG_(helper_offset) ( Addr a );
893extern Int VG_(shadow_reg_offset) ( Int arch );
894extern Int VG_(shadow_flags_offset) ( void );
njn25e49d8e72002-09-23 09:36:25 +0000895
njnd5bb0a52002-09-27 10:24:48 +0000896/* Convert reg ranks <-> Intel register ordering, for using register
897 liveness information. */
njn4ba5a792002-09-30 10:23:54 +0000898extern Int VG_(realreg_to_rank) ( Int realreg );
899extern Int VG_(rank_to_realreg) ( Int rank );
njn25e49d8e72002-09-23 09:36:25 +0000900
njnd5bb0a52002-09-27 10:24:48 +0000901/* Call a subroutine. Does no argument passing, stack manipulations, etc. */
sewardjfa492d42002-12-08 18:20:01 +0000902extern void VG_(synth_call) ( Bool ensure_shortform, Int word_offset,
903 Bool upd_cc, FlagSet use_flags, FlagSet set_flags );
njn25e49d8e72002-09-23 09:36:25 +0000904
njnd5bb0a52002-09-27 10:24:48 +0000905/* For calling C functions -- saves caller save regs, pushes args, calls,
906 clears the stack, restores caller save regs. `fn' must be registered in
907 the baseBlock first. Acceptable tags are RealReg and Literal. Optimises
908 things, eg. by not preserving non-live caller-save registers.
njn25e49d8e72002-09-23 09:36:25 +0000909
njnd5bb0a52002-09-27 10:24:48 +0000910 WARNING: a UInstr should *not* be translated with synth_ccall() followed
911 by some other x86 assembly code; this will invalidate the results of
912 vg_realreg_liveness_analysis() and everything will fall over. */
njn4ba5a792002-09-30 10:23:54 +0000913extern void VG_(synth_ccall) ( Addr fn, Int argc, Int regparms_n, UInt argv[],
914 Tag tagv[], Int ret_reg,
915 RRegSet regs_live_before,
916 RRegSet regs_live_after );
njn25e49d8e72002-09-23 09:36:25 +0000917
918/* Addressing modes */
njn4ba5a792002-09-30 10:23:54 +0000919extern void VG_(emit_amode_offregmem_reg)( Int off, Int regmem, Int reg );
920extern void VG_(emit_amode_ereg_greg) ( Int e_reg, Int g_reg );
njn25e49d8e72002-09-23 09:36:25 +0000921
922/* v-size (4, or 2 with OSO) insn emitters */
njn4ba5a792002-09-30 10:23:54 +0000923extern void VG_(emit_movv_offregmem_reg) ( Int sz, Int off, Int areg, Int reg );
924extern void VG_(emit_movv_reg_offregmem) ( Int sz, Int reg, Int off, Int areg );
925extern void VG_(emit_movv_reg_reg) ( Int sz, Int reg1, Int reg2 );
sewardjfa492d42002-12-08 18:20:01 +0000926extern void VG_(emit_nonshiftopv_lit_reg)( Bool upd_cc, Int sz, Opcode opc, UInt lit,
njn4ba5a792002-09-30 10:23:54 +0000927 Int reg );
sewardjfa492d42002-12-08 18:20:01 +0000928extern void VG_(emit_shiftopv_lit_reg) ( Bool upd_cc, Int sz, Opcode opc, UInt lit,
njn4ba5a792002-09-30 10:23:54 +0000929 Int reg );
sewardjfa492d42002-12-08 18:20:01 +0000930extern void VG_(emit_nonshiftopv_reg_reg)( Bool upd_cc, Int sz, Opcode opc,
njn4ba5a792002-09-30 10:23:54 +0000931 Int reg1, Int reg2 );
932extern void VG_(emit_movv_lit_reg) ( Int sz, UInt lit, Int reg );
sewardjfa492d42002-12-08 18:20:01 +0000933extern void VG_(emit_unaryopv_reg) ( Bool upd_cc, Int sz, Opcode opc, Int reg );
njn4ba5a792002-09-30 10:23:54 +0000934extern void VG_(emit_pushv_reg) ( Int sz, Int reg );
935extern void VG_(emit_popv_reg) ( Int sz, Int reg );
njn25e49d8e72002-09-23 09:36:25 +0000936
njn4ba5a792002-09-30 10:23:54 +0000937extern void VG_(emit_pushl_lit32) ( UInt int32 );
938extern void VG_(emit_pushl_lit8) ( Int lit8 );
sewardjfa492d42002-12-08 18:20:01 +0000939extern void VG_(emit_cmpl_zero_reg) ( Bool upd_cc, Int reg );
njn4ba5a792002-09-30 10:23:54 +0000940extern void VG_(emit_swapl_reg_EAX) ( Int reg );
941extern void VG_(emit_movv_lit_offregmem) ( Int sz, UInt lit, Int off,
942 Int memreg );
njn25e49d8e72002-09-23 09:36:25 +0000943
944/* b-size (1 byte) instruction emitters */
njn4ba5a792002-09-30 10:23:54 +0000945extern void VG_(emit_movb_lit_offregmem) ( UInt lit, Int off, Int memreg );
946extern void VG_(emit_movb_reg_offregmem) ( Int reg, Int off, Int areg );
sewardjfa492d42002-12-08 18:20:01 +0000947extern void VG_(emit_unaryopb_reg) ( Bool upd_cc, Opcode opc, Int reg );
948extern void VG_(emit_testb_lit_reg) ( Bool upd_cc, UInt lit, Int reg );
njn25e49d8e72002-09-23 09:36:25 +0000949
950/* zero-extended load emitters */
njn4ba5a792002-09-30 10:23:54 +0000951extern void VG_(emit_movzbl_offregmem_reg) ( Int off, Int regmem, Int reg );
952extern void VG_(emit_movzwl_offregmem_reg) ( Int off, Int areg, Int reg );
njn25e49d8e72002-09-23 09:36:25 +0000953
954/* misc instruction emitters */
njn4ba5a792002-09-30 10:23:54 +0000955extern void VG_(emit_call_reg) ( Int reg );
956extern void VG_(emit_add_lit_to_esp) ( Int lit );
njn4ba5a792002-09-30 10:23:54 +0000957extern void VG_(emit_pushal) ( void );
958extern void VG_(emit_popal) ( void );
959extern void VG_(emit_AMD_prefetch_reg) ( Int reg );
njn25e49d8e72002-09-23 09:36:25 +0000960
sewardja2113f92002-12-12 23:42:48 +0000961/* jump emitters */
962extern void VG_(init_target) ( Int *tgt );
963
964extern void VG_(target_back) ( Int *tgt );
965extern void VG_(target_forward) ( Int *tgt );
966extern void VG_(emit_target_delta) ( Int *tgt );
967
968extern void VG_(emit_jcondshort_delta) ( Bool simd_cc, Condcode cond, Int delta );
969extern void VG_(emit_jcondshort_target)( Bool simd_cc, Condcode cond, Int *tgt );
970
njn25e49d8e72002-09-23 09:36:25 +0000971
972/*====================================================================*/
973/*=== Execution contexts ===*/
974/*====================================================================*/
975
976/* Generic resolution type used in a few different ways, such as deciding
977 how closely to compare two errors for equality. */
978typedef
979 enum { Vg_LowRes, Vg_MedRes, Vg_HighRes }
980 VgRes;
981
982typedef
983 struct _ExeContext
984 ExeContext;
985
njnd5bb0a52002-09-27 10:24:48 +0000986/* Compare two ExeContexts. Number of callers considered depends on `res':
987 Vg_LowRes: 2
988 Vg_MedRes: 4
989 Vg_HighRes: all */
njn25e49d8e72002-09-23 09:36:25 +0000990extern Bool VG_(eq_ExeContext) ( VgRes res,
991 ExeContext* e1, ExeContext* e2 );
992
993/* Print an ExeContext. */
994extern void VG_(pp_ExeContext) ( ExeContext* );
995
996/* Take a snapshot of the client's stack. Search our collection of
997 ExeContexts to see if we already have it, and if not, allocate a
998 new one. Either way, return a pointer to the context. */
999extern ExeContext* VG_(get_ExeContext) ( ThreadState *tst );
1000
sewardj499e3de2002-11-13 22:22:25 +00001001/* Just grab the client's EIP, as a much smaller and cheaper
1002 indication of where they are. */
1003extern Addr VG_(get_EIP)( ThreadState *tst );
njn25e49d8e72002-09-23 09:36:25 +00001004
1005/*====================================================================*/
1006/*=== Error reporting ===*/
1007/*====================================================================*/
1008
1009/* ------------------------------------------------------------------ */
1010/* Suppressions describe errors which we want to suppress, ie, not
1011 show the user, usually because it is caused by a problem in a library
1012 which we can't fix, replace or work around. Suppressions are read from
njnd5bb0a52002-09-27 10:24:48 +00001013 a file at startup time. This gives flexibility so that new
njn25e49d8e72002-09-23 09:36:25 +00001014 suppressions can be added to the file as and when needed.
1015*/
1016
1017typedef
1018 Int /* Do not make this unsigned! */
1019 SuppKind;
1020
njn810086f2002-11-14 12:42:47 +00001021/* The skin-relevant parts of a suppression are:
1022 kind: what kind of suppression; must be in the range (0..)
1023 string: use is optional. NULL by default.
1024 extra: use is optional. NULL by default. void* so it's extensible.
njn25e49d8e72002-09-23 09:36:25 +00001025*/
1026typedef
njn810086f2002-11-14 12:42:47 +00001027 struct _Supp
1028 Supp;
1029
1030/* Useful in SK_(error_matches_suppression)() */
1031SuppKind VG_(get_supp_kind) ( Supp* su );
1032Char* VG_(get_supp_string) ( Supp* su );
1033void* VG_(get_supp_extra) ( Supp* su );
1034
1035/* Must be used in VG_(recognised_suppression)() */
1036void VG_(set_supp_kind) ( Supp* su, SuppKind suppkind );
1037/* May be used in VG_(read_extra_suppression_info)() */
1038void VG_(set_supp_string) ( Supp* su, Char* string );
1039void VG_(set_supp_extra) ( Supp* su, void* extra );
njn25e49d8e72002-09-23 09:36:25 +00001040
1041
1042/* ------------------------------------------------------------------ */
1043/* Error records contain enough info to generate an error report. The idea
1044 is that (typically) the same few points in the program generate thousands
njnd5bb0a52002-09-27 10:24:48 +00001045 of errors, and we don't want to spew out a fresh error message for each
1046 one. Instead, we use these structures to common up duplicates.
njn25e49d8e72002-09-23 09:36:25 +00001047*/
1048
1049typedef
1050 Int /* Do not make this unsigned! */
1051 ErrorKind;
1052
njn810086f2002-11-14 12:42:47 +00001053/* The skin-relevant parts of an Error are:
1054 kind: what kind of error; must be in the range (0..)
1055 addr: use is optional. 0 by default.
1056 string: use is optional. NULL by default.
1057 extra: use is optional. NULL by default. void* so it's extensible.
njn25e49d8e72002-09-23 09:36:25 +00001058*/
1059typedef
njn810086f2002-11-14 12:42:47 +00001060 struct _Error
1061 Error;
njn25e49d8e72002-09-23 09:36:25 +00001062
njn810086f2002-11-14 12:42:47 +00001063/* Useful in SK_(error_matches_suppression)(), SK_(pp_SkinError)(), etc */
njnb877d492003-01-28 20:40:57 +00001064ExeContext* VG_(get_error_where) ( Error* err );
1065SuppKind VG_(get_error_kind) ( Error* err );
1066Addr VG_(get_error_address) ( Error* err );
1067Char* VG_(get_error_string) ( Error* err );
1068void* VG_(get_error_extra) ( Error* err );
njn25e49d8e72002-09-23 09:36:25 +00001069
njnd5bb0a52002-09-27 10:24:48 +00001070/* Call this when an error occurs. It will be recorded if it hasn't been
njn25e49d8e72002-09-23 09:36:25 +00001071 seen before. If it has, the existing error record will have its count
1072 incremented.
1073
1074 If the error occurs in generated code, 'tst' should be NULL. If the
1075 error occurs in non-generated code, 'tst' should be non-NULL. The
1076 `extra' field can be stack-allocated; it will be copied (using
1077 SKN_(dup_extra_and_update)()) if needed. But it won't be copied
1078 if it's NULL.
njnd5bb0a52002-09-27 10:24:48 +00001079
1080 If no 'a', 's' or 'extra' of interest needs to be recorded, just use
1081 NULL for them.
njn25e49d8e72002-09-23 09:36:25 +00001082*/
1083extern void VG_(maybe_record_error) ( ThreadState* tst, ErrorKind ekind,
1084 Addr a, Char* s, void* extra );
1085
1086/* Gets a non-blank, non-comment line of at most nBuf chars from fd.
1087 Skips leading spaces on the line. Returns True if EOF was hit instead.
1088 Useful for reading in extra skin-specific suppression lines.
1089*/
njn4ba5a792002-09-30 10:23:54 +00001090extern Bool VG_(get_line) ( Int fd, Char* buf, Int nBuf );
njn25e49d8e72002-09-23 09:36:25 +00001091
1092
1093/*====================================================================*/
1094/*=== Obtaining debug information ===*/
1095/*====================================================================*/
1096
sewardj6e008cb2002-12-15 13:11:39 +00001097/* Get the file/function/line number of the instruction at address
1098 'a'. For these four, if debug info for the address is found, it
1099 copies the info into the buffer/UInt and returns True. If not, it
1100 returns False and nothing is copied. VG_(get_fnname) always
1101 demangles C++ function names. VG_(get_fnname_w_offset) is the
1102 same, except it appends "+N" to symbol names to indicate offsets.
njn25e49d8e72002-09-23 09:36:25 +00001103*/
1104extern Bool VG_(get_filename) ( Addr a, Char* filename, Int n_filename );
1105extern Bool VG_(get_fnname) ( Addr a, Char* fnname, Int n_fnname );
1106extern Bool VG_(get_linenum) ( Addr a, UInt* linenum );
sewardj6e008cb2002-12-15 13:11:39 +00001107extern Bool VG_(get_fnname_w_offset)
1108 ( Addr a, Char* fnname, Int n_fnname );
njn25e49d8e72002-09-23 09:36:25 +00001109
1110/* This one is more efficient if getting both filename and line number,
1111 because the two lookups are done together. */
1112extern Bool VG_(get_filename_linenum)
1113 ( Addr a, Char* filename, Int n_filename,
1114 UInt* linenum );
1115
1116/* Succeeds only if we find from debug info that 'a' is the address of the
1117 first instruction in a function -- as opposed to VG_(get_fnname) which
1118 succeeds if we find from debug info that 'a' is the address of any
1119 instruction in a function. Use this to instrument the start of
njnd5bb0a52002-09-27 10:24:48 +00001120 a particular function. Nb: if an executable/shared object is stripped
njn25e49d8e72002-09-23 09:36:25 +00001121 of its symbols, this function will not be able to recognise function
1122 entry points within it. */
1123extern Bool VG_(get_fnname_if_entry) ( Addr a, Char* filename, Int n_filename );
1124
1125/* Succeeds if the address is within a shared object or the main executable.
1126 It doesn't matter if debug info is present or not. */
1127extern Bool VG_(get_objname) ( Addr a, Char* objname, Int n_objname );
1128
sewardj47104382002-10-20 18:35:48 +00001129/* A way to get information about what segments are mapped */
1130typedef struct _SegInfo SegInfo;
1131
njnb877d492003-01-28 20:40:57 +00001132/* Returns NULL if the SegInfo isn't found. It doesn't matter if debug info
1133 is present or not. */
1134extern SegInfo* VG_(get_obj) ( Addr a );
1135
1136extern const SegInfo* VG_(next_seginfo) ( const SegInfo *seg );
1137extern Addr VG_(seg_start) ( const SegInfo *seg );
1138extern UInt VG_(seg_size) ( const SegInfo *seg );
1139extern const UChar* VG_(seg_filename) ( const SegInfo *seg );
1140extern UInt VG_(seg_sym_offset)( const SegInfo *seg );
sewardj47104382002-10-20 18:35:48 +00001141
1142typedef
1143 enum {
1144 Vg_SectUnknown,
1145 Vg_SectText,
sewardj8fe15a32002-10-20 19:29:21 +00001146 Vg_SectData,
1147 Vg_SectBSS,
sewardj47104382002-10-20 18:35:48 +00001148 Vg_SectGOT,
sewardj8fe15a32002-10-20 19:29:21 +00001149 Vg_SectPLT,
sewardj47104382002-10-20 18:35:48 +00001150 }
1151 VgSectKind;
1152
1153extern VgSectKind VG_(seg_sect_kind)(Addr);
1154
njn25e49d8e72002-09-23 09:36:25 +00001155
1156/*====================================================================*/
1157/*=== Shadow chunks and block-finding ===*/
1158/*====================================================================*/
1159
njn810086f2002-11-14 12:42:47 +00001160/* The skin-relevant parts of a ShadowChunk are:
1161 size: size of the block in bytes
1162 addr: addr of the block
1163 extra: anything extra kept by the skin; size is determined by
1164 VG_(needs).sizeof_shadow_chunk
1165*/
njn25e49d8e72002-09-23 09:36:25 +00001166typedef
njn810086f2002-11-14 12:42:47 +00001167 struct _ShadowChunk
njn25e49d8e72002-09-23 09:36:25 +00001168 ShadowChunk;
1169
njn810086f2002-11-14 12:42:47 +00001170extern UInt VG_(get_sc_size) ( ShadowChunk* sc );
1171extern Addr VG_(get_sc_data) ( ShadowChunk* sc );
1172/* Gets the ith word of the `extra' field. */
1173extern UInt VG_(get_sc_extra) ( ShadowChunk* sc, UInt i );
1174/* Sets the ith word of the `extra' field to `word'. */
1175extern void VG_(set_sc_extra) ( ShadowChunk* sc, UInt i, UInt word );
1176
1177/* These two should only be used if the `alternative_free' need is set, once
1178 we reach the point where the block would have been free'd. */
1179extern ShadowChunk* VG_(get_sc_next) ( ShadowChunk* sc );
1180extern void VG_(set_sc_next) ( ShadowChunk* sc, ShadowChunk* next );
1181
1182
njn25e49d8e72002-09-23 09:36:25 +00001183/* Use this to free blocks if VG_(needs).alternative_free == True.
1184 It frees the ShadowChunk and the malloc'd block it points to. */
njn4ba5a792002-09-30 10:23:54 +00001185extern void VG_(free_ShadowChunk) ( ShadowChunk* sc );
njn25e49d8e72002-09-23 09:36:25 +00001186
1187/* Makes an array of pointers to all the shadow chunks of malloc'd blocks */
1188extern ShadowChunk** VG_(get_malloc_shadows) ( /*OUT*/ UInt* n_shadows );
1189
1190/* Determines if address 'a' is within the bounds of the block at start.
1191 Allows a little 'slop' round the edges. */
1192extern Bool VG_(addr_is_in_block) ( Addr a, Addr start, UInt size );
1193
1194/* Searches through currently malloc'd blocks until a matching one is found.
1195 Returns NULL if none match. Extra arguments can be implicitly passed to
njnd5bb0a52002-09-27 10:24:48 +00001196 p using nested functions; see memcheck/mc_errcontext.c for an example. */
njn25e49d8e72002-09-23 09:36:25 +00001197extern ShadowChunk* VG_(any_matching_mallocd_ShadowChunks)
1198 ( Bool (*p) ( ShadowChunk* ));
1199
1200/* Searches through all thread's stacks to see if any match. Returns
1201 * VG_INVALID_THREADID if none match. */
1202extern ThreadId VG_(any_matching_thread_stack)
1203 ( Bool (*p) ( Addr stack_min, Addr stack_max ));
1204
sewardja4495682002-10-21 07:29:59 +00001205/* Do memory leak detection. */
1206extern void VG_(generic_detect_memory_leaks) (
1207 Bool is_valid_64k_chunk ( UInt ),
1208 Bool is_valid_address ( Addr ),
1209 ExeContext* get_where ( ShadowChunk* ),
1210 VgRes leak_resolution,
sewardj99aac972002-12-26 01:53:45 +00001211 Bool show_reachable,
1212 UInt /*CoreErrorKind*/ leakSupp
sewardja4495682002-10-21 07:29:59 +00001213 );
1214
1215
njn25e49d8e72002-09-23 09:36:25 +00001216/*====================================================================*/
1217/*=== Skin-specific stuff ===*/
1218/*====================================================================*/
1219
njnd04b7c62002-10-03 14:05:52 +00001220/* ------------------------------------------------------------------ */
1221/* Details */
njnd04b7c62002-10-03 14:05:52 +00001222
njn810086f2002-11-14 12:42:47 +00001223/* Information used in the startup message. `name' also determines the
1224 string used for identifying suppressions in a suppression file as
1225 belonging to this skin. `version' can be NULL, in which case (not
1226 surprisingly) no version info is printed; this mechanism is designed for
1227 skins distributed with Valgrind that share a version number with
1228 Valgrind. Other skins not distributed as part of Valgrind should
sewardjc0d8f682002-11-30 00:49:43 +00001229 probably have their own version number. */
1230extern void VG_(details_name) ( Char* name );
1231extern void VG_(details_version) ( Char* version );
1232extern void VG_(details_description) ( Char* description );
1233extern void VG_(details_copyright_author) ( Char* copyright_author );
1234
1235/* Average size of a translation, in bytes, so that the translation
1236 storage machinery can allocate memory appropriately. Not critical.
1237 If you're unsure, set to 100 (indicating typical code expansion of
1238 about 6:1). */
1239extern void VG_(details_avg_translation_sizeB) ( Int );
njnd04b7c62002-10-03 14:05:52 +00001240
njn810086f2002-11-14 12:42:47 +00001241/* String printed if an `sk_assert' assertion fails or VG_(skin_panic)
1242 is called. Should probably be an email address. */
1243extern void VG_(details_bug_reports_to) ( Char* bug_reports_to );
njnd04b7c62002-10-03 14:05:52 +00001244
1245/* ------------------------------------------------------------------ */
1246/* Needs */
1247
njn810086f2002-11-14 12:42:47 +00001248/* Booleans that decide core behaviour, but don't require extra
1249 operations to be defined if `True' */
njnd5bb0a52002-09-27 10:24:48 +00001250
njn810086f2002-11-14 12:42:47 +00001251/* Should __libc_freeres() be run? Bugs in it can crash the skin. */
1252extern void VG_(needs_libc_freeres) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001253
njn810086f2002-11-14 12:42:47 +00001254/* Want to have errors detected by Valgrind's core reported? Includes:
1255 - pthread API errors (many; eg. unlocking a non-locked mutex)
1256 - silly arguments to malloc() et al (eg. negative size)
1257 - invalid file descriptors to blocking syscalls read() and write()
1258 - bad signal numbers passed to sigaction()
1259 - attempt to install signal handler for SIGKILL or SIGSTOP */
1260extern void VG_(needs_core_errors) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001261
njn810086f2002-11-14 12:42:47 +00001262/* Booleans that indicate extra operations are defined; if these are True,
1263 the corresponding template functions (given below) must be defined. A
1264 lot like being a member of a type class. */
njn25e49d8e72002-09-23 09:36:25 +00001265
njn810086f2002-11-14 12:42:47 +00001266/* Want to report errors from skin? This implies use of suppressions, too. */
1267extern void VG_(needs_skin_errors) ( void );
njnd5bb0a52002-09-27 10:24:48 +00001268
njn810086f2002-11-14 12:42:47 +00001269/* Is information kept about specific individual basic blocks? (Eg. for
1270 cachegrind there are cost-centres for every instruction, stored at a
1271 basic block level.) If so, it sometimes has to be discarded, because
1272 .so mmap/munmap-ping or self-modifying code (informed by the
1273 DISCARD_TRANSLATIONS user request) can cause one instruction address
1274 to be used for more than one instruction in one program run... */
1275extern void VG_(needs_basic_block_discards) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001276
njn810086f2002-11-14 12:42:47 +00001277/* Skin maintains information about each register? */
1278extern void VG_(needs_shadow_regs) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001279
njn810086f2002-11-14 12:42:47 +00001280/* Skin defines its own command line options? */
1281extern void VG_(needs_command_line_options) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001282
njn810086f2002-11-14 12:42:47 +00001283/* Skin defines its own client requests? */
1284extern void VG_(needs_client_requests) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001285
njn810086f2002-11-14 12:42:47 +00001286/* Skin defines its own UInstrs? */
1287extern void VG_(needs_extended_UCode) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001288
njn810086f2002-11-14 12:42:47 +00001289/* Skin does stuff before and/or after system calls? */
1290extern void VG_(needs_syscall_wrapper) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001291
njn810086f2002-11-14 12:42:47 +00001292/* Size, in words, of extra info about malloc'd blocks recorded by
1293 skin. Be careful to get this right or you'll get seg faults! */
1294extern void VG_(needs_sizeof_shadow_block) ( Int size );
njn25e49d8e72002-09-23 09:36:25 +00001295
njn810086f2002-11-14 12:42:47 +00001296/* Skin does free()s itself? Useful if a skin needs to keep track of
1297 blocks in some way after they're free'd.
1298 WARNING: don't forget to call VG_(free_ShadowChunk)() for each block
1299 eventually! */
1300extern void VG_(needs_alternative_free) ( void );
sewardj8fe15a32002-10-20 19:29:21 +00001301
njn810086f2002-11-14 12:42:47 +00001302/* Are skin-state sanity checks performed? */
1303extern void VG_(needs_sanity_checks) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001304
njn810086f2002-11-14 12:42:47 +00001305/* Do we need to see data symbols? */
1306extern void VG_(needs_data_syms) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001307
1308/* ------------------------------------------------------------------ */
1309/* Core events to track */
1310
1311/* Part of the core from which this call was made. Useful for determining
njnd5bb0a52002-09-27 10:24:48 +00001312 what kind of error message should be emitted. */
njn25e49d8e72002-09-23 09:36:25 +00001313typedef
1314 enum { Vg_CorePThread, Vg_CoreSignal, Vg_CoreSysCall, Vg_CoreTranslate }
1315 CorePart;
1316
njn810086f2002-11-14 12:42:47 +00001317#define EV extern void
njn25e49d8e72002-09-23 09:36:25 +00001318
njn810086f2002-11-14 12:42:47 +00001319/* Events happening in core to track. To be notified, pass a callback
1320 function to the appropriate function. To ignore an event, don't do
1321 anything (default is for events to be ignored). */
1322
1323/* Memory events */
1324
1325EV VG_(track_new_mem_startup) ( void (*f)(Addr a, UInt len,
1326 Bool rr, Bool ww, Bool xx) );
1327EV VG_(track_new_mem_heap) ( void (*f)(Addr a, UInt len, Bool is_inited) );
1328EV VG_(track_new_mem_stack) ( void (*f)(Addr a, UInt len) );
1329EV VG_(track_new_mem_stack_aligned) ( void (*f)(Addr a, UInt len) );
1330EV VG_(track_new_mem_stack_signal) ( void (*f)(Addr a, UInt len) );
1331EV VG_(track_new_mem_brk) ( void (*f)(Addr a, UInt len) );
1332EV VG_(track_new_mem_mmap) ( void (*f)(Addr a, UInt len,
1333 Bool rr, Bool ww, Bool xx) );
1334
1335EV VG_(track_copy_mem_heap) ( void (*f)(Addr from, Addr to, UInt len) );
1336EV VG_(track_copy_mem_remap) ( void (*f)(Addr from, Addr to, UInt len) );
1337EV VG_(track_change_mem_mprotect) ( void (*f)(Addr a, UInt len,
1338 Bool rr, Bool ww, Bool xx) );
njn25e49d8e72002-09-23 09:36:25 +00001339
njn810086f2002-11-14 12:42:47 +00001340/* Used on redzones around malloc'd blocks and at end of stack */
1341EV VG_(track_ban_mem_heap) ( void (*f)(Addr a, UInt len) );
1342EV VG_(track_ban_mem_stack) ( void (*f)(Addr a, UInt len) );
njn25e49d8e72002-09-23 09:36:25 +00001343
njn810086f2002-11-14 12:42:47 +00001344EV VG_(track_die_mem_heap) ( void (*f)(Addr a, UInt len) );
1345EV VG_(track_die_mem_stack) ( void (*f)(Addr a, UInt len) );
1346EV VG_(track_die_mem_stack_aligned) ( void (*f)(Addr a, UInt len) );
1347EV VG_(track_die_mem_stack_signal) ( void (*f)(Addr a, UInt len) );
1348EV VG_(track_die_mem_brk) ( void (*f)(Addr a, UInt len) );
1349EV VG_(track_die_mem_munmap) ( void (*f)(Addr a, UInt len) );
njn25e49d8e72002-09-23 09:36:25 +00001350
njn810086f2002-11-14 12:42:47 +00001351EV VG_(track_bad_free) ( void (*f)(ThreadState* tst, Addr a) );
1352EV VG_(track_mismatched_free) ( void (*f)(ThreadState* tst, Addr a) );
njn25e49d8e72002-09-23 09:36:25 +00001353
njn810086f2002-11-14 12:42:47 +00001354EV VG_(track_pre_mem_read) ( void (*f)(CorePart part, ThreadState* tst,
1355 Char* s, Addr a, UInt size) );
1356EV VG_(track_pre_mem_read_asciiz) ( void (*f)(CorePart part, ThreadState* tst,
1357 Char* s, Addr a) );
1358EV VG_(track_pre_mem_write) ( void (*f)(CorePart part, ThreadState* tst,
1359 Char* s, Addr a, UInt size) );
1360/* Not implemented yet -- have to add in lots of places, which is a
1361 pain. Won't bother unless/until there's a need. */
1362/* EV VG_(track_post_mem_read) ( void (*f)(ThreadState* tst, Char* s,
1363 Addr a, UInt size) ); */
1364EV VG_(track_post_mem_write) ( void (*f)(Addr a, UInt size) );
njn25e49d8e72002-09-23 09:36:25 +00001365
1366
njn810086f2002-11-14 12:42:47 +00001367/* Scheduler events (not exhaustive) */
njn25e49d8e72002-09-23 09:36:25 +00001368
njn810086f2002-11-14 12:42:47 +00001369EV VG_(track_thread_run) ( void (*f)(ThreadId tid) );
njn25e49d8e72002-09-23 09:36:25 +00001370
njn810086f2002-11-14 12:42:47 +00001371/* Thread events (not exhaustive) */
sewardjdca84112002-11-13 22:29:34 +00001372
njn810086f2002-11-14 12:42:47 +00001373/* Called during thread create, before the new thread has run any
1374 instructions (or touched any memory). */
1375EV VG_(track_post_thread_create)( void (*f)(ThreadId tid, ThreadId child) );
1376/* Called once the joinee thread is terminated and the joining thread is
1377 about to resume. */
1378EV VG_(track_post_thread_join) ( void (*f)(ThreadId joiner, ThreadId joinee) );
njn25e49d8e72002-09-23 09:36:25 +00001379
njnd5bb0a52002-09-27 10:24:48 +00001380
njn810086f2002-11-14 12:42:47 +00001381/* Mutex events (not exhaustive) */
njn25e49d8e72002-09-23 09:36:25 +00001382
njn810086f2002-11-14 12:42:47 +00001383/* Called before a thread can block while waiting for a mutex (called
1384 regardless of whether the thread will block or not). */
1385EV VG_(track_pre_mutex_lock) ( void (*f)(ThreadId tid,
1386 void* /*pthread_mutex_t* */ mutex) );
1387/* Called once the thread actually holds the mutex (always paired with
1388 pre_mutex_lock). */
1389EV VG_(track_post_mutex_lock) ( void (*f)(ThreadId tid,
1390 void* /*pthread_mutex_t* */ mutex) );
1391/* Called after a thread has released a mutex (no need for a corresponding
1392 pre_mutex_unlock, because unlocking can't block). */
1393EV VG_(track_post_mutex_unlock) ( void (*f)(ThreadId tid,
1394 void* /*pthread_mutex_t* */ mutex) );
njn25e49d8e72002-09-23 09:36:25 +00001395
njn810086f2002-11-14 12:42:47 +00001396/* Others... condition variable, signal events... */
1397/* ... */
1398
1399#undef EV
njn25e49d8e72002-09-23 09:36:25 +00001400
1401/* ------------------------------------------------------------------ */
1402/* Template functions */
1403
1404/* These are the parameterised functions in the core. The default definitions
njnd5bb0a52002-09-27 10:24:48 +00001405 are overridden by LD_PRELOADed skin version. At the very least, a skin
1406 must define the fundamental template functions. Depending on what needs
1407 are set, extra template functions will be used too. Functions are
1408 grouped under the needs that govern their use. */
njn25e49d8e72002-09-23 09:36:25 +00001409
1410
1411/* ------------------------------------------------------------------ */
1412/* Fundamental template functions */
1413
1414/* Initialise skin. Must do the following:
njn810086f2002-11-14 12:42:47 +00001415 - initialise the `details' struct, via the VG_(details_*)() functions
njn25e49d8e72002-09-23 09:36:25 +00001416 - register any helpers called by generated code
1417
1418 May do the following:
njn810086f2002-11-14 12:42:47 +00001419 - initialise the `needs' struct to indicate certain requirements, via
1420 the VG_(needs_*)() functions
1421 - initialise the `track' struct to indicate core events of interest, via
1422 the VG_(track_*)() functions
njn25e49d8e72002-09-23 09:36:25 +00001423 - register any skin-specific profiling events
1424 - any other skin-specific initialisation
1425*/
njn810086f2002-11-14 12:42:47 +00001426extern void SK_(pre_clo_init) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001427
njnd5bb0a52002-09-27 10:24:48 +00001428/* Do initialisation that can only be done after command line processing. */
njn25e49d8e72002-09-23 09:36:25 +00001429extern void SK_(post_clo_init)( void );
1430
1431/* Instrument a basic block. Must be a true function, ie. the same input
1432 always results in the same output, because basic blocks can be
1433 retranslated. Unless you're doing something really strange...
1434 'orig_addr' is the address of the first instruction in the block. */
1435extern UCodeBlock* SK_(instrument) ( UCodeBlock* cb, Addr orig_addr );
1436
1437/* Finish up, print out any results, etc. */
1438extern void SK_(fini) ( void );
1439
1440
1441/* ------------------------------------------------------------------ */
1442/* VG_(needs).report_errors */
1443
1444/* Identify if two errors are equal, or equal enough. `res' indicates how
1445 close is "close enough". `res' should be passed on as necessary, eg. if
njn810086f2002-11-14 12:42:47 +00001446 the Error's `extra' part contains an ExeContext, `res' should be
njn25e49d8e72002-09-23 09:36:25 +00001447 passed to VG_(eq_ExeContext)() if the ExeContexts are considered. Other
1448 than that, probably don't worry about it unless you have lots of very
1449 similar errors occurring.
1450 */
njn810086f2002-11-14 12:42:47 +00001451extern Bool SK_(eq_SkinError) ( VgRes res, Error* e1, Error* e2 );
njn25e49d8e72002-09-23 09:36:25 +00001452
1453/* Print error context. The passed function pp_ExeContext() can be (and
1454 probably should be) used to print the location of the error. */
njn810086f2002-11-14 12:42:47 +00001455extern void SK_(pp_SkinError) ( Error* err, void (*pp_ExeContext)(void) );
njn25e49d8e72002-09-23 09:36:25 +00001456
njn810086f2002-11-14 12:42:47 +00001457/* Should copy the `extra' part which the core uses to override the old
1458 version. This is necessary to move from a temporary stack copy to a
1459 permanent heap one.
njn25e49d8e72002-09-23 09:36:25 +00001460
njn810086f2002-11-14 12:42:47 +00001461 Then should fill in any details that could be postponed until after the
1462 decision whether to ignore the error (ie. details not affecting the
1463 result of SK_(eq_SkinError)()). This saves time when errors are ignored.
njn25e49d8e72002-09-23 09:36:25 +00001464
1465 Yuk.
1466*/
njn810086f2002-11-14 12:42:47 +00001467extern void* SK_(dup_extra_and_update) ( Error* err );
njn25e49d8e72002-09-23 09:36:25 +00001468
njn810086f2002-11-14 12:42:47 +00001469/* Return value indicates recognition. If recognised, must set skind using
1470 VG_(set_supp_kind)(). */
1471extern Bool SK_(recognised_suppression) ( Char* name, Supp* su );
njn25e49d8e72002-09-23 09:36:25 +00001472
njn810086f2002-11-14 12:42:47 +00001473/* Read any extra info for this suppression kind. Most likely for filling
1474 in the `extra' and `string' parts (with VG_(set_supp_{extra,string})())
1475 of a suppression if necessary. Should return False if a syntax error
1476 occurred, True otherwise. */
1477extern Bool SK_(read_extra_suppression_info) ( Int fd, Char* buf, Int nBuf,
1478 Supp* su );
njn25e49d8e72002-09-23 09:36:25 +00001479
1480/* This should just check the kinds match and maybe some stuff in the
njn810086f2002-11-14 12:42:47 +00001481 `string' and `extra' field if appropriate (using VG_(get_supp_*)() to
1482 get the relevant suppression parts). */
1483extern Bool SK_(error_matches_suppression)(Error* err, Supp* su);
njn25e49d8e72002-09-23 09:36:25 +00001484
1485
1486/* ------------------------------------------------------------------ */
1487/* VG_(needs).basic_block_discards */
1488
njnd5bb0a52002-09-27 10:24:48 +00001489/* Should discard any information that pertains to specific basic blocks
1490 or instructions within the address range given. */
njn25e49d8e72002-09-23 09:36:25 +00001491extern void SK_(discard_basic_block_info) ( Addr a, UInt size );
1492
1493
1494/* ------------------------------------------------------------------ */
1495/* VG_(needs).shadow_regs */
1496
1497/* Valid values for general registers and EFLAGS register, for initialising
1498 and updating registers when written in certain places in core. */
1499extern void SK_(written_shadow_regs_values) ( UInt* gen_reg, UInt* eflags );
1500
1501
1502/* ------------------------------------------------------------------ */
1503/* VG_(needs).command_line_options */
1504
njnd5bb0a52002-09-27 10:24:48 +00001505/* Return True if option was recognised. Presumably sets some state to
1506 record the option as well. */
1507extern Bool SK_(process_cmd_line_option) ( Char* argv );
njn25e49d8e72002-09-23 09:36:25 +00001508
1509/* Print out command line usage for skin options */
1510extern Char* SK_(usage) ( void );
1511
1512
1513/* ------------------------------------------------------------------ */
1514/* VG_(needs).client_requests */
1515
sewardj34042512002-10-22 04:14:35 +00001516extern Bool SK_(handle_client_request) ( ThreadState* tst, UInt* arg_block, UInt *ret );
njn25e49d8e72002-09-23 09:36:25 +00001517
1518
1519/* ------------------------------------------------------------------ */
1520/* VG_(needs).extends_UCode */
1521
njn4ba5a792002-09-30 10:23:54 +00001522/* Useful to use in VG_(get_Xreg_usage)() */
njn810086f2002-11-14 12:42:47 +00001523#define VG_UINSTR_READS_REG(ono,regs,isWrites) \
njn25e49d8e72002-09-23 09:36:25 +00001524 { if (mycat(u->tag,ono) == tag) \
njn810086f2002-11-14 12:42:47 +00001525 { regs[n] = mycat(u->val,ono); \
1526 isWrites[n] = False; \
njn25e49d8e72002-09-23 09:36:25 +00001527 n++; \
1528 } \
1529 }
njn810086f2002-11-14 12:42:47 +00001530#define VG_UINSTR_WRITES_REG(ono,regs,isWrites) \
njnd5bb0a52002-09-27 10:24:48 +00001531 { if (mycat(u->tag,ono) == tag) \
njn810086f2002-11-14 12:42:47 +00001532 { regs[n] = mycat(u->val,ono); \
1533 isWrites[n] = True; \
njnd5bb0a52002-09-27 10:24:48 +00001534 n++; \
1535 } \
njn25e49d8e72002-09-23 09:36:25 +00001536 }
1537
njn4ba5a792002-09-30 10:23:54 +00001538/* 'X' prefix indicates eXtended UCode. */
njn810086f2002-11-14 12:42:47 +00001539extern Int SK_(get_Xreg_usage) ( UInstr* u, Tag tag, Int* regs,
1540 Bool* isWrites );
njn4ba5a792002-09-30 10:23:54 +00001541extern void SK_(emit_XUInstr) ( UInstr* u, RRegSet regs_live_before );
1542extern Bool SK_(sane_XUInstr) ( Bool beforeRA, Bool beforeLiveness,
njn25e49d8e72002-09-23 09:36:25 +00001543 UInstr* u );
njn4ba5a792002-09-30 10:23:54 +00001544extern Char* SK_(name_XUOpcode) ( Opcode opc );
1545extern void SK_(pp_XUInstr) ( UInstr* u );
njn25e49d8e72002-09-23 09:36:25 +00001546
1547
1548/* ------------------------------------------------------------------ */
1549/* VG_(needs).syscall_wrapper */
1550
1551/* If either of the pre_ functions malloc() something to return, the
1552 * corresponding post_ function had better free() it!
1553 */
1554extern void* SK_( pre_syscall) ( ThreadId tid, UInt syscallno,
1555 Bool is_blocking );
1556extern void SK_(post_syscall) ( ThreadId tid, UInt syscallno,
1557 void* pre_result, Int res,
1558 Bool is_blocking );
1559
njnd5bb0a52002-09-27 10:24:48 +00001560
njn25e49d8e72002-09-23 09:36:25 +00001561/* ------------------------------------------------------------------ */
njnd5bb0a52002-09-27 10:24:48 +00001562/* VG_(needs).sizeof_shadow_chunk (if > 0) */
njn25e49d8e72002-09-23 09:36:25 +00001563
njn810086f2002-11-14 12:42:47 +00001564/* Must fill in the `extra' part, using VG_(set_sc_extra)(). */
njn25e49d8e72002-09-23 09:36:25 +00001565extern void SK_(complete_shadow_chunk) ( ShadowChunk* sc, ThreadState* tst );
1566
1567
1568/* ------------------------------------------------------------------ */
1569/* VG_(needs).alternative_free */
1570
njn810086f2002-11-14 12:42:47 +00001571/* If this need is set, when a dynamic block would normally be free'd, this
1572 is called instead. The block is contained inside the ShadowChunk; use
1573 the VG_(get_sc_*)() functions to access it. */
njn25e49d8e72002-09-23 09:36:25 +00001574extern void SK_(alt_free) ( ShadowChunk* sc, ThreadState* tst );
1575
njnd5bb0a52002-09-27 10:24:48 +00001576
njn25e49d8e72002-09-23 09:36:25 +00001577/* ---------------------------------------------------------------------
1578 VG_(needs).sanity_checks */
1579
njnd5bb0a52002-09-27 10:24:48 +00001580/* Can be useful for ensuring a skin's correctness. SK_(cheap_sanity_check)
1581 is called very frequently; SK_(expensive_sanity_check) is called less
1582 frequently and can be more involved. */
njn25e49d8e72002-09-23 09:36:25 +00001583extern Bool SK_(cheap_sanity_check) ( void );
1584extern Bool SK_(expensive_sanity_check) ( void );
1585
1586
1587#endif /* NDEF __VG_SKIN_H */
1588
1589/*--------------------------------------------------------------------*/
1590/*--- end vg_skin.h ---*/
1591/*--------------------------------------------------------------------*/
1592