tools/jprof/coff.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/tools/jprof/coff.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,99 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +#include "leaky.h"
     1.9 +
    1.10 +#ifdef USE_COFF
    1.11 +
    1.12 +#define	LANGUAGE_C
    1.13 +#include <sym.h>
    1.14 +#include <cmplrs/stsupport.h>
    1.15 +#include <symconst.h>
    1.16 +#include <filehdr.h>
    1.17 +#include <ldfcn.h>
    1.18 +#include <string.h>
    1.19 +#include <stdlib.h>
    1.20 +
    1.21 +#ifdef IRIX4
    1.22 +extern "C" {
    1.23 +    extern char *demangle(char const* in);
    1.24 +};
    1.25 +#else
    1.26 +#include <dem.h>
    1.27 +#endif
    1.28 +
    1.29 +static char *Demangle(char *rawName)
    1.30 +{
    1.31 +#ifdef IRIX4
    1.32 +    return strdup(demangle(rawName));
    1.33 +#else
    1.34 +    char namebuf[4000];
    1.35 +    demangle(rawName, namebuf);
    1.36 +    return strdup(namebuf);
    1.37 +#endif
    1.38 +}
    1.39 +
    1.40 +void leaky::readSymbols(const char *fileName)
    1.41 +{
    1.42 +    LDFILE *ldptr;
    1.43 +
    1.44 +    ldptr = ldopen(fileName, nullptr);
    1.45 +    if (!ldptr) {
    1.46 +	fprintf(stderr, "%s: unable to open \"%s\"\n", applicationName,
    1.47 +		fileName);
    1.48 +	exit(-1);
    1.49 +    }
    1.50 +    if (PSYMTAB(ldptr) == 0) {
    1.51 +	fprintf(stderr, "%s: \"%s\": has no symbol table\n", applicationName,
    1.52 +		fileName);
    1.53 +	exit(-1);
    1.54 +    }
    1.55 +
    1.56 +    long isymMax = SYMHEADER(ldptr).isymMax;
    1.57 +    long iextMax = SYMHEADER(ldptr).iextMax;
    1.58 +    long iMax = isymMax + iextMax;
    1.59 +
    1.60 +    long alloced = 10000;
    1.61 +    Symbol* syms = (Symbol*) malloc(sizeof(Symbol) * 10000);
    1.62 +    Symbol* sp = syms;
    1.63 +    Symbol* last = syms + alloced;
    1.64 +    SYMR symr;
    1.65 +
    1.66 +    for (long isym = 0; isym < iMax; isym++) {
    1.67 +	if (ldtbread(ldptr, isym, &symr) != SUCCESS) {
    1.68 +	    fprintf(stderr, "%s: can't read symbol #%d\n", applicationName,
    1.69 +		    isym);
    1.70 +	    exit(-1);
    1.71 +	}
    1.72 +	if (isym < isymMax) {
    1.73 +	    if ((symr.st == stStaticProc)
    1.74 +		|| ((symr.st == stProc) &&
    1.75 +		    ((symr.sc == scText) || (symr.sc == scAbs)))
    1.76 +		|| ((symr.st == stBlock) &&
    1.77 +		    (symr.sc == scText))) {
    1.78 +		// Text symbol. Set name field to point to the symbol name
    1.79 +		sp->name = Demangle(ldgetname(ldptr, &symr));
    1.80 +		sp->address = symr.value;
    1.81 +		sp++;
    1.82 +		if (sp >= last) {
    1.83 +		    long n = alloced + 10000;
    1.84 +		    syms = (Symbol*)
    1.85 +			realloc(syms, (size_t) (sizeof(Symbol) * n));
    1.86 +		    last = syms + n;
    1.87 +		    sp = syms + alloced;
    1.88 +		    alloced = n;
    1.89 +		}
    1.90 +	    }
    1.91 +	}
    1.92 +    }
    1.93 +
    1.94 +    int interesting = sp - syms;
    1.95 +    if (!quiet) {
    1.96 +	printf("Total of %d symbols\n", interesting);
    1.97 +    }
    1.98 +    usefulSymbols = interesting;
    1.99 +    externalSymbols = syms;
   1.100 +}
   1.101 +
   1.102 +#endif /* USE_COFF */

mercurial