From: Florian Krohm Date: Tue, 11 Nov 2014 20:46:34 +0000 (+0000) Subject: Merge r14340,14346-14348,14352,14354-14355 from the BUF_REMOVAL branch to X-Git-Tag: svn/VALGRIND_3_11_0~835 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=73df75ebba72c800241cb81fca9f3a624aa7039f;p=thirdparty%2Fvalgrind.git Merge r14340,14346-14348,14352,14354-14355 from the BUF_REMOVAL branch to trunk. Convert callgrind to use VG_(fprintf). git-svn-id: svn://svn.valgrind.org/valgrind/trunk@14713 --- diff --git a/callgrind/bbcc.c b/callgrind/bbcc.c index a354b0b3fa..1580a47c29 100644 --- a/callgrind/bbcc.c +++ b/callgrind/bbcc.c @@ -336,13 +336,20 @@ void insert_bbcc_into_hash(BBCC* bbcc) current_bbccs.entries); } -static const HChar* mangled_cxt(Context* cxt, int rec_index) +/* String is returned in a dynamically allocated buffer. Caller is + responsible for free'ing it. */ +static HChar* mangled_cxt(const Context* cxt, Int rec_index) { - static HChar mangled[FN_NAME_LEN]; - int i, p; + Int i, p; - if (!cxt) return "(no context)"; + if (!cxt) return VG_(strdup)("cl.bbcc.mcxt", "(no context)"); + /* Overestimate the number of bytes we need to hold the string. */ + SizeT need = 20; // rec_index + nul-terminator + for (i = 0; i < cxt->size; ++i) + need += VG_(strlen)(cxt->fn[i]->name) + 1; // 1 for leading ' + + HChar *mangled = CLG_MALLOC("cl.bbcc.mcxt", need); p = VG_(sprintf)(mangled, "%s", cxt->fn[0]->name); if (rec_index >0) p += VG_(sprintf)(mangled+p, "'%d", rec_index +1); @@ -411,14 +418,16 @@ static BBCC* clone_bbcc(BBCC* orig, Context* cxt, Int rec_index) CLG_DEBUGIF(3) CLG_(print_bbcc)(-2, bbcc); - // FIXME: mangled_cxt returns a pointer to a static buffer that - // gets overwritten with each invocation. + HChar *mangled_orig = mangled_cxt(orig->cxt, orig->rec_index); + HChar *mangled_bbcc = mangled_cxt(bbcc->cxt, bbcc->rec_index); CLG_DEBUG(2,"- clone_BBCC(%p, %d) for BB %#lx\n" " orig %s\n" " new %s\n", orig, rec_index, bb_addr(orig->bb), - mangled_cxt(orig->cxt, orig->rec_index), - mangled_cxt(bbcc->cxt, bbcc->rec_index)); + mangled_orig, + mangled_bbcc); + CLG_FREE(mangled_orig); + CLG_FREE(mangled_bbcc); CLG_(stat).bbcc_clones++; diff --git a/callgrind/dump.c b/callgrind/dump.c index 515d7ec69a..687f4df1ea 100644 --- a/callgrind/dump.c +++ b/callgrind/dump.c @@ -50,12 +50,6 @@ static FullCost dump_total_cost = 0; EventMapping* CLG_(dumpmap) = 0; -/* Temporary output buffer for - * print_fn_pos, fprint_apos, fprint_fcost, fprint_jcc, - * fprint_fcc_ln, dump_run_info, dump_state_info - */ -static HChar outbuf[FILENAME_LEN + FN_NAME_LEN + OBJ_NAME_LEN + COSTS_LEN]; - Int CLG_(get_dump_counter)(void) { return out_counter; @@ -130,68 +124,25 @@ void init_fpos(FnPos* p) } -#if 0 -static __inline__ -static void my_fwrite(Int fd, const HChar* buf, Int len) +static void print_obj(VgFile *fp, const HChar* prefix, obj_node* obj) { - VG_(write)(fd, buf, len); -} -#else - -#define FWRITE_BUFSIZE 32000 -#define FWRITE_THROUGH 10000 -static HChar fwrite_buf[FWRITE_BUFSIZE]; -static Int fwrite_pos; -static Int fwrite_fd = -1; - -static __inline__ -void fwrite_flush(void) -{ - if ((fwrite_fd>=0) && (fwrite_pos>0)) - VG_(write)(fwrite_fd, fwrite_buf, fwrite_pos); - fwrite_pos = 0; -} - -static void my_fwrite(Int fd, const HChar* buf, Int len) -{ - if (fwrite_fd != fd) { - fwrite_flush(); - fwrite_fd = fd; - } - if (len > FWRITE_THROUGH) { - fwrite_flush(); - VG_(write)(fd, buf, len); - return; - } - if (FWRITE_BUFSIZE - fwrite_pos <= len) fwrite_flush(); - VG_(strncpy)(fwrite_buf + fwrite_pos, buf, len); - fwrite_pos += len; -} -#endif - - -static void print_obj(HChar* buf, obj_node* obj) -{ - //int n; - if (CLG_(clo).compress_strings) { CLG_ASSERT(obj_dumped != 0); if (obj_dumped[obj->number]) - /*n =*/ VG_(sprintf)(buf, "(%d)\n", obj->number); + VG_(fprintf)(fp, "%s(%d)\n", prefix, obj->number); else { - /*n =*/ VG_(sprintf)(buf, "(%d) %s\n", - obj->number, obj->name); + VG_(fprintf)(fp, "%s(%d) %s\n", prefix, obj->number, obj->name); } } else - /*n =*/ VG_(sprintf)(buf, "%s\n", obj->name); + VG_(fprintf)(fp, "%s%s\n", prefix, obj->name); #if 0 /* add mapping parameters the first time a object is dumped * format: mp=0xSTART SIZE 0xOFFSET */ if (!obj_dumped[obj->number]) { obj_dumped[obj->number]; - VG_(sprintf)(buf+n, "mp=%p %p %p\n", + VG_(fprintf)(fp, "mp=%p %p %p\n", pos->obj->start, pos->obj->size, pos->obj->offset); } #else @@ -199,54 +150,44 @@ static void print_obj(HChar* buf, obj_node* obj) #endif } -static void print_file(Int fd, const char *prefix, file_node* file) +static void print_file(VgFile *fp, const char *prefix, const file_node* file) { - // 32 for file->number + peripheral characters - HChar buf[VG_(strlen)(prefix) + VG_(strlen)(file->name) + 32]; - if (CLG_(clo).compress_strings) { CLG_ASSERT(file_dumped != 0); if (file_dumped[file->number]) - VG_(sprintf)(buf, "%s(%d)\n", prefix, file->number); + VG_(fprintf)(fp, "%s(%d)\n", prefix, file->number); else { - VG_(sprintf)(buf, "%s(%d) %s\n", prefix, - file->number, file->name); + VG_(fprintf)(fp, "%s(%d) %s\n", prefix, file->number, file->name); file_dumped[file->number] = True; } } else - VG_(sprintf)(buf, "%s%s\n", prefix, file->name); - - my_fwrite(fd, buf, VG_(strlen)(buf)); + VG_(fprintf)(fp, "%s%s\n", prefix, file->name); } /* * tag can be "fn", "cfn", "jfn" */ -static void print_fn(Int fd, HChar* buf, const HChar* tag, fn_node* fn) +static void print_fn(VgFile *fp, const HChar* tag, const fn_node* fn) { - int p; - p = VG_(sprintf)(buf, "%s=",tag); + VG_(fprintf)(fp, "%s=",tag); if (CLG_(clo).compress_strings) { CLG_ASSERT(fn_dumped != 0); if (fn_dumped[fn->number]) - p += VG_(sprintf)(buf+p, "(%d)\n", fn->number); + VG_(fprintf)(fp, "(%d)\n", fn->number); else { - p += VG_(sprintf)(buf+p, "(%d) %s\n", - fn->number, fn->name); + VG_(fprintf)(fp, "(%d) %s\n", fn->number, fn->name); fn_dumped[fn->number] = True; } } else - p += VG_(sprintf)(buf+p, "%s\n", fn->name); - - my_fwrite(fd, buf, p); + VG_(fprintf)(fp, "%s\n", fn->name); } -static void print_mangled_fn(Int fd, HChar* buf, const HChar* tag, +static void print_mangled_fn(VgFile *fp, const HChar* tag, Context* cxt, int rec_index) { - int p, i; + int i; if (CLG_(clo).compress_strings && CLG_(clo).compress_mangled) { @@ -255,9 +196,8 @@ static void print_mangled_fn(Int fd, HChar* buf, const HChar* tag, CLG_ASSERT(cxt_dumped != 0); if (cxt_dumped[cxt->base_number+rec_index]) { - p = VG_(sprintf)(buf, "%s=(%d)\n", + VG_(fprintf)(fp, "%s=(%d)\n", tag, cxt->base_number + rec_index); - my_fwrite(fd, buf, p); return; } @@ -267,9 +207,8 @@ static void print_mangled_fn(Int fd, HChar* buf, const HChar* tag, CLG_ASSERT(cxt->fn[i-1]->pure_cxt != 0); n = cxt->fn[i-1]->pure_cxt->base_number; if (cxt_dumped[n]) continue; - p = VG_(sprintf)(buf, "%s=(%d) %s\n", + VG_(fprintf)(fp, "%s=(%d) %s\n", tag, n, cxt->fn[i-1]->name); - my_fwrite(fd, buf, p); cxt_dumped[n] = True; last = cxt->fn[i-1]->pure_cxt; @@ -277,44 +216,41 @@ static void print_mangled_fn(Int fd, HChar* buf, const HChar* tag, /* If the last context was the context to print, we are finished */ if ((last == cxt) && (rec_index == 0)) return; - p = VG_(sprintf)(buf, "%s=(%d) (%d)", tag, + VG_(fprintf)(fp, "%s=(%d) (%d)", tag, cxt->base_number + rec_index, cxt->fn[0]->pure_cxt->base_number); if (rec_index >0) - p += VG_(sprintf)(buf+p, "'%d", rec_index +1); + VG_(fprintf)(fp, "'%d", rec_index +1); for(i=1;isize;i++) - p += VG_(sprintf)(buf+p, "'(%d)", + VG_(fprintf)(fp, "'(%d)", cxt->fn[i]->pure_cxt->base_number); - p += VG_(sprintf)(buf+p, "\n"); - my_fwrite(fd, buf, p); + VG_(fprintf)(fp, "\n"); cxt_dumped[cxt->base_number+rec_index] = True; return; } - p = VG_(sprintf)(buf, "%s=", tag); + VG_(fprintf)(fp, "%s=", tag); if (CLG_(clo).compress_strings) { CLG_ASSERT(cxt_dumped != 0); if (cxt_dumped[cxt->base_number+rec_index]) { - p += VG_(sprintf)(buf+p, "(%d)\n", cxt->base_number + rec_index); - my_fwrite(fd, buf, p); + VG_(fprintf)(fp, "(%d)\n", cxt->base_number + rec_index); return; } else { - p += VG_(sprintf)(buf+p, "(%d) ", cxt->base_number + rec_index); + VG_(fprintf)(fp, "(%d) ", cxt->base_number + rec_index); cxt_dumped[cxt->base_number+rec_index] = True; } } - p += VG_(sprintf)(buf+p, "%s", cxt->fn[0]->name); + VG_(fprintf)(fp, "%s", cxt->fn[0]->name); if (rec_index >0) - p += VG_(sprintf)(buf+p, "'%d", rec_index +1); + VG_(fprintf)(fp, "'%d", rec_index +1); for(i=1;isize;i++) - p += VG_(sprintf)(buf+p, "'%s", cxt->fn[i]->name); + VG_(fprintf)(fp, "'%s", cxt->fn[i]->name); - p += VG_(sprintf)(buf+p, "\n"); - my_fwrite(fd, buf, p); + VG_(fprintf)(fp, "\n"); } @@ -324,7 +260,7 @@ static void print_mangled_fn(Int fd, HChar* buf, const HChar* tag, * the position, update * Return True if something changes. */ -static Bool print_fn_pos(int fd, FnPos* last, BBCC* bbcc) +static Bool print_fn_pos(VgFile *fp, FnPos* last, BBCC* bbcc) { Bool res = False; @@ -337,8 +273,7 @@ static Bool print_fn_pos(int fd, FnPos* last, BBCC* bbcc) if (!CLG_(clo).mangle_names) { if (last->rec_index != bbcc->rec_index) { - VG_(sprintf)(outbuf, "rec=%d\n\n", bbcc->rec_index); - my_fwrite(fd, outbuf, VG_(strlen)(outbuf)); + VG_(fprintf)(fp, "rec=%d\n\n", bbcc->rec_index); last->rec_index = bbcc->rec_index; last->cxt = 0; /* reprint context */ res = True; @@ -352,13 +287,12 @@ static Bool print_fn_pos(int fd, FnPos* last, BBCC* bbcc) if (curr_from == 0) { if (last_from != 0) { /* switch back to no context */ - VG_(sprintf)(outbuf, "frfn=(spontaneous)\n"); - my_fwrite(fd, outbuf, VG_(strlen)(outbuf)); + VG_(fprintf)(fp, "frfn=(spontaneous)\n"); res = True; } } else if (last_from != curr_from) { - print_fn(fd,outbuf,"frfn", curr_from); + print_fn(fp, "frfn", curr_from); res = True; } last->cxt = bbcc->cxt; @@ -366,22 +300,20 @@ static Bool print_fn_pos(int fd, FnPos* last, BBCC* bbcc) } if (last->obj != bbcc->cxt->fn[0]->file->obj) { - VG_(sprintf)(outbuf, "ob="); - print_obj(outbuf+3, bbcc->cxt->fn[0]->file->obj); - my_fwrite(fd, outbuf, VG_(strlen)(outbuf)); + print_obj(fp, "ob=", bbcc->cxt->fn[0]->file->obj); last->obj = bbcc->cxt->fn[0]->file->obj; res = True; } if (last->file != bbcc->cxt->fn[0]->file) { - print_file(fd, "fl=", bbcc->cxt->fn[0]->file); + print_file(fp, "fl=", bbcc->cxt->fn[0]->file); last->file = bbcc->cxt->fn[0]->file; res = True; } if (!CLG_(clo).mangle_names) { if (last->fn != bbcc->cxt->fn[0]) { - print_fn(fd,outbuf, "fn", bbcc->cxt->fn[0]); + print_fn(fp, "fn", bbcc->cxt->fn[0]); last->fn = bbcc->cxt->fn[0]; res = True; } @@ -391,7 +323,7 @@ static Bool print_fn_pos(int fd, FnPos* last, BBCC* bbcc) if ((last->rec_index != bbcc->rec_index) || (last->cxt != bbcc->cxt)) { - print_mangled_fn(fd, outbuf, "fn", bbcc->cxt, bbcc->rec_index); + print_mangled_fn(fp, "fn", bbcc->cxt, bbcc->rec_index); last->fn = bbcc->cxt->fn[0]; last->rec_index = bbcc->rec_index; res = True; @@ -501,7 +433,8 @@ static void init_fcost(AddrCost* c, Addr addr, Addr bbaddr, file_node* file) * print position change inside of a BB (last -> curr) * this doesn't update last to curr! */ -static void fprint_apos(Int fd, AddrPos* curr, AddrPos* last, file_node* func_file) +static void fprint_apos(VgFile *fp, AddrPos* curr, AddrPos* last, + file_node* func_file) { CLG_ASSERT(curr->file != 0); CLG_DEBUG(2, " print_apos(file '%s', line %d, bb %#lx, addr %#lx) fnFile '%s'\n", @@ -512,15 +445,14 @@ static void fprint_apos(Int fd, AddrPos* curr, AddrPos* last, file_node* func_fi /* if we switch back to orig file, use fe=... */ if (curr->file == func_file) - print_file(fd, "fe=", curr->file); + print_file(fp, "fe=", curr->file); else - print_file(fd, "fi=", curr->file); + print_file(fp, "fi=", curr->file); } if (CLG_(clo).dump_bbs) { if (curr->line != last->line) { - VG_(sprintf)(outbuf, "ln=%d\n", curr->line); - my_fwrite(fd, outbuf, VG_(strlen)(outbuf)); + VG_(fprintf)(fp, "ln=%d\n", curr->line); } } } @@ -534,25 +466,24 @@ static void fprint_apos(Int fd, AddrPos* curr, AddrPos* last, file_node* func_fi * This doesn't set last to curr afterwards! */ static -void fprint_pos(Int fd, AddrPos* curr, AddrPos* last) +void fprint_pos(VgFile *fp, const AddrPos* curr, const AddrPos* last) { if (0) //CLG_(clo).dump_bbs) - VG_(sprintf)(outbuf, "%lu ", curr->addr - curr->bb_addr); + VG_(fprintf)(fp, "%lu ", curr->addr - curr->bb_addr); else { - int p = 0; if (CLG_(clo).dump_instr) { int diff = curr->addr - last->addr; if ( CLG_(clo).compress_pos && (last->addr >0) && (diff > -100) && (diff < 100)) { if (diff >0) - p = VG_(sprintf)(outbuf, "+%d ", diff); + VG_(fprintf)(fp, "+%d ", diff); else if (diff==0) - p = VG_(sprintf)(outbuf, "* "); + VG_(fprintf)(fp, "* "); else - p = VG_(sprintf)(outbuf, "%d ", diff); + VG_(fprintf)(fp, "%d ", diff); } else - p = VG_(sprintf)(outbuf, "%#lx ", curr->addr); + VG_(fprintf)(fp, "%#lx ", curr->addr); } if (CLG_(clo).dump_bb) { @@ -560,14 +491,14 @@ void fprint_pos(Int fd, AddrPos* curr, AddrPos* last) if ( CLG_(clo).compress_pos && (last->bb_addr >0) && (diff > -100) && (diff < 100)) { if (diff >0) - p += VG_(sprintf)(outbuf+p, "+%d ", diff); + VG_(fprintf)(fp, "+%d ", diff); else if (diff==0) - p += VG_(sprintf)(outbuf+p, "* "); + VG_(fprintf)(fp, "* "); else - p += VG_(sprintf)(outbuf+p, "%d ", diff); + VG_(fprintf)(fp, "%d ", diff); } else - p += VG_(sprintf)(outbuf+p, "%#lx ", curr->bb_addr); + VG_(fprintf)(fp, "%#lx ", curr->bb_addr); } if (CLG_(clo).dump_line) { @@ -576,17 +507,16 @@ void fprint_pos(Int fd, AddrPos* curr, AddrPos* last) (diff > -100) && (diff < 100)) { if (diff >0) - VG_(sprintf)(outbuf+p, "+%d ", diff); + VG_(fprintf)(fp, "+%d ", diff); else if (diff==0) - VG_(sprintf)(outbuf+p, "* "); + VG_(fprintf)(fp, "* "); else - VG_(sprintf)(outbuf+p, "%d ", diff); + VG_(fprintf)(fp, "%d ", diff); } else - VG_(sprintf)(outbuf+p, "%u ", curr->line); + VG_(fprintf)(fp, "%u ", curr->line); } } - my_fwrite(fd, outbuf, VG_(strlen)(outbuf)); } @@ -595,13 +525,11 @@ void fprint_pos(Int fd, AddrPos* curr, AddrPos* last) */ static -void fprint_cost(int fd, EventMapping* es, ULong* cost) +void fprint_cost(VgFile *fp, const EventMapping* es, const ULong* cost) { HChar *mcost = CLG_(mappingcost_as_string)(es, cost); - VG_(sprintf)(outbuf, "%s\n", mcost); - my_fwrite(fd, outbuf, VG_(strlen)(outbuf)); + VG_(fprintf)(fp, "%s\n", mcost); CLG_FREE(mcost); - return; } @@ -611,7 +539,7 @@ void fprint_cost(int fd, EventMapping* es, ULong* cost) * funcPos is the source position of the first line of actual function. * Something is written only if cost != 0; returns True in this case. */ -static void fprint_fcost(Int fd, AddrCost* c, AddrPos* last) +static void fprint_fcost(VgFile *fp, AddrCost* c, AddrPos* last) { CLG_DEBUGIF(3) { CLG_DEBUG(2, " print_fcost(file '%s', line %d, bb %#lx, addr %#lx):\n", @@ -619,10 +547,10 @@ static void fprint_fcost(Int fd, AddrCost* c, AddrPos* last) CLG_(print_cost)(-5, CLG_(sets).full, c->cost); } - fprint_pos(fd, &(c->p), last); + fprint_pos(fp, &(c->p), last); copy_apos( last, &(c->p) ); /* update last to current position */ - fprint_cost(fd, CLG_(dumpmap), c->cost); + fprint_cost(fp, CLG_(dumpmap), c->cost); /* add cost to total */ CLG_(add_and_zero_cost)( CLG_(sets).full, dump_total_cost, c->cost ); @@ -631,7 +559,8 @@ static void fprint_fcost(Int fd, AddrCost* c, AddrPos* last) /* Write out the calls from jcc (at pos) */ -static void fprint_jcc(Int fd, jCC* jcc, AddrPos* curr, AddrPos* last, ULong ecounter) +static void fprint_jcc(VgFile *fp, jCC* jcc, AddrPos* curr, AddrPos* last, + ULong ecounter) { static AddrPos target; file_node* file; @@ -671,33 +600,32 @@ static void fprint_jcc(Int fd, jCC* jcc, AddrPos* curr, AddrPos* last, ULong eco * which change the stack, and thus context */ if (last->file != target.file) { - print_file(fd, "jfi=", target.file); + print_file(fp, "jfi=", target.file); } if (jcc->from->cxt != jcc->to->cxt) { if (CLG_(clo).mangle_names) - print_mangled_fn(fd, outbuf, "jfn", + print_mangled_fn(fp, "jfn", jcc->to->cxt, jcc->to->rec_index); else - print_fn(fd, outbuf, "jfn", jcc->to->cxt->fn[0]); + print_fn(fp, "jfn", jcc->to->cxt->fn[0]); } if (jcc->jmpkind == jk_CondJump) { /* format: jcnd=/ */ - VG_(sprintf)(outbuf, "jcnd=%llu/%llu ", + VG_(fprintf)(fp, "jcnd=%llu/%llu ", jcc->call_counter, ecounter); } else { /* format: jump= */ - VG_(sprintf)(outbuf, "jump=%llu ", + VG_(fprintf)(fp, "jump=%llu ", jcc->call_counter); } - my_fwrite(fd, outbuf, VG_(strlen)(outbuf)); - fprint_pos(fd, &target, last); - my_fwrite(fd, "\n", 1); - fprint_pos(fd, curr, last); - my_fwrite(fd, "\n", 1); + fprint_pos(fp, &target, last); + VG_(fprintf)(fp, "\n"); + fprint_pos(fp, curr, last); + VG_(fprintf)(fp, "\n"); jcc->call_counter = 0; return; @@ -708,30 +636,27 @@ static void fprint_jcc(Int fd, jCC* jcc, AddrPos* curr, AddrPos* last, ULong eco /* object of called position different to object of this function?*/ if (jcc->from->cxt->fn[0]->file->obj != obj) { - VG_(sprintf)(outbuf, "cob="); - print_obj(outbuf+4, obj); - my_fwrite(fd, outbuf, VG_(strlen)(outbuf)); + print_obj(fp, "cob=", obj); } /* file of called position different to current file? */ if (last->file != file) { - print_file(fd, "cfi=", file); + print_file(fp, "cfi=", file); } if (CLG_(clo).mangle_names) - print_mangled_fn(fd, outbuf, "cfn", jcc->to->cxt, jcc->to->rec_index); + print_mangled_fn(fp, "cfn", jcc->to->cxt, jcc->to->rec_index); else - print_fn(fd, outbuf, "cfn", jcc->to->cxt->fn[0]); + print_fn(fp, "cfn", jcc->to->cxt->fn[0]); if (!CLG_(is_zero_cost)( CLG_(sets).full, jcc->cost)) { - VG_(sprintf)(outbuf, "calls=%llu ", + VG_(fprintf)(fp, "calls=%llu ", jcc->call_counter); - my_fwrite(fd, outbuf, VG_(strlen)(outbuf)); - fprint_pos(fd, &target, last); - my_fwrite(fd, "\n", 1); - fprint_pos(fd, curr, last); - fprint_cost(fd, CLG_(dumpmap), jcc->cost); + fprint_pos(fp, &target, last); + VG_(fprintf)(fp, "\n"); + fprint_pos(fp, curr, last); + fprint_cost(fp, CLG_(dumpmap), jcc->cost); CLG_(init_cost)( CLG_(sets).full, jcc->cost ); @@ -753,7 +678,7 @@ static int currSum; * - JCCs of the unique jump of this BB * returns True if something was written */ -static Bool fprint_bbcc(Int fd, BBCC* bbcc, AddrPos* last) +static Bool fprint_bbcc(VgFile *fp, BBCC* bbcc, AddrPos* last) { InstrInfo* instr_info; ULong ecounter; @@ -794,8 +719,8 @@ static Bool fprint_bbcc(Int fd, BBCC* bbcc, AddrPos* last) if (!CLG_(is_zero_cost)( CLG_(sets).full, currCost->cost )) { something_written = True; - fprint_apos(fd, &(currCost->p), last, bbcc->cxt->fn[0]->file); - fprint_fcost(fd, currCost, last); + fprint_apos(fp, &(currCost->p), last, bbcc->cxt->fn[0]->file); + fprint_fcost(fp, currCost, last); } /* switch buffers */ @@ -818,16 +743,16 @@ static Bool fprint_bbcc(Int fd, BBCC* bbcc, AddrPos* last) if (jcc_count>0) { if (!CLG_(is_zero_cost)( CLG_(sets).full, currCost->cost )) { /* no need to switch buffers, as position is the same */ - fprint_apos(fd, &(currCost->p), last, bbcc->cxt->fn[0]->file); - fprint_fcost(fd, currCost, last); + fprint_apos(fp, &(currCost->p), last, bbcc->cxt->fn[0]->file); + fprint_fcost(fp, currCost, last); } get_debug_pos(bbcc, bb_addr(bb)+instr_info->instr_offset, &(currCost->p)); - fprint_apos(fd, &(currCost->p), last, bbcc->cxt->fn[0]->file); + fprint_apos(fp, &(currCost->p), last, bbcc->cxt->fn[0]->file); something_written = True; for(jcc=bbcc->jmp[jmp].jcc_list; jcc; jcc=jcc->next_from) { if (((jcc->jmpkind != jk_Call) && (jcc->call_counter >0)) || (!CLG_(is_zero_cost)( CLG_(sets).full, jcc->cost ))) - fprint_jcc(fd, jcc, &(currCost->p), last, ecounter); + fprint_jcc(fp, jcc, &(currCost->p), last, ecounter); } } } @@ -855,12 +780,12 @@ static Bool fprint_bbcc(Int fd, BBCC* bbcc, AddrPos* last) if (!CLG_(is_zero_cost)( CLG_(sets).full, currCost->cost )) { /* no need to switch buffers, as position is the same */ - fprint_apos(fd, &(currCost->p), last, bbcc->cxt->fn[0]->file); - fprint_fcost(fd, currCost, last); + fprint_apos(fp, &(currCost->p), last, bbcc->cxt->fn[0]->file); + fprint_fcost(fp, currCost, last); } get_debug_pos(bbcc, bb_jmpaddr(bb), &(currCost->p)); - fprint_apos(fd, &(currCost->p), last, bbcc->cxt->fn[0]->file); + fprint_apos(fp, &(currCost->p), last, bbcc->cxt->fn[0]->file); something_written = True; /* first, print skipped costs for calls */ @@ -869,10 +794,9 @@ static Bool fprint_bbcc(Int fd, BBCC* bbcc, AddrPos* last) CLG_(add_and_zero_cost)( CLG_(sets).full, currCost->cost, bbcc->skipped ); #if 0 - VG_(sprintf)(outbuf, "# Skipped\n"); - my_fwrite(fd, outbuf, VG_(strlen)(outbuf)); + VG_(fprintf)(fp, "# Skipped\n"); #endif - fprint_fcost(fd, currCost, last); + fprint_fcost(fp, currCost, last); } if (jcc_count > 0) @@ -881,7 +805,7 @@ static Bool fprint_bbcc(Int fd, BBCC* bbcc, AddrPos* last) if ( ((jcc->jmpkind != jk_Call) && (jcc->call_counter >0)) || (!CLG_(is_zero_cost)( CLG_(sets).full, jcc->cost ))) - fprint_jcc(fd, jcc, &(currCost->p), last, ecounter); + fprint_jcc(fp, jcc, &(currCost->p), last, ecounter); } } @@ -889,10 +813,10 @@ static Bool fprint_bbcc(Int fd, BBCC* bbcc, AddrPos* last) if (!CLG_(is_zero_cost)( CLG_(sets).full, currCost->cost )) { something_written = True; - fprint_apos(fd, &(currCost->p), last, bbcc->cxt->fn[0]->file); - fprint_fcost(fd, currCost, last); + fprint_apos(fp, &(currCost->p), last, bbcc->cxt->fn[0]->file); + fprint_fcost(fp, currCost, last); } - if (CLG_(clo).dump_bbs) my_fwrite(fd, "\n", 1); + if (CLG_(clo).dump_bbs) VG_(fprintf)(fp, "\n"); /* when every cost was immediatly written, we must have done so, * as this function is only called when there's cost in a BBCC @@ -1218,12 +1142,11 @@ BBCC** prepare_dump(void) -static void fprint_cost_ln(int fd, const HChar* prefix, - EventMapping* em, ULong* cost) +static void fprint_cost_ln(VgFile *fp, const HChar* prefix, + const EventMapping* em, const ULong* cost) { HChar *mcost = CLG_(mappingcost_as_string)(em, cost); - VG_(sprintf)(outbuf, "%s%s\n", prefix, mcost); - my_fwrite(fd, outbuf, VG_(strlen)(outbuf)); + VG_(fprintf)(fp, "%s%s\n", prefix, mcost); CLG_FREE(mcost); } @@ -1248,12 +1171,12 @@ void file_err(void) * * Returns the file descriptor, and -1 on error (no write permission) */ -static int new_dumpfile(HChar buf[BUF_LEN], int tid, const HChar* trigger) +static VgFile *new_dumpfile(int tid, const HChar* trigger) { Bool appending = False; - int i, fd; + int i; FullCost sum = 0; - SysRes res; + VgFile *fp; CLG_ASSERT(dumps_initialized); CLG_ASSERT(filename != 0); @@ -1267,25 +1190,24 @@ static int new_dumpfile(HChar buf[BUF_LEN], int tid, const HChar* trigger) if (CLG_(clo).separate_threads) VG_(sprintf)(filename+i, "-%02d", tid); - res = VG_(open)(filename, VKI_O_WRONLY|VKI_O_TRUNC, 0); + fp = VG_(fopen)(filename, VKI_O_WRONLY|VKI_O_TRUNC, 0); } else { VG_(sprintf)(filename, "%s", out_file); - res = VG_(open)(filename, VKI_O_WRONLY|VKI_O_APPEND, 0); - if (!sr_isError(res) && out_counter>1) + fp = VG_(fopen)(filename, VKI_O_WRONLY|VKI_O_APPEND, 0); + if (fp && out_counter>1) appending = True; } - if (sr_isError(res)) { - res = VG_(open)(filename, VKI_O_CREAT|VKI_O_WRONLY, - VKI_S_IRUSR|VKI_S_IWUSR); - if (sr_isError(res)) { + if (fp == NULL) { + fp = VG_(fopen)(filename, VKI_O_CREAT|VKI_O_WRONLY, + VKI_S_IRUSR|VKI_S_IWUSR); + if (fp == NULL) { /* If the file can not be opened for whatever reason (conflict between multiple supervised processes?), give up now. */ file_err(); } } - fd = (Int) sr_Res(res); CLG_DEBUG(2, " new_dumpfile '%s'\n", filename); @@ -1295,68 +1217,52 @@ static int new_dumpfile(HChar buf[BUF_LEN], int tid, const HChar* trigger) if (!appending) { /* version */ - VG_(sprintf)(buf, "version: 1\n"); - my_fwrite(fd, buf, VG_(strlen)(buf)); + VG_(fprintf)(fp, "version: 1\n"); /* creator */ - VG_(sprintf)(buf, "creator: callgrind-" VERSION "\n"); - my_fwrite(fd, buf, VG_(strlen)(buf)); + VG_(fprintf)(fp, "creator: callgrind-" VERSION "\n"); /* "pid:" line */ - VG_(sprintf)(buf, "pid: %d\n", VG_(getpid)()); - my_fwrite(fd, buf, VG_(strlen)(buf)); + VG_(fprintf)(fp, "pid: %d\n", VG_(getpid)()); /* "cmd:" line */ - VG_(strcpy)(buf, "cmd: "); - my_fwrite(fd, buf, VG_(strlen)(buf)); - my_fwrite(fd, cmdbuf, VG_(strlen)(cmdbuf)); + VG_(fprintf)(fp, "cmd: %s", cmdbuf); } - VG_(sprintf)(buf, "\npart: %d\n", out_counter); - my_fwrite(fd, buf, VG_(strlen)(buf)); + VG_(fprintf)(fp, "\npart: %d\n", out_counter); if (CLG_(clo).separate_threads) { - VG_(sprintf)(buf, "thread: %d\n", tid); - my_fwrite(fd, buf, VG_(strlen)(buf)); + VG_(fprintf)(fp, "thread: %d\n", tid); } /* "desc:" lines */ if (!appending) { - my_fwrite(fd, "\n", 1); + VG_(fprintf)(fp, "\n"); #if 0 /* Global options changing the tracing behaviour */ - VG_(sprintf)(buf, "\ndesc: Option: --skip-plt=%s\n", + VG_(fprintf)(fp, "\ndesc: Option: --skip-plt=%s\n", CLG_(clo).skip_plt ? "yes" : "no"); - my_fwrite(fd, buf, VG_(strlen)(buf)); - VG_(sprintf)(buf, "desc: Option: --collect-jumps=%s\n", + VG_(fprintf)(fp, "desc: Option: --collect-jumps=%s\n", CLG_(clo).collect_jumps ? "yes" : "no"); - my_fwrite(fd, buf, VG_(strlen)(buf)); - VG_(sprintf)(buf, "desc: Option: --separate-recs=%d\n", + VG_(fprintf)(fp, "desc: Option: --separate-recs=%d\n", CLG_(clo).separate_recursions); - my_fwrite(fd, buf, VG_(strlen)(buf)); - VG_(sprintf)(buf, "desc: Option: --separate-callers=%d\n", + VG_(fprintf)(fp, "desc: Option: --separate-callers=%d\n", CLG_(clo).separate_callers); - my_fwrite(fd, buf, VG_(strlen)(buf)); - VG_(sprintf)(buf, "desc: Option: --dump-bbs=%s\n", + VG_(fprintf)(fp, "desc: Option: --dump-bbs=%s\n", CLG_(clo).dump_bbs ? "yes" : "no"); - my_fwrite(fd, buf, VG_(strlen)(buf)); - VG_(sprintf)(buf, "desc: Option: --separate-threads=%s\n", + VG_(fprintf)(fp, "desc: Option: --separate-threads=%s\n", CLG_(clo).separate_threads ? "yes" : "no"); - my_fwrite(fd, buf, VG_(strlen)(buf)); #endif - (*CLG_(cachesim).getdesc)(buf); - my_fwrite(fd, buf, VG_(strlen)(buf)); + (*CLG_(cachesim).dump_desc)(fp); } - VG_(sprintf)(buf, "\ndesc: Timerange: Basic block %llu - %llu\n", + VG_(fprintf)(fp, "\ndesc: Timerange: Basic block %llu - %llu\n", bbs_done, CLG_(stat).bb_executions); - my_fwrite(fd, buf, VG_(strlen)(buf)); - VG_(sprintf)(buf, "desc: Trigger: %s\n", + VG_(fprintf)(fp, "desc: Trigger: %s\n", trigger ? trigger : "Program termination"); - my_fwrite(fd, buf, VG_(strlen)(buf)); #if 0 /* Output function specific config @@ -1365,28 +1271,23 @@ static int new_dumpfile(HChar buf[BUF_LEN], int tid, const HChar* trigger) fnc = fnc_table[i]; while (fnc) { if (fnc->skip) { - VG_(sprintf)(buf, "desc: Option: --fn-skip=%s\n", fnc->name); - my_fwrite(fd, buf, VG_(strlen)(buf)); + VG_(fprintf)(fp, "desc: Option: --fn-skip=%s\n", fnc->name); } if (fnc->dump_at_enter) { - VG_(sprintf)(buf, "desc: Option: --fn-dump-at-enter=%s\n", + VG_(fprintf)(fp, "desc: Option: --fn-dump-at-enter=%s\n", fnc->name); - my_fwrite(fd, buf, VG_(strlen)(buf)); } if (fnc->dump_at_leave) { - VG_(sprintf)(buf, "desc: Option: --fn-dump-at-leave=%s\n", + VG_(fprintf)(fp, "desc: Option: --fn-dump-at-leave=%s\n", fnc->name); - my_fwrite(fd, buf, VG_(strlen)(buf)); } if (fnc->separate_callers != CLG_(clo).separate_callers) { - VG_(sprintf)(buf, "desc: Option: --separate-callers%d=%s\n", + VG_(fprintf)(fp, "desc: Option: --separate-callers%d=%s\n", fnc->separate_callers, fnc->name); - my_fwrite(fd, buf, VG_(strlen)(buf)); } if (fnc->separate_recursions != CLG_(clo).separate_recursions) { - VG_(sprintf)(buf, "desc: Option: --separate-recs%d=%s\n", + VG_(fprintf)(fp, "desc: Option: --separate-recs%d=%s\n", fnc->separate_recursions, fnc->name); - my_fwrite(fd, buf, VG_(strlen)(buf)); } fnc = fnc->next; } @@ -1394,17 +1295,15 @@ static int new_dumpfile(HChar buf[BUF_LEN], int tid, const HChar* trigger) #endif /* "positions:" line */ - VG_(sprintf)(buf, "\npositions:%s%s%s\n", + VG_(fprintf)(fp, "\npositions:%s%s%s\n", CLG_(clo).dump_instr ? " instr" : "", CLG_(clo).dump_bb ? " bb" : "", CLG_(clo).dump_line ? " line" : ""); - my_fwrite(fd, buf, VG_(strlen)(buf)); /* "events:" line */ HChar *evmap = CLG_(eventmapping_as_string)(CLG_(dumpmap)); - VG_(sprintf)(buf, "events: %s\n", evmap); + VG_(fprintf)(fp, "events: %s\n", evmap); VG_(free)(evmap); - my_fwrite(fd, buf, VG_(strlen)(buf)); /* summary lines */ sum = CLG_(get_eventset_cost)( CLG_(sets).full ); @@ -1428,32 +1327,31 @@ static int new_dumpfile(HChar buf[BUF_LEN], int tid, const HChar* trigger) thr[t]->states.entry[0]->cost); } } - fprint_cost_ln(fd, "summary: ", CLG_(dumpmap), sum); + fprint_cost_ln(fp, "summary: ", CLG_(dumpmap), sum); /* all dumped cost will be added to total_fcc */ CLG_(init_cost_lz)( CLG_(sets).full, &dump_total_cost ); - my_fwrite(fd, "\n\n",2); + VG_(fprintf)(fp, "\n\n"); if (VG_(clo_verbosity) > 1) VG_(message)(Vg_DebugMsg, "Dump to %s\n", filename); - return fd; + return fp; } -static void close_dumpfile(int fd) +static void close_dumpfile(VgFile *fp) { - if (fd <0) return; + if (fp == NULL) return; - fprint_cost_ln(fd, "totals: ", CLG_(dumpmap), + fprint_cost_ln(fp, "totals: ", CLG_(dumpmap), dump_total_cost); - //fprint_fcc_ln(fd, "summary: ", &dump_total_fcc); + //fprint_fcc_ln(fp, "summary: ", &dump_total_fcc); CLG_(add_cost_lz)(CLG_(sets).full, &CLG_(total_cost), dump_total_cost); - fwrite_flush(); - VG_(close)(fd); + VG_(fclose)(fp); if (filename[0] == '.') { if (-1 == VG_(rename) (filename, filename+1)) { @@ -1468,7 +1366,6 @@ static void close_dumpfile(int fd) /* Helper for print_bbccs */ static const HChar* print_trigger; -static HChar print_buf[BUF_LEN]; static void print_bbccs_of_thread(thread_info* ti) { @@ -1478,8 +1375,8 @@ static void print_bbccs_of_thread(thread_info* ti) CLG_DEBUG(1, "+ print_bbccs(tid %d)\n", CLG_(current_tid)); - Int print_fd = new_dumpfile(print_buf, CLG_(current_tid), print_trigger); - if (print_fd <0) { + VgFile *print_fp = new_dumpfile(CLG_(current_tid), print_trigger); + if (print_fp == NULL) { CLG_DEBUG(1, "- print_bbccs(tid %d): No output...\n", CLG_(current_tid)); return; } @@ -1496,21 +1393,21 @@ static void print_bbccs_of_thread(thread_info* ti) (lastFnPos.rec_index != (*p)->rec_index))) { if (!CLG_(is_zero_cost)( CLG_(sets).full, ccSum[currSum].cost )) { /* no need to switch buffers, as position is the same */ - fprint_apos(print_fd, &(ccSum[currSum].p), &lastAPos, + fprint_apos(print_fp, &(ccSum[currSum].p), &lastAPos, lastFnPos.cxt->fn[0]->file); - fprint_fcost(print_fd, &ccSum[currSum], &lastAPos); + fprint_fcost(print_fp, &ccSum[currSum], &lastAPos); } if (ccSum[currSum].p.file != lastFnPos.cxt->fn[0]->file) { /* switch back to file of function */ - print_file(print_fd, "fe=", lastFnPos.cxt->fn[0]->file); + print_file(print_fp, "fe=", lastFnPos.cxt->fn[0]->file); } - my_fwrite(print_fd, "\n", 1); + VG_(fprintf)(print_fp, "\n"); } if (*p == 0) break; - if (print_fn_pos(print_fd, &lastFnPos, *p)) { + if (print_fn_pos(print_fp, &lastFnPos, *p)) { /* new function */ init_apos(&lastAPos, 0, 0, (*p)->cxt->fn[0]->file); @@ -1521,27 +1418,26 @@ static void print_bbccs_of_thread(thread_info* ti) if (CLG_(clo).dump_bbs) { /* FIXME: Specify Object of BB if different to object of fn */ - int i, pos = 0; + int i; ULong ecounter = (*p)->ecounter_sum; - pos = VG_(sprintf)(print_buf, "bb=%#lx ", (*p)->bb->offset); + VG_(fprintf)(print_fp, "bb=%#lx ", (*p)->bb->offset); for(i = 0; i<(*p)->bb->cjmp_count;i++) { - pos += VG_(sprintf)(print_buf+pos, "%d %llu ", + VG_(fprintf)(print_fp, "%d %llu ", (*p)->bb->jmp[i].instr, ecounter); ecounter -= (*p)->jmp[i].ecounter; } - VG_(sprintf)(print_buf+pos, "%d %llu\n", + VG_(fprintf)(print_fp, "%d %llu\n", (*p)->bb->instr_count, ecounter); - my_fwrite(print_fd, print_buf, VG_(strlen)(print_buf)); } - fprint_bbcc(print_fd, *p, &lastAPos); + fprint_bbcc(print_fp, *p, &lastAPos); p++; } - close_dumpfile(print_fd); + close_dumpfile(print_fp); VG_(free)(array); /* set counters of last dump */ diff --git a/callgrind/global.h b/callgrind/global.h index 03deb6179b..5f0c68ab22 100644 --- a/callgrind/global.h +++ b/callgrind/global.h @@ -128,11 +128,6 @@ struct _CommandLineOptions { #define MIN_LINE_SIZE 16 /* Size of various buffers used for storing strings */ -#define FILENAME_LEN VKI_PATH_MAX -#define FN_NAME_LEN 4096 /* for C++ code :-) */ -#define OBJ_NAME_LEN 256 -#define COSTS_LEN 512 /* at least 17x 64bit values */ -#define BUF_LEN 512 #define RESULTS_BUF_LEN 256 @@ -635,7 +630,7 @@ struct cachesim_if Bool (*parse_opt)(const HChar* arg); void (*post_clo_init)(void); void (*clear)(void); - void (*getdesc)(HChar* buf); + void (*dump_desc)(VgFile *fp); void (*printstat)(Int,Int,Int); void (*add_icost)(SimCost, BBCC*, InstrInfo*, ULong); void (*finish)(void); diff --git a/callgrind/sim.c b/callgrind/sim.c index 5e72e1c90f..ae9ccc1104 100644 --- a/callgrind/sim.c +++ b/callgrind/sim.c @@ -1434,12 +1434,11 @@ void cachesim_clear(void) } -static void cachesim_getdesc(HChar* buf) +static void cachesim_dump_desc(VgFile *fp) { - Int p; - p = VG_(sprintf)(buf, "\ndesc: I1 cache: %s\n", I1.desc_line); - p += VG_(sprintf)(buf+p, "desc: D1 cache: %s\n", D1.desc_line); - VG_(sprintf)(buf+p, "desc: LL cache: %s\n", LL.desc_line); + VG_(fprintf)(fp, "\ndesc: I1 cache: %s\n", I1.desc_line); + VG_(fprintf)(fp, "desc: D1 cache: %s\n", D1.desc_line); + VG_(fprintf)(fp, "desc: LL cache: %s\n", LL.desc_line); } static @@ -1781,7 +1780,7 @@ struct cachesim_if CLG_(cachesim) = { .parse_opt = cachesim_parse_opt, .post_clo_init = cachesim_post_clo_init, .clear = cachesim_clear, - .getdesc = cachesim_getdesc, + .dump_desc = cachesim_dump_desc, .printstat = cachesim_printstat, .add_icost = cachesim_add_icost, .finish = cachesim_finish,