Deleted Added
full compact
gprof.c (38194) gprof.c (38928)
1/*
2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 28 unchanged lines hidden (view full) ---

37 The Regents of the University of California. All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41#if 0
42static char sccsid[] = "@(#)gprof.c 8.1 (Berkeley) 6/6/93";
43#endif
44static const char rcsid[] =
1/*
2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 28 unchanged lines hidden (view full) ---

37 The Regents of the University of California. All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41#if 0
42static char sccsid[] = "@(#)gprof.c 8.1 (Berkeley) 6/6/93";
43#endif
44static const char rcsid[] =
45 "$Id: gprof.c,v 1.6 1997/07/15 08:04:40 charnier Exp $";
45 "$Id: gprof.c,v 1.7 1998/08/08 17:48:26 jdp Exp $";
46#endif /* not lint */
47
48#include <err.h>
49#include "gprof.h"
50
46#endif /* not lint */
47
48#include <err.h>
49#include "gprof.h"
50
51 /*
52 * things which get -E excluded by default.
53 */
54char *defaultEs[] = { "mcount" , "__mcleanup" , 0 };
51static int valcmp(const void *, const void *);
55
52
53
56static struct gmonhdr gmonhdr;
54static struct gmonhdr gmonhdr;
57static bool uflag;
58static int lflag;
59static int Lflag;
60
61main(argc, argv)
62 int argc;
63 char **argv;
64{
65 char **sp;
66 nltype **timesortnlp;
55static int lflag;
56static int Lflag;
57
58main(argc, argv)
59 int argc;
60 char **argv;
61{
62 char **sp;
63 nltype **timesortnlp;
64 char **defaultEs;
67
68 --argc;
69 argv++;
70 debug = 0;
71 bflag = TRUE;
72 while ( *argv != 0 && **argv == '-' ) {
73 (*argv)++;
74 switch ( **argv ) {

--- 80 unchanged lines hidden (view full) ---

155 }
156 if ( *argv != 0 ) {
157 gmonname = *argv;
158 argv++;
159 } else {
160 gmonname = GMONNAME;
161 }
162 /*
65
66 --argc;
67 argv++;
68 debug = 0;
69 bflag = TRUE;
70 while ( *argv != 0 && **argv == '-' ) {
71 (*argv)++;
72 switch ( **argv ) {

--- 80 unchanged lines hidden (view full) ---

153 }
154 if ( *argv != 0 ) {
155 gmonname = *argv;
156 argv++;
157 } else {
158 gmonname = GMONNAME;
159 }
160 /*
161 * get information from the executable file.
162 */
163 if (elf_getnfile(a_outname, &defaultEs) == -1 &&
164 aout_getnfile(a_outname, &defaultEs) == -1)
165 errx(1, "%s: bad format", a_outname);
166 /*
167 * sort symbol table.
168 */
169 qsort(nl, nname, sizeof(nltype), valcmp);
170 /*
163 * turn off default functions
164 */
171 * turn off default functions
172 */
165 for ( sp = &defaultEs[0] ; *sp ; sp++ ) {
173 for ( sp = defaultEs ; *sp ; sp++ ) {
166 Eflag = TRUE;
167 addlist( Elist , *sp );
168 eflag = TRUE;
169 addlist( elist , *sp );
170 }
171 /*
174 Eflag = TRUE;
175 addlist( Elist , *sp );
176 eflag = TRUE;
177 addlist( elist , *sp );
178 }
179 /*
172 * get information about a.out file.
173 */
174 getnfile();
175 /*
176 * get information about mon.out file(s).
177 */
178 do {
179 getpfile( gmonname );
180 if ( *argv != 0 ) {
181 gmonname = *argv;
182 }
183 } while ( *argv++ != 0 );

--- 34 unchanged lines hidden (view full) ---

218 /*
219 * print the index
220 */
221 printindex();
222 done();
223}
224
225 /*
180 * get information about mon.out file(s).
181 */
182 do {
183 getpfile( gmonname );
184 if ( *argv != 0 ) {
185 gmonname = *argv;
186 }
187 } while ( *argv++ != 0 );

--- 34 unchanged lines hidden (view full) ---

222 /*
223 * print the index
224 */
225 printindex();
226 done();
227}
228
229 /*
226 * Set up string and symbol tables from a.out.
227 * and optionally the text space.
228 * On return symbol table is sorted by value.
229 */
230getnfile()
231{
232 FILE *nfile;
233 int valcmp();
234
235 nfile = fopen( a_outname ,"r");
236 if (nfile == NULL) {
237 perror( a_outname );
238 done();
239 }
240 fread(&xbuf, 1, sizeof(xbuf), nfile);
241 if (N_BADMAG(xbuf)) {
242 warnx("%s: bad format", a_outname );
243 done();
244 }
245 getstrtab(nfile);
246 getsymtab(nfile);
247 gettextspace( nfile );
248 qsort(nl, nname, sizeof(nltype), valcmp);
249 fclose(nfile);
250# ifdef DEBUG
251 if ( debug & AOUTDEBUG ) {
252 register int j;
253
254 for (j = 0; j < nname; j++){
255 printf("[getnfile] 0X%08x\t%s\n", nl[j].value, nl[j].name);
256 }
257 }
258# endif DEBUG
259}
260
261getstrtab(nfile)
262 FILE *nfile;
263{
264
265 fseek(nfile, (long)(N_SYMOFF(xbuf) + xbuf.a_syms), 0);
266 if (fread(&ssiz, sizeof (ssiz), 1, nfile) == 0) {
267 warnx("%s: no string table (old format?)" , a_outname );
268 done();
269 }
270 strtab = calloc(ssiz, 1);
271 if (strtab == NULL) {
272 warnx("%s: no room for %d bytes of string table", a_outname , ssiz);
273 done();
274 }
275 if (fread(strtab+sizeof(ssiz), ssiz-sizeof(ssiz), 1, nfile) != 1) {
276 warnx("%s: error reading string table", a_outname );
277 done();
278 }
279}
280
281 /*
282 * Read in symbol table
283 */
284getsymtab(nfile)
285 FILE *nfile;
286{
287 register long i;
288 int askfor;
289 struct nlist nbuf;
290
291 /* pass1 - count symbols */
292 fseek(nfile, (long)N_SYMOFF(xbuf), 0);
293 nname = 0;
294 for (i = xbuf.a_syms; i > 0; i -= sizeof(struct nlist)) {
295 fread(&nbuf, sizeof(nbuf), 1, nfile);
296 if ( ! funcsymbol( &nbuf ) ) {
297 continue;
298 }
299 nname++;
300 }
301 if (nname == 0) {
302 warnx("%s: no symbols", a_outname );
303 done();
304 }
305 askfor = nname + 1;
306 nl = (nltype *) calloc( askfor , sizeof(nltype) );
307 if (nl == 0) {
308 warnx("no room for %d bytes of symbol table", askfor * sizeof(nltype) );
309 done();
310 }
311
312 /* pass2 - read symbols */
313 fseek(nfile, (long)N_SYMOFF(xbuf), 0);
314 npe = nl;
315 nname = 0;
316 for (i = xbuf.a_syms; i > 0; i -= sizeof(struct nlist)) {
317 fread(&nbuf, sizeof(nbuf), 1, nfile);
318 if ( ! funcsymbol( &nbuf ) ) {
319# ifdef DEBUG
320 if ( debug & AOUTDEBUG ) {
321 printf( "[getsymtab] rejecting: 0x%x %s\n" ,
322 nbuf.n_type , strtab + nbuf.n_un.n_strx );
323 }
324# endif DEBUG
325 continue;
326 }
327 npe->value = nbuf.n_value;
328 npe->name = strtab+nbuf.n_un.n_strx;
329# ifdef DEBUG
330 if ( debug & AOUTDEBUG ) {
331 printf( "[getsymtab] %d %s 0x%08x\n" ,
332 nname , npe -> name , npe -> value );
333 }
334# endif DEBUG
335 npe++;
336 nname++;
337 }
338 npe->value = -1;
339}
340
341 /*
342 * read in the text space of an a.out file
343 */
344gettextspace( nfile )
345 FILE *nfile;
346{
347
348 if ( cflag == 0 ) {
349 return;
350 }
351 textspace = (u_char *) malloc( xbuf.a_text );
352 if ( textspace == 0 ) {
353 warnx("ran out room for %d bytes of text space: can't do -c" ,
354 xbuf.a_text );
355 return;
356 }
357 (void) fseek( nfile , N_TXTOFF( xbuf ) , 0 );
358 if ( fread( textspace , 1 , xbuf.a_text , nfile ) != xbuf.a_text ) {
359 warnx("couldn't read text space: can't do -c");
360 free( textspace );
361 textspace = 0;
362 return;
363 }
364}
365 /*
366 * information from a gmon.out file is in two parts:
367 * an array of sampling hits within pc ranges,
368 * and the arcs.
369 */
370getpfile(filename)
371 char *filename;
372{
373 FILE *pfile;

--- 152 unchanged lines hidden (view full) ---

526 arc.raw_frompc , arc.raw_selfpc , arc.raw_count );
527 }
528# endif DEBUG
529 }
530 }
531 fclose( sfile );
532}
533
230 * information from a gmon.out file is in two parts:
231 * an array of sampling hits within pc ranges,
232 * and the arcs.
233 */
234getpfile(filename)
235 char *filename;
236{
237 FILE *pfile;

--- 152 unchanged lines hidden (view full) ---

390 arc.raw_frompc , arc.raw_selfpc , arc.raw_count );
391 }
392# endif DEBUG
393 }
394 }
395 fclose( sfile );
396}
397
534valcmp(p1, p2)
535 nltype *p1, *p2;
398static int
399valcmp(v1, v2)
400 const void *v1;
401 const void *v2;
536{
402{
403 const nltype *p1 = (const nltype *)v1;
404 const nltype *p2 = (const nltype *)v2;
405
537 if ( p1 -> value < p2 -> value ) {
538 return LESSTHAN;
539 }
540 if ( p1 -> value > p2 -> value ) {
541 return GREATERTHAN;
542 }
543 return EQUALTO;
544}

--- 161 unchanged lines hidden (view full) ---

706 nlp->svalue, nlp->svalue + UNITS_TO_CODE);
707 }
708# endif DEBUG
709 nlp->svalue += UNITS_TO_CODE;
710 }
711 }
712}
713
406 if ( p1 -> value < p2 -> value ) {
407 return LESSTHAN;
408 }
409 if ( p1 -> value > p2 -> value ) {
410 return GREATERTHAN;
411 }
412 return EQUALTO;
413}

--- 161 unchanged lines hidden (view full) ---

575 nlp->svalue, nlp->svalue + UNITS_TO_CODE);
576 }
577# endif DEBUG
578 nlp->svalue += UNITS_TO_CODE;
579 }
580 }
581}
582
714bool
715funcsymbol( nlistp )
716 struct nlist *nlistp;
717{
718 char *name, c;
719
720 /*
721 * must be a text symbol,
722 * and static text symbols don't qualify if aflag set.
723 */
724 if ( ! ( ( nlistp -> n_type == ( N_TEXT | N_EXT ) )
725 || ( ( nlistp -> n_type == N_TEXT ) && ( aflag == 0 ) ) ) ) {
726 return FALSE;
727 }
728 /*
729 * name must start with an underscore if uflag is set.
730 * can't have any `funny' characters in name,
731 * where `funny' means `.' (.o file names)
732 * need to make an exception for sparc .mul & co.
733 * perhaps we should just drop this code entirely...
734 */
735 name = strtab + nlistp -> n_un.n_strx;
736 if ( uflag && *name != '_' )
737 return FALSE;
738#ifdef sparc
739 if ( *name == '.' ) {
740 char *p = name + 1;
741 if ( *p == 'u' )
742 p++;
743 if ( strcmp ( p, "mul" ) == 0 || strcmp ( p, "div" ) == 0 ||
744 strcmp ( p, "rem" ) == 0 )
745 return TRUE;
746 }
747#endif
748 while ( c = *name++ ) {
749 if ( c == '.' ) {
750 return FALSE;
751 }
752 }
753 return TRUE;
754}
755
756done()
757{
758
759 exit(0);
760}
583done()
584{
585
586 exit(0);
587}