weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 1 | /*--------------------------------------------------------------------*/ |
| 2 | /*--- Callgrind ---*/ |
| 3 | /*--- dump.c ---*/ |
| 4 | /*--------------------------------------------------------------------*/ |
| 5 | |
| 6 | /* |
| 7 | This file is part of Callgrind, a Valgrind tool for call tracing. |
| 8 | |
sewardj | 4505790 | 2006-06-05 23:27:18 +0000 | [diff] [blame] | 9 | Copyright (C) 2002-2006, Josef Weidendorfer (Josef.Weidendorfer@gmx.de) |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 10 | |
| 11 | This program is free software; you can redistribute it and/or |
| 12 | modify it under the terms of the GNU General Public License as |
| 13 | published by the Free Software Foundation; either version 2 of the |
| 14 | License, or (at your option) any later version. |
| 15 | |
| 16 | This program is distributed in the hope that it will be useful, but |
| 17 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 19 | General Public License for more details. |
| 20 | |
| 21 | You should have received a copy of the GNU General Public License |
| 22 | along with this program; if not, write to the Free Software |
| 23 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 24 | 02111-1307, USA. |
| 25 | |
| 26 | The GNU General Public License is contained in the file COPYING. |
| 27 | */ |
| 28 | |
| 29 | #include "config.h" |
| 30 | #include "global.h" |
| 31 | |
| 32 | #include <pub_tool_threadstate.h> |
| 33 | #include <pub_tool_libcfile.h> |
| 34 | |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 35 | |
| 36 | /* Dump Part Counter */ |
| 37 | static Int out_counter = 0; |
| 38 | |
| 39 | static Char* dump_file_base = 0; |
| 40 | static Char* base_directory = 0; |
weidendo | 4ce5e79 | 2006-09-20 21:29:39 +0000 | [diff] [blame] | 41 | static Bool dumps_initialized = False; |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 42 | |
| 43 | /* Command */ |
| 44 | static Char cmdbuf[BUF_LEN]; |
| 45 | |
| 46 | /* Total reads/writes/misses sum over all dumps and threads. |
| 47 | * Updated during CC traversal at dump time. |
| 48 | */ |
| 49 | FullCost CLG_(total_cost) = 0; |
| 50 | static FullCost dump_total_cost = 0; |
| 51 | |
| 52 | EventMapping* CLG_(dumpmap) = 0; |
| 53 | |
| 54 | /* Temporary output buffer for |
| 55 | * print_fn_pos, fprint_apos, fprint_fcost, fprint_jcc, |
| 56 | * fprint_fcc_ln, dump_run_info, dump_state_info |
| 57 | */ |
| 58 | static Char outbuf[FILENAME_LEN + FN_NAME_LEN + OBJ_NAME_LEN]; |
| 59 | |
| 60 | Int CLG_(get_dump_counter)(void) |
| 61 | { |
| 62 | return out_counter; |
| 63 | } |
| 64 | |
| 65 | Char* CLG_(get_dump_file_base)() |
| 66 | { |
weidendo | 4ce5e79 | 2006-09-20 21:29:39 +0000 | [diff] [blame] | 67 | CLG_ASSERT(dumps_initialized); |
| 68 | return dump_file_base; |
| 69 | } |
| 70 | |
| 71 | Char* CLG_(get_base_directory)() |
| 72 | { |
| 73 | CLG_ASSERT(dumps_initialized); |
| 74 | return base_directory; |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | /*------------------------------------------------------------*/ |
| 78 | /*--- Output file related stuff ---*/ |
| 79 | /*------------------------------------------------------------*/ |
| 80 | |
| 81 | /* Boolean dumping array */ |
| 82 | static Bool* dump_array = 0; |
| 83 | static Int dump_array_size = 0; |
| 84 | static Bool* obj_dumped = 0; |
| 85 | static Bool* file_dumped = 0; |
| 86 | static Bool* fn_dumped = 0; |
| 87 | static Bool* cxt_dumped = 0; |
| 88 | |
| 89 | static |
| 90 | void reset_dump_array(void) |
| 91 | { |
| 92 | int i; |
| 93 | |
| 94 | CLG_ASSERT(dump_array != 0); |
| 95 | |
| 96 | for(i=0;i<dump_array_size;i++) |
| 97 | dump_array[i] = False; |
| 98 | } |
| 99 | |
| 100 | static |
| 101 | void init_dump_array(void) |
| 102 | { |
| 103 | dump_array_size = CLG_(stat).distinct_objs + |
| 104 | CLG_(stat).distinct_files + |
| 105 | CLG_(stat).distinct_fns + |
| 106 | CLG_(stat).context_counter; |
| 107 | CLG_ASSERT(dump_array == 0); |
| 108 | dump_array = (Bool*) CLG_MALLOC(dump_array_size * sizeof(Bool)); |
| 109 | obj_dumped = dump_array; |
| 110 | file_dumped = obj_dumped + CLG_(stat).distinct_objs; |
| 111 | fn_dumped = file_dumped + CLG_(stat).distinct_files; |
| 112 | cxt_dumped = fn_dumped + CLG_(stat).distinct_fns; |
| 113 | |
| 114 | reset_dump_array(); |
| 115 | |
| 116 | CLG_DEBUG(1, " init_dump_array: size %d\n", dump_array_size); |
| 117 | } |
| 118 | |
| 119 | static __inline__ |
| 120 | void free_dump_array(void) |
| 121 | { |
| 122 | CLG_ASSERT(dump_array != 0); |
| 123 | VG_(free)(dump_array); |
| 124 | |
| 125 | dump_array = 0; |
| 126 | obj_dumped = 0; |
| 127 | file_dumped = 0; |
| 128 | fn_dumped = 0; |
| 129 | cxt_dumped = 0; |
| 130 | } |
| 131 | |
| 132 | |
| 133 | /* Initialize to an invalid position */ |
| 134 | static __inline__ |
| 135 | void init_fpos(FnPos* p) |
| 136 | { |
| 137 | p->file = 0; |
| 138 | p->fn = 0; |
| 139 | p->obj = 0; |
| 140 | p->cxt = 0; |
| 141 | p->rec_index = 0; |
| 142 | } |
| 143 | |
| 144 | |
| 145 | #if 0 |
| 146 | static __inline__ |
| 147 | static void my_fwrite(Int fd, Char* buf, Int len) |
| 148 | { |
| 149 | VG_(write)(fd, (void*)buf, len); |
| 150 | } |
| 151 | #else |
| 152 | |
| 153 | #define FWRITE_BUFSIZE 32000 |
| 154 | #define FWRITE_THROUGH 10000 |
| 155 | static Char fwrite_buf[FWRITE_BUFSIZE]; |
| 156 | static Int fwrite_pos; |
| 157 | static Int fwrite_fd = -1; |
| 158 | |
| 159 | static __inline__ |
| 160 | void fwrite_flush(void) |
| 161 | { |
| 162 | if ((fwrite_fd>=0) && (fwrite_pos>0)) |
| 163 | VG_(write)(fwrite_fd, (void*)fwrite_buf, fwrite_pos); |
| 164 | fwrite_pos = 0; |
| 165 | } |
| 166 | |
| 167 | static void my_fwrite(Int fd, Char* buf, Int len) |
| 168 | { |
| 169 | if (fwrite_fd != fd) { |
| 170 | fwrite_flush(); |
| 171 | fwrite_fd = fd; |
| 172 | } |
| 173 | if (len > FWRITE_THROUGH) { |
| 174 | fwrite_flush(); |
| 175 | VG_(write)(fd, (void*)buf, len); |
| 176 | return; |
| 177 | } |
| 178 | if (FWRITE_BUFSIZE - fwrite_pos <= len) fwrite_flush(); |
| 179 | VG_(strncpy)(fwrite_buf + fwrite_pos, buf, len); |
| 180 | fwrite_pos += len; |
| 181 | } |
| 182 | #endif |
| 183 | |
| 184 | |
| 185 | static void print_obj(Char* buf, obj_node* obj) |
| 186 | { |
| 187 | int n; |
| 188 | |
| 189 | if (CLG_(clo).compress_strings) { |
| 190 | CLG_ASSERT(obj_dumped != 0); |
| 191 | if (obj_dumped[obj->number]) |
| 192 | n = VG_(sprintf)(buf, "(%d)\n", obj->number); |
| 193 | else { |
| 194 | n = VG_(sprintf)(buf, "(%d) %s\n", |
| 195 | obj->number, obj->name); |
| 196 | } |
| 197 | } |
| 198 | else |
| 199 | n = VG_(sprintf)(buf, "%s\n", obj->name); |
| 200 | |
| 201 | #if 0 |
| 202 | /* add mapping parameters the first time a object is dumped |
| 203 | * format: mp=0xSTART SIZE 0xOFFSET */ |
| 204 | if (!obj_dumped[obj->number]) { |
| 205 | obj_dumped[obj->number]; |
| 206 | VG_(sprintf)(buf+n, "mp=%p %p %p\n", |
| 207 | pos->obj->start, pos->obj->size, pos->obj->offset); |
| 208 | } |
| 209 | #else |
| 210 | obj_dumped[obj->number] = True; |
| 211 | #endif |
| 212 | } |
| 213 | |
| 214 | static void print_file(Char* buf, file_node* file) |
| 215 | { |
| 216 | if (CLG_(clo).compress_strings) { |
| 217 | CLG_ASSERT(file_dumped != 0); |
| 218 | if (file_dumped[file->number]) |
| 219 | VG_(sprintf)(buf, "(%d)\n", file->number); |
| 220 | else { |
| 221 | VG_(sprintf)(buf, "(%d) %s\n", |
| 222 | file->number, file->name); |
| 223 | file_dumped[file->number] = True; |
| 224 | } |
| 225 | } |
| 226 | else |
| 227 | VG_(sprintf)(buf, "%s\n", file->name); |
| 228 | } |
| 229 | |
| 230 | /* |
| 231 | * tag can be "fn", "cfn", "jfn" |
| 232 | */ |
| 233 | static void print_fn(Int fd, Char* buf, Char* tag, fn_node* fn) |
| 234 | { |
| 235 | int p; |
| 236 | p = VG_(sprintf)(buf, "%s=",tag); |
| 237 | if (CLG_(clo).compress_strings) { |
| 238 | CLG_ASSERT(fn_dumped != 0); |
| 239 | if (fn_dumped[fn->number]) |
| 240 | p += VG_(sprintf)(buf+p, "(%d)\n", fn->number); |
| 241 | else { |
| 242 | p += VG_(sprintf)(buf+p, "(%d) %s\n", |
| 243 | fn->number, fn->name); |
| 244 | fn_dumped[fn->number] = True; |
| 245 | } |
| 246 | } |
| 247 | else |
| 248 | p += VG_(sprintf)(buf+p, "%s\n", fn->name); |
| 249 | |
| 250 | my_fwrite(fd, buf, p); |
| 251 | } |
| 252 | |
| 253 | static void print_mangled_fn(Int fd, Char* buf, Char* tag, |
| 254 | Context* cxt, int rec_index) |
| 255 | { |
| 256 | int p, i; |
| 257 | |
| 258 | if (CLG_(clo).compress_strings && CLG_(clo).compress_mangled) { |
| 259 | |
| 260 | int n; |
| 261 | Context* last; |
| 262 | |
| 263 | CLG_ASSERT(cxt_dumped != 0); |
| 264 | if (cxt_dumped[cxt->base_number+rec_index]) { |
| 265 | p = VG_(sprintf)(buf, "%s=(%d)\n", |
| 266 | tag, cxt->base_number + rec_index); |
| 267 | my_fwrite(fd, buf, p); |
| 268 | return; |
| 269 | } |
| 270 | |
| 271 | last = 0; |
| 272 | /* make sure that for all context parts compressed data is written */ |
| 273 | for(i=cxt->size;i>0;i--) { |
| 274 | CLG_ASSERT(cxt->fn[i-1]->pure_cxt != 0); |
| 275 | n = cxt->fn[i-1]->pure_cxt->base_number; |
| 276 | if (cxt_dumped[n]) continue; |
| 277 | p = VG_(sprintf)(buf, "%s=(%d) %s\n", |
| 278 | tag, n, cxt->fn[i-1]->name); |
| 279 | my_fwrite(fd, buf, p); |
| 280 | |
| 281 | cxt_dumped[n] = True; |
| 282 | last = cxt->fn[i-1]->pure_cxt; |
| 283 | } |
| 284 | /* If the last context was the context to print, we are finished */ |
| 285 | if ((last == cxt) && (rec_index == 0)) return; |
| 286 | |
| 287 | p = VG_(sprintf)(buf, "%s=(%d) (%d)", tag, |
| 288 | cxt->base_number + rec_index, |
| 289 | cxt->fn[0]->pure_cxt->base_number); |
| 290 | if (rec_index >0) |
| 291 | p += VG_(sprintf)(buf+p, "'%d", rec_index +1); |
| 292 | for(i=1;i<cxt->size;i++) |
| 293 | p += VG_(sprintf)(buf+p, "'(%d)", |
| 294 | cxt->fn[i]->pure_cxt->base_number); |
| 295 | p += VG_(sprintf)(buf+p, "\n"); |
| 296 | my_fwrite(fd, buf, p); |
| 297 | |
| 298 | cxt_dumped[cxt->base_number+rec_index] = True; |
| 299 | return; |
| 300 | } |
| 301 | |
| 302 | |
| 303 | p = VG_(sprintf)(buf, "%s=", tag); |
| 304 | if (CLG_(clo).compress_strings) { |
| 305 | CLG_ASSERT(cxt_dumped != 0); |
| 306 | if (cxt_dumped[cxt->base_number+rec_index]) { |
| 307 | p += VG_(sprintf)(buf+p, "(%d)\n", cxt->base_number + rec_index); |
| 308 | my_fwrite(fd, buf, p); |
| 309 | return; |
| 310 | } |
| 311 | else { |
| 312 | p += VG_(sprintf)(buf+p, "(%d) ", cxt->base_number + rec_index); |
| 313 | cxt_dumped[cxt->base_number+rec_index] = True; |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | p += VG_(sprintf)(buf+p, "%s", cxt->fn[0]->name); |
| 318 | if (rec_index >0) |
| 319 | p += VG_(sprintf)(buf+p, "'%d", rec_index +1); |
| 320 | for(i=1;i<cxt->size;i++) |
| 321 | p += VG_(sprintf)(buf+p, "'%s", cxt->fn[i]->name); |
| 322 | |
| 323 | p += VG_(sprintf)(buf+p, "\n"); |
| 324 | my_fwrite(fd, buf, p); |
| 325 | } |
| 326 | |
| 327 | |
| 328 | |
| 329 | /** |
| 330 | * Print function position of the BBCC, but only print info differing to |
| 331 | * the <last> position, update <last> |
| 332 | * Return True if something changes. |
| 333 | */ |
| 334 | static Bool print_fn_pos(int fd, FnPos* last, BBCC* bbcc) |
| 335 | { |
| 336 | Bool res = False; |
| 337 | |
| 338 | CLG_DEBUGIF(3) { |
| 339 | CLG_DEBUG(2, "+ print_fn_pos: "); |
| 340 | CLG_(print_cxt)(16, bbcc->cxt, bbcc->rec_index); |
| 341 | } |
| 342 | |
| 343 | if (!CLG_(clo).mangle_names) { |
| 344 | if (last->rec_index != bbcc->rec_index) { |
| 345 | VG_(sprintf)(outbuf, "rec=%d\n\n", bbcc->rec_index); |
| 346 | my_fwrite(fd, (void*)outbuf, VG_(strlen)(outbuf)); |
| 347 | last->rec_index = bbcc->rec_index; |
| 348 | last->cxt = 0; /* reprint context */ |
| 349 | res = True; |
| 350 | } |
| 351 | |
| 352 | if (last->cxt != bbcc->cxt) { |
| 353 | fn_node* last_from = (last->cxt && last->cxt->size>1) ? |
| 354 | last->cxt->fn[1] : 0; |
| 355 | fn_node* curr_from = (bbcc->cxt && bbcc->cxt->size>1) ? |
| 356 | bbcc->cxt->fn[1] : 0; |
| 357 | if (curr_from == 0) { |
| 358 | if (last_from != 0) { |
| 359 | /* switch back to no context */ |
| 360 | VG_(sprintf)(outbuf, "frfn=(spontaneous)\n"); |
| 361 | my_fwrite(fd, (void*)outbuf, VG_(strlen)(outbuf)); |
| 362 | res = True; |
| 363 | } |
| 364 | } |
| 365 | else if (last_from != curr_from) { |
| 366 | print_fn(fd,outbuf,"frfn", curr_from); |
| 367 | res = True; |
| 368 | } |
| 369 | last->cxt = bbcc->cxt; |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | if (last->obj != bbcc->cxt->fn[0]->file->obj) { |
| 374 | VG_(sprintf)(outbuf, "ob="); |
| 375 | print_obj(outbuf+3, bbcc->cxt->fn[0]->file->obj); |
| 376 | my_fwrite(fd, (void*)outbuf, VG_(strlen)(outbuf)); |
| 377 | last->obj = bbcc->cxt->fn[0]->file->obj; |
| 378 | res = True; |
| 379 | } |
| 380 | |
| 381 | if (last->file != bbcc->cxt->fn[0]->file) { |
| 382 | VG_(sprintf)(outbuf, "fl="); |
| 383 | print_file(outbuf+3, bbcc->cxt->fn[0]->file); |
| 384 | my_fwrite(fd, (void*)outbuf, VG_(strlen)(outbuf)); |
| 385 | last->file = bbcc->cxt->fn[0]->file; |
| 386 | res = True; |
| 387 | } |
| 388 | |
| 389 | if (!CLG_(clo).mangle_names) { |
| 390 | if (last->fn != bbcc->cxt->fn[0]) { |
| 391 | print_fn(fd,outbuf, "fn", bbcc->cxt->fn[0]); |
| 392 | last->fn = bbcc->cxt->fn[0]; |
| 393 | res = True; |
| 394 | } |
| 395 | } |
| 396 | else { |
| 397 | /* Print mangled name if context or rec_index changes */ |
| 398 | if ((last->rec_index != bbcc->rec_index) || |
| 399 | (last->cxt != bbcc->cxt)) { |
| 400 | |
| 401 | print_mangled_fn(fd, outbuf, "fn", bbcc->cxt, bbcc->rec_index); |
| 402 | last->fn = bbcc->cxt->fn[0]; |
| 403 | last->rec_index = bbcc->rec_index; |
| 404 | res = True; |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | last->cxt = bbcc->cxt; |
| 409 | |
| 410 | CLG_DEBUG(2, "- print_fn_pos: %s\n", res ? "changed" : ""); |
| 411 | |
| 412 | return res; |
| 413 | } |
| 414 | |
| 415 | /* the debug lookup cache is useful if BBCC for same BB are |
| 416 | * dumped directly in a row. This is a direct mapped cache. |
| 417 | */ |
| 418 | #define DEBUG_CACHE_SIZE 1777 |
| 419 | |
| 420 | static Addr debug_cache_addr[DEBUG_CACHE_SIZE]; |
| 421 | static file_node* debug_cache_file[DEBUG_CACHE_SIZE]; |
| 422 | static int debug_cache_line[DEBUG_CACHE_SIZE]; |
| 423 | static Bool debug_cache_info[DEBUG_CACHE_SIZE]; |
| 424 | |
| 425 | static __inline__ |
| 426 | void init_debug_cache(void) |
| 427 | { |
| 428 | int i; |
| 429 | for(i=0;i<DEBUG_CACHE_SIZE;i++) { |
| 430 | debug_cache_addr[i] = 0; |
| 431 | debug_cache_file[i] = 0; |
| 432 | debug_cache_line[i] = 0; |
| 433 | debug_cache_info[i] = 0; |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | static __inline__ |
| 438 | Bool get_debug_pos(BBCC* bbcc, Addr addr, AddrPos* p) |
| 439 | { |
| 440 | Char file[FILENAME_LEN]; |
| 441 | Bool res; |
| 442 | |
| 443 | int cachepos = addr % DEBUG_CACHE_SIZE; |
| 444 | |
| 445 | if (debug_cache_addr[cachepos] == addr) { |
| 446 | p->line = debug_cache_line[cachepos]; |
| 447 | p->file = debug_cache_file[cachepos]; |
| 448 | res = debug_cache_info[cachepos]; |
| 449 | } |
| 450 | else { |
| 451 | res = VG_(get_filename_linenum)(addr, |
| 452 | file, FILENAME_LEN, |
| 453 | NULL, 0, NULL, //FIXME |
| 454 | &(p->line)); |
| 455 | if (!res) { |
| 456 | VG_(strcpy)(file, "???"); |
| 457 | p->line = 0; |
| 458 | } |
| 459 | p->file = CLG_(get_file_node)(bbcc->bb->obj, file); |
| 460 | |
| 461 | debug_cache_info[cachepos] = res; |
| 462 | debug_cache_addr[cachepos] = addr; |
| 463 | debug_cache_line[cachepos] = p->line; |
| 464 | debug_cache_file[cachepos] = p->file; |
| 465 | } |
| 466 | |
| 467 | /* Address offset from bbcc start address */ |
| 468 | p->addr = addr - bbcc->bb->obj->offset; |
| 469 | p->bb_addr = bbcc->bb->offset; |
| 470 | |
| 471 | CLG_DEBUG(3, " get_debug_pos(%p): BB %p, fn '%s', file '%s', line %u\n", |
| 472 | addr, bb_addr(bbcc->bb), bbcc->cxt->fn[0]->name, |
| 473 | p->file->name, p->line); |
| 474 | |
| 475 | return res; |
| 476 | } |
| 477 | |
| 478 | |
| 479 | /* copy file position and init cost */ |
| 480 | static void init_apos(AddrPos* p, Addr addr, Addr bbaddr, file_node* file) |
| 481 | { |
| 482 | p->addr = addr; |
| 483 | p->bb_addr = bbaddr; |
| 484 | p->file = file; |
| 485 | p->line = 0; |
| 486 | } |
| 487 | |
| 488 | static void copy_apos(AddrPos* dst, AddrPos* src) |
| 489 | { |
| 490 | dst->addr = src->addr; |
| 491 | dst->bb_addr = src->bb_addr; |
| 492 | dst->file = src->file; |
| 493 | dst->line = src->line; |
| 494 | } |
| 495 | |
| 496 | /* copy file position and init cost */ |
| 497 | static void init_fcost(AddrCost* c, Addr addr, Addr bbaddr, file_node* file) |
| 498 | { |
| 499 | init_apos( &(c->p), addr, bbaddr, file); |
| 500 | /* FIXME: This is a memory leak as a AddrCost is inited multiple times */ |
| 501 | c->cost = CLG_(get_eventset_cost)( CLG_(sets).full ); |
| 502 | CLG_(init_cost)( CLG_(sets).full, c->cost ); |
| 503 | } |
| 504 | |
| 505 | |
| 506 | /** |
| 507 | * print position change inside of a BB (last -> curr) |
| 508 | * this doesn't update last to curr! |
| 509 | */ |
| 510 | static void fprint_apos(Int fd, AddrPos* curr, AddrPos* last, file_node* func_file) |
| 511 | { |
| 512 | CLG_ASSERT(curr->file != 0); |
| 513 | CLG_DEBUG(2, " print_apos(file '%s', line %d, bb %p, addr %p) fnFile '%s'\n", |
| 514 | curr->file->name, curr->line, curr->bb_addr, curr->addr, |
| 515 | func_file->name); |
| 516 | |
| 517 | if (curr->file != last->file) { |
| 518 | |
| 519 | /* if we switch back to orig file, use fe=... */ |
| 520 | if (curr->file == func_file) |
| 521 | VG_(sprintf)(outbuf, "fe="); |
| 522 | else |
| 523 | VG_(sprintf)(outbuf, "fi="); |
| 524 | print_file(outbuf+3, curr->file); |
| 525 | my_fwrite(fd, (void*)outbuf, VG_(strlen)(outbuf)); |
| 526 | } |
| 527 | |
| 528 | if (CLG_(clo).dump_bbs) { |
| 529 | if (curr->line != last->line) { |
| 530 | VG_(sprintf)(outbuf, "ln=%d\n", curr->line); |
| 531 | my_fwrite(fd, (void*)outbuf, VG_(strlen)(outbuf)); |
| 532 | } |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | |
| 537 | |
| 538 | /** |
| 539 | * Print a position. |
| 540 | * This prints out differences if allowed |
| 541 | * |
| 542 | * This doesn't set last to curr afterwards! |
| 543 | */ |
| 544 | static |
| 545 | void fprint_pos(Int fd, AddrPos* curr, AddrPos* last) |
| 546 | { |
| 547 | if (0) //CLG_(clo).dump_bbs) |
| 548 | VG_(sprintf)(outbuf, "%u ", curr->addr - curr->bb_addr); |
| 549 | else { |
| 550 | int p = 0; |
| 551 | if (CLG_(clo).dump_instr) { |
| 552 | int diff = curr->addr - last->addr; |
| 553 | if ( CLG_(clo).compress_pos && (last->addr >0) && |
| 554 | (diff > -100) && (diff < 100)) { |
| 555 | if (diff >0) |
| 556 | p = VG_(sprintf)(outbuf, "+%d ", diff); |
| 557 | else if (diff==0) |
| 558 | p = VG_(sprintf)(outbuf, "* "); |
| 559 | else |
| 560 | p = VG_(sprintf)(outbuf, "%d ", diff); |
| 561 | } |
| 562 | else |
| 563 | p = VG_(sprintf)(outbuf, "%p ", curr->addr); |
| 564 | } |
| 565 | |
| 566 | if (CLG_(clo).dump_bb) { |
| 567 | int diff = curr->bb_addr - last->bb_addr; |
| 568 | if ( CLG_(clo).compress_pos && (last->bb_addr >0) && |
| 569 | (diff > -100) && (diff < 100)) { |
| 570 | if (diff >0) |
| 571 | p += VG_(sprintf)(outbuf+p, "+%d ", diff); |
| 572 | else if (diff==0) |
| 573 | p += VG_(sprintf)(outbuf+p, "* "); |
| 574 | else |
| 575 | p += VG_(sprintf)(outbuf+p, "%d ", diff); |
| 576 | } |
| 577 | else |
| 578 | p += VG_(sprintf)(outbuf+p, "%p ", curr->bb_addr); |
| 579 | } |
| 580 | |
| 581 | if (CLG_(clo).dump_line) { |
| 582 | int diff = curr->line - last->line; |
| 583 | if ( CLG_(clo).compress_pos && (last->line >0) && |
| 584 | (diff > -100) && (diff < 100)) { |
| 585 | |
| 586 | if (diff >0) |
| 587 | VG_(sprintf)(outbuf+p, "+%d ", diff); |
| 588 | else if (diff==0) |
| 589 | VG_(sprintf)(outbuf+p, "* "); |
| 590 | else |
| 591 | VG_(sprintf)(outbuf+p, "%d ", diff); |
| 592 | } |
| 593 | else |
| 594 | VG_(sprintf)(outbuf+p, "%u ", curr->line); |
| 595 | } |
| 596 | } |
| 597 | my_fwrite(fd, (void*)outbuf, VG_(strlen)(outbuf)); |
| 598 | } |
| 599 | |
| 600 | |
| 601 | /** |
| 602 | * Print events. |
| 603 | */ |
| 604 | |
| 605 | static |
| 606 | void fprint_cost(int fd, EventMapping* es, ULong* cost) |
| 607 | { |
| 608 | int p = CLG_(sprint_mappingcost)(outbuf, es, cost); |
| 609 | VG_(sprintf)(outbuf+p, "\n"); |
| 610 | my_fwrite(fd, (void*)outbuf, VG_(strlen)(outbuf)); |
| 611 | return; |
| 612 | } |
| 613 | |
| 614 | |
| 615 | |
| 616 | /* Write the cost of a source line; only that parts of the source |
| 617 | * position are written that changed relative to last written position. |
| 618 | * funcPos is the source position of the first line of actual function. |
| 619 | * Something is written only if cost != 0; returns True in this case. |
| 620 | */ |
| 621 | static void fprint_fcost(Int fd, AddrCost* c, AddrPos* last) |
| 622 | { |
| 623 | CLG_DEBUGIF(3) { |
| 624 | CLG_DEBUG(2, " print_fcost(file '%s', line %d, bb %p, addr %p):\n", |
| 625 | c->p.file->name, c->p.line, c->p.bb_addr, c->p.addr); |
| 626 | CLG_(print_cost)(-5, CLG_(sets).full, c->cost); |
| 627 | } |
| 628 | |
| 629 | fprint_pos(fd, &(c->p), last); |
| 630 | copy_apos( last, &(c->p) ); /* update last to current position */ |
| 631 | |
| 632 | fprint_cost(fd, CLG_(dumpmap), c->cost); |
| 633 | |
| 634 | /* add cost to total */ |
| 635 | CLG_(add_and_zero_cost)( CLG_(sets).full, dump_total_cost, c->cost ); |
| 636 | } |
| 637 | |
| 638 | |
| 639 | /* Write out the calls from jcc (at pos) |
| 640 | */ |
| 641 | static void fprint_jcc(Int fd, jCC* jcc, AddrPos* curr, AddrPos* last, ULong ecounter) |
| 642 | { |
| 643 | static AddrPos target; |
| 644 | file_node* file; |
| 645 | obj_node* obj; |
| 646 | |
| 647 | CLG_DEBUGIF(2) { |
| 648 | CLG_DEBUG(2, " fprint_jcc (jkind %d)\n", jcc->jmpkind); |
| 649 | CLG_(print_jcc)(-10, jcc); |
| 650 | } |
| 651 | |
| 652 | if (!get_debug_pos(jcc->to, bb_addr(jcc->to->bb), &target)) { |
| 653 | /* if we don't have debug info, don't switch to file "???" */ |
| 654 | target.file = last->file; |
| 655 | } |
| 656 | |
| 657 | if (jcc->from && |
| 658 | (jcc->jmpkind == JmpCond || jcc->jmpkind == Ijk_Boring)) { |
| 659 | |
| 660 | /* this is a JCC for a followed conditional or boring jump. */ |
| 661 | CLG_ASSERT(CLG_(is_zero_cost)( CLG_(sets).full, jcc->cost)); |
| 662 | |
| 663 | /* objects among jumps should be the same. |
| 664 | * Otherwise this jump would have been changed to a call |
| 665 | * (see setup_bbcc) |
| 666 | */ |
| 667 | CLG_ASSERT(jcc->from->bb->obj == jcc->to->bb->obj); |
| 668 | |
| 669 | /* only print if target position info is usefull */ |
| 670 | if (!CLG_(clo).dump_instr && !CLG_(clo).dump_bb && target.line==0) { |
| 671 | jcc->call_counter = 0; |
| 672 | return; |
| 673 | } |
| 674 | |
| 675 | /* Different files/functions are possible e.g. with longjmp's |
| 676 | * which change the stack, and thus context |
| 677 | */ |
| 678 | if (last->file != target.file) { |
| 679 | VG_(sprintf)(outbuf, "jfi="); |
| 680 | print_file(outbuf+4, target.file); |
| 681 | my_fwrite(fd, (void*)outbuf, VG_(strlen)(outbuf)); |
| 682 | } |
| 683 | |
| 684 | if (jcc->from->cxt != jcc->to->cxt) { |
| 685 | if (CLG_(clo).mangle_names) |
| 686 | print_mangled_fn(fd, outbuf, "jfn", |
| 687 | jcc->to->cxt, jcc->to->rec_index); |
| 688 | else |
| 689 | print_fn(fd, outbuf, "jfn", jcc->to->cxt->fn[0]); |
| 690 | } |
| 691 | |
| 692 | if (jcc->jmpkind == JmpCond) { |
| 693 | /* format: jcnd=<followed>/<executions> <target> */ |
| 694 | VG_(sprintf)(outbuf, "jcnd=%llu/%llu ", |
| 695 | jcc->call_counter, ecounter); |
| 696 | } |
| 697 | else { |
| 698 | /* format: jump=<jump count> <target> */ |
| 699 | VG_(sprintf)(outbuf, "jump=%llu ", |
| 700 | jcc->call_counter); |
| 701 | } |
| 702 | my_fwrite(fd, (void*)outbuf, VG_(strlen)(outbuf)); |
| 703 | |
| 704 | fprint_pos(fd, &target, last); |
| 705 | my_fwrite(fd, "\n", 1); |
| 706 | fprint_pos(fd, curr, last); |
| 707 | my_fwrite(fd, "\n", 1); |
| 708 | |
| 709 | jcc->call_counter = 0; |
| 710 | return; |
| 711 | } |
| 712 | |
| 713 | CLG_ASSERT(jcc->to !=0); |
| 714 | |
| 715 | file = jcc->to->cxt->fn[0]->file; |
| 716 | obj = jcc->to->bb->obj; |
| 717 | |
| 718 | /* object of called position different to object of this function?*/ |
| 719 | if (jcc->from->cxt->fn[0]->file->obj != obj) { |
| 720 | VG_(sprintf)(outbuf, "cob="); |
| 721 | print_obj(outbuf+4, obj); |
| 722 | my_fwrite(fd, (void*)outbuf, VG_(strlen)(outbuf)); |
| 723 | } |
| 724 | |
| 725 | /* file of called position different to current file? */ |
| 726 | if (last->file != file) { |
| 727 | VG_(sprintf)(outbuf, "cfi="); |
| 728 | print_file(outbuf+4, file); |
| 729 | my_fwrite(fd, (void*)outbuf, VG_(strlen)(outbuf)); |
| 730 | } |
| 731 | |
| 732 | if (CLG_(clo).mangle_names) |
| 733 | print_mangled_fn(fd, outbuf, "cfn", jcc->to->cxt, jcc->to->rec_index); |
| 734 | else |
| 735 | print_fn(fd, outbuf, "cfn", jcc->to->cxt->fn[0]); |
| 736 | |
| 737 | if (!CLG_(is_zero_cost)( CLG_(sets).full, jcc->cost)) { |
| 738 | VG_(sprintf)(outbuf, "calls=%llu ", |
| 739 | jcc->call_counter); |
| 740 | my_fwrite(fd, (void*)outbuf, VG_(strlen)(outbuf)); |
| 741 | |
| 742 | fprint_pos(fd, &target, last); |
| 743 | my_fwrite(fd, "\n", 1); |
| 744 | fprint_pos(fd, curr, last); |
| 745 | fprint_cost(fd, CLG_(dumpmap), jcc->cost); |
| 746 | |
| 747 | CLG_(init_cost)( CLG_(sets).full, jcc->cost ); |
| 748 | |
| 749 | jcc->call_counter = 0; |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | |
| 754 | |
| 755 | /* Cost summation of functions.We use alternately ccSum[0/1], thus |
| 756 | * ssSum[currSum] for recently read lines with same line number. |
| 757 | */ |
| 758 | static AddrCost ccSum[2]; |
| 759 | static int currSum; |
| 760 | |
| 761 | /* |
| 762 | * Print all costs of a BBCC: |
| 763 | * - FCCs of instructions |
| 764 | * - JCCs of the unique jump of this BB |
| 765 | * returns True if something was written |
| 766 | */ |
| 767 | static Bool fprint_bbcc(Int fd, BBCC* bbcc, AddrPos* last) |
| 768 | { |
| 769 | InstrInfo* instr_info; |
| 770 | ULong ecounter; |
| 771 | Bool something_written = False; |
| 772 | jCC* jcc; |
| 773 | AddrCost *currCost, *newCost; |
| 774 | Int jcc_count = 0, instr, i, jmp; |
| 775 | BB* bb = bbcc->bb; |
| 776 | |
| 777 | CLG_ASSERT(bbcc->cxt != 0); |
| 778 | CLG_DEBUGIF(1) { |
| 779 | VG_(printf)("+ fprint_bbcc (Instr %d): ", bb->instr_count); |
| 780 | CLG_(print_bbcc)(15, bbcc, False); |
| 781 | } |
| 782 | |
| 783 | CLG_ASSERT(currSum == 0 || currSum == 1); |
| 784 | currCost = &(ccSum[currSum]); |
| 785 | newCost = &(ccSum[1-currSum]); |
| 786 | |
| 787 | ecounter = bbcc->ecounter_sum; |
| 788 | jmp = 0; |
| 789 | instr_info = &(bb->instr[0]); |
| 790 | for(instr=0; instr<bb->instr_count; instr++, instr_info++) { |
| 791 | |
| 792 | /* get debug info of current instruction address and dump cost |
| 793 | * if CLG_(clo).dump_bbs or file/line has changed |
| 794 | */ |
| 795 | if (!get_debug_pos(bbcc, bb_addr(bb) + instr_info->instr_offset, |
| 796 | &(newCost->p))) { |
| 797 | /* if we don't have debug info, don't switch to file "???" */ |
| 798 | newCost->p.file = bbcc->cxt->fn[0]->file; |
| 799 | } |
| 800 | |
| 801 | if (CLG_(clo).dump_bbs || CLG_(clo).dump_instr || |
| 802 | (newCost->p.line != currCost->p.line) || |
| 803 | (newCost->p.file != currCost->p.file)) { |
| 804 | |
| 805 | if (!CLG_(is_zero_cost)( CLG_(sets).full, currCost->cost )) { |
| 806 | something_written = True; |
| 807 | |
| 808 | fprint_apos(fd, &(currCost->p), last, bbcc->cxt->fn[0]->file); |
| 809 | fprint_fcost(fd, currCost, last); |
| 810 | } |
| 811 | |
| 812 | /* switch buffers */ |
| 813 | currSum = 1 - currSum; |
| 814 | currCost = &(ccSum[currSum]); |
| 815 | newCost = &(ccSum[1-currSum]); |
| 816 | } |
| 817 | |
| 818 | /* add line cost to current cost sum */ |
| 819 | (*CLG_(cachesim).add_icost)(currCost->cost, bbcc, instr_info, ecounter); |
| 820 | |
| 821 | /* print jcc's if there are: only jumps */ |
| 822 | if (bb->jmp[jmp].instr == instr) { |
| 823 | jcc_count=0; |
| 824 | for(jcc=bbcc->jmp[jmp].jcc_list; jcc; jcc=jcc->next_from) |
| 825 | if ((jcc->jmpkind != Ijk_Call) && (jcc->call_counter >0)) |
| 826 | jcc_count++; |
| 827 | |
| 828 | if (jcc_count>0) { |
| 829 | if (!CLG_(is_zero_cost)( CLG_(sets).full, currCost->cost )) { |
| 830 | /* no need to switch buffers, as position is the same */ |
| 831 | fprint_apos(fd, &(currCost->p), last, bbcc->cxt->fn[0]->file); |
| 832 | fprint_fcost(fd, currCost, last); |
| 833 | } |
| 834 | get_debug_pos(bbcc, bb_addr(bb)+instr_info->instr_offset, &(currCost->p)); |
| 835 | fprint_apos(fd, &(currCost->p), last, bbcc->cxt->fn[0]->file); |
| 836 | something_written = True; |
| 837 | for(jcc=bbcc->jmp[jmp].jcc_list; jcc; jcc=jcc->next_from) { |
| 838 | if ((jcc->jmpkind != Ijk_Call) && (jcc->call_counter >0)) |
| 839 | fprint_jcc(fd, jcc, &(currCost->p), last, ecounter); |
| 840 | } |
| 841 | } |
| 842 | } |
| 843 | |
| 844 | /* update execution counter */ |
| 845 | if (jmp < bb->cjmp_count) |
| 846 | if (bb->jmp[jmp].instr == instr) { |
| 847 | ecounter -= bbcc->jmp[jmp].ecounter; |
| 848 | jmp++; |
| 849 | } |
| 850 | } |
| 851 | |
| 852 | /* jCCs at end? If yes, dump cumulated line info first */ |
| 853 | jcc_count = 0; |
| 854 | for(jcc=bbcc->jmp[jmp].jcc_list; jcc; jcc=jcc->next_from) { |
| 855 | /* yes, if JCC only counts jmp arcs or cost >0 */ |
| 856 | if ( ((jcc->jmpkind != Ijk_Call) && (jcc->call_counter >0)) || |
| 857 | (!CLG_(is_zero_cost)( CLG_(sets).full, jcc->cost ))) |
| 858 | jcc_count++; |
| 859 | } |
| 860 | |
| 861 | if ( (bbcc->skipped && |
| 862 | !CLG_(is_zero_cost)(CLG_(sets).full, bbcc->skipped)) || |
| 863 | (jcc_count>0) ) { |
| 864 | |
| 865 | if (!CLG_(is_zero_cost)( CLG_(sets).full, currCost->cost )) { |
| 866 | /* no need to switch buffers, as position is the same */ |
| 867 | fprint_apos(fd, &(currCost->p), last, bbcc->cxt->fn[0]->file); |
| 868 | fprint_fcost(fd, currCost, last); |
| 869 | } |
| 870 | |
| 871 | get_debug_pos(bbcc, bb_jmpaddr(bb), &(currCost->p)); |
| 872 | fprint_apos(fd, &(currCost->p), last, bbcc->cxt->fn[0]->file); |
| 873 | something_written = True; |
| 874 | |
| 875 | /* first, print skipped costs for calls */ |
| 876 | if (bbcc->skipped && !CLG_(is_zero_cost)( CLG_(sets).full, |
| 877 | bbcc->skipped )) { |
| 878 | CLG_(add_and_zero_cost)( CLG_(sets).full, |
| 879 | currCost->cost, bbcc->skipped ); |
| 880 | #if 0 |
| 881 | VG_(sprintf)(outbuf, "# Skipped\n"); |
| 882 | my_fwrite(fd, (void*)outbuf, VG_(strlen)(outbuf)); |
| 883 | #endif |
| 884 | fprint_fcost(fd, currCost, last); |
| 885 | } |
| 886 | |
| 887 | if (jcc_count > 0) |
| 888 | for(jcc=bbcc->jmp[jmp].jcc_list; jcc; jcc=jcc->next_from) { |
| 889 | CLG_ASSERT(jcc->jmp == jmp); |
| 890 | if ( ((jcc->jmpkind != Ijk_Call) && (jcc->call_counter >0)) || |
| 891 | (!CLG_(is_zero_cost)( CLG_(sets).full, jcc->cost ))) |
| 892 | |
| 893 | fprint_jcc(fd, jcc, &(currCost->p), last, ecounter); |
| 894 | } |
| 895 | } |
| 896 | |
| 897 | if (CLG_(clo).dump_bbs || CLG_(clo).dump_bb) { |
| 898 | if (!CLG_(is_zero_cost)( CLG_(sets).full, currCost->cost )) { |
| 899 | something_written = True; |
| 900 | |
| 901 | fprint_apos(fd, &(currCost->p), last, bbcc->cxt->fn[0]->file); |
| 902 | fprint_fcost(fd, currCost, last); |
| 903 | } |
| 904 | if (CLG_(clo).dump_bbs) my_fwrite(fd, (void*)"\n", 1); |
| 905 | |
| 906 | /* when every cost was immediatly written, we must have done so, |
| 907 | * as this function is only called when there's cost in a BBCC |
| 908 | */ |
| 909 | CLG_ASSERT(something_written); |
| 910 | } |
| 911 | |
| 912 | bbcc->ecounter_sum = 0; |
| 913 | for(i=0; i<=bbcc->bb->cjmp_count; i++) |
| 914 | bbcc->jmp[i].ecounter = 0; |
| 915 | bbcc->ret_counter = 0; |
| 916 | |
| 917 | CLG_DEBUG(1, "- fprint_bbcc: JCCs %d\n", jcc_count); |
| 918 | |
| 919 | return something_written; |
| 920 | } |
| 921 | |
| 922 | /* order by |
| 923 | * recursion, |
| 924 | * from->bb->obj, from->bb->fn |
| 925 | * obj, fn[0]->file, fn |
| 926 | * address |
| 927 | */ |
| 928 | static int my_cmp(BBCC** pbbcc1, BBCC** pbbcc2) |
| 929 | { |
| 930 | #if 0 |
| 931 | return (*pbbcc1)->bb->offset - (*pbbcc2)->bb->offset; |
| 932 | #else |
| 933 | BBCC *bbcc1 = *pbbcc1; |
| 934 | BBCC *bbcc2 = *pbbcc2; |
| 935 | Context* cxt1 = bbcc1->cxt; |
| 936 | Context* cxt2 = bbcc2->cxt; |
| 937 | int off = 1; |
| 938 | |
| 939 | if (cxt1->fn[0]->file->obj != cxt2->fn[0]->file->obj) |
| 940 | return cxt1->fn[0]->file->obj - cxt2->fn[0]->file->obj; |
| 941 | |
| 942 | if (cxt1->fn[0]->file != cxt2->fn[0]->file) |
| 943 | return cxt1->fn[0]->file - cxt2->fn[0]->file; |
| 944 | |
| 945 | if (cxt1->fn[0] != cxt2->fn[0]) |
| 946 | return cxt1->fn[0] - cxt2->fn[0]; |
| 947 | |
| 948 | if (bbcc1->rec_index != bbcc2->rec_index) |
| 949 | return bbcc1->rec_index - bbcc2->rec_index; |
| 950 | |
| 951 | while((off < cxt1->size) && (off < cxt2->size)) { |
| 952 | fn_node* ffn1 = cxt1->fn[off]; |
| 953 | fn_node* ffn2 = cxt2->fn[off]; |
| 954 | if (ffn1->file->obj != ffn2->file->obj) |
| 955 | return ffn1->file->obj - ffn2->file->obj; |
| 956 | if (ffn1 != ffn2) |
| 957 | return ffn1 - ffn2; |
| 958 | off++; |
| 959 | } |
| 960 | if (cxt1->size > cxt2->size) return 1; |
| 961 | else if (cxt1->size < cxt2->size) return -1; |
| 962 | |
| 963 | return bbcc1->bb->offset - bbcc2->bb->offset; |
| 964 | #endif |
| 965 | } |
| 966 | |
| 967 | |
| 968 | |
| 969 | |
| 970 | |
| 971 | /* modified version of: |
| 972 | * |
| 973 | * qsort -- qsort interface implemented by faster quicksort. |
| 974 | * J. L. Bentley and M. D. McIlroy, SPE 23 (1993) 1249-1265. |
| 975 | * Copyright 1993, John Wiley. |
| 976 | */ |
| 977 | |
| 978 | static __inline__ |
| 979 | void swapfunc(BBCC** a, BBCC** b, int n) |
| 980 | { |
| 981 | while(n>0) { |
| 982 | BBCC* t = *a; *a = *b; *b = t; |
| 983 | a++, b++; |
| 984 | n--; |
| 985 | } |
| 986 | } |
| 987 | |
| 988 | static __inline__ |
| 989 | void swap(BBCC** a, BBCC** b) |
| 990 | { |
| 991 | BBCC* t; |
| 992 | t = *a; *a = *b; *b = t; |
| 993 | } |
| 994 | |
| 995 | #define min(x, y) ((x)<=(y) ? (x) : (y)) |
| 996 | |
| 997 | static |
| 998 | BBCC** med3(BBCC **a, BBCC **b, BBCC **c, int (*cmp)(BBCC**,BBCC**)) |
| 999 | { return cmp(a, b) < 0 ? |
| 1000 | (cmp(b, c) < 0 ? b : cmp(a, c) < 0 ? c : a) |
| 1001 | : (cmp(b, c) > 0 ? b : cmp(a, c) > 0 ? c : a); |
| 1002 | } |
| 1003 | |
| 1004 | static BBCC** qsort_start = 0; |
| 1005 | |
| 1006 | static void qsort(BBCC **a, int n, int (*cmp)(BBCC**,BBCC**)) |
| 1007 | { |
| 1008 | BBCC **pa, **pb, **pc, **pd, **pl, **pm, **pn, **pv; |
| 1009 | int s, r; |
| 1010 | BBCC* v; |
| 1011 | |
| 1012 | CLG_DEBUG(8, " qsort(%d,%d)\n", a-qsort_start, n); |
| 1013 | |
| 1014 | if (n < 7) { /* Insertion sort on smallest arrays */ |
| 1015 | for (pm = a+1; pm < a+n; pm++) |
| 1016 | for (pl = pm; pl > a && cmp(pl-1, pl) > 0; pl --) |
| 1017 | swap(pl, pl-1); |
| 1018 | |
| 1019 | CLG_DEBUGIF(8) { |
| 1020 | for (pm = a; pm < a+n; pm++) { |
| 1021 | VG_(printf)(" %3d BB %p, ", pm - qsort_start, |
| 1022 | bb_addr((*pm)->bb)); |
| 1023 | CLG_(print_cxt)(9, (*pm)->cxt, (*pm)->rec_index); |
| 1024 | } |
| 1025 | } |
| 1026 | return; |
| 1027 | } |
| 1028 | pm = a + n/2; /* Small arrays, middle element */ |
| 1029 | if (n > 7) { |
| 1030 | pl = a; |
| 1031 | pn = a + (n-1); |
| 1032 | if (n > 40) { /* Big arrays, pseudomedian of 9 */ |
| 1033 | s = n/8; |
| 1034 | pl = med3(pl, pl+s, pl+2*s, cmp); |
| 1035 | pm = med3(pm-s, pm, pm+s, cmp); |
| 1036 | pn = med3(pn-2*s, pn-s, pn, cmp); |
| 1037 | } |
| 1038 | pm = med3(pl, pm, pn, cmp); /* Mid-size, med of 3 */ |
| 1039 | } |
| 1040 | |
| 1041 | |
| 1042 | v = *pm; |
| 1043 | pv = &v; |
| 1044 | pa = pb = a; |
| 1045 | pc = pd = a + (n-1); |
| 1046 | for (;;) { |
| 1047 | while ((pb <= pc) && ((r=cmp(pb, pv)) <= 0)) { |
| 1048 | if (r==0) { |
| 1049 | /* same as pivot, to start */ |
| 1050 | swap(pa,pb); pa++; |
| 1051 | } |
| 1052 | pb ++; |
| 1053 | } |
| 1054 | while ((pb <= pc) && ((r=cmp(pc, pv)) >= 0)) { |
| 1055 | if (r==0) { |
| 1056 | /* same as pivot, to end */ |
| 1057 | swap(pc,pd); pd--; |
| 1058 | } |
| 1059 | pc --; |
| 1060 | } |
| 1061 | if (pb > pc) { break; } |
| 1062 | swap(pb, pc); |
| 1063 | pb ++; |
| 1064 | pc --; |
| 1065 | } |
| 1066 | pb--; |
| 1067 | pc++; |
| 1068 | |
| 1069 | /* put pivot from start into middle */ |
| 1070 | if ((s = pa-a)>0) { for(r=0;r<s;r++) swap(a+r, pb+1-s+r); } |
| 1071 | /* put pivot from end into middle */ |
| 1072 | if ((s = a+n-1-pd)>0) { for(r=0;r<s;r++) swap(pc+r, a+n-s+r); } |
| 1073 | |
| 1074 | CLG_DEBUGIF(8) { |
| 1075 | VG_(printf)(" PV BB %p, ", bb_addr((*pv)->bb)); |
| 1076 | CLG_(print_cxt)(9, (*pv)->cxt, (*pv)->rec_index); |
| 1077 | |
| 1078 | s = pb-pa+1; |
| 1079 | VG_(printf)(" Lower %d - %d:\n", a-qsort_start, a+s-1-qsort_start); |
| 1080 | for (r=0;r<s;r++) { |
| 1081 | pm = a+r; |
| 1082 | VG_(printf)(" %3d BB %p, ", |
| 1083 | pm-qsort_start,bb_addr((*pm)->bb)); |
| 1084 | CLG_(print_cxt)(9, (*pm)->cxt, (*pm)->rec_index); |
| 1085 | } |
| 1086 | |
| 1087 | s = pd-pc+1; |
| 1088 | VG_(printf)(" Upper %d - %d:\n", |
| 1089 | a+n-s-qsort_start, a+n-1-qsort_start); |
| 1090 | for (r=0;r<s;r++) { |
| 1091 | pm = a+n-s+r; |
| 1092 | VG_(printf)(" %3d BB %p, ", |
| 1093 | pm-qsort_start,bb_addr((*pm)->bb)); |
| 1094 | CLG_(print_cxt)(9, (*pm)->cxt, (*pm)->rec_index); |
| 1095 | } |
| 1096 | } |
| 1097 | |
| 1098 | if ((s = pb+1-pa) > 1) qsort(a, s, cmp); |
| 1099 | if ((s = pd+1-pc) > 1) qsort(a+n-s, s, cmp); |
| 1100 | } |
| 1101 | |
| 1102 | |
| 1103 | /* Helpers for prepare_dump */ |
| 1104 | |
| 1105 | static Int prepare_count; |
| 1106 | static BBCC** prepare_ptr; |
| 1107 | |
| 1108 | |
| 1109 | static void hash_addCount(BBCC* bbcc) |
| 1110 | { |
| 1111 | if ((bbcc->ecounter_sum > 0) || (bbcc->ret_counter>0)) |
| 1112 | prepare_count++; |
| 1113 | } |
| 1114 | |
| 1115 | static void hash_addPtr(BBCC* bbcc) |
| 1116 | { |
| 1117 | if ((bbcc->ecounter_sum == 0) && |
| 1118 | (bbcc->ret_counter == 0)) return; |
| 1119 | |
| 1120 | *prepare_ptr = bbcc; |
| 1121 | prepare_ptr++; |
| 1122 | } |
| 1123 | |
| 1124 | |
| 1125 | static void cs_addCount(thread_info* ti) |
| 1126 | { |
| 1127 | Int i; |
| 1128 | BBCC* bbcc; |
| 1129 | |
| 1130 | /* add BBCCs with active call in call stack of current thread. |
| 1131 | * update cost sums for active calls |
| 1132 | */ |
| 1133 | |
| 1134 | for(i = 0; i < CLG_(current_call_stack).sp; i++) { |
| 1135 | call_entry* e = &(CLG_(current_call_stack).entry[i]); |
| 1136 | if (e->jcc == 0) continue; |
| 1137 | |
| 1138 | CLG_(add_diff_cost_lz)( CLG_(sets).full, &(e->jcc->cost), |
| 1139 | e->enter_cost, CLG_(current_state).cost); |
| 1140 | bbcc = e->jcc->from; |
| 1141 | |
| 1142 | CLG_DEBUG(1, " [%2d] (tid %d), added active: %s\n", |
| 1143 | i,CLG_(current_tid),bbcc->cxt->fn[0]->name); |
| 1144 | |
| 1145 | if (bbcc->ecounter_sum>0 || bbcc->ret_counter>0) { |
| 1146 | /* already counted */ |
| 1147 | continue; |
| 1148 | } |
| 1149 | prepare_count++; |
| 1150 | } |
| 1151 | } |
| 1152 | |
| 1153 | static void cs_addPtr(thread_info* ti) |
| 1154 | { |
| 1155 | Int i; |
| 1156 | BBCC* bbcc; |
| 1157 | |
| 1158 | /* add BBCCs with active call in call stack of current thread. |
| 1159 | * update cost sums for active calls |
| 1160 | */ |
| 1161 | |
| 1162 | for(i = 0; i < CLG_(current_call_stack).sp; i++) { |
| 1163 | call_entry* e = &(CLG_(current_call_stack).entry[i]); |
| 1164 | if (e->jcc == 0) continue; |
| 1165 | |
| 1166 | bbcc = e->jcc->from; |
| 1167 | |
| 1168 | if (bbcc->ecounter_sum>0 || bbcc->ret_counter>0) { |
| 1169 | /* already counted */ |
| 1170 | continue; |
| 1171 | } |
| 1172 | |
| 1173 | *prepare_ptr = bbcc; |
| 1174 | prepare_ptr++; |
| 1175 | } |
| 1176 | } |
| 1177 | |
| 1178 | |
| 1179 | /** |
| 1180 | * Put all BBCCs with costs into a sorted array. |
| 1181 | * The returned arrays ends with a null pointer. |
| 1182 | * Must be freed after dumping. |
| 1183 | */ |
| 1184 | static |
| 1185 | BBCC** prepare_dump(void) |
| 1186 | { |
| 1187 | BBCC **array; |
| 1188 | |
| 1189 | prepare_count = 0; |
| 1190 | |
| 1191 | /* if we do not separate among threads, this gives all */ |
| 1192 | /* count number of BBCCs with >0 executions */ |
| 1193 | CLG_(forall_bbccs)(hash_addCount); |
| 1194 | |
| 1195 | /* even if we do not separate among threads, |
| 1196 | * call stacks are separated */ |
| 1197 | if (CLG_(clo).separate_threads) |
| 1198 | cs_addCount(0); |
| 1199 | else |
| 1200 | CLG_(forall_threads)(cs_addCount); |
| 1201 | |
| 1202 | CLG_DEBUG(0, "prepare_dump: %d BBCCs\n", prepare_count); |
| 1203 | |
| 1204 | /* allocate bbcc array, insert BBCCs and sort */ |
| 1205 | prepare_ptr = array = |
| 1206 | (BBCC**) CLG_MALLOC((prepare_count+1) * sizeof(BBCC*)); |
| 1207 | |
| 1208 | CLG_(forall_bbccs)(hash_addPtr); |
| 1209 | |
| 1210 | if (CLG_(clo).separate_threads) |
| 1211 | cs_addPtr(0); |
| 1212 | else |
| 1213 | CLG_(forall_threads)(cs_addPtr); |
| 1214 | |
| 1215 | CLG_ASSERT(array + prepare_count == prepare_ptr); |
| 1216 | |
| 1217 | /* end mark */ |
| 1218 | *prepare_ptr = 0; |
| 1219 | |
| 1220 | CLG_DEBUG(0," BBCCs inserted\n"); |
| 1221 | |
| 1222 | qsort_start = array; |
| 1223 | qsort(array, prepare_count, my_cmp); |
| 1224 | |
| 1225 | CLG_DEBUG(0," BBCCs sorted\n"); |
| 1226 | |
| 1227 | return array; |
| 1228 | } |
| 1229 | |
| 1230 | |
| 1231 | |
| 1232 | |
| 1233 | static void fprint_cost_ln(int fd, Char* prefix, |
| 1234 | EventMapping* em, ULong* cost) |
| 1235 | { |
| 1236 | int p; |
| 1237 | |
| 1238 | p = VG_(sprintf)(outbuf, "%s", prefix); |
| 1239 | p += CLG_(sprint_mappingcost)(outbuf + p, em, cost); |
| 1240 | VG_(sprintf)(outbuf + p, "\n"); |
| 1241 | my_fwrite(fd, (void*)outbuf, VG_(strlen)(outbuf)); |
| 1242 | } |
| 1243 | |
| 1244 | static ULong bbs_done = 0; |
| 1245 | static Char* filename = 0; |
| 1246 | |
| 1247 | static |
| 1248 | void file_err(void) |
| 1249 | { |
| 1250 | VG_(message)(Vg_UserMsg, |
| 1251 | "Error: can not open cache simulation output file `%s'", |
| 1252 | filename ); |
| 1253 | VG_(exit)(1); |
| 1254 | } |
| 1255 | |
| 1256 | /** |
| 1257 | * Create a new dump file and write header. |
| 1258 | * |
| 1259 | * Naming: <CLG_(clo).filename_base>.<pid>[.<part>][-<tid>] |
| 1260 | * <part> is skipped for final dump (trigger==0) |
| 1261 | * <tid> is skipped for thread 1 with CLG_(clo).separate_threads=no |
| 1262 | * |
| 1263 | * Returns the file descriptor, and -1 on error (no write permission) |
| 1264 | */ |
| 1265 | static int new_dumpfile(Char buf[BUF_LEN], int tid, Char* trigger) |
| 1266 | { |
| 1267 | Bool appending = False; |
| 1268 | int i, fd; |
| 1269 | FullCost sum = 0; |
| 1270 | SysRes res; |
| 1271 | |
weidendo | 4ce5e79 | 2006-09-20 21:29:39 +0000 | [diff] [blame] | 1272 | CLG_ASSERT(dumps_initialized); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 1273 | CLG_ASSERT(filename != 0); |
| 1274 | |
| 1275 | if (!CLG_(clo).combine_dumps) { |
| 1276 | i = VG_(sprintf)(filename, "%s.%d", dump_file_base, VG_(getpid)()); |
| 1277 | |
| 1278 | if (trigger) |
| 1279 | i += VG_(sprintf)(filename+i, ".%d", out_counter); |
| 1280 | |
| 1281 | if (CLG_(clo).separate_threads) |
| 1282 | i += VG_(sprintf)(filename+i, "-%02d", tid); |
| 1283 | |
| 1284 | res = VG_(open)(filename, VKI_O_WRONLY|VKI_O_TRUNC, 0); |
| 1285 | } |
| 1286 | else { |
| 1287 | VG_(sprintf)(filename, "%s.%d", dump_file_base, VG_(getpid)()); |
| 1288 | res = VG_(open)(filename, VKI_O_WRONLY|VKI_O_APPEND, 0); |
| 1289 | if (!res.isError && out_counter>1) |
| 1290 | appending = True; |
| 1291 | } |
| 1292 | |
| 1293 | if (res.isError) { |
| 1294 | res = VG_(open)(filename, VKI_O_CREAT|VKI_O_WRONLY, |
| 1295 | VKI_S_IRUSR|VKI_S_IWUSR); |
| 1296 | if (res.isError) { |
| 1297 | /* If the file can not be opened for whatever reason (conflict |
| 1298 | between multiple supervised processes?), give up now. */ |
| 1299 | file_err(); |
| 1300 | } |
| 1301 | } |
sewardj | e808930 | 2006-10-17 02:15:17 +0000 | [diff] [blame^] | 1302 | fd = (Int) res.res; |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 1303 | |
| 1304 | CLG_DEBUG(2, " new_dumpfile '%s'\n", filename); |
| 1305 | |
| 1306 | if (!appending) |
| 1307 | reset_dump_array(); |
| 1308 | |
| 1309 | |
| 1310 | if (!appending) { |
| 1311 | /* version */ |
| 1312 | VG_(sprintf)(buf, "version: 1\n"); |
| 1313 | my_fwrite(fd, (void*)buf, VG_(strlen)(buf)); |
| 1314 | |
| 1315 | /* creator */ |
| 1316 | VG_(sprintf)(buf, "creator: callgrind-" VERSION "\n"); |
| 1317 | my_fwrite(fd, (void*)buf, VG_(strlen)(buf)); |
| 1318 | |
| 1319 | /* "pid:" line */ |
| 1320 | VG_(sprintf)(buf, "pid: %d\n", VG_(getpid)()); |
| 1321 | my_fwrite(fd, (void*)buf, VG_(strlen)(buf)); |
| 1322 | |
| 1323 | /* "cmd:" line */ |
| 1324 | VG_(strcpy)(buf, "cmd: "); |
| 1325 | my_fwrite(fd, (void*)buf, VG_(strlen)(buf)); |
| 1326 | my_fwrite(fd, (void*)cmdbuf, VG_(strlen)(cmdbuf)); |
| 1327 | } |
| 1328 | |
| 1329 | VG_(sprintf)(buf, "\npart: %d\n", out_counter); |
| 1330 | my_fwrite(fd, (void*)buf, VG_(strlen)(buf)); |
| 1331 | if (CLG_(clo).separate_threads) { |
| 1332 | VG_(sprintf)(buf, "thread: %d\n", tid); |
| 1333 | my_fwrite(fd, (void*)buf, VG_(strlen)(buf)); |
| 1334 | } |
| 1335 | |
| 1336 | /* "desc:" lines */ |
| 1337 | if (!appending) { |
| 1338 | my_fwrite(fd, "\n", 1); |
| 1339 | |
| 1340 | #if 0 |
| 1341 | /* Global options changing the tracing behaviour */ |
| 1342 | VG_(sprintf)(buf, "\ndesc: Option: --skip-plt=%s\n", |
| 1343 | CLG_(clo).skip_plt ? "yes" : "no"); |
| 1344 | my_fwrite(fd, (void*)buf, VG_(strlen)(buf)); |
| 1345 | VG_(sprintf)(buf, "desc: Option: --collect-jumps=%s\n", |
| 1346 | CLG_(clo).collect_jumps ? "yes" : "no"); |
| 1347 | my_fwrite(fd, (void*)buf, VG_(strlen)(buf)); |
| 1348 | VG_(sprintf)(buf, "desc: Option: --separate-recs=%d\n", |
| 1349 | CLG_(clo).separate_recursions); |
| 1350 | my_fwrite(fd, (void*)buf, VG_(strlen)(buf)); |
| 1351 | VG_(sprintf)(buf, "desc: Option: --separate-callers=%d\n", |
| 1352 | CLG_(clo).separate_callers); |
| 1353 | my_fwrite(fd, (void*)buf, VG_(strlen)(buf)); |
| 1354 | |
| 1355 | VG_(sprintf)(buf, "desc: Option: --dump-bbs=%s\n", |
| 1356 | CLG_(clo).dump_bbs ? "yes" : "no"); |
| 1357 | my_fwrite(fd, (void*)buf, VG_(strlen)(buf)); |
| 1358 | VG_(sprintf)(buf, "desc: Option: --separate-threads=%s\n", |
| 1359 | CLG_(clo).separate_threads ? "yes" : "no"); |
| 1360 | my_fwrite(fd, (void*)buf, VG_(strlen)(buf)); |
| 1361 | #endif |
| 1362 | |
| 1363 | (*CLG_(cachesim).getdesc)(buf); |
| 1364 | my_fwrite(fd, (void*)buf, VG_(strlen)(buf)); |
| 1365 | } |
| 1366 | |
| 1367 | VG_(sprintf)(buf, "\ndesc: Timerange: Basic block %llu - %llu\n", |
| 1368 | bbs_done, CLG_(stat).bb_executions); |
| 1369 | |
| 1370 | my_fwrite(fd, (void*)buf, VG_(strlen)(buf)); |
| 1371 | VG_(sprintf)(buf, "desc: Trigger: %s\n", |
| 1372 | trigger ? trigger : (Char*)"Program termination"); |
| 1373 | my_fwrite(fd, (void*)buf, VG_(strlen)(buf)); |
| 1374 | |
| 1375 | #if 0 |
| 1376 | /* Output function specific config |
| 1377 | * FIXME */ |
| 1378 | for (i = 0; i < N_FNCONFIG_ENTRIES; i++) { |
| 1379 | fnc = fnc_table[i]; |
| 1380 | while (fnc) { |
| 1381 | if (fnc->skip) { |
| 1382 | VG_(sprintf)(buf, "desc: Option: --fn-skip=%s\n", fnc->name); |
| 1383 | my_fwrite(fd, (void*)buf, VG_(strlen)(buf)); |
| 1384 | } |
| 1385 | if (fnc->dump_at_enter) { |
| 1386 | VG_(sprintf)(buf, "desc: Option: --fn-dump-at-enter=%s\n", |
| 1387 | fnc->name); |
| 1388 | my_fwrite(fd, (void*)buf, VG_(strlen)(buf)); |
| 1389 | } |
| 1390 | if (fnc->dump_at_leave) { |
| 1391 | VG_(sprintf)(buf, "desc: Option: --fn-dump-at-leave=%s\n", |
| 1392 | fnc->name); |
| 1393 | my_fwrite(fd, (void*)buf, VG_(strlen)(buf)); |
| 1394 | } |
| 1395 | if (fnc->separate_callers != CLG_(clo).separate_callers) { |
| 1396 | VG_(sprintf)(buf, "desc: Option: --separate-callers%d=%s\n", |
| 1397 | fnc->separate_callers, fnc->name); |
| 1398 | my_fwrite(fd, (void*)buf, VG_(strlen)(buf)); |
| 1399 | } |
| 1400 | if (fnc->separate_recursions != CLG_(clo).separate_recursions) { |
| 1401 | VG_(sprintf)(buf, "desc: Option: --separate-recs%d=%s\n", |
| 1402 | fnc->separate_recursions, fnc->name); |
| 1403 | my_fwrite(fd, (void*)buf, VG_(strlen)(buf)); |
| 1404 | } |
| 1405 | fnc = fnc->next; |
| 1406 | } |
| 1407 | } |
| 1408 | #endif |
| 1409 | |
| 1410 | /* "positions:" line */ |
| 1411 | VG_(sprintf)(buf, "\npositions:%s%s%s\n", |
| 1412 | CLG_(clo).dump_instr ? " instr" : "", |
| 1413 | CLG_(clo).dump_bb ? " bb" : "", |
| 1414 | CLG_(clo).dump_line ? " line" : ""); |
| 1415 | my_fwrite(fd, (void*)buf, VG_(strlen)(buf)); |
| 1416 | |
| 1417 | /* "events:" line */ |
| 1418 | i = VG_(sprintf)(buf, "events: "); |
| 1419 | CLG_(sprint_eventmapping)(buf+i, CLG_(dumpmap)); |
| 1420 | my_fwrite(fd, (void*)buf, VG_(strlen)(buf)); |
| 1421 | my_fwrite(fd, "\n", 1); |
| 1422 | |
| 1423 | /* summary lines */ |
| 1424 | sum = CLG_(get_eventset_cost)( CLG_(sets).full ); |
| 1425 | CLG_(zero_cost)(CLG_(sets).full, sum); |
| 1426 | if (CLG_(clo).separate_threads) { |
| 1427 | thread_info* ti = CLG_(get_current_thread)(); |
| 1428 | CLG_(add_diff_cost)(CLG_(sets).full, sum, ti->lastdump_cost, |
| 1429 | ti->states.entry[0]->cost); |
| 1430 | } |
| 1431 | else { |
| 1432 | /* This function is called once for thread 1, where |
| 1433 | * all costs are summed up when not dumping separate per thread. |
| 1434 | * But this is not true for summary: we need to add all threads. |
| 1435 | */ |
| 1436 | int t; |
| 1437 | thread_info** thr = CLG_(get_threads)(); |
| 1438 | for(t=1;t<VG_N_THREADS;t++) { |
| 1439 | if (!thr[t]) continue; |
| 1440 | CLG_(add_diff_cost)(CLG_(sets).full, sum, |
| 1441 | thr[t]->lastdump_cost, |
| 1442 | thr[t]->states.entry[0]->cost); |
| 1443 | } |
| 1444 | } |
| 1445 | fprint_cost_ln(fd, "summary: ", CLG_(dumpmap), sum); |
| 1446 | |
| 1447 | /* all dumped cost will be added to total_fcc */ |
| 1448 | CLG_(init_cost_lz)( CLG_(sets).full, &dump_total_cost ); |
| 1449 | |
| 1450 | my_fwrite(fd, "\n\n",2); |
| 1451 | |
| 1452 | if (VG_(clo_verbosity) > 1) |
| 1453 | VG_(message)(Vg_DebugMsg, "Dump to %s", filename); |
| 1454 | |
| 1455 | return fd; |
| 1456 | } |
| 1457 | |
| 1458 | |
| 1459 | static void close_dumpfile(Char buf[BUF_LEN], int fd, int tid) |
| 1460 | { |
| 1461 | if (fd <0) return; |
| 1462 | |
| 1463 | fprint_cost_ln(fd, "totals: ", CLG_(dumpmap), |
| 1464 | dump_total_cost); |
| 1465 | //fprint_fcc_ln(fd, "summary: ", &dump_total_fcc); |
| 1466 | CLG_(add_cost_lz)(CLG_(sets).full, |
| 1467 | &CLG_(total_cost), dump_total_cost); |
| 1468 | |
| 1469 | fwrite_flush(); |
| 1470 | VG_(close)(fd); |
| 1471 | |
| 1472 | if (filename[0] == '.') { |
| 1473 | if (-1 == VG_(rename) (filename, filename+1)) { |
| 1474 | /* Can not rename to correct file name: give out warning */ |
| 1475 | VG_(message)(Vg_DebugMsg, "Warning: Can not rename .%s to %s", |
| 1476 | filename, filename); |
| 1477 | } |
| 1478 | } |
| 1479 | } |
| 1480 | |
| 1481 | |
| 1482 | /* Helper for print_bbccs */ |
| 1483 | |
| 1484 | static Int print_fd; |
| 1485 | static Char* print_trigger; |
| 1486 | static Char print_buf[BUF_LEN]; |
| 1487 | |
| 1488 | static void print_bbccs_of_thread(thread_info* ti) |
| 1489 | { |
| 1490 | BBCC **p, **array; |
| 1491 | FnPos lastFnPos; |
| 1492 | AddrPos lastAPos; |
| 1493 | |
| 1494 | CLG_DEBUG(1, "+ print_bbccs(tid %d)\n", CLG_(current_tid)); |
| 1495 | |
| 1496 | print_fd = new_dumpfile(print_buf, CLG_(current_tid), print_trigger); |
| 1497 | if (print_fd <0) { |
| 1498 | CLG_DEBUG(1, "- print_bbccs(tid %d): No output...\n", CLG_(current_tid)); |
| 1499 | return; |
| 1500 | } |
| 1501 | |
| 1502 | p = array = prepare_dump(); |
| 1503 | init_fpos(&lastFnPos); |
| 1504 | init_apos(&lastAPos, 0, 0, 0); |
| 1505 | |
| 1506 | if (p) while(1) { |
| 1507 | |
| 1508 | /* on context/function change, print old cost buffer before */ |
| 1509 | if (lastFnPos.cxt && ((*p==0) || |
| 1510 | (lastFnPos.cxt != (*p)->cxt) || |
| 1511 | (lastFnPos.rec_index != (*p)->rec_index))) { |
| 1512 | if (!CLG_(is_zero_cost)( CLG_(sets).full, ccSum[currSum].cost )) { |
| 1513 | /* no need to switch buffers, as position is the same */ |
| 1514 | fprint_apos(print_fd, &(ccSum[currSum].p), &lastAPos, |
| 1515 | lastFnPos.cxt->fn[0]->file); |
| 1516 | fprint_fcost(print_fd, &ccSum[currSum], &lastAPos); |
| 1517 | } |
| 1518 | |
| 1519 | if (ccSum[currSum].p.file != lastFnPos.cxt->fn[0]->file) { |
| 1520 | /* switch back to file of function */ |
| 1521 | VG_(sprintf)(print_buf, "fe="); |
| 1522 | print_file(print_buf+3, lastFnPos.cxt->fn[0]->file); |
| 1523 | my_fwrite(print_fd, (void*)print_buf, VG_(strlen)(print_buf)); |
| 1524 | } |
| 1525 | my_fwrite(print_fd, "\n", 1); |
| 1526 | } |
| 1527 | |
| 1528 | if (*p == 0) break; |
| 1529 | |
| 1530 | if (print_fn_pos(print_fd, &lastFnPos, *p)) { |
| 1531 | |
| 1532 | /* new function */ |
| 1533 | init_apos(&lastAPos, 0, 0, (*p)->cxt->fn[0]->file); |
| 1534 | init_fcost(&ccSum[0], 0, 0, 0); |
| 1535 | init_fcost(&ccSum[1], 0, 0, 0); |
| 1536 | currSum = 0; |
| 1537 | } |
| 1538 | |
| 1539 | if (CLG_(clo).dump_bbs) { |
| 1540 | /* FIXME: Specify Object of BB if different to object of fn */ |
| 1541 | int i, pos = 0; |
| 1542 | ULong ecounter = (*p)->ecounter_sum; |
| 1543 | pos = VG_(sprintf)(print_buf, "bb=%p ", (*p)->bb->offset); |
| 1544 | for(i = 0; i<(*p)->bb->cjmp_count;i++) { |
| 1545 | pos += VG_(sprintf)(print_buf+pos, "%d %llu ", |
| 1546 | (*p)->bb->jmp[i].instr, |
| 1547 | ecounter); |
| 1548 | ecounter -= (*p)->jmp[i].ecounter; |
| 1549 | } |
| 1550 | VG_(sprintf)(print_buf+pos, "%d %llu\n", |
| 1551 | (*p)->bb->instr_count, |
| 1552 | ecounter); |
| 1553 | my_fwrite(print_fd, (void*)print_buf, VG_(strlen)(print_buf)); |
| 1554 | } |
| 1555 | |
| 1556 | fprint_bbcc(print_fd, *p, &lastAPos); |
| 1557 | |
| 1558 | p++; |
| 1559 | } |
sewardj | e808930 | 2006-10-17 02:15:17 +0000 | [diff] [blame^] | 1560 | |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 1561 | close_dumpfile(print_buf, print_fd, CLG_(current_tid)); |
| 1562 | if (array) VG_(free)(array); |
| 1563 | |
| 1564 | /* set counters of last dump */ |
| 1565 | CLG_(copy_cost)( CLG_(sets).full, ti->lastdump_cost, |
| 1566 | CLG_(current_state).cost ); |
| 1567 | |
| 1568 | CLG_DEBUG(1, "- print_bbccs(tid %d)\n", CLG_(current_tid)); |
| 1569 | } |
| 1570 | |
| 1571 | |
| 1572 | static void print_bbccs(Char* trigger, Bool only_current_thread) |
| 1573 | { |
| 1574 | init_dump_array(); |
| 1575 | init_debug_cache(); |
| 1576 | |
| 1577 | print_fd = -1; |
| 1578 | print_trigger = trigger; |
| 1579 | |
| 1580 | if (!CLG_(clo).separate_threads) { |
| 1581 | /* All BBCC/JCC costs is stored for thread 1 */ |
| 1582 | Int orig_tid = CLG_(current_tid); |
| 1583 | |
| 1584 | CLG_(switch_thread)(1); |
| 1585 | print_bbccs_of_thread( CLG_(get_current_thread)() ); |
| 1586 | CLG_(switch_thread)(orig_tid); |
| 1587 | } |
| 1588 | else if (only_current_thread) |
| 1589 | print_bbccs_of_thread( CLG_(get_current_thread)() ); |
| 1590 | else |
| 1591 | CLG_(forall_threads)(print_bbccs_of_thread); |
| 1592 | |
| 1593 | free_dump_array(); |
| 1594 | } |
| 1595 | |
| 1596 | |
| 1597 | void CLG_(dump_profile)(Char* trigger, Bool only_current_thread) |
| 1598 | { |
| 1599 | CLG_DEBUG(2, "+ dump_profile(Trigger '%s')\n", |
| 1600 | trigger ? trigger : (Char*)"Prg.Term."); |
| 1601 | |
| 1602 | if (VG_(clo_verbosity) > 1) |
| 1603 | VG_(message)(Vg_DebugMsg, "Start dumping at BB %llu (%s)...", |
| 1604 | CLG_(stat).bb_executions, |
| 1605 | trigger ? trigger : (Char*)"Prg.Term."); |
| 1606 | |
| 1607 | out_counter++; |
| 1608 | |
| 1609 | print_bbccs(trigger, only_current_thread); |
| 1610 | |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 1611 | bbs_done = CLG_(stat).bb_executions++; |
| 1612 | |
| 1613 | if (VG_(clo_verbosity) > 1) |
| 1614 | VG_(message)(Vg_DebugMsg, "Dumping done."); |
| 1615 | } |
| 1616 | |
| 1617 | /* copy command to cmd buffer (could change) */ |
| 1618 | static |
| 1619 | void init_cmdbuf(void) |
| 1620 | { |
| 1621 | Int i,j,size = 0; |
| 1622 | HChar* argv; |
| 1623 | |
| 1624 | #if VG_CORE_INTERFACE_VERSION > 8 |
| 1625 | if (VG_(args_the_exename)) |
| 1626 | size = VG_(sprintf)(cmdbuf, " %s", VG_(args_the_exename)); |
| 1627 | |
| 1628 | for(i = 0; i < VG_(args_for_client).used; i++) { |
| 1629 | argv = VG_(args_for_client).strs[i]; |
| 1630 | if (!argv) continue; |
| 1631 | if ((size>0) && (size < BUF_LEN)) cmdbuf[size++] = ' '; |
| 1632 | for(j=0;argv[j]!=0;j++) |
| 1633 | if (size < BUF_LEN) cmdbuf[size++] = argv[j]; |
| 1634 | } |
| 1635 | #else |
| 1636 | for(i = 0; i < VG_(client_argc); i++) { |
| 1637 | argv = VG_(client_argv[i]); |
| 1638 | if (!argv) continue; |
| 1639 | if ((size>0) && (size < BUF_LEN)) cmdbuf[size++] = ' '; |
| 1640 | for(j=0;argv[j]!=0;j++) |
| 1641 | if (size < BUF_LEN) cmdbuf[size++] = argv[j]; |
| 1642 | } |
| 1643 | #endif |
| 1644 | |
| 1645 | if (size == BUF_LEN) size--; |
| 1646 | cmdbuf[size] = 0; |
| 1647 | } |
| 1648 | |
weidendo | 4ce5e79 | 2006-09-20 21:29:39 +0000 | [diff] [blame] | 1649 | /* |
| 1650 | * Set up file names for dump output: base_directory, dump_file_base |
| 1651 | * The final filename of a dump is constructed at dump time from |
| 1652 | * the PID, thread ID and dump counter. |
| 1653 | * |
| 1654 | * These always will contain a full absolute path. |
| 1655 | * If no prefix is given (via option "--base=<prefix>"), the current |
| 1656 | * working directory at program start is used, otherwise <prefix> can |
| 1657 | * be relative to cwd or absolute. |
| 1658 | */ |
| 1659 | void CLG_(init_dumps)() |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 1660 | { |
weidendo | 4ce5e79 | 2006-09-20 21:29:39 +0000 | [diff] [blame] | 1661 | Int size; |
| 1662 | SysRes res; |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 1663 | |
| 1664 | if (!CLG_(clo).filename_base) |
| 1665 | CLG_(clo).filename_base = DEFAULT_DUMPNAME; |
| 1666 | |
| 1667 | /* get base directory for dump/command/result files */ |
| 1668 | if (CLG_(clo).filename_base[0] == '/') { |
| 1669 | int lastSlash = 0, i =1; |
| 1670 | while(CLG_(clo).filename_base[i]) { |
| 1671 | for(; CLG_(clo).filename_base[i] && |
| 1672 | CLG_(clo).filename_base[i] != '/'; i++); |
| 1673 | if (CLG_(clo).filename_base[i] != '/') break; |
| 1674 | lastSlash = i; |
| 1675 | i++; |
| 1676 | } |
weidendo | 6bb0825 | 2006-04-21 01:02:13 +0000 | [diff] [blame] | 1677 | i = lastSlash; |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 1678 | base_directory = (Char*) CLG_MALLOC(i+1); |
| 1679 | VG_(strncpy)(base_directory, CLG_(clo).filename_base, i); |
| 1680 | base_directory[i] = 0; |
| 1681 | |
| 1682 | dump_file_base = CLG_(clo).filename_base; |
| 1683 | } |
| 1684 | else { |
| 1685 | size = 100; |
| 1686 | base_directory = 0; |
| 1687 | |
| 1688 | /* getcwd() fails if the buffer isn't big enough -- keep doubling size |
| 1689 | until it succeeds. */ |
| 1690 | while (NULL == base_directory) { |
| 1691 | base_directory = CLG_MALLOC(size); |
| 1692 | if (!VG_(getcwd)(base_directory, size)) { |
| 1693 | VG_(free)(base_directory); |
| 1694 | base_directory = 0; |
| 1695 | size *= 2; |
| 1696 | } |
| 1697 | } |
| 1698 | |
| 1699 | size = VG_(strlen)(base_directory) + VG_(strlen)(CLG_(clo).filename_base) +2; |
| 1700 | dump_file_base = (Char*) CLG_MALLOC(size); |
| 1701 | CLG_ASSERT(dump_file_base != 0); |
| 1702 | VG_(sprintf)(dump_file_base, "%s/%s", |
| 1703 | base_directory, CLG_(clo).filename_base); |
| 1704 | } |
| 1705 | |
| 1706 | /* allocate space big enough for final filenames */ |
| 1707 | filename = (Char*) CLG_MALLOC(VG_(strlen)(dump_file_base)+32); |
| 1708 | CLG_ASSERT(filename != 0); |
| 1709 | |
| 1710 | /* Make sure the output base file can be written. |
| 1711 | * This is used for the dump at program termination. |
| 1712 | * We stop with an error here if we can not create the |
| 1713 | * file: This is probably because of missing rights, |
| 1714 | * and trace parts wouldn't be allowed to be written, too. |
| 1715 | */ |
| 1716 | VG_(sprintf)(filename, "%s.%d", dump_file_base, VG_(getpid)()); |
| 1717 | res = VG_(open)(filename, VKI_O_WRONLY|VKI_O_TRUNC, 0); |
| 1718 | if (res.isError) { |
| 1719 | res = VG_(open)(filename, VKI_O_CREAT|VKI_O_WRONLY, |
| 1720 | VKI_S_IRUSR|VKI_S_IWUSR); |
| 1721 | if (res.isError) { |
| 1722 | file_err(); |
| 1723 | } |
| 1724 | } |
sewardj | e808930 | 2006-10-17 02:15:17 +0000 | [diff] [blame^] | 1725 | if (!res.isError) VG_(close)( (Int)res.res ); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 1726 | |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 1727 | init_cmdbuf(); |
weidendo | 4ce5e79 | 2006-09-20 21:29:39 +0000 | [diff] [blame] | 1728 | |
| 1729 | dumps_initialized = True; |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 1730 | } |