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 | c953984 | 2002-10-02 13:26:35 +0000 | [diff] [blame] | 9 | This file is part of Valgrind, an extensible x86 protected-mode |
| 10 | emulator for monitoring program execution on x86-Unixes. |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 11 | |
njn | 0e1b514 | 2003-04-15 14:58:06 +0000 | [diff] [blame] | 12 | Copyright (C) 2000-2003 Nicholas Nethercote |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 13 | njn25@cam.ac.uk |
| 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 | |
| 34 | /* These functions aren't intended to be run. Replacement functions used by |
nethercote | 137bc55 | 2003-11-14 17:47:54 +0000 | [diff] [blame] | 35 | * the chosen tool are substituted by compiling the tool into a .so and |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 36 | * LD_PRELOADing it. Nasty :) */ |
| 37 | |
| 38 | #include "vg_include.h" |
| 39 | |
| 40 | /* --------------------------------------------------------------------- |
nethercote | 137bc55 | 2003-11-14 17:47:54 +0000 | [diff] [blame] | 41 | Error messages (for malformed tools) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 42 | ------------------------------------------------------------------ */ |
| 43 | |
nethercote | 137bc55 | 2003-11-14 17:47:54 +0000 | [diff] [blame] | 44 | /* If the tool fails to define one or more of the required functions, |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 45 | * make it very clear what went wrong! */ |
| 46 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame^] | 47 | __attribute__ ((noreturn)) |
| 48 | void VG_(missing_tool_func) ( const Char* fn ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 49 | { |
| 50 | VG_(printf)( |
nethercote | 137bc55 | 2003-11-14 17:47:54 +0000 | [diff] [blame] | 51 | "\nTool error:\n" |
| 52 | " The tool you have selected is missing the function `%s',\n" |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 53 | " which is required.\n\n", |
| 54 | fn); |
nethercote | 137bc55 | 2003-11-14 17:47:54 +0000 | [diff] [blame] | 55 | VG_(skin_panic)("Missing tool function"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | static __attribute__ ((noreturn)) |
daywalker | 3222e0a | 2003-09-18 01:39:50 +0000 | [diff] [blame] | 59 | void malloc_panic ( const Char* fn ) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 60 | { |
| 61 | VG_(printf)( |
nethercote | 137bc55 | 2003-11-14 17:47:54 +0000 | [diff] [blame] | 62 | "\nTool error:\n" |
| 63 | " The tool you have selected is missing the function `%s'\n" |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 64 | " required because it is replacing malloc() et al.\n\n", |
| 65 | fn); |
nethercote | 137bc55 | 2003-11-14 17:47:54 +0000 | [diff] [blame] | 66 | VG_(skin_panic)("Missing tool function"); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 67 | } |
| 68 | |
njn | 308955f | 2003-05-21 10:48:24 +0000 | [diff] [blame] | 69 | #define MALLOC(proto) \ |
| 70 | __attribute__((weak)) \ |
| 71 | proto \ |
| 72 | { \ |
| 73 | malloc_panic(__PRETTY_FUNCTION__); \ |
| 74 | } |
| 75 | |
| 76 | /* --------------------------------------------------------------------- |
| 77 | Default functions |
| 78 | ------------------------------------------------------------------ */ |
| 79 | |
njn | 308955f | 2003-05-21 10:48:24 +0000 | [diff] [blame] | 80 | /*------------------------------------------------------------*/ |
| 81 | /*--- Replacing malloc et al ---*/ |
| 82 | /*------------------------------------------------------------*/ |
| 83 | |
| 84 | /* Default redzone for CLIENT arena of Valgrind's malloc() is 4 bytes */ |
| 85 | __attribute__ ((weak)) |
| 86 | UInt VG_(vg_malloc_redzone_szB) = 4; |
| 87 | |
| 88 | Bool VG_(sk_malloc_called_by_scheduler) = False; |
| 89 | |
nethercote | 137bc55 | 2003-11-14 17:47:54 +0000 | [diff] [blame] | 90 | /* If the tool hasn't replaced malloc(), this one can be called from the |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 91 | scheduler, for the USERREQ__MALLOC user request used by vg_libpthread.c. |
| 92 | (Nb: it cannot call glibc's malloc().) The lock variable ensures that the |
| 93 | scheduler is the only place this can be called from; this ensures that a |
nethercote | 137bc55 | 2003-11-14 17:47:54 +0000 | [diff] [blame] | 94 | malloc()-replacing tool cannot forget to implement SK_(malloc)() or |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 95 | SK_(free)(). */ |
| 96 | __attribute__ ((weak)) |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 97 | void* SK_(malloc)( Int size ) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 98 | { |
| 99 | if (VG_(sk_malloc_called_by_scheduler)) |
| 100 | return VG_(cli_malloc)(4, size); |
| 101 | else |
njn | 308955f | 2003-05-21 10:48:24 +0000 | [diff] [blame] | 102 | malloc_panic(__PRETTY_FUNCTION__); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | __attribute__ ((weak)) |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 106 | void SK_(free)( void* p ) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 107 | { |
| 108 | /* see comment for SK_(malloc)() above */ |
| 109 | if (VG_(sk_malloc_called_by_scheduler)) |
| 110 | VG_(cli_free)(p); |
| 111 | else |
njn | 308955f | 2003-05-21 10:48:24 +0000 | [diff] [blame] | 112 | malloc_panic(__PRETTY_FUNCTION__); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 113 | } |
| 114 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 115 | /*--------------------------------------------------------------------*/ |
| 116 | /*--- end vg_defaults.c ---*/ |
| 117 | /*--------------------------------------------------------------------*/ |