sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1 | |
| 2 | /*--------------------------------------------------------------------*/ |
| 3 | /*--- Storage, and equality on, execution contexts (backtraces). ---*/ |
| 4 | /*--- vg_execontext.c ---*/ |
| 5 | /*--------------------------------------------------------------------*/ |
| 6 | |
| 7 | /* |
njn | c953984 | 2002-10-02 13:26:35 +0000 | [diff] [blame] | 8 | This file is part of Valgrind, an extensible x86 protected-mode |
| 9 | emulator for monitoring program execution on x86-Unixes. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 10 | |
nethercote | bb1c991 | 2004-01-04 16:43:23 +0000 | [diff] [blame] | 11 | Copyright (C) 2000-2004 Julian Seward |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 12 | jseward@acm.org |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 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 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 29 | The GNU General Public License is contained in the file COPYING. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 30 | */ |
| 31 | |
nethercote | f1e5e15 | 2004-09-01 23:58:16 +0000 | [diff] [blame] | 32 | #include "core.h" |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 33 | |
| 34 | |
| 35 | /*------------------------------------------------------------*/ |
| 36 | /*--- Low-level ExeContext storage. ---*/ |
| 37 | /*------------------------------------------------------------*/ |
| 38 | |
| 39 | /* The idea is only to ever store any one context once, so as to save |
| 40 | space and make exact comparisons faster. */ |
| 41 | |
| 42 | static ExeContext* vg_ec_list[VG_N_EC_LISTS]; |
| 43 | |
| 44 | /* Stats only: the number of times the system was searched to locate a |
| 45 | context. */ |
| 46 | static UInt vg_ec_searchreqs; |
| 47 | |
| 48 | /* Stats only: the number of full context comparisons done. */ |
| 49 | static UInt vg_ec_searchcmps; |
| 50 | |
| 51 | /* Stats only: total number of stored contexts. */ |
| 52 | static UInt vg_ec_totstored; |
| 53 | |
| 54 | /* Number of 2, 4 and (fast) full cmps done. */ |
| 55 | static UInt vg_ec_cmp2s; |
| 56 | static UInt vg_ec_cmp4s; |
| 57 | static UInt vg_ec_cmpAlls; |
| 58 | |
| 59 | |
| 60 | /*------------------------------------------------------------*/ |
| 61 | /*--- Exported functions. ---*/ |
| 62 | /*------------------------------------------------------------*/ |
| 63 | |
| 64 | |
| 65 | /* Initialise this subsystem. */ |
sewardj | c6b0fe5 | 2003-07-23 23:01:11 +0000 | [diff] [blame] | 66 | static void init_ExeContext_storage ( void ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 67 | { |
| 68 | Int i; |
sewardj | c6b0fe5 | 2003-07-23 23:01:11 +0000 | [diff] [blame] | 69 | static Bool init_done = False; |
| 70 | if (init_done) |
| 71 | return; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 72 | vg_ec_searchreqs = 0; |
| 73 | vg_ec_searchcmps = 0; |
| 74 | vg_ec_totstored = 0; |
| 75 | vg_ec_cmp2s = 0; |
| 76 | vg_ec_cmp4s = 0; |
| 77 | vg_ec_cmpAlls = 0; |
| 78 | for (i = 0; i < VG_N_EC_LISTS; i++) |
| 79 | vg_ec_list[i] = NULL; |
sewardj | c6b0fe5 | 2003-07-23 23:01:11 +0000 | [diff] [blame] | 80 | init_done = True; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | |
nethercote | 3a42fb8 | 2004-08-03 18:08:50 +0000 | [diff] [blame] | 84 | /* Print stats. */ |
| 85 | void VG_(print_ExeContext_stats) ( void ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 86 | { |
sewardj | c6b0fe5 | 2003-07-23 23:01:11 +0000 | [diff] [blame] | 87 | init_ExeContext_storage(); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 88 | VG_(message)(Vg_DebugMsg, |
| 89 | "exectx: %d lists, %d contexts (avg %d per list)", |
| 90 | VG_N_EC_LISTS, vg_ec_totstored, |
| 91 | vg_ec_totstored / VG_N_EC_LISTS |
| 92 | ); |
| 93 | VG_(message)(Vg_DebugMsg, |
| 94 | "exectx: %d searches, %d full compares (%d per 1000)", |
| 95 | vg_ec_searchreqs, vg_ec_searchcmps, |
| 96 | vg_ec_searchreqs == 0 |
| 97 | ? 0 |
| 98 | : (UInt)( (((ULong)vg_ec_searchcmps) * 1000) |
| 99 | / ((ULong)vg_ec_searchreqs )) |
| 100 | ); |
| 101 | VG_(message)(Vg_DebugMsg, |
| 102 | "exectx: %d cmp2, %d cmp4, %d cmpAll", |
| 103 | vg_ec_cmp2s, vg_ec_cmp4s, vg_ec_cmpAlls |
| 104 | ); |
| 105 | } |
| 106 | |
| 107 | |
| 108 | /* Print an ExeContext. */ |
| 109 | void VG_(pp_ExeContext) ( ExeContext* e ) |
| 110 | { |
sewardj | c6b0fe5 | 2003-07-23 23:01:11 +0000 | [diff] [blame] | 111 | init_ExeContext_storage(); |
nethercote | 86c5dcb | 2004-09-05 21:32:37 +0000 | [diff] [blame] | 112 | VG_(mini_stack_dump) ( e->ips, VG_(clo_backtrace_size) ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | |
| 116 | /* Compare two ExeContexts, comparing all callers. */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 117 | Bool VG_(eq_ExeContext) ( VgRes res, ExeContext* e1, ExeContext* e2 ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 118 | { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 119 | if (e1 == NULL || e2 == NULL) |
| 120 | return False; |
| 121 | switch (res) { |
| 122 | case Vg_LowRes: |
| 123 | /* Just compare the top two callers. */ |
| 124 | vg_ec_cmp2s++; |
nethercote | 86c5dcb | 2004-09-05 21:32:37 +0000 | [diff] [blame] | 125 | if (e1->ips[0] != e2->ips[0] |
| 126 | || e1->ips[1] != e2->ips[1]) return False; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 127 | return True; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 128 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 129 | case Vg_MedRes: |
| 130 | /* Just compare the top four callers. */ |
| 131 | vg_ec_cmp4s++; |
nethercote | 86c5dcb | 2004-09-05 21:32:37 +0000 | [diff] [blame] | 132 | if (e1->ips[0] != e2->ips[0]) return False; |
njn | 6c84655 | 2003-09-16 07:41:43 +0000 | [diff] [blame] | 133 | |
| 134 | if (VG_(clo_backtrace_size) < 2) return True; |
nethercote | 86c5dcb | 2004-09-05 21:32:37 +0000 | [diff] [blame] | 135 | if (e1->ips[1] != e2->ips[1]) return False; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 136 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 137 | if (VG_(clo_backtrace_size) < 3) return True; |
nethercote | 86c5dcb | 2004-09-05 21:32:37 +0000 | [diff] [blame] | 138 | if (e1->ips[2] != e2->ips[2]) return False; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 139 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 140 | if (VG_(clo_backtrace_size) < 4) return True; |
nethercote | 86c5dcb | 2004-09-05 21:32:37 +0000 | [diff] [blame] | 141 | if (e1->ips[3] != e2->ips[3]) return False; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 142 | return True; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 143 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 144 | case Vg_HighRes: |
| 145 | vg_ec_cmpAlls++; |
| 146 | /* Compare them all -- just do pointer comparison. */ |
| 147 | if (e1 != e2) return False; |
| 148 | return True; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 149 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 150 | default: |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 151 | VG_(core_panic)("VG_(eq_ExeContext): unrecognised VgRes"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 152 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | |
nethercote | 86c5dcb | 2004-09-05 21:32:37 +0000 | [diff] [blame] | 156 | /* Take a snapshot of the client's stack, putting the up to 'n_ips' IPs |
| 157 | into 'ips'. In order to be thread-safe, we pass in the thread's IP |
| 158 | and FP. Returns number of IPs put in 'ips'. */ |
| 159 | static UInt stack_snapshot2 ( Addr* ips, UInt n_ips, Addr ip, Addr fp, |
| 160 | Addr fp_min, Addr fp_max_orig ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 161 | { |
| 162 | Int i; |
nethercote | 86c5dcb | 2004-09-05 21:32:37 +0000 | [diff] [blame] | 163 | Addr fp_max; |
njn | 6c84655 | 2003-09-16 07:41:43 +0000 | [diff] [blame] | 164 | UInt n_found = 0; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 165 | |
| 166 | VGP_PUSHCC(VgpExeContext); |
| 167 | |
nethercote | 86c5dcb | 2004-09-05 21:32:37 +0000 | [diff] [blame] | 168 | /* First snaffle IPs from the client's stack into ips[0 .. n_ips-1], |
njn | 6c84655 | 2003-09-16 07:41:43 +0000 | [diff] [blame] | 169 | putting zeroes in when the trail goes cold, which we guess to be when |
nethercote | 86c5dcb | 2004-09-05 21:32:37 +0000 | [diff] [blame] | 170 | FP is not a reasonable stack location. We also assert that FP |
njn | 6c84655 | 2003-09-16 07:41:43 +0000 | [diff] [blame] | 171 | increases down the chain. */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 172 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 173 | // Gives shorter stack trace for tests/badjump.c |
| 174 | // JRS 2002-aug-16: I don't think this is a big deal; looks ok for |
| 175 | // most "normal" backtraces. |
| 176 | // NJN 2002-sep-05: traces for pthreaded programs are particularly bad. |
| 177 | |
nethercote | 86c5dcb | 2004-09-05 21:32:37 +0000 | [diff] [blame] | 178 | // JRS 2002-sep-17: hack, to round up fp_max to the end of the |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 179 | // current page, at least. Dunno if it helps. |
| 180 | // NJN 2002-sep-17: seems to -- stack traces look like 1.0.X again |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 181 | fp_max = (fp_max_orig + VKI_PAGE_SIZE - 1) & ~(VKI_PAGE_SIZE - 1); |
nethercote | 86c5dcb | 2004-09-05 21:32:37 +0000 | [diff] [blame] | 182 | fp_max -= sizeof(Addr); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 183 | |
| 184 | /* Assertion broken before main() is reached in pthreaded programs; the |
| 185 | * offending stack traces only have one item. --njn, 2002-aug-16 */ |
nethercote | 86c5dcb | 2004-09-05 21:32:37 +0000 | [diff] [blame] | 186 | /* vg_assert(fp_min <= fp_max);*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 187 | |
nethercote | 86c5dcb | 2004-09-05 21:32:37 +0000 | [diff] [blame] | 188 | if (fp_min + 4000000 <= fp_max) { |
sewardj | c32b9d6 | 2002-10-01 09:02:47 +0000 | [diff] [blame] | 189 | /* If the stack is ridiculously big, don't poke around ... but |
| 190 | don't bomb out either. Needed to make John Regehr's |
| 191 | user-space threads package work. JRS 20021001 */ |
nethercote | 86c5dcb | 2004-09-05 21:32:37 +0000 | [diff] [blame] | 192 | ips[0] = ip; |
sewardj | c32b9d6 | 2002-10-01 09:02:47 +0000 | [diff] [blame] | 193 | i = 1; |
| 194 | } else { |
| 195 | /* Get whatever we safely can ... */ |
nethercote | 86c5dcb | 2004-09-05 21:32:37 +0000 | [diff] [blame] | 196 | ips[0] = ip; |
| 197 | fp = FIRST_STACK_FRAME(fp); |
| 198 | for (i = 1; i < n_ips; i++) { |
| 199 | if (!(fp_min <= fp && fp <= fp_max)) { |
| 200 | //VG_(printf)("... out of range %p\n", fp); |
| 201 | break; /* fp gone baaaad */ |
sewardj | c32b9d6 | 2002-10-01 09:02:47 +0000 | [diff] [blame] | 202 | } |
| 203 | // NJN 2002-sep-17: monotonicity doesn't work -- gives wrong traces... |
nethercote | 86c5dcb | 2004-09-05 21:32:37 +0000 | [diff] [blame] | 204 | // if (fp >= ((UInt*)fp)[0]) { |
sewardj | c32b9d6 | 2002-10-01 09:02:47 +0000 | [diff] [blame] | 205 | // VG_(printf)("nonmonotonic\n"); |
nethercote | 86c5dcb | 2004-09-05 21:32:37 +0000 | [diff] [blame] | 206 | // break; /* fp gone nonmonotonic */ |
sewardj | c32b9d6 | 2002-10-01 09:02:47 +0000 | [diff] [blame] | 207 | // } |
nethercote | 86c5dcb | 2004-09-05 21:32:37 +0000 | [diff] [blame] | 208 | ips[i] = STACK_FRAME_RET(fp); /* ret addr */ |
| 209 | fp = STACK_FRAME_NEXT(fp); /* old fp */ |
| 210 | //VG_(printf)(" %p\n", ips[i]); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 211 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 212 | } |
njn | 6c84655 | 2003-09-16 07:41:43 +0000 | [diff] [blame] | 213 | n_found = i; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 214 | |
| 215 | /* Put zeroes in the rest. */ |
nethercote | 86c5dcb | 2004-09-05 21:32:37 +0000 | [diff] [blame] | 216 | for (; i < n_ips; i++) { |
| 217 | ips[i] = 0; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 218 | } |
njn | 6c84655 | 2003-09-16 07:41:43 +0000 | [diff] [blame] | 219 | VGP_POPCC(VgpExeContext); |
| 220 | |
| 221 | return n_found; |
| 222 | } |
| 223 | |
| 224 | /* This guy is the head honcho here. Take a snapshot of the client's |
| 225 | stack. Search our collection of ExeContexts to see if we already |
| 226 | have it, and if not, allocate a new one. Either way, return a |
| 227 | pointer to the context. If there is a matching context we |
| 228 | guarantee to not allocate a new one. Thus we never store |
| 229 | duplicates, and so exact equality can be quickly done as equality |
| 230 | on the returned ExeContext* values themselves. Inspired by Hugs's |
| 231 | Text type. |
| 232 | */ |
nethercote | 86c5dcb | 2004-09-05 21:32:37 +0000 | [diff] [blame] | 233 | ExeContext* VG_(get_ExeContext2) ( Addr ip, Addr fp, |
| 234 | Addr fp_min, Addr fp_max_orig ) |
njn | 6c84655 | 2003-09-16 07:41:43 +0000 | [diff] [blame] | 235 | { |
| 236 | Int i; |
nethercote | 86c5dcb | 2004-09-05 21:32:37 +0000 | [diff] [blame] | 237 | Addr ips[VG_DEEPEST_BACKTRACE]; |
njn | 6c84655 | 2003-09-16 07:41:43 +0000 | [diff] [blame] | 238 | Bool same; |
nethercote | 50397c2 | 2004-11-04 18:03:06 +0000 | [diff] [blame] | 239 | UWord hash; |
njn | 6c84655 | 2003-09-16 07:41:43 +0000 | [diff] [blame] | 240 | ExeContext* new_ec; |
| 241 | ExeContext* list; |
| 242 | |
| 243 | VGP_PUSHCC(VgpExeContext); |
| 244 | |
| 245 | init_ExeContext_storage(); |
| 246 | vg_assert(VG_(clo_backtrace_size) >= 1 |
| 247 | && VG_(clo_backtrace_size) <= VG_DEEPEST_BACKTRACE); |
| 248 | |
nethercote | 86c5dcb | 2004-09-05 21:32:37 +0000 | [diff] [blame] | 249 | stack_snapshot2( ips, VG_(clo_backtrace_size), |
| 250 | ip, fp, fp_min, fp_max_orig ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 251 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 252 | /* Now figure out if we've seen this one before. First hash it so |
| 253 | as to determine the list number. */ |
| 254 | |
| 255 | hash = 0; |
| 256 | for (i = 0; i < VG_(clo_backtrace_size); i++) { |
nethercote | 50397c2 | 2004-11-04 18:03:06 +0000 | [diff] [blame] | 257 | hash ^= ips[i]; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 258 | hash = (hash << 29) | (hash >> 3); |
| 259 | } |
| 260 | hash = hash % VG_N_EC_LISTS; |
| 261 | |
| 262 | /* And (the expensive bit) look a matching entry in the list. */ |
| 263 | |
| 264 | vg_ec_searchreqs++; |
| 265 | |
| 266 | list = vg_ec_list[hash]; |
| 267 | |
| 268 | while (True) { |
| 269 | if (list == NULL) break; |
| 270 | vg_ec_searchcmps++; |
| 271 | same = True; |
| 272 | for (i = 0; i < VG_(clo_backtrace_size); i++) { |
nethercote | 86c5dcb | 2004-09-05 21:32:37 +0000 | [diff] [blame] | 273 | if (list->ips[i] != ips[i]) { |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 274 | same = False; |
| 275 | break; |
| 276 | } |
| 277 | } |
| 278 | if (same) break; |
| 279 | list = list->next; |
| 280 | } |
| 281 | |
| 282 | if (list != NULL) { |
| 283 | /* Yay! We found it. */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 284 | VGP_POPCC(VgpExeContext); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 285 | return list; |
| 286 | } |
| 287 | |
| 288 | /* Bummer. We have to allocate a new context record. */ |
| 289 | vg_ec_totstored++; |
| 290 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 291 | new_ec = VG_(arena_malloc)( VG_AR_EXECTXT, |
| 292 | sizeof(struct _ExeContext *) |
| 293 | + VG_(clo_backtrace_size) * sizeof(Addr) ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 294 | |
| 295 | for (i = 0; i < VG_(clo_backtrace_size); i++) |
nethercote | 86c5dcb | 2004-09-05 21:32:37 +0000 | [diff] [blame] | 296 | new_ec->ips[i] = ips[i]; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 297 | |
| 298 | new_ec->next = vg_ec_list[hash]; |
| 299 | vg_ec_list[hash] = new_ec; |
| 300 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 301 | VGP_POPCC(VgpExeContext); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 302 | return new_ec; |
| 303 | } |
| 304 | |
nethercote | 86c5dcb | 2004-09-05 21:32:37 +0000 | [diff] [blame] | 305 | void get_needed_regs(ThreadId tid, Addr* ip, Addr* fp, Addr* sp, |
njn | 6c84655 | 2003-09-16 07:41:43 +0000 | [diff] [blame] | 306 | Addr* stack_highest_word) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 307 | { |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 308 | if (VG_(is_running_thread)(tid)) { |
sewardj | 499e3de | 2002-11-13 22:22:25 +0000 | [diff] [blame] | 309 | /* thread currently in baseblock */ |
nethercote | 86c5dcb | 2004-09-05 21:32:37 +0000 | [diff] [blame] | 310 | *ip = VG_(baseBlock)[VGOFF_INSTR_PTR]; |
| 311 | *fp = VG_(baseBlock)[VGOFF_FRAME_PTR]; |
| 312 | *sp = VG_(baseBlock)[VGOFF_STACK_PTR]; |
njn | 6c84655 | 2003-09-16 07:41:43 +0000 | [diff] [blame] | 313 | *stack_highest_word = VG_(threads)[tid].stack_highest_word; |
sewardj | 499e3de | 2002-11-13 22:22:25 +0000 | [diff] [blame] | 314 | } else { |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 315 | /* thread in thread table */ |
| 316 | ThreadState* tst = & VG_(threads)[ tid ]; |
nethercote | 86c5dcb | 2004-09-05 21:32:37 +0000 | [diff] [blame] | 317 | *ip = ARCH_INSTR_PTR(tst->arch); |
| 318 | *fp = ARCH_FRAME_PTR(tst->arch); |
| 319 | *sp = ARCH_STACK_PTR(tst->arch); |
njn | 6c84655 | 2003-09-16 07:41:43 +0000 | [diff] [blame] | 320 | *stack_highest_word = tst->stack_highest_word; |
sewardj | 499e3de | 2002-11-13 22:22:25 +0000 | [diff] [blame] | 321 | } |
fitzhardinge | 47735af | 2004-01-21 01:27:27 +0000 | [diff] [blame] | 322 | |
| 323 | /* Nasty little hack to deal with sysinfo syscalls - if libc is |
| 324 | using the sysinfo page for syscalls (the TLS version does), then |
nethercote | 86c5dcb | 2004-09-05 21:32:37 +0000 | [diff] [blame] | 325 | ip will always appear to be in that page when doing a syscall, |
fitzhardinge | 47735af | 2004-01-21 01:27:27 +0000 | [diff] [blame] | 326 | not the actual libc function doing the syscall. This check sees |
nethercote | 86c5dcb | 2004-09-05 21:32:37 +0000 | [diff] [blame] | 327 | if IP is within the syscall code, and pops the return address |
| 328 | off the stack so that ip is placed within the library function |
fitzhardinge | 47735af | 2004-01-21 01:27:27 +0000 | [diff] [blame] | 329 | calling the syscall. This makes stack backtraces much more |
| 330 | useful. */ |
nethercote | 86c5dcb | 2004-09-05 21:32:37 +0000 | [diff] [blame] | 331 | if (*ip >= VG_(client_trampoline_code)+VG_(tramp_syscall_offset) && |
| 332 | *ip < VG_(client_trampoline_code)+VG_(trampoline_code_length) && |
| 333 | VG_(is_addressable)(*sp, sizeof(Addr))) { |
| 334 | *ip = *(Addr *)*sp; |
| 335 | *sp += sizeof(Addr); |
fitzhardinge | 47735af | 2004-01-21 01:27:27 +0000 | [diff] [blame] | 336 | } |
njn | 6c84655 | 2003-09-16 07:41:43 +0000 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | ExeContext* VG_(get_ExeContext) ( ThreadId tid ) |
| 340 | { |
nethercote | 86c5dcb | 2004-09-05 21:32:37 +0000 | [diff] [blame] | 341 | Addr ip, fp, sp, stack_highest_word; |
njn | 6c84655 | 2003-09-16 07:41:43 +0000 | [diff] [blame] | 342 | |
nethercote | 86c5dcb | 2004-09-05 21:32:37 +0000 | [diff] [blame] | 343 | get_needed_regs(tid, &ip, &fp, &sp, &stack_highest_word); |
| 344 | return VG_(get_ExeContext2)(ip, fp, sp, stack_highest_word); |
njn | 6c84655 | 2003-09-16 07:41:43 +0000 | [diff] [blame] | 345 | } |
| 346 | |
nethercote | 86c5dcb | 2004-09-05 21:32:37 +0000 | [diff] [blame] | 347 | /* Take a snapshot of the client's stack, putting the up to 'n_ips' |
| 348 | instruction pointers into 'ips'. In order to be thread-safe, we pass in |
| 349 | the thread's IP and FP. Returns number of IPs put in 'ips'. */ |
| 350 | UInt VG_(stack_snapshot) ( ThreadId tid, Addr* ips, UInt n_ips ) |
njn | 6c84655 | 2003-09-16 07:41:43 +0000 | [diff] [blame] | 351 | { |
nethercote | 86c5dcb | 2004-09-05 21:32:37 +0000 | [diff] [blame] | 352 | Addr ip, fp, sp, stack_highest_word; |
njn | 6c84655 | 2003-09-16 07:41:43 +0000 | [diff] [blame] | 353 | |
nethercote | 86c5dcb | 2004-09-05 21:32:37 +0000 | [diff] [blame] | 354 | get_needed_regs(tid, &ip, &fp, &sp, &stack_highest_word); |
| 355 | return stack_snapshot2(ips, n_ips, ip, fp, sp, stack_highest_word); |
njn | 6c84655 | 2003-09-16 07:41:43 +0000 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | |
| 359 | Addr VG_(get_EIP_from_ExeContext) ( ExeContext* e, UInt n ) |
| 360 | { |
| 361 | if (n > VG_(clo_backtrace_size)) return 0; |
nethercote | 86c5dcb | 2004-09-05 21:32:37 +0000 | [diff] [blame] | 362 | return e->ips[n]; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 363 | } |
| 364 | |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 365 | Addr VG_(get_EIP) ( ThreadId tid ) |
sewardj | 499e3de | 2002-11-13 22:22:25 +0000 | [diff] [blame] | 366 | { |
| 367 | Addr ret; |
| 368 | |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 369 | if (VG_(is_running_thread)(tid)) |
nethercote | 86c5dcb | 2004-09-05 21:32:37 +0000 | [diff] [blame] | 370 | ret = VG_(baseBlock)[VGOFF_INSTR_PTR]; |
sewardj | 499e3de | 2002-11-13 22:22:25 +0000 | [diff] [blame] | 371 | else |
nethercote | 86c5dcb | 2004-09-05 21:32:37 +0000 | [diff] [blame] | 372 | ret = ARCH_INSTR_PTR(VG_(threads)[ tid ].arch); |
sewardj | 499e3de | 2002-11-13 22:22:25 +0000 | [diff] [blame] | 373 | |
| 374 | return ret; |
| 375 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 376 | |
| 377 | /*--------------------------------------------------------------------*/ |
nethercote | 86c5dcb | 2004-09-05 21:32:37 +0000 | [diff] [blame] | 378 | /*--- end ---*/ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 379 | /*--------------------------------------------------------------------*/ |