fitzhardinge | c2dbbac | 2004-01-23 23:09:01 +0000 | [diff] [blame^] | 1 | /*-*- c -*- ----------------------------------------------------------*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2 | /*--- The only header your skin will ever need to #include... ---*/ |
| 3 | /*--- vg_skin.h ---*/ |
| 4 | /*--------------------------------------------------------------------*/ |
| 5 | |
| 6 | /* |
njn | c953984 | 2002-10-02 13:26:35 +0000 | [diff] [blame] | 7 | This file is part of Valgrind, an extensible x86 protected-mode |
| 8 | emulator for monitoring program execution on x86-Unixes. |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 9 | |
nethercote | bb1c991 | 2004-01-04 16:43:23 +0000 | [diff] [blame] | 10 | Copyright (C) 2000-2004 Julian Seward |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 11 | jseward@acm.org |
| 12 | |
| 13 | This program is free software; you can redistribute it and/or |
| 14 | modify it under the terms of the GNU General Public License as |
| 15 | published by the Free Software Foundation; either version 2 of the |
| 16 | License, or (at your option) any later version. |
| 17 | |
| 18 | This program is distributed in the hope that it will be useful, but |
| 19 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 21 | General Public License for more details. |
| 22 | |
| 23 | You should have received a copy of the GNU General Public License |
| 24 | along with this program; if not, write to the Free Software |
| 25 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 26 | 02111-1307, USA. |
| 27 | |
| 28 | The GNU General Public License is contained in the file COPYING. |
| 29 | */ |
| 30 | |
| 31 | #ifndef __VG_SKIN_H |
| 32 | #define __VG_SKIN_H |
| 33 | |
| 34 | #include <stdarg.h> /* ANSI varargs stuff */ |
| 35 | #include <setjmp.h> /* for jmp_buf */ |
| 36 | |
| 37 | #include "vg_constants_skin.h" |
| 38 | |
| 39 | |
nethercote | 421281e | 2003-11-20 16:20:55 +0000 | [diff] [blame] | 40 | /* --------------------------------------------------------------------- |
| 41 | Where to send bug reports to. |
| 42 | ------------------------------------------------------------------ */ |
| 43 | |
| 44 | #define VG_BUGS_TO "valgrind.kde.org" |
| 45 | |
| 46 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 47 | /*====================================================================*/ |
| 48 | /*=== Build options and table sizes. ===*/ |
| 49 | /*====================================================================*/ |
| 50 | |
daywalker | 5d945de | 2003-09-26 00:32:53 +0000 | [diff] [blame] | 51 | /* You should be able to change these options or sizes, recompile, and |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 52 | still have a working system. */ |
| 53 | |
| 54 | /* The maximum number of pthreads that we support. This is |
| 55 | deliberately not very high since our implementation of some of the |
| 56 | scheduler algorithms is surely O(N) in the number of threads, since |
| 57 | that's simple, at least. And (in practice) we hope that most |
| 58 | programs do not need many threads. */ |
sewardj | 989dad9 | 2003-07-06 01:52:32 +0000 | [diff] [blame] | 59 | #define VG_N_THREADS 100 |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 60 | |
| 61 | /* Maximum number of pthread keys available. Again, we start low until |
| 62 | the need for a higher number presents itself. */ |
| 63 | #define VG_N_THREAD_KEYS 50 |
| 64 | |
| 65 | /* Total number of integer registers available for allocation -- all of |
| 66 | them except %esp, %ebp. %ebp permanently points at VG_(baseBlock). |
daywalker | 5d945de | 2003-09-26 00:32:53 +0000 | [diff] [blame] | 67 | |
njn | 211b6ad | 2003-02-03 12:33:31 +0000 | [diff] [blame] | 68 | If you increase this you'll have to also change at least these: |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 69 | - VG_(rank_to_realreg)() |
| 70 | - VG_(realreg_to_rank)() |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 71 | - ppRegsLiveness() |
| 72 | - the RegsLive type (maybe -- RegsLive type must have more than |
| 73 | VG_MAX_REALREGS bits) |
njn | 211b6ad | 2003-02-03 12:33:31 +0000 | [diff] [blame] | 74 | |
| 75 | You can decrease it, and performance will drop because more spills will |
| 76 | occur. If you decrease it too much, everything will fall over. |
daywalker | 5d945de | 2003-09-26 00:32:53 +0000 | [diff] [blame] | 77 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 78 | Do not change this unless you really know what you are doing! */ |
| 79 | #define VG_MAX_REALREGS 6 |
| 80 | |
| 81 | |
| 82 | /*====================================================================*/ |
njn | 1a1dd8b | 2002-09-27 10:42:20 +0000 | [diff] [blame] | 83 | /*=== Basic types, useful macros ===*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 84 | /*====================================================================*/ |
| 85 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 86 | typedef unsigned char UChar; |
| 87 | typedef unsigned short UShort; |
| 88 | typedef unsigned int UInt; |
| 89 | typedef unsigned long long int ULong; |
| 90 | |
| 91 | typedef signed char Char; |
| 92 | typedef signed short Short; |
| 93 | typedef signed int Int; |
| 94 | typedef signed long long int Long; |
| 95 | |
| 96 | typedef unsigned int Addr; |
| 97 | |
| 98 | typedef unsigned char Bool; |
| 99 | #define False ((Bool)0) |
| 100 | #define True ((Bool)1) |
| 101 | |
| 102 | |
njn | 1a1dd8b | 2002-09-27 10:42:20 +0000 | [diff] [blame] | 103 | #define mycat_wrk(aaa,bbb) aaa##bbb |
| 104 | #define mycat(aaa,bbb) mycat_wrk(aaa,bbb) |
| 105 | |
| 106 | /* No, really. I _am_ that strange. */ |
| 107 | #define OINK(nnn) VG_(message)(Vg_DebugMsg, "OINK %d",nnn) |
| 108 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 109 | /* --------------------------------------------------------------------- |
| 110 | Now the basic types are set up, we can haul in the kernel-interface |
| 111 | definitions. |
| 112 | ------------------------------------------------------------------ */ |
| 113 | |
njn | 78adbf4 | 2003-07-24 19:35:00 +0000 | [diff] [blame] | 114 | #include "vg_kerneliface.h" |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 115 | |
| 116 | |
| 117 | /*====================================================================*/ |
njn | 27f1a38 | 2002-11-08 15:48:16 +0000 | [diff] [blame] | 118 | /*=== Core/skin interface version ===*/ |
| 119 | /*====================================================================*/ |
| 120 | |
| 121 | /* The major version number indicates binary-incompatible changes to the |
| 122 | interface; if the core and skin major versions don't match, Valgrind |
| 123 | will abort. The minor version indicates binary-compatible changes. |
njn | 27f1a38 | 2002-11-08 15:48:16 +0000 | [diff] [blame] | 124 | */ |
jseward | 157689b | 2003-11-05 23:59:21 +0000 | [diff] [blame] | 125 | #define VG_CORE_INTERFACE_MAJOR_VERSION 5 |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 126 | #define VG_CORE_INTERFACE_MINOR_VERSION 0 |
njn | 27f1a38 | 2002-11-08 15:48:16 +0000 | [diff] [blame] | 127 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 128 | typedef struct _ToolInfo { |
| 129 | Int sizeof_ToolInfo; |
| 130 | Int interface_major_version; |
| 131 | Int interface_minor_version; |
| 132 | |
| 133 | /* Initialise skin. Must do the following: |
| 134 | - initialise the `details' struct, via the VG_(details_*)() functions |
| 135 | - register any helpers called by generated code |
| 136 | |
| 137 | May do the following: |
| 138 | - initialise the `needs' struct to indicate certain requirements, via |
| 139 | the VG_(needs_*)() functions |
| 140 | - initialize all the tool's entrypoints via the VG_(init_*)() functions |
| 141 | - register any skin-specific profiling events |
| 142 | - any other skin-specific initialisation |
| 143 | */ |
| 144 | void (*sk_pre_clo_init) ( void ); |
| 145 | |
| 146 | /* Specifies how big the shadow segment should be as a ratio to the |
| 147 | client address space. 0 for no shadow segment. */ |
| 148 | float shadow_ratio; |
| 149 | } ToolInfo; |
njn | 27f1a38 | 2002-11-08 15:48:16 +0000 | [diff] [blame] | 150 | |
njn | 563f96f | 2003-02-03 11:17:46 +0000 | [diff] [blame] | 151 | /* Every skin must include this macro somewhere, exactly once. */ |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 152 | #define VG_DETERMINE_INTERFACE_VERSION(pre_clo_init, shadow) \ |
| 153 | const ToolInfo SK_(tool_info) = { \ |
| 154 | .sizeof_ToolInfo = sizeof(ToolInfo), \ |
| 155 | .interface_major_version = VG_CORE_INTERFACE_MAJOR_VERSION, \ |
| 156 | .interface_minor_version = VG_CORE_INTERFACE_MINOR_VERSION, \ |
| 157 | .sk_pre_clo_init = pre_clo_init, \ |
| 158 | .shadow_ratio = shadow, \ |
| 159 | }; |
njn | 211b6ad | 2003-02-03 12:33:31 +0000 | [diff] [blame] | 160 | |
njn | 27f1a38 | 2002-11-08 15:48:16 +0000 | [diff] [blame] | 161 | /*====================================================================*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 162 | /*=== Command-line options ===*/ |
| 163 | /*====================================================================*/ |
| 164 | |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 165 | /* Use this for normal null-termination-style string comparison */ |
| 166 | #define VG_STREQ(s1,s2) (s1 != NULL && s2 != NULL \ |
| 167 | && VG_(strcmp)((s1),(s2))==0) |
| 168 | |
| 169 | /* Use these for recognising skin command line options -- stops comparing |
| 170 | once whitespace is reached. */ |
| 171 | # define VG_CLO_STREQ(s1,s2) (0==VG_(strcmp_ws)((s1),(s2))) |
| 172 | # define VG_CLO_STREQN(nn,s1,s2) (0==VG_(strncmp_ws)((s1),(s2),(nn))) |
| 173 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 174 | /* Verbosity level: 0 = silent, 1 (default), > 1 = more verbose. */ |
| 175 | extern Int VG_(clo_verbosity); |
| 176 | |
| 177 | /* Profile? */ |
| 178 | extern Bool VG_(clo_profile); |
| 179 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 180 | /* Call this if a recognised option was bad for some reason. |
| 181 | Note: don't use it just because an option was unrecognised -- return 'False' |
| 182 | from SKN_(process_cmd_line_option) to indicate that. */ |
| 183 | extern void VG_(bad_option) ( Char* opt ); |
| 184 | |
| 185 | /* Client args */ |
| 186 | extern Int VG_(client_argc); |
| 187 | extern Char** VG_(client_argv); |
| 188 | |
njn | d5bb0a5 | 2002-09-27 10:24:48 +0000 | [diff] [blame] | 189 | /* Client environment. Can be inspected with VG_(getenv)() */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 190 | extern Char** VG_(client_envp); |
| 191 | |
| 192 | |
| 193 | /*====================================================================*/ |
| 194 | /*=== Printing messages for the user ===*/ |
| 195 | /*====================================================================*/ |
| 196 | |
| 197 | /* Print a message prefixed by "??<pid>?? "; '?' depends on the VgMsgKind. |
| 198 | Should be used for all user output. */ |
| 199 | |
| 200 | typedef |
| 201 | enum { Vg_UserMsg, /* '?' == '=' */ |
| 202 | Vg_DebugMsg, /* '?' == '-' */ |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 203 | Vg_DebugExtraMsg, /* '?' == '+' */ |
| 204 | Vg_ClientMsg, /* '?' == '*' */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 205 | } |
| 206 | VgMsgKind; |
| 207 | |
| 208 | /* Functions for building a message from multiple parts. */ |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 209 | extern int VG_(start_msg) ( VgMsgKind kind ); |
| 210 | extern int VG_(add_to_msg) ( Char* format, ... ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 211 | /* Ends and prints the message. Appends a newline. */ |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 212 | extern int VG_(end_msg) ( void ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 213 | |
njn | d5bb0a5 | 2002-09-27 10:24:48 +0000 | [diff] [blame] | 214 | /* Send a single-part message. Appends a newline. */ |
fitzhardinge | 39de4b4 | 2003-10-31 07:12:21 +0000 | [diff] [blame] | 215 | extern int VG_(message) ( VgMsgKind kind, Char* format, ... ); |
| 216 | extern int VG_(vmessage) ( VgMsgKind kind, Char* format, va_list vargs ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 217 | |
| 218 | |
| 219 | /*====================================================================*/ |
| 220 | /*=== Profiling ===*/ |
| 221 | /*====================================================================*/ |
| 222 | |
| 223 | /* Nb: VGP_(register_profile_event)() relies on VgpUnc being the first one */ |
| 224 | #define VGP_CORE_LIST \ |
| 225 | /* These ones depend on the core */ \ |
| 226 | VGP_PAIR(VgpUnc, "unclassified"), \ |
| 227 | VGP_PAIR(VgpRun, "running"), \ |
| 228 | VGP_PAIR(VgpSched, "scheduler"), \ |
| 229 | VGP_PAIR(VgpMalloc, "low-lev malloc/free"), \ |
| 230 | VGP_PAIR(VgpCliMalloc, "client malloc/free"), \ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 231 | VGP_PAIR(VgpTranslate, "translate-main"), \ |
| 232 | VGP_PAIR(VgpToUCode, "to-ucode"), \ |
| 233 | VGP_PAIR(VgpFromUcode, "from-ucode"), \ |
| 234 | VGP_PAIR(VgpImprove, "improve"), \ |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 235 | VGP_PAIR(VgpESPUpdate, "ESP-update"), \ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 236 | VGP_PAIR(VgpRegAlloc, "reg-alloc"), \ |
| 237 | VGP_PAIR(VgpLiveness, "liveness-analysis"), \ |
| 238 | VGP_PAIR(VgpDoLRU, "do-lru"), \ |
| 239 | VGP_PAIR(VgpSlowFindT, "slow-search-transtab"), \ |
| 240 | VGP_PAIR(VgpInitMem, "init-memory"), \ |
| 241 | VGP_PAIR(VgpExeContext, "exe-context"), \ |
| 242 | VGP_PAIR(VgpReadSyms, "read-syms"), \ |
| 243 | VGP_PAIR(VgpSearchSyms, "search-syms"), \ |
| 244 | VGP_PAIR(VgpAddToT, "add-to-transtab"), \ |
| 245 | VGP_PAIR(VgpCoreSysWrap, "core-syscall-wrapper"), \ |
| 246 | VGP_PAIR(VgpDemangle, "demangle"), \ |
njn | 37cea30 | 2002-09-30 11:24:00 +0000 | [diff] [blame] | 247 | VGP_PAIR(VgpCoreCheapSanity, "core-cheap-sanity"), \ |
| 248 | VGP_PAIR(VgpCoreExpensiveSanity, "core-expensive-sanity"), \ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 249 | /* These ones depend on the skin */ \ |
| 250 | VGP_PAIR(VgpPreCloInit, "pre-clo-init"), \ |
| 251 | VGP_PAIR(VgpPostCloInit, "post-clo-init"), \ |
| 252 | VGP_PAIR(VgpInstrument, "instrument"), \ |
| 253 | VGP_PAIR(VgpSkinSysWrap, "skin-syscall-wrapper"), \ |
njn | 37cea30 | 2002-09-30 11:24:00 +0000 | [diff] [blame] | 254 | VGP_PAIR(VgpSkinCheapSanity, "skin-cheap-sanity"), \ |
| 255 | VGP_PAIR(VgpSkinExpensiveSanity, "skin-expensive-sanity"), \ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 256 | VGP_PAIR(VgpFini, "fini") |
| 257 | |
| 258 | #define VGP_PAIR(n,name) n |
| 259 | typedef enum { VGP_CORE_LIST } VgpCoreCC; |
| 260 | #undef VGP_PAIR |
| 261 | |
| 262 | /* When registering skin profiling events, ensure that the 'n' value is in |
| 263 | * the range (VgpFini+1..) */ |
| 264 | extern void VGP_(register_profile_event) ( Int n, Char* name ); |
| 265 | |
| 266 | extern void VGP_(pushcc) ( UInt cc ); |
| 267 | extern void VGP_(popcc) ( UInt cc ); |
| 268 | |
| 269 | /* Define them only if they haven't already been defined by vg_profile.c */ |
| 270 | #ifndef VGP_PUSHCC |
| 271 | # define VGP_PUSHCC(x) |
| 272 | #endif |
| 273 | #ifndef VGP_POPCC |
| 274 | # define VGP_POPCC(x) |
| 275 | #endif |
| 276 | |
| 277 | |
| 278 | /*====================================================================*/ |
| 279 | /*=== Useful stuff to call from generated code ===*/ |
| 280 | /*====================================================================*/ |
| 281 | |
| 282 | /* ------------------------------------------------------------------ */ |
| 283 | /* General stuff */ |
| 284 | |
njn | 4155712 | 2002-10-14 09:25:37 +0000 | [diff] [blame] | 285 | /* 64-bit counter for the number of basic blocks done. */ |
| 286 | extern ULong VG_(bbs_done); |
| 287 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 288 | /* Get the simulated %esp */ |
| 289 | extern Addr VG_(get_stack_pointer) ( void ); |
| 290 | |
sewardj | ecf8e10 | 2003-07-12 12:11:39 +0000 | [diff] [blame] | 291 | /* Detect if an address is within Valgrind's stack, Valgrind's |
| 292 | m_state_static, or the VG_(threads) array. This is useful for |
| 293 | memory leak detectors to rule out spurious pointers to a block. */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 294 | extern Bool VG_(within_stack)(Addr a); |
sewardj | ecf8e10 | 2003-07-12 12:11:39 +0000 | [diff] [blame] | 295 | extern Bool VG_(within_m_state_static_OR_threads)(Addr a); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 296 | |
| 297 | /* Check if an address is 4-byte aligned */ |
| 298 | #define IS_ALIGNED4_ADDR(aaa_p) (0 == (((UInt)(aaa_p)) & 3)) |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 299 | #define IS_ALIGNED8_ADDR(aaa_p) (0 == (((UInt)(aaa_p)) & 7)) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 300 | |
| 301 | |
| 302 | /* ------------------------------------------------------------------ */ |
| 303 | /* Thread-related stuff */ |
| 304 | |
| 305 | /* Special magic value for an invalid ThreadId. It corresponds to |
| 306 | LinuxThreads using zero as the initial value for |
| 307 | pthread_mutex_t.__m_owner and pthread_cond_t.__c_waiting. */ |
| 308 | #define VG_INVALID_THREADID ((ThreadId)(0)) |
| 309 | |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 310 | /* ThreadIds are simply indices into the VG_(threads)[] array. */ |
daywalker | 5d945de | 2003-09-26 00:32:53 +0000 | [diff] [blame] | 311 | typedef |
| 312 | UInt |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 313 | ThreadId; |
| 314 | |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 315 | /* When looking for the current ThreadId, this is the safe option and |
| 316 | probably the one you want. |
daywalker | 5d945de | 2003-09-26 00:32:53 +0000 | [diff] [blame] | 317 | |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 318 | Details: Use this one from non-generated code, eg. from functions called |
| 319 | on events like 'new_mem_heap'. In such a case, the "current" thread is |
| 320 | temporarily suspended as Valgrind's dispatcher is running. This function |
| 321 | is also suitable to be called from generated code (ie. from UCode, or a C |
| 322 | function called directly from UCode). |
daywalker | 5d945de | 2003-09-26 00:32:53 +0000 | [diff] [blame] | 323 | |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 324 | If you use VG_(get_current_tid)() from non-generated code, it will return |
| 325 | 0 signifying the invalid thread, which is probably not what you want. */ |
| 326 | extern ThreadId VG_(get_current_or_recent_tid) ( void ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 327 | |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 328 | /* When looking for the current ThreadId, only use this one if you know what |
| 329 | you are doing. |
daywalker | 5d945de | 2003-09-26 00:32:53 +0000 | [diff] [blame] | 330 | |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 331 | Details: Use this one from generated code, eg. from C functions called |
| 332 | from UCode. (VG_(get_current_or_recent_tid)() is also suitable in that |
| 333 | case.) If you use this function from non-generated code, it will return |
| 334 | 0 signifying the invalid thread, which is probably not what you want. */ |
| 335 | extern ThreadId VG_(get_current_tid) ( void ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 336 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 337 | /* Searches through all thread's stacks to see if any match. Returns |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 338 | VG_INVALID_THREADID if none match. */ |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 339 | extern ThreadId VG_(first_matching_thread_stack) |
| 340 | ( Bool (*p) ( Addr stack_min, Addr stack_max )); |
| 341 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 342 | |
| 343 | /*====================================================================*/ |
| 344 | /*=== Valgrind's version of libc ===*/ |
| 345 | /*====================================================================*/ |
| 346 | |
| 347 | /* Valgrind doesn't use libc at all, for good reasons (trust us). So here |
| 348 | are its own versions of C library functions, but with VG_ prefixes. Note |
| 349 | that the types of some are slightly different to the real ones. Some |
njn | f4ce3d3 | 2003-02-10 10:17:26 +0000 | [diff] [blame] | 350 | additional useful functions are provided too; descriptions of how they |
| 351 | work are given below. */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 352 | |
| 353 | #if !defined(NULL) |
| 354 | # define NULL ((void*)0) |
| 355 | #endif |
| 356 | |
| 357 | |
| 358 | /* ------------------------------------------------------------------ */ |
| 359 | /* stdio.h |
| 360 | * |
| 361 | * Note that they all output to the file descriptor given by the |
| 362 | * --logfile-fd=N argument, which defaults to 2 (stderr). Hence no |
daywalker | 5d945de | 2003-09-26 00:32:53 +0000 | [diff] [blame] | 363 | * need for VG_(fprintf)(). |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 364 | */ |
sewardj | 78e3cd9 | 2002-10-22 04:45:48 +0000 | [diff] [blame] | 365 | extern UInt VG_(printf) ( const char *format, ... ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 366 | /* too noisy ... __attribute__ ((format (printf, 1, 2))) ; */ |
sewardj | 78e3cd9 | 2002-10-22 04:45:48 +0000 | [diff] [blame] | 367 | extern UInt VG_(sprintf) ( Char* buf, Char *format, ... ); |
daywalker | 5d945de | 2003-09-26 00:32:53 +0000 | [diff] [blame] | 368 | extern UInt VG_(vprintf) ( void(*send)(Char), |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 369 | const Char *format, va_list vargs ); |
| 370 | |
njn | 4155712 | 2002-10-14 09:25:37 +0000 | [diff] [blame] | 371 | extern Int VG_(rename) ( Char* old_name, Char* new_name ); |
| 372 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 373 | /* ------------------------------------------------------------------ */ |
| 374 | /* stdlib.h */ |
| 375 | |
| 376 | extern void* VG_(malloc) ( Int nbytes ); |
njn | d5bb0a5 | 2002-09-27 10:24:48 +0000 | [diff] [blame] | 377 | extern void VG_(free) ( void* p ); |
| 378 | extern void* VG_(calloc) ( Int n, Int nbytes ); |
| 379 | extern void* VG_(realloc) ( void* p, Int size ); |
| 380 | extern void* VG_(malloc_aligned) ( Int align_bytes, Int nbytes ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 381 | |
| 382 | extern void VG_(print_malloc_stats) ( void ); |
| 383 | |
| 384 | |
| 385 | extern void VG_(exit)( Int status ) |
| 386 | __attribute__ ((__noreturn__)); |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 387 | /* Prints a panic message (a constant string), appends newline and bug |
| 388 | reporting info, aborts. */ |
| 389 | __attribute__ ((__noreturn__)) |
| 390 | extern void VG_(skin_panic) ( Char* str ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 391 | |
njn | d5bb0a5 | 2002-09-27 10:24:48 +0000 | [diff] [blame] | 392 | /* Looks up VG_(client_envp) */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 393 | extern Char* VG_(getenv) ( Char* name ); |
| 394 | |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 395 | /* Get client resource limit*/ |
| 396 | extern Int VG_(getrlimit) ( Int resource, struct vki_rlimit *rlim ); |
| 397 | |
fitzhardinge | f0046f2 | 2003-12-18 02:39:22 +0000 | [diff] [blame] | 398 | /* Set client resource limit*/ |
| 399 | extern Int VG_(setrlimit) ( Int resource, struct vki_rlimit *rlim ); |
| 400 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 401 | /* Crude stand-in for the glibc system() call. */ |
| 402 | extern Int VG_(system) ( Char* cmd ); |
| 403 | |
njn | d5bb0a5 | 2002-09-27 10:24:48 +0000 | [diff] [blame] | 404 | extern Long VG_(atoll) ( Char* str ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 405 | |
rjwalsh | 8b42672 | 2004-01-04 23:15:22 +0000 | [diff] [blame] | 406 | /* Like atoll(), but converts a number of base 16 */ |
| 407 | extern Long VG_(atoll16) ( Char* str ); |
| 408 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 409 | /* Like atoll(), but converts a number of base 2..36 */ |
| 410 | extern Long VG_(atoll36) ( UInt base, Char* str ); |
| 411 | |
njn | e36543a | 2003-09-30 15:37:24 +0000 | [diff] [blame] | 412 | /* Like qsort(), but does shell-sort. The size==1/2/4 cases are specialised. */ |
njn | d3b0c5f | 2003-09-30 14:43:54 +0000 | [diff] [blame] | 413 | extern void VG_(ssort)( void* base, UInt nmemb, UInt size, |
| 414 | Int (*compar)(void*, void*) ); |
| 415 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 416 | |
| 417 | /* ------------------------------------------------------------------ */ |
njn | d5bb0a5 | 2002-09-27 10:24:48 +0000 | [diff] [blame] | 418 | /* ctype.h */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 419 | extern Bool VG_(isspace) ( Char c ); |
| 420 | extern Bool VG_(isdigit) ( Char c ); |
| 421 | extern Char VG_(toupper) ( Char c ); |
| 422 | |
| 423 | |
| 424 | /* ------------------------------------------------------------------ */ |
| 425 | /* string.h */ |
| 426 | extern Int VG_(strlen) ( const Char* str ); |
| 427 | extern Char* VG_(strcat) ( Char* dest, const Char* src ); |
| 428 | extern Char* VG_(strncat) ( Char* dest, const Char* src, Int n ); |
| 429 | extern Char* VG_(strpbrk) ( const Char* s, const Char* accept ); |
| 430 | extern Char* VG_(strcpy) ( Char* dest, const Char* src ); |
| 431 | extern Char* VG_(strncpy) ( Char* dest, const Char* src, Int ndest ); |
| 432 | extern Int VG_(strcmp) ( const Char* s1, const Char* s2 ); |
| 433 | extern Int VG_(strncmp) ( const Char* s1, const Char* s2, Int nmax ); |
| 434 | extern Char* VG_(strstr) ( const Char* haystack, Char* needle ); |
| 435 | extern Char* VG_(strchr) ( const Char* s, Char c ); |
| 436 | extern Char* VG_(strdup) ( const Char* s); |
sewardj | 7ab2aca | 2002-10-20 19:40:32 +0000 | [diff] [blame] | 437 | extern void* VG_(memcpy) ( void *d, const void *s, Int sz ); |
| 438 | extern void* VG_(memset) ( void *s, Int c, Int sz ); |
njn | 6d68e79 | 2003-02-24 10:49:08 +0000 | [diff] [blame] | 439 | extern Int VG_(memcmp) ( const void* s1, const void* s2, Int n ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 440 | |
njn | d5bb0a5 | 2002-09-27 10:24:48 +0000 | [diff] [blame] | 441 | /* Like strcmp() and strncmp(), but stop comparing at any whitespace. */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 442 | extern Int VG_(strcmp_ws) ( const Char* s1, const Char* s2 ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 443 | extern Int VG_(strncmp_ws) ( const Char* s1, const Char* s2, Int nmax ); |
| 444 | |
daywalker | 5d945de | 2003-09-26 00:32:53 +0000 | [diff] [blame] | 445 | /* Like strncpy(), but if 'src' is longer than 'ndest' inserts a '\0' as the |
njn | d5bb0a5 | 2002-09-27 10:24:48 +0000 | [diff] [blame] | 446 | last character. */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 447 | extern void VG_(strncpy_safely) ( Char* dest, const Char* src, Int ndest ); |
| 448 | |
| 449 | /* Mini-regexp function. Searches for 'pat' in 'str'. Supports |
| 450 | * meta-symbols '*' and '?'. '\' escapes meta-symbols. */ |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 451 | extern Bool VG_(string_match) ( const Char* pat, const Char* str ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 452 | |
| 453 | |
| 454 | /* ------------------------------------------------------------------ */ |
| 455 | /* math.h */ |
njn | d5bb0a5 | 2002-09-27 10:24:48 +0000 | [diff] [blame] | 456 | /* Returns the base-2 logarithm of x. */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 457 | extern Int VG_(log2) ( Int x ); |
| 458 | |
| 459 | |
| 460 | /* ------------------------------------------------------------------ */ |
njn | d5bb0a5 | 2002-09-27 10:24:48 +0000 | [diff] [blame] | 461 | /* unistd.h, fcntl.h, sys/stat.h */ |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 462 | extern Int VG_(getdents)( UInt fd, struct vki_dirent *dirp, UInt count ); |
| 463 | extern Int VG_(readlink)( Char* path, Char* buf, UInt bufsize ); |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 464 | extern Int VG_(getpid) ( void ); |
| 465 | extern Int VG_(getppid) ( void ); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 466 | extern Int VG_(getpgrp) ( void ); |
| 467 | extern Int VG_(gettid) ( void ); |
| 468 | extern Int VG_(setpgid) ( Int pid, Int pgrp ); |
njn | d5bb0a5 | 2002-09-27 10:24:48 +0000 | [diff] [blame] | 469 | |
njn | 4aca2d2 | 2002-10-04 10:29:38 +0000 | [diff] [blame] | 470 | extern Int VG_(open) ( const Char* pathname, Int flags, Int mode ); |
| 471 | extern Int VG_(read) ( Int fd, void* buf, Int count); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 472 | extern Int VG_(write) ( Int fd, const void* buf, Int count); |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 473 | extern Int VG_(lseek) ( Int fd, Long offset, Int whence); |
njn | 4aca2d2 | 2002-10-04 10:29:38 +0000 | [diff] [blame] | 474 | extern void VG_(close) ( Int fd ); |
njn | d5bb0a5 | 2002-09-27 10:24:48 +0000 | [diff] [blame] | 475 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 476 | extern Int VG_(pipe) ( Int fd[2] ); |
| 477 | |
njn | 4155712 | 2002-10-14 09:25:37 +0000 | [diff] [blame] | 478 | /* Nb: VG_(rename)() declared in stdio.h section above */ |
njn | 4aca2d2 | 2002-10-04 10:29:38 +0000 | [diff] [blame] | 479 | extern Int VG_(unlink) ( Char* file_name ); |
| 480 | extern Int VG_(stat) ( Char* file_name, struct vki_stat* buf ); |
njn | c9d4ba7 | 2003-10-15 10:34:03 +0000 | [diff] [blame] | 481 | extern Int VG_(fstat) ( Int fd, struct vki_stat* buf ); |
fitzhardinge | e1c06d8 | 2003-10-30 07:21:44 +0000 | [diff] [blame] | 482 | extern Int VG_(dup2) ( Int oldfd, Int newfd ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 483 | |
njn | 13f0293 | 2003-04-30 20:23:58 +0000 | [diff] [blame] | 484 | extern Char* VG_(getcwd) ( Char* buf, Int size ); |
| 485 | |
njn | 99ccf08 | 2003-09-30 13:51:23 +0000 | [diff] [blame] | 486 | /* Easier to use than VG_(getcwd)() -- does the buffer fiddling itself. |
| 487 | String put into 'cwd' is VG_(malloc)'d, and should be VG_(free)'d. |
| 488 | Returns False if it fails. Will fail if the pathname is > 65535 bytes. */ |
| 489 | extern Bool VG_(getcwd_alloc) ( Char** cwd ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 490 | |
| 491 | /* ------------------------------------------------------------------ */ |
| 492 | /* assert.h */ |
| 493 | /* Asserts permanently enabled -- no turning off with NDEBUG. Hurrah! */ |
| 494 | #define VG__STRING(__str) #__str |
| 495 | |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 496 | #define sk_assert(expr) \ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 497 | ((void) ((expr) ? 0 : \ |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 498 | (VG_(skin_assert_fail) (VG__STRING(expr), \ |
| 499 | __FILE__, __LINE__, \ |
| 500 | __PRETTY_FUNCTION__), 0))) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 501 | |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 502 | __attribute__ ((__noreturn__)) |
daywalker | 5d945de | 2003-09-26 00:32:53 +0000 | [diff] [blame] | 503 | extern void VG_(skin_assert_fail) ( const Char* expr, const Char* file, |
daywalker | df9ae42 | 2003-09-18 01:41:48 +0000 | [diff] [blame] | 504 | Int line, const Char* fn ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 505 | |
| 506 | |
| 507 | /* ------------------------------------------------------------------ */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 508 | /* Get memory by anonymous mmap. */ |
| 509 | extern void* VG_(get_memory_from_mmap) ( Int nBytes, Char* who ); |
| 510 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 511 | extern Bool VG_(is_client_addr) (Addr a); |
| 512 | extern Addr VG_(get_client_base)(void); |
| 513 | extern Addr VG_(get_client_end) (void); |
| 514 | extern Addr VG_(get_client_size)(void); |
| 515 | |
| 516 | extern Bool VG_(is_shadow_addr) (Addr a); |
| 517 | extern Addr VG_(get_shadow_base)(void); |
| 518 | extern Addr VG_(get_shadow_end) (void); |
| 519 | extern Addr VG_(get_shadow_size)(void); |
| 520 | |
| 521 | extern void *VG_(shadow_alloc)(UInt size); |
| 522 | |
| 523 | extern Bool VG_(is_addressable)(Addr p, Int sz); |
| 524 | |
| 525 | extern Addr VG_(client_alloc)(Addr base, UInt len, UInt prot, UInt flags); |
| 526 | extern void VG_(client_free)(Addr addr); |
| 527 | |
| 528 | extern Bool VG_(is_valgrind_addr)(Addr a); |
| 529 | |
| 530 | /* initialize shadow pages in the range [p, p+sz) This calls |
| 531 | init_shadow_page for each one. It should be a lot more efficient |
| 532 | for bulk-initializing shadow pages than faulting on each one. |
| 533 | */ |
| 534 | extern void VG_(init_shadow_range)(Addr p, UInt sz, Bool call_init); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 535 | |
| 536 | /* ------------------------------------------------------------------ */ |
daywalker | 5d945de | 2003-09-26 00:32:53 +0000 | [diff] [blame] | 537 | /* signal.h. |
| 538 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 539 | Note that these use the vk_ (kernel) structure |
| 540 | definitions, which are different in places from those that glibc |
| 541 | defines -- hence the 'k' prefix. Since we're operating right at the |
| 542 | kernel interface, glibc's view of the world is entirely irrelevant. */ |
| 543 | |
| 544 | /* --- Signal set ops --- */ |
njn | d5bb0a5 | 2002-09-27 10:24:48 +0000 | [diff] [blame] | 545 | extern Int VG_(ksigfillset) ( vki_ksigset_t* set ); |
| 546 | extern Int VG_(ksigemptyset) ( vki_ksigset_t* set ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 547 | |
njn | d5bb0a5 | 2002-09-27 10:24:48 +0000 | [diff] [blame] | 548 | extern Bool VG_(kisfullsigset) ( vki_ksigset_t* set ); |
| 549 | extern Bool VG_(kisemptysigset) ( vki_ksigset_t* set ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 550 | |
njn | d5bb0a5 | 2002-09-27 10:24:48 +0000 | [diff] [blame] | 551 | extern Int VG_(ksigaddset) ( vki_ksigset_t* set, Int signum ); |
| 552 | extern Int VG_(ksigdelset) ( vki_ksigset_t* set, Int signum ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 553 | extern Int VG_(ksigismember) ( vki_ksigset_t* set, Int signum ); |
| 554 | |
njn | d5bb0a5 | 2002-09-27 10:24:48 +0000 | [diff] [blame] | 555 | extern void VG_(ksigaddset_from_set) ( vki_ksigset_t* dst, vki_ksigset_t* src ); |
| 556 | extern void VG_(ksigdelset_from_set) ( vki_ksigset_t* dst, vki_ksigset_t* src ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 557 | |
| 558 | /* --- Mess with the kernel's sig state --- */ |
daywalker | 5d945de | 2003-09-26 00:32:53 +0000 | [diff] [blame] | 559 | extern Int VG_(ksigprocmask) ( Int how, const vki_ksigset_t* set, |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 560 | vki_ksigset_t* oldset ); |
daywalker | 5d945de | 2003-09-26 00:32:53 +0000 | [diff] [blame] | 561 | extern Int VG_(ksigaction) ( Int signum, |
| 562 | const vki_ksigaction* act, |
njn | d5bb0a5 | 2002-09-27 10:24:48 +0000 | [diff] [blame] | 563 | vki_ksigaction* oldact ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 564 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 565 | extern Int VG_(ksigtimedwait)( const vki_ksigset_t *, vki_ksiginfo_t *, |
| 566 | const struct vki_timespec * ); |
| 567 | |
njn | d5bb0a5 | 2002-09-27 10:24:48 +0000 | [diff] [blame] | 568 | extern Int VG_(ksignal) ( Int signum, void (*sighandler)(Int) ); |
| 569 | extern Int VG_(ksigaltstack) ( const vki_kstack_t* ss, vki_kstack_t* oss ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 570 | |
sewardj | dcaf312 | 2002-09-30 23:12:33 +0000 | [diff] [blame] | 571 | extern Int VG_(kkill) ( Int pid, Int signo ); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 572 | extern Int VG_(ktkill) ( Int pid, Int signo ); |
sewardj | dcaf312 | 2002-09-30 23:12:33 +0000 | [diff] [blame] | 573 | extern Int VG_(ksigpending) ( vki_ksigset_t* set ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 574 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 575 | extern Int VG_(waitpid) ( Int pid, Int *status, Int options ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 576 | |
njn | e7442cf | 2003-09-30 14:03:21 +0000 | [diff] [blame] | 577 | /* ------------------------------------------------------------------ */ |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 578 | /* socket.h. */ |
| 579 | |
| 580 | extern Int VG_(getsockname) ( Int sd, struct vki_sockaddr *name, Int *namelen); |
| 581 | extern Int VG_(getpeername) ( Int sd, struct vki_sockaddr *name, Int *namelen); |
| 582 | extern Int VG_(getsockopt) ( Int sd, Int level, Int optname, void *optval, |
| 583 | Int *optlen); |
| 584 | |
| 585 | /* ------------------------------------------------------------------ */ |
njn | e7442cf | 2003-09-30 14:03:21 +0000 | [diff] [blame] | 586 | /* other, randomly useful functions */ |
| 587 | extern UInt VG_(read_millisecond_timer) ( void ); |
| 588 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 589 | /*====================================================================*/ |
| 590 | /*=== UCode definition ===*/ |
| 591 | /*====================================================================*/ |
| 592 | |
sewardj | e104247 | 2002-09-30 12:33:11 +0000 | [diff] [blame] | 593 | /* Tags which describe what operands are. Must fit into 4 bits, which |
| 594 | they clearly do. */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 595 | typedef |
sewardj | e104247 | 2002-09-30 12:33:11 +0000 | [diff] [blame] | 596 | enum { TempReg =0, /* virtual temp-reg */ |
| 597 | ArchReg =1, /* simulated integer reg */ |
| 598 | ArchRegS =2, /* simulated segment reg */ |
| 599 | RealReg =3, /* real machine's real reg */ |
| 600 | SpillNo =4, /* spill slot location */ |
| 601 | Literal =5, /* literal; .lit32 field has actual value */ |
| 602 | Lit16 =6, /* literal; .val[123] field has actual value */ |
| 603 | NoValue =7 /* operand not in use */ |
| 604 | } |
| 605 | Tag; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 606 | |
njn | d5bb0a5 | 2002-09-27 10:24:48 +0000 | [diff] [blame] | 607 | /* Invalid register numbers (can't be negative) */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 608 | #define INVALID_TEMPREG 999999999 |
| 609 | #define INVALID_REALREG 999999999 |
| 610 | |
| 611 | /* Microinstruction opcodes. */ |
| 612 | typedef |
| 613 | enum { |
njn | d5bb0a5 | 2002-09-27 10:24:48 +0000 | [diff] [blame] | 614 | NOP, /* Null op */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 615 | |
daywalker | 7e73e5f | 2003-07-04 16:18:15 +0000 | [diff] [blame] | 616 | LOCK, /* Indicate the existence of a LOCK prefix (functionally NOP) */ |
sewardj | 7a5ebcf | 2002-11-13 22:42:13 +0000 | [diff] [blame] | 617 | |
njn | d5bb0a5 | 2002-09-27 10:24:48 +0000 | [diff] [blame] | 618 | /* Moving values around */ |
| 619 | GET, PUT, /* simulated register <--> TempReg */ |
| 620 | GETF, PUTF, /* simulated %eflags <--> TempReg */ |
| 621 | LOAD, STORE, /* memory <--> TempReg */ |
| 622 | MOV, /* TempReg <--> TempReg */ |
| 623 | CMOV, /* Used for cmpxchg and cmov */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 624 | |
njn | d5bb0a5 | 2002-09-27 10:24:48 +0000 | [diff] [blame] | 625 | /* Arithmetic/logical ops */ |
jsgf | 5efa4fd | 2003-10-14 21:49:11 +0000 | [diff] [blame] | 626 | MUL, UMUL, /* Multiply */ |
njn | d5bb0a5 | 2002-09-27 10:24:48 +0000 | [diff] [blame] | 627 | ADD, ADC, SUB, SBB, /* Add/subtract (w/wo carry) */ |
| 628 | AND, OR, XOR, NOT, /* Boolean ops */ |
| 629 | SHL, SHR, SAR, ROL, ROR, RCL, RCR, /* Shift/rotate (w/wo carry) */ |
| 630 | NEG, /* Negate */ |
| 631 | INC, DEC, /* Increment/decrement */ |
| 632 | BSWAP, /* Big-endian <--> little-endian */ |
| 633 | CC2VAL, /* Condition code --> 0 or 1 */ |
| 634 | WIDEN, /* Signed or unsigned widening */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 635 | |
njn | d5bb0a5 | 2002-09-27 10:24:48 +0000 | [diff] [blame] | 636 | /* Conditional or unconditional jump */ |
daywalker | 5d945de | 2003-09-26 00:32:53 +0000 | [diff] [blame] | 637 | JMP, |
njn | d5bb0a5 | 2002-09-27 10:24:48 +0000 | [diff] [blame] | 638 | |
| 639 | /* FPU ops */ |
| 640 | FPU, /* Doesn't touch memory */ |
| 641 | FPU_R, FPU_W, /* Reads/writes memory */ |
| 642 | |
sewardj | 3d7c9c8 | 2003-03-26 21:08:13 +0000 | [diff] [blame] | 643 | /* ------------ MMX ops ------------ */ |
sewardj | 8cec6ee | 2003-05-18 11:56:39 +0000 | [diff] [blame] | 644 | /* In this and the SSE encoding, bytes at higher addresses are |
| 645 | held in bits [7:0] in these 16-bit words. I guess this means |
| 646 | it is a big-endian encoding. */ |
sewardj | 3d7c9c8 | 2003-03-26 21:08:13 +0000 | [diff] [blame] | 647 | |
| 648 | /* 1 byte, no memrefs, no iregdefs, copy exactly to the |
| 649 | output. Held in val1[7:0]. */ |
| 650 | MMX1, |
| 651 | |
| 652 | /* 2 bytes, no memrefs, no iregdefs, copy exactly to the |
| 653 | output. Held in val1[15:0]. */ |
| 654 | MMX2, |
| 655 | |
| 656 | /* 3 bytes, no memrefs, no iregdefs, copy exactly to the |
| 657 | output. Held in val1[15:0] and val2[7:0]. */ |
| 658 | MMX3, |
| 659 | |
| 660 | /* 2 bytes, reads/writes mem. Insns of the form |
| 661 | bbbbbbbb:mod mmxreg r/m. |
| 662 | Held in val1[15:0], and mod and rm are to be replaced |
daywalker | 5d945de | 2003-09-26 00:32:53 +0000 | [diff] [blame] | 663 | at codegen time by a reference to the Temp/RealReg holding |
sewardj | 3d7c9c8 | 2003-03-26 21:08:13 +0000 | [diff] [blame] | 664 | the address. Arg2 holds this Temp/Real Reg. |
| 665 | Transfer is always at size 8. |
| 666 | */ |
| 667 | MMX2_MemRd, |
| 668 | MMX2_MemWr, |
| 669 | |
sewardj | 4fbe6e9 | 2003-06-15 21:54:34 +0000 | [diff] [blame] | 670 | /* 2 bytes, reads/writes an integer ("E") register. Insns of the form |
sewardj | 3d7c9c8 | 2003-03-26 21:08:13 +0000 | [diff] [blame] | 671 | bbbbbbbb:11 mmxreg ireg. |
| 672 | Held in val1[15:0], and ireg is to be replaced |
| 673 | at codegen time by a reference to the relevant RealReg. |
| 674 | Transfer is always at size 4. Arg2 holds this Temp/Real Reg. |
| 675 | */ |
sewardj | 4fbe6e9 | 2003-06-15 21:54:34 +0000 | [diff] [blame] | 676 | MMX2_ERegRd, |
| 677 | MMX2_ERegWr, |
sewardj | 3d7c9c8 | 2003-03-26 21:08:13 +0000 | [diff] [blame] | 678 | |
sewardj | 8cec6ee | 2003-05-18 11:56:39 +0000 | [diff] [blame] | 679 | /* ------------ SSE/SSE2 ops ------------ */ |
| 680 | /* In the following: |
| 681 | |
sewardj | febaa3b | 2003-05-25 01:07:34 +0000 | [diff] [blame] | 682 | a digit N indicates the next N bytes are to be copied exactly |
| 683 | to the output. |
sewardj | 8cec6ee | 2003-05-18 11:56:39 +0000 | [diff] [blame] | 684 | |
| 685 | 'a' indicates a mod-xmmreg-rm byte, where the mod-rm part is |
| 686 | to be replaced at codegen time to a Temp/RealReg holding the |
| 687 | address. |
| 688 | |
sewardj | 4fbe6e9 | 2003-06-15 21:54:34 +0000 | [diff] [blame] | 689 | 'e' indicates a byte of the form '11 xmmreg ireg', where ireg |
| 690 | is read or written, and is to be replaced at codegen time by |
| 691 | a reference to the relevant RealReg. 'e' because it's the E |
| 692 | reg in Intel encoding parlance. |
sewardj | 8cec6ee | 2003-05-18 11:56:39 +0000 | [diff] [blame] | 693 | |
sewardj | 4fbe6e9 | 2003-06-15 21:54:34 +0000 | [diff] [blame] | 694 | 'g' indicates a byte of the form '11 ireg xmmreg', where ireg |
| 695 | is read or written, and is to be replaced at codegen time by |
| 696 | a reference to the relevant RealReg. 'g' because it's called |
| 697 | G in Intel parlance. */ |
sewardj | 8cec6ee | 2003-05-18 11:56:39 +0000 | [diff] [blame] | 698 | |
| 699 | /* 3 bytes, no memrefs, no iregdefs, copy exactly to the |
| 700 | output. Held in val1[15:0] and val2[7:0]. */ |
| 701 | SSE3, |
| 702 | |
| 703 | /* 3 bytes, reads/writes mem. Insns of the form |
| 704 | bbbbbbbb:bbbbbbbb:mod mmxreg r/m. |
| 705 | Held in val1[15:0] and val2[7:0], and mod and rm are to be |
| 706 | replaced at codegen time by a reference to the Temp/RealReg |
| 707 | holding the address. Arg3 holds this Temp/Real Reg. |
sewardj | febaa3b | 2003-05-25 01:07:34 +0000 | [diff] [blame] | 708 | Transfer is usually, but not always, at size 16. */ |
sewardj | 8cec6ee | 2003-05-18 11:56:39 +0000 | [diff] [blame] | 709 | SSE2a_MemRd, |
| 710 | SSE2a_MemWr, |
| 711 | |
nethercote | b1affa8 | 2004-01-19 19:14:18 +0000 | [diff] [blame] | 712 | /* 4 bytes, writes an integer register. Insns of the form |
| 713 | bbbbbbbb:bbbbbbbb:11 ireg bbb. |
| 714 | Held in val1[15:0] and val2[7:0], and ireg is to be replaced |
| 715 | at codegen time by a reference to the relevant RealReg. |
| 716 | Transfer is always at size 4. Arg3 holds this Temp/Real Reg. |
| 717 | */ |
| 718 | SSE2g_RegWr, |
| 719 | |
| 720 | /* 5 bytes, writes an integer register. Insns of the form |
| 721 | bbbbbbbb:bbbbbbbb:11 ireg bbb :bbbbbbbb. Held in |
| 722 | val1[15:0] and val2[7:0] and lit32[7:0], and ireg is to be |
| 723 | replaced at codegen time by a reference to the relevant |
| 724 | RealReg. Transfer is always at size 4. Arg3 holds this |
| 725 | Temp/Real Reg. |
| 726 | */ |
| 727 | SSE2g1_RegWr, |
| 728 | |
| 729 | /* 5 bytes, reads an integer register. Insns of the form |
| 730 | bbbbbbbb:bbbbbbbb:11 bbb ireg :bbbbbbbb. Held in |
| 731 | val1[15:0] and val2[7:0] and lit32[7:0], and ireg is to be |
| 732 | replaced at codegen time by a reference to the relevant |
| 733 | RealReg. Transfer is always at size 4. Arg3 holds this |
| 734 | Temp/Real Reg. |
| 735 | */ |
| 736 | SSE2e1_RegRd, |
| 737 | |
sewardj | 8cec6ee | 2003-05-18 11:56:39 +0000 | [diff] [blame] | 738 | /* 4 bytes, no memrefs, no iregdefs, copy exactly to the |
| 739 | output. Held in val1[15:0] and val2[15:0]. */ |
| 740 | SSE4, |
| 741 | |
| 742 | /* 4 bytes, reads/writes mem. Insns of the form |
| 743 | bbbbbbbb:bbbbbbbb:bbbbbbbb:mod mmxreg r/m. |
| 744 | Held in val1[15:0] and val2[15:0], and mod and rm are to be |
| 745 | replaced at codegen time by a reference to the Temp/RealReg |
| 746 | holding the address. Arg3 holds this Temp/Real Reg. |
sewardj | 9dd209f | 2003-06-18 23:30:52 +0000 | [diff] [blame] | 747 | Transfer is at stated size. */ |
sewardj | 8cec6ee | 2003-05-18 11:56:39 +0000 | [diff] [blame] | 748 | SSE3a_MemRd, |
| 749 | SSE3a_MemWr, |
| 750 | |
| 751 | /* 4 bytes, reads/writes mem. Insns of the form |
| 752 | bbbbbbbb:bbbbbbbb:mod mmxreg r/m:bbbbbbbb |
| 753 | Held in val1[15:0] and val2[15:0], and mod and rm are to be |
| 754 | replaced at codegen time by a reference to the Temp/RealReg |
| 755 | holding the address. Arg3 holds this Temp/Real Reg. |
sewardj | 9dd209f | 2003-06-18 23:30:52 +0000 | [diff] [blame] | 756 | Transfer is at stated size. */ |
sewardj | 8cec6ee | 2003-05-18 11:56:39 +0000 | [diff] [blame] | 757 | SSE2a1_MemRd, |
sewardj | 77d30a2 | 2003-10-19 08:18:52 +0000 | [diff] [blame] | 758 | |
sewardj | 8cec6ee | 2003-05-18 11:56:39 +0000 | [diff] [blame] | 759 | /* 4 bytes, writes an integer register. Insns of the form |
sewardj | 4fbe6e9 | 2003-06-15 21:54:34 +0000 | [diff] [blame] | 760 | bbbbbbbb:bbbbbbbb:bbbbbbbb:11 ireg bbb. |
sewardj | 8cec6ee | 2003-05-18 11:56:39 +0000 | [diff] [blame] | 761 | Held in val1[15:0] and val2[15:0], and ireg is to be replaced |
| 762 | at codegen time by a reference to the relevant RealReg. |
| 763 | Transfer is always at size 4. Arg3 holds this Temp/Real Reg. |
| 764 | */ |
| 765 | SSE3g_RegWr, |
| 766 | |
sewardj | b31b06d | 2003-06-13 00:26:02 +0000 | [diff] [blame] | 767 | /* 5 bytes, writes an integer register. Insns of the form |
| 768 | bbbbbbbb:bbbbbbbb:bbbbbbbb: 11 ireg bbb :bbbbbbbb. Held in |
| 769 | val1[15:0] and val2[15:0] and lit32[7:0], and ireg is to be |
| 770 | replaced at codegen time by a reference to the relevant |
| 771 | RealReg. Transfer is always at size 4. Arg3 holds this |
| 772 | Temp/Real Reg. |
| 773 | */ |
| 774 | SSE3g1_RegWr, |
| 775 | |
sewardj | 4fbe6e9 | 2003-06-15 21:54:34 +0000 | [diff] [blame] | 776 | /* 4 bytes, reads an integer register. Insns of the form |
| 777 | bbbbbbbb:bbbbbbbb:bbbbbbbb:11 bbb ireg. |
| 778 | Held in val1[15:0] and val2[15:0], and ireg is to be replaced |
| 779 | at codegen time by a reference to the relevant RealReg. |
| 780 | Transfer is always at size 4. Arg3 holds this Temp/Real Reg. |
| 781 | */ |
| 782 | SSE3e_RegRd, |
sewardj | abf8bf8 | 2003-06-15 22:28:05 +0000 | [diff] [blame] | 783 | SSE3e_RegWr, /* variant that writes Ereg, not reads it */ |
sewardj | 4fbe6e9 | 2003-06-15 21:54:34 +0000 | [diff] [blame] | 784 | |
sewardj | b31b06d | 2003-06-13 00:26:02 +0000 | [diff] [blame] | 785 | /* 5 bytes, reads an integer register. Insns of the form |
| 786 | bbbbbbbb:bbbbbbbb:bbbbbbbb: 11 bbb ireg :bbbbbbbb. Held in |
| 787 | val1[15:0] and val2[15:0] and lit32[7:0], and ireg is to be |
| 788 | replaced at codegen time by a reference to the relevant |
| 789 | RealReg. Transfer is always at size 4. Arg3 holds this |
| 790 | Temp/Real Reg. |
| 791 | */ |
sewardj | 4fbe6e9 | 2003-06-15 21:54:34 +0000 | [diff] [blame] | 792 | SSE3e1_RegRd, |
sewardj | 8cec6ee | 2003-05-18 11:56:39 +0000 | [diff] [blame] | 793 | |
sewardj | 02af6bc | 2003-06-12 00:56:06 +0000 | [diff] [blame] | 794 | /* 4 bytes, reads memory, writes an integer register, but is |
| 795 | nevertheless an SSE insn. The insn is of the form |
| 796 | bbbbbbbb:bbbbbbbb:bbbbbbbb:mod ireg rm where mod indicates |
| 797 | memory (ie is not 11b) and ireg is the int reg written. The |
sewardj | e3891fa | 2003-06-15 03:13:48 +0000 | [diff] [blame] | 798 | first 4 bytes are held in lit32[31:0] since there is |
sewardj | 02af6bc | 2003-06-12 00:56:06 +0000 | [diff] [blame] | 799 | insufficient space elsewhere. mod and rm are to be replaced |
| 800 | at codegen time by a reference to the Temp/RealReg holding |
| 801 | the address. Arg1 holds this Temp/RealReg. ireg is to be |
| 802 | replaced at codegen time by a reference to the relevant |
| 803 | RealReg in which the answer is to be written. Arg2 holds |
| 804 | this Temp/RealReg. Transfer to the destination reg is always |
| 805 | at size 4. However the memory read can be at sizes 4 or 8 |
sewardj | e3891fa | 2003-06-15 03:13:48 +0000 | [diff] [blame] | 806 | and so this is what the sz field holds. Note that the 4th |
| 807 | byte of the instruction (the modrm byte) is redundant, but we |
| 808 | store it anyway so as to be consistent with all other SSE |
| 809 | uinstrs. |
sewardj | 02af6bc | 2003-06-12 00:56:06 +0000 | [diff] [blame] | 810 | */ |
| 811 | SSE3ag_MemRd_RegWr, |
sewardj | e3891fa | 2003-06-15 03:13:48 +0000 | [diff] [blame] | 812 | |
sewardj | 8cec6ee | 2003-05-18 11:56:39 +0000 | [diff] [blame] | 813 | /* 5 bytes, no memrefs, no iregdefs, copy exactly to the |
| 814 | output. Held in val1[15:0], val2[15:0] and val3[7:0]. */ |
| 815 | SSE5, |
sewardj | 77d30a2 | 2003-10-19 08:18:52 +0000 | [diff] [blame] | 816 | |
sewardj | 8cec6ee | 2003-05-18 11:56:39 +0000 | [diff] [blame] | 817 | /* 5 bytes, reads/writes mem. Insns of the form |
| 818 | bbbbbbbb:bbbbbbbb:bbbbbbbb:mod mmxreg r/m:bbbbbbbb |
daywalker | 5d945de | 2003-09-26 00:32:53 +0000 | [diff] [blame] | 819 | Held in val1[15:0], val2[15:0], lit32[7:0]. |
| 820 | mod and rm are to be replaced at codegen time by a reference |
| 821 | to the Temp/RealReg holding the address. Arg3 holds this |
sewardj | 8cec6ee | 2003-05-18 11:56:39 +0000 | [diff] [blame] | 822 | Temp/Real Reg. Transfer is always at size 16. */ |
| 823 | SSE3a1_MemRd, |
sewardj | 77d30a2 | 2003-10-19 08:18:52 +0000 | [diff] [blame] | 824 | |
sewardj | 3d7c9c8 | 2003-03-26 21:08:13 +0000 | [diff] [blame] | 825 | /* ------------------------ */ |
| 826 | |
njn | d5bb0a5 | 2002-09-27 10:24:48 +0000 | [diff] [blame] | 827 | /* Not strictly needed, but improve address calculation translations. */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 828 | LEA1, /* reg2 := const + reg1 */ |
| 829 | LEA2, /* reg3 := const + reg1 + reg2 * 1,2,4 or 8 */ |
| 830 | |
sewardj | 8cec6ee | 2003-05-18 11:56:39 +0000 | [diff] [blame] | 831 | /* Hack for x86 REP insns. Jump to literal if TempReg/RealReg |
| 832 | is zero. */ |
njn | d5bb0a5 | 2002-09-27 10:24:48 +0000 | [diff] [blame] | 833 | JIFZ, |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 834 | |
njn | d5bb0a5 | 2002-09-27 10:24:48 +0000 | [diff] [blame] | 835 | /* Advance the simulated %eip by some small (< 128) number. */ |
| 836 | INCEIP, |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 837 | |
sewardj | e104247 | 2002-09-30 12:33:11 +0000 | [diff] [blame] | 838 | /* Dealing with segment registers */ |
| 839 | GETSEG, PUTSEG, /* simulated segment register <--> TempReg */ |
| 840 | USESEG, /* (LDT/GDT index, virtual addr) --> linear addr */ |
| 841 | |
njn | d5bb0a5 | 2002-09-27 10:24:48 +0000 | [diff] [blame] | 842 | /* Not for translating x86 calls -- only to call helpers */ |
| 843 | CALLM_S, CALLM_E, /* Mark start/end of CALLM push/pop sequence */ |
| 844 | PUSH, POP, CLEAR, /* Add/remove/zap args for helpers */ |
| 845 | CALLM, /* Call assembly-code helper */ |
| 846 | |
| 847 | /* Not for translating x86 calls -- only to call C helper functions of |
| 848 | up to three arguments (or two if the functions has a return value). |
| 849 | Arguments and return value must be word-sized. More arguments can |
| 850 | be faked with global variables (eg. use VG_(set_global_var)()). |
| 851 | |
| 852 | Seven possibilities: 'arg[123]' show where args go, 'ret' shows |
| 853 | where return value goes (if present). |
daywalker | 5d945de | 2003-09-26 00:32:53 +0000 | [diff] [blame] | 854 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 855 | CCALL(-, -, - ) void f(void) |
| 856 | CCALL(arg1, -, - ) void f(UInt arg1) |
| 857 | CCALL(arg1, arg2, - ) void f(UInt arg1, UInt arg2) |
| 858 | CCALL(arg1, arg2, arg3) void f(UInt arg1, UInt arg2, UInt arg3) |
| 859 | CCALL(-, -, ret ) UInt f(UInt) |
| 860 | CCALL(arg1, -, ret ) UInt f(UInt arg1) |
njn | d5bb0a5 | 2002-09-27 10:24:48 +0000 | [diff] [blame] | 861 | CCALL(arg1, arg2, ret ) UInt f(UInt arg1, UInt arg2) */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 862 | CCALL, |
| 863 | |
njn | d5bb0a5 | 2002-09-27 10:24:48 +0000 | [diff] [blame] | 864 | /* This opcode makes it easy for skins that extend UCode to do this to |
| 865 | avoid opcode overlap: |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 866 | |
daywalker | 5d945de | 2003-09-26 00:32:53 +0000 | [diff] [blame] | 867 | enum { EU_OP1 = DUMMY_FINAL_UOPCODE + 1, ... } |
| 868 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 869 | WARNING: Do not add new opcodes after this one! They can be added |
| 870 | before, though. */ |
| 871 | DUMMY_FINAL_UOPCODE |
| 872 | } |
| 873 | Opcode; |
| 874 | |
| 875 | |
njn | d5bb0a5 | 2002-09-27 10:24:48 +0000 | [diff] [blame] | 876 | /* Condition codes, using the Intel encoding. CondAlways is an extra. */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 877 | typedef |
| 878 | enum { |
| 879 | CondO = 0, /* overflow */ |
| 880 | CondNO = 1, /* no overflow */ |
| 881 | CondB = 2, /* below */ |
| 882 | CondNB = 3, /* not below */ |
| 883 | CondZ = 4, /* zero */ |
| 884 | CondNZ = 5, /* not zero */ |
| 885 | CondBE = 6, /* below or equal */ |
| 886 | CondNBE = 7, /* not below or equal */ |
| 887 | CondS = 8, /* negative */ |
sewardj | e104247 | 2002-09-30 12:33:11 +0000 | [diff] [blame] | 888 | CondNS = 9, /* not negative */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 889 | CondP = 10, /* parity even */ |
| 890 | CondNP = 11, /* not parity even */ |
| 891 | CondL = 12, /* jump less */ |
| 892 | CondNL = 13, /* not less */ |
| 893 | CondLE = 14, /* less or equal */ |
| 894 | CondNLE = 15, /* not less or equal */ |
| 895 | CondAlways = 16 /* Jump always */ |
daywalker | 5d945de | 2003-09-26 00:32:53 +0000 | [diff] [blame] | 896 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 897 | Condcode; |
| 898 | |
| 899 | |
| 900 | /* Descriptions of additional properties of *unconditional* jumps. */ |
| 901 | typedef |
| 902 | enum { |
| 903 | JmpBoring=0, /* boring unconditional jump */ |
| 904 | JmpCall=1, /* jump due to an x86 call insn */ |
| 905 | JmpRet=2, /* jump due to an x86 ret insn */ |
| 906 | JmpSyscall=3, /* do a system call, then jump */ |
fitzhardinge | a02f881 | 2003-12-18 09:06:09 +0000 | [diff] [blame] | 907 | JmpClientReq=4,/* do a client request, then jump */ |
| 908 | JmpYield=5 /* do a yield, then jump */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 909 | } |
| 910 | JmpKind; |
| 911 | |
| 912 | |
| 913 | /* Flags. User-level code can only read/write O(verflow), S(ign), |
| 914 | Z(ero), A(ux-carry), C(arry), P(arity), and may also write |
| 915 | D(irection). That's a total of 7 flags. A FlagSet is a bitset, |
daywalker | 5d945de | 2003-09-26 00:32:53 +0000 | [diff] [blame] | 916 | thusly: |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 917 | 76543210 |
| 918 | DOSZACP |
| 919 | and bit 7 must always be zero since it is unused. |
sewardj | 2370f3b | 2002-11-30 15:01:01 +0000 | [diff] [blame] | 920 | |
| 921 | Note: these Flag? values are **not** the positions in the actual |
| 922 | %eflags register. */ |
| 923 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 924 | typedef UChar FlagSet; |
| 925 | |
| 926 | #define FlagD (1<<6) |
| 927 | #define FlagO (1<<5) |
| 928 | #define FlagS (1<<4) |
| 929 | #define FlagZ (1<<3) |
| 930 | #define FlagA (1<<2) |
| 931 | #define FlagC (1<<1) |
| 932 | #define FlagP (1<<0) |
| 933 | |
| 934 | #define FlagsOSZACP (FlagO | FlagS | FlagZ | FlagA | FlagC | FlagP) |
| 935 | #define FlagsOSZAP (FlagO | FlagS | FlagZ | FlagA | FlagP) |
| 936 | #define FlagsOSZCP (FlagO | FlagS | FlagZ | FlagC | FlagP) |
| 937 | #define FlagsOSACP (FlagO | FlagS | FlagA | FlagC | FlagP) |
| 938 | #define FlagsSZACP ( FlagS | FlagZ | FlagA | FlagC | FlagP) |
| 939 | #define FlagsSZAP ( FlagS | FlagZ | FlagA | FlagP) |
| 940 | #define FlagsZCP ( FlagZ | FlagC | FlagP) |
| 941 | #define FlagsOC (FlagO | FlagC ) |
| 942 | #define FlagsAC ( FlagA | FlagC ) |
| 943 | |
| 944 | #define FlagsALL (FlagsOSZACP | FlagD) |
| 945 | #define FlagsEmpty (FlagSet)0 |
| 946 | |
| 947 | |
sewardj | 2370f3b | 2002-11-30 15:01:01 +0000 | [diff] [blame] | 948 | /* flag positions in eflags */ |
| 949 | #define EFlagC (1 << 0) /* carry */ |
| 950 | #define EFlagP (1 << 2) /* parity */ |
sewardj | fa492d4 | 2002-12-08 18:20:01 +0000 | [diff] [blame] | 951 | #define EFlagA (1 << 4) /* aux carry */ |
sewardj | 2370f3b | 2002-11-30 15:01:01 +0000 | [diff] [blame] | 952 | #define EFlagZ (1 << 6) /* zero */ |
| 953 | #define EFlagS (1 << 7) /* sign */ |
sewardj | fa492d4 | 2002-12-08 18:20:01 +0000 | [diff] [blame] | 954 | #define EFlagD (1 << 10) /* direction */ |
sewardj | 2370f3b | 2002-11-30 15:01:01 +0000 | [diff] [blame] | 955 | #define EFlagO (1 << 11) /* overflow */ |
fitzhardinge | c2dbbac | 2004-01-23 23:09:01 +0000 | [diff] [blame^] | 956 | #define EFlagID (1 << 21) /* changable if CPUID exists */ |
sewardj | 2370f3b | 2002-11-30 15:01:01 +0000 | [diff] [blame] | 957 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 958 | /* Liveness of general purpose registers, useful for code generation. |
| 959 | Reg rank order 0..N-1 corresponds to bits 0..N-1, ie. first |
| 960 | reg's liveness in bit 0, last reg's in bit N-1. Note that |
| 961 | these rankings don't match the Intel register ordering. */ |
| 962 | typedef UInt RRegSet; |
| 963 | |
| 964 | #define ALL_RREGS_DEAD 0 /* 0000...00b */ |
njn | e7c258c | 2002-10-03 08:57:01 +0000 | [diff] [blame] | 965 | #define ALL_RREGS_LIVE ((1 << VG_MAX_REALREGS)-1) /* 0011...11b */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 966 | #define UNIT_RREGSET(rank) (1 << (rank)) |
| 967 | |
| 968 | #define IS_RREG_LIVE(rank,rregs_live) (rregs_live & UNIT_RREGSET(rank)) |
| 969 | #define SET_RREG_LIVENESS(rank,rregs_live,b) \ |
| 970 | do { RRegSet unit = UNIT_RREGSET(rank); \ |
| 971 | if (b) rregs_live |= unit; \ |
| 972 | else rregs_live &= ~unit; \ |
| 973 | } while(0) |
| 974 | |
| 975 | |
| 976 | /* A Micro (u)-instruction. */ |
| 977 | typedef |
| 978 | struct { |
| 979 | /* word 1 */ |
| 980 | UInt lit32; /* 32-bit literal */ |
| 981 | |
| 982 | /* word 2 */ |
| 983 | UShort val1; /* first operand */ |
| 984 | UShort val2; /* second operand */ |
| 985 | |
| 986 | /* word 3 */ |
| 987 | UShort val3; /* third operand */ |
| 988 | UChar opcode; /* opcode */ |
jseward | fca6018 | 2004-01-04 23:30:55 +0000 | [diff] [blame] | 989 | UShort size; /* data transfer size */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 990 | |
| 991 | /* word 4 */ |
| 992 | FlagSet flags_r; /* :: FlagSet */ |
| 993 | FlagSet flags_w; /* :: FlagSet */ |
| 994 | UChar tag1:4; /* first operand tag */ |
| 995 | UChar tag2:4; /* second operand tag */ |
| 996 | UChar tag3:4; /* third operand tag */ |
| 997 | UChar extra4b:4; /* Spare field, used by WIDEN for src |
| 998 | -size, and by LEA2 for scale (1,2,4 or 8), |
| 999 | and by JMPs for original x86 instr size */ |
| 1000 | |
| 1001 | /* word 5 */ |
| 1002 | UChar cond; /* condition, for jumps */ |
| 1003 | Bool signed_widen:1; /* signed or unsigned WIDEN ? */ |
| 1004 | JmpKind jmpkind:3; /* additional properties of unconditional JMP */ |
| 1005 | |
daywalker | 5d945de | 2003-09-26 00:32:53 +0000 | [diff] [blame] | 1006 | /* Additional properties for UInstrs that call C functions: |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1007 | - CCALL |
| 1008 | - PUT (when %ESP is the target) |
| 1009 | - possibly skin-specific UInstrs |
| 1010 | */ |
| 1011 | UChar argc:2; /* Number of args, max 3 */ |
| 1012 | UChar regparms_n:2; /* Number of args passed in registers */ |
| 1013 | Bool has_ret_val:1; /* Function has return value? */ |
| 1014 | |
| 1015 | /* RealReg liveness; only sensical after reg alloc and liveness |
| 1016 | analysis done. This info is a little bit arch-specific -- |
| 1017 | VG_MAX_REALREGS can vary on different architectures. Note that |
| 1018 | to use this information requires converting between register ranks |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 1019 | and the Intel register numbers, using VG_(realreg_to_rank)() |
| 1020 | and/or VG_(rank_to_realreg)() */ |
daywalker | 5d945de | 2003-09-26 00:32:53 +0000 | [diff] [blame] | 1021 | RRegSet regs_live_after:VG_MAX_REALREGS; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1022 | } |
| 1023 | UInstr; |
| 1024 | |
| 1025 | |
daywalker | 5d945de | 2003-09-26 00:32:53 +0000 | [diff] [blame] | 1026 | typedef |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 1027 | struct _UCodeBlock |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1028 | UCodeBlock; |
| 1029 | |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 1030 | extern Int VG_(get_num_instrs) (UCodeBlock* cb); |
| 1031 | extern Int VG_(get_num_temps) (UCodeBlock* cb); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1032 | |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 1033 | extern UInstr* VG_(get_instr) (UCodeBlock* cb, Int i); |
| 1034 | extern UInstr* VG_(get_last_instr) (UCodeBlock* cb); |
daywalker | 5d945de | 2003-09-26 00:32:53 +0000 | [diff] [blame] | 1035 | |
njn | 211b6ad | 2003-02-03 12:33:31 +0000 | [diff] [blame] | 1036 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1037 | /*====================================================================*/ |
| 1038 | /*=== Instrumenting UCode ===*/ |
| 1039 | /*====================================================================*/ |
| 1040 | |
njn | f4ce3d3 | 2003-02-10 10:17:26 +0000 | [diff] [blame] | 1041 | /* Maximum number of registers read or written by a single UInstruction. */ |
| 1042 | #define VG_MAX_REGS_USED 3 |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1043 | |
njn | f4ce3d3 | 2003-02-10 10:17:26 +0000 | [diff] [blame] | 1044 | /* Find what this instruction does to its regs, useful for |
| 1045 | analysis/optimisation passes. `tag' indicates whether we're considering |
| 1046 | TempRegs (pre-reg-alloc) or RealRegs (post-reg-alloc). `regs' is filled |
| 1047 | with the affected register numbers, `isWrites' parallels it and indicates |
| 1048 | if the reg is read or written. If a reg is read and written, it will |
| 1049 | appear twice in `regs'. `regs' and `isWrites' must be able to fit |
| 1050 | VG_MAX_REGS_USED elements. */ |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 1051 | extern Int VG_(get_reg_usage) ( UInstr* u, Tag tag, Int* regs, Bool* isWrites ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1052 | |
| 1053 | |
njn | d5bb0a5 | 2002-09-27 10:24:48 +0000 | [diff] [blame] | 1054 | /* Used to register helper functions to be called from generated code. A |
| 1055 | limited number of compact helpers can be registered; the code generated |
| 1056 | to call them is slightly shorter -- so register the mostly frequently |
| 1057 | called helpers as compact. */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1058 | extern void VG_(register_compact_helper) ( Addr a ); |
| 1059 | extern void VG_(register_noncompact_helper) ( Addr a ); |
| 1060 | |
| 1061 | |
| 1062 | /* ------------------------------------------------------------------ */ |
| 1063 | /* Virtual register allocation */ |
| 1064 | |
| 1065 | /* Get a new virtual register */ |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 1066 | extern Int VG_(get_new_temp) ( UCodeBlock* cb ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1067 | |
| 1068 | /* Get a new virtual shadow register */ |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 1069 | extern Int VG_(get_new_shadow) ( UCodeBlock* cb ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1070 | |
| 1071 | /* Get a virtual register's corresponding virtual shadow register */ |
| 1072 | #define SHADOW(tempreg) ((tempreg)+1) |
| 1073 | |
| 1074 | |
| 1075 | /* ------------------------------------------------------------------ */ |
| 1076 | /* Low-level UInstr builders */ |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 1077 | extern void VG_(new_NOP) ( UInstr* u ); |
| 1078 | extern void VG_(new_UInstr0) ( UCodeBlock* cb, Opcode opcode, Int sz ); |
| 1079 | extern void VG_(new_UInstr1) ( UCodeBlock* cb, Opcode opcode, Int sz, |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1080 | Tag tag1, UInt val1 ); |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 1081 | extern void VG_(new_UInstr2) ( UCodeBlock* cb, Opcode opcode, Int sz, |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1082 | Tag tag1, UInt val1, |
| 1083 | Tag tag2, UInt val2 ); |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 1084 | extern void VG_(new_UInstr3) ( UCodeBlock* cb, Opcode opcode, Int sz, |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1085 | Tag tag1, UInt val1, |
| 1086 | Tag tag2, UInt val2, |
| 1087 | Tag tag3, UInt val3 ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1088 | |
daywalker | 5d945de | 2003-09-26 00:32:53 +0000 | [diff] [blame] | 1089 | /* Set read/write/undefined flags. Undefined flags are treaten as written, |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 1090 | but it's worth keeping them logically distinct. */ |
| 1091 | extern void VG_(set_flag_fields) ( UCodeBlock* cb, FlagSet fr, FlagSet fw, |
| 1092 | FlagSet fu); |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 1093 | extern void VG_(set_lit_field) ( UCodeBlock* cb, UInt lit32 ); |
| 1094 | extern void VG_(set_ccall_fields) ( UCodeBlock* cb, Addr fn, UChar argc, |
| 1095 | UChar regparms_n, Bool has_ret_val ); |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 1096 | extern void VG_(set_cond_field) ( UCodeBlock* cb, Condcode code ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1097 | |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 1098 | extern void VG_(copy_UInstr) ( UCodeBlock* cb, UInstr* instr ); |
| 1099 | |
| 1100 | extern Bool VG_(any_flag_use)( UInstr* u ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1101 | |
njn | ac6c176 | 2002-10-04 14:34:15 +0000 | [diff] [blame] | 1102 | /* Macro versions of the above; just shorter to type. */ |
| 1103 | #define uInstr0 VG_(new_UInstr0) |
| 1104 | #define uInstr1 VG_(new_UInstr1) |
| 1105 | #define uInstr2 VG_(new_UInstr2) |
| 1106 | #define uInstr3 VG_(new_UInstr3) |
| 1107 | #define uLiteral VG_(set_lit_field) |
| 1108 | #define uCCall VG_(set_ccall_fields) |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 1109 | #define uCond VG_(set_cond_field) |
| 1110 | #define uFlagsRWU VG_(set_flag_fields) |
njn | ac6c176 | 2002-10-04 14:34:15 +0000 | [diff] [blame] | 1111 | #define newTemp VG_(get_new_temp) |
| 1112 | #define newShadow VG_(get_new_shadow) |
| 1113 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1114 | /* Refer to `the last instruction stuffed in' (can be lvalue). */ |
| 1115 | #define LAST_UINSTR(cb) (cb)->instrs[(cb)->used-1] |
| 1116 | |
| 1117 | |
| 1118 | /* ------------------------------------------------------------------ */ |
| 1119 | /* Higher-level UInstr sequence builders */ |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 1120 | extern void VG_(call_helper_0_0) ( UCodeBlock* cb, Addr f); |
| 1121 | extern void VG_(call_helper_1_0) ( UCodeBlock* cb, Addr f, UInt arg1, |
| 1122 | UInt regparms_n); |
| 1123 | extern void VG_(call_helper_2_0) ( UCodeBlock* cb, Addr f, UInt arg1, UInt arg2, |
| 1124 | UInt regparms_n); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1125 | |
| 1126 | /* One way around the 3-arg C function limit is to pass args via global |
njn | 6fefa1b | 2003-02-24 10:32:51 +0000 | [diff] [blame] | 1127 | * variables... ugly, but it works. This one puts a literal in there. */ |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 1128 | extern void VG_(set_global_var) ( UCodeBlock* cb, Addr globvar_ptr, UInt val); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1129 | |
njn | 6fefa1b | 2003-02-24 10:32:51 +0000 | [diff] [blame] | 1130 | /* This one puts the contents of a TempReg in the global variable. */ |
| 1131 | extern void VG_(set_global_var_tempreg) ( UCodeBlock* cb, Addr globvar_ptr, |
| 1132 | UInt t_val); |
| 1133 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1134 | /* ------------------------------------------------------------------ */ |
njn | d5bb0a5 | 2002-09-27 10:24:48 +0000 | [diff] [blame] | 1135 | /* Allocating/freeing basic blocks of UCode */ |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 1136 | extern UCodeBlock* VG_(setup_UCodeBlock) ( UCodeBlock* cb ); |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 1137 | extern void VG_(free_UCodeBlock) ( UCodeBlock* cb ); |
njn | d5bb0a5 | 2002-09-27 10:24:48 +0000 | [diff] [blame] | 1138 | |
| 1139 | /* ------------------------------------------------------------------ */ |
daywalker | 5d945de | 2003-09-26 00:32:53 +0000 | [diff] [blame] | 1140 | /* UCode pretty/ugly printing. Probably only useful to call from a skin |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1141 | if VG_(needs).extended_UCode == True. */ |
| 1142 | |
| 1143 | /* When True, all generated code is/should be printed. */ |
| 1144 | extern Bool VG_(print_codegen); |
| 1145 | |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 1146 | /* Pretty/ugly printing functions */ |
| 1147 | extern void VG_(pp_UCodeBlock) ( UCodeBlock* cb, Char* title ); |
| 1148 | extern void VG_(pp_UInstr) ( Int instrNo, UInstr* u ); |
| 1149 | extern void VG_(pp_UInstr_regs) ( Int instrNo, UInstr* u ); |
| 1150 | extern void VG_(up_UInstr) ( Int instrNo, UInstr* u ); |
| 1151 | extern Char* VG_(name_UOpcode) ( Bool upper, Opcode opc ); |
njn | 563f96f | 2003-02-03 11:17:46 +0000 | [diff] [blame] | 1152 | extern Char* VG_(name_UCondcode) ( Condcode cond ); |
daywalker | 5d945de | 2003-09-26 00:32:53 +0000 | [diff] [blame] | 1153 | extern void VG_(pp_UOperand) ( UInstr* u, Int operandNo, |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 1154 | Int sz, Bool parens ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1155 | |
njn | b93d178 | 2003-02-03 12:03:22 +0000 | [diff] [blame] | 1156 | /* ------------------------------------------------------------------ */ |
njn | f4ce3d3 | 2003-02-10 10:17:26 +0000 | [diff] [blame] | 1157 | /* Accessing archregs and their shadows */ |
| 1158 | extern UInt VG_(get_archreg) ( UInt archreg ); |
| 1159 | extern UInt VG_(get_thread_archreg) ( ThreadId tid, UInt archreg ); |
| 1160 | |
njn | b93d178 | 2003-02-03 12:03:22 +0000 | [diff] [blame] | 1161 | extern UInt VG_(get_shadow_archreg) ( UInt archreg ); |
| 1162 | extern void VG_(set_shadow_archreg) ( UInt archreg, UInt val ); |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 1163 | extern void VG_(set_shadow_eflags) ( UInt val ); |
njn | b93d178 | 2003-02-03 12:03:22 +0000 | [diff] [blame] | 1164 | extern Addr VG_(shadow_archreg_address) ( UInt archreg ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1165 | |
njn | f4ce3d3 | 2003-02-10 10:17:26 +0000 | [diff] [blame] | 1166 | extern UInt VG_(get_thread_shadow_archreg) ( ThreadId tid, UInt archreg ); |
| 1167 | extern void VG_(set_thread_shadow_archreg) ( ThreadId tid, UInt archreg, |
| 1168 | UInt val ); |
njn | 211b6ad | 2003-02-03 12:33:31 +0000 | [diff] [blame] | 1169 | |
| 1170 | /* ------------------------------------------------------------------ */ |
| 1171 | /* Offsets of addresses of helper functions. A "helper" function is one |
| 1172 | which is called from generated code via CALLM. */ |
| 1173 | |
| 1174 | extern Int VGOFF_(helper_idiv_64_32); |
| 1175 | extern Int VGOFF_(helper_div_64_32); |
| 1176 | extern Int VGOFF_(helper_idiv_32_16); |
| 1177 | extern Int VGOFF_(helper_div_32_16); |
| 1178 | extern Int VGOFF_(helper_idiv_16_8); |
| 1179 | extern Int VGOFF_(helper_div_16_8); |
| 1180 | |
| 1181 | extern Int VGOFF_(helper_imul_32_64); |
| 1182 | extern Int VGOFF_(helper_mul_32_64); |
| 1183 | extern Int VGOFF_(helper_imul_16_32); |
| 1184 | extern Int VGOFF_(helper_mul_16_32); |
| 1185 | extern Int VGOFF_(helper_imul_8_16); |
| 1186 | extern Int VGOFF_(helper_mul_8_16); |
| 1187 | |
| 1188 | extern Int VGOFF_(helper_CLD); |
| 1189 | extern Int VGOFF_(helper_STD); |
| 1190 | extern Int VGOFF_(helper_get_dirflag); |
| 1191 | |
| 1192 | extern Int VGOFF_(helper_CLC); |
| 1193 | extern Int VGOFF_(helper_STC); |
| 1194 | |
| 1195 | extern Int VGOFF_(helper_shldl); |
| 1196 | extern Int VGOFF_(helper_shldw); |
| 1197 | extern Int VGOFF_(helper_shrdl); |
| 1198 | extern Int VGOFF_(helper_shrdw); |
| 1199 | |
| 1200 | extern Int VGOFF_(helper_RDTSC); |
| 1201 | extern Int VGOFF_(helper_CPUID); |
| 1202 | |
daywalker | b18d253 | 2003-09-27 20:15:01 +0000 | [diff] [blame] | 1203 | extern Int VGOFF_(helper_IN); |
| 1204 | extern Int VGOFF_(helper_OUT); |
| 1205 | |
njn | 211b6ad | 2003-02-03 12:33:31 +0000 | [diff] [blame] | 1206 | extern Int VGOFF_(helper_bsf); |
| 1207 | extern Int VGOFF_(helper_bsr); |
| 1208 | |
| 1209 | extern Int VGOFF_(helper_fstsw_AX); |
| 1210 | extern Int VGOFF_(helper_SAHF); |
njn | d6251f1 | 2003-06-03 13:38:51 +0000 | [diff] [blame] | 1211 | extern Int VGOFF_(helper_LAHF); |
njn | 211b6ad | 2003-02-03 12:33:31 +0000 | [diff] [blame] | 1212 | extern Int VGOFF_(helper_DAS); |
| 1213 | extern Int VGOFF_(helper_DAA); |
| 1214 | |
mueller | f217c73 | 2004-01-02 22:42:29 +0000 | [diff] [blame] | 1215 | extern Int VGOFF_(helper_cmpxchg8b); |
| 1216 | |
njn | 211b6ad | 2003-02-03 12:33:31 +0000 | [diff] [blame] | 1217 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1218 | /*====================================================================*/ |
njn | d5bb0a5 | 2002-09-27 10:24:48 +0000 | [diff] [blame] | 1219 | /*=== Generating x86 code from UCode ===*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1220 | /*====================================================================*/ |
| 1221 | |
njn | d5bb0a5 | 2002-09-27 10:24:48 +0000 | [diff] [blame] | 1222 | /* All this only necessary for skins with VG_(needs).extends_UCode == True. */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1223 | |
sewardj | e104247 | 2002-09-30 12:33:11 +0000 | [diff] [blame] | 1224 | /* This is the Intel register encoding -- integer regs. */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1225 | #define R_EAX 0 |
| 1226 | #define R_ECX 1 |
| 1227 | #define R_EDX 2 |
| 1228 | #define R_EBX 3 |
| 1229 | #define R_ESP 4 |
| 1230 | #define R_EBP 5 |
| 1231 | #define R_ESI 6 |
| 1232 | #define R_EDI 7 |
| 1233 | |
| 1234 | #define R_AL (0+R_EAX) |
| 1235 | #define R_CL (0+R_ECX) |
| 1236 | #define R_DL (0+R_EDX) |
| 1237 | #define R_BL (0+R_EBX) |
| 1238 | #define R_AH (4+R_EAX) |
| 1239 | #define R_CH (4+R_ECX) |
| 1240 | #define R_DH (4+R_EDX) |
| 1241 | #define R_BH (4+R_EBX) |
| 1242 | |
sewardj | e104247 | 2002-09-30 12:33:11 +0000 | [diff] [blame] | 1243 | /* This is the Intel register encoding -- segment regs. */ |
| 1244 | #define R_ES 0 |
| 1245 | #define R_CS 1 |
| 1246 | #define R_SS 2 |
| 1247 | #define R_DS 3 |
| 1248 | #define R_FS 4 |
| 1249 | #define R_GS 5 |
| 1250 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1251 | /* For pretty printing x86 code */ |
daywalker | 5d945de | 2003-09-26 00:32:53 +0000 | [diff] [blame] | 1252 | extern const Char* VG_(name_of_mmx_gran) ( UChar gran ); |
| 1253 | extern const Char* VG_(name_of_mmx_reg) ( Int mmxreg ); |
| 1254 | extern const Char* VG_(name_of_seg_reg) ( Int sreg ); |
| 1255 | extern const Char* VG_(name_of_int_reg) ( Int size, Int reg ); |
| 1256 | extern const Char VG_(name_of_int_size) ( Int size ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1257 | |
njn | ac6c176 | 2002-10-04 14:34:15 +0000 | [diff] [blame] | 1258 | /* Shorter macros for convenience */ |
sewardj | 3d7c9c8 | 2003-03-26 21:08:13 +0000 | [diff] [blame] | 1259 | #define nameIReg VG_(name_of_int_reg) |
| 1260 | #define nameISize VG_(name_of_int_size) |
| 1261 | #define nameSReg VG_(name_of_seg_reg) |
| 1262 | #define nameMMXReg VG_(name_of_mmx_reg) |
| 1263 | #define nameMMXGran VG_(name_of_mmx_gran) |
sewardj | febaa3b | 2003-05-25 01:07:34 +0000 | [diff] [blame] | 1264 | #define nameXMMReg VG_(name_of_xmm_reg) |
njn | ac6c176 | 2002-10-04 14:34:15 +0000 | [diff] [blame] | 1265 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1266 | /* Randomly useful things */ |
| 1267 | extern UInt VG_(extend_s_8to32) ( UInt x ); |
| 1268 | |
| 1269 | /* Code emitters */ |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 1270 | extern void VG_(emitB) ( UInt b ); |
| 1271 | extern void VG_(emitW) ( UInt w ); |
| 1272 | extern void VG_(emitL) ( UInt l ); |
sewardj | fa492d4 | 2002-12-08 18:20:01 +0000 | [diff] [blame] | 1273 | extern void VG_(new_emit) ( Bool upd_cc, FlagSet uses_flags, FlagSet sets_flags ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1274 | |
| 1275 | /* Finding offsets */ |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 1276 | extern Int VG_(helper_offset) ( Addr a ); |
| 1277 | extern Int VG_(shadow_reg_offset) ( Int arch ); |
| 1278 | extern Int VG_(shadow_flags_offset) ( void ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1279 | |
njn | d5bb0a5 | 2002-09-27 10:24:48 +0000 | [diff] [blame] | 1280 | /* Convert reg ranks <-> Intel register ordering, for using register |
| 1281 | liveness information. */ |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 1282 | extern Int VG_(realreg_to_rank) ( Int realreg ); |
| 1283 | extern Int VG_(rank_to_realreg) ( Int rank ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1284 | |
njn | d5bb0a5 | 2002-09-27 10:24:48 +0000 | [diff] [blame] | 1285 | /* Call a subroutine. Does no argument passing, stack manipulations, etc. */ |
daywalker | 5d945de | 2003-09-26 00:32:53 +0000 | [diff] [blame] | 1286 | extern void VG_(synth_call) ( Bool ensure_shortform, Int word_offset, |
sewardj | fa492d4 | 2002-12-08 18:20:01 +0000 | [diff] [blame] | 1287 | Bool upd_cc, FlagSet use_flags, FlagSet set_flags ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1288 | |
njn | d5bb0a5 | 2002-09-27 10:24:48 +0000 | [diff] [blame] | 1289 | /* For calling C functions -- saves caller save regs, pushes args, calls, |
| 1290 | clears the stack, restores caller save regs. `fn' must be registered in |
| 1291 | the baseBlock first. Acceptable tags are RealReg and Literal. Optimises |
| 1292 | things, eg. by not preserving non-live caller-save registers. |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1293 | |
njn | d5bb0a5 | 2002-09-27 10:24:48 +0000 | [diff] [blame] | 1294 | WARNING: a UInstr should *not* be translated with synth_ccall() followed |
| 1295 | by some other x86 assembly code; this will invalidate the results of |
| 1296 | vg_realreg_liveness_analysis() and everything will fall over. */ |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 1297 | extern void VG_(synth_ccall) ( Addr fn, Int argc, Int regparms_n, UInt argv[], |
daywalker | 5d945de | 2003-09-26 00:32:53 +0000 | [diff] [blame] | 1298 | Tag tagv[], Int ret_reg, |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 1299 | RRegSet regs_live_before, |
| 1300 | RRegSet regs_live_after ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1301 | |
| 1302 | /* Addressing modes */ |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 1303 | extern void VG_(emit_amode_offregmem_reg)( Int off, Int regmem, Int reg ); |
| 1304 | extern void VG_(emit_amode_ereg_greg) ( Int e_reg, Int g_reg ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1305 | |
| 1306 | /* v-size (4, or 2 with OSO) insn emitters */ |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 1307 | extern void VG_(emit_movv_offregmem_reg) ( Int sz, Int off, Int areg, Int reg ); |
| 1308 | extern void VG_(emit_movv_reg_offregmem) ( Int sz, Int reg, Int off, Int areg ); |
| 1309 | extern void VG_(emit_movv_reg_reg) ( Int sz, Int reg1, Int reg2 ); |
sewardj | fa492d4 | 2002-12-08 18:20:01 +0000 | [diff] [blame] | 1310 | extern void VG_(emit_nonshiftopv_lit_reg)( Bool upd_cc, Int sz, Opcode opc, UInt lit, |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 1311 | Int reg ); |
sewardj | fa492d4 | 2002-12-08 18:20:01 +0000 | [diff] [blame] | 1312 | extern void VG_(emit_shiftopv_lit_reg) ( Bool upd_cc, Int sz, Opcode opc, UInt lit, |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 1313 | Int reg ); |
sewardj | fa492d4 | 2002-12-08 18:20:01 +0000 | [diff] [blame] | 1314 | extern void VG_(emit_nonshiftopv_reg_reg)( Bool upd_cc, Int sz, Opcode opc, |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 1315 | Int reg1, Int reg2 ); |
| 1316 | extern void VG_(emit_movv_lit_reg) ( Int sz, UInt lit, Int reg ); |
sewardj | fa492d4 | 2002-12-08 18:20:01 +0000 | [diff] [blame] | 1317 | extern void VG_(emit_unaryopv_reg) ( Bool upd_cc, Int sz, Opcode opc, Int reg ); |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 1318 | extern void VG_(emit_pushv_reg) ( Int sz, Int reg ); |
| 1319 | extern void VG_(emit_popv_reg) ( Int sz, Int reg ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1320 | |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 1321 | extern void VG_(emit_pushl_lit32) ( UInt int32 ); |
| 1322 | extern void VG_(emit_pushl_lit8) ( Int lit8 ); |
sewardj | fa492d4 | 2002-12-08 18:20:01 +0000 | [diff] [blame] | 1323 | extern void VG_(emit_cmpl_zero_reg) ( Bool upd_cc, Int reg ); |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 1324 | extern void VG_(emit_swapl_reg_EAX) ( Int reg ); |
| 1325 | extern void VG_(emit_movv_lit_offregmem) ( Int sz, UInt lit, Int off, |
| 1326 | Int memreg ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1327 | |
| 1328 | /* b-size (1 byte) instruction emitters */ |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 1329 | extern void VG_(emit_movb_lit_offregmem) ( UInt lit, Int off, Int memreg ); |
| 1330 | extern void VG_(emit_movb_reg_offregmem) ( Int reg, Int off, Int areg ); |
sewardj | fa492d4 | 2002-12-08 18:20:01 +0000 | [diff] [blame] | 1331 | extern void VG_(emit_unaryopb_reg) ( Bool upd_cc, Opcode opc, Int reg ); |
| 1332 | extern void VG_(emit_testb_lit_reg) ( Bool upd_cc, UInt lit, Int reg ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1333 | |
| 1334 | /* zero-extended load emitters */ |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1335 | extern void VG_(emit_movzbl_offregmem_reg) ( Bool bounds, Int off, Int regmem, Int reg ); |
| 1336 | extern void VG_(emit_movzwl_offregmem_reg) ( Bool bounds, Int off, Int areg, Int reg ); |
| 1337 | extern void VG_(emit_movzwl_regmem_reg) ( Bool bounds, Int reg1, Int reg2 ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1338 | |
| 1339 | /* misc instruction emitters */ |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 1340 | extern void VG_(emit_call_reg) ( Int reg ); |
| 1341 | extern void VG_(emit_add_lit_to_esp) ( Int lit ); |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 1342 | extern void VG_(emit_pushal) ( void ); |
| 1343 | extern void VG_(emit_popal) ( void ); |
| 1344 | extern void VG_(emit_AMD_prefetch_reg) ( Int reg ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1345 | |
sewardj | a2113f9 | 2002-12-12 23:42:48 +0000 | [diff] [blame] | 1346 | /* jump emitters */ |
| 1347 | extern void VG_(init_target) ( Int *tgt ); |
| 1348 | |
| 1349 | extern void VG_(target_back) ( Int *tgt ); |
| 1350 | extern void VG_(target_forward) ( Int *tgt ); |
| 1351 | extern void VG_(emit_target_delta) ( Int *tgt ); |
| 1352 | |
fitzhardinge | 462f4f9 | 2003-12-18 02:10:54 +0000 | [diff] [blame] | 1353 | typedef enum { |
| 1354 | JP_NONE, /* no prediction */ |
| 1355 | JP_TAKEN, /* predict taken */ |
| 1356 | JP_NOT_TAKEN, /* predict not taken */ |
| 1357 | } JumpPred; |
| 1358 | |
| 1359 | extern void VG_(emit_jcondshort_delta) ( Bool simd_cc, Condcode cond, Int delta, JumpPred ); |
| 1360 | extern void VG_(emit_jcondshort_target)( Bool simd_cc, Condcode cond, Int *tgt, JumpPred ); |
sewardj | a2113f9 | 2002-12-12 23:42:48 +0000 | [diff] [blame] | 1361 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1362 | |
| 1363 | /*====================================================================*/ |
| 1364 | /*=== Execution contexts ===*/ |
| 1365 | /*====================================================================*/ |
| 1366 | |
| 1367 | /* Generic resolution type used in a few different ways, such as deciding |
| 1368 | how closely to compare two errors for equality. */ |
daywalker | 5d945de | 2003-09-26 00:32:53 +0000 | [diff] [blame] | 1369 | typedef |
| 1370 | enum { Vg_LowRes, Vg_MedRes, Vg_HighRes } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1371 | VgRes; |
| 1372 | |
| 1373 | typedef |
| 1374 | struct _ExeContext |
| 1375 | ExeContext; |
| 1376 | |
daywalker | 5d945de | 2003-09-26 00:32:53 +0000 | [diff] [blame] | 1377 | /* Compare two ExeContexts. Number of callers considered depends on `res': |
| 1378 | Vg_LowRes: 2 |
| 1379 | Vg_MedRes: 4 |
njn | d5bb0a5 | 2002-09-27 10:24:48 +0000 | [diff] [blame] | 1380 | Vg_HighRes: all */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1381 | extern Bool VG_(eq_ExeContext) ( VgRes res, |
| 1382 | ExeContext* e1, ExeContext* e2 ); |
| 1383 | |
| 1384 | /* Print an ExeContext. */ |
| 1385 | extern void VG_(pp_ExeContext) ( ExeContext* ); |
| 1386 | |
| 1387 | /* Take a snapshot of the client's stack. Search our collection of |
| 1388 | ExeContexts to see if we already have it, and if not, allocate a |
njn | 6c84655 | 2003-09-16 07:41:43 +0000 | [diff] [blame] | 1389 | new one. Either way, return a pointer to the context. Context size |
| 1390 | controlled by --num-callers option. |
daywalker | 5d945de | 2003-09-26 00:32:53 +0000 | [diff] [blame] | 1391 | |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 1392 | If called from generated code, use VG_(get_current_tid)() to get the |
| 1393 | current ThreadId. If called from non-generated code, the current |
daywalker | 5d945de | 2003-09-26 00:32:53 +0000 | [diff] [blame] | 1394 | ThreadId should be passed in by the core. |
njn | 7b456f5 | 2003-05-19 11:16:50 +0000 | [diff] [blame] | 1395 | */ |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 1396 | extern ExeContext* VG_(get_ExeContext) ( ThreadId tid ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1397 | |
njn | 6c84655 | 2003-09-16 07:41:43 +0000 | [diff] [blame] | 1398 | /* Get the nth EIP from the ExeContext. 0 is the EIP of the top function, 1 |
| 1399 | is its caller, etc. Returns 0 if there isn't one, or if n is greater |
| 1400 | than VG_(clo_backtrace_size), set by the --num-callers option. */ |
| 1401 | extern Addr VG_(get_EIP_from_ExeContext) ( ExeContext* e, UInt n ); |
| 1402 | |
sewardj | 499e3de | 2002-11-13 22:22:25 +0000 | [diff] [blame] | 1403 | /* Just grab the client's EIP, as a much smaller and cheaper |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 1404 | indication of where they are. Use is basically same as for |
daywalker | 5d945de | 2003-09-26 00:32:53 +0000 | [diff] [blame] | 1405 | VG_(get_ExeContext)() above. |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 1406 | */ |
| 1407 | extern Addr VG_(get_EIP)( ThreadId tid ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1408 | |
njn | 6c84655 | 2003-09-16 07:41:43 +0000 | [diff] [blame] | 1409 | /* For skins needing more control over stack traces: walks the stack to get |
| 1410 | %eips from the top stack frames for thread 'tid'. Maximum of 'n_eips' |
| 1411 | addresses put into 'eips'; 0 is the top of the stack, 1 is its caller, |
| 1412 | etc. */ |
| 1413 | extern UInt VG_(stack_snapshot) ( ThreadId tid, Addr* eips, UInt n_eips ); |
| 1414 | |
| 1415 | /* Does the same thing as VG_(pp_ExeContext)(), just with slightly |
| 1416 | different input. */ |
| 1417 | extern void VG_(mini_stack_dump) ( Addr eips[], UInt n_eips ); |
| 1418 | |
njn | 211b6ad | 2003-02-03 12:33:31 +0000 | [diff] [blame] | 1419 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1420 | /*====================================================================*/ |
| 1421 | /*=== Error reporting ===*/ |
| 1422 | /*====================================================================*/ |
| 1423 | |
| 1424 | /* ------------------------------------------------------------------ */ |
daywalker | 5d945de | 2003-09-26 00:32:53 +0000 | [diff] [blame] | 1425 | /* Suppressions describe errors which we want to suppress, ie, not |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1426 | show the user, usually because it is caused by a problem in a library |
daywalker | 5d945de | 2003-09-26 00:32:53 +0000 | [diff] [blame] | 1427 | which we can't fix, replace or work around. Suppressions are read from |
njn | d5bb0a5 | 2002-09-27 10:24:48 +0000 | [diff] [blame] | 1428 | a file at startup time. This gives flexibility so that new |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1429 | suppressions can be added to the file as and when needed. |
| 1430 | */ |
| 1431 | |
| 1432 | typedef |
| 1433 | Int /* Do not make this unsigned! */ |
| 1434 | SuppKind; |
| 1435 | |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 1436 | /* The skin-relevant parts of a suppression are: |
| 1437 | kind: what kind of suppression; must be in the range (0..) |
| 1438 | string: use is optional. NULL by default. |
| 1439 | extra: use is optional. NULL by default. void* so it's extensible. |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1440 | */ |
| 1441 | typedef |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 1442 | struct _Supp |
| 1443 | Supp; |
| 1444 | |
| 1445 | /* Useful in SK_(error_matches_suppression)() */ |
| 1446 | SuppKind VG_(get_supp_kind) ( Supp* su ); |
| 1447 | Char* VG_(get_supp_string) ( Supp* su ); |
| 1448 | void* VG_(get_supp_extra) ( Supp* su ); |
| 1449 | |
| 1450 | /* Must be used in VG_(recognised_suppression)() */ |
| 1451 | void VG_(set_supp_kind) ( Supp* su, SuppKind suppkind ); |
| 1452 | /* May be used in VG_(read_extra_suppression_info)() */ |
| 1453 | void VG_(set_supp_string) ( Supp* su, Char* string ); |
| 1454 | void VG_(set_supp_extra) ( Supp* su, void* extra ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1455 | |
| 1456 | |
| 1457 | /* ------------------------------------------------------------------ */ |
| 1458 | /* Error records contain enough info to generate an error report. The idea |
| 1459 | is that (typically) the same few points in the program generate thousands |
njn | d5bb0a5 | 2002-09-27 10:24:48 +0000 | [diff] [blame] | 1460 | of errors, and we don't want to spew out a fresh error message for each |
| 1461 | one. Instead, we use these structures to common up duplicates. |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1462 | */ |
| 1463 | |
| 1464 | typedef |
| 1465 | Int /* Do not make this unsigned! */ |
| 1466 | ErrorKind; |
| 1467 | |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 1468 | /* The skin-relevant parts of an Error are: |
| 1469 | kind: what kind of error; must be in the range (0..) |
| 1470 | addr: use is optional. 0 by default. |
| 1471 | string: use is optional. NULL by default. |
| 1472 | extra: use is optional. NULL by default. void* so it's extensible. |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1473 | */ |
| 1474 | typedef |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 1475 | struct _Error |
| 1476 | Error; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1477 | |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 1478 | /* Useful in SK_(error_matches_suppression)(), SK_(pp_SkinError)(), etc */ |
njn | b877d49 | 2003-01-28 20:40:57 +0000 | [diff] [blame] | 1479 | ExeContext* VG_(get_error_where) ( Error* err ); |
| 1480 | SuppKind VG_(get_error_kind) ( Error* err ); |
| 1481 | Addr VG_(get_error_address) ( Error* err ); |
| 1482 | Char* VG_(get_error_string) ( Error* err ); |
| 1483 | void* VG_(get_error_extra) ( Error* err ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1484 | |
njn | d5bb0a5 | 2002-09-27 10:24:48 +0000 | [diff] [blame] | 1485 | /* Call this when an error occurs. It will be recorded if it hasn't been |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1486 | seen before. If it has, the existing error record will have its count |
daywalker | 5d945de | 2003-09-26 00:32:53 +0000 | [diff] [blame] | 1487 | incremented. |
| 1488 | |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 1489 | 'tid' can be found as for VG_(get_ExeContext)(). The `extra' field can |
| 1490 | be stack-allocated; it will be copied by the core if needed (but it |
| 1491 | won't be copied if it's NULL). |
njn | d5bb0a5 | 2002-09-27 10:24:48 +0000 | [diff] [blame] | 1492 | |
| 1493 | If no 'a', 's' or 'extra' of interest needs to be recorded, just use |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1494 | NULL for them. */ |
daywalker | 5d945de | 2003-09-26 00:32:53 +0000 | [diff] [blame] | 1495 | extern void VG_(maybe_record_error) ( ThreadId tid, ErrorKind ekind, |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1496 | Addr a, Char* s, void* extra ); |
| 1497 | |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 1498 | /* Similar to VG_(maybe_record_error)(), except this one doesn't record the |
| 1499 | error -- useful for errors that can only happen once. The errors can be |
| 1500 | suppressed, though. Return value is True if it was suppressed. |
daywalker | 5d945de | 2003-09-26 00:32:53 +0000 | [diff] [blame] | 1501 | `print_error' dictates whether to print the error, which is a bit of a |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 1502 | hack that's useful sometimes if you just want to know if the error would |
daywalker | 5d945de | 2003-09-26 00:32:53 +0000 | [diff] [blame] | 1503 | be suppressed without possibly printing it. `count_error' dictates |
njn | 47363ab | 2003-04-21 13:24:40 +0000 | [diff] [blame] | 1504 | whether to add the error in the error total count (another mild hack). */ |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 1505 | extern Bool VG_(unique_error) ( ThreadId tid, ErrorKind ekind, |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 1506 | Addr a, Char* s, void* extra, |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1507 | ExeContext* where, Bool print_error, |
njn | 47363ab | 2003-04-21 13:24:40 +0000 | [diff] [blame] | 1508 | Bool allow_GDB_attach, Bool count_error ); |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 1509 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1510 | /* Gets a non-blank, non-comment line of at most nBuf chars from fd. |
daywalker | 5d945de | 2003-09-26 00:32:53 +0000 | [diff] [blame] | 1511 | Skips leading spaces on the line. Returns True if EOF was hit instead. |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1512 | Useful for reading in extra skin-specific suppression lines. */ |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 1513 | extern Bool VG_(get_line) ( Int fd, Char* buf, Int nBuf ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1514 | |
| 1515 | |
| 1516 | /*====================================================================*/ |
| 1517 | /*=== Obtaining debug information ===*/ |
| 1518 | /*====================================================================*/ |
| 1519 | |
sewardj | 6e008cb | 2002-12-15 13:11:39 +0000 | [diff] [blame] | 1520 | /* Get the file/function/line number of the instruction at address |
| 1521 | 'a'. For these four, if debug info for the address is found, it |
| 1522 | copies the info into the buffer/UInt and returns True. If not, it |
| 1523 | returns False and nothing is copied. VG_(get_fnname) always |
| 1524 | demangles C++ function names. VG_(get_fnname_w_offset) is the |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1525 | same, except it appends "+N" to symbol names to indicate offsets. */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1526 | extern Bool VG_(get_filename) ( Addr a, Char* filename, Int n_filename ); |
| 1527 | extern Bool VG_(get_fnname) ( Addr a, Char* fnname, Int n_fnname ); |
| 1528 | extern Bool VG_(get_linenum) ( Addr a, UInt* linenum ); |
daywalker | 5d945de | 2003-09-26 00:32:53 +0000 | [diff] [blame] | 1529 | extern Bool VG_(get_fnname_w_offset) |
sewardj | 6e008cb | 2002-12-15 13:11:39 +0000 | [diff] [blame] | 1530 | ( Addr a, Char* fnname, Int n_fnname ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1531 | |
| 1532 | /* This one is more efficient if getting both filename and line number, |
| 1533 | because the two lookups are done together. */ |
daywalker | 5d945de | 2003-09-26 00:32:53 +0000 | [diff] [blame] | 1534 | extern Bool VG_(get_filename_linenum) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1535 | ( Addr a, Char* filename, Int n_filename, |
| 1536 | UInt* linenum ); |
| 1537 | |
| 1538 | /* Succeeds only if we find from debug info that 'a' is the address of the |
| 1539 | first instruction in a function -- as opposed to VG_(get_fnname) which |
| 1540 | succeeds if we find from debug info that 'a' is the address of any |
| 1541 | instruction in a function. Use this to instrument the start of |
njn | d5bb0a5 | 2002-09-27 10:24:48 +0000 | [diff] [blame] | 1542 | a particular function. Nb: if an executable/shared object is stripped |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1543 | of its symbols, this function will not be able to recognise function |
| 1544 | entry points within it. */ |
njn | 497ec3f | 2003-05-19 08:38:23 +0000 | [diff] [blame] | 1545 | extern Bool VG_(get_fnname_if_entry) ( Addr a, Char* fnname, Int n_fnname ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1546 | |
| 1547 | /* Succeeds if the address is within a shared object or the main executable. |
| 1548 | It doesn't matter if debug info is present or not. */ |
| 1549 | extern Bool VG_(get_objname) ( Addr a, Char* objname, Int n_objname ); |
| 1550 | |
njn | 6c84655 | 2003-09-16 07:41:43 +0000 | [diff] [blame] | 1551 | /* Puts into 'buf' info about the code address %eip: the address, function |
| 1552 | name (if known) and filename/line number (if known), like this: |
| 1553 | |
| 1554 | 0x4001BF05: realloc (vg_replace_malloc.c:339) |
| 1555 | |
| 1556 | 'n_buf' gives length of 'buf'. Returns 'buf'. |
| 1557 | */ |
| 1558 | extern Char* VG_(describe_eip)(Addr eip, Char* buf, Int n_buf); |
| 1559 | |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 1560 | /* Returns a string containing an expression for the given |
| 1561 | address. String is malloced with VG_(malloc)() */ |
| 1562 | Char *VG_(describe_addr)(ThreadId, Addr); |
| 1563 | |
sewardj | 4710438 | 2002-10-20 18:35:48 +0000 | [diff] [blame] | 1564 | /* A way to get information about what segments are mapped */ |
| 1565 | typedef struct _SegInfo SegInfo; |
| 1566 | |
njn | b877d49 | 2003-01-28 20:40:57 +0000 | [diff] [blame] | 1567 | /* Returns NULL if the SegInfo isn't found. It doesn't matter if debug info |
| 1568 | is present or not. */ |
| 1569 | extern SegInfo* VG_(get_obj) ( Addr a ); |
| 1570 | |
| 1571 | extern const SegInfo* VG_(next_seginfo) ( const SegInfo *seg ); |
| 1572 | extern Addr VG_(seg_start) ( const SegInfo *seg ); |
| 1573 | extern UInt VG_(seg_size) ( const SegInfo *seg ); |
| 1574 | extern const UChar* VG_(seg_filename) ( const SegInfo *seg ); |
| 1575 | extern UInt VG_(seg_sym_offset)( const SegInfo *seg ); |
sewardj | 4710438 | 2002-10-20 18:35:48 +0000 | [diff] [blame] | 1576 | |
| 1577 | typedef |
| 1578 | enum { |
| 1579 | Vg_SectUnknown, |
| 1580 | Vg_SectText, |
sewardj | 8fe15a3 | 2002-10-20 19:29:21 +0000 | [diff] [blame] | 1581 | Vg_SectData, |
| 1582 | Vg_SectBSS, |
sewardj | 4710438 | 2002-10-20 18:35:48 +0000 | [diff] [blame] | 1583 | Vg_SectGOT, |
sewardj | 8fe15a3 | 2002-10-20 19:29:21 +0000 | [diff] [blame] | 1584 | Vg_SectPLT, |
sewardj | 4710438 | 2002-10-20 18:35:48 +0000 | [diff] [blame] | 1585 | } |
| 1586 | VgSectKind; |
| 1587 | |
| 1588 | extern VgSectKind VG_(seg_sect_kind)(Addr); |
| 1589 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1590 | |
| 1591 | /*====================================================================*/ |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1592 | /*=== Generic hash table ===*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1593 | /*====================================================================*/ |
| 1594 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1595 | /* Generic type for a separately-chained hash table. Via a kind of dodgy |
| 1596 | C-as-C++ style inheritance, skins can extend the VgHashNode type, so long |
| 1597 | as the first two fields match the sizes of these two fields. Requires |
| 1598 | a bit of casting by the skin. */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1599 | typedef |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1600 | struct _VgHashNode { |
| 1601 | struct _VgHashNode * next; |
| 1602 | UInt key; |
| 1603 | } |
| 1604 | VgHashNode; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1605 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1606 | typedef |
| 1607 | VgHashNode** |
| 1608 | VgHashTable; |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 1609 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1610 | /* Make a new table. */ |
| 1611 | extern VgHashTable VG_(HT_construct) ( void ); |
| 1612 | |
njn | 69c0687 | 2003-09-30 15:43:51 +0000 | [diff] [blame] | 1613 | /* Count the number of nodes in a table. */ |
| 1614 | extern Int VG_(HT_count_nodes) ( VgHashTable table ); |
| 1615 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1616 | /* Add a node to the table. */ |
| 1617 | extern void VG_(HT_add_node) ( VgHashTable t, VgHashNode* node ); |
| 1618 | |
daywalker | 5d945de | 2003-09-26 00:32:53 +0000 | [diff] [blame] | 1619 | /* Looks up a node in the hash table. Also returns the address of the |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1620 | previous node's `next' pointer which allows it to be removed from the |
| 1621 | list later without having to look it up again. */ |
| 1622 | extern VgHashNode* VG_(HT_get_node) ( VgHashTable t, UInt key, |
| 1623 | /*OUT*/VgHashNode*** next_ptr ); |
| 1624 | |
njn | 06072ec | 2003-09-30 15:35:13 +0000 | [diff] [blame] | 1625 | /* Allocates an array of pointers to all the shadow chunks of malloc'd |
| 1626 | blocks. Must be freed with VG_(free)(). */ |
| 1627 | extern VgHashNode** VG_(HT_to_array) ( VgHashTable t, /*OUT*/ UInt* n_shadows ); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1628 | |
| 1629 | /* Returns first node that matches predicate `p', or NULL if none do. |
| 1630 | Extra arguments can be implicitly passed to `p' using nested functions; |
| 1631 | see memcheck/mc_errcontext.c for an example. */ |
| 1632 | extern VgHashNode* VG_(HT_first_match) ( VgHashTable t, |
| 1633 | Bool (*p)(VgHashNode*) ); |
| 1634 | |
| 1635 | /* Applies a function f() once to each node. Again, nested functions |
| 1636 | can be very useful. */ |
| 1637 | extern void VG_(HT_apply_to_all_nodes)( VgHashTable t, void (*f)(VgHashNode*) ); |
| 1638 | |
| 1639 | /* Destroy a table. */ |
| 1640 | extern void VG_(HT_destruct) ( VgHashTable t ); |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 1641 | |
| 1642 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1643 | /*====================================================================*/ |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1644 | /*=== A generic skiplist ===*/ |
| 1645 | /*====================================================================*/ |
| 1646 | |
| 1647 | /* |
| 1648 | The idea here is that the skiplist puts its per-element data at the |
| 1649 | end of the structure. When you initialize the skiplist, you tell |
| 1650 | it what structure your list elements are going to be. Then you |
| 1651 | should allocate them with VG_(SkipNode_Alloc), which will allocate |
| 1652 | enough memory for the extra bits. |
| 1653 | */ |
| 1654 | #include <stddef.h> /* for offsetof */ |
| 1655 | |
| 1656 | typedef struct _SkipList SkipList; |
| 1657 | typedef struct _SkipNode SkipNode; |
| 1658 | |
| 1659 | typedef Int (*SkipCmp_t)(const void *key1, const void *key2); |
| 1660 | |
| 1661 | struct _SkipList { |
| 1662 | const Short arena; /* allocation arena */ |
| 1663 | const UShort size; /* structure size (not including SkipNode) */ |
| 1664 | const UShort keyoff; /* key offset */ |
| 1665 | const SkipCmp_t cmp; /* compare two keys */ |
| 1666 | Char * (*strkey)(void *); /* stringify a key (for debugging) */ |
| 1667 | SkipNode *head; /* list head */ |
| 1668 | }; |
| 1669 | |
| 1670 | /* Use this macro to initialize your skiplist head. The arguments are pretty self explanitory: |
| 1671 | _type is the type of your element structure |
| 1672 | _key is the field within that type which you want to use as the key |
| 1673 | _cmp is the comparison function for keys - it gets two typeof(_key) pointers as args |
| 1674 | _strkey is a function which can return a string of your key - it's only used for debugging |
| 1675 | _arena is the arena to use for allocation - -1 is the default |
| 1676 | */ |
| 1677 | #define SKIPLIST_INIT(_type, _key, _cmp, _strkey, _arena) \ |
| 1678 | { \ |
| 1679 | .arena = _arena, \ |
| 1680 | .size = sizeof(_type), \ |
| 1681 | .keyoff = offsetof(_type, _key), \ |
| 1682 | .cmp = _cmp, \ |
| 1683 | .strkey = _strkey, \ |
| 1684 | .head = NULL, \ |
| 1685 | } |
| 1686 | |
| 1687 | /* List operations: |
| 1688 | SkipList_Find searchs a list. If it can't find an exact match, it either returns NULL |
| 1689 | or a pointer to the element before where k would go |
| 1690 | SkipList_Insert inserts a new element into the list. Duplicates are forbidden. |
| 1691 | SkipList_Remove removes an element from the list and returns it. It doesn't free the memory. |
| 1692 | */ |
| 1693 | extern void *VG_(SkipList_Find) (const SkipList *l, void *key); |
| 1694 | extern void VG_(SkipList_Insert)( SkipList *l, void *data); |
| 1695 | extern void *VG_(SkipList_Remove)( SkipList *l, void *key); |
| 1696 | |
| 1697 | /* Node (element) operations: |
| 1698 | SkipNode_Alloc: allocate memory for a new element on the list |
| 1699 | SkipNode_Free: free memory allocated above |
| 1700 | SkipNode_First: return the first element on the list |
| 1701 | SkipNode_Next: return the next element after "data" on the list - |
| 1702 | NULL for none |
| 1703 | */ |
| 1704 | extern void *VG_(SkipNode_Alloc) (const SkipList *l); |
| 1705 | extern void VG_(SkipNode_Free) (const SkipList *l, void *p); |
| 1706 | extern void *VG_(SkipNode_First) (const SkipList *l); |
| 1707 | extern void *VG_(SkipNode_Next) (const SkipList *l, void *data); |
| 1708 | |
| 1709 | /*====================================================================*/ |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 1710 | /*=== Functions for shadow registers ===*/ |
| 1711 | /*====================================================================*/ |
| 1712 | |
| 1713 | /* Nb: make sure the shadow_regs 'need' is set before using these! */ |
| 1714 | |
| 1715 | /* This one lets you override the shadow of the return value register for a |
| 1716 | syscall. Call it from SK_(post_syscall)() (not SK_(pre_syscall)()!) to |
| 1717 | override the default shadow register value. */ |
daywalker | 5d945de | 2003-09-26 00:32:53 +0000 | [diff] [blame] | 1718 | extern void VG_(set_return_from_syscall_shadow) ( ThreadId tid, |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 1719 | UInt ret_shadow ); |
| 1720 | |
| 1721 | /* This can be called from SK_(fini)() to find the shadow of the argument |
| 1722 | to exit(), ie. the shadow of the program's return value. */ |
| 1723 | extern UInt VG_(get_exit_status_shadow) ( void ); |
| 1724 | |
| 1725 | |
| 1726 | /*====================================================================*/ |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1727 | /*=== General stuff for replacing functions ===*/ |
| 1728 | /*====================================================================*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1729 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1730 | /* Some skins need to replace the standard definitions of some functions. */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1731 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1732 | /* ------------------------------------------------------------------ */ |
| 1733 | /* General stuff, for replacing any functions */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1734 | |
daywalker | 5d945de | 2003-09-26 00:32:53 +0000 | [diff] [blame] | 1735 | /* Is the client running on the simulated CPU or the real one? |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1736 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1737 | Nb: If it is, and you want to call a function to be run on the real CPU, |
njn | 057c65f | 2003-04-21 13:30:55 +0000 | [diff] [blame] | 1738 | use one of the VALGRIND_NON_SIMD_CALL[123] macros in valgrind.h to call it. |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1739 | |
daywalker | 5d945de | 2003-09-26 00:32:53 +0000 | [diff] [blame] | 1740 | Nb: don't forget the function parentheses when using this in a |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1741 | condition... write this: |
| 1742 | |
| 1743 | if (VG_(is_running_on_simd_CPU)()) { ... } // calls function |
| 1744 | |
| 1745 | not this: |
daywalker | 5d945de | 2003-09-26 00:32:53 +0000 | [diff] [blame] | 1746 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1747 | if (VG_(is_running_on_simd_CPU)) { ... } // address of var! |
| 1748 | */ |
daywalker | 5d945de | 2003-09-26 00:32:53 +0000 | [diff] [blame] | 1749 | extern Bool VG_(is_running_on_simd_CPU) ( void ); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1750 | |
| 1751 | |
| 1752 | /*====================================================================*/ |
| 1753 | /*=== Specific stuff for replacing malloc() and friends ===*/ |
| 1754 | /*====================================================================*/ |
| 1755 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1756 | /* If a skin replaces malloc() et al, the easiest way to do so is to |
| 1757 | link with vg_replace_malloc.o into its vgpreload_*.so file, and |
| 1758 | follow the following instructions. You can do it from scratch, |
| 1759 | though, if you enjoy that sort of thing. */ |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1760 | |
| 1761 | /* Arena size for valgrind's own malloc(); default value is 0, but can |
| 1762 | be overridden by skin -- but must be done so *statically*, eg: |
daywalker | 5d945de | 2003-09-26 00:32:53 +0000 | [diff] [blame] | 1763 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1764 | Int VG_(vg_malloc_redzone_szB) = 4; |
daywalker | 5d945de | 2003-09-26 00:32:53 +0000 | [diff] [blame] | 1765 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1766 | It can't be done from a function like SK_(pre_clo_init)(). So it can't, |
| 1767 | for example, be controlled with a command line option, unfortunately. */ |
| 1768 | extern UInt VG_(vg_malloc_redzone_szB); |
| 1769 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1770 | /* Can be called from SK_(malloc) et al to do the actual alloc/freeing. */ |
daywalker | 5d945de | 2003-09-26 00:32:53 +0000 | [diff] [blame] | 1771 | extern void* VG_(cli_malloc) ( UInt align, Int nbytes ); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1772 | extern void VG_(cli_free) ( void* p ); |
| 1773 | |
| 1774 | /* Check if an address is within a range, allowing for redzones at edges */ |
| 1775 | extern Bool VG_(addr_is_in_block)( Addr a, Addr start, UInt size ); |
| 1776 | |
| 1777 | /* ------------------------------------------------------------------ */ |
daywalker | 5d945de | 2003-09-26 00:32:53 +0000 | [diff] [blame] | 1778 | /* Some options that can be used by a skin if malloc() et al are replaced. |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 1779 | The skin should call the functions in the appropriate places to give |
| 1780 | control over these aspects of Valgrind's version of malloc(). */ |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1781 | |
| 1782 | /* Round malloc sizes upwards to integral number of words? default: NO */ |
| 1783 | extern Bool VG_(clo_sloppy_malloc); |
| 1784 | /* DEBUG: print malloc details? default: NO */ |
| 1785 | extern Bool VG_(clo_trace_malloc); |
| 1786 | /* Minimum alignment in functions that don't specify alignment explicitly. |
| 1787 | default: 0, i.e. use default of the machine (== 4) */ |
| 1788 | extern Int VG_(clo_alignment); |
| 1789 | |
| 1790 | extern Bool VG_(replacement_malloc_process_cmd_line_option) ( Char* arg ); |
| 1791 | extern void VG_(replacement_malloc_print_usage) ( void ); |
| 1792 | extern void VG_(replacement_malloc_print_debug_usage) ( void ); |
sewardj | a449568 | 2002-10-21 07:29:59 +0000 | [diff] [blame] | 1793 | |
| 1794 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1795 | /*====================================================================*/ |
| 1796 | /*=== Skin-specific stuff ===*/ |
| 1797 | /*====================================================================*/ |
| 1798 | |
njn | d04b7c6 | 2002-10-03 14:05:52 +0000 | [diff] [blame] | 1799 | /* ------------------------------------------------------------------ */ |
| 1800 | /* Details */ |
njn | d04b7c6 | 2002-10-03 14:05:52 +0000 | [diff] [blame] | 1801 | |
njn | 120281f | 2003-02-03 12:20:07 +0000 | [diff] [blame] | 1802 | /* Default value for avg_translations_sizeB (in bytes), indicating typical |
| 1803 | code expansion of about 6:1. */ |
| 1804 | #define VG_DEFAULT_TRANS_SIZEB 100 |
| 1805 | |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 1806 | /* Information used in the startup message. `name' also determines the |
| 1807 | string used for identifying suppressions in a suppression file as |
| 1808 | belonging to this skin. `version' can be NULL, in which case (not |
| 1809 | surprisingly) no version info is printed; this mechanism is designed for |
| 1810 | skins distributed with Valgrind that share a version number with |
| 1811 | Valgrind. Other skins not distributed as part of Valgrind should |
sewardj | c0d8f68 | 2002-11-30 00:49:43 +0000 | [diff] [blame] | 1812 | probably have their own version number. */ |
| 1813 | extern void VG_(details_name) ( Char* name ); |
| 1814 | extern void VG_(details_version) ( Char* version ); |
| 1815 | extern void VG_(details_description) ( Char* description ); |
| 1816 | extern void VG_(details_copyright_author) ( Char* copyright_author ); |
| 1817 | |
| 1818 | /* Average size of a translation, in bytes, so that the translation |
njn | 120281f | 2003-02-03 12:20:07 +0000 | [diff] [blame] | 1819 | storage machinery can allocate memory appropriately. Not critical, |
daywalker | 5d945de | 2003-09-26 00:32:53 +0000 | [diff] [blame] | 1820 | setting is optional. */ |
njn | 120281f | 2003-02-03 12:20:07 +0000 | [diff] [blame] | 1821 | extern void VG_(details_avg_translation_sizeB) ( UInt size ); |
njn | d04b7c6 | 2002-10-03 14:05:52 +0000 | [diff] [blame] | 1822 | |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 1823 | /* String printed if an `sk_assert' assertion fails or VG_(skin_panic) |
| 1824 | is called. Should probably be an email address. */ |
| 1825 | extern void VG_(details_bug_reports_to) ( Char* bug_reports_to ); |
njn | d04b7c6 | 2002-10-03 14:05:52 +0000 | [diff] [blame] | 1826 | |
| 1827 | /* ------------------------------------------------------------------ */ |
| 1828 | /* Needs */ |
| 1829 | |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 1830 | /* Booleans that decide core behaviour, but don't require extra |
| 1831 | operations to be defined if `True' */ |
njn | d5bb0a5 | 2002-09-27 10:24:48 +0000 | [diff] [blame] | 1832 | |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 1833 | /* Should __libc_freeres() be run? Bugs in it can crash the skin. */ |
| 1834 | extern void VG_(needs_libc_freeres) ( void ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1835 | |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 1836 | /* Want to have errors detected by Valgrind's core reported? Includes: |
| 1837 | - pthread API errors (many; eg. unlocking a non-locked mutex) |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 1838 | - invalid file descriptors to blocking syscalls read() and write() |
| 1839 | - bad signal numbers passed to sigaction() |
daywalker | 5d945de | 2003-09-26 00:32:53 +0000 | [diff] [blame] | 1840 | - attempt to install signal handler for SIGKILL or SIGSTOP */ |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 1841 | extern void VG_(needs_core_errors) ( void ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1842 | |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 1843 | /* Booleans that indicate extra operations are defined; if these are True, |
| 1844 | the corresponding template functions (given below) must be defined. A |
| 1845 | lot like being a member of a type class. */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1846 | |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 1847 | /* Want to report errors from skin? This implies use of suppressions, too. */ |
| 1848 | extern void VG_(needs_skin_errors) ( void ); |
njn | d5bb0a5 | 2002-09-27 10:24:48 +0000 | [diff] [blame] | 1849 | |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 1850 | /* Is information kept about specific individual basic blocks? (Eg. for |
| 1851 | cachegrind there are cost-centres for every instruction, stored at a |
| 1852 | basic block level.) If so, it sometimes has to be discarded, because |
| 1853 | .so mmap/munmap-ping or self-modifying code (informed by the |
| 1854 | DISCARD_TRANSLATIONS user request) can cause one instruction address |
| 1855 | to be used for more than one instruction in one program run... */ |
| 1856 | extern void VG_(needs_basic_block_discards) ( void ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1857 | |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 1858 | /* Skin maintains information about each register? */ |
| 1859 | extern void VG_(needs_shadow_regs) ( void ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1860 | |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 1861 | /* Skin defines its own command line options? */ |
| 1862 | extern void VG_(needs_command_line_options) ( void ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1863 | |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 1864 | /* Skin defines its own client requests? */ |
| 1865 | extern void VG_(needs_client_requests) ( void ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1866 | |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 1867 | /* Skin defines its own UInstrs? */ |
| 1868 | extern void VG_(needs_extended_UCode) ( void ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1869 | |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 1870 | /* Skin does stuff before and/or after system calls? */ |
| 1871 | extern void VG_(needs_syscall_wrapper) ( void ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1872 | |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 1873 | /* Are skin-state sanity checks performed? */ |
| 1874 | extern void VG_(needs_sanity_checks) ( void ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1875 | |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 1876 | /* Do we need to see data symbols? */ |
| 1877 | extern void VG_(needs_data_syms) ( void ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1878 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1879 | /* Does the skin need shadow memory allocated (if you set this, you must also statically initialize |
| 1880 | float SK_(shadow_ratio) = n./m; |
| 1881 | to define how many shadow bits you need per client address space bit. |
| 1882 | */ |
| 1883 | extern void VG_(needs_shadow_memory)( void ); |
| 1884 | extern float SK_(shadow_ratio); |
| 1885 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1886 | /* ------------------------------------------------------------------ */ |
| 1887 | /* Core events to track */ |
| 1888 | |
| 1889 | /* Part of the core from which this call was made. Useful for determining |
njn | d5bb0a5 | 2002-09-27 10:24:48 +0000 | [diff] [blame] | 1890 | what kind of error message should be emitted. */ |
daywalker | 5d945de | 2003-09-26 00:32:53 +0000 | [diff] [blame] | 1891 | typedef |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1892 | enum { Vg_CorePThread, Vg_CoreSignal, Vg_CoreSysCall, Vg_CoreTranslate } |
| 1893 | CorePart; |
| 1894 | |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 1895 | /* Useful to use in VG_(get_Xreg_usage)() */ |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 1896 | #define VG_UINSTR_READS_REG(ono,regs,isWrites) \ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1897 | { if (mycat(u->tag,ono) == tag) \ |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 1898 | { regs[n] = mycat(u->val,ono); \ |
| 1899 | isWrites[n] = False; \ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1900 | n++; \ |
| 1901 | } \ |
| 1902 | } |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 1903 | #define VG_UINSTR_WRITES_REG(ono,regs,isWrites) \ |
njn | d5bb0a5 | 2002-09-27 10:24:48 +0000 | [diff] [blame] | 1904 | { if (mycat(u->tag,ono) == tag) \ |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 1905 | { regs[n] = mycat(u->val,ono); \ |
| 1906 | isWrites[n] = True; \ |
njn | d5bb0a5 | 2002-09-27 10:24:48 +0000 | [diff] [blame] | 1907 | n++; \ |
| 1908 | } \ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1909 | } |
| 1910 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1911 | #endif /* NDEF __VG_SKIN_H */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1912 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1913 | /* gen_toolint.pl will put the VG_(init_*)() functions here: */ |