diff --git a/tools/codesighs/Makefile.in b/tools/codesighs/Makefile.in new file mode 100644 index 000000000000..7f7b50c0c570 --- /dev/null +++ b/tools/codesighs/Makefile.in @@ -0,0 +1,42 @@ +# +# The contents of this file are subject to the Netscape Public +# License Version 1.1 (the "License"); you may not use this file +# except in compliance with the License. You may obtain a copy of +# the License at http://www.mozilla.org/NPL/ +# +# Software distributed under the License is distributed on an "AS +# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or +# implied. See the License for the specific language governing +# rights and limitations under the License. +# +# The Original Code is mozilla.org code. +# +# The Initial Developer of the Original Code is Netscape +# Communications Corporation. Portions created by Netscape are +# Copyright (C) 1998 Netscape Communications Corporation. All +# Rights Reserved. +# +# Contributor(s): +# Garrett Arch Blythe +# + +DEPTH = ../.. +topsrcdir = @top_srcdir@ +srcdir = @srcdir@ +VPATH = @srcdir@ + +include $(DEPTH)/config/autoconf.mk + +REQUIRES = $(NULL) + +SIMPLECSRCS += \ + msmap2tsv.c \ + codesighs.c \ + maptsvdifftool.c \ + $(NULL) + +SIMPLE_PROGRAMS = $(SIMPLECSRCS:.c=$(BIN_SUFFIX)) + +include $(topsrcdir)/config/config.mk + +include $(topsrcdir)/config/rules.mk diff --git a/tools/codesighs/autosummary.win.bash b/tools/codesighs/autosummary.win.bash new file mode 100755 index 000000000000..e3cc7d16e482 --- /dev/null +++ b/tools/codesighs/autosummary.win.bash @@ -0,0 +1,142 @@ +#!/bin/bash + +# +# Assumed to be run from the parent directory of the mozilla source tree. +# + + +# +# A little help for my friends. +# +if [ "-h" == "$1" ];then + SHOWHELP="1" +fi +if [ "--help" == "$1" ];then + SHOWHELP="1" +fi +if [ "" == "$1" ]; then + SHOWHELP="1" +fi +if [ "" == "$2" ]; then + SHOWHELP="1" +fi +if [ "" == "$3" ]; then + SHOWHELP="1" +fi + + +# +# Show the help if required. +# +if [ $SHOWHELP ]; then + echo "usage: $0 " + echo " is a file that will receive the results of this run." + echo " This file can be used in a future run as the old results." + echo " is a results file from a previous run." + echo " It is used to diff with current results and come up with a summary" + echo " of changes." + echo " It is OK if the file does not exist, just supply the argument." + echo " is a file which will contain a human readable report." + echo " This file is most useful by providing more information than the" + echo " normally single digit output of this script." + echo "" + echo "Run this command from the parent directory of the mozilla tree." + echo "" + echo "This command will output two numbers to stdout that will represent" + echo " the total size of all code and data, and a delta from the prior." + echo " the old results." + echo "For much more detail to size drifts refer to the summary report." + exit +fi + + +# +# Exclude certain path patterns. +# Be sure to modify the grep command below as well. +# +EXCLUDE_PATH_01="/test/" +EXCLUDE_PATH_02="/tests/" +EXCLUDE_PATH_03="/tools/" +EXCLUDE_PATH_04="/config/" +EXCLUDE_PATH_05="IBMNEC.map" + + +# +# Stash our arguments away. +# +COPYSORTTSV="$1" +OLDTSVFILE="$2" +SUMMARYFILE="$3" + + +# +# Create our temporary directory. +# +TMPDIR="$TMP/codesighs.$PPID" +mkdir -p $TMPDIR + + +# +# Find all map files. +# +ALLMAPSFILE="$TMPDIR/allmaps.list" +find ./mozilla -type f -name *.map > $ALLMAPSFILE + + +# +# Reduce the map files to a revelant set. +# +MAPSFILE="$TMPDIR/maps.list" +grep -v $EXCLUDE_PATH_01 < $ALLMAPSFILE | grep -v $EXCLUDE_PATH_02 | grep -v $EXCLUDE_PATH_03 | grep -v $EXCLUDE_PATH_04 | grep -v $EXCLUDE_PATH_05 > $MAPSFILE + + +# +# Produce the TSV output. +# +RAWTSVFILE="$TMPDIR/raw.tsv" +xargs -n 1 ./mozilla/dist/bin/msmap2tsv --input < $MAPSFILE > $RAWTSVFILE + + +# +# Sort the TSV output for useful diffing and eyeballing in general. +# +sort -r $RAWTSVFILE > $COPYSORTTSV + + +# +# If a historical file was specified, diff it with our sorted tsv values. +# Run it through a tool to summaries the diffs to the module +# level report. +# +rm -f $SUMMARYFILE +DIFFFILE="$TMPDIR/diff.txt" +if [ -e $OLDTSVFILE ]; then + diff $OLDTSVFILE $COPYSORTTSV > $DIFFFILE + ./mozilla/dist/bin/maptsvdifftool --input $DIFFFILE >> $SUMMARYFILE + echo "" >> $SUMMARYFILE + echo "" >> $SUMMARYFILE +fi + + +# +# Generate the module level report from our new data. +# +./mozilla/dist/bin/codesighs --modules --input $COPYSORTTSV >> $SUMMARYFILE + + +# +# Output our numbers, that will let tinderbox specify everything all +# at once. +# First number is in fact the total size of all code and data in the map +# files parsed. +# Second number, if present, is growth/shrinkage. +# +./mozilla/dist/bin/codesighs --totalonly --input $COPYSORTTSV +if [ -e $DIFFFILE ]; then + ./mozilla/dist/bin/maptsvdifftool --totalonly --input $DIFFFILE +fi + +# +# Remove our temporary directory. +# +rm -rf $TMPDIR diff --git a/tools/codesighs/codesighs.c b/tools/codesighs/codesighs.c new file mode 100644 index 000000000000..14ffcc4b75d4 --- /dev/null +++ b/tools/codesighs/codesighs.c @@ -0,0 +1,563 @@ +#include +#include +#include +#include +#include + +#define ERROR_REPORT(num, val, msg) fprintf(stderr, "error(%d):\t\"%s\"\t%s\n", (num), (val), (msg)); +#define CLEANUP(ptr) do { if(NULL != ptr) { free(ptr); ptr = NULL; } } while(0) + + +typedef struct __struct_Options +/* +** Options to control how we perform. +** +** mProgramName Used in help text. +** mInput File to read for input. +** Default is stdin. +** mInputName Name of the file. +** mOutput Output file, append. +** Default is stdout. +** mOutputName Name of the file. +** mHelp Wether or not help should be shown. +** mModules Output module by module information. +** mTotalOnly Only output one number, the total. +*/ +{ + const char* mProgramName; + FILE* mInput; + char* mInputName; + FILE* mOutput; + char* mOutputName; + int mHelp; + int mModules; + int mTotalOnly; +} +Options; + + +typedef struct __struct_Switch +/* +** Command line options. +*/ +{ + const char* mLongName; + const char* mShortName; + int mHasValue; + const char* mValue; + const char* mDescription; +} +Switch; + +#define DESC_NEWLINE "\n\t\t" + +static Switch gInputSwitch = {"--input", "-i", 1, NULL, "Specify input file." DESC_NEWLINE "stdin is default."}; +static Switch gOutputSwitch = {"--output", "-o", 1, NULL, "Specify output file." DESC_NEWLINE "Appends if file exists." DESC_NEWLINE "stdout is default."}; +static Switch gHelpSwitch = {"--help", "-h", 0, NULL, "Information on usage."}; +static Switch gModuleSwitch = {"--modules", "-m", 0, NULL, "Output individual module numbers as well."}; +static Switch gTotalSwitch = {"--totalonly", "-t", 0, NULL, "Output only one number." DESC_NEWLINE "The total overall size." DESC_NEWLINE "Overrides other output options."}; + +static Switch* gSwitches[] = { + &gInputSwitch, + &gOutputSwitch, + &gModuleSwitch, + &gTotalSwitch, + &gHelpSwitch +}; + + +typedef struct __struct_SizeStats +/* +** Track totals. +** +** mData Size of data. +** mCode Size of code. +*/ +{ + unsigned long mData; + unsigned long mCode; +} +SizeStats; + + +typedef struct __struct_ModuleStats +/* +** Track module level information. +** +** mModule Module name. +** mSize Size of module. +*/ +{ + char* mModule; + SizeStats mSize; +} +ModuleStats; + +typedef enum __enum_SegmentClass +{ + CODE, + DATA +} +SegmentClass; + + +static int moduleCompare(const void* in1, const void* in2) +/* +** qsort helper function. +*/ +{ + int retval = 0; + + const ModuleStats* one = (const ModuleStats*)in1; + const ModuleStats* two = (const ModuleStats*)in2; + unsigned long oneSize = one->mSize.mCode + one->mSize.mData; + unsigned long twoSize = two->mSize.mCode + two->mSize.mData; + + if(oneSize < twoSize) + { + retval = 1; + } + else if(oneSize > twoSize) + { + retval = -1; + } + + return retval; +} + + +int codesighs(Options* inOptions) +/* +** Output a simplistic report based on our options. +*/ +{ + int retval = 0; + char lineBuffer[0x500]; + int scanRes = 0; + unsigned long size; + char segClass[0x10]; + char scope[0x10]; + char module[0x100]; + char segment[0x40]; + char object[0x100]; + char symbol[0x200]; + SizeStats overall; + ModuleStats* modules = NULL; + unsigned moduleCount = 0; + + memset(&overall, 0, sizeof(overall)); + + /* + ** Read the file line by line, regardless of number of fields. + ** We assume tab seperated value formatting, at least 7 lead values: + ** size class scope module segment object symbol .... + */ + while(0 == retval && NULL != fgets(lineBuffer, sizeof(lineBuffer), inOptions->mInput)) + { + scanRes = sscanf(lineBuffer, + "%x\t%s\t%s\t%s\t%s\t%s\t%s", + &size, + segClass, + scope, + module, + segment, + object, + symbol); + + if(7 == scanRes) + { + SegmentClass segmentClass = CODE; + + if(0 == strcmp(segClass, "DATA")) + { + segmentClass = DATA; + } + else if(0 == strcmp(segClass, "CODE")) + { + segmentClass = CODE; + } + else + { + retval = __LINE__; + ERROR_REPORT(retval, segClass, "Unable to determine segment class."); + } + + if(0 == retval) + { + /* + ** Update overall totals. + */ + if(CODE == segmentClass) + { + overall.mCode += size; + } + else if(DATA == segmentClass) + { + overall.mData += size; + } + + /* + ** See what else we should be tracking. + */ + if(0 == inOptions->mTotalOnly) + { + if(inOptions->mModules) + { + unsigned index = 0; + + /* + ** Find the module to modify. + */ + for(index = 0; index < moduleCount; index++) + { + if(0 == strcmp(modules[index].mModule, module)) + { + break; + } + } + + /* + ** If the index is the same as the count, we need to + ** add a new module. + */ + if(index == moduleCount) + { + void* moved = NULL; + + moved = realloc(modules, sizeof(ModuleStats) * (moduleCount + 1)); + if(NULL != moved) + { + modules = (ModuleStats*)moved; + moduleCount++; + + memset(modules + index, 0, sizeof(ModuleStats)); + modules[index].mModule = strdup(module); + if(NULL == modules[index].mModule) + { + retval = __LINE__; + ERROR_REPORT(retval, module, "Unable to duplicate string."); + } + } + else + { + retval = __LINE__; + ERROR_REPORT(retval, inOptions->mProgramName, "Unable to allocate module memory."); + } + } + + if(0 == retval) + { + if(CODE == segmentClass) + { + modules[index].mSize.mCode += size; + } + else if(DATA == segmentClass) + { + modules[index].mSize.mData += size; + } + } + } + } + } + } + else + { + retval = __LINE__; + ERROR_REPORT(retval, inOptions->mInputName, "Problem extracting values from file."); + } + } + + if(0 == retval && 0 != ferror(inOptions->mInput)) + { + retval = __LINE__; + ERROR_REPORT(retval, inOptions->mInputName, "Unable to read file."); + } + + /* + ** If all went well, time to report. + */ + if(0 == retval) + { + if(inOptions->mTotalOnly) + { + fprintf(inOptions->mOutput, "%u\n", overall.mCode + overall.mData); + } + else + { + fprintf(inOptions->mOutput, "Overall Size\n"); + fprintf(inOptions->mOutput, "\tTotal:\t%10u\n", overall.mCode + overall.mData); + fprintf(inOptions->mOutput, "\tCode:\t%10u\n", overall.mCode); + fprintf(inOptions->mOutput, "\tData:\t%10u\n", overall.mData); + } + + /* + ** Check options to see what else we should output. + */ + if(inOptions->mModules && moduleCount) + { + unsigned loop = 0; + + /* + ** Sort the modules by their size. + */ + qsort(modules, (size_t)moduleCount, sizeof(ModuleStats), moduleCompare); + + /* + ** Output each one. + ** Might as well clean up while we go too. + */ + for(loop = 0; loop < moduleCount; loop++) + { + fprintf(inOptions->mOutput, "\n"); + fprintf(inOptions->mOutput, "%s\n", modules[loop].mModule); + fprintf(inOptions->mOutput, "\tTotal:\t%10u\n", modules[loop].mSize.mCode + modules[loop].mSize.mData); + fprintf(inOptions->mOutput, "\tCode:\t%10u\n", modules[loop].mSize.mCode); + fprintf(inOptions->mOutput, "\tData:\t%10u\n", modules[loop].mSize.mData); + + CLEANUP(modules[loop].mModule); + } + + /* + ** Done with modules. + */ + CLEANUP(modules); + moduleCount = 0; + } + } + + return retval; +} + + +int initOptions(Options* outOptions, int inArgc, char** inArgv) +/* +** returns int 0 if successful. +*/ +{ + int retval = 0; + int loop = 0; + int switchLoop = 0; + int match = 0; + const int switchCount = sizeof(gSwitches) / sizeof(gSwitches[0]); + Switch* current = NULL; + + /* + ** Set any defaults. + */ + memset(outOptions, 0, sizeof(Options)); + outOptions->mProgramName = inArgv[0]; + outOptions->mInput = stdin; + outOptions->mInputName = strdup("stdin"); + outOptions->mOutput = stdout; + outOptions->mOutputName = strdup("stdout"); + + if(NULL == outOptions->mOutputName || NULL == outOptions->mInputName) + { + retval = __LINE__; + ERROR_REPORT(retval, "stdin/stdout", "Unable to strdup."); + } + + /* + ** Go through and attempt to do the right thing. + */ + for(loop = 1; loop < inArgc && 0 == retval; loop++) + { + match = 0; + current = NULL; + + for(switchLoop = 0; switchLoop < switchCount && 0 == retval; switchLoop++) + { + if(0 == strcmp(gSwitches[switchLoop]->mLongName, inArgv[loop])) + { + match = __LINE__; + } + else if(0 == strcmp(gSwitches[switchLoop]->mShortName, inArgv[loop])) + { + match = __LINE__; + } + + if(match) + { + if(gSwitches[switchLoop]->mHasValue) + { + /* + ** Attempt to absorb next option to fullfill value. + */ + if(loop + 1 < inArgc) + { + loop++; + + current = gSwitches[switchLoop]; + current->mValue = inArgv[loop]; + } + } + else + { + current = gSwitches[switchLoop]; + } + + break; + } + } + + if(0 == match) + { + outOptions->mHelp = __LINE__; + retval = __LINE__; + ERROR_REPORT(retval, inArgv[loop], "Unknown command line switch."); + } + else if(NULL == current) + { + outOptions->mHelp = __LINE__; + retval = __LINE__; + ERROR_REPORT(retval, inArgv[loop], "Command line switch requires a value."); + } + else + { + /* + ** Do something based on address/swtich. + */ + if(current == &gInputSwitch) + { + CLEANUP(outOptions->mInputName); + if(NULL != outOptions->mInput && stdin != outOptions->mInput) + { + fclose(outOptions->mInput); + outOptions->mInput = NULL; + } + + outOptions->mInput = fopen(current->mValue, "r"); + if(NULL == outOptions->mInput) + { + retval = __LINE__; + ERROR_REPORT(retval, current->mValue, "Unable to open input file."); + } + else + { + outOptions->mInputName = strdup(current->mValue); + if(NULL == outOptions->mInputName) + { + retval = __LINE__; + ERROR_REPORT(retval, current->mValue, "Unable to strdup."); + } + } + } + else if(current == &gOutputSwitch) + { + CLEANUP(outOptions->mOutputName); + if(NULL != outOptions->mOutput && stdout != outOptions->mOutput) + { + fclose(outOptions->mOutput); + outOptions->mOutput = NULL; + } + + outOptions->mOutput = fopen(current->mValue, "a"); + if(NULL == outOptions->mOutput) + { + retval = __LINE__; + ERROR_REPORT(retval, current->mValue, "Unable to open output file."); + } + else + { + outOptions->mOutputName = strdup(current->mValue); + if(NULL == outOptions->mOutputName) + { + retval = __LINE__; + ERROR_REPORT(retval, current->mValue, "Unable to strdup."); + } + } + } + else if(current == &gHelpSwitch) + { + outOptions->mHelp = __LINE__; + } + else if(current == &gModuleSwitch) + { + outOptions->mModules = __LINE__; + } + else if(current == &gTotalSwitch) + { + outOptions->mTotalOnly = __LINE__; + } + else + { + retval = __LINE__; + ERROR_REPORT(retval, current->mLongName, "No hanlder for command line switch."); + } + } + } + + return retval; +} + + +void cleanOptions(Options* inOptions) +/* +** Clean up any open handles. +*/ +{ + CLEANUP(inOptions->mInputName); + if(NULL != inOptions->mInput && stdin != inOptions->mInput) + { + fclose(inOptions->mInput); + } + CLEANUP(inOptions->mOutputName); + if(NULL != inOptions->mOutput && stdout != inOptions->mOutput) + { + fclose(inOptions->mOutput); + } + + memset(inOptions, 0, sizeof(Options)); +} + + +void showHelp(Options* inOptions) +/* +** Show some simple help text on usage. +*/ +{ + int loop = 0; + const int switchCount = sizeof(gSwitches) / sizeof(gSwitches[0]); + const char* valueText = NULL; + + printf("usage:\t%s [arguments]\n", inOptions->mProgramName); + printf("\n"); + printf("arguments:\n"); + + for(loop = 0; loop < switchCount; loop++) + { + if(gSwitches[loop]->mHasValue) + { + valueText = " "; + } + else + { + valueText = ""; + } + + printf("\t%s%s\n", gSwitches[loop]->mLongName, valueText); + printf("\t %s%s", gSwitches[loop]->mShortName, valueText); + printf(DESC_NEWLINE "%s\n\n", gSwitches[loop]->mDescription); + } +} + + +int main(int inArgc, char** inArgv) +{ + int retval = 0; + Options options; + + retval = initOptions(&options, inArgc, inArgv); + if(options.mHelp) + { + showHelp(&options); + } + else if(0 == retval) + { + retval = codesighs(&options); + } + + cleanOptions(&options); + return retval; +} + diff --git a/tools/codesighs/maptsvdifftool.c b/tools/codesighs/maptsvdifftool.c new file mode 100644 index 000000000000..57fa8e498d7e --- /dev/null +++ b/tools/codesighs/maptsvdifftool.c @@ -0,0 +1,981 @@ +#include +#include +#include +#include +#include + +#define ERROR_REPORT(num, val, msg) fprintf(stderr, "error(%d):\t\"%s\"\t%s\n", (num), (val), (msg)); +#define CLEANUP(ptr) do { if(NULL != ptr) { free(ptr); ptr = NULL; } } while(0) + + +typedef struct __struct_Options +/* +** Options to control how we perform. +** +** mProgramName Used in help text. +** mInput File to read for input. +** Default is stdin. +** mInputName Name of the file. +** mOutput Output file, append. +** Default is stdout. +** mOutputName Name of the file. +** mHelp Wether or not help should be shown. +** mTotalOnly Only output a signle digit. +** mZeroDrift Output zero drift data. +*/ +{ + const char* mProgramName; + FILE* mInput; + char* mInputName; + FILE* mOutput; + char* mOutputName; + int mHelp; + int mTotalOnly; + int mZeroDrift; +} +Options; + + +typedef struct __struct_Switch +/* +** Command line options. +*/ +{ + const char* mLongName; + const char* mShortName; + int mHasValue; + const char* mValue; + const char* mDescription; +} +Switch; + +#define DESC_NEWLINE "\n\t\t" + +static Switch gInputSwitch = {"--input", "-i", 1, NULL, "Specify input file." DESC_NEWLINE "stdin is default."}; +static Switch gOutputSwitch = {"--output", "-o", 1, NULL, "Specify output file." DESC_NEWLINE "Appends if file exists." DESC_NEWLINE "stdout is default."}; +static Switch gTotalSwitch = {"--totalonly", "-t", 0, NULL, "Only output a single number." DESC_NEWLINE "The cumulative size change." DESC_NEWLINE "Overrides all other output options."}; +static Switch gZeroDriftSwitch = {"--zerodrift", "-z", 0, NULL, "Output zero drift data." DESC_NEWLINE "Zero drift data includes all changes, even if they cancel out."}; +static Switch gHelpSwitch = {"--help", "-h", 0, NULL, "Information on usage."}; + +static Switch* gSwitches[] = { + &gInputSwitch, + &gOutputSwitch, + &gTotalSwitch, + &gZeroDriftSwitch, + &gHelpSwitch +}; + + +typedef struct __struct_SizeStats +/* +** Keep track of sizes. +** Use signed integers so that negatives are valid, in which case we shrunk. +*/ +{ + int mCode; + int mData; +} +SizeStats; + + +typedef enum __enum_SegmentClass +/* +** What type of data a segment holds. +*/ +{ + CODE, + DATA +} +SegmentClass; + + +typedef struct __struct_SymbolStats +/* +** Symbol level stats. +*/ +{ + char* mSymbol; + int mSize; +} +SymbolStats; + + +typedef struct __struct_ObjectStats +/* +** Object level stats. +*/ +{ + char* mObject; + int mSize; + SymbolStats* mSymbols; + unsigned mSymbolCount; +} +ObjectStats; + + +typedef struct __struct_SegmentStats +/* +** Segment level stats. +*/ +{ + char* mSegment; + SegmentClass mClass; + int mSize; + ObjectStats* mObjects; + unsigned mObjectCount; +} +SegmentStats; + + +typedef struct __struct_ModuleStats +/* +** Module level stats. +*/ +{ + char* mModule; + SizeStats mSize; + SegmentStats* mSegments; + unsigned mSegmentCount; +} +ModuleStats; + + +static int moduleCompare(const void* in1, const void* in2) +/* +** qsort helper. +*/ +{ + int retval = 0; + + ModuleStats* one = (ModuleStats*)in1; + ModuleStats* two = (ModuleStats*)in2; + + int oneSize = (one->mSize.mCode + one->mSize.mData); + int twoSize = (two->mSize.mCode + two->mSize.mData); + + if(oneSize < twoSize) + { + retval = 1; + } + else if(oneSize > twoSize) + { + retval = -1; + } + + return retval; +} + + +static int segmentCompare(const void* in1, const void* in2) +/* +** qsort helper. +*/ +{ + int retval = 0; + + SegmentStats* one = (SegmentStats*)in1; + SegmentStats* two = (SegmentStats*)in2; + + if(one->mSize < two->mSize) + { + retval = 1; + } + else if(one->mSize > two->mSize) + { + retval = -1; + } + + return retval; +} + + +static int objectCompare(const void* in1, const void* in2) +/* +** qsort helper. +*/ +{ + int retval = 0; + + ObjectStats* one = (ObjectStats*)in1; + ObjectStats* two = (ObjectStats*)in2; + + if(one->mSize < two->mSize) + { + retval = 1; + } + else if(one->mSize > two->mSize) + { + retval = -1; + } + + return retval; +} + + +static int symbolCompare(const void* in1, const void* in2) +/* +** qsort helper. +*/ +{ + int retval = 0; + + SymbolStats* one = (SymbolStats*)in1; + SymbolStats* two = (SymbolStats*)in2; + + if(one->mSize < two->mSize) + { + retval = 1; + } + else if(one->mSize > two->mSize) + { + retval = -1; + } + + return retval; +} + + +int difftool(Options* inOptions) +/* +** Read a diff file and spit out relevant information. +*/ +{ + int retval = 0; + char lineBuffer[0x500]; + SizeStats overall; + ModuleStats* modules = NULL; + unsigned moduleCount = 0; + unsigned moduleLoop = 0; + ModuleStats* theModule = NULL; + unsigned segmentLoop = 0; + SegmentStats* theSegment = NULL; + unsigned objectLoop = 0; + ObjectStats* theObject = NULL; + unsigned symbolLoop = 0; + SymbolStats* theSymbol = NULL; + + memset(&overall, 0, sizeof(overall)); + + /* + ** Read the entire diff file. + ** We're only interested in lines beginning with < or > + */ + while(0 == retval && NULL != fgets(lineBuffer, sizeof(lineBuffer), inOptions->mInput)) + { + if(('<' == lineBuffer[0] || '>' == lineBuffer[0]) && ' ' == lineBuffer[1]) + { + int additive = 0; + char* theLine = &lineBuffer[2]; + int scanRes = 0; + int size; + char segClass[0x10]; + char scope[0x10]; + char module[0x100]; + char segment[0x40]; + char object[0x100]; + char symbol[0x200]; + + /* + ** Figure out if the line adds or subtracts from something. + */ + if('>' == lineBuffer[0]) + { + additive = __LINE__; + } + + + /* + ** Scan the line for information. + */ + scanRes = sscanf(theLine, + "%x\t%s\t%s\t%s\t%s\t%s\t%s", + &size, + segClass, + scope, + module, + segment, + object, + symbol); + + if(7 == scanRes) + { + SegmentClass segmentClass = DATA; + + if(0 == strcmp(segClass, "CODE")) + { + segmentClass = CODE; + } + else if(0 == strcmp(segClass, "DATA")) + { + segmentClass = DATA; + } + else + { + retval = __LINE__; + ERROR_REPORT(retval, segClass, "Unable to determine segment class."); + } + + if(0 == retval) + { + /* + ** Update our overall totals. + */ + if(CODE == segmentClass) + { + if(additive) + { + overall.mCode += size; + } + else + { + overall.mCode -= size; + } + } + else + { + if(additive) + { + overall.mData += size; + } + else + { + overall.mData -= size; + } + } + + /* + ** Anything else to track? + */ + if(0 == inOptions->mTotalOnly) + { + unsigned moduleIndex = 0; + + /* + ** Find, in succession, the following things: + ** the module + ** the segment + ** the object + ** the symbol + ** Failure to find any one of these means to create it. + */ + + for(moduleIndex = 0; moduleIndex < moduleCount; moduleIndex++) + { + if(0 == strcmp(modules[moduleIndex].mModule, module)) + { + break; + } + } + + if(moduleIndex == moduleCount) + { + void* moved = NULL; + + moved = realloc(modules, sizeof(ModuleStats) * (1 + moduleCount)); + if(NULL != moved) + { + modules = (ModuleStats*)moved; + moduleCount++; + memset(modules + moduleIndex, 0, sizeof(ModuleStats)); + + modules[moduleIndex].mModule = strdup(module); + if(NULL == modules[moduleIndex].mModule) + { + retval = __LINE__; + ERROR_REPORT(retval, module, "Unable to duplicate string."); + } + } + else + { + retval = __LINE__; + ERROR_REPORT(retval, inOptions->mProgramName, "Unable to increase module array."); + } + } + + if(0 == retval) + { + unsigned segmentIndex = 0; + ModuleStats* theModule = (modules + moduleIndex); + + if(CODE == segmentClass) + { + if(additive) + { + modules[moduleIndex].mSize.mCode += size; + } + else + { + modules[moduleIndex].mSize.mCode -= size; + } + } + else + { + if(additive) + { + modules[moduleIndex].mSize.mData += size; + } + else + { + modules[moduleIndex].mSize.mData -= size; + } + } + + for(segmentIndex = 0; segmentIndex < theModule->mSegmentCount; segmentIndex++) + { + if(0 == strcmp(segment, theModule->mSegments[segmentIndex].mSegment)) + { + break; + } + } + + if(segmentIndex == theModule->mSegmentCount) + { + void* moved = NULL; + + moved = realloc(theModule->mSegments, sizeof(SegmentStats) * (theModule->mSegmentCount + 1)); + if(NULL != moved) + { + theModule->mSegments = (SegmentStats*)moved; + theModule->mSegmentCount++; + memset(theModule->mSegments + segmentIndex, 0, sizeof(SegmentStats)); + + theModule->mSegments[segmentIndex].mClass = segmentClass; + theModule->mSegments[segmentIndex].mSegment = strdup(segment); + if(NULL == theModule->mSegments[segmentIndex].mSegment) + { + retval = __LINE__; + ERROR_REPORT(retval, segment, "Unable to duplicate string."); + } + } + else + { + retval = __LINE__; + ERROR_REPORT(retval, inOptions->mProgramName, "Unable to increase segment array."); + } + } + + if(0 == retval) + { + unsigned objectIndex = 0; + SegmentStats* theSegment = (theModule->mSegments + segmentIndex); + + if(additive) + { + theSegment->mSize += size; + } + else + { + theSegment->mSize -= size; + } + + for(objectIndex = 0; objectIndex < theSegment->mObjectCount; objectIndex++) + { + if(0 == strcmp(object, theSegment->mObjects[objectIndex].mObject)) + { + break; + } + } + + if(objectIndex == theSegment->mObjectCount) + { + void* moved = NULL; + + moved = realloc(theSegment->mObjects, sizeof(ObjectStats) * (1 + theSegment->mObjectCount)); + if(NULL != moved) + { + theSegment->mObjects = (ObjectStats*)moved; + theSegment->mObjectCount++; + memset(theSegment->mObjects + objectIndex, 0, sizeof(ObjectStats)); + + theSegment->mObjects[objectIndex].mObject = strdup(object); + if(NULL == theSegment->mObjects[objectIndex].mObject) + { + retval = __LINE__; + ERROR_REPORT(retval, object, "Unable to duplicate string."); + } + } + else + { + retval = __LINE__; + ERROR_REPORT(retval, inOptions->mProgramName, "Unable to increase object array."); + } + } + + if(0 == retval) + { + unsigned symbolIndex = 0; + ObjectStats* theObject = (theSegment->mObjects + objectIndex); + + if(additive) + { + theObject->mSize += size; + } + else + { + theObject->mSize -= size; + } + + for(symbolIndex = 0; symbolIndex < theObject->mSymbolCount; symbolIndex++) + { + if(0 == strcmp(symbol, theObject->mSymbols[symbolIndex].mSymbol)) + { + break; + } + } + + if(symbolIndex == theObject->mSymbolCount) + { + void* moved = NULL; + + moved = realloc(theObject->mSymbols, sizeof(SymbolStats) * (1 + theObject->mSymbolCount)); + if(NULL != moved) + { + theObject->mSymbols = (SymbolStats*)moved; + theObject->mSymbolCount++; + memset(theObject->mSymbols + symbolIndex, 0, sizeof(SymbolStats)); + + theObject->mSymbols[symbolIndex].mSymbol = strdup(symbol); + if(NULL == theObject->mSymbols[symbolIndex].mSymbol) + { + retval = __LINE__; + ERROR_REPORT(retval, symbol, "Unable to duplicate string."); + } + } + else + { + retval = __LINE__; + ERROR_REPORT(retval, inOptions->mProgramName, "Unable to increase symbol array."); + } + } + + if(0 == retval) + { + SymbolStats* theSymbol = (theObject->mSymbols + symbolIndex); + + if(additive) + { + theSymbol->mSize += size; + } + else + { + theSymbol->mSize -= size; + } + } + } + } + } + } + } + } + else + { + retval = __LINE__; + ERROR_REPORT(retval, inOptions->mInputName, "Unable to scan line data."); + } + } + } + + if(0 == retval && 0 != ferror(inOptions->mInput)) + { + retval = __LINE__; + ERROR_REPORT(retval, inOptions->mInputName, "Unable to read file."); + } + + /* + ** If all went well, time to report. + */ + if(0 == retval) + { + if(inOptions->mTotalOnly) + { + fprintf(inOptions->mOutput, "%+d\n", overall.mCode + overall.mData); + } + else + { + fprintf(inOptions->mOutput, "Overall Change in Size\n"); + fprintf(inOptions->mOutput, "\tTotal:\t%+11d\n", overall.mCode + overall.mData); + fprintf(inOptions->mOutput, "\tCode:\t%+11d\n", overall.mCode); + fprintf(inOptions->mOutput, "\tData:\t%+11d\n", overall.mData); + } + + /* + ** Check what else we should output. + */ + if(NULL != modules && moduleCount) + { + const char* segmentType = NULL; + + /* + ** We're going to sort everything. + */ + qsort(modules, moduleCount, sizeof(ModuleStats), moduleCompare); + for(moduleLoop = 0; moduleLoop < moduleCount; moduleLoop++) + { + theModule = modules + moduleLoop; + + qsort(theModule->mSegments, theModule->mSegmentCount, sizeof(SegmentStats), segmentCompare); + + for(segmentLoop = 0; segmentLoop < theModule->mSegmentCount; segmentLoop++) + { + theSegment = theModule->mSegments + segmentLoop; + + qsort(theSegment->mObjects, theSegment->mObjectCount, sizeof(ObjectStats), objectCompare); + + for(objectLoop = 0; objectLoop < theSegment->mObjectCount; objectLoop++) + { + theObject = theSegment->mObjects + objectLoop; + + qsort(theObject->mSymbols, theObject->mSymbolCount, sizeof(SymbolStats), symbolCompare); + } + } + } + + /* + ** Loop through for output. + */ + for(moduleLoop = 0; moduleLoop < moduleCount; moduleLoop++) + { + theModule = modules + moduleLoop; + + /* + ** Skip if there is zero drift, or no net change. + */ + if(0 == inOptions->mZeroDrift && 0 == (theModule->mSize.mCode + theModule->mSize.mCode )) + { + continue; + } + + fprintf(inOptions->mOutput, "\n"); + fprintf(inOptions->mOutput, "%s\n", theModule->mModule); + fprintf(inOptions->mOutput, "\tTotal:\t%+11d\n", theModule->mSize.mCode + theModule->mSize.mData); + fprintf(inOptions->mOutput, "\tCode:\t%+11d\n", theModule->mSize.mCode); + fprintf(inOptions->mOutput, "\tData:\t%+11d\n", theModule->mSize.mData); + + for(segmentLoop = 0; segmentLoop < theModule->mSegmentCount; segmentLoop++) + { + theSegment = theModule->mSegments + segmentLoop; + + /* + ** Skip if there is zero drift, or no net change. + */ + if(0 == inOptions->mZeroDrift && 0 == theSegment->mSize) + { + continue; + } + + if(CODE == theSegment->mClass) + { + segmentType = "CODE"; + } + else if(DATA == theSegment->mClass) + { + segmentType = "DATA"; + } + + fprintf(inOptions->mOutput, "\t%+11d\t%s (%s)\n", theSegment->mSize, theSegment->mSegment, segmentType); + + for(objectLoop = 0; objectLoop < theSegment->mObjectCount; objectLoop++) + { + theObject = theSegment->mObjects + objectLoop; + + /* + ** Skip if there is zero drift, or no net change. + */ + if(0 == inOptions->mZeroDrift && 0 == theObject->mSize) + { + continue; + } + + fprintf(inOptions->mOutput, "\t\t%+11d\t%s\n", theObject->mSize, theObject->mObject); + + for(symbolLoop = 0; symbolLoop < theObject->mSymbolCount; symbolLoop++) + { + theSymbol = theObject->mSymbols + symbolLoop; + + /* + ** Skip if there is zero drift, or no net change. + */ + if(0 == inOptions->mZeroDrift && 0 == theSymbol->mSize) + { + continue; + } + + fprintf(inOptions->mOutput, "\t\t\t%+11d\t%s\n", theSymbol->mSize, theSymbol->mSymbol); + } + } + } + } + } + } + + /* + ** Cleanup time. + */ + for(moduleLoop = 0; moduleLoop < moduleCount; moduleLoop++) + { + theModule = modules + moduleLoop; + + for(segmentLoop = 0; segmentLoop < theModule->mSegmentCount; segmentLoop++) + { + theSegment = theModule->mSegments + segmentLoop; + + for(objectLoop = 0; objectLoop < theSegment->mObjectCount; objectLoop++) + { + theObject = theSegment->mObjects + objectLoop; + + for(symbolLoop = 0; symbolLoop < theObject->mSymbolCount; symbolLoop++) + { + theSymbol = theObject->mSymbols + symbolLoop; + + CLEANUP(theSymbol->mSymbol); + } + + CLEANUP(theObject->mSymbols); + CLEANUP(theObject->mObject); + } + + CLEANUP(theSegment->mObjects); + CLEANUP(theSegment->mSegment); + } + + CLEANUP(theModule->mSegments); + CLEANUP(theModule->mModule); + } + CLEANUP(modules); + + return retval; +} + + +int initOptions(Options* outOptions, int inArgc, char** inArgv) +/* +** returns int 0 if successful. +*/ +{ + int retval = 0; + int loop = 0; + int switchLoop = 0; + int match = 0; + const int switchCount = sizeof(gSwitches) / sizeof(gSwitches[0]); + Switch* current = NULL; + + /* + ** Set any defaults. + */ + memset(outOptions, 0, sizeof(Options)); + outOptions->mProgramName = inArgv[0]; + outOptions->mInput = stdin; + outOptions->mInputName = strdup("stdin"); + outOptions->mOutput = stdout; + outOptions->mOutputName = strdup("stdout"); + + if(NULL == outOptions->mOutputName || NULL == outOptions->mInputName) + { + retval = __LINE__; + ERROR_REPORT(retval, "stdin/stdout", "Unable to strdup."); + } + + /* + ** Go through and attempt to do the right thing. + */ + for(loop = 1; loop < inArgc && 0 == retval; loop++) + { + match = 0; + current = NULL; + + for(switchLoop = 0; switchLoop < switchCount && 0 == retval; switchLoop++) + { + if(0 == strcmp(gSwitches[switchLoop]->mLongName, inArgv[loop])) + { + match = __LINE__; + } + else if(0 == strcmp(gSwitches[switchLoop]->mShortName, inArgv[loop])) + { + match = __LINE__; + } + + if(match) + { + if(gSwitches[switchLoop]->mHasValue) + { + /* + ** Attempt to absorb next option to fullfill value. + */ + if(loop + 1 < inArgc) + { + loop++; + + current = gSwitches[switchLoop]; + current->mValue = inArgv[loop]; + } + } + else + { + current = gSwitches[switchLoop]; + } + + break; + } + } + + if(0 == match) + { + outOptions->mHelp = __LINE__; + retval = __LINE__; + ERROR_REPORT(retval, inArgv[loop], "Unknown command line switch."); + } + else if(NULL == current) + { + outOptions->mHelp = __LINE__; + retval = __LINE__; + ERROR_REPORT(retval, inArgv[loop], "Command line switch requires a value."); + } + else + { + /* + ** Do something based on address/swtich. + */ + if(current == &gInputSwitch) + { + CLEANUP(outOptions->mInputName); + if(NULL != outOptions->mInput && stdin != outOptions->mInput) + { + fclose(outOptions->mInput); + outOptions->mInput = NULL; + } + + outOptions->mInput = fopen(current->mValue, "r"); + if(NULL == outOptions->mInput) + { + retval = __LINE__; + ERROR_REPORT(retval, current->mValue, "Unable to open input file."); + } + else + { + outOptions->mInputName = strdup(current->mValue); + if(NULL == outOptions->mInputName) + { + retval = __LINE__; + ERROR_REPORT(retval, current->mValue, "Unable to strdup."); + } + } + } + else if(current == &gOutputSwitch) + { + CLEANUP(outOptions->mOutputName); + if(NULL != outOptions->mOutput && stdout != outOptions->mOutput) + { + fclose(outOptions->mOutput); + outOptions->mOutput = NULL; + } + + outOptions->mOutput = fopen(current->mValue, "a"); + if(NULL == outOptions->mOutput) + { + retval = __LINE__; + ERROR_REPORT(retval, current->mValue, "Unable to open output file."); + } + else + { + outOptions->mOutputName = strdup(current->mValue); + if(NULL == outOptions->mOutputName) + { + retval = __LINE__; + ERROR_REPORT(retval, current->mValue, "Unable to strdup."); + } + } + } + else if(current == &gHelpSwitch) + { + outOptions->mHelp = __LINE__; + } + else if(current == &gTotalSwitch) + { + outOptions->mTotalOnly = __LINE__; + } + else if(current == &gZeroDriftSwitch) + { + outOptions->mZeroDrift = __LINE__; + } + else + { + retval = __LINE__; + ERROR_REPORT(retval, current->mLongName, "No hanlder for command line switch."); + } + } + } + + return retval; +} + + +void cleanOptions(Options* inOptions) +/* +** Clean up any open handles. +*/ +{ + CLEANUP(inOptions->mInputName); + if(NULL != inOptions->mInput && stdin != inOptions->mInput) + { + fclose(inOptions->mInput); + } + CLEANUP(inOptions->mOutputName); + if(NULL != inOptions->mOutput && stdout != inOptions->mOutput) + { + fclose(inOptions->mOutput); + } + + memset(inOptions, 0, sizeof(Options)); +} + + +void showHelp(Options* inOptions) +/* +** Show some simple help text on usage. +*/ +{ + int loop = 0; + const int switchCount = sizeof(gSwitches) / sizeof(gSwitches[0]); + const char* valueText = NULL; + + printf("usage:\t%s [arguments]\n", inOptions->mProgramName); + printf("\n"); + printf("arguments:\n"); + + for(loop = 0; loop < switchCount; loop++) + { + if(gSwitches[loop]->mHasValue) + { + valueText = " "; + } + else + { + valueText = ""; + } + + printf("\t%s%s\n", gSwitches[loop]->mLongName, valueText); + printf("\t %s%s", gSwitches[loop]->mShortName, valueText); + printf(DESC_NEWLINE "%s\n\n", gSwitches[loop]->mDescription); + } +} + + +int main(int inArgc, char** inArgv) +{ + int retval = 0; + Options options; + + retval = initOptions(&options, inArgc, inArgv); + if(options.mHelp) + { + showHelp(&options); + } + else if(0 == retval) + { + retval = difftool(&options); + } + + cleanOptions(&options); + return retval; +} + diff --git a/tools/codesighs/msmap.h b/tools/codesighs/msmap.h new file mode 100644 index 000000000000..a230187387e8 --- /dev/null +++ b/tools/codesighs/msmap.h @@ -0,0 +1,106 @@ +#if !defined __msmap_H +#define __msmap_H + + +#if defined(__cplusplus) +extern "C" { +#endif +#if 0 +} +#endif + + +/* +** Used to numerically represent addresses. +*/ +typedef unsigned long address; + + +typedef enum __enum_MSMap_SymbolScope +/* +** Symbol scope. +*/ +{ + PUBLIC, + STATIC, + UNDEFINED +} +MSMap_SymbolScope; + + +typedef struct __struct_MSMap_Symbol +/* +** Information about a symbol. +*/ +{ + address mPrefix; + address mOffset; + char* mSymbol; + address mRVABase; + char* mObject; + MSMap_SymbolScope mScope; +} +MSMap_Symbol; + + +typedef enum __enum_MSMap_SegmentClass +/* +** Segment class. +*/ +{ + CODE, + DATA +} +MSMap_SegmentClass; + + +typedef struct __struct_MSMap_Segment +/* +** Information about a segment. +*/ +{ + address mPrefix; + address mOffset; + address mLength; + char* mSegment; + MSMap_SegmentClass mClass; +} +MSMap_Segment; + + +typedef struct __struct_MSMap_Module +/* +** Top level container of the map data. +*/ +{ + char* mModule; + time_t mTimestamp; + address mPreferredLoadAddress; + MSMap_Segment* mSegments; + unsigned mSegmentCount; + unsigned mSegmentCapacity; + address mEntryPrefix; + address mEntryOffset; + MSMap_Symbol* mSymbols; + unsigned mSymbolCount; + unsigned mSymbolCapacity; +} +MSMap_Module; + + +/* +** How much to grow our arrays by. +*/ +#define MSMAP_SEGMENT_GROWBY 0x10 +#define MSMAP_SYMBOL_GROWBY 0x100 + + +#if 0 +{ +#endif +#if defined(__cplusplus) +} /* extern "C" */ +#endif + + +#endif /* __msmap_H */ diff --git a/tools/codesighs/msmap2tsv.c b/tools/codesighs/msmap2tsv.c new file mode 100644 index 000000000000..3523b091ea3f --- /dev/null +++ b/tools/codesighs/msmap2tsv.c @@ -0,0 +1,1107 @@ +#include +#include +#include +#include +#include + +#include "msmap.h" + + +#define ERROR_REPORT(num, val, msg) fprintf(stderr, "error(%d):\t\"%s\"\t%s\n", (num), (val), (msg)); +#define CLEANUP(ptr) do { if(NULL != ptr) { free(ptr); ptr = NULL; } } while(0) + + +typedef struct __struct_Options +/* +** Options to control how we perform. +** +** mProgramName Used in help text. +** mInput File to read for input. +** Default is stdin. +** mInputName Name of the file. +** mOutput Output file, append. +** Default is stdout. +** mOutputName Name of the file. +** mHelp Wether or not help should be shown. +** mAddress Wether or not to output addresses. +*/ +{ + const char* mProgramName; + FILE* mInput; + char* mInputName; + FILE* mOutput; + char* mOutputName; + int mHelp; + int mAddresses; +} +Options; + + +typedef struct __struct_Switch +/* +** Command line options. +*/ +{ + const char* mLongName; + const char* mShortName; + int mHasValue; + const char* mValue; + const char* mDescription; +} +Switch; + +#define DESC_NEWLINE "\n\t\t" + +static Switch gInputSwitch = {"--input", "-i", 1, NULL, "Specify input file." DESC_NEWLINE "stdin is default."}; +static Switch gOutputSwitch = {"--output", "-o", 1, NULL, "Specify output file." DESC_NEWLINE "Appends if file exists." DESC_NEWLINE "stdout is default."}; +static Switch gHelpSwitch = {"--help", "-h", 0, NULL, "Information on usage."}; +static Switch gAddressesSwitch = {"--addresses", "-a", 0, NULL, "Output segment addresses." DESC_NEWLINE "Helps reveal symbol ordering." DESC_NEWLINE "Lack of simplifies size diffing."}; + +static Switch* gSwitches[] = { + &gInputSwitch, + &gOutputSwitch, + &gAddressesSwitch, + &gHelpSwitch +}; + + +typedef struct __struct_MSMap_ReadState +/* +** Keep track of what state we are while reading input. +** This gives the input context in which we absorb the datum. +*/ +{ + int mHasModule; + + int mHasTimestamp; + + int mHasPreferredLoadAddress; + + int mHasSegmentData; + int mSegmentDataSkippedLine; + + int mHasPublicSymbolData; + int mHasPublicSymbolDataSkippedLines; + + int mHasEntryPoint; + + int mFoundStaticSymbols; +} +MSMap_ReadState; + + +char* skipWhite(char* inScan) +/* +** Skip whitespace. +*/ +{ + char* retval = inScan; + + while(isspace(*retval)) + { + retval++; + } + + return retval; +} + +void trimWhite(char* inString) +/* +** Remove any whitespace from the end of the string. +*/ +{ + int len = strlen(inString); + + while(len) + { + len--; + + if(isspace(*(inString + len))) + { + *(inString + len) = '\0'; + } + else + { + break; + } + } +} + + +char* lastWord(char* inString) +/* +** Finds and returns the last word in a string. +** It is assumed no whitespace is at the end of the string. +*/ +{ + int mod = 0; + int len = strlen(inString); + + while(len) + { + len--; + if(isspace(*(inString + len))) + { + mod = 1; + break; + } + } + + return inString + len + mod; +} + + +int readmap(Options* inOptions, MSMap_Module* inModule) +/* +** Read the input line by line, adding it to the module. +*/ +{ + int retval = 0; + char lineBuffer[0x400]; + char* current = NULL; + MSMap_ReadState fsm; + int len = 0; + int forceContinue = 0; + + memset(&fsm, 0, sizeof(fsm)); + + /* + ** Read the map file line by line. + ** We keep a simple state machine to determine what we're looking at. + */ + while(0 == retval && NULL != fgets(lineBuffer, sizeof(lineBuffer), inOptions->mInput)) + { + if(forceContinue) + { + /* + ** Used to skip anticipated blank lines. + */ + forceContinue--; + continue; + } + + current = skipWhite(lineBuffer); + trimWhite(current); + + len = strlen(current); + + if(fsm.mHasModule) + { + if(fsm.mHasTimestamp) + { + if(fsm.mHasPreferredLoadAddress) + { + if(fsm.mHasSegmentData) + { + if(fsm.mHasPublicSymbolData) + { + if(fsm.mHasEntryPoint) + { + if(fsm.mFoundStaticSymbols) + { + /* + ** A blank line means we've reached the end of all static symbols. + */ + if(len) + { + /* + ** We're adding a new symbol. + ** Make sure we have room for it. + */ + if(inModule->mSymbolCapacity == inModule->mSymbolCount) + { + void* moved = NULL; + + moved = realloc(inModule->mSymbols, sizeof(MSMap_Symbol) * (inModule->mSymbolCapacity + MSMAP_SYMBOL_GROWBY)); + if(NULL != moved) + { + inModule->mSymbolCapacity += MSMAP_SYMBOL_GROWBY; + inModule->mSymbols = (MSMap_Symbol*)moved; + } + else + { + retval = __LINE__; + ERROR_REPORT(retval, inModule->mModule, "Unable to grow symbols."); + } + } + + if(0 == retval && inModule->mSymbolCapacity > inModule->mSymbolCount) + { + MSMap_Symbol* theSymbol = NULL; + unsigned index = 0; + int scanRes = 0; + char symbolBuf[0x200]; + + index = inModule->mSymbolCount; + inModule->mSymbolCount++; + theSymbol = (inModule->mSymbols + index); + + memset(theSymbol, 0, sizeof(MSMap_Symbol)); + theSymbol->mScope = STATIC; + + scanRes = sscanf(current, "%x:%x %s %x", &(theSymbol->mPrefix), &(theSymbol->mOffset), symbolBuf, &(theSymbol->mRVABase)); + if(4 == scanRes) + { + theSymbol->mSymbol = strdup(symbolBuf); + if(NULL != theSymbol->mSymbol) + { + char *last = lastWord(current); + + theSymbol->mObject = strdup(last); + if(NULL == theSymbol->mObject) + { + retval = __LINE__; + ERROR_REPORT(retval, last, "Unable to copy object name."); + } + } + else + { + retval = __LINE__; + ERROR_REPORT(retval, symbolBuf, "Unable to copy symbol name."); + } + } + else + { + retval = __LINE__; + ERROR_REPORT(retval, inModule->mModule, "Unable to scan static symbols."); + } + } + } + else + { + /* + ** All done. + */ + break; + } + } + else + { + /* + ** Static symbols are optional. + ** If no static symbols we're done. + ** Otherwise, set the flag such that it will work more. + */ + if(0 == strcmp(current, "Static symbols")) + { + fsm.mFoundStaticSymbols = __LINE__; + forceContinue = 1; + } + else + { + /* + ** All done. + */ + break; + } + } + } + else + { + int scanRes = 0; + + scanRes = sscanf(current, "entry point at %x:%x", &(inModule->mEntryPrefix), &(inModule->mEntryOffset)); + if(2 == scanRes) + { + fsm.mHasEntryPoint = __LINE__; + forceContinue = 1; + } + else + { + retval = __LINE__; + ERROR_REPORT(retval, current, "Unable to obtain entry point."); + } + } + } + else + { + /* + ** Skip the N lines of public symbol data (column headers). + */ + if(2 <= fsm.mHasPublicSymbolDataSkippedLines) + { + /* + ** A blank line indicates end of public symbols. + */ + if(len) + { + /* + ** We're adding a new symbol. + ** Make sure we have room for it. + */ + if(inModule->mSymbolCapacity == inModule->mSymbolCount) + { + void* moved = NULL; + + moved = realloc(inModule->mSymbols, sizeof(MSMap_Symbol) * (inModule->mSymbolCapacity + MSMAP_SYMBOL_GROWBY)); + if(NULL != moved) + { + inModule->mSymbolCapacity += MSMAP_SYMBOL_GROWBY; + inModule->mSymbols = (MSMap_Symbol*)moved; + } + else + { + retval = __LINE__; + ERROR_REPORT(retval, inModule->mModule, "Unable to grow symbols."); + } + } + + if(0 == retval && inModule->mSymbolCapacity > inModule->mSymbolCount) + { + MSMap_Symbol* theSymbol = NULL; + unsigned index = 0; + int scanRes = 0; + char symbolBuf[0x200]; + + index = inModule->mSymbolCount; + inModule->mSymbolCount++; + theSymbol = (inModule->mSymbols + index); + + memset(theSymbol, 0, sizeof(MSMap_Symbol)); + theSymbol->mScope = PUBLIC; + + scanRes = sscanf(current, "%x:%x %s %x", &(theSymbol->mPrefix), &(theSymbol->mOffset), symbolBuf, &(theSymbol->mRVABase)); + if(4 == scanRes) + { + theSymbol->mSymbol = strdup(symbolBuf); + if(NULL != theSymbol->mSymbol) + { + char *last = lastWord(current); + + theSymbol->mObject = strdup(last); + if(NULL == theSymbol->mObject) + { + retval = __LINE__; + ERROR_REPORT(retval, last, "Unable to copy object name."); + } + } + else + { + retval = __LINE__; + ERROR_REPORT(retval, symbolBuf, "Unable to copy symbol name."); + } + } + else + { + retval = __LINE__; + ERROR_REPORT(retval, inModule->mModule, "Unable to scan public symbols."); + } + } + } + else + { + fsm.mHasPublicSymbolData = __LINE__; + } + } + else + { + fsm.mHasPublicSymbolDataSkippedLines++; + } + } + } + else + { + /* + ** Skip the first line of segment data (column headers). + ** Mark that we've begun grabbing segement data. + */ + if(fsm.mSegmentDataSkippedLine) + { + /* + ** A blank line means end of the segment data. + */ + if(len) + { + /* + ** We're adding a new segment. + ** Make sure we have room for it. + */ + if(inModule->mSegmentCapacity == inModule->mSegmentCount) + { + void* moved = NULL; + + moved = realloc(inModule->mSegments, sizeof(MSMap_Segment) * (inModule->mSegmentCapacity + MSMAP_SEGMENT_GROWBY)); + if(NULL != moved) + { + inModule->mSegmentCapacity += MSMAP_SEGMENT_GROWBY; + inModule->mSegments = (MSMap_Segment*)moved; + } + else + { + retval = __LINE__; + ERROR_REPORT(retval, inModule->mModule, "Unable to grow segments."); + } + } + + if(0 == retval && inModule->mSegmentCapacity > inModule->mSegmentCount) + { + MSMap_Segment* theSegment = NULL; + unsigned index = 0; + char classBuf[0x10]; + char nameBuf[0x20]; + int scanRes = 0; + + index = inModule->mSegmentCount; + inModule->mSegmentCount++; + theSegment = (inModule->mSegments + index); + + memset(theSegment, 0, sizeof(MSMap_Segment)); + + scanRes = sscanf(current, "%x:%x %xH %s %s", &(theSegment->mPrefix), &(theSegment->mOffset), &(theSegment->mLength), nameBuf, classBuf); + if(5 == scanRes) + { + if('.' == nameBuf[0]) + { + theSegment->mSegment = strdup(&nameBuf[1]); + } + else + { + theSegment->mSegment = strdup(nameBuf); + } + + if(NULL != theSegment->mSegment) + { + if(0 == strcmp("DATA", classBuf)) + { + theSegment->mClass = DATA; + } + else if(0 == strcmp("CODE", classBuf)) + { + theSegment->mClass = CODE; + } + else + { + retval = __LINE__; + ERROR_REPORT(retval, classBuf, "Unrecognized segment class."); + } + } + else + { + retval = __LINE__; + ERROR_REPORT(retval, nameBuf, "Unable to copy segment name."); + } + } + else + { + retval = __LINE__; + ERROR_REPORT(retval, inModule->mModule, "Unable to scan segments."); + } + } + } + else + { + fsm.mHasSegmentData = __LINE__; + } + } + else + { + fsm.mSegmentDataSkippedLine = __LINE__; + } + } + } + else + { + int scanRes = 0; + + /* + ** The PLA has a particular format. + */ + scanRes = sscanf(current, "Preferred load address is %x", &(inModule->mPreferredLoadAddress)); + if(1 == scanRes) + { + fsm.mHasPreferredLoadAddress = __LINE__; + forceContinue = 1; + } + else + { + retval = __LINE__; + ERROR_REPORT(retval, current, "Unable to obtain preferred load address."); + } + } + } + else + { + int scanRes = 0; + + /* + ** The timestamp has a particular format. + */ + scanRes = sscanf(current, "Timestamp is %x", &(inModule->mTimestamp)); + if(1 == scanRes) + { + fsm.mHasTimestamp = __LINE__; + forceContinue = 1; + } + else + { + retval = __LINE__; + ERROR_REPORT(retval, current, "Unable to obtain timestamp."); + } + } + } + else + { + /* + ** The module is on a line by itself. + */ + inModule->mModule = strdup(current); + if(NULL != inModule->mModule) + { + fsm.mHasModule = __LINE__; + forceContinue = 1; + } + else + { + retval = __LINE__; + ERROR_REPORT(retval, current, "Unable to obtain module."); + } + } + } + + if(0 == retval && 0 != ferror(inOptions->mInput)) + { + retval = __LINE__; + ERROR_REPORT(retval, inOptions->mInputName, "Unable to read file."); + } + + return retval; +} + + +static int qsortRVABase(const void* in1, const void* in2) +/* +** qsort callback to sort the symbols by their RVABase. +*/ +{ + MSMap_Symbol* sym1 = (MSMap_Symbol*)in1; + MSMap_Symbol* sym2 = (MSMap_Symbol*)in2; + int retval = 0; + + if(sym1->mRVABase < sym2->mRVABase) + { + retval = -1; + } + else if(sym1->mRVABase > sym2->mRVABase) + { + retval = 1; + } + + return retval; +} + + +static int tsvout(Options* inOptions, unsigned inSize, MSMap_SegmentClass inClass, MSMap_SymbolScope inScope, const char* inModule, const char* inSegment, address inPrefix, address inOffset, const char* inObject, const char* inSymbol) +/* +** Output a line of map information seperated by tabs. +** Some items (const char*), if not present, will receive a default value. +*/ +{ + int retval = 0; + + /* + ** No need to output on no size. + ** This can happen with zero sized segments, + ** or an imported symbol which has multiple names (one will count). + */ + if(0 != inSize) + { + char objectBuf[0x100]; + const char* symScope = NULL; + const char* segClass = NULL; + const char* undefined = "UNDEF"; + + /* + ** Fill in unspecified values. + */ + if(NULL == inObject) + { + sprintf(objectBuf, "%s:%s:%s", undefined, inModule, inSegment); + inObject = objectBuf; + } + if(NULL == inSymbol) + { + inSymbol = inObject; + } + + /* + ** Convert some enumerations to text. + */ + switch(inClass) + { + case CODE: + segClass = "CODE"; + break; + case DATA: + segClass = "DATA"; + break; + default: + retval = __LINE__; + ERROR_REPORT(retval, "", "Unable to determine class for output."); + break; + } + + switch(inScope) + { + case PUBLIC: + symScope = "PUBLIC"; + break; + case STATIC: + symScope = "STATIC"; + break; + case UNDEFINED: + symScope = undefined; + break; + default: + retval = __LINE__; + ERROR_REPORT(retval, "", "Unable to determine scope for symbol."); + break; + } + + if(0 == retval) + { + int printRes = 0; + + printRes = fprintf(inOptions->mOutput, + "%.8X\t%s\t%s\t%s\t%s\t%s\t%s", + inSize, + segClass, + symScope, + inModule, + inSegment, + inObject, + inSymbol + ); + + if(0 <= printRes && inOptions->mAddresses) + { + printRes = fprintf(inOptions->mOutput, + "\t%.4X:%.8X", + inPrefix, + inOffset + ); + } + + if(0 <= printRes) + { + printRes = fprintf(inOptions->mOutput, "\n"); + } + + if(0 > printRes) + { + retval = __LINE__; + ERROR_REPORT(retval, inOptions->mOutputName, "Unable to output tsv data."); + } + } + } + + return retval; +} + + +void cleanModule(MSMap_Module* inModule) +{ + unsigned loop = 0; + + for(loop = 0; loop < inModule->mSymbolCount; loop++) + { + CLEANUP(inModule->mSymbols[loop].mObject); + CLEANUP(inModule->mSymbols[loop].mSymbol); + } + CLEANUP(inModule->mSymbols); + + for(loop = 0; loop < inModule->mSegmentCount; loop++) + { + CLEANUP(inModule->mSegments[loop].mSegment); + } + CLEANUP(inModule->mSegments); + + CLEANUP(inModule->mModule); + + memset(inModule, 0, sizeof(MSMap_Module)); +} + + +int map2tsv(Options* inOptions) +/* +** Read all input. +** Output tab seperated value data. +*/ +{ + int retval = 0; + MSMap_Module module; + + memset(&module, 0, sizeof(module)); + + /* + ** Read in the map file. + */ + retval = readmap(inOptions, &module); + if(0 == retval) + { + unsigned segLoop = 0; + MSMap_Segment* segment = NULL; + unsigned symLoop = 0; + MSMap_Symbol* symbol = NULL; + MSMap_Symbol* firstSymbol = NULL; + MSMap_Symbol* matchSymbol = NULL; + MSMap_Symbol* prevSymbol = NULL; + + /* + ** Quick sort the symbols via RVABase. + */ + qsort(module.mSymbols, module.mSymbolCount, sizeof(MSMap_Symbol), qsortRVABase); + + /* + ** Go through each segment in order: + ** Output symbols and sizes, already in order. + ** Make up symbol names for those missing symbols, + ** or for blank spots at the beginning of a segment. + */ + for(segLoop = 0; 0 == retval && segLoop < module.mSegmentCount; segLoop++) + { + segment = (module.mSegments + segLoop); + firstSymbol = NULL; + matchSymbol = NULL; + prevSymbol = NULL; + + for(symLoop = 0; 0 == retval && symLoop < module.mSymbolCount; symLoop++) + { + symbol = (module.mSymbols + symLoop); + + /* + ** Symbol must fall in range of segment for consideration. + */ + if(symbol->mPrefix == segment->mPrefix) + { + if(symbol->mOffset >= segment->mOffset) + { + if(symbol->mOffset < (segment->mOffset + segment->mLength)) + { + /* + ** Matched. + */ + prevSymbol = matchSymbol; + matchSymbol = symbol; + + if(NULL == firstSymbol) + { + firstSymbol = matchSymbol; + + /* + ** Check to see if we need to output a dummy + ** to start the segment. + */ + if(0 == retval && firstSymbol->mOffset != segment->mOffset) + { + retval = tsvout(inOptions, + firstSymbol->mOffset - segment->mOffset, + segment->mClass, + UNDEFINED, + module.mModule, + segment->mSegment, + segment->mPrefix, + segment->mOffset, + NULL, + NULL + ); + } + } + + /* + ** Can now output previous symbol as can calculate size. + */ + if(0 == retval && NULL != prevSymbol) + { + retval = tsvout(inOptions, + matchSymbol->mOffset - prevSymbol->mOffset, + segment->mClass, + prevSymbol->mScope, + module.mModule, + segment->mSegment, + prevSymbol->mPrefix, + prevSymbol->mOffset, + prevSymbol->mObject, + prevSymbol->mSymbol + ); + } + } + } + } + } + + /* + ** If there was no symbol, output a fake one for the entire segment. + ** Otherwise, there is always one final match which we must output + ** taking up the remainder of the segment. + */ + if(0 == retval) + { + if(NULL == firstSymbol) + { + retval = tsvout(inOptions, + segment->mLength, + segment->mClass, + UNDEFINED, + module.mModule, + segment->mSegment, + segment->mPrefix, + segment->mOffset, + NULL, + NULL + ); + } + else + { + retval = tsvout(inOptions, + (segment->mOffset + segment->mLength) - matchSymbol->mOffset, + segment->mClass, + matchSymbol->mScope, + module.mModule, + segment->mSegment, + matchSymbol->mPrefix, + matchSymbol->mOffset, + matchSymbol->mObject, + matchSymbol->mSymbol + ); + } + } + } + } + + /* + ** Cleanup. + */ + cleanModule(&module); + + return retval; +} + + +int initOptions(Options* outOptions, int inArgc, char** inArgv) +/* +** returns int 0 if successful. +*/ +{ + int retval = 0; + int loop = 0; + int switchLoop = 0; + int match = 0; + const int switchCount = sizeof(gSwitches) / sizeof(gSwitches[0]); + Switch* current = NULL; + + /* + ** Set any defaults. + */ + memset(outOptions, 0, sizeof(Options)); + outOptions->mProgramName = inArgv[0]; + outOptions->mInput = stdin; + outOptions->mInputName = strdup("stdin"); + outOptions->mOutput = stdout; + outOptions->mOutputName = strdup("stdout"); + + if(NULL == outOptions->mOutputName || NULL == outOptions->mInputName) + { + retval = __LINE__; + ERROR_REPORT(retval, "stdin/stdout", "Unable to strdup."); + } + + /* + ** Go through and attempt to do the right thing. + */ + for(loop = 1; loop < inArgc && 0 == retval; loop++) + { + match = 0; + current = NULL; + + for(switchLoop = 0; switchLoop < switchCount && 0 == retval; switchLoop++) + { + if(0 == strcmp(gSwitches[switchLoop]->mLongName, inArgv[loop])) + { + match = __LINE__; + } + else if(0 == strcmp(gSwitches[switchLoop]->mShortName, inArgv[loop])) + { + match = __LINE__; + } + + if(match) + { + if(gSwitches[switchLoop]->mHasValue) + { + /* + ** Attempt to absorb next option to fullfill value. + */ + if(loop + 1 < inArgc) + { + loop++; + + current = gSwitches[switchLoop]; + current->mValue = inArgv[loop]; + } + } + else + { + current = gSwitches[switchLoop]; + } + + break; + } + } + + if(0 == match) + { + outOptions->mHelp = __LINE__; + retval = __LINE__; + ERROR_REPORT(retval, inArgv[loop], "Unknown command line switch."); + } + else if(NULL == current) + { + outOptions->mHelp = __LINE__; + retval = __LINE__; + ERROR_REPORT(retval, inArgv[loop], "Command line switch requires a value."); + } + else + { + /* + ** Do something based on address/swtich. + */ + if(current == &gInputSwitch) + { + CLEANUP(outOptions->mInputName); + if(NULL != outOptions->mInput && stdin != outOptions->mInput) + { + fclose(outOptions->mInput); + outOptions->mInput = NULL; + } + + outOptions->mInput = fopen(current->mValue, "r"); + if(NULL == outOptions->mInput) + { + retval = __LINE__; + ERROR_REPORT(retval, current->mValue, "Unable to open input file."); + } + else + { + outOptions->mInputName = strdup(current->mValue); + if(NULL == outOptions->mInputName) + { + retval = __LINE__; + ERROR_REPORT(retval, current->mValue, "Unable to strdup."); + } + } + } + else if(current == &gOutputSwitch) + { + CLEANUP(outOptions->mOutputName); + if(NULL != outOptions->mOutput && stdout != outOptions->mOutput) + { + fclose(outOptions->mOutput); + outOptions->mOutput = NULL; + } + + outOptions->mOutput = fopen(current->mValue, "a"); + if(NULL == outOptions->mOutput) + { + retval = __LINE__; + ERROR_REPORT(retval, current->mValue, "Unable to open output file."); + } + else + { + outOptions->mOutputName = strdup(current->mValue); + if(NULL == outOptions->mOutputName) + { + retval = __LINE__; + ERROR_REPORT(retval, current->mValue, "Unable to strdup."); + } + } + } + else if(current == &gAddressesSwitch) + { + outOptions->mAddresses = __LINE__; + } + else if(current == &gHelpSwitch) + { + outOptions->mHelp = __LINE__; + } + else + { + retval = __LINE__; + ERROR_REPORT(retval, current->mLongName, "No hanlder for command line switch."); + } + } + } + + return retval; +} + + +void cleanOptions(Options* inOptions) +/* +** Clean up any open handles. +*/ +{ + CLEANUP(inOptions->mInputName); + if(NULL != inOptions->mInput && stdin != inOptions->mInput) + { + fclose(inOptions->mInput); + } + CLEANUP(inOptions->mOutputName); + if(NULL != inOptions->mOutput && stdout != inOptions->mOutput) + { + fclose(inOptions->mOutput); + } + + memset(inOptions, 0, sizeof(Options)); +} + + +void showHelp(Options* inOptions) +/* +** Show some simple help text on usage. +*/ +{ + int loop = 0; + const int switchCount = sizeof(gSwitches) / sizeof(gSwitches[0]); + const char* valueText = NULL; + + printf("usage:\t%s [arguments]\n", inOptions->mProgramName); + printf("\n"); + printf("arguments:\n"); + + for(loop = 0; loop < switchCount; loop++) + { + if(gSwitches[loop]->mHasValue) + { + valueText = " "; + } + else + { + valueText = ""; + } + + printf("\t%s%s\n", gSwitches[loop]->mLongName, valueText); + printf("\t %s%s", gSwitches[loop]->mShortName, valueText); + printf(DESC_NEWLINE "%s\n\n", gSwitches[loop]->mDescription); + } +} + + +int main(int inArgc, char** inArgv) +{ + int retval = 0; + Options options; + + retval = initOptions(&options, inArgc, inArgv); + if(options.mHelp) + { + showHelp(&options); + } + else if(0 == retval) + { + retval = map2tsv(&options); + } + + cleanOptions(&options); + return retval; +} +