blob: 7ae35d10639a17d9104a44adaa3487c7f3cdbb99 [file] [log] [blame]
njn43b9a8a2005-05-10 04:37:01 +00001
2/*--------------------------------------------------------------------*/
3/*--- The core/tool interface. pub_tool_tooliface.h ---*/
4/*--------------------------------------------------------------------*/
5
6/*
7 This file is part of Valgrind, a dynamic binary instrumentation
8 framework.
9
10 Copyright (C) 2000-2005 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 __PUB_TOOL_TOOLIFACE_H
32#define __PUB_TOOL_TOOLIFACE_H
33
njnacd885a2005-05-16 20:40:51 +000034#include "pub_tool_errormgr.h" // for Error, Supp
35#include "libvex.h" // for VexGuestLayout
36
njn43b9a8a2005-05-10 04:37:01 +000037/* ------------------------------------------------------------------ */
38/* The interface version */
39
40/* The version number indicates binary-incompatible changes to the
41 interface; if the core and tool versions don't match, Valgrind
42 will abort. */
njn1d0cb0d2005-08-15 01:52:02 +000043#define VG_CORE_INTERFACE_VERSION 9
njn43b9a8a2005-05-10 04:37:01 +000044
45typedef struct _ToolInfo {
46 Int sizeof_ToolInfo;
47 Int interface_version;
48
49 /* Initialise tool. Must do the following:
50 - initialise the `details' struct, via the VG_(details_*)() functions
51 - register any helpers called by generated code
52
53 May do the following:
54 - initialise the `needs' struct to indicate certain requirements, via
55 the VG_(needs_*)() functions
56 - initialize all the tool's entrypoints via the VG_(init_*)() functions
57 - register any tool-specific profiling events
58 - any other tool-specific initialisation
59 */
60 void (*tl_pre_clo_init) ( void );
njn43b9a8a2005-05-10 04:37:01 +000061} ToolInfo;
62
sewardj45f4e7c2005-09-27 19:20:21 +000063extern const ToolInfo VG_(tool_info);
64
njn43b9a8a2005-05-10 04:37:01 +000065/* Every tool must include this macro somewhere, exactly once. */
sewardj45f4e7c2005-09-27 19:20:21 +000066#define VG_DETERMINE_INTERFACE_VERSION(pre_clo_init) \
njn43b9a8a2005-05-10 04:37:01 +000067 const ToolInfo VG_(tool_info) = { \
68 .sizeof_ToolInfo = sizeof(ToolInfo), \
69 .interface_version = VG_CORE_INTERFACE_VERSION, \
70 .tl_pre_clo_init = pre_clo_init, \
njn43b9a8a2005-05-10 04:37:01 +000071 };
72
73/* ------------------------------------------------------------------ */
74/* Basic tool functions */
75
sewardj461df9c2006-01-17 02:06:39 +000076/* The tool_instrument function is passed as a callback to
77 LibVEX_Translate. VgInstrumentClosure carries additional info
78 which the instrumenter might like to know, but which is opaque to
79 Vex.
80*/
81typedef
82 struct {
83 Addr64 nraddr; /* non-redirected guest address */
84 Addr64 readdr; /* redirected guest address */
85 ThreadId tid; /* tid requesting translation */
86 }
87 VgCallbackClosure;
88
njn43b9a8a2005-05-10 04:37:01 +000089extern void VG_(basic_tool_funcs)(
90 // Do any initialisation that can only be done after command line
91 // processing.
92 void (*post_clo_init)(void),
93
sewardj4ba057c2005-10-18 12:04:18 +000094 // Instrument a basic block. Must be a true function, ie. the same
95 // input always results in the same output, because basic blocks
96 // can be retranslated. Unless you're doing something really
97 // strange... Note that orig_addr_noredir is not necessarily the
98 // same as the address of the first instruction in the IR, due to
99 // function redirection.
sewardj461df9c2006-01-17 02:06:39 +0000100 IRBB*(*instrument)(VgCallbackClosure*,
101 IRBB* bb_in,
102 VexGuestLayout*, VexGuestExtents*,
103 IRType gWordTy, IRType hWordTy),
njn43b9a8a2005-05-10 04:37:01 +0000104
105 // Finish up, print out any results, etc. `exitcode' is program's exit
106 // code. The shadow can be found with VG_(get_exit_status_shadow)().
107 void (*fini)(Int)
108);
109
110/* ------------------------------------------------------------------ */
111/* Details */
112
113/* Default value for avg_translations_sizeB (in bytes), indicating typical
114 code expansion of about 6:1. */
115#define VG_DEFAULT_TRANS_SIZEB 100
116
117/* Information used in the startup message. `name' also determines the
118 string used for identifying suppressions in a suppression file as
119 belonging to this tool. `version' can be NULL, in which case (not
120 surprisingly) no version info is printed; this mechanism is designed for
121 tools distributed with Valgrind that share a version number with
122 Valgrind. Other tools not distributed as part of Valgrind should
123 probably have their own version number. */
124extern void VG_(details_name) ( Char* name );
125extern void VG_(details_version) ( Char* version );
126extern void VG_(details_description) ( Char* description );
127extern void VG_(details_copyright_author) ( Char* copyright_author );
128
129/* Average size of a translation, in bytes, so that the translation
130 storage machinery can allocate memory appropriately. Not critical,
131 setting is optional. */
132extern void VG_(details_avg_translation_sizeB) ( UInt size );
133
134/* String printed if an `tl_assert' assertion fails or VG_(tool_panic)
135 is called. Should probably be an email address. */
136extern void VG_(details_bug_reports_to) ( Char* bug_reports_to );
137
138/* ------------------------------------------------------------------ */
139/* Needs */
140
141/* Booleans that decide core behaviour, but don't require extra
142 operations to be defined if `True' */
143
144/* Should __libc_freeres() be run? Bugs in it can crash the tool. */
145extern void VG_(needs_libc_freeres) ( void );
146
147/* Want to have errors detected by Valgrind's core reported? Includes:
njn0087c502005-07-01 04:15:36 +0000148 - pthread API errors (many; eg. unlocking a non-locked mutex)
149 [currently disabled]
150 - invalid file descriptors to syscalls like read() and write()
njn43b9a8a2005-05-10 04:37:01 +0000151 - bad signal numbers passed to sigaction()
152 - attempt to install signal handler for SIGKILL or SIGSTOP */
153extern void VG_(needs_core_errors) ( void );
154
155/* Booleans that indicate extra operations are defined; if these are True,
156 the corresponding template functions (given below) must be defined. A
157 lot like being a member of a type class. */
158
159/* Want to report errors from tool? This implies use of suppressions, too. */
160extern void VG_(needs_tool_errors) (
161 // Identify if two errors are equal, or equal enough. `res' indicates how
162 // close is "close enough". `res' should be passed on as necessary, eg. if
163 // the Error's `extra' part contains an ExeContext, `res' should be
164 // passed to VG_(eq_ExeContext)() if the ExeContexts are considered. Other
165 // than that, probably don't worry about it unless you have lots of very
166 // similar errors occurring.
167 Bool (*eq_Error)(VgRes res, Error* e1, Error* e2),
168
169 // Print error context.
170 void (*pp_Error)(Error* err),
171
172 // Should fill in any details that could be postponed until after the
173 // decision whether to ignore the error (ie. details not affecting the
174 // result of VG_(tdict).tool_eq_Error()). This saves time when errors
175 // are ignored.
176 // Yuk.
177 // Return value: must be the size of the `extra' part in bytes -- used by
178 // the core to make a copy.
179 UInt (*update_extra)(Error* err),
180
181 // Return value indicates recognition. If recognised, must set skind using
182 // VG_(set_supp_kind)().
183 Bool (*recognised_suppression)(Char* name, Supp* su),
184
185 // Read any extra info for this suppression kind. Most likely for filling
186 // in the `extra' and `string' parts (with VG_(set_supp_{extra, string})())
187 // of a suppression if necessary. Should return False if a syntax error
188 // occurred, True otherwise.
189 Bool (*read_extra_suppression_info)(Int fd, Char* buf, Int nBuf, Supp* su),
190
191 // This should just check the kinds match and maybe some stuff in the
192 // `string' and `extra' field if appropriate (using VG_(get_supp_*)() to
193 // get the relevant suppression parts).
194 Bool (*error_matches_suppression)(Error* err, Supp* su),
195
196 // This should return the suppression name, for --gen-suppressions, or NULL
197 // if that error type cannot be suppressed. This is the inverse of
198 // VG_(tdict).tool_recognised_suppression().
199 Char* (*get_error_name)(Error* err),
200
201 // This should print any extra info for the error, for --gen-suppressions,
202 // including the newline. This is the inverse of
203 // VG_(tdict).tool_read_extra_suppression_info().
204 void (*print_extra_suppression_info)(Error* err)
205);
206
sewardj5155dec2005-10-12 10:09:23 +0000207/* Is information kept by the tool about specific instructions or
208 translations? (Eg. for cachegrind there are cost-centres for every
209 instruction, stored in a per-translation fashion.) If so, the info
210 may have to be discarded when translations are unloaded (eg. due to
211 .so unloading, or otherwise at the discretion of m_transtab, eg
212 when the table becomes too full) to avoid stale information being
213 reused for new translations. */
njn43b9a8a2005-05-10 04:37:01 +0000214extern void VG_(needs_basic_block_discards) (
sewardj5155dec2005-10-12 10:09:23 +0000215 // Discard any information that pertains to specific translations
sewardj4ba057c2005-10-18 12:04:18 +0000216 // or instructions within the address range given. There are two
217 // possible approaches.
218 // - If info is being stored at a per-translation level, use orig_addr
219 // to identify which translation is being discarded. Each translation
220 // will be discarded exactly once.
221 // This orig_addr will match the orig_addr which was passed to
222 // to instrument() when this translation was made. Note that orig_addr
223 // won't necessarily be the same as the first address in "extents".
sewardj5155dec2005-10-12 10:09:23 +0000224 // - If info is being stored at a per-instruction level, you can get
225 // the address range(s) being discarded by stepping through "extents".
226 // Note that any single instruction may belong to more than one
227 // translation, and so could be covered by the "extents" of more than
228 // one call to this function.
229 // Doing it the first way (as eg. Cachegrind does) is probably easier.
sewardj4ba057c2005-10-18 12:04:18 +0000230 void (*discard_basic_block_info)(Addr64 orig_addr, VexGuestExtents extents)
njn43b9a8a2005-05-10 04:37:01 +0000231);
232
233/* Tool defines its own command line options? */
234extern void VG_(needs_command_line_options) (
235 // Return True if option was recognised. Presumably sets some state to
236 // record the option as well.
237 Bool (*process_cmd_line_option)(Char* argv),
238
239 // Print out command line usage for options for normal tool operation.
240 void (*print_usage)(void),
241
242 // Print out command line usage for options for debugging the tool.
243 void (*print_debug_usage)(void)
244);
245
246/* Tool defines its own client requests? */
247extern void VG_(needs_client_requests) (
248 // If using client requests, the number of the first request should be equal
249 // to VG_USERREQ_TOOL_BASE('X', 'Y'), where 'X' and 'Y' form a suitable two
250 // character identification for the string. The second and subsequent
251 // requests should follow.
252 //
253 // This function should use the VG_IS_TOOL_USERREQ macro (in
254 // include/valgrind.h) to first check if it's a request for this tool. Then
255 // should handle it if it's recognised (and return True), or return False if
256 // not recognised. arg_block[0] holds the request number, any further args
257 // from the request are in arg_block[1..]. 'ret' is for the return value...
258 // it should probably be filled, if only with 0.
259 Bool (*handle_client_request)(ThreadId tid, UWord* arg_block, UWord* ret)
260);
261
262/* Tool does stuff before and/or after system calls? */
263// Nb: If either of the pre_ functions malloc() something to return, the
264// corresponding post_ function had better free() it!
265extern void VG_(needs_syscall_wrapper) (
266 void (* pre_syscall)(ThreadId tid, UInt syscallno),
sewardja8d8e232005-06-07 20:04:56 +0000267 void (*post_syscall)(ThreadId tid, UInt syscallno, SysRes res)
njn43b9a8a2005-05-10 04:37:01 +0000268);
269
270/* Are tool-state sanity checks performed? */
271// Can be useful for ensuring a tool's correctness. cheap_sanity_check()
272// is called very frequently; expensive_sanity_check() is called less
273// frequently and can be more involved.
274extern void VG_(needs_sanity_checks) (
275 Bool(*cheap_sanity_check)(void),
276 Bool(*expensive_sanity_check)(void)
277);
278
279/* Do we need to see data symbols? */
280extern void VG_(needs_data_syms) ( void );
281
282/* Does the tool need shadow memory allocated? */
283extern void VG_(needs_shadow_memory)( void );
284
njn09ca09b2005-10-16 17:48:09 +0000285/* Does the tool replace malloc() and friends with its own versions?
286 This has to be combined with the use of a vgpreload_<tool>.so module
287 or it won't work. See massif/Makefile.am for how to build it. */
njn43b9a8a2005-05-10 04:37:01 +0000288// The 'p' prefix avoids GCC complaints about overshadowing global names.
njnfc51f8d2005-06-21 03:20:17 +0000289extern void VG_(needs_malloc_replacement)(
njn43b9a8a2005-05-10 04:37:01 +0000290 void* (*pmalloc) ( ThreadId tid, SizeT n ),
291 void* (*p__builtin_new) ( ThreadId tid, SizeT n ),
292 void* (*p__builtin_vec_new) ( ThreadId tid, SizeT n ),
293 void* (*pmemalign) ( ThreadId tid, SizeT align, SizeT n ),
294 void* (*pcalloc) ( ThreadId tid, SizeT nmemb, SizeT size1 ),
295 void (*pfree) ( ThreadId tid, void* p ),
296 void (*p__builtin_delete) ( ThreadId tid, void* p ),
297 void (*p__builtin_vec_delete) ( ThreadId tid, void* p ),
298 void* (*prealloc) ( ThreadId tid, void* p, SizeT new_size ),
299 SizeT client_malloc_redzone_szB
300);
301
302/* ------------------------------------------------------------------ */
303/* Core events to track */
304
305/* Part of the core from which this call was made. Useful for determining
306 what kind of error message should be emitted. */
307typedef
308 enum { Vg_CoreStartup, Vg_CorePThread, Vg_CoreSignal, Vg_CoreSysCall,
309 Vg_CoreTranslate, Vg_CoreClientReq }
310 CorePart;
311
312/* Events happening in core to track. To be notified, pass a callback
313 function to the appropriate function. To ignore an event, don't do
314 anything (the default is for events to be ignored).
315
316 Note that most events aren't passed a ThreadId. If the event is one called
317 from generated code (eg. new_mem_stack_*), you can use
318 VG_(get_running_tid)() to find it. Otherwise, it has to be passed in,
319 as in pre_mem_read, and so the event signature will require changing.
320
321 Memory events (Nb: to track heap allocation/freeing, a tool must replace
322 malloc() et al. See above how to do this.)
323
324 These ones occur at startup, upon some signals, and upon some syscalls
325 */
326void VG_(track_new_mem_startup) (void(*f)(Addr a, SizeT len,
327 Bool rr, Bool ww, Bool xx));
328void VG_(track_new_mem_stack_signal)(void(*f)(Addr a, SizeT len));
329void VG_(track_new_mem_brk) (void(*f)(Addr a, SizeT len));
330void VG_(track_new_mem_mmap) (void(*f)(Addr a, SizeT len,
331 Bool rr, Bool ww, Bool xx));
332
333void VG_(track_copy_mem_remap) (void(*f)(Addr from, Addr to, SizeT len));
334void VG_(track_change_mem_mprotect) (void(*f)(Addr a, SizeT len,
335 Bool rr, Bool ww, Bool xx));
336void VG_(track_die_mem_stack_signal)(void(*f)(Addr a, SizeT len));
337void VG_(track_die_mem_brk) (void(*f)(Addr a, SizeT len));
338void VG_(track_die_mem_munmap) (void(*f)(Addr a, SizeT len));
339
340/* These ones are called when SP changes. A tool could track these itself
341 (except for ban_mem_stack) but it's much easier to use the core's help.
342
343 The specialised ones are called in preference to the general one, if they
344 are defined. These functions are called a lot if they are used, so
345 specialising can optimise things significantly. If any of the
346 specialised cases are defined, the general case must be defined too.
347
njnaf839f52005-06-23 03:27:57 +0000348 Nb: all the specialised ones must use the VG_REGPARM(n) attribute.
njn43b9a8a2005-05-10 04:37:01 +0000349 */
sewardjf5c8e372006-02-12 15:42:20 +0000350void VG_(track_new_mem_stack_4) (VG_REGPARM(1) void(*f)(Addr new_ESP));
351void VG_(track_new_mem_stack_8) (VG_REGPARM(1) void(*f)(Addr new_ESP));
352void VG_(track_new_mem_stack_12) (VG_REGPARM(1) void(*f)(Addr new_ESP));
353void VG_(track_new_mem_stack_16) (VG_REGPARM(1) void(*f)(Addr new_ESP));
354void VG_(track_new_mem_stack_32) (VG_REGPARM(1) void(*f)(Addr new_ESP));
355void VG_(track_new_mem_stack_112)(VG_REGPARM(1) void(*f)(Addr new_ESP));
356void VG_(track_new_mem_stack_128)(VG_REGPARM(1) void(*f)(Addr new_ESP));
357void VG_(track_new_mem_stack_144)(VG_REGPARM(1) void(*f)(Addr new_ESP));
358void VG_(track_new_mem_stack_160)(VG_REGPARM(1) void(*f)(Addr new_ESP));
359void VG_(track_new_mem_stack) (void(*f)(Addr a, SizeT len));
njn43b9a8a2005-05-10 04:37:01 +0000360
sewardjf5c8e372006-02-12 15:42:20 +0000361void VG_(track_die_mem_stack_4) (VG_REGPARM(1) void(*f)(Addr die_ESP));
362void VG_(track_die_mem_stack_8) (VG_REGPARM(1) void(*f)(Addr die_ESP));
363void VG_(track_die_mem_stack_12) (VG_REGPARM(1) void(*f)(Addr die_ESP));
364void VG_(track_die_mem_stack_16) (VG_REGPARM(1) void(*f)(Addr die_ESP));
365void VG_(track_die_mem_stack_32) (VG_REGPARM(1) void(*f)(Addr die_ESP));
366void VG_(track_die_mem_stack_112)(VG_REGPARM(1) void(*f)(Addr die_ESP));
367void VG_(track_die_mem_stack_128)(VG_REGPARM(1) void(*f)(Addr die_ESP));
368void VG_(track_die_mem_stack_144)(VG_REGPARM(1) void(*f)(Addr die_ESP));
369void VG_(track_die_mem_stack_160)(VG_REGPARM(1) void(*f)(Addr die_ESP));
370void VG_(track_die_mem_stack) (void(*f)(Addr a, SizeT len));
njn43b9a8a2005-05-10 04:37:01 +0000371
372/* Used for redzone at end of thread stacks */
373void VG_(track_ban_mem_stack) (void(*f)(Addr a, SizeT len));
374
375/* These ones occur around syscalls, signal handling, etc */
376void VG_(track_pre_mem_read) (void(*f)(CorePart part, ThreadId tid,
377 Char* s, Addr a, SizeT size));
378void VG_(track_pre_mem_read_asciiz)(void(*f)(CorePart part, ThreadId tid,
379 Char* s, Addr a));
380void VG_(track_pre_mem_write) (void(*f)(CorePart part, ThreadId tid,
381 Char* s, Addr a, SizeT size));
382void VG_(track_post_mem_write) (void(*f)(CorePart part, ThreadId tid,
383 Addr a, SizeT size));
384
385/* Register events. Use VG_(set_shadow_state_area)() to set the shadow regs
386 for these events. */
387void VG_(track_pre_reg_read) (void(*f)(CorePart part, ThreadId tid,
388 Char* s, OffT guest_state_offset,
389 SizeT size));
390void VG_(track_post_reg_write)(void(*f)(CorePart part, ThreadId tid,
391 OffT guest_state_offset,
392 SizeT size));
393
394/* This one is called for malloc() et al if they are replaced by a tool. */
395void VG_(track_post_reg_write_clientcall_return)(
396 void(*f)(ThreadId tid, OffT guest_state_offset, SizeT size, Addr f));
397
398
399/* Scheduler events (not exhaustive) */
400void VG_(track_thread_run)(void(*f)(ThreadId tid));
401
402
403/* Thread events (not exhaustive)
404
405 Called during thread create, before the new thread has run any
406 instructions (or touched any memory).
407 */
408void VG_(track_post_thread_create)(void(*f)(ThreadId tid, ThreadId child));
409void VG_(track_post_thread_join) (void(*f)(ThreadId joiner, ThreadId joinee));
410
411/* Mutex events (not exhaustive)
412 "void *mutex" is really a pthread_mutex *
413
414 Called before a thread can block while waiting for a mutex (called
415 regardless of whether the thread will block or not). */
416void VG_(track_pre_mutex_lock)(void(*f)(ThreadId tid, void* mutex));
417
418/* Called once the thread actually holds the mutex (always paired with
419 pre_mutex_lock). */
420void VG_(track_post_mutex_lock)(void(*f)(ThreadId tid, void* mutex));
421
422/* Called after a thread has released a mutex (no need for a corresponding
423 pre_mutex_unlock, because unlocking can't block). */
424void VG_(track_post_mutex_unlock)(void(*f)(ThreadId tid, void* mutex));
425
426/* Signal events (not exhaustive)
427
428 ... pre_send_signal, post_send_signal ...
429
430 Called before a signal is delivered; `alt_stack' indicates if it is
431 delivered on an alternative stack. */
432void VG_(track_pre_deliver_signal) (void(*f)(ThreadId tid, Int sigNo,
433 Bool alt_stack));
434/* Called after a signal is delivered. Nb: unfortunately, if the signal
435 handler longjmps, this won't be called. */
436void VG_(track_post_deliver_signal)(void(*f)(ThreadId tid, Int sigNo));
437
438/* Others... condition variables...
439 ...
njn43b9a8a2005-05-10 04:37:01 +0000440 */
njn43b9a8a2005-05-10 04:37:01 +0000441
442#endif // __PUB_TOOL_TOOLIFACE_H
443
444/*--------------------------------------------------------------------*/
445/*--- end ---*/
446/*--------------------------------------------------------------------*/