blob: 7d4b168d7f6e199abcbd20545556f56d93b592a7 [file] [log] [blame]
nethercote37aac2e2004-09-02 08:54:27 +00001/*-*- c -*- ----------------------------------------------------------*/
njnd01fef72005-03-25 23:35:48 +00002/*--- Header for lots of tool stuff. tool.h ---*/
nethercote37aac2e2004-09-02 08:54:27 +00003/*--------------------------------------------------------------------*/
4
5/*
njnb9c427c2004-12-01 14:14:42 +00006 This file is part of Valgrind, a dynamic binary instrumentation
7 framework.
nethercote37aac2e2004-09-02 08:54:27 +00008
njn53612422005-03-12 16:22:54 +00009 Copyright (C) 2000-2005 Julian Seward
nethercote37aac2e2004-09-02 08:54:27 +000010 jseward@acm.org
11
12 This program is free software; you can redistribute it and/or
13 modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation; either version 2 of the
15 License, or (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful, but
18 WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
25 02111-1307, USA.
26
27 The GNU General Public License is contained in the file COPYING.
28*/
29
30#ifndef __TOOL_H
31#define __TOOL_H
32
33#include <stdarg.h> /* ANSI varargs stuff */
nethercote37aac2e2004-09-02 08:54:27 +000034
nethercoteebf1d862004-11-01 18:22:05 +000035#include "basic_types.h"
sewardjb5f6f512005-03-10 23:59:00 +000036#include "tool_asm.h" /* asm stuff */
37#include "tool_arch.h" /* arch-specific tool stuff */
nethercote73b526f2004-10-31 18:48:21 +000038#include "vki.h"
nethercote37aac2e2004-09-02 08:54:27 +000039
njnd2b17112005-04-19 04:10:25 +000040#include "pub_tool_errormgr.h" // needed for 'Error', 'Supp'
41#include "pub_tool_execontext.h" // needed for 'ExeContext'
njnd01fef72005-03-25 23:35:48 +000042
sewardj8b635a42004-11-22 19:01:47 +000043#include "libvex.h"
44#include "libvex_ir.h"
45
nethercote37aac2e2004-09-02 08:54:27 +000046/*====================================================================*/
47/*=== Build options and table sizes. ===*/
48/*====================================================================*/
49
50/* You should be able to change these options or sizes, recompile, and
51 still have a working system. */
52
53/* The maximum number of pthreads that we support. This is
54 deliberately not very high since our implementation of some of the
55 scheduler algorithms is surely O(N) in the number of threads, since
56 that's simple, at least. And (in practice) we hope that most
57 programs do not need many threads. */
58#define VG_N_THREADS 100
59
60/* Maximum number of pthread keys available. Again, we start low until
61 the need for a higher number presents itself. */
62#define VG_N_THREAD_KEYS 50
63
nethercote37aac2e2004-09-02 08:54:27 +000064
65/*====================================================================*/
nethercote2e05c332004-09-06 16:43:37 +000066/*=== Useful macros ===*/
nethercote37aac2e2004-09-02 08:54:27 +000067/*====================================================================*/
68
nethercote37aac2e2004-09-02 08:54:27 +000069/* No, really. I _am_ that strange. */
70#define OINK(nnn) VG_(message)(Vg_DebugMsg, "OINK %d",nnn)
71
nethercote37aac2e2004-09-02 08:54:27 +000072/* Path to all our library/aux files */
73extern const Char *VG_(libdir);
74
75
76/*====================================================================*/
77/*=== Core/tool interface version ===*/
78/*====================================================================*/
79
80/* The major version number indicates binary-incompatible changes to the
81 interface; if the core and tool major versions don't match, Valgrind
82 will abort. The minor version indicates binary-compatible changes.
nethercote836d46c2004-11-18 12:58:53 +000083
84 (Update: as it happens, we're never using the minor version number, because
85 there's no point in doing so.)
nethercote37aac2e2004-09-02 08:54:27 +000086*/
nethercote836d46c2004-11-18 12:58:53 +000087#define VG_CORE_INTERFACE_MAJOR_VERSION 7
nethercote37aac2e2004-09-02 08:54:27 +000088#define VG_CORE_INTERFACE_MINOR_VERSION 0
89
90typedef struct _ToolInfo {
91 Int sizeof_ToolInfo;
92 Int interface_major_version;
93 Int interface_minor_version;
94
95 /* Initialise tool. Must do the following:
96 - initialise the `details' struct, via the VG_(details_*)() functions
97 - register any helpers called by generated code
98
99 May do the following:
100 - initialise the `needs' struct to indicate certain requirements, via
101 the VG_(needs_*)() functions
102 - initialize all the tool's entrypoints via the VG_(init_*)() functions
103 - register any tool-specific profiling events
104 - any other tool-specific initialisation
105 */
njnd2252832004-11-26 10:53:33 +0000106 void (*tl_pre_clo_init) ( void );
nethercote37aac2e2004-09-02 08:54:27 +0000107
108 /* Specifies how big the shadow segment should be as a ratio to the
109 client address space. 0 for no shadow segment. */
110 float shadow_ratio;
111} ToolInfo;
112
113/* Every tool must include this macro somewhere, exactly once. */
114#define VG_DETERMINE_INTERFACE_VERSION(pre_clo_init, shadow) \
njn26f02512004-11-22 18:33:15 +0000115 const ToolInfo TL_(tool_info) = { \
nethercote37aac2e2004-09-02 08:54:27 +0000116 .sizeof_ToolInfo = sizeof(ToolInfo), \
117 .interface_major_version = VG_CORE_INTERFACE_MAJOR_VERSION, \
118 .interface_minor_version = VG_CORE_INTERFACE_MINOR_VERSION, \
njnd2252832004-11-26 10:53:33 +0000119 .tl_pre_clo_init = pre_clo_init, \
nethercote37aac2e2004-09-02 08:54:27 +0000120 .shadow_ratio = shadow, \
121 };
122
123/*====================================================================*/
124/*=== Command-line options ===*/
125/*====================================================================*/
126
127/* Use this for normal null-termination-style string comparison */
128#define VG_STREQ(s1,s2) (s1 != NULL && s2 != NULL \
129 && VG_(strcmp)((s1),(s2))==0)
130
131/* Use these for recognising tool command line options -- stops comparing
132 once whitespace is reached. */
133#define VG_CLO_STREQ(s1,s2) (0==VG_(strcmp_ws)((s1),(s2)))
134#define VG_CLO_STREQN(nn,s1,s2) (0==VG_(strncmp_ws)((s1),(s2),(nn)))
135
sewardjb5f6f512005-03-10 23:59:00 +0000136/* Higher-level command-line option recognisers; use in if/else chains */
nethercote37aac2e2004-09-02 08:54:27 +0000137
njn45270a22005-03-27 01:00:11 +0000138#define VG_BOOL_CLO(qq_arg, qq_option, qq_var) \
139 if (VG_CLO_STREQ(qq_arg, qq_option"=yes")) { (qq_var) = True; } \
140 else if (VG_CLO_STREQ(qq_arg, qq_option"=no")) { (qq_var) = False; }
nethercote37aac2e2004-09-02 08:54:27 +0000141
njn45270a22005-03-27 01:00:11 +0000142#define VG_STR_CLO(qq_arg, qq_option, qq_var) \
143 if (VG_CLO_STREQN(VG_(strlen)(qq_option)+1, qq_arg, qq_option"=")) { \
144 (qq_var) = &qq_arg[ VG_(strlen)(qq_option)+1 ]; \
nethercote37aac2e2004-09-02 08:54:27 +0000145 }
146
njn45270a22005-03-27 01:00:11 +0000147#define VG_NUM_CLO(qq_arg, qq_option, qq_var) \
148 if (VG_CLO_STREQN(VG_(strlen)(qq_option)+1, qq_arg, qq_option"=")) { \
149 (qq_var) = (Int)VG_(atoll)( &qq_arg[ VG_(strlen)(qq_option)+1 ] ); \
nethercote37aac2e2004-09-02 08:54:27 +0000150 }
151
sewardjb5f6f512005-03-10 23:59:00 +0000152/* Bounded integer arg */
njn45270a22005-03-27 01:00:11 +0000153#define VG_BNUM_CLO(qq_arg, qq_option, qq_var, qq_lo, qq_hi) \
154 if (VG_CLO_STREQN(VG_(strlen)(qq_option)+1, qq_arg, qq_option"=")) { \
155 (qq_var) = (Int)VG_(atoll)( &qq_arg[ VG_(strlen)(qq_option)+1 ] ); \
nethercote37aac2e2004-09-02 08:54:27 +0000156 if ((qq_var) < (qq_lo)) (qq_var) = (qq_lo); \
157 if ((qq_var) > (qq_hi)) (qq_var) = (qq_hi); \
158 }
159
160
161/* Verbosity level: 0 = silent, 1 (default), > 1 = more verbose. */
162extern Int VG_(clo_verbosity);
163
164/* Profile? */
165extern Bool VG_(clo_profile);
166
167/* Call this if a recognised option was bad for some reason.
168 Note: don't use it just because an option was unrecognised -- return 'False'
njn94065fd2004-11-22 19:26:27 +0000169 from TL_(process_cmd_line_option) to indicate that. */
nethercote37aac2e2004-09-02 08:54:27 +0000170extern void VG_(bad_option) ( Char* opt );
171
172/* Client args */
173extern Int VG_(client_argc);
174extern Char** VG_(client_argv);
175
176/* Client environment. Can be inspected with VG_(getenv)() */
177extern Char** VG_(client_envp);
178
179
180/*====================================================================*/
181/*=== Printing messages for the user ===*/
182/*====================================================================*/
183
184/* Print a message prefixed by "??<pid>?? "; '?' depends on the VgMsgKind.
185 Should be used for all user output. */
186
187typedef
188 enum { Vg_UserMsg, /* '?' == '=' */
189 Vg_DebugMsg, /* '?' == '-' */
190 Vg_DebugExtraMsg, /* '?' == '+' */
sewardjb5f6f512005-03-10 23:59:00 +0000191 Vg_ClientMsg /* '?' == '*' */
nethercote37aac2e2004-09-02 08:54:27 +0000192 }
193 VgMsgKind;
194
nethercote37aac2e2004-09-02 08:54:27 +0000195/* Send a single-part message. Appends a newline. */
sewardjb5f6f512005-03-10 23:59:00 +0000196extern int VG_(message) ( VgMsgKind kind, const Char* format, ... );
197extern int VG_(vmessage) ( VgMsgKind kind, const Char* format, va_list vargs );
nethercote37aac2e2004-09-02 08:54:27 +0000198
199
200/*====================================================================*/
201/*=== Profiling ===*/
202/*====================================================================*/
203
njn31066fd2005-03-26 00:42:02 +0000204/* Nb: VG_(register_profile_event)() relies on VgpUnc being the first one */
nethercote37aac2e2004-09-02 08:54:27 +0000205#define VGP_CORE_LIST \
206 /* These ones depend on the core */ \
207 VGP_PAIR(VgpUnc, "unclassified"), \
208 VGP_PAIR(VgpStartup, "startup"), \
209 VGP_PAIR(VgpRun, "running"), \
210 VGP_PAIR(VgpSched, "scheduler"), \
211 VGP_PAIR(VgpMalloc, "low-lev malloc/free"), \
212 VGP_PAIR(VgpCliMalloc, "client malloc/free"), \
213 VGP_PAIR(VgpTranslate, "translate-main"), \
214 VGP_PAIR(VgpToUCode, "to-ucode"), \
215 VGP_PAIR(VgpFromUcode, "from-ucode"), \
216 VGP_PAIR(VgpImprove, "improve"), \
217 VGP_PAIR(VgpESPUpdate, "ESP-update"), \
218 VGP_PAIR(VgpRegAlloc, "reg-alloc"), \
219 VGP_PAIR(VgpLiveness, "liveness-analysis"), \
220 VGP_PAIR(VgpDoLRU, "do-lru"), \
221 VGP_PAIR(VgpSlowFindT, "slow-search-transtab"), \
222 VGP_PAIR(VgpExeContext, "exe-context"), \
223 VGP_PAIR(VgpReadSyms, "read-syms"), \
224 VGP_PAIR(VgpSearchSyms, "search-syms"), \
225 VGP_PAIR(VgpAddToT, "add-to-transtab"), \
226 VGP_PAIR(VgpCoreSysWrap, "core-syscall-wrapper"), \
227 VGP_PAIR(VgpDemangle, "demangle"), \
228 VGP_PAIR(VgpCoreCheapSanity, "core-cheap-sanity"), \
229 VGP_PAIR(VgpCoreExpensiveSanity, "core-expensive-sanity"), \
230 /* These ones depend on the tool */ \
231 VGP_PAIR(VgpPreCloInit, "pre-clo-init"), \
232 VGP_PAIR(VgpPostCloInit, "post-clo-init"), \
233 VGP_PAIR(VgpInstrument, "instrument"), \
njn26f02512004-11-22 18:33:15 +0000234 VGP_PAIR(VgpToolSysWrap, "tool-syscall-wrapper"), \
235 VGP_PAIR(VgpToolCheapSanity, "tool-cheap-sanity"), \
236 VGP_PAIR(VgpToolExpensiveSanity, "tool-expensive-sanity"), \
nethercote37aac2e2004-09-02 08:54:27 +0000237 VGP_PAIR(VgpFini, "fini")
238
239#define VGP_PAIR(n,name) n
240typedef enum { VGP_CORE_LIST } VgpCoreCC;
241#undef VGP_PAIR
242
243/* When registering tool profiling events, ensure that the 'n' value is in
244 * the range (VgpFini+1..) */
njn31066fd2005-03-26 00:42:02 +0000245extern void VG_(register_profile_event) ( Int n, Char* name );
nethercote37aac2e2004-09-02 08:54:27 +0000246
njn31066fd2005-03-26 00:42:02 +0000247extern void VG_(pushcc) ( UInt cc );
248extern void VG_(popcc) ( UInt cc );
nethercote37aac2e2004-09-02 08:54:27 +0000249
250/* Define them only if they haven't already been defined by vg_profile.c */
251#ifndef VGP_PUSHCC
252# define VGP_PUSHCC(x)
253#endif
254#ifndef VGP_POPCC
255# define VGP_POPCC(x)
256#endif
257
258
259/*====================================================================*/
260/*=== Useful stuff to call from generated code ===*/
261/*====================================================================*/
262
263/* ------------------------------------------------------------------ */
264/* General stuff */
265
njnedfa0f62004-11-30 18:08:05 +0000266/* Check if an address/whatever is aligned */
njnbe91aae2005-03-27 01:42:41 +0000267#define VG_IS_4_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & 0x3))
268#define VG_IS_8_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & 0x7))
269#define VG_IS_16_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & 0xf))
270#define VG_IS_WORD_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & (sizeof(Addr)-1)))
271#define VG_IS_PAGE_ALIGNED(aaa_p) (0 == (((Addr)(aaa_p)) & (VKI_PAGE_SIZE-1)))
nethercote37aac2e2004-09-02 08:54:27 +0000272
273
274/* ------------------------------------------------------------------ */
275/* Thread-related stuff */
276
277/* Special magic value for an invalid ThreadId. It corresponds to
278 LinuxThreads using zero as the initial value for
279 pthread_mutex_t.__m_owner and pthread_cond_t.__c_waiting. */
280#define VG_INVALID_THREADID ((ThreadId)(0))
281
sewardjb5f6f512005-03-10 23:59:00 +0000282/* Get the TID of the thread which currently has the CPU. */
283extern ThreadId VG_(get_running_tid) ( void );
nethercote37aac2e2004-09-02 08:54:27 +0000284
285/* Searches through all thread's stacks to see if any match. Returns
286 VG_INVALID_THREADID if none match. */
287extern ThreadId VG_(first_matching_thread_stack)
288 ( Bool (*p) ( Addr stack_min, Addr stack_max, void* d ),
289 void* d );
290
njn67516132005-03-22 04:02:43 +0000291/* Get parts of the client's state. */
292extern Addr VG_(get_SP) ( ThreadId tid );
293extern Addr VG_(get_IP) ( ThreadId tid );
sewardj2a99cf62004-11-24 10:44:19 +0000294
nethercote37aac2e2004-09-02 08:54:27 +0000295
296/*====================================================================*/
297/*=== Valgrind's version of libc ===*/
298/*====================================================================*/
299
300/* Valgrind doesn't use libc at all, for good reasons (trust us). So here
301 are its own versions of C library functions, but with VG_ prefixes. Note
302 that the types of some are slightly different to the real ones. Some
303 additional useful functions are provided too; descriptions of how they
304 work are given below. */
305
306#if !defined(NULL)
307# define NULL ((void*)0)
308#endif
309
310
311/* ------------------------------------------------------------------ */
312/* stdio.h
313 *
314 * Note that they all output to the file descriptor given by the
315 * --log-fd/--log-file/--log-socket argument, which defaults to 2 (stderr).
316 * Hence no need for VG_(fprintf)().
317 */
318extern UInt VG_(printf) ( const char *format, ... );
319/* too noisy ... __attribute__ ((format (printf, 1, 2))) ; */
320extern UInt VG_(sprintf) ( Char* buf, Char *format, ... );
nethercote37aac2e2004-09-02 08:54:27 +0000321
322extern Int VG_(rename) ( Char* old_name, Char* new_name );
323
324/* ------------------------------------------------------------------ */
325/* stdlib.h */
326
nethercote7ac7f7b2004-11-02 12:36:02 +0000327extern void* VG_(malloc) ( SizeT nbytes );
nethercote37aac2e2004-09-02 08:54:27 +0000328extern void VG_(free) ( void* p );
njn926ed472005-03-11 04:44:10 +0000329extern void* VG_(calloc) ( SizeT n, SizeT bytes_per_elem );
nethercote7ac7f7b2004-11-02 12:36:02 +0000330extern void* VG_(realloc) ( void* p, SizeT size );
nethercote37aac2e2004-09-02 08:54:27 +0000331
sewardjb5f6f512005-03-10 23:59:00 +0000332/* terminate everything */
njn8a97c6d2005-03-31 04:37:24 +0000333extern void VG_(exit)( Int status )
334 __attribute__ ((__noreturn__));
sewardjb5f6f512005-03-10 23:59:00 +0000335
336/* terminate the calling thread - probably not what you want */
337extern void VG_(exit_single)( Int status )
338 __attribute__ ((__noreturn__));
339
nethercote37aac2e2004-09-02 08:54:27 +0000340/* Prints a panic message (a constant string), appends newline and bug
341 reporting info, aborts. */
342__attribute__ ((__noreturn__))
njn67993252004-11-22 18:02:32 +0000343extern void VG_(tool_panic) ( Char* str );
nethercote37aac2e2004-09-02 08:54:27 +0000344
345/* Looks up VG_(client_envp) */
346extern Char* VG_(getenv) ( Char* name );
347
348/* Get client resource limit*/
349extern Int VG_(getrlimit) ( Int resource, struct vki_rlimit *rlim );
350
351/* Set client resource limit*/
sewardjb5f6f512005-03-10 23:59:00 +0000352extern Int VG_(setrlimit) ( Int resource, const struct vki_rlimit *rlim );
nethercote37aac2e2004-09-02 08:54:27 +0000353
354/* Crude stand-in for the glibc system() call. */
355extern Int VG_(system) ( Char* cmd );
356
357extern Long VG_(atoll) ( Char* str );
358
359/* Like atoll(), but converts a number of base 16 */
360extern Long VG_(atoll16) ( Char* str );
361
362/* Like atoll(), but converts a number of base 2..36 */
363extern Long VG_(atoll36) ( UInt base, Char* str );
364
365/* Like qsort(), but does shell-sort. The size==1/2/4 cases are specialised. */
nethercote928a5f72004-11-03 18:10:37 +0000366extern void VG_(ssort)( void* base, SizeT nmemb, SizeT size,
nethercote37aac2e2004-09-02 08:54:27 +0000367 Int (*compar)(void*, void*) );
368
369
370/* ------------------------------------------------------------------ */
371/* ctype.h */
372extern Bool VG_(isspace) ( Char c );
373extern Bool VG_(isdigit) ( Char c );
374extern Char VG_(toupper) ( Char c );
375
376
377/* ------------------------------------------------------------------ */
378/* string.h */
379extern Int VG_(strlen) ( const Char* str );
380extern Char* VG_(strcat) ( Char* dest, const Char* src );
381extern Char* VG_(strncat) ( Char* dest, const Char* src, Int n );
382extern Char* VG_(strpbrk) ( const Char* s, const Char* accept );
383extern Char* VG_(strcpy) ( Char* dest, const Char* src );
384extern Char* VG_(strncpy) ( Char* dest, const Char* src, Int ndest );
385extern Int VG_(strcmp) ( const Char* s1, const Char* s2 );
386extern Int VG_(strncmp) ( const Char* s1, const Char* s2, Int nmax );
387extern Char* VG_(strstr) ( const Char* haystack, Char* needle );
388extern Char* VG_(strchr) ( const Char* s, Char c );
389extern Char* VG_(strrchr) ( const Char* s, Char c );
390extern Char* VG_(strdup) ( const Char* s);
391extern void* VG_(memcpy) ( void *d, const void *s, Int sz );
392extern void* VG_(memset) ( void *s, Int c, Int sz );
393extern Int VG_(memcmp) ( const void* s1, const void* s2, Int n );
394
395/* Like strcmp() and strncmp(), but stop comparing at any whitespace. */
396extern Int VG_(strcmp_ws) ( const Char* s1, const Char* s2 );
397extern Int VG_(strncmp_ws) ( const Char* s1, const Char* s2, Int nmax );
398
399/* Like strncpy(), but if 'src' is longer than 'ndest' inserts a '\0' as the
400 last character. */
401extern void VG_(strncpy_safely) ( Char* dest, const Char* src, Int ndest );
402
403/* Mini-regexp function. Searches for 'pat' in 'str'. Supports
404 * meta-symbols '*' and '?'. '\' escapes meta-symbols. */
405extern Bool VG_(string_match) ( const Char* pat, const Char* str );
406
407
408/* ------------------------------------------------------------------ */
409/* math.h */
410/* Returns the base-2 logarithm of x. */
411extern Int VG_(log2) ( Int x );
412
413
414/* ------------------------------------------------------------------ */
415/* unistd.h, fcntl.h, sys/stat.h */
416extern Int VG_(getdents)( UInt fd, struct vki_dirent *dirp, UInt count );
417extern Int VG_(readlink)( Char* path, Char* buf, UInt bufsize );
418extern Int VG_(getpid) ( void );
419extern Int VG_(getppid) ( void );
420extern Int VG_(getpgrp) ( void );
421extern Int VG_(gettid) ( void );
422extern Int VG_(setpgid) ( Int pid, Int pgrp );
423
424extern Int VG_(open) ( const Char* pathname, Int flags, Int mode );
425extern Int VG_(read) ( Int fd, void* buf, Int count);
426extern Int VG_(write) ( Int fd, const void* buf, Int count);
nethercote5b9fafd2004-11-04 18:39:22 +0000427extern OffT VG_(lseek) ( Int fd, OffT offset, Int whence);
nethercote37aac2e2004-09-02 08:54:27 +0000428extern void VG_(close) ( Int fd );
429
430extern Int VG_(pipe) ( Int fd[2] );
431
432/* Nb: VG_(rename)() declared in stdio.h section above */
433extern Int VG_(unlink) ( Char* file_name );
434extern Int VG_(stat) ( Char* file_name, struct vki_stat* buf );
435extern Int VG_(fstat) ( Int fd, struct vki_stat* buf );
436extern Int VG_(dup2) ( Int oldfd, Int newfd );
437
nethercote928a5f72004-11-03 18:10:37 +0000438extern Char* VG_(getcwd) ( Char* buf, SizeT size );
nethercote37aac2e2004-09-02 08:54:27 +0000439
440/* Easier to use than VG_(getcwd)() -- does the buffer fiddling itself.
441 String put into 'cwd' is VG_(malloc)'d, and should be VG_(free)'d.
442 Returns False if it fails. Will fail if the pathname is > 65535 bytes. */
443extern Bool VG_(getcwd_alloc) ( Char** cwd );
444
445/* ------------------------------------------------------------------ */
446/* assert.h */
447/* Asserts permanently enabled -- no turning off with NDEBUG. Hurrah! */
njn8c74b002005-03-27 01:25:38 +0000448
449/* This odd definition lets us stringify VG_(x) function names to
450 "vgPlain_x". We need to do two macroexpansions to get the VG_ macro
451 expanded before stringifying. */
452#define VG_STRINGIFY_WRK(x) #x
453#define VG_STRINGIFY(x) VG_STRINGIFY_WRK(x)
nethercote37aac2e2004-09-02 08:54:27 +0000454
njn50ae1a72005-04-08 23:28:23 +0000455#define tl_assert(expr) \
456 ((void) ((expr) ? 0 : \
457 (VG_(assert_fail) (/*isCore?*/False, VG_STRINGIFY(expr), \
458 __FILE__, __LINE__, __PRETTY_FUNCTION__, \
459 ""), \
460 0)))
461
462#define tl_assert2(expr, format, args...) \
463 ((void) ((expr) ? 0 : \
464 (VG_(assert_fail) (/*isCore?*/False, VG_STRINGIFY(expr), \
465 __FILE__, __LINE__, __PRETTY_FUNCTION__, \
466 format, ##args), \
467 0)))
nethercote37aac2e2004-09-02 08:54:27 +0000468
469__attribute__ ((__noreturn__))
njn50ae1a72005-04-08 23:28:23 +0000470extern void VG_(assert_fail) ( Bool isCore, const Char* expr, const Char* file,
471 Int line, const Char* fn,
472 const Char* format, ... );
nethercote37aac2e2004-09-02 08:54:27 +0000473
474/* ------------------------------------------------------------------ */
475/* Get memory by anonymous mmap. */
nethercote8b5f40c2004-11-02 13:29:50 +0000476extern void* VG_(get_memory_from_mmap) ( SizeT nBytes, Char* who );
nethercote37aac2e2004-09-02 08:54:27 +0000477
478extern Bool VG_(is_client_addr) (Addr a);
nethercote37aac2e2004-09-02 08:54:27 +0000479
480extern Bool VG_(is_shadow_addr) (Addr a);
nethercote37aac2e2004-09-02 08:54:27 +0000481extern Addr VG_(get_shadow_size)(void);
482
483extern void *VG_(shadow_alloc)(UInt size);
484
sewardjb5f6f512005-03-10 23:59:00 +0000485extern Bool VG_(is_addressable)(Addr p, SizeT sz, UInt prot);
nethercote37aac2e2004-09-02 08:54:27 +0000486
sewardjb5f6f512005-03-10 23:59:00 +0000487/* Register an interest in apparently internal faults; used code which
488 wanders around dangerous memory (ie, leakcheck). The catcher is
489 not expected to return. */
490extern void VG_(set_fault_catcher)(void (*catcher)(Int sig, Addr addr));
491
nethercote37aac2e2004-09-02 08:54:27 +0000492/* initialize shadow pages in the range [p, p+sz) This calls
493 init_shadow_page for each one. It should be a lot more efficient
494 for bulk-initializing shadow pages than faulting on each one.
495*/
496extern void VG_(init_shadow_range)(Addr p, UInt sz, Bool call_init);
497
sewardjb5f6f512005-03-10 23:59:00 +0000498/* Calls into the core used by leak-checking */
499
500/* Calls "add_rootrange" with each range of memory which looks like a
501 plausible source of root pointers. */
502extern void VG_(find_root_memory)(void (*add_rootrange)(Addr addr, SizeT sz));
503
504/* Calls "mark_addr" with register values (which may or may not be pointers) */
505extern void VG_(mark_from_registers)(void (*mark_addr)(Addr addr));
506
nethercote37aac2e2004-09-02 08:54:27 +0000507/* ------------------------------------------------------------------ */
508/* signal.h.
509
510 Note that these use the vk_ (kernel) structure
511 definitions, which are different in places from those that glibc
nethercote73b526f2004-10-31 18:48:21 +0000512 defines. Since we're operating right at the kernel interface, glibc's view
513 of the world is entirely irrelevant. */
nethercote37aac2e2004-09-02 08:54:27 +0000514
515/* --- Signal set ops --- */
nethercote73b526f2004-10-31 18:48:21 +0000516extern Int VG_(sigfillset) ( vki_sigset_t* set );
517extern Int VG_(sigemptyset) ( vki_sigset_t* set );
nethercote37aac2e2004-09-02 08:54:27 +0000518
sewardjb5f6f512005-03-10 23:59:00 +0000519extern Bool VG_(isfullsigset) ( const vki_sigset_t* set );
520extern Bool VG_(isemptysigset) ( const vki_sigset_t* set );
njn4c4d57b2005-03-26 20:03:07 +0000521extern Bool VG_(iseqsigset) ( const vki_sigset_t* set1,
522 const vki_sigset_t* set2 );
nethercote37aac2e2004-09-02 08:54:27 +0000523
nethercote73b526f2004-10-31 18:48:21 +0000524extern Int VG_(sigaddset) ( vki_sigset_t* set, Int signum );
525extern Int VG_(sigdelset) ( vki_sigset_t* set, Int signum );
sewardjb5f6f512005-03-10 23:59:00 +0000526extern Int VG_(sigismember) ( const vki_sigset_t* set, Int signum );
nethercote37aac2e2004-09-02 08:54:27 +0000527
nethercote73b526f2004-10-31 18:48:21 +0000528extern void VG_(sigaddset_from_set) ( vki_sigset_t* dst, vki_sigset_t* src );
529extern void VG_(sigdelset_from_set) ( vki_sigset_t* dst, vki_sigset_t* src );
nethercote37aac2e2004-09-02 08:54:27 +0000530
531/* --- Mess with the kernel's sig state --- */
nethercote73b526f2004-10-31 18:48:21 +0000532extern Int VG_(sigprocmask) ( Int how, const vki_sigset_t* set,
533 vki_sigset_t* oldset );
534extern Int VG_(sigaction) ( Int signum,
535 const struct vki_sigaction* act,
536 struct vki_sigaction* oldact );
nethercote37aac2e2004-09-02 08:54:27 +0000537
nethercote73b526f2004-10-31 18:48:21 +0000538extern Int VG_(sigtimedwait)( const vki_sigset_t *, vki_siginfo_t *,
539 const struct vki_timespec * );
nethercote37aac2e2004-09-02 08:54:27 +0000540
nethercote73b526f2004-10-31 18:48:21 +0000541extern Int VG_(signal) ( Int signum, void (*sighandler)(Int) );
542extern Int VG_(sigaltstack) ( const vki_stack_t* ss, vki_stack_t* oss );
nethercote37aac2e2004-09-02 08:54:27 +0000543
nethercote73b526f2004-10-31 18:48:21 +0000544extern Int VG_(kill) ( Int pid, Int signo );
njnc6168192004-11-29 13:54:10 +0000545extern Int VG_(tkill) ( ThreadId tid, Int signo );
nethercote73b526f2004-10-31 18:48:21 +0000546extern Int VG_(sigpending) ( vki_sigset_t* set );
nethercote37aac2e2004-09-02 08:54:27 +0000547
nethercote73b526f2004-10-31 18:48:21 +0000548extern Int VG_(waitpid) ( Int pid, Int *status, Int options );
nethercote37aac2e2004-09-02 08:54:27 +0000549
550/* ------------------------------------------------------------------ */
551/* socket.h. */
552
553extern Int VG_(getsockname) ( Int sd, struct vki_sockaddr *name, Int *namelen);
554extern Int VG_(getpeername) ( Int sd, struct vki_sockaddr *name, Int *namelen);
555extern Int VG_(getsockopt) ( Int sd, Int level, Int optname, void *optval,
556 Int *optlen);
557
558/* ------------------------------------------------------------------ */
559/* other, randomly useful functions */
560extern UInt VG_(read_millisecond_timer) ( void );
561
sewardjb5f6f512005-03-10 23:59:00 +0000562extern Bool VG_(has_cpuid) ( void );
563
nethercote37aac2e2004-09-02 08:54:27 +0000564extern void VG_(cpuid) ( UInt eax,
565 UInt *eax_ret, UInt *ebx_ret,
566 UInt *ecx_ret, UInt *edx_ret );
567
nethercote37aac2e2004-09-02 08:54:27 +0000568/*====================================================================*/
nethercote37aac2e2004-09-02 08:54:27 +0000569/*=== Obtaining debug information ===*/
570/*====================================================================*/
571
572/* Get the file/function/line number of the instruction at address
573 'a'. For these four, if debug info for the address is found, it
574 copies the info into the buffer/UInt and returns True. If not, it
575 returns False and nothing is copied. VG_(get_fnname) always
576 demangles C++ function names. VG_(get_fnname_w_offset) is the
577 same, except it appends "+N" to symbol names to indicate offsets. */
578extern Bool VG_(get_filename) ( Addr a, Char* filename, Int n_filename );
579extern Bool VG_(get_fnname) ( Addr a, Char* fnname, Int n_fnname );
580extern Bool VG_(get_linenum) ( Addr a, UInt* linenum );
581extern Bool VG_(get_fnname_w_offset)
582 ( Addr a, Char* fnname, Int n_fnname );
583
584/* This one is more efficient if getting both filename and line number,
585 because the two lookups are done together. */
586extern Bool VG_(get_filename_linenum)
587 ( Addr a, Char* filename, Int n_filename,
588 UInt* linenum );
589
590/* Succeeds only if we find from debug info that 'a' is the address of the
591 first instruction in a function -- as opposed to VG_(get_fnname) which
592 succeeds if we find from debug info that 'a' is the address of any
593 instruction in a function. Use this to instrument the start of
594 a particular function. Nb: if an executable/shared object is stripped
595 of its symbols, this function will not be able to recognise function
596 entry points within it. */
597extern Bool VG_(get_fnname_if_entry) ( Addr a, Char* fnname, Int n_fnname );
598
599/* Succeeds if the address is within a shared object or the main executable.
600 It doesn't matter if debug info is present or not. */
601extern Bool VG_(get_objname) ( Addr a, Char* objname, Int n_objname );
602
603/* Puts into 'buf' info about the code address %eip: the address, function
604 name (if known) and filename/line number (if known), like this:
605
606 0x4001BF05: realloc (vg_replace_malloc.c:339)
607
608 'n_buf' gives length of 'buf'. Returns 'buf'.
609*/
njnd01fef72005-03-25 23:35:48 +0000610extern Char* VG_(describe_IP)(Addr eip, Char* buf, Int n_buf);
nethercote37aac2e2004-09-02 08:54:27 +0000611
612/* Returns a string containing an expression for the given
613 address. String is malloced with VG_(malloc)() */
614Char *VG_(describe_addr)(ThreadId, Addr);
615
616/* A way to get information about what segments are mapped */
617typedef struct _SegInfo SegInfo;
618
619/* Returns NULL if the SegInfo isn't found. It doesn't matter if debug info
620 is present or not. */
621extern SegInfo* VG_(get_obj) ( Addr a );
622
623extern const SegInfo* VG_(next_seginfo) ( const SegInfo *seg );
624extern Addr VG_(seg_start) ( const SegInfo *seg );
nethercote928a5f72004-11-03 18:10:37 +0000625extern SizeT VG_(seg_size) ( const SegInfo *seg );
nethercote37aac2e2004-09-02 08:54:27 +0000626extern const UChar* VG_(seg_filename) ( const SegInfo *seg );
nethercote928a5f72004-11-03 18:10:37 +0000627extern ULong VG_(seg_sym_offset)( const SegInfo *seg );
nethercote37aac2e2004-09-02 08:54:27 +0000628
629typedef
630 enum {
631 Vg_SectUnknown,
632 Vg_SectText,
633 Vg_SectData,
634 Vg_SectBSS,
635 Vg_SectGOT,
sewardjb5f6f512005-03-10 23:59:00 +0000636 Vg_SectPLT
nethercote37aac2e2004-09-02 08:54:27 +0000637 }
638 VgSectKind;
639
640extern VgSectKind VG_(seg_sect_kind)(Addr);
641
nethercote37aac2e2004-09-02 08:54:27 +0000642/*====================================================================*/
643/*=== Generic hash table ===*/
644/*====================================================================*/
645
646/* Generic type for a separately-chained hash table. Via a kind of dodgy
647 C-as-C++ style inheritance, tools can extend the VgHashNode type, so long
648 as the first two fields match the sizes of these two fields. Requires
649 a bit of casting by the tool. */
650typedef
651 struct _VgHashNode {
652 struct _VgHashNode * next;
nethercote3d6b6112004-11-04 16:39:43 +0000653 UWord key;
nethercote37aac2e2004-09-02 08:54:27 +0000654 }
655 VgHashNode;
656
657typedef
658 VgHashNode**
659 VgHashTable;
660
njn0674acb2005-03-13 06:04:49 +0000661/* Make a new table. Allocates the memory with VG_(calloc)(), so can be freed
662 * with VG_(free)(). */
nethercote37aac2e2004-09-02 08:54:27 +0000663extern VgHashTable VG_(HT_construct) ( void );
664
665/* Count the number of nodes in a table. */
666extern Int VG_(HT_count_nodes) ( VgHashTable table );
667
668/* Add a node to the table. */
669extern void VG_(HT_add_node) ( VgHashTable t, VgHashNode* node );
670
671/* Looks up a node in the hash table. Also returns the address of the
672 previous node's `next' pointer which allows it to be removed from the
673 list later without having to look it up again. */
nethercote3d6b6112004-11-04 16:39:43 +0000674extern VgHashNode* VG_(HT_get_node) ( VgHashTable t, UWord key,
nethercote37aac2e2004-09-02 08:54:27 +0000675 /*OUT*/VgHashNode*** next_ptr );
676
677/* Allocates an array of pointers to all the shadow chunks of malloc'd
678 blocks. Must be freed with VG_(free)(). */
679extern VgHashNode** VG_(HT_to_array) ( VgHashTable t, /*OUT*/ UInt* n_shadows );
680
681/* Returns first node that matches predicate `p', or NULL if none do.
682 Extra arguments can be implicitly passed to `p' using `d' which is an
683 opaque pointer passed to `p' each time it is called. */
684extern VgHashNode* VG_(HT_first_match) ( VgHashTable t,
685 Bool (*p)(VgHashNode*, void*),
686 void* d );
687
688/* Applies a function f() once to each node. Again, `d' can be used
689 to pass extra information to the function. */
690extern void VG_(HT_apply_to_all_nodes)( VgHashTable t,
691 void (*f)(VgHashNode*, void*),
692 void* d );
693
694/* Destroy a table. */
695extern void VG_(HT_destruct) ( VgHashTable t );
696
697
698/*====================================================================*/
699/*=== A generic skiplist ===*/
700/*====================================================================*/
701
702/*
703 The idea here is that the skiplist puts its per-element data at the
704 end of the structure. When you initialize the skiplist, you tell
705 it what structure your list elements are going to be. Then you
706 should allocate them with VG_(SkipNode_Alloc), which will allocate
707 enough memory for the extra bits.
708 */
nethercote37aac2e2004-09-02 08:54:27 +0000709
710typedef struct _SkipList SkipList;
711typedef struct _SkipNode SkipNode;
712
713typedef Int (*SkipCmp_t)(const void *key1, const void *key2);
714
715struct _SkipList {
716 const Short arena; /* allocation arena */
717 const UShort size; /* structure size (not including SkipNode) */
718 const UShort keyoff; /* key offset */
719 const SkipCmp_t cmp; /* compare two keys */
720 Char * (*strkey)(void *); /* stringify a key (for debugging) */
721 SkipNode *head; /* list head */
722};
723
724/* Use this macro to initialize your skiplist head. The arguments are pretty self explanitory:
725 _type is the type of your element structure
726 _key is the field within that type which you want to use as the key
727 _cmp is the comparison function for keys - it gets two typeof(_key) pointers as args
728 _strkey is a function which can return a string of your key - it's only used for debugging
729 _arena is the arena to use for allocation - -1 is the default
730 */
njnbe91aae2005-03-27 01:42:41 +0000731#define VG_SKIPLIST_INIT(_type, _key, _cmp, _strkey, _arena) \
nethercote37aac2e2004-09-02 08:54:27 +0000732 { \
733 .arena = _arena, \
734 .size = sizeof(_type), \
735 .keyoff = offsetof(_type, _key), \
736 .cmp = _cmp, \
737 .strkey = _strkey, \
738 .head = NULL, \
739 }
740
741/* List operations:
sewardjb5f6f512005-03-10 23:59:00 +0000742 SkipList_Find_* search a list. The 3 variants are:
743 Before: returns a node which is <= key, or NULL if none
744 Exact: returns a node which is == key, or NULL if none
745 After: returns a node which is >= key, or NULL if none
nethercote37aac2e2004-09-02 08:54:27 +0000746 SkipList_Insert inserts a new element into the list. Duplicates are
747 forbidden. The element must have been created with SkipList_Alloc!
748 SkipList_Remove removes an element from the list and returns it. It
749 doesn't free the memory.
750*/
sewardjb5f6f512005-03-10 23:59:00 +0000751extern void *VG_(SkipList_Find_Before) (const SkipList *l, void *key);
752extern void *VG_(SkipList_Find_Exact) (const SkipList *l, void *key);
753extern void *VG_(SkipList_Find_After) (const SkipList *l, void *key);
754extern void VG_(SkipList_Insert) ( SkipList *l, void *data);
755extern void *VG_(SkipList_Remove) ( SkipList *l, void *key);
756
757/* Some useful standard comparisons */
758extern Int VG_(cmp_Addr) (const void *a, const void *b);
759extern Int VG_(cmp_Int) (const void *a, const void *b);
760extern Int VG_(cmp_UInt) (const void *a, const void *b);
761extern Int VG_(cmp_string)(const void *a, const void *b);
nethercote37aac2e2004-09-02 08:54:27 +0000762
763/* Node (element) operations:
764 SkipNode_Alloc: allocate memory for a new element on the list. Must be
765 used before an element can be inserted! Returns NULL if not enough
766 memory.
767 SkipNode_Free: free memory allocated above
768 SkipNode_First: return the first element on the list
769 SkipNode_Next: return the next element after "data" on the list -
770 NULL for none
771
772 You can iterate through a SkipList like this:
773
774 for(x = VG_(SkipNode_First)(&list); // or SkipList_Find
775 x != NULL;
776 x = VG_(SkipNode_Next)(&list, x)) { ... }
777*/
778extern void *VG_(SkipNode_Alloc) (const SkipList *l);
779extern void VG_(SkipNode_Free) (const SkipList *l, void *p);
780extern void *VG_(SkipNode_First) (const SkipList *l);
781extern void *VG_(SkipNode_Next) (const SkipList *l, void *data);
782
njnabb14ad2004-11-24 16:57:16 +0000783
nethercote37aac2e2004-09-02 08:54:27 +0000784/*====================================================================*/
785/*=== Functions for shadow registers ===*/
786/*====================================================================*/
787
njnabb14ad2004-11-24 16:57:16 +0000788// For get/set, 'area' is where the asked-for shadow state will be copied
789// into/from.
790extern void VG_(get_shadow_regs_area) ( ThreadId tid, OffT guest_state_offset,
791 SizeT size, UChar* area );
792extern void VG_(set_shadow_regs_area) ( ThreadId tid, OffT guest_state_offset,
793 SizeT size, const UChar* area );
nethercote37aac2e2004-09-02 08:54:27 +0000794
795/* This one lets you override the shadow of the return value register for a
njn26f02512004-11-22 18:33:15 +0000796 syscall. Call it from TL_(post_syscall)() (not TL_(pre_syscall)()!) to
nethercote37aac2e2004-09-02 08:54:27 +0000797 override the default shadow register value. */
798extern void VG_(set_return_from_syscall_shadow) ( ThreadId tid,
njncf45fd42004-11-24 16:30:22 +0000799 UWord ret_shadow );
nethercote37aac2e2004-09-02 08:54:27 +0000800
njn26f02512004-11-22 18:33:15 +0000801/* This can be called from TL_(fini)() to find the shadow of the argument
nethercote37aac2e2004-09-02 08:54:27 +0000802 to exit(), ie. the shadow of the program's return value. */
sewardj2a99cf62004-11-24 10:44:19 +0000803extern UInt VG_(get_exit_status_shadow) ( ThreadId );
nethercote37aac2e2004-09-02 08:54:27 +0000804
805
806/*====================================================================*/
807/*=== Specific stuff for replacing malloc() and friends ===*/
808/*====================================================================*/
809
810/* If a tool replaces malloc() et al, the easiest way to do so is to
811 link with vg_replace_malloc.o into its vgpreload_*.so file, and
812 follow the following instructions. You can do it from scratch,
813 though, if you enjoy that sort of thing. */
814
njn26f02512004-11-22 18:33:15 +0000815/* Can be called from TL_(malloc) et al to do the actual alloc/freeing. */
nethercote7ac7f7b2004-11-02 12:36:02 +0000816extern void* VG_(cli_malloc) ( SizeT align, SizeT nbytes );
nethercote37aac2e2004-09-02 08:54:27 +0000817extern void VG_(cli_free) ( void* p );
818
819/* Check if an address is within a range, allowing for redzones at edges */
nethercote7ac7f7b2004-11-02 12:36:02 +0000820extern Bool VG_(addr_is_in_block)( Addr a, Addr start, SizeT size );
nethercote37aac2e2004-09-02 08:54:27 +0000821
822/* ------------------------------------------------------------------ */
823/* Some options that can be used by a tool if malloc() et al are replaced.
824 The tool should call the functions in the appropriate places to give
825 control over these aspects of Valgrind's version of malloc(). */
826
nethercote37aac2e2004-09-02 08:54:27 +0000827/* DEBUG: print malloc details? default: NO */
828extern Bool VG_(clo_trace_malloc);
829/* Minimum alignment in functions that don't specify alignment explicitly.
njn38629a72005-03-13 06:03:45 +0000830 default: VG_MIN_MALLOC_SZB */
nethercote7ac7f7b2004-11-02 12:36:02 +0000831extern UInt VG_(clo_alignment);
nethercote37aac2e2004-09-02 08:54:27 +0000832
833extern Bool VG_(replacement_malloc_process_cmd_line_option) ( Char* arg );
834extern void VG_(replacement_malloc_print_usage) ( void );
835extern void VG_(replacement_malloc_print_debug_usage) ( void );
836
837
838/*====================================================================*/
839/*=== Tool-specific stuff ===*/
840/*====================================================================*/
841
842/* ------------------------------------------------------------------ */
njn8a97c6d2005-03-31 04:37:24 +0000843/* Basic tool functions */
844
845extern void VG_(basic_tool_funcs)(
846 void(*post_clo_init)(void),
847 IRBB*(*instrument)(IRBB* bb_in, VexGuestLayout* layout,
848 IRType gWordTy, IRType hWordTy ),
849 void(*fini)(Int)
850);
851
852/* ------------------------------------------------------------------ */
nethercote37aac2e2004-09-02 08:54:27 +0000853/* Details */
854
855/* Default value for avg_translations_sizeB (in bytes), indicating typical
856 code expansion of about 6:1. */
857#define VG_DEFAULT_TRANS_SIZEB 100
858
859/* Information used in the startup message. `name' also determines the
860 string used for identifying suppressions in a suppression file as
861 belonging to this tool. `version' can be NULL, in which case (not
862 surprisingly) no version info is printed; this mechanism is designed for
863 tools distributed with Valgrind that share a version number with
864 Valgrind. Other tools not distributed as part of Valgrind should
865 probably have their own version number. */
866extern void VG_(details_name) ( Char* name );
867extern void VG_(details_version) ( Char* version );
868extern void VG_(details_description) ( Char* description );
869extern void VG_(details_copyright_author) ( Char* copyright_author );
870
871/* Average size of a translation, in bytes, so that the translation
872 storage machinery can allocate memory appropriately. Not critical,
873 setting is optional. */
874extern void VG_(details_avg_translation_sizeB) ( UInt size );
875
njn67993252004-11-22 18:02:32 +0000876/* String printed if an `tl_assert' assertion fails or VG_(tool_panic)
nethercote37aac2e2004-09-02 08:54:27 +0000877 is called. Should probably be an email address. */
878extern void VG_(details_bug_reports_to) ( Char* bug_reports_to );
879
880/* ------------------------------------------------------------------ */
881/* Needs */
882
883/* Booleans that decide core behaviour, but don't require extra
884 operations to be defined if `True' */
885
886/* Should __libc_freeres() be run? Bugs in it can crash the tool. */
887extern void VG_(needs_libc_freeres) ( void );
888
889/* Want to have errors detected by Valgrind's core reported? Includes:
890 - pthread API errors (many; eg. unlocking a non-locked mutex)
891 - invalid file descriptors to blocking syscalls read() and write()
892 - bad signal numbers passed to sigaction()
893 - attempt to install signal handler for SIGKILL or SIGSTOP */
894extern void VG_(needs_core_errors) ( void );
895
896/* Booleans that indicate extra operations are defined; if these are True,
897 the corresponding template functions (given below) must be defined. A
898 lot like being a member of a type class. */
899
900/* Want to report errors from tool? This implies use of suppressions, too. */
njn8a97c6d2005-03-31 04:37:24 +0000901extern void VG_(needs_tool_errors) (
902 // Identify if two errors are equal, or equal enough. `res' indicates how
903 // close is "close enough". `res' should be passed on as necessary, eg. if
904 // the Error's `extra' part contains an ExeContext, `res' should be
905 // passed to VG_(eq_ExeContext)() if the ExeContexts are considered. Other
906 // than that, probably don't worry about it unless you have lots of very
907 // similar errors occurring.
908 Bool (*eq_Error)(VgRes res, Error* e1, Error* e2),
909
910 // Print error context.
911 void (*pp_Error)(Error* err),
912
913 // Should fill in any details that could be postponed until after the
914 // decision whether to ignore the error (ie. details not affecting the
915 // result of TL_(eq_Error)()). This saves time when errors are ignored.
916 // Yuk.
917 // Return value: must be the size of the `extra' part in bytes -- used by
918 // the core to make a copy.
919 UInt (*update_extra)(Error* err),
920
921 // Return value indicates recognition. If recognised, must set skind using
922 // VG_(set_supp_kind)().
923 Bool (*recognised_suppression)(Char* name, Supp* su),
924
925 // Read any extra info for this suppression kind. Most likely for filling
926 // in the `extra' and `string' parts (with VG_(set_supp_{extra, string})())
927 // of a suppression if necessary. Should return False if a syntax error
928 // occurred, True otherwise.
929 Bool (*read_extra_suppression_info)(Int fd, Char* buf, Int nBuf, Supp* su),
930
931 // This should just check the kinds match and maybe some stuff in the
932 // `string' and `extra' field if appropriate (using VG_(get_supp_*)() to
933 // get the relevant suppression parts).
934 Bool (*error_matches_suppression)(Error* err, Supp* su),
935
936 // This should return the suppression name, for --gen-suppressions, or NULL
937 // if that error type cannot be suppressed. This is the inverse of
938 // TL_(recognised_suppression)().
939 Char* (*get_error_name)(Error* err),
940
941 // This should print any extra info for the error, for --gen-suppressions,
942 // including the newline. This is the inverse of
943 // TL_(read_extra_suppression_info)().
944 void (*print_extra_suppression_info)(Error* err)
945);
946
nethercote37aac2e2004-09-02 08:54:27 +0000947
948/* Is information kept about specific individual basic blocks? (Eg. for
949 cachegrind there are cost-centres for every instruction, stored at a
950 basic block level.) If so, it sometimes has to be discarded, because
951 .so mmap/munmap-ping or self-modifying code (informed by the
952 DISCARD_TRANSLATIONS user request) can cause one instruction address
953 to be used for more than one instruction in one program run... */
njn8a97c6d2005-03-31 04:37:24 +0000954extern void VG_(needs_basic_block_discards) (
955 // Should discard any information that pertains to specific basic blocks
956 // or instructions within the address range given.
957 void (*discard_basic_block_info)(Addr a, SizeT size)
958);
nethercote37aac2e2004-09-02 08:54:27 +0000959
nethercote37aac2e2004-09-02 08:54:27 +0000960/* Tool defines its own command line options? */
njn8a97c6d2005-03-31 04:37:24 +0000961extern void VG_(needs_command_line_options) (
962 // Return True if option was recognised. Presumably sets some state to
963 // record the option as well.
964 Bool (*process_cmd_line_option)(Char* argv),
965
966 // Print out command line usage for options for normal tool operation.
967 void (*print_usage)(void),
968
969 // Print out command line usage for options for debugging the tool.
970 void (*print_debug_usage)(void)
971);
nethercote37aac2e2004-09-02 08:54:27 +0000972
973/* Tool defines its own client requests? */
njn8a97c6d2005-03-31 04:37:24 +0000974extern void VG_(needs_client_requests) (
975 // If using client requests, the number of the first request should be equal
976 // to VG_USERREQ_TOOL_BASE('X', 'Y'), where 'X' and 'Y' form a suitable two
977 // character identification for the string. The second and subsequent
978 // requests should follow.
979 //
980 // This function should use the VG_IS_TOOL_USERREQ macro (in
981 // include/valgrind.h) to first check if it's a request for this tool. Then
982 // should handle it if it's recognised (and return True), or return False if
983 // not recognised. arg_block[0] holds the request number, any further args
984 // from the request are in arg_block[1..]. 'ret' is for the return value...
985 // it should probably be filled, if only with 0.
986 Bool (*handle_client_request)(ThreadId tid, UWord* arg_block, UWord* ret)
987);
nethercote37aac2e2004-09-02 08:54:27 +0000988
nethercote37aac2e2004-09-02 08:54:27 +0000989/* Tool does stuff before and/or after system calls? */
njn8a97c6d2005-03-31 04:37:24 +0000990// Nb: If either of the pre_ functions malloc() something to return, the
991// corresponding post_ function had better free() it!
992extern void VG_(needs_syscall_wrapper) (
993 void (* pre_syscall)(ThreadId tid, UInt syscallno),
994 void (*post_syscall)(ThreadId tid, UInt syscallno, Int res)
995);
nethercote37aac2e2004-09-02 08:54:27 +0000996
997/* Are tool-state sanity checks performed? */
njn8a97c6d2005-03-31 04:37:24 +0000998// Can be useful for ensuring a tool's correctness. TL_(cheap_sanity_check)
999// is called very frequently; TL_(expensive_sanity_check) is called less
1000// frequently and can be more involved.
1001extern void VG_(needs_sanity_checks) (
1002 Bool(*cheap_sanity_check)(void),
1003 Bool(*expensive_sanity_check)(void)
1004);
nethercote37aac2e2004-09-02 08:54:27 +00001005
1006/* Do we need to see data symbols? */
1007extern void VG_(needs_data_syms) ( void );
1008
1009/* Does the tool need shadow memory allocated (if you set this, you must also statically initialize
njn26f02512004-11-22 18:33:15 +00001010 float TL_(shadow_ratio) = n./m;
nethercote37aac2e2004-09-02 08:54:27 +00001011 to define how many shadow bits you need per client address space bit.
1012*/
1013extern void VG_(needs_shadow_memory)( void );
njn26f02512004-11-22 18:33:15 +00001014extern float TL_(shadow_ratio);
nethercote37aac2e2004-09-02 08:54:27 +00001015
1016/* ------------------------------------------------------------------ */
njn8a97c6d2005-03-31 04:37:24 +00001017/* Malloc replacement */
1018
1019// The 'p' prefix avoids GCC complaints about overshadowing global names.
1020extern void VG_(malloc_funcs)(
1021 void* (*pmalloc) ( ThreadId tid, SizeT n ),
1022 void* (*p__builtin_new) ( ThreadId tid, SizeT n ),
1023 void* (*p__builtin_vec_new) ( ThreadId tid, SizeT n ),
1024 void* (*pmemalign) ( ThreadId tid, SizeT align, SizeT n ),
1025 void* (*pcalloc) ( ThreadId tid, SizeT nmemb, SizeT size1 ),
1026 void (*pfree) ( ThreadId tid, void* p ),
1027 void (*p__builtin_delete) ( ThreadId tid, void* p ),
1028 void (*p__builtin_vec_delete) ( ThreadId tid, void* p ),
1029 void* (*prealloc) ( ThreadId tid, void* p, SizeT new_size ),
1030 SizeT client_malloc_redzone_szB
1031);
1032
1033/* ------------------------------------------------------------------ */
nethercote37aac2e2004-09-02 08:54:27 +00001034/* Core events to track */
1035
1036/* Part of the core from which this call was made. Useful for determining
1037 what kind of error message should be emitted. */
1038typedef
njncf45fd42004-11-24 16:30:22 +00001039 enum { Vg_CoreStartup, Vg_CorePThread, Vg_CoreSignal, Vg_CoreSysCall,
1040 Vg_CoreTranslate, Vg_CoreClientReq }
nethercote37aac2e2004-09-02 08:54:27 +00001041 CorePart;
1042
nethercote37aac2e2004-09-02 08:54:27 +00001043#endif /* NDEF __TOOL_H */
1044
1045/* gen_toolint.pl will put the VG_(init_*)() functions here: */