njn | c953984 | 2002-10-02 13:26:35 +0000 | [diff] [blame] | 1 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2 | /*--------------------------------------------------------------------*/ |
nethercote | 137bc55 | 2003-11-14 17:47:54 +0000 | [diff] [blame] | 3 | /*--- Default panicky definitions of template functions that tools ---*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 4 | /*--- should override. ---*/ |
| 5 | /*--- vg_defaults.c ---*/ |
| 6 | /*--------------------------------------------------------------------*/ |
| 7 | |
| 8 | /* |
njn | b9c427c | 2004-12-01 14:14:42 +0000 | [diff] [blame] | 9 | This file is part of Valgrind, a dynamic binary instrumentation |
| 10 | framework. |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 11 | |
njn | 5361242 | 2005-03-12 16:22:54 +0000 | [diff] [blame] | 12 | Copyright (C) 2000-2005 Nicholas Nethercote |
njn | 2bc1012 | 2005-05-08 02:10:27 +0000 | [diff] [blame] | 13 | njn@valgrind.org |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 14 | |
| 15 | This program is free software; you can redistribute it and/or |
| 16 | modify it under the terms of the GNU General Public License as |
| 17 | published by the Free Software Foundation; either version 2 of the |
| 18 | License, or (at your option) any later version. |
| 19 | |
| 20 | This program is distributed in the hope that it will be useful, but |
| 21 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 23 | General Public License for more details. |
| 24 | |
| 25 | You should have received a copy of the GNU General Public License |
| 26 | along with this program; if not, write to the Free Software |
| 27 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 28 | 02111-1307, USA. |
| 29 | |
| 30 | The GNU General Public License is contained in the file COPYING. |
| 31 | */ |
| 32 | |
| 33 | |
nethercote | f1e5e15 | 2004-09-01 23:58:16 +0000 | [diff] [blame] | 34 | #include "core.h" |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 35 | |
| 36 | /* --------------------------------------------------------------------- |
nethercote | 137bc55 | 2003-11-14 17:47:54 +0000 | [diff] [blame] | 37 | Error messages (for malformed tools) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 38 | ------------------------------------------------------------------ */ |
| 39 | |
nethercote | 137bc55 | 2003-11-14 17:47:54 +0000 | [diff] [blame] | 40 | /* If the tool fails to define one or more of the required functions, |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 41 | * make it very clear what went wrong! */ |
| 42 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 43 | __attribute__ ((noreturn)) |
| 44 | void VG_(missing_tool_func) ( const Char* fn ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 45 | { |
| 46 | VG_(printf)( |
nethercote | 137bc55 | 2003-11-14 17:47:54 +0000 | [diff] [blame] | 47 | "\nTool error:\n" |
| 48 | " The tool you have selected is missing the function `%s',\n" |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 49 | " which is required.\n\n", |
| 50 | fn); |
njn | 6799325 | 2004-11-22 18:02:32 +0000 | [diff] [blame] | 51 | VG_(tool_panic)("Missing tool function"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | static __attribute__ ((noreturn)) |
daywalker | 3222e0a | 2003-09-18 01:39:50 +0000 | [diff] [blame] | 55 | void malloc_panic ( const Char* fn ) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 56 | { |
| 57 | VG_(printf)( |
nethercote | 137bc55 | 2003-11-14 17:47:54 +0000 | [diff] [blame] | 58 | "\nTool error:\n" |
| 59 | " The tool you have selected is missing the function `%s'\n" |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 60 | " required because it is replacing malloc() et al.\n\n", |
| 61 | fn); |
njn | 6799325 | 2004-11-22 18:02:32 +0000 | [diff] [blame] | 62 | VG_(tool_panic)("Missing tool function"); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 63 | } |
| 64 | |
njn | 308955f | 2003-05-21 10:48:24 +0000 | [diff] [blame] | 65 | /*------------------------------------------------------------*/ |
| 66 | /*--- Replacing malloc et al ---*/ |
| 67 | /*------------------------------------------------------------*/ |
| 68 | |
njn | d13e5e6 | 2005-03-26 03:50:14 +0000 | [diff] [blame] | 69 | Bool VG_(tl_malloc_called_deliberately) = False; |
njn | 308955f | 2003-05-21 10:48:24 +0000 | [diff] [blame] | 70 | |
njn | d13e5e6 | 2005-03-26 03:50:14 +0000 | [diff] [blame] | 71 | /* If the tool hasn't replaced malloc(), this one can be called |
| 72 | deliberately. The lock variable ensures that this isn't called by |
| 73 | accident, which could happen if a malloc()-replacing tool forgot to |
| 74 | implement TL_(malloc)() or TL_(free)(). */ |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 75 | __attribute__ ((weak)) |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 76 | void* TL_(malloc)( ThreadId tid, SizeT size ) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 77 | { |
njn | d13e5e6 | 2005-03-26 03:50:14 +0000 | [diff] [blame] | 78 | if (VG_(tl_malloc_called_deliberately)) |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 79 | return VG_(cli_malloc)(VG_MIN_MALLOC_SZB, size); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 80 | else |
njn | 308955f | 2003-05-21 10:48:24 +0000 | [diff] [blame] | 81 | malloc_panic(__PRETTY_FUNCTION__); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | __attribute__ ((weak)) |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 85 | void TL_(free)( ThreadId tid, void* p ) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 86 | { |
njn | 26f0251 | 2004-11-22 18:33:15 +0000 | [diff] [blame] | 87 | /* see comment for TL_(malloc)() above */ |
njn | d13e5e6 | 2005-03-26 03:50:14 +0000 | [diff] [blame] | 88 | if (VG_(tl_malloc_called_deliberately)) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 89 | VG_(cli_free)(p); |
| 90 | else |
njn | 308955f | 2003-05-21 10:48:24 +0000 | [diff] [blame] | 91 | malloc_panic(__PRETTY_FUNCTION__); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 92 | } |
| 93 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 94 | /*--------------------------------------------------------------------*/ |
| 95 | /*--- end vg_defaults.c ---*/ |
| 96 | /*--------------------------------------------------------------------*/ |