You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by GitBox <gi...@apache.org> on 2018/01/09 10:14:33 UTC

[GitHub] michal-narajowski closed pull request #121: newt: Add option to print size report for any section

michal-narajowski closed pull request #121: newt: Add option to print size report for any section
URL: https://github.com/apache/mynewt-newt/pull/121
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/newt/builder/size.go b/newt/builder/size.go
index b00feb7..7119946 100644
--- a/newt/builder/size.go
+++ b/newt/builder/size.go
@@ -510,7 +510,7 @@ func (b *Builder) Size() error {
 	return nil
 }
 
-func (t *TargetBuilder) SizeReport(ram, flash bool) error {
+func (t *TargetBuilder) SizeReport(sectionName string) error {
 
 	err := t.PrepBuild()
 
@@ -519,21 +519,21 @@ func (t *TargetBuilder) SizeReport(ram, flash bool) error {
 	}
 
 	fmt.Printf("Size of Application Image: %s\n", t.AppBuilder.buildName)
-	err = t.AppBuilder.SizeReport(ram, flash)
+	err = t.AppBuilder.SizeReport(sectionName)
 
 	if err == nil {
 		if t.LoaderBuilder != nil {
 			fmt.Printf("Size of Loader Image: %s\n", t.LoaderBuilder.buildName)
-			err = t.LoaderBuilder.SizeReport(ram, flash)
+			err = t.LoaderBuilder.SizeReport(sectionName)
 		}
 	}
 
 	return err
 }
 
-func (b *Builder) SizeReport(ram, flash bool) error {
+func (b *Builder) SizeReport(sectionName string) error {
 	srcBase := b.targetBuilder.GetTarget().App().Repo().Path() + "/"
-	err := SizeReport(b.AppElfPath(), srcBase, ram, flash)
+	err := SizeReport(b.AppElfPath(), srcBase, sectionName)
 	if err != nil {
 		return util.NewNewtError(err.Error())
 	}
diff --git a/newt/builder/size_report.go b/newt/builder/size_report.go
index ebe8063..bfb4ad7 100644
--- a/newt/builder/size_report.go
+++ b/newt/builder/size_report.go
@@ -179,18 +179,18 @@ func loadSymbolsAndSections(elfFilePath string) (map[string]*Symbol, error) {
 	return symbols, nil
 }
 
-func generateMemoryRegions(elfFilePath string) (*MemoryRegion, *MemoryRegion,
+func getMemoryRegion(elfFilePath string, sectionName string) (*MemoryRegion,
 	error) {
 
 	mapFile := elfFilePath + ".map"
-	flashRegion, ramRegion, err := parseMapFileRegions(mapFile)
+	sectionRegion, err := parseMapFileRegions(mapFile, sectionName)
 	if err != nil {
-		return nil, nil, err
+		return nil, err
 	}
 
 	objdumpOut, err := runObjdumpCommand(elfFilePath, "-hw")
 	if err != nil {
-		return nil, nil, err
+		return nil, err
 	}
 
 	lines := strings.Split(string(objdumpOut), "\n")
@@ -208,38 +208,30 @@ func generateMemoryRegions(elfFilePath string) (*MemoryRegion, *MemoryRegion,
 			continue
 		}
 
-		if flashRegion.PartOf(address) {
-			flashRegion.TotalSize += size
-			flashRegion.SectionNames[fields[1]] = struct{}{}
-			flashRegion.NamesSizes[fields[1]] = size
-			continue
-		}
-
-		if ramRegion.PartOf(address) {
-			ramRegion.TotalSize += size
-			ramRegion.SectionNames[fields[1]] = struct{}{}
-			ramRegion.NamesSizes[fields[1]] = size
+		if sectionRegion.PartOf(address) {
+			sectionRegion.TotalSize += size
+			sectionRegion.SectionNames[fields[1]] = struct{}{}
+			sectionRegion.NamesSizes[fields[1]] = size
 			continue
 		}
 	}
 
-	return flashRegion, ramRegion, nil
+	return sectionRegion, nil
 }
 
 /*
  * Go through GCC generated mapfile, and collect info about symbol sizes
  */
-func parseMapFileRegions(fileName string) (*MemoryRegion, *MemoryRegion,
+func parseMapFileRegions(fileName string, sectionName string) (*MemoryRegion,
 	error) {
 	var state int = 0
 
 	file, err := os.Open(fileName)
 	if err != nil {
-		return nil, nil, err
+		return nil, err
 	}
 
-	flashRegion := MakeMemoryRegion()
-	ramRegion := MakeMemoryRegion()
+	sectionRegion := MakeMemoryRegion()
 
 	scanner := bufio.NewScanner(file)
 	for scanner.Scan() {
@@ -266,52 +258,39 @@ func parseMapFileRegions(fileName string) (*MemoryRegion, *MemoryRegion,
 			if err != nil {
 				continue
 			}
-			if strings.EqualFold(array[0], "flash") {
-				flashRegion.Name = array[0]
-				flashRegion.Offset = offset
-				flashRegion.EndOff = offset + size
-			} else if strings.EqualFold(array[0], "ram") {
-				ramRegion.Name = array[0]
-				ramRegion.Offset = offset
-				ramRegion.EndOff = offset + size
+
+			if strings.EqualFold(array[0], sectionName) {
+				sectionRegion.Name = array[0]
+				sectionRegion.Offset = offset
+				sectionRegion.EndOff = offset + size
 			}
 		case 3:
 			fallthrough
 		default:
-			return flashRegion, ramRegion, nil
+			return sectionRegion, nil
 		}
 
 	}
-	return flashRegion, flashRegion, nil
+	return sectionRegion, nil
 }
 
-func logMemoryRegionStats(flashRegion, ramRegion *MemoryRegion) {
-	util.StatusMessage(util.VERBOSITY_VERBOSE, "%-10s 0x%08x-0x%08x\n",
-		"Mem FLASH:", flashRegion.Offset, flashRegion.EndOff)
+func logMemoryRegionStats(memRegion *MemoryRegion, sectionName string) {
+	memName := fmt.Sprintf("Mem %s:", sectionName)
 	util.StatusMessage(util.VERBOSITY_VERBOSE, "%-10s 0x%08x-0x%08x\n",
-		"Mem RAM:", ramRegion.Offset, ramRegion.EndOff)
+		memName, memRegion.Offset, memRegion.EndOff)
 	util.StatusMessage(util.VERBOSITY_VERBOSE, "\n")
-	util.StatusMessage(util.VERBOSITY_VERBOSE, "Mem: FLASH\n")
+	util.StatusMessage(util.VERBOSITY_VERBOSE, "Mem: %s\n", sectionName)
 	util.StatusMessage(util.VERBOSITY_VERBOSE, "%-20s %10s\n", "Name", "Size")
-	for sectionName, size := range flashRegion.NamesSizes {
+	for sectionName, size := range memRegion.NamesSizes {
 		util.StatusMessage(util.VERBOSITY_VERBOSE, "%-20s %10d\n",
 			sectionName, size)
 	}
 	util.StatusMessage(util.VERBOSITY_VERBOSE, "%-20s %10d\n", "Total",
-		flashRegion.TotalSize)
-	util.StatusMessage(util.VERBOSITY_VERBOSE, "\n")
-	util.StatusMessage(util.VERBOSITY_VERBOSE, "Mem: RAM\n")
-	util.StatusMessage(util.VERBOSITY_VERBOSE, "%-20s %10s\n", "Name", "Size")
-	for sectionName, size := range ramRegion.NamesSizes {
-		util.StatusMessage(util.VERBOSITY_VERBOSE, "%-20s %10d\n",
-			sectionName, size)
-	}
-	util.StatusMessage(util.VERBOSITY_VERBOSE, "%-20s %10d\n", "Total",
-		ramRegion.TotalSize)
+		memRegion.TotalSize)
 	util.StatusMessage(util.VERBOSITY_VERBOSE, "\n")
 }
 
-func SizeReport(elfFilePath, srcBase string, ram bool, flash bool) error {
+func SizeReport(elfFilePath, srcBase string, sectionName string) error {
 	symbolsPath, err := loadSymbolsAndPaths(elfFilePath, srcBase)
 	if err != nil {
 		return err
@@ -320,35 +299,22 @@ func SizeReport(elfFilePath, srcBase string, ram bool, flash bool) error {
 	if err != nil {
 		return err
 	}
-	flashRegion, ramRegion, err := generateMemoryRegions(elfFilePath)
+	sectionRegion, err := getMemoryRegion(elfFilePath, sectionName)
 	if err != nil {
 		return err
 	}
 
-	logMemoryRegionStats(flashRegion, ramRegion)
+	logMemoryRegionStats(sectionRegion, sectionName)
 
 	startPath := "."
 
-	if flash {
-		flashNodes := newFolder(startPath)
-		for _, symbol := range loadedSectionSizes {
-			if _, ok := flashRegion.SectionNames[symbol.Section]; ok {
-				flashNodes.addSymbol(symbol, symbolsPath[symbol.Name])
-			}
-		}
-		fmt.Println("FLASH report:")
-		fmt.Printf("%v", flashNodes.ToString(flashRegion.TotalSize))
-	}
-
-	if ram {
-		ramNodes := newFolder(startPath)
-		for _, symbol := range loadedSectionSizes {
-			if _, ok := ramRegion.SectionNames[symbol.Section]; ok {
-				ramNodes.addSymbol(symbol, symbolsPath[symbol.Name])
-			}
+	sectionNodes := newFolder(startPath)
+	for _, symbol := range loadedSectionSizes {
+		if _, ok := sectionRegion.SectionNames[symbol.Section]; ok {
+			sectionNodes.addSymbol(symbol, symbolsPath[symbol.Name])
 		}
-		fmt.Println("RAM report:")
-		fmt.Printf("%v", ramNodes.ToString(ramRegion.TotalSize))
 	}
+	fmt.Printf("%s report:\n", sectionName)
+	fmt.Printf("%v", sectionNodes.ToString(sectionRegion.TotalSize))
 	return nil
 }
diff --git a/newt/cli/build_cmds.go b/newt/cli/build_cmds.go
index 9df1a9d..c858e32 100644
--- a/newt/cli/build_cmds.go
+++ b/newt/cli/build_cmds.go
@@ -364,7 +364,7 @@ func debugRunCmd(cmd *cobra.Command, args []string) {
 	}
 }
 
-func sizeRunCmd(cmd *cobra.Command, args []string, ram bool, flash bool) {
+func sizeRunCmd(cmd *cobra.Command, args []string, ram bool, flash bool, section string) {
 	if len(args) < 1 {
 		NewtUsage(cmd, util.NewNewtError("Must specify target"))
 	}
@@ -381,10 +381,28 @@ func sizeRunCmd(cmd *cobra.Command, args []string, ram bool, flash bool) {
 		NewtUsage(nil, err)
 	}
 
-	if ram || flash {
-		if err := b.SizeReport(ram, flash); err != nil {
-			NewtUsage(cmd, err)
+	var sections []string
+
+
+	if ram {
+		sections = append(sections, "RAM")
+	}
+
+	if flash {
+		sections = append(sections, "FLASH")
+	}
+
+	if section != "" {
+		sections = append(sections, section)
+	}
+
+	if len(sections) > 0 {
+		for _, sectionName := range sections {
+			if err := b.SizeReport(sectionName); err != nil {
+				NewtUsage(cmd, err)
+			}
 		}
+
 		return
 	}
 
@@ -479,18 +497,20 @@ func AddBuildCommands(cmd *cobra.Command) {
 		"<target-name>."
 
 	var ram, flash bool
+	var section string
 	sizeCmd := &cobra.Command{
 		Use:   "size <target-name>",
 		Short: "Size of target components",
 		Long:  sizeHelpText,
 		Run: func(cmd *cobra.Command, args []string) {
-			sizeRunCmd(cmd, args, ram, flash)
+			sizeRunCmd(cmd, args, ram, flash, section)
 		},
 	}
 
 	sizeCmd.Flags().BoolVarP(&ram, "ram", "R", false, "Print RAM statistics")
 	sizeCmd.Flags().BoolVarP(&flash, "flash", "F", false,
 		"Print FLASH statistics")
+	sizeCmd.Flags().StringVarP(&section, "section", "S", "", "Print section statistics")
 
 	cmd.AddCommand(sizeCmd)
 	AddTabCompleteFn(sizeCmd, targetList)


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services