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