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