m_tooliface.c revision 436e89c602e787e7a27dd6624b09beed41a0da8a
1
2/*--------------------------------------------------------------------*/
3/*--- Stuff relating to tool data structures.                      ---*/
4/*---                                                m_tooliface.c ---*/
5/*--------------------------------------------------------------------*/
6
7/*
8   This file is part of Valgrind, a dynamic binary instrumentation
9   framework.
10
11   Copyright (C) 2000-2013 Nicholas Nethercote
12      njn@valgrind.org
13
14   This program is free software; you can redistribute it and/or
15   modify it under the terms of the GNU General Public License as
16   published by the Free Software Foundation; either version 2 of the
17   License, or (at your option) any later version.
18
19   This program is distributed in the hope that it will be useful, but
20   WITHOUT ANY WARRANTY; without even the implied warranty of
21   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22   General Public License for more details.
23
24   You should have received a copy of the GNU General Public License
25   along with this program; if not, write to the Free Software
26   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
27   02111-1307, USA.
28
29   The GNU General Public License is contained in the file COPYING.
30*/
31
32#include "pub_core_basics.h"
33#include "pub_core_tooliface.h"
34
35// The core/tool dictionary of functions (initially zeroed, as we want it)
36VgToolInterface VG_(tdict);
37
38/*--------------------------------------------------------------------*/
39/* Setting basic functions */
40
41void VG_(basic_tool_funcs)(
42   void(*post_clo_init)(void),
43   IRSB*(*instrument)(VgCallbackClosure*, IRSB*,
44                      VexGuestLayout*, VexGuestExtents*, VexArchInfo*,
45                      IRType, IRType),
46   void(*fini)(Int)
47)
48{
49   VG_(tdict).tool_post_clo_init = post_clo_init;
50   VG_(tdict).tool_instrument    = instrument;
51   VG_(tdict).tool_fini          = fini;
52}
53
54
55/*--------------------------------------------------------------------*/
56/* Setting details */
57
58/* Init with default values. */
59VgDetails VG_(details) = {
60   .name                  = NULL,
61   .version               = NULL,
62   .description           = NULL,
63   .copyright_author      = NULL,
64   .bug_reports_to        = NULL,
65   .avg_translation_sizeB = VG_DEFAULT_TRANS_SIZEB,
66};
67
68/* Use macro because they're so repetitive */
69#define DETAILS(type, detail)                       \
70   extern void VG_(details_##detail)(type detail)   \
71   {                                                \
72      VG_(details).detail = detail;                 \
73   }
74
75DETAILS(const HChar*, name)
76DETAILS(const HChar*, version)
77DETAILS(const HChar*, description)
78DETAILS(const HChar*, copyright_author)
79DETAILS(const HChar*, bug_reports_to)
80DETAILS(UInt,  avg_translation_sizeB)
81
82
83/*--------------------------------------------------------------------*/
84/* Setting needs */
85
86VgNeeds VG_(needs) = {
87   .core_errors          = False,
88   .tool_errors          = False,
89   .libc_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   .var_info	         = False,
97   .malloc_replacement   = False,
98   .xml_output           = False,
99   .final_IR_tidy_pass   = False
100};
101
102/* static */
103Bool VG_(sanity_check_needs)(const HChar** failmsg)
104{
105   Bool any_new_mem_stack_N, any_new_mem_stack_N_w_ECU;
106   Bool any_new_mem_stack_w_conflicting_otags;
107   Bool any_die_mem_stack_N;
108
109#define CHECK_NOT(var, value)                                  \
110   if ((var)==(value)) {                                       \
111      *failmsg = "Tool error: '" #var "' not initialised\n";   \
112      return False;                                            \
113   }
114
115   /* Ones that must be set */
116   CHECK_NOT(VG_(details).name,             NULL);
117   /* Nb: .version can be NULL */
118   CHECK_NOT(VG_(details).description,      NULL);
119   CHECK_NOT(VG_(details).copyright_author, NULL);
120   CHECK_NOT(VG_(details).bug_reports_to,   NULL);
121
122   /* Check that new_mem_stack is defined if any new_mem_stack_N
123      are. */
124   any_new_mem_stack_N
125      = VG_(tdict).track_new_mem_stack_4   ||
126        VG_(tdict).track_new_mem_stack_8   ||
127        VG_(tdict).track_new_mem_stack_12  ||
128        VG_(tdict).track_new_mem_stack_16  ||
129        VG_(tdict).track_new_mem_stack_32  ||
130        VG_(tdict).track_new_mem_stack_112 ||
131        VG_(tdict).track_new_mem_stack_128 ||
132        VG_(tdict).track_new_mem_stack_144 ||
133        VG_(tdict).track_new_mem_stack_160;
134
135   if (any_new_mem_stack_N && ! VG_(tdict).track_new_mem_stack) {
136      *failmsg = "Tool error: one of the specialised 'new_mem_stack_N'\n"
137                 "   events tracked, but not the generic 'new_mem_stack' one.\n"
138                 "   'new_mem_stack' should be defined\n";
139      return False;
140   }
141
142   /* Check that new_mem_stack_w_ECU is defined if any
143      new_mem_stack_N_w_ECU are. */
144   any_new_mem_stack_N_w_ECU
145      = VG_(tdict).track_new_mem_stack_4_w_ECU   ||
146        VG_(tdict).track_new_mem_stack_8_w_ECU   ||
147        VG_(tdict).track_new_mem_stack_12_w_ECU  ||
148        VG_(tdict).track_new_mem_stack_16_w_ECU  ||
149        VG_(tdict).track_new_mem_stack_32_w_ECU  ||
150        VG_(tdict).track_new_mem_stack_112_w_ECU ||
151        VG_(tdict).track_new_mem_stack_128_w_ECU ||
152        VG_(tdict).track_new_mem_stack_144_w_ECU ||
153        VG_(tdict).track_new_mem_stack_160_w_ECU;
154
155   if (any_new_mem_stack_N_w_ECU && ! VG_(tdict).track_new_mem_stack_w_ECU) {
156      *failmsg = "Tool error: one of the specialised 'new_mem_stack_N_w_ECU'\n"
157                 "   events tracked, but not the generic 'new_mem_stack_w_ECU' one.\n"
158                 "   'new_mem_stack_w_ECU' should be defined\n";
159      return False;
160   }
161
162   /* Check that in no cases are both with- and without-otag versions of the
163      same new_mem_stack_ function defined. */
164   any_new_mem_stack_w_conflicting_otags
165      = (VG_(tdict).track_new_mem_stack_4   && VG_(tdict).track_new_mem_stack_4_w_ECU)   ||
166        (VG_(tdict).track_new_mem_stack_8   && VG_(tdict).track_new_mem_stack_8_w_ECU)   ||
167        (VG_(tdict).track_new_mem_stack_12  && VG_(tdict).track_new_mem_stack_12_w_ECU)  ||
168        (VG_(tdict).track_new_mem_stack_16  && VG_(tdict).track_new_mem_stack_16_w_ECU)  ||
169        (VG_(tdict).track_new_mem_stack_32  && VG_(tdict).track_new_mem_stack_32_w_ECU)  ||
170        (VG_(tdict).track_new_mem_stack_112 && VG_(tdict).track_new_mem_stack_112_w_ECU) ||
171        (VG_(tdict).track_new_mem_stack_128 && VG_(tdict).track_new_mem_stack_128_w_ECU) ||
172        (VG_(tdict).track_new_mem_stack_144 && VG_(tdict).track_new_mem_stack_144_w_ECU) ||
173        (VG_(tdict).track_new_mem_stack_160 && VG_(tdict).track_new_mem_stack_160_w_ECU) ||
174        (VG_(tdict).track_new_mem_stack     && VG_(tdict).track_new_mem_stack_w_ECU);
175
176   if (any_new_mem_stack_w_conflicting_otags) {
177      *failmsg = "Tool error: tool supplies both a 'new_mem_stack_N' and a\n"
178                 "   'new_mem_stack_N_w_ECU' function for some N (or none),\n"
179                 "   but you can only have one or the other (not both)\n";
180      return False;
181   }
182
183   /* Check that die_mem_stack is defined if any die_mem_stack_N
184      are. */
185   any_die_mem_stack_N
186      = VG_(tdict).track_die_mem_stack_4   ||
187        VG_(tdict).track_die_mem_stack_8   ||
188        VG_(tdict).track_die_mem_stack_12  ||
189        VG_(tdict).track_die_mem_stack_16  ||
190        VG_(tdict).track_die_mem_stack_32  ||
191        VG_(tdict).track_die_mem_stack_112 ||
192        VG_(tdict).track_die_mem_stack_128 ||
193        VG_(tdict).track_die_mem_stack_144 ||
194        VG_(tdict).track_die_mem_stack_160;
195
196    if (any_die_mem_stack_N && ! VG_(tdict).track_die_mem_stack) {
197      *failmsg = "Tool error: one of the specialised 'die_mem_stack_N'\n"
198                 "   events tracked, but not the generic 'die_mem_stack' one.\n"
199                 "   'die_mem_stack' should be defined\n";
200      return False;
201   }
202
203   return True;
204
205#undef CHECK_NOT
206}
207
208/* Use macro because they're so repetitive */
209#define NEEDS(need)  \
210   extern void VG_(needs_##need)(void) \
211   {                                   \
212      VG_(needs).need = True;          \
213   }
214
215// These ones don't require any tool-supplied functions
216NEEDS(libc_freeres)
217NEEDS(core_errors)
218NEEDS(var_info)
219
220void VG_(needs_superblock_discards)(
221   void (*discard)(Addr64, VexGuestExtents)
222)
223{
224   VG_(needs).superblock_discards = True;
225   VG_(tdict).tool_discard_superblock_info = discard;
226}
227
228void VG_(needs_tool_errors)(
229   Bool (*eq)         (VgRes, Error*, Error*),
230   void (*before_pp)  (Error*),
231   void (*pp)         (Error*),
232   Bool show_TIDs,
233   UInt (*update)     (Error*),
234   Bool (*recog)      (const HChar*, Supp*),
235   Bool (*read_extra) (Int, HChar**, SizeT*, Int*, Supp*),
236   Bool (*matches)    (Error*, Supp*),
237   const HChar* (*name) (Error*),
238   Bool (*get_xtra_si)(Error*,/*OUT*/HChar*,Int),
239   Bool (*print_xtra_su)(Supp*,/*OUT*/HChar*,Int),
240   void (*update_xtra_su)(Error*, Supp*)
241)
242{
243   VG_(needs).tool_errors = True;
244   VG_(tdict).tool_eq_Error                     = eq;
245   VG_(tdict).tool_before_pp_Error              = before_pp;
246   VG_(tdict).tool_pp_Error                     = pp;
247   VG_(tdict).tool_show_ThreadIDs_for_errors    = show_TIDs;
248   VG_(tdict).tool_update_extra                 = update;
249   VG_(tdict).tool_recognised_suppression       = recog;
250   VG_(tdict).tool_read_extra_suppression_info  = read_extra;
251   VG_(tdict).tool_error_matches_suppression    = matches;
252   VG_(tdict).tool_get_error_name               = name;
253   VG_(tdict).tool_get_extra_suppression_info   = get_xtra_si;
254   VG_(tdict).tool_print_extra_suppression_use  = print_xtra_su;
255   VG_(tdict).tool_update_extra_suppression_use = update_xtra_su;
256}
257
258void VG_(needs_command_line_options)(
259   Bool (*process)(const HChar*),
260   void (*usage)(void),
261   void (*debug_usage)(void)
262)
263{
264   VG_(needs).command_line_options = True;
265   VG_(tdict).tool_process_cmd_line_option = process;
266   VG_(tdict).tool_print_usage             = usage;
267   VG_(tdict).tool_print_debug_usage       = debug_usage;
268}
269
270void VG_(needs_client_requests)(
271   Bool (*handle)(ThreadId, UWord*, UWord*)
272)
273{
274   VG_(needs).client_requests = True;
275   VG_(tdict).tool_handle_client_request = handle;
276}
277
278void VG_(needs_syscall_wrapper)(
279   void(*pre) (ThreadId, UInt, UWord*, UInt),
280   void(*post)(ThreadId, UInt, UWord*, UInt, SysRes res)
281)
282{
283   VG_(needs).syscall_wrapper = True;
284   VG_(tdict).tool_pre_syscall  = pre;
285   VG_(tdict).tool_post_syscall = post;
286}
287
288void VG_(needs_sanity_checks)(
289   Bool(*cheap)(void),
290   Bool(*expen)(void)
291)
292{
293   VG_(needs).sanity_checks = True;
294   VG_(tdict).tool_cheap_sanity_check     = cheap;
295   VG_(tdict).tool_expensive_sanity_check = expen;
296}
297
298void VG_(needs_print_stats) (
299   void (*print_stats)(void)
300)
301{
302   VG_(needs).print_stats = True;
303   VG_(tdict).tool_print_stats = print_stats;
304}
305
306void VG_(needs_malloc_replacement)(
307   void* (*malloc)               ( ThreadId, SizeT ),
308   void* (*__builtin_new)        ( ThreadId, SizeT ),
309   void* (*__builtin_vec_new)    ( ThreadId, SizeT ),
310   void* (*memalign)             ( ThreadId, SizeT, SizeT ),
311   void* (*calloc)               ( ThreadId, SizeT, SizeT ),
312   void  (*free)                 ( ThreadId, void* ),
313   void  (*__builtin_delete)     ( ThreadId, void* ),
314   void  (*__builtin_vec_delete) ( ThreadId, void* ),
315   void* (*realloc)              ( ThreadId, void*, SizeT ),
316   SizeT (*malloc_usable_size)   ( ThreadId, void* ),
317   SizeT client_malloc_redzone_szB
318)
319{
320   VG_(needs).malloc_replacement        = True;
321   VG_(tdict).tool_malloc               = malloc;
322   VG_(tdict).tool___builtin_new        = __builtin_new;
323   VG_(tdict).tool___builtin_vec_new    = __builtin_vec_new;
324   VG_(tdict).tool_memalign             = memalign;
325   VG_(tdict).tool_calloc               = calloc;
326   VG_(tdict).tool_free                 = free;
327   VG_(tdict).tool___builtin_delete     = __builtin_delete;
328   VG_(tdict).tool___builtin_vec_delete = __builtin_vec_delete;
329   VG_(tdict).tool_realloc              = realloc;
330   VG_(tdict).tool_malloc_usable_size   = malloc_usable_size;
331   VG_(tdict).tool_client_redzone_szB   = client_malloc_redzone_szB;
332}
333
334void VG_(needs_xml_output)( void )
335{
336   VG_(needs).xml_output = True;
337}
338
339void VG_(needs_final_IR_tidy_pass)(
340   IRSB*(*final_tidy)(IRSB*)
341)
342{
343   VG_(needs).final_IR_tidy_pass = True;
344   VG_(tdict).tool_final_IR_tidy_pass = final_tidy;
345}
346
347/*--------------------------------------------------------------------*/
348/* Tracked events.  Digit 'n' on DEFn is the REGPARMness. */
349
350#define DEF0(fn, args...) \
351void VG_(fn)(void(*f)(args)) { \
352   VG_(tdict).fn = f; \
353}
354
355#define DEF1(fn, args...) \
356void VG_(fn)(VG_REGPARM(1) void(*f)(args)) { \
357   VG_(tdict).fn = f; \
358}
359
360#define DEF2(fn, args...) \
361void VG_(fn)(VG_REGPARM(2) void(*f)(args)) { \
362   VG_(tdict).fn = f; \
363}
364
365DEF0(track_new_mem_startup,       Addr, SizeT, Bool, Bool, Bool, ULong)
366DEF0(track_new_mem_stack_signal,  Addr, SizeT, UInt)
367DEF0(track_new_mem_brk,           Addr, SizeT, UInt)
368DEF0(track_new_mem_mmap,          Addr, SizeT, Bool, Bool, Bool, ULong)
369
370DEF0(track_copy_mem_remap,        Addr, Addr, SizeT)
371DEF0(track_change_mem_mprotect,   Addr, SizeT, Bool, Bool, Bool)
372DEF0(track_die_mem_stack_signal,  Addr, SizeT)
373DEF0(track_die_mem_brk,           Addr, SizeT)
374DEF0(track_die_mem_munmap,        Addr, SizeT)
375
376DEF2(track_new_mem_stack_4_w_ECU,    Addr, UInt)
377DEF2(track_new_mem_stack_8_w_ECU,    Addr, UInt)
378DEF2(track_new_mem_stack_12_w_ECU,   Addr, UInt)
379DEF2(track_new_mem_stack_16_w_ECU,   Addr, UInt)
380DEF2(track_new_mem_stack_32_w_ECU,   Addr, UInt)
381DEF2(track_new_mem_stack_112_w_ECU,  Addr, UInt)
382DEF2(track_new_mem_stack_128_w_ECU,  Addr, UInt)
383DEF2(track_new_mem_stack_144_w_ECU,  Addr, UInt)
384DEF2(track_new_mem_stack_160_w_ECU,  Addr, UInt)
385DEF0(track_new_mem_stack_w_ECU,      Addr, SizeT, UInt)
386
387DEF1(track_new_mem_stack_4,       Addr)
388DEF1(track_new_mem_stack_8,       Addr)
389DEF1(track_new_mem_stack_12,      Addr)
390DEF1(track_new_mem_stack_16,      Addr)
391DEF1(track_new_mem_stack_32,      Addr)
392DEF1(track_new_mem_stack_112,     Addr)
393DEF1(track_new_mem_stack_128,     Addr)
394DEF1(track_new_mem_stack_144,     Addr)
395DEF1(track_new_mem_stack_160,     Addr)
396DEF0(track_new_mem_stack,         Addr, SizeT)
397
398DEF1(track_die_mem_stack_4,       Addr)
399DEF1(track_die_mem_stack_8,       Addr)
400DEF1(track_die_mem_stack_12,      Addr)
401DEF1(track_die_mem_stack_16,      Addr)
402DEF1(track_die_mem_stack_32,      Addr)
403DEF1(track_die_mem_stack_112,     Addr)
404DEF1(track_die_mem_stack_128,     Addr)
405DEF1(track_die_mem_stack_144,     Addr)
406DEF1(track_die_mem_stack_160,     Addr)
407DEF0(track_die_mem_stack,         Addr, SizeT)
408
409DEF0(track_ban_mem_stack,         Addr, SizeT)
410
411DEF0(track_pre_mem_read,          CorePart, ThreadId, const HChar*, Addr, SizeT)
412DEF0(track_pre_mem_read_asciiz,   CorePart, ThreadId, const HChar*, Addr)
413DEF0(track_pre_mem_write,         CorePart, ThreadId, const HChar*, Addr, SizeT)
414DEF0(track_post_mem_write,        CorePart, ThreadId, Addr, SizeT)
415
416DEF0(track_pre_reg_read,          CorePart, ThreadId, const HChar*, PtrdiffT, SizeT)
417DEF0(track_post_reg_write,        CorePart, ThreadId,               PtrdiffT, SizeT)
418
419DEF0(track_post_reg_write_clientcall_return, ThreadId, PtrdiffT, SizeT, Addr)
420
421DEF0(track_start_client_code,     ThreadId, ULong)
422DEF0(track_stop_client_code,      ThreadId, ULong)
423
424DEF0(track_pre_thread_ll_create,  ThreadId, ThreadId)
425DEF0(track_pre_thread_first_insn, ThreadId)
426DEF0(track_pre_thread_ll_exit,    ThreadId)
427
428DEF0(track_pre_deliver_signal,    ThreadId, Int sigNo, Bool)
429DEF0(track_post_deliver_signal,   ThreadId, Int sigNo)
430
431/*--------------------------------------------------------------------*/
432/*--- end                                                          ---*/
433/*--------------------------------------------------------------------*/
434