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