njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 1 | |
| 2 | /*--------------------------------------------------------------------*/ |
| 3 | /*--- Code that is shared between MemCheck and AddrCheck. ---*/ |
njn | 00cae24 | 2005-05-09 01:13:44 +0000 | [diff] [blame] | 4 | /*--- mac_shared.c ---*/ |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 5 | /*--------------------------------------------------------------------*/ |
| 6 | |
| 7 | /* |
nethercote | 137bc55 | 2003-11-14 17:47:54 +0000 | [diff] [blame] | 8 | This file is part of MemCheck, a heavyweight Valgrind tool for |
| 9 | detecting memory errors, and AddrCheck, a lightweight Valgrind tool |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 10 | for detecting memory errors. |
| 11 | |
njn | 5361242 | 2005-03-12 16:22:54 +0000 | [diff] [blame] | 12 | Copyright (C) 2000-2005 Julian Seward |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 13 | jseward@acm.org |
| 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 | |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame^] | 33 | #include "pub_tool_basics.h" |
| 34 | #include "pub_tool_errormgr.h" // For mac_shared.h |
| 35 | #include "pub_tool_execontext.h" // For mac_shared.h |
| 36 | #include "pub_tool_hashtable.h" // For mac_shared.h |
njn | 132bfcc | 2005-06-04 19:16:06 +0000 | [diff] [blame] | 37 | #include "pub_tool_libcassert.h" |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 38 | #include "pub_tool_libcbase.h" |
njn | 36a20fa | 2005-06-03 03:08:39 +0000 | [diff] [blame] | 39 | #include "pub_tool_libcprint.h" |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame^] | 40 | #include "pub_tool_mallocfree.h" |
njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 41 | #include "pub_tool_machine.h" |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame^] | 42 | #include "pub_tool_options.h" |
| 43 | #include "pub_tool_profile.h" // For mac_shared.h |
| 44 | #include "pub_tool_replacemalloc.h" |
| 45 | #include "pub_tool_threadstate.h" |
| 46 | #include "mac_shared.h" |
njn | 47363ab | 2003-04-21 13:24:40 +0000 | [diff] [blame] | 47 | #include "memcheck.h" /* for VG_USERREQ__* */ |
| 48 | |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 49 | /*------------------------------------------------------------*/ |
| 50 | /*--- Defns ---*/ |
| 51 | /*------------------------------------------------------------*/ |
| 52 | |
| 53 | /* These many bytes below %ESP are considered addressible if we're |
| 54 | doing the --workaround-gcc296-bugs hack. */ |
| 55 | #define VG_GCC296_BUG_STACK_SLOP 1024 |
| 56 | |
| 57 | /*------------------------------------------------------------*/ |
| 58 | /*--- Command line options ---*/ |
| 59 | /*------------------------------------------------------------*/ |
| 60 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 61 | Bool MAC_(clo_partial_loads_ok) = True; |
| 62 | Int MAC_(clo_freelist_vol) = 1000000; |
njn | 6a32942 | 2005-03-12 20:38:13 +0000 | [diff] [blame] | 63 | LeakCheckMode MAC_(clo_leak_check) = LC_Summary; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 64 | VgRes MAC_(clo_leak_resolution) = Vg_LowRes; |
| 65 | Bool MAC_(clo_show_reachable) = False; |
| 66 | Bool MAC_(clo_workaround_gcc296_bugs) = False; |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 67 | |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 68 | Bool MAC_(process_common_cmd_line_option)(Char* arg) |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 69 | { |
njn | 45270a2 | 2005-03-27 01:00:11 +0000 | [diff] [blame] | 70 | VG_BOOL_CLO(arg, "--partial-loads-ok", MAC_(clo_partial_loads_ok)) |
| 71 | else VG_BOOL_CLO(arg, "--show-reachable", MAC_(clo_show_reachable)) |
| 72 | else VG_BOOL_CLO(arg, "--workaround-gcc296-bugs",MAC_(clo_workaround_gcc296_bugs)) |
nethercote | 27fec90 | 2004-06-16 21:26:32 +0000 | [diff] [blame] | 73 | |
njn | 45270a2 | 2005-03-27 01:00:11 +0000 | [diff] [blame] | 74 | else VG_BNUM_CLO(arg, "--freelist-vol", MAC_(clo_freelist_vol), 0, 1000000000) |
nethercote | 27fec90 | 2004-06-16 21:26:32 +0000 | [diff] [blame] | 75 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 76 | else if (VG_CLO_STREQ(arg, "--leak-check=no")) |
| 77 | MAC_(clo_leak_check) = LC_Off; |
| 78 | else if (VG_CLO_STREQ(arg, "--leak-check=summary")) |
| 79 | MAC_(clo_leak_check) = LC_Summary; |
| 80 | else if (VG_CLO_STREQ(arg, "--leak-check=yes") || |
| 81 | VG_CLO_STREQ(arg, "--leak-check=full")) |
| 82 | MAC_(clo_leak_check) = LC_Full; |
| 83 | |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 84 | else if (VG_CLO_STREQ(arg, "--leak-resolution=low")) |
| 85 | MAC_(clo_leak_resolution) = Vg_LowRes; |
| 86 | else if (VG_CLO_STREQ(arg, "--leak-resolution=med")) |
| 87 | MAC_(clo_leak_resolution) = Vg_MedRes; |
| 88 | else if (VG_CLO_STREQ(arg, "--leak-resolution=high")) |
| 89 | MAC_(clo_leak_resolution) = Vg_HighRes; |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 90 | |
| 91 | else |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 92 | return VG_(replacement_malloc_process_cmd_line_option)(arg); |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 93 | |
| 94 | return True; |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 95 | } |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 96 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 97 | void MAC_(print_common_usage)(void) |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 98 | { |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 99 | VG_(printf)( |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 100 | " --partial-loads-ok=no|yes too hard to explain here; see manual [yes]\n" |
| 101 | " --freelist-vol=<number> volume of freed blocks queue [1000000]\n" |
njn | 6a32942 | 2005-03-12 20:38:13 +0000 | [diff] [blame] | 102 | " --leak-check=no|summary|full search for memory leaks at exit? [summary]\n" |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 103 | " --leak-resolution=low|med|high how much bt merging in leak check [low]\n" |
| 104 | " --show-reachable=no|yes show reachable blocks in leak check? [no]\n" |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 105 | " --workaround-gcc296-bugs=no|yes self explanatory [no]\n" |
| 106 | ); |
| 107 | VG_(replacement_malloc_print_usage)(); |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 108 | } |
| 109 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 110 | void MAC_(print_common_debug_usage)(void) |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 111 | { |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 112 | VG_(replacement_malloc_print_debug_usage)(); |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /*------------------------------------------------------------*/ |
| 116 | /*--- Comparing and printing errors ---*/ |
| 117 | /*------------------------------------------------------------*/ |
| 118 | |
| 119 | static __inline__ |
| 120 | void clear_AddrInfo ( AddrInfo* ai ) |
| 121 | { |
| 122 | ai->akind = Unknown; |
| 123 | ai->blksize = 0; |
| 124 | ai->rwoffset = 0; |
| 125 | ai->lastchange = NULL; |
| 126 | ai->stack_tid = VG_INVALID_THREADID; |
| 127 | ai->maybe_gcc = False; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 128 | ai->desc = NULL; |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 129 | } |
| 130 | |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 131 | void MAC_(clear_MAC_Error) ( MAC_Error* err_extra ) |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 132 | { |
| 133 | err_extra->axskind = ReadAxs; |
| 134 | err_extra->size = 0; |
| 135 | clear_AddrInfo ( &err_extra->addrinfo ); |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 136 | err_extra->isUnaddr = True; |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | __attribute__ ((unused)) |
| 140 | static Bool eq_AddrInfo ( VgRes res, AddrInfo* ai1, AddrInfo* ai2 ) |
| 141 | { |
| 142 | if (ai1->akind != Undescribed |
| 143 | && ai2->akind != Undescribed |
| 144 | && ai1->akind != ai2->akind) |
| 145 | return False; |
| 146 | if (ai1->akind == Freed || ai1->akind == Mallocd) { |
| 147 | if (ai1->blksize != ai2->blksize) |
| 148 | return False; |
| 149 | if (!VG_(eq_ExeContext)(res, ai1->lastchange, ai2->lastchange)) |
| 150 | return False; |
| 151 | } |
| 152 | return True; |
| 153 | } |
| 154 | |
| 155 | /* Compare error contexts, to detect duplicates. Note that if they |
| 156 | are otherwise the same, the faulting addrs and associated rwoffsets |
| 157 | are allowed to be different. */ |
| 158 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 159 | Bool MAC_(eq_Error) ( VgRes res, Error* e1, Error* e2 ) |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 160 | { |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 161 | MAC_Error* e1_extra = VG_(get_error_extra)(e1); |
| 162 | MAC_Error* e2_extra = VG_(get_error_extra)(e2); |
njn | 7cc53a8 | 2002-11-19 16:19:32 +0000 | [diff] [blame] | 163 | |
| 164 | /* Guaranteed by calling function */ |
njn | ca82cc0 | 2004-11-22 17:18:48 +0000 | [diff] [blame] | 165 | tl_assert(VG_(get_error_kind)(e1) == VG_(get_error_kind)(e2)); |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 166 | |
| 167 | switch (VG_(get_error_kind)(e1)) { |
| 168 | case CoreMemErr: { |
| 169 | Char *e1s, *e2s; |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 170 | if (e1_extra->isUnaddr != e2_extra->isUnaddr) return False; |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 171 | e1s = VG_(get_error_string)(e1); |
| 172 | e2s = VG_(get_error_string)(e2); |
| 173 | if (e1s == e2s) return True; |
| 174 | if (0 == VG_(strcmp)(e1s, e2s)) return True; |
| 175 | return False; |
| 176 | } |
| 177 | |
njn | 1639046 | 2005-05-08 15:26:00 +0000 | [diff] [blame] | 178 | // Perhaps we should also check the addrinfo.akinds for equality. |
| 179 | // That would result in more error reports, but only in cases where |
| 180 | // a register contains uninitialised bytes and points to memory |
| 181 | // containing uninitialised bytes. Currently, the 2nd of those to be |
| 182 | // detected won't be reported. That is (nearly?) always the memory |
| 183 | // error, which is good. |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 184 | case ParamErr: |
njn | 1639046 | 2005-05-08 15:26:00 +0000 | [diff] [blame] | 185 | if (0 != VG_(strcmp)(VG_(get_error_string)(e1), |
| 186 | VG_(get_error_string)(e2))) return False; |
| 187 | // fall through |
| 188 | case UserErr: |
| 189 | if (e1_extra->isUnaddr != e2_extra->isUnaddr) return False; |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 190 | return True; |
| 191 | |
| 192 | case FreeErr: |
| 193 | case FreeMismatchErr: |
| 194 | /* JRS 2002-Aug-26: comparing addrs seems overkill and can |
| 195 | cause excessive duplication of errors. Not even AddrErr |
| 196 | below does that. So don't compare either the .addr field |
| 197 | or the .addrinfo fields. */ |
| 198 | /* if (e1->addr != e2->addr) return False; */ |
| 199 | /* if (!eq_AddrInfo(res, &e1_extra->addrinfo, &e2_extra->addrinfo)) |
| 200 | return False; |
| 201 | */ |
| 202 | return True; |
| 203 | |
| 204 | case AddrErr: |
| 205 | /* if (e1_extra->axskind != e2_extra->axskind) return False; */ |
| 206 | if (e1_extra->size != e2_extra->size) return False; |
| 207 | /* |
| 208 | if (!eq_AddrInfo(res, &e1_extra->addrinfo, &e2_extra->addrinfo)) |
| 209 | return False; |
| 210 | */ |
| 211 | return True; |
| 212 | |
| 213 | case ValueErr: |
| 214 | if (e1_extra->size != e2_extra->size) return False; |
| 215 | return True; |
| 216 | |
njn | 34419c1 | 2003-05-02 17:24:29 +0000 | [diff] [blame] | 217 | case OverlapErr: |
| 218 | return True; |
| 219 | |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 220 | case LeakErr: |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 221 | VG_(tool_panic)("Shouldn't get LeakErr in MAC_(eq_Error),\n" |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 222 | "since it's handled with VG_(unique_error)()!"); |
| 223 | |
rjwalsh | bc0bb83 | 2004-06-19 18:12:36 +0000 | [diff] [blame] | 224 | case IllegalMempoolErr: |
| 225 | return True; |
| 226 | |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 227 | default: |
| 228 | VG_(printf)("Error:\n unknown error code %d\n", |
| 229 | VG_(get_error_kind)(e1)); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 230 | VG_(tool_panic)("unknown error code in MAC_(eq_Error)"); |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 234 | void MAC_(pp_AddrInfo) ( Addr a, AddrInfo* ai ) |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 235 | { |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 236 | HChar* xpre = VG_(clo_xml) ? " <auxwhat>" : " "; |
| 237 | HChar* xpost = VG_(clo_xml) ? "</auxwhat>" : ""; |
| 238 | |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 239 | switch (ai->akind) { |
| 240 | case Stack: |
| 241 | VG_(message)(Vg_UserMsg, |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 242 | "%sAddress 0x%llx is on thread %d's stack%s", |
| 243 | xpre, (ULong)a, ai->stack_tid, xpost); |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 244 | break; |
| 245 | case Unknown: |
| 246 | if (ai->maybe_gcc) { |
| 247 | VG_(message)(Vg_UserMsg, |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 248 | "%sAddress 0x%llx is just below the stack ptr. " |
| 249 | "To suppress, use: --workaround-gcc296-bugs=yes%s", |
| 250 | xpre, (ULong)a, xpost |
| 251 | ); |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 252 | } else { |
| 253 | VG_(message)(Vg_UserMsg, |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 254 | "%sAddress 0x%llx " |
| 255 | "is not stack'd, malloc'd or (recently) free'd%s", |
| 256 | xpre, (ULong)a, xpost); |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 257 | } |
| 258 | break; |
rjwalsh | bc0bb83 | 2004-06-19 18:12:36 +0000 | [diff] [blame] | 259 | case Freed: case Mallocd: case UserG: case Mempool: { |
nethercote | 50397c2 | 2004-11-04 18:03:06 +0000 | [diff] [blame] | 260 | SizeT delta; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 261 | const Char* relative; |
| 262 | const Char* kind; |
rjwalsh | bc0bb83 | 2004-06-19 18:12:36 +0000 | [diff] [blame] | 263 | if (ai->akind == Mempool) { |
| 264 | kind = "mempool"; |
| 265 | } else { |
| 266 | kind = "block"; |
| 267 | } |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 268 | if (ai->desc != NULL) |
| 269 | kind = ai->desc; |
| 270 | |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 271 | if (ai->rwoffset < 0) { |
nethercote | 50397c2 | 2004-11-04 18:03:06 +0000 | [diff] [blame] | 272 | delta = (SizeT)(- ai->rwoffset); |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 273 | relative = "before"; |
| 274 | } else if (ai->rwoffset >= ai->blksize) { |
| 275 | delta = ai->rwoffset - ai->blksize; |
| 276 | relative = "after"; |
| 277 | } else { |
| 278 | delta = ai->rwoffset; |
| 279 | relative = "inside"; |
| 280 | } |
sewardj | a81709d | 2002-12-28 12:55:48 +0000 | [diff] [blame] | 281 | VG_(message)(Vg_UserMsg, |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 282 | "%sAddress 0x%llx is %llu bytes %s a %s of size %d %s%s", |
| 283 | xpre, |
tom | 14ea685 | 2005-05-03 18:16:12 +0000 | [diff] [blame] | 284 | (ULong)a, (ULong)delta, relative, kind, |
sewardj | a81709d | 2002-12-28 12:55:48 +0000 | [diff] [blame] | 285 | ai->blksize, |
| 286 | ai->akind==Mallocd ? "alloc'd" |
| 287 | : ai->akind==Freed ? "free'd" |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 288 | : "client-defined", |
| 289 | xpost); |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 290 | VG_(pp_ExeContext)(ai->lastchange); |
| 291 | break; |
| 292 | } |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 293 | case Register: |
| 294 | // print nothing |
njn | ca82cc0 | 2004-11-22 17:18:48 +0000 | [diff] [blame] | 295 | tl_assert(0 == a); |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 296 | break; |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 297 | default: |
njn | 6799325 | 2004-11-22 18:02:32 +0000 | [diff] [blame] | 298 | VG_(tool_panic)("MAC_(pp_AddrInfo)"); |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 299 | } |
| 300 | } |
| 301 | |
| 302 | /* This prints out the message for the error types where Memcheck and |
| 303 | Addrcheck have identical messages */ |
njn | b126f73 | 2004-11-22 17:57:07 +0000 | [diff] [blame] | 304 | void MAC_(pp_shared_Error) ( Error* err ) |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 305 | { |
| 306 | MAC_Error* err_extra = VG_(get_error_extra)(err); |
| 307 | |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 308 | HChar* xpre = VG_(clo_xml) ? " <what>" : ""; |
| 309 | HChar* xpost = VG_(clo_xml) ? "</what>" : ""; |
| 310 | |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 311 | switch (VG_(get_error_kind)(err)) { |
| 312 | case FreeErr: |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 313 | if (VG_(clo_xml)) |
| 314 | VG_(message)(Vg_UserMsg, " <kind>InvalidFree</kind>"); |
| 315 | VG_(message)(Vg_UserMsg, |
| 316 | "%sInvalid free() / delete / delete[]%s", |
| 317 | xpre, xpost); |
| 318 | VG_(pp_ExeContext)( VG_(get_error_where)(err) ); |
| 319 | MAC_(pp_AddrInfo)(VG_(get_error_address)(err), &err_extra->addrinfo); |
| 320 | break; |
| 321 | |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 322 | case FreeMismatchErr: |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 323 | if (VG_(clo_xml)) |
| 324 | VG_(message)(Vg_UserMsg, " <kind>MismatchedFree</kind>"); |
| 325 | VG_(message)(Vg_UserMsg, |
| 326 | "%sMismatched free() / delete / delete []%s", |
| 327 | xpre, xpost); |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 328 | VG_(pp_ExeContext)( VG_(get_error_where)(err) ); |
| 329 | MAC_(pp_AddrInfo)(VG_(get_error_address)(err), &err_extra->addrinfo); |
| 330 | break; |
| 331 | |
njn | 89409f8 | 2003-09-26 14:55:31 +0000 | [diff] [blame] | 332 | case AddrErr: |
| 333 | switch (err_extra->axskind) { |
| 334 | case ReadAxs: |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 335 | if (VG_(clo_xml)) |
| 336 | VG_(message)(Vg_UserMsg, " <kind>InvalidRead</kind>"); |
| 337 | VG_(message)(Vg_UserMsg, |
| 338 | "%sInvalid read of size %d%s", |
| 339 | xpre, err_extra->size, xpost ); |
njn | 89409f8 | 2003-09-26 14:55:31 +0000 | [diff] [blame] | 340 | break; |
| 341 | case WriteAxs: |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 342 | if (VG_(clo_xml)) |
| 343 | VG_(message)(Vg_UserMsg, " <kind>InvalidWrite</kind>"); |
| 344 | VG_(message)(Vg_UserMsg, |
| 345 | "%sInvalid write of size %d%s", |
| 346 | xpre, err_extra->size, xpost ); |
njn | 89409f8 | 2003-09-26 14:55:31 +0000 | [diff] [blame] | 347 | break; |
| 348 | case ExecAxs: |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 349 | if (VG_(clo_xml)) |
| 350 | VG_(message)(Vg_UserMsg, " <kind>InvalidJump</kind>"); |
| 351 | VG_(message)(Vg_UserMsg, |
| 352 | "%sJump to the invalid address " |
| 353 | "stated on the next line%s", |
| 354 | xpre, xpost); |
njn | 89409f8 | 2003-09-26 14:55:31 +0000 | [diff] [blame] | 355 | break; |
| 356 | default: |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 357 | VG_(tool_panic)("MAC_(pp_shared_Error)(axskind)"); |
njn | 89409f8 | 2003-09-26 14:55:31 +0000 | [diff] [blame] | 358 | } |
| 359 | VG_(pp_ExeContext)( VG_(get_error_where)(err) ); |
| 360 | MAC_(pp_AddrInfo)(VG_(get_error_address)(err), &err_extra->addrinfo); |
| 361 | break; |
| 362 | |
njn | b6cae9f | 2003-09-04 20:50:47 +0000 | [diff] [blame] | 363 | case OverlapErr: { |
| 364 | OverlapExtra* ov_extra = (OverlapExtra*)VG_(get_error_extra)(err); |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 365 | if (VG_(clo_xml)) |
| 366 | VG_(message)(Vg_UserMsg, " <kind>Overlap</kind>"); |
njn | b6cae9f | 2003-09-04 20:50:47 +0000 | [diff] [blame] | 367 | if (ov_extra->len == -1) |
| 368 | VG_(message)(Vg_UserMsg, |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 369 | "%sSource and destination overlap in %s(%p, %p)%s", |
| 370 | xpre, |
njn | b6cae9f | 2003-09-04 20:50:47 +0000 | [diff] [blame] | 371 | VG_(get_error_string)(err), |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 372 | ov_extra->dst, ov_extra->src, |
| 373 | xpost); |
njn | b6cae9f | 2003-09-04 20:50:47 +0000 | [diff] [blame] | 374 | else |
| 375 | VG_(message)(Vg_UserMsg, |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 376 | "%sSource and destination overlap in %s(%p, %p, %d)%s", |
| 377 | xpre, |
njn | b6cae9f | 2003-09-04 20:50:47 +0000 | [diff] [blame] | 378 | VG_(get_error_string)(err), |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 379 | ov_extra->dst, ov_extra->src, ov_extra->len, |
| 380 | xpost); |
njn | 66fe05a | 2003-07-22 09:12:33 +0000 | [diff] [blame] | 381 | VG_(pp_ExeContext)( VG_(get_error_where)(err) ); |
| 382 | break; |
njn | b6cae9f | 2003-09-04 20:50:47 +0000 | [diff] [blame] | 383 | } |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 384 | case LeakErr: { |
njn | 0297703 | 2005-05-17 04:00:11 +0000 | [diff] [blame] | 385 | MAC_(pp_LeakError)(err_extra); |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 386 | break; |
| 387 | } |
| 388 | |
rjwalsh | bc0bb83 | 2004-06-19 18:12:36 +0000 | [diff] [blame] | 389 | case IllegalMempoolErr: |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 390 | if (VG_(clo_xml)) |
| 391 | VG_(message)(Vg_UserMsg, " <kind>InvalidMemPool</kind>"); |
| 392 | VG_(message)(Vg_UserMsg, "%sIllegal memory pool address%s", |
| 393 | xpre, xpost); |
rjwalsh | bc0bb83 | 2004-06-19 18:12:36 +0000 | [diff] [blame] | 394 | VG_(pp_ExeContext)( VG_(get_error_where)(err) ); |
| 395 | MAC_(pp_AddrInfo)(VG_(get_error_address)(err), &err_extra->addrinfo); |
| 396 | break; |
| 397 | |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 398 | default: |
| 399 | VG_(printf)("Error:\n unknown Memcheck/Addrcheck error code %d\n", |
| 400 | VG_(get_error_kind)(err)); |
njn | 6799325 | 2004-11-22 18:02:32 +0000 | [diff] [blame] | 401 | VG_(tool_panic)("unknown error code in MAC_(pp_shared_Error)"); |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 402 | } |
| 403 | } |
| 404 | |
| 405 | /*------------------------------------------------------------*/ |
| 406 | /*--- Recording errors ---*/ |
| 407 | /*------------------------------------------------------------*/ |
| 408 | |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 409 | /* Additional description function for describe_addr(); used by |
| 410 | MemCheck for user blocks, which Addrcheck doesn't support. */ |
| 411 | Bool (*MAC_(describe_addr_supp)) ( Addr a, AddrInfo* ai ) = NULL; |
thughes | 4ad52d0 | 2004-06-27 17:37:21 +0000 | [diff] [blame] | 412 | |
| 413 | /* Callback for searching thread stacks */ |
| 414 | static Bool addr_is_in_bounds(Addr stack_min, Addr stack_max, void *ap) |
| 415 | { |
| 416 | Addr a = *(Addr *)ap; |
| 417 | |
| 418 | return (stack_min <= a && a <= stack_max); |
| 419 | } |
| 420 | |
| 421 | /* Callback for searching free'd list */ |
| 422 | static Bool addr_is_in_MAC_Chunk(MAC_Chunk* mc, void *ap) |
| 423 | { |
| 424 | Addr a = *(Addr *)ap; |
| 425 | |
njn | 717cde5 | 2005-05-10 02:47:21 +0000 | [diff] [blame] | 426 | return VG_(addr_is_in_block)( a, mc->data, mc->size, |
| 427 | MAC_MALLOC_REDZONE_SZB ); |
thughes | 4ad52d0 | 2004-06-27 17:37:21 +0000 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | /* Callback for searching malloc'd lists */ |
| 431 | static Bool addr_is_in_HashNode(VgHashNode* sh_ch, void *ap) |
| 432 | { |
| 433 | return addr_is_in_MAC_Chunk( (MAC_Chunk*)sh_ch, ap ); |
| 434 | } |
| 435 | |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 436 | /* Describe an address as best you can, for error messages, |
| 437 | putting the result in ai. */ |
| 438 | static void describe_addr ( Addr a, AddrInfo* ai ) |
| 439 | { |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 440 | MAC_Chunk* sc; |
| 441 | ThreadId tid; |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 442 | |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 443 | /* Perhaps it's a user-def'd block ? (only check if requested, though) */ |
| 444 | if (NULL != MAC_(describe_addr_supp)) { |
| 445 | if (MAC_(describe_addr_supp)( a, ai )) |
| 446 | return; |
| 447 | } |
| 448 | /* Perhaps it's on a thread's stack? */ |
thughes | 4ad52d0 | 2004-06-27 17:37:21 +0000 | [diff] [blame] | 449 | tid = VG_(first_matching_thread_stack)(addr_is_in_bounds, &a); |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 450 | if (tid != VG_INVALID_THREADID) { |
| 451 | ai->akind = Stack; |
| 452 | ai->stack_tid = tid; |
| 453 | return; |
| 454 | } |
| 455 | /* Search for a recently freed block which might bracket it. */ |
thughes | 4ad52d0 | 2004-06-27 17:37:21 +0000 | [diff] [blame] | 456 | sc = MAC_(first_matching_freed_MAC_Chunk)(addr_is_in_MAC_Chunk, &a); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 457 | if (NULL != sc) { |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 458 | ai->akind = Freed; |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 459 | ai->blksize = sc->size; |
| 460 | ai->rwoffset = (Int)a - (Int)sc->data; |
| 461 | ai->lastchange = sc->where; |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 462 | return; |
| 463 | } |
| 464 | /* Search for a currently malloc'd block which might bracket it. */ |
thughes | 4ad52d0 | 2004-06-27 17:37:21 +0000 | [diff] [blame] | 465 | sc = (MAC_Chunk*)VG_(HT_first_match)(MAC_(malloc_list), addr_is_in_HashNode, &a); |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 466 | if (NULL != sc) { |
| 467 | ai->akind = Mallocd; |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 468 | ai->blksize = sc->size; |
| 469 | ai->rwoffset = (Int)(a) - (Int)sc->data; |
| 470 | ai->lastchange = sc->where; |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 471 | return; |
| 472 | } |
| 473 | /* Clueless ... */ |
| 474 | ai->akind = Unknown; |
| 475 | return; |
| 476 | } |
| 477 | |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 478 | /* Is this address within some small distance below %ESP? Used only |
| 479 | for the --workaround-gcc296-bugs kludge. */ |
| 480 | static Bool is_just_below_ESP( Addr esp, Addr aa ) |
| 481 | { |
nethercote | 50397c2 | 2004-11-04 18:03:06 +0000 | [diff] [blame] | 482 | if (esp > aa && (esp - aa) <= VG_GCC296_BUG_STACK_SLOP) |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 483 | return True; |
| 484 | else |
| 485 | return False; |
| 486 | } |
| 487 | |
sewardj | af48a60 | 2003-07-06 00:54:47 +0000 | [diff] [blame] | 488 | /* This one called from generated code and non-generated code. */ |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 489 | |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 490 | void MAC_(record_address_error) ( ThreadId tid, Addr a, Int size, |
sewardj | af48a60 | 2003-07-06 00:54:47 +0000 | [diff] [blame] | 491 | Bool isWrite ) |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 492 | { |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 493 | MAC_Error err_extra; |
| 494 | Bool just_below_esp; |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 495 | |
njn | 6751613 | 2005-03-22 04:02:43 +0000 | [diff] [blame] | 496 | just_below_esp = is_just_below_ESP( VG_(get_SP)(tid), a ); |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 497 | |
| 498 | /* If this is caused by an access immediately below %ESP, and the |
| 499 | user asks nicely, we just ignore it. */ |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 500 | if (MAC_(clo_workaround_gcc296_bugs) && just_below_esp) |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 501 | return; |
| 502 | |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 503 | MAC_(clear_MAC_Error)( &err_extra ); |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 504 | err_extra.axskind = isWrite ? WriteAxs : ReadAxs; |
| 505 | err_extra.size = size; |
| 506 | err_extra.addrinfo.akind = Undescribed; |
| 507 | err_extra.addrinfo.maybe_gcc = just_below_esp; |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 508 | VG_(maybe_record_error)( tid, AddrErr, a, /*s*/NULL, &err_extra ); |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | /* These ones are called from non-generated code */ |
| 512 | |
| 513 | /* This is for memory errors in pthread functions, as opposed to pthread API |
| 514 | errors which are found by the core. */ |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 515 | void MAC_(record_core_mem_error) ( ThreadId tid, Bool isUnaddr, Char* msg ) |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 516 | { |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 517 | MAC_Error err_extra; |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 518 | |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 519 | MAC_(clear_MAC_Error)( &err_extra ); |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 520 | err_extra.isUnaddr = isUnaddr; |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 521 | VG_(maybe_record_error)( tid, CoreMemErr, /*addr*/0, msg, &err_extra ); |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 522 | } |
| 523 | |
njn | 1639046 | 2005-05-08 15:26:00 +0000 | [diff] [blame] | 524 | // Three kinds of param errors: |
| 525 | // - register arg contains undefined bytes |
| 526 | // - memory arg is unaddressable |
| 527 | // - memory arg contains undefined bytes |
| 528 | // 'isReg' and 'isUnaddr' dictate which of these it is. |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 529 | void MAC_(record_param_error) ( ThreadId tid, Addr a, Bool isReg, |
| 530 | Bool isUnaddr, Char* msg ) |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 531 | { |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 532 | MAC_Error err_extra; |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 533 | |
njn | ca82cc0 | 2004-11-22 17:18:48 +0000 | [diff] [blame] | 534 | tl_assert(VG_INVALID_THREADID != tid); |
njn | 1639046 | 2005-05-08 15:26:00 +0000 | [diff] [blame] | 535 | if (isUnaddr) tl_assert(!isReg); // unaddressable register is impossible |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 536 | MAC_(clear_MAC_Error)( &err_extra ); |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 537 | err_extra.addrinfo.akind = ( isReg ? Register : Undescribed ); |
| 538 | err_extra.isUnaddr = isUnaddr; |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 539 | VG_(maybe_record_error)( tid, ParamErr, a, msg, &err_extra ); |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 540 | } |
| 541 | |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 542 | void MAC_(record_jump_error) ( ThreadId tid, Addr a ) |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 543 | { |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 544 | MAC_Error err_extra; |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 545 | |
njn | ca82cc0 | 2004-11-22 17:18:48 +0000 | [diff] [blame] | 546 | tl_assert(VG_INVALID_THREADID != tid); |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 547 | MAC_(clear_MAC_Error)( &err_extra ); |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 548 | err_extra.axskind = ExecAxs; |
nethercote | 7250b8f | 2004-04-13 08:47:35 +0000 | [diff] [blame] | 549 | err_extra.size = 1; // size only used for suppressions |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 550 | err_extra.addrinfo.akind = Undescribed; |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 551 | VG_(maybe_record_error)( tid, AddrErr, a, /*s*/NULL, &err_extra ); |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 552 | } |
| 553 | |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 554 | void MAC_(record_free_error) ( ThreadId tid, Addr a ) |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 555 | { |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 556 | MAC_Error err_extra; |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 557 | |
njn | ca82cc0 | 2004-11-22 17:18:48 +0000 | [diff] [blame] | 558 | tl_assert(VG_INVALID_THREADID != tid); |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 559 | MAC_(clear_MAC_Error)( &err_extra ); |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 560 | err_extra.addrinfo.akind = Undescribed; |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 561 | VG_(maybe_record_error)( tid, FreeErr, a, /*s*/NULL, &err_extra ); |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 562 | } |
| 563 | |
rjwalsh | bc0bb83 | 2004-06-19 18:12:36 +0000 | [diff] [blame] | 564 | void MAC_(record_illegal_mempool_error) ( ThreadId tid, Addr a ) |
| 565 | { |
| 566 | MAC_Error err_extra; |
| 567 | |
njn | ca82cc0 | 2004-11-22 17:18:48 +0000 | [diff] [blame] | 568 | tl_assert(VG_INVALID_THREADID != tid); |
rjwalsh | bc0bb83 | 2004-06-19 18:12:36 +0000 | [diff] [blame] | 569 | MAC_(clear_MAC_Error)( &err_extra ); |
| 570 | err_extra.addrinfo.akind = Undescribed; |
| 571 | VG_(maybe_record_error)( tid, IllegalMempoolErr, a, /*s*/NULL, &err_extra ); |
| 572 | } |
| 573 | |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 574 | void MAC_(record_freemismatch_error) ( ThreadId tid, Addr a ) |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 575 | { |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 576 | MAC_Error err_extra; |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 577 | |
njn | ca82cc0 | 2004-11-22 17:18:48 +0000 | [diff] [blame] | 578 | tl_assert(VG_INVALID_THREADID != tid); |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 579 | MAC_(clear_MAC_Error)( &err_extra ); |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 580 | err_extra.addrinfo.akind = Undescribed; |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 581 | VG_(maybe_record_error)( tid, FreeMismatchErr, a, /*s*/NULL, &err_extra ); |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 582 | } |
| 583 | |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 584 | void MAC_(record_overlap_error) ( ThreadId tid, |
| 585 | Char* function, OverlapExtra* ov_extra ) |
njn | 66fe05a | 2003-07-22 09:12:33 +0000 | [diff] [blame] | 586 | { |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 587 | VG_(maybe_record_error)( |
| 588 | tid, OverlapErr, /*addr*/0, /*s*/function, ov_extra ); |
njn | 66fe05a | 2003-07-22 09:12:33 +0000 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | |
njn | b6cae9f | 2003-09-04 20:50:47 +0000 | [diff] [blame] | 592 | /* Updates the copy with address info if necessary (but not for all errors). */ |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 593 | UInt MAC_(update_extra)( Error* err ) |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 594 | { |
njn | b6cae9f | 2003-09-04 20:50:47 +0000 | [diff] [blame] | 595 | switch (VG_(get_error_kind)(err)) { |
| 596 | case ValueErr: |
| 597 | case CoreMemErr: |
| 598 | case AddrErr: |
| 599 | case ParamErr: |
| 600 | case UserErr: |
| 601 | case FreeErr: |
rjwalsh | bc0bb83 | 2004-06-19 18:12:36 +0000 | [diff] [blame] | 602 | case IllegalMempoolErr: |
njn | b6cae9f | 2003-09-04 20:50:47 +0000 | [diff] [blame] | 603 | case FreeMismatchErr: { |
nethercote | 8b76fe5 | 2004-11-08 19:20:09 +0000 | [diff] [blame] | 604 | MAC_Error* extra = VG_(get_error_extra)(err); |
njn | b6cae9f | 2003-09-04 20:50:47 +0000 | [diff] [blame] | 605 | if (extra != NULL && Undescribed == extra->addrinfo.akind) { |
| 606 | describe_addr ( VG_(get_error_address)(err), &(extra->addrinfo) ); |
| 607 | } |
| 608 | return sizeof(MAC_Error); |
| 609 | } |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 610 | /* Don't need to return the correct size -- LeakErrs are always shown with |
| 611 | VG_(unique_error)() so they're not copied anyway. */ |
njn | b6cae9f | 2003-09-04 20:50:47 +0000 | [diff] [blame] | 612 | case LeakErr: return 0; |
| 613 | case OverlapErr: return sizeof(OverlapExtra); |
njn | 6799325 | 2004-11-22 18:02:32 +0000 | [diff] [blame] | 614 | default: VG_(tool_panic)("update_extra: bad errkind"); |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 615 | } |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 619 | /*------------------------------------------------------------*/ |
| 620 | /*--- Suppressions ---*/ |
| 621 | /*------------------------------------------------------------*/ |
| 622 | |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 623 | Bool MAC_(shared_recognised_suppression) ( Char* name, Supp* su ) |
| 624 | { |
| 625 | SuppKind skind; |
| 626 | |
| 627 | if (VG_STREQ(name, "Param")) skind = ParamSupp; |
| 628 | else if (VG_STREQ(name, "CoreMem")) skind = CoreMemSupp; |
| 629 | else if (VG_STREQ(name, "Addr1")) skind = Addr1Supp; |
| 630 | else if (VG_STREQ(name, "Addr2")) skind = Addr2Supp; |
| 631 | else if (VG_STREQ(name, "Addr4")) skind = Addr4Supp; |
| 632 | else if (VG_STREQ(name, "Addr8")) skind = Addr8Supp; |
njn | c061666 | 2003-06-12 09:58:41 +0000 | [diff] [blame] | 633 | else if (VG_STREQ(name, "Addr16")) skind = Addr16Supp; |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 634 | else if (VG_STREQ(name, "Free")) skind = FreeSupp; |
| 635 | else if (VG_STREQ(name, "Leak")) skind = LeakSupp; |
njn | e9cab14 | 2003-09-19 08:10:07 +0000 | [diff] [blame] | 636 | else if (VG_STREQ(name, "Overlap")) skind = OverlapSupp; |
rjwalsh | bc0bb83 | 2004-06-19 18:12:36 +0000 | [diff] [blame] | 637 | else if (VG_STREQ(name, "Mempool")) skind = MempoolSupp; |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 638 | else |
| 639 | return False; |
| 640 | |
| 641 | VG_(set_supp_kind)(su, skind); |
| 642 | return True; |
| 643 | } |
| 644 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 645 | Bool MAC_(read_extra_suppression_info) ( Int fd, Char* buf, Int nBuf, Supp *su ) |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 646 | { |
| 647 | Bool eof; |
| 648 | |
| 649 | if (VG_(get_supp_kind)(su) == ParamSupp) { |
| 650 | eof = VG_(get_line) ( fd, buf, nBuf ); |
| 651 | if (eof) return False; |
| 652 | VG_(set_supp_string)(su, VG_(strdup)(buf)); |
| 653 | } |
| 654 | return True; |
| 655 | } |
| 656 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 657 | Bool MAC_(error_matches_suppression)(Error* err, Supp* su) |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 658 | { |
sewardj | 05bcdcb | 2003-05-18 10:05:38 +0000 | [diff] [blame] | 659 | Int su_size; |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 660 | MAC_Error* err_extra = VG_(get_error_extra)(err); |
| 661 | ErrorKind ekind = VG_(get_error_kind )(err); |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 662 | |
| 663 | switch (VG_(get_supp_kind)(su)) { |
| 664 | case ParamSupp: |
| 665 | return (ekind == ParamErr |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 666 | && VG_STREQ(VG_(get_error_string)(err), |
| 667 | VG_(get_supp_string)(su))); |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 668 | |
| 669 | case CoreMemSupp: |
| 670 | return (ekind == CoreMemErr |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 671 | && VG_STREQ(VG_(get_error_string)(err), |
| 672 | VG_(get_supp_string)(su))); |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 673 | |
| 674 | case Value0Supp: su_size = 0; goto value_case; |
| 675 | case Value1Supp: su_size = 1; goto value_case; |
| 676 | case Value2Supp: su_size = 2; goto value_case; |
| 677 | case Value4Supp: su_size = 4; goto value_case; |
| 678 | case Value8Supp: su_size = 8; goto value_case; |
njn | c061666 | 2003-06-12 09:58:41 +0000 | [diff] [blame] | 679 | case Value16Supp:su_size =16; goto value_case; |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 680 | value_case: |
| 681 | return (ekind == ValueErr && err_extra->size == su_size); |
| 682 | |
| 683 | case Addr1Supp: su_size = 1; goto addr_case; |
| 684 | case Addr2Supp: su_size = 2; goto addr_case; |
| 685 | case Addr4Supp: su_size = 4; goto addr_case; |
| 686 | case Addr8Supp: su_size = 8; goto addr_case; |
njn | c061666 | 2003-06-12 09:58:41 +0000 | [diff] [blame] | 687 | case Addr16Supp:su_size =16; goto addr_case; |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 688 | addr_case: |
| 689 | return (ekind == AddrErr && err_extra->size == su_size); |
| 690 | |
| 691 | case FreeSupp: |
| 692 | return (ekind == FreeErr || ekind == FreeMismatchErr); |
| 693 | |
njn | 34419c1 | 2003-05-02 17:24:29 +0000 | [diff] [blame] | 694 | case OverlapSupp: |
| 695 | return (ekind = OverlapErr); |
| 696 | |
sewardj | 4a19e2f | 2002-12-26 11:50:21 +0000 | [diff] [blame] | 697 | case LeakSupp: |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 698 | return (ekind == LeakErr); |
sewardj | 4a19e2f | 2002-12-26 11:50:21 +0000 | [diff] [blame] | 699 | |
rjwalsh | bc0bb83 | 2004-06-19 18:12:36 +0000 | [diff] [blame] | 700 | case MempoolSupp: |
| 701 | return (ekind == IllegalMempoolErr); |
| 702 | |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 703 | default: |
| 704 | VG_(printf)("Error:\n" |
| 705 | " unknown suppression type %d\n", |
| 706 | VG_(get_supp_kind)(su)); |
njn | 6799325 | 2004-11-22 18:02:32 +0000 | [diff] [blame] | 707 | VG_(tool_panic)("unknown suppression type in " |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 708 | "MAC_(error_matches_suppression)"); |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 709 | } |
| 710 | } |
| 711 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 712 | Char* MAC_(get_error_name) ( Error* err ) |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 713 | { |
| 714 | Char* s; |
| 715 | switch (VG_(get_error_kind)(err)) { |
| 716 | case ParamErr: return "Param"; |
| 717 | case UserErr: return NULL; /* Can't suppress User errors */ |
| 718 | case FreeMismatchErr: return "Free"; |
rjwalsh | bc0bb83 | 2004-06-19 18:12:36 +0000 | [diff] [blame] | 719 | case IllegalMempoolErr: return "Mempool"; |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 720 | case FreeErr: return "Free"; |
| 721 | case AddrErr: |
| 722 | switch ( ((MAC_Error*)VG_(get_error_extra)(err))->size ) { |
| 723 | case 1: return "Addr1"; |
| 724 | case 2: return "Addr2"; |
| 725 | case 4: return "Addr4"; |
| 726 | case 8: return "Addr8"; |
njn | c061666 | 2003-06-12 09:58:41 +0000 | [diff] [blame] | 727 | case 16: return "Addr16"; |
njn | 6799325 | 2004-11-22 18:02:32 +0000 | [diff] [blame] | 728 | default: VG_(tool_panic)("unexpected size for Addr"); |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | case ValueErr: |
| 732 | switch ( ((MAC_Error*)VG_(get_error_extra)(err))->size ) { |
| 733 | case 0: return "Cond"; |
| 734 | case 1: return "Value1"; |
| 735 | case 2: return "Value2"; |
| 736 | case 4: return "Value4"; |
| 737 | case 8: return "Value8"; |
njn | c061666 | 2003-06-12 09:58:41 +0000 | [diff] [blame] | 738 | case 16: return "Value16"; |
njn | 6799325 | 2004-11-22 18:02:32 +0000 | [diff] [blame] | 739 | default: VG_(tool_panic)("unexpected size for Value"); |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 740 | } |
| 741 | case CoreMemErr: return "CoreMem"; |
njn | 34419c1 | 2003-05-02 17:24:29 +0000 | [diff] [blame] | 742 | case OverlapErr: return "Overlap"; |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 743 | case LeakErr: return "Leak"; |
njn | 6799325 | 2004-11-22 18:02:32 +0000 | [diff] [blame] | 744 | default: VG_(tool_panic)("get_error_name: unexpected type"); |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 745 | } |
| 746 | VG_(printf)(s); |
| 747 | } |
| 748 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 749 | void MAC_(print_extra_suppression_info) ( Error* err ) |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 750 | { |
| 751 | if (ParamErr == VG_(get_error_kind)(err)) { |
| 752 | VG_(printf)(" %s\n", VG_(get_error_string)(err)); |
| 753 | } |
| 754 | } |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 755 | |
| 756 | /*------------------------------------------------------------*/ |
| 757 | /*--- Crude profiling machinery. ---*/ |
| 758 | /*------------------------------------------------------------*/ |
| 759 | |
| 760 | /* Event index. If just the name of the fn is given, this means the |
| 761 | number of calls to the fn. Otherwise it is the specified event. |
| 762 | Ones marked 'M' are MemCheck only. Ones marked 'A' are AddrCheck only. |
| 763 | The rest are shared. |
| 764 | |
| 765 | 10 alloc_secondary_map |
| 766 | |
| 767 | 20 get_abit |
| 768 | M 21 get_vbyte |
| 769 | 22 set_abit |
| 770 | M 23 set_vbyte |
| 771 | 24 get_abits4_ALIGNED |
| 772 | M 25 get_vbytes4_ALIGNED |
| 773 | |
| 774 | 30 set_address_range_perms |
| 775 | 31 set_address_range_perms(lower byte loop) |
| 776 | 32 set_address_range_perms(quadword loop) |
| 777 | 33 set_address_range_perms(upper byte loop) |
| 778 | |
| 779 | 35 make_noaccess |
| 780 | 36 make_writable |
| 781 | 37 make_readable |
| 782 | A 38 make_accessible |
| 783 | |
| 784 | 40 copy_address_range_state |
| 785 | 41 copy_address_range_state(byte loop) |
| 786 | 42 check_writable |
| 787 | 43 check_writable(byte loop) |
| 788 | 44 check_readable |
| 789 | 45 check_readable(byte loop) |
| 790 | 46 check_readable_asciiz |
| 791 | 47 check_readable_asciiz(byte loop) |
| 792 | A 48 check_accessible |
| 793 | A 49 check_accessible(byte loop) |
| 794 | |
| 795 | 50 make_noaccess_aligned |
| 796 | 51 make_writable_aligned |
| 797 | |
| 798 | M 60 helperc_LOADV4 |
| 799 | M 61 helperc_STOREV4 |
| 800 | M 62 helperc_LOADV2 |
| 801 | M 63 helperc_STOREV2 |
| 802 | M 64 helperc_LOADV1 |
| 803 | M 65 helperc_STOREV1 |
| 804 | |
| 805 | A 66 helperc_ACCESS4 |
| 806 | A 67 helperc_ACCESS2 |
| 807 | A 68 helperc_ACCESS1 |
| 808 | |
| 809 | M 70 rim_rd_V4_SLOWLY |
| 810 | M 71 rim_wr_V4_SLOWLY |
| 811 | M 72 rim_rd_V2_SLOWLY |
| 812 | M 73 rim_wr_V2_SLOWLY |
| 813 | M 74 rim_rd_V1_SLOWLY |
| 814 | M 75 rim_wr_V1_SLOWLY |
| 815 | |
| 816 | A 76 ACCESS4_SLOWLY |
| 817 | A 77 ACCESS2_SLOWLY |
| 818 | A 78 ACCESS1_SLOWLY |
| 819 | |
| 820 | 80 fpu_read |
| 821 | 81 fpu_read aligned 4 |
| 822 | 82 fpu_read aligned 8 |
| 823 | 83 fpu_read 2 |
jseward | fca6018 | 2004-01-04 23:30:55 +0000 | [diff] [blame] | 824 | 84 fpu_read 10/28/108/512 |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 825 | |
| 826 | M 85 fpu_write |
| 827 | M 86 fpu_write aligned 4 |
| 828 | M 87 fpu_write aligned 8 |
| 829 | M 88 fpu_write 2 |
jseward | fca6018 | 2004-01-04 23:30:55 +0000 | [diff] [blame] | 830 | M 89 fpu_write 10/28/108/512 |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 831 | |
| 832 | 90 fpu_access |
| 833 | 91 fpu_access aligned 4 |
| 834 | 92 fpu_access aligned 8 |
| 835 | 93 fpu_access 2 |
jseward | fca6018 | 2004-01-04 23:30:55 +0000 | [diff] [blame] | 836 | 94 fpu_access 10/28/108/512 |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 837 | |
| 838 | 100 fpu_access_check_SLOWLY |
| 839 | 101 fpu_access_check_SLOWLY(byte loop) |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 840 | |
| 841 | 110 new_mem_stack_4 |
| 842 | 111 new_mem_stack_8 |
| 843 | 112 new_mem_stack_12 |
| 844 | 113 new_mem_stack_16 |
| 845 | 114 new_mem_stack_32 |
| 846 | 115 new_mem_stack |
| 847 | |
| 848 | 120 die_mem_stack_4 |
| 849 | 121 die_mem_stack_8 |
| 850 | 122 die_mem_stack_12 |
| 851 | 123 die_mem_stack_16 |
| 852 | 124 die_mem_stack_32 |
| 853 | 125 die_mem_stack |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 854 | */ |
| 855 | |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 856 | #ifdef MAC_PROFILE_MEMORY |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 857 | |
sewardj | c1a2cda | 2005-04-21 17:34:00 +0000 | [diff] [blame] | 858 | UInt MAC_(event_ctr)[N_PROF_EVENTS]; |
| 859 | HChar* MAC_(event_ctr_name)[N_PROF_EVENTS]; |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 860 | |
njn | b4aee05 | 2003-04-15 14:09:58 +0000 | [diff] [blame] | 861 | static void init_prof_mem ( void ) |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 862 | { |
| 863 | Int i; |
sewardj | c1a2cda | 2005-04-21 17:34:00 +0000 | [diff] [blame] | 864 | for (i = 0; i < N_PROF_EVENTS; i++) { |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 865 | MAC_(event_ctr)[i] = 0; |
sewardj | c1a2cda | 2005-04-21 17:34:00 +0000 | [diff] [blame] | 866 | MAC_(event_ctr_name)[i] = NULL; |
| 867 | } |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 868 | } |
| 869 | |
njn | b4aee05 | 2003-04-15 14:09:58 +0000 | [diff] [blame] | 870 | static void done_prof_mem ( void ) |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 871 | { |
sewardj | c1a2cda | 2005-04-21 17:34:00 +0000 | [diff] [blame] | 872 | Int i; |
| 873 | Bool spaced = False; |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 874 | for (i = 0; i < N_PROF_EVENTS; i++) { |
sewardj | c1a2cda | 2005-04-21 17:34:00 +0000 | [diff] [blame] | 875 | if (!spaced && (i % 10) == 0) { |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 876 | VG_(printf)("\n"); |
sewardj | c1a2cda | 2005-04-21 17:34:00 +0000 | [diff] [blame] | 877 | spaced = True; |
| 878 | } |
| 879 | if (MAC_(event_ctr)[i] > 0) { |
| 880 | spaced = False; |
| 881 | VG_(printf)( "prof mem event %3d: %9d %s\n", |
| 882 | i, MAC_(event_ctr)[i], |
| 883 | MAC_(event_ctr_name)[i] |
| 884 | ? MAC_(event_ctr_name)[i] : "unnamed"); |
| 885 | } |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 886 | } |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 887 | } |
| 888 | |
| 889 | #else |
| 890 | |
njn | b4aee05 | 2003-04-15 14:09:58 +0000 | [diff] [blame] | 891 | static void init_prof_mem ( void ) { } |
| 892 | static void done_prof_mem ( void ) { } |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 893 | |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 894 | #endif |
| 895 | |
| 896 | /*------------------------------------------------------------*/ |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 897 | /*--- Common initialisation + finalisation ---*/ |
| 898 | /*------------------------------------------------------------*/ |
| 899 | |
| 900 | void MAC_(common_pre_clo_init)(void) |
| 901 | { |
| 902 | MAC_(malloc_list) = VG_(HT_construct)(); |
rjwalsh | bc0bb83 | 2004-06-19 18:12:36 +0000 | [diff] [blame] | 903 | MAC_(mempool_list) = VG_(HT_construct)(); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 904 | init_prof_mem(); |
| 905 | } |
| 906 | |
njn | b8dca86 | 2005-03-14 02:42:44 +0000 | [diff] [blame] | 907 | void MAC_(common_fini)(void (*leak_check)(ThreadId tid, LeakCheckMode mode)) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 908 | { |
njn | 86f12dc | 2005-03-14 01:16:05 +0000 | [diff] [blame] | 909 | MAC_(print_malloc_stats)(); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 910 | |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 911 | if (VG_(clo_verbosity) == 1 && !VG_(clo_xml)) { |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 912 | if (MAC_(clo_leak_check) == LC_Off) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 913 | VG_(message)(Vg_UserMsg, |
| 914 | "For a detailed leak analysis, rerun with: --leak-check=yes"); |
| 915 | |
| 916 | VG_(message)(Vg_UserMsg, |
| 917 | "For counts of detected errors, rerun with: -v"); |
| 918 | } |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 919 | if (MAC_(clo_leak_check) != LC_Off) |
njn | b8dca86 | 2005-03-14 02:42:44 +0000 | [diff] [blame] | 920 | leak_check(1/*bogus ThreadId*/, MAC_(clo_leak_check)); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 921 | |
| 922 | done_prof_mem(); |
| 923 | } |
| 924 | |
| 925 | /*------------------------------------------------------------*/ |
njn | 47363ab | 2003-04-21 13:24:40 +0000 | [diff] [blame] | 926 | /*--- Common client request handling ---*/ |
| 927 | /*------------------------------------------------------------*/ |
| 928 | |
nethercote | d1b64b2 | 2004-11-04 18:22:28 +0000 | [diff] [blame] | 929 | Bool MAC_(handle_common_client_requests)(ThreadId tid, UWord* arg, UWord* ret ) |
njn | 47363ab | 2003-04-21 13:24:40 +0000 | [diff] [blame] | 930 | { |
njn | d799418 | 2003-10-02 13:44:04 +0000 | [diff] [blame] | 931 | Char* err = |
| 932 | "The client requests VALGRIND_MALLOCLIKE_BLOCK and\n" |
| 933 | " VALGRIND_FREELIKE_BLOCK have moved. Please recompile your\n" |
| 934 | " program to incorporate the updates in the Valgrind header files.\n" |
| 935 | " You shouldn't need to change the text of your program at all.\n" |
| 936 | " Everything should then work as before. Sorry for the bother.\n"; |
njn | 47363ab | 2003-04-21 13:24:40 +0000 | [diff] [blame] | 937 | |
| 938 | switch (arg[0]) { |
njn | 1078545 | 2003-05-20 16:38:24 +0000 | [diff] [blame] | 939 | case VG_USERREQ__COUNT_LEAKS: { /* count leaked bytes */ |
nethercote | d1b64b2 | 2004-11-04 18:22:28 +0000 | [diff] [blame] | 940 | UWord** argp = (UWord**)arg; |
njn | e8b5c05 | 2003-07-22 22:03:58 +0000 | [diff] [blame] | 941 | // MAC_(bytes_leaked) et al were set by the last leak check (or zero |
| 942 | // if no prior leak checks performed). |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 943 | *argp[1] = MAC_(bytes_leaked) + MAC_(bytes_indirect); |
njn | e8b5c05 | 2003-07-22 22:03:58 +0000 | [diff] [blame] | 944 | *argp[2] = MAC_(bytes_dubious); |
| 945 | *argp[3] = MAC_(bytes_reachable); |
| 946 | *argp[4] = MAC_(bytes_suppressed); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 947 | // there is no argp[5] |
| 948 | //*argp[5] = MAC_(bytes_indirect); |
| 949 | // XXX need to make *argp[1-4] readable |
njn | 47363ab | 2003-04-21 13:24:40 +0000 | [diff] [blame] | 950 | *ret = 0; |
| 951 | return True; |
njn | 1078545 | 2003-05-20 16:38:24 +0000 | [diff] [blame] | 952 | } |
njn | d799418 | 2003-10-02 13:44:04 +0000 | [diff] [blame] | 953 | case VG_USERREQ__MALLOCLIKE_BLOCK__OLD_DO_NOT_USE: |
| 954 | case VG_USERREQ__FREELIKE_BLOCK__OLD_DO_NOT_USE: |
njn | 6799325 | 2004-11-22 18:02:32 +0000 | [diff] [blame] | 955 | VG_(tool_panic)(err); |
njn | d799418 | 2003-10-02 13:44:04 +0000 | [diff] [blame] | 956 | |
njn | 1078545 | 2003-05-20 16:38:24 +0000 | [diff] [blame] | 957 | case VG_USERREQ__MALLOCLIKE_BLOCK: { |
njn | d799418 | 2003-10-02 13:44:04 +0000 | [diff] [blame] | 958 | Addr p = (Addr)arg[1]; |
nethercote | 928a5f7 | 2004-11-03 18:10:37 +0000 | [diff] [blame] | 959 | SizeT sizeB = arg[2]; |
njn | d799418 | 2003-10-02 13:44:04 +0000 | [diff] [blame] | 960 | UInt rzB = arg[3]; |
| 961 | Bool is_zeroed = (Bool)arg[4]; |
njn | 47363ab | 2003-04-21 13:24:40 +0000 | [diff] [blame] | 962 | |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 963 | MAC_(new_block) ( tid, p, sizeB, /*ignored*/0, rzB, is_zeroed, |
nethercote | 57e36b3 | 2004-07-10 14:56:28 +0000 | [diff] [blame] | 964 | MAC_AllocCustom, MAC_(malloc_list) ); |
njn | 1078545 | 2003-05-20 16:38:24 +0000 | [diff] [blame] | 965 | return True; |
| 966 | } |
| 967 | case VG_USERREQ__FREELIKE_BLOCK: { |
njn | d799418 | 2003-10-02 13:44:04 +0000 | [diff] [blame] | 968 | Addr p = (Addr)arg[1]; |
| 969 | UInt rzB = arg[2]; |
njn | 1078545 | 2003-05-20 16:38:24 +0000 | [diff] [blame] | 970 | |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 971 | MAC_(handle_free) ( tid, p, rzB, MAC_AllocCustom ); |
njn | 1078545 | 2003-05-20 16:38:24 +0000 | [diff] [blame] | 972 | return True; |
| 973 | } |
njn | d799418 | 2003-10-02 13:44:04 +0000 | [diff] [blame] | 974 | |
njn | 1f8b3e7 | 2005-03-22 04:27:14 +0000 | [diff] [blame] | 975 | case _VG_USERREQ__MEMCHECK_RECORD_OVERLAP_ERROR: { |
| 976 | Char* s = (Char*) arg[1]; |
| 977 | OverlapExtra* extra = (OverlapExtra*)arg[2]; |
| 978 | MAC_(record_overlap_error)(tid, s, extra); |
nethercote | e3a5372 | 2004-05-05 10:46:22 +0000 | [diff] [blame] | 979 | return True; |
njn | 1f8b3e7 | 2005-03-22 04:27:14 +0000 | [diff] [blame] | 980 | } |
nethercote | e3a5372 | 2004-05-05 10:46:22 +0000 | [diff] [blame] | 981 | |
rjwalsh | bc0bb83 | 2004-06-19 18:12:36 +0000 | [diff] [blame] | 982 | case VG_USERREQ__CREATE_MEMPOOL: { |
| 983 | Addr pool = (Addr)arg[1]; |
| 984 | UInt rzB = arg[2]; |
| 985 | Bool is_zeroed = (Bool)arg[3]; |
| 986 | |
| 987 | MAC_(create_mempool) ( pool, rzB, is_zeroed ); |
| 988 | return True; |
| 989 | } |
| 990 | |
| 991 | case VG_USERREQ__DESTROY_MEMPOOL: { |
| 992 | Addr pool = (Addr)arg[1]; |
| 993 | |
| 994 | MAC_(destroy_mempool) ( pool ); |
| 995 | return True; |
| 996 | } |
| 997 | |
| 998 | case VG_USERREQ__MEMPOOL_ALLOC: { |
| 999 | Addr pool = (Addr)arg[1]; |
| 1000 | Addr addr = (Addr)arg[2]; |
| 1001 | UInt size = arg[3]; |
| 1002 | |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 1003 | MAC_(mempool_alloc) ( tid, pool, addr, size ); |
rjwalsh | bc0bb83 | 2004-06-19 18:12:36 +0000 | [diff] [blame] | 1004 | return True; |
| 1005 | } |
| 1006 | |
| 1007 | case VG_USERREQ__MEMPOOL_FREE: { |
| 1008 | Addr pool = (Addr)arg[1]; |
| 1009 | Addr addr = (Addr)arg[2]; |
| 1010 | |
| 1011 | MAC_(mempool_free) ( pool, addr ); |
| 1012 | return True; |
| 1013 | } |
| 1014 | |
njn | 47363ab | 2003-04-21 13:24:40 +0000 | [diff] [blame] | 1015 | default: |
| 1016 | return False; |
| 1017 | } |
| 1018 | } |
| 1019 | |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 1020 | /*--------------------------------------------------------------------*/ |
njn | 00cae24 | 2005-05-09 01:13:44 +0000 | [diff] [blame] | 1021 | /*--- end ---*/ |
njn | 5c004e4 | 2002-11-18 11:04:50 +0000 | [diff] [blame] | 1022 | /*--------------------------------------------------------------------*/ |