njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1 | |
| 2 | /*--------------------------------------------------------------------*/ |
| 3 | /*--- The only header your skin will ever need to #include... ---*/ |
| 4 | /*--- vg_skin.h ---*/ |
| 5 | /*--------------------------------------------------------------------*/ |
| 6 | |
| 7 | /* |
| 8 | This file is part of Valgrind, an x86 protected-mode emulator |
| 9 | designed for debugging and profiling binaries on x86-Unixes. |
| 10 | |
| 11 | Copyright (C) 2000-2002 Julian Seward |
| 12 | jseward@acm.org |
| 13 | |
| 14 | This program is free software; you can redistribute it and/or |
| 15 | modify it under the terms of the GNU General Public License as |
| 16 | published by the Free Software Foundation; either version 2 of the |
| 17 | License, or (at your option) any later version. |
| 18 | |
| 19 | This program is distributed in the hope that it will be useful, but |
| 20 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 22 | General Public License for more details. |
| 23 | |
| 24 | You should have received a copy of the GNU General Public License |
| 25 | along with this program; if not, write to the Free Software |
| 26 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 27 | 02111-1307, USA. |
| 28 | |
| 29 | The GNU General Public License is contained in the file COPYING. |
| 30 | */ |
| 31 | |
| 32 | #ifndef __VG_SKIN_H |
| 33 | #define __VG_SKIN_H |
| 34 | |
| 35 | #include <stdarg.h> /* ANSI varargs stuff */ |
| 36 | #include <setjmp.h> /* for jmp_buf */ |
| 37 | |
| 38 | #include "vg_constants_skin.h" |
| 39 | |
| 40 | |
| 41 | /*====================================================================*/ |
| 42 | /*=== Build options and table sizes. ===*/ |
| 43 | /*====================================================================*/ |
| 44 | |
| 45 | /* You should be able to change these options or sizes, recompile, and |
| 46 | still have a working system. */ |
| 47 | |
| 48 | /* The maximum number of pthreads that we support. This is |
| 49 | deliberately not very high since our implementation of some of the |
| 50 | scheduler algorithms is surely O(N) in the number of threads, since |
| 51 | that's simple, at least. And (in practice) we hope that most |
| 52 | programs do not need many threads. */ |
| 53 | #define VG_N_THREADS 50 |
| 54 | |
| 55 | /* Maximum number of pthread keys available. Again, we start low until |
| 56 | the need for a higher number presents itself. */ |
| 57 | #define VG_N_THREAD_KEYS 50 |
| 58 | |
| 59 | /* Total number of integer registers available for allocation -- all of |
| 60 | them except %esp, %ebp. %ebp permanently points at VG_(baseBlock). |
| 61 | |
| 62 | If you change this you'll have to also change at least these: |
| 63 | - VG_(rankToRealRegNum)() |
| 64 | - VG_(realRegNumToRank)() |
| 65 | - ppRegsLiveness() |
| 66 | - the RegsLive type (maybe -- RegsLive type must have more than |
| 67 | VG_MAX_REALREGS bits) |
| 68 | |
| 69 | Do not change this unless you really know what you are doing! */ |
| 70 | #define VG_MAX_REALREGS 6 |
| 71 | |
| 72 | |
| 73 | /*====================================================================*/ |
| 74 | /*=== Basic types ===*/ |
| 75 | /*====================================================================*/ |
| 76 | |
| 77 | #define mycat_wrk(aaa,bbb) aaa##bbb |
| 78 | #define mycat(aaa,bbb) mycat_wrk(aaa,bbb) |
| 79 | |
| 80 | typedef unsigned char UChar; |
| 81 | typedef unsigned short UShort; |
| 82 | typedef unsigned int UInt; |
| 83 | typedef unsigned long long int ULong; |
| 84 | |
| 85 | typedef signed char Char; |
| 86 | typedef signed short Short; |
| 87 | typedef signed int Int; |
| 88 | typedef signed long long int Long; |
| 89 | |
| 90 | typedef unsigned int Addr; |
| 91 | |
| 92 | typedef unsigned char Bool; |
| 93 | #define False ((Bool)0) |
| 94 | #define True ((Bool)1) |
| 95 | |
| 96 | |
| 97 | /* --------------------------------------------------------------------- |
| 98 | Now the basic types are set up, we can haul in the kernel-interface |
| 99 | definitions. |
| 100 | ------------------------------------------------------------------ */ |
| 101 | |
njn25 | 74bb476 | 2002-09-24 11:23:50 +0000 | [diff] [blame^] | 102 | #include "../coregrind/vg_kerneliface.h" |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 103 | |
| 104 | |
| 105 | /*====================================================================*/ |
| 106 | /*=== Command-line options ===*/ |
| 107 | /*====================================================================*/ |
| 108 | |
| 109 | /* Verbosity level: 0 = silent, 1 (default), > 1 = more verbose. */ |
| 110 | extern Int VG_(clo_verbosity); |
| 111 | |
| 112 | /* Profile? */ |
| 113 | extern Bool VG_(clo_profile); |
| 114 | |
| 115 | |
| 116 | /* Call this if a recognised option was bad for some reason. |
| 117 | Note: don't use it just because an option was unrecognised -- return 'False' |
| 118 | from SKN_(process_cmd_line_option) to indicate that. */ |
| 119 | extern void VG_(bad_option) ( Char* opt ); |
| 120 | |
| 121 | /* Client args */ |
| 122 | extern Int VG_(client_argc); |
| 123 | extern Char** VG_(client_argv); |
| 124 | |
| 125 | /* Client environment. Can be inspected with VG_(getenv)() (below) */ |
| 126 | extern Char** VG_(client_envp); |
| 127 | |
| 128 | |
| 129 | /*====================================================================*/ |
| 130 | /*=== Printing messages for the user ===*/ |
| 131 | /*====================================================================*/ |
| 132 | |
| 133 | /* Print a message prefixed by "??<pid>?? "; '?' depends on the VgMsgKind. |
| 134 | Should be used for all user output. */ |
| 135 | |
| 136 | typedef |
| 137 | enum { Vg_UserMsg, /* '?' == '=' */ |
| 138 | Vg_DebugMsg, /* '?' == '-' */ |
| 139 | Vg_DebugExtraMsg /* '?' == '+' */ |
| 140 | } |
| 141 | VgMsgKind; |
| 142 | |
| 143 | /* Functions for building a message from multiple parts. */ |
| 144 | extern void VG_(start_msg) ( VgMsgKind kind ); |
| 145 | extern void VG_(add_to_msg) ( Char* format, ... ); |
| 146 | /* Ends and prints the message. Appends a newline. */ |
| 147 | extern void VG_(end_msg) ( void ); |
| 148 | |
| 149 | /* Send a simple, single-part message. Appends a newline. */ |
| 150 | extern void VG_(message) ( VgMsgKind kind, Char* format, ... ); |
| 151 | |
| 152 | |
| 153 | /*====================================================================*/ |
| 154 | /*=== Profiling ===*/ |
| 155 | /*====================================================================*/ |
| 156 | |
| 157 | /* Nb: VGP_(register_profile_event)() relies on VgpUnc being the first one */ |
| 158 | #define VGP_CORE_LIST \ |
| 159 | /* These ones depend on the core */ \ |
| 160 | VGP_PAIR(VgpUnc, "unclassified"), \ |
| 161 | VGP_PAIR(VgpRun, "running"), \ |
| 162 | VGP_PAIR(VgpSched, "scheduler"), \ |
| 163 | VGP_PAIR(VgpMalloc, "low-lev malloc/free"), \ |
| 164 | VGP_PAIR(VgpCliMalloc, "client malloc/free"), \ |
| 165 | VGP_PAIR(VgpStack, "adjust-stack"), \ |
| 166 | VGP_PAIR(VgpTranslate, "translate-main"), \ |
| 167 | VGP_PAIR(VgpToUCode, "to-ucode"), \ |
| 168 | VGP_PAIR(VgpFromUcode, "from-ucode"), \ |
| 169 | VGP_PAIR(VgpImprove, "improve"), \ |
| 170 | VGP_PAIR(VgpRegAlloc, "reg-alloc"), \ |
| 171 | VGP_PAIR(VgpLiveness, "liveness-analysis"), \ |
| 172 | VGP_PAIR(VgpDoLRU, "do-lru"), \ |
| 173 | VGP_PAIR(VgpSlowFindT, "slow-search-transtab"), \ |
| 174 | VGP_PAIR(VgpInitMem, "init-memory"), \ |
| 175 | VGP_PAIR(VgpExeContext, "exe-context"), \ |
| 176 | VGP_PAIR(VgpReadSyms, "read-syms"), \ |
| 177 | VGP_PAIR(VgpSearchSyms, "search-syms"), \ |
| 178 | VGP_PAIR(VgpAddToT, "add-to-transtab"), \ |
| 179 | VGP_PAIR(VgpCoreSysWrap, "core-syscall-wrapper"), \ |
| 180 | VGP_PAIR(VgpDemangle, "demangle"), \ |
| 181 | /* These ones depend on the skin */ \ |
| 182 | VGP_PAIR(VgpPreCloInit, "pre-clo-init"), \ |
| 183 | VGP_PAIR(VgpPostCloInit, "post-clo-init"), \ |
| 184 | VGP_PAIR(VgpInstrument, "instrument"), \ |
| 185 | VGP_PAIR(VgpSkinSysWrap, "skin-syscall-wrapper"), \ |
| 186 | VGP_PAIR(VgpFini, "fini") |
| 187 | |
| 188 | #define VGP_PAIR(n,name) n |
| 189 | typedef enum { VGP_CORE_LIST } VgpCoreCC; |
| 190 | #undef VGP_PAIR |
| 191 | |
| 192 | /* When registering skin profiling events, ensure that the 'n' value is in |
| 193 | * the range (VgpFini+1..) */ |
| 194 | extern void VGP_(register_profile_event) ( Int n, Char* name ); |
| 195 | |
| 196 | extern void VGP_(pushcc) ( UInt cc ); |
| 197 | extern void VGP_(popcc) ( UInt cc ); |
| 198 | |
| 199 | /* Define them only if they haven't already been defined by vg_profile.c */ |
| 200 | #ifndef VGP_PUSHCC |
| 201 | # define VGP_PUSHCC(x) |
| 202 | #endif |
| 203 | #ifndef VGP_POPCC |
| 204 | # define VGP_POPCC(x) |
| 205 | #endif |
| 206 | |
| 207 | |
| 208 | /*====================================================================*/ |
| 209 | /*=== Useful stuff to call from generated code ===*/ |
| 210 | /*====================================================================*/ |
| 211 | |
| 212 | /* ------------------------------------------------------------------ */ |
| 213 | /* General stuff */ |
| 214 | |
| 215 | /* Get the simulated %esp */ |
| 216 | extern Addr VG_(get_stack_pointer) ( void ); |
| 217 | |
| 218 | /* Detect if an address is within Valgrind's stack */ |
| 219 | extern Bool VG_(within_stack)(Addr a); |
| 220 | |
| 221 | /* Detect if an address is in Valgrind's m_state_static */ |
| 222 | extern Bool VG_(within_m_state_static)(Addr a); |
| 223 | |
| 224 | /* Check if an address is 4-byte aligned */ |
| 225 | #define IS_ALIGNED4_ADDR(aaa_p) (0 == (((UInt)(aaa_p)) & 3)) |
| 226 | |
| 227 | |
| 228 | /* ------------------------------------------------------------------ */ |
| 229 | /* Thread-related stuff */ |
| 230 | |
| 231 | /* Special magic value for an invalid ThreadId. It corresponds to |
| 232 | LinuxThreads using zero as the initial value for |
| 233 | pthread_mutex_t.__m_owner and pthread_cond_t.__c_waiting. */ |
| 234 | #define VG_INVALID_THREADID ((ThreadId)(0)) |
| 235 | |
| 236 | /* ThreadIds are simply indices into the vg_threads[] array. */ |
| 237 | typedef |
| 238 | UInt |
| 239 | ThreadId; |
| 240 | |
| 241 | typedef |
| 242 | struct _ThreadState |
| 243 | ThreadState; |
| 244 | |
| 245 | extern ThreadId VG_(get_current_tid_1_if_root) ( void ); |
| 246 | extern ThreadState* VG_(get_ThreadState) ( ThreadId tid ); |
| 247 | |
| 248 | |
| 249 | /*====================================================================*/ |
| 250 | /*=== Valgrind's version of libc ===*/ |
| 251 | /*====================================================================*/ |
| 252 | |
| 253 | /* Valgrind doesn't use libc at all, for good reasons (trust us). So here |
| 254 | are its own versions of C library functions, but with VG_ prefixes. Note |
| 255 | that the types of some are slightly different to the real ones. Some |
| 256 | extra useful functions are provided too; descriptions of how they work |
| 257 | are given below. */ |
| 258 | |
| 259 | #if !defined(NULL) |
| 260 | # define NULL ((void*)0) |
| 261 | #endif |
| 262 | |
| 263 | |
| 264 | /* ------------------------------------------------------------------ */ |
| 265 | /* stdio.h |
| 266 | * |
| 267 | * Note that they all output to the file descriptor given by the |
| 268 | * --logfile-fd=N argument, which defaults to 2 (stderr). Hence no |
| 269 | * need for VG_(fprintf)(). |
| 270 | * |
| 271 | * Also note that VG_(printf)() and VG_(vprintf)() |
| 272 | */ |
| 273 | extern void VG_(printf) ( const char *format, ... ); |
| 274 | /* too noisy ... __attribute__ ((format (printf, 1, 2))) ; */ |
| 275 | extern void VG_(sprintf) ( Char* buf, Char *format, ... ); |
| 276 | extern void VG_(vprintf) ( void(*send)(Char), |
| 277 | const Char *format, va_list vargs ); |
| 278 | |
| 279 | /* ------------------------------------------------------------------ */ |
| 280 | /* stdlib.h */ |
| 281 | |
| 282 | extern void* VG_(malloc) ( Int nbytes ); |
| 283 | extern void VG_(free) ( void* ptr ); |
| 284 | extern void* VG_(calloc) ( Int nmemb, Int nbytes ); |
| 285 | extern void* VG_(realloc) ( void* ptr, Int size ); |
| 286 | extern void* VG_(malloc_aligned) ( Int req_alignB, Int req_pszB ); |
| 287 | |
| 288 | extern void VG_(print_malloc_stats) ( void ); |
| 289 | |
| 290 | |
| 291 | extern void VG_(exit)( Int status ) |
| 292 | __attribute__ ((__noreturn__)); |
| 293 | /* Print a (panic) message (constant string) appending newline, and abort. */ |
| 294 | extern void VG_(panic) ( Char* str ) |
| 295 | __attribute__ ((__noreturn__)); |
| 296 | |
| 297 | /* Looks up VG_(client_envp) (above) */ |
| 298 | extern Char* VG_(getenv) ( Char* name ); |
| 299 | |
| 300 | /* Crude stand-in for the glibc system() call. */ |
| 301 | extern Int VG_(system) ( Char* cmd ); |
| 302 | |
| 303 | extern Long VG_(atoll) ( Char* str ); |
| 304 | |
| 305 | /* Like atoll(), but converts a number of base 2..36 */ |
| 306 | extern Long VG_(atoll36) ( UInt base, Char* str ); |
| 307 | |
| 308 | |
| 309 | /* ------------------------------------------------------------------ */ |
| 310 | /* ctype.h functions and related */ |
| 311 | extern Bool VG_(isspace) ( Char c ); |
| 312 | extern Bool VG_(isdigit) ( Char c ); |
| 313 | extern Char VG_(toupper) ( Char c ); |
| 314 | |
| 315 | |
| 316 | /* ------------------------------------------------------------------ */ |
| 317 | /* string.h */ |
| 318 | extern Int VG_(strlen) ( const Char* str ); |
| 319 | extern Char* VG_(strcat) ( Char* dest, const Char* src ); |
| 320 | extern Char* VG_(strncat) ( Char* dest, const Char* src, Int n ); |
| 321 | extern Char* VG_(strpbrk) ( const Char* s, const Char* accept ); |
| 322 | extern Char* VG_(strcpy) ( Char* dest, const Char* src ); |
| 323 | extern Char* VG_(strncpy) ( Char* dest, const Char* src, Int ndest ); |
| 324 | extern Int VG_(strcmp) ( const Char* s1, const Char* s2 ); |
| 325 | extern Int VG_(strncmp) ( const Char* s1, const Char* s2, Int nmax ); |
| 326 | extern Char* VG_(strstr) ( const Char* haystack, Char* needle ); |
| 327 | extern Char* VG_(strchr) ( const Char* s, Char c ); |
| 328 | extern Char* VG_(strdup) ( const Char* s); |
| 329 | |
| 330 | /* Like strcmp(), but stops comparing at any whitespace. */ |
| 331 | extern Int VG_(strcmp_ws) ( const Char* s1, const Char* s2 ); |
| 332 | |
| 333 | /* Like strncmp(), but stops comparing at any whitespace. */ |
| 334 | extern Int VG_(strncmp_ws) ( const Char* s1, const Char* s2, Int nmax ); |
| 335 | |
| 336 | /* Like strncpy(), but if 'src' is longer than 'ndest' inserts a '\0' at the |
| 337 | Nth character. */ |
| 338 | extern void VG_(strncpy_safely) ( Char* dest, const Char* src, Int ndest ); |
| 339 | |
| 340 | /* Mini-regexp function. Searches for 'pat' in 'str'. Supports |
| 341 | * meta-symbols '*' and '?'. '\' escapes meta-symbols. */ |
| 342 | extern Bool VG_(stringMatch) ( Char* pat, Char* str ); |
| 343 | |
| 344 | |
| 345 | /* ------------------------------------------------------------------ */ |
| 346 | /* math.h */ |
| 347 | /* Returns the base-2 logarithm of its argument. */ |
| 348 | extern Int VG_(log2) ( Int x ); |
| 349 | |
| 350 | |
| 351 | /* ------------------------------------------------------------------ */ |
| 352 | /* unistd.h */ |
| 353 | extern Int VG_(getpid) ( void ); |
| 354 | |
| 355 | |
| 356 | /* ------------------------------------------------------------------ */ |
| 357 | /* assert.h */ |
| 358 | /* Asserts permanently enabled -- no turning off with NDEBUG. Hurrah! */ |
| 359 | #define VG__STRING(__str) #__str |
| 360 | |
| 361 | #define vg_assert(expr) \ |
| 362 | ((void) ((expr) ? 0 : \ |
| 363 | (VG_(assert_fail) (VG__STRING(expr), \ |
| 364 | __FILE__, __LINE__, \ |
| 365 | __PRETTY_FUNCTION__), 0))) |
| 366 | |
| 367 | extern void VG_(assert_fail) ( Char* expr, Char* file, |
| 368 | Int line, Char* fn ) |
| 369 | __attribute__ ((__noreturn__)); |
| 370 | |
| 371 | |
| 372 | /* ------------------------------------------------------------------ */ |
| 373 | /* Reading and writing files. */ |
| 374 | |
| 375 | /* As per the system calls */ |
| 376 | extern Int VG_(open) ( const Char* pathname, Int flags, Int mode ); |
| 377 | extern Int VG_(read) ( Int fd, void* buf, Int count); |
| 378 | extern Int VG_(write) ( Int fd, void* buf, Int count); |
| 379 | extern void VG_(close) ( Int fd ); |
| 380 | |
| 381 | extern Int VG_(stat) ( Char* file_name, struct vki_stat* buf ); |
| 382 | |
| 383 | |
| 384 | /* ------------------------------------------------------------------ */ |
| 385 | /* mmap and related functions ... */ |
| 386 | extern void* VG_(mmap)( void* start, UInt length, |
| 387 | UInt prot, UInt flags, UInt fd, UInt offset ); |
| 388 | extern Int VG_(munmap)( void* start, Int length ); |
| 389 | |
| 390 | /* Get memory by anonymous mmap. */ |
| 391 | extern void* VG_(get_memory_from_mmap) ( Int nBytes, Char* who ); |
| 392 | |
| 393 | |
| 394 | /* ------------------------------------------------------------------ */ |
| 395 | /* signal.h. |
| 396 | |
| 397 | Note that these use the vk_ (kernel) structure |
| 398 | definitions, which are different in places from those that glibc |
| 399 | defines -- hence the 'k' prefix. Since we're operating right at the |
| 400 | kernel interface, glibc's view of the world is entirely irrelevant. */ |
| 401 | |
| 402 | /* --- Signal set ops --- */ |
| 403 | extern Int VG_(ksigfillset)( vki_ksigset_t* set ); |
| 404 | extern Int VG_(ksigemptyset)( vki_ksigset_t* set ); |
| 405 | |
| 406 | extern Bool VG_(kisfullsigset)( vki_ksigset_t* set ); |
| 407 | extern Bool VG_(kisemptysigset)( vki_ksigset_t* set ); |
| 408 | |
| 409 | extern Int VG_(ksigaddset)( vki_ksigset_t* set, Int signum ); |
| 410 | extern Int VG_(ksigdelset)( vki_ksigset_t* set, Int signum ); |
| 411 | extern Int VG_(ksigismember) ( vki_ksigset_t* set, Int signum ); |
| 412 | |
| 413 | extern void VG_(ksigaddset_from_set)( vki_ksigset_t* dst, |
| 414 | vki_ksigset_t* src ); |
| 415 | extern void VG_(ksigdelset_from_set)( vki_ksigset_t* dst, |
| 416 | vki_ksigset_t* src ); |
| 417 | |
| 418 | /* --- Mess with the kernel's sig state --- */ |
| 419 | extern Int VG_(ksigprocmask)( Int how, const vki_ksigset_t* set, |
| 420 | vki_ksigset_t* oldset ); |
| 421 | extern Int VG_(ksigaction) ( Int signum, |
| 422 | const vki_ksigaction* act, |
| 423 | vki_ksigaction* oldact ); |
| 424 | |
| 425 | extern Int VG_(ksignal)(Int signum, void (*sighandler)(Int)); |
| 426 | |
| 427 | extern Int VG_(ksigaltstack)( const vki_kstack_t* ss, vki_kstack_t* oss ); |
| 428 | |
| 429 | extern Int VG_(kill)( Int pid, Int signo ); |
| 430 | extern Int VG_(sigpending) ( vki_ksigset_t* set ); |
| 431 | |
| 432 | |
| 433 | /*====================================================================*/ |
| 434 | /*=== UCode definition ===*/ |
| 435 | /*====================================================================*/ |
| 436 | |
| 437 | /* Tags which describe what operands are. */ |
| 438 | typedef |
| 439 | enum { TempReg=0, ArchReg=1, RealReg=2, |
| 440 | SpillNo=3, Literal=4, Lit16=5, |
| 441 | NoValue=6 } |
| 442 | Tag; |
| 443 | |
| 444 | /* Invalid register numbers :-) */ |
| 445 | #define INVALID_TEMPREG 999999999 |
| 446 | #define INVALID_REALREG 999999999 |
| 447 | |
| 448 | /* Microinstruction opcodes. */ |
| 449 | typedef |
| 450 | enum { |
| 451 | NOP, |
| 452 | GET, |
| 453 | PUT, |
| 454 | LOAD, |
| 455 | STORE, |
| 456 | MOV, |
| 457 | CMOV, /* Used for cmpxchg and cmov */ |
| 458 | WIDEN, |
| 459 | JMP, |
| 460 | |
| 461 | /* Read/write the %EFLAGS register into a TempReg. */ |
| 462 | GETF, PUTF, |
| 463 | |
| 464 | ADD, ADC, AND, OR, XOR, SUB, SBB, |
| 465 | SHL, SHR, SAR, ROL, ROR, RCL, RCR, |
| 466 | NOT, NEG, INC, DEC, BSWAP, |
| 467 | CC2VAL, |
| 468 | |
| 469 | /* Not strictly needed, but useful for making better |
| 470 | translations of address calculations. */ |
| 471 | LEA1, /* reg2 := const + reg1 */ |
| 472 | LEA2, /* reg3 := const + reg1 + reg2 * 1,2,4 or 8 */ |
| 473 | |
| 474 | /* not for translating x86 calls -- only to call helpers */ |
| 475 | CALLM_S, CALLM_E, /* Mark start and end of push/pop sequences |
| 476 | for CALLM. */ |
| 477 | PUSH, POP, CLEAR, /* Add/remove/zap args for helpers. */ |
| 478 | CALLM, /* call to a machine-code helper */ |
| 479 | |
| 480 | /* For calling C functions of up to three arguments (or two if the |
| 481 | functions has a return value). Arguments and return value must be |
| 482 | word-sized. If you want to pass more arguments than this to a C |
| 483 | function you have to use global variables to fake it (eg. use |
| 484 | VG_(set_global_var)()). |
| 485 | |
| 486 | Seven possibilities: 'arg1..3' show where args go, 'ret' shows |
| 487 | where return values go. |
| 488 | |
| 489 | CCALL(-, -, - ) void f(void) |
| 490 | CCALL(arg1, -, - ) void f(UInt arg1) |
| 491 | CCALL(arg1, arg2, - ) void f(UInt arg1, UInt arg2) |
| 492 | CCALL(arg1, arg2, arg3) void f(UInt arg1, UInt arg2, UInt arg3) |
| 493 | CCALL(-, -, ret ) UInt f(UInt) |
| 494 | CCALL(arg1, -, ret ) UInt f(UInt arg1) |
| 495 | CCALL(arg1, arg2, ret ) UInt f(UInt arg1, UInt arg2) |
| 496 | */ |
| 497 | CCALL, |
| 498 | |
| 499 | /* Hack for translating string (REP-) insns. Jump to literal if |
| 500 | TempReg/RealReg is zero. */ |
| 501 | JIFZ, |
| 502 | |
| 503 | /* FPU ops which read/write mem or don't touch mem at all. */ |
| 504 | FPU_R, |
| 505 | FPU_W, |
| 506 | FPU, |
| 507 | |
| 508 | /* Advance the simulated %eip by some small (< 128) number. */ |
| 509 | INCEIP, |
| 510 | |
| 511 | /* Makes it easy for extended-UCode ops by doing: |
| 512 | |
| 513 | enum { EU_OP1 = DUMMY_FINAL_OP + 1, ... } |
| 514 | |
| 515 | WARNING: Do not add new opcodes after this one! They can be added |
| 516 | before, though. */ |
| 517 | DUMMY_FINAL_UOPCODE |
| 518 | } |
| 519 | Opcode; |
| 520 | |
| 521 | |
| 522 | /* Condition codes, observing the Intel encoding. CondAlways is an |
| 523 | extra. */ |
| 524 | typedef |
| 525 | enum { |
| 526 | CondO = 0, /* overflow */ |
| 527 | CondNO = 1, /* no overflow */ |
| 528 | CondB = 2, /* below */ |
| 529 | CondNB = 3, /* not below */ |
| 530 | CondZ = 4, /* zero */ |
| 531 | CondNZ = 5, /* not zero */ |
| 532 | CondBE = 6, /* below or equal */ |
| 533 | CondNBE = 7, /* not below or equal */ |
| 534 | CondS = 8, /* negative */ |
| 535 | ConsNS = 9, /* not negative */ |
| 536 | CondP = 10, /* parity even */ |
| 537 | CondNP = 11, /* not parity even */ |
| 538 | CondL = 12, /* jump less */ |
| 539 | CondNL = 13, /* not less */ |
| 540 | CondLE = 14, /* less or equal */ |
| 541 | CondNLE = 15, /* not less or equal */ |
| 542 | CondAlways = 16 /* Jump always */ |
| 543 | } |
| 544 | Condcode; |
| 545 | |
| 546 | |
| 547 | /* Descriptions of additional properties of *unconditional* jumps. */ |
| 548 | typedef |
| 549 | enum { |
| 550 | JmpBoring=0, /* boring unconditional jump */ |
| 551 | JmpCall=1, /* jump due to an x86 call insn */ |
| 552 | JmpRet=2, /* jump due to an x86 ret insn */ |
| 553 | JmpSyscall=3, /* do a system call, then jump */ |
| 554 | JmpClientReq=4 /* do a client request, then jump */ |
| 555 | } |
| 556 | JmpKind; |
| 557 | |
| 558 | |
| 559 | /* Flags. User-level code can only read/write O(verflow), S(ign), |
| 560 | Z(ero), A(ux-carry), C(arry), P(arity), and may also write |
| 561 | D(irection). That's a total of 7 flags. A FlagSet is a bitset, |
| 562 | thusly: |
| 563 | 76543210 |
| 564 | DOSZACP |
| 565 | and bit 7 must always be zero since it is unused. |
| 566 | */ |
| 567 | typedef UChar FlagSet; |
| 568 | |
| 569 | #define FlagD (1<<6) |
| 570 | #define FlagO (1<<5) |
| 571 | #define FlagS (1<<4) |
| 572 | #define FlagZ (1<<3) |
| 573 | #define FlagA (1<<2) |
| 574 | #define FlagC (1<<1) |
| 575 | #define FlagP (1<<0) |
| 576 | |
| 577 | #define FlagsOSZACP (FlagO | FlagS | FlagZ | FlagA | FlagC | FlagP) |
| 578 | #define FlagsOSZAP (FlagO | FlagS | FlagZ | FlagA | FlagP) |
| 579 | #define FlagsOSZCP (FlagO | FlagS | FlagZ | FlagC | FlagP) |
| 580 | #define FlagsOSACP (FlagO | FlagS | FlagA | FlagC | FlagP) |
| 581 | #define FlagsSZACP ( FlagS | FlagZ | FlagA | FlagC | FlagP) |
| 582 | #define FlagsSZAP ( FlagS | FlagZ | FlagA | FlagP) |
| 583 | #define FlagsZCP ( FlagZ | FlagC | FlagP) |
| 584 | #define FlagsOC (FlagO | FlagC ) |
| 585 | #define FlagsAC ( FlagA | FlagC ) |
| 586 | |
| 587 | #define FlagsALL (FlagsOSZACP | FlagD) |
| 588 | #define FlagsEmpty (FlagSet)0 |
| 589 | |
| 590 | |
| 591 | /* Liveness of general purpose registers, useful for code generation. |
| 592 | Reg rank order 0..N-1 corresponds to bits 0..N-1, ie. first |
| 593 | reg's liveness in bit 0, last reg's in bit N-1. Note that |
| 594 | these rankings don't match the Intel register ordering. */ |
| 595 | typedef UInt RRegSet; |
| 596 | |
| 597 | #define ALL_RREGS_DEAD 0 /* 0000...00b */ |
| 598 | #define ALL_RREGS_LIVE (1 << (VG_MAX_REALREGS-1)) /* 0011...11b */ |
| 599 | #define UNIT_RREGSET(rank) (1 << (rank)) |
| 600 | |
| 601 | #define IS_RREG_LIVE(rank,rregs_live) (rregs_live & UNIT_RREGSET(rank)) |
| 602 | #define SET_RREG_LIVENESS(rank,rregs_live,b) \ |
| 603 | do { RRegSet unit = UNIT_RREGSET(rank); \ |
| 604 | if (b) rregs_live |= unit; \ |
| 605 | else rregs_live &= ~unit; \ |
| 606 | } while(0) |
| 607 | |
| 608 | |
| 609 | /* A Micro (u)-instruction. */ |
| 610 | typedef |
| 611 | struct { |
| 612 | /* word 1 */ |
| 613 | UInt lit32; /* 32-bit literal */ |
| 614 | |
| 615 | /* word 2 */ |
| 616 | UShort val1; /* first operand */ |
| 617 | UShort val2; /* second operand */ |
| 618 | |
| 619 | /* word 3 */ |
| 620 | UShort val3; /* third operand */ |
| 621 | UChar opcode; /* opcode */ |
| 622 | UChar size; /* data transfer size */ |
| 623 | |
| 624 | /* word 4 */ |
| 625 | FlagSet flags_r; /* :: FlagSet */ |
| 626 | FlagSet flags_w; /* :: FlagSet */ |
| 627 | UChar tag1:4; /* first operand tag */ |
| 628 | UChar tag2:4; /* second operand tag */ |
| 629 | UChar tag3:4; /* third operand tag */ |
| 630 | UChar extra4b:4; /* Spare field, used by WIDEN for src |
| 631 | -size, and by LEA2 for scale (1,2,4 or 8), |
| 632 | and by JMPs for original x86 instr size */ |
| 633 | |
| 634 | /* word 5 */ |
| 635 | UChar cond; /* condition, for jumps */ |
| 636 | Bool signed_widen:1; /* signed or unsigned WIDEN ? */ |
| 637 | JmpKind jmpkind:3; /* additional properties of unconditional JMP */ |
| 638 | |
| 639 | /* Additional properties for UInstrs that call C functions: |
| 640 | - CCALL |
| 641 | - PUT (when %ESP is the target) |
| 642 | - possibly skin-specific UInstrs |
| 643 | */ |
| 644 | UChar argc:2; /* Number of args, max 3 */ |
| 645 | UChar regparms_n:2; /* Number of args passed in registers */ |
| 646 | Bool has_ret_val:1; /* Function has return value? */ |
| 647 | |
| 648 | /* RealReg liveness; only sensical after reg alloc and liveness |
| 649 | analysis done. This info is a little bit arch-specific -- |
| 650 | VG_MAX_REALREGS can vary on different architectures. Note that |
| 651 | to use this information requires converting between register ranks |
| 652 | and the Intel register numbers, using VG_(realRegNumToRank)() |
| 653 | and/or VG_(rankToRealRegNum)() */ |
| 654 | RRegSet regs_live_after:VG_MAX_REALREGS; |
| 655 | } |
| 656 | UInstr; |
| 657 | |
| 658 | |
| 659 | /* Expandable arrays of uinstrs. */ |
| 660 | typedef |
| 661 | struct { |
| 662 | Int used; |
| 663 | Int size; |
| 664 | UInstr* instrs; |
| 665 | Int nextTemp; |
| 666 | } |
| 667 | UCodeBlock; |
| 668 | |
| 669 | |
| 670 | /*====================================================================*/ |
| 671 | /*=== Instrumenting UCode ===*/ |
| 672 | /*====================================================================*/ |
| 673 | |
| 674 | /* A structure for communicating TempReg and RealReg uses of UInstrs. */ |
| 675 | typedef |
| 676 | struct { |
| 677 | Int num; |
| 678 | Bool isWrite; |
| 679 | } |
| 680 | RegUse; |
| 681 | |
| 682 | /* Find what this instruction does to its regs. Tag indicates whether we're |
| 683 | * considering TempRegs (pre-reg-alloc) or RealRegs (post-reg-alloc). |
| 684 | * Useful for analysis/optimisation passes. */ |
| 685 | extern Int VG_(getRegUsage) ( UInstr* u, Tag tag, RegUse* arr ); |
| 686 | |
| 687 | |
| 688 | /* ------------------------------------------------------------------ */ |
| 689 | /* Used to register helper functions to be called from generated code */ |
| 690 | extern void VG_(register_compact_helper) ( Addr a ); |
| 691 | extern void VG_(register_noncompact_helper) ( Addr a ); |
| 692 | |
| 693 | |
| 694 | /* ------------------------------------------------------------------ */ |
| 695 | /* Virtual register allocation */ |
| 696 | |
| 697 | /* Get a new virtual register */ |
| 698 | extern Int VG_(getNewTemp) ( UCodeBlock* cb ); |
| 699 | |
| 700 | /* Get a new virtual shadow register */ |
| 701 | extern Int VG_(getNewShadow) ( UCodeBlock* cb ); |
| 702 | |
| 703 | /* Get a virtual register's corresponding virtual shadow register */ |
| 704 | #define SHADOW(tempreg) ((tempreg)+1) |
| 705 | |
| 706 | |
| 707 | /* ------------------------------------------------------------------ */ |
| 708 | /* Low-level UInstr builders */ |
| 709 | extern void VG_(newNOP) ( UInstr* u ); |
| 710 | extern void VG_(newUInstr0) ( UCodeBlock* cb, Opcode opcode, Int sz ); |
| 711 | extern void VG_(newUInstr1) ( UCodeBlock* cb, Opcode opcode, Int sz, |
| 712 | Tag tag1, UInt val1 ); |
| 713 | extern void VG_(newUInstr2) ( UCodeBlock* cb, Opcode opcode, Int sz, |
| 714 | Tag tag1, UInt val1, |
| 715 | Tag tag2, UInt val2 ); |
| 716 | extern void VG_(newUInstr3) ( UCodeBlock* cb, Opcode opcode, Int sz, |
| 717 | Tag tag1, UInt val1, |
| 718 | Tag tag2, UInt val2, |
| 719 | Tag tag3, UInt val3 ); |
| 720 | extern void VG_(setFlagRW) ( UInstr* u, |
| 721 | FlagSet fr, FlagSet fw ); |
| 722 | extern void VG_(setLiteralField) ( UCodeBlock* cb, UInt lit32 ); |
| 723 | extern void VG_(setCCallFields) ( UCodeBlock* cb, Addr fn, UChar argc, |
| 724 | UChar regparms_n, Bool has_ret_val ); |
| 725 | |
| 726 | extern void VG_(copyUInstr) ( UCodeBlock* cb, UInstr* instr ); |
| 727 | |
| 728 | extern Bool VG_(anyFlagUse) ( UInstr* u ); |
| 729 | |
| 730 | /* Refer to `the last instruction stuffed in' (can be lvalue). */ |
| 731 | #define LAST_UINSTR(cb) (cb)->instrs[(cb)->used-1] |
| 732 | |
| 733 | |
| 734 | /* ------------------------------------------------------------------ */ |
| 735 | /* Higher-level UInstr sequence builders */ |
| 736 | extern void VG_(callHelper_0_0) ( UCodeBlock* cb, Addr f); |
| 737 | extern void VG_(callHelper_1_0) ( UCodeBlock* cb, Addr f, UInt arg1, |
| 738 | UInt regparms_n); |
| 739 | extern void VG_(callHelper_2_0) ( UCodeBlock* cb, Addr f, UInt arg1, UInt arg2, |
| 740 | UInt regparms_n); |
| 741 | |
| 742 | /* One way around the 3-arg C function limit is to pass args via global |
| 743 | * variables... ugly, but it works. */ |
| 744 | void VG_(set_global_var) ( UCodeBlock* cb, Addr globvar_ptr, UInt val); |
| 745 | |
| 746 | /* ------------------------------------------------------------------ */ |
| 747 | /* UCode pretty/ugly printing, to help debugging skins; but only useful |
| 748 | if VG_(needs).extended_UCode == True. */ |
| 749 | |
| 750 | /* When True, all generated code is/should be printed. */ |
| 751 | extern Bool VG_(print_codegen); |
| 752 | |
| 753 | extern void VG_(ppUCodeBlock) ( UCodeBlock* cb, Char* title ); |
| 754 | extern void VG_(ppUInstr) ( Int instrNo, UInstr* u ); |
| 755 | extern void VG_(ppUInstrWithRegs) ( Int instrNo, UInstr* u ); |
| 756 | extern void VG_(upUInstr) ( Int instrNo, UInstr* u ); |
| 757 | extern Char* VG_(nameUOpcode) ( Bool upper, Opcode opc ); |
| 758 | extern void VG_(ppUOperand) ( UInstr* u, Int operandNo, |
| 759 | Int sz, Bool parens ); |
| 760 | |
| 761 | /* ------------------------------------------------------------------ */ |
| 762 | /* Allocating/freeing basic blocks of UCode */ |
| 763 | extern UCodeBlock* VG_(allocCodeBlock) ( void ); |
| 764 | extern void VG_(freeCodeBlock) ( UCodeBlock* cb ); |
| 765 | |
| 766 | /*====================================================================*/ |
| 767 | /*=== Functions for generating x86 code from UCode ===*/ |
| 768 | /*====================================================================*/ |
| 769 | |
| 770 | /* These are only of interest for skins where |
| 771 | VG_(needs).extends_UCode == True. */ |
| 772 | |
| 773 | /* This is the Intel register encoding. */ |
| 774 | #define R_EAX 0 |
| 775 | #define R_ECX 1 |
| 776 | #define R_EDX 2 |
| 777 | #define R_EBX 3 |
| 778 | #define R_ESP 4 |
| 779 | #define R_EBP 5 |
| 780 | #define R_ESI 6 |
| 781 | #define R_EDI 7 |
| 782 | |
| 783 | #define R_AL (0+R_EAX) |
| 784 | #define R_CL (0+R_ECX) |
| 785 | #define R_DL (0+R_EDX) |
| 786 | #define R_BL (0+R_EBX) |
| 787 | #define R_AH (4+R_EAX) |
| 788 | #define R_CH (4+R_ECX) |
| 789 | #define R_DH (4+R_EDX) |
| 790 | #define R_BH (4+R_EBX) |
| 791 | |
| 792 | /* For pretty printing x86 code */ |
| 793 | extern Char* VG_(nameOfIntReg) ( Int size, Int reg ); |
| 794 | extern Char VG_(nameOfIntSize) ( Int size ); |
| 795 | |
| 796 | /* Randomly useful things */ |
| 797 | extern UInt VG_(extend_s_8to32) ( UInt x ); |
| 798 | |
| 799 | /* Code emitters */ |
| 800 | extern void VG_(emitB) ( UInt b ); |
| 801 | extern void VG_(emitW) ( UInt w ); |
| 802 | extern void VG_(emitL) ( UInt l ); |
| 803 | extern void VG_(newEmit)( void ); |
| 804 | |
| 805 | /* Finding offsets */ |
| 806 | extern Int VG_(helper_offset) ( Addr a ); |
| 807 | extern Int VG_(shadowRegOffset) ( Int arch ); |
| 808 | extern Int VG_(shadowFlagsOffset) ( void ); |
| 809 | |
| 810 | /* Converting reg ranks <-> Intel register ordering, for using register |
| 811 | liveness info */ |
| 812 | extern Int VG_(realRegNumToRank) ( Int realReg ); |
| 813 | extern Int VG_(rankToRealRegNum) ( Int rank ); |
| 814 | |
| 815 | /* Subroutine calls */ |
| 816 | /* This one just calls it. */ |
| 817 | void VG_(synth_call) ( Bool ensure_shortform, Int word_offset ); |
| 818 | |
| 819 | /* This one is good for calling C functions -- saves caller save regs, |
| 820 | pushes args, calls, clears the stack, restores caller save regs. |
| 821 | `fn' must be registered in the baseBlock first. Acceptable tags are |
| 822 | RealReg and Literal. |
| 823 | |
| 824 | WARNING: a UInstr should *not* be translated with synth_ccall followed |
| 825 | by some other x86 assembly code; this will confuse |
| 826 | vg_ccall_reg_save_analysis() and everything will fall over. |
| 827 | */ |
| 828 | void VG_(synth_ccall) ( Addr fn, Int argc, Int regparms_n, UInt argv[], |
| 829 | Tag tagv[], Int ret_reg, |
| 830 | RRegSet regs_live_before, RRegSet regs_live_after ); |
| 831 | |
| 832 | /* Addressing modes */ |
| 833 | void VG_(emit_amode_offregmem_reg) ( Int off, Int regmem, Int reg ); |
| 834 | void VG_(emit_amode_ereg_greg) ( Int e_reg, Int g_reg ); |
| 835 | |
| 836 | /* v-size (4, or 2 with OSO) insn emitters */ |
| 837 | void VG_(emit_movv_offregmem_reg) ( Int sz, Int off, Int areg, Int reg ); |
| 838 | void VG_(emit_movv_reg_offregmem) ( Int sz, Int reg, Int off, Int areg ); |
| 839 | void VG_(emit_movv_reg_reg) ( Int sz, Int reg1, Int reg2 ); |
| 840 | void VG_(emit_nonshiftopv_lit_reg)( Int sz, Opcode opc, UInt lit, Int reg ); |
| 841 | void VG_(emit_shiftopv_lit_reg) ( Int sz, Opcode opc, UInt lit, Int reg ); |
| 842 | void VG_(emit_nonshiftopv_reg_reg)( Int sz, Opcode opc, Int reg1, Int reg2 ); |
| 843 | void VG_(emit_movv_lit_reg) ( Int sz, UInt lit, Int reg ); |
| 844 | void VG_(emit_unaryopv_reg) ( Int sz, Opcode opc, Int reg ); |
| 845 | void VG_(emit_pushv_reg) ( Int sz, Int reg ); |
| 846 | void VG_(emit_popv_reg) ( Int sz, Int reg ); |
| 847 | |
| 848 | void VG_(emit_pushl_lit32) ( UInt int32 ); |
| 849 | void VG_(emit_pushl_lit8) ( Int lit8 ); |
| 850 | void VG_(emit_cmpl_zero_reg) ( Int reg ); |
| 851 | void VG_(emit_swapl_reg_EAX) ( Int reg ); |
| 852 | void VG_(emit_movv_lit_offregmem) ( Int sz, UInt lit, Int off, Int memreg ); |
| 853 | |
| 854 | /* b-size (1 byte) instruction emitters */ |
| 855 | void VG_(emit_movb_lit_offregmem) ( UInt lit, Int off, Int memreg ); |
| 856 | void VG_(emit_movb_reg_offregmem) ( Int reg, Int off, Int areg ); |
| 857 | void VG_(emit_unaryopb_reg) ( Opcode opc, Int reg ); |
| 858 | void VG_(emit_testb_lit_reg) ( UInt lit, Int reg ); |
| 859 | |
| 860 | /* zero-extended load emitters */ |
| 861 | void VG_(emit_movzbl_offregmem_reg) ( Int off, Int regmem, Int reg ); |
| 862 | void VG_(emit_movzwl_offregmem_reg) ( Int off, Int areg, Int reg ); |
| 863 | |
| 864 | /* misc instruction emitters */ |
| 865 | void VG_(emit_call_reg) ( Int reg ); |
| 866 | void VG_(emit_add_lit_to_esp) ( Int lit ); |
| 867 | void VG_(emit_jcondshort_delta) ( Condcode cond, Int delta ); |
| 868 | void VG_(emit_pushal) ( void ); |
| 869 | void VG_(emit_popal) ( void ); |
| 870 | void VG_(emit_AMD_prefetch_reg) ( Int reg ); |
| 871 | |
| 872 | |
| 873 | /*====================================================================*/ |
| 874 | /*=== Execution contexts ===*/ |
| 875 | /*====================================================================*/ |
| 876 | |
| 877 | /* Generic resolution type used in a few different ways, such as deciding |
| 878 | how closely to compare two errors for equality. */ |
| 879 | typedef |
| 880 | enum { Vg_LowRes, Vg_MedRes, Vg_HighRes } |
| 881 | VgRes; |
| 882 | |
| 883 | typedef |
| 884 | struct _ExeContext |
| 885 | ExeContext; |
| 886 | |
| 887 | /* Compare two ExeContexts, just comparing the top two callers. */ |
| 888 | extern Bool VG_(eq_ExeContext) ( VgRes res, |
| 889 | ExeContext* e1, ExeContext* e2 ); |
| 890 | |
| 891 | /* Print an ExeContext. */ |
| 892 | extern void VG_(pp_ExeContext) ( ExeContext* ); |
| 893 | |
| 894 | /* Take a snapshot of the client's stack. Search our collection of |
| 895 | ExeContexts to see if we already have it, and if not, allocate a |
| 896 | new one. Either way, return a pointer to the context. */ |
| 897 | extern ExeContext* VG_(get_ExeContext) ( ThreadState *tst ); |
| 898 | |
| 899 | |
| 900 | /*====================================================================*/ |
| 901 | /*=== Error reporting ===*/ |
| 902 | /*====================================================================*/ |
| 903 | |
| 904 | /* ------------------------------------------------------------------ */ |
| 905 | /* Suppressions describe errors which we want to suppress, ie, not |
| 906 | show the user, usually because it is caused by a problem in a library |
| 907 | which we can't fix, replace or work around. Suppressions are read from |
| 908 | a file at startup time, specified by vg_clo_suppressions, and placed in |
| 909 | the vg_suppressions list. This gives flexibility so that new |
| 910 | suppressions can be added to the file as and when needed. |
| 911 | */ |
| 912 | |
| 913 | typedef |
| 914 | Int /* Do not make this unsigned! */ |
| 915 | SuppKind; |
| 916 | |
| 917 | /* An extensible (via the 'extra' field) suppression record. This holds |
| 918 | the suppression details of interest to a skin. Skins can use a normal |
| 919 | enum (with element values in the normal range (0..)) for `skind'. |
| 920 | |
| 921 | If VG_(needs).report_errors==True, for each suppression read in by core |
| 922 | SKN_(recognised_suppression)() and SKN_(read_extra_suppression_info) will |
| 923 | be called. The `skind' field is filled in by the value returned in the |
| 924 | argument of the first function; the second function can fill in the |
| 925 | `string' and `extra' fields if it wants. |
| 926 | */ |
| 927 | typedef |
| 928 | struct { |
| 929 | /* What kind of suppression. Must use the range (0..) */ |
| 930 | SuppKind skind; |
| 931 | /* String -- use is optional. NULL by default. */ |
| 932 | Char* string; |
| 933 | /* Anything else -- use is optional. NULL by default. */ |
| 934 | void* extra; |
| 935 | } |
| 936 | SkinSupp; |
| 937 | |
| 938 | |
| 939 | /* ------------------------------------------------------------------ */ |
| 940 | /* Error records contain enough info to generate an error report. The idea |
| 941 | is that (typically) the same few points in the program generate thousands |
| 942 | of illegal accesses, and we don't want to spew out a fresh error message |
| 943 | for each one. Instead, we use these structures to common up duplicates. |
| 944 | */ |
| 945 | |
| 946 | typedef |
| 947 | Int /* Do not make this unsigned! */ |
| 948 | ErrorKind; |
| 949 | |
| 950 | /* An extensible (via the 'extra' field) error record. This holds |
| 951 | the error details of interest to a skin. Skins can use a normal |
| 952 | enum (with element values in the normal range (0..)) for `ekind'. |
| 953 | |
| 954 | When errors are found and recorded with VG_(maybe_record_error)(), all |
| 955 | the skin must do is pass in the four parameters; core will |
| 956 | allocate/initialise the error record. |
| 957 | */ |
| 958 | typedef |
| 959 | struct { |
| 960 | /* Used by ALL. Must be in the range (0..) */ |
| 961 | Int ekind; |
| 962 | /* Used frequently */ |
| 963 | Addr addr; |
| 964 | /* Used frequently */ |
| 965 | Char* string; |
| 966 | /* For any skin-specific extras: size and the extra fields */ |
| 967 | void* extra; |
| 968 | } |
| 969 | SkinError; |
| 970 | |
| 971 | |
| 972 | /* ------------------------------------------------------------------ */ |
| 973 | /* Call this when an error occurs. It will be recorded if it's not been |
| 974 | seen before. If it has, the existing error record will have its count |
| 975 | incremented. |
| 976 | |
| 977 | If the error occurs in generated code, 'tst' should be NULL. If the |
| 978 | error occurs in non-generated code, 'tst' should be non-NULL. The |
| 979 | `extra' field can be stack-allocated; it will be copied (using |
| 980 | SKN_(dup_extra_and_update)()) if needed. But it won't be copied |
| 981 | if it's NULL. |
| 982 | */ |
| 983 | extern void VG_(maybe_record_error) ( ThreadState* tst, ErrorKind ekind, |
| 984 | Addr a, Char* s, void* extra ); |
| 985 | |
| 986 | /* Gets a non-blank, non-comment line of at most nBuf chars from fd. |
| 987 | Skips leading spaces on the line. Returns True if EOF was hit instead. |
| 988 | Useful for reading in extra skin-specific suppression lines. |
| 989 | */ |
| 990 | extern Bool VG_(getLine) ( Int fd, Char* buf, Int nBuf ); |
| 991 | |
| 992 | |
| 993 | /*====================================================================*/ |
| 994 | /*=== Obtaining debug information ===*/ |
| 995 | /*====================================================================*/ |
| 996 | |
| 997 | /* Get the file/function/line number of the instruction at address 'a'. |
| 998 | For these four, if debug info for the address is found, it copies the |
| 999 | info into the buffer/UInt and returns True. If not, it returns False and |
| 1000 | nothing is copied. VG_(get_fnname) always demangles C++ function names. |
| 1001 | */ |
| 1002 | extern Bool VG_(get_filename) ( Addr a, Char* filename, Int n_filename ); |
| 1003 | extern Bool VG_(get_fnname) ( Addr a, Char* fnname, Int n_fnname ); |
| 1004 | extern Bool VG_(get_linenum) ( Addr a, UInt* linenum ); |
| 1005 | |
| 1006 | /* This one is more efficient if getting both filename and line number, |
| 1007 | because the two lookups are done together. */ |
| 1008 | extern Bool VG_(get_filename_linenum) |
| 1009 | ( Addr a, Char* filename, Int n_filename, |
| 1010 | UInt* linenum ); |
| 1011 | |
| 1012 | /* Succeeds only if we find from debug info that 'a' is the address of the |
| 1013 | first instruction in a function -- as opposed to VG_(get_fnname) which |
| 1014 | succeeds if we find from debug info that 'a' is the address of any |
| 1015 | instruction in a function. Use this to instrument the start of |
| 1016 | a particular function. Nb: if a executable/shared object is stripped |
| 1017 | of its symbols, this function will not be able to recognise function |
| 1018 | entry points within it. */ |
| 1019 | extern Bool VG_(get_fnname_if_entry) ( Addr a, Char* filename, Int n_filename ); |
| 1020 | |
| 1021 | /* Succeeds if the address is within a shared object or the main executable. |
| 1022 | It doesn't matter if debug info is present or not. */ |
| 1023 | extern Bool VG_(get_objname) ( Addr a, Char* objname, Int n_objname ); |
| 1024 | |
| 1025 | |
| 1026 | /*====================================================================*/ |
| 1027 | /*=== Shadow chunks and block-finding ===*/ |
| 1028 | /*====================================================================*/ |
| 1029 | |
| 1030 | typedef |
| 1031 | enum { |
| 1032 | Vg_AllocMalloc = 0, |
| 1033 | Vg_AllocNew = 1, |
| 1034 | Vg_AllocNewVec = 2 |
| 1035 | } |
| 1036 | VgAllocKind; |
| 1037 | |
| 1038 | /* Description of a malloc'd chunk. skin_extra[] part can be used by |
| 1039 | the skin; size of array is given by VG_(needs).sizeof_shadow_chunk. */ |
| 1040 | typedef |
| 1041 | struct _ShadowChunk { |
| 1042 | struct _ShadowChunk* next; |
| 1043 | UInt size : 30; /* size requested */ |
| 1044 | VgAllocKind allockind : 2; /* which wrapper did the allocation */ |
| 1045 | Addr data; /* ptr to actual block */ |
| 1046 | UInt skin_extra[0]; /* extra skin-specific info */ |
| 1047 | } |
| 1048 | ShadowChunk; |
| 1049 | |
| 1050 | /* Use this to free blocks if VG_(needs).alternative_free == True. |
| 1051 | It frees the ShadowChunk and the malloc'd block it points to. */ |
| 1052 | extern void VG_(freeShadowChunk) ( ShadowChunk* sc ); |
| 1053 | |
| 1054 | /* Makes an array of pointers to all the shadow chunks of malloc'd blocks */ |
| 1055 | extern ShadowChunk** VG_(get_malloc_shadows) ( /*OUT*/ UInt* n_shadows ); |
| 1056 | |
| 1057 | /* Determines if address 'a' is within the bounds of the block at start. |
| 1058 | Allows a little 'slop' round the edges. */ |
| 1059 | extern Bool VG_(addr_is_in_block) ( Addr a, Addr start, UInt size ); |
| 1060 | |
| 1061 | /* Searches through currently malloc'd blocks until a matching one is found. |
| 1062 | Returns NULL if none match. Extra arguments can be implicitly passed to |
| 1063 | p using nested functions; see vg_memcheck_errcontext.c for an example. */ |
| 1064 | extern ShadowChunk* VG_(any_matching_mallocd_ShadowChunks) |
| 1065 | ( Bool (*p) ( ShadowChunk* )); |
| 1066 | |
| 1067 | /* Searches through all thread's stacks to see if any match. Returns |
| 1068 | * VG_INVALID_THREADID if none match. */ |
| 1069 | extern ThreadId VG_(any_matching_thread_stack) |
| 1070 | ( Bool (*p) ( Addr stack_min, Addr stack_max )); |
| 1071 | |
| 1072 | /*====================================================================*/ |
| 1073 | /*=== Skin-specific stuff ===*/ |
| 1074 | /*====================================================================*/ |
| 1075 | |
| 1076 | /* Skin-specific settings. |
| 1077 | * |
| 1078 | * If new fields are added to this type, update: |
| 1079 | * - vg_main.c:VG_(needs) initialisation |
| 1080 | * - vg_main.c:sanity_check_needs() |
| 1081 | * |
| 1082 | * If the name of this type or any of its fields change, update: |
| 1083 | * - dependent comments (just search for "VG_(needs)"). |
| 1084 | */ |
| 1085 | typedef |
| 1086 | struct { |
| 1087 | /* name and description used in the startup message */ |
| 1088 | Char* name; |
| 1089 | Char* description; |
| 1090 | |
| 1091 | /* Booleans that decide core behaviour */ |
| 1092 | |
| 1093 | /* Want to have errors detected by Valgrind's core reported? Includes: |
| 1094 | - pthread API errors (many; eg. unlocking a non-locked mutex) |
| 1095 | - silly arguments to malloc() et al (eg. negative size) |
| 1096 | - invalid file descriptors to blocking syscalls read() and write() |
| 1097 | - bad signal numbers passed to sigaction() |
| 1098 | - attempt to install signal handler for SIGKILL or SIGSTOP */ |
| 1099 | Bool core_errors; |
| 1100 | /* Want to report errors from the skin? This implies use of |
| 1101 | suppressions, too. */ |
| 1102 | Bool skin_errors; |
| 1103 | |
| 1104 | /* Should __libc_freeres() be run? Bugs in it crash the skin. */ |
| 1105 | Bool run_libc_freeres; |
| 1106 | |
| 1107 | /* Booleans that indicate extra operations are defined; if these are |
| 1108 | True, the corresponding template functions (given below) must be |
| 1109 | defined. A lot like being a member of a type class. */ |
| 1110 | |
| 1111 | /* Is information kept about specific individual basic blocks? (Eg. for |
| 1112 | cachesim there are cost-centres for every instruction, stored at a |
| 1113 | basic block level.) If so, it sometimes has to be discarded, because |
| 1114 | .so mmap/munmap-ping or self-modifying code (informed by the |
| 1115 | DISCARD_TRANSLATIONS user request) can cause one instruction address |
| 1116 | to store information about more than one instruction in one program |
| 1117 | run! */ |
| 1118 | Bool basic_block_discards; |
| 1119 | |
| 1120 | /* Maintains information about each register? */ |
| 1121 | Bool shadow_regs; |
| 1122 | |
| 1123 | /* Skin defines its own command line options? */ |
| 1124 | Bool command_line_options; |
| 1125 | /* Skin defines its own client requests? */ |
| 1126 | Bool client_requests; |
| 1127 | |
| 1128 | /* Skin defines its own UInstrs? */ |
| 1129 | Bool extended_UCode; |
| 1130 | |
| 1131 | /* Skin does stuff before and/or after system calls? */ |
| 1132 | Bool syscall_wrapper; |
| 1133 | |
| 1134 | /* Size, in words, of extra info about malloc'd blocks recorded by |
| 1135 | skin. Be careful to get this right or you'll get seg faults! */ |
| 1136 | UInt sizeof_shadow_block; |
| 1137 | |
| 1138 | /* Skin does free()s itself? */ |
| 1139 | Bool alternative_free; |
| 1140 | |
| 1141 | /* Are skin-state sanity checks performed? */ |
| 1142 | Bool sanity_checks; |
| 1143 | } |
| 1144 | VgNeeds; |
| 1145 | |
| 1146 | extern VgNeeds VG_(needs); |
| 1147 | |
| 1148 | |
| 1149 | /* ------------------------------------------------------------------ */ |
| 1150 | /* Core events to track */ |
| 1151 | |
| 1152 | /* Part of the core from which this call was made. Useful for determining |
| 1153 | * what kind of error message should be emitted. */ |
| 1154 | typedef |
| 1155 | enum { Vg_CorePThread, Vg_CoreSignal, Vg_CoreSysCall, Vg_CoreTranslate } |
| 1156 | CorePart; |
| 1157 | |
| 1158 | /* Events happening in core to track. To be notified, assign a function |
| 1159 | * to the function pointer. To ignore an event, don't do anything |
| 1160 | * (default assignment is to NULL in which case the call is skipped). */ |
| 1161 | typedef |
| 1162 | struct { |
| 1163 | /* Memory events */ |
| 1164 | void (*new_mem_startup)( Addr a, UInt len, Bool rr, Bool ww, Bool xx ); |
| 1165 | void (*new_mem_heap) ( Addr a, UInt len, Bool is_inited ); |
| 1166 | void (*new_mem_stack) ( Addr a, UInt len ); |
| 1167 | void (*new_mem_stack_aligned) ( Addr a, UInt len ); |
| 1168 | void (*new_mem_stack_signal) ( Addr a, UInt len ); |
| 1169 | void (*new_mem_brk) ( Addr a, UInt len ); |
| 1170 | void (*new_mem_mmap) ( Addr a, UInt len, |
| 1171 | Bool nn, Bool rr, Bool ww, Bool xx ); |
| 1172 | |
| 1173 | void (*copy_mem_heap) ( Addr from, Addr to, UInt len ); |
| 1174 | void (*copy_mem_remap) ( Addr from, Addr to, UInt len ); |
| 1175 | void (*change_mem_mprotect) ( Addr a, UInt len, |
| 1176 | Bool nn, Bool rr, Bool ww, Bool xx ); |
| 1177 | |
| 1178 | void (*ban_mem_heap) ( Addr a, UInt len ); |
| 1179 | void (*ban_mem_stack) ( Addr a, UInt len ); |
| 1180 | |
| 1181 | void (*die_mem_heap) ( Addr a, UInt len ); |
| 1182 | void (*die_mem_stack) ( Addr a, UInt len ); |
| 1183 | void (*die_mem_stack_aligned) ( Addr a, UInt len ); |
| 1184 | void (*die_mem_stack_signal) ( Addr a, UInt len ); |
| 1185 | void (*die_mem_brk) ( Addr a, UInt len ); |
| 1186 | void (*die_mem_munmap) ( Addr a, UInt len ); |
| 1187 | |
| 1188 | void (*bad_free) ( ThreadState* tst, Addr a ); |
| 1189 | void (*mismatched_free) ( ThreadState* tst, Addr a ); |
| 1190 | |
| 1191 | void (*pre_mem_read) ( CorePart part, ThreadState* tst, |
| 1192 | Char* s, Addr a, UInt size ); |
| 1193 | void (*pre_mem_read_asciiz) ( CorePart part, ThreadState* tst, |
| 1194 | Char* s, Addr a ); |
| 1195 | void (*pre_mem_write) ( CorePart part, ThreadState* tst, |
| 1196 | Char* s, Addr a, UInt size ); |
| 1197 | /* Not implemented yet -- have to add in lots of places, which is a |
| 1198 | pain. Won't bother unless/until there's a need. */ |
| 1199 | /* void (*post_mem_read) ( ThreadState* tst, Char* s, |
| 1200 | Addr a, UInt size ); */ |
| 1201 | void (*post_mem_write) ( Addr a, UInt size ); |
| 1202 | |
| 1203 | |
| 1204 | /* Scheduler events */ |
| 1205 | void (*thread_run) ( ThreadId tid ); |
| 1206 | |
| 1207 | |
| 1208 | /* Mutex events */ |
| 1209 | void (*post_mutex_lock) ( ThreadId tid, |
| 1210 | void* /*pthread_mutex_t* */ mutex ); |
| 1211 | void (*post_mutex_unlock) ( ThreadId tid, |
| 1212 | void* /*pthread_mutex_t* */ mutex ); |
| 1213 | |
| 1214 | /* Others... threads, condition variables, etc... */ |
| 1215 | |
| 1216 | /* ... */ |
| 1217 | } |
| 1218 | VgTrackEvents; |
| 1219 | |
| 1220 | /* Declare the struct instance */ |
| 1221 | extern VgTrackEvents VG_(track_events); |
| 1222 | |
| 1223 | |
| 1224 | /* ------------------------------------------------------------------ */ |
| 1225 | /* Template functions */ |
| 1226 | |
| 1227 | /* These are the parameterised functions in the core. The default definitions |
| 1228 | * are replaced by LD_PRELOADing skin substitutes. At the very least, a skin |
| 1229 | * must define the fundamental template functions. Depending on what needs |
| 1230 | * boolean variables are set, extra templates will be used too. For each |
| 1231 | * group, the need governing its use is mentioned. */ |
| 1232 | |
| 1233 | |
| 1234 | /* ------------------------------------------------------------------ */ |
| 1235 | /* Fundamental template functions */ |
| 1236 | |
| 1237 | /* Initialise skin. Must do the following: |
| 1238 | - initialise the 'needs' struct |
| 1239 | - register any helpers called by generated code |
| 1240 | |
| 1241 | May do the following: |
| 1242 | - indicate events to track by initialising part or all of the 'track' |
| 1243 | struct |
| 1244 | - register any skin-specific profiling events |
| 1245 | - any other skin-specific initialisation |
| 1246 | */ |
| 1247 | extern void SK_(pre_clo_init) ( VgNeeds* needs, VgTrackEvents* track ); |
| 1248 | |
| 1249 | /* Do any initialisation that relies on the results of command line option |
| 1250 | processing. */ |
| 1251 | extern void SK_(post_clo_init)( void ); |
| 1252 | |
| 1253 | /* Instrument a basic block. Must be a true function, ie. the same input |
| 1254 | always results in the same output, because basic blocks can be |
| 1255 | retranslated. Unless you're doing something really strange... |
| 1256 | 'orig_addr' is the address of the first instruction in the block. */ |
| 1257 | extern UCodeBlock* SK_(instrument) ( UCodeBlock* cb, Addr orig_addr ); |
| 1258 | |
| 1259 | /* Finish up, print out any results, etc. */ |
| 1260 | extern void SK_(fini) ( void ); |
| 1261 | |
| 1262 | |
| 1263 | /* ------------------------------------------------------------------ */ |
| 1264 | /* VG_(needs).report_errors */ |
| 1265 | |
| 1266 | /* Identify if two errors are equal, or equal enough. `res' indicates how |
| 1267 | close is "close enough". `res' should be passed on as necessary, eg. if |
| 1268 | the SkinError's extra field contains an ExeContext, `res' should be |
| 1269 | passed to VG_(eq_ExeContext)() if the ExeContexts are considered. Other |
| 1270 | than that, probably don't worry about it unless you have lots of very |
| 1271 | similar errors occurring. |
| 1272 | */ |
| 1273 | extern Bool SK_(eq_SkinError) ( VgRes res, |
| 1274 | SkinError* e1, SkinError* e2 ); |
| 1275 | |
| 1276 | /* Print error context. The passed function pp_ExeContext() can be (and |
| 1277 | probably should be) used to print the location of the error. */ |
| 1278 | extern void SK_(pp_SkinError) ( SkinError* ec, void (*pp_ExeContext)(void) ); |
| 1279 | |
| 1280 | /* Copy the ec->extra part and replace ec->extra with the new copy. This is |
| 1281 | necessary to move from a temporary stack copy to a permanent heap one. |
| 1282 | |
| 1283 | Then fill in any details that could be postponed until after the decision |
| 1284 | whether to ignore the error (ie. details not affecting the result of |
| 1285 | SK_(eq_SkinError)()). This saves time when errors are ignored. |
| 1286 | |
| 1287 | Yuk. |
| 1288 | */ |
| 1289 | extern void SK_(dup_extra_and_update)(SkinError* ec); |
| 1290 | |
| 1291 | /* Return value indicates recognition. If recognised, type goes in `skind'. */ |
| 1292 | extern Bool SK_(recognised_suppression) ( Char* name, SuppKind *skind ); |
| 1293 | |
| 1294 | /* Read any extra info for this suppression kind. For filling up the |
| 1295 | `string' and `extra' fields in a `SkinSupp' struct if necessary. */ |
| 1296 | extern Bool SK_(read_extra_suppression_info) ( Int fd, Char* buf, |
| 1297 | Int nBuf, SkinSupp *s ); |
| 1298 | |
| 1299 | /* This should just check the kinds match and maybe some stuff in the |
| 1300 | 'extra' field if appropriate */ |
| 1301 | extern Bool SK_(error_matches_suppression)(SkinError* ec, SkinSupp* su); |
| 1302 | |
| 1303 | |
| 1304 | /* ------------------------------------------------------------------ */ |
| 1305 | /* VG_(needs).basic_block_discards */ |
| 1306 | |
| 1307 | extern void SK_(discard_basic_block_info) ( Addr a, UInt size ); |
| 1308 | |
| 1309 | |
| 1310 | /* ------------------------------------------------------------------ */ |
| 1311 | /* VG_(needs).shadow_regs */ |
| 1312 | |
| 1313 | /* Valid values for general registers and EFLAGS register, for initialising |
| 1314 | and updating registers when written in certain places in core. */ |
| 1315 | extern void SK_(written_shadow_regs_values) ( UInt* gen_reg, UInt* eflags ); |
| 1316 | |
| 1317 | |
| 1318 | /* ------------------------------------------------------------------ */ |
| 1319 | /* VG_(needs).command_line_options */ |
| 1320 | |
| 1321 | /* Return True if option was recognised */ |
| 1322 | extern Bool SK_(process_cmd_line_option)( Char* argv ); |
| 1323 | |
| 1324 | /* Print out command line usage for skin options */ |
| 1325 | extern Char* SK_(usage) ( void ); |
| 1326 | |
| 1327 | |
| 1328 | /* ------------------------------------------------------------------ */ |
| 1329 | /* VG_(needs).client_requests */ |
| 1330 | |
| 1331 | extern UInt SK_(handle_client_request) ( ThreadState* tst, UInt* arg_block ); |
| 1332 | |
| 1333 | |
| 1334 | /* ------------------------------------------------------------------ */ |
| 1335 | /* VG_(needs).extends_UCode */ |
| 1336 | |
| 1337 | /* Used in VG_(getExtRegUsage)() */ |
| 1338 | # define VG_UINSTR_READS_REG(ono) \ |
| 1339 | { if (mycat(u->tag,ono) == tag) \ |
| 1340 | { arr[n].num = mycat(u->val,ono); \ |
| 1341 | arr[n].isWrite = False; \ |
| 1342 | n++; \ |
| 1343 | } \ |
| 1344 | } |
| 1345 | # define VG_UINSTR_WRITES_REG(ono) \ |
| 1346 | { if (mycat(u->tag,ono) == tag) \ |
| 1347 | { arr[n].num = mycat(u->val,ono); \ |
| 1348 | arr[n].isWrite = True; \ |
| 1349 | n++; \ |
| 1350 | } \ |
| 1351 | } |
| 1352 | |
| 1353 | // SSS: only ones using camel caps |
| 1354 | extern Int SK_(getExtRegUsage) ( UInstr* u, Tag tag, RegUse* arr ); |
| 1355 | extern void SK_(emitExtUInstr) ( UInstr* u, RRegSet regs_live_before ); |
| 1356 | extern Bool SK_(saneExtUInstr) ( Bool beforeRA, Bool beforeLiveness, |
| 1357 | UInstr* u ); |
| 1358 | extern Char* SK_(nameExtUOpcode) ( Opcode opc ); |
| 1359 | extern void SK_(ppExtUInstr) ( UInstr* u ); |
| 1360 | |
| 1361 | |
| 1362 | /* ------------------------------------------------------------------ */ |
| 1363 | /* VG_(needs).syscall_wrapper */ |
| 1364 | |
| 1365 | /* If either of the pre_ functions malloc() something to return, the |
| 1366 | * corresponding post_ function had better free() it! |
| 1367 | */ |
| 1368 | extern void* SK_( pre_syscall) ( ThreadId tid, UInt syscallno, |
| 1369 | Bool is_blocking ); |
| 1370 | extern void SK_(post_syscall) ( ThreadId tid, UInt syscallno, |
| 1371 | void* pre_result, Int res, |
| 1372 | Bool is_blocking ); |
| 1373 | |
| 1374 | /* ------------------------------------------------------------------ */ |
| 1375 | /* VG_(needs).sizeof_shadow_chunk > 0 */ |
| 1376 | |
| 1377 | extern void SK_(complete_shadow_chunk) ( ShadowChunk* sc, ThreadState* tst ); |
| 1378 | |
| 1379 | |
| 1380 | /* ------------------------------------------------------------------ */ |
| 1381 | /* VG_(needs).alternative_free */ |
| 1382 | |
| 1383 | extern void SK_(alt_free) ( ShadowChunk* sc, ThreadState* tst ); |
| 1384 | |
| 1385 | /* --------------------------------------------------------------------- |
| 1386 | VG_(needs).sanity_checks */ |
| 1387 | |
| 1388 | extern Bool SK_(cheap_sanity_check) ( void ); |
| 1389 | extern Bool SK_(expensive_sanity_check) ( void ); |
| 1390 | |
| 1391 | |
| 1392 | #endif /* NDEF __VG_SKIN_H */ |
| 1393 | |
| 1394 | /*--------------------------------------------------------------------*/ |
| 1395 | /*--- end vg_skin.h ---*/ |
| 1396 | /*--------------------------------------------------------------------*/ |
| 1397 | |