weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 1 | |
| 2 | /*--------------------------------------------------------------------*/ |
| 3 | /*--- Callgrind ---*/ |
| 4 | /*--- main.c ---*/ |
| 5 | /*--------------------------------------------------------------------*/ |
| 6 | |
| 7 | /* |
| 8 | This file is part of Callgrind, a Valgrind tool for call graph |
| 9 | profiling programs. |
| 10 | |
njn | 9f20746 | 2009-03-10 22:02:09 +0000 | [diff] [blame] | 11 | Copyright (C) 2002-2009, Josef Weidendorfer (Josef.Weidendorfer@gmx.de) |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 12 | |
njn | 9a0cba4 | 2007-04-15 22:15:57 +0000 | [diff] [blame] | 13 | This tool is derived from and contains code from Cachegrind |
njn | 9f20746 | 2009-03-10 22:02:09 +0000 | [diff] [blame] | 14 | Copyright (C) 2002-2009 Nicholas Nethercote (njn@valgrind.org) |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 15 | |
| 16 | This program is free software; you can redistribute it and/or |
| 17 | modify it under the terms of the GNU General Public License as |
| 18 | published by the Free Software Foundation; either version 2 of the |
| 19 | License, or (at your option) any later version. |
| 20 | |
| 21 | This program is distributed in the hope that it will be useful, but |
| 22 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 24 | General Public License for more details. |
| 25 | |
| 26 | You should have received a copy of the GNU General Public License |
| 27 | along with this program; if not, write to the Free Software |
| 28 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 29 | 02111-1307, USA. |
| 30 | |
| 31 | The GNU General Public License is contained in the file COPYING. |
| 32 | */ |
| 33 | |
| 34 | #include "config.h" |
| 35 | #include "callgrind.h" |
| 36 | #include "global.h" |
| 37 | |
| 38 | #include <pub_tool_threadstate.h> |
| 39 | |
| 40 | /*------------------------------------------------------------*/ |
| 41 | /*--- Global variables ---*/ |
| 42 | /*------------------------------------------------------------*/ |
| 43 | |
| 44 | /* for all threads */ |
| 45 | CommandLineOptions CLG_(clo); |
| 46 | Statistics CLG_(stat); |
| 47 | Bool CLG_(instrument_state) = True; /* Instrumentation on ? */ |
| 48 | |
| 49 | /* thread and signal handler specific */ |
| 50 | exec_state CLG_(current_state); |
| 51 | |
| 52 | |
| 53 | /*------------------------------------------------------------*/ |
| 54 | /*--- Statistics ---*/ |
| 55 | /*------------------------------------------------------------*/ |
| 56 | |
| 57 | static void CLG_(init_statistics)(Statistics* s) |
| 58 | { |
| 59 | s->call_counter = 0; |
| 60 | s->jcnd_counter = 0; |
| 61 | s->jump_counter = 0; |
| 62 | s->rec_call_counter = 0; |
| 63 | s->ret_counter = 0; |
| 64 | s->bb_executions = 0; |
| 65 | |
| 66 | s->context_counter = 0; |
| 67 | s->bb_retranslations = 0; |
| 68 | |
| 69 | s->distinct_objs = 0; |
| 70 | s->distinct_files = 0; |
| 71 | s->distinct_fns = 0; |
| 72 | s->distinct_contexts = 0; |
| 73 | s->distinct_bbs = 0; |
| 74 | s->distinct_bbccs = 0; |
| 75 | s->distinct_instrs = 0; |
| 76 | s->distinct_skips = 0; |
| 77 | |
| 78 | s->bb_hash_resizes = 0; |
| 79 | s->bbcc_hash_resizes = 0; |
| 80 | s->jcc_hash_resizes = 0; |
| 81 | s->cxt_hash_resizes = 0; |
| 82 | s->fn_array_resizes = 0; |
| 83 | s->call_stack_resizes = 0; |
| 84 | s->fn_stack_resizes = 0; |
| 85 | |
| 86 | s->full_debug_BBs = 0; |
| 87 | s->file_line_debug_BBs = 0; |
| 88 | s->fn_name_debug_BBs = 0; |
| 89 | s->no_debug_BBs = 0; |
| 90 | s->bbcc_lru_misses = 0; |
| 91 | s->jcc_lru_misses = 0; |
| 92 | s->cxt_lru_misses = 0; |
| 93 | s->bbcc_clones = 0; |
| 94 | } |
| 95 | |
| 96 | |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 97 | /*------------------------------------------------------------*/ |
weidendo | 0a1951d | 2009-06-15 00:16:36 +0000 | [diff] [blame] | 98 | /*--- Instrumentation structures and event queue handling ---*/ |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 99 | /*------------------------------------------------------------*/ |
| 100 | |
weidendo | 0a1951d | 2009-06-15 00:16:36 +0000 | [diff] [blame] | 101 | /* Maintain an ordered list of memory events which are outstanding, in |
| 102 | the sense that no IR has yet been generated to do the relevant |
| 103 | helper calls. The BB is scanned top to bottom and memory events |
| 104 | are added to the end of the list, merging with the most recent |
| 105 | notified event where possible (Dw immediately following Dr and |
| 106 | having the same size and EA can be merged). |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 107 | |
weidendo | 0a1951d | 2009-06-15 00:16:36 +0000 | [diff] [blame] | 108 | This merging is done so that for architectures which have |
| 109 | load-op-store instructions (x86, amd64), the insn is treated as if |
| 110 | it makes just one memory reference (a modify), rather than two (a |
| 111 | read followed by a write at the same address). |
| 112 | |
| 113 | At various points the list will need to be flushed, that is, IR |
| 114 | generated from it. That must happen before any possible exit from |
| 115 | the block (the end, or an IRStmt_Exit). Flushing also takes place |
| 116 | when there is no space to add a new event. |
| 117 | |
| 118 | If we require the simulation statistics to be up to date with |
| 119 | respect to possible memory exceptions, then the list would have to |
| 120 | be flushed before each memory reference. That would however lose |
| 121 | performance by inhibiting event-merging during flushing. |
| 122 | |
| 123 | Flushing the list consists of walking it start to end and emitting |
| 124 | instrumentation IR for each event, in the order in which they |
| 125 | appear. It may be possible to emit a single call for two adjacent |
| 126 | events in order to reduce the number of helper function calls made. |
| 127 | For example, it could well be profitable to handle two adjacent Ir |
| 128 | events with a single helper call. */ |
| 129 | |
| 130 | typedef |
| 131 | IRExpr |
| 132 | IRAtom; |
| 133 | |
| 134 | typedef |
| 135 | enum { |
| 136 | Ev_Ir, // Instruction read |
| 137 | Ev_Dr, // Data read |
| 138 | Ev_Dw, // Data write |
| 139 | Ev_Dm, // Data modify (read then write) |
| 140 | } |
| 141 | EventTag; |
| 142 | |
| 143 | typedef |
| 144 | struct { |
| 145 | EventTag tag; |
| 146 | InstrInfo* inode; |
| 147 | union { |
| 148 | struct { |
| 149 | } Ir; |
| 150 | struct { |
| 151 | IRAtom* ea; |
| 152 | Int szB; |
| 153 | } Dr; |
| 154 | struct { |
| 155 | IRAtom* ea; |
| 156 | Int szB; |
| 157 | } Dw; |
| 158 | struct { |
| 159 | IRAtom* ea; |
| 160 | Int szB; |
| 161 | } Dm; |
| 162 | } Ev; |
| 163 | } |
| 164 | Event; |
| 165 | |
| 166 | static void init_Event ( Event* ev ) { |
| 167 | VG_(memset)(ev, 0, sizeof(Event)); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 168 | } |
| 169 | |
weidendo | 0a1951d | 2009-06-15 00:16:36 +0000 | [diff] [blame] | 170 | static IRAtom* get_Event_dea ( Event* ev ) { |
| 171 | switch (ev->tag) { |
| 172 | case Ev_Dr: return ev->Ev.Dr.ea; |
| 173 | case Ev_Dw: return ev->Ev.Dw.ea; |
| 174 | case Ev_Dm: return ev->Ev.Dm.ea; |
| 175 | default: tl_assert(0); |
| 176 | } |
| 177 | } |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 178 | |
weidendo | 0a1951d | 2009-06-15 00:16:36 +0000 | [diff] [blame] | 179 | static Int get_Event_dszB ( Event* ev ) { |
| 180 | switch (ev->tag) { |
| 181 | case Ev_Dr: return ev->Ev.Dr.szB; |
| 182 | case Ev_Dw: return ev->Ev.Dw.szB; |
| 183 | case Ev_Dm: return ev->Ev.Dm.szB; |
| 184 | default: tl_assert(0); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | |
| 189 | /* Up to this many unnotified events are allowed. Number is |
| 190 | arbitrary. Larger numbers allow more event merging to occur, but |
| 191 | potentially induce more spilling due to extending live ranges of |
| 192 | address temporaries. */ |
| 193 | #define N_EVENTS 16 |
| 194 | |
| 195 | |
| 196 | /* A struct which holds all the running state during instrumentation. |
| 197 | Mostly to avoid passing loads of parameters everywhere. */ |
| 198 | typedef struct { |
| 199 | /* The current outstanding-memory-event list. */ |
| 200 | Event events[N_EVENTS]; |
| 201 | Int events_used; |
| 202 | |
| 203 | /* The array of InstrInfo's is part of BB struct. */ |
| 204 | BB* bb; |
| 205 | |
| 206 | /* BB seen before (ie. re-instrumentation) */ |
| 207 | Bool seen_before; |
| 208 | |
| 209 | /* Number InstrInfo bins 'used' so far. */ |
| 210 | UInt ii_index; |
| 211 | |
| 212 | // current offset of guest instructions from BB start |
| 213 | UInt instr_offset; |
| 214 | |
| 215 | /* The output SB being constructed. */ |
| 216 | IRSB* sbOut; |
| 217 | } ClgState; |
| 218 | |
| 219 | |
| 220 | static void showEvent ( Event* ev ) |
| 221 | { |
| 222 | switch (ev->tag) { |
| 223 | case Ev_Ir: |
| 224 | VG_(printf)("Ir (InstrInfo %p) at +%d\n", |
| 225 | ev->inode, ev->inode->instr_offset); |
| 226 | break; |
| 227 | case Ev_Dr: |
| 228 | VG_(printf)("Dr (InstrInfo %p) at +%d %d EA=", |
| 229 | ev->inode, ev->inode->instr_offset, ev->Ev.Dr.szB); |
| 230 | ppIRExpr(ev->Ev.Dr.ea); |
| 231 | VG_(printf)("\n"); |
| 232 | break; |
| 233 | case Ev_Dw: |
| 234 | VG_(printf)("Dw (InstrInfo %p) at +%d %d EA=", |
| 235 | ev->inode, ev->inode->instr_offset, ev->Ev.Dw.szB); |
| 236 | ppIRExpr(ev->Ev.Dw.ea); |
| 237 | VG_(printf)("\n"); |
| 238 | break; |
| 239 | case Ev_Dm: |
| 240 | VG_(printf)("Dm (InstrInfo %p) at +%d %d EA=", |
| 241 | ev->inode, ev->inode->instr_offset, ev->Ev.Dm.szB); |
| 242 | ppIRExpr(ev->Ev.Dm.ea); |
| 243 | VG_(printf)("\n"); |
| 244 | break; |
| 245 | default: |
| 246 | tl_assert(0); |
| 247 | break; |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | /* Generate code for all outstanding memory events, and mark the queue |
| 252 | empty. Code is generated into cgs->sbOut, and this activity |
| 253 | 'consumes' slots in cgs->bb. */ |
| 254 | |
| 255 | static void flushEvents ( ClgState* clgs ) |
| 256 | { |
| 257 | Int i, regparms, inew; |
| 258 | Char* helperName; |
| 259 | void* helperAddr; |
| 260 | IRExpr** argv; |
| 261 | IRExpr* i_node_expr; |
| 262 | IRDirty* di; |
| 263 | Event* ev; |
| 264 | Event* ev2; |
| 265 | Event* ev3; |
| 266 | |
| 267 | if (!clgs->seen_before) { |
| 268 | // extend event sets as needed |
| 269 | // available sets: D0 Dr |
| 270 | for(i=0; i<clgs->events_used; i++) { |
| 271 | ev = &clgs->events[i]; |
| 272 | switch(ev->tag) { |
| 273 | case Ev_Ir: |
| 274 | // Ir event always is first for a guest instruction |
| 275 | CLG_ASSERT(ev->inode->eventset == 0); |
| 276 | ev->inode->eventset = CLG_(sets).UIr; |
| 277 | break; |
| 278 | case Ev_Dr: |
| 279 | // extend event set by Dr counter |
| 280 | if ((ev->inode->eventset == CLG_(sets).UIrDr) || |
| 281 | (ev->inode->eventset == CLG_(sets).UIrDrDw) || |
| 282 | (ev->inode->eventset == CLG_(sets).UIrDwDr)) |
| 283 | break; |
| 284 | if (ev->inode->eventset == CLG_(sets).UIrDw) { |
| 285 | ev->inode->eventset = CLG_(sets).UIrDwDr; |
| 286 | break; |
| 287 | } |
| 288 | CLG_ASSERT(ev->inode->eventset == CLG_(sets).UIr); |
| 289 | ev->inode->eventset = CLG_(sets).UIrDr; |
| 290 | break; |
| 291 | case Ev_Dw: |
| 292 | case Ev_Dm: |
| 293 | // extend event set by Dw counter |
| 294 | if ((ev->inode->eventset == CLG_(sets).UIrDw) || |
| 295 | (ev->inode->eventset == CLG_(sets).UIrDwDr) || |
| 296 | (ev->inode->eventset == CLG_(sets).UIrDrDw)) |
| 297 | break; |
| 298 | if (ev->inode->eventset == CLG_(sets).UIrDr) { |
| 299 | ev->inode->eventset = CLG_(sets).UIrDrDw; |
| 300 | break; |
| 301 | } |
| 302 | CLG_ASSERT(ev->inode->eventset == CLG_(sets).UIr); |
| 303 | ev->inode->eventset = CLG_(sets).UIrDw; |
| 304 | break; |
| 305 | default: |
| 306 | tl_assert(0); |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | for(i = 0; i < clgs->events_used; i = inew) { |
| 312 | |
| 313 | helperName = NULL; |
| 314 | helperAddr = NULL; |
| 315 | argv = NULL; |
| 316 | regparms = 0; |
| 317 | |
| 318 | /* generate IR to notify event i and possibly the ones |
| 319 | immediately following it. */ |
| 320 | tl_assert(i >= 0 && i < clgs->events_used); |
| 321 | |
| 322 | ev = &clgs->events[i]; |
| 323 | ev2 = ( i < clgs->events_used-1 ? &clgs->events[i+1] : NULL ); |
| 324 | ev3 = ( i < clgs->events_used-2 ? &clgs->events[i+2] : NULL ); |
| 325 | |
| 326 | CLG_DEBUGIF(5) { |
| 327 | VG_(printf)(" flush "); |
| 328 | showEvent( ev ); |
| 329 | } |
| 330 | |
| 331 | i_node_expr = mkIRExpr_HWord( (HWord)ev->inode ); |
| 332 | |
| 333 | /* Decide on helper fn to call and args to pass it, and advance |
| 334 | i appropriately. |
| 335 | Dm events have same effect as Dw events */ |
| 336 | switch (ev->tag) { |
| 337 | case Ev_Ir: |
| 338 | /* Merge an Ir with a following Dr. */ |
| 339 | if (ev2 && ev2->tag == Ev_Dr) { |
| 340 | /* Why is this true? It's because we're merging an Ir |
| 341 | with a following Dr. The Ir derives from the |
| 342 | instruction's IMark and the Dr from data |
| 343 | references which follow it. In short it holds |
| 344 | because each insn starts with an IMark, hence an |
| 345 | Ev_Ir, and so these Dr must pertain to the |
| 346 | immediately preceding Ir. Same applies to analogous |
| 347 | assertions in the subsequent cases. */ |
| 348 | tl_assert(ev2->inode == ev->inode); |
| 349 | helperName = CLG_(cachesim).log_1I1Dr_name; |
| 350 | helperAddr = CLG_(cachesim).log_1I1Dr; |
| 351 | argv = mkIRExprVec_3( i_node_expr, |
| 352 | get_Event_dea(ev2), |
| 353 | mkIRExpr_HWord( get_Event_dszB(ev2) ) ); |
| 354 | regparms = 3; |
| 355 | inew = i+2; |
| 356 | } |
| 357 | /* Merge an Ir with a following Dw/Dm. */ |
| 358 | else |
| 359 | if (ev2 && (ev2->tag == Ev_Dw || ev2->tag == Ev_Dm)) { |
| 360 | tl_assert(ev2->inode == ev->inode); |
| 361 | helperName = CLG_(cachesim).log_1I1Dw_name; |
| 362 | helperAddr = CLG_(cachesim).log_1I1Dw; |
| 363 | argv = mkIRExprVec_3( i_node_expr, |
| 364 | get_Event_dea(ev2), |
| 365 | mkIRExpr_HWord( get_Event_dszB(ev2) ) ); |
| 366 | regparms = 3; |
| 367 | inew = i+2; |
| 368 | } |
| 369 | /* Merge an Ir with two following Irs. */ |
| 370 | else |
| 371 | if (ev2 && ev3 && ev2->tag == Ev_Ir && ev3->tag == Ev_Ir) { |
| 372 | helperName = CLG_(cachesim).log_3I0D_name; |
| 373 | helperAddr = CLG_(cachesim).log_3I0D; |
| 374 | argv = mkIRExprVec_3( i_node_expr, |
| 375 | mkIRExpr_HWord( (HWord)ev2->inode ), |
| 376 | mkIRExpr_HWord( (HWord)ev3->inode ) ); |
| 377 | regparms = 3; |
| 378 | inew = i+3; |
| 379 | } |
| 380 | /* Merge an Ir with one following Ir. */ |
| 381 | else |
| 382 | if (ev2 && ev2->tag == Ev_Ir) { |
| 383 | helperName = CLG_(cachesim).log_2I0D_name; |
| 384 | helperAddr = CLG_(cachesim).log_2I0D; |
| 385 | argv = mkIRExprVec_2( i_node_expr, |
| 386 | mkIRExpr_HWord( (HWord)ev2->inode ) ); |
| 387 | regparms = 2; |
| 388 | inew = i+2; |
| 389 | } |
| 390 | /* No merging possible; emit as-is. */ |
| 391 | else { |
| 392 | helperName = CLG_(cachesim).log_1I0D_name; |
| 393 | helperAddr = CLG_(cachesim).log_1I0D; |
| 394 | argv = mkIRExprVec_1( i_node_expr ); |
| 395 | regparms = 1; |
| 396 | inew = i+1; |
| 397 | } |
| 398 | break; |
| 399 | case Ev_Dr: |
| 400 | /* Data read or modify */ |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 401 | helperName = CLG_(cachesim).log_0I1Dr_name; |
| 402 | helperAddr = CLG_(cachesim).log_0I1Dr; |
weidendo | 0a1951d | 2009-06-15 00:16:36 +0000 | [diff] [blame] | 403 | argv = mkIRExprVec_3( i_node_expr, |
| 404 | get_Event_dea(ev), |
| 405 | mkIRExpr_HWord( get_Event_dszB(ev) ) ); |
| 406 | regparms = 3; |
| 407 | inew = i+1; |
| 408 | break; |
| 409 | case Ev_Dw: |
| 410 | case Ev_Dm: |
| 411 | /* Data write */ |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 412 | helperName = CLG_(cachesim).log_0I1Dw_name; |
| 413 | helperAddr = CLG_(cachesim).log_0I1Dw; |
weidendo | 0a1951d | 2009-06-15 00:16:36 +0000 | [diff] [blame] | 414 | argv = mkIRExprVec_3( i_node_expr, |
| 415 | get_Event_dea(ev), |
| 416 | mkIRExpr_HWord( get_Event_dszB(ev) ) ); |
| 417 | regparms = 3; |
| 418 | inew = i+1; |
| 419 | break; |
| 420 | default: |
| 421 | tl_assert(0); |
| 422 | } |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 423 | |
weidendo | 0a1951d | 2009-06-15 00:16:36 +0000 | [diff] [blame] | 424 | CLG_DEBUGIF(5) { |
| 425 | if (inew > i+1) { |
| 426 | VG_(printf)(" merge "); |
| 427 | showEvent( ev2 ); |
| 428 | } |
| 429 | if (inew > i+2) { |
| 430 | VG_(printf)(" merge "); |
| 431 | showEvent( ev3 ); |
| 432 | } |
| 433 | if (helperAddr) |
| 434 | VG_(printf)(" call %s (%p)\n", |
| 435 | helperName, helperAddr); |
| 436 | } |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 437 | |
weidendo | 0a1951d | 2009-06-15 00:16:36 +0000 | [diff] [blame] | 438 | /* helper could be unset depending on the simulator used */ |
| 439 | if (helperAddr == 0) continue; |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 440 | |
weidendo | 0a1951d | 2009-06-15 00:16:36 +0000 | [diff] [blame] | 441 | /* Add the helper. */ |
| 442 | tl_assert(helperName); |
| 443 | tl_assert(helperAddr); |
| 444 | tl_assert(argv); |
| 445 | di = unsafeIRDirty_0_N( regparms, |
| 446 | helperName, VG_(fnptr_to_fnentry)( helperAddr ), |
| 447 | argv ); |
| 448 | addStmtToIRSB( clgs->sbOut, IRStmt_Dirty(di) ); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 449 | } |
| 450 | |
weidendo | 0a1951d | 2009-06-15 00:16:36 +0000 | [diff] [blame] | 451 | clgs->events_used = 0; |
| 452 | } |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 453 | |
weidendo | 0a1951d | 2009-06-15 00:16:36 +0000 | [diff] [blame] | 454 | static void addEvent_Ir ( ClgState* clgs, InstrInfo* inode ) |
| 455 | { |
| 456 | Event* evt; |
| 457 | tl_assert(clgs->seen_before || (inode->eventset == 0)); |
| 458 | if (!CLG_(clo).simulate_cache) return; |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 459 | |
weidendo | 0a1951d | 2009-06-15 00:16:36 +0000 | [diff] [blame] | 460 | if (clgs->events_used == N_EVENTS) |
| 461 | flushEvents(clgs); |
| 462 | tl_assert(clgs->events_used >= 0 && clgs->events_used < N_EVENTS); |
| 463 | evt = &clgs->events[clgs->events_used]; |
| 464 | init_Event(evt); |
| 465 | evt->tag = Ev_Ir; |
| 466 | evt->inode = inode; |
| 467 | clgs->events_used++; |
| 468 | } |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 469 | |
weidendo | 0a1951d | 2009-06-15 00:16:36 +0000 | [diff] [blame] | 470 | static |
| 471 | void addEvent_Dr ( ClgState* clgs, InstrInfo* inode, Int datasize, IRAtom* ea ) |
| 472 | { |
| 473 | Event* evt; |
| 474 | tl_assert(isIRAtom(ea)); |
| 475 | tl_assert(datasize >= 1 && datasize <= MIN_LINE_SIZE); |
| 476 | if (!CLG_(clo).simulate_cache) return; |
weidendo | c8e7615 | 2006-05-27 15:30:58 +0000 | [diff] [blame] | 477 | |
weidendo | 0a1951d | 2009-06-15 00:16:36 +0000 | [diff] [blame] | 478 | if (clgs->events_used == N_EVENTS) |
| 479 | flushEvents(clgs); |
| 480 | tl_assert(clgs->events_used >= 0 && clgs->events_used < N_EVENTS); |
| 481 | evt = &clgs->events[clgs->events_used]; |
| 482 | init_Event(evt); |
| 483 | evt->tag = Ev_Dr; |
| 484 | evt->inode = inode; |
| 485 | evt->Ev.Dr.szB = datasize; |
| 486 | evt->Ev.Dr.ea = ea; |
| 487 | clgs->events_used++; |
| 488 | } |
weidendo | c8e7615 | 2006-05-27 15:30:58 +0000 | [diff] [blame] | 489 | |
weidendo | 0a1951d | 2009-06-15 00:16:36 +0000 | [diff] [blame] | 490 | static |
| 491 | void addEvent_Dw ( ClgState* clgs, InstrInfo* inode, Int datasize, IRAtom* ea ) |
| 492 | { |
| 493 | Event* lastEvt; |
| 494 | Event* evt; |
| 495 | tl_assert(isIRAtom(ea)); |
| 496 | tl_assert(datasize >= 1 && datasize <= MIN_LINE_SIZE); |
| 497 | if (!CLG_(clo).simulate_cache) return; |
weidendo | c8e7615 | 2006-05-27 15:30:58 +0000 | [diff] [blame] | 498 | |
weidendo | 0a1951d | 2009-06-15 00:16:36 +0000 | [diff] [blame] | 499 | /* Is it possible to merge this write with the preceding read? */ |
| 500 | lastEvt = &clgs->events[clgs->events_used-1]; |
| 501 | if (clgs->events_used > 0 |
| 502 | && lastEvt->tag == Ev_Dr |
| 503 | && lastEvt->Ev.Dr.szB == datasize |
| 504 | && lastEvt->inode == inode |
| 505 | && eqIRAtom(lastEvt->Ev.Dr.ea, ea)) |
| 506 | { |
| 507 | lastEvt->tag = Ev_Dm; |
| 508 | return; |
| 509 | } |
weidendo | c8e7615 | 2006-05-27 15:30:58 +0000 | [diff] [blame] | 510 | |
weidendo | 0a1951d | 2009-06-15 00:16:36 +0000 | [diff] [blame] | 511 | /* No. Add as normal. */ |
| 512 | if (clgs->events_used == N_EVENTS) |
| 513 | flushEvents(clgs); |
| 514 | tl_assert(clgs->events_used >= 0 && clgs->events_used < N_EVENTS); |
| 515 | evt = &clgs->events[clgs->events_used]; |
| 516 | init_Event(evt); |
| 517 | evt->tag = Ev_Dw; |
| 518 | evt->inode = inode; |
| 519 | evt->Ev.Dw.szB = datasize; |
| 520 | evt->Ev.Dw.ea = ea; |
| 521 | clgs->events_used++; |
| 522 | } |
| 523 | |
| 524 | /* Initialise or check (if already seen before) an InstrInfo for next insn. |
| 525 | We only can set instr_offset/instr_size here. The required event set and |
| 526 | resulting cost offset depend on events (Ir/Dr/Dw/Dm) in guest |
| 527 | instructions. The event set is extended as required on flush of the event |
| 528 | queue (when Dm events were determined), cost offsets are determined at |
| 529 | end of BB instrumentation. */ |
| 530 | static |
| 531 | InstrInfo* next_InstrInfo ( ClgState* clgs, UInt instr_size ) |
| 532 | { |
| 533 | InstrInfo* ii; |
| 534 | tl_assert(clgs->ii_index >= 0); |
| 535 | tl_assert(clgs->ii_index < clgs->bb->instr_count); |
| 536 | ii = &clgs->bb->instr[ clgs->ii_index ]; |
| 537 | |
| 538 | if (clgs->seen_before) { |
| 539 | CLG_ASSERT(ii->instr_offset == clgs->instr_offset); |
| 540 | CLG_ASSERT(ii->instr_size == instr_size); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 541 | } |
| 542 | else { |
weidendo | 0a1951d | 2009-06-15 00:16:36 +0000 | [diff] [blame] | 543 | ii->instr_offset = clgs->instr_offset; |
| 544 | ii->instr_size = instr_size; |
| 545 | ii->cost_offset = 0; |
| 546 | ii->eventset = 0; |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 547 | } |
| 548 | |
weidendo | 0a1951d | 2009-06-15 00:16:36 +0000 | [diff] [blame] | 549 | clgs->ii_index++; |
| 550 | clgs->instr_offset += instr_size; |
| 551 | CLG_(stat).distinct_instrs++; |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 552 | |
weidendo | 0a1951d | 2009-06-15 00:16:36 +0000 | [diff] [blame] | 553 | return ii; |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 554 | } |
| 555 | |
weidendo | 0a1951d | 2009-06-15 00:16:36 +0000 | [diff] [blame] | 556 | // return total number of cost values needed for this BB |
| 557 | static |
| 558 | UInt update_cost_offsets( ClgState* clgs ) |
| 559 | { |
| 560 | Int i; |
| 561 | InstrInfo* ii; |
| 562 | UInt cost_offset = 0; |
| 563 | |
| 564 | CLG_ASSERT(clgs->bb->instr_count == clgs->ii_index); |
| 565 | for(i=0; i<clgs->ii_index; i++) { |
| 566 | ii = &clgs->bb->instr[i]; |
| 567 | if (clgs->seen_before) { |
| 568 | CLG_ASSERT(ii->cost_offset == cost_offset); |
| 569 | } else |
| 570 | ii->cost_offset = cost_offset; |
| 571 | cost_offset += ii->eventset ? ii->eventset->size : 0; |
| 572 | } |
| 573 | |
| 574 | return cost_offset; |
| 575 | } |
| 576 | |
| 577 | /*------------------------------------------------------------*/ |
| 578 | /*--- Instrumentation ---*/ |
| 579 | /*------------------------------------------------------------*/ |
| 580 | |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 581 | #if defined(VG_BIGENDIAN) |
| 582 | # define CLGEndness Iend_BE |
| 583 | #elif defined(VG_LITTLEENDIAN) |
| 584 | # define CLGEndness Iend_LE |
| 585 | #else |
| 586 | # error "Unknown endianness" |
| 587 | #endif |
| 588 | |
| 589 | static |
| 590 | Addr IRConst2Addr(IRConst* con) |
| 591 | { |
| 592 | Addr addr; |
| 593 | |
| 594 | if (sizeof(Addr) == 4) { |
| 595 | CLG_ASSERT( con->tag == Ico_U32 ); |
| 596 | addr = con->Ico.U32; |
| 597 | } |
| 598 | else if (sizeof(Addr) == 8) { |
| 599 | CLG_ASSERT( con->tag == Ico_U64 ); |
| 600 | addr = con->Ico.U64; |
| 601 | } |
| 602 | else |
| 603 | VG_(tool_panic)("Callgrind: invalid Addr type"); |
| 604 | |
| 605 | return addr; |
| 606 | } |
| 607 | |
| 608 | /* First pass over a BB to instrument, counting instructions and jumps |
| 609 | * This is needed for the size of the BB struct to allocate |
| 610 | * |
| 611 | * Called from CLG_(get_bb) |
| 612 | */ |
weidendo | 0a1951d | 2009-06-15 00:16:36 +0000 | [diff] [blame] | 613 | void CLG_(collectBlockInfo)(IRSB* sbIn, |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 614 | /*INOUT*/ UInt* instrs, |
| 615 | /*INOUT*/ UInt* cjmps, |
| 616 | /*INOUT*/ Bool* cjmp_inverted) |
| 617 | { |
| 618 | Int i; |
| 619 | IRStmt* st; |
| 620 | Addr instrAddr =0, jumpDst; |
| 621 | UInt instrLen = 0; |
| 622 | Bool toNextInstr = False; |
| 623 | |
| 624 | // Ist_Exit has to be ignored in preamble code, before first IMark: |
| 625 | // preamble code is added by VEX for self modifying code, and has |
| 626 | // nothing to do with client code |
| 627 | Bool inPreamble = True; |
| 628 | |
weidendo | 0a1951d | 2009-06-15 00:16:36 +0000 | [diff] [blame] | 629 | if (!sbIn) return; |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 630 | |
weidendo | 0a1951d | 2009-06-15 00:16:36 +0000 | [diff] [blame] | 631 | for (i = 0; i < sbIn->stmts_used; i++) { |
| 632 | st = sbIn->stmts[i]; |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 633 | if (Ist_IMark == st->tag) { |
| 634 | inPreamble = False; |
| 635 | |
| 636 | instrAddr = (Addr)ULong_to_Ptr(st->Ist.IMark.addr); |
| 637 | instrLen = st->Ist.IMark.len; |
| 638 | |
| 639 | (*instrs)++; |
| 640 | toNextInstr = False; |
| 641 | } |
| 642 | if (inPreamble) continue; |
| 643 | if (Ist_Exit == st->tag) { |
| 644 | jumpDst = IRConst2Addr(st->Ist.Exit.dst); |
| 645 | toNextInstr = (jumpDst == instrAddr + instrLen); |
weidendo | 0a1951d | 2009-06-15 00:16:36 +0000 | [diff] [blame] | 646 | |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 647 | (*cjmps)++; |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | /* if the last instructions of BB conditionally jumps to next instruction |
| 652 | * (= first instruction of next BB in memory), this is a inverted by VEX. |
| 653 | */ |
| 654 | *cjmp_inverted = toNextInstr; |
| 655 | } |
| 656 | |
| 657 | static |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 658 | void addConstMemStoreStmt( IRSB* bbOut, UWord addr, UInt val, IRType hWordTy) |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 659 | { |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 660 | /* JRS 2009june01: re IRTemp_INVALID, am assuming that this |
| 661 | function is used only to create instrumentation, and not to |
| 662 | copy/reconstruct IRStmt_Stores that were in the incoming IR |
| 663 | superblock. If that is not a correct assumption, then things |
| 664 | will break badly on PowerPC, esp w/ threaded apps. */ |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 665 | addStmtToIRSB( bbOut, |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 666 | IRStmt_Store(CLGEndness, |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 667 | IRTemp_INVALID, |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 668 | IRExpr_Const(hWordTy == Ity_I32 ? |
| 669 | IRConst_U32( addr ) : |
| 670 | IRConst_U64( addr )), |
| 671 | IRExpr_Const(IRConst_U32(val)) )); |
| 672 | } |
| 673 | |
weidendo | 0a1951d | 2009-06-15 00:16:36 +0000 | [diff] [blame] | 674 | |
| 675 | /* add helper call to setup_bbcc, with pointer to BB struct as argument |
| 676 | * |
| 677 | * precondition for setup_bbcc: |
| 678 | * - jmps_passed has number of cond.jumps passed in last executed BB |
| 679 | * - current_bbcc has a pointer to the BBCC of the last executed BB |
| 680 | * Thus, if bbcc_jmpkind is != -1 (JmpNone), |
| 681 | * current_bbcc->bb->jmp_addr |
| 682 | * gives the address of the jump source. |
| 683 | * |
| 684 | * the setup does 2 things: |
| 685 | * - trace call: |
| 686 | * * Unwind own call stack, i.e sync our ESP with real ESP |
| 687 | * This is for ESP manipulation (longjmps, C++ exec handling) and RET |
| 688 | * * For CALLs or JMPs crossing objects, record call arg + |
| 689 | * push are on own call stack |
| 690 | * |
| 691 | * - prepare for cache log functions: |
| 692 | * set current_bbcc to BBCC that gets the costs for this BB execution |
| 693 | * attached |
| 694 | */ |
| 695 | static |
| 696 | void addBBSetupCall(ClgState* clgs) |
| 697 | { |
| 698 | IRDirty* di; |
| 699 | IRExpr *arg1, **argv; |
| 700 | |
| 701 | arg1 = mkIRExpr_HWord( (HWord)clgs->bb ); |
| 702 | argv = mkIRExprVec_1(arg1); |
| 703 | di = unsafeIRDirty_0_N( 1, "setup_bbcc", |
| 704 | VG_(fnptr_to_fnentry)( & CLG_(setup_bbcc) ), |
| 705 | argv); |
| 706 | addStmtToIRSB( clgs->sbOut, IRStmt_Dirty(di) ); |
| 707 | } |
| 708 | |
| 709 | |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 710 | static |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 711 | IRSB* CLG_(instrument)( VgCallbackClosure* closure, |
weidendo | 0a1951d | 2009-06-15 00:16:36 +0000 | [diff] [blame] | 712 | IRSB* sbIn, |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 713 | VexGuestLayout* layout, |
| 714 | VexGuestExtents* vge, |
| 715 | IRType gWordTy, IRType hWordTy ) |
| 716 | { |
weidendo | 0a1951d | 2009-06-15 00:16:36 +0000 | [diff] [blame] | 717 | Int i, isize; |
| 718 | IRStmt* st; |
| 719 | Addr origAddr; |
| 720 | InstrInfo* curr_inode = NULL; |
| 721 | ClgState clgs; |
| 722 | UInt cJumps = 0; |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 723 | |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 724 | |
| 725 | if (gWordTy != hWordTy) { |
| 726 | /* We don't currently support this case. */ |
| 727 | VG_(tool_panic)("host/guest word size mismatch"); |
| 728 | } |
| 729 | |
| 730 | // No instrumentation if it is switched off |
| 731 | if (! CLG_(instrument_state)) { |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 732 | CLG_DEBUG(5, "instrument(BB %#lx) [Instrumentation OFF]\n", |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 733 | (Addr)closure->readdr); |
weidendo | 0a1951d | 2009-06-15 00:16:36 +0000 | [diff] [blame] | 734 | return sbIn; |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 735 | } |
| 736 | |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 737 | CLG_DEBUG(3, "+ instrument(BB %#lx)\n", (Addr)closure->readdr); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 738 | |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 739 | /* Set up SB for instrumented IR */ |
weidendo | 0a1951d | 2009-06-15 00:16:36 +0000 | [diff] [blame] | 740 | clgs.sbOut = deepCopyIRSBExceptStmts(sbIn); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 741 | |
| 742 | // Copy verbatim any IR preamble preceding the first IMark |
| 743 | i = 0; |
weidendo | 0a1951d | 2009-06-15 00:16:36 +0000 | [diff] [blame] | 744 | while (i < sbIn->stmts_used && sbIn->stmts[i]->tag != Ist_IMark) { |
| 745 | addStmtToIRSB( clgs.sbOut, sbIn->stmts[i] ); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 746 | i++; |
| 747 | } |
| 748 | |
| 749 | // Get the first statement, and origAddr from it |
weidendo | 0a1951d | 2009-06-15 00:16:36 +0000 | [diff] [blame] | 750 | CLG_ASSERT(sbIn->stmts_used >0); |
| 751 | CLG_ASSERT(i < sbIn->stmts_used); |
| 752 | st = sbIn->stmts[i]; |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 753 | CLG_ASSERT(Ist_IMark == st->tag); |
weidendo | 0a1951d | 2009-06-15 00:16:36 +0000 | [diff] [blame] | 754 | |
| 755 | origAddr = (Addr)st->Ist.IMark.addr; |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 756 | CLG_ASSERT(origAddr == st->Ist.IMark.addr); // XXX: check no overflow |
| 757 | |
weidendo | 0a1951d | 2009-06-15 00:16:36 +0000 | [diff] [blame] | 758 | /* Get BB struct (creating if necessary). |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 759 | * JS: The hash table is keyed with orig_addr_noredir -- important! |
| 760 | * JW: Why? If it is because of different chasing of the redirection, |
| 761 | * this is not needed, as chasing is switched off in callgrind |
| 762 | */ |
weidendo | 0a1951d | 2009-06-15 00:16:36 +0000 | [diff] [blame] | 763 | clgs.bb = CLG_(get_bb)(origAddr, sbIn, &(clgs.seen_before)); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 764 | |
weidendo | 0a1951d | 2009-06-15 00:16:36 +0000 | [diff] [blame] | 765 | addBBSetupCall(&clgs); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 766 | |
weidendo | 0a1951d | 2009-06-15 00:16:36 +0000 | [diff] [blame] | 767 | // Set up running state |
| 768 | clgs.events_used = 0; |
| 769 | clgs.ii_index = 0; |
| 770 | clgs.instr_offset = 0; |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 771 | |
weidendo | 0a1951d | 2009-06-15 00:16:36 +0000 | [diff] [blame] | 772 | for (/*use current i*/; i < sbIn->stmts_used; i++) { |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 773 | |
weidendo | 0a1951d | 2009-06-15 00:16:36 +0000 | [diff] [blame] | 774 | st = sbIn->stmts[i]; |
| 775 | CLG_ASSERT(isFlatIRStmt(st)); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 776 | |
weidendo | 0a1951d | 2009-06-15 00:16:36 +0000 | [diff] [blame] | 777 | switch (st->tag) { |
| 778 | case Ist_NoOp: |
| 779 | case Ist_AbiHint: |
| 780 | case Ist_Put: |
| 781 | case Ist_PutI: |
| 782 | case Ist_MBE: |
| 783 | break; |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 784 | |
weidendo | 0a1951d | 2009-06-15 00:16:36 +0000 | [diff] [blame] | 785 | case Ist_IMark: { |
| 786 | CLG_ASSERT(clgs.instr_offset == (Addr)st->Ist.IMark.addr - origAddr); |
| 787 | isize = st->Ist.IMark.len; |
| 788 | // If Vex fails to decode an instruction, the size will be zero. |
| 789 | // Pretend otherwise. |
| 790 | if (isize == 0) isize = VG_MIN_INSTR_SZB; |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 791 | |
weidendo | 0a1951d | 2009-06-15 00:16:36 +0000 | [diff] [blame] | 792 | // Sanity-check size. |
| 793 | tl_assert( (VG_MIN_INSTR_SZB <= isize && isize <= VG_MAX_INSTR_SZB) |
| 794 | || VG_CLREQ_SZB == isize ); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 795 | |
weidendo | 0a1951d | 2009-06-15 00:16:36 +0000 | [diff] [blame] | 796 | // Init the inode, record it as the current one. |
| 797 | // Subsequent Dr/Dw/Dm events from the same instruction will |
| 798 | // also use it. |
| 799 | curr_inode = next_InstrInfo (&clgs, isize); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 800 | |
weidendo | 0a1951d | 2009-06-15 00:16:36 +0000 | [diff] [blame] | 801 | addEvent_Ir( &clgs, curr_inode ); |
| 802 | break; |
| 803 | } |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 804 | |
weidendo | 0a1951d | 2009-06-15 00:16:36 +0000 | [diff] [blame] | 805 | case Ist_WrTmp: { |
| 806 | IRExpr* data = st->Ist.WrTmp.data; |
| 807 | if (data->tag == Iex_Load) { |
| 808 | IRExpr* aexpr = data->Iex.Load.addr; |
| 809 | // Note also, endianness info is ignored. I guess |
| 810 | // that's not interesting. |
| 811 | addEvent_Dr( &clgs, curr_inode, |
| 812 | sizeofIRType(data->Iex.Load.ty), aexpr ); |
| 813 | } |
| 814 | break; |
| 815 | } |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 816 | |
weidendo | 0a1951d | 2009-06-15 00:16:36 +0000 | [diff] [blame] | 817 | case Ist_Store: { |
| 818 | IRExpr* data = st->Ist.Store.data; |
| 819 | IRExpr* aexpr = st->Ist.Store.addr; |
| 820 | addEvent_Dw( &clgs, curr_inode, |
| 821 | sizeofIRType(typeOfIRExpr(sbIn->tyenv, data)), aexpr ); |
| 822 | break; |
| 823 | } |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 824 | |
weidendo | 0a1951d | 2009-06-15 00:16:36 +0000 | [diff] [blame] | 825 | case Ist_Dirty: { |
| 826 | Int dataSize; |
| 827 | IRDirty* d = st->Ist.Dirty.details; |
| 828 | if (d->mFx != Ifx_None) { |
| 829 | /* This dirty helper accesses memory. Collect the details. */ |
| 830 | tl_assert(d->mAddr != NULL); |
| 831 | tl_assert(d->mSize != 0); |
| 832 | dataSize = d->mSize; |
| 833 | // Large (eg. 28B, 108B, 512B on x86) data-sized |
| 834 | // instructions will be done inaccurately, but they're |
| 835 | // very rare and this avoids errors from hitting more |
| 836 | // than two cache lines in the simulation. |
| 837 | if (dataSize > MIN_LINE_SIZE) |
| 838 | dataSize = MIN_LINE_SIZE; |
| 839 | if (d->mFx == Ifx_Read || d->mFx == Ifx_Modify) |
| 840 | addEvent_Dr( &clgs, curr_inode, dataSize, d->mAddr ); |
| 841 | if (d->mFx == Ifx_Write || d->mFx == Ifx_Modify) |
| 842 | addEvent_Dw( &clgs, curr_inode, dataSize, d->mAddr ); |
| 843 | } else { |
| 844 | tl_assert(d->mAddr == NULL); |
| 845 | tl_assert(d->mSize == 0); |
| 846 | } |
| 847 | break; |
| 848 | } |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 849 | |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 850 | case Ist_CAS: { |
| 851 | /* We treat it as a read and a write of the location. I |
| 852 | think that is the same behaviour as it was before IRCAS |
| 853 | was introduced, since prior to that point, the Vex |
| 854 | front ends would translate a lock-prefixed instruction |
| 855 | into a (normal) read followed by a (normal) write. */ |
| 856 | Int dataSize; |
| 857 | IRCAS* cas = st->Ist.CAS.details; |
| 858 | CLG_ASSERT(cas->addr && isIRAtom(cas->addr)); |
| 859 | CLG_ASSERT(cas->dataLo); |
| 860 | dataSize = sizeofIRType(typeOfIRExpr(sbIn->tyenv, cas->dataLo)); |
| 861 | if (cas->dataHi != NULL) |
| 862 | dataSize *= 2; /* since this is a doubleword-cas */ |
| 863 | addEvent_Dr( &clgs, curr_inode, dataSize, cas->addr ); |
| 864 | addEvent_Dw( &clgs, curr_inode, dataSize, cas->addr ); |
| 865 | break; |
| 866 | } |
| 867 | |
weidendo | 0a1951d | 2009-06-15 00:16:36 +0000 | [diff] [blame] | 868 | case Ist_Exit: { |
| 869 | UInt jmps_passed; |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 870 | |
weidendo | 0a1951d | 2009-06-15 00:16:36 +0000 | [diff] [blame] | 871 | /* We may never reach the next statement, so need to flush |
| 872 | all outstanding transactions now. */ |
| 873 | flushEvents( &clgs ); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 874 | |
weidendo | 0a1951d | 2009-06-15 00:16:36 +0000 | [diff] [blame] | 875 | CLG_ASSERT(clgs.ii_index>0); |
| 876 | if (!clgs.seen_before) { |
| 877 | clgs.bb->jmp[cJumps].instr = clgs.ii_index-1; |
| 878 | clgs.bb->jmp[cJumps].skip = False; |
| 879 | } |
| 880 | |
| 881 | /* Update global variable jmps_passed before the jump |
| 882 | * A correction is needed if VEX inverted the last jump condition |
| 883 | */ |
| 884 | jmps_passed = cJumps; |
| 885 | if ((cJumps+1 == clgs.bb->cjmp_count) && clgs.bb->cjmp_inverted) |
| 886 | jmps_passed++; |
| 887 | addConstMemStoreStmt( clgs.sbOut, |
| 888 | (UWord) &CLG_(current_state).jmps_passed, |
| 889 | jmps_passed, hWordTy); |
| 890 | cJumps++; |
| 891 | |
| 892 | break; |
| 893 | } |
| 894 | |
| 895 | default: |
| 896 | tl_assert(0); |
| 897 | break; |
| 898 | } |
| 899 | |
| 900 | /* Copy the original statement */ |
| 901 | addStmtToIRSB( clgs.sbOut, st ); |
| 902 | |
| 903 | CLG_DEBUGIF(5) { |
| 904 | VG_(printf)(" pass "); |
| 905 | ppIRStmt(st); |
| 906 | VG_(printf)("\n"); |
| 907 | } |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 908 | } |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 909 | |
weidendo | 0a1951d | 2009-06-15 00:16:36 +0000 | [diff] [blame] | 910 | /* At the end of the bb. Flush outstandings. */ |
| 911 | flushEvents( &clgs ); |
| 912 | |
| 913 | /* Always update global variable jmps_passed at end of bb. |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 914 | * A correction is needed if VEX inverted the last jump condition |
| 915 | */ |
weidendo | 0a1951d | 2009-06-15 00:16:36 +0000 | [diff] [blame] | 916 | { |
| 917 | UInt jmps_passed = cJumps; |
| 918 | if (clgs.bb->cjmp_inverted) jmps_passed--; |
| 919 | addConstMemStoreStmt( clgs.sbOut, |
| 920 | (UWord) &CLG_(current_state).jmps_passed, |
| 921 | jmps_passed, hWordTy); |
| 922 | } |
| 923 | CLG_ASSERT(clgs.bb->cjmp_count == cJumps); |
| 924 | CLG_ASSERT(clgs.bb->instr_count = clgs.ii_index); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 925 | |
| 926 | /* This stores the instr of the call/ret at BB end */ |
weidendo | 0a1951d | 2009-06-15 00:16:36 +0000 | [diff] [blame] | 927 | clgs.bb->jmp[cJumps].instr = clgs.ii_index-1; |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 928 | |
weidendo | 0a1951d | 2009-06-15 00:16:36 +0000 | [diff] [blame] | 929 | if (clgs.seen_before) { |
| 930 | CLG_ASSERT(clgs.bb->cost_count == update_cost_offsets(&clgs)); |
| 931 | CLG_ASSERT(clgs.bb->instr_len = clgs.instr_offset); |
| 932 | CLG_ASSERT(clgs.bb->jmpkind == sbIn->jumpkind); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 933 | } |
| 934 | else { |
weidendo | 0a1951d | 2009-06-15 00:16:36 +0000 | [diff] [blame] | 935 | clgs.bb->cost_count = update_cost_offsets(&clgs); |
| 936 | clgs.bb->instr_len = clgs.instr_offset; |
| 937 | clgs.bb->jmpkind = sbIn->jumpkind; |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 938 | } |
weidendo | 0a1951d | 2009-06-15 00:16:36 +0000 | [diff] [blame] | 939 | |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 940 | CLG_DEBUG(3, "- instrument(BB %#lx): byteLen %u, CJumps %u, CostLen %u\n", |
weidendo | 0a1951d | 2009-06-15 00:16:36 +0000 | [diff] [blame] | 941 | origAddr, clgs.bb->instr_len, |
| 942 | clgs.bb->cjmp_count, clgs.bb->cost_count); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 943 | if (cJumps>0) { |
| 944 | CLG_DEBUG(3, " [ "); |
| 945 | for (i=0;i<cJumps;i++) |
weidendo | 0a1951d | 2009-06-15 00:16:36 +0000 | [diff] [blame] | 946 | CLG_DEBUG(3, "%d ", clgs.bb->jmp[i].instr); |
| 947 | CLG_DEBUG(3, "], last inverted: %s \n", |
| 948 | clgs.bb->cjmp_inverted ? "yes":"no"); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 949 | } |
| 950 | |
weidendo | 0a1951d | 2009-06-15 00:16:36 +0000 | [diff] [blame] | 951 | return clgs.sbOut; |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 952 | } |
| 953 | |
| 954 | /*--------------------------------------------------------------------*/ |
| 955 | /*--- Discarding BB info ---*/ |
| 956 | /*--------------------------------------------------------------------*/ |
| 957 | |
| 958 | // Called when a translation is removed from the translation cache for |
| 959 | // any reason at all: to free up space, because the guest code was |
| 960 | // unmapped or modified, or for any arbitrary reason. |
| 961 | static |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 962 | void clg_discard_superblock_info ( Addr64 orig_addr64, VexGuestExtents vge ) |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 963 | { |
| 964 | Addr orig_addr = (Addr)orig_addr64; |
| 965 | |
| 966 | tl_assert(vge.n_used > 0); |
| 967 | |
| 968 | if (0) |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 969 | VG_(printf)( "discard_superblock_info: %p, %p, %llu\n", |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 970 | (void*)(Addr)orig_addr, |
| 971 | (void*)(Addr)vge.base[0], (ULong)vge.len[0]); |
| 972 | |
| 973 | // Get BB info, remove from table, free BB info. Simple! Note that we |
| 974 | // use orig_addr, not the first instruction address in vge. |
| 975 | CLG_(delete_bb)(orig_addr); |
| 976 | } |
| 977 | |
| 978 | |
| 979 | /*------------------------------------------------------------*/ |
| 980 | /*--- CLG_(fini)() and related function ---*/ |
| 981 | /*------------------------------------------------------------*/ |
| 982 | |
| 983 | |
| 984 | |
| 985 | static void zero_thread_cost(thread_info* t) |
| 986 | { |
| 987 | Int i; |
| 988 | |
| 989 | for(i = 0; i < CLG_(current_call_stack).sp; i++) { |
| 990 | if (!CLG_(current_call_stack).entry[i].jcc) continue; |
| 991 | |
| 992 | /* reset call counters to current for active calls */ |
| 993 | CLG_(copy_cost)( CLG_(sets).full, |
| 994 | CLG_(current_call_stack).entry[i].enter_cost, |
| 995 | CLG_(current_state).cost ); |
| 996 | } |
| 997 | |
| 998 | CLG_(forall_bbccs)(CLG_(zero_bbcc)); |
| 999 | |
| 1000 | /* set counter for last dump */ |
| 1001 | CLG_(copy_cost)( CLG_(sets).full, |
| 1002 | t->lastdump_cost, CLG_(current_state).cost ); |
| 1003 | } |
| 1004 | |
| 1005 | void CLG_(zero_all_cost)(Bool only_current_thread) |
| 1006 | { |
| 1007 | if (VG_(clo_verbosity) > 1) |
sewardj | 0f33adf | 2009-07-15 14:51:03 +0000 | [diff] [blame] | 1008 | VG_(message)(Vg_DebugMsg, " Zeroing costs...\n"); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 1009 | |
| 1010 | if (only_current_thread) |
| 1011 | zero_thread_cost(CLG_(get_current_thread)()); |
| 1012 | else |
| 1013 | CLG_(forall_threads)(zero_thread_cost); |
| 1014 | |
| 1015 | if (VG_(clo_verbosity) > 1) |
sewardj | 0f33adf | 2009-07-15 14:51:03 +0000 | [diff] [blame] | 1016 | VG_(message)(Vg_DebugMsg, " ...done\n"); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 1017 | } |
| 1018 | |
| 1019 | static |
| 1020 | void unwind_thread(thread_info* t) |
| 1021 | { |
| 1022 | /* unwind signal handlers */ |
| 1023 | while(CLG_(current_state).sig !=0) |
| 1024 | CLG_(post_signal)(CLG_(current_tid),CLG_(current_state).sig); |
| 1025 | |
| 1026 | /* unwind regular call stack */ |
| 1027 | while(CLG_(current_call_stack).sp>0) |
| 1028 | CLG_(pop_call_stack)(); |
weidendo | f3e0b49 | 2006-09-10 22:34:20 +0000 | [diff] [blame] | 1029 | |
| 1030 | /* reset context and function stack for context generation */ |
| 1031 | CLG_(init_exec_state)( &CLG_(current_state) ); |
| 1032 | CLG_(current_fn_stack).top = CLG_(current_fn_stack).bottom; |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 1033 | } |
| 1034 | |
| 1035 | /* Ups, this can go wrong... */ |
| 1036 | extern void VG_(discard_translations) ( Addr64 start, ULong range ); |
| 1037 | |
| 1038 | void CLG_(set_instrument_state)(Char* reason, Bool state) |
| 1039 | { |
| 1040 | if (CLG_(instrument_state) == state) { |
| 1041 | CLG_DEBUG(2, "%s: instrumentation already %s\n", |
| 1042 | reason, state ? "ON" : "OFF"); |
| 1043 | return; |
| 1044 | } |
| 1045 | CLG_(instrument_state) = state; |
| 1046 | CLG_DEBUG(2, "%s: Switching instrumentation %s ...\n", |
| 1047 | reason, state ? "ON" : "OFF"); |
| 1048 | |
| 1049 | VG_(discard_translations)( (Addr64)0x1000, (ULong) ~0xfffl); |
| 1050 | |
| 1051 | /* reset internal state: call stacks, simulator */ |
| 1052 | CLG_(forall_threads)(unwind_thread); |
| 1053 | (*CLG_(cachesim).clear)(); |
| 1054 | if (0) |
| 1055 | CLG_(forall_threads)(zero_thread_cost); |
| 1056 | |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 1057 | if (VG_(clo_verbosity) > 1) |
sewardj | 0f33adf | 2009-07-15 14:51:03 +0000 | [diff] [blame] | 1058 | VG_(message)(Vg_DebugMsg, "%s: instrumentation switched %s\n", |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 1059 | reason, state ? "ON" : "OFF"); |
| 1060 | } |
| 1061 | |
| 1062 | |
| 1063 | static |
| 1064 | Bool CLG_(handle_client_request)(ThreadId tid, UWord *args, UWord *ret) |
| 1065 | { |
| 1066 | if (!VG_IS_TOOL_USERREQ('C','T',args[0])) |
| 1067 | return False; |
| 1068 | |
| 1069 | switch(args[0]) { |
| 1070 | case VG_USERREQ__DUMP_STATS: |
| 1071 | CLG_(dump_profile)("Client Request", True); |
| 1072 | *ret = 0; /* meaningless */ |
| 1073 | break; |
| 1074 | |
| 1075 | case VG_USERREQ__DUMP_STATS_AT: |
| 1076 | { |
| 1077 | Char buf[512]; |
njn | 8a7b41b | 2007-09-23 00:51:24 +0000 | [diff] [blame] | 1078 | VG_(sprintf)(buf,"Client Request: %s", (Char*)args[1]); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 1079 | CLG_(dump_profile)(buf, True); |
| 1080 | *ret = 0; /* meaningless */ |
| 1081 | } |
| 1082 | break; |
| 1083 | |
| 1084 | case VG_USERREQ__ZERO_STATS: |
| 1085 | CLG_(zero_all_cost)(True); |
| 1086 | *ret = 0; /* meaningless */ |
| 1087 | break; |
| 1088 | |
| 1089 | case VG_USERREQ__TOGGLE_COLLECT: |
| 1090 | CLG_(current_state).collect = !CLG_(current_state).collect; |
| 1091 | CLG_DEBUG(2, "Client Request: toggled collection state to %s\n", |
| 1092 | CLG_(current_state).collect ? "ON" : "OFF"); |
| 1093 | *ret = 0; /* meaningless */ |
| 1094 | break; |
| 1095 | |
| 1096 | case VG_USERREQ__START_INSTRUMENTATION: |
| 1097 | CLG_(set_instrument_state)("Client Request", True); |
| 1098 | *ret = 0; /* meaningless */ |
| 1099 | break; |
| 1100 | |
| 1101 | case VG_USERREQ__STOP_INSTRUMENTATION: |
| 1102 | CLG_(set_instrument_state)("Client Request", False); |
| 1103 | *ret = 0; /* meaningless */ |
| 1104 | break; |
| 1105 | |
| 1106 | default: |
| 1107 | return False; |
| 1108 | } |
| 1109 | |
| 1110 | return True; |
| 1111 | } |
| 1112 | |
| 1113 | |
| 1114 | /* Syscall Timing */ |
| 1115 | |
| 1116 | /* struct timeval syscalltime[VG_N_THREADS]; */ |
| 1117 | #if CLG_MICROSYSTIME |
| 1118 | #include <sys/time.h> |
| 1119 | #include <sys/syscall.h> |
| 1120 | extern Int VG_(do_syscall) ( UInt, ... ); |
| 1121 | |
| 1122 | ULong syscalltime[VG_N_THREADS]; |
| 1123 | #else |
| 1124 | UInt syscalltime[VG_N_THREADS]; |
| 1125 | #endif |
| 1126 | |
| 1127 | static |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 1128 | void CLG_(pre_syscalltime)(ThreadId tid, UInt syscallno, |
| 1129 | UWord* args, UInt nArgs) |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 1130 | { |
| 1131 | if (CLG_(clo).collect_systime) { |
| 1132 | #if CLG_MICROSYSTIME |
| 1133 | struct vki_timeval tv_now; |
| 1134 | VG_(do_syscall)(__NR_gettimeofday, (UInt)&tv_now, (UInt)NULL); |
| 1135 | syscalltime[tid] = tv_now.tv_sec * 1000000ULL + tv_now.tv_usec; |
| 1136 | #else |
| 1137 | syscalltime[tid] = VG_(read_millisecond_timer)(); |
| 1138 | #endif |
| 1139 | } |
| 1140 | } |
| 1141 | |
| 1142 | static |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 1143 | void CLG_(post_syscalltime)(ThreadId tid, UInt syscallno, |
| 1144 | UWord* args, UInt nArgs, SysRes res) |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 1145 | { |
weidendo | ae0bb6f | 2007-02-16 13:12:43 +0000 | [diff] [blame] | 1146 | if (CLG_(clo).collect_systime && |
| 1147 | CLG_(current_state).bbcc) { |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 1148 | Int o = CLG_(sets).off_full_systime; |
| 1149 | #if CLG_MICROSYSTIME |
| 1150 | struct vki_timeval tv_now; |
| 1151 | ULong diff; |
| 1152 | |
| 1153 | VG_(do_syscall)(__NR_gettimeofday, (UInt)&tv_now, (UInt)NULL); |
| 1154 | diff = (tv_now.tv_sec * 1000000ULL + tv_now.tv_usec) - syscalltime[tid]; |
| 1155 | #else |
| 1156 | UInt diff = VG_(read_millisecond_timer)() - syscalltime[tid]; |
| 1157 | #endif |
| 1158 | |
| 1159 | CLG_DEBUG(0," Time (Off %d) for Syscall %d: %ull\n", o, syscallno, diff); |
| 1160 | |
| 1161 | if (o<0) return; |
| 1162 | |
| 1163 | CLG_(current_state).cost[o] ++; |
| 1164 | CLG_(current_state).cost[o+1] += diff; |
| 1165 | if (!CLG_(current_state).bbcc->skipped) |
| 1166 | CLG_(init_cost_lz)(CLG_(sets).full, |
| 1167 | &(CLG_(current_state).bbcc->skipped)); |
| 1168 | CLG_(current_state).bbcc->skipped[o] ++; |
| 1169 | CLG_(current_state).bbcc->skipped[o+1] += diff; |
| 1170 | } |
| 1171 | } |
| 1172 | |
| 1173 | static |
| 1174 | void finish(void) |
| 1175 | { |
| 1176 | char buf[RESULTS_BUF_LEN]; |
| 1177 | |
| 1178 | CLG_DEBUG(0, "finish()\n"); |
| 1179 | |
| 1180 | (*CLG_(cachesim).finish)(); |
| 1181 | |
| 1182 | /* pop all remaining items from CallStack for correct sum |
| 1183 | */ |
| 1184 | CLG_(forall_threads)(unwind_thread); |
sewardj | e45a799 | 2006-10-17 02:24:18 +0000 | [diff] [blame] | 1185 | |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 1186 | CLG_(dump_profile)(0, False); |
sewardj | e45a799 | 2006-10-17 02:24:18 +0000 | [diff] [blame] | 1187 | |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 1188 | CLG_(finish_command)(); |
sewardj | e45a799 | 2006-10-17 02:24:18 +0000 | [diff] [blame] | 1189 | |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 1190 | if (VG_(clo_verbosity) == 0) return; |
| 1191 | |
| 1192 | /* Hash table stats */ |
sewardj | 2d9e874 | 2009-08-07 15:46:56 +0000 | [diff] [blame^] | 1193 | if (VG_(clo_stats)) { |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 1194 | int BB_lookups = |
| 1195 | CLG_(stat).full_debug_BBs + |
| 1196 | CLG_(stat).fn_name_debug_BBs + |
| 1197 | CLG_(stat).file_line_debug_BBs + |
| 1198 | CLG_(stat).no_debug_BBs; |
| 1199 | |
sewardj | 0f33adf | 2009-07-15 14:51:03 +0000 | [diff] [blame] | 1200 | VG_(message)(Vg_DebugMsg, "\n"); |
| 1201 | VG_(message)(Vg_DebugMsg, "Distinct objects: %d\n", |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 1202 | CLG_(stat).distinct_objs); |
sewardj | 0f33adf | 2009-07-15 14:51:03 +0000 | [diff] [blame] | 1203 | VG_(message)(Vg_DebugMsg, "Distinct files: %d\n", |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 1204 | CLG_(stat).distinct_files); |
sewardj | 0f33adf | 2009-07-15 14:51:03 +0000 | [diff] [blame] | 1205 | VG_(message)(Vg_DebugMsg, "Distinct fns: %d\n", |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 1206 | CLG_(stat).distinct_fns); |
sewardj | 0f33adf | 2009-07-15 14:51:03 +0000 | [diff] [blame] | 1207 | VG_(message)(Vg_DebugMsg, "Distinct contexts:%d\n", |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 1208 | CLG_(stat).distinct_contexts); |
sewardj | 0f33adf | 2009-07-15 14:51:03 +0000 | [diff] [blame] | 1209 | VG_(message)(Vg_DebugMsg, "Distinct BBs: %d\n", |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 1210 | CLG_(stat).distinct_bbs); |
sewardj | 0f33adf | 2009-07-15 14:51:03 +0000 | [diff] [blame] | 1211 | VG_(message)(Vg_DebugMsg, "Cost entries: %d (Chunks %d)\n", |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 1212 | CLG_(costarray_entries), CLG_(costarray_chunks)); |
sewardj | 0f33adf | 2009-07-15 14:51:03 +0000 | [diff] [blame] | 1213 | VG_(message)(Vg_DebugMsg, "Distinct BBCCs: %d\n", |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 1214 | CLG_(stat).distinct_bbccs); |
sewardj | 0f33adf | 2009-07-15 14:51:03 +0000 | [diff] [blame] | 1215 | VG_(message)(Vg_DebugMsg, "Distinct JCCs: %d\n", |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 1216 | CLG_(stat).distinct_jccs); |
sewardj | 0f33adf | 2009-07-15 14:51:03 +0000 | [diff] [blame] | 1217 | VG_(message)(Vg_DebugMsg, "Distinct skips: %d\n", |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 1218 | CLG_(stat).distinct_skips); |
sewardj | 0f33adf | 2009-07-15 14:51:03 +0000 | [diff] [blame] | 1219 | VG_(message)(Vg_DebugMsg, "BB lookups: %d\n", |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 1220 | BB_lookups); |
| 1221 | if (BB_lookups>0) { |
sewardj | 0f33adf | 2009-07-15 14:51:03 +0000 | [diff] [blame] | 1222 | VG_(message)(Vg_DebugMsg, "With full debug info:%3d%% (%d)\n", |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 1223 | CLG_(stat).full_debug_BBs * 100 / BB_lookups, |
| 1224 | CLG_(stat).full_debug_BBs); |
sewardj | 0f33adf | 2009-07-15 14:51:03 +0000 | [diff] [blame] | 1225 | VG_(message)(Vg_DebugMsg, "With file/line debug info:%3d%% (%d)\n", |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 1226 | CLG_(stat).file_line_debug_BBs * 100 / BB_lookups, |
| 1227 | CLG_(stat).file_line_debug_BBs); |
sewardj | 0f33adf | 2009-07-15 14:51:03 +0000 | [diff] [blame] | 1228 | VG_(message)(Vg_DebugMsg, "With fn name debug info:%3d%% (%d)\n", |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 1229 | CLG_(stat).fn_name_debug_BBs * 100 / BB_lookups, |
| 1230 | CLG_(stat).fn_name_debug_BBs); |
sewardj | 0f33adf | 2009-07-15 14:51:03 +0000 | [diff] [blame] | 1231 | VG_(message)(Vg_DebugMsg, "With no debug info:%3d%% (%d)\n", |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 1232 | CLG_(stat).no_debug_BBs * 100 / BB_lookups, |
| 1233 | CLG_(stat).no_debug_BBs); |
| 1234 | } |
sewardj | 0f33adf | 2009-07-15 14:51:03 +0000 | [diff] [blame] | 1235 | VG_(message)(Vg_DebugMsg, "BBCC Clones: %d\n", |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 1236 | CLG_(stat).bbcc_clones); |
sewardj | 0f33adf | 2009-07-15 14:51:03 +0000 | [diff] [blame] | 1237 | VG_(message)(Vg_DebugMsg, "BBs Retranslated: %d\n", |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 1238 | CLG_(stat).bb_retranslations); |
sewardj | 0f33adf | 2009-07-15 14:51:03 +0000 | [diff] [blame] | 1239 | VG_(message)(Vg_DebugMsg, "Distinct instrs: %d\n", |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 1240 | CLG_(stat).distinct_instrs); |
| 1241 | VG_(message)(Vg_DebugMsg, ""); |
| 1242 | |
sewardj | 0f33adf | 2009-07-15 14:51:03 +0000 | [diff] [blame] | 1243 | VG_(message)(Vg_DebugMsg, "LRU Contxt Misses: %d\n", |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 1244 | CLG_(stat).cxt_lru_misses); |
sewardj | 0f33adf | 2009-07-15 14:51:03 +0000 | [diff] [blame] | 1245 | VG_(message)(Vg_DebugMsg, "LRU BBCC Misses: %d\n", |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 1246 | CLG_(stat).bbcc_lru_misses); |
sewardj | 0f33adf | 2009-07-15 14:51:03 +0000 | [diff] [blame] | 1247 | VG_(message)(Vg_DebugMsg, "LRU JCC Misses: %d\n", |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 1248 | CLG_(stat).jcc_lru_misses); |
sewardj | 0f33adf | 2009-07-15 14:51:03 +0000 | [diff] [blame] | 1249 | VG_(message)(Vg_DebugMsg, "BBs Executed: %llu\n", |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 1250 | CLG_(stat).bb_executions); |
sewardj | 0f33adf | 2009-07-15 14:51:03 +0000 | [diff] [blame] | 1251 | VG_(message)(Vg_DebugMsg, "Calls: %llu\n", |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 1252 | CLG_(stat).call_counter); |
sewardj | 0f33adf | 2009-07-15 14:51:03 +0000 | [diff] [blame] | 1253 | VG_(message)(Vg_DebugMsg, "CondJMP followed: %llu\n", |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 1254 | CLG_(stat).jcnd_counter); |
sewardj | 0f33adf | 2009-07-15 14:51:03 +0000 | [diff] [blame] | 1255 | VG_(message)(Vg_DebugMsg, "Boring JMPs: %llu\n", |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 1256 | CLG_(stat).jump_counter); |
sewardj | 0f33adf | 2009-07-15 14:51:03 +0000 | [diff] [blame] | 1257 | VG_(message)(Vg_DebugMsg, "Recursive calls: %llu\n", |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 1258 | CLG_(stat).rec_call_counter); |
sewardj | 0f33adf | 2009-07-15 14:51:03 +0000 | [diff] [blame] | 1259 | VG_(message)(Vg_DebugMsg, "Returns: %llu\n", |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 1260 | CLG_(stat).ret_counter); |
| 1261 | |
| 1262 | VG_(message)(Vg_DebugMsg, ""); |
| 1263 | } |
| 1264 | |
| 1265 | CLG_(sprint_eventmapping)(buf, CLG_(dumpmap)); |
sewardj | 0f33adf | 2009-07-15 14:51:03 +0000 | [diff] [blame] | 1266 | VG_(message)(Vg_UserMsg, "Events : %s\n", buf); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 1267 | CLG_(sprint_mappingcost)(buf, CLG_(dumpmap), CLG_(total_cost)); |
sewardj | 0f33adf | 2009-07-15 14:51:03 +0000 | [diff] [blame] | 1268 | VG_(message)(Vg_UserMsg, "Collected : %s\n", buf); |
| 1269 | VG_(message)(Vg_UserMsg, "\n"); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 1270 | |
| 1271 | // if (CLG_(clo).simulate_cache) |
| 1272 | (*CLG_(cachesim).printstat)(); |
| 1273 | } |
| 1274 | |
| 1275 | |
| 1276 | void CLG_(fini)(Int exitcode) |
| 1277 | { |
| 1278 | finish(); |
| 1279 | } |
| 1280 | |
| 1281 | |
| 1282 | /*--------------------------------------------------------------------*/ |
| 1283 | /*--- Setup ---*/ |
| 1284 | /*--------------------------------------------------------------------*/ |
| 1285 | |
njn | 3e32c87 | 2006-12-24 07:51:17 +0000 | [diff] [blame] | 1286 | static void clg_start_client_code_callback ( ThreadId tid, ULong blocks_done ) |
sewardj | 9756181 | 2006-12-23 01:21:12 +0000 | [diff] [blame] | 1287 | { |
weidendo | 134657c | 2006-12-23 23:11:20 +0000 | [diff] [blame] | 1288 | static ULong last_blocks_done = 0; |
| 1289 | |
sewardj | 9756181 | 2006-12-23 01:21:12 +0000 | [diff] [blame] | 1290 | if (0) |
njn | 3e32c87 | 2006-12-24 07:51:17 +0000 | [diff] [blame] | 1291 | VG_(printf)("%d R %llu\n", (Int)tid, blocks_done); |
weidendo | 134657c | 2006-12-23 23:11:20 +0000 | [diff] [blame] | 1292 | |
| 1293 | /* throttle calls to CLG_(run_thread) by number of BBs executed */ |
| 1294 | if (blocks_done - last_blocks_done < 5000) return; |
| 1295 | last_blocks_done = blocks_done; |
| 1296 | |
| 1297 | CLG_(run_thread)( tid ); |
sewardj | 9756181 | 2006-12-23 01:21:12 +0000 | [diff] [blame] | 1298 | } |
| 1299 | |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 1300 | static |
| 1301 | void CLG_(post_clo_init)(void) |
| 1302 | { |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 1303 | VG_(clo_vex_control).iropt_unroll_thresh = 0; |
| 1304 | VG_(clo_vex_control).guest_chase_thresh = 0; |
| 1305 | |
| 1306 | CLG_DEBUG(1, " dump threads: %s\n", CLG_(clo).separate_threads ? "Yes":"No"); |
| 1307 | CLG_DEBUG(1, " call sep. : %d\n", CLG_(clo).separate_callers); |
| 1308 | CLG_DEBUG(1, " rec. sep. : %d\n", CLG_(clo).separate_recursions); |
| 1309 | |
| 1310 | if (!CLG_(clo).dump_line && !CLG_(clo).dump_instr && !CLG_(clo).dump_bb) { |
sewardj | 0f33adf | 2009-07-15 14:51:03 +0000 | [diff] [blame] | 1311 | VG_(message)(Vg_UserMsg, "Using source line as position.\n"); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 1312 | CLG_(clo).dump_line = True; |
| 1313 | } |
| 1314 | |
weidendo | 4ce5e79 | 2006-09-20 21:29:39 +0000 | [diff] [blame] | 1315 | CLG_(init_dumps)(); |
| 1316 | CLG_(init_command)(); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 1317 | |
| 1318 | (*CLG_(cachesim).post_clo_init)(); |
| 1319 | |
| 1320 | CLG_(init_eventsets)(0); |
| 1321 | CLG_(init_statistics)(& CLG_(stat)); |
| 1322 | CLG_(init_cost_lz)( CLG_(sets).full, &CLG_(total_cost) ); |
| 1323 | |
| 1324 | /* initialize hash tables */ |
| 1325 | CLG_(init_obj_table)(); |
| 1326 | CLG_(init_cxt_table)(); |
| 1327 | CLG_(init_bb_hash)(); |
| 1328 | |
| 1329 | CLG_(init_threads)(); |
| 1330 | CLG_(run_thread)(1); |
| 1331 | |
| 1332 | CLG_(instrument_state) = CLG_(clo).instrument_atstart; |
| 1333 | |
weidendo | ca472c5 | 2006-03-31 19:34:51 +0000 | [diff] [blame] | 1334 | if (VG_(clo_verbosity > 0)) { |
weidendo | ca472c5 | 2006-03-31 19:34:51 +0000 | [diff] [blame] | 1335 | VG_(message)(Vg_UserMsg, |
sewardj | 0f33adf | 2009-07-15 14:51:03 +0000 | [diff] [blame] | 1336 | "For interactive control, run 'callgrind_control -h'.\n"); |
weidendo | ca472c5 | 2006-03-31 19:34:51 +0000 | [diff] [blame] | 1337 | } |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 1338 | } |
| 1339 | |
| 1340 | static |
| 1341 | void CLG_(pre_clo_init)(void) |
| 1342 | { |
| 1343 | VG_(details_name) ("Callgrind"); |
weidendo | ca472c5 | 2006-03-31 19:34:51 +0000 | [diff] [blame] | 1344 | VG_(details_version) (NULL); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 1345 | VG_(details_description) ("a call-graph generating cache profiler"); |
njn | 9f20746 | 2009-03-10 22:02:09 +0000 | [diff] [blame] | 1346 | VG_(details_copyright_author)("Copyright (C) 2002-2009, and GNU GPL'd, " |
weidendo | ca472c5 | 2006-03-31 19:34:51 +0000 | [diff] [blame] | 1347 | "by Josef Weidendorfer et al."); |
weidendo | db70ed7 | 2006-05-27 15:39:45 +0000 | [diff] [blame] | 1348 | VG_(details_bug_reports_to) (VG_BUGS_TO); |
sewardj | e45a799 | 2006-10-17 02:24:18 +0000 | [diff] [blame] | 1349 | VG_(details_avg_translation_sizeB) ( 500 ); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 1350 | |
| 1351 | VG_(basic_tool_funcs) (CLG_(post_clo_init), |
| 1352 | CLG_(instrument), |
| 1353 | CLG_(fini)); |
| 1354 | |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1355 | VG_(needs_superblock_discards)(clg_discard_superblock_info); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 1356 | |
| 1357 | |
| 1358 | VG_(needs_command_line_options)(CLG_(process_cmd_line_option), |
| 1359 | CLG_(print_usage), |
| 1360 | CLG_(print_debug_usage)); |
| 1361 | |
| 1362 | VG_(needs_client_requests)(CLG_(handle_client_request)); |
| 1363 | VG_(needs_syscall_wrapper)(CLG_(pre_syscalltime), |
| 1364 | CLG_(post_syscalltime)); |
| 1365 | |
njn | 3e32c87 | 2006-12-24 07:51:17 +0000 | [diff] [blame] | 1366 | VG_(track_start_client_code) ( & clg_start_client_code_callback ); |
| 1367 | VG_(track_pre_deliver_signal) ( & CLG_(pre_signal) ); |
| 1368 | VG_(track_post_deliver_signal)( & CLG_(post_signal) ); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 1369 | |
| 1370 | CLG_(set_clo_defaults)(); |
| 1371 | } |
| 1372 | |
| 1373 | VG_DETERMINE_INTERFACE_VERSION(CLG_(pre_clo_init)) |
| 1374 | |
| 1375 | /*--------------------------------------------------------------------*/ |
| 1376 | /*--- end main.c ---*/ |
| 1377 | /*--------------------------------------------------------------------*/ |