blob: 8c328ed647bb55de912c19e157eab09fc2650fe7 [file] [log] [blame]
fitzhardinge98abfc72003-12-16 02:05:15 +00001/* -*- c -*- */
njn25e49d8e72002-09-23 09:36:25 +00002/*--------------------------------------------------------------------*/
3/*--- The only header your skin will ever need to #include... ---*/
4/*--- vg_skin.h ---*/
5/*--------------------------------------------------------------------*/
6
7/*
njnc9539842002-10-02 13:26:35 +00008 This file is part of Valgrind, an extensible x86 protected-mode
9 emulator for monitoring program execution on x86-Unixes.
njn25e49d8e72002-09-23 09:36:25 +000010
daywalker5d945de2003-09-26 00:32:53 +000011 Copyright (C) 2000-2003 Julian Seward
njn25e49d8e72002-09-23 09:36:25 +000012 jseward@acm.org
13
14 This program is free software; you can redistribute it and/or
15 modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation; either version 2 of the
17 License, or (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful, but
20 WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
27 02111-1307, USA.
28
29 The GNU General Public License is contained in the file COPYING.
30*/
31
32#ifndef __VG_SKIN_H
33#define __VG_SKIN_H
34
35#include <stdarg.h> /* ANSI varargs stuff */
36#include <setjmp.h> /* for jmp_buf */
37
38#include "vg_constants_skin.h"
39
40
nethercote421281e2003-11-20 16:20:55 +000041/* ---------------------------------------------------------------------
42 Where to send bug reports to.
43 ------------------------------------------------------------------ */
44
45#define VG_BUGS_TO "valgrind.kde.org"
46
47
njn25e49d8e72002-09-23 09:36:25 +000048/*====================================================================*/
49/*=== Build options and table sizes. ===*/
50/*====================================================================*/
51
daywalker5d945de2003-09-26 00:32:53 +000052/* You should be able to change these options or sizes, recompile, and
njn25e49d8e72002-09-23 09:36:25 +000053 still have a working system. */
54
55/* The maximum number of pthreads that we support. This is
56 deliberately not very high since our implementation of some of the
57 scheduler algorithms is surely O(N) in the number of threads, since
58 that's simple, at least. And (in practice) we hope that most
59 programs do not need many threads. */
sewardj989dad92003-07-06 01:52:32 +000060#define VG_N_THREADS 100
njn25e49d8e72002-09-23 09:36:25 +000061
62/* Maximum number of pthread keys available. Again, we start low until
63 the need for a higher number presents itself. */
64#define VG_N_THREAD_KEYS 50
65
66/* Total number of integer registers available for allocation -- all of
67 them except %esp, %ebp. %ebp permanently points at VG_(baseBlock).
daywalker5d945de2003-09-26 00:32:53 +000068
njn211b6ad2003-02-03 12:33:31 +000069 If you increase this you'll have to also change at least these:
njn4ba5a792002-09-30 10:23:54 +000070 - VG_(rank_to_realreg)()
71 - VG_(realreg_to_rank)()
njn25e49d8e72002-09-23 09:36:25 +000072 - ppRegsLiveness()
73 - the RegsLive type (maybe -- RegsLive type must have more than
74 VG_MAX_REALREGS bits)
njn211b6ad2003-02-03 12:33:31 +000075
76 You can decrease it, and performance will drop because more spills will
77 occur. If you decrease it too much, everything will fall over.
daywalker5d945de2003-09-26 00:32:53 +000078
njn25e49d8e72002-09-23 09:36:25 +000079 Do not change this unless you really know what you are doing! */
80#define VG_MAX_REALREGS 6
81
82
83/*====================================================================*/
njn1a1dd8b2002-09-27 10:42:20 +000084/*=== Basic types, useful macros ===*/
njn25e49d8e72002-09-23 09:36:25 +000085/*====================================================================*/
86
njn25e49d8e72002-09-23 09:36:25 +000087typedef unsigned char UChar;
88typedef unsigned short UShort;
89typedef unsigned int UInt;
90typedef unsigned long long int ULong;
91
92typedef signed char Char;
93typedef signed short Short;
94typedef signed int Int;
95typedef signed long long int Long;
96
97typedef unsigned int Addr;
98
99typedef unsigned char Bool;
100#define False ((Bool)0)
101#define True ((Bool)1)
102
103
njn1a1dd8b2002-09-27 10:42:20 +0000104#define mycat_wrk(aaa,bbb) aaa##bbb
105#define mycat(aaa,bbb) mycat_wrk(aaa,bbb)
106
107/* No, really. I _am_ that strange. */
108#define OINK(nnn) VG_(message)(Vg_DebugMsg, "OINK %d",nnn)
109
njn25e49d8e72002-09-23 09:36:25 +0000110/* ---------------------------------------------------------------------
111 Now the basic types are set up, we can haul in the kernel-interface
112 definitions.
113 ------------------------------------------------------------------ */
114
njn78adbf42003-07-24 19:35:00 +0000115#include "vg_kerneliface.h"
njn25e49d8e72002-09-23 09:36:25 +0000116
117
118/*====================================================================*/
njn27f1a382002-11-08 15:48:16 +0000119/*=== Core/skin interface version ===*/
120/*====================================================================*/
121
122/* The major version number indicates binary-incompatible changes to the
123 interface; if the core and skin major versions don't match, Valgrind
124 will abort. The minor version indicates binary-compatible changes.
njn27f1a382002-11-08 15:48:16 +0000125*/
jseward157689b2003-11-05 23:59:21 +0000126#define VG_CORE_INTERFACE_MAJOR_VERSION 5
njn9b007f62003-04-07 14:40:25 +0000127#define VG_CORE_INTERFACE_MINOR_VERSION 0
njn27f1a382002-11-08 15:48:16 +0000128
fitzhardinge98abfc72003-12-16 02:05:15 +0000129typedef struct _ToolInfo {
130 Int sizeof_ToolInfo;
131 Int interface_major_version;
132 Int interface_minor_version;
133
134 /* Initialise skin. Must do the following:
135 - initialise the `details' struct, via the VG_(details_*)() functions
136 - register any helpers called by generated code
137
138 May do the following:
139 - initialise the `needs' struct to indicate certain requirements, via
140 the VG_(needs_*)() functions
141 - initialize all the tool's entrypoints via the VG_(init_*)() functions
142 - register any skin-specific profiling events
143 - any other skin-specific initialisation
144 */
145 void (*sk_pre_clo_init) ( void );
146
147 /* Specifies how big the shadow segment should be as a ratio to the
148 client address space. 0 for no shadow segment. */
149 float shadow_ratio;
150} ToolInfo;
njn27f1a382002-11-08 15:48:16 +0000151
njn563f96f2003-02-03 11:17:46 +0000152/* Every skin must include this macro somewhere, exactly once. */
fitzhardinge98abfc72003-12-16 02:05:15 +0000153#define VG_DETERMINE_INTERFACE_VERSION(pre_clo_init, shadow) \
154 const ToolInfo SK_(tool_info) = { \
155 .sizeof_ToolInfo = sizeof(ToolInfo), \
156 .interface_major_version = VG_CORE_INTERFACE_MAJOR_VERSION, \
157 .interface_minor_version = VG_CORE_INTERFACE_MINOR_VERSION, \
158 .sk_pre_clo_init = pre_clo_init, \
159 .shadow_ratio = shadow, \
160 };
njn211b6ad2003-02-03 12:33:31 +0000161
njn27f1a382002-11-08 15:48:16 +0000162/*====================================================================*/
njn25e49d8e72002-09-23 09:36:25 +0000163/*=== Command-line options ===*/
164/*====================================================================*/
165
njn43c799e2003-04-08 00:08:52 +0000166/* Use this for normal null-termination-style string comparison */
167#define VG_STREQ(s1,s2) (s1 != NULL && s2 != NULL \
168 && VG_(strcmp)((s1),(s2))==0)
169
170/* Use these for recognising skin command line options -- stops comparing
171 once whitespace is reached. */
172# define VG_CLO_STREQ(s1,s2) (0==VG_(strcmp_ws)((s1),(s2)))
173# define VG_CLO_STREQN(nn,s1,s2) (0==VG_(strncmp_ws)((s1),(s2),(nn)))
174
njn25e49d8e72002-09-23 09:36:25 +0000175/* Verbosity level: 0 = silent, 1 (default), > 1 = more verbose. */
176extern Int VG_(clo_verbosity);
177
178/* Profile? */
179extern Bool VG_(clo_profile);
180
njn25e49d8e72002-09-23 09:36:25 +0000181/* Call this if a recognised option was bad for some reason.
182 Note: don't use it just because an option was unrecognised -- return 'False'
183 from SKN_(process_cmd_line_option) to indicate that. */
184extern void VG_(bad_option) ( Char* opt );
185
186/* Client args */
187extern Int VG_(client_argc);
188extern Char** VG_(client_argv);
189
njnd5bb0a52002-09-27 10:24:48 +0000190/* Client environment. Can be inspected with VG_(getenv)() */
njn25e49d8e72002-09-23 09:36:25 +0000191extern Char** VG_(client_envp);
192
193
194/*====================================================================*/
195/*=== Printing messages for the user ===*/
196/*====================================================================*/
197
198/* Print a message prefixed by "??<pid>?? "; '?' depends on the VgMsgKind.
199 Should be used for all user output. */
200
201typedef
202 enum { Vg_UserMsg, /* '?' == '=' */
203 Vg_DebugMsg, /* '?' == '-' */
fitzhardinge39de4b42003-10-31 07:12:21 +0000204 Vg_DebugExtraMsg, /* '?' == '+' */
205 Vg_ClientMsg, /* '?' == '*' */
njn25e49d8e72002-09-23 09:36:25 +0000206 }
207 VgMsgKind;
208
209/* Functions for building a message from multiple parts. */
fitzhardinge39de4b42003-10-31 07:12:21 +0000210extern int VG_(start_msg) ( VgMsgKind kind );
211extern int VG_(add_to_msg) ( Char* format, ... );
njn25e49d8e72002-09-23 09:36:25 +0000212/* Ends and prints the message. Appends a newline. */
fitzhardinge39de4b42003-10-31 07:12:21 +0000213extern int VG_(end_msg) ( void );
njn25e49d8e72002-09-23 09:36:25 +0000214
njnd5bb0a52002-09-27 10:24:48 +0000215/* Send a single-part message. Appends a newline. */
fitzhardinge39de4b42003-10-31 07:12:21 +0000216extern int VG_(message) ( VgMsgKind kind, Char* format, ... );
217extern int VG_(vmessage) ( VgMsgKind kind, Char* format, va_list vargs );
njn25e49d8e72002-09-23 09:36:25 +0000218
219
220/*====================================================================*/
221/*=== Profiling ===*/
222/*====================================================================*/
223
224/* Nb: VGP_(register_profile_event)() relies on VgpUnc being the first one */
225#define VGP_CORE_LIST \
226 /* These ones depend on the core */ \
227 VGP_PAIR(VgpUnc, "unclassified"), \
228 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"), \
241 VGP_PAIR(VgpInitMem, "init-memory"), \
242 VGP_PAIR(VgpExeContext, "exe-context"), \
243 VGP_PAIR(VgpReadSyms, "read-syms"), \
244 VGP_PAIR(VgpSearchSyms, "search-syms"), \
245 VGP_PAIR(VgpAddToT, "add-to-transtab"), \
246 VGP_PAIR(VgpCoreSysWrap, "core-syscall-wrapper"), \
247 VGP_PAIR(VgpDemangle, "demangle"), \
njn37cea302002-09-30 11:24:00 +0000248 VGP_PAIR(VgpCoreCheapSanity, "core-cheap-sanity"), \
249 VGP_PAIR(VgpCoreExpensiveSanity, "core-expensive-sanity"), \
njn25e49d8e72002-09-23 09:36:25 +0000250 /* These ones depend on the skin */ \
251 VGP_PAIR(VgpPreCloInit, "pre-clo-init"), \
252 VGP_PAIR(VgpPostCloInit, "post-clo-init"), \
253 VGP_PAIR(VgpInstrument, "instrument"), \
254 VGP_PAIR(VgpSkinSysWrap, "skin-syscall-wrapper"), \
njn37cea302002-09-30 11:24:00 +0000255 VGP_PAIR(VgpSkinCheapSanity, "skin-cheap-sanity"), \
256 VGP_PAIR(VgpSkinExpensiveSanity, "skin-expensive-sanity"), \
njn25e49d8e72002-09-23 09:36:25 +0000257 VGP_PAIR(VgpFini, "fini")
258
259#define VGP_PAIR(n,name) n
260typedef enum { VGP_CORE_LIST } VgpCoreCC;
261#undef VGP_PAIR
262
263/* When registering skin profiling events, ensure that the 'n' value is in
264 * the range (VgpFini+1..) */
265extern void VGP_(register_profile_event) ( Int n, Char* name );
266
267extern void VGP_(pushcc) ( UInt cc );
268extern void VGP_(popcc) ( UInt cc );
269
270/* Define them only if they haven't already been defined by vg_profile.c */
271#ifndef VGP_PUSHCC
272# define VGP_PUSHCC(x)
273#endif
274#ifndef VGP_POPCC
275# define VGP_POPCC(x)
276#endif
277
278
279/*====================================================================*/
280/*=== Useful stuff to call from generated code ===*/
281/*====================================================================*/
282
283/* ------------------------------------------------------------------ */
284/* General stuff */
285
njn41557122002-10-14 09:25:37 +0000286/* 64-bit counter for the number of basic blocks done. */
287extern ULong VG_(bbs_done);
288
njn25e49d8e72002-09-23 09:36:25 +0000289/* Get the simulated %esp */
290extern Addr VG_(get_stack_pointer) ( void );
291
sewardjecf8e102003-07-12 12:11:39 +0000292/* Detect if an address is within Valgrind's stack, Valgrind's
293 m_state_static, or the VG_(threads) array. This is useful for
294 memory leak detectors to rule out spurious pointers to a block. */
njn25e49d8e72002-09-23 09:36:25 +0000295extern Bool VG_(within_stack)(Addr a);
sewardjecf8e102003-07-12 12:11:39 +0000296extern Bool VG_(within_m_state_static_OR_threads)(Addr a);
njn25e49d8e72002-09-23 09:36:25 +0000297
298/* Check if an address is 4-byte aligned */
299#define IS_ALIGNED4_ADDR(aaa_p) (0 == (((UInt)(aaa_p)) & 3))
njn9b007f62003-04-07 14:40:25 +0000300#define IS_ALIGNED8_ADDR(aaa_p) (0 == (((UInt)(aaa_p)) & 7))
njn25e49d8e72002-09-23 09:36:25 +0000301
302
303/* ------------------------------------------------------------------ */
304/* Thread-related stuff */
305
306/* Special magic value for an invalid ThreadId. It corresponds to
307 LinuxThreads using zero as the initial value for
308 pthread_mutex_t.__m_owner and pthread_cond_t.__c_waiting. */
309#define VG_INVALID_THREADID ((ThreadId)(0))
310
njn72718642003-07-24 08:45:32 +0000311/* ThreadIds are simply indices into the VG_(threads)[] array. */
daywalker5d945de2003-09-26 00:32:53 +0000312typedef
313 UInt
njn25e49d8e72002-09-23 09:36:25 +0000314 ThreadId;
315
njn72718642003-07-24 08:45:32 +0000316/* When looking for the current ThreadId, this is the safe option and
317 probably the one you want.
daywalker5d945de2003-09-26 00:32:53 +0000318
njn72718642003-07-24 08:45:32 +0000319 Details: Use this one from non-generated code, eg. from functions called
320 on events like 'new_mem_heap'. In such a case, the "current" thread is
321 temporarily suspended as Valgrind's dispatcher is running. This function
322 is also suitable to be called from generated code (ie. from UCode, or a C
323 function called directly from UCode).
daywalker5d945de2003-09-26 00:32:53 +0000324
njn72718642003-07-24 08:45:32 +0000325 If you use VG_(get_current_tid)() from non-generated code, it will return
326 0 signifying the invalid thread, which is probably not what you want. */
327extern ThreadId VG_(get_current_or_recent_tid) ( void );
njn25e49d8e72002-09-23 09:36:25 +0000328
njn72718642003-07-24 08:45:32 +0000329/* When looking for the current ThreadId, only use this one if you know what
330 you are doing.
daywalker5d945de2003-09-26 00:32:53 +0000331
njn72718642003-07-24 08:45:32 +0000332 Details: Use this one from generated code, eg. from C functions called
333 from UCode. (VG_(get_current_or_recent_tid)() is also suitable in that
334 case.) If you use this function from non-generated code, it will return
335 0 signifying the invalid thread, which is probably not what you want. */
336extern ThreadId VG_(get_current_tid) ( void );
njn25e49d8e72002-09-23 09:36:25 +0000337
njn3e884182003-04-15 13:03:23 +0000338/* Searches through all thread's stacks to see if any match. Returns
njn72718642003-07-24 08:45:32 +0000339 VG_INVALID_THREADID if none match. */
njn3e884182003-04-15 13:03:23 +0000340extern ThreadId VG_(first_matching_thread_stack)
341 ( Bool (*p) ( Addr stack_min, Addr stack_max ));
342
njn25e49d8e72002-09-23 09:36:25 +0000343
344/*====================================================================*/
345/*=== Valgrind's version of libc ===*/
346/*====================================================================*/
347
348/* Valgrind doesn't use libc at all, for good reasons (trust us). So here
349 are its own versions of C library functions, but with VG_ prefixes. Note
350 that the types of some are slightly different to the real ones. Some
njnf4ce3d32003-02-10 10:17:26 +0000351 additional useful functions are provided too; descriptions of how they
352 work are given below. */
njn25e49d8e72002-09-23 09:36:25 +0000353
354#if !defined(NULL)
355# define NULL ((void*)0)
356#endif
357
358
359/* ------------------------------------------------------------------ */
360/* stdio.h
361 *
362 * Note that they all output to the file descriptor given by the
363 * --logfile-fd=N argument, which defaults to 2 (stderr). Hence no
daywalker5d945de2003-09-26 00:32:53 +0000364 * need for VG_(fprintf)().
njn25e49d8e72002-09-23 09:36:25 +0000365 */
sewardj78e3cd92002-10-22 04:45:48 +0000366extern UInt VG_(printf) ( const char *format, ... );
njn25e49d8e72002-09-23 09:36:25 +0000367/* too noisy ... __attribute__ ((format (printf, 1, 2))) ; */
sewardj78e3cd92002-10-22 04:45:48 +0000368extern UInt VG_(sprintf) ( Char* buf, Char *format, ... );
daywalker5d945de2003-09-26 00:32:53 +0000369extern UInt VG_(vprintf) ( void(*send)(Char),
njn25e49d8e72002-09-23 09:36:25 +0000370 const Char *format, va_list vargs );
371
njn41557122002-10-14 09:25:37 +0000372extern Int VG_(rename) ( Char* old_name, Char* new_name );
373
njn25e49d8e72002-09-23 09:36:25 +0000374/* ------------------------------------------------------------------ */
375/* stdlib.h */
376
377extern void* VG_(malloc) ( Int nbytes );
njnd5bb0a52002-09-27 10:24:48 +0000378extern void VG_(free) ( void* p );
379extern void* VG_(calloc) ( Int n, Int nbytes );
380extern void* VG_(realloc) ( void* p, Int size );
381extern void* VG_(malloc_aligned) ( Int align_bytes, Int nbytes );
njn25e49d8e72002-09-23 09:36:25 +0000382
383extern void VG_(print_malloc_stats) ( void );
384
385
386extern void VG_(exit)( Int status )
387 __attribute__ ((__noreturn__));
njne427a662002-10-02 11:08:25 +0000388/* Prints a panic message (a constant string), appends newline and bug
389 reporting info, aborts. */
390__attribute__ ((__noreturn__))
391extern void VG_(skin_panic) ( Char* str );
njn25e49d8e72002-09-23 09:36:25 +0000392
njnd5bb0a52002-09-27 10:24:48 +0000393/* Looks up VG_(client_envp) */
njn25e49d8e72002-09-23 09:36:25 +0000394extern Char* VG_(getenv) ( Char* name );
395
rjwalshf5f536f2003-11-17 17:45:00 +0000396/* Get client resource limit*/
397extern Int VG_(getrlimit) ( Int resource, struct vki_rlimit *rlim );
398
njn25e49d8e72002-09-23 09:36:25 +0000399/* Crude stand-in for the glibc system() call. */
400extern Int VG_(system) ( Char* cmd );
401
njnd5bb0a52002-09-27 10:24:48 +0000402extern Long VG_(atoll) ( Char* str );
njn25e49d8e72002-09-23 09:36:25 +0000403
404/* Like atoll(), but converts a number of base 2..36 */
405extern Long VG_(atoll36) ( UInt base, Char* str );
406
njne36543a2003-09-30 15:37:24 +0000407/* Like qsort(), but does shell-sort. The size==1/2/4 cases are specialised. */
njnd3b0c5f2003-09-30 14:43:54 +0000408extern void VG_(ssort)( void* base, UInt nmemb, UInt size,
409 Int (*compar)(void*, void*) );
410
njn25e49d8e72002-09-23 09:36:25 +0000411
412/* ------------------------------------------------------------------ */
njnd5bb0a52002-09-27 10:24:48 +0000413/* ctype.h */
njn25e49d8e72002-09-23 09:36:25 +0000414extern Bool VG_(isspace) ( Char c );
415extern Bool VG_(isdigit) ( Char c );
416extern Char VG_(toupper) ( Char c );
417
418
419/* ------------------------------------------------------------------ */
420/* string.h */
421extern Int VG_(strlen) ( const Char* str );
422extern Char* VG_(strcat) ( Char* dest, const Char* src );
423extern Char* VG_(strncat) ( Char* dest, const Char* src, Int n );
424extern Char* VG_(strpbrk) ( const Char* s, const Char* accept );
425extern Char* VG_(strcpy) ( Char* dest, const Char* src );
426extern Char* VG_(strncpy) ( Char* dest, const Char* src, Int ndest );
427extern Int VG_(strcmp) ( const Char* s1, const Char* s2 );
428extern Int VG_(strncmp) ( const Char* s1, const Char* s2, Int nmax );
429extern Char* VG_(strstr) ( const Char* haystack, Char* needle );
430extern Char* VG_(strchr) ( const Char* s, Char c );
431extern Char* VG_(strdup) ( const Char* s);
sewardj7ab2aca2002-10-20 19:40:32 +0000432extern void* VG_(memcpy) ( void *d, const void *s, Int sz );
433extern void* VG_(memset) ( void *s, Int c, Int sz );
njn6d68e792003-02-24 10:49:08 +0000434extern Int VG_(memcmp) ( const void* s1, const void* s2, Int n );
njn25e49d8e72002-09-23 09:36:25 +0000435
njnd5bb0a52002-09-27 10:24:48 +0000436/* Like strcmp() and strncmp(), but stop comparing at any whitespace. */
njn25e49d8e72002-09-23 09:36:25 +0000437extern Int VG_(strcmp_ws) ( const Char* s1, const Char* s2 );
njn25e49d8e72002-09-23 09:36:25 +0000438extern Int VG_(strncmp_ws) ( const Char* s1, const Char* s2, Int nmax );
439
daywalker5d945de2003-09-26 00:32:53 +0000440/* Like strncpy(), but if 'src' is longer than 'ndest' inserts a '\0' as the
njnd5bb0a52002-09-27 10:24:48 +0000441 last character. */
njn25e49d8e72002-09-23 09:36:25 +0000442extern void VG_(strncpy_safely) ( Char* dest, const Char* src, Int ndest );
443
444/* Mini-regexp function. Searches for 'pat' in 'str'. Supports
445 * meta-symbols '*' and '?'. '\' escapes meta-symbols. */
fitzhardinge98abfc72003-12-16 02:05:15 +0000446extern Bool VG_(string_match) ( const Char* pat, const Char* str );
njn25e49d8e72002-09-23 09:36:25 +0000447
448
449/* ------------------------------------------------------------------ */
450/* math.h */
njnd5bb0a52002-09-27 10:24:48 +0000451/* Returns the base-2 logarithm of x. */
njn25e49d8e72002-09-23 09:36:25 +0000452extern Int VG_(log2) ( Int x );
453
454
455/* ------------------------------------------------------------------ */
njnd5bb0a52002-09-27 10:24:48 +0000456/* unistd.h, fcntl.h, sys/stat.h */
rjwalshf5f536f2003-11-17 17:45:00 +0000457extern Int VG_(getdents)( UInt fd, struct vki_dirent *dirp, UInt count );
458extern Int VG_(readlink)( Char* path, Char* buf, UInt bufsize );
sewardj4cf05692002-10-27 20:28:29 +0000459extern Int VG_(getpid) ( void );
460extern Int VG_(getppid) ( void );
jsgf855d93d2003-10-13 22:26:55 +0000461extern Int VG_(getpgrp) ( void );
462extern Int VG_(gettid) ( void );
463extern Int VG_(setpgid) ( Int pid, Int pgrp );
njnd5bb0a52002-09-27 10:24:48 +0000464
njn4aca2d22002-10-04 10:29:38 +0000465extern Int VG_(open) ( const Char* pathname, Int flags, Int mode );
466extern Int VG_(read) ( Int fd, void* buf, Int count);
jsgf855d93d2003-10-13 22:26:55 +0000467extern Int VG_(write) ( Int fd, const void* buf, Int count);
rjwalshf5f536f2003-11-17 17:45:00 +0000468extern Int VG_(lseek) ( Int fd, Long offset, Int whence);
njn4aca2d22002-10-04 10:29:38 +0000469extern void VG_(close) ( Int fd );
njnd5bb0a52002-09-27 10:24:48 +0000470
jsgf855d93d2003-10-13 22:26:55 +0000471extern Int VG_(pipe) ( Int fd[2] );
472
njn41557122002-10-14 09:25:37 +0000473/* Nb: VG_(rename)() declared in stdio.h section above */
njn4aca2d22002-10-04 10:29:38 +0000474extern Int VG_(unlink) ( Char* file_name );
475extern Int VG_(stat) ( Char* file_name, struct vki_stat* buf );
njnc9d4ba72003-10-15 10:34:03 +0000476extern Int VG_(fstat) ( Int fd, struct vki_stat* buf );
fitzhardingee1c06d82003-10-30 07:21:44 +0000477extern Int VG_(dup2) ( Int oldfd, Int newfd );
njn25e49d8e72002-09-23 09:36:25 +0000478
njn13f02932003-04-30 20:23:58 +0000479extern Char* VG_(getcwd) ( Char* buf, Int size );
480
njn99ccf082003-09-30 13:51:23 +0000481/* Easier to use than VG_(getcwd)() -- does the buffer fiddling itself.
482 String put into 'cwd' is VG_(malloc)'d, and should be VG_(free)'d.
483 Returns False if it fails. Will fail if the pathname is > 65535 bytes. */
484extern Bool VG_(getcwd_alloc) ( Char** cwd );
njn25e49d8e72002-09-23 09:36:25 +0000485
486/* ------------------------------------------------------------------ */
487/* assert.h */
488/* Asserts permanently enabled -- no turning off with NDEBUG. Hurrah! */
489#define VG__STRING(__str) #__str
490
njne427a662002-10-02 11:08:25 +0000491#define sk_assert(expr) \
njn25e49d8e72002-09-23 09:36:25 +0000492 ((void) ((expr) ? 0 : \
njne427a662002-10-02 11:08:25 +0000493 (VG_(skin_assert_fail) (VG__STRING(expr), \
494 __FILE__, __LINE__, \
495 __PRETTY_FUNCTION__), 0)))
njn25e49d8e72002-09-23 09:36:25 +0000496
njne427a662002-10-02 11:08:25 +0000497__attribute__ ((__noreturn__))
daywalker5d945de2003-09-26 00:32:53 +0000498extern void VG_(skin_assert_fail) ( const Char* expr, const Char* file,
daywalkerdf9ae422003-09-18 01:41:48 +0000499 Int line, const Char* fn );
njn25e49d8e72002-09-23 09:36:25 +0000500
501
502/* ------------------------------------------------------------------ */
njn25e49d8e72002-09-23 09:36:25 +0000503/* Get memory by anonymous mmap. */
504extern void* VG_(get_memory_from_mmap) ( Int nBytes, Char* who );
505
fitzhardinge98abfc72003-12-16 02:05:15 +0000506extern Bool VG_(is_client_addr) (Addr a);
507extern Addr VG_(get_client_base)(void);
508extern Addr VG_(get_client_end) (void);
509extern Addr VG_(get_client_size)(void);
510
511extern Bool VG_(is_shadow_addr) (Addr a);
512extern Addr VG_(get_shadow_base)(void);
513extern Addr VG_(get_shadow_end) (void);
514extern Addr VG_(get_shadow_size)(void);
515
516extern void *VG_(shadow_alloc)(UInt size);
517
518extern Bool VG_(is_addressable)(Addr p, Int sz);
519
520extern Addr VG_(client_alloc)(Addr base, UInt len, UInt prot, UInt flags);
521extern void VG_(client_free)(Addr addr);
522
523extern Bool VG_(is_valgrind_addr)(Addr a);
524
525/* initialize shadow pages in the range [p, p+sz) This calls
526 init_shadow_page for each one. It should be a lot more efficient
527 for bulk-initializing shadow pages than faulting on each one.
528*/
529extern void VG_(init_shadow_range)(Addr p, UInt sz, Bool call_init);
njn25e49d8e72002-09-23 09:36:25 +0000530
531/* ------------------------------------------------------------------ */
daywalker5d945de2003-09-26 00:32:53 +0000532/* signal.h.
533
njn25e49d8e72002-09-23 09:36:25 +0000534 Note that these use the vk_ (kernel) structure
535 definitions, which are different in places from those that glibc
536 defines -- hence the 'k' prefix. Since we're operating right at the
537 kernel interface, glibc's view of the world is entirely irrelevant. */
538
539/* --- Signal set ops --- */
njnd5bb0a52002-09-27 10:24:48 +0000540extern Int VG_(ksigfillset) ( vki_ksigset_t* set );
541extern Int VG_(ksigemptyset) ( vki_ksigset_t* set );
njn25e49d8e72002-09-23 09:36:25 +0000542
njnd5bb0a52002-09-27 10:24:48 +0000543extern Bool VG_(kisfullsigset) ( vki_ksigset_t* set );
544extern Bool VG_(kisemptysigset) ( vki_ksigset_t* set );
njn25e49d8e72002-09-23 09:36:25 +0000545
njnd5bb0a52002-09-27 10:24:48 +0000546extern Int VG_(ksigaddset) ( vki_ksigset_t* set, Int signum );
547extern Int VG_(ksigdelset) ( vki_ksigset_t* set, Int signum );
njn25e49d8e72002-09-23 09:36:25 +0000548extern Int VG_(ksigismember) ( vki_ksigset_t* set, Int signum );
549
njnd5bb0a52002-09-27 10:24:48 +0000550extern void VG_(ksigaddset_from_set) ( vki_ksigset_t* dst, vki_ksigset_t* src );
551extern void VG_(ksigdelset_from_set) ( vki_ksigset_t* dst, vki_ksigset_t* src );
njn25e49d8e72002-09-23 09:36:25 +0000552
553/* --- Mess with the kernel's sig state --- */
daywalker5d945de2003-09-26 00:32:53 +0000554extern Int VG_(ksigprocmask) ( Int how, const vki_ksigset_t* set,
njn25e49d8e72002-09-23 09:36:25 +0000555 vki_ksigset_t* oldset );
daywalker5d945de2003-09-26 00:32:53 +0000556extern Int VG_(ksigaction) ( Int signum,
557 const vki_ksigaction* act,
njnd5bb0a52002-09-27 10:24:48 +0000558 vki_ksigaction* oldact );
njn25e49d8e72002-09-23 09:36:25 +0000559
jsgf855d93d2003-10-13 22:26:55 +0000560extern Int VG_(ksigtimedwait)( const vki_ksigset_t *, vki_ksiginfo_t *,
561 const struct vki_timespec * );
562
njnd5bb0a52002-09-27 10:24:48 +0000563extern Int VG_(ksignal) ( Int signum, void (*sighandler)(Int) );
564extern Int VG_(ksigaltstack) ( const vki_kstack_t* ss, vki_kstack_t* oss );
njn25e49d8e72002-09-23 09:36:25 +0000565
sewardjdcaf3122002-09-30 23:12:33 +0000566extern Int VG_(kkill) ( Int pid, Int signo );
jsgf855d93d2003-10-13 22:26:55 +0000567extern Int VG_(ktkill) ( Int pid, Int signo );
sewardjdcaf3122002-09-30 23:12:33 +0000568extern Int VG_(ksigpending) ( vki_ksigset_t* set );
njn25e49d8e72002-09-23 09:36:25 +0000569
jsgf855d93d2003-10-13 22:26:55 +0000570extern Int VG_(waitpid) ( Int pid, Int *status, Int options );
njn25e49d8e72002-09-23 09:36:25 +0000571
njne7442cf2003-09-30 14:03:21 +0000572/* ------------------------------------------------------------------ */
rjwalshf5f536f2003-11-17 17:45:00 +0000573/* socket.h. */
574
575extern Int VG_(getsockname) ( Int sd, struct vki_sockaddr *name, Int *namelen);
576extern Int VG_(getpeername) ( Int sd, struct vki_sockaddr *name, Int *namelen);
577extern Int VG_(getsockopt) ( Int sd, Int level, Int optname, void *optval,
578 Int *optlen);
579
580/* ------------------------------------------------------------------ */
njne7442cf2003-09-30 14:03:21 +0000581/* other, randomly useful functions */
582extern UInt VG_(read_millisecond_timer) ( void );
583
njn25e49d8e72002-09-23 09:36:25 +0000584/*====================================================================*/
585/*=== UCode definition ===*/
586/*====================================================================*/
587
sewardje1042472002-09-30 12:33:11 +0000588/* Tags which describe what operands are. Must fit into 4 bits, which
589 they clearly do. */
njn25e49d8e72002-09-23 09:36:25 +0000590typedef
sewardje1042472002-09-30 12:33:11 +0000591enum { TempReg =0, /* virtual temp-reg */
592 ArchReg =1, /* simulated integer reg */
593 ArchRegS =2, /* simulated segment reg */
594 RealReg =3, /* real machine's real reg */
595 SpillNo =4, /* spill slot location */
596 Literal =5, /* literal; .lit32 field has actual value */
597 Lit16 =6, /* literal; .val[123] field has actual value */
598 NoValue =7 /* operand not in use */
599 }
600 Tag;
njn25e49d8e72002-09-23 09:36:25 +0000601
njnd5bb0a52002-09-27 10:24:48 +0000602/* Invalid register numbers (can't be negative) */
njn25e49d8e72002-09-23 09:36:25 +0000603#define INVALID_TEMPREG 999999999
604#define INVALID_REALREG 999999999
605
606/* Microinstruction opcodes. */
607typedef
608 enum {
njnd5bb0a52002-09-27 10:24:48 +0000609 NOP, /* Null op */
njn25e49d8e72002-09-23 09:36:25 +0000610
daywalker7e73e5f2003-07-04 16:18:15 +0000611 LOCK, /* Indicate the existence of a LOCK prefix (functionally NOP) */
sewardj7a5ebcf2002-11-13 22:42:13 +0000612
njnd5bb0a52002-09-27 10:24:48 +0000613 /* Moving values around */
614 GET, PUT, /* simulated register <--> TempReg */
615 GETF, PUTF, /* simulated %eflags <--> TempReg */
616 LOAD, STORE, /* memory <--> TempReg */
617 MOV, /* TempReg <--> TempReg */
618 CMOV, /* Used for cmpxchg and cmov */
njn25e49d8e72002-09-23 09:36:25 +0000619
njnd5bb0a52002-09-27 10:24:48 +0000620 /* Arithmetic/logical ops */
jsgf5efa4fd2003-10-14 21:49:11 +0000621 MUL, UMUL, /* Multiply */
njnd5bb0a52002-09-27 10:24:48 +0000622 ADD, ADC, SUB, SBB, /* Add/subtract (w/wo carry) */
623 AND, OR, XOR, NOT, /* Boolean ops */
624 SHL, SHR, SAR, ROL, ROR, RCL, RCR, /* Shift/rotate (w/wo carry) */
625 NEG, /* Negate */
626 INC, DEC, /* Increment/decrement */
627 BSWAP, /* Big-endian <--> little-endian */
628 CC2VAL, /* Condition code --> 0 or 1 */
629 WIDEN, /* Signed or unsigned widening */
njn25e49d8e72002-09-23 09:36:25 +0000630
njnd5bb0a52002-09-27 10:24:48 +0000631 /* Conditional or unconditional jump */
daywalker5d945de2003-09-26 00:32:53 +0000632 JMP,
njnd5bb0a52002-09-27 10:24:48 +0000633
634 /* FPU ops */
635 FPU, /* Doesn't touch memory */
636 FPU_R, FPU_W, /* Reads/writes memory */
637
sewardj3d7c9c82003-03-26 21:08:13 +0000638 /* ------------ MMX ops ------------ */
sewardj8cec6ee2003-05-18 11:56:39 +0000639 /* In this and the SSE encoding, bytes at higher addresses are
640 held in bits [7:0] in these 16-bit words. I guess this means
641 it is a big-endian encoding. */
sewardj3d7c9c82003-03-26 21:08:13 +0000642
643 /* 1 byte, no memrefs, no iregdefs, copy exactly to the
644 output. Held in val1[7:0]. */
645 MMX1,
646
647 /* 2 bytes, no memrefs, no iregdefs, copy exactly to the
648 output. Held in val1[15:0]. */
649 MMX2,
650
651 /* 3 bytes, no memrefs, no iregdefs, copy exactly to the
652 output. Held in val1[15:0] and val2[7:0]. */
653 MMX3,
654
655 /* 2 bytes, reads/writes mem. Insns of the form
656 bbbbbbbb:mod mmxreg r/m.
657 Held in val1[15:0], and mod and rm are to be replaced
daywalker5d945de2003-09-26 00:32:53 +0000658 at codegen time by a reference to the Temp/RealReg holding
sewardj3d7c9c82003-03-26 21:08:13 +0000659 the address. Arg2 holds this Temp/Real Reg.
660 Transfer is always at size 8.
661 */
662 MMX2_MemRd,
663 MMX2_MemWr,
664
sewardj4fbe6e92003-06-15 21:54:34 +0000665 /* 2 bytes, reads/writes an integer ("E") register. Insns of the form
sewardj3d7c9c82003-03-26 21:08:13 +0000666 bbbbbbbb:11 mmxreg ireg.
667 Held in val1[15:0], and ireg is to be replaced
668 at codegen time by a reference to the relevant RealReg.
669 Transfer is always at size 4. Arg2 holds this Temp/Real Reg.
670 */
sewardj4fbe6e92003-06-15 21:54:34 +0000671 MMX2_ERegRd,
672 MMX2_ERegWr,
sewardj3d7c9c82003-03-26 21:08:13 +0000673
sewardj8cec6ee2003-05-18 11:56:39 +0000674 /* ------------ SSE/SSE2 ops ------------ */
675 /* In the following:
676
sewardjfebaa3b2003-05-25 01:07:34 +0000677 a digit N indicates the next N bytes are to be copied exactly
678 to the output.
sewardj8cec6ee2003-05-18 11:56:39 +0000679
680 'a' indicates a mod-xmmreg-rm byte, where the mod-rm part is
681 to be replaced at codegen time to a Temp/RealReg holding the
682 address.
683
sewardj4fbe6e92003-06-15 21:54:34 +0000684 'e' indicates a byte of the form '11 xmmreg ireg', where ireg
685 is read or written, and is to be replaced at codegen time by
686 a reference to the relevant RealReg. 'e' because it's the E
687 reg in Intel encoding parlance.
sewardj8cec6ee2003-05-18 11:56:39 +0000688
sewardj4fbe6e92003-06-15 21:54:34 +0000689 'g' indicates a byte of the form '11 ireg xmmreg', where ireg
690 is read or written, and is to be replaced at codegen time by
691 a reference to the relevant RealReg. 'g' because it's called
692 G in Intel parlance. */
sewardj8cec6ee2003-05-18 11:56:39 +0000693
694 /* 3 bytes, no memrefs, no iregdefs, copy exactly to the
695 output. Held in val1[15:0] and val2[7:0]. */
696 SSE3,
697
698 /* 3 bytes, reads/writes mem. Insns of the form
699 bbbbbbbb:bbbbbbbb:mod mmxreg r/m.
700 Held in val1[15:0] and val2[7:0], and mod and rm are to be
701 replaced at codegen time by a reference to the Temp/RealReg
702 holding the address. Arg3 holds this Temp/Real Reg.
sewardjfebaa3b2003-05-25 01:07:34 +0000703 Transfer is usually, but not always, at size 16. */
sewardj8cec6ee2003-05-18 11:56:39 +0000704 SSE2a_MemRd,
705 SSE2a_MemWr,
706
707 /* 4 bytes, no memrefs, no iregdefs, copy exactly to the
708 output. Held in val1[15:0] and val2[15:0]. */
709 SSE4,
710
711 /* 4 bytes, reads/writes mem. Insns of the form
712 bbbbbbbb:bbbbbbbb:bbbbbbbb:mod mmxreg r/m.
713 Held in val1[15:0] and val2[15:0], and mod and rm are to be
714 replaced at codegen time by a reference to the Temp/RealReg
715 holding the address. Arg3 holds this Temp/Real Reg.
sewardj9dd209f2003-06-18 23:30:52 +0000716 Transfer is at stated size. */
sewardj8cec6ee2003-05-18 11:56:39 +0000717 SSE3a_MemRd,
718 SSE3a_MemWr,
719
720 /* 4 bytes, reads/writes mem. Insns of the form
721 bbbbbbbb:bbbbbbbb:mod mmxreg r/m:bbbbbbbb
722 Held in val1[15:0] and val2[15:0], and mod and rm are to be
723 replaced at codegen time by a reference to the Temp/RealReg
724 holding the address. Arg3 holds this Temp/Real Reg.
sewardj9dd209f2003-06-18 23:30:52 +0000725 Transfer is at stated size. */
sewardj8cec6ee2003-05-18 11:56:39 +0000726 SSE2a1_MemRd,
sewardj77d30a22003-10-19 08:18:52 +0000727
sewardj8cec6ee2003-05-18 11:56:39 +0000728 /* 4 bytes, writes an integer register. Insns of the form
sewardj4fbe6e92003-06-15 21:54:34 +0000729 bbbbbbbb:bbbbbbbb:bbbbbbbb:11 ireg bbb.
sewardj8cec6ee2003-05-18 11:56:39 +0000730 Held in val1[15:0] and val2[15:0], and ireg is to be replaced
731 at codegen time by a reference to the relevant RealReg.
732 Transfer is always at size 4. Arg3 holds this Temp/Real Reg.
733 */
734 SSE3g_RegWr,
735
sewardjb31b06d2003-06-13 00:26:02 +0000736 /* 5 bytes, writes an integer register. Insns of the form
737 bbbbbbbb:bbbbbbbb:bbbbbbbb: 11 ireg bbb :bbbbbbbb. Held in
738 val1[15:0] and val2[15:0] and lit32[7:0], and ireg is to be
739 replaced at codegen time by a reference to the relevant
740 RealReg. Transfer is always at size 4. Arg3 holds this
741 Temp/Real Reg.
742 */
743 SSE3g1_RegWr,
744
sewardj4fbe6e92003-06-15 21:54:34 +0000745 /* 4 bytes, reads an integer register. Insns of the form
746 bbbbbbbb:bbbbbbbb:bbbbbbbb:11 bbb ireg.
747 Held in val1[15:0] and val2[15:0], and ireg is to be replaced
748 at codegen time by a reference to the relevant RealReg.
749 Transfer is always at size 4. Arg3 holds this Temp/Real Reg.
750 */
751 SSE3e_RegRd,
sewardjabf8bf82003-06-15 22:28:05 +0000752 SSE3e_RegWr, /* variant that writes Ereg, not reads it */
sewardj4fbe6e92003-06-15 21:54:34 +0000753
sewardjb31b06d2003-06-13 00:26:02 +0000754 /* 5 bytes, reads an integer register. Insns of the form
755 bbbbbbbb:bbbbbbbb:bbbbbbbb: 11 bbb ireg :bbbbbbbb. Held in
756 val1[15:0] and val2[15:0] and lit32[7:0], and ireg is to be
757 replaced at codegen time by a reference to the relevant
758 RealReg. Transfer is always at size 4. Arg3 holds this
759 Temp/Real Reg.
760 */
sewardj4fbe6e92003-06-15 21:54:34 +0000761 SSE3e1_RegRd,
sewardj8cec6ee2003-05-18 11:56:39 +0000762
sewardj02af6bc2003-06-12 00:56:06 +0000763 /* 4 bytes, reads memory, writes an integer register, but is
764 nevertheless an SSE insn. The insn is of the form
765 bbbbbbbb:bbbbbbbb:bbbbbbbb:mod ireg rm where mod indicates
766 memory (ie is not 11b) and ireg is the int reg written. The
sewardje3891fa2003-06-15 03:13:48 +0000767 first 4 bytes are held in lit32[31:0] since there is
sewardj02af6bc2003-06-12 00:56:06 +0000768 insufficient space elsewhere. mod and rm are to be replaced
769 at codegen time by a reference to the Temp/RealReg holding
770 the address. Arg1 holds this Temp/RealReg. ireg is to be
771 replaced at codegen time by a reference to the relevant
772 RealReg in which the answer is to be written. Arg2 holds
773 this Temp/RealReg. Transfer to the destination reg is always
774 at size 4. However the memory read can be at sizes 4 or 8
sewardje3891fa2003-06-15 03:13:48 +0000775 and so this is what the sz field holds. Note that the 4th
776 byte of the instruction (the modrm byte) is redundant, but we
777 store it anyway so as to be consistent with all other SSE
778 uinstrs.
sewardj02af6bc2003-06-12 00:56:06 +0000779 */
780 SSE3ag_MemRd_RegWr,
sewardje3891fa2003-06-15 03:13:48 +0000781
sewardj8cec6ee2003-05-18 11:56:39 +0000782 /* 5 bytes, no memrefs, no iregdefs, copy exactly to the
783 output. Held in val1[15:0], val2[15:0] and val3[7:0]. */
784 SSE5,
sewardj77d30a22003-10-19 08:18:52 +0000785
sewardj8cec6ee2003-05-18 11:56:39 +0000786 /* 5 bytes, reads/writes mem. Insns of the form
787 bbbbbbbb:bbbbbbbb:bbbbbbbb:mod mmxreg r/m:bbbbbbbb
daywalker5d945de2003-09-26 00:32:53 +0000788 Held in val1[15:0], val2[15:0], lit32[7:0].
789 mod and rm are to be replaced at codegen time by a reference
790 to the Temp/RealReg holding the address. Arg3 holds this
sewardj8cec6ee2003-05-18 11:56:39 +0000791 Temp/Real Reg. Transfer is always at size 16. */
792 SSE3a1_MemRd,
sewardj77d30a22003-10-19 08:18:52 +0000793
sewardj3d7c9c82003-03-26 21:08:13 +0000794 /* ------------------------ */
795
njnd5bb0a52002-09-27 10:24:48 +0000796 /* Not strictly needed, but improve address calculation translations. */
njn25e49d8e72002-09-23 09:36:25 +0000797 LEA1, /* reg2 := const + reg1 */
798 LEA2, /* reg3 := const + reg1 + reg2 * 1,2,4 or 8 */
799
sewardj8cec6ee2003-05-18 11:56:39 +0000800 /* Hack for x86 REP insns. Jump to literal if TempReg/RealReg
801 is zero. */
njnd5bb0a52002-09-27 10:24:48 +0000802 JIFZ,
njn25e49d8e72002-09-23 09:36:25 +0000803
njnd5bb0a52002-09-27 10:24:48 +0000804 /* Advance the simulated %eip by some small (< 128) number. */
805 INCEIP,
njn25e49d8e72002-09-23 09:36:25 +0000806
sewardje1042472002-09-30 12:33:11 +0000807 /* Dealing with segment registers */
808 GETSEG, PUTSEG, /* simulated segment register <--> TempReg */
809 USESEG, /* (LDT/GDT index, virtual addr) --> linear addr */
810
njnd5bb0a52002-09-27 10:24:48 +0000811 /* Not for translating x86 calls -- only to call helpers */
812 CALLM_S, CALLM_E, /* Mark start/end of CALLM push/pop sequence */
813 PUSH, POP, CLEAR, /* Add/remove/zap args for helpers */
814 CALLM, /* Call assembly-code helper */
815
816 /* Not for translating x86 calls -- only to call C helper functions of
817 up to three arguments (or two if the functions has a return value).
818 Arguments and return value must be word-sized. More arguments can
819 be faked with global variables (eg. use VG_(set_global_var)()).
820
821 Seven possibilities: 'arg[123]' show where args go, 'ret' shows
822 where return value goes (if present).
daywalker5d945de2003-09-26 00:32:53 +0000823
njn25e49d8e72002-09-23 09:36:25 +0000824 CCALL(-, -, - ) void f(void)
825 CCALL(arg1, -, - ) void f(UInt arg1)
826 CCALL(arg1, arg2, - ) void f(UInt arg1, UInt arg2)
827 CCALL(arg1, arg2, arg3) void f(UInt arg1, UInt arg2, UInt arg3)
828 CCALL(-, -, ret ) UInt f(UInt)
829 CCALL(arg1, -, ret ) UInt f(UInt arg1)
njnd5bb0a52002-09-27 10:24:48 +0000830 CCALL(arg1, arg2, ret ) UInt f(UInt arg1, UInt arg2) */
njn25e49d8e72002-09-23 09:36:25 +0000831 CCALL,
832
njnd5bb0a52002-09-27 10:24:48 +0000833 /* This opcode makes it easy for skins that extend UCode to do this to
834 avoid opcode overlap:
njn25e49d8e72002-09-23 09:36:25 +0000835
daywalker5d945de2003-09-26 00:32:53 +0000836 enum { EU_OP1 = DUMMY_FINAL_UOPCODE + 1, ... }
837
njn25e49d8e72002-09-23 09:36:25 +0000838 WARNING: Do not add new opcodes after this one! They can be added
839 before, though. */
840 DUMMY_FINAL_UOPCODE
841 }
842 Opcode;
843
844
njnd5bb0a52002-09-27 10:24:48 +0000845/* Condition codes, using the Intel encoding. CondAlways is an extra. */
njn25e49d8e72002-09-23 09:36:25 +0000846typedef
847 enum {
848 CondO = 0, /* overflow */
849 CondNO = 1, /* no overflow */
850 CondB = 2, /* below */
851 CondNB = 3, /* not below */
852 CondZ = 4, /* zero */
853 CondNZ = 5, /* not zero */
854 CondBE = 6, /* below or equal */
855 CondNBE = 7, /* not below or equal */
856 CondS = 8, /* negative */
sewardje1042472002-09-30 12:33:11 +0000857 CondNS = 9, /* not negative */
njn25e49d8e72002-09-23 09:36:25 +0000858 CondP = 10, /* parity even */
859 CondNP = 11, /* not parity even */
860 CondL = 12, /* jump less */
861 CondNL = 13, /* not less */
862 CondLE = 14, /* less or equal */
863 CondNLE = 15, /* not less or equal */
864 CondAlways = 16 /* Jump always */
daywalker5d945de2003-09-26 00:32:53 +0000865 }
njn25e49d8e72002-09-23 09:36:25 +0000866 Condcode;
867
868
869/* Descriptions of additional properties of *unconditional* jumps. */
870typedef
871 enum {
872 JmpBoring=0, /* boring unconditional jump */
873 JmpCall=1, /* jump due to an x86 call insn */
874 JmpRet=2, /* jump due to an x86 ret insn */
875 JmpSyscall=3, /* do a system call, then jump */
876 JmpClientReq=4 /* do a client request, then jump */
877 }
878 JmpKind;
879
880
881/* Flags. User-level code can only read/write O(verflow), S(ign),
882 Z(ero), A(ux-carry), C(arry), P(arity), and may also write
883 D(irection). That's a total of 7 flags. A FlagSet is a bitset,
daywalker5d945de2003-09-26 00:32:53 +0000884 thusly:
njn25e49d8e72002-09-23 09:36:25 +0000885 76543210
886 DOSZACP
887 and bit 7 must always be zero since it is unused.
sewardj2370f3b2002-11-30 15:01:01 +0000888
889 Note: these Flag? values are **not** the positions in the actual
890 %eflags register. */
891
njn25e49d8e72002-09-23 09:36:25 +0000892typedef UChar FlagSet;
893
894#define FlagD (1<<6)
895#define FlagO (1<<5)
896#define FlagS (1<<4)
897#define FlagZ (1<<3)
898#define FlagA (1<<2)
899#define FlagC (1<<1)
900#define FlagP (1<<0)
901
902#define FlagsOSZACP (FlagO | FlagS | FlagZ | FlagA | FlagC | FlagP)
903#define FlagsOSZAP (FlagO | FlagS | FlagZ | FlagA | FlagP)
904#define FlagsOSZCP (FlagO | FlagS | FlagZ | FlagC | FlagP)
905#define FlagsOSACP (FlagO | FlagS | FlagA | FlagC | FlagP)
906#define FlagsSZACP ( FlagS | FlagZ | FlagA | FlagC | FlagP)
907#define FlagsSZAP ( FlagS | FlagZ | FlagA | FlagP)
908#define FlagsZCP ( FlagZ | FlagC | FlagP)
909#define FlagsOC (FlagO | FlagC )
910#define FlagsAC ( FlagA | FlagC )
911
912#define FlagsALL (FlagsOSZACP | FlagD)
913#define FlagsEmpty (FlagSet)0
914
915
sewardj2370f3b2002-11-30 15:01:01 +0000916/* flag positions in eflags */
917#define EFlagC (1 << 0) /* carry */
918#define EFlagP (1 << 2) /* parity */
sewardjfa492d42002-12-08 18:20:01 +0000919#define EFlagA (1 << 4) /* aux carry */
sewardj2370f3b2002-11-30 15:01:01 +0000920#define EFlagZ (1 << 6) /* zero */
921#define EFlagS (1 << 7) /* sign */
sewardjfa492d42002-12-08 18:20:01 +0000922#define EFlagD (1 << 10) /* direction */
sewardj2370f3b2002-11-30 15:01:01 +0000923#define EFlagO (1 << 11) /* overflow */
924
njn25e49d8e72002-09-23 09:36:25 +0000925/* Liveness of general purpose registers, useful for code generation.
926 Reg rank order 0..N-1 corresponds to bits 0..N-1, ie. first
927 reg's liveness in bit 0, last reg's in bit N-1. Note that
928 these rankings don't match the Intel register ordering. */
929typedef UInt RRegSet;
930
931#define ALL_RREGS_DEAD 0 /* 0000...00b */
njne7c258c2002-10-03 08:57:01 +0000932#define ALL_RREGS_LIVE ((1 << VG_MAX_REALREGS)-1) /* 0011...11b */
njn25e49d8e72002-09-23 09:36:25 +0000933#define UNIT_RREGSET(rank) (1 << (rank))
934
935#define IS_RREG_LIVE(rank,rregs_live) (rregs_live & UNIT_RREGSET(rank))
936#define SET_RREG_LIVENESS(rank,rregs_live,b) \
937 do { RRegSet unit = UNIT_RREGSET(rank); \
938 if (b) rregs_live |= unit; \
939 else rregs_live &= ~unit; \
940 } while(0)
941
942
943/* A Micro (u)-instruction. */
944typedef
945 struct {
946 /* word 1 */
947 UInt lit32; /* 32-bit literal */
948
949 /* word 2 */
950 UShort val1; /* first operand */
951 UShort val2; /* second operand */
952
953 /* word 3 */
954 UShort val3; /* third operand */
955 UChar opcode; /* opcode */
956 UChar size; /* data transfer size */
957
958 /* word 4 */
959 FlagSet flags_r; /* :: FlagSet */
960 FlagSet flags_w; /* :: FlagSet */
961 UChar tag1:4; /* first operand tag */
962 UChar tag2:4; /* second operand tag */
963 UChar tag3:4; /* third operand tag */
964 UChar extra4b:4; /* Spare field, used by WIDEN for src
965 -size, and by LEA2 for scale (1,2,4 or 8),
966 and by JMPs for original x86 instr size */
967
968 /* word 5 */
969 UChar cond; /* condition, for jumps */
970 Bool signed_widen:1; /* signed or unsigned WIDEN ? */
971 JmpKind jmpkind:3; /* additional properties of unconditional JMP */
972
daywalker5d945de2003-09-26 00:32:53 +0000973 /* Additional properties for UInstrs that call C functions:
njn25e49d8e72002-09-23 09:36:25 +0000974 - CCALL
975 - PUT (when %ESP is the target)
976 - possibly skin-specific UInstrs
977 */
978 UChar argc:2; /* Number of args, max 3 */
979 UChar regparms_n:2; /* Number of args passed in registers */
980 Bool has_ret_val:1; /* Function has return value? */
981
982 /* RealReg liveness; only sensical after reg alloc and liveness
983 analysis done. This info is a little bit arch-specific --
984 VG_MAX_REALREGS can vary on different architectures. Note that
985 to use this information requires converting between register ranks
njn4ba5a792002-09-30 10:23:54 +0000986 and the Intel register numbers, using VG_(realreg_to_rank)()
987 and/or VG_(rank_to_realreg)() */
daywalker5d945de2003-09-26 00:32:53 +0000988 RRegSet regs_live_after:VG_MAX_REALREGS;
njn25e49d8e72002-09-23 09:36:25 +0000989 }
990 UInstr;
991
992
daywalker5d945de2003-09-26 00:32:53 +0000993typedef
njn810086f2002-11-14 12:42:47 +0000994 struct _UCodeBlock
njn25e49d8e72002-09-23 09:36:25 +0000995 UCodeBlock;
996
njn810086f2002-11-14 12:42:47 +0000997extern Int VG_(get_num_instrs) (UCodeBlock* cb);
998extern Int VG_(get_num_temps) (UCodeBlock* cb);
njn25e49d8e72002-09-23 09:36:25 +0000999
njn810086f2002-11-14 12:42:47 +00001000extern UInstr* VG_(get_instr) (UCodeBlock* cb, Int i);
1001extern UInstr* VG_(get_last_instr) (UCodeBlock* cb);
daywalker5d945de2003-09-26 00:32:53 +00001002
njn211b6ad2003-02-03 12:33:31 +00001003
njn25e49d8e72002-09-23 09:36:25 +00001004/*====================================================================*/
1005/*=== Instrumenting UCode ===*/
1006/*====================================================================*/
1007
njnf4ce3d32003-02-10 10:17:26 +00001008/* Maximum number of registers read or written by a single UInstruction. */
1009#define VG_MAX_REGS_USED 3
njn25e49d8e72002-09-23 09:36:25 +00001010
njnf4ce3d32003-02-10 10:17:26 +00001011/* Find what this instruction does to its regs, useful for
1012 analysis/optimisation passes. `tag' indicates whether we're considering
1013 TempRegs (pre-reg-alloc) or RealRegs (post-reg-alloc). `regs' is filled
1014 with the affected register numbers, `isWrites' parallels it and indicates
1015 if the reg is read or written. If a reg is read and written, it will
1016 appear twice in `regs'. `regs' and `isWrites' must be able to fit
1017 VG_MAX_REGS_USED elements. */
njn810086f2002-11-14 12:42:47 +00001018extern Int VG_(get_reg_usage) ( UInstr* u, Tag tag, Int* regs, Bool* isWrites );
njn25e49d8e72002-09-23 09:36:25 +00001019
1020
njnd5bb0a52002-09-27 10:24:48 +00001021/* Used to register helper functions to be called from generated code. A
1022 limited number of compact helpers can be registered; the code generated
1023 to call them is slightly shorter -- so register the mostly frequently
1024 called helpers as compact. */
njn25e49d8e72002-09-23 09:36:25 +00001025extern void VG_(register_compact_helper) ( Addr a );
1026extern void VG_(register_noncompact_helper) ( Addr a );
1027
1028
1029/* ------------------------------------------------------------------ */
1030/* Virtual register allocation */
1031
1032/* Get a new virtual register */
njn4ba5a792002-09-30 10:23:54 +00001033extern Int VG_(get_new_temp) ( UCodeBlock* cb );
njn25e49d8e72002-09-23 09:36:25 +00001034
1035/* Get a new virtual shadow register */
njn4ba5a792002-09-30 10:23:54 +00001036extern Int VG_(get_new_shadow) ( UCodeBlock* cb );
njn25e49d8e72002-09-23 09:36:25 +00001037
1038/* Get a virtual register's corresponding virtual shadow register */
1039#define SHADOW(tempreg) ((tempreg)+1)
1040
1041
1042/* ------------------------------------------------------------------ */
1043/* Low-level UInstr builders */
njn4ba5a792002-09-30 10:23:54 +00001044extern void VG_(new_NOP) ( UInstr* u );
1045extern void VG_(new_UInstr0) ( UCodeBlock* cb, Opcode opcode, Int sz );
1046extern void VG_(new_UInstr1) ( UCodeBlock* cb, Opcode opcode, Int sz,
njn25e49d8e72002-09-23 09:36:25 +00001047 Tag tag1, UInt val1 );
njn4ba5a792002-09-30 10:23:54 +00001048extern void VG_(new_UInstr2) ( UCodeBlock* cb, Opcode opcode, Int sz,
njn25e49d8e72002-09-23 09:36:25 +00001049 Tag tag1, UInt val1,
1050 Tag tag2, UInt val2 );
njn4ba5a792002-09-30 10:23:54 +00001051extern void VG_(new_UInstr3) ( UCodeBlock* cb, Opcode opcode, Int sz,
njn25e49d8e72002-09-23 09:36:25 +00001052 Tag tag1, UInt val1,
1053 Tag tag2, UInt val2,
1054 Tag tag3, UInt val3 );
njn25e49d8e72002-09-23 09:36:25 +00001055
daywalker5d945de2003-09-26 00:32:53 +00001056/* Set read/write/undefined flags. Undefined flags are treaten as written,
njn810086f2002-11-14 12:42:47 +00001057 but it's worth keeping them logically distinct. */
1058extern void VG_(set_flag_fields) ( UCodeBlock* cb, FlagSet fr, FlagSet fw,
1059 FlagSet fu);
njn4ba5a792002-09-30 10:23:54 +00001060extern void VG_(set_lit_field) ( UCodeBlock* cb, UInt lit32 );
1061extern void VG_(set_ccall_fields) ( UCodeBlock* cb, Addr fn, UChar argc,
1062 UChar regparms_n, Bool has_ret_val );
njn810086f2002-11-14 12:42:47 +00001063extern void VG_(set_cond_field) ( UCodeBlock* cb, Condcode code );
njn25e49d8e72002-09-23 09:36:25 +00001064
njn4ba5a792002-09-30 10:23:54 +00001065extern void VG_(copy_UInstr) ( UCodeBlock* cb, UInstr* instr );
1066
1067extern Bool VG_(any_flag_use)( UInstr* u );
njn25e49d8e72002-09-23 09:36:25 +00001068
njnac6c1762002-10-04 14:34:15 +00001069/* Macro versions of the above; just shorter to type. */
1070#define uInstr0 VG_(new_UInstr0)
1071#define uInstr1 VG_(new_UInstr1)
1072#define uInstr2 VG_(new_UInstr2)
1073#define uInstr3 VG_(new_UInstr3)
1074#define uLiteral VG_(set_lit_field)
1075#define uCCall VG_(set_ccall_fields)
njn810086f2002-11-14 12:42:47 +00001076#define uCond VG_(set_cond_field)
1077#define uFlagsRWU VG_(set_flag_fields)
njnac6c1762002-10-04 14:34:15 +00001078#define newTemp VG_(get_new_temp)
1079#define newShadow VG_(get_new_shadow)
1080
njn25e49d8e72002-09-23 09:36:25 +00001081/* Refer to `the last instruction stuffed in' (can be lvalue). */
1082#define LAST_UINSTR(cb) (cb)->instrs[(cb)->used-1]
1083
1084
1085/* ------------------------------------------------------------------ */
1086/* Higher-level UInstr sequence builders */
njn4ba5a792002-09-30 10:23:54 +00001087extern void VG_(call_helper_0_0) ( UCodeBlock* cb, Addr f);
1088extern void VG_(call_helper_1_0) ( UCodeBlock* cb, Addr f, UInt arg1,
1089 UInt regparms_n);
1090extern void VG_(call_helper_2_0) ( UCodeBlock* cb, Addr f, UInt arg1, UInt arg2,
1091 UInt regparms_n);
njn25e49d8e72002-09-23 09:36:25 +00001092
1093/* One way around the 3-arg C function limit is to pass args via global
njn6fefa1b2003-02-24 10:32:51 +00001094 * variables... ugly, but it works. This one puts a literal in there. */
njn4ba5a792002-09-30 10:23:54 +00001095extern void VG_(set_global_var) ( UCodeBlock* cb, Addr globvar_ptr, UInt val);
njn25e49d8e72002-09-23 09:36:25 +00001096
njn6fefa1b2003-02-24 10:32:51 +00001097/* This one puts the contents of a TempReg in the global variable. */
1098extern void VG_(set_global_var_tempreg) ( UCodeBlock* cb, Addr globvar_ptr,
1099 UInt t_val);
1100
njn25e49d8e72002-09-23 09:36:25 +00001101/* ------------------------------------------------------------------ */
njnd5bb0a52002-09-27 10:24:48 +00001102/* Allocating/freeing basic blocks of UCode */
njn810086f2002-11-14 12:42:47 +00001103extern UCodeBlock* VG_(setup_UCodeBlock) ( UCodeBlock* cb );
njn4ba5a792002-09-30 10:23:54 +00001104extern void VG_(free_UCodeBlock) ( UCodeBlock* cb );
njnd5bb0a52002-09-27 10:24:48 +00001105
1106/* ------------------------------------------------------------------ */
daywalker5d945de2003-09-26 00:32:53 +00001107/* UCode pretty/ugly printing. Probably only useful to call from a skin
njn25e49d8e72002-09-23 09:36:25 +00001108 if VG_(needs).extended_UCode == True. */
1109
1110/* When True, all generated code is/should be printed. */
1111extern Bool VG_(print_codegen);
1112
njn4ba5a792002-09-30 10:23:54 +00001113/* Pretty/ugly printing functions */
1114extern void VG_(pp_UCodeBlock) ( UCodeBlock* cb, Char* title );
1115extern void VG_(pp_UInstr) ( Int instrNo, UInstr* u );
1116extern void VG_(pp_UInstr_regs) ( Int instrNo, UInstr* u );
1117extern void VG_(up_UInstr) ( Int instrNo, UInstr* u );
1118extern Char* VG_(name_UOpcode) ( Bool upper, Opcode opc );
njn563f96f2003-02-03 11:17:46 +00001119extern Char* VG_(name_UCondcode) ( Condcode cond );
daywalker5d945de2003-09-26 00:32:53 +00001120extern void VG_(pp_UOperand) ( UInstr* u, Int operandNo,
njn4ba5a792002-09-30 10:23:54 +00001121 Int sz, Bool parens );
njn25e49d8e72002-09-23 09:36:25 +00001122
njnb93d1782003-02-03 12:03:22 +00001123/* ------------------------------------------------------------------ */
njnf4ce3d32003-02-10 10:17:26 +00001124/* Accessing archregs and their shadows */
1125extern UInt VG_(get_archreg) ( UInt archreg );
1126extern UInt VG_(get_thread_archreg) ( ThreadId tid, UInt archreg );
1127
njnb93d1782003-02-03 12:03:22 +00001128extern UInt VG_(get_shadow_archreg) ( UInt archreg );
1129extern void VG_(set_shadow_archreg) ( UInt archreg, UInt val );
njnd3040452003-05-19 15:04:06 +00001130extern void VG_(set_shadow_eflags) ( UInt val );
njnb93d1782003-02-03 12:03:22 +00001131extern Addr VG_(shadow_archreg_address) ( UInt archreg );
njn25e49d8e72002-09-23 09:36:25 +00001132
njnf4ce3d32003-02-10 10:17:26 +00001133extern UInt VG_(get_thread_shadow_archreg) ( ThreadId tid, UInt archreg );
1134extern void VG_(set_thread_shadow_archreg) ( ThreadId tid, UInt archreg,
1135 UInt val );
njn211b6ad2003-02-03 12:33:31 +00001136
1137/* ------------------------------------------------------------------ */
1138/* Offsets of addresses of helper functions. A "helper" function is one
1139 which is called from generated code via CALLM. */
1140
1141extern Int VGOFF_(helper_idiv_64_32);
1142extern Int VGOFF_(helper_div_64_32);
1143extern Int VGOFF_(helper_idiv_32_16);
1144extern Int VGOFF_(helper_div_32_16);
1145extern Int VGOFF_(helper_idiv_16_8);
1146extern Int VGOFF_(helper_div_16_8);
1147
1148extern Int VGOFF_(helper_imul_32_64);
1149extern Int VGOFF_(helper_mul_32_64);
1150extern Int VGOFF_(helper_imul_16_32);
1151extern Int VGOFF_(helper_mul_16_32);
1152extern Int VGOFF_(helper_imul_8_16);
1153extern Int VGOFF_(helper_mul_8_16);
1154
1155extern Int VGOFF_(helper_CLD);
1156extern Int VGOFF_(helper_STD);
1157extern Int VGOFF_(helper_get_dirflag);
1158
1159extern Int VGOFF_(helper_CLC);
1160extern Int VGOFF_(helper_STC);
1161
1162extern Int VGOFF_(helper_shldl);
1163extern Int VGOFF_(helper_shldw);
1164extern Int VGOFF_(helper_shrdl);
1165extern Int VGOFF_(helper_shrdw);
1166
1167extern Int VGOFF_(helper_RDTSC);
1168extern Int VGOFF_(helper_CPUID);
1169
daywalkerb18d2532003-09-27 20:15:01 +00001170extern Int VGOFF_(helper_IN);
1171extern Int VGOFF_(helper_OUT);
1172
njn211b6ad2003-02-03 12:33:31 +00001173extern Int VGOFF_(helper_bsf);
1174extern Int VGOFF_(helper_bsr);
1175
1176extern Int VGOFF_(helper_fstsw_AX);
1177extern Int VGOFF_(helper_SAHF);
njnd6251f12003-06-03 13:38:51 +00001178extern Int VGOFF_(helper_LAHF);
njn211b6ad2003-02-03 12:33:31 +00001179extern Int VGOFF_(helper_DAS);
1180extern Int VGOFF_(helper_DAA);
1181
1182
njn25e49d8e72002-09-23 09:36:25 +00001183/*====================================================================*/
njnd5bb0a52002-09-27 10:24:48 +00001184/*=== Generating x86 code from UCode ===*/
njn25e49d8e72002-09-23 09:36:25 +00001185/*====================================================================*/
1186
njnd5bb0a52002-09-27 10:24:48 +00001187/* All this only necessary for skins with VG_(needs).extends_UCode == True. */
njn25e49d8e72002-09-23 09:36:25 +00001188
sewardje1042472002-09-30 12:33:11 +00001189/* This is the Intel register encoding -- integer regs. */
njn25e49d8e72002-09-23 09:36:25 +00001190#define R_EAX 0
1191#define R_ECX 1
1192#define R_EDX 2
1193#define R_EBX 3
1194#define R_ESP 4
1195#define R_EBP 5
1196#define R_ESI 6
1197#define R_EDI 7
1198
1199#define R_AL (0+R_EAX)
1200#define R_CL (0+R_ECX)
1201#define R_DL (0+R_EDX)
1202#define R_BL (0+R_EBX)
1203#define R_AH (4+R_EAX)
1204#define R_CH (4+R_ECX)
1205#define R_DH (4+R_EDX)
1206#define R_BH (4+R_EBX)
1207
sewardje1042472002-09-30 12:33:11 +00001208/* This is the Intel register encoding -- segment regs. */
1209#define R_ES 0
1210#define R_CS 1
1211#define R_SS 2
1212#define R_DS 3
1213#define R_FS 4
1214#define R_GS 5
1215
njn25e49d8e72002-09-23 09:36:25 +00001216/* For pretty printing x86 code */
daywalker5d945de2003-09-26 00:32:53 +00001217extern const Char* VG_(name_of_mmx_gran) ( UChar gran );
1218extern const Char* VG_(name_of_mmx_reg) ( Int mmxreg );
1219extern const Char* VG_(name_of_seg_reg) ( Int sreg );
1220extern const Char* VG_(name_of_int_reg) ( Int size, Int reg );
1221extern const Char VG_(name_of_int_size) ( Int size );
njn25e49d8e72002-09-23 09:36:25 +00001222
njnac6c1762002-10-04 14:34:15 +00001223/* Shorter macros for convenience */
sewardj3d7c9c82003-03-26 21:08:13 +00001224#define nameIReg VG_(name_of_int_reg)
1225#define nameISize VG_(name_of_int_size)
1226#define nameSReg VG_(name_of_seg_reg)
1227#define nameMMXReg VG_(name_of_mmx_reg)
1228#define nameMMXGran VG_(name_of_mmx_gran)
sewardjfebaa3b2003-05-25 01:07:34 +00001229#define nameXMMReg VG_(name_of_xmm_reg)
njnac6c1762002-10-04 14:34:15 +00001230
njn25e49d8e72002-09-23 09:36:25 +00001231/* Randomly useful things */
1232extern UInt VG_(extend_s_8to32) ( UInt x );
1233
1234/* Code emitters */
njn4ba5a792002-09-30 10:23:54 +00001235extern void VG_(emitB) ( UInt b );
1236extern void VG_(emitW) ( UInt w );
1237extern void VG_(emitL) ( UInt l );
sewardjfa492d42002-12-08 18:20:01 +00001238extern void VG_(new_emit) ( Bool upd_cc, FlagSet uses_flags, FlagSet sets_flags );
njn25e49d8e72002-09-23 09:36:25 +00001239
1240/* Finding offsets */
njn4ba5a792002-09-30 10:23:54 +00001241extern Int VG_(helper_offset) ( Addr a );
1242extern Int VG_(shadow_reg_offset) ( Int arch );
1243extern Int VG_(shadow_flags_offset) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001244
njnd5bb0a52002-09-27 10:24:48 +00001245/* Convert reg ranks <-> Intel register ordering, for using register
1246 liveness information. */
njn4ba5a792002-09-30 10:23:54 +00001247extern Int VG_(realreg_to_rank) ( Int realreg );
1248extern Int VG_(rank_to_realreg) ( Int rank );
njn25e49d8e72002-09-23 09:36:25 +00001249
njnd5bb0a52002-09-27 10:24:48 +00001250/* Call a subroutine. Does no argument passing, stack manipulations, etc. */
daywalker5d945de2003-09-26 00:32:53 +00001251extern void VG_(synth_call) ( Bool ensure_shortform, Int word_offset,
sewardjfa492d42002-12-08 18:20:01 +00001252 Bool upd_cc, FlagSet use_flags, FlagSet set_flags );
njn25e49d8e72002-09-23 09:36:25 +00001253
njnd5bb0a52002-09-27 10:24:48 +00001254/* For calling C functions -- saves caller save regs, pushes args, calls,
1255 clears the stack, restores caller save regs. `fn' must be registered in
1256 the baseBlock first. Acceptable tags are RealReg and Literal. Optimises
1257 things, eg. by not preserving non-live caller-save registers.
njn25e49d8e72002-09-23 09:36:25 +00001258
njnd5bb0a52002-09-27 10:24:48 +00001259 WARNING: a UInstr should *not* be translated with synth_ccall() followed
1260 by some other x86 assembly code; this will invalidate the results of
1261 vg_realreg_liveness_analysis() and everything will fall over. */
njn4ba5a792002-09-30 10:23:54 +00001262extern void VG_(synth_ccall) ( Addr fn, Int argc, Int regparms_n, UInt argv[],
daywalker5d945de2003-09-26 00:32:53 +00001263 Tag tagv[], Int ret_reg,
njn4ba5a792002-09-30 10:23:54 +00001264 RRegSet regs_live_before,
1265 RRegSet regs_live_after );
njn25e49d8e72002-09-23 09:36:25 +00001266
1267/* Addressing modes */
njn4ba5a792002-09-30 10:23:54 +00001268extern void VG_(emit_amode_offregmem_reg)( Int off, Int regmem, Int reg );
1269extern void VG_(emit_amode_ereg_greg) ( Int e_reg, Int g_reg );
njn25e49d8e72002-09-23 09:36:25 +00001270
1271/* v-size (4, or 2 with OSO) insn emitters */
njn4ba5a792002-09-30 10:23:54 +00001272extern void VG_(emit_movv_offregmem_reg) ( Int sz, Int off, Int areg, Int reg );
1273extern void VG_(emit_movv_reg_offregmem) ( Int sz, Int reg, Int off, Int areg );
1274extern void VG_(emit_movv_reg_reg) ( Int sz, Int reg1, Int reg2 );
sewardjfa492d42002-12-08 18:20:01 +00001275extern void VG_(emit_nonshiftopv_lit_reg)( Bool upd_cc, Int sz, Opcode opc, UInt lit,
njn4ba5a792002-09-30 10:23:54 +00001276 Int reg );
sewardjfa492d42002-12-08 18:20:01 +00001277extern void VG_(emit_shiftopv_lit_reg) ( Bool upd_cc, Int sz, Opcode opc, UInt lit,
njn4ba5a792002-09-30 10:23:54 +00001278 Int reg );
sewardjfa492d42002-12-08 18:20:01 +00001279extern void VG_(emit_nonshiftopv_reg_reg)( Bool upd_cc, Int sz, Opcode opc,
njn4ba5a792002-09-30 10:23:54 +00001280 Int reg1, Int reg2 );
1281extern void VG_(emit_movv_lit_reg) ( Int sz, UInt lit, Int reg );
sewardjfa492d42002-12-08 18:20:01 +00001282extern void VG_(emit_unaryopv_reg) ( Bool upd_cc, Int sz, Opcode opc, Int reg );
njn4ba5a792002-09-30 10:23:54 +00001283extern void VG_(emit_pushv_reg) ( Int sz, Int reg );
1284extern void VG_(emit_popv_reg) ( Int sz, Int reg );
njn25e49d8e72002-09-23 09:36:25 +00001285
njn4ba5a792002-09-30 10:23:54 +00001286extern void VG_(emit_pushl_lit32) ( UInt int32 );
1287extern void VG_(emit_pushl_lit8) ( Int lit8 );
sewardjfa492d42002-12-08 18:20:01 +00001288extern void VG_(emit_cmpl_zero_reg) ( Bool upd_cc, Int reg );
njn4ba5a792002-09-30 10:23:54 +00001289extern void VG_(emit_swapl_reg_EAX) ( Int reg );
1290extern void VG_(emit_movv_lit_offregmem) ( Int sz, UInt lit, Int off,
1291 Int memreg );
njn25e49d8e72002-09-23 09:36:25 +00001292
1293/* b-size (1 byte) instruction emitters */
njn4ba5a792002-09-30 10:23:54 +00001294extern void VG_(emit_movb_lit_offregmem) ( UInt lit, Int off, Int memreg );
1295extern void VG_(emit_movb_reg_offregmem) ( Int reg, Int off, Int areg );
sewardjfa492d42002-12-08 18:20:01 +00001296extern void VG_(emit_unaryopb_reg) ( Bool upd_cc, Opcode opc, Int reg );
1297extern void VG_(emit_testb_lit_reg) ( Bool upd_cc, UInt lit, Int reg );
njn25e49d8e72002-09-23 09:36:25 +00001298
1299/* zero-extended load emitters */
fitzhardinge98abfc72003-12-16 02:05:15 +00001300extern void VG_(emit_movzbl_offregmem_reg) ( Bool bounds, Int off, Int regmem, Int reg );
1301extern void VG_(emit_movzwl_offregmem_reg) ( Bool bounds, Int off, Int areg, Int reg );
1302extern void VG_(emit_movzwl_regmem_reg) ( Bool bounds, Int reg1, Int reg2 );
njn25e49d8e72002-09-23 09:36:25 +00001303
1304/* misc instruction emitters */
njn4ba5a792002-09-30 10:23:54 +00001305extern void VG_(emit_call_reg) ( Int reg );
1306extern void VG_(emit_add_lit_to_esp) ( Int lit );
njn4ba5a792002-09-30 10:23:54 +00001307extern void VG_(emit_pushal) ( void );
1308extern void VG_(emit_popal) ( void );
1309extern void VG_(emit_AMD_prefetch_reg) ( Int reg );
njn25e49d8e72002-09-23 09:36:25 +00001310
sewardja2113f92002-12-12 23:42:48 +00001311/* jump emitters */
1312extern void VG_(init_target) ( Int *tgt );
1313
1314extern void VG_(target_back) ( Int *tgt );
1315extern void VG_(target_forward) ( Int *tgt );
1316extern void VG_(emit_target_delta) ( Int *tgt );
1317
1318extern void VG_(emit_jcondshort_delta) ( Bool simd_cc, Condcode cond, Int delta );
1319extern void VG_(emit_jcondshort_target)( Bool simd_cc, Condcode cond, Int *tgt );
1320
njn25e49d8e72002-09-23 09:36:25 +00001321
1322/*====================================================================*/
1323/*=== Execution contexts ===*/
1324/*====================================================================*/
1325
1326/* Generic resolution type used in a few different ways, such as deciding
1327 how closely to compare two errors for equality. */
daywalker5d945de2003-09-26 00:32:53 +00001328typedef
1329 enum { Vg_LowRes, Vg_MedRes, Vg_HighRes }
njn25e49d8e72002-09-23 09:36:25 +00001330 VgRes;
1331
1332typedef
1333 struct _ExeContext
1334 ExeContext;
1335
daywalker5d945de2003-09-26 00:32:53 +00001336/* Compare two ExeContexts. Number of callers considered depends on `res':
1337 Vg_LowRes: 2
1338 Vg_MedRes: 4
njnd5bb0a52002-09-27 10:24:48 +00001339 Vg_HighRes: all */
njn25e49d8e72002-09-23 09:36:25 +00001340extern Bool VG_(eq_ExeContext) ( VgRes res,
1341 ExeContext* e1, ExeContext* e2 );
1342
1343/* Print an ExeContext. */
1344extern void VG_(pp_ExeContext) ( ExeContext* );
1345
1346/* Take a snapshot of the client's stack. Search our collection of
1347 ExeContexts to see if we already have it, and if not, allocate a
njn6c846552003-09-16 07:41:43 +00001348 new one. Either way, return a pointer to the context. Context size
1349 controlled by --num-callers option.
daywalker5d945de2003-09-26 00:32:53 +00001350
njn72718642003-07-24 08:45:32 +00001351 If called from generated code, use VG_(get_current_tid)() to get the
1352 current ThreadId. If called from non-generated code, the current
daywalker5d945de2003-09-26 00:32:53 +00001353 ThreadId should be passed in by the core.
njn7b456f52003-05-19 11:16:50 +00001354*/
njn72718642003-07-24 08:45:32 +00001355extern ExeContext* VG_(get_ExeContext) ( ThreadId tid );
njn25e49d8e72002-09-23 09:36:25 +00001356
njn6c846552003-09-16 07:41:43 +00001357/* Get the nth EIP from the ExeContext. 0 is the EIP of the top function, 1
1358 is its caller, etc. Returns 0 if there isn't one, or if n is greater
1359 than VG_(clo_backtrace_size), set by the --num-callers option. */
1360extern Addr VG_(get_EIP_from_ExeContext) ( ExeContext* e, UInt n );
1361
sewardj499e3de2002-11-13 22:22:25 +00001362/* Just grab the client's EIP, as a much smaller and cheaper
njn72718642003-07-24 08:45:32 +00001363 indication of where they are. Use is basically same as for
daywalker5d945de2003-09-26 00:32:53 +00001364 VG_(get_ExeContext)() above.
njn72718642003-07-24 08:45:32 +00001365*/
1366extern Addr VG_(get_EIP)( ThreadId tid );
njn25e49d8e72002-09-23 09:36:25 +00001367
njn6c846552003-09-16 07:41:43 +00001368/* For skins needing more control over stack traces: walks the stack to get
1369 %eips from the top stack frames for thread 'tid'. Maximum of 'n_eips'
1370 addresses put into 'eips'; 0 is the top of the stack, 1 is its caller,
1371 etc. */
1372extern UInt VG_(stack_snapshot) ( ThreadId tid, Addr* eips, UInt n_eips );
1373
1374/* Does the same thing as VG_(pp_ExeContext)(), just with slightly
1375 different input. */
1376extern void VG_(mini_stack_dump) ( Addr eips[], UInt n_eips );
1377
njn211b6ad2003-02-03 12:33:31 +00001378
njn25e49d8e72002-09-23 09:36:25 +00001379/*====================================================================*/
1380/*=== Error reporting ===*/
1381/*====================================================================*/
1382
1383/* ------------------------------------------------------------------ */
daywalker5d945de2003-09-26 00:32:53 +00001384/* Suppressions describe errors which we want to suppress, ie, not
njn25e49d8e72002-09-23 09:36:25 +00001385 show the user, usually because it is caused by a problem in a library
daywalker5d945de2003-09-26 00:32:53 +00001386 which we can't fix, replace or work around. Suppressions are read from
njnd5bb0a52002-09-27 10:24:48 +00001387 a file at startup time. This gives flexibility so that new
njn25e49d8e72002-09-23 09:36:25 +00001388 suppressions can be added to the file as and when needed.
1389*/
1390
1391typedef
1392 Int /* Do not make this unsigned! */
1393 SuppKind;
1394
njn810086f2002-11-14 12:42:47 +00001395/* The skin-relevant parts of a suppression are:
1396 kind: what kind of suppression; must be in the range (0..)
1397 string: use is optional. NULL by default.
1398 extra: use is optional. NULL by default. void* so it's extensible.
njn25e49d8e72002-09-23 09:36:25 +00001399*/
1400typedef
njn810086f2002-11-14 12:42:47 +00001401 struct _Supp
1402 Supp;
1403
1404/* Useful in SK_(error_matches_suppression)() */
1405SuppKind VG_(get_supp_kind) ( Supp* su );
1406Char* VG_(get_supp_string) ( Supp* su );
1407void* VG_(get_supp_extra) ( Supp* su );
1408
1409/* Must be used in VG_(recognised_suppression)() */
1410void VG_(set_supp_kind) ( Supp* su, SuppKind suppkind );
1411/* May be used in VG_(read_extra_suppression_info)() */
1412void VG_(set_supp_string) ( Supp* su, Char* string );
1413void VG_(set_supp_extra) ( Supp* su, void* extra );
njn25e49d8e72002-09-23 09:36:25 +00001414
1415
1416/* ------------------------------------------------------------------ */
1417/* Error records contain enough info to generate an error report. The idea
1418 is that (typically) the same few points in the program generate thousands
njnd5bb0a52002-09-27 10:24:48 +00001419 of errors, and we don't want to spew out a fresh error message for each
1420 one. Instead, we use these structures to common up duplicates.
njn25e49d8e72002-09-23 09:36:25 +00001421*/
1422
1423typedef
1424 Int /* Do not make this unsigned! */
1425 ErrorKind;
1426
njn810086f2002-11-14 12:42:47 +00001427/* The skin-relevant parts of an Error are:
1428 kind: what kind of error; must be in the range (0..)
1429 addr: use is optional. 0 by default.
1430 string: use is optional. NULL by default.
1431 extra: use is optional. NULL by default. void* so it's extensible.
njn25e49d8e72002-09-23 09:36:25 +00001432*/
1433typedef
njn810086f2002-11-14 12:42:47 +00001434 struct _Error
1435 Error;
njn25e49d8e72002-09-23 09:36:25 +00001436
njn810086f2002-11-14 12:42:47 +00001437/* Useful in SK_(error_matches_suppression)(), SK_(pp_SkinError)(), etc */
njnb877d492003-01-28 20:40:57 +00001438ExeContext* VG_(get_error_where) ( Error* err );
1439SuppKind VG_(get_error_kind) ( Error* err );
1440Addr VG_(get_error_address) ( Error* err );
1441Char* VG_(get_error_string) ( Error* err );
1442void* VG_(get_error_extra) ( Error* err );
njn25e49d8e72002-09-23 09:36:25 +00001443
njnd5bb0a52002-09-27 10:24:48 +00001444/* Call this when an error occurs. It will be recorded if it hasn't been
njn25e49d8e72002-09-23 09:36:25 +00001445 seen before. If it has, the existing error record will have its count
daywalker5d945de2003-09-26 00:32:53 +00001446 incremented.
1447
njn72718642003-07-24 08:45:32 +00001448 'tid' can be found as for VG_(get_ExeContext)(). The `extra' field can
1449 be stack-allocated; it will be copied by the core if needed (but it
1450 won't be copied if it's NULL).
njnd5bb0a52002-09-27 10:24:48 +00001451
1452 If no 'a', 's' or 'extra' of interest needs to be recorded, just use
njn3e884182003-04-15 13:03:23 +00001453 NULL for them. */
daywalker5d945de2003-09-26 00:32:53 +00001454extern void VG_(maybe_record_error) ( ThreadId tid, ErrorKind ekind,
njn25e49d8e72002-09-23 09:36:25 +00001455 Addr a, Char* s, void* extra );
1456
njn43c799e2003-04-08 00:08:52 +00001457/* Similar to VG_(maybe_record_error)(), except this one doesn't record the
1458 error -- useful for errors that can only happen once. The errors can be
1459 suppressed, though. Return value is True if it was suppressed.
daywalker5d945de2003-09-26 00:32:53 +00001460 `print_error' dictates whether to print the error, which is a bit of a
njn43c799e2003-04-08 00:08:52 +00001461 hack that's useful sometimes if you just want to know if the error would
daywalker5d945de2003-09-26 00:32:53 +00001462 be suppressed without possibly printing it. `count_error' dictates
njn47363ab2003-04-21 13:24:40 +00001463 whether to add the error in the error total count (another mild hack). */
njn72718642003-07-24 08:45:32 +00001464extern Bool VG_(unique_error) ( ThreadId tid, ErrorKind ekind,
njn43c799e2003-04-08 00:08:52 +00001465 Addr a, Char* s, void* extra,
njn3e884182003-04-15 13:03:23 +00001466 ExeContext* where, Bool print_error,
njn47363ab2003-04-21 13:24:40 +00001467 Bool allow_GDB_attach, Bool count_error );
njn43c799e2003-04-08 00:08:52 +00001468
njn25e49d8e72002-09-23 09:36:25 +00001469/* Gets a non-blank, non-comment line of at most nBuf chars from fd.
daywalker5d945de2003-09-26 00:32:53 +00001470 Skips leading spaces on the line. Returns True if EOF was hit instead.
njn3e884182003-04-15 13:03:23 +00001471 Useful for reading in extra skin-specific suppression lines. */
njn4ba5a792002-09-30 10:23:54 +00001472extern Bool VG_(get_line) ( Int fd, Char* buf, Int nBuf );
njn25e49d8e72002-09-23 09:36:25 +00001473
1474
1475/*====================================================================*/
1476/*=== Obtaining debug information ===*/
1477/*====================================================================*/
1478
sewardj6e008cb2002-12-15 13:11:39 +00001479/* Get the file/function/line number of the instruction at address
1480 'a'. For these four, if debug info for the address is found, it
1481 copies the info into the buffer/UInt and returns True. If not, it
1482 returns False and nothing is copied. VG_(get_fnname) always
1483 demangles C++ function names. VG_(get_fnname_w_offset) is the
njn3e884182003-04-15 13:03:23 +00001484 same, except it appends "+N" to symbol names to indicate offsets. */
njn25e49d8e72002-09-23 09:36:25 +00001485extern Bool VG_(get_filename) ( Addr a, Char* filename, Int n_filename );
1486extern Bool VG_(get_fnname) ( Addr a, Char* fnname, Int n_fnname );
1487extern Bool VG_(get_linenum) ( Addr a, UInt* linenum );
daywalker5d945de2003-09-26 00:32:53 +00001488extern Bool VG_(get_fnname_w_offset)
sewardj6e008cb2002-12-15 13:11:39 +00001489 ( Addr a, Char* fnname, Int n_fnname );
njn25e49d8e72002-09-23 09:36:25 +00001490
1491/* This one is more efficient if getting both filename and line number,
1492 because the two lookups are done together. */
daywalker5d945de2003-09-26 00:32:53 +00001493extern Bool VG_(get_filename_linenum)
njn25e49d8e72002-09-23 09:36:25 +00001494 ( Addr a, Char* filename, Int n_filename,
1495 UInt* linenum );
1496
1497/* Succeeds only if we find from debug info that 'a' is the address of the
1498 first instruction in a function -- as opposed to VG_(get_fnname) which
1499 succeeds if we find from debug info that 'a' is the address of any
1500 instruction in a function. Use this to instrument the start of
njnd5bb0a52002-09-27 10:24:48 +00001501 a particular function. Nb: if an executable/shared object is stripped
njn25e49d8e72002-09-23 09:36:25 +00001502 of its symbols, this function will not be able to recognise function
1503 entry points within it. */
njn497ec3f2003-05-19 08:38:23 +00001504extern Bool VG_(get_fnname_if_entry) ( Addr a, Char* fnname, Int n_fnname );
njn25e49d8e72002-09-23 09:36:25 +00001505
1506/* Succeeds if the address is within a shared object or the main executable.
1507 It doesn't matter if debug info is present or not. */
1508extern Bool VG_(get_objname) ( Addr a, Char* objname, Int n_objname );
1509
njn6c846552003-09-16 07:41:43 +00001510/* Puts into 'buf' info about the code address %eip: the address, function
1511 name (if known) and filename/line number (if known), like this:
1512
1513 0x4001BF05: realloc (vg_replace_malloc.c:339)
1514
1515 'n_buf' gives length of 'buf'. Returns 'buf'.
1516*/
1517extern Char* VG_(describe_eip)(Addr eip, Char* buf, Int n_buf);
1518
jsgfcb1d1c02003-10-14 21:55:10 +00001519/* Returns a string containing an expression for the given
1520 address. String is malloced with VG_(malloc)() */
1521Char *VG_(describe_addr)(ThreadId, Addr);
1522
sewardj47104382002-10-20 18:35:48 +00001523/* A way to get information about what segments are mapped */
1524typedef struct _SegInfo SegInfo;
1525
njnb877d492003-01-28 20:40:57 +00001526/* Returns NULL if the SegInfo isn't found. It doesn't matter if debug info
1527 is present or not. */
1528extern SegInfo* VG_(get_obj) ( Addr a );
1529
1530extern const SegInfo* VG_(next_seginfo) ( const SegInfo *seg );
1531extern Addr VG_(seg_start) ( const SegInfo *seg );
1532extern UInt VG_(seg_size) ( const SegInfo *seg );
1533extern const UChar* VG_(seg_filename) ( const SegInfo *seg );
1534extern UInt VG_(seg_sym_offset)( const SegInfo *seg );
sewardj47104382002-10-20 18:35:48 +00001535
1536typedef
1537 enum {
1538 Vg_SectUnknown,
1539 Vg_SectText,
sewardj8fe15a32002-10-20 19:29:21 +00001540 Vg_SectData,
1541 Vg_SectBSS,
sewardj47104382002-10-20 18:35:48 +00001542 Vg_SectGOT,
sewardj8fe15a32002-10-20 19:29:21 +00001543 Vg_SectPLT,
sewardj47104382002-10-20 18:35:48 +00001544 }
1545 VgSectKind;
1546
1547extern VgSectKind VG_(seg_sect_kind)(Addr);
1548
njn25e49d8e72002-09-23 09:36:25 +00001549
1550/*====================================================================*/
njn3e884182003-04-15 13:03:23 +00001551/*=== Generic hash table ===*/
njn25e49d8e72002-09-23 09:36:25 +00001552/*====================================================================*/
1553
njn3e884182003-04-15 13:03:23 +00001554/* Generic type for a separately-chained hash table. Via a kind of dodgy
1555 C-as-C++ style inheritance, skins can extend the VgHashNode type, so long
1556 as the first two fields match the sizes of these two fields. Requires
1557 a bit of casting by the skin. */
njn25e49d8e72002-09-23 09:36:25 +00001558typedef
njn3e884182003-04-15 13:03:23 +00001559 struct _VgHashNode {
1560 struct _VgHashNode * next;
1561 UInt key;
1562 }
1563 VgHashNode;
njn25e49d8e72002-09-23 09:36:25 +00001564
njn3e884182003-04-15 13:03:23 +00001565typedef
1566 VgHashNode**
1567 VgHashTable;
njn810086f2002-11-14 12:42:47 +00001568
njn3e884182003-04-15 13:03:23 +00001569/* Make a new table. */
1570extern VgHashTable VG_(HT_construct) ( void );
1571
njn69c06872003-09-30 15:43:51 +00001572/* Count the number of nodes in a table. */
1573extern Int VG_(HT_count_nodes) ( VgHashTable table );
1574
njn3e884182003-04-15 13:03:23 +00001575/* Add a node to the table. */
1576extern void VG_(HT_add_node) ( VgHashTable t, VgHashNode* node );
1577
daywalker5d945de2003-09-26 00:32:53 +00001578/* Looks up a node in the hash table. Also returns the address of the
njn3e884182003-04-15 13:03:23 +00001579 previous node's `next' pointer which allows it to be removed from the
1580 list later without having to look it up again. */
1581extern VgHashNode* VG_(HT_get_node) ( VgHashTable t, UInt key,
1582 /*OUT*/VgHashNode*** next_ptr );
1583
njn06072ec2003-09-30 15:35:13 +00001584/* Allocates an array of pointers to all the shadow chunks of malloc'd
1585 blocks. Must be freed with VG_(free)(). */
1586extern VgHashNode** VG_(HT_to_array) ( VgHashTable t, /*OUT*/ UInt* n_shadows );
njn3e884182003-04-15 13:03:23 +00001587
1588/* Returns first node that matches predicate `p', or NULL if none do.
1589 Extra arguments can be implicitly passed to `p' using nested functions;
1590 see memcheck/mc_errcontext.c for an example. */
1591extern VgHashNode* VG_(HT_first_match) ( VgHashTable t,
1592 Bool (*p)(VgHashNode*) );
1593
1594/* Applies a function f() once to each node. Again, nested functions
1595 can be very useful. */
1596extern void VG_(HT_apply_to_all_nodes)( VgHashTable t, void (*f)(VgHashNode*) );
1597
1598/* Destroy a table. */
1599extern void VG_(HT_destruct) ( VgHashTable t );
njn810086f2002-11-14 12:42:47 +00001600
1601
njn3e884182003-04-15 13:03:23 +00001602/*====================================================================*/
fitzhardinge98abfc72003-12-16 02:05:15 +00001603/*=== A generic skiplist ===*/
1604/*====================================================================*/
1605
1606/*
1607 The idea here is that the skiplist puts its per-element data at the
1608 end of the structure. When you initialize the skiplist, you tell
1609 it what structure your list elements are going to be. Then you
1610 should allocate them with VG_(SkipNode_Alloc), which will allocate
1611 enough memory for the extra bits.
1612 */
1613#include <stddef.h> /* for offsetof */
1614
1615typedef struct _SkipList SkipList;
1616typedef struct _SkipNode SkipNode;
1617
1618typedef Int (*SkipCmp_t)(const void *key1, const void *key2);
1619
1620struct _SkipList {
1621 const Short arena; /* allocation arena */
1622 const UShort size; /* structure size (not including SkipNode) */
1623 const UShort keyoff; /* key offset */
1624 const SkipCmp_t cmp; /* compare two keys */
1625 Char * (*strkey)(void *); /* stringify a key (for debugging) */
1626 SkipNode *head; /* list head */
1627};
1628
1629/* Use this macro to initialize your skiplist head. The arguments are pretty self explanitory:
1630 _type is the type of your element structure
1631 _key is the field within that type which you want to use as the key
1632 _cmp is the comparison function for keys - it gets two typeof(_key) pointers as args
1633 _strkey is a function which can return a string of your key - it's only used for debugging
1634 _arena is the arena to use for allocation - -1 is the default
1635 */
1636#define SKIPLIST_INIT(_type, _key, _cmp, _strkey, _arena) \
1637 { \
1638 .arena = _arena, \
1639 .size = sizeof(_type), \
1640 .keyoff = offsetof(_type, _key), \
1641 .cmp = _cmp, \
1642 .strkey = _strkey, \
1643 .head = NULL, \
1644 }
1645
1646/* List operations:
1647 SkipList_Find searchs a list. If it can't find an exact match, it either returns NULL
1648 or a pointer to the element before where k would go
1649 SkipList_Insert inserts a new element into the list. Duplicates are forbidden.
1650 SkipList_Remove removes an element from the list and returns it. It doesn't free the memory.
1651 */
1652extern void *VG_(SkipList_Find) (const SkipList *l, void *key);
1653extern void VG_(SkipList_Insert)( SkipList *l, void *data);
1654extern void *VG_(SkipList_Remove)( SkipList *l, void *key);
1655
1656/* Node (element) operations:
1657 SkipNode_Alloc: allocate memory for a new element on the list
1658 SkipNode_Free: free memory allocated above
1659 SkipNode_First: return the first element on the list
1660 SkipNode_Next: return the next element after "data" on the list -
1661 NULL for none
1662 */
1663extern void *VG_(SkipNode_Alloc) (const SkipList *l);
1664extern void VG_(SkipNode_Free) (const SkipList *l, void *p);
1665extern void *VG_(SkipNode_First) (const SkipList *l);
1666extern void *VG_(SkipNode_Next) (const SkipList *l, void *data);
1667
1668/*====================================================================*/
njnd3040452003-05-19 15:04:06 +00001669/*=== Functions for shadow registers ===*/
1670/*====================================================================*/
1671
1672/* Nb: make sure the shadow_regs 'need' is set before using these! */
1673
1674/* This one lets you override the shadow of the return value register for a
1675 syscall. Call it from SK_(post_syscall)() (not SK_(pre_syscall)()!) to
1676 override the default shadow register value. */
daywalker5d945de2003-09-26 00:32:53 +00001677extern void VG_(set_return_from_syscall_shadow) ( ThreadId tid,
njnd3040452003-05-19 15:04:06 +00001678 UInt ret_shadow );
1679
1680/* This can be called from SK_(fini)() to find the shadow of the argument
1681 to exit(), ie. the shadow of the program's return value. */
1682extern UInt VG_(get_exit_status_shadow) ( void );
1683
1684
1685/*====================================================================*/
njn3e884182003-04-15 13:03:23 +00001686/*=== General stuff for replacing functions ===*/
1687/*====================================================================*/
njn25e49d8e72002-09-23 09:36:25 +00001688
njn3e884182003-04-15 13:03:23 +00001689/* Some skins need to replace the standard definitions of some functions. */
njn25e49d8e72002-09-23 09:36:25 +00001690
njn3e884182003-04-15 13:03:23 +00001691/* ------------------------------------------------------------------ */
1692/* General stuff, for replacing any functions */
njn25e49d8e72002-09-23 09:36:25 +00001693
daywalker5d945de2003-09-26 00:32:53 +00001694/* Is the client running on the simulated CPU or the real one?
njn25e49d8e72002-09-23 09:36:25 +00001695
njn3e884182003-04-15 13:03:23 +00001696 Nb: If it is, and you want to call a function to be run on the real CPU,
njn057c65f2003-04-21 13:30:55 +00001697 use one of the VALGRIND_NON_SIMD_CALL[123] macros in valgrind.h to call it.
njn25e49d8e72002-09-23 09:36:25 +00001698
daywalker5d945de2003-09-26 00:32:53 +00001699 Nb: don't forget the function parentheses when using this in a
njn3e884182003-04-15 13:03:23 +00001700 condition... write this:
1701
1702 if (VG_(is_running_on_simd_CPU)()) { ... } // calls function
1703
1704 not this:
daywalker5d945de2003-09-26 00:32:53 +00001705
njn3e884182003-04-15 13:03:23 +00001706 if (VG_(is_running_on_simd_CPU)) { ... } // address of var!
1707*/
daywalker5d945de2003-09-26 00:32:53 +00001708extern Bool VG_(is_running_on_simd_CPU) ( void );
njn3e884182003-04-15 13:03:23 +00001709
1710
1711/*====================================================================*/
1712/*=== Specific stuff for replacing malloc() and friends ===*/
1713/*====================================================================*/
1714
fitzhardinge98abfc72003-12-16 02:05:15 +00001715/* If a skin replaces malloc() et al, the easiest way to do so is to
1716 link with vg_replace_malloc.o into its vgpreload_*.so file, and
1717 follow the following instructions. You can do it from scratch,
1718 though, if you enjoy that sort of thing. */
njn3e884182003-04-15 13:03:23 +00001719
1720/* Arena size for valgrind's own malloc(); default value is 0, but can
1721 be overridden by skin -- but must be done so *statically*, eg:
daywalker5d945de2003-09-26 00:32:53 +00001722
njn3e884182003-04-15 13:03:23 +00001723 Int VG_(vg_malloc_redzone_szB) = 4;
daywalker5d945de2003-09-26 00:32:53 +00001724
njn3e884182003-04-15 13:03:23 +00001725 It can't be done from a function like SK_(pre_clo_init)(). So it can't,
1726 for example, be controlled with a command line option, unfortunately. */
1727extern UInt VG_(vg_malloc_redzone_szB);
1728
njn3e884182003-04-15 13:03:23 +00001729/* Can be called from SK_(malloc) et al to do the actual alloc/freeing. */
daywalker5d945de2003-09-26 00:32:53 +00001730extern void* VG_(cli_malloc) ( UInt align, Int nbytes );
njn3e884182003-04-15 13:03:23 +00001731extern void VG_(cli_free) ( void* p );
1732
1733/* Check if an address is within a range, allowing for redzones at edges */
1734extern Bool VG_(addr_is_in_block)( Addr a, Addr start, UInt size );
1735
1736/* ------------------------------------------------------------------ */
daywalker5d945de2003-09-26 00:32:53 +00001737/* Some options that can be used by a skin if malloc() et al are replaced.
njnd3040452003-05-19 15:04:06 +00001738 The skin should call the functions in the appropriate places to give
1739 control over these aspects of Valgrind's version of malloc(). */
njn3e884182003-04-15 13:03:23 +00001740
1741/* Round malloc sizes upwards to integral number of words? default: NO */
1742extern Bool VG_(clo_sloppy_malloc);
1743/* DEBUG: print malloc details? default: NO */
1744extern Bool VG_(clo_trace_malloc);
1745/* Minimum alignment in functions that don't specify alignment explicitly.
1746 default: 0, i.e. use default of the machine (== 4) */
1747extern Int VG_(clo_alignment);
1748
1749extern Bool VG_(replacement_malloc_process_cmd_line_option) ( Char* arg );
1750extern void VG_(replacement_malloc_print_usage) ( void );
1751extern void VG_(replacement_malloc_print_debug_usage) ( void );
sewardja4495682002-10-21 07:29:59 +00001752
1753
njn25e49d8e72002-09-23 09:36:25 +00001754/*====================================================================*/
1755/*=== Skin-specific stuff ===*/
1756/*====================================================================*/
1757
njnd04b7c62002-10-03 14:05:52 +00001758/* ------------------------------------------------------------------ */
1759/* Details */
njnd04b7c62002-10-03 14:05:52 +00001760
njn120281f2003-02-03 12:20:07 +00001761/* Default value for avg_translations_sizeB (in bytes), indicating typical
1762 code expansion of about 6:1. */
1763#define VG_DEFAULT_TRANS_SIZEB 100
1764
njn810086f2002-11-14 12:42:47 +00001765/* Information used in the startup message. `name' also determines the
1766 string used for identifying suppressions in a suppression file as
1767 belonging to this skin. `version' can be NULL, in which case (not
1768 surprisingly) no version info is printed; this mechanism is designed for
1769 skins distributed with Valgrind that share a version number with
1770 Valgrind. Other skins not distributed as part of Valgrind should
sewardjc0d8f682002-11-30 00:49:43 +00001771 probably have their own version number. */
1772extern void VG_(details_name) ( Char* name );
1773extern void VG_(details_version) ( Char* version );
1774extern void VG_(details_description) ( Char* description );
1775extern void VG_(details_copyright_author) ( Char* copyright_author );
1776
1777/* Average size of a translation, in bytes, so that the translation
njn120281f2003-02-03 12:20:07 +00001778 storage machinery can allocate memory appropriately. Not critical,
daywalker5d945de2003-09-26 00:32:53 +00001779 setting is optional. */
njn120281f2003-02-03 12:20:07 +00001780extern void VG_(details_avg_translation_sizeB) ( UInt size );
njnd04b7c62002-10-03 14:05:52 +00001781
njn810086f2002-11-14 12:42:47 +00001782/* String printed if an `sk_assert' assertion fails or VG_(skin_panic)
1783 is called. Should probably be an email address. */
1784extern void VG_(details_bug_reports_to) ( Char* bug_reports_to );
njnd04b7c62002-10-03 14:05:52 +00001785
1786/* ------------------------------------------------------------------ */
1787/* Needs */
1788
njn810086f2002-11-14 12:42:47 +00001789/* Booleans that decide core behaviour, but don't require extra
1790 operations to be defined if `True' */
njnd5bb0a52002-09-27 10:24:48 +00001791
njn810086f2002-11-14 12:42:47 +00001792/* Should __libc_freeres() be run? Bugs in it can crash the skin. */
1793extern void VG_(needs_libc_freeres) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001794
njn810086f2002-11-14 12:42:47 +00001795/* Want to have errors detected by Valgrind's core reported? Includes:
1796 - pthread API errors (many; eg. unlocking a non-locked mutex)
njn810086f2002-11-14 12:42:47 +00001797 - invalid file descriptors to blocking syscalls read() and write()
1798 - bad signal numbers passed to sigaction()
daywalker5d945de2003-09-26 00:32:53 +00001799 - attempt to install signal handler for SIGKILL or SIGSTOP */
njn810086f2002-11-14 12:42:47 +00001800extern void VG_(needs_core_errors) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001801
njn810086f2002-11-14 12:42:47 +00001802/* Booleans that indicate extra operations are defined; if these are True,
1803 the corresponding template functions (given below) must be defined. A
1804 lot like being a member of a type class. */
njn25e49d8e72002-09-23 09:36:25 +00001805
njn810086f2002-11-14 12:42:47 +00001806/* Want to report errors from skin? This implies use of suppressions, too. */
1807extern void VG_(needs_skin_errors) ( void );
njnd5bb0a52002-09-27 10:24:48 +00001808
njn810086f2002-11-14 12:42:47 +00001809/* Is information kept about specific individual basic blocks? (Eg. for
1810 cachegrind there are cost-centres for every instruction, stored at a
1811 basic block level.) If so, it sometimes has to be discarded, because
1812 .so mmap/munmap-ping or self-modifying code (informed by the
1813 DISCARD_TRANSLATIONS user request) can cause one instruction address
1814 to be used for more than one instruction in one program run... */
1815extern void VG_(needs_basic_block_discards) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001816
njn810086f2002-11-14 12:42:47 +00001817/* Skin maintains information about each register? */
1818extern void VG_(needs_shadow_regs) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001819
njn810086f2002-11-14 12:42:47 +00001820/* Skin defines its own command line options? */
1821extern void VG_(needs_command_line_options) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001822
njn810086f2002-11-14 12:42:47 +00001823/* Skin defines its own client requests? */
1824extern void VG_(needs_client_requests) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001825
njn810086f2002-11-14 12:42:47 +00001826/* Skin defines its own UInstrs? */
1827extern void VG_(needs_extended_UCode) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001828
njn810086f2002-11-14 12:42:47 +00001829/* Skin does stuff before and/or after system calls? */
1830extern void VG_(needs_syscall_wrapper) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001831
njn810086f2002-11-14 12:42:47 +00001832/* Are skin-state sanity checks performed? */
1833extern void VG_(needs_sanity_checks) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001834
njn810086f2002-11-14 12:42:47 +00001835/* Do we need to see data symbols? */
1836extern void VG_(needs_data_syms) ( void );
njn25e49d8e72002-09-23 09:36:25 +00001837
fitzhardinge98abfc72003-12-16 02:05:15 +00001838/* Does the skin need shadow memory allocated (if you set this, you must also statically initialize
1839 float SK_(shadow_ratio) = n./m;
1840 to define how many shadow bits you need per client address space bit.
1841*/
1842extern void VG_(needs_shadow_memory)( void );
1843extern float SK_(shadow_ratio);
1844
njn25e49d8e72002-09-23 09:36:25 +00001845/* ------------------------------------------------------------------ */
1846/* Core events to track */
1847
1848/* Part of the core from which this call was made. Useful for determining
njnd5bb0a52002-09-27 10:24:48 +00001849 what kind of error message should be emitted. */
daywalker5d945de2003-09-26 00:32:53 +00001850typedef
njn25e49d8e72002-09-23 09:36:25 +00001851 enum { Vg_CorePThread, Vg_CoreSignal, Vg_CoreSysCall, Vg_CoreTranslate }
1852 CorePart;
1853
njn4ba5a792002-09-30 10:23:54 +00001854/* Useful to use in VG_(get_Xreg_usage)() */
njn810086f2002-11-14 12:42:47 +00001855#define VG_UINSTR_READS_REG(ono,regs,isWrites) \
njn25e49d8e72002-09-23 09:36:25 +00001856 { if (mycat(u->tag,ono) == tag) \
njn810086f2002-11-14 12:42:47 +00001857 { regs[n] = mycat(u->val,ono); \
1858 isWrites[n] = False; \
njn25e49d8e72002-09-23 09:36:25 +00001859 n++; \
1860 } \
1861 }
njn810086f2002-11-14 12:42:47 +00001862#define VG_UINSTR_WRITES_REG(ono,regs,isWrites) \
njnd5bb0a52002-09-27 10:24:48 +00001863 { if (mycat(u->tag,ono) == tag) \
njn810086f2002-11-14 12:42:47 +00001864 { regs[n] = mycat(u->val,ono); \
1865 isWrites[n] = True; \
njnd5bb0a52002-09-27 10:24:48 +00001866 n++; \
1867 } \
njn25e49d8e72002-09-23 09:36:25 +00001868 }
1869
fitzhardinge98abfc72003-12-16 02:05:15 +00001870#endif /* NDEF __VG_SKIN_H */
njn25e49d8e72002-09-23 09:36:25 +00001871
fitzhardinge98abfc72003-12-16 02:05:15 +00001872/* gen_toolint.pl will put the VG_(init_*)() functions here: */
1873/* Generated by "gen_toolint.pl toolproto" */
njn25e49d8e72002-09-23 09:36:25 +00001874
fitzhardinge98abfc72003-12-16 02:05:15 +00001875/* These are the parameterised functions in the core. The default definitions
1876 are overridden by LD_PRELOADed skin version. At the very least, a skin
1877 must define the fundamental template functions. Depending on what needs
1878 are set, extra template functions will be used too. Functions are
1879 grouped under the needs that govern their use.
njn25e49d8e72002-09-23 09:36:25 +00001880
fitzhardinge98abfc72003-12-16 02:05:15 +00001881 ------------------------------------------------------------------
1882 Fundamental template functions
1883
1884 Do initialisation that can only be done after command line processing.
daywalker5d945de2003-09-26 00:32:53 +00001885 */
fitzhardinge98abfc72003-12-16 02:05:15 +00001886void SK_(post_clo_init)(void);
1887
1888/* Instrument a basic block. Must be a true function, ie. the same input
1889 always results in the same output, because basic blocks can be
1890 retranslated. Unless you're doing something really strange...
1891 'orig_addr' is the address of the first instruction in the block.
1892 */
1893UCodeBlock* SK_(instrument)(UCodeBlock* cb, Addr orig_addr);
1894
1895/* Finish up, print out any results, etc. `exitcode' is program's exit
1896 code. The shadow (if the `shadow_regs' need is set) can be found with
1897 VG_(get_shadow_archreg)(R_EBX), since %ebx holds the argument to the
1898 exit() syscall.
1899 */
1900void SK_(fini)(Int exitcode);
1901
1902
1903/* ------------------------------------------------------------------
1904 VG_(needs).core_errors
1905
1906 (none needed)
1907
1908 ------------------------------------------------------------------
1909 VG_(needs).skin_errors
1910
1911 Identify if two errors are equal, or equal enough. `res' indicates how
1912 close is "close enough". `res' should be passed on as necessary, eg. if
1913 the Error's `extra' part contains an ExeContext, `res' should be
1914 passed to VG_(eq_ExeContext)() if the ExeContexts are considered. Other
1915 than that, probably don't worry about it unless you have lots of very
1916 similar errors occurring.
1917 */
1918Bool SK_(eq_SkinError)(VgRes res, Error* e1, Error* e2);
1919
1920/* Print error context. */
1921void SK_(pp_SkinError)(Error* err);
1922
1923/* Should fill in any details that could be postponed until after the
1924 decision whether to ignore the error (ie. details not affecting the
1925 result of SK_(eq_SkinError)()). This saves time when errors are ignored.
1926 Yuk.
1927
1928 Return value: must be the size of the `extra' part in bytes -- used by
1929 the core to make a copy.
1930 */
1931UInt SK_(update_extra)(Error* err);
1932
1933/* Return value indicates recognition. If recognised, must set skind using
1934 VG_(set_supp_kind)().
1935 */
1936Bool SK_(recognised_suppression)(Char* name, Supp* su);
1937
1938/* Read any extra info for this suppression kind. Most likely for filling
1939 in the `extra' and `string' parts (with VG_(set_supp_{extra, string})())
1940 of a suppression if necessary. Should return False if a syntax error
1941 occurred, True otherwise.
1942 */
1943Bool SK_(read_extra_suppression_info)(Int fd, Char* buf, Int nBuf, Supp* su);
1944
1945/* This should just check the kinds match and maybe some stuff in the
1946 `string' and `extra' field if appropriate (using VG_(get_supp_*)() to
1947 get the relevant suppression parts).
1948 */
1949Bool SK_(error_matches_suppression)(Error* err, Supp* su);
1950
1951/* This should return the suppression name, for --gen-suppressions, or NULL
1952 if that error type cannot be suppressed. This is the inverse of
1953 SK_(recognised_suppression)().
1954 */
1955Char* SK_(get_error_name)(Error* err);
1956
1957/* This should print any extra info for the error, for --gen-suppressions,
1958 including the newline. This is the inverse of
1959 SK_(read_extra_suppression_info)().
1960 */
1961void SK_(print_extra_suppression_info)(Error* err);
1962
1963
1964/* ------------------------------------------------------------------
1965 VG_(needs).basic_block_discards
1966
1967 Should discard any information that pertains to specific basic blocks
1968 or instructions within the address range given.
1969 */
1970void SK_(discard_basic_block_info)(Addr a, UInt size);
1971
1972
1973/* ------------------------------------------------------------------
1974 VG_(needs).shadow_regs
1975
1976 No functions must be defined, but the post_reg[s]_write_* events should
1977 be tracked.
1978
1979 ------------------------------------------------------------------
1980 VG_(needs).command_line_options
1981
1982 Return True if option was recognised. Presumably sets some state to
1983 record the option as well.
1984 */
1985Bool SK_(process_cmd_line_option)(Char* argv);
1986
1987/* Print out command line usage for options for normal skin operation. */
1988void SK_(print_usage)(void);
1989
1990/* Print out command line usage for options for debugging the skin. */
1991void SK_(print_debug_usage)(void);
1992
1993/* ------------------------------------------------------------------
1994 VG_(needs).client_requests
1995
1996 If using client requests, the number of the first request should be equal
1997 to VG_USERREQ_SKIN_BASE('X', 'Y'), where 'X' and 'Y' form a suitable two
1998 character identification for the string. The second and subsequent
1999 requests should follow.
2000
2001 This function should use the VG_IS_SKIN_USERREQ macro (in
2002 include/valgrind.h) to first check if it's a request for this skin. Then
2003 should handle it if it's recognised (and return True), or return False if
2004 not recognised. arg_block[0] holds the request number, any further args
2005 from the request are in arg_block[1..]. 'ret' is for the return value...
2006 it should probably be filled, if only with 0.
2007 */
2008Bool SK_(handle_client_request)(ThreadId tid, UInt* arg_block, UInt* ret);
2009
2010
2011/* ------------------------------------------------------------------
2012 VG_(needs).extends_UCode
2013
2014 'X' prefix indicates eXtended UCode.
2015 */
2016Int SK_(get_Xreg_usage)(UInstr* u, Tag tag, Int* regs, Bool* isWrites);
2017void SK_(emit_XUInstr)(UInstr* u, RRegSet regs_live_before);
2018Bool SK_(sane_XUInstr)(Bool beforeRA, Bool beforeLiveness, UInstr* u);
2019Char * SK_(name_XUOpcode)(Opcode opc);
2020void SK_(pp_XUInstr)(UInstr* u);
2021
2022
2023/* ------------------------------------------------------------------
2024 VG_(needs).syscall_wrapper
2025
2026 If either of the pre_ functions malloc() something to return, the
2027 corresponding post_ function had better free() it!
2028
2029 */
2030void * SK_(pre_syscall)(ThreadId tid, UInt syscallno, Bool is_blocking);
2031void SK_(post_syscall)(ThreadId tid, UInt syscallno, void* pre_result, Int res, Bool is_blocking);
njn25e49d8e72002-09-23 09:36:25 +00002032
njnd5bb0a52002-09-27 10:24:48 +00002033
njn25e49d8e72002-09-23 09:36:25 +00002034/* ---------------------------------------------------------------------
fitzhardinge98abfc72003-12-16 02:05:15 +00002035 VG_(needs).sanity_checks
njn25e49d8e72002-09-23 09:36:25 +00002036
fitzhardinge98abfc72003-12-16 02:05:15 +00002037 Can be useful for ensuring a skin's correctness. SK_(cheap_sanity_check)
njnd5bb0a52002-09-27 10:24:48 +00002038 is called very frequently; SK_(expensive_sanity_check) is called less
fitzhardinge98abfc72003-12-16 02:05:15 +00002039 frequently and can be more involved.
2040 */
2041Bool SK_(cheap_sanity_check)(void);
2042Bool SK_(expensive_sanity_check)(void);
njn25e49d8e72002-09-23 09:36:25 +00002043
2044
fitzhardinge98abfc72003-12-16 02:05:15 +00002045/* ================================================================================
2046 Event tracking functions
njn25e49d8e72002-09-23 09:36:25 +00002047
fitzhardinge98abfc72003-12-16 02:05:15 +00002048 Events happening in core to track. To be notified, pass a callback
2049 function to the appropriate function. To ignore an event, don't do
2050 anything (default is for events to be ignored).
njn25e49d8e72002-09-23 09:36:25 +00002051
fitzhardinge98abfc72003-12-16 02:05:15 +00002052 Note that most events aren't passed a ThreadId. To find out the ThreadId
2053 of the affected thread, use VG_(get_current_or_recent_tid)(). For the
2054 ones passed a ThreadId, use that instead, since
2055 VG_(get_current_or_recent_tid)() might not give the right ThreadId in
2056 that case.
2057
2058 Memory events (Nb: to track heap allocation/freeing, a skin must replace
2059 malloc() et al. See above how to do this.)
2060
2061 These ones occur at startup, upon some signals, and upon some syscalls
2062 */
2063void SK_(new_mem_startup)(Addr a, UInt len, Bool rr, Bool ww, Bool xx);
2064void SK_(new_mem_stack_signal)(Addr a, UInt len);
2065void SK_(new_mem_brk)(Addr a, UInt len);
2066void SK_(new_mem_mmap)(Addr a, UInt len, Bool rr, Bool ww, Bool xx);
2067
2068void SK_(copy_mem_remap)(Addr from, Addr to, UInt len);
2069void SK_(change_mem_mprotect)(Addr a, UInt len, Bool rr, Bool ww, Bool xx);
2070void SK_(die_mem_stack_signal)(Addr a, UInt len);
2071void SK_(die_mem_brk)(Addr a, UInt len);
2072void SK_(die_mem_munmap)(Addr a, UInt len);
2073
2074/* These ones are called when %esp changes. A skin could track these itself
2075 (except for ban_mem_stack) but it's much easier to use the core's help.
2076
2077 The specialised ones are called in preference to the general one, if they
2078 are defined. These functions are called a lot if they are used, so
2079 specialising can optimise things significantly. If any of the
2080 specialised cases are defined, the general case must be defined too.
2081
2082 Nb: they must all use the __attribute__((regparm(n))) attribute.
2083 */
2084void SK_(new_mem_stack_4)(Addr new_ESP);
2085void SK_(new_mem_stack_8)(Addr new_ESP);
2086void SK_(new_mem_stack_12)(Addr new_ESP);
2087void SK_(new_mem_stack_16)(Addr new_ESP);
2088void SK_(new_mem_stack_32)(Addr new_ESP);
2089void SK_(new_mem_stack)(Addr a, UInt len);
2090
2091void SK_(die_mem_stack_4)(Addr die_ESP);
2092void SK_(die_mem_stack_8)(Addr die_ESP);
2093void SK_(die_mem_stack_12)(Addr die_ESP);
2094void SK_(die_mem_stack_16)(Addr die_ESP);
2095void SK_(die_mem_stack_32)(Addr die_ESP);
2096void SK_(die_mem_stack)(Addr a, UInt len);
2097
2098/* Used for redzone at end of thread stacks */
2099void SK_(ban_mem_stack)(Addr a, UInt len);
2100
2101/* These ones occur around syscalls, signal handling, etc */
2102void SK_(pre_mem_read)(CorePart part, ThreadId tid, Char* s, Addr a, UInt size);
2103void SK_(pre_mem_read_asciiz)(CorePart part, ThreadId tid, Char* s, Addr a);
2104void SK_(pre_mem_write)(CorePart part, ThreadId tid, Char* s, Addr a, UInt size);
2105/* Not implemented yet -- have to add in lots of places, which is a
2106 pain. Won't bother unless/until there's a need.
2107 void (*post_mem_read) ( ThreadState* tst, Char* s, Addr a, UInt size );
2108 */
2109void SK_(post_mem_write)(Addr a, UInt size);
2110
2111
2112/* Register events -- if `shadow_regs' need is set, all should probably be
2113 used. Use VG_(set_thread_shadow_archreg)() to set the shadow of the
2114 changed register.
2115
2116 Use VG_(set_shadow_archreg)() to set the eight general purpose regs,
2117 and use VG_(set_shadow_eflags)() to set eflags.
2118 */
2119void SK_(post_regs_write_init)(void);
2120
2121/* Use VG_(set_thread_shadow_archreg)() to set the shadow regs for these
2122 events.
2123 */
2124void SK_(post_reg_write_syscall_return)(ThreadId tid, UInt reg);
2125void SK_(post_reg_write_deliver_signal)(ThreadId tid, UInt reg);
2126void SK_(post_reg_write_pthread_return)(ThreadId tid, UInt reg);
2127void SK_(post_reg_write_clientreq_return)(ThreadId tid, UInt reg);
2128/* This one is called for malloc() et al if they are replaced by a skin. */
2129void SK_(post_reg_write_clientcall_return)(ThreadId tid, UInt reg, Addr f);
2130
2131
2132/* Scheduler events (not exhaustive) */
2133void SK_(thread_run)(ThreadId tid);
2134
2135
2136/* Thread events (not exhaustive)
2137
2138 Called during thread create, before the new thread has run any
2139 instructions (or touched any memory).
2140 */
2141void SK_(post_thread_create)(ThreadId tid, ThreadId child);
2142void SK_(post_thread_join)(ThreadId joiner, ThreadId joinee);
2143
2144
2145/* Mutex events (not exhaustive)
2146 "void *mutex" is really a pthread_mutex *
2147
2148 Called before a thread can block while waiting for a mutex (called
2149 regardless of whether the thread will block or not).
2150 */
2151void SK_(pre_mutex_lock)(ThreadId tid, void* mutex);
2152/* Called once the thread actually holds the mutex (always paired with
2153 pre_mutex_lock).
2154 */
2155void SK_(post_mutex_lock)(ThreadId tid, void* mutex);
2156/* Called after a thread has released a mutex (no need for a corresponding
2157 pre_mutex_unlock, because unlocking can't block).
2158 */
2159void SK_(post_mutex_unlock)(ThreadId tid, void* mutex);
2160
2161/* Signal events (not exhaustive)
2162
2163 ... pre_send_signal, post_send_signal ...
2164
2165 Called before a signal is delivered; `alt_stack' indicates if it is
2166 delivered on an alternative stack.
2167 */
2168void SK_(pre_deliver_signal)(ThreadId tid, Int sigNo, Bool alt_stack);
2169/* Called after a signal is delivered. Nb: unfortunately, if the signal
2170 handler longjmps, this won't be called.
2171 */
2172void SK_(post_deliver_signal)(ThreadId tid, Int sigNo);
2173
2174
2175/* Others... condition variable...
2176 ...
2177
2178 Shadow memory management
2179 */
2180void SK_(init_shadow_page)(Addr p);
2181
2182/* ================================================================================
2183 malloc and friends
2184 */
2185void* SK_(malloc)(Int n);
2186void* SK_(__builtin_new)(Int n);
2187void* SK_(__builtin_vec_new)(Int n);
2188void* SK_(memalign)(Int align, Int n);
2189void* SK_(calloc)(Int nmemb, Int n);
2190void SK_(free)(void* p);
2191void SK_(__builtin_delete)(void* p);
2192void SK_(__builtin_vec_delete)(void* p);
2193void* SK_(realloc)(void* p, Int size);
2194/* Generated by "gen_toolint.pl initproto" */
2195
2196#ifndef VG_toolint_initproto
2197#define VG_toolint_initproto
2198
2199
2200/* These are the parameterised functions in the core. The default definitions
2201 are overridden by LD_PRELOADed skin version. At the very least, a skin
2202 must define the fundamental template functions. Depending on what needs
2203 are set, extra template functions will be used too. Functions are
2204 grouped under the needs that govern their use.
2205
2206 ------------------------------------------------------------------
2207 Fundamental template functions
2208
2209 Do initialisation that can only be done after command line processing.
2210 */
2211void VG_(init_post_clo_init)(void (*func)(void));
2212
2213/* Instrument a basic block. Must be a true function, ie. the same input
2214 always results in the same output, because basic blocks can be
2215 retranslated. Unless you're doing something really strange...
2216 'orig_addr' is the address of the first instruction in the block.
2217 */
2218void VG_(init_instrument)(UCodeBlock* (*func)(UCodeBlock* cb, Addr orig_addr));
2219
2220/* Finish up, print out any results, etc. `exitcode' is program's exit
2221 code. The shadow (if the `shadow_regs' need is set) can be found with
2222 VG_(get_shadow_archreg)(R_EBX), since %ebx holds the argument to the
2223 exit() syscall.
2224 */
2225void VG_(init_fini)(void (*func)(Int exitcode));
2226
2227
2228/* ------------------------------------------------------------------
2229 VG_(needs).core_errors
2230
2231 (none needed)
2232
2233 ------------------------------------------------------------------
2234 VG_(needs).skin_errors
2235
2236 Identify if two errors are equal, or equal enough. `res' indicates how
2237 close is "close enough". `res' should be passed on as necessary, eg. if
2238 the Error's `extra' part contains an ExeContext, `res' should be
2239 passed to VG_(eq_ExeContext)() if the ExeContexts are considered. Other
2240 than that, probably don't worry about it unless you have lots of very
2241 similar errors occurring.
2242 */
2243void VG_(init_eq_SkinError)(Bool (*func)(VgRes res, Error* e1, Error* e2));
2244
2245/* Print error context. */
2246void VG_(init_pp_SkinError)(void (*func)(Error* err));
2247
2248/* Should fill in any details that could be postponed until after the
2249 decision whether to ignore the error (ie. details not affecting the
2250 result of SK_(eq_SkinError)()). This saves time when errors are ignored.
2251 Yuk.
2252
2253 Return value: must be the size of the `extra' part in bytes -- used by
2254 the core to make a copy.
2255 */
2256void VG_(init_update_extra)(UInt (*func)(Error* err));
2257
2258/* Return value indicates recognition. If recognised, must set skind using
2259 VG_(set_supp_kind)().
2260 */
2261void VG_(init_recognised_suppression)(Bool (*func)(Char* name, Supp* su));
2262
2263/* Read any extra info for this suppression kind. Most likely for filling
2264 in the `extra' and `string' parts (with VG_(set_supp_{extra, string})())
2265 of a suppression if necessary. Should return False if a syntax error
2266 occurred, True otherwise.
2267 */
2268void VG_(init_read_extra_suppression_info)(Bool (*func)(Int fd, Char* buf, Int nBuf, Supp* su));
2269
2270/* This should just check the kinds match and maybe some stuff in the
2271 `string' and `extra' field if appropriate (using VG_(get_supp_*)() to
2272 get the relevant suppression parts).
2273 */
2274void VG_(init_error_matches_suppression)(Bool (*func)(Error* err, Supp* su));
2275
2276/* This should return the suppression name, for --gen-suppressions, or NULL
2277 if that error type cannot be suppressed. This is the inverse of
2278 SK_(recognised_suppression)().
2279 */
2280void VG_(init_get_error_name)(Char* (*func)(Error* err));
2281
2282/* This should print any extra info for the error, for --gen-suppressions,
2283 including the newline. This is the inverse of
2284 SK_(read_extra_suppression_info)().
2285 */
2286void VG_(init_print_extra_suppression_info)(void (*func)(Error* err));
2287
2288
2289/* ------------------------------------------------------------------
2290 VG_(needs).basic_block_discards
2291
2292 Should discard any information that pertains to specific basic blocks
2293 or instructions within the address range given.
2294 */
2295void VG_(init_discard_basic_block_info)(void (*func)(Addr a, UInt size));
2296
2297
2298/* ------------------------------------------------------------------
2299 VG_(needs).shadow_regs
2300
2301 No functions must be defined, but the post_reg[s]_write_* events should
2302 be tracked.
2303
2304 ------------------------------------------------------------------
2305 VG_(needs).command_line_options
2306
2307 Return True if option was recognised. Presumably sets some state to
2308 record the option as well.
2309 */
2310void VG_(init_process_cmd_line_option)(Bool (*func)(Char* argv));
2311
2312/* Print out command line usage for options for normal skin operation. */
2313void VG_(init_print_usage)(void (*func)(void));
2314
2315/* Print out command line usage for options for debugging the skin. */
2316void VG_(init_print_debug_usage)(void (*func)(void));
2317
2318/* ------------------------------------------------------------------
2319 VG_(needs).client_requests
2320
2321 If using client requests, the number of the first request should be equal
2322 to VG_USERREQ_SKIN_BASE('X', 'Y'), where 'X' and 'Y' form a suitable two
2323 character identification for the string. The second and subsequent
2324 requests should follow.
2325
2326 This function should use the VG_IS_SKIN_USERREQ macro (in
2327 include/valgrind.h) to first check if it's a request for this skin. Then
2328 should handle it if it's recognised (and return True), or return False if
2329 not recognised. arg_block[0] holds the request number, any further args
2330 from the request are in arg_block[1..]. 'ret' is for the return value...
2331 it should probably be filled, if only with 0.
2332 */
2333void VG_(init_handle_client_request)(Bool (*func)(ThreadId tid, UInt* arg_block, UInt* ret));
2334
2335
2336/* ------------------------------------------------------------------
2337 VG_(needs).extends_UCode
2338
2339 'X' prefix indicates eXtended UCode.
2340 */
2341void VG_(init_get_Xreg_usage)(Int (*func)(UInstr* u, Tag tag, Int* regs, Bool* isWrites));
2342void VG_(init_emit_XUInstr)(void (*func)(UInstr* u, RRegSet regs_live_before));
2343void VG_(init_sane_XUInstr)(Bool (*func)(Bool beforeRA, Bool beforeLiveness, UInstr* u));
2344void VG_(init_name_XUOpcode)(Char * (*func)(Opcode opc));
2345void VG_(init_pp_XUInstr)(void (*func)(UInstr* u));
2346
2347
2348/* ------------------------------------------------------------------
2349 VG_(needs).syscall_wrapper
2350
2351 If either of the pre_ functions malloc() something to return, the
2352 corresponding post_ function had better free() it!
2353
2354 */
2355void VG_(init_pre_syscall)(void * (*func)(ThreadId tid, UInt syscallno, Bool is_blocking));
2356void VG_(init_post_syscall)(void (*func)(ThreadId tid, UInt syscallno, void* pre_result, Int res, Bool is_blocking));
2357
2358
2359/* ---------------------------------------------------------------------
2360 VG_(needs).sanity_checks
2361
2362 Can be useful for ensuring a skin's correctness. SK_(cheap_sanity_check)
2363 is called very frequently; SK_(expensive_sanity_check) is called less
2364 frequently and can be more involved.
2365 */
2366void VG_(init_cheap_sanity_check)(Bool (*func)(void));
2367void VG_(init_expensive_sanity_check)(Bool (*func)(void));
2368
2369
2370/* ================================================================================
2371 Event tracking functions
2372
2373 Events happening in core to track. To be notified, pass a callback
2374 function to the appropriate function. To ignore an event, don't do
2375 anything (default is for events to be ignored).
2376
2377 Note that most events aren't passed a ThreadId. To find out the ThreadId
2378 of the affected thread, use VG_(get_current_or_recent_tid)(). For the
2379 ones passed a ThreadId, use that instead, since
2380 VG_(get_current_or_recent_tid)() might not give the right ThreadId in
2381 that case.
2382
2383 Memory events (Nb: to track heap allocation/freeing, a skin must replace
2384 malloc() et al. See above how to do this.)
2385
2386 These ones occur at startup, upon some signals, and upon some syscalls
2387 */
2388void VG_(init_new_mem_startup)(void (*func)(Addr a, UInt len, Bool rr, Bool ww, Bool xx));
2389void VG_(init_new_mem_stack_signal)(void (*func)(Addr a, UInt len));
2390void VG_(init_new_mem_brk)(void (*func)(Addr a, UInt len));
2391void VG_(init_new_mem_mmap)(void (*func)(Addr a, UInt len, Bool rr, Bool ww, Bool xx));
2392
2393void VG_(init_copy_mem_remap)(void (*func)(Addr from, Addr to, UInt len));
2394void VG_(init_change_mem_mprotect)(void (*func)(Addr a, UInt len, Bool rr, Bool ww, Bool xx));
2395void VG_(init_die_mem_stack_signal)(void (*func)(Addr a, UInt len));
2396void VG_(init_die_mem_brk)(void (*func)(Addr a, UInt len));
2397void VG_(init_die_mem_munmap)(void (*func)(Addr a, UInt len));
2398
2399/* These ones are called when %esp changes. A skin could track these itself
2400 (except for ban_mem_stack) but it's much easier to use the core's help.
2401
2402 The specialised ones are called in preference to the general one, if they
2403 are defined. These functions are called a lot if they are used, so
2404 specialising can optimise things significantly. If any of the
2405 specialised cases are defined, the general case must be defined too.
2406
2407 Nb: they must all use the __attribute__((regparm(n))) attribute.
2408 */
2409void VG_(init_new_mem_stack_4)(void (*func)(Addr new_ESP));
2410void VG_(init_new_mem_stack_8)(void (*func)(Addr new_ESP));
2411void VG_(init_new_mem_stack_12)(void (*func)(Addr new_ESP));
2412void VG_(init_new_mem_stack_16)(void (*func)(Addr new_ESP));
2413void VG_(init_new_mem_stack_32)(void (*func)(Addr new_ESP));
2414void VG_(init_new_mem_stack)(void (*func)(Addr a, UInt len));
2415
2416void VG_(init_die_mem_stack_4)(void (*func)(Addr die_ESP));
2417void VG_(init_die_mem_stack_8)(void (*func)(Addr die_ESP));
2418void VG_(init_die_mem_stack_12)(void (*func)(Addr die_ESP));
2419void VG_(init_die_mem_stack_16)(void (*func)(Addr die_ESP));
2420void VG_(init_die_mem_stack_32)(void (*func)(Addr die_ESP));
2421void VG_(init_die_mem_stack)(void (*func)(Addr a, UInt len));
2422
2423/* Used for redzone at end of thread stacks */
2424void VG_(init_ban_mem_stack)(void (*func)(Addr a, UInt len));
2425
2426/* These ones occur around syscalls, signal handling, etc */
2427void VG_(init_pre_mem_read)(void (*func)(CorePart part, ThreadId tid, Char* s, Addr a, UInt size));
2428void VG_(init_pre_mem_read_asciiz)(void (*func)(CorePart part, ThreadId tid, Char* s, Addr a));
2429void VG_(init_pre_mem_write)(void (*func)(CorePart part, ThreadId tid, Char* s, Addr a, UInt size));
2430/* Not implemented yet -- have to add in lots of places, which is a
2431 pain. Won't bother unless/until there's a need.
2432 void (*post_mem_read) ( ThreadState* tst, Char* s, Addr a, UInt size );
2433 */
2434void VG_(init_post_mem_write)(void (*func)(Addr a, UInt size));
2435
2436
2437/* Register events -- if `shadow_regs' need is set, all should probably be
2438 used. Use VG_(set_thread_shadow_archreg)() to set the shadow of the
2439 changed register.
2440
2441 Use VG_(set_shadow_archreg)() to set the eight general purpose regs,
2442 and use VG_(set_shadow_eflags)() to set eflags.
2443 */
2444void VG_(init_post_regs_write_init)(void (*func)(void));
2445
2446/* Use VG_(set_thread_shadow_archreg)() to set the shadow regs for these
2447 events.
2448 */
2449void VG_(init_post_reg_write_syscall_return)(void (*func)(ThreadId tid, UInt reg));
2450void VG_(init_post_reg_write_deliver_signal)(void (*func)(ThreadId tid, UInt reg));
2451void VG_(init_post_reg_write_pthread_return)(void (*func)(ThreadId tid, UInt reg));
2452void VG_(init_post_reg_write_clientreq_return)(void (*func)(ThreadId tid, UInt reg));
2453/* This one is called for malloc() et al if they are replaced by a skin. */
2454void VG_(init_post_reg_write_clientcall_return)(void (*func)(ThreadId tid, UInt reg, Addr f));
2455
2456
2457/* Scheduler events (not exhaustive) */
2458void VG_(init_thread_run)(void (*func)(ThreadId tid));
2459
2460
2461/* Thread events (not exhaustive)
2462
2463 Called during thread create, before the new thread has run any
2464 instructions (or touched any memory).
2465 */
2466void VG_(init_post_thread_create)(void (*func)(ThreadId tid, ThreadId child));
2467void VG_(init_post_thread_join)(void (*func)(ThreadId joiner, ThreadId joinee));
2468
2469
2470/* Mutex events (not exhaustive)
2471 "void *mutex" is really a pthread_mutex *
2472
2473 Called before a thread can block while waiting for a mutex (called
2474 regardless of whether the thread will block or not).
2475 */
2476void VG_(init_pre_mutex_lock)(void (*func)(ThreadId tid, void* mutex));
2477/* Called once the thread actually holds the mutex (always paired with
2478 pre_mutex_lock).
2479 */
2480void VG_(init_post_mutex_lock)(void (*func)(ThreadId tid, void* mutex));
2481/* Called after a thread has released a mutex (no need for a corresponding
2482 pre_mutex_unlock, because unlocking can't block).
2483 */
2484void VG_(init_post_mutex_unlock)(void (*func)(ThreadId tid, void* mutex));
2485
2486/* Signal events (not exhaustive)
2487
2488 ... pre_send_signal, post_send_signal ...
2489
2490 Called before a signal is delivered; `alt_stack' indicates if it is
2491 delivered on an alternative stack.
2492 */
2493void VG_(init_pre_deliver_signal)(void (*func)(ThreadId tid, Int sigNo, Bool alt_stack));
2494/* Called after a signal is delivered. Nb: unfortunately, if the signal
2495 handler longjmps, this won't be called.
2496 */
2497void VG_(init_post_deliver_signal)(void (*func)(ThreadId tid, Int sigNo));
2498
2499
2500/* Others... condition variable...
2501 ...
2502
2503 Shadow memory management
2504 */
2505void VG_(init_init_shadow_page)(void (*func)(Addr p));
2506
2507/* ================================================================================
2508 malloc and friends
2509 */
2510void VG_(init_malloc)(void* (*func)(Int n));
2511void VG_(init___builtin_new)(void* (*func)(Int n));
2512void VG_(init___builtin_vec_new)(void* (*func)(Int n));
2513void VG_(init_memalign)(void* (*func)(Int align, Int n));
2514void VG_(init_calloc)(void* (*func)(Int nmemb, Int n));
2515void VG_(init_free)(void (*func)(void* p));
2516void VG_(init___builtin_delete)(void (*func)(void* p));
2517void VG_(init___builtin_vec_delete)(void (*func)(void* p));
2518void VG_(init_realloc)(void* (*func)(void* p, Int size));
2519
2520#endif /* VG_toolint_initproto */