blob: 73b5fb88805ff525730eaca83c1a1e4cfb410d9f [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
njn0e1b5142003-04-15 14:58:06 +000011 Copyright (C) 2000-2003 Julian Seward
njn25e49d8e72002-09-23 09:36:25 +000012 jseward@acm.org
13
14 This program is free software; you can redistribute it and/or
15 modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation; either version 2 of the
17 License, or (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful, but
20 WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
27 02111-1307, USA.
28
29 The GNU General Public License is contained in the file COPYING.
30*/
31
32#ifndef __VG_SKIN_H
33#define __VG_SKIN_H
34
35#include <stdarg.h> /* ANSI varargs stuff */
36#include <setjmp.h> /* for jmp_buf */
37
38#include "vg_constants_skin.h"
39
40
41/*====================================================================*/
42/*=== Build options and table sizes. ===*/
43/*====================================================================*/
44
45/* You should be able to change these options or sizes, recompile, and
46 still have a working system. */
47
48/* The maximum number of pthreads that we support. This is
49 deliberately not very high since our implementation of some of the
50 scheduler algorithms is surely O(N) in the number of threads, since
51 that's simple, at least. And (in practice) we hope that most
52 programs do not need many threads. */
53#define VG_N_THREADS 50
54
55/* Maximum number of pthread keys available. Again, we start low until
56 the need for a higher number presents itself. */
57#define VG_N_THREAD_KEYS 50
58
59/* Total number of integer registers available for allocation -- all of
60 them except %esp, %ebp. %ebp permanently points at VG_(baseBlock).
61
njn211b6ad2003-02-03 12:33:31 +000062 If you increase this you'll have to also change at least these:
njn4ba5a792002-09-30 10:23:54 +000063 - VG_(rank_to_realreg)()
64 - VG_(realreg_to_rank)()
njn25e49d8e72002-09-23 09:36:25 +000065 - ppRegsLiveness()
66 - the RegsLive type (maybe -- RegsLive type must have more than
67 VG_MAX_REALREGS bits)
njn211b6ad2003-02-03 12:33:31 +000068
69 You can decrease it, and performance will drop because more spills will
70 occur. If you decrease it too much, everything will fall over.
njn25e49d8e72002-09-23 09:36:25 +000071
72 Do not change this unless you really know what you are doing! */
73#define VG_MAX_REALREGS 6
74
75
76/*====================================================================*/
njn1a1dd8b2002-09-27 10:42:20 +000077/*=== Basic types, useful macros ===*/
njn25e49d8e72002-09-23 09:36:25 +000078/*====================================================================*/
79
njn25e49d8e72002-09-23 09:36:25 +000080typedef unsigned char UChar;
81typedef unsigned short UShort;
82typedef unsigned int UInt;
83typedef unsigned long long int ULong;
84
85typedef signed char Char;
86typedef signed short Short;
87typedef signed int Int;
88typedef signed long long int Long;
89
90typedef unsigned int Addr;
91
92typedef unsigned char Bool;
93#define False ((Bool)0)
94#define True ((Bool)1)
95
96
njn1a1dd8b2002-09-27 10:42:20 +000097#define mycat_wrk(aaa,bbb) aaa##bbb
98#define mycat(aaa,bbb) mycat_wrk(aaa,bbb)
99
100/* No, really. I _am_ that strange. */
101#define OINK(nnn) VG_(message)(Vg_DebugMsg, "OINK %d",nnn)
102
njn25e49d8e72002-09-23 09:36:25 +0000103/* ---------------------------------------------------------------------
104 Now the basic types are set up, we can haul in the kernel-interface
105 definitions.
106 ------------------------------------------------------------------ */
107
njn2574bb4762002-09-24 11:23:50 +0000108#include "../coregrind/vg_kerneliface.h"
njn25e49d8e72002-09-23 09:36:25 +0000109
110
111/*====================================================================*/
njn27f1a382002-11-08 15:48:16 +0000112/*=== Core/skin interface version ===*/
113/*====================================================================*/
114
115/* The major version number indicates binary-incompatible changes to the
116 interface; if the core and skin major versions don't match, Valgrind
117 will abort. The minor version indicates binary-compatible changes.
njn27f1a382002-11-08 15:48:16 +0000118*/
njn9b007f62003-04-07 14:40:25 +0000119#define VG_CORE_INTERFACE_MAJOR_VERSION 2
120#define VG_CORE_INTERFACE_MINOR_VERSION 0
njn27f1a382002-11-08 15:48:16 +0000121
122extern const Int VG_(skin_interface_major_version);
123extern const Int VG_(skin_interface_minor_version);
124
njn563f96f2003-02-03 11:17:46 +0000125/* Every skin must include this macro somewhere, exactly once. */
njn27f1a382002-11-08 15:48:16 +0000126#define VG_DETERMINE_INTERFACE_VERSION \
127const Int VG_(skin_interface_major_version) = VG_CORE_INTERFACE_MAJOR_VERSION; \
128const Int VG_(skin_interface_minor_version) = VG_CORE_INTERFACE_MINOR_VERSION;
129
njn211b6ad2003-02-03 12:33:31 +0000130
njn27f1a382002-11-08 15:48:16 +0000131/*====================================================================*/
njn25e49d8e72002-09-23 09:36:25 +0000132/*=== Command-line options ===*/
133/*====================================================================*/
134
njn43c799e2003-04-08 00:08:52 +0000135/* Use this for normal null-termination-style string comparison */
136#define VG_STREQ(s1,s2) (s1 != NULL && s2 != NULL \
137 && VG_(strcmp)((s1),(s2))==0)
138
139/* Use these for recognising skin command line options -- stops comparing
140 once whitespace is reached. */
141# define VG_CLO_STREQ(s1,s2) (0==VG_(strcmp_ws)((s1),(s2)))
142# define VG_CLO_STREQN(nn,s1,s2) (0==VG_(strncmp_ws)((s1),(s2),(nn)))
143
njn25e49d8e72002-09-23 09:36:25 +0000144/* Verbosity level: 0 = silent, 1 (default), > 1 = more verbose. */
145extern Int VG_(clo_verbosity);
146
147/* Profile? */
148extern Bool VG_(clo_profile);
149
njn25e49d8e72002-09-23 09:36:25 +0000150/* Call this if a recognised option was bad for some reason.
151 Note: don't use it just because an option was unrecognised -- return 'False'
152 from SKN_(process_cmd_line_option) to indicate that. */
153extern void VG_(bad_option) ( Char* opt );
154
155/* Client args */
156extern Int VG_(client_argc);
157extern Char** VG_(client_argv);
158
njnd5bb0a52002-09-27 10:24:48 +0000159/* Client environment. Can be inspected with VG_(getenv)() */
njn25e49d8e72002-09-23 09:36:25 +0000160extern Char** VG_(client_envp);
161
162
163/*====================================================================*/
164/*=== Printing messages for the user ===*/
165/*====================================================================*/
166
167/* Print a message prefixed by "??<pid>?? "; '?' depends on the VgMsgKind.
168 Should be used for all user output. */
169
170typedef
171 enum { Vg_UserMsg, /* '?' == '=' */
172 Vg_DebugMsg, /* '?' == '-' */
173 Vg_DebugExtraMsg /* '?' == '+' */
174 }
175 VgMsgKind;
176
177/* Functions for building a message from multiple parts. */
178extern void VG_(start_msg) ( VgMsgKind kind );
179extern void VG_(add_to_msg) ( Char* format, ... );
180/* Ends and prints the message. Appends a newline. */
181extern void VG_(end_msg) ( void );
182
njnd5bb0a52002-09-27 10:24:48 +0000183/* Send a single-part message. Appends a newline. */
njn25e49d8e72002-09-23 09:36:25 +0000184extern void VG_(message) ( VgMsgKind kind, Char* format, ... );
185
186
187/*====================================================================*/
188/*=== Profiling ===*/
189/*====================================================================*/
190
191/* Nb: VGP_(register_profile_event)() relies on VgpUnc being the first one */
192#define VGP_CORE_LIST \
193 /* These ones depend on the core */ \
194 VGP_PAIR(VgpUnc, "unclassified"), \
195 VGP_PAIR(VgpRun, "running"), \
196 VGP_PAIR(VgpSched, "scheduler"), \
197 VGP_PAIR(VgpMalloc, "low-lev malloc/free"), \
198 VGP_PAIR(VgpCliMalloc, "client malloc/free"), \
njn25e49d8e72002-09-23 09:36:25 +0000199 VGP_PAIR(VgpTranslate, "translate-main"), \
200 VGP_PAIR(VgpToUCode, "to-ucode"), \
201 VGP_PAIR(VgpFromUcode, "from-ucode"), \
202 VGP_PAIR(VgpImprove, "improve"), \
njn9b007f62003-04-07 14:40:25 +0000203 VGP_PAIR(VgpESPUpdate, "ESP-update"), \
njn25e49d8e72002-09-23 09:36:25 +0000204 VGP_PAIR(VgpRegAlloc, "reg-alloc"), \
205 VGP_PAIR(VgpLiveness, "liveness-analysis"), \
206 VGP_PAIR(VgpDoLRU, "do-lru"), \
207 VGP_PAIR(VgpSlowFindT, "slow-search-transtab"), \
208 VGP_PAIR(VgpInitMem, "init-memory"), \
209 VGP_PAIR(VgpExeContext, "exe-context"), \
210 VGP_PAIR(VgpReadSyms, "read-syms"), \
211 VGP_PAIR(VgpSearchSyms, "search-syms"), \
212 VGP_PAIR(VgpAddToT, "add-to-transtab"), \
213 VGP_PAIR(VgpCoreSysWrap, "core-syscall-wrapper"), \
214 VGP_PAIR(VgpDemangle, "demangle"), \
njn37cea302002-09-30 11:24:00 +0000215 VGP_PAIR(VgpCoreCheapSanity, "core-cheap-sanity"), \
216 VGP_PAIR(VgpCoreExpensiveSanity, "core-expensive-sanity"), \
njn25e49d8e72002-09-23 09:36:25 +0000217 /* These ones depend on the skin */ \
218 VGP_PAIR(VgpPreCloInit, "pre-clo-init"), \
219 VGP_PAIR(VgpPostCloInit, "post-clo-init"), \
220 VGP_PAIR(VgpInstrument, "instrument"), \
221 VGP_PAIR(VgpSkinSysWrap, "skin-syscall-wrapper"), \
njn37cea302002-09-30 11:24:00 +0000222 VGP_PAIR(VgpSkinCheapSanity, "skin-cheap-sanity"), \
223 VGP_PAIR(VgpSkinExpensiveSanity, "skin-expensive-sanity"), \
njn25e49d8e72002-09-23 09:36:25 +0000224 VGP_PAIR(VgpFini, "fini")
225
226#define VGP_PAIR(n,name) n
227typedef enum { VGP_CORE_LIST } VgpCoreCC;
228#undef VGP_PAIR
229
230/* When registering skin profiling events, ensure that the 'n' value is in
231 * the range (VgpFini+1..) */
232extern void VGP_(register_profile_event) ( Int n, Char* name );
233
234extern void VGP_(pushcc) ( UInt cc );
235extern void VGP_(popcc) ( UInt cc );
236
237/* Define them only if they haven't already been defined by vg_profile.c */
238#ifndef VGP_PUSHCC
239# define VGP_PUSHCC(x)
240#endif
241#ifndef VGP_POPCC
242# define VGP_POPCC(x)
243#endif
244
245
246/*====================================================================*/
247/*=== Useful stuff to call from generated code ===*/
248/*====================================================================*/
249
250/* ------------------------------------------------------------------ */
251/* General stuff */
252
njn41557122002-10-14 09:25:37 +0000253/* 64-bit counter for the number of basic blocks done. */
254extern ULong VG_(bbs_done);
255
njn25e49d8e72002-09-23 09:36:25 +0000256/* Get the simulated %esp */
257extern Addr VG_(get_stack_pointer) ( void );
258
njnd5bb0a52002-09-27 10:24:48 +0000259/* Detect if an address is within Valgrind's stack or Valgrind's
260 m_state_static; useful for memory leak detectors to tell if a block
261 is used by Valgrind (and thus can be ignored). */
njn25e49d8e72002-09-23 09:36:25 +0000262extern Bool VG_(within_stack)(Addr a);
njn25e49d8e72002-09-23 09:36:25 +0000263extern Bool VG_(within_m_state_static)(Addr a);
264
265/* Check if an address is 4-byte aligned */
266#define IS_ALIGNED4_ADDR(aaa_p) (0 == (((UInt)(aaa_p)) & 3))
njn9b007f62003-04-07 14:40:25 +0000267#define IS_ALIGNED8_ADDR(aaa_p) (0 == (((UInt)(aaa_p)) & 7))
njn25e49d8e72002-09-23 09:36:25 +0000268
269
270/* ------------------------------------------------------------------ */
271/* Thread-related stuff */
272
273/* Special magic value for an invalid ThreadId. It corresponds to
274 LinuxThreads using zero as the initial value for
275 pthread_mutex_t.__m_owner and pthread_cond_t.__c_waiting. */
276#define VG_INVALID_THREADID ((ThreadId)(0))
277
278/* ThreadIds are simply indices into the vg_threads[] array. */
279typedef
280 UInt
281 ThreadId;
282
njnd5bb0a52002-09-27 10:24:48 +0000283/* struct _ThreadState defined elsewhere; ThreadState is abstract as its
284 definition is not important for skins. */
njn25e49d8e72002-09-23 09:36:25 +0000285typedef
286 struct _ThreadState
287 ThreadState;
288
njnc5c1b182003-05-30 09:37:26 +0000289/* Use this one from generated code */
sewardj7ab2aca2002-10-20 19:40:32 +0000290extern ThreadId VG_(get_current_tid) ( void );
njnc5c1b182003-05-30 09:37:26 +0000291
292/* Use this one from non-generated code -- if you use VG_(get_current_tid)(),
293 it will return 0 for the invalid thread, which is not what you want. */
sewardjb52a1b02002-10-23 21:38:22 +0000294extern ThreadId VG_(get_current_or_recent_tid) ( void );
njnc5c1b182003-05-30 09:37:26 +0000295
sewardj7ab2aca2002-10-20 19:40:32 +0000296extern ThreadId VG_(get_tid_from_ThreadState) ( ThreadState* );
njn25e49d8e72002-09-23 09:36:25 +0000297extern ThreadState* VG_(get_ThreadState) ( ThreadId tid );
298
njn3e884182003-04-15 13:03:23 +0000299/* Searches through all thread's stacks to see if any match. Returns
300 * VG_INVALID_THREADID if none match. */
301extern ThreadId VG_(first_matching_thread_stack)
302 ( Bool (*p) ( Addr stack_min, Addr stack_max ));
303
njn25e49d8e72002-09-23 09:36:25 +0000304
305/*====================================================================*/
306/*=== Valgrind's version of libc ===*/
307/*====================================================================*/
308
309/* Valgrind doesn't use libc at all, for good reasons (trust us). So here
310 are its own versions of C library functions, but with VG_ prefixes. Note
311 that the types of some are slightly different to the real ones. Some
njnf4ce3d32003-02-10 10:17:26 +0000312 additional useful functions are provided too; descriptions of how they
313 work are given below. */
njn25e49d8e72002-09-23 09:36:25 +0000314
315#if !defined(NULL)
316# define NULL ((void*)0)
317#endif
318
319
320/* ------------------------------------------------------------------ */
321/* stdio.h
322 *
323 * Note that they all output to the file descriptor given by the
324 * --logfile-fd=N argument, which defaults to 2 (stderr). Hence no
325 * need for VG_(fprintf)().
njn25e49d8e72002-09-23 09:36:25 +0000326 */
sewardj78e3cd92002-10-22 04:45:48 +0000327extern UInt VG_(printf) ( const char *format, ... );
njn25e49d8e72002-09-23 09:36:25 +0000328/* too noisy ... __attribute__ ((format (printf, 1, 2))) ; */
sewardj78e3cd92002-10-22 04:45:48 +0000329extern UInt VG_(sprintf) ( Char* buf, Char *format, ... );
330extern UInt VG_(vprintf) ( void(*send)(Char),
njn25e49d8e72002-09-23 09:36:25 +0000331 const Char *format, va_list vargs );
332
njn41557122002-10-14 09:25:37 +0000333extern Int VG_(rename) ( Char* old_name, Char* new_name );
334
njn25e49d8e72002-09-23 09:36:25 +0000335/* ------------------------------------------------------------------ */
336/* stdlib.h */
337
338extern void* VG_(malloc) ( Int nbytes );
njnd5bb0a52002-09-27 10:24:48 +0000339extern void VG_(free) ( void* p );
340extern void* VG_(calloc) ( Int n, Int nbytes );
341extern void* VG_(realloc) ( void* p, Int size );
342extern void* VG_(malloc_aligned) ( Int align_bytes, Int nbytes );
njn25e49d8e72002-09-23 09:36:25 +0000343
344extern void VG_(print_malloc_stats) ( void );
345
346
347extern void VG_(exit)( Int status )
348 __attribute__ ((__noreturn__));
njne427a662002-10-02 11:08:25 +0000349/* Prints a panic message (a constant string), appends newline and bug
350 reporting info, aborts. */
351__attribute__ ((__noreturn__))
352extern void VG_(skin_panic) ( Char* str );
njn25e49d8e72002-09-23 09:36:25 +0000353
njnd5bb0a52002-09-27 10:24:48 +0000354/* Looks up VG_(client_envp) */
njn25e49d8e72002-09-23 09:36:25 +0000355extern Char* VG_(getenv) ( Char* name );
356
357/* Crude stand-in for the glibc system() call. */
358extern Int VG_(system) ( Char* cmd );
359
njnd5bb0a52002-09-27 10:24:48 +0000360extern Long VG_(atoll) ( Char* str );
njn25e49d8e72002-09-23 09:36:25 +0000361
362/* Like atoll(), but converts a number of base 2..36 */
363extern Long VG_(atoll36) ( UInt base, Char* str );
364
365
366/* ------------------------------------------------------------------ */
njnd5bb0a52002-09-27 10:24:48 +0000367/* ctype.h */
njn25e49d8e72002-09-23 09:36:25 +0000368extern Bool VG_(isspace) ( Char c );
369extern Bool VG_(isdigit) ( Char c );
370extern Char VG_(toupper) ( Char c );
371
372
373/* ------------------------------------------------------------------ */
374/* string.h */
375extern Int VG_(strlen) ( const Char* str );
376extern Char* VG_(strcat) ( Char* dest, const Char* src );
377extern Char* VG_(strncat) ( Char* dest, const Char* src, Int n );
378extern Char* VG_(strpbrk) ( const Char* s, const Char* accept );
379extern Char* VG_(strcpy) ( Char* dest, const Char* src );
380extern Char* VG_(strncpy) ( Char* dest, const Char* src, Int ndest );
381extern Int VG_(strcmp) ( const Char* s1, const Char* s2 );
382extern Int VG_(strncmp) ( const Char* s1, const Char* s2, Int nmax );
383extern Char* VG_(strstr) ( const Char* haystack, Char* needle );
384extern Char* VG_(strchr) ( const Char* s, Char c );
385extern Char* VG_(strdup) ( const Char* s);
sewardj7ab2aca2002-10-20 19:40:32 +0000386extern void* VG_(memcpy) ( void *d, const void *s, Int sz );
387extern void* VG_(memset) ( void *s, Int c, Int sz );
njn6d68e792003-02-24 10:49:08 +0000388extern Int VG_(memcmp) ( const void* s1, const void* s2, Int n );
njn25e49d8e72002-09-23 09:36:25 +0000389
njnd5bb0a52002-09-27 10:24:48 +0000390/* Like strcmp() and strncmp(), but stop comparing at any whitespace. */
njn25e49d8e72002-09-23 09:36:25 +0000391extern Int VG_(strcmp_ws) ( const Char* s1, const Char* s2 );
njn25e49d8e72002-09-23 09:36:25 +0000392extern Int VG_(strncmp_ws) ( const Char* s1, const Char* s2, Int nmax );
393
njnd5bb0a52002-09-27 10:24:48 +0000394/* Like strncpy(), but if 'src' is longer than 'ndest' inserts a '\0' as the
395 last character. */
njn25e49d8e72002-09-23 09:36:25 +0000396extern void VG_(strncpy_safely) ( Char* dest, const Char* src, Int ndest );
397
398/* Mini-regexp function. Searches for 'pat' in 'str'. Supports
399 * meta-symbols '*' and '?'. '\' escapes meta-symbols. */
njn4ba5a792002-09-30 10:23:54 +0000400extern Bool VG_(string_match) ( Char* pat, Char* str );
njn25e49d8e72002-09-23 09:36:25 +0000401
402
403/* ------------------------------------------------------------------ */
404/* math.h */
njnd5bb0a52002-09-27 10:24:48 +0000405/* Returns the base-2 logarithm of x. */
njn25e49d8e72002-09-23 09:36:25 +0000406extern Int VG_(log2) ( Int x );
407
408
409/* ------------------------------------------------------------------ */
njnd5bb0a52002-09-27 10:24:48 +0000410/* unistd.h, fcntl.h, sys/stat.h */
sewardj4cf05692002-10-27 20:28:29 +0000411extern Int VG_(getpid) ( void );
412extern Int VG_(getppid) ( void );
njnd5bb0a52002-09-27 10:24:48 +0000413
njn4aca2d22002-10-04 10:29:38 +0000414extern Int VG_(open) ( const Char* pathname, Int flags, Int mode );
415extern Int VG_(read) ( Int fd, void* buf, Int count);
416extern Int VG_(write) ( Int fd, void* buf, Int count);
417extern void VG_(close) ( Int fd );
njnd5bb0a52002-09-27 10:24:48 +0000418
njn41557122002-10-14 09:25:37 +0000419/* Nb: VG_(rename)() declared in stdio.h section above */
njn4aca2d22002-10-04 10:29:38 +0000420extern Int VG_(unlink) ( Char* file_name );
421extern Int VG_(stat) ( Char* file_name, struct vki_stat* buf );
njn25e49d8e72002-09-23 09:36:25 +0000422
njn13f02932003-04-30 20:23:58 +0000423extern Char* VG_(getcwd) ( Char* buf, Int size );
424
njn25e49d8e72002-09-23 09:36:25 +0000425
426/* ------------------------------------------------------------------ */
427/* assert.h */
428/* Asserts permanently enabled -- no turning off with NDEBUG. Hurrah! */
429#define VG__STRING(__str) #__str
430
njne427a662002-10-02 11:08:25 +0000431#define sk_assert(expr) \
njn25e49d8e72002-09-23 09:36:25 +0000432 ((void) ((expr) ? 0 : \
njne427a662002-10-02 11:08:25 +0000433 (VG_(skin_assert_fail) (VG__STRING(expr), \
434 __FILE__, __LINE__, \
435 __PRETTY_FUNCTION__), 0)))
njn25e49d8e72002-09-23 09:36:25 +0000436
njne427a662002-10-02 11:08:25 +0000437__attribute__ ((__noreturn__))
438extern void VG_(skin_assert_fail) ( Char* expr, Char* file,
439 Int line, Char* fn );
njn25e49d8e72002-09-23 09:36:25 +0000440
441
442/* ------------------------------------------------------------------ */
njnd5bb0a52002-09-27 10:24:48 +0000443/* system/mman.h */
njn25e49d8e72002-09-23 09:36:25 +0000444extern void* VG_(mmap)( void* start, UInt length,
445 UInt prot, UInt flags, UInt fd, UInt offset );
446extern Int VG_(munmap)( void* start, Int length );
447
448/* Get memory by anonymous mmap. */
449extern void* VG_(get_memory_from_mmap) ( Int nBytes, Char* who );
450
451
452/* ------------------------------------------------------------------ */
453/* signal.h.
454
455 Note that these use the vk_ (kernel) structure
456 definitions, which are different in places from those that glibc
457 defines -- hence the 'k' prefix. Since we're operating right at the
458 kernel interface, glibc's view of the world is entirely irrelevant. */
459
460/* --- Signal set ops --- */
njnd5bb0a52002-09-27 10:24:48 +0000461extern Int VG_(ksigfillset) ( vki_ksigset_t* set );
462extern Int VG_(ksigemptyset) ( vki_ksigset_t* set );
njn25e49d8e72002-09-23 09:36:25 +0000463
njnd5bb0a52002-09-27 10:24:48 +0000464extern Bool VG_(kisfullsigset) ( vki_ksigset_t* set );
465extern Bool VG_(kisemptysigset) ( vki_ksigset_t* set );
njn25e49d8e72002-09-23 09:36:25 +0000466
njnd5bb0a52002-09-27 10:24:48 +0000467extern Int VG_(ksigaddset) ( vki_ksigset_t* set, Int signum );
468extern Int VG_(ksigdelset) ( vki_ksigset_t* set, Int signum );
njn25e49d8e72002-09-23 09:36:25 +0000469extern Int VG_(ksigismember) ( vki_ksigset_t* set, Int signum );
470
njnd5bb0a52002-09-27 10:24:48 +0000471extern void VG_(ksigaddset_from_set) ( vki_ksigset_t* dst, vki_ksigset_t* src );
472extern void VG_(ksigdelset_from_set) ( vki_ksigset_t* dst, vki_ksigset_t* src );
njn25e49d8e72002-09-23 09:36:25 +0000473
474/* --- Mess with the kernel's sig state --- */
njnd5bb0a52002-09-27 10:24:48 +0000475extern Int VG_(ksigprocmask) ( Int how, const vki_ksigset_t* set,
njn25e49d8e72002-09-23 09:36:25 +0000476 vki_ksigset_t* oldset );
njnd5bb0a52002-09-27 10:24:48 +0000477extern Int VG_(ksigaction) ( Int signum,
478 const vki_ksigaction* act,
479 vki_ksigaction* oldact );
njn25e49d8e72002-09-23 09:36:25 +0000480
njnd5bb0a52002-09-27 10:24:48 +0000481extern Int VG_(ksignal) ( Int signum, void (*sighandler)(Int) );
482extern Int VG_(ksigaltstack) ( const vki_kstack_t* ss, vki_kstack_t* oss );
njn25e49d8e72002-09-23 09:36:25 +0000483
sewardjdcaf3122002-09-30 23:12:33 +0000484extern Int VG_(kkill) ( Int pid, Int signo );
485extern Int VG_(ksigpending) ( vki_ksigset_t* set );
njn25e49d8e72002-09-23 09:36:25 +0000486
487
488/*====================================================================*/
489/*=== UCode definition ===*/
490/*====================================================================*/
491
sewardje1042472002-09-30 12:33:11 +0000492/* Tags which describe what operands are. Must fit into 4 bits, which
493 they clearly do. */
njn25e49d8e72002-09-23 09:36:25 +0000494typedef
sewardje1042472002-09-30 12:33:11 +0000495enum { TempReg =0, /* virtual temp-reg */
496 ArchReg =1, /* simulated integer reg */
497 ArchRegS =2, /* simulated segment reg */
498 RealReg =3, /* real machine's real reg */
499 SpillNo =4, /* spill slot location */
500 Literal =5, /* literal; .lit32 field has actual value */
501 Lit16 =6, /* literal; .val[123] field has actual value */
502 NoValue =7 /* operand not in use */
503 }
504 Tag;
njn25e49d8e72002-09-23 09:36:25 +0000505
njnd5bb0a52002-09-27 10:24:48 +0000506/* Invalid register numbers (can't be negative) */
njn25e49d8e72002-09-23 09:36:25 +0000507#define INVALID_TEMPREG 999999999
508#define INVALID_REALREG 999999999
509
510/* Microinstruction opcodes. */
511typedef
512 enum {
njnd5bb0a52002-09-27 10:24:48 +0000513 NOP, /* Null op */
njn25e49d8e72002-09-23 09:36:25 +0000514
sewardj7a5ebcf2002-11-13 22:42:13 +0000515 LOCK, /* Indicate the existance of a LOCK prefix (functionally NOP) */
516
njnd5bb0a52002-09-27 10:24:48 +0000517 /* Moving values around */
518 GET, PUT, /* simulated register <--> TempReg */
519 GETF, PUTF, /* simulated %eflags <--> TempReg */
520 LOAD, STORE, /* memory <--> TempReg */
521 MOV, /* TempReg <--> TempReg */
522 CMOV, /* Used for cmpxchg and cmov */
njn25e49d8e72002-09-23 09:36:25 +0000523
njnd5bb0a52002-09-27 10:24:48 +0000524 /* Arithmetic/logical ops */
525 ADD, ADC, SUB, SBB, /* Add/subtract (w/wo carry) */
526 AND, OR, XOR, NOT, /* Boolean ops */
527 SHL, SHR, SAR, ROL, ROR, RCL, RCR, /* Shift/rotate (w/wo carry) */
528 NEG, /* Negate */
529 INC, DEC, /* Increment/decrement */
530 BSWAP, /* Big-endian <--> little-endian */
531 CC2VAL, /* Condition code --> 0 or 1 */
532 WIDEN, /* Signed or unsigned widening */
njn25e49d8e72002-09-23 09:36:25 +0000533
njnd5bb0a52002-09-27 10:24:48 +0000534 /* Conditional or unconditional jump */
535 JMP,
536
537 /* FPU ops */
538 FPU, /* Doesn't touch memory */
539 FPU_R, FPU_W, /* Reads/writes memory */
540
sewardj3d7c9c82003-03-26 21:08:13 +0000541 /* ------------ MMX ops ------------ */
sewardj8cec6ee2003-05-18 11:56:39 +0000542 /* In this and the SSE encoding, bytes at higher addresses are
543 held in bits [7:0] in these 16-bit words. I guess this means
544 it is a big-endian encoding. */
sewardj3d7c9c82003-03-26 21:08:13 +0000545
546 /* 1 byte, no memrefs, no iregdefs, copy exactly to the
547 output. Held in val1[7:0]. */
548 MMX1,
549
550 /* 2 bytes, no memrefs, no iregdefs, copy exactly to the
551 output. Held in val1[15:0]. */
552 MMX2,
553
554 /* 3 bytes, no memrefs, no iregdefs, copy exactly to the
555 output. Held in val1[15:0] and val2[7:0]. */
556 MMX3,
557
558 /* 2 bytes, reads/writes mem. Insns of the form
559 bbbbbbbb:mod mmxreg r/m.
560 Held in val1[15:0], and mod and rm are to be replaced
561 at codegen time by a reference to the Temp/RealReg holding
562 the address. Arg2 holds this Temp/Real Reg.
563 Transfer is always at size 8.
564 */
565 MMX2_MemRd,
566 MMX2_MemWr,
567
568 /* 2 bytes, reads/writes an integer register. Insns of the form
569 bbbbbbbb:11 mmxreg ireg.
570 Held in val1[15:0], and ireg is to be replaced
571 at codegen time by a reference to the relevant RealReg.
572 Transfer is always at size 4. Arg2 holds this Temp/Real Reg.
573 */
574 MMX2_RegRd,
575 MMX2_RegWr,
576
sewardj8cec6ee2003-05-18 11:56:39 +0000577 /* ------------ SSE/SSE2 ops ------------ */
578 /* In the following:
579
sewardjfebaa3b2003-05-25 01:07:34 +0000580 a digit N indicates the next N bytes are to be copied exactly
581 to the output.
sewardj8cec6ee2003-05-18 11:56:39 +0000582
583 'a' indicates a mod-xmmreg-rm byte, where the mod-rm part is
584 to be replaced at codegen time to a Temp/RealReg holding the
585 address.
586
587 'g' indicates a byte of the form '11 xmmreg ireg', where ireg
588 is written, and is to be replaced at codegen time by a
589 reference to the relevant RealReg.
590
591 'h' indicates a byte of the form '11 ireg xmmreg', where ireg
592 is read, and is to be replaced at codegen time by a reference
593 to the relevant RealReg. */
594
595 /* 3 bytes, no memrefs, no iregdefs, copy exactly to the
596 output. Held in val1[15:0] and val2[7:0]. */
597 SSE3,
598
599 /* 3 bytes, reads/writes mem. Insns of the form
600 bbbbbbbb:bbbbbbbb:mod mmxreg r/m.
601 Held in val1[15:0] and val2[7:0], and mod and rm are to be
602 replaced at codegen time by a reference to the Temp/RealReg
603 holding the address. Arg3 holds this Temp/Real Reg.
sewardjfebaa3b2003-05-25 01:07:34 +0000604 Transfer is usually, but not always, at size 16. */
sewardj8cec6ee2003-05-18 11:56:39 +0000605 SSE2a_MemRd,
606 SSE2a_MemWr,
607
608 /* 4 bytes, no memrefs, no iregdefs, copy exactly to the
609 output. Held in val1[15:0] and val2[15:0]. */
610 SSE4,
611
612 /* 4 bytes, reads/writes mem. Insns of the form
613 bbbbbbbb:bbbbbbbb:bbbbbbbb:mod mmxreg r/m.
614 Held in val1[15:0] and val2[15:0], and mod and rm are to be
615 replaced at codegen time by a reference to the Temp/RealReg
616 holding the address. Arg3 holds this Temp/Real Reg.
617 Transfer is always at size 16. */
618 SSE3a_MemRd,
619 SSE3a_MemWr,
620
621 /* 4 bytes, reads/writes mem. Insns of the form
622 bbbbbbbb:bbbbbbbb:mod mmxreg r/m:bbbbbbbb
623 Held in val1[15:0] and val2[15:0], and mod and rm are to be
624 replaced at codegen time by a reference to the Temp/RealReg
625 holding the address. Arg3 holds this Temp/Real Reg.
626 Transfer is always at size 16. */
627 SSE2a1_MemRd,
628 SSE2a1_MemWr,
629
630 /* 4 bytes, writes an integer register. Insns of the form
631 bbbbbbbb:bbbbbbbb:bbbbbbbb:11 xmmreg ireg.
632 Held in val1[15:0] and val2[15:0], and ireg is to be replaced
633 at codegen time by a reference to the relevant RealReg.
634 Transfer is always at size 4. Arg3 holds this Temp/Real Reg.
635 */
636 SSE3g_RegWr,
637
sewardjb31b06d2003-06-13 00:26:02 +0000638 /* 5 bytes, writes an integer register. Insns of the form
639 bbbbbbbb:bbbbbbbb:bbbbbbbb: 11 ireg bbb :bbbbbbbb. Held in
640 val1[15:0] and val2[15:0] and lit32[7:0], and ireg is to be
641 replaced at codegen time by a reference to the relevant
642 RealReg. Transfer is always at size 4. Arg3 holds this
643 Temp/Real Reg.
644 */
645 SSE3g1_RegWr,
646
647 /* 5 bytes, reads an integer register. Insns of the form
648 bbbbbbbb:bbbbbbbb:bbbbbbbb: 11 bbb ireg :bbbbbbbb. Held in
649 val1[15:0] and val2[15:0] and lit32[7:0], and ireg is to be
650 replaced at codegen time by a reference to the relevant
651 RealReg. Transfer is always at size 4. Arg3 holds this
652 Temp/Real Reg.
653 */
654 SSE3g1_RegRd,
655
sewardj8cec6ee2003-05-18 11:56:39 +0000656 /* 4 bytes, reads an integer register. Insns of the form
sewardjfebaa3b2003-05-25 01:07:34 +0000657 bbbbbbbb:bbbbbbbb:bbbbbbbb:11 xmmreg ireg.
sewardj8cec6ee2003-05-18 11:56:39 +0000658 Held in val1[15:0] and val2[15:0], and ireg is to be replaced
659 at codegen time by a reference to the relevant RealReg.
660 Transfer is always at size 4. Arg3 holds this Temp/Real Reg.
661 */
sewardjfebaa3b2003-05-25 01:07:34 +0000662 SSE3g_RegRd,
sewardj8cec6ee2003-05-18 11:56:39 +0000663
sewardj02af6bc2003-06-12 00:56:06 +0000664 /* 4 bytes, reads memory, writes an integer register, but is
665 nevertheless an SSE insn. The insn is of the form
666 bbbbbbbb:bbbbbbbb:bbbbbbbb:mod ireg rm where mod indicates
667 memory (ie is not 11b) and ireg is the int reg written. The
668 first 3 bytes are held in lit32[23:0] since there is
669 insufficient space elsewhere. mod and rm are to be replaced
670 at codegen time by a reference to the Temp/RealReg holding
671 the address. Arg1 holds this Temp/RealReg. ireg is to be
672 replaced at codegen time by a reference to the relevant
673 RealReg in which the answer is to be written. Arg2 holds
674 this Temp/RealReg. Transfer to the destination reg is always
675 at size 4. However the memory read can be at sizes 4 or 8
676 and so this is what the sz field holds.
677 */
678 SSE3ag_MemRd_RegWr,
679
sewardj8cec6ee2003-05-18 11:56:39 +0000680 /* 5 bytes, no memrefs, no iregdefs, copy exactly to the
681 output. Held in val1[15:0], val2[15:0] and val3[7:0]. */
682 SSE5,
683
684 /* 5 bytes, reads/writes mem. Insns of the form
685 bbbbbbbb:bbbbbbbb:bbbbbbbb:mod mmxreg r/m:bbbbbbbb
686 Held in val1[15:0], val2[15:0], lit32[7:0].
687 mod and rm are to be replaced at codegen time by a reference
688 to the Temp/RealReg holding the address. Arg3 holds this
689 Temp/Real Reg. Transfer is always at size 16. */
690 SSE3a1_MemRd,
691 SSE3a1_MemWr,
692
sewardj3d7c9c82003-03-26 21:08:13 +0000693 /* ------------------------ */
694
njnd5bb0a52002-09-27 10:24:48 +0000695 /* Not strictly needed, but improve address calculation translations. */
njn25e49d8e72002-09-23 09:36:25 +0000696 LEA1, /* reg2 := const + reg1 */
697 LEA2, /* reg3 := const + reg1 + reg2 * 1,2,4 or 8 */
698
sewardj8cec6ee2003-05-18 11:56:39 +0000699 /* Hack for x86 REP insns. Jump to literal if TempReg/RealReg
700 is zero. */
njnd5bb0a52002-09-27 10:24:48 +0000701 JIFZ,
njn25e49d8e72002-09-23 09:36:25 +0000702
njnd5bb0a52002-09-27 10:24:48 +0000703 /* Advance the simulated %eip by some small (< 128) number. */
704 INCEIP,
njn25e49d8e72002-09-23 09:36:25 +0000705
sewardje1042472002-09-30 12:33:11 +0000706 /* Dealing with segment registers */
707 GETSEG, PUTSEG, /* simulated segment register <--> TempReg */
708 USESEG, /* (LDT/GDT index, virtual addr) --> linear addr */
709
njnd5bb0a52002-09-27 10:24:48 +0000710 /* Not for translating x86 calls -- only to call helpers */
711 CALLM_S, CALLM_E, /* Mark start/end of CALLM push/pop sequence */
712 PUSH, POP, CLEAR, /* Add/remove/zap args for helpers */
713 CALLM, /* Call assembly-code helper */
714
715 /* Not for translating x86 calls -- only to call C helper functions of
716 up to three arguments (or two if the functions has a return value).
717 Arguments and return value must be word-sized. More arguments can
718 be faked with global variables (eg. use VG_(set_global_var)()).
719
720 Seven possibilities: 'arg[123]' show where args go, 'ret' shows
721 where return value goes (if present).
njn25e49d8e72002-09-23 09:36:25 +0000722
723 CCALL(-, -, - ) void f(void)
724 CCALL(arg1, -, - ) void f(UInt arg1)
725 CCALL(arg1, arg2, - ) void f(UInt arg1, UInt arg2)
726 CCALL(arg1, arg2, arg3) void f(UInt arg1, UInt arg2, UInt arg3)
727 CCALL(-, -, ret ) UInt f(UInt)
728 CCALL(arg1, -, ret ) UInt f(UInt arg1)
njnd5bb0a52002-09-27 10:24:48 +0000729 CCALL(arg1, arg2, ret ) UInt f(UInt arg1, UInt arg2) */
njn25e49d8e72002-09-23 09:36:25 +0000730 CCALL,
731
njnd5bb0a52002-09-27 10:24:48 +0000732 /* This opcode makes it easy for skins that extend UCode to do this to
733 avoid opcode overlap:
njn25e49d8e72002-09-23 09:36:25 +0000734
njnd5bb0a52002-09-27 10:24:48 +0000735 enum { EU_OP1 = DUMMY_FINAL_UOPCODE + 1, ... }
njn25e49d8e72002-09-23 09:36:25 +0000736
737 WARNING: Do not add new opcodes after this one! They can be added
738 before, though. */
739 DUMMY_FINAL_UOPCODE
740 }
741 Opcode;
742
743
njnd5bb0a52002-09-27 10:24:48 +0000744/* Condition codes, using the Intel encoding. CondAlways is an extra. */
njn25e49d8e72002-09-23 09:36:25 +0000745typedef
746 enum {
747 CondO = 0, /* overflow */
748 CondNO = 1, /* no overflow */
749 CondB = 2, /* below */
750 CondNB = 3, /* not below */
751 CondZ = 4, /* zero */
752 CondNZ = 5, /* not zero */
753 CondBE = 6, /* below or equal */
754 CondNBE = 7, /* not below or equal */
755 CondS = 8, /* negative */
sewardje1042472002-09-30 12:33:11 +0000756 CondNS = 9, /* not negative */
njn25e49d8e72002-09-23 09:36:25 +0000757 CondP = 10, /* parity even */
758 CondNP = 11, /* not parity even */
759 CondL = 12, /* jump less */
760 CondNL = 13, /* not less */
761 CondLE = 14, /* less or equal */
762 CondNLE = 15, /* not less or equal */
763 CondAlways = 16 /* Jump always */
764 }
765 Condcode;
766
767
768/* Descriptions of additional properties of *unconditional* jumps. */
769typedef
770 enum {
771 JmpBoring=0, /* boring unconditional jump */
772 JmpCall=1, /* jump due to an x86 call insn */
773 JmpRet=2, /* jump due to an x86 ret insn */
774 JmpSyscall=3, /* do a system call, then jump */
775 JmpClientReq=4 /* do a client request, then jump */
776 }
777 JmpKind;
778
779
780/* Flags. User-level code can only read/write O(verflow), S(ign),
781 Z(ero), A(ux-carry), C(arry), P(arity), and may also write
782 D(irection). That's a total of 7 flags. A FlagSet is a bitset,
783 thusly:
784 76543210
785 DOSZACP
786 and bit 7 must always be zero since it is unused.
sewardj2370f3b2002-11-30 15:01:01 +0000787
788 Note: these Flag? values are **not** the positions in the actual
789 %eflags register. */
790
njn25e49d8e72002-09-23 09:36:25 +0000791typedef UChar FlagSet;
792
793#define FlagD (1<<6)
794#define FlagO (1<<5)
795#define FlagS (1<<4)
796#define FlagZ (1<<3)
797#define FlagA (1<<2)
798#define FlagC (1<<1)
799#define FlagP (1<<0)
800
801#define FlagsOSZACP (FlagO | FlagS | FlagZ | FlagA | FlagC | FlagP)
802#define FlagsOSZAP (FlagO | FlagS | FlagZ | FlagA | FlagP)
803#define FlagsOSZCP (FlagO | FlagS | FlagZ | FlagC | FlagP)
804#define FlagsOSACP (FlagO | FlagS | FlagA | FlagC | FlagP)
805#define FlagsSZACP ( FlagS | FlagZ | FlagA | FlagC | FlagP)
806#define FlagsSZAP ( FlagS | FlagZ | FlagA | FlagP)
807#define FlagsZCP ( FlagZ | FlagC | FlagP)
808#define FlagsOC (FlagO | FlagC )
809#define FlagsAC ( FlagA | FlagC )
810
811#define FlagsALL (FlagsOSZACP | FlagD)
812#define FlagsEmpty (FlagSet)0
813
814
sewardj2370f3b2002-11-30 15:01:01 +0000815/* flag positions in eflags */
816#define EFlagC (1 << 0) /* carry */
817#define EFlagP (1 << 2) /* parity */
sewardjfa492d42002-12-08 18:20:01 +0000818#define EFlagA (1 << 4) /* aux carry */
sewardj2370f3b2002-11-30 15:01:01 +0000819#define EFlagZ (1 << 6) /* zero */
820#define EFlagS (1 << 7) /* sign */
sewardjfa492d42002-12-08 18:20:01 +0000821#define EFlagD (1 << 10) /* direction */
sewardj2370f3b2002-11-30 15:01:01 +0000822#define EFlagO (1 << 11) /* overflow */
823
njn25e49d8e72002-09-23 09:36:25 +0000824/* Liveness of general purpose registers, useful for code generation.
825 Reg rank order 0..N-1 corresponds to bits 0..N-1, ie. first
826 reg's liveness in bit 0, last reg's in bit N-1. Note that
827 these rankings don't match the Intel register ordering. */
828typedef UInt RRegSet;
829
830#define ALL_RREGS_DEAD 0 /* 0000...00b */
njne7c258c2002-10-03 08:57:01 +0000831#define ALL_RREGS_LIVE ((1 << VG_MAX_REALREGS)-1) /* 0011...11b */
njn25e49d8e72002-09-23 09:36:25 +0000832#define UNIT_RREGSET(rank) (1 << (rank))
833
834#define IS_RREG_LIVE(rank,rregs_live) (rregs_live & UNIT_RREGSET(rank))
835#define SET_RREG_LIVENESS(rank,rregs_live,b) \
836 do { RRegSet unit = UNIT_RREGSET(rank); \
837 if (b) rregs_live |= unit; \
838 else rregs_live &= ~unit; \
839 } while(0)
840
841
842/* A Micro (u)-instruction. */
843typedef
844 struct {
845 /* word 1 */
846 UInt lit32; /* 32-bit literal */
847
848 /* word 2 */
849 UShort val1; /* first operand */
850 UShort val2; /* second operand */
851
852 /* word 3 */
853 UShort val3; /* third operand */
854 UChar opcode; /* opcode */
855 UChar size; /* data transfer size */
856
857 /* word 4 */
858 FlagSet flags_r; /* :: FlagSet */
859 FlagSet flags_w; /* :: FlagSet */
860 UChar tag1:4; /* first operand tag */
861 UChar tag2:4; /* second operand tag */
862 UChar tag3:4; /* third operand tag */
863 UChar extra4b:4; /* Spare field, used by WIDEN for src
864 -size, and by LEA2 for scale (1,2,4 or 8),
865 and by JMPs for original x86 instr size */
866
867 /* word 5 */
868 UChar cond; /* condition, for jumps */
869 Bool signed_widen:1; /* signed or unsigned WIDEN ? */
870 JmpKind jmpkind:3; /* additional properties of unconditional JMP */
871
872 /* Additional properties for UInstrs that call C functions:
873 - CCALL
874 - PUT (when %ESP is the target)
875 - possibly skin-specific UInstrs
876 */
877 UChar argc:2; /* Number of args, max 3 */
878 UChar regparms_n:2; /* Number of args passed in registers */
879 Bool has_ret_val:1; /* Function has return value? */
880
881 /* RealReg liveness; only sensical after reg alloc and liveness
882 analysis done. This info is a little bit arch-specific --
883 VG_MAX_REALREGS can vary on different architectures. Note that
884 to use this information requires converting between register ranks
njn4ba5a792002-09-30 10:23:54 +0000885 and the Intel register numbers, using VG_(realreg_to_rank)()
886 and/or VG_(rank_to_realreg)() */
njn25e49d8e72002-09-23 09:36:25 +0000887 RRegSet regs_live_after:VG_MAX_REALREGS;
888 }
889 UInstr;
890
891
njn25e49d8e72002-09-23 09:36:25 +0000892typedef
njn810086f2002-11-14 12:42:47 +0000893 struct _UCodeBlock
njn25e49d8e72002-09-23 09:36:25 +0000894 UCodeBlock;
895
njn810086f2002-11-14 12:42:47 +0000896extern Int VG_(get_num_instrs) (UCodeBlock* cb);
897extern Int VG_(get_num_temps) (UCodeBlock* cb);
njn25e49d8e72002-09-23 09:36:25 +0000898
njn810086f2002-11-14 12:42:47 +0000899extern UInstr* VG_(get_instr) (UCodeBlock* cb, Int i);
900extern UInstr* VG_(get_last_instr) (UCodeBlock* cb);
901
njn211b6ad2003-02-03 12:33:31 +0000902
njn25e49d8e72002-09-23 09:36:25 +0000903/*====================================================================*/
904/*=== Instrumenting UCode ===*/
905/*====================================================================*/
906
njnf4ce3d32003-02-10 10:17:26 +0000907/* Maximum number of registers read or written by a single UInstruction. */
908#define VG_MAX_REGS_USED 3
njn25e49d8e72002-09-23 09:36:25 +0000909
njnf4ce3d32003-02-10 10:17:26 +0000910/* Find what this instruction does to its regs, useful for
911 analysis/optimisation passes. `tag' indicates whether we're considering
912 TempRegs (pre-reg-alloc) or RealRegs (post-reg-alloc). `regs' is filled
913 with the affected register numbers, `isWrites' parallels it and indicates
914 if the reg is read or written. If a reg is read and written, it will
915 appear twice in `regs'. `regs' and `isWrites' must be able to fit
916 VG_MAX_REGS_USED elements. */
njn810086f2002-11-14 12:42:47 +0000917extern Int VG_(get_reg_usage) ( UInstr* u, Tag tag, Int* regs, Bool* isWrites );
njn25e49d8e72002-09-23 09:36:25 +0000918
919
njnd5bb0a52002-09-27 10:24:48 +0000920/* Used to register helper functions to be called from generated code. A
921 limited number of compact helpers can be registered; the code generated
922 to call them is slightly shorter -- so register the mostly frequently
923 called helpers as compact. */
njn25e49d8e72002-09-23 09:36:25 +0000924extern void VG_(register_compact_helper) ( Addr a );
925extern void VG_(register_noncompact_helper) ( Addr a );
926
927
928/* ------------------------------------------------------------------ */
929/* Virtual register allocation */
930
931/* Get a new virtual register */
njn4ba5a792002-09-30 10:23:54 +0000932extern Int VG_(get_new_temp) ( UCodeBlock* cb );
njn25e49d8e72002-09-23 09:36:25 +0000933
934/* Get a new virtual shadow register */
njn4ba5a792002-09-30 10:23:54 +0000935extern Int VG_(get_new_shadow) ( UCodeBlock* cb );
njn25e49d8e72002-09-23 09:36:25 +0000936
937/* Get a virtual register's corresponding virtual shadow register */
938#define SHADOW(tempreg) ((tempreg)+1)
939
940
941/* ------------------------------------------------------------------ */
942/* Low-level UInstr builders */
njn4ba5a792002-09-30 10:23:54 +0000943extern void VG_(new_NOP) ( UInstr* u );
944extern void VG_(new_UInstr0) ( UCodeBlock* cb, Opcode opcode, Int sz );
945extern void VG_(new_UInstr1) ( UCodeBlock* cb, Opcode opcode, Int sz,
njn25e49d8e72002-09-23 09:36:25 +0000946 Tag tag1, UInt val1 );
njn4ba5a792002-09-30 10:23:54 +0000947extern void VG_(new_UInstr2) ( UCodeBlock* cb, Opcode opcode, Int sz,
njn25e49d8e72002-09-23 09:36:25 +0000948 Tag tag1, UInt val1,
949 Tag tag2, UInt val2 );
njn4ba5a792002-09-30 10:23:54 +0000950extern void VG_(new_UInstr3) ( UCodeBlock* cb, Opcode opcode, Int sz,
njn25e49d8e72002-09-23 09:36:25 +0000951 Tag tag1, UInt val1,
952 Tag tag2, UInt val2,
953 Tag tag3, UInt val3 );
njn25e49d8e72002-09-23 09:36:25 +0000954
njn810086f2002-11-14 12:42:47 +0000955/* Set read/write/undefined flags. Undefined flags are treaten as written,
956 but it's worth keeping them logically distinct. */
957extern void VG_(set_flag_fields) ( UCodeBlock* cb, FlagSet fr, FlagSet fw,
958 FlagSet fu);
njn4ba5a792002-09-30 10:23:54 +0000959extern void VG_(set_lit_field) ( UCodeBlock* cb, UInt lit32 );
960extern void VG_(set_ccall_fields) ( UCodeBlock* cb, Addr fn, UChar argc,
961 UChar regparms_n, Bool has_ret_val );
njn810086f2002-11-14 12:42:47 +0000962extern void VG_(set_cond_field) ( UCodeBlock* cb, Condcode code );
njn25e49d8e72002-09-23 09:36:25 +0000963
njn4ba5a792002-09-30 10:23:54 +0000964extern void VG_(copy_UInstr) ( UCodeBlock* cb, UInstr* instr );
965
966extern Bool VG_(any_flag_use)( UInstr* u );
njn25e49d8e72002-09-23 09:36:25 +0000967
njnac6c1762002-10-04 14:34:15 +0000968/* Macro versions of the above; just shorter to type. */
969#define uInstr0 VG_(new_UInstr0)
970#define uInstr1 VG_(new_UInstr1)
971#define uInstr2 VG_(new_UInstr2)
972#define uInstr3 VG_(new_UInstr3)
973#define uLiteral VG_(set_lit_field)
974#define uCCall VG_(set_ccall_fields)
njn810086f2002-11-14 12:42:47 +0000975#define uCond VG_(set_cond_field)
976#define uFlagsRWU VG_(set_flag_fields)
njnac6c1762002-10-04 14:34:15 +0000977#define newTemp VG_(get_new_temp)
978#define newShadow VG_(get_new_shadow)
979
njn25e49d8e72002-09-23 09:36:25 +0000980/* Refer to `the last instruction stuffed in' (can be lvalue). */
981#define LAST_UINSTR(cb) (cb)->instrs[(cb)->used-1]
982
983
984/* ------------------------------------------------------------------ */
985/* Higher-level UInstr sequence builders */
njn4ba5a792002-09-30 10:23:54 +0000986extern void VG_(call_helper_0_0) ( UCodeBlock* cb, Addr f);
987extern void VG_(call_helper_1_0) ( UCodeBlock* cb, Addr f, UInt arg1,
988 UInt regparms_n);
989extern void VG_(call_helper_2_0) ( UCodeBlock* cb, Addr f, UInt arg1, UInt arg2,
990 UInt regparms_n);
njn25e49d8e72002-09-23 09:36:25 +0000991
992/* One way around the 3-arg C function limit is to pass args via global
njn6fefa1b2003-02-24 10:32:51 +0000993 * variables... ugly, but it works. This one puts a literal in there. */
njn4ba5a792002-09-30 10:23:54 +0000994extern void VG_(set_global_var) ( UCodeBlock* cb, Addr globvar_ptr, UInt val);
njn25e49d8e72002-09-23 09:36:25 +0000995
njn6fefa1b2003-02-24 10:32:51 +0000996/* This one puts the contents of a TempReg in the global variable. */
997extern void VG_(set_global_var_tempreg) ( UCodeBlock* cb, Addr globvar_ptr,
998 UInt t_val);
999
njn25e49d8e72002-09-23 09:36:25 +00001000/* ------------------------------------------------------------------ */
njnd5bb0a52002-09-27 10:24:48 +00001001/* Allocating/freeing basic blocks of UCode */
njn810086f2002-11-14 12:42:47 +00001002extern UCodeBlock* VG_(setup_UCodeBlock) ( UCodeBlock* cb );
njn4ba5a792002-09-30 10:23:54 +00001003extern void VG_(free_UCodeBlock) ( UCodeBlock* cb );
njnd5bb0a52002-09-27 10:24:48 +00001004
1005/* ------------------------------------------------------------------ */
1006/* UCode pretty/ugly printing. Probably only useful to call from a skin
njn25e49d8e72002-09-23 09:36:25 +00001007 if VG_(needs).extended_UCode == True. */
1008
1009/* When True, all generated code is/should be printed. */
1010extern Bool VG_(print_codegen);
1011
njn4ba5a792002-09-30 10:23:54 +00001012/* Pretty/ugly printing functions */
1013extern void VG_(pp_UCodeBlock) ( UCodeBlock* cb, Char* title );
1014extern void VG_(pp_UInstr) ( Int instrNo, UInstr* u );
1015extern void VG_(pp_UInstr_regs) ( Int instrNo, UInstr* u );
1016extern void VG_(up_UInstr) ( Int instrNo, UInstr* u );
1017extern Char* VG_(name_UOpcode) ( Bool upper, Opcode opc );
njn563f96f2003-02-03 11:17:46 +00001018extern Char* VG_(name_UCondcode) ( Condcode cond );
njn4ba5a792002-09-30 10:23:54 +00001019extern void VG_(pp_UOperand) ( UInstr* u, Int operandNo,
1020 Int sz, Bool parens );
njn25e49d8e72002-09-23 09:36:25 +00001021
njnb93d1782003-02-03 12:03:22 +00001022/* ------------------------------------------------------------------ */
njnf4ce3d32003-02-10 10:17:26 +00001023/* Accessing archregs and their shadows */
1024extern UInt VG_(get_archreg) ( UInt archreg );
1025extern UInt VG_(get_thread_archreg) ( ThreadId tid, UInt archreg );
1026
njnb93d1782003-02-03 12:03:22 +00001027extern UInt VG_(get_shadow_archreg) ( UInt archreg );
1028extern void VG_(set_shadow_archreg) ( UInt archreg, UInt val );
njnd3040452003-05-19 15:04:06 +00001029extern void VG_(set_shadow_eflags) ( UInt val );
njnb93d1782003-02-03 12:03:22 +00001030extern Addr VG_(shadow_archreg_address) ( UInt archreg );
njn25e49d8e72002-09-23 09:36:25 +00001031
njnf4ce3d32003-02-10 10:17:26 +00001032extern UInt VG_(get_thread_shadow_archreg) ( ThreadId tid, UInt archreg );
1033extern void VG_(set_thread_shadow_archreg) ( ThreadId tid, UInt archreg,
1034 UInt val );
njn211b6ad2003-02-03 12:33:31 +00001035
1036/* ------------------------------------------------------------------ */
1037/* Offsets of addresses of helper functions. A "helper" function is one
1038 which is called from generated code via CALLM. */
1039
1040extern Int VGOFF_(helper_idiv_64_32);
1041extern Int VGOFF_(helper_div_64_32);
1042extern Int VGOFF_(helper_idiv_32_16);
1043extern Int VGOFF_(helper_div_32_16);
1044extern Int VGOFF_(helper_idiv_16_8);
1045extern Int VGOFF_(helper_div_16_8);
1046
1047extern Int VGOFF_(helper_imul_32_64);
1048extern Int VGOFF_(helper_mul_32_64);
1049extern Int VGOFF_(helper_imul_16_32);
1050extern Int VGOFF_(helper_mul_16_32);
1051extern Int VGOFF_(helper_imul_8_16);
1052extern Int VGOFF_(helper_mul_8_16);
1053
1054extern Int VGOFF_(helper_CLD);
1055extern Int VGOFF_(helper_STD);
1056extern Int VGOFF_(helper_get_dirflag);
1057
1058extern Int VGOFF_(helper_CLC);
1059extern Int VGOFF_(helper_STC);
1060
1061extern Int VGOFF_(helper_shldl);
1062extern Int VGOFF_(helper_shldw);
1063extern Int VGOFF_(helper_shrdl);
1064extern Int VGOFF_(helper_shrdw);
1065
1066extern Int VGOFF_(helper_RDTSC);
1067extern Int VGOFF_(helper_CPUID);
1068
1069extern Int VGOFF_(helper_bsf);
1070extern Int VGOFF_(helper_bsr);
1071
1072extern Int VGOFF_(helper_fstsw_AX);
1073extern Int VGOFF_(helper_SAHF);
njnd6251f12003-06-03 13:38:51 +00001074extern Int VGOFF_(helper_LAHF);
njn211b6ad2003-02-03 12:33:31 +00001075extern Int VGOFF_(helper_DAS);
1076extern Int VGOFF_(helper_DAA);
1077
1078
njn25e49d8e72002-09-23 09:36:25 +00001079/*====================================================================*/
njnd5bb0a52002-09-27 10:24:48 +00001080/*=== Generating x86 code from UCode ===*/
njn25e49d8e72002-09-23 09:36:25 +00001081/*====================================================================*/
1082
njnd5bb0a52002-09-27 10:24:48 +00001083/* All this only necessary for skins with VG_(needs).extends_UCode == True. */
njn25e49d8e72002-09-23 09:36:25 +00001084
sewardje1042472002-09-30 12:33:11 +00001085/* This is the Intel register encoding -- integer regs. */
njn25e49d8e72002-09-23 09:36:25 +00001086#define R_EAX 0
1087#define R_ECX 1
1088#define R_EDX 2
1089#define R_EBX 3
1090#define R_ESP 4
1091#define R_EBP 5
1092#define R_ESI 6
1093#define R_EDI 7
1094
1095#define R_AL (0+R_EAX)
1096#define R_CL (0+R_ECX)
1097#define R_DL (0+R_EDX)
1098#define R_BL (0+R_EBX)
1099#define R_AH (4+R_EAX)
1100#define R_CH (4+R_ECX)
1101#define R_DH (4+R_EDX)
1102#define R_BH (4+R_EBX)
1103
sewardje1042472002-09-30 12:33:11 +00001104/* This is the Intel register encoding -- segment regs. */
1105#define R_ES 0
1106#define R_CS 1
1107#define R_SS 2
1108#define R_DS 3
1109#define R_FS 4
1110#define R_GS 5
1111
njn25e49d8e72002-09-23 09:36:25 +00001112/* For pretty printing x86 code */
sewardj3d7c9c82003-03-26 21:08:13 +00001113extern Char* VG_(name_of_mmx_gran) ( UChar gran );
1114extern Char* VG_(name_of_mmx_reg) ( Int mmxreg );
sewardje1042472002-09-30 12:33:11 +00001115extern Char* VG_(name_of_seg_reg) ( Int sreg );
njn4ba5a792002-09-30 10:23:54 +00001116extern Char* VG_(name_of_int_reg) ( Int size, Int reg );
1117extern Char VG_(name_of_int_size) ( Int size );
njn25e49d8e72002-09-23 09:36:25 +00001118
njnac6c1762002-10-04 14:34:15 +00001119/* Shorter macros for convenience */
sewardj3d7c9c82003-03-26 21:08:13 +00001120#define nameIReg VG_(name_of_int_reg)
1121#define nameISize VG_(name_of_int_size)
1122#define nameSReg VG_(name_of_seg_reg)
1123#define nameMMXReg VG_(name_of_mmx_reg)
1124#define nameMMXGran VG_(name_of_mmx_gran)
sewardjfebaa3b2003-05-25 01:07:34 +00001125#define nameXMMReg VG_(name_of_xmm_reg)
njnac6c1762002-10-04 14:34:15 +00001126
njn25e49d8e72002-09-23 09:36:25 +00001127/* Randomly useful things */
1128extern UInt VG_(extend_s_8to32) ( UInt x );
1129
1130/* Code emitters */
njn4ba5a792002-09-30 10:23:54 +00001131extern void VG_(emitB) ( UInt b );
1132extern void VG_(emitW) ( UInt w );
1133extern void VG_(emitL) ( UInt l );
sewardjfa492d42002-12-08 18:20:01 +00001134extern void VG_(new_emit) ( Bool upd_cc, FlagSet uses_flags, FlagSet sets_flags );
njn25e49d8e72002-09-23 09:36:25 +00001135
1136/* Finding offsets */
njn4ba5a792002-09-30 10:23:54 +00001137extern Int VG_(helper_offset) ( Addr a );
1138extern Int VG_(shadow_reg_offset) ( Int arch );
1139extern Int VG_(shadow_flags_offset) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001140
njnd5bb0a52002-09-27 10:24:48 +00001141/* Convert reg ranks <-> Intel register ordering, for using register
1142 liveness information. */
njn4ba5a792002-09-30 10:23:54 +00001143extern Int VG_(realreg_to_rank) ( Int realreg );
1144extern Int VG_(rank_to_realreg) ( Int rank );
njn25e49d8e72002-09-23 09:36:25 +00001145
njnd5bb0a52002-09-27 10:24:48 +00001146/* Call a subroutine. Does no argument passing, stack manipulations, etc. */
sewardjfa492d42002-12-08 18:20:01 +00001147extern void VG_(synth_call) ( Bool ensure_shortform, Int word_offset,
1148 Bool upd_cc, FlagSet use_flags, FlagSet set_flags );
njn25e49d8e72002-09-23 09:36:25 +00001149
njnd5bb0a52002-09-27 10:24:48 +00001150/* For calling C functions -- saves caller save regs, pushes args, calls,
1151 clears the stack, restores caller save regs. `fn' must be registered in
1152 the baseBlock first. Acceptable tags are RealReg and Literal. Optimises
1153 things, eg. by not preserving non-live caller-save registers.
njn25e49d8e72002-09-23 09:36:25 +00001154
njnd5bb0a52002-09-27 10:24:48 +00001155 WARNING: a UInstr should *not* be translated with synth_ccall() followed
1156 by some other x86 assembly code; this will invalidate the results of
1157 vg_realreg_liveness_analysis() and everything will fall over. */
njn4ba5a792002-09-30 10:23:54 +00001158extern void VG_(synth_ccall) ( Addr fn, Int argc, Int regparms_n, UInt argv[],
1159 Tag tagv[], Int ret_reg,
1160 RRegSet regs_live_before,
1161 RRegSet regs_live_after );
njn25e49d8e72002-09-23 09:36:25 +00001162
1163/* Addressing modes */
njn4ba5a792002-09-30 10:23:54 +00001164extern void VG_(emit_amode_offregmem_reg)( Int off, Int regmem, Int reg );
1165extern void VG_(emit_amode_ereg_greg) ( Int e_reg, Int g_reg );
njn25e49d8e72002-09-23 09:36:25 +00001166
1167/* v-size (4, or 2 with OSO) insn emitters */
njn4ba5a792002-09-30 10:23:54 +00001168extern void VG_(emit_movv_offregmem_reg) ( Int sz, Int off, Int areg, Int reg );
1169extern void VG_(emit_movv_reg_offregmem) ( Int sz, Int reg, Int off, Int areg );
1170extern void VG_(emit_movv_reg_reg) ( Int sz, Int reg1, Int reg2 );
sewardjfa492d42002-12-08 18:20:01 +00001171extern void VG_(emit_nonshiftopv_lit_reg)( Bool upd_cc, Int sz, Opcode opc, UInt lit,
njn4ba5a792002-09-30 10:23:54 +00001172 Int reg );
sewardjfa492d42002-12-08 18:20:01 +00001173extern void VG_(emit_shiftopv_lit_reg) ( Bool upd_cc, Int sz, Opcode opc, UInt lit,
njn4ba5a792002-09-30 10:23:54 +00001174 Int reg );
sewardjfa492d42002-12-08 18:20:01 +00001175extern void VG_(emit_nonshiftopv_reg_reg)( Bool upd_cc, Int sz, Opcode opc,
njn4ba5a792002-09-30 10:23:54 +00001176 Int reg1, Int reg2 );
1177extern void VG_(emit_movv_lit_reg) ( Int sz, UInt lit, Int reg );
sewardjfa492d42002-12-08 18:20:01 +00001178extern void VG_(emit_unaryopv_reg) ( Bool upd_cc, Int sz, Opcode opc, Int reg );
njn4ba5a792002-09-30 10:23:54 +00001179extern void VG_(emit_pushv_reg) ( Int sz, Int reg );
1180extern void VG_(emit_popv_reg) ( Int sz, Int reg );
njn25e49d8e72002-09-23 09:36:25 +00001181
njn4ba5a792002-09-30 10:23:54 +00001182extern void VG_(emit_pushl_lit32) ( UInt int32 );
1183extern void VG_(emit_pushl_lit8) ( Int lit8 );
sewardjfa492d42002-12-08 18:20:01 +00001184extern void VG_(emit_cmpl_zero_reg) ( Bool upd_cc, Int reg );
njn4ba5a792002-09-30 10:23:54 +00001185extern void VG_(emit_swapl_reg_EAX) ( Int reg );
1186extern void VG_(emit_movv_lit_offregmem) ( Int sz, UInt lit, Int off,
1187 Int memreg );
njn25e49d8e72002-09-23 09:36:25 +00001188
1189/* b-size (1 byte) instruction emitters */
njn4ba5a792002-09-30 10:23:54 +00001190extern void VG_(emit_movb_lit_offregmem) ( UInt lit, Int off, Int memreg );
1191extern void VG_(emit_movb_reg_offregmem) ( Int reg, Int off, Int areg );
sewardjfa492d42002-12-08 18:20:01 +00001192extern void VG_(emit_unaryopb_reg) ( Bool upd_cc, Opcode opc, Int reg );
1193extern void VG_(emit_testb_lit_reg) ( Bool upd_cc, UInt lit, Int reg );
njn25e49d8e72002-09-23 09:36:25 +00001194
1195/* zero-extended load emitters */
njn4ba5a792002-09-30 10:23:54 +00001196extern void VG_(emit_movzbl_offregmem_reg) ( Int off, Int regmem, Int reg );
1197extern void VG_(emit_movzwl_offregmem_reg) ( Int off, Int areg, Int reg );
njn25e49d8e72002-09-23 09:36:25 +00001198
1199/* misc instruction emitters */
njn4ba5a792002-09-30 10:23:54 +00001200extern void VG_(emit_call_reg) ( Int reg );
1201extern void VG_(emit_add_lit_to_esp) ( Int lit );
njn4ba5a792002-09-30 10:23:54 +00001202extern void VG_(emit_pushal) ( void );
1203extern void VG_(emit_popal) ( void );
1204extern void VG_(emit_AMD_prefetch_reg) ( Int reg );
njn25e49d8e72002-09-23 09:36:25 +00001205
sewardja2113f92002-12-12 23:42:48 +00001206/* jump emitters */
1207extern void VG_(init_target) ( Int *tgt );
1208
1209extern void VG_(target_back) ( Int *tgt );
1210extern void VG_(target_forward) ( Int *tgt );
1211extern void VG_(emit_target_delta) ( Int *tgt );
1212
1213extern void VG_(emit_jcondshort_delta) ( Bool simd_cc, Condcode cond, Int delta );
1214extern void VG_(emit_jcondshort_target)( Bool simd_cc, Condcode cond, Int *tgt );
1215
njn25e49d8e72002-09-23 09:36:25 +00001216
1217/*====================================================================*/
1218/*=== Execution contexts ===*/
1219/*====================================================================*/
1220
1221/* Generic resolution type used in a few different ways, such as deciding
1222 how closely to compare two errors for equality. */
1223typedef
1224 enum { Vg_LowRes, Vg_MedRes, Vg_HighRes }
1225 VgRes;
1226
1227typedef
1228 struct _ExeContext
1229 ExeContext;
1230
njnd5bb0a52002-09-27 10:24:48 +00001231/* Compare two ExeContexts. Number of callers considered depends on `res':
1232 Vg_LowRes: 2
1233 Vg_MedRes: 4
1234 Vg_HighRes: all */
njn25e49d8e72002-09-23 09:36:25 +00001235extern Bool VG_(eq_ExeContext) ( VgRes res,
1236 ExeContext* e1, ExeContext* e2 );
1237
1238/* Print an ExeContext. */
1239extern void VG_(pp_ExeContext) ( ExeContext* );
1240
1241/* Take a snapshot of the client's stack. Search our collection of
1242 ExeContexts to see if we already have it, and if not, allocate a
njn7b456f52003-05-19 11:16:50 +00001243 new one. Either way, return a pointer to the context.
1244
1245 If called from generated code, `tst' can be NULL and it will use the
1246 ThreadState of the current thread. If called from elsewhere, `tst'
1247 should not be NULL.
1248*/
njn25e49d8e72002-09-23 09:36:25 +00001249extern ExeContext* VG_(get_ExeContext) ( ThreadState *tst );
1250
sewardj499e3de2002-11-13 22:22:25 +00001251/* Just grab the client's EIP, as a much smaller and cheaper
njnd3040452003-05-19 15:04:06 +00001252 indication of where they are. ThreadState should be NULL if it's called
1253 from within generated code. */
sewardj499e3de2002-11-13 22:22:25 +00001254extern Addr VG_(get_EIP)( ThreadState *tst );
njn25e49d8e72002-09-23 09:36:25 +00001255
njn211b6ad2003-02-03 12:33:31 +00001256
njn25e49d8e72002-09-23 09:36:25 +00001257/*====================================================================*/
1258/*=== Error reporting ===*/
1259/*====================================================================*/
1260
1261/* ------------------------------------------------------------------ */
1262/* Suppressions describe errors which we want to suppress, ie, not
1263 show the user, usually because it is caused by a problem in a library
1264 which we can't fix, replace or work around. Suppressions are read from
njnd5bb0a52002-09-27 10:24:48 +00001265 a file at startup time. This gives flexibility so that new
njn25e49d8e72002-09-23 09:36:25 +00001266 suppressions can be added to the file as and when needed.
1267*/
1268
1269typedef
1270 Int /* Do not make this unsigned! */
1271 SuppKind;
1272
njn810086f2002-11-14 12:42:47 +00001273/* The skin-relevant parts of a suppression are:
1274 kind: what kind of suppression; must be in the range (0..)
1275 string: use is optional. NULL by default.
1276 extra: use is optional. NULL by default. void* so it's extensible.
njn25e49d8e72002-09-23 09:36:25 +00001277*/
1278typedef
njn810086f2002-11-14 12:42:47 +00001279 struct _Supp
1280 Supp;
1281
1282/* Useful in SK_(error_matches_suppression)() */
1283SuppKind VG_(get_supp_kind) ( Supp* su );
1284Char* VG_(get_supp_string) ( Supp* su );
1285void* VG_(get_supp_extra) ( Supp* su );
1286
1287/* Must be used in VG_(recognised_suppression)() */
1288void VG_(set_supp_kind) ( Supp* su, SuppKind suppkind );
1289/* May be used in VG_(read_extra_suppression_info)() */
1290void VG_(set_supp_string) ( Supp* su, Char* string );
1291void VG_(set_supp_extra) ( Supp* su, void* extra );
njn25e49d8e72002-09-23 09:36:25 +00001292
1293
1294/* ------------------------------------------------------------------ */
1295/* Error records contain enough info to generate an error report. The idea
1296 is that (typically) the same few points in the program generate thousands
njnd5bb0a52002-09-27 10:24:48 +00001297 of errors, and we don't want to spew out a fresh error message for each
1298 one. Instead, we use these structures to common up duplicates.
njn25e49d8e72002-09-23 09:36:25 +00001299*/
1300
1301typedef
1302 Int /* Do not make this unsigned! */
1303 ErrorKind;
1304
njn810086f2002-11-14 12:42:47 +00001305/* The skin-relevant parts of an Error are:
1306 kind: what kind of error; must be in the range (0..)
1307 addr: use is optional. 0 by default.
1308 string: use is optional. NULL by default.
1309 extra: use is optional. NULL by default. void* so it's extensible.
njn25e49d8e72002-09-23 09:36:25 +00001310*/
1311typedef
njn810086f2002-11-14 12:42:47 +00001312 struct _Error
1313 Error;
njn25e49d8e72002-09-23 09:36:25 +00001314
njn810086f2002-11-14 12:42:47 +00001315/* Useful in SK_(error_matches_suppression)(), SK_(pp_SkinError)(), etc */
njnb877d492003-01-28 20:40:57 +00001316ExeContext* VG_(get_error_where) ( Error* err );
1317SuppKind VG_(get_error_kind) ( Error* err );
1318Addr VG_(get_error_address) ( Error* err );
1319Char* VG_(get_error_string) ( Error* err );
1320void* VG_(get_error_extra) ( Error* err );
njn25e49d8e72002-09-23 09:36:25 +00001321
njnd5bb0a52002-09-27 10:24:48 +00001322/* Call this when an error occurs. It will be recorded if it hasn't been
njn25e49d8e72002-09-23 09:36:25 +00001323 seen before. If it has, the existing error record will have its count
1324 incremented.
1325
1326 If the error occurs in generated code, 'tst' should be NULL. If the
1327 error occurs in non-generated code, 'tst' should be non-NULL. The
njn43c799e2003-04-08 00:08:52 +00001328 `extra' field can be stack-allocated; it will be copied by the core
1329 if needed. But it won't be copied if it's NULL.
njnd5bb0a52002-09-27 10:24:48 +00001330
1331 If no 'a', 's' or 'extra' of interest needs to be recorded, just use
njn3e884182003-04-15 13:03:23 +00001332 NULL for them. */
njn25e49d8e72002-09-23 09:36:25 +00001333extern void VG_(maybe_record_error) ( ThreadState* tst, ErrorKind ekind,
1334 Addr a, Char* s, void* extra );
1335
njn43c799e2003-04-08 00:08:52 +00001336/* Similar to VG_(maybe_record_error)(), except this one doesn't record the
1337 error -- useful for errors that can only happen once. The errors can be
1338 suppressed, though. Return value is True if it was suppressed.
1339 `print_error' dictates whether to print the error, which is a bit of a
1340 hack that's useful sometimes if you just want to know if the error would
njn47363ab2003-04-21 13:24:40 +00001341 be suppressed without possibly printing it. `count_error' dictates
1342 whether to add the error in the error total count (another mild hack). */
njn43c799e2003-04-08 00:08:52 +00001343extern Bool VG_(unique_error) ( ThreadState* tst, ErrorKind ekind,
1344 Addr a, Char* s, void* extra,
njn3e884182003-04-15 13:03:23 +00001345 ExeContext* where, Bool print_error,
njn47363ab2003-04-21 13:24:40 +00001346 Bool allow_GDB_attach, Bool count_error );
njn43c799e2003-04-08 00:08:52 +00001347
njn25e49d8e72002-09-23 09:36:25 +00001348/* Gets a non-blank, non-comment line of at most nBuf chars from fd.
1349 Skips leading spaces on the line. Returns True if EOF was hit instead.
njn3e884182003-04-15 13:03:23 +00001350 Useful for reading in extra skin-specific suppression lines. */
njn4ba5a792002-09-30 10:23:54 +00001351extern Bool VG_(get_line) ( Int fd, Char* buf, Int nBuf );
njn25e49d8e72002-09-23 09:36:25 +00001352
1353
1354/*====================================================================*/
1355/*=== Obtaining debug information ===*/
1356/*====================================================================*/
1357
sewardj6e008cb2002-12-15 13:11:39 +00001358/* Get the file/function/line number of the instruction at address
1359 'a'. For these four, if debug info for the address is found, it
1360 copies the info into the buffer/UInt and returns True. If not, it
1361 returns False and nothing is copied. VG_(get_fnname) always
1362 demangles C++ function names. VG_(get_fnname_w_offset) is the
njn3e884182003-04-15 13:03:23 +00001363 same, except it appends "+N" to symbol names to indicate offsets. */
njn25e49d8e72002-09-23 09:36:25 +00001364extern Bool VG_(get_filename) ( Addr a, Char* filename, Int n_filename );
1365extern Bool VG_(get_fnname) ( Addr a, Char* fnname, Int n_fnname );
1366extern Bool VG_(get_linenum) ( Addr a, UInt* linenum );
sewardj6e008cb2002-12-15 13:11:39 +00001367extern Bool VG_(get_fnname_w_offset)
1368 ( Addr a, Char* fnname, Int n_fnname );
njn25e49d8e72002-09-23 09:36:25 +00001369
1370/* This one is more efficient if getting both filename and line number,
1371 because the two lookups are done together. */
1372extern Bool VG_(get_filename_linenum)
1373 ( Addr a, Char* filename, Int n_filename,
1374 UInt* linenum );
1375
1376/* Succeeds only if we find from debug info that 'a' is the address of the
1377 first instruction in a function -- as opposed to VG_(get_fnname) which
1378 succeeds if we find from debug info that 'a' is the address of any
1379 instruction in a function. Use this to instrument the start of
njnd5bb0a52002-09-27 10:24:48 +00001380 a particular function. Nb: if an executable/shared object is stripped
njn25e49d8e72002-09-23 09:36:25 +00001381 of its symbols, this function will not be able to recognise function
1382 entry points within it. */
njn497ec3f2003-05-19 08:38:23 +00001383extern Bool VG_(get_fnname_if_entry) ( Addr a, Char* fnname, Int n_fnname );
njn25e49d8e72002-09-23 09:36:25 +00001384
1385/* Succeeds if the address is within a shared object or the main executable.
1386 It doesn't matter if debug info is present or not. */
1387extern Bool VG_(get_objname) ( Addr a, Char* objname, Int n_objname );
1388
sewardj47104382002-10-20 18:35:48 +00001389/* A way to get information about what segments are mapped */
1390typedef struct _SegInfo SegInfo;
1391
njnb877d492003-01-28 20:40:57 +00001392/* Returns NULL if the SegInfo isn't found. It doesn't matter if debug info
1393 is present or not. */
1394extern SegInfo* VG_(get_obj) ( Addr a );
1395
1396extern const SegInfo* VG_(next_seginfo) ( const SegInfo *seg );
1397extern Addr VG_(seg_start) ( const SegInfo *seg );
1398extern UInt VG_(seg_size) ( const SegInfo *seg );
1399extern const UChar* VG_(seg_filename) ( const SegInfo *seg );
1400extern UInt VG_(seg_sym_offset)( const SegInfo *seg );
sewardj47104382002-10-20 18:35:48 +00001401
1402typedef
1403 enum {
1404 Vg_SectUnknown,
1405 Vg_SectText,
sewardj8fe15a32002-10-20 19:29:21 +00001406 Vg_SectData,
1407 Vg_SectBSS,
sewardj47104382002-10-20 18:35:48 +00001408 Vg_SectGOT,
sewardj8fe15a32002-10-20 19:29:21 +00001409 Vg_SectPLT,
sewardj47104382002-10-20 18:35:48 +00001410 }
1411 VgSectKind;
1412
1413extern VgSectKind VG_(seg_sect_kind)(Addr);
1414
njn25e49d8e72002-09-23 09:36:25 +00001415
1416/*====================================================================*/
njn4c791212003-05-02 17:53:54 +00001417/*=== Calling functions from the sim'd CPU ===*/
1418/*====================================================================*/
1419
1420#define VG_USERREQ__CLIENT_tstCALL0 0x2101
1421#define VG_USERREQ__CLIENT_tstCALL1 0x2102
1422#define VG_USERREQ__CLIENT_tstCALL2 0x2103
1423#define VG_USERREQ__CLIENT_tstCALL3 0x2104
1424
1425/* These requests are like VALGRIND_NON_SIMD_CALL[0123] in valgrind.h,
1426 except they insert the current ThreadState as the first argument to the
1427 called function. */
1428#define VALGRIND_NON_SIMD_tstCALL0(_qyy_fn) \
1429 ({unsigned int _qyy_res; \
1430 VALGRIND_MAGIC_SEQUENCE(_qyy_res, 0 /* default return */, \
1431 VG_USERREQ__CLIENT_tstCALL0, \
1432 _qyy_fn, \
1433 0, 0, 0); \
1434 _qyy_res; \
1435 })
1436
1437#define VALGRIND_NON_SIMD_tstCALL1(_qyy_fn, _qyy_arg1) \
1438 ({unsigned int _qyy_res; \
1439 VALGRIND_MAGIC_SEQUENCE(_qyy_res, 0 /* default return */, \
1440 VG_USERREQ__CLIENT_tstCALL1, \
1441 _qyy_fn, \
1442 _qyy_arg1, 0, 0); \
1443 _qyy_res; \
1444 })
1445
1446#define VALGRIND_NON_SIMD_tstCALL2(_qyy_fn, _qyy_arg1, _qyy_arg2) \
1447 ({unsigned int _qyy_res; \
1448 VALGRIND_MAGIC_SEQUENCE(_qyy_res, 0 /* default return */, \
1449 VG_USERREQ__CLIENT_tstCALL2, \
1450 _qyy_fn, \
1451 _qyy_arg1, _qyy_arg2, 0); \
1452 _qyy_res; \
1453 })
1454
1455#define VALGRIND_NON_SIMD_tstCALL3(_qyy_fn, _qyy_arg1, _qyy_arg2, _qyy_arg3) \
1456 ({unsigned int _qyy_res; \
1457 VALGRIND_MAGIC_SEQUENCE(_qyy_res, 0 /* default return */, \
1458 VG_USERREQ__CLIENT_tstCALL3, \
1459 _qyy_fn, \
1460 _qyy_arg1, _qyy_arg2, _qyy_arg3); \
1461 _qyy_res; \
1462 })
1463
1464
1465/*====================================================================*/
njn3e884182003-04-15 13:03:23 +00001466/*=== Generic hash table ===*/
njn25e49d8e72002-09-23 09:36:25 +00001467/*====================================================================*/
1468
njn3e884182003-04-15 13:03:23 +00001469/* Generic type for a separately-chained hash table. Via a kind of dodgy
1470 C-as-C++ style inheritance, skins can extend the VgHashNode type, so long
1471 as the first two fields match the sizes of these two fields. Requires
1472 a bit of casting by the skin. */
njn25e49d8e72002-09-23 09:36:25 +00001473typedef
njn3e884182003-04-15 13:03:23 +00001474 struct _VgHashNode {
1475 struct _VgHashNode * next;
1476 UInt key;
1477 }
1478 VgHashNode;
njn25e49d8e72002-09-23 09:36:25 +00001479
njn3e884182003-04-15 13:03:23 +00001480typedef
1481 VgHashNode**
1482 VgHashTable;
njn810086f2002-11-14 12:42:47 +00001483
njn3e884182003-04-15 13:03:23 +00001484/* Make a new table. */
1485extern VgHashTable VG_(HT_construct) ( void );
1486
1487/* Add a node to the table. */
1488extern void VG_(HT_add_node) ( VgHashTable t, VgHashNode* node );
1489
1490/* Looks up a node in the hash table. Also returns the address of the
1491 previous node's `next' pointer which allows it to be removed from the
1492 list later without having to look it up again. */
1493extern VgHashNode* VG_(HT_get_node) ( VgHashTable t, UInt key,
1494 /*OUT*/VgHashNode*** next_ptr );
1495
1496/* Allocates a sorted array of pointers to all the shadow chunks of malloc'd
1497 blocks. */
1498extern VgHashNode** VG_(HT_to_sorted_array) ( VgHashTable t,
1499 /*OUT*/ UInt* n_shadows );
1500
1501/* Returns first node that matches predicate `p', or NULL if none do.
1502 Extra arguments can be implicitly passed to `p' using nested functions;
1503 see memcheck/mc_errcontext.c for an example. */
1504extern VgHashNode* VG_(HT_first_match) ( VgHashTable t,
1505 Bool (*p)(VgHashNode*) );
1506
1507/* Applies a function f() once to each node. Again, nested functions
1508 can be very useful. */
1509extern void VG_(HT_apply_to_all_nodes)( VgHashTable t, void (*f)(VgHashNode*) );
1510
1511/* Destroy a table. */
1512extern void VG_(HT_destruct) ( VgHashTable t );
njn810086f2002-11-14 12:42:47 +00001513
1514
njn3e884182003-04-15 13:03:23 +00001515/*====================================================================*/
njnd3040452003-05-19 15:04:06 +00001516/*=== Functions for shadow registers ===*/
1517/*====================================================================*/
1518
1519/* Nb: make sure the shadow_regs 'need' is set before using these! */
1520
1521/* This one lets you override the shadow of the return value register for a
1522 syscall. Call it from SK_(post_syscall)() (not SK_(pre_syscall)()!) to
1523 override the default shadow register value. */
1524extern void VG_(set_return_from_syscall_shadow) ( ThreadId tid,
1525 UInt ret_shadow );
1526
1527/* This can be called from SK_(fini)() to find the shadow of the argument
1528 to exit(), ie. the shadow of the program's return value. */
1529extern UInt VG_(get_exit_status_shadow) ( void );
1530
1531
1532/*====================================================================*/
njn3e884182003-04-15 13:03:23 +00001533/*=== General stuff for replacing functions ===*/
1534/*====================================================================*/
njn25e49d8e72002-09-23 09:36:25 +00001535
njn3e884182003-04-15 13:03:23 +00001536/* Some skins need to replace the standard definitions of some functions. */
njn25e49d8e72002-09-23 09:36:25 +00001537
njn3e884182003-04-15 13:03:23 +00001538/* ------------------------------------------------------------------ */
1539/* General stuff, for replacing any functions */
njn25e49d8e72002-09-23 09:36:25 +00001540
njn3e884182003-04-15 13:03:23 +00001541/* Is the client running on the simulated CPU or the real one?
njn25e49d8e72002-09-23 09:36:25 +00001542
njn3e884182003-04-15 13:03:23 +00001543 Nb: If it is, and you want to call a function to be run on the real CPU,
njn057c65f2003-04-21 13:30:55 +00001544 use one of the VALGRIND_NON_SIMD_CALL[123] macros in valgrind.h to call it.
njn25e49d8e72002-09-23 09:36:25 +00001545
njn3e884182003-04-15 13:03:23 +00001546 Nb: don't forget the function parentheses when using this in a
1547 condition... write this:
1548
1549 if (VG_(is_running_on_simd_CPU)()) { ... } // calls function
1550
1551 not this:
1552
1553 if (VG_(is_running_on_simd_CPU)) { ... } // address of var!
1554*/
1555extern Bool VG_(is_running_on_simd_CPU) ( void );
1556
1557
1558/*====================================================================*/
1559/*=== Specific stuff for replacing malloc() and friends ===*/
1560/*====================================================================*/
1561
njn3e884182003-04-15 13:03:23 +00001562/* If a skin replaces malloc() et al, the easiest way to do so is to link
1563 with coregrind/vg_replace_malloc.c, and follow the following instructions.
1564 You can do it from scratch, though, if you enjoy that sort of thing. */
1565
1566/* Arena size for valgrind's own malloc(); default value is 0, but can
1567 be overridden by skin -- but must be done so *statically*, eg:
1568
1569 Int VG_(vg_malloc_redzone_szB) = 4;
1570
1571 It can't be done from a function like SK_(pre_clo_init)(). So it can't,
1572 for example, be controlled with a command line option, unfortunately. */
1573extern UInt VG_(vg_malloc_redzone_szB);
1574
1575/* If a skin links with vg_replace_malloc.c, the following functions will be
1576 called appropriately when malloc() et al are called. */
1577extern void* SK_(malloc) ( ThreadState* tst, Int n );
1578extern void* SK_(__builtin_new) ( ThreadState* tst, Int n );
1579extern void* SK_(__builtin_vec_new) ( ThreadState* tst, Int n );
1580extern void* SK_(memalign) ( ThreadState* tst, Int align, Int n );
1581extern void* SK_(calloc) ( ThreadState* tst, Int nmemb, Int n );
1582extern void SK_(free) ( ThreadState* tst, void* p );
1583extern void SK_(__builtin_delete) ( ThreadState* tst, void* p );
1584extern void SK_(__builtin_vec_delete) ( ThreadState* tst, void* p );
1585extern void* SK_(realloc) ( ThreadState* tst, void* p, Int size );
1586
1587/* Can be called from SK_(malloc) et al to do the actual alloc/freeing. */
1588extern void* VG_(cli_malloc) ( UInt align, Int nbytes );
1589extern void VG_(cli_free) ( void* p );
1590
1591/* Check if an address is within a range, allowing for redzones at edges */
1592extern Bool VG_(addr_is_in_block)( Addr a, Addr start, UInt size );
1593
1594/* ------------------------------------------------------------------ */
1595/* Some options that can be used by a skin if malloc() et al are replaced.
njnd3040452003-05-19 15:04:06 +00001596 The skin should call the functions in the appropriate places to give
1597 control over these aspects of Valgrind's version of malloc(). */
njn3e884182003-04-15 13:03:23 +00001598
1599/* Round malloc sizes upwards to integral number of words? default: NO */
1600extern Bool VG_(clo_sloppy_malloc);
1601/* DEBUG: print malloc details? default: NO */
1602extern Bool VG_(clo_trace_malloc);
1603/* Minimum alignment in functions that don't specify alignment explicitly.
1604 default: 0, i.e. use default of the machine (== 4) */
1605extern Int VG_(clo_alignment);
1606
1607extern Bool VG_(replacement_malloc_process_cmd_line_option) ( Char* arg );
1608extern void VG_(replacement_malloc_print_usage) ( void );
1609extern void VG_(replacement_malloc_print_debug_usage) ( void );
sewardja4495682002-10-21 07:29:59 +00001610
1611
njn25e49d8e72002-09-23 09:36:25 +00001612/*====================================================================*/
1613/*=== Skin-specific stuff ===*/
1614/*====================================================================*/
1615
njnd04b7c62002-10-03 14:05:52 +00001616/* ------------------------------------------------------------------ */
1617/* Details */
njnd04b7c62002-10-03 14:05:52 +00001618
njn120281f2003-02-03 12:20:07 +00001619/* Default value for avg_translations_sizeB (in bytes), indicating typical
1620 code expansion of about 6:1. */
1621#define VG_DEFAULT_TRANS_SIZEB 100
1622
njn810086f2002-11-14 12:42:47 +00001623/* Information used in the startup message. `name' also determines the
1624 string used for identifying suppressions in a suppression file as
1625 belonging to this skin. `version' can be NULL, in which case (not
1626 surprisingly) no version info is printed; this mechanism is designed for
1627 skins distributed with Valgrind that share a version number with
1628 Valgrind. Other skins not distributed as part of Valgrind should
sewardjc0d8f682002-11-30 00:49:43 +00001629 probably have their own version number. */
1630extern void VG_(details_name) ( Char* name );
1631extern void VG_(details_version) ( Char* version );
1632extern void VG_(details_description) ( Char* description );
1633extern void VG_(details_copyright_author) ( Char* copyright_author );
1634
1635/* Average size of a translation, in bytes, so that the translation
njn120281f2003-02-03 12:20:07 +00001636 storage machinery can allocate memory appropriately. Not critical,
1637 setting is optional. */
1638extern void VG_(details_avg_translation_sizeB) ( UInt size );
njnd04b7c62002-10-03 14:05:52 +00001639
njn810086f2002-11-14 12:42:47 +00001640/* String printed if an `sk_assert' assertion fails or VG_(skin_panic)
1641 is called. Should probably be an email address. */
1642extern void VG_(details_bug_reports_to) ( Char* bug_reports_to );
njnd04b7c62002-10-03 14:05:52 +00001643
1644/* ------------------------------------------------------------------ */
1645/* Needs */
1646
njn810086f2002-11-14 12:42:47 +00001647/* Booleans that decide core behaviour, but don't require extra
1648 operations to be defined if `True' */
njnd5bb0a52002-09-27 10:24:48 +00001649
njn810086f2002-11-14 12:42:47 +00001650/* Should __libc_freeres() be run? Bugs in it can crash the skin. */
1651extern void VG_(needs_libc_freeres) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001652
njn810086f2002-11-14 12:42:47 +00001653/* Want to have errors detected by Valgrind's core reported? Includes:
1654 - pthread API errors (many; eg. unlocking a non-locked mutex)
njn810086f2002-11-14 12:42:47 +00001655 - invalid file descriptors to blocking syscalls read() and write()
1656 - bad signal numbers passed to sigaction()
1657 - attempt to install signal handler for SIGKILL or SIGSTOP */
1658extern void VG_(needs_core_errors) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001659
njn810086f2002-11-14 12:42:47 +00001660/* Booleans that indicate extra operations are defined; if these are True,
1661 the corresponding template functions (given below) must be defined. A
1662 lot like being a member of a type class. */
njn25e49d8e72002-09-23 09:36:25 +00001663
njn810086f2002-11-14 12:42:47 +00001664/* Want to report errors from skin? This implies use of suppressions, too. */
1665extern void VG_(needs_skin_errors) ( void );
njnd5bb0a52002-09-27 10:24:48 +00001666
njn810086f2002-11-14 12:42:47 +00001667/* Is information kept about specific individual basic blocks? (Eg. for
1668 cachegrind there are cost-centres for every instruction, stored at a
1669 basic block level.) If so, it sometimes has to be discarded, because
1670 .so mmap/munmap-ping or self-modifying code (informed by the
1671 DISCARD_TRANSLATIONS user request) can cause one instruction address
1672 to be used for more than one instruction in one program run... */
1673extern void VG_(needs_basic_block_discards) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001674
njn810086f2002-11-14 12:42:47 +00001675/* Skin maintains information about each register? */
1676extern void VG_(needs_shadow_regs) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001677
njn810086f2002-11-14 12:42:47 +00001678/* Skin defines its own command line options? */
1679extern void VG_(needs_command_line_options) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001680
njn810086f2002-11-14 12:42:47 +00001681/* Skin defines its own client requests? */
1682extern void VG_(needs_client_requests) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001683
njn810086f2002-11-14 12:42:47 +00001684/* Skin defines its own UInstrs? */
1685extern void VG_(needs_extended_UCode) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001686
njn810086f2002-11-14 12:42:47 +00001687/* Skin does stuff before and/or after system calls? */
1688extern void VG_(needs_syscall_wrapper) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001689
njn810086f2002-11-14 12:42:47 +00001690/* Are skin-state sanity checks performed? */
1691extern void VG_(needs_sanity_checks) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001692
njn810086f2002-11-14 12:42:47 +00001693/* Do we need to see data symbols? */
1694extern void VG_(needs_data_syms) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001695
1696/* ------------------------------------------------------------------ */
1697/* Core events to track */
1698
1699/* Part of the core from which this call was made. Useful for determining
njnd5bb0a52002-09-27 10:24:48 +00001700 what kind of error message should be emitted. */
njn25e49d8e72002-09-23 09:36:25 +00001701typedef
1702 enum { Vg_CorePThread, Vg_CoreSignal, Vg_CoreSysCall, Vg_CoreTranslate }
1703 CorePart;
1704
njn810086f2002-11-14 12:42:47 +00001705#define EV extern void
njn25e49d8e72002-09-23 09:36:25 +00001706
njn810086f2002-11-14 12:42:47 +00001707/* Events happening in core to track. To be notified, pass a callback
1708 function to the appropriate function. To ignore an event, don't do
1709 anything (default is for events to be ignored). */
1710
njn810086f2002-11-14 12:42:47 +00001711
njn3e884182003-04-15 13:03:23 +00001712/* Memory events (Nb: to track heap allocation/freeing, a skin must replace
1713 malloc() et al. See above how to do this.) */
1714
1715/* These ones occur at startup, upon some signals, and upon some syscalls */
njn810086f2002-11-14 12:42:47 +00001716EV VG_(track_new_mem_startup) ( void (*f)(Addr a, UInt len,
1717 Bool rr, Bool ww, Bool xx) );
njn810086f2002-11-14 12:42:47 +00001718EV VG_(track_new_mem_stack_signal) ( void (*f)(Addr a, UInt len) );
1719EV VG_(track_new_mem_brk) ( void (*f)(Addr a, UInt len) );
1720EV VG_(track_new_mem_mmap) ( void (*f)(Addr a, UInt len,
1721 Bool rr, Bool ww, Bool xx) );
1722
njn3e884182003-04-15 13:03:23 +00001723EV VG_(track_copy_mem_remap) ( void (*f)(Addr from, Addr to, UInt len) );
1724EV VG_(track_change_mem_mprotect) ( void (*f)(Addr a, UInt len,
1725 Bool rr, Bool ww, Bool xx) );
1726EV VG_(track_die_mem_stack_signal) ( void (*f)(Addr a, UInt len) );
1727EV VG_(track_die_mem_brk) ( void (*f)(Addr a, UInt len) );
1728EV VG_(track_die_mem_munmap) ( void (*f)(Addr a, UInt len) );
1729
1730
1731/* These ones are called when %esp changes. A skin could track these itself
1732 (except for ban_mem_stack) but it's much easier to use the core's help.
1733
1734 The specialised ones are called in preference to the general one, if they
njn9b007f62003-04-07 14:40:25 +00001735 are defined. These functions are called a lot if they are used, so
1736 specialising can optimise things significantly. If any of the
1737 specialised cases are defined, the general case must be defined too.
1738
1739 Nb: they must all use the __attribute__((regparm(n))) attribute. */
1740EV VG_(track_new_mem_stack_4) ( void (*f)(Addr new_ESP) );
1741EV VG_(track_new_mem_stack_8) ( void (*f)(Addr new_ESP) );
1742EV VG_(track_new_mem_stack_12) ( void (*f)(Addr new_ESP) );
1743EV VG_(track_new_mem_stack_16) ( void (*f)(Addr new_ESP) );
1744EV VG_(track_new_mem_stack_32) ( void (*f)(Addr new_ESP) );
1745EV VG_(track_new_mem_stack) ( void (*f)(Addr a, UInt len) );
1746
njn9b007f62003-04-07 14:40:25 +00001747EV VG_(track_die_mem_stack_4) ( void (*f)(Addr die_ESP) );
1748EV VG_(track_die_mem_stack_8) ( void (*f)(Addr die_ESP) );
1749EV VG_(track_die_mem_stack_12) ( void (*f)(Addr die_ESP) );
1750EV VG_(track_die_mem_stack_16) ( void (*f)(Addr die_ESP) );
1751EV VG_(track_die_mem_stack_32) ( void (*f)(Addr die_ESP) );
1752EV VG_(track_die_mem_stack) ( void (*f)(Addr a, UInt len) );
1753
njn3e884182003-04-15 13:03:23 +00001754/* Used for redzone at end of thread stacks */
1755EV VG_(track_ban_mem_stack) ( void (*f)(Addr a, UInt len) );
njn25e49d8e72002-09-23 09:36:25 +00001756
njn3e884182003-04-15 13:03:23 +00001757/* These ones occur around syscalls, signal handling, etc */
njn810086f2002-11-14 12:42:47 +00001758EV VG_(track_pre_mem_read) ( void (*f)(CorePart part, ThreadState* tst,
1759 Char* s, Addr a, UInt size) );
1760EV VG_(track_pre_mem_read_asciiz) ( void (*f)(CorePart part, ThreadState* tst,
1761 Char* s, Addr a) );
1762EV VG_(track_pre_mem_write) ( void (*f)(CorePart part, ThreadState* tst,
1763 Char* s, Addr a, UInt size) );
1764/* Not implemented yet -- have to add in lots of places, which is a
1765 pain. Won't bother unless/until there's a need. */
1766/* EV VG_(track_post_mem_read) ( void (*f)(ThreadState* tst, Char* s,
1767 Addr a, UInt size) ); */
1768EV VG_(track_post_mem_write) ( void (*f)(Addr a, UInt size) );
njn25e49d8e72002-09-23 09:36:25 +00001769
1770
njnd3040452003-05-19 15:04:06 +00001771/* Register events -- if `shadow_regs' need is set, all should probably be
1772 used. Use VG_(set_thread_shadow_archreg)() to set the shadow of the
1773 changed register. */
1774
1775/* Use VG_(set_shadow_archreg)() to set the eight general purpose regs,
1776 and use VG_(set_shadow_eflags)() to set eflags. */
1777EV VG_(track_post_regs_write_init) ( void (*f)() );
1778
1779/* Use VG_(set_thread_shadow_archreg)() to set the shadow regs for these
1780 events. */
1781EV VG_(track_post_reg_write_syscall_return)
1782 ( void (*f)(ThreadId tid, UInt reg) );
1783EV VG_(track_post_reg_write_deliver_signal)
1784 ( void (*f)(ThreadId tid, UInt reg) );
1785EV VG_(track_post_reg_write_pthread_return)
1786 ( void (*f)(ThreadId tid, UInt reg) );
1787EV VG_(track_post_reg_write_clientreq_return)
1788 ( void (*f)(ThreadId tid, UInt reg) );
1789 /* This one is called for malloc() et al if they are replaced by a skin. */
1790EV VG_(track_post_reg_write_clientcall_return)
1791 ( void (*f)(ThreadId tid, UInt reg,
1792 Addr called_function) );
1793
1794
njn810086f2002-11-14 12:42:47 +00001795/* Scheduler events (not exhaustive) */
njn25e49d8e72002-09-23 09:36:25 +00001796
njn810086f2002-11-14 12:42:47 +00001797EV VG_(track_thread_run) ( void (*f)(ThreadId tid) );
njn25e49d8e72002-09-23 09:36:25 +00001798
njn810086f2002-11-14 12:42:47 +00001799/* Thread events (not exhaustive) */
sewardjdca84112002-11-13 22:29:34 +00001800
njn810086f2002-11-14 12:42:47 +00001801/* Called during thread create, before the new thread has run any
1802 instructions (or touched any memory). */
1803EV VG_(track_post_thread_create)( void (*f)(ThreadId tid, ThreadId child) );
1804/* Called once the joinee thread is terminated and the joining thread is
1805 about to resume. */
1806EV VG_(track_post_thread_join) ( void (*f)(ThreadId joiner, ThreadId joinee) );
njn25e49d8e72002-09-23 09:36:25 +00001807
njnd5bb0a52002-09-27 10:24:48 +00001808
njn810086f2002-11-14 12:42:47 +00001809/* Mutex events (not exhaustive) */
njn25e49d8e72002-09-23 09:36:25 +00001810
njn810086f2002-11-14 12:42:47 +00001811/* Called before a thread can block while waiting for a mutex (called
1812 regardless of whether the thread will block or not). */
1813EV VG_(track_pre_mutex_lock) ( void (*f)(ThreadId tid,
1814 void* /*pthread_mutex_t* */ mutex) );
1815/* Called once the thread actually holds the mutex (always paired with
1816 pre_mutex_lock). */
1817EV VG_(track_post_mutex_lock) ( void (*f)(ThreadId tid,
1818 void* /*pthread_mutex_t* */ mutex) );
1819/* Called after a thread has released a mutex (no need for a corresponding
1820 pre_mutex_unlock, because unlocking can't block). */
1821EV VG_(track_post_mutex_unlock) ( void (*f)(ThreadId tid,
1822 void* /*pthread_mutex_t* */ mutex) );
njn25e49d8e72002-09-23 09:36:25 +00001823
njn1045b0a2003-02-24 10:42:47 +00001824
1825/* Signal events (not exhaustive) */
1826
njn7b456f52003-05-19 11:16:50 +00001827/* ... pre_send_signal, post_send_signal ... */
1828
njn1045b0a2003-02-24 10:42:47 +00001829/* Called before a signal is delivered; `alt_stack' indicates if it is
1830 delivered on an alternative stack. */
1831EV VG_(track_pre_deliver_signal) ( void (*f)(ThreadId tid, Int sigNum,
1832 Bool alt_stack) );
njn7b456f52003-05-19 11:16:50 +00001833/* Called after a signal is delivered. Nb: unfortunately, if the signal
1834 handler longjmps, this won't be called. */
njn1045b0a2003-02-24 10:42:47 +00001835EV VG_(track_post_deliver_signal) ( void (*f)(ThreadId tid, Int sigNum ) );
1836
1837
1838/* Others... condition variables... */
njn810086f2002-11-14 12:42:47 +00001839/* ... */
1840
1841#undef EV
njn25e49d8e72002-09-23 09:36:25 +00001842
1843/* ------------------------------------------------------------------ */
1844/* Template functions */
1845
1846/* These are the parameterised functions in the core. The default definitions
njnd5bb0a52002-09-27 10:24:48 +00001847 are overridden by LD_PRELOADed skin version. At the very least, a skin
1848 must define the fundamental template functions. Depending on what needs
1849 are set, extra template functions will be used too. Functions are
1850 grouped under the needs that govern their use. */
njn25e49d8e72002-09-23 09:36:25 +00001851
1852
1853/* ------------------------------------------------------------------ */
1854/* Fundamental template functions */
1855
1856/* Initialise skin. Must do the following:
njn810086f2002-11-14 12:42:47 +00001857 - initialise the `details' struct, via the VG_(details_*)() functions
njn25e49d8e72002-09-23 09:36:25 +00001858 - register any helpers called by generated code
1859
1860 May do the following:
njn810086f2002-11-14 12:42:47 +00001861 - initialise the `needs' struct to indicate certain requirements, via
1862 the VG_(needs_*)() functions
1863 - initialise the `track' struct to indicate core events of interest, via
1864 the VG_(track_*)() functions
njn25e49d8e72002-09-23 09:36:25 +00001865 - register any skin-specific profiling events
1866 - any other skin-specific initialisation
1867*/
njn810086f2002-11-14 12:42:47 +00001868extern void SK_(pre_clo_init) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001869
njnd5bb0a52002-09-27 10:24:48 +00001870/* Do initialisation that can only be done after command line processing. */
njn25e49d8e72002-09-23 09:36:25 +00001871extern void SK_(post_clo_init)( void );
1872
1873/* Instrument a basic block. Must be a true function, ie. the same input
1874 always results in the same output, because basic blocks can be
1875 retranslated. Unless you're doing something really strange...
1876 'orig_addr' is the address of the first instruction in the block. */
1877extern UCodeBlock* SK_(instrument) ( UCodeBlock* cb, Addr orig_addr );
1878
njn7d9f94d2003-04-22 21:41:40 +00001879/* Finish up, print out any results, etc. `exitcode' is program's exit
1880 code. The shadow (if the `shadow_regs' need is set) can be found with
1881 VG_(get_shadow_archreg)(R_EBX), since %ebx holds the argument to the
1882 exit() syscall. */
1883extern void SK_(fini) ( Int exitcode );
njn25e49d8e72002-09-23 09:36:25 +00001884
1885
1886/* ------------------------------------------------------------------ */
1887/* VG_(needs).report_errors */
1888
1889/* Identify if two errors are equal, or equal enough. `res' indicates how
1890 close is "close enough". `res' should be passed on as necessary, eg. if
njn810086f2002-11-14 12:42:47 +00001891 the Error's `extra' part contains an ExeContext, `res' should be
njn25e49d8e72002-09-23 09:36:25 +00001892 passed to VG_(eq_ExeContext)() if the ExeContexts are considered. Other
1893 than that, probably don't worry about it unless you have lots of very
1894 similar errors occurring.
1895 */
njn810086f2002-11-14 12:42:47 +00001896extern Bool SK_(eq_SkinError) ( VgRes res, Error* e1, Error* e2 );
njn25e49d8e72002-09-23 09:36:25 +00001897
njn43c799e2003-04-08 00:08:52 +00001898/* Print error context. */
1899extern void SK_(pp_SkinError) ( Error* err );
njn25e49d8e72002-09-23 09:36:25 +00001900
njn43c799e2003-04-08 00:08:52 +00001901/* Should fill in any details that could be postponed until after the
njn810086f2002-11-14 12:42:47 +00001902 decision whether to ignore the error (ie. details not affecting the
1903 result of SK_(eq_SkinError)()). This saves time when errors are ignored.
njn25e49d8e72002-09-23 09:36:25 +00001904 Yuk.
njn43c799e2003-04-08 00:08:52 +00001905
1906 Return value: must be the size of the `extra' part in bytes -- used by
1907 the core to make a copy.
njn25e49d8e72002-09-23 09:36:25 +00001908*/
njn43c799e2003-04-08 00:08:52 +00001909extern UInt SK_(update_extra) ( Error* err );
njn25e49d8e72002-09-23 09:36:25 +00001910
njn810086f2002-11-14 12:42:47 +00001911/* Return value indicates recognition. If recognised, must set skind using
1912 VG_(set_supp_kind)(). */
1913extern Bool SK_(recognised_suppression) ( Char* name, Supp* su );
njn25e49d8e72002-09-23 09:36:25 +00001914
njn810086f2002-11-14 12:42:47 +00001915/* Read any extra info for this suppression kind. Most likely for filling
1916 in the `extra' and `string' parts (with VG_(set_supp_{extra,string})())
1917 of a suppression if necessary. Should return False if a syntax error
1918 occurred, True otherwise. */
1919extern Bool SK_(read_extra_suppression_info) ( Int fd, Char* buf, Int nBuf,
1920 Supp* su );
njn25e49d8e72002-09-23 09:36:25 +00001921
1922/* This should just check the kinds match and maybe some stuff in the
njn810086f2002-11-14 12:42:47 +00001923 `string' and `extra' field if appropriate (using VG_(get_supp_*)() to
1924 get the relevant suppression parts). */
njn43c799e2003-04-08 00:08:52 +00001925extern Bool SK_(error_matches_suppression) ( Error* err, Supp* su );
1926
1927/* This should return the suppression name, for --gen-suppressions, or NULL
1928 if that error type cannot be suppressed. This is the inverse of
1929 SK_(recognised_suppression)(). */
1930extern Char* SK_(get_error_name) ( Error* err );
1931
1932/* This should print any extra info for the error, for --gen-suppressions,
1933 including the newline. This is the inverse of
1934 SK_(read_extra_suppression_info)(). */
1935extern void SK_(print_extra_suppression_info) ( Error* err );
njn25e49d8e72002-09-23 09:36:25 +00001936
1937
1938/* ------------------------------------------------------------------ */
1939/* VG_(needs).basic_block_discards */
1940
njnd5bb0a52002-09-27 10:24:48 +00001941/* Should discard any information that pertains to specific basic blocks
1942 or instructions within the address range given. */
njn25e49d8e72002-09-23 09:36:25 +00001943extern void SK_(discard_basic_block_info) ( Addr a, UInt size );
1944
1945
1946/* ------------------------------------------------------------------ */
1947/* VG_(needs).shadow_regs */
1948
njnd3040452003-05-19 15:04:06 +00001949/* No functions must be defined, but the post_reg[s]_write_* events should
1950 be tracked. */
njn25e49d8e72002-09-23 09:36:25 +00001951
1952/* ------------------------------------------------------------------ */
1953/* VG_(needs).command_line_options */
1954
njnd5bb0a52002-09-27 10:24:48 +00001955/* Return True if option was recognised. Presumably sets some state to
1956 record the option as well. */
1957extern Bool SK_(process_cmd_line_option) ( Char* argv );
njn25e49d8e72002-09-23 09:36:25 +00001958
njn3e884182003-04-15 13:03:23 +00001959/* Print out command line usage for options for normal skin operation. */
1960extern void SK_(print_usage) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001961
njn3e884182003-04-15 13:03:23 +00001962/* Print out command line usage for options for debugging the skin. */
1963extern void SK_(print_debug_usage) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001964
1965/* ------------------------------------------------------------------ */
1966/* VG_(needs).client_requests */
1967
njnd3040452003-05-19 15:04:06 +00001968/* If using client requests, the number of the first request should be equal
1969 to VG_USERREQ_SKIN_BASE('X','Y'), where 'X' and 'Y' form a suitable two
1970 character identification for the string. The second and subsequent
1971 requests should follow. */
1972
1973/* This function should use the VG_IS_SKIN_USERREQ macro (in
1974 include/valgrind.h) to first check if it's a request for this skin. Then
1975 should handle it if it's recognised (and return True), or return False if
1976 not recognised. arg_block[0] holds the request number, any further args
1977 from the request are in arg_block[1..]. 'ret' is for the return value...
1978 it should probably be filled, if only with 0. */
1979extern Bool SK_(handle_client_request) ( ThreadState* tst, UInt* arg_block,
1980 UInt *ret );
njn25e49d8e72002-09-23 09:36:25 +00001981
1982
1983/* ------------------------------------------------------------------ */
1984/* VG_(needs).extends_UCode */
1985
njn4ba5a792002-09-30 10:23:54 +00001986/* Useful to use in VG_(get_Xreg_usage)() */
njn810086f2002-11-14 12:42:47 +00001987#define VG_UINSTR_READS_REG(ono,regs,isWrites) \
njn25e49d8e72002-09-23 09:36:25 +00001988 { if (mycat(u->tag,ono) == tag) \
njn810086f2002-11-14 12:42:47 +00001989 { regs[n] = mycat(u->val,ono); \
1990 isWrites[n] = False; \
njn25e49d8e72002-09-23 09:36:25 +00001991 n++; \
1992 } \
1993 }
njn810086f2002-11-14 12:42:47 +00001994#define VG_UINSTR_WRITES_REG(ono,regs,isWrites) \
njnd5bb0a52002-09-27 10:24:48 +00001995 { if (mycat(u->tag,ono) == tag) \
njn810086f2002-11-14 12:42:47 +00001996 { regs[n] = mycat(u->val,ono); \
1997 isWrites[n] = True; \
njnd5bb0a52002-09-27 10:24:48 +00001998 n++; \
1999 } \
njn25e49d8e72002-09-23 09:36:25 +00002000 }
2001
njn4ba5a792002-09-30 10:23:54 +00002002/* 'X' prefix indicates eXtended UCode. */
njn810086f2002-11-14 12:42:47 +00002003extern Int SK_(get_Xreg_usage) ( UInstr* u, Tag tag, Int* regs,
2004 Bool* isWrites );
njn4ba5a792002-09-30 10:23:54 +00002005extern void SK_(emit_XUInstr) ( UInstr* u, RRegSet regs_live_before );
2006extern Bool SK_(sane_XUInstr) ( Bool beforeRA, Bool beforeLiveness,
njn25e49d8e72002-09-23 09:36:25 +00002007 UInstr* u );
njn4ba5a792002-09-30 10:23:54 +00002008extern Char* SK_(name_XUOpcode) ( Opcode opc );
2009extern void SK_(pp_XUInstr) ( UInstr* u );
njn25e49d8e72002-09-23 09:36:25 +00002010
2011
2012/* ------------------------------------------------------------------ */
2013/* VG_(needs).syscall_wrapper */
2014
2015/* If either of the pre_ functions malloc() something to return, the
2016 * corresponding post_ function had better free() it!
2017 */
2018extern void* SK_( pre_syscall) ( ThreadId tid, UInt syscallno,
2019 Bool is_blocking );
2020extern void SK_(post_syscall) ( ThreadId tid, UInt syscallno,
2021 void* pre_result, Int res,
2022 Bool is_blocking );
2023
njnd5bb0a52002-09-27 10:24:48 +00002024
njn25e49d8e72002-09-23 09:36:25 +00002025/* ---------------------------------------------------------------------
2026 VG_(needs).sanity_checks */
2027
njnd5bb0a52002-09-27 10:24:48 +00002028/* Can be useful for ensuring a skin's correctness. SK_(cheap_sanity_check)
2029 is called very frequently; SK_(expensive_sanity_check) is called less
2030 frequently and can be more involved. */
njn25e49d8e72002-09-23 09:36:25 +00002031extern Bool SK_(cheap_sanity_check) ( void );
2032extern Bool SK_(expensive_sanity_check) ( void );
2033
2034
2035#endif /* NDEF __VG_SKIN_H */
2036
2037/*--------------------------------------------------------------------*/
2038/*--- end vg_skin.h ---*/
2039/*--------------------------------------------------------------------*/
2040