blob: fb154123c6782b6c7583fab02173885a119b0926 [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.
115
116 We don't want the variables themselves in the core, only in the skins,
117 hence the #ifndef. But the core needs to know of their existence, hence
118 the #else branch. Phew.
119
120 In summary: skins don't need to do anything, the core works it all out.
121*/
122
123#define VG_CORE_INTERFACE_MAJOR_VERSION 1
124#define VG_CORE_INTERFACE_MINOR_VERSION 1
125
126extern const Int VG_(skin_interface_major_version);
127extern const Int VG_(skin_interface_minor_version);
128
129/* Every skin must define this macro somewhere, exactly once. */
130#define VG_DETERMINE_INTERFACE_VERSION \
131const Int VG_(skin_interface_major_version) = VG_CORE_INTERFACE_MAJOR_VERSION; \
132const Int VG_(skin_interface_minor_version) = VG_CORE_INTERFACE_MINOR_VERSION;
133
134/*====================================================================*/
njn25e49d8e72002-09-23 09:36:25 +0000135/*=== Command-line options ===*/
136/*====================================================================*/
137
138/* Verbosity level: 0 = silent, 1 (default), > 1 = more verbose. */
139extern Int VG_(clo_verbosity);
140
141/* Profile? */
142extern Bool VG_(clo_profile);
143
njn25e49d8e72002-09-23 09:36:25 +0000144/* Call this if a recognised option was bad for some reason.
145 Note: don't use it just because an option was unrecognised -- return 'False'
146 from SKN_(process_cmd_line_option) to indicate that. */
147extern void VG_(bad_option) ( Char* opt );
148
149/* Client args */
150extern Int VG_(client_argc);
151extern Char** VG_(client_argv);
152
njnd5bb0a52002-09-27 10:24:48 +0000153/* Client environment. Can be inspected with VG_(getenv)() */
njn25e49d8e72002-09-23 09:36:25 +0000154extern Char** VG_(client_envp);
155
156
157/*====================================================================*/
158/*=== Printing messages for the user ===*/
159/*====================================================================*/
160
161/* Print a message prefixed by "??<pid>?? "; '?' depends on the VgMsgKind.
162 Should be used for all user output. */
163
164typedef
165 enum { Vg_UserMsg, /* '?' == '=' */
166 Vg_DebugMsg, /* '?' == '-' */
167 Vg_DebugExtraMsg /* '?' == '+' */
168 }
169 VgMsgKind;
170
171/* Functions for building a message from multiple parts. */
172extern void VG_(start_msg) ( VgMsgKind kind );
173extern void VG_(add_to_msg) ( Char* format, ... );
174/* Ends and prints the message. Appends a newline. */
175extern void VG_(end_msg) ( void );
176
njnd5bb0a52002-09-27 10:24:48 +0000177/* Send a single-part message. Appends a newline. */
njn25e49d8e72002-09-23 09:36:25 +0000178extern void VG_(message) ( VgMsgKind kind, Char* format, ... );
179
180
181/*====================================================================*/
182/*=== Profiling ===*/
183/*====================================================================*/
184
185/* Nb: VGP_(register_profile_event)() relies on VgpUnc being the first one */
186#define VGP_CORE_LIST \
187 /* These ones depend on the core */ \
188 VGP_PAIR(VgpUnc, "unclassified"), \
189 VGP_PAIR(VgpRun, "running"), \
190 VGP_PAIR(VgpSched, "scheduler"), \
191 VGP_PAIR(VgpMalloc, "low-lev malloc/free"), \
192 VGP_PAIR(VgpCliMalloc, "client malloc/free"), \
193 VGP_PAIR(VgpStack, "adjust-stack"), \
194 VGP_PAIR(VgpTranslate, "translate-main"), \
195 VGP_PAIR(VgpToUCode, "to-ucode"), \
196 VGP_PAIR(VgpFromUcode, "from-ucode"), \
197 VGP_PAIR(VgpImprove, "improve"), \
198 VGP_PAIR(VgpRegAlloc, "reg-alloc"), \
199 VGP_PAIR(VgpLiveness, "liveness-analysis"), \
200 VGP_PAIR(VgpDoLRU, "do-lru"), \
201 VGP_PAIR(VgpSlowFindT, "slow-search-transtab"), \
202 VGP_PAIR(VgpInitMem, "init-memory"), \
203 VGP_PAIR(VgpExeContext, "exe-context"), \
204 VGP_PAIR(VgpReadSyms, "read-syms"), \
205 VGP_PAIR(VgpSearchSyms, "search-syms"), \
206 VGP_PAIR(VgpAddToT, "add-to-transtab"), \
207 VGP_PAIR(VgpCoreSysWrap, "core-syscall-wrapper"), \
208 VGP_PAIR(VgpDemangle, "demangle"), \
njn37cea302002-09-30 11:24:00 +0000209 VGP_PAIR(VgpCoreCheapSanity, "core-cheap-sanity"), \
210 VGP_PAIR(VgpCoreExpensiveSanity, "core-expensive-sanity"), \
njn25e49d8e72002-09-23 09:36:25 +0000211 /* These ones depend on the skin */ \
212 VGP_PAIR(VgpPreCloInit, "pre-clo-init"), \
213 VGP_PAIR(VgpPostCloInit, "post-clo-init"), \
214 VGP_PAIR(VgpInstrument, "instrument"), \
215 VGP_PAIR(VgpSkinSysWrap, "skin-syscall-wrapper"), \
njn37cea302002-09-30 11:24:00 +0000216 VGP_PAIR(VgpSkinCheapSanity, "skin-cheap-sanity"), \
217 VGP_PAIR(VgpSkinExpensiveSanity, "skin-expensive-sanity"), \
njn25e49d8e72002-09-23 09:36:25 +0000218 VGP_PAIR(VgpFini, "fini")
219
220#define VGP_PAIR(n,name) n
221typedef enum { VGP_CORE_LIST } VgpCoreCC;
222#undef VGP_PAIR
223
224/* When registering skin profiling events, ensure that the 'n' value is in
225 * the range (VgpFini+1..) */
226extern void VGP_(register_profile_event) ( Int n, Char* name );
227
228extern void VGP_(pushcc) ( UInt cc );
229extern void VGP_(popcc) ( UInt cc );
230
231/* Define them only if they haven't already been defined by vg_profile.c */
232#ifndef VGP_PUSHCC
233# define VGP_PUSHCC(x)
234#endif
235#ifndef VGP_POPCC
236# define VGP_POPCC(x)
237#endif
238
239
240/*====================================================================*/
241/*=== Useful stuff to call from generated code ===*/
242/*====================================================================*/
243
244/* ------------------------------------------------------------------ */
245/* General stuff */
246
njn41557122002-10-14 09:25:37 +0000247/* 64-bit counter for the number of basic blocks done. */
248extern ULong VG_(bbs_done);
249
njn25e49d8e72002-09-23 09:36:25 +0000250/* Get the simulated %esp */
251extern Addr VG_(get_stack_pointer) ( void );
252
njnd5bb0a52002-09-27 10:24:48 +0000253/* Detect if an address is within Valgrind's stack or Valgrind's
254 m_state_static; useful for memory leak detectors to tell if a block
255 is used by Valgrind (and thus can be ignored). */
njn25e49d8e72002-09-23 09:36:25 +0000256extern Bool VG_(within_stack)(Addr a);
njn25e49d8e72002-09-23 09:36:25 +0000257extern Bool VG_(within_m_state_static)(Addr a);
258
259/* Check if an address is 4-byte aligned */
260#define IS_ALIGNED4_ADDR(aaa_p) (0 == (((UInt)(aaa_p)) & 3))
261
262
263/* ------------------------------------------------------------------ */
264/* Thread-related stuff */
265
266/* Special magic value for an invalid ThreadId. It corresponds to
267 LinuxThreads using zero as the initial value for
268 pthread_mutex_t.__m_owner and pthread_cond_t.__c_waiting. */
269#define VG_INVALID_THREADID ((ThreadId)(0))
270
271/* ThreadIds are simply indices into the vg_threads[] array. */
272typedef
273 UInt
274 ThreadId;
275
njnd5bb0a52002-09-27 10:24:48 +0000276/* struct _ThreadState defined elsewhere; ThreadState is abstract as its
277 definition is not important for skins. */
njn25e49d8e72002-09-23 09:36:25 +0000278typedef
279 struct _ThreadState
280 ThreadState;
281
sewardj7ab2aca2002-10-20 19:40:32 +0000282extern ThreadId VG_(get_current_tid) ( void );
sewardjb52a1b02002-10-23 21:38:22 +0000283extern ThreadId VG_(get_current_or_recent_tid) ( void );
sewardj7ab2aca2002-10-20 19:40:32 +0000284extern ThreadId VG_(get_tid_from_ThreadState) ( ThreadState* );
njn25e49d8e72002-09-23 09:36:25 +0000285extern ThreadState* VG_(get_ThreadState) ( ThreadId tid );
286
287
288/*====================================================================*/
289/*=== Valgrind's version of libc ===*/
290/*====================================================================*/
291
292/* Valgrind doesn't use libc at all, for good reasons (trust us). So here
293 are its own versions of C library functions, but with VG_ prefixes. Note
294 that the types of some are slightly different to the real ones. Some
295 extra useful functions are provided too; descriptions of how they work
296 are given below. */
297
298#if !defined(NULL)
299# define NULL ((void*)0)
300#endif
301
302
303/* ------------------------------------------------------------------ */
304/* stdio.h
305 *
306 * Note that they all output to the file descriptor given by the
307 * --logfile-fd=N argument, which defaults to 2 (stderr). Hence no
308 * need for VG_(fprintf)().
njn25e49d8e72002-09-23 09:36:25 +0000309 */
sewardj78e3cd92002-10-22 04:45:48 +0000310extern UInt VG_(printf) ( const char *format, ... );
njn25e49d8e72002-09-23 09:36:25 +0000311/* too noisy ... __attribute__ ((format (printf, 1, 2))) ; */
sewardj78e3cd92002-10-22 04:45:48 +0000312extern UInt VG_(sprintf) ( Char* buf, Char *format, ... );
313extern UInt VG_(vprintf) ( void(*send)(Char),
njn25e49d8e72002-09-23 09:36:25 +0000314 const Char *format, va_list vargs );
315
njn41557122002-10-14 09:25:37 +0000316extern Int VG_(rename) ( Char* old_name, Char* new_name );
317
njn25e49d8e72002-09-23 09:36:25 +0000318/* ------------------------------------------------------------------ */
319/* stdlib.h */
320
321extern void* VG_(malloc) ( Int nbytes );
njnd5bb0a52002-09-27 10:24:48 +0000322extern void VG_(free) ( void* p );
323extern void* VG_(calloc) ( Int n, Int nbytes );
324extern void* VG_(realloc) ( void* p, Int size );
325extern void* VG_(malloc_aligned) ( Int align_bytes, Int nbytes );
njn25e49d8e72002-09-23 09:36:25 +0000326
327extern void VG_(print_malloc_stats) ( void );
328
329
330extern void VG_(exit)( Int status )
331 __attribute__ ((__noreturn__));
njne427a662002-10-02 11:08:25 +0000332/* Prints a panic message (a constant string), appends newline and bug
333 reporting info, aborts. */
334__attribute__ ((__noreturn__))
335extern void VG_(skin_panic) ( Char* str );
njn25e49d8e72002-09-23 09:36:25 +0000336
njnd5bb0a52002-09-27 10:24:48 +0000337/* Looks up VG_(client_envp) */
njn25e49d8e72002-09-23 09:36:25 +0000338extern Char* VG_(getenv) ( Char* name );
339
340/* Crude stand-in for the glibc system() call. */
341extern Int VG_(system) ( Char* cmd );
342
njnd5bb0a52002-09-27 10:24:48 +0000343extern Long VG_(atoll) ( Char* str );
njn25e49d8e72002-09-23 09:36:25 +0000344
345/* Like atoll(), but converts a number of base 2..36 */
346extern Long VG_(atoll36) ( UInt base, Char* str );
347
348
349/* ------------------------------------------------------------------ */
njnd5bb0a52002-09-27 10:24:48 +0000350/* ctype.h */
njn25e49d8e72002-09-23 09:36:25 +0000351extern Bool VG_(isspace) ( Char c );
352extern Bool VG_(isdigit) ( Char c );
353extern Char VG_(toupper) ( Char c );
354
355
356/* ------------------------------------------------------------------ */
357/* string.h */
358extern Int VG_(strlen) ( const Char* str );
359extern Char* VG_(strcat) ( Char* dest, const Char* src );
360extern Char* VG_(strncat) ( Char* dest, const Char* src, Int n );
361extern Char* VG_(strpbrk) ( const Char* s, const Char* accept );
362extern Char* VG_(strcpy) ( Char* dest, const Char* src );
363extern Char* VG_(strncpy) ( Char* dest, const Char* src, Int ndest );
364extern Int VG_(strcmp) ( const Char* s1, const Char* s2 );
365extern Int VG_(strncmp) ( const Char* s1, const Char* s2, Int nmax );
366extern Char* VG_(strstr) ( const Char* haystack, Char* needle );
367extern Char* VG_(strchr) ( const Char* s, Char c );
368extern Char* VG_(strdup) ( const Char* s);
sewardj7ab2aca2002-10-20 19:40:32 +0000369extern void* VG_(memcpy) ( void *d, const void *s, Int sz );
370extern void* VG_(memset) ( void *s, Int c, Int sz );
njn25e49d8e72002-09-23 09:36:25 +0000371
njnd5bb0a52002-09-27 10:24:48 +0000372/* Like strcmp() and strncmp(), but stop comparing at any whitespace. */
njn25e49d8e72002-09-23 09:36:25 +0000373extern Int VG_(strcmp_ws) ( const Char* s1, const Char* s2 );
njn25e49d8e72002-09-23 09:36:25 +0000374extern Int VG_(strncmp_ws) ( const Char* s1, const Char* s2, Int nmax );
375
njnd5bb0a52002-09-27 10:24:48 +0000376/* Like strncpy(), but if 'src' is longer than 'ndest' inserts a '\0' as the
377 last character. */
njn25e49d8e72002-09-23 09:36:25 +0000378extern void VG_(strncpy_safely) ( Char* dest, const Char* src, Int ndest );
379
380/* Mini-regexp function. Searches for 'pat' in 'str'. Supports
381 * meta-symbols '*' and '?'. '\' escapes meta-symbols. */
njn4ba5a792002-09-30 10:23:54 +0000382extern Bool VG_(string_match) ( Char* pat, Char* str );
njn25e49d8e72002-09-23 09:36:25 +0000383
384
385/* ------------------------------------------------------------------ */
386/* math.h */
njnd5bb0a52002-09-27 10:24:48 +0000387/* Returns the base-2 logarithm of x. */
njn25e49d8e72002-09-23 09:36:25 +0000388extern Int VG_(log2) ( Int x );
389
390
391/* ------------------------------------------------------------------ */
njnd5bb0a52002-09-27 10:24:48 +0000392/* unistd.h, fcntl.h, sys/stat.h */
sewardj4cf05692002-10-27 20:28:29 +0000393extern Int VG_(getpid) ( void );
394extern Int VG_(getppid) ( void );
njnd5bb0a52002-09-27 10:24:48 +0000395
njn4aca2d22002-10-04 10:29:38 +0000396extern Int VG_(open) ( const Char* pathname, Int flags, Int mode );
397extern Int VG_(read) ( Int fd, void* buf, Int count);
398extern Int VG_(write) ( Int fd, void* buf, Int count);
399extern void VG_(close) ( Int fd );
njnd5bb0a52002-09-27 10:24:48 +0000400
njn41557122002-10-14 09:25:37 +0000401/* Nb: VG_(rename)() declared in stdio.h section above */
njn4aca2d22002-10-04 10:29:38 +0000402extern Int VG_(unlink) ( Char* file_name );
403extern Int VG_(stat) ( Char* file_name, struct vki_stat* buf );
njn25e49d8e72002-09-23 09:36:25 +0000404
405
406/* ------------------------------------------------------------------ */
407/* assert.h */
408/* Asserts permanently enabled -- no turning off with NDEBUG. Hurrah! */
409#define VG__STRING(__str) #__str
410
njne427a662002-10-02 11:08:25 +0000411#define sk_assert(expr) \
njn25e49d8e72002-09-23 09:36:25 +0000412 ((void) ((expr) ? 0 : \
njne427a662002-10-02 11:08:25 +0000413 (VG_(skin_assert_fail) (VG__STRING(expr), \
414 __FILE__, __LINE__, \
415 __PRETTY_FUNCTION__), 0)))
njn25e49d8e72002-09-23 09:36:25 +0000416
njne427a662002-10-02 11:08:25 +0000417__attribute__ ((__noreturn__))
418extern void VG_(skin_assert_fail) ( Char* expr, Char* file,
419 Int line, Char* fn );
njn25e49d8e72002-09-23 09:36:25 +0000420
421
422/* ------------------------------------------------------------------ */
njnd5bb0a52002-09-27 10:24:48 +0000423/* system/mman.h */
njn25e49d8e72002-09-23 09:36:25 +0000424extern void* VG_(mmap)( void* start, UInt length,
425 UInt prot, UInt flags, UInt fd, UInt offset );
426extern Int VG_(munmap)( void* start, Int length );
427
428/* Get memory by anonymous mmap. */
429extern void* VG_(get_memory_from_mmap) ( Int nBytes, Char* who );
430
431
432/* ------------------------------------------------------------------ */
433/* signal.h.
434
435 Note that these use the vk_ (kernel) structure
436 definitions, which are different in places from those that glibc
437 defines -- hence the 'k' prefix. Since we're operating right at the
438 kernel interface, glibc's view of the world is entirely irrelevant. */
439
440/* --- Signal set ops --- */
njnd5bb0a52002-09-27 10:24:48 +0000441extern Int VG_(ksigfillset) ( vki_ksigset_t* set );
442extern Int VG_(ksigemptyset) ( vki_ksigset_t* set );
njn25e49d8e72002-09-23 09:36:25 +0000443
njnd5bb0a52002-09-27 10:24:48 +0000444extern Bool VG_(kisfullsigset) ( vki_ksigset_t* set );
445extern Bool VG_(kisemptysigset) ( vki_ksigset_t* set );
njn25e49d8e72002-09-23 09:36:25 +0000446
njnd5bb0a52002-09-27 10:24:48 +0000447extern Int VG_(ksigaddset) ( vki_ksigset_t* set, Int signum );
448extern Int VG_(ksigdelset) ( vki_ksigset_t* set, Int signum );
njn25e49d8e72002-09-23 09:36:25 +0000449extern Int VG_(ksigismember) ( vki_ksigset_t* set, Int signum );
450
njnd5bb0a52002-09-27 10:24:48 +0000451extern void VG_(ksigaddset_from_set) ( vki_ksigset_t* dst, vki_ksigset_t* src );
452extern void VG_(ksigdelset_from_set) ( vki_ksigset_t* dst, vki_ksigset_t* src );
njn25e49d8e72002-09-23 09:36:25 +0000453
454/* --- Mess with the kernel's sig state --- */
njnd5bb0a52002-09-27 10:24:48 +0000455extern Int VG_(ksigprocmask) ( Int how, const vki_ksigset_t* set,
njn25e49d8e72002-09-23 09:36:25 +0000456 vki_ksigset_t* oldset );
njnd5bb0a52002-09-27 10:24:48 +0000457extern Int VG_(ksigaction) ( Int signum,
458 const vki_ksigaction* act,
459 vki_ksigaction* oldact );
njn25e49d8e72002-09-23 09:36:25 +0000460
njnd5bb0a52002-09-27 10:24:48 +0000461extern Int VG_(ksignal) ( Int signum, void (*sighandler)(Int) );
462extern Int VG_(ksigaltstack) ( const vki_kstack_t* ss, vki_kstack_t* oss );
njn25e49d8e72002-09-23 09:36:25 +0000463
sewardjdcaf3122002-09-30 23:12:33 +0000464extern Int VG_(kkill) ( Int pid, Int signo );
465extern Int VG_(ksigpending) ( vki_ksigset_t* set );
njn25e49d8e72002-09-23 09:36:25 +0000466
467
468/*====================================================================*/
469/*=== UCode definition ===*/
470/*====================================================================*/
471
sewardje1042472002-09-30 12:33:11 +0000472/* Tags which describe what operands are. Must fit into 4 bits, which
473 they clearly do. */
njn25e49d8e72002-09-23 09:36:25 +0000474typedef
sewardje1042472002-09-30 12:33:11 +0000475enum { TempReg =0, /* virtual temp-reg */
476 ArchReg =1, /* simulated integer reg */
477 ArchRegS =2, /* simulated segment reg */
478 RealReg =3, /* real machine's real reg */
479 SpillNo =4, /* spill slot location */
480 Literal =5, /* literal; .lit32 field has actual value */
481 Lit16 =6, /* literal; .val[123] field has actual value */
482 NoValue =7 /* operand not in use */
483 }
484 Tag;
njn25e49d8e72002-09-23 09:36:25 +0000485
njnd5bb0a52002-09-27 10:24:48 +0000486/* Invalid register numbers (can't be negative) */
njn25e49d8e72002-09-23 09:36:25 +0000487#define INVALID_TEMPREG 999999999
488#define INVALID_REALREG 999999999
489
490/* Microinstruction opcodes. */
491typedef
492 enum {
njnd5bb0a52002-09-27 10:24:48 +0000493 NOP, /* Null op */
njn25e49d8e72002-09-23 09:36:25 +0000494
sewardj7a5ebcf2002-11-13 22:42:13 +0000495 LOCK, /* Indicate the existance of a LOCK prefix (functionally NOP) */
496
njnd5bb0a52002-09-27 10:24:48 +0000497 /* Moving values around */
498 GET, PUT, /* simulated register <--> TempReg */
499 GETF, PUTF, /* simulated %eflags <--> TempReg */
500 LOAD, STORE, /* memory <--> TempReg */
501 MOV, /* TempReg <--> TempReg */
502 CMOV, /* Used for cmpxchg and cmov */
njn25e49d8e72002-09-23 09:36:25 +0000503
njnd5bb0a52002-09-27 10:24:48 +0000504 /* Arithmetic/logical ops */
505 ADD, ADC, SUB, SBB, /* Add/subtract (w/wo carry) */
506 AND, OR, XOR, NOT, /* Boolean ops */
507 SHL, SHR, SAR, ROL, ROR, RCL, RCR, /* Shift/rotate (w/wo carry) */
508 NEG, /* Negate */
509 INC, DEC, /* Increment/decrement */
510 BSWAP, /* Big-endian <--> little-endian */
511 CC2VAL, /* Condition code --> 0 or 1 */
512 WIDEN, /* Signed or unsigned widening */
njn25e49d8e72002-09-23 09:36:25 +0000513
njnd5bb0a52002-09-27 10:24:48 +0000514 /* Conditional or unconditional jump */
515 JMP,
516
517 /* FPU ops */
518 FPU, /* Doesn't touch memory */
519 FPU_R, FPU_W, /* Reads/writes memory */
520
521 /* Not strictly needed, but improve address calculation translations. */
njn25e49d8e72002-09-23 09:36:25 +0000522 LEA1, /* reg2 := const + reg1 */
523 LEA2, /* reg3 := const + reg1 + reg2 * 1,2,4 or 8 */
524
njnd5bb0a52002-09-27 10:24:48 +0000525 /* Hack for x86 REP insns. Jump to literal if TempReg/RealReg is zero. */
526 JIFZ,
njn25e49d8e72002-09-23 09:36:25 +0000527
njnd5bb0a52002-09-27 10:24:48 +0000528 /* Advance the simulated %eip by some small (< 128) number. */
529 INCEIP,
njn25e49d8e72002-09-23 09:36:25 +0000530
sewardje1042472002-09-30 12:33:11 +0000531 /* Dealing with segment registers */
532 GETSEG, PUTSEG, /* simulated segment register <--> TempReg */
533 USESEG, /* (LDT/GDT index, virtual addr) --> linear addr */
534
njnd5bb0a52002-09-27 10:24:48 +0000535 /* Not for translating x86 calls -- only to call helpers */
536 CALLM_S, CALLM_E, /* Mark start/end of CALLM push/pop sequence */
537 PUSH, POP, CLEAR, /* Add/remove/zap args for helpers */
538 CALLM, /* Call assembly-code helper */
539
540 /* Not for translating x86 calls -- only to call C helper functions of
541 up to three arguments (or two if the functions has a return value).
542 Arguments and return value must be word-sized. More arguments can
543 be faked with global variables (eg. use VG_(set_global_var)()).
544
545 Seven possibilities: 'arg[123]' show where args go, 'ret' shows
546 where return value goes (if present).
njn25e49d8e72002-09-23 09:36:25 +0000547
548 CCALL(-, -, - ) void f(void)
549 CCALL(arg1, -, - ) void f(UInt arg1)
550 CCALL(arg1, arg2, - ) void f(UInt arg1, UInt arg2)
551 CCALL(arg1, arg2, arg3) void f(UInt arg1, UInt arg2, UInt arg3)
552 CCALL(-, -, ret ) UInt f(UInt)
553 CCALL(arg1, -, ret ) UInt f(UInt arg1)
njnd5bb0a52002-09-27 10:24:48 +0000554 CCALL(arg1, arg2, ret ) UInt f(UInt arg1, UInt arg2) */
njn25e49d8e72002-09-23 09:36:25 +0000555 CCALL,
556
njnd5bb0a52002-09-27 10:24:48 +0000557 /* This opcode makes it easy for skins that extend UCode to do this to
558 avoid opcode overlap:
njn25e49d8e72002-09-23 09:36:25 +0000559
njnd5bb0a52002-09-27 10:24:48 +0000560 enum { EU_OP1 = DUMMY_FINAL_UOPCODE + 1, ... }
njn25e49d8e72002-09-23 09:36:25 +0000561
562 WARNING: Do not add new opcodes after this one! They can be added
563 before, though. */
564 DUMMY_FINAL_UOPCODE
565 }
566 Opcode;
567
568
njnd5bb0a52002-09-27 10:24:48 +0000569/* Condition codes, using the Intel encoding. CondAlways is an extra. */
njn25e49d8e72002-09-23 09:36:25 +0000570typedef
571 enum {
572 CondO = 0, /* overflow */
573 CondNO = 1, /* no overflow */
574 CondB = 2, /* below */
575 CondNB = 3, /* not below */
576 CondZ = 4, /* zero */
577 CondNZ = 5, /* not zero */
578 CondBE = 6, /* below or equal */
579 CondNBE = 7, /* not below or equal */
580 CondS = 8, /* negative */
sewardje1042472002-09-30 12:33:11 +0000581 CondNS = 9, /* not negative */
njn25e49d8e72002-09-23 09:36:25 +0000582 CondP = 10, /* parity even */
583 CondNP = 11, /* not parity even */
584 CondL = 12, /* jump less */
585 CondNL = 13, /* not less */
586 CondLE = 14, /* less or equal */
587 CondNLE = 15, /* not less or equal */
588 CondAlways = 16 /* Jump always */
589 }
590 Condcode;
591
592
593/* Descriptions of additional properties of *unconditional* jumps. */
594typedef
595 enum {
596 JmpBoring=0, /* boring unconditional jump */
597 JmpCall=1, /* jump due to an x86 call insn */
598 JmpRet=2, /* jump due to an x86 ret insn */
599 JmpSyscall=3, /* do a system call, then jump */
600 JmpClientReq=4 /* do a client request, then jump */
601 }
602 JmpKind;
603
604
605/* Flags. User-level code can only read/write O(verflow), S(ign),
606 Z(ero), A(ux-carry), C(arry), P(arity), and may also write
607 D(irection). That's a total of 7 flags. A FlagSet is a bitset,
608 thusly:
609 76543210
610 DOSZACP
611 and bit 7 must always be zero since it is unused.
sewardj2370f3b2002-11-30 15:01:01 +0000612
613 Note: these Flag? values are **not** the positions in the actual
614 %eflags register. */
615
njn25e49d8e72002-09-23 09:36:25 +0000616typedef UChar FlagSet;
617
618#define FlagD (1<<6)
619#define FlagO (1<<5)
620#define FlagS (1<<4)
621#define FlagZ (1<<3)
622#define FlagA (1<<2)
623#define FlagC (1<<1)
624#define FlagP (1<<0)
625
626#define FlagsOSZACP (FlagO | FlagS | FlagZ | FlagA | FlagC | FlagP)
627#define FlagsOSZAP (FlagO | FlagS | FlagZ | FlagA | FlagP)
628#define FlagsOSZCP (FlagO | FlagS | FlagZ | FlagC | FlagP)
629#define FlagsOSACP (FlagO | FlagS | FlagA | FlagC | FlagP)
630#define FlagsSZACP ( FlagS | FlagZ | FlagA | FlagC | FlagP)
631#define FlagsSZAP ( FlagS | FlagZ | FlagA | FlagP)
632#define FlagsZCP ( FlagZ | FlagC | FlagP)
633#define FlagsOC (FlagO | FlagC )
634#define FlagsAC ( FlagA | FlagC )
635
636#define FlagsALL (FlagsOSZACP | FlagD)
637#define FlagsEmpty (FlagSet)0
638
639
sewardj2370f3b2002-11-30 15:01:01 +0000640/* flag positions in eflags */
641#define EFlagC (1 << 0) /* carry */
642#define EFlagP (1 << 2) /* parity */
sewardjfa492d42002-12-08 18:20:01 +0000643#define EFlagA (1 << 4) /* aux carry */
sewardj2370f3b2002-11-30 15:01:01 +0000644#define EFlagZ (1 << 6) /* zero */
645#define EFlagS (1 << 7) /* sign */
sewardjfa492d42002-12-08 18:20:01 +0000646#define EFlagD (1 << 10) /* direction */
sewardj2370f3b2002-11-30 15:01:01 +0000647#define EFlagO (1 << 11) /* overflow */
648
njn25e49d8e72002-09-23 09:36:25 +0000649/* Liveness of general purpose registers, useful for code generation.
650 Reg rank order 0..N-1 corresponds to bits 0..N-1, ie. first
651 reg's liveness in bit 0, last reg's in bit N-1. Note that
652 these rankings don't match the Intel register ordering. */
653typedef UInt RRegSet;
654
655#define ALL_RREGS_DEAD 0 /* 0000...00b */
njne7c258c2002-10-03 08:57:01 +0000656#define ALL_RREGS_LIVE ((1 << VG_MAX_REALREGS)-1) /* 0011...11b */
njn25e49d8e72002-09-23 09:36:25 +0000657#define UNIT_RREGSET(rank) (1 << (rank))
658
659#define IS_RREG_LIVE(rank,rregs_live) (rregs_live & UNIT_RREGSET(rank))
660#define SET_RREG_LIVENESS(rank,rregs_live,b) \
661 do { RRegSet unit = UNIT_RREGSET(rank); \
662 if (b) rregs_live |= unit; \
663 else rregs_live &= ~unit; \
664 } while(0)
665
666
667/* A Micro (u)-instruction. */
668typedef
669 struct {
670 /* word 1 */
671 UInt lit32; /* 32-bit literal */
672
673 /* word 2 */
674 UShort val1; /* first operand */
675 UShort val2; /* second operand */
676
677 /* word 3 */
678 UShort val3; /* third operand */
679 UChar opcode; /* opcode */
680 UChar size; /* data transfer size */
681
682 /* word 4 */
683 FlagSet flags_r; /* :: FlagSet */
684 FlagSet flags_w; /* :: FlagSet */
685 UChar tag1:4; /* first operand tag */
686 UChar tag2:4; /* second operand tag */
687 UChar tag3:4; /* third operand tag */
688 UChar extra4b:4; /* Spare field, used by WIDEN for src
689 -size, and by LEA2 for scale (1,2,4 or 8),
690 and by JMPs for original x86 instr size */
691
692 /* word 5 */
693 UChar cond; /* condition, for jumps */
694 Bool signed_widen:1; /* signed or unsigned WIDEN ? */
695 JmpKind jmpkind:3; /* additional properties of unconditional JMP */
696
697 /* Additional properties for UInstrs that call C functions:
698 - CCALL
699 - PUT (when %ESP is the target)
700 - possibly skin-specific UInstrs
701 */
702 UChar argc:2; /* Number of args, max 3 */
703 UChar regparms_n:2; /* Number of args passed in registers */
704 Bool has_ret_val:1; /* Function has return value? */
705
706 /* RealReg liveness; only sensical after reg alloc and liveness
707 analysis done. This info is a little bit arch-specific --
708 VG_MAX_REALREGS can vary on different architectures. Note that
709 to use this information requires converting between register ranks
njn4ba5a792002-09-30 10:23:54 +0000710 and the Intel register numbers, using VG_(realreg_to_rank)()
711 and/or VG_(rank_to_realreg)() */
njn25e49d8e72002-09-23 09:36:25 +0000712 RRegSet regs_live_after:VG_MAX_REALREGS;
713 }
714 UInstr;
715
716
njn25e49d8e72002-09-23 09:36:25 +0000717typedef
njn810086f2002-11-14 12:42:47 +0000718 struct _UCodeBlock
njn25e49d8e72002-09-23 09:36:25 +0000719 UCodeBlock;
720
njn810086f2002-11-14 12:42:47 +0000721extern Int VG_(get_num_instrs) (UCodeBlock* cb);
722extern Int VG_(get_num_temps) (UCodeBlock* cb);
njn25e49d8e72002-09-23 09:36:25 +0000723
njn810086f2002-11-14 12:42:47 +0000724extern UInstr* VG_(get_instr) (UCodeBlock* cb, Int i);
725extern UInstr* VG_(get_last_instr) (UCodeBlock* cb);
726
njn25e49d8e72002-09-23 09:36:25 +0000727/*====================================================================*/
728/*=== Instrumenting UCode ===*/
729/*====================================================================*/
730
njn810086f2002-11-14 12:42:47 +0000731/* Find what this instruction does to its regs. `tag' indicates whether we're
732 considering TempRegs (pre-reg-alloc) or RealRegs (post-reg-alloc).
733 `regs' is filled with the affected register numbers, `isWrites' parallels
734 it and indicates if the reg is read or written. If a reg is read and
735 written, it will appear twice in `regs'. `regs' and `isWrites' must be
736 able to fit 3 elements.
njn25e49d8e72002-09-23 09:36:25 +0000737
njn810086f2002-11-14 12:42:47 +0000738 Useful for analysis/optimisation passes. */
739extern Int VG_(get_reg_usage) ( UInstr* u, Tag tag, Int* regs, Bool* isWrites );
njn25e49d8e72002-09-23 09:36:25 +0000740
741
njnd5bb0a52002-09-27 10:24:48 +0000742/* Used to register helper functions to be called from generated code. A
743 limited number of compact helpers can be registered; the code generated
744 to call them is slightly shorter -- so register the mostly frequently
745 called helpers as compact. */
njn25e49d8e72002-09-23 09:36:25 +0000746extern void VG_(register_compact_helper) ( Addr a );
747extern void VG_(register_noncompact_helper) ( Addr a );
748
749
750/* ------------------------------------------------------------------ */
751/* Virtual register allocation */
752
753/* Get a new virtual register */
njn4ba5a792002-09-30 10:23:54 +0000754extern Int VG_(get_new_temp) ( UCodeBlock* cb );
njn25e49d8e72002-09-23 09:36:25 +0000755
756/* Get a new virtual shadow register */
njn4ba5a792002-09-30 10:23:54 +0000757extern Int VG_(get_new_shadow) ( UCodeBlock* cb );
njn25e49d8e72002-09-23 09:36:25 +0000758
759/* Get a virtual register's corresponding virtual shadow register */
760#define SHADOW(tempreg) ((tempreg)+1)
761
762
763/* ------------------------------------------------------------------ */
764/* Low-level UInstr builders */
njn4ba5a792002-09-30 10:23:54 +0000765extern void VG_(new_NOP) ( UInstr* u );
766extern void VG_(new_UInstr0) ( UCodeBlock* cb, Opcode opcode, Int sz );
767extern void VG_(new_UInstr1) ( UCodeBlock* cb, Opcode opcode, Int sz,
njn25e49d8e72002-09-23 09:36:25 +0000768 Tag tag1, UInt val1 );
njn4ba5a792002-09-30 10:23:54 +0000769extern void VG_(new_UInstr2) ( UCodeBlock* cb, Opcode opcode, Int sz,
njn25e49d8e72002-09-23 09:36:25 +0000770 Tag tag1, UInt val1,
771 Tag tag2, UInt val2 );
njn4ba5a792002-09-30 10:23:54 +0000772extern void VG_(new_UInstr3) ( UCodeBlock* cb, Opcode opcode, Int sz,
njn25e49d8e72002-09-23 09:36:25 +0000773 Tag tag1, UInt val1,
774 Tag tag2, UInt val2,
775 Tag tag3, UInt val3 );
njn25e49d8e72002-09-23 09:36:25 +0000776
njn810086f2002-11-14 12:42:47 +0000777/* Set read/write/undefined flags. Undefined flags are treaten as written,
778 but it's worth keeping them logically distinct. */
779extern void VG_(set_flag_fields) ( UCodeBlock* cb, FlagSet fr, FlagSet fw,
780 FlagSet fu);
njn4ba5a792002-09-30 10:23:54 +0000781extern void VG_(set_lit_field) ( UCodeBlock* cb, UInt lit32 );
782extern void VG_(set_ccall_fields) ( UCodeBlock* cb, Addr fn, UChar argc,
783 UChar regparms_n, Bool has_ret_val );
njn810086f2002-11-14 12:42:47 +0000784extern void VG_(set_cond_field) ( UCodeBlock* cb, Condcode code );
njn25e49d8e72002-09-23 09:36:25 +0000785
njn4ba5a792002-09-30 10:23:54 +0000786extern void VG_(copy_UInstr) ( UCodeBlock* cb, UInstr* instr );
787
788extern Bool VG_(any_flag_use)( UInstr* u );
njn25e49d8e72002-09-23 09:36:25 +0000789
njnac6c1762002-10-04 14:34:15 +0000790/* Macro versions of the above; just shorter to type. */
791#define uInstr0 VG_(new_UInstr0)
792#define uInstr1 VG_(new_UInstr1)
793#define uInstr2 VG_(new_UInstr2)
794#define uInstr3 VG_(new_UInstr3)
795#define uLiteral VG_(set_lit_field)
796#define uCCall VG_(set_ccall_fields)
njn810086f2002-11-14 12:42:47 +0000797#define uCond VG_(set_cond_field)
798#define uFlagsRWU VG_(set_flag_fields)
njnac6c1762002-10-04 14:34:15 +0000799#define newTemp VG_(get_new_temp)
800#define newShadow VG_(get_new_shadow)
801
njn25e49d8e72002-09-23 09:36:25 +0000802/* Refer to `the last instruction stuffed in' (can be lvalue). */
803#define LAST_UINSTR(cb) (cb)->instrs[(cb)->used-1]
804
805
806/* ------------------------------------------------------------------ */
807/* Higher-level UInstr sequence builders */
njn4ba5a792002-09-30 10:23:54 +0000808extern void VG_(call_helper_0_0) ( UCodeBlock* cb, Addr f);
809extern void VG_(call_helper_1_0) ( UCodeBlock* cb, Addr f, UInt arg1,
810 UInt regparms_n);
811extern void VG_(call_helper_2_0) ( UCodeBlock* cb, Addr f, UInt arg1, UInt arg2,
812 UInt regparms_n);
njn25e49d8e72002-09-23 09:36:25 +0000813
814/* One way around the 3-arg C function limit is to pass args via global
815 * variables... ugly, but it works. */
njn4ba5a792002-09-30 10:23:54 +0000816extern void VG_(set_global_var) ( UCodeBlock* cb, Addr globvar_ptr, UInt val);
njn25e49d8e72002-09-23 09:36:25 +0000817
818/* ------------------------------------------------------------------ */
njnd5bb0a52002-09-27 10:24:48 +0000819/* Allocating/freeing basic blocks of UCode */
njn810086f2002-11-14 12:42:47 +0000820extern UCodeBlock* VG_(setup_UCodeBlock) ( UCodeBlock* cb );
njn4ba5a792002-09-30 10:23:54 +0000821extern void VG_(free_UCodeBlock) ( UCodeBlock* cb );
njnd5bb0a52002-09-27 10:24:48 +0000822
823/* ------------------------------------------------------------------ */
824/* UCode pretty/ugly printing. Probably only useful to call from a skin
njn25e49d8e72002-09-23 09:36:25 +0000825 if VG_(needs).extended_UCode == True. */
826
827/* When True, all generated code is/should be printed. */
828extern Bool VG_(print_codegen);
829
njn4ba5a792002-09-30 10:23:54 +0000830/* Pretty/ugly printing functions */
831extern void VG_(pp_UCodeBlock) ( UCodeBlock* cb, Char* title );
832extern void VG_(pp_UInstr) ( Int instrNo, UInstr* u );
833extern void VG_(pp_UInstr_regs) ( Int instrNo, UInstr* u );
834extern void VG_(up_UInstr) ( Int instrNo, UInstr* u );
835extern Char* VG_(name_UOpcode) ( Bool upper, Opcode opc );
836extern void VG_(pp_UOperand) ( UInstr* u, Int operandNo,
837 Int sz, Bool parens );
njn25e49d8e72002-09-23 09:36:25 +0000838
njn25e49d8e72002-09-23 09:36:25 +0000839
840/*====================================================================*/
njnd5bb0a52002-09-27 10:24:48 +0000841/*=== Generating x86 code from UCode ===*/
njn25e49d8e72002-09-23 09:36:25 +0000842/*====================================================================*/
843
njnd5bb0a52002-09-27 10:24:48 +0000844/* All this only necessary for skins with VG_(needs).extends_UCode == True. */
njn25e49d8e72002-09-23 09:36:25 +0000845
sewardje1042472002-09-30 12:33:11 +0000846/* This is the Intel register encoding -- integer regs. */
njn25e49d8e72002-09-23 09:36:25 +0000847#define R_EAX 0
848#define R_ECX 1
849#define R_EDX 2
850#define R_EBX 3
851#define R_ESP 4
852#define R_EBP 5
853#define R_ESI 6
854#define R_EDI 7
855
856#define R_AL (0+R_EAX)
857#define R_CL (0+R_ECX)
858#define R_DL (0+R_EDX)
859#define R_BL (0+R_EBX)
860#define R_AH (4+R_EAX)
861#define R_CH (4+R_ECX)
862#define R_DH (4+R_EDX)
863#define R_BH (4+R_EBX)
864
sewardje1042472002-09-30 12:33:11 +0000865/* This is the Intel register encoding -- segment regs. */
866#define R_ES 0
867#define R_CS 1
868#define R_SS 2
869#define R_DS 3
870#define R_FS 4
871#define R_GS 5
872
njn25e49d8e72002-09-23 09:36:25 +0000873/* For pretty printing x86 code */
sewardje1042472002-09-30 12:33:11 +0000874extern Char* VG_(name_of_seg_reg) ( Int sreg );
njn4ba5a792002-09-30 10:23:54 +0000875extern Char* VG_(name_of_int_reg) ( Int size, Int reg );
876extern Char VG_(name_of_int_size) ( Int size );
njn25e49d8e72002-09-23 09:36:25 +0000877
njnac6c1762002-10-04 14:34:15 +0000878/* Shorter macros for convenience */
879#define nameIReg VG_(name_of_int_reg)
880#define nameISize VG_(name_of_int_size)
881#define nameSReg VG_(name_of_seg_reg)
882
njn25e49d8e72002-09-23 09:36:25 +0000883/* Randomly useful things */
884extern UInt VG_(extend_s_8to32) ( UInt x );
885
886/* Code emitters */
njn4ba5a792002-09-30 10:23:54 +0000887extern void VG_(emitB) ( UInt b );
888extern void VG_(emitW) ( UInt w );
889extern void VG_(emitL) ( UInt l );
sewardjfa492d42002-12-08 18:20:01 +0000890extern void VG_(new_emit) ( Bool upd_cc, FlagSet uses_flags, FlagSet sets_flags );
njn25e49d8e72002-09-23 09:36:25 +0000891
892/* Finding offsets */
njn4ba5a792002-09-30 10:23:54 +0000893extern Int VG_(helper_offset) ( Addr a );
894extern Int VG_(shadow_reg_offset) ( Int arch );
895extern Int VG_(shadow_flags_offset) ( void );
njn25e49d8e72002-09-23 09:36:25 +0000896
njnd5bb0a52002-09-27 10:24:48 +0000897/* Convert reg ranks <-> Intel register ordering, for using register
898 liveness information. */
njn4ba5a792002-09-30 10:23:54 +0000899extern Int VG_(realreg_to_rank) ( Int realreg );
900extern Int VG_(rank_to_realreg) ( Int rank );
njn25e49d8e72002-09-23 09:36:25 +0000901
njnd5bb0a52002-09-27 10:24:48 +0000902/* Call a subroutine. Does no argument passing, stack manipulations, etc. */
sewardjfa492d42002-12-08 18:20:01 +0000903extern void VG_(synth_call) ( Bool ensure_shortform, Int word_offset,
904 Bool upd_cc, FlagSet use_flags, FlagSet set_flags );
njn25e49d8e72002-09-23 09:36:25 +0000905
njnd5bb0a52002-09-27 10:24:48 +0000906/* For calling C functions -- saves caller save regs, pushes args, calls,
907 clears the stack, restores caller save regs. `fn' must be registered in
908 the baseBlock first. Acceptable tags are RealReg and Literal. Optimises
909 things, eg. by not preserving non-live caller-save registers.
njn25e49d8e72002-09-23 09:36:25 +0000910
njnd5bb0a52002-09-27 10:24:48 +0000911 WARNING: a UInstr should *not* be translated with synth_ccall() followed
912 by some other x86 assembly code; this will invalidate the results of
913 vg_realreg_liveness_analysis() and everything will fall over. */
njn4ba5a792002-09-30 10:23:54 +0000914extern void VG_(synth_ccall) ( Addr fn, Int argc, Int regparms_n, UInt argv[],
915 Tag tagv[], Int ret_reg,
916 RRegSet regs_live_before,
917 RRegSet regs_live_after );
njn25e49d8e72002-09-23 09:36:25 +0000918
919/* Addressing modes */
njn4ba5a792002-09-30 10:23:54 +0000920extern void VG_(emit_amode_offregmem_reg)( Int off, Int regmem, Int reg );
921extern void VG_(emit_amode_ereg_greg) ( Int e_reg, Int g_reg );
njn25e49d8e72002-09-23 09:36:25 +0000922
923/* v-size (4, or 2 with OSO) insn emitters */
njn4ba5a792002-09-30 10:23:54 +0000924extern void VG_(emit_movv_offregmem_reg) ( Int sz, Int off, Int areg, Int reg );
925extern void VG_(emit_movv_reg_offregmem) ( Int sz, Int reg, Int off, Int areg );
926extern void VG_(emit_movv_reg_reg) ( Int sz, Int reg1, Int reg2 );
sewardjfa492d42002-12-08 18:20:01 +0000927extern void VG_(emit_nonshiftopv_lit_reg)( Bool upd_cc, Int sz, Opcode opc, UInt lit,
njn4ba5a792002-09-30 10:23:54 +0000928 Int reg );
sewardjfa492d42002-12-08 18:20:01 +0000929extern void VG_(emit_shiftopv_lit_reg) ( Bool upd_cc, Int sz, Opcode opc, UInt lit,
njn4ba5a792002-09-30 10:23:54 +0000930 Int reg );
sewardjfa492d42002-12-08 18:20:01 +0000931extern void VG_(emit_nonshiftopv_reg_reg)( Bool upd_cc, Int sz, Opcode opc,
njn4ba5a792002-09-30 10:23:54 +0000932 Int reg1, Int reg2 );
933extern void VG_(emit_movv_lit_reg) ( Int sz, UInt lit, Int reg );
sewardjfa492d42002-12-08 18:20:01 +0000934extern void VG_(emit_unaryopv_reg) ( Bool upd_cc, Int sz, Opcode opc, Int reg );
njn4ba5a792002-09-30 10:23:54 +0000935extern void VG_(emit_pushv_reg) ( Int sz, Int reg );
936extern void VG_(emit_popv_reg) ( Int sz, Int reg );
njn25e49d8e72002-09-23 09:36:25 +0000937
njn4ba5a792002-09-30 10:23:54 +0000938extern void VG_(emit_pushl_lit32) ( UInt int32 );
939extern void VG_(emit_pushl_lit8) ( Int lit8 );
sewardjfa492d42002-12-08 18:20:01 +0000940extern void VG_(emit_cmpl_zero_reg) ( Bool upd_cc, Int reg );
njn4ba5a792002-09-30 10:23:54 +0000941extern void VG_(emit_swapl_reg_EAX) ( Int reg );
942extern void VG_(emit_movv_lit_offregmem) ( Int sz, UInt lit, Int off,
943 Int memreg );
njn25e49d8e72002-09-23 09:36:25 +0000944
945/* b-size (1 byte) instruction emitters */
njn4ba5a792002-09-30 10:23:54 +0000946extern void VG_(emit_movb_lit_offregmem) ( UInt lit, Int off, Int memreg );
947extern void VG_(emit_movb_reg_offregmem) ( Int reg, Int off, Int areg );
sewardjfa492d42002-12-08 18:20:01 +0000948extern void VG_(emit_unaryopb_reg) ( Bool upd_cc, Opcode opc, Int reg );
949extern void VG_(emit_testb_lit_reg) ( Bool upd_cc, UInt lit, Int reg );
njn25e49d8e72002-09-23 09:36:25 +0000950
951/* zero-extended load emitters */
njn4ba5a792002-09-30 10:23:54 +0000952extern void VG_(emit_movzbl_offregmem_reg) ( Int off, Int regmem, Int reg );
953extern void VG_(emit_movzwl_offregmem_reg) ( Int off, Int areg, Int reg );
njn25e49d8e72002-09-23 09:36:25 +0000954
955/* misc instruction emitters */
njn4ba5a792002-09-30 10:23:54 +0000956extern void VG_(emit_call_reg) ( Int reg );
957extern void VG_(emit_add_lit_to_esp) ( Int lit );
njn4ba5a792002-09-30 10:23:54 +0000958extern void VG_(emit_pushal) ( void );
959extern void VG_(emit_popal) ( void );
960extern void VG_(emit_AMD_prefetch_reg) ( Int reg );
njn25e49d8e72002-09-23 09:36:25 +0000961
sewardja2113f92002-12-12 23:42:48 +0000962/* jump emitters */
963extern void VG_(init_target) ( Int *tgt );
964
965extern void VG_(target_back) ( Int *tgt );
966extern void VG_(target_forward) ( Int *tgt );
967extern void VG_(emit_target_delta) ( Int *tgt );
968
969extern void VG_(emit_jcondshort_delta) ( Bool simd_cc, Condcode cond, Int delta );
970extern void VG_(emit_jcondshort_target)( Bool simd_cc, Condcode cond, Int *tgt );
971
njn25e49d8e72002-09-23 09:36:25 +0000972
973/*====================================================================*/
974/*=== Execution contexts ===*/
975/*====================================================================*/
976
977/* Generic resolution type used in a few different ways, such as deciding
978 how closely to compare two errors for equality. */
979typedef
980 enum { Vg_LowRes, Vg_MedRes, Vg_HighRes }
981 VgRes;
982
983typedef
984 struct _ExeContext
985 ExeContext;
986
njnd5bb0a52002-09-27 10:24:48 +0000987/* Compare two ExeContexts. Number of callers considered depends on `res':
988 Vg_LowRes: 2
989 Vg_MedRes: 4
990 Vg_HighRes: all */
njn25e49d8e72002-09-23 09:36:25 +0000991extern Bool VG_(eq_ExeContext) ( VgRes res,
992 ExeContext* e1, ExeContext* e2 );
993
994/* Print an ExeContext. */
995extern void VG_(pp_ExeContext) ( ExeContext* );
996
997/* Take a snapshot of the client's stack. Search our collection of
998 ExeContexts to see if we already have it, and if not, allocate a
999 new one. Either way, return a pointer to the context. */
1000extern ExeContext* VG_(get_ExeContext) ( ThreadState *tst );
1001
sewardj499e3de2002-11-13 22:22:25 +00001002/* Just grab the client's EIP, as a much smaller and cheaper
1003 indication of where they are. */
1004extern Addr VG_(get_EIP)( ThreadState *tst );
njn25e49d8e72002-09-23 09:36:25 +00001005
1006/*====================================================================*/
1007/*=== Error reporting ===*/
1008/*====================================================================*/
1009
1010/* ------------------------------------------------------------------ */
1011/* Suppressions describe errors which we want to suppress, ie, not
1012 show the user, usually because it is caused by a problem in a library
1013 which we can't fix, replace or work around. Suppressions are read from
njnd5bb0a52002-09-27 10:24:48 +00001014 a file at startup time. This gives flexibility so that new
njn25e49d8e72002-09-23 09:36:25 +00001015 suppressions can be added to the file as and when needed.
1016*/
1017
1018typedef
1019 Int /* Do not make this unsigned! */
1020 SuppKind;
1021
njn810086f2002-11-14 12:42:47 +00001022/* The skin-relevant parts of a suppression are:
1023 kind: what kind of suppression; must be in the range (0..)
1024 string: use is optional. NULL by default.
1025 extra: use is optional. NULL by default. void* so it's extensible.
njn25e49d8e72002-09-23 09:36:25 +00001026*/
1027typedef
njn810086f2002-11-14 12:42:47 +00001028 struct _Supp
1029 Supp;
1030
1031/* Useful in SK_(error_matches_suppression)() */
1032SuppKind VG_(get_supp_kind) ( Supp* su );
1033Char* VG_(get_supp_string) ( Supp* su );
1034void* VG_(get_supp_extra) ( Supp* su );
1035
1036/* Must be used in VG_(recognised_suppression)() */
1037void VG_(set_supp_kind) ( Supp* su, SuppKind suppkind );
1038/* May be used in VG_(read_extra_suppression_info)() */
1039void VG_(set_supp_string) ( Supp* su, Char* string );
1040void VG_(set_supp_extra) ( Supp* su, void* extra );
njn25e49d8e72002-09-23 09:36:25 +00001041
1042
1043/* ------------------------------------------------------------------ */
1044/* Error records contain enough info to generate an error report. The idea
1045 is that (typically) the same few points in the program generate thousands
njnd5bb0a52002-09-27 10:24:48 +00001046 of errors, and we don't want to spew out a fresh error message for each
1047 one. Instead, we use these structures to common up duplicates.
njn25e49d8e72002-09-23 09:36:25 +00001048*/
1049
1050typedef
1051 Int /* Do not make this unsigned! */
1052 ErrorKind;
1053
njn810086f2002-11-14 12:42:47 +00001054/* The skin-relevant parts of an Error are:
1055 kind: what kind of error; must be in the range (0..)
1056 addr: use is optional. 0 by default.
1057 string: use is optional. NULL by default.
1058 extra: use is optional. NULL by default. void* so it's extensible.
njn25e49d8e72002-09-23 09:36:25 +00001059*/
1060typedef
njn810086f2002-11-14 12:42:47 +00001061 struct _Error
1062 Error;
njn25e49d8e72002-09-23 09:36:25 +00001063
njn810086f2002-11-14 12:42:47 +00001064/* Useful in SK_(error_matches_suppression)(), SK_(pp_SkinError)(), etc */
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
1097/* Get the file/function/line number of the instruction at address 'a'.
1098 For these four, if debug info for the address is found, it copies the
1099 info into the buffer/UInt and returns True. If not, it returns False and
1100 nothing is copied. VG_(get_fnname) always demangles C++ function names.
1101*/
1102extern Bool VG_(get_filename) ( Addr a, Char* filename, Int n_filename );
1103extern Bool VG_(get_fnname) ( Addr a, Char* fnname, Int n_fnname );
1104extern Bool VG_(get_linenum) ( Addr a, UInt* linenum );
1105
1106/* This one is more efficient if getting both filename and line number,
1107 because the two lookups are done together. */
1108extern Bool VG_(get_filename_linenum)
1109 ( Addr a, Char* filename, Int n_filename,
1110 UInt* linenum );
1111
1112/* Succeeds only if we find from debug info that 'a' is the address of the
1113 first instruction in a function -- as opposed to VG_(get_fnname) which
1114 succeeds if we find from debug info that 'a' is the address of any
1115 instruction in a function. Use this to instrument the start of
njnd5bb0a52002-09-27 10:24:48 +00001116 a particular function. Nb: if an executable/shared object is stripped
njn25e49d8e72002-09-23 09:36:25 +00001117 of its symbols, this function will not be able to recognise function
1118 entry points within it. */
1119extern Bool VG_(get_fnname_if_entry) ( Addr a, Char* filename, Int n_filename );
1120
1121/* Succeeds if the address is within a shared object or the main executable.
1122 It doesn't matter if debug info is present or not. */
1123extern Bool VG_(get_objname) ( Addr a, Char* objname, Int n_objname );
1124
sewardj47104382002-10-20 18:35:48 +00001125/* A way to get information about what segments are mapped */
1126typedef struct _SegInfo SegInfo;
1127
1128extern const SegInfo* VG_(next_seginfo)(const SegInfo *);
1129extern Addr VG_(seg_start)(const SegInfo *);
1130extern UInt VG_(seg_size)(const SegInfo *);
1131extern const UChar* VG_(seg_filename)(const SegInfo *);
1132extern UInt VG_(seg_sym_offset)(const SegInfo *);
1133
1134typedef
1135 enum {
1136 Vg_SectUnknown,
1137 Vg_SectText,
sewardj8fe15a32002-10-20 19:29:21 +00001138 Vg_SectData,
1139 Vg_SectBSS,
sewardj47104382002-10-20 18:35:48 +00001140 Vg_SectGOT,
sewardj8fe15a32002-10-20 19:29:21 +00001141 Vg_SectPLT,
sewardj47104382002-10-20 18:35:48 +00001142 }
1143 VgSectKind;
1144
1145extern VgSectKind VG_(seg_sect_kind)(Addr);
1146
njn25e49d8e72002-09-23 09:36:25 +00001147
1148/*====================================================================*/
1149/*=== Shadow chunks and block-finding ===*/
1150/*====================================================================*/
1151
njn810086f2002-11-14 12:42:47 +00001152/* The skin-relevant parts of a ShadowChunk are:
1153 size: size of the block in bytes
1154 addr: addr of the block
1155 extra: anything extra kept by the skin; size is determined by
1156 VG_(needs).sizeof_shadow_chunk
1157*/
njn25e49d8e72002-09-23 09:36:25 +00001158typedef
njn810086f2002-11-14 12:42:47 +00001159 struct _ShadowChunk
njn25e49d8e72002-09-23 09:36:25 +00001160 ShadowChunk;
1161
njn810086f2002-11-14 12:42:47 +00001162extern UInt VG_(get_sc_size) ( ShadowChunk* sc );
1163extern Addr VG_(get_sc_data) ( ShadowChunk* sc );
1164/* Gets the ith word of the `extra' field. */
1165extern UInt VG_(get_sc_extra) ( ShadowChunk* sc, UInt i );
1166/* Sets the ith word of the `extra' field to `word'. */
1167extern void VG_(set_sc_extra) ( ShadowChunk* sc, UInt i, UInt word );
1168
1169/* These two should only be used if the `alternative_free' need is set, once
1170 we reach the point where the block would have been free'd. */
1171extern ShadowChunk* VG_(get_sc_next) ( ShadowChunk* sc );
1172extern void VG_(set_sc_next) ( ShadowChunk* sc, ShadowChunk* next );
1173
1174
njn25e49d8e72002-09-23 09:36:25 +00001175/* Use this to free blocks if VG_(needs).alternative_free == True.
1176 It frees the ShadowChunk and the malloc'd block it points to. */
njn4ba5a792002-09-30 10:23:54 +00001177extern void VG_(free_ShadowChunk) ( ShadowChunk* sc );
njn25e49d8e72002-09-23 09:36:25 +00001178
1179/* Makes an array of pointers to all the shadow chunks of malloc'd blocks */
1180extern ShadowChunk** VG_(get_malloc_shadows) ( /*OUT*/ UInt* n_shadows );
1181
1182/* Determines if address 'a' is within the bounds of the block at start.
1183 Allows a little 'slop' round the edges. */
1184extern Bool VG_(addr_is_in_block) ( Addr a, Addr start, UInt size );
1185
1186/* Searches through currently malloc'd blocks until a matching one is found.
1187 Returns NULL if none match. Extra arguments can be implicitly passed to
njnd5bb0a52002-09-27 10:24:48 +00001188 p using nested functions; see memcheck/mc_errcontext.c for an example. */
njn25e49d8e72002-09-23 09:36:25 +00001189extern ShadowChunk* VG_(any_matching_mallocd_ShadowChunks)
1190 ( Bool (*p) ( ShadowChunk* ));
1191
1192/* Searches through all thread's stacks to see if any match. Returns
1193 * VG_INVALID_THREADID if none match. */
1194extern ThreadId VG_(any_matching_thread_stack)
1195 ( Bool (*p) ( Addr stack_min, Addr stack_max ));
1196
sewardja4495682002-10-21 07:29:59 +00001197/* Do memory leak detection. */
1198extern void VG_(generic_detect_memory_leaks) (
1199 Bool is_valid_64k_chunk ( UInt ),
1200 Bool is_valid_address ( Addr ),
1201 ExeContext* get_where ( ShadowChunk* ),
1202 VgRes leak_resolution,
1203 Bool show_reachable
1204 );
1205
1206
njn25e49d8e72002-09-23 09:36:25 +00001207/*====================================================================*/
1208/*=== Skin-specific stuff ===*/
1209/*====================================================================*/
1210
njnd04b7c62002-10-03 14:05:52 +00001211/* ------------------------------------------------------------------ */
1212/* Details */
njnd04b7c62002-10-03 14:05:52 +00001213
njn810086f2002-11-14 12:42:47 +00001214/* Information used in the startup message. `name' also determines the
1215 string used for identifying suppressions in a suppression file as
1216 belonging to this skin. `version' can be NULL, in which case (not
1217 surprisingly) no version info is printed; this mechanism is designed for
1218 skins distributed with Valgrind that share a version number with
1219 Valgrind. Other skins not distributed as part of Valgrind should
sewardjc0d8f682002-11-30 00:49:43 +00001220 probably have their own version number. */
1221extern void VG_(details_name) ( Char* name );
1222extern void VG_(details_version) ( Char* version );
1223extern void VG_(details_description) ( Char* description );
1224extern void VG_(details_copyright_author) ( Char* copyright_author );
1225
1226/* Average size of a translation, in bytes, so that the translation
1227 storage machinery can allocate memory appropriately. Not critical.
1228 If you're unsure, set to 100 (indicating typical code expansion of
1229 about 6:1). */
1230extern void VG_(details_avg_translation_sizeB) ( Int );
njnd04b7c62002-10-03 14:05:52 +00001231
njn810086f2002-11-14 12:42:47 +00001232/* String printed if an `sk_assert' assertion fails or VG_(skin_panic)
1233 is called. Should probably be an email address. */
1234extern void VG_(details_bug_reports_to) ( Char* bug_reports_to );
njnd04b7c62002-10-03 14:05:52 +00001235
1236/* ------------------------------------------------------------------ */
1237/* Needs */
1238
njn810086f2002-11-14 12:42:47 +00001239/* Booleans that decide core behaviour, but don't require extra
1240 operations to be defined if `True' */
njnd5bb0a52002-09-27 10:24:48 +00001241
njn810086f2002-11-14 12:42:47 +00001242/* Should __libc_freeres() be run? Bugs in it can crash the skin. */
1243extern void VG_(needs_libc_freeres) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001244
njn810086f2002-11-14 12:42:47 +00001245/* Want to have errors detected by Valgrind's core reported? Includes:
1246 - pthread API errors (many; eg. unlocking a non-locked mutex)
1247 - silly arguments to malloc() et al (eg. negative size)
1248 - invalid file descriptors to blocking syscalls read() and write()
1249 - bad signal numbers passed to sigaction()
1250 - attempt to install signal handler for SIGKILL or SIGSTOP */
1251extern void VG_(needs_core_errors) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001252
njn810086f2002-11-14 12:42:47 +00001253/* Booleans that indicate extra operations are defined; if these are True,
1254 the corresponding template functions (given below) must be defined. A
1255 lot like being a member of a type class. */
njn25e49d8e72002-09-23 09:36:25 +00001256
njn810086f2002-11-14 12:42:47 +00001257/* Want to report errors from skin? This implies use of suppressions, too. */
1258extern void VG_(needs_skin_errors) ( void );
njnd5bb0a52002-09-27 10:24:48 +00001259
njn810086f2002-11-14 12:42:47 +00001260/* Is information kept about specific individual basic blocks? (Eg. for
1261 cachegrind there are cost-centres for every instruction, stored at a
1262 basic block level.) If so, it sometimes has to be discarded, because
1263 .so mmap/munmap-ping or self-modifying code (informed by the
1264 DISCARD_TRANSLATIONS user request) can cause one instruction address
1265 to be used for more than one instruction in one program run... */
1266extern void VG_(needs_basic_block_discards) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001267
njn810086f2002-11-14 12:42:47 +00001268/* Skin maintains information about each register? */
1269extern void VG_(needs_shadow_regs) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001270
njn810086f2002-11-14 12:42:47 +00001271/* Skin defines its own command line options? */
1272extern void VG_(needs_command_line_options) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001273
njn810086f2002-11-14 12:42:47 +00001274/* Skin defines its own client requests? */
1275extern void VG_(needs_client_requests) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001276
njn810086f2002-11-14 12:42:47 +00001277/* Skin defines its own UInstrs? */
1278extern void VG_(needs_extended_UCode) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001279
njn810086f2002-11-14 12:42:47 +00001280/* Skin does stuff before and/or after system calls? */
1281extern void VG_(needs_syscall_wrapper) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001282
njn810086f2002-11-14 12:42:47 +00001283/* Size, in words, of extra info about malloc'd blocks recorded by
1284 skin. Be careful to get this right or you'll get seg faults! */
1285extern void VG_(needs_sizeof_shadow_block) ( Int size );
njn25e49d8e72002-09-23 09:36:25 +00001286
njn810086f2002-11-14 12:42:47 +00001287/* Skin does free()s itself? Useful if a skin needs to keep track of
1288 blocks in some way after they're free'd.
1289 WARNING: don't forget to call VG_(free_ShadowChunk)() for each block
1290 eventually! */
1291extern void VG_(needs_alternative_free) ( void );
sewardj8fe15a32002-10-20 19:29:21 +00001292
njn810086f2002-11-14 12:42:47 +00001293/* Are skin-state sanity checks performed? */
1294extern void VG_(needs_sanity_checks) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001295
njn810086f2002-11-14 12:42:47 +00001296/* Do we need to see data symbols? */
1297extern void VG_(needs_data_syms) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001298
1299/* ------------------------------------------------------------------ */
1300/* Core events to track */
1301
1302/* Part of the core from which this call was made. Useful for determining
njnd5bb0a52002-09-27 10:24:48 +00001303 what kind of error message should be emitted. */
njn25e49d8e72002-09-23 09:36:25 +00001304typedef
1305 enum { Vg_CorePThread, Vg_CoreSignal, Vg_CoreSysCall, Vg_CoreTranslate }
1306 CorePart;
1307
njn810086f2002-11-14 12:42:47 +00001308#define EV extern void
njn25e49d8e72002-09-23 09:36:25 +00001309
njn810086f2002-11-14 12:42:47 +00001310/* Events happening in core to track. To be notified, pass a callback
1311 function to the appropriate function. To ignore an event, don't do
1312 anything (default is for events to be ignored). */
1313
1314/* Memory events */
1315
1316EV VG_(track_new_mem_startup) ( void (*f)(Addr a, UInt len,
1317 Bool rr, Bool ww, Bool xx) );
1318EV VG_(track_new_mem_heap) ( void (*f)(Addr a, UInt len, Bool is_inited) );
1319EV VG_(track_new_mem_stack) ( void (*f)(Addr a, UInt len) );
1320EV VG_(track_new_mem_stack_aligned) ( void (*f)(Addr a, UInt len) );
1321EV VG_(track_new_mem_stack_signal) ( void (*f)(Addr a, UInt len) );
1322EV VG_(track_new_mem_brk) ( void (*f)(Addr a, UInt len) );
1323EV VG_(track_new_mem_mmap) ( void (*f)(Addr a, UInt len,
1324 Bool rr, Bool ww, Bool xx) );
1325
1326EV VG_(track_copy_mem_heap) ( void (*f)(Addr from, Addr to, UInt len) );
1327EV VG_(track_copy_mem_remap) ( void (*f)(Addr from, Addr to, UInt len) );
1328EV VG_(track_change_mem_mprotect) ( void (*f)(Addr a, UInt len,
1329 Bool rr, Bool ww, Bool xx) );
njn25e49d8e72002-09-23 09:36:25 +00001330
njn810086f2002-11-14 12:42:47 +00001331/* Used on redzones around malloc'd blocks and at end of stack */
1332EV VG_(track_ban_mem_heap) ( void (*f)(Addr a, UInt len) );
1333EV VG_(track_ban_mem_stack) ( void (*f)(Addr a, UInt len) );
njn25e49d8e72002-09-23 09:36:25 +00001334
njn810086f2002-11-14 12:42:47 +00001335EV VG_(track_die_mem_heap) ( void (*f)(Addr a, UInt len) );
1336EV VG_(track_die_mem_stack) ( void (*f)(Addr a, UInt len) );
1337EV VG_(track_die_mem_stack_aligned) ( void (*f)(Addr a, UInt len) );
1338EV VG_(track_die_mem_stack_signal) ( void (*f)(Addr a, UInt len) );
1339EV VG_(track_die_mem_brk) ( void (*f)(Addr a, UInt len) );
1340EV VG_(track_die_mem_munmap) ( void (*f)(Addr a, UInt len) );
njn25e49d8e72002-09-23 09:36:25 +00001341
njn810086f2002-11-14 12:42:47 +00001342EV VG_(track_bad_free) ( void (*f)(ThreadState* tst, Addr a) );
1343EV VG_(track_mismatched_free) ( void (*f)(ThreadState* tst, Addr a) );
njn25e49d8e72002-09-23 09:36:25 +00001344
njn810086f2002-11-14 12:42:47 +00001345EV VG_(track_pre_mem_read) ( void (*f)(CorePart part, ThreadState* tst,
1346 Char* s, Addr a, UInt size) );
1347EV VG_(track_pre_mem_read_asciiz) ( void (*f)(CorePart part, ThreadState* tst,
1348 Char* s, Addr a) );
1349EV VG_(track_pre_mem_write) ( void (*f)(CorePart part, ThreadState* tst,
1350 Char* s, Addr a, UInt size) );
1351/* Not implemented yet -- have to add in lots of places, which is a
1352 pain. Won't bother unless/until there's a need. */
1353/* EV VG_(track_post_mem_read) ( void (*f)(ThreadState* tst, Char* s,
1354 Addr a, UInt size) ); */
1355EV VG_(track_post_mem_write) ( void (*f)(Addr a, UInt size) );
njn25e49d8e72002-09-23 09:36:25 +00001356
1357
njn810086f2002-11-14 12:42:47 +00001358/* Scheduler events (not exhaustive) */
njn25e49d8e72002-09-23 09:36:25 +00001359
njn810086f2002-11-14 12:42:47 +00001360EV VG_(track_thread_run) ( void (*f)(ThreadId tid) );
njn25e49d8e72002-09-23 09:36:25 +00001361
njn810086f2002-11-14 12:42:47 +00001362/* Thread events (not exhaustive) */
sewardjdca84112002-11-13 22:29:34 +00001363
njn810086f2002-11-14 12:42:47 +00001364/* Called during thread create, before the new thread has run any
1365 instructions (or touched any memory). */
1366EV VG_(track_post_thread_create)( void (*f)(ThreadId tid, ThreadId child) );
1367/* Called once the joinee thread is terminated and the joining thread is
1368 about to resume. */
1369EV VG_(track_post_thread_join) ( void (*f)(ThreadId joiner, ThreadId joinee) );
njn25e49d8e72002-09-23 09:36:25 +00001370
njnd5bb0a52002-09-27 10:24:48 +00001371
njn810086f2002-11-14 12:42:47 +00001372/* Mutex events (not exhaustive) */
njn25e49d8e72002-09-23 09:36:25 +00001373
njn810086f2002-11-14 12:42:47 +00001374/* Called before a thread can block while waiting for a mutex (called
1375 regardless of whether the thread will block or not). */
1376EV VG_(track_pre_mutex_lock) ( void (*f)(ThreadId tid,
1377 void* /*pthread_mutex_t* */ mutex) );
1378/* Called once the thread actually holds the mutex (always paired with
1379 pre_mutex_lock). */
1380EV VG_(track_post_mutex_lock) ( void (*f)(ThreadId tid,
1381 void* /*pthread_mutex_t* */ mutex) );
1382/* Called after a thread has released a mutex (no need for a corresponding
1383 pre_mutex_unlock, because unlocking can't block). */
1384EV VG_(track_post_mutex_unlock) ( void (*f)(ThreadId tid,
1385 void* /*pthread_mutex_t* */ mutex) );
njn25e49d8e72002-09-23 09:36:25 +00001386
njn810086f2002-11-14 12:42:47 +00001387/* Others... condition variable, signal events... */
1388/* ... */
1389
1390#undef EV
njn25e49d8e72002-09-23 09:36:25 +00001391
1392/* ------------------------------------------------------------------ */
1393/* Template functions */
1394
1395/* These are the parameterised functions in the core. The default definitions
njnd5bb0a52002-09-27 10:24:48 +00001396 are overridden by LD_PRELOADed skin version. At the very least, a skin
1397 must define the fundamental template functions. Depending on what needs
1398 are set, extra template functions will be used too. Functions are
1399 grouped under the needs that govern their use. */
njn25e49d8e72002-09-23 09:36:25 +00001400
1401
1402/* ------------------------------------------------------------------ */
1403/* Fundamental template functions */
1404
1405/* Initialise skin. Must do the following:
njn810086f2002-11-14 12:42:47 +00001406 - initialise the `details' struct, via the VG_(details_*)() functions
njn25e49d8e72002-09-23 09:36:25 +00001407 - register any helpers called by generated code
1408
1409 May do the following:
njn810086f2002-11-14 12:42:47 +00001410 - initialise the `needs' struct to indicate certain requirements, via
1411 the VG_(needs_*)() functions
1412 - initialise the `track' struct to indicate core events of interest, via
1413 the VG_(track_*)() functions
njn25e49d8e72002-09-23 09:36:25 +00001414 - register any skin-specific profiling events
1415 - any other skin-specific initialisation
1416*/
njn810086f2002-11-14 12:42:47 +00001417extern void SK_(pre_clo_init) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001418
njnd5bb0a52002-09-27 10:24:48 +00001419/* Do initialisation that can only be done after command line processing. */
njn25e49d8e72002-09-23 09:36:25 +00001420extern void SK_(post_clo_init)( void );
1421
1422/* Instrument a basic block. Must be a true function, ie. the same input
1423 always results in the same output, because basic blocks can be
1424 retranslated. Unless you're doing something really strange...
1425 'orig_addr' is the address of the first instruction in the block. */
1426extern UCodeBlock* SK_(instrument) ( UCodeBlock* cb, Addr orig_addr );
1427
1428/* Finish up, print out any results, etc. */
1429extern void SK_(fini) ( void );
1430
1431
1432/* ------------------------------------------------------------------ */
1433/* VG_(needs).report_errors */
1434
1435/* Identify if two errors are equal, or equal enough. `res' indicates how
1436 close is "close enough". `res' should be passed on as necessary, eg. if
njn810086f2002-11-14 12:42:47 +00001437 the Error's `extra' part contains an ExeContext, `res' should be
njn25e49d8e72002-09-23 09:36:25 +00001438 passed to VG_(eq_ExeContext)() if the ExeContexts are considered. Other
1439 than that, probably don't worry about it unless you have lots of very
1440 similar errors occurring.
1441 */
njn810086f2002-11-14 12:42:47 +00001442extern Bool SK_(eq_SkinError) ( VgRes res, Error* e1, Error* e2 );
njn25e49d8e72002-09-23 09:36:25 +00001443
1444/* Print error context. The passed function pp_ExeContext() can be (and
1445 probably should be) used to print the location of the error. */
njn810086f2002-11-14 12:42:47 +00001446extern void SK_(pp_SkinError) ( Error* err, void (*pp_ExeContext)(void) );
njn25e49d8e72002-09-23 09:36:25 +00001447
njn810086f2002-11-14 12:42:47 +00001448/* Should copy the `extra' part which the core uses to override the old
1449 version. This is necessary to move from a temporary stack copy to a
1450 permanent heap one.
njn25e49d8e72002-09-23 09:36:25 +00001451
njn810086f2002-11-14 12:42:47 +00001452 Then should fill in any details that could be postponed until after the
1453 decision whether to ignore the error (ie. details not affecting the
1454 result of SK_(eq_SkinError)()). This saves time when errors are ignored.
njn25e49d8e72002-09-23 09:36:25 +00001455
1456 Yuk.
1457*/
njn810086f2002-11-14 12:42:47 +00001458extern void* SK_(dup_extra_and_update) ( Error* err );
njn25e49d8e72002-09-23 09:36:25 +00001459
njn810086f2002-11-14 12:42:47 +00001460/* Return value indicates recognition. If recognised, must set skind using
1461 VG_(set_supp_kind)(). */
1462extern Bool SK_(recognised_suppression) ( Char* name, Supp* su );
njn25e49d8e72002-09-23 09:36:25 +00001463
njn810086f2002-11-14 12:42:47 +00001464/* Read any extra info for this suppression kind. Most likely for filling
1465 in the `extra' and `string' parts (with VG_(set_supp_{extra,string})())
1466 of a suppression if necessary. Should return False if a syntax error
1467 occurred, True otherwise. */
1468extern Bool SK_(read_extra_suppression_info) ( Int fd, Char* buf, Int nBuf,
1469 Supp* su );
njn25e49d8e72002-09-23 09:36:25 +00001470
1471/* This should just check the kinds match and maybe some stuff in the
njn810086f2002-11-14 12:42:47 +00001472 `string' and `extra' field if appropriate (using VG_(get_supp_*)() to
1473 get the relevant suppression parts). */
1474extern Bool SK_(error_matches_suppression)(Error* err, Supp* su);
njn25e49d8e72002-09-23 09:36:25 +00001475
1476
1477/* ------------------------------------------------------------------ */
1478/* VG_(needs).basic_block_discards */
1479
njnd5bb0a52002-09-27 10:24:48 +00001480/* Should discard any information that pertains to specific basic blocks
1481 or instructions within the address range given. */
njn25e49d8e72002-09-23 09:36:25 +00001482extern void SK_(discard_basic_block_info) ( Addr a, UInt size );
1483
1484
1485/* ------------------------------------------------------------------ */
1486/* VG_(needs).shadow_regs */
1487
1488/* Valid values for general registers and EFLAGS register, for initialising
1489 and updating registers when written in certain places in core. */
1490extern void SK_(written_shadow_regs_values) ( UInt* gen_reg, UInt* eflags );
1491
1492
1493/* ------------------------------------------------------------------ */
1494/* VG_(needs).command_line_options */
1495
njnd5bb0a52002-09-27 10:24:48 +00001496/* Return True if option was recognised. Presumably sets some state to
1497 record the option as well. */
1498extern Bool SK_(process_cmd_line_option) ( Char* argv );
njn25e49d8e72002-09-23 09:36:25 +00001499
1500/* Print out command line usage for skin options */
1501extern Char* SK_(usage) ( void );
1502
1503
1504/* ------------------------------------------------------------------ */
1505/* VG_(needs).client_requests */
1506
sewardj34042512002-10-22 04:14:35 +00001507extern Bool SK_(handle_client_request) ( ThreadState* tst, UInt* arg_block, UInt *ret );
njn25e49d8e72002-09-23 09:36:25 +00001508
1509
1510/* ------------------------------------------------------------------ */
1511/* VG_(needs).extends_UCode */
1512
njn4ba5a792002-09-30 10:23:54 +00001513/* Useful to use in VG_(get_Xreg_usage)() */
njn810086f2002-11-14 12:42:47 +00001514#define VG_UINSTR_READS_REG(ono,regs,isWrites) \
njn25e49d8e72002-09-23 09:36:25 +00001515 { if (mycat(u->tag,ono) == tag) \
njn810086f2002-11-14 12:42:47 +00001516 { regs[n] = mycat(u->val,ono); \
1517 isWrites[n] = False; \
njn25e49d8e72002-09-23 09:36:25 +00001518 n++; \
1519 } \
1520 }
njn810086f2002-11-14 12:42:47 +00001521#define VG_UINSTR_WRITES_REG(ono,regs,isWrites) \
njnd5bb0a52002-09-27 10:24:48 +00001522 { if (mycat(u->tag,ono) == tag) \
njn810086f2002-11-14 12:42:47 +00001523 { regs[n] = mycat(u->val,ono); \
1524 isWrites[n] = True; \
njnd5bb0a52002-09-27 10:24:48 +00001525 n++; \
1526 } \
njn25e49d8e72002-09-23 09:36:25 +00001527 }
1528
njn4ba5a792002-09-30 10:23:54 +00001529/* 'X' prefix indicates eXtended UCode. */
njn810086f2002-11-14 12:42:47 +00001530extern Int SK_(get_Xreg_usage) ( UInstr* u, Tag tag, Int* regs,
1531 Bool* isWrites );
njn4ba5a792002-09-30 10:23:54 +00001532extern void SK_(emit_XUInstr) ( UInstr* u, RRegSet regs_live_before );
1533extern Bool SK_(sane_XUInstr) ( Bool beforeRA, Bool beforeLiveness,
njn25e49d8e72002-09-23 09:36:25 +00001534 UInstr* u );
njn4ba5a792002-09-30 10:23:54 +00001535extern Char* SK_(name_XUOpcode) ( Opcode opc );
1536extern void SK_(pp_XUInstr) ( UInstr* u );
njn25e49d8e72002-09-23 09:36:25 +00001537
1538
1539/* ------------------------------------------------------------------ */
1540/* VG_(needs).syscall_wrapper */
1541
1542/* If either of the pre_ functions malloc() something to return, the
1543 * corresponding post_ function had better free() it!
1544 */
1545extern void* SK_( pre_syscall) ( ThreadId tid, UInt syscallno,
1546 Bool is_blocking );
1547extern void SK_(post_syscall) ( ThreadId tid, UInt syscallno,
1548 void* pre_result, Int res,
1549 Bool is_blocking );
1550
njnd5bb0a52002-09-27 10:24:48 +00001551
njn25e49d8e72002-09-23 09:36:25 +00001552/* ------------------------------------------------------------------ */
njnd5bb0a52002-09-27 10:24:48 +00001553/* VG_(needs).sizeof_shadow_chunk (if > 0) */
njn25e49d8e72002-09-23 09:36:25 +00001554
njn810086f2002-11-14 12:42:47 +00001555/* Must fill in the `extra' part, using VG_(set_sc_extra)(). */
njn25e49d8e72002-09-23 09:36:25 +00001556extern void SK_(complete_shadow_chunk) ( ShadowChunk* sc, ThreadState* tst );
1557
1558
1559/* ------------------------------------------------------------------ */
1560/* VG_(needs).alternative_free */
1561
njn810086f2002-11-14 12:42:47 +00001562/* If this need is set, when a dynamic block would normally be free'd, this
1563 is called instead. The block is contained inside the ShadowChunk; use
1564 the VG_(get_sc_*)() functions to access it. */
njn25e49d8e72002-09-23 09:36:25 +00001565extern void SK_(alt_free) ( ShadowChunk* sc, ThreadState* tst );
1566
njnd5bb0a52002-09-27 10:24:48 +00001567
njn25e49d8e72002-09-23 09:36:25 +00001568/* ---------------------------------------------------------------------
1569 VG_(needs).sanity_checks */
1570
njnd5bb0a52002-09-27 10:24:48 +00001571/* Can be useful for ensuring a skin's correctness. SK_(cheap_sanity_check)
1572 is called very frequently; SK_(expensive_sanity_check) is called less
1573 frequently and can be more involved. */
njn25e49d8e72002-09-23 09:36:25 +00001574extern Bool SK_(cheap_sanity_check) ( void );
1575extern Bool SK_(expensive_sanity_check) ( void );
1576
1577
1578#endif /* NDEF __VG_SKIN_H */
1579
1580/*--------------------------------------------------------------------*/
1581/*--- end vg_skin.h ---*/
1582/*--------------------------------------------------------------------*/
1583