📄 bprint
字号:
#include <assert.h>#include <ctype.h>#include <stdio.h>#include <stdlib.h>#include <string.h>/* bprint [ -c | -Idir... | -f | -b | -n ] [ file... ] * annotate listings of files with prof.out data */static char rcsid[] = "$Id: bprint.nw,v 1.5 1997/05/29 00:43:26 drh Exp $";#define NDIRS (sizeof dirs/sizeof dirs[0] - 1)#define NEW(p,a) ((p) = alloc(sizeof *(p)))#define newarray(m,n,a) alloc((m)*(n))#define NELEMS(a) ((int)(sizeof (a)/sizeof ((a)[0])))#define MAXTOKEN 64struct count { /* count data: */ int x, y; /* source coordinate */ int count; /* associated execution count */};char *progname;int number;char *dirs[20];int fcount;struct file { /* per-file prof.out data: */ struct file *link; /* link to next file */ char *name; /* file name */ int size; /* size of counts[] */ int count; /* counts[0..count-1] hold valid data */ struct count *counts; /* count data */ struct func { /* function data: */ struct func *link; /* link to next function */ char *name; /* function name */ struct count count; /* total number of calls */ struct caller { /* caller data: */ struct caller *link; /* link to next caller */ char *name; /* caller's name */ char *file; /* call site: file, x, y */ int x, y; int count; /* number of calls from this site */ } *callers; } *funcs; /* list of functions */} *filelist;FILE *fp;extern int process(char *);extern int findfunc(char *, char *);extern int findcount(char *, int, int);void *alloc(unsigned);char *string(char *);int process(char *);void emitdata(char *);void printfile(struct file *, int);void printfuncs(struct file *, int);/* alloc - allocate n bytes or die */void *alloc(unsigned n) { void *new = malloc(n); assert(new); return new;}/* emitdata - write prof.out data to file */void emitdata(char *file) { FILE *fp; if (fp = fopen(file, "w")) { struct file *p; for (p = filelist; p; p = p->link) { int i; struct func *q; struct caller *r; fprintf(fp, "1\n%s\n", p->name); for (i = 0, q = p->funcs; q; i++, q = q->link) if (r = q->callers) for (i--; r; r = r->link) i++; fprintf(fp, "%d\n", i); for (q = p->funcs; q; q = q->link) if (q->count.count == 0 || !q->callers) fprintf(fp, "%s 1 %d %d %d ? ? 0 0\n", q->name, q->count.x, q->count.y, q->count.count); else for (r = q->callers; r; r = r->link) fprintf(fp, "%s 1 %d %d %d %s %s %d %d\n", q->name, q->count.x, q->count.y, r->count, r->name, r->file, r->x, r->y); fprintf(fp, "%d\n", p->count); for (i = 0; i < p->count; i++) fprintf(fp, "1 %d %d %d\n", p->counts[i].x, p->counts[i].y, p->counts[i].count); } fclose(fp); } else fprintf(stderr, "%s: can't create `%s'\n", progname, file);}/* openfile - open name for reading, searching -I directories */FILE *openfile(char *name) { int i; FILE *fp; if (*name != '/') for (i = 0; dirs[i]; i++) { char buf[200]; sprintf(buf, "%s/%s", dirs[i], name); if (fp = fopen(buf, "r")) return fp; } return fopen(name, "r");}/* printfile - print annotated listing for p */void printfile(struct file *p, int nf) { int lineno; FILE *fp; char *s, buf[512]; struct count *u = p->counts, *r, *uend; if (u == 0 || p->count <= 0) return; uend = &p->counts[p->count]; if ((fp = openfile(p->name)) == NULL) { fprintf(stderr, "%s: can't open `%s'\n", progname, p->name); return; } if (nf) printf("%s%s:\n\n", nf == 1 ? "" : "\f", p->name); for (lineno = 1; fgets(buf, sizeof buf, fp); lineno++) { if (number) printf("%d\t", lineno); while (u < uend && u->y < lineno) u++; for (s = buf; *s; ) { char *t = s + 1; while (u < uend && u->y == lineno && u->x < s - buf) u++; if (isalnum(*s) || *s == '_') while (isalnum(*t) || *t == '_') t++; while (u < uend && u->y == lineno && u->x < t - buf) { printf("<%d>", u->count); for (r = u++; u < uend && u->x == r->x && u->y == r->y && u->count == r->count; u++) ; } while (s < t) putchar(*s++); } if (*s) printf("%s", s); } fclose(fp);}/* printfuncs - summarize data for functions in p */void printfuncs(struct file *p, int nf) { struct func *q; if (nf) printf("%s:\n", p->name); for (q = p->funcs; q; q = q->link) if (fcount <= 1 || q->count.count == 0 || !q->callers) printf("%d\t%s\n", q->count.count, q->name); else { struct caller *r; for (r = q->callers; r; r = r->link) printf("%d\t%s\tfrom %s\tin %s:%d.%d\n", r->count, q->name, r->name, r->file, r->y, r->x + 1); } }/* string - save a copy of str, if necessary */char *string(char *str) { static struct string { struct string *link; char str[1]; } *list; struct string *p; for (p = list; p; p = p->link) if (strcmp(p->str, str) == 0) return p->str; p = (struct string *)alloc(strlen(str) + sizeof *p); strcpy(p->str, str); p->link = list; list = p; return p->str;}/* acaller - add caller and site (file,x,y) to callee's callers list */static void acaller(char *caller, char *file, int x, int y, int count, struct func *callee) { struct caller *q; assert(callee); for (q = callee->callers; q && (caller != q->name || file != q->file || x != q->x || y != q->y); q = q->link) ; if (!q) { struct caller **r; NEW(q, PERM); q->name = caller; q->file = file; q->x = x; q->y = y; q->count = 0; for (r = &callee->callers; *r && (strcmp(q->name, (*r)->name) > 0 || strcmp(q->file, (*r)->file) > 0 || q->y > (*r)->y || q->y > (*r)->y); r = &(*r)->link) ; q->link = *r; *r = q; } q->count += count;}/* compare - return <0, 0, >0 if a<b, a==b, a>b, resp. */static int compare(struct count *a, struct count *b) { if (a->y == b->y) return a->x - b->x; return a->y - b->y;}/* findfile - return file name's file list entry, or 0 */static struct file *findfile(char *name) { struct file *p; for (p = filelist; p; p = p->link) if (p->name == name) return p; return 0;}/* afunction - add function name and its data to file's function list */static struct func *afunction(char *name, char *file, int x, int y, int count) { struct file *p = findfile(file); struct func *q;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -