FreeBSD: add file descriptor tracking for _umtx_op
[valgrind.git] / coregrind / m_tooliface.c
blob58aa82ccf721fc0b0237332bf45e94160ed5c816
2 /*--------------------------------------------------------------------*/
3 /*--- Stuff relating to tool data structures. ---*/
4 /*--- m_tooliface.c ---*/
5 /*--------------------------------------------------------------------*/
7 /*
8 This file is part of Valgrind, a dynamic binary instrumentation
9 framework.
11 Copyright (C) 2000-2017 Nicholas Nethercote
12 njn@valgrind.org
14 This program is free software; you can redistribute it and/or
15 modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation; either version 2 of the
17 License, or (at your option) any later version.
19 This program is distributed in the hope that it will be useful, but
20 WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, see <http://www.gnu.org/licenses/>.
27 The GNU General Public License is contained in the file COPYING.
30 #include "pub_core_basics.h"
31 #include "pub_core_tooliface.h"
32 #include "pub_core_transtab.h" /* VG_(ok_to_discard_translations) */
34 // The core/tool dictionary of functions (initially zeroed, as we want it)
35 VgToolInterface VG_(tdict);
37 /*--------------------------------------------------------------------*/
38 /* Setting basic functions */
40 void VG_(basic_tool_funcs)(
41 void(*post_clo_init)(void),
42 IRSB*(*instrument)(VgCallbackClosure*, IRSB*,
43 const VexGuestLayout*, const VexGuestExtents*,
44 const VexArchInfo*, IRType, IRType),
45 void(*fini)(Int)
48 VG_(tdict).tool_post_clo_init = post_clo_init;
49 VG_(tdict).tool_instrument = instrument;
50 VG_(tdict).tool_fini = fini;
54 /*--------------------------------------------------------------------*/
55 /* Setting details */
57 /* Init with default values. */
58 VgDetails VG_(details) = {
59 .name = NULL,
60 .version = NULL,
61 .description = NULL,
62 .copyright_author = NULL,
63 .bug_reports_to = NULL,
64 .avg_translation_sizeB = VG_DEFAULT_TRANS_SIZEB,
67 /* Use macro because they're so repetitive */
68 #define DETAILS(type, detail) \
69 extern void VG_(details_##detail)(type detail) \
70 { \
71 VG_(details).detail = detail; \
74 DETAILS(const HChar*, name)
75 DETAILS(const HChar*, version)
76 DETAILS(const HChar*, description)
77 DETAILS(const HChar*, copyright_author)
78 DETAILS(const HChar*, bug_reports_to)
79 DETAILS(UInt, avg_translation_sizeB)
82 /*--------------------------------------------------------------------*/
83 /* Setting needs */
85 VgNeeds VG_(needs) = {
86 .core_errors = False,
87 .tool_errors = False,
88 .libc_freeres = False,
89 .cxx_freeres = False,
90 .superblock_discards = False,
91 .command_line_options = False,
92 .client_requests = False,
93 .syscall_wrapper = False,
94 .sanity_checks = False,
95 .print_stats = False,
96 .info_location = False,
97 .var_info = False,
98 .malloc_replacement = False,
99 .xml_output = False,
100 .final_IR_tidy_pass = False
103 /* static */
104 Bool VG_(finish_needs_init)(const HChar** failmsg)
106 Bool any_new_mem_stack_N, any_new_mem_stack_N_w_ECU;
107 Bool any_new_mem_stack_w_conflicting_otags;
108 Bool any_die_mem_stack_N;
110 #define CHECK_NOT(var, value) \
111 if ((var)==(value)) { \
112 *failmsg = "Tool error: '" #var "' not initialised\n"; \
113 return False; \
116 /* Ones that must be set */
117 CHECK_NOT(VG_(details).name, NULL);
118 /* Nb: .version can be NULL */
119 CHECK_NOT(VG_(details).description, NULL);
120 CHECK_NOT(VG_(details).copyright_author, NULL);
121 CHECK_NOT(VG_(details).bug_reports_to, NULL);
123 /* Check that new_mem_stack is defined if any new_mem_stack_N
124 are. */
125 any_new_mem_stack_N
126 = VG_(tdict).track_new_mem_stack_4 ||
127 VG_(tdict).track_new_mem_stack_8 ||
128 VG_(tdict).track_new_mem_stack_12 ||
129 VG_(tdict).track_new_mem_stack_16 ||
130 VG_(tdict).track_new_mem_stack_32 ||
131 VG_(tdict).track_new_mem_stack_112 ||
132 VG_(tdict).track_new_mem_stack_128 ||
133 VG_(tdict).track_new_mem_stack_144 ||
134 VG_(tdict).track_new_mem_stack_160;
136 if (any_new_mem_stack_N && ! VG_(tdict).track_new_mem_stack) {
137 *failmsg = "Tool error: one of the specialised 'new_mem_stack_N'\n"
138 " events tracked, but not the generic 'new_mem_stack' one.\n"
139 " 'new_mem_stack' should be defined\n";
140 return False;
143 /* Check that new_mem_stack_w_ECU is defined if any
144 new_mem_stack_N_w_ECU are. */
145 any_new_mem_stack_N_w_ECU
146 = VG_(tdict).track_new_mem_stack_4_w_ECU ||
147 VG_(tdict).track_new_mem_stack_8_w_ECU ||
148 VG_(tdict).track_new_mem_stack_12_w_ECU ||
149 VG_(tdict).track_new_mem_stack_16_w_ECU ||
150 VG_(tdict).track_new_mem_stack_32_w_ECU ||
151 VG_(tdict).track_new_mem_stack_112_w_ECU ||
152 VG_(tdict).track_new_mem_stack_128_w_ECU ||
153 VG_(tdict).track_new_mem_stack_144_w_ECU ||
154 VG_(tdict).track_new_mem_stack_160_w_ECU;
156 if (any_new_mem_stack_N_w_ECU && ! VG_(tdict).track_new_mem_stack_w_ECU) {
157 *failmsg = "Tool error: one of the specialised 'new_mem_stack_N_w_ECU'\n"
158 " events tracked, but not the generic 'new_mem_stack_w_ECU' one.\n"
159 " 'new_mem_stack_w_ECU' should be defined\n";
160 return False;
163 /* Check that in no cases are both with- and without-otag versions of the
164 same new_mem_stack_ function defined. */
165 any_new_mem_stack_w_conflicting_otags
166 = (VG_(tdict).track_new_mem_stack_4 && VG_(tdict).track_new_mem_stack_4_w_ECU) ||
167 (VG_(tdict).track_new_mem_stack_8 && VG_(tdict).track_new_mem_stack_8_w_ECU) ||
168 (VG_(tdict).track_new_mem_stack_12 && VG_(tdict).track_new_mem_stack_12_w_ECU) ||
169 (VG_(tdict).track_new_mem_stack_16 && VG_(tdict).track_new_mem_stack_16_w_ECU) ||
170 (VG_(tdict).track_new_mem_stack_32 && VG_(tdict).track_new_mem_stack_32_w_ECU) ||
171 (VG_(tdict).track_new_mem_stack_112 && VG_(tdict).track_new_mem_stack_112_w_ECU) ||
172 (VG_(tdict).track_new_mem_stack_128 && VG_(tdict).track_new_mem_stack_128_w_ECU) ||
173 (VG_(tdict).track_new_mem_stack_144 && VG_(tdict).track_new_mem_stack_144_w_ECU) ||
174 (VG_(tdict).track_new_mem_stack_160 && VG_(tdict).track_new_mem_stack_160_w_ECU) ||
175 (VG_(tdict).track_new_mem_stack && VG_(tdict).track_new_mem_stack_w_ECU);
177 if (any_new_mem_stack_w_conflicting_otags) {
178 *failmsg = "Tool error: tool supplies both a 'new_mem_stack_N' and a\n"
179 " 'new_mem_stack_N_w_ECU' function for some N (or none),\n"
180 " but you can only have one or the other (not both)\n";
181 return False;
183 VG_(tdict).any_new_mem_stack
184 = VG_(tdict).track_new_mem_stack || VG_(tdict).track_new_mem_stack_w_ECU
185 || any_new_mem_stack_N || any_new_mem_stack_N_w_ECU;
187 /* Check that die_mem_stack is defined if any die_mem_stack_N
188 are. */
189 any_die_mem_stack_N
190 = VG_(tdict).track_die_mem_stack_4 ||
191 VG_(tdict).track_die_mem_stack_8 ||
192 VG_(tdict).track_die_mem_stack_12 ||
193 VG_(tdict).track_die_mem_stack_16 ||
194 VG_(tdict).track_die_mem_stack_32 ||
195 VG_(tdict).track_die_mem_stack_112 ||
196 VG_(tdict).track_die_mem_stack_128 ||
197 VG_(tdict).track_die_mem_stack_144 ||
198 VG_(tdict).track_die_mem_stack_160;
200 if (any_die_mem_stack_N && ! VG_(tdict).track_die_mem_stack) {
201 *failmsg = "Tool error: one of the specialised 'die_mem_stack_N'\n"
202 " events tracked, but not the generic 'die_mem_stack' one.\n"
203 " 'die_mem_stack' should be defined\n";
204 return False;
206 VG_(tdict).any_die_mem_stack
207 = VG_(tdict).track_die_mem_stack || any_die_mem_stack_N;
209 return True;
211 #undef CHECK_NOT
214 /* Use macro because they're so repetitive */
215 #define NEEDS(need) \
216 extern void VG_(needs_##need)(void) \
218 VG_(needs).need = True; \
221 // These ones don't require any tool-supplied functions
222 NEEDS(libc_freeres)
223 NEEDS(cxx_freeres)
224 NEEDS(var_info)
226 void VG_(needs_core_errors)( Bool need )
228 VG_(needs).core_errors = need;
231 void VG_(needs_superblock_discards)(
232 void (*discard)(Addr, VexGuestExtents)
235 VG_(needs).superblock_discards = True;
236 VG_(tdict).tool_discard_superblock_info = discard;
239 void VG_(needs_tool_errors)(
240 Bool (*eq) (VgRes, const Error*, const Error*),
241 void (*before_pp) (const Error*),
242 void (*pp) (const Error*),
243 Bool show_TIDs,
244 UInt (*update) (const Error*),
245 Bool (*recog) (const HChar*, Supp*),
246 Bool (*read_extra) (Int, HChar**, SizeT*, Int*, Supp*),
247 Bool (*matches) (const Error*, const Supp*),
248 const HChar* (*name) (const Error*),
249 SizeT (*get_xtra_si)(const Error*,/*OUT*/HChar*,Int),
250 SizeT (*print_xtra_su)(const Supp*,/*OUT*/HChar*,Int),
251 void (*update_xtra_su)(const Error*, const Supp*)
254 VG_(needs).tool_errors = True;
255 VG_(tdict).tool_eq_Error = eq;
256 VG_(tdict).tool_before_pp_Error = before_pp;
257 VG_(tdict).tool_pp_Error = pp;
258 VG_(tdict).tool_show_ThreadIDs_for_errors = show_TIDs;
259 VG_(tdict).tool_update_extra = update;
260 VG_(tdict).tool_recognised_suppression = recog;
261 VG_(tdict).tool_read_extra_suppression_info = read_extra;
262 VG_(tdict).tool_error_matches_suppression = matches;
263 VG_(tdict).tool_get_error_name = name;
264 VG_(tdict).tool_get_extra_suppression_info = get_xtra_si;
265 VG_(tdict).tool_print_extra_suppression_use = print_xtra_su;
266 VG_(tdict).tool_update_extra_suppression_use = update_xtra_su;
269 void VG_(needs_command_line_options)(
270 Bool (*process)(const HChar*),
271 void (*usage)(void),
272 void (*debug_usage)(void)
275 VG_(needs).command_line_options = True;
276 VG_(tdict).tool_process_cmd_line_option = process;
277 VG_(tdict).tool_print_usage = usage;
278 VG_(tdict).tool_print_debug_usage = debug_usage;
281 /* The tool's function for handling client requests. */
282 static Bool (*tool_handle_client_request_func)(ThreadId, UWord *, UWord *);
284 static Bool wrap_tool_handle_client_request(ThreadId tid, UWord *arg1,
285 UWord *arg2)
287 Bool ret;
288 VG_(ok_to_discard_translations) = True;
289 ret = tool_handle_client_request_func(tid, arg1, arg2);
290 VG_(ok_to_discard_translations) = False;
291 return ret;
294 void VG_(needs_client_requests)(
295 Bool (*handle)(ThreadId, UWord*, UWord*)
298 VG_(needs).client_requests = True;
299 tool_handle_client_request_func = handle; /* Stash away */
300 /* Register the wrapper function */
301 VG_(tdict).tool_handle_client_request = wrap_tool_handle_client_request;
304 void VG_(needs_syscall_wrapper)(
305 void(*pre) (ThreadId, UInt, UWord*, UInt),
306 void(*post)(ThreadId, UInt, UWord*, UInt, SysRes res)
309 VG_(needs).syscall_wrapper = True;
310 VG_(tdict).tool_pre_syscall = pre;
311 VG_(tdict).tool_post_syscall = post;
314 void VG_(needs_sanity_checks)(
315 Bool(*cheap)(void),
316 Bool(*expen)(void)
319 VG_(needs).sanity_checks = True;
320 VG_(tdict).tool_cheap_sanity_check = cheap;
321 VG_(tdict).tool_expensive_sanity_check = expen;
324 void VG_(needs_print_stats) (
325 void (*print_stats)(void)
328 VG_(needs).print_stats = True;
329 VG_(tdict).tool_print_stats = print_stats;
332 void VG_(needs_info_location) (
333 void (*info_location)(DiEpoch, Addr)
336 VG_(needs).info_location = True;
337 VG_(tdict).tool_info_location = info_location;
340 void VG_(needs_malloc_replacement)(
341 void* (*malloc) ( ThreadId, SizeT ),
342 void* (*__builtin_new) ( ThreadId, SizeT ),
343 void* (*__builtin_new_aligned)( ThreadId, SizeT, SizeT, SizeT ),
344 void* (*__builtin_vec_new) ( ThreadId, SizeT ),
345 void* (*__builtin_vec_new_aligned)( ThreadId, SizeT, SizeT, SizeT ),
346 void* (*memalign) ( ThreadId, SizeT, SizeT, SizeT ),
347 void* (*calloc) ( ThreadId, SizeT, SizeT ),
348 void (*free) ( ThreadId, void* ),
349 void (*__builtin_delete) ( ThreadId, void* ),
350 void (*__builtin_delete_aligned) ( ThreadId, void*, SizeT ),
351 void (*__builtin_vec_delete) ( ThreadId, void* ),
352 void (*__builtin_vec_delete_aligned) ( ThreadId, void*, SizeT ),
353 void* (*realloc) ( ThreadId, void*, SizeT ),
354 SizeT (*malloc_usable_size) ( ThreadId, void* ),
355 SizeT client_malloc_redzone_szB
358 VG_(needs).malloc_replacement = True;
359 VG_(tdict).tool_malloc = malloc;
360 VG_(tdict).tool___builtin_new = __builtin_new;
361 VG_(tdict).tool___builtin_new_aligned = __builtin_new_aligned;
362 VG_(tdict).tool___builtin_vec_new = __builtin_vec_new;
363 VG_(tdict).tool___builtin_vec_new_aligned = __builtin_vec_new_aligned;
364 VG_(tdict).tool_memalign = memalign;
365 VG_(tdict).tool_calloc = calloc;
366 VG_(tdict).tool_free = free;
367 VG_(tdict).tool___builtin_delete = __builtin_delete;
368 VG_(tdict).tool___builtin_delete_aligned = __builtin_delete_aligned;
369 VG_(tdict).tool___builtin_vec_delete = __builtin_vec_delete;
370 VG_(tdict).tool___builtin_vec_delete_aligned = __builtin_vec_delete_aligned;
371 VG_(tdict).tool_realloc = realloc;
372 VG_(tdict).tool_malloc_usable_size = malloc_usable_size;
373 VG_(tdict).tool_client_redzone_szB = client_malloc_redzone_szB;
376 void VG_(needs_xml_output)( void )
378 VG_(needs).xml_output = True;
381 void VG_(needs_final_IR_tidy_pass)(
382 IRSB*(*final_tidy)(IRSB*)
385 VG_(needs).final_IR_tidy_pass = True;
386 VG_(tdict).tool_final_IR_tidy_pass = final_tidy;
389 /*--------------------------------------------------------------------*/
390 /* Tracked events. Digit 'n' on DEFn is the REGPARMness. */
392 #define DEF0(fn, args...) \
393 void VG_(fn)(void(*f)(args)) { \
394 VG_(tdict).fn = f; \
397 #define DEF1(fn, args...) \
398 void VG_(fn)(VG_REGPARM(1) void(*f)(args)) { \
399 VG_(tdict).fn = f; \
402 #define DEF2(fn, args...) \
403 void VG_(fn)(VG_REGPARM(2) void(*f)(args)) { \
404 VG_(tdict).fn = f; \
407 DEF0(track_new_mem_startup, Addr, SizeT, Bool, Bool, Bool, ULong)
408 DEF0(track_new_mem_stack_signal, Addr, SizeT, UInt)
409 DEF0(track_new_mem_brk, Addr, SizeT, UInt)
410 DEF0(track_new_mem_mmap, Addr, SizeT, Bool, Bool, Bool, ULong)
412 DEF0(track_copy_mem_remap, Addr, Addr, SizeT)
413 DEF0(track_change_mem_mprotect, Addr, SizeT, Bool, Bool, Bool)
414 DEF0(track_die_mem_stack_signal, Addr, SizeT)
415 DEF0(track_die_mem_brk, Addr, SizeT)
416 DEF0(track_die_mem_munmap, Addr, SizeT)
418 DEF2(track_new_mem_stack_4_w_ECU, Addr, UInt)
419 DEF2(track_new_mem_stack_8_w_ECU, Addr, UInt)
420 DEF2(track_new_mem_stack_12_w_ECU, Addr, UInt)
421 DEF2(track_new_mem_stack_16_w_ECU, Addr, UInt)
422 DEF2(track_new_mem_stack_32_w_ECU, Addr, UInt)
423 DEF2(track_new_mem_stack_112_w_ECU, Addr, UInt)
424 DEF2(track_new_mem_stack_128_w_ECU, Addr, UInt)
425 DEF2(track_new_mem_stack_144_w_ECU, Addr, UInt)
426 DEF2(track_new_mem_stack_160_w_ECU, Addr, UInt)
427 DEF0(track_new_mem_stack_w_ECU, Addr, SizeT, UInt)
429 DEF1(track_new_mem_stack_4, Addr)
430 DEF1(track_new_mem_stack_8, Addr)
431 DEF1(track_new_mem_stack_12, Addr)
432 DEF1(track_new_mem_stack_16, Addr)
433 DEF1(track_new_mem_stack_32, Addr)
434 DEF1(track_new_mem_stack_112, Addr)
435 DEF1(track_new_mem_stack_128, Addr)
436 DEF1(track_new_mem_stack_144, Addr)
437 DEF1(track_new_mem_stack_160, Addr)
438 DEF0(track_new_mem_stack, Addr, SizeT)
440 DEF1(track_die_mem_stack_4, Addr)
441 DEF1(track_die_mem_stack_8, Addr)
442 DEF1(track_die_mem_stack_12, Addr)
443 DEF1(track_die_mem_stack_16, Addr)
444 DEF1(track_die_mem_stack_32, Addr)
445 DEF1(track_die_mem_stack_112, Addr)
446 DEF1(track_die_mem_stack_128, Addr)
447 DEF1(track_die_mem_stack_144, Addr)
448 DEF1(track_die_mem_stack_160, Addr)
449 DEF0(track_die_mem_stack, Addr, SizeT)
451 DEF0(track_ban_mem_stack, Addr, SizeT)
453 DEF0(track_register_stack, Addr, Addr)
455 DEF0(track_pre_mem_read, CorePart, ThreadId, const HChar*, Addr, SizeT)
456 DEF0(track_pre_mem_read_asciiz, CorePart, ThreadId, const HChar*, Addr)
457 DEF0(track_pre_mem_write, CorePart, ThreadId, const HChar*, Addr, SizeT)
458 DEF0(track_post_mem_write, CorePart, ThreadId, Addr, SizeT)
460 DEF0(track_pre_reg_read, CorePart, ThreadId, const HChar*, PtrdiffT, SizeT)
461 DEF0(track_post_reg_write, CorePart, ThreadId, PtrdiffT, SizeT)
463 DEF0(track_copy_mem_to_reg, CorePart, ThreadId, Addr, PtrdiffT, SizeT)
464 DEF0(track_copy_reg_to_mem, CorePart, ThreadId, PtrdiffT, Addr, SizeT)
466 DEF0(track_post_reg_write_clientcall_return, ThreadId, PtrdiffT, SizeT, Addr)
468 DEF0(track_start_client_code, ThreadId, ULong)
469 DEF0(track_stop_client_code, ThreadId, ULong)
471 DEF0(track_pre_thread_ll_create, ThreadId, ThreadId)
472 DEF0(track_pre_thread_first_insn, ThreadId)
473 DEF0(track_pre_thread_ll_exit, ThreadId)
475 DEF0(track_pre_deliver_signal, ThreadId, Int sigNo, Bool)
476 DEF0(track_post_deliver_signal, ThreadId, Int sigNo)
478 /*--------------------------------------------------------------------*/
479 /*--- end ---*/
480 /*--------------------------------------------------------------------*/