blob: dd61569fb6ce1e1c78b4167e088b776cdddf86da [file] [log] [blame]
fitzhardingec2dbbac2004-01-23 23:09:01 +00001/*-*- c -*- ----------------------------------------------------------*/
njn25e49d8e72002-09-23 09:36:25 +00002/*--- The only header your skin will ever need to #include... ---*/
3/*--- vg_skin.h ---*/
4/*--------------------------------------------------------------------*/
5
6/*
njnc9539842002-10-02 13:26:35 +00007 This file is part of Valgrind, an extensible x86 protected-mode
8 emulator for monitoring program execution on x86-Unixes.
njn25e49d8e72002-09-23 09:36:25 +00009
nethercotebb1c9912004-01-04 16:43:23 +000010 Copyright (C) 2000-2004 Julian Seward
njn25e49d8e72002-09-23 09:36:25 +000011 jseward@acm.org
12
13 This program is free software; you can redistribute it and/or
14 modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation; either version 2 of the
16 License, or (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
26 02111-1307, USA.
27
28 The GNU General Public License is contained in the file COPYING.
29*/
30
31#ifndef __VG_SKIN_H
32#define __VG_SKIN_H
33
34#include <stdarg.h> /* ANSI varargs stuff */
35#include <setjmp.h> /* for jmp_buf */
36
37#include "vg_constants_skin.h"
38
39
nethercote421281e2003-11-20 16:20:55 +000040/* ---------------------------------------------------------------------
41 Where to send bug reports to.
42 ------------------------------------------------------------------ */
43
44#define VG_BUGS_TO "valgrind.kde.org"
45
46
njn25e49d8e72002-09-23 09:36:25 +000047/*====================================================================*/
48/*=== Build options and table sizes. ===*/
49/*====================================================================*/
50
daywalker5d945de2003-09-26 00:32:53 +000051/* You should be able to change these options or sizes, recompile, and
njn25e49d8e72002-09-23 09:36:25 +000052 still have a working system. */
53
54/* The maximum number of pthreads that we support. This is
55 deliberately not very high since our implementation of some of the
56 scheduler algorithms is surely O(N) in the number of threads, since
57 that's simple, at least. And (in practice) we hope that most
58 programs do not need many threads. */
sewardj989dad92003-07-06 01:52:32 +000059#define VG_N_THREADS 100
njn25e49d8e72002-09-23 09:36:25 +000060
61/* Maximum number of pthread keys available. Again, we start low until
62 the need for a higher number presents itself. */
63#define VG_N_THREAD_KEYS 50
64
65/* Total number of integer registers available for allocation -- all of
66 them except %esp, %ebp. %ebp permanently points at VG_(baseBlock).
daywalker5d945de2003-09-26 00:32:53 +000067
njn211b6ad2003-02-03 12:33:31 +000068 If you increase this you'll have to also change at least these:
njn4ba5a792002-09-30 10:23:54 +000069 - VG_(rank_to_realreg)()
70 - VG_(realreg_to_rank)()
njn25e49d8e72002-09-23 09:36:25 +000071 - ppRegsLiveness()
72 - the RegsLive type (maybe -- RegsLive type must have more than
73 VG_MAX_REALREGS bits)
njn211b6ad2003-02-03 12:33:31 +000074
75 You can decrease it, and performance will drop because more spills will
76 occur. If you decrease it too much, everything will fall over.
daywalker5d945de2003-09-26 00:32:53 +000077
njn25e49d8e72002-09-23 09:36:25 +000078 Do not change this unless you really know what you are doing! */
79#define VG_MAX_REALREGS 6
80
81
82/*====================================================================*/
njn1a1dd8b2002-09-27 10:42:20 +000083/*=== Basic types, useful macros ===*/
njn25e49d8e72002-09-23 09:36:25 +000084/*====================================================================*/
85
njn25e49d8e72002-09-23 09:36:25 +000086typedef unsigned char UChar;
87typedef unsigned short UShort;
88typedef unsigned int UInt;
89typedef unsigned long long int ULong;
90
91typedef signed char Char;
92typedef signed short Short;
93typedef signed int Int;
94typedef signed long long int Long;
95
96typedef unsigned int Addr;
97
98typedef unsigned char Bool;
99#define False ((Bool)0)
100#define True ((Bool)1)
101
102
njn1a1dd8b2002-09-27 10:42:20 +0000103#define mycat_wrk(aaa,bbb) aaa##bbb
104#define mycat(aaa,bbb) mycat_wrk(aaa,bbb)
105
106/* No, really. I _am_ that strange. */
107#define OINK(nnn) VG_(message)(Vg_DebugMsg, "OINK %d",nnn)
108
njn25e49d8e72002-09-23 09:36:25 +0000109/* ---------------------------------------------------------------------
110 Now the basic types are set up, we can haul in the kernel-interface
111 definitions.
112 ------------------------------------------------------------------ */
113
njn78adbf42003-07-24 19:35:00 +0000114#include "vg_kerneliface.h"
njn25e49d8e72002-09-23 09:36:25 +0000115
116
117/*====================================================================*/
njn27f1a382002-11-08 15:48:16 +0000118/*=== Core/skin interface version ===*/
119/*====================================================================*/
120
121/* The major version number indicates binary-incompatible changes to the
122 interface; if the core and skin major versions don't match, Valgrind
123 will abort. The minor version indicates binary-compatible changes.
njn27f1a382002-11-08 15:48:16 +0000124*/
jseward157689b2003-11-05 23:59:21 +0000125#define VG_CORE_INTERFACE_MAJOR_VERSION 5
njn9b007f62003-04-07 14:40:25 +0000126#define VG_CORE_INTERFACE_MINOR_VERSION 0
njn27f1a382002-11-08 15:48:16 +0000127
fitzhardinge98abfc72003-12-16 02:05:15 +0000128typedef struct _ToolInfo {
129 Int sizeof_ToolInfo;
130 Int interface_major_version;
131 Int interface_minor_version;
132
133 /* Initialise skin. Must do the following:
134 - initialise the `details' struct, via the VG_(details_*)() functions
135 - register any helpers called by generated code
136
137 May do the following:
138 - initialise the `needs' struct to indicate certain requirements, via
139 the VG_(needs_*)() functions
140 - initialize all the tool's entrypoints via the VG_(init_*)() functions
141 - register any skin-specific profiling events
142 - any other skin-specific initialisation
143 */
144 void (*sk_pre_clo_init) ( void );
145
146 /* Specifies how big the shadow segment should be as a ratio to the
147 client address space. 0 for no shadow segment. */
148 float shadow_ratio;
149} ToolInfo;
njn27f1a382002-11-08 15:48:16 +0000150
njn563f96f2003-02-03 11:17:46 +0000151/* Every skin must include this macro somewhere, exactly once. */
fitzhardinge98abfc72003-12-16 02:05:15 +0000152#define VG_DETERMINE_INTERFACE_VERSION(pre_clo_init, shadow) \
153 const ToolInfo SK_(tool_info) = { \
154 .sizeof_ToolInfo = sizeof(ToolInfo), \
155 .interface_major_version = VG_CORE_INTERFACE_MAJOR_VERSION, \
156 .interface_minor_version = VG_CORE_INTERFACE_MINOR_VERSION, \
157 .sk_pre_clo_init = pre_clo_init, \
158 .shadow_ratio = shadow, \
159 };
njn211b6ad2003-02-03 12:33:31 +0000160
njn27f1a382002-11-08 15:48:16 +0000161/*====================================================================*/
njn25e49d8e72002-09-23 09:36:25 +0000162/*=== Command-line options ===*/
163/*====================================================================*/
164
njn43c799e2003-04-08 00:08:52 +0000165/* Use this for normal null-termination-style string comparison */
166#define VG_STREQ(s1,s2) (s1 != NULL && s2 != NULL \
167 && VG_(strcmp)((s1),(s2))==0)
168
169/* Use these for recognising skin command line options -- stops comparing
170 once whitespace is reached. */
171# define VG_CLO_STREQ(s1,s2) (0==VG_(strcmp_ws)((s1),(s2)))
172# define VG_CLO_STREQN(nn,s1,s2) (0==VG_(strncmp_ws)((s1),(s2),(nn)))
173
njn25e49d8e72002-09-23 09:36:25 +0000174/* Verbosity level: 0 = silent, 1 (default), > 1 = more verbose. */
175extern Int VG_(clo_verbosity);
176
177/* Profile? */
178extern Bool VG_(clo_profile);
179
njn25e49d8e72002-09-23 09:36:25 +0000180/* Call this if a recognised option was bad for some reason.
181 Note: don't use it just because an option was unrecognised -- return 'False'
182 from SKN_(process_cmd_line_option) to indicate that. */
183extern void VG_(bad_option) ( Char* opt );
184
185/* Client args */
186extern Int VG_(client_argc);
187extern Char** VG_(client_argv);
188
njnd5bb0a52002-09-27 10:24:48 +0000189/* Client environment. Can be inspected with VG_(getenv)() */
njn25e49d8e72002-09-23 09:36:25 +0000190extern Char** VG_(client_envp);
191
192
193/*====================================================================*/
194/*=== Printing messages for the user ===*/
195/*====================================================================*/
196
197/* Print a message prefixed by "??<pid>?? "; '?' depends on the VgMsgKind.
198 Should be used for all user output. */
199
200typedef
201 enum { Vg_UserMsg, /* '?' == '=' */
202 Vg_DebugMsg, /* '?' == '-' */
fitzhardinge39de4b42003-10-31 07:12:21 +0000203 Vg_DebugExtraMsg, /* '?' == '+' */
204 Vg_ClientMsg, /* '?' == '*' */
njn25e49d8e72002-09-23 09:36:25 +0000205 }
206 VgMsgKind;
207
208/* Functions for building a message from multiple parts. */
fitzhardinge39de4b42003-10-31 07:12:21 +0000209extern int VG_(start_msg) ( VgMsgKind kind );
210extern int VG_(add_to_msg) ( Char* format, ... );
njn25e49d8e72002-09-23 09:36:25 +0000211/* Ends and prints the message. Appends a newline. */
fitzhardinge39de4b42003-10-31 07:12:21 +0000212extern int VG_(end_msg) ( void );
njn25e49d8e72002-09-23 09:36:25 +0000213
njnd5bb0a52002-09-27 10:24:48 +0000214/* Send a single-part message. Appends a newline. */
fitzhardinge39de4b42003-10-31 07:12:21 +0000215extern int VG_(message) ( VgMsgKind kind, Char* format, ... );
216extern int VG_(vmessage) ( VgMsgKind kind, Char* format, va_list vargs );
njn25e49d8e72002-09-23 09:36:25 +0000217
218
219/*====================================================================*/
220/*=== Profiling ===*/
221/*====================================================================*/
222
223/* Nb: VGP_(register_profile_event)() relies on VgpUnc being the first one */
224#define VGP_CORE_LIST \
225 /* These ones depend on the core */ \
226 VGP_PAIR(VgpUnc, "unclassified"), \
nethercote71980f02004-01-24 18:18:54 +0000227 VGP_PAIR(VgpStartup, "startup"), \
njn25e49d8e72002-09-23 09:36:25 +0000228 VGP_PAIR(VgpRun, "running"), \
229 VGP_PAIR(VgpSched, "scheduler"), \
230 VGP_PAIR(VgpMalloc, "low-lev malloc/free"), \
231 VGP_PAIR(VgpCliMalloc, "client malloc/free"), \
njn25e49d8e72002-09-23 09:36:25 +0000232 VGP_PAIR(VgpTranslate, "translate-main"), \
233 VGP_PAIR(VgpToUCode, "to-ucode"), \
234 VGP_PAIR(VgpFromUcode, "from-ucode"), \
235 VGP_PAIR(VgpImprove, "improve"), \
njn9b007f62003-04-07 14:40:25 +0000236 VGP_PAIR(VgpESPUpdate, "ESP-update"), \
njn25e49d8e72002-09-23 09:36:25 +0000237 VGP_PAIR(VgpRegAlloc, "reg-alloc"), \
238 VGP_PAIR(VgpLiveness, "liveness-analysis"), \
239 VGP_PAIR(VgpDoLRU, "do-lru"), \
240 VGP_PAIR(VgpSlowFindT, "slow-search-transtab"), \
njn25e49d8e72002-09-23 09:36:25 +0000241 VGP_PAIR(VgpExeContext, "exe-context"), \
242 VGP_PAIR(VgpReadSyms, "read-syms"), \
243 VGP_PAIR(VgpSearchSyms, "search-syms"), \
244 VGP_PAIR(VgpAddToT, "add-to-transtab"), \
245 VGP_PAIR(VgpCoreSysWrap, "core-syscall-wrapper"), \
246 VGP_PAIR(VgpDemangle, "demangle"), \
njn37cea302002-09-30 11:24:00 +0000247 VGP_PAIR(VgpCoreCheapSanity, "core-cheap-sanity"), \
248 VGP_PAIR(VgpCoreExpensiveSanity, "core-expensive-sanity"), \
njn25e49d8e72002-09-23 09:36:25 +0000249 /* These ones depend on the skin */ \
250 VGP_PAIR(VgpPreCloInit, "pre-clo-init"), \
251 VGP_PAIR(VgpPostCloInit, "post-clo-init"), \
252 VGP_PAIR(VgpInstrument, "instrument"), \
253 VGP_PAIR(VgpSkinSysWrap, "skin-syscall-wrapper"), \
njn37cea302002-09-30 11:24:00 +0000254 VGP_PAIR(VgpSkinCheapSanity, "skin-cheap-sanity"), \
255 VGP_PAIR(VgpSkinExpensiveSanity, "skin-expensive-sanity"), \
njn25e49d8e72002-09-23 09:36:25 +0000256 VGP_PAIR(VgpFini, "fini")
257
258#define VGP_PAIR(n,name) n
259typedef enum { VGP_CORE_LIST } VgpCoreCC;
260#undef VGP_PAIR
261
262/* When registering skin profiling events, ensure that the 'n' value is in
263 * the range (VgpFini+1..) */
264extern void VGP_(register_profile_event) ( Int n, Char* name );
265
266extern void VGP_(pushcc) ( UInt cc );
267extern void VGP_(popcc) ( UInt cc );
268
269/* Define them only if they haven't already been defined by vg_profile.c */
270#ifndef VGP_PUSHCC
271# define VGP_PUSHCC(x)
272#endif
273#ifndef VGP_POPCC
274# define VGP_POPCC(x)
275#endif
276
277
278/*====================================================================*/
279/*=== Useful stuff to call from generated code ===*/
280/*====================================================================*/
281
282/* ------------------------------------------------------------------ */
283/* General stuff */
284
njn41557122002-10-14 09:25:37 +0000285/* 64-bit counter for the number of basic blocks done. */
286extern ULong VG_(bbs_done);
287
njn25e49d8e72002-09-23 09:36:25 +0000288/* Get the simulated %esp */
289extern Addr VG_(get_stack_pointer) ( void );
290
njn25e49d8e72002-09-23 09:36:25 +0000291/* Check if an address is 4-byte aligned */
292#define IS_ALIGNED4_ADDR(aaa_p) (0 == (((UInt)(aaa_p)) & 3))
njn9b007f62003-04-07 14:40:25 +0000293#define IS_ALIGNED8_ADDR(aaa_p) (0 == (((UInt)(aaa_p)) & 7))
njn25e49d8e72002-09-23 09:36:25 +0000294
295
296/* ------------------------------------------------------------------ */
297/* Thread-related stuff */
298
299/* Special magic value for an invalid ThreadId. It corresponds to
300 LinuxThreads using zero as the initial value for
301 pthread_mutex_t.__m_owner and pthread_cond_t.__c_waiting. */
302#define VG_INVALID_THREADID ((ThreadId)(0))
303
njn72718642003-07-24 08:45:32 +0000304/* ThreadIds are simply indices into the VG_(threads)[] array. */
daywalker5d945de2003-09-26 00:32:53 +0000305typedef
306 UInt
njn25e49d8e72002-09-23 09:36:25 +0000307 ThreadId;
308
njn72718642003-07-24 08:45:32 +0000309/* When looking for the current ThreadId, this is the safe option and
310 probably the one you want.
daywalker5d945de2003-09-26 00:32:53 +0000311
njn72718642003-07-24 08:45:32 +0000312 Details: Use this one from non-generated code, eg. from functions called
313 on events like 'new_mem_heap'. In such a case, the "current" thread is
314 temporarily suspended as Valgrind's dispatcher is running. This function
315 is also suitable to be called from generated code (ie. from UCode, or a C
316 function called directly from UCode).
daywalker5d945de2003-09-26 00:32:53 +0000317
njn72718642003-07-24 08:45:32 +0000318 If you use VG_(get_current_tid)() from non-generated code, it will return
319 0 signifying the invalid thread, which is probably not what you want. */
320extern ThreadId VG_(get_current_or_recent_tid) ( void );
njn25e49d8e72002-09-23 09:36:25 +0000321
njn72718642003-07-24 08:45:32 +0000322/* When looking for the current ThreadId, only use this one if you know what
323 you are doing.
daywalker5d945de2003-09-26 00:32:53 +0000324
njn72718642003-07-24 08:45:32 +0000325 Details: Use this one from generated code, eg. from C functions called
326 from UCode. (VG_(get_current_or_recent_tid)() is also suitable in that
327 case.) If you use this function from non-generated code, it will return
328 0 signifying the invalid thread, which is probably not what you want. */
329extern ThreadId VG_(get_current_tid) ( void );
njn25e49d8e72002-09-23 09:36:25 +0000330
njn3e884182003-04-15 13:03:23 +0000331/* Searches through all thread's stacks to see if any match. Returns
njn72718642003-07-24 08:45:32 +0000332 VG_INVALID_THREADID if none match. */
njn3e884182003-04-15 13:03:23 +0000333extern ThreadId VG_(first_matching_thread_stack)
334 ( Bool (*p) ( Addr stack_min, Addr stack_max ));
335
njn25e49d8e72002-09-23 09:36:25 +0000336
337/*====================================================================*/
338/*=== Valgrind's version of libc ===*/
339/*====================================================================*/
340
341/* Valgrind doesn't use libc at all, for good reasons (trust us). So here
342 are its own versions of C library functions, but with VG_ prefixes. Note
343 that the types of some are slightly different to the real ones. Some
njnf4ce3d32003-02-10 10:17:26 +0000344 additional useful functions are provided too; descriptions of how they
345 work are given below. */
njn25e49d8e72002-09-23 09:36:25 +0000346
347#if !defined(NULL)
348# define NULL ((void*)0)
349#endif
350
351
352/* ------------------------------------------------------------------ */
353/* stdio.h
354 *
355 * Note that they all output to the file descriptor given by the
356 * --logfile-fd=N argument, which defaults to 2 (stderr). Hence no
daywalker5d945de2003-09-26 00:32:53 +0000357 * need for VG_(fprintf)().
njn25e49d8e72002-09-23 09:36:25 +0000358 */
sewardj78e3cd92002-10-22 04:45:48 +0000359extern UInt VG_(printf) ( const char *format, ... );
njn25e49d8e72002-09-23 09:36:25 +0000360/* too noisy ... __attribute__ ((format (printf, 1, 2))) ; */
sewardj78e3cd92002-10-22 04:45:48 +0000361extern UInt VG_(sprintf) ( Char* buf, Char *format, ... );
daywalker5d945de2003-09-26 00:32:53 +0000362extern UInt VG_(vprintf) ( void(*send)(Char),
njn25e49d8e72002-09-23 09:36:25 +0000363 const Char *format, va_list vargs );
364
njn41557122002-10-14 09:25:37 +0000365extern Int VG_(rename) ( Char* old_name, Char* new_name );
366
njn25e49d8e72002-09-23 09:36:25 +0000367/* ------------------------------------------------------------------ */
368/* stdlib.h */
369
370extern void* VG_(malloc) ( Int nbytes );
njnd5bb0a52002-09-27 10:24:48 +0000371extern void VG_(free) ( void* p );
372extern void* VG_(calloc) ( Int n, Int nbytes );
373extern void* VG_(realloc) ( void* p, Int size );
374extern void* VG_(malloc_aligned) ( Int align_bytes, Int nbytes );
njn25e49d8e72002-09-23 09:36:25 +0000375
376extern void VG_(print_malloc_stats) ( void );
377
378
379extern void VG_(exit)( Int status )
380 __attribute__ ((__noreturn__));
njne427a662002-10-02 11:08:25 +0000381/* Prints a panic message (a constant string), appends newline and bug
382 reporting info, aborts. */
383__attribute__ ((__noreturn__))
384extern void VG_(skin_panic) ( Char* str );
njn25e49d8e72002-09-23 09:36:25 +0000385
njnd5bb0a52002-09-27 10:24:48 +0000386/* Looks up VG_(client_envp) */
njn25e49d8e72002-09-23 09:36:25 +0000387extern Char* VG_(getenv) ( Char* name );
388
rjwalshf5f536f2003-11-17 17:45:00 +0000389/* Get client resource limit*/
390extern Int VG_(getrlimit) ( Int resource, struct vki_rlimit *rlim );
391
fitzhardingef0046f22003-12-18 02:39:22 +0000392/* Set client resource limit*/
393extern Int VG_(setrlimit) ( Int resource, struct vki_rlimit *rlim );
394
njn25e49d8e72002-09-23 09:36:25 +0000395/* Crude stand-in for the glibc system() call. */
396extern Int VG_(system) ( Char* cmd );
397
njnd5bb0a52002-09-27 10:24:48 +0000398extern Long VG_(atoll) ( Char* str );
njn25e49d8e72002-09-23 09:36:25 +0000399
rjwalsh8b426722004-01-04 23:15:22 +0000400/* Like atoll(), but converts a number of base 16 */
401extern Long VG_(atoll16) ( Char* str );
402
njn25e49d8e72002-09-23 09:36:25 +0000403/* Like atoll(), but converts a number of base 2..36 */
404extern Long VG_(atoll36) ( UInt base, Char* str );
405
njne36543a2003-09-30 15:37:24 +0000406/* Like qsort(), but does shell-sort. The size==1/2/4 cases are specialised. */
njnd3b0c5f2003-09-30 14:43:54 +0000407extern void VG_(ssort)( void* base, UInt nmemb, UInt size,
408 Int (*compar)(void*, void*) );
409
njn25e49d8e72002-09-23 09:36:25 +0000410
411/* ------------------------------------------------------------------ */
njnd5bb0a52002-09-27 10:24:48 +0000412/* ctype.h */
njn25e49d8e72002-09-23 09:36:25 +0000413extern Bool VG_(isspace) ( Char c );
414extern Bool VG_(isdigit) ( Char c );
415extern Char VG_(toupper) ( Char c );
416
417
418/* ------------------------------------------------------------------ */
419/* string.h */
420extern Int VG_(strlen) ( const Char* str );
421extern Char* VG_(strcat) ( Char* dest, const Char* src );
422extern Char* VG_(strncat) ( Char* dest, const Char* src, Int n );
423extern Char* VG_(strpbrk) ( const Char* s, const Char* accept );
424extern Char* VG_(strcpy) ( Char* dest, const Char* src );
425extern Char* VG_(strncpy) ( Char* dest, const Char* src, Int ndest );
426extern Int VG_(strcmp) ( const Char* s1, const Char* s2 );
427extern Int VG_(strncmp) ( const Char* s1, const Char* s2, Int nmax );
428extern Char* VG_(strstr) ( const Char* haystack, Char* needle );
429extern Char* VG_(strchr) ( const Char* s, Char c );
430extern Char* VG_(strdup) ( const Char* s);
sewardj7ab2aca2002-10-20 19:40:32 +0000431extern void* VG_(memcpy) ( void *d, const void *s, Int sz );
432extern void* VG_(memset) ( void *s, Int c, Int sz );
njn6d68e792003-02-24 10:49:08 +0000433extern Int VG_(memcmp) ( const void* s1, const void* s2, Int n );
njn25e49d8e72002-09-23 09:36:25 +0000434
njnd5bb0a52002-09-27 10:24:48 +0000435/* Like strcmp() and strncmp(), but stop comparing at any whitespace. */
njn25e49d8e72002-09-23 09:36:25 +0000436extern Int VG_(strcmp_ws) ( const Char* s1, const Char* s2 );
njn25e49d8e72002-09-23 09:36:25 +0000437extern Int VG_(strncmp_ws) ( const Char* s1, const Char* s2, Int nmax );
438
daywalker5d945de2003-09-26 00:32:53 +0000439/* Like strncpy(), but if 'src' is longer than 'ndest' inserts a '\0' as the
njnd5bb0a52002-09-27 10:24:48 +0000440 last character. */
njn25e49d8e72002-09-23 09:36:25 +0000441extern void VG_(strncpy_safely) ( Char* dest, const Char* src, Int ndest );
442
443/* Mini-regexp function. Searches for 'pat' in 'str'. Supports
444 * meta-symbols '*' and '?'. '\' escapes meta-symbols. */
fitzhardinge98abfc72003-12-16 02:05:15 +0000445extern Bool VG_(string_match) ( const Char* pat, const Char* str );
njn25e49d8e72002-09-23 09:36:25 +0000446
447
448/* ------------------------------------------------------------------ */
449/* math.h */
njnd5bb0a52002-09-27 10:24:48 +0000450/* Returns the base-2 logarithm of x. */
njn25e49d8e72002-09-23 09:36:25 +0000451extern Int VG_(log2) ( Int x );
452
453
454/* ------------------------------------------------------------------ */
njnd5bb0a52002-09-27 10:24:48 +0000455/* unistd.h, fcntl.h, sys/stat.h */
rjwalshf5f536f2003-11-17 17:45:00 +0000456extern Int VG_(getdents)( UInt fd, struct vki_dirent *dirp, UInt count );
457extern Int VG_(readlink)( Char* path, Char* buf, UInt bufsize );
sewardj4cf05692002-10-27 20:28:29 +0000458extern Int VG_(getpid) ( void );
459extern Int VG_(getppid) ( void );
jsgf855d93d2003-10-13 22:26:55 +0000460extern Int VG_(getpgrp) ( void );
461extern Int VG_(gettid) ( void );
462extern Int VG_(setpgid) ( Int pid, Int pgrp );
njnd5bb0a52002-09-27 10:24:48 +0000463
njn4aca2d22002-10-04 10:29:38 +0000464extern Int VG_(open) ( const Char* pathname, Int flags, Int mode );
465extern Int VG_(read) ( Int fd, void* buf, Int count);
jsgf855d93d2003-10-13 22:26:55 +0000466extern Int VG_(write) ( Int fd, const void* buf, Int count);
rjwalshf5f536f2003-11-17 17:45:00 +0000467extern Int VG_(lseek) ( Int fd, Long offset, Int whence);
njn4aca2d22002-10-04 10:29:38 +0000468extern void VG_(close) ( Int fd );
njnd5bb0a52002-09-27 10:24:48 +0000469
jsgf855d93d2003-10-13 22:26:55 +0000470extern Int VG_(pipe) ( Int fd[2] );
471
njn41557122002-10-14 09:25:37 +0000472/* Nb: VG_(rename)() declared in stdio.h section above */
njn4aca2d22002-10-04 10:29:38 +0000473extern Int VG_(unlink) ( Char* file_name );
474extern Int VG_(stat) ( Char* file_name, struct vki_stat* buf );
njnc9d4ba72003-10-15 10:34:03 +0000475extern Int VG_(fstat) ( Int fd, struct vki_stat* buf );
fitzhardingee1c06d82003-10-30 07:21:44 +0000476extern Int VG_(dup2) ( Int oldfd, Int newfd );
njn25e49d8e72002-09-23 09:36:25 +0000477
njn13f02932003-04-30 20:23:58 +0000478extern Char* VG_(getcwd) ( Char* buf, Int size );
479
njn99ccf082003-09-30 13:51:23 +0000480/* Easier to use than VG_(getcwd)() -- does the buffer fiddling itself.
481 String put into 'cwd' is VG_(malloc)'d, and should be VG_(free)'d.
482 Returns False if it fails. Will fail if the pathname is > 65535 bytes. */
483extern Bool VG_(getcwd_alloc) ( Char** cwd );
njn25e49d8e72002-09-23 09:36:25 +0000484
485/* ------------------------------------------------------------------ */
486/* assert.h */
487/* Asserts permanently enabled -- no turning off with NDEBUG. Hurrah! */
488#define VG__STRING(__str) #__str
489
njne427a662002-10-02 11:08:25 +0000490#define sk_assert(expr) \
njn25e49d8e72002-09-23 09:36:25 +0000491 ((void) ((expr) ? 0 : \
njne427a662002-10-02 11:08:25 +0000492 (VG_(skin_assert_fail) (VG__STRING(expr), \
493 __FILE__, __LINE__, \
494 __PRETTY_FUNCTION__), 0)))
njn25e49d8e72002-09-23 09:36:25 +0000495
njne427a662002-10-02 11:08:25 +0000496__attribute__ ((__noreturn__))
daywalker5d945de2003-09-26 00:32:53 +0000497extern void VG_(skin_assert_fail) ( const Char* expr, const Char* file,
daywalkerdf9ae422003-09-18 01:41:48 +0000498 Int line, const Char* fn );
njn25e49d8e72002-09-23 09:36:25 +0000499
500
501/* ------------------------------------------------------------------ */
njn25e49d8e72002-09-23 09:36:25 +0000502/* Get memory by anonymous mmap. */
503extern void* VG_(get_memory_from_mmap) ( Int nBytes, Char* who );
504
fitzhardinge98abfc72003-12-16 02:05:15 +0000505extern Bool VG_(is_client_addr) (Addr a);
506extern Addr VG_(get_client_base)(void);
507extern Addr VG_(get_client_end) (void);
508extern Addr VG_(get_client_size)(void);
509
510extern Bool VG_(is_shadow_addr) (Addr a);
511extern Addr VG_(get_shadow_base)(void);
512extern Addr VG_(get_shadow_end) (void);
513extern Addr VG_(get_shadow_size)(void);
514
515extern void *VG_(shadow_alloc)(UInt size);
516
517extern Bool VG_(is_addressable)(Addr p, Int sz);
518
519extern Addr VG_(client_alloc)(Addr base, UInt len, UInt prot, UInt flags);
520extern void VG_(client_free)(Addr addr);
521
522extern Bool VG_(is_valgrind_addr)(Addr a);
523
524/* initialize shadow pages in the range [p, p+sz) This calls
525 init_shadow_page for each one. It should be a lot more efficient
526 for bulk-initializing shadow pages than faulting on each one.
527*/
528extern void VG_(init_shadow_range)(Addr p, UInt sz, Bool call_init);
njn25e49d8e72002-09-23 09:36:25 +0000529
530/* ------------------------------------------------------------------ */
daywalker5d945de2003-09-26 00:32:53 +0000531/* signal.h.
532
njn25e49d8e72002-09-23 09:36:25 +0000533 Note that these use the vk_ (kernel) structure
534 definitions, which are different in places from those that glibc
535 defines -- hence the 'k' prefix. Since we're operating right at the
536 kernel interface, glibc's view of the world is entirely irrelevant. */
537
538/* --- Signal set ops --- */
njnd5bb0a52002-09-27 10:24:48 +0000539extern Int VG_(ksigfillset) ( vki_ksigset_t* set );
540extern Int VG_(ksigemptyset) ( vki_ksigset_t* set );
njn25e49d8e72002-09-23 09:36:25 +0000541
njnd5bb0a52002-09-27 10:24:48 +0000542extern Bool VG_(kisfullsigset) ( vki_ksigset_t* set );
543extern Bool VG_(kisemptysigset) ( vki_ksigset_t* set );
njn25e49d8e72002-09-23 09:36:25 +0000544
njnd5bb0a52002-09-27 10:24:48 +0000545extern Int VG_(ksigaddset) ( vki_ksigset_t* set, Int signum );
546extern Int VG_(ksigdelset) ( vki_ksigset_t* set, Int signum );
njn25e49d8e72002-09-23 09:36:25 +0000547extern Int VG_(ksigismember) ( vki_ksigset_t* set, Int signum );
548
njnd5bb0a52002-09-27 10:24:48 +0000549extern void VG_(ksigaddset_from_set) ( vki_ksigset_t* dst, vki_ksigset_t* src );
550extern void VG_(ksigdelset_from_set) ( vki_ksigset_t* dst, vki_ksigset_t* src );
njn25e49d8e72002-09-23 09:36:25 +0000551
552/* --- Mess with the kernel's sig state --- */
daywalker5d945de2003-09-26 00:32:53 +0000553extern Int VG_(ksigprocmask) ( Int how, const vki_ksigset_t* set,
njn25e49d8e72002-09-23 09:36:25 +0000554 vki_ksigset_t* oldset );
daywalker5d945de2003-09-26 00:32:53 +0000555extern Int VG_(ksigaction) ( Int signum,
556 const vki_ksigaction* act,
njnd5bb0a52002-09-27 10:24:48 +0000557 vki_ksigaction* oldact );
njn25e49d8e72002-09-23 09:36:25 +0000558
jsgf855d93d2003-10-13 22:26:55 +0000559extern Int VG_(ksigtimedwait)( const vki_ksigset_t *, vki_ksiginfo_t *,
560 const struct vki_timespec * );
561
njnd5bb0a52002-09-27 10:24:48 +0000562extern Int VG_(ksignal) ( Int signum, void (*sighandler)(Int) );
563extern Int VG_(ksigaltstack) ( const vki_kstack_t* ss, vki_kstack_t* oss );
njn25e49d8e72002-09-23 09:36:25 +0000564
sewardjdcaf3122002-09-30 23:12:33 +0000565extern Int VG_(kkill) ( Int pid, Int signo );
jsgf855d93d2003-10-13 22:26:55 +0000566extern Int VG_(ktkill) ( Int pid, Int signo );
sewardjdcaf3122002-09-30 23:12:33 +0000567extern Int VG_(ksigpending) ( vki_ksigset_t* set );
njn25e49d8e72002-09-23 09:36:25 +0000568
jsgf855d93d2003-10-13 22:26:55 +0000569extern Int VG_(waitpid) ( Int pid, Int *status, Int options );
njn25e49d8e72002-09-23 09:36:25 +0000570
njne7442cf2003-09-30 14:03:21 +0000571/* ------------------------------------------------------------------ */
rjwalshf5f536f2003-11-17 17:45:00 +0000572/* socket.h. */
573
574extern Int VG_(getsockname) ( Int sd, struct vki_sockaddr *name, Int *namelen);
575extern Int VG_(getpeername) ( Int sd, struct vki_sockaddr *name, Int *namelen);
576extern Int VG_(getsockopt) ( Int sd, Int level, Int optname, void *optval,
577 Int *optlen);
578
579/* ------------------------------------------------------------------ */
njne7442cf2003-09-30 14:03:21 +0000580/* other, randomly useful functions */
581extern UInt VG_(read_millisecond_timer) ( void );
582
njn25e49d8e72002-09-23 09:36:25 +0000583/*====================================================================*/
584/*=== UCode definition ===*/
585/*====================================================================*/
586
sewardje1042472002-09-30 12:33:11 +0000587/* Tags which describe what operands are. Must fit into 4 bits, which
588 they clearly do. */
njn25e49d8e72002-09-23 09:36:25 +0000589typedef
sewardje1042472002-09-30 12:33:11 +0000590enum { TempReg =0, /* virtual temp-reg */
591 ArchReg =1, /* simulated integer reg */
592 ArchRegS =2, /* simulated segment reg */
593 RealReg =3, /* real machine's real reg */
594 SpillNo =4, /* spill slot location */
595 Literal =5, /* literal; .lit32 field has actual value */
596 Lit16 =6, /* literal; .val[123] field has actual value */
597 NoValue =7 /* operand not in use */
598 }
599 Tag;
njn25e49d8e72002-09-23 09:36:25 +0000600
njnd5bb0a52002-09-27 10:24:48 +0000601/* Invalid register numbers (can't be negative) */
njn25e49d8e72002-09-23 09:36:25 +0000602#define INVALID_TEMPREG 999999999
603#define INVALID_REALREG 999999999
604
605/* Microinstruction opcodes. */
606typedef
607 enum {
njnd5bb0a52002-09-27 10:24:48 +0000608 NOP, /* Null op */
njn25e49d8e72002-09-23 09:36:25 +0000609
daywalker7e73e5f2003-07-04 16:18:15 +0000610 LOCK, /* Indicate the existence of a LOCK prefix (functionally NOP) */
sewardj7a5ebcf2002-11-13 22:42:13 +0000611
njnd5bb0a52002-09-27 10:24:48 +0000612 /* Moving values around */
613 GET, PUT, /* simulated register <--> TempReg */
614 GETF, PUTF, /* simulated %eflags <--> TempReg */
615 LOAD, STORE, /* memory <--> TempReg */
616 MOV, /* TempReg <--> TempReg */
617 CMOV, /* Used for cmpxchg and cmov */
njn25e49d8e72002-09-23 09:36:25 +0000618
njnd5bb0a52002-09-27 10:24:48 +0000619 /* Arithmetic/logical ops */
jsgf5efa4fd2003-10-14 21:49:11 +0000620 MUL, UMUL, /* Multiply */
njnd5bb0a52002-09-27 10:24:48 +0000621 ADD, ADC, SUB, SBB, /* Add/subtract (w/wo carry) */
622 AND, OR, XOR, NOT, /* Boolean ops */
623 SHL, SHR, SAR, ROL, ROR, RCL, RCR, /* Shift/rotate (w/wo carry) */
624 NEG, /* Negate */
625 INC, DEC, /* Increment/decrement */
626 BSWAP, /* Big-endian <--> little-endian */
627 CC2VAL, /* Condition code --> 0 or 1 */
628 WIDEN, /* Signed or unsigned widening */
njn25e49d8e72002-09-23 09:36:25 +0000629
njnd5bb0a52002-09-27 10:24:48 +0000630 /* Conditional or unconditional jump */
daywalker5d945de2003-09-26 00:32:53 +0000631 JMP,
njnd5bb0a52002-09-27 10:24:48 +0000632
633 /* FPU ops */
634 FPU, /* Doesn't touch memory */
635 FPU_R, FPU_W, /* Reads/writes memory */
636
sewardj3d7c9c82003-03-26 21:08:13 +0000637 /* ------------ MMX ops ------------ */
sewardj8cec6ee2003-05-18 11:56:39 +0000638 /* In this and the SSE encoding, bytes at higher addresses are
639 held in bits [7:0] in these 16-bit words. I guess this means
640 it is a big-endian encoding. */
sewardj3d7c9c82003-03-26 21:08:13 +0000641
642 /* 1 byte, no memrefs, no iregdefs, copy exactly to the
643 output. Held in val1[7:0]. */
644 MMX1,
645
646 /* 2 bytes, no memrefs, no iregdefs, copy exactly to the
647 output. Held in val1[15:0]. */
648 MMX2,
649
650 /* 3 bytes, no memrefs, no iregdefs, copy exactly to the
651 output. Held in val1[15:0] and val2[7:0]. */
652 MMX3,
653
654 /* 2 bytes, reads/writes mem. Insns of the form
655 bbbbbbbb:mod mmxreg r/m.
656 Held in val1[15:0], and mod and rm are to be replaced
daywalker5d945de2003-09-26 00:32:53 +0000657 at codegen time by a reference to the Temp/RealReg holding
sewardj3d7c9c82003-03-26 21:08:13 +0000658 the address. Arg2 holds this Temp/Real Reg.
659 Transfer is always at size 8.
660 */
661 MMX2_MemRd,
662 MMX2_MemWr,
663
sewardj4fbe6e92003-06-15 21:54:34 +0000664 /* 2 bytes, reads/writes an integer ("E") register. Insns of the form
sewardj3d7c9c82003-03-26 21:08:13 +0000665 bbbbbbbb:11 mmxreg ireg.
666 Held in val1[15:0], and ireg is to be replaced
667 at codegen time by a reference to the relevant RealReg.
668 Transfer is always at size 4. Arg2 holds this Temp/Real Reg.
669 */
sewardj4fbe6e92003-06-15 21:54:34 +0000670 MMX2_ERegRd,
671 MMX2_ERegWr,
sewardj3d7c9c82003-03-26 21:08:13 +0000672
sewardj8cec6ee2003-05-18 11:56:39 +0000673 /* ------------ SSE/SSE2 ops ------------ */
674 /* In the following:
675
sewardjfebaa3b2003-05-25 01:07:34 +0000676 a digit N indicates the next N bytes are to be copied exactly
677 to the output.
sewardj8cec6ee2003-05-18 11:56:39 +0000678
679 'a' indicates a mod-xmmreg-rm byte, where the mod-rm part is
680 to be replaced at codegen time to a Temp/RealReg holding the
681 address.
682
sewardj4fbe6e92003-06-15 21:54:34 +0000683 'e' indicates a byte of the form '11 xmmreg ireg', where ireg
684 is read or written, and is to be replaced at codegen time by
685 a reference to the relevant RealReg. 'e' because it's the E
686 reg in Intel encoding parlance.
sewardj8cec6ee2003-05-18 11:56:39 +0000687
sewardj4fbe6e92003-06-15 21:54:34 +0000688 'g' indicates a byte of the form '11 ireg xmmreg', where ireg
689 is read or written, and is to be replaced at codegen time by
690 a reference to the relevant RealReg. 'g' because it's called
691 G in Intel parlance. */
sewardj8cec6ee2003-05-18 11:56:39 +0000692
693 /* 3 bytes, no memrefs, no iregdefs, copy exactly to the
694 output. Held in val1[15:0] and val2[7:0]. */
695 SSE3,
696
697 /* 3 bytes, reads/writes mem. Insns of the form
698 bbbbbbbb:bbbbbbbb:mod mmxreg r/m.
699 Held in val1[15:0] and val2[7:0], and mod and rm are to be
700 replaced at codegen time by a reference to the Temp/RealReg
701 holding the address. Arg3 holds this Temp/Real Reg.
sewardjfebaa3b2003-05-25 01:07:34 +0000702 Transfer is usually, but not always, at size 16. */
sewardj8cec6ee2003-05-18 11:56:39 +0000703 SSE2a_MemRd,
704 SSE2a_MemWr,
705
nethercoteb1affa82004-01-19 19:14:18 +0000706 /* 4 bytes, writes an integer register. Insns of the form
707 bbbbbbbb:bbbbbbbb:11 ireg bbb.
708 Held in val1[15:0] and val2[7:0], and ireg is to be replaced
709 at codegen time by a reference to the relevant RealReg.
710 Transfer is always at size 4. Arg3 holds this Temp/Real Reg.
711 */
712 SSE2g_RegWr,
713
714 /* 5 bytes, writes an integer register. Insns of the form
715 bbbbbbbb:bbbbbbbb:11 ireg bbb :bbbbbbbb. Held in
716 val1[15:0] and val2[7:0] and lit32[7:0], and ireg is to be
717 replaced at codegen time by a reference to the relevant
718 RealReg. Transfer is always at size 4. Arg3 holds this
719 Temp/Real Reg.
720 */
721 SSE2g1_RegWr,
722
723 /* 5 bytes, reads an integer register. Insns of the form
724 bbbbbbbb:bbbbbbbb:11 bbb ireg :bbbbbbbb. Held in
725 val1[15:0] and val2[7:0] and lit32[7:0], and ireg is to be
726 replaced at codegen time by a reference to the relevant
727 RealReg. Transfer is always at size 4. Arg3 holds this
728 Temp/Real Reg.
729 */
730 SSE2e1_RegRd,
731
sewardj8cec6ee2003-05-18 11:56:39 +0000732 /* 4 bytes, no memrefs, no iregdefs, copy exactly to the
733 output. Held in val1[15:0] and val2[15:0]. */
734 SSE4,
735
736 /* 4 bytes, reads/writes mem. Insns of the form
737 bbbbbbbb:bbbbbbbb:bbbbbbbb:mod mmxreg r/m.
738 Held in val1[15:0] and val2[15:0], and mod and rm are to be
739 replaced at codegen time by a reference to the Temp/RealReg
740 holding the address. Arg3 holds this Temp/Real Reg.
sewardj9dd209f2003-06-18 23:30:52 +0000741 Transfer is at stated size. */
sewardj8cec6ee2003-05-18 11:56:39 +0000742 SSE3a_MemRd,
743 SSE3a_MemWr,
744
745 /* 4 bytes, reads/writes mem. Insns of the form
746 bbbbbbbb:bbbbbbbb:mod mmxreg r/m:bbbbbbbb
747 Held in val1[15:0] and val2[15:0], and mod and rm are to be
748 replaced at codegen time by a reference to the Temp/RealReg
749 holding the address. Arg3 holds this Temp/Real Reg.
sewardj9dd209f2003-06-18 23:30:52 +0000750 Transfer is at stated size. */
sewardj8cec6ee2003-05-18 11:56:39 +0000751 SSE2a1_MemRd,
sewardj77d30a22003-10-19 08:18:52 +0000752
sewardj8cec6ee2003-05-18 11:56:39 +0000753 /* 4 bytes, writes an integer register. Insns of the form
sewardj4fbe6e92003-06-15 21:54:34 +0000754 bbbbbbbb:bbbbbbbb:bbbbbbbb:11 ireg bbb.
sewardj8cec6ee2003-05-18 11:56:39 +0000755 Held in val1[15:0] and val2[15:0], and ireg is to be replaced
756 at codegen time by a reference to the relevant RealReg.
757 Transfer is always at size 4. Arg3 holds this Temp/Real Reg.
758 */
759 SSE3g_RegWr,
760
sewardjb31b06d2003-06-13 00:26:02 +0000761 /* 5 bytes, writes an integer register. Insns of the form
762 bbbbbbbb:bbbbbbbb:bbbbbbbb: 11 ireg bbb :bbbbbbbb. Held in
763 val1[15:0] and val2[15:0] and lit32[7:0], and ireg is to be
764 replaced at codegen time by a reference to the relevant
765 RealReg. Transfer is always at size 4. Arg3 holds this
766 Temp/Real Reg.
767 */
768 SSE3g1_RegWr,
769
sewardj4fbe6e92003-06-15 21:54:34 +0000770 /* 4 bytes, reads an integer register. Insns of the form
771 bbbbbbbb:bbbbbbbb:bbbbbbbb:11 bbb ireg.
772 Held in val1[15:0] and val2[15:0], and ireg is to be replaced
773 at codegen time by a reference to the relevant RealReg.
774 Transfer is always at size 4. Arg3 holds this Temp/Real Reg.
775 */
776 SSE3e_RegRd,
sewardjabf8bf82003-06-15 22:28:05 +0000777 SSE3e_RegWr, /* variant that writes Ereg, not reads it */
sewardj4fbe6e92003-06-15 21:54:34 +0000778
sewardjb31b06d2003-06-13 00:26:02 +0000779 /* 5 bytes, reads an integer register. Insns of the form
780 bbbbbbbb:bbbbbbbb:bbbbbbbb: 11 bbb ireg :bbbbbbbb. Held in
781 val1[15:0] and val2[15:0] and lit32[7:0], and ireg is to be
782 replaced at codegen time by a reference to the relevant
783 RealReg. Transfer is always at size 4. Arg3 holds this
784 Temp/Real Reg.
785 */
sewardj4fbe6e92003-06-15 21:54:34 +0000786 SSE3e1_RegRd,
sewardj8cec6ee2003-05-18 11:56:39 +0000787
sewardj02af6bc2003-06-12 00:56:06 +0000788 /* 4 bytes, reads memory, writes an integer register, but is
789 nevertheless an SSE insn. The insn is of the form
790 bbbbbbbb:bbbbbbbb:bbbbbbbb:mod ireg rm where mod indicates
791 memory (ie is not 11b) and ireg is the int reg written. The
sewardje3891fa2003-06-15 03:13:48 +0000792 first 4 bytes are held in lit32[31:0] since there is
sewardj02af6bc2003-06-12 00:56:06 +0000793 insufficient space elsewhere. mod and rm are to be replaced
794 at codegen time by a reference to the Temp/RealReg holding
795 the address. Arg1 holds this Temp/RealReg. ireg is to be
796 replaced at codegen time by a reference to the relevant
797 RealReg in which the answer is to be written. Arg2 holds
798 this Temp/RealReg. Transfer to the destination reg is always
799 at size 4. However the memory read can be at sizes 4 or 8
sewardje3891fa2003-06-15 03:13:48 +0000800 and so this is what the sz field holds. Note that the 4th
801 byte of the instruction (the modrm byte) is redundant, but we
802 store it anyway so as to be consistent with all other SSE
803 uinstrs.
sewardj02af6bc2003-06-12 00:56:06 +0000804 */
805 SSE3ag_MemRd_RegWr,
sewardje3891fa2003-06-15 03:13:48 +0000806
sewardj8cec6ee2003-05-18 11:56:39 +0000807 /* 5 bytes, no memrefs, no iregdefs, copy exactly to the
808 output. Held in val1[15:0], val2[15:0] and val3[7:0]. */
809 SSE5,
sewardj77d30a22003-10-19 08:18:52 +0000810
sewardj8cec6ee2003-05-18 11:56:39 +0000811 /* 5 bytes, reads/writes mem. Insns of the form
812 bbbbbbbb:bbbbbbbb:bbbbbbbb:mod mmxreg r/m:bbbbbbbb
daywalker5d945de2003-09-26 00:32:53 +0000813 Held in val1[15:0], val2[15:0], lit32[7:0].
814 mod and rm are to be replaced at codegen time by a reference
815 to the Temp/RealReg holding the address. Arg3 holds this
sewardj8cec6ee2003-05-18 11:56:39 +0000816 Temp/Real Reg. Transfer is always at size 16. */
817 SSE3a1_MemRd,
sewardj77d30a22003-10-19 08:18:52 +0000818
sewardj3d7c9c82003-03-26 21:08:13 +0000819 /* ------------------------ */
820
njnd5bb0a52002-09-27 10:24:48 +0000821 /* Not strictly needed, but improve address calculation translations. */
njn25e49d8e72002-09-23 09:36:25 +0000822 LEA1, /* reg2 := const + reg1 */
823 LEA2, /* reg3 := const + reg1 + reg2 * 1,2,4 or 8 */
824
sewardj8cec6ee2003-05-18 11:56:39 +0000825 /* Hack for x86 REP insns. Jump to literal if TempReg/RealReg
826 is zero. */
njnd5bb0a52002-09-27 10:24:48 +0000827 JIFZ,
njn25e49d8e72002-09-23 09:36:25 +0000828
njnd5bb0a52002-09-27 10:24:48 +0000829 /* Advance the simulated %eip by some small (< 128) number. */
830 INCEIP,
njn25e49d8e72002-09-23 09:36:25 +0000831
sewardje1042472002-09-30 12:33:11 +0000832 /* Dealing with segment registers */
833 GETSEG, PUTSEG, /* simulated segment register <--> TempReg */
834 USESEG, /* (LDT/GDT index, virtual addr) --> linear addr */
835
njnd5bb0a52002-09-27 10:24:48 +0000836 /* Not for translating x86 calls -- only to call helpers */
837 CALLM_S, CALLM_E, /* Mark start/end of CALLM push/pop sequence */
838 PUSH, POP, CLEAR, /* Add/remove/zap args for helpers */
839 CALLM, /* Call assembly-code helper */
840
841 /* Not for translating x86 calls -- only to call C helper functions of
842 up to three arguments (or two if the functions has a return value).
843 Arguments and return value must be word-sized. More arguments can
844 be faked with global variables (eg. use VG_(set_global_var)()).
845
846 Seven possibilities: 'arg[123]' show where args go, 'ret' shows
847 where return value goes (if present).
daywalker5d945de2003-09-26 00:32:53 +0000848
njn25e49d8e72002-09-23 09:36:25 +0000849 CCALL(-, -, - ) void f(void)
850 CCALL(arg1, -, - ) void f(UInt arg1)
851 CCALL(arg1, arg2, - ) void f(UInt arg1, UInt arg2)
852 CCALL(arg1, arg2, arg3) void f(UInt arg1, UInt arg2, UInt arg3)
853 CCALL(-, -, ret ) UInt f(UInt)
854 CCALL(arg1, -, ret ) UInt f(UInt arg1)
njnd5bb0a52002-09-27 10:24:48 +0000855 CCALL(arg1, arg2, ret ) UInt f(UInt arg1, UInt arg2) */
njn25e49d8e72002-09-23 09:36:25 +0000856 CCALL,
857
njnd5bb0a52002-09-27 10:24:48 +0000858 /* This opcode makes it easy for skins that extend UCode to do this to
859 avoid opcode overlap:
njn25e49d8e72002-09-23 09:36:25 +0000860
daywalker5d945de2003-09-26 00:32:53 +0000861 enum { EU_OP1 = DUMMY_FINAL_UOPCODE + 1, ... }
862
njn25e49d8e72002-09-23 09:36:25 +0000863 WARNING: Do not add new opcodes after this one! They can be added
864 before, though. */
865 DUMMY_FINAL_UOPCODE
866 }
867 Opcode;
868
869
njnd5bb0a52002-09-27 10:24:48 +0000870/* Condition codes, using the Intel encoding. CondAlways is an extra. */
njn25e49d8e72002-09-23 09:36:25 +0000871typedef
872 enum {
873 CondO = 0, /* overflow */
874 CondNO = 1, /* no overflow */
875 CondB = 2, /* below */
876 CondNB = 3, /* not below */
877 CondZ = 4, /* zero */
878 CondNZ = 5, /* not zero */
879 CondBE = 6, /* below or equal */
880 CondNBE = 7, /* not below or equal */
881 CondS = 8, /* negative */
sewardje1042472002-09-30 12:33:11 +0000882 CondNS = 9, /* not negative */
njn25e49d8e72002-09-23 09:36:25 +0000883 CondP = 10, /* parity even */
884 CondNP = 11, /* not parity even */
885 CondL = 12, /* jump less */
886 CondNL = 13, /* not less */
887 CondLE = 14, /* less or equal */
888 CondNLE = 15, /* not less or equal */
889 CondAlways = 16 /* Jump always */
daywalker5d945de2003-09-26 00:32:53 +0000890 }
njn25e49d8e72002-09-23 09:36:25 +0000891 Condcode;
892
893
894/* Descriptions of additional properties of *unconditional* jumps. */
895typedef
896 enum {
897 JmpBoring=0, /* boring unconditional jump */
898 JmpCall=1, /* jump due to an x86 call insn */
899 JmpRet=2, /* jump due to an x86 ret insn */
900 JmpSyscall=3, /* do a system call, then jump */
fitzhardingea02f8812003-12-18 09:06:09 +0000901 JmpClientReq=4,/* do a client request, then jump */
902 JmpYield=5 /* do a yield, then jump */
njn25e49d8e72002-09-23 09:36:25 +0000903 }
904 JmpKind;
905
906
907/* Flags. User-level code can only read/write O(verflow), S(ign),
908 Z(ero), A(ux-carry), C(arry), P(arity), and may also write
909 D(irection). That's a total of 7 flags. A FlagSet is a bitset,
daywalker5d945de2003-09-26 00:32:53 +0000910 thusly:
njn25e49d8e72002-09-23 09:36:25 +0000911 76543210
912 DOSZACP
913 and bit 7 must always be zero since it is unused.
sewardj2370f3b2002-11-30 15:01:01 +0000914
915 Note: these Flag? values are **not** the positions in the actual
916 %eflags register. */
917
njn25e49d8e72002-09-23 09:36:25 +0000918typedef UChar FlagSet;
919
920#define FlagD (1<<6)
921#define FlagO (1<<5)
922#define FlagS (1<<4)
923#define FlagZ (1<<3)
924#define FlagA (1<<2)
925#define FlagC (1<<1)
926#define FlagP (1<<0)
927
928#define FlagsOSZACP (FlagO | FlagS | FlagZ | FlagA | FlagC | FlagP)
929#define FlagsOSZAP (FlagO | FlagS | FlagZ | FlagA | FlagP)
930#define FlagsOSZCP (FlagO | FlagS | FlagZ | FlagC | FlagP)
931#define FlagsOSACP (FlagO | FlagS | FlagA | FlagC | FlagP)
932#define FlagsSZACP ( FlagS | FlagZ | FlagA | FlagC | FlagP)
933#define FlagsSZAP ( FlagS | FlagZ | FlagA | FlagP)
934#define FlagsZCP ( FlagZ | FlagC | FlagP)
935#define FlagsOC (FlagO | FlagC )
936#define FlagsAC ( FlagA | FlagC )
937
938#define FlagsALL (FlagsOSZACP | FlagD)
939#define FlagsEmpty (FlagSet)0
940
941
sewardj2370f3b2002-11-30 15:01:01 +0000942/* flag positions in eflags */
943#define EFlagC (1 << 0) /* carry */
944#define EFlagP (1 << 2) /* parity */
sewardjfa492d42002-12-08 18:20:01 +0000945#define EFlagA (1 << 4) /* aux carry */
sewardj2370f3b2002-11-30 15:01:01 +0000946#define EFlagZ (1 << 6) /* zero */
947#define EFlagS (1 << 7) /* sign */
sewardjfa492d42002-12-08 18:20:01 +0000948#define EFlagD (1 << 10) /* direction */
sewardj2370f3b2002-11-30 15:01:01 +0000949#define EFlagO (1 << 11) /* overflow */
fitzhardingec2dbbac2004-01-23 23:09:01 +0000950#define EFlagID (1 << 21) /* changable if CPUID exists */
sewardj2370f3b2002-11-30 15:01:01 +0000951
njn25e49d8e72002-09-23 09:36:25 +0000952/* Liveness of general purpose registers, useful for code generation.
953 Reg rank order 0..N-1 corresponds to bits 0..N-1, ie. first
954 reg's liveness in bit 0, last reg's in bit N-1. Note that
955 these rankings don't match the Intel register ordering. */
956typedef UInt RRegSet;
957
958#define ALL_RREGS_DEAD 0 /* 0000...00b */
njne7c258c2002-10-03 08:57:01 +0000959#define ALL_RREGS_LIVE ((1 << VG_MAX_REALREGS)-1) /* 0011...11b */
njn25e49d8e72002-09-23 09:36:25 +0000960#define UNIT_RREGSET(rank) (1 << (rank))
961
962#define IS_RREG_LIVE(rank,rregs_live) (rregs_live & UNIT_RREGSET(rank))
963#define SET_RREG_LIVENESS(rank,rregs_live,b) \
964 do { RRegSet unit = UNIT_RREGSET(rank); \
965 if (b) rregs_live |= unit; \
966 else rregs_live &= ~unit; \
967 } while(0)
968
969
970/* A Micro (u)-instruction. */
971typedef
972 struct {
973 /* word 1 */
974 UInt lit32; /* 32-bit literal */
975
976 /* word 2 */
977 UShort val1; /* first operand */
978 UShort val2; /* second operand */
979
980 /* word 3 */
981 UShort val3; /* third operand */
982 UChar opcode; /* opcode */
jsewardfca60182004-01-04 23:30:55 +0000983 UShort size; /* data transfer size */
njn25e49d8e72002-09-23 09:36:25 +0000984
985 /* word 4 */
986 FlagSet flags_r; /* :: FlagSet */
987 FlagSet flags_w; /* :: FlagSet */
988 UChar tag1:4; /* first operand tag */
989 UChar tag2:4; /* second operand tag */
990 UChar tag3:4; /* third operand tag */
991 UChar extra4b:4; /* Spare field, used by WIDEN for src
992 -size, and by LEA2 for scale (1,2,4 or 8),
993 and by JMPs for original x86 instr size */
994
995 /* word 5 */
996 UChar cond; /* condition, for jumps */
997 Bool signed_widen:1; /* signed or unsigned WIDEN ? */
998 JmpKind jmpkind:3; /* additional properties of unconditional JMP */
999
daywalker5d945de2003-09-26 00:32:53 +00001000 /* Additional properties for UInstrs that call C functions:
njn25e49d8e72002-09-23 09:36:25 +00001001 - CCALL
1002 - PUT (when %ESP is the target)
1003 - possibly skin-specific UInstrs
1004 */
1005 UChar argc:2; /* Number of args, max 3 */
1006 UChar regparms_n:2; /* Number of args passed in registers */
1007 Bool has_ret_val:1; /* Function has return value? */
1008
1009 /* RealReg liveness; only sensical after reg alloc and liveness
1010 analysis done. This info is a little bit arch-specific --
1011 VG_MAX_REALREGS can vary on different architectures. Note that
1012 to use this information requires converting between register ranks
njn4ba5a792002-09-30 10:23:54 +00001013 and the Intel register numbers, using VG_(realreg_to_rank)()
1014 and/or VG_(rank_to_realreg)() */
daywalker5d945de2003-09-26 00:32:53 +00001015 RRegSet regs_live_after:VG_MAX_REALREGS;
njn25e49d8e72002-09-23 09:36:25 +00001016 }
1017 UInstr;
1018
1019
daywalker5d945de2003-09-26 00:32:53 +00001020typedef
njn810086f2002-11-14 12:42:47 +00001021 struct _UCodeBlock
njn25e49d8e72002-09-23 09:36:25 +00001022 UCodeBlock;
1023
njn810086f2002-11-14 12:42:47 +00001024extern Int VG_(get_num_instrs) (UCodeBlock* cb);
1025extern Int VG_(get_num_temps) (UCodeBlock* cb);
njn25e49d8e72002-09-23 09:36:25 +00001026
njn810086f2002-11-14 12:42:47 +00001027extern UInstr* VG_(get_instr) (UCodeBlock* cb, Int i);
1028extern UInstr* VG_(get_last_instr) (UCodeBlock* cb);
daywalker5d945de2003-09-26 00:32:53 +00001029
njn211b6ad2003-02-03 12:33:31 +00001030
njn25e49d8e72002-09-23 09:36:25 +00001031/*====================================================================*/
1032/*=== Instrumenting UCode ===*/
1033/*====================================================================*/
1034
njnf4ce3d32003-02-10 10:17:26 +00001035/* Maximum number of registers read or written by a single UInstruction. */
1036#define VG_MAX_REGS_USED 3
njn25e49d8e72002-09-23 09:36:25 +00001037
njnf4ce3d32003-02-10 10:17:26 +00001038/* Find what this instruction does to its regs, useful for
1039 analysis/optimisation passes. `tag' indicates whether we're considering
1040 TempRegs (pre-reg-alloc) or RealRegs (post-reg-alloc). `regs' is filled
1041 with the affected register numbers, `isWrites' parallels it and indicates
1042 if the reg is read or written. If a reg is read and written, it will
1043 appear twice in `regs'. `regs' and `isWrites' must be able to fit
1044 VG_MAX_REGS_USED elements. */
njn810086f2002-11-14 12:42:47 +00001045extern Int VG_(get_reg_usage) ( UInstr* u, Tag tag, Int* regs, Bool* isWrites );
njn25e49d8e72002-09-23 09:36:25 +00001046
1047
njnd5bb0a52002-09-27 10:24:48 +00001048/* Used to register helper functions to be called from generated code. A
1049 limited number of compact helpers can be registered; the code generated
1050 to call them is slightly shorter -- so register the mostly frequently
1051 called helpers as compact. */
njn25e49d8e72002-09-23 09:36:25 +00001052extern void VG_(register_compact_helper) ( Addr a );
1053extern void VG_(register_noncompact_helper) ( Addr a );
1054
1055
1056/* ------------------------------------------------------------------ */
1057/* Virtual register allocation */
1058
1059/* Get a new virtual register */
njn4ba5a792002-09-30 10:23:54 +00001060extern Int VG_(get_new_temp) ( UCodeBlock* cb );
njn25e49d8e72002-09-23 09:36:25 +00001061
1062/* Get a new virtual shadow register */
njn4ba5a792002-09-30 10:23:54 +00001063extern Int VG_(get_new_shadow) ( UCodeBlock* cb );
njn25e49d8e72002-09-23 09:36:25 +00001064
1065/* Get a virtual register's corresponding virtual shadow register */
1066#define SHADOW(tempreg) ((tempreg)+1)
1067
1068
1069/* ------------------------------------------------------------------ */
1070/* Low-level UInstr builders */
njn4ba5a792002-09-30 10:23:54 +00001071extern void VG_(new_NOP) ( UInstr* u );
1072extern void VG_(new_UInstr0) ( UCodeBlock* cb, Opcode opcode, Int sz );
1073extern void VG_(new_UInstr1) ( UCodeBlock* cb, Opcode opcode, Int sz,
njn25e49d8e72002-09-23 09:36:25 +00001074 Tag tag1, UInt val1 );
njn4ba5a792002-09-30 10:23:54 +00001075extern void VG_(new_UInstr2) ( UCodeBlock* cb, Opcode opcode, Int sz,
njn25e49d8e72002-09-23 09:36:25 +00001076 Tag tag1, UInt val1,
1077 Tag tag2, UInt val2 );
njn4ba5a792002-09-30 10:23:54 +00001078extern void VG_(new_UInstr3) ( UCodeBlock* cb, Opcode opcode, Int sz,
njn25e49d8e72002-09-23 09:36:25 +00001079 Tag tag1, UInt val1,
1080 Tag tag2, UInt val2,
1081 Tag tag3, UInt val3 );
njn25e49d8e72002-09-23 09:36:25 +00001082
daywalker5d945de2003-09-26 00:32:53 +00001083/* Set read/write/undefined flags. Undefined flags are treaten as written,
njn810086f2002-11-14 12:42:47 +00001084 but it's worth keeping them logically distinct. */
1085extern void VG_(set_flag_fields) ( UCodeBlock* cb, FlagSet fr, FlagSet fw,
1086 FlagSet fu);
njn4ba5a792002-09-30 10:23:54 +00001087extern void VG_(set_lit_field) ( UCodeBlock* cb, UInt lit32 );
1088extern void VG_(set_ccall_fields) ( UCodeBlock* cb, Addr fn, UChar argc,
1089 UChar regparms_n, Bool has_ret_val );
njn810086f2002-11-14 12:42:47 +00001090extern void VG_(set_cond_field) ( UCodeBlock* cb, Condcode code );
njn25e49d8e72002-09-23 09:36:25 +00001091
njn4ba5a792002-09-30 10:23:54 +00001092extern void VG_(copy_UInstr) ( UCodeBlock* cb, UInstr* instr );
1093
1094extern Bool VG_(any_flag_use)( UInstr* u );
njn25e49d8e72002-09-23 09:36:25 +00001095
njnac6c1762002-10-04 14:34:15 +00001096/* Macro versions of the above; just shorter to type. */
1097#define uInstr0 VG_(new_UInstr0)
1098#define uInstr1 VG_(new_UInstr1)
1099#define uInstr2 VG_(new_UInstr2)
1100#define uInstr3 VG_(new_UInstr3)
1101#define uLiteral VG_(set_lit_field)
1102#define uCCall VG_(set_ccall_fields)
njn810086f2002-11-14 12:42:47 +00001103#define uCond VG_(set_cond_field)
1104#define uFlagsRWU VG_(set_flag_fields)
njnac6c1762002-10-04 14:34:15 +00001105#define newTemp VG_(get_new_temp)
1106#define newShadow VG_(get_new_shadow)
1107
njn25e49d8e72002-09-23 09:36:25 +00001108/* Refer to `the last instruction stuffed in' (can be lvalue). */
1109#define LAST_UINSTR(cb) (cb)->instrs[(cb)->used-1]
1110
1111
1112/* ------------------------------------------------------------------ */
1113/* Higher-level UInstr sequence builders */
njn4ba5a792002-09-30 10:23:54 +00001114extern void VG_(call_helper_0_0) ( UCodeBlock* cb, Addr f);
1115extern void VG_(call_helper_1_0) ( UCodeBlock* cb, Addr f, UInt arg1,
1116 UInt regparms_n);
1117extern void VG_(call_helper_2_0) ( UCodeBlock* cb, Addr f, UInt arg1, UInt arg2,
1118 UInt regparms_n);
njn25e49d8e72002-09-23 09:36:25 +00001119
1120/* One way around the 3-arg C function limit is to pass args via global
njn6fefa1b2003-02-24 10:32:51 +00001121 * variables... ugly, but it works. This one puts a literal in there. */
njn4ba5a792002-09-30 10:23:54 +00001122extern void VG_(set_global_var) ( UCodeBlock* cb, Addr globvar_ptr, UInt val);
njn25e49d8e72002-09-23 09:36:25 +00001123
njn6fefa1b2003-02-24 10:32:51 +00001124/* This one puts the contents of a TempReg in the global variable. */
1125extern void VG_(set_global_var_tempreg) ( UCodeBlock* cb, Addr globvar_ptr,
1126 UInt t_val);
1127
njn25e49d8e72002-09-23 09:36:25 +00001128/* ------------------------------------------------------------------ */
njnd5bb0a52002-09-27 10:24:48 +00001129/* Allocating/freeing basic blocks of UCode */
njn810086f2002-11-14 12:42:47 +00001130extern UCodeBlock* VG_(setup_UCodeBlock) ( UCodeBlock* cb );
njn4ba5a792002-09-30 10:23:54 +00001131extern void VG_(free_UCodeBlock) ( UCodeBlock* cb );
njnd5bb0a52002-09-27 10:24:48 +00001132
1133/* ------------------------------------------------------------------ */
daywalker5d945de2003-09-26 00:32:53 +00001134/* UCode pretty/ugly printing. Probably only useful to call from a skin
njn25e49d8e72002-09-23 09:36:25 +00001135 if VG_(needs).extended_UCode == True. */
1136
1137/* When True, all generated code is/should be printed. */
1138extern Bool VG_(print_codegen);
1139
njn4ba5a792002-09-30 10:23:54 +00001140/* Pretty/ugly printing functions */
1141extern void VG_(pp_UCodeBlock) ( UCodeBlock* cb, Char* title );
1142extern void VG_(pp_UInstr) ( Int instrNo, UInstr* u );
1143extern void VG_(pp_UInstr_regs) ( Int instrNo, UInstr* u );
1144extern void VG_(up_UInstr) ( Int instrNo, UInstr* u );
1145extern Char* VG_(name_UOpcode) ( Bool upper, Opcode opc );
njn563f96f2003-02-03 11:17:46 +00001146extern Char* VG_(name_UCondcode) ( Condcode cond );
daywalker5d945de2003-09-26 00:32:53 +00001147extern void VG_(pp_UOperand) ( UInstr* u, Int operandNo,
njn4ba5a792002-09-30 10:23:54 +00001148 Int sz, Bool parens );
njn25e49d8e72002-09-23 09:36:25 +00001149
njnb93d1782003-02-03 12:03:22 +00001150/* ------------------------------------------------------------------ */
njnf4ce3d32003-02-10 10:17:26 +00001151/* Accessing archregs and their shadows */
1152extern UInt VG_(get_archreg) ( UInt archreg );
1153extern UInt VG_(get_thread_archreg) ( ThreadId tid, UInt archreg );
1154
njnb93d1782003-02-03 12:03:22 +00001155extern UInt VG_(get_shadow_archreg) ( UInt archreg );
1156extern void VG_(set_shadow_archreg) ( UInt archreg, UInt val );
njnd3040452003-05-19 15:04:06 +00001157extern void VG_(set_shadow_eflags) ( UInt val );
njnb93d1782003-02-03 12:03:22 +00001158extern Addr VG_(shadow_archreg_address) ( UInt archreg );
njn25e49d8e72002-09-23 09:36:25 +00001159
njnf4ce3d32003-02-10 10:17:26 +00001160extern UInt VG_(get_thread_shadow_archreg) ( ThreadId tid, UInt archreg );
1161extern void VG_(set_thread_shadow_archreg) ( ThreadId tid, UInt archreg,
1162 UInt val );
njn211b6ad2003-02-03 12:33:31 +00001163
1164/* ------------------------------------------------------------------ */
1165/* Offsets of addresses of helper functions. A "helper" function is one
1166 which is called from generated code via CALLM. */
1167
1168extern Int VGOFF_(helper_idiv_64_32);
1169extern Int VGOFF_(helper_div_64_32);
1170extern Int VGOFF_(helper_idiv_32_16);
1171extern Int VGOFF_(helper_div_32_16);
1172extern Int VGOFF_(helper_idiv_16_8);
1173extern Int VGOFF_(helper_div_16_8);
1174
1175extern Int VGOFF_(helper_imul_32_64);
1176extern Int VGOFF_(helper_mul_32_64);
1177extern Int VGOFF_(helper_imul_16_32);
1178extern Int VGOFF_(helper_mul_16_32);
1179extern Int VGOFF_(helper_imul_8_16);
1180extern Int VGOFF_(helper_mul_8_16);
1181
1182extern Int VGOFF_(helper_CLD);
1183extern Int VGOFF_(helper_STD);
1184extern Int VGOFF_(helper_get_dirflag);
1185
1186extern Int VGOFF_(helper_CLC);
1187extern Int VGOFF_(helper_STC);
1188
1189extern Int VGOFF_(helper_shldl);
1190extern Int VGOFF_(helper_shldw);
1191extern Int VGOFF_(helper_shrdl);
1192extern Int VGOFF_(helper_shrdw);
1193
1194extern Int VGOFF_(helper_RDTSC);
1195extern Int VGOFF_(helper_CPUID);
1196
daywalkerb18d2532003-09-27 20:15:01 +00001197extern Int VGOFF_(helper_IN);
1198extern Int VGOFF_(helper_OUT);
1199
njn211b6ad2003-02-03 12:33:31 +00001200extern Int VGOFF_(helper_bsf);
1201extern Int VGOFF_(helper_bsr);
1202
1203extern Int VGOFF_(helper_fstsw_AX);
1204extern Int VGOFF_(helper_SAHF);
njnd6251f12003-06-03 13:38:51 +00001205extern Int VGOFF_(helper_LAHF);
njn211b6ad2003-02-03 12:33:31 +00001206extern Int VGOFF_(helper_DAS);
1207extern Int VGOFF_(helper_DAA);
1208
muellerf217c732004-01-02 22:42:29 +00001209extern Int VGOFF_(helper_cmpxchg8b);
1210
njn211b6ad2003-02-03 12:33:31 +00001211
njn25e49d8e72002-09-23 09:36:25 +00001212/*====================================================================*/
njnd5bb0a52002-09-27 10:24:48 +00001213/*=== Generating x86 code from UCode ===*/
njn25e49d8e72002-09-23 09:36:25 +00001214/*====================================================================*/
1215
njnd5bb0a52002-09-27 10:24:48 +00001216/* All this only necessary for skins with VG_(needs).extends_UCode == True. */
njn25e49d8e72002-09-23 09:36:25 +00001217
sewardje1042472002-09-30 12:33:11 +00001218/* This is the Intel register encoding -- integer regs. */
njn25e49d8e72002-09-23 09:36:25 +00001219#define R_EAX 0
1220#define R_ECX 1
1221#define R_EDX 2
1222#define R_EBX 3
1223#define R_ESP 4
1224#define R_EBP 5
1225#define R_ESI 6
1226#define R_EDI 7
1227
1228#define R_AL (0+R_EAX)
1229#define R_CL (0+R_ECX)
1230#define R_DL (0+R_EDX)
1231#define R_BL (0+R_EBX)
1232#define R_AH (4+R_EAX)
1233#define R_CH (4+R_ECX)
1234#define R_DH (4+R_EDX)
1235#define R_BH (4+R_EBX)
1236
sewardje1042472002-09-30 12:33:11 +00001237/* This is the Intel register encoding -- segment regs. */
1238#define R_ES 0
1239#define R_CS 1
1240#define R_SS 2
1241#define R_DS 3
1242#define R_FS 4
1243#define R_GS 5
1244
njn25e49d8e72002-09-23 09:36:25 +00001245/* For pretty printing x86 code */
daywalker5d945de2003-09-26 00:32:53 +00001246extern const Char* VG_(name_of_mmx_gran) ( UChar gran );
1247extern const Char* VG_(name_of_mmx_reg) ( Int mmxreg );
1248extern const Char* VG_(name_of_seg_reg) ( Int sreg );
1249extern const Char* VG_(name_of_int_reg) ( Int size, Int reg );
1250extern const Char VG_(name_of_int_size) ( Int size );
njn25e49d8e72002-09-23 09:36:25 +00001251
njnac6c1762002-10-04 14:34:15 +00001252/* Shorter macros for convenience */
sewardj3d7c9c82003-03-26 21:08:13 +00001253#define nameIReg VG_(name_of_int_reg)
1254#define nameISize VG_(name_of_int_size)
1255#define nameSReg VG_(name_of_seg_reg)
1256#define nameMMXReg VG_(name_of_mmx_reg)
1257#define nameMMXGran VG_(name_of_mmx_gran)
sewardjfebaa3b2003-05-25 01:07:34 +00001258#define nameXMMReg VG_(name_of_xmm_reg)
njnac6c1762002-10-04 14:34:15 +00001259
njn25e49d8e72002-09-23 09:36:25 +00001260/* Randomly useful things */
1261extern UInt VG_(extend_s_8to32) ( UInt x );
1262
1263/* Code emitters */
njn4ba5a792002-09-30 10:23:54 +00001264extern void VG_(emitB) ( UInt b );
1265extern void VG_(emitW) ( UInt w );
1266extern void VG_(emitL) ( UInt l );
sewardjfa492d42002-12-08 18:20:01 +00001267extern void VG_(new_emit) ( Bool upd_cc, FlagSet uses_flags, FlagSet sets_flags );
njn25e49d8e72002-09-23 09:36:25 +00001268
1269/* Finding offsets */
njn4ba5a792002-09-30 10:23:54 +00001270extern Int VG_(helper_offset) ( Addr a );
1271extern Int VG_(shadow_reg_offset) ( Int arch );
1272extern Int VG_(shadow_flags_offset) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001273
njnd5bb0a52002-09-27 10:24:48 +00001274/* Convert reg ranks <-> Intel register ordering, for using register
1275 liveness information. */
njn4ba5a792002-09-30 10:23:54 +00001276extern Int VG_(realreg_to_rank) ( Int realreg );
1277extern Int VG_(rank_to_realreg) ( Int rank );
njn25e49d8e72002-09-23 09:36:25 +00001278
njnd5bb0a52002-09-27 10:24:48 +00001279/* Call a subroutine. Does no argument passing, stack manipulations, etc. */
daywalker5d945de2003-09-26 00:32:53 +00001280extern void VG_(synth_call) ( Bool ensure_shortform, Int word_offset,
sewardjfa492d42002-12-08 18:20:01 +00001281 Bool upd_cc, FlagSet use_flags, FlagSet set_flags );
njn25e49d8e72002-09-23 09:36:25 +00001282
njnd5bb0a52002-09-27 10:24:48 +00001283/* For calling C functions -- saves caller save regs, pushes args, calls,
1284 clears the stack, restores caller save regs. `fn' must be registered in
1285 the baseBlock first. Acceptable tags are RealReg and Literal. Optimises
1286 things, eg. by not preserving non-live caller-save registers.
njn25e49d8e72002-09-23 09:36:25 +00001287
njnd5bb0a52002-09-27 10:24:48 +00001288 WARNING: a UInstr should *not* be translated with synth_ccall() followed
1289 by some other x86 assembly code; this will invalidate the results of
1290 vg_realreg_liveness_analysis() and everything will fall over. */
njn4ba5a792002-09-30 10:23:54 +00001291extern void VG_(synth_ccall) ( Addr fn, Int argc, Int regparms_n, UInt argv[],
daywalker5d945de2003-09-26 00:32:53 +00001292 Tag tagv[], Int ret_reg,
njn4ba5a792002-09-30 10:23:54 +00001293 RRegSet regs_live_before,
1294 RRegSet regs_live_after );
njn25e49d8e72002-09-23 09:36:25 +00001295
1296/* Addressing modes */
njn4ba5a792002-09-30 10:23:54 +00001297extern void VG_(emit_amode_offregmem_reg)( Int off, Int regmem, Int reg );
1298extern void VG_(emit_amode_ereg_greg) ( Int e_reg, Int g_reg );
njn25e49d8e72002-09-23 09:36:25 +00001299
1300/* v-size (4, or 2 with OSO) insn emitters */
njn4ba5a792002-09-30 10:23:54 +00001301extern void VG_(emit_movv_offregmem_reg) ( Int sz, Int off, Int areg, Int reg );
1302extern void VG_(emit_movv_reg_offregmem) ( Int sz, Int reg, Int off, Int areg );
1303extern void VG_(emit_movv_reg_reg) ( Int sz, Int reg1, Int reg2 );
sewardjfa492d42002-12-08 18:20:01 +00001304extern void VG_(emit_nonshiftopv_lit_reg)( Bool upd_cc, Int sz, Opcode opc, UInt lit,
njn4ba5a792002-09-30 10:23:54 +00001305 Int reg );
sewardjfa492d42002-12-08 18:20:01 +00001306extern void VG_(emit_shiftopv_lit_reg) ( Bool upd_cc, Int sz, Opcode opc, UInt lit,
njn4ba5a792002-09-30 10:23:54 +00001307 Int reg );
sewardjfa492d42002-12-08 18:20:01 +00001308extern void VG_(emit_nonshiftopv_reg_reg)( Bool upd_cc, Int sz, Opcode opc,
njn4ba5a792002-09-30 10:23:54 +00001309 Int reg1, Int reg2 );
1310extern void VG_(emit_movv_lit_reg) ( Int sz, UInt lit, Int reg );
sewardjfa492d42002-12-08 18:20:01 +00001311extern void VG_(emit_unaryopv_reg) ( Bool upd_cc, Int sz, Opcode opc, Int reg );
njn4ba5a792002-09-30 10:23:54 +00001312extern void VG_(emit_pushv_reg) ( Int sz, Int reg );
1313extern void VG_(emit_popv_reg) ( Int sz, Int reg );
njn25e49d8e72002-09-23 09:36:25 +00001314
njn4ba5a792002-09-30 10:23:54 +00001315extern void VG_(emit_pushl_lit32) ( UInt int32 );
1316extern void VG_(emit_pushl_lit8) ( Int lit8 );
sewardjfa492d42002-12-08 18:20:01 +00001317extern void VG_(emit_cmpl_zero_reg) ( Bool upd_cc, Int reg );
njn4ba5a792002-09-30 10:23:54 +00001318extern void VG_(emit_swapl_reg_EAX) ( Int reg );
1319extern void VG_(emit_movv_lit_offregmem) ( Int sz, UInt lit, Int off,
1320 Int memreg );
njn25e49d8e72002-09-23 09:36:25 +00001321
1322/* b-size (1 byte) instruction emitters */
njn4ba5a792002-09-30 10:23:54 +00001323extern void VG_(emit_movb_lit_offregmem) ( UInt lit, Int off, Int memreg );
1324extern void VG_(emit_movb_reg_offregmem) ( Int reg, Int off, Int areg );
sewardjfa492d42002-12-08 18:20:01 +00001325extern void VG_(emit_unaryopb_reg) ( Bool upd_cc, Opcode opc, Int reg );
1326extern void VG_(emit_testb_lit_reg) ( Bool upd_cc, UInt lit, Int reg );
njn25e49d8e72002-09-23 09:36:25 +00001327
1328/* zero-extended load emitters */
fitzhardinge98abfc72003-12-16 02:05:15 +00001329extern void VG_(emit_movzbl_offregmem_reg) ( Bool bounds, Int off, Int regmem, Int reg );
1330extern void VG_(emit_movzwl_offregmem_reg) ( Bool bounds, Int off, Int areg, Int reg );
1331extern void VG_(emit_movzwl_regmem_reg) ( Bool bounds, Int reg1, Int reg2 );
njn25e49d8e72002-09-23 09:36:25 +00001332
1333/* misc instruction emitters */
njn4ba5a792002-09-30 10:23:54 +00001334extern void VG_(emit_call_reg) ( Int reg );
1335extern void VG_(emit_add_lit_to_esp) ( Int lit );
njn4ba5a792002-09-30 10:23:54 +00001336extern void VG_(emit_pushal) ( void );
1337extern void VG_(emit_popal) ( void );
1338extern void VG_(emit_AMD_prefetch_reg) ( Int reg );
njn25e49d8e72002-09-23 09:36:25 +00001339
sewardja2113f92002-12-12 23:42:48 +00001340/* jump emitters */
1341extern void VG_(init_target) ( Int *tgt );
1342
1343extern void VG_(target_back) ( Int *tgt );
1344extern void VG_(target_forward) ( Int *tgt );
1345extern void VG_(emit_target_delta) ( Int *tgt );
1346
fitzhardinge462f4f92003-12-18 02:10:54 +00001347typedef enum {
1348 JP_NONE, /* no prediction */
1349 JP_TAKEN, /* predict taken */
1350 JP_NOT_TAKEN, /* predict not taken */
1351} JumpPred;
1352
1353extern void VG_(emit_jcondshort_delta) ( Bool simd_cc, Condcode cond, Int delta, JumpPred );
1354extern void VG_(emit_jcondshort_target)( Bool simd_cc, Condcode cond, Int *tgt, JumpPred );
sewardja2113f92002-12-12 23:42:48 +00001355
njn25e49d8e72002-09-23 09:36:25 +00001356
1357/*====================================================================*/
1358/*=== Execution contexts ===*/
1359/*====================================================================*/
1360
1361/* Generic resolution type used in a few different ways, such as deciding
1362 how closely to compare two errors for equality. */
daywalker5d945de2003-09-26 00:32:53 +00001363typedef
1364 enum { Vg_LowRes, Vg_MedRes, Vg_HighRes }
njn25e49d8e72002-09-23 09:36:25 +00001365 VgRes;
1366
1367typedef
1368 struct _ExeContext
1369 ExeContext;
1370
daywalker5d945de2003-09-26 00:32:53 +00001371/* Compare two ExeContexts. Number of callers considered depends on `res':
1372 Vg_LowRes: 2
1373 Vg_MedRes: 4
njnd5bb0a52002-09-27 10:24:48 +00001374 Vg_HighRes: all */
njn25e49d8e72002-09-23 09:36:25 +00001375extern Bool VG_(eq_ExeContext) ( VgRes res,
1376 ExeContext* e1, ExeContext* e2 );
1377
1378/* Print an ExeContext. */
1379extern void VG_(pp_ExeContext) ( ExeContext* );
1380
1381/* Take a snapshot of the client's stack. Search our collection of
1382 ExeContexts to see if we already have it, and if not, allocate a
njn6c846552003-09-16 07:41:43 +00001383 new one. Either way, return a pointer to the context. Context size
1384 controlled by --num-callers option.
daywalker5d945de2003-09-26 00:32:53 +00001385
njn72718642003-07-24 08:45:32 +00001386 If called from generated code, use VG_(get_current_tid)() to get the
1387 current ThreadId. If called from non-generated code, the current
daywalker5d945de2003-09-26 00:32:53 +00001388 ThreadId should be passed in by the core.
njn7b456f52003-05-19 11:16:50 +00001389*/
njn72718642003-07-24 08:45:32 +00001390extern ExeContext* VG_(get_ExeContext) ( ThreadId tid );
njn25e49d8e72002-09-23 09:36:25 +00001391
njn6c846552003-09-16 07:41:43 +00001392/* Get the nth EIP from the ExeContext. 0 is the EIP of the top function, 1
1393 is its caller, etc. Returns 0 if there isn't one, or if n is greater
1394 than VG_(clo_backtrace_size), set by the --num-callers option. */
1395extern Addr VG_(get_EIP_from_ExeContext) ( ExeContext* e, UInt n );
1396
sewardj499e3de2002-11-13 22:22:25 +00001397/* Just grab the client's EIP, as a much smaller and cheaper
njn72718642003-07-24 08:45:32 +00001398 indication of where they are. Use is basically same as for
daywalker5d945de2003-09-26 00:32:53 +00001399 VG_(get_ExeContext)() above.
njn72718642003-07-24 08:45:32 +00001400*/
1401extern Addr VG_(get_EIP)( ThreadId tid );
njn25e49d8e72002-09-23 09:36:25 +00001402
njn6c846552003-09-16 07:41:43 +00001403/* For skins needing more control over stack traces: walks the stack to get
1404 %eips from the top stack frames for thread 'tid'. Maximum of 'n_eips'
1405 addresses put into 'eips'; 0 is the top of the stack, 1 is its caller,
1406 etc. */
1407extern UInt VG_(stack_snapshot) ( ThreadId tid, Addr* eips, UInt n_eips );
1408
1409/* Does the same thing as VG_(pp_ExeContext)(), just with slightly
1410 different input. */
1411extern void VG_(mini_stack_dump) ( Addr eips[], UInt n_eips );
1412
njn211b6ad2003-02-03 12:33:31 +00001413
njn25e49d8e72002-09-23 09:36:25 +00001414/*====================================================================*/
1415/*=== Error reporting ===*/
1416/*====================================================================*/
1417
1418/* ------------------------------------------------------------------ */
daywalker5d945de2003-09-26 00:32:53 +00001419/* Suppressions describe errors which we want to suppress, ie, not
njn25e49d8e72002-09-23 09:36:25 +00001420 show the user, usually because it is caused by a problem in a library
daywalker5d945de2003-09-26 00:32:53 +00001421 which we can't fix, replace or work around. Suppressions are read from
njnd5bb0a52002-09-27 10:24:48 +00001422 a file at startup time. This gives flexibility so that new
njn25e49d8e72002-09-23 09:36:25 +00001423 suppressions can be added to the file as and when needed.
1424*/
1425
1426typedef
1427 Int /* Do not make this unsigned! */
1428 SuppKind;
1429
njn810086f2002-11-14 12:42:47 +00001430/* The skin-relevant parts of a suppression are:
1431 kind: what kind of suppression; must be in the range (0..)
1432 string: use is optional. NULL by default.
1433 extra: use is optional. NULL by default. void* so it's extensible.
njn25e49d8e72002-09-23 09:36:25 +00001434*/
1435typedef
njn810086f2002-11-14 12:42:47 +00001436 struct _Supp
1437 Supp;
1438
1439/* Useful in SK_(error_matches_suppression)() */
1440SuppKind VG_(get_supp_kind) ( Supp* su );
1441Char* VG_(get_supp_string) ( Supp* su );
1442void* VG_(get_supp_extra) ( Supp* su );
1443
1444/* Must be used in VG_(recognised_suppression)() */
1445void VG_(set_supp_kind) ( Supp* su, SuppKind suppkind );
1446/* May be used in VG_(read_extra_suppression_info)() */
1447void VG_(set_supp_string) ( Supp* su, Char* string );
1448void VG_(set_supp_extra) ( Supp* su, void* extra );
njn25e49d8e72002-09-23 09:36:25 +00001449
1450
1451/* ------------------------------------------------------------------ */
1452/* Error records contain enough info to generate an error report. The idea
1453 is that (typically) the same few points in the program generate thousands
njnd5bb0a52002-09-27 10:24:48 +00001454 of errors, and we don't want to spew out a fresh error message for each
1455 one. Instead, we use these structures to common up duplicates.
njn25e49d8e72002-09-23 09:36:25 +00001456*/
1457
1458typedef
1459 Int /* Do not make this unsigned! */
1460 ErrorKind;
1461
njn810086f2002-11-14 12:42:47 +00001462/* The skin-relevant parts of an Error are:
1463 kind: what kind of error; must be in the range (0..)
1464 addr: use is optional. 0 by default.
1465 string: use is optional. NULL by default.
1466 extra: use is optional. NULL by default. void* so it's extensible.
njn25e49d8e72002-09-23 09:36:25 +00001467*/
1468typedef
njn810086f2002-11-14 12:42:47 +00001469 struct _Error
1470 Error;
njn25e49d8e72002-09-23 09:36:25 +00001471
njn810086f2002-11-14 12:42:47 +00001472/* Useful in SK_(error_matches_suppression)(), SK_(pp_SkinError)(), etc */
njnb877d492003-01-28 20:40:57 +00001473ExeContext* VG_(get_error_where) ( Error* err );
1474SuppKind VG_(get_error_kind) ( Error* err );
1475Addr VG_(get_error_address) ( Error* err );
1476Char* VG_(get_error_string) ( Error* err );
1477void* VG_(get_error_extra) ( Error* err );
njn25e49d8e72002-09-23 09:36:25 +00001478
njnd5bb0a52002-09-27 10:24:48 +00001479/* Call this when an error occurs. It will be recorded if it hasn't been
njn25e49d8e72002-09-23 09:36:25 +00001480 seen before. If it has, the existing error record will have its count
daywalker5d945de2003-09-26 00:32:53 +00001481 incremented.
1482
njn72718642003-07-24 08:45:32 +00001483 'tid' can be found as for VG_(get_ExeContext)(). The `extra' field can
1484 be stack-allocated; it will be copied by the core if needed (but it
1485 won't be copied if it's NULL).
njnd5bb0a52002-09-27 10:24:48 +00001486
1487 If no 'a', 's' or 'extra' of interest needs to be recorded, just use
njn3e884182003-04-15 13:03:23 +00001488 NULL for them. */
daywalker5d945de2003-09-26 00:32:53 +00001489extern void VG_(maybe_record_error) ( ThreadId tid, ErrorKind ekind,
njn25e49d8e72002-09-23 09:36:25 +00001490 Addr a, Char* s, void* extra );
1491
njn43c799e2003-04-08 00:08:52 +00001492/* Similar to VG_(maybe_record_error)(), except this one doesn't record the
1493 error -- useful for errors that can only happen once. The errors can be
1494 suppressed, though. Return value is True if it was suppressed.
daywalker5d945de2003-09-26 00:32:53 +00001495 `print_error' dictates whether to print the error, which is a bit of a
njn43c799e2003-04-08 00:08:52 +00001496 hack that's useful sometimes if you just want to know if the error would
daywalker5d945de2003-09-26 00:32:53 +00001497 be suppressed without possibly printing it. `count_error' dictates
njn47363ab2003-04-21 13:24:40 +00001498 whether to add the error in the error total count (another mild hack). */
njn72718642003-07-24 08:45:32 +00001499extern Bool VG_(unique_error) ( ThreadId tid, ErrorKind ekind,
njn43c799e2003-04-08 00:08:52 +00001500 Addr a, Char* s, void* extra,
njn3e884182003-04-15 13:03:23 +00001501 ExeContext* where, Bool print_error,
njn47363ab2003-04-21 13:24:40 +00001502 Bool allow_GDB_attach, Bool count_error );
njn43c799e2003-04-08 00:08:52 +00001503
njn25e49d8e72002-09-23 09:36:25 +00001504/* Gets a non-blank, non-comment line of at most nBuf chars from fd.
daywalker5d945de2003-09-26 00:32:53 +00001505 Skips leading spaces on the line. Returns True if EOF was hit instead.
njn3e884182003-04-15 13:03:23 +00001506 Useful for reading in extra skin-specific suppression lines. */
njn4ba5a792002-09-30 10:23:54 +00001507extern Bool VG_(get_line) ( Int fd, Char* buf, Int nBuf );
njn25e49d8e72002-09-23 09:36:25 +00001508
1509
1510/*====================================================================*/
1511/*=== Obtaining debug information ===*/
1512/*====================================================================*/
1513
sewardj6e008cb2002-12-15 13:11:39 +00001514/* Get the file/function/line number of the instruction at address
1515 'a'. For these four, if debug info for the address is found, it
1516 copies the info into the buffer/UInt and returns True. If not, it
1517 returns False and nothing is copied. VG_(get_fnname) always
1518 demangles C++ function names. VG_(get_fnname_w_offset) is the
njn3e884182003-04-15 13:03:23 +00001519 same, except it appends "+N" to symbol names to indicate offsets. */
njn25e49d8e72002-09-23 09:36:25 +00001520extern Bool VG_(get_filename) ( Addr a, Char* filename, Int n_filename );
1521extern Bool VG_(get_fnname) ( Addr a, Char* fnname, Int n_fnname );
1522extern Bool VG_(get_linenum) ( Addr a, UInt* linenum );
daywalker5d945de2003-09-26 00:32:53 +00001523extern Bool VG_(get_fnname_w_offset)
sewardj6e008cb2002-12-15 13:11:39 +00001524 ( Addr a, Char* fnname, Int n_fnname );
njn25e49d8e72002-09-23 09:36:25 +00001525
1526/* This one is more efficient if getting both filename and line number,
1527 because the two lookups are done together. */
daywalker5d945de2003-09-26 00:32:53 +00001528extern Bool VG_(get_filename_linenum)
njn25e49d8e72002-09-23 09:36:25 +00001529 ( Addr a, Char* filename, Int n_filename,
1530 UInt* linenum );
1531
1532/* Succeeds only if we find from debug info that 'a' is the address of the
1533 first instruction in a function -- as opposed to VG_(get_fnname) which
1534 succeeds if we find from debug info that 'a' is the address of any
1535 instruction in a function. Use this to instrument the start of
njnd5bb0a52002-09-27 10:24:48 +00001536 a particular function. Nb: if an executable/shared object is stripped
njn25e49d8e72002-09-23 09:36:25 +00001537 of its symbols, this function will not be able to recognise function
1538 entry points within it. */
njn497ec3f2003-05-19 08:38:23 +00001539extern Bool VG_(get_fnname_if_entry) ( Addr a, Char* fnname, Int n_fnname );
njn25e49d8e72002-09-23 09:36:25 +00001540
1541/* Succeeds if the address is within a shared object or the main executable.
1542 It doesn't matter if debug info is present or not. */
1543extern Bool VG_(get_objname) ( Addr a, Char* objname, Int n_objname );
1544
njn6c846552003-09-16 07:41:43 +00001545/* Puts into 'buf' info about the code address %eip: the address, function
1546 name (if known) and filename/line number (if known), like this:
1547
1548 0x4001BF05: realloc (vg_replace_malloc.c:339)
1549
1550 'n_buf' gives length of 'buf'. Returns 'buf'.
1551*/
1552extern Char* VG_(describe_eip)(Addr eip, Char* buf, Int n_buf);
1553
jsgfcb1d1c02003-10-14 21:55:10 +00001554/* Returns a string containing an expression for the given
1555 address. String is malloced with VG_(malloc)() */
1556Char *VG_(describe_addr)(ThreadId, Addr);
1557
sewardj47104382002-10-20 18:35:48 +00001558/* A way to get information about what segments are mapped */
1559typedef struct _SegInfo SegInfo;
1560
njnb877d492003-01-28 20:40:57 +00001561/* Returns NULL if the SegInfo isn't found. It doesn't matter if debug info
1562 is present or not. */
1563extern SegInfo* VG_(get_obj) ( Addr a );
1564
1565extern const SegInfo* VG_(next_seginfo) ( const SegInfo *seg );
1566extern Addr VG_(seg_start) ( const SegInfo *seg );
1567extern UInt VG_(seg_size) ( const SegInfo *seg );
1568extern const UChar* VG_(seg_filename) ( const SegInfo *seg );
1569extern UInt VG_(seg_sym_offset)( const SegInfo *seg );
sewardj47104382002-10-20 18:35:48 +00001570
1571typedef
1572 enum {
1573 Vg_SectUnknown,
1574 Vg_SectText,
sewardj8fe15a32002-10-20 19:29:21 +00001575 Vg_SectData,
1576 Vg_SectBSS,
sewardj47104382002-10-20 18:35:48 +00001577 Vg_SectGOT,
sewardj8fe15a32002-10-20 19:29:21 +00001578 Vg_SectPLT,
sewardj47104382002-10-20 18:35:48 +00001579 }
1580 VgSectKind;
1581
1582extern VgSectKind VG_(seg_sect_kind)(Addr);
1583
njn25e49d8e72002-09-23 09:36:25 +00001584
1585/*====================================================================*/
njn3e884182003-04-15 13:03:23 +00001586/*=== Generic hash table ===*/
njn25e49d8e72002-09-23 09:36:25 +00001587/*====================================================================*/
1588
njn3e884182003-04-15 13:03:23 +00001589/* Generic type for a separately-chained hash table. Via a kind of dodgy
1590 C-as-C++ style inheritance, skins can extend the VgHashNode type, so long
1591 as the first two fields match the sizes of these two fields. Requires
1592 a bit of casting by the skin. */
njn25e49d8e72002-09-23 09:36:25 +00001593typedef
njn3e884182003-04-15 13:03:23 +00001594 struct _VgHashNode {
1595 struct _VgHashNode * next;
1596 UInt key;
1597 }
1598 VgHashNode;
njn25e49d8e72002-09-23 09:36:25 +00001599
njn3e884182003-04-15 13:03:23 +00001600typedef
1601 VgHashNode**
1602 VgHashTable;
njn810086f2002-11-14 12:42:47 +00001603
njn3e884182003-04-15 13:03:23 +00001604/* Make a new table. */
1605extern VgHashTable VG_(HT_construct) ( void );
1606
njn69c06872003-09-30 15:43:51 +00001607/* Count the number of nodes in a table. */
1608extern Int VG_(HT_count_nodes) ( VgHashTable table );
1609
njn3e884182003-04-15 13:03:23 +00001610/* Add a node to the table. */
1611extern void VG_(HT_add_node) ( VgHashTable t, VgHashNode* node );
1612
daywalker5d945de2003-09-26 00:32:53 +00001613/* Looks up a node in the hash table. Also returns the address of the
njn3e884182003-04-15 13:03:23 +00001614 previous node's `next' pointer which allows it to be removed from the
1615 list later without having to look it up again. */
1616extern VgHashNode* VG_(HT_get_node) ( VgHashTable t, UInt key,
1617 /*OUT*/VgHashNode*** next_ptr );
1618
njn06072ec2003-09-30 15:35:13 +00001619/* Allocates an array of pointers to all the shadow chunks of malloc'd
1620 blocks. Must be freed with VG_(free)(). */
1621extern VgHashNode** VG_(HT_to_array) ( VgHashTable t, /*OUT*/ UInt* n_shadows );
njn3e884182003-04-15 13:03:23 +00001622
1623/* Returns first node that matches predicate `p', or NULL if none do.
1624 Extra arguments can be implicitly passed to `p' using nested functions;
1625 see memcheck/mc_errcontext.c for an example. */
1626extern VgHashNode* VG_(HT_first_match) ( VgHashTable t,
1627 Bool (*p)(VgHashNode*) );
1628
1629/* Applies a function f() once to each node. Again, nested functions
1630 can be very useful. */
1631extern void VG_(HT_apply_to_all_nodes)( VgHashTable t, void (*f)(VgHashNode*) );
1632
1633/* Destroy a table. */
1634extern void VG_(HT_destruct) ( VgHashTable t );
njn810086f2002-11-14 12:42:47 +00001635
1636
njn3e884182003-04-15 13:03:23 +00001637/*====================================================================*/
fitzhardinge98abfc72003-12-16 02:05:15 +00001638/*=== A generic skiplist ===*/
1639/*====================================================================*/
1640
1641/*
1642 The idea here is that the skiplist puts its per-element data at the
1643 end of the structure. When you initialize the skiplist, you tell
1644 it what structure your list elements are going to be. Then you
1645 should allocate them with VG_(SkipNode_Alloc), which will allocate
1646 enough memory for the extra bits.
1647 */
1648#include <stddef.h> /* for offsetof */
1649
1650typedef struct _SkipList SkipList;
1651typedef struct _SkipNode SkipNode;
1652
1653typedef Int (*SkipCmp_t)(const void *key1, const void *key2);
1654
1655struct _SkipList {
1656 const Short arena; /* allocation arena */
1657 const UShort size; /* structure size (not including SkipNode) */
1658 const UShort keyoff; /* key offset */
1659 const SkipCmp_t cmp; /* compare two keys */
1660 Char * (*strkey)(void *); /* stringify a key (for debugging) */
1661 SkipNode *head; /* list head */
1662};
1663
1664/* Use this macro to initialize your skiplist head. The arguments are pretty self explanitory:
1665 _type is the type of your element structure
1666 _key is the field within that type which you want to use as the key
1667 _cmp is the comparison function for keys - it gets two typeof(_key) pointers as args
1668 _strkey is a function which can return a string of your key - it's only used for debugging
1669 _arena is the arena to use for allocation - -1 is the default
1670 */
1671#define SKIPLIST_INIT(_type, _key, _cmp, _strkey, _arena) \
1672 { \
1673 .arena = _arena, \
1674 .size = sizeof(_type), \
1675 .keyoff = offsetof(_type, _key), \
1676 .cmp = _cmp, \
1677 .strkey = _strkey, \
1678 .head = NULL, \
1679 }
1680
1681/* List operations:
1682 SkipList_Find searchs a list. If it can't find an exact match, it either returns NULL
1683 or a pointer to the element before where k would go
1684 SkipList_Insert inserts a new element into the list. Duplicates are forbidden.
1685 SkipList_Remove removes an element from the list and returns it. It doesn't free the memory.
1686 */
1687extern void *VG_(SkipList_Find) (const SkipList *l, void *key);
1688extern void VG_(SkipList_Insert)( SkipList *l, void *data);
1689extern void *VG_(SkipList_Remove)( SkipList *l, void *key);
1690
1691/* Node (element) operations:
1692 SkipNode_Alloc: allocate memory for a new element on the list
1693 SkipNode_Free: free memory allocated above
1694 SkipNode_First: return the first element on the list
1695 SkipNode_Next: return the next element after "data" on the list -
1696 NULL for none
1697 */
1698extern void *VG_(SkipNode_Alloc) (const SkipList *l);
1699extern void VG_(SkipNode_Free) (const SkipList *l, void *p);
1700extern void *VG_(SkipNode_First) (const SkipList *l);
1701extern void *VG_(SkipNode_Next) (const SkipList *l, void *data);
1702
1703/*====================================================================*/
njnd3040452003-05-19 15:04:06 +00001704/*=== Functions for shadow registers ===*/
1705/*====================================================================*/
1706
1707/* Nb: make sure the shadow_regs 'need' is set before using these! */
1708
1709/* This one lets you override the shadow of the return value register for a
1710 syscall. Call it from SK_(post_syscall)() (not SK_(pre_syscall)()!) to
1711 override the default shadow register value. */
daywalker5d945de2003-09-26 00:32:53 +00001712extern void VG_(set_return_from_syscall_shadow) ( ThreadId tid,
njnd3040452003-05-19 15:04:06 +00001713 UInt ret_shadow );
1714
1715/* This can be called from SK_(fini)() to find the shadow of the argument
1716 to exit(), ie. the shadow of the program's return value. */
1717extern UInt VG_(get_exit_status_shadow) ( void );
1718
1719
1720/*====================================================================*/
njn3e884182003-04-15 13:03:23 +00001721/*=== General stuff for replacing functions ===*/
1722/*====================================================================*/
njn25e49d8e72002-09-23 09:36:25 +00001723
njn3e884182003-04-15 13:03:23 +00001724/* Some skins need to replace the standard definitions of some functions. */
njn25e49d8e72002-09-23 09:36:25 +00001725
njn3e884182003-04-15 13:03:23 +00001726/* ------------------------------------------------------------------ */
1727/* General stuff, for replacing any functions */
njn25e49d8e72002-09-23 09:36:25 +00001728
daywalker5d945de2003-09-26 00:32:53 +00001729/* Is the client running on the simulated CPU or the real one?
njn25e49d8e72002-09-23 09:36:25 +00001730
njn3e884182003-04-15 13:03:23 +00001731 Nb: If it is, and you want to call a function to be run on the real CPU,
njn057c65f2003-04-21 13:30:55 +00001732 use one of the VALGRIND_NON_SIMD_CALL[123] macros in valgrind.h to call it.
njn25e49d8e72002-09-23 09:36:25 +00001733
daywalker5d945de2003-09-26 00:32:53 +00001734 Nb: don't forget the function parentheses when using this in a
njn3e884182003-04-15 13:03:23 +00001735 condition... write this:
1736
1737 if (VG_(is_running_on_simd_CPU)()) { ... } // calls function
1738
1739 not this:
daywalker5d945de2003-09-26 00:32:53 +00001740
njn3e884182003-04-15 13:03:23 +00001741 if (VG_(is_running_on_simd_CPU)) { ... } // address of var!
1742*/
daywalker5d945de2003-09-26 00:32:53 +00001743extern Bool VG_(is_running_on_simd_CPU) ( void );
njn3e884182003-04-15 13:03:23 +00001744
1745
1746/*====================================================================*/
1747/*=== Specific stuff for replacing malloc() and friends ===*/
1748/*====================================================================*/
1749
fitzhardinge98abfc72003-12-16 02:05:15 +00001750/* If a skin replaces malloc() et al, the easiest way to do so is to
1751 link with vg_replace_malloc.o into its vgpreload_*.so file, and
1752 follow the following instructions. You can do it from scratch,
1753 though, if you enjoy that sort of thing. */
njn3e884182003-04-15 13:03:23 +00001754
1755/* Arena size for valgrind's own malloc(); default value is 0, but can
1756 be overridden by skin -- but must be done so *statically*, eg:
daywalker5d945de2003-09-26 00:32:53 +00001757
njn3e884182003-04-15 13:03:23 +00001758 Int VG_(vg_malloc_redzone_szB) = 4;
daywalker5d945de2003-09-26 00:32:53 +00001759
njn3e884182003-04-15 13:03:23 +00001760 It can't be done from a function like SK_(pre_clo_init)(). So it can't,
1761 for example, be controlled with a command line option, unfortunately. */
1762extern UInt VG_(vg_malloc_redzone_szB);
1763
njn3e884182003-04-15 13:03:23 +00001764/* Can be called from SK_(malloc) et al to do the actual alloc/freeing. */
daywalker5d945de2003-09-26 00:32:53 +00001765extern void* VG_(cli_malloc) ( UInt align, Int nbytes );
njn3e884182003-04-15 13:03:23 +00001766extern void VG_(cli_free) ( void* p );
1767
1768/* Check if an address is within a range, allowing for redzones at edges */
1769extern Bool VG_(addr_is_in_block)( Addr a, Addr start, UInt size );
1770
1771/* ------------------------------------------------------------------ */
daywalker5d945de2003-09-26 00:32:53 +00001772/* Some options that can be used by a skin if malloc() et al are replaced.
njnd3040452003-05-19 15:04:06 +00001773 The skin should call the functions in the appropriate places to give
1774 control over these aspects of Valgrind's version of malloc(). */
njn3e884182003-04-15 13:03:23 +00001775
1776/* Round malloc sizes upwards to integral number of words? default: NO */
1777extern Bool VG_(clo_sloppy_malloc);
1778/* DEBUG: print malloc details? default: NO */
1779extern Bool VG_(clo_trace_malloc);
1780/* Minimum alignment in functions that don't specify alignment explicitly.
1781 default: 0, i.e. use default of the machine (== 4) */
1782extern Int VG_(clo_alignment);
1783
1784extern Bool VG_(replacement_malloc_process_cmd_line_option) ( Char* arg );
1785extern void VG_(replacement_malloc_print_usage) ( void );
1786extern void VG_(replacement_malloc_print_debug_usage) ( void );
sewardja4495682002-10-21 07:29:59 +00001787
1788
njn25e49d8e72002-09-23 09:36:25 +00001789/*====================================================================*/
1790/*=== Skin-specific stuff ===*/
1791/*====================================================================*/
1792
njnd04b7c62002-10-03 14:05:52 +00001793/* ------------------------------------------------------------------ */
1794/* Details */
njnd04b7c62002-10-03 14:05:52 +00001795
njn120281f2003-02-03 12:20:07 +00001796/* Default value for avg_translations_sizeB (in bytes), indicating typical
1797 code expansion of about 6:1. */
1798#define VG_DEFAULT_TRANS_SIZEB 100
1799
njn810086f2002-11-14 12:42:47 +00001800/* Information used in the startup message. `name' also determines the
1801 string used for identifying suppressions in a suppression file as
1802 belonging to this skin. `version' can be NULL, in which case (not
1803 surprisingly) no version info is printed; this mechanism is designed for
1804 skins distributed with Valgrind that share a version number with
1805 Valgrind. Other skins not distributed as part of Valgrind should
sewardjc0d8f682002-11-30 00:49:43 +00001806 probably have their own version number. */
1807extern void VG_(details_name) ( Char* name );
1808extern void VG_(details_version) ( Char* version );
1809extern void VG_(details_description) ( Char* description );
1810extern void VG_(details_copyright_author) ( Char* copyright_author );
1811
1812/* Average size of a translation, in bytes, so that the translation
njn120281f2003-02-03 12:20:07 +00001813 storage machinery can allocate memory appropriately. Not critical,
daywalker5d945de2003-09-26 00:32:53 +00001814 setting is optional. */
njn120281f2003-02-03 12:20:07 +00001815extern void VG_(details_avg_translation_sizeB) ( UInt size );
njnd04b7c62002-10-03 14:05:52 +00001816
njn810086f2002-11-14 12:42:47 +00001817/* String printed if an `sk_assert' assertion fails or VG_(skin_panic)
1818 is called. Should probably be an email address. */
1819extern void VG_(details_bug_reports_to) ( Char* bug_reports_to );
njnd04b7c62002-10-03 14:05:52 +00001820
1821/* ------------------------------------------------------------------ */
1822/* Needs */
1823
njn810086f2002-11-14 12:42:47 +00001824/* Booleans that decide core behaviour, but don't require extra
1825 operations to be defined if `True' */
njnd5bb0a52002-09-27 10:24:48 +00001826
njn810086f2002-11-14 12:42:47 +00001827/* Should __libc_freeres() be run? Bugs in it can crash the skin. */
1828extern void VG_(needs_libc_freeres) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001829
njn810086f2002-11-14 12:42:47 +00001830/* Want to have errors detected by Valgrind's core reported? Includes:
1831 - pthread API errors (many; eg. unlocking a non-locked mutex)
njn810086f2002-11-14 12:42:47 +00001832 - invalid file descriptors to blocking syscalls read() and write()
1833 - bad signal numbers passed to sigaction()
daywalker5d945de2003-09-26 00:32:53 +00001834 - attempt to install signal handler for SIGKILL or SIGSTOP */
njn810086f2002-11-14 12:42:47 +00001835extern void VG_(needs_core_errors) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001836
njn810086f2002-11-14 12:42:47 +00001837/* Booleans that indicate extra operations are defined; if these are True,
1838 the corresponding template functions (given below) must be defined. A
1839 lot like being a member of a type class. */
njn25e49d8e72002-09-23 09:36:25 +00001840
njn810086f2002-11-14 12:42:47 +00001841/* Want to report errors from skin? This implies use of suppressions, too. */
1842extern void VG_(needs_skin_errors) ( void );
njnd5bb0a52002-09-27 10:24:48 +00001843
njn810086f2002-11-14 12:42:47 +00001844/* Is information kept about specific individual basic blocks? (Eg. for
1845 cachegrind there are cost-centres for every instruction, stored at a
1846 basic block level.) If so, it sometimes has to be discarded, because
1847 .so mmap/munmap-ping or self-modifying code (informed by the
1848 DISCARD_TRANSLATIONS user request) can cause one instruction address
1849 to be used for more than one instruction in one program run... */
1850extern void VG_(needs_basic_block_discards) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001851
njn810086f2002-11-14 12:42:47 +00001852/* Skin maintains information about each register? */
1853extern void VG_(needs_shadow_regs) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001854
njn810086f2002-11-14 12:42:47 +00001855/* Skin defines its own command line options? */
1856extern void VG_(needs_command_line_options) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001857
njn810086f2002-11-14 12:42:47 +00001858/* Skin defines its own client requests? */
1859extern void VG_(needs_client_requests) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001860
njn810086f2002-11-14 12:42:47 +00001861/* Skin defines its own UInstrs? */
1862extern void VG_(needs_extended_UCode) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001863
njn810086f2002-11-14 12:42:47 +00001864/* Skin does stuff before and/or after system calls? */
1865extern void VG_(needs_syscall_wrapper) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001866
njn810086f2002-11-14 12:42:47 +00001867/* Are skin-state sanity checks performed? */
1868extern void VG_(needs_sanity_checks) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001869
njn810086f2002-11-14 12:42:47 +00001870/* Do we need to see data symbols? */
1871extern void VG_(needs_data_syms) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001872
fitzhardinge98abfc72003-12-16 02:05:15 +00001873/* Does the skin need shadow memory allocated (if you set this, you must also statically initialize
1874 float SK_(shadow_ratio) = n./m;
1875 to define how many shadow bits you need per client address space bit.
1876*/
1877extern void VG_(needs_shadow_memory)( void );
1878extern float SK_(shadow_ratio);
1879
njn25e49d8e72002-09-23 09:36:25 +00001880/* ------------------------------------------------------------------ */
1881/* Core events to track */
1882
1883/* Part of the core from which this call was made. Useful for determining
njnd5bb0a52002-09-27 10:24:48 +00001884 what kind of error message should be emitted. */
daywalker5d945de2003-09-26 00:32:53 +00001885typedef
njn25e49d8e72002-09-23 09:36:25 +00001886 enum { Vg_CorePThread, Vg_CoreSignal, Vg_CoreSysCall, Vg_CoreTranslate }
1887 CorePart;
1888
njn4ba5a792002-09-30 10:23:54 +00001889/* Useful to use in VG_(get_Xreg_usage)() */
njn810086f2002-11-14 12:42:47 +00001890#define VG_UINSTR_READS_REG(ono,regs,isWrites) \
njn25e49d8e72002-09-23 09:36:25 +00001891 { if (mycat(u->tag,ono) == tag) \
njn810086f2002-11-14 12:42:47 +00001892 { regs[n] = mycat(u->val,ono); \
1893 isWrites[n] = False; \
njn25e49d8e72002-09-23 09:36:25 +00001894 n++; \
1895 } \
1896 }
njn810086f2002-11-14 12:42:47 +00001897#define VG_UINSTR_WRITES_REG(ono,regs,isWrites) \
njnd5bb0a52002-09-27 10:24:48 +00001898 { if (mycat(u->tag,ono) == tag) \
njn810086f2002-11-14 12:42:47 +00001899 { regs[n] = mycat(u->val,ono); \
1900 isWrites[n] = True; \
njnd5bb0a52002-09-27 10:24:48 +00001901 n++; \
1902 } \
njn25e49d8e72002-09-23 09:36:25 +00001903 }
1904
fitzhardinge98abfc72003-12-16 02:05:15 +00001905#endif /* NDEF __VG_SKIN_H */
njn25e49d8e72002-09-23 09:36:25 +00001906
fitzhardinge98abfc72003-12-16 02:05:15 +00001907/* gen_toolint.pl will put the VG_(init_*)() functions here: */