Source file src/cmd/vendor/github.com/google/pprof/internal/report/source.go

     1  // Copyright 2014 Google Inc. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package report
    16  
    17  // This file contains routines related to the generation of annotated
    18  // source listings.
    19  
    20  import (
    21  	"bufio"
    22  	"fmt"
    23  	"html/template"
    24  	"io"
    25  	"os"
    26  	"path/filepath"
    27  	"regexp"
    28  	"slices"
    29  	"sort"
    30  	"strconv"
    31  	"strings"
    32  
    33  	"github.com/google/pprof/internal/graph"
    34  	"github.com/google/pprof/internal/measurement"
    35  	"github.com/google/pprof/internal/plugin"
    36  	"github.com/google/pprof/profile"
    37  )
    38  
    39  // printSource prints an annotated source listing, include all
    40  // functions with samples that match the regexp rpt.options.symbol.
    41  // The sources are sorted by function name and then by filename to
    42  // eliminate potential nondeterminism.
    43  func printSource(w io.Writer, rpt *Report) error {
    44  	o := rpt.options
    45  	g := rpt.newGraph(nil)
    46  
    47  	// Identify all the functions that match the regexp provided.
    48  	// Group nodes for each matching function.
    49  	var functions graph.Nodes
    50  	functionNodes := make(map[string]graph.Nodes)
    51  	for _, n := range g.Nodes {
    52  		if !o.Symbol.MatchString(n.Info.Name) {
    53  			continue
    54  		}
    55  		if functionNodes[n.Info.Name] == nil {
    56  			functions = append(functions, n)
    57  		}
    58  		functionNodes[n.Info.Name] = append(functionNodes[n.Info.Name], n)
    59  	}
    60  	functions.Sort(graph.NameOrder)
    61  
    62  	if len(functionNodes) == 0 {
    63  		return fmt.Errorf("no matches found for regexp: %s", o.Symbol)
    64  	}
    65  
    66  	sourcePath := o.SourcePath
    67  	if sourcePath == "" {
    68  		wd, err := os.Getwd()
    69  		if err != nil {
    70  			return fmt.Errorf("could not stat current dir: %v", err)
    71  		}
    72  		sourcePath = wd
    73  	}
    74  	reader := newSourceReader(sourcePath, o.TrimPath)
    75  
    76  	fmt.Fprintf(w, "Total: %s\n", rpt.formatValue(rpt.total))
    77  	for _, fn := range functions {
    78  		name := fn.Info.Name
    79  
    80  		// Identify all the source files associated to this function.
    81  		// Group nodes for each source file.
    82  		var sourceFiles graph.Nodes
    83  		fileNodes := make(map[string]graph.Nodes)
    84  		for _, n := range functionNodes[name] {
    85  			if n.Info.File == "" {
    86  				continue
    87  			}
    88  			if fileNodes[n.Info.File] == nil {
    89  				sourceFiles = append(sourceFiles, n)
    90  			}
    91  			fileNodes[n.Info.File] = append(fileNodes[n.Info.File], n)
    92  		}
    93  
    94  		if len(sourceFiles) == 0 {
    95  			fmt.Fprintf(w, "No source information for %s\n", name)
    96  			continue
    97  		}
    98  
    99  		sourceFiles.Sort(graph.FileOrder)
   100  
   101  		// Print each file associated with this function.
   102  		for _, fl := range sourceFiles {
   103  			filename := fl.Info.File
   104  			fns := fileNodes[filename]
   105  			flatSum, cumSum := fns.Sum()
   106  
   107  			fnodes, _, err := getSourceFromFile(filename, reader, fns, 0, 0)
   108  			fmt.Fprintf(w, "ROUTINE ======================== %s in %s\n", name, filename)
   109  			fmt.Fprintf(w, "%10s %10s (flat, cum) %s of Total\n",
   110  				rpt.formatValue(flatSum), rpt.formatValue(cumSum),
   111  				measurement.Percentage(cumSum, rpt.total))
   112  
   113  			if err != nil {
   114  				fmt.Fprintf(w, " Error: %v\n", err)
   115  				continue
   116  			}
   117  
   118  			for _, fn := range fnodes {
   119  				fmt.Fprintf(w, "%10s %10s %6d:%s\n", valueOrDot(fn.Flat, rpt), valueOrDot(fn.Cum, rpt), fn.Info.Lineno, fn.Info.Name)
   120  			}
   121  		}
   122  	}
   123  	return nil
   124  }
   125  
   126  // sourcePrinter holds state needed for generating source+asm HTML listing.
   127  type sourcePrinter struct {
   128  	reader     *sourceReader
   129  	synth      *synthCode
   130  	objectTool plugin.ObjTool
   131  	objects    map[string]plugin.ObjFile  // Opened object files
   132  	sym        *regexp.Regexp             // May be nil
   133  	files      map[string]*sourceFile     // Set of files to print.
   134  	insts      map[uint64]instructionInfo // Instructions of interest (keyed by address).
   135  
   136  	// Set of function names that we are interested in (because they had
   137  	// a sample and match sym).
   138  	interest map[string]bool
   139  
   140  	// Mapping from system function names to printable names.
   141  	prettyNames map[string]string
   142  }
   143  
   144  // addrInfo holds information for an address we are interested in.
   145  type addrInfo struct {
   146  	loc *profile.Location // Always non-nil
   147  	obj plugin.ObjFile    // May be nil
   148  }
   149  
   150  // instructionInfo holds collected information for an instruction.
   151  type instructionInfo struct {
   152  	objAddr   uint64 // Address in object file (with base subtracted out)
   153  	length    int    // Instruction length in bytes
   154  	disasm    string // Disassembly of instruction
   155  	file      string // For top-level function in which instruction occurs
   156  	line      int    // For top-level function in which instruction occurs
   157  	flat, cum int64  // Samples to report (divisor already applied)
   158  }
   159  
   160  // sourceFile contains collected information for files we will print.
   161  type sourceFile struct {
   162  	fname    string
   163  	cum      int64
   164  	flat     int64
   165  	lines    map[int][]sourceInst // Instructions to show per line
   166  	funcName map[int]string       // Function name per line
   167  }
   168  
   169  // sourceInst holds information for an instruction to be displayed.
   170  type sourceInst struct {
   171  	addr  uint64
   172  	stack []callID // Inlined call-stack
   173  }
   174  
   175  // sourceFunction contains information for a contiguous range of lines per function we
   176  // will print.
   177  type sourceFunction struct {
   178  	name       string
   179  	begin, end int // Line numbers (end is not included in the range)
   180  	flat, cum  int64
   181  }
   182  
   183  // addressRange is a range of addresses plus the object file that contains it.
   184  type addressRange struct {
   185  	begin, end uint64
   186  	obj        plugin.ObjFile
   187  	mapping    *profile.Mapping
   188  	score      int64 // Used to order ranges for processing
   189  }
   190  
   191  // WebListData holds the data needed to generate HTML source code listing.
   192  type WebListData struct {
   193  	Total string
   194  	Files []WebListFile
   195  }
   196  
   197  // WebListFile holds the per-file information for HTML source code listing.
   198  type WebListFile struct {
   199  	Funcs []WebListFunc
   200  }
   201  
   202  // WebListFunc holds the per-function information for HTML source code listing.
   203  type WebListFunc struct {
   204  	Name       string
   205  	File       string
   206  	Flat       string
   207  	Cumulative string
   208  	Percent    string
   209  	Lines      []WebListLine
   210  }
   211  
   212  // WebListLine holds the per-source-line information for HTML source code listing.
   213  type WebListLine struct {
   214  	SrcLine      string
   215  	HTMLClass    string
   216  	Line         int
   217  	Flat         string
   218  	Cumulative   string
   219  	Instructions []WebListInstruction
   220  }
   221  
   222  // WebListInstruction holds the per-instruction information for HTML source code listing.
   223  type WebListInstruction struct {
   224  	NewBlock     bool // Insert marker that indicates separation from previous block
   225  	Flat         string
   226  	Cumulative   string
   227  	Synthetic    bool
   228  	Address      uint64
   229  	Disasm       string
   230  	FileLine     string
   231  	InlinedCalls []WebListCall
   232  }
   233  
   234  // WebListCall holds the per-inlined-call information for HTML source code listing.
   235  type WebListCall struct {
   236  	SrcLine  string
   237  	FileBase string
   238  	Line     int
   239  }
   240  
   241  // MakeWebList returns an annotated source listing of rpt.
   242  // rpt.prof should contain inlined call info.
   243  func MakeWebList(rpt *Report, obj plugin.ObjTool, maxFiles int) (WebListData, error) {
   244  	sourcePath := rpt.options.SourcePath
   245  	if sourcePath == "" {
   246  		wd, err := os.Getwd()
   247  		if err != nil {
   248  			return WebListData{}, fmt.Errorf("could not stat current dir: %v", err)
   249  		}
   250  		sourcePath = wd
   251  	}
   252  	sp := newSourcePrinter(rpt, obj, sourcePath)
   253  	if len(sp.interest) == 0 {
   254  		return WebListData{}, fmt.Errorf("no matches found for regexp: %s", rpt.options.Symbol)
   255  	}
   256  	defer sp.close()
   257  	return sp.generate(maxFiles, rpt), nil
   258  }
   259  
   260  func newSourcePrinter(rpt *Report, obj plugin.ObjTool, sourcePath string) *sourcePrinter {
   261  	sp := &sourcePrinter{
   262  		reader:      newSourceReader(sourcePath, rpt.options.TrimPath),
   263  		synth:       newSynthCode(rpt.prof.Mapping),
   264  		objectTool:  obj,
   265  		objects:     map[string]plugin.ObjFile{},
   266  		sym:         rpt.options.Symbol,
   267  		files:       map[string]*sourceFile{},
   268  		insts:       map[uint64]instructionInfo{},
   269  		prettyNames: map[string]string{},
   270  		interest:    map[string]bool{},
   271  	}
   272  
   273  	// If the regexp source can be parsed as an address, also match
   274  	// functions that land on that address.
   275  	var address *uint64
   276  	if sp.sym != nil {
   277  		if hex, err := strconv.ParseUint(sp.sym.String(), 0, 64); err == nil {
   278  			address = &hex
   279  		}
   280  	}
   281  
   282  	addrs := map[uint64]addrInfo{}
   283  	flat := map[uint64]int64{}
   284  	cum := map[uint64]int64{}
   285  
   286  	// Record an interest in the function corresponding to lines[index].
   287  	markInterest := func(addr uint64, loc *profile.Location, index int) {
   288  		fn := loc.Line[index]
   289  		if fn.Function == nil {
   290  			return
   291  		}
   292  		sp.interest[fn.Function.Name] = true
   293  		sp.interest[fn.Function.SystemName] = true
   294  		if _, ok := addrs[addr]; !ok {
   295  			addrs[addr] = addrInfo{loc, sp.objectFile(loc.Mapping)}
   296  		}
   297  	}
   298  
   299  	// See if sp.sym matches line.
   300  	matches := func(line profile.Line) bool {
   301  		if line.Function == nil {
   302  			return false
   303  		}
   304  		return sp.sym.MatchString(line.Function.Name) ||
   305  			sp.sym.MatchString(line.Function.SystemName) ||
   306  			sp.sym.MatchString(line.Function.Filename)
   307  	}
   308  
   309  	// Extract sample counts and compute set of interesting functions.
   310  	for _, sample := range rpt.prof.Sample {
   311  		value := rpt.options.SampleValue(sample.Value)
   312  		if rpt.options.SampleMeanDivisor != nil {
   313  			div := rpt.options.SampleMeanDivisor(sample.Value)
   314  			if div != 0 {
   315  				value /= div
   316  			}
   317  		}
   318  
   319  		// Find call-sites matching sym.
   320  		for i := len(sample.Location) - 1; i >= 0; i-- {
   321  			loc := sample.Location[i]
   322  			for _, line := range loc.Line {
   323  				if line.Function == nil {
   324  					continue
   325  				}
   326  				sp.prettyNames[line.Function.SystemName] = line.Function.Name
   327  			}
   328  
   329  			addr := loc.Address
   330  			if addr == 0 {
   331  				// Some profiles are missing valid addresses.
   332  				addr = sp.synth.address(loc)
   333  			}
   334  
   335  			cum[addr] += value
   336  			if i == 0 {
   337  				flat[addr] += value
   338  			}
   339  
   340  			if sp.sym == nil || (address != nil && addr == *address) {
   341  				// Interested in top-level entry of stack.
   342  				if len(loc.Line) > 0 {
   343  					markInterest(addr, loc, len(loc.Line)-1)
   344  				}
   345  				continue
   346  			}
   347  
   348  			// Search in inlined stack for a match.
   349  			matchFile := (loc.Mapping != nil && sp.sym.MatchString(loc.Mapping.File))
   350  			for j, line := range loc.Line {
   351  				if (j == 0 && matchFile) || matches(line) {
   352  					markInterest(addr, loc, j)
   353  				}
   354  			}
   355  		}
   356  	}
   357  
   358  	sp.expandAddresses(rpt, addrs, flat)
   359  	sp.initSamples(flat, cum)
   360  	return sp
   361  }
   362  
   363  func (sp *sourcePrinter) close() {
   364  	for _, objFile := range sp.objects {
   365  		if objFile != nil {
   366  			objFile.Close()
   367  		}
   368  	}
   369  }
   370  
   371  func (sp *sourcePrinter) expandAddresses(rpt *Report, addrs map[uint64]addrInfo, flat map[uint64]int64) {
   372  	// We found interesting addresses (ones with non-zero samples) above.
   373  	// Get covering address ranges and disassemble the ranges.
   374  	ranges, unprocessed := sp.splitIntoRanges(rpt.prof, addrs, flat)
   375  	sp.handleUnprocessed(addrs, unprocessed)
   376  
   377  	// Trim ranges if there are too many.
   378  	const maxRanges = 25
   379  	sort.Slice(ranges, func(i, j int) bool {
   380  		return ranges[i].score > ranges[j].score
   381  	})
   382  	if len(ranges) > maxRanges {
   383  		ranges = ranges[:maxRanges]
   384  	}
   385  
   386  	for _, r := range ranges {
   387  		objBegin, err := r.obj.ObjAddr(r.begin)
   388  		if err != nil {
   389  			fmt.Fprintf(os.Stderr, "Failed to compute objdump address for range start %x: %v\n", r.begin, err)
   390  			continue
   391  		}
   392  		objEnd, err := r.obj.ObjAddr(r.end)
   393  		if err != nil {
   394  			fmt.Fprintf(os.Stderr, "Failed to compute objdump address for range end %x: %v\n", r.end, err)
   395  			continue
   396  		}
   397  		base := r.begin - objBegin
   398  		insts, err := sp.objectTool.Disasm(r.mapping.File, objBegin, objEnd, rpt.options.IntelSyntax)
   399  		if err != nil {
   400  			// TODO(sanjay): Report that the covered addresses are missing.
   401  			continue
   402  		}
   403  
   404  		var lastFrames []plugin.Frame
   405  		var lastAddr, maxAddr uint64
   406  		for i, inst := range insts {
   407  			addr := inst.Addr + base
   408  
   409  			// Guard against duplicate output from Disasm.
   410  			if addr <= maxAddr {
   411  				continue
   412  			}
   413  			maxAddr = addr
   414  
   415  			length := 1
   416  			if i+1 < len(insts) && insts[i+1].Addr > inst.Addr {
   417  				// Extend to next instruction.
   418  				length = int(insts[i+1].Addr - inst.Addr)
   419  			}
   420  
   421  			// Get inlined-call-stack for address.
   422  			frames, err := r.obj.SourceLine(addr)
   423  			if err != nil {
   424  				// Construct a frame from disassembler output.
   425  				frames = []plugin.Frame{{Func: inst.Function, File: inst.File, Line: inst.Line}}
   426  			}
   427  
   428  			x := instructionInfo{objAddr: inst.Addr, length: length, disasm: inst.Text}
   429  			if len(frames) > 0 {
   430  				// We could consider using the outer-most caller's source
   431  				// location so we give the some hint as to where the
   432  				// inlining happened that led to this instruction. So for
   433  				// example, suppose we have the following (inlined) call
   434  				// chains for this instruction:
   435  				//   F1->G->H
   436  				//   F2->G->H
   437  				// We could tag the instructions from the first call with
   438  				// F1 and instructions from the second call with F2. But
   439  				// that leads to a somewhat confusing display. So for now,
   440  				// we stick with just the inner-most location (i.e., H).
   441  				// In the future we will consider changing the display to
   442  				// make caller info more visible.
   443  				index := 0 // Inner-most frame
   444  				x.file = frames[index].File
   445  				x.line = frames[index].Line
   446  			}
   447  			sp.insts[addr] = x
   448  
   449  			// We sometimes get instructions with a zero reported line number.
   450  			// Make such instructions have the same line info as the preceding
   451  			// instruction, if an earlier instruction is found close enough.
   452  			const neighborhood = 32
   453  			if len(frames) > 0 && frames[0].Line != 0 {
   454  				lastFrames = frames
   455  				lastAddr = addr
   456  			} else if (addr-lastAddr <= neighborhood) && lastFrames != nil {
   457  				frames = lastFrames
   458  			}
   459  
   460  			sp.addStack(addr, frames)
   461  		}
   462  	}
   463  }
   464  
   465  func (sp *sourcePrinter) addStack(addr uint64, frames []plugin.Frame) {
   466  	// See if the stack contains a function we are interested in.
   467  	for i, f := range frames {
   468  		if !sp.interest[f.Func] {
   469  			continue
   470  		}
   471  
   472  		// Record sub-stack under frame's file/line.
   473  		fname := canonicalizeFileName(f.File)
   474  		file := sp.files[fname]
   475  		if file == nil {
   476  			file = &sourceFile{
   477  				fname:    fname,
   478  				lines:    map[int][]sourceInst{},
   479  				funcName: map[int]string{},
   480  			}
   481  			sp.files[fname] = file
   482  		}
   483  		callees := frames[:i]
   484  		stack := make([]callID, 0, len(callees))
   485  		for j := len(callees) - 1; j >= 0; j-- { // Reverse so caller is first
   486  			stack = append(stack, callID{
   487  				file: callees[j].File,
   488  				line: callees[j].Line,
   489  			})
   490  		}
   491  		file.lines[f.Line] = append(file.lines[f.Line], sourceInst{addr, stack})
   492  
   493  		// Remember the first function name encountered per source line
   494  		// and assume that line belongs to that function.
   495  		if _, ok := file.funcName[f.Line]; !ok {
   496  			file.funcName[f.Line] = f.Func
   497  		}
   498  	}
   499  }
   500  
   501  // synthAsm is the special disassembler value used for instructions without an object file.
   502  const synthAsm = ""
   503  
   504  // handleUnprocessed handles addresses that were skipped by splitIntoRanges because they
   505  // did not belong to a known object file.
   506  func (sp *sourcePrinter) handleUnprocessed(addrs map[uint64]addrInfo, unprocessed []uint64) {
   507  	// makeFrames synthesizes a []plugin.Frame list for the specified address.
   508  	// The result will typically have length 1, but may be longer if address corresponds
   509  	// to inlined calls.
   510  	makeFrames := func(addr uint64) []plugin.Frame {
   511  		loc := addrs[addr].loc
   512  		stack := make([]plugin.Frame, 0, len(loc.Line))
   513  		for _, line := range loc.Line {
   514  			fn := line.Function
   515  			if fn == nil {
   516  				continue
   517  			}
   518  			stack = append(stack, plugin.Frame{
   519  				Func: fn.Name,
   520  				File: fn.Filename,
   521  				Line: int(line.Line),
   522  			})
   523  		}
   524  		return stack
   525  	}
   526  
   527  	for _, addr := range unprocessed {
   528  		frames := makeFrames(addr)
   529  		x := instructionInfo{
   530  			objAddr: addr,
   531  			length:  1,
   532  			disasm:  synthAsm,
   533  		}
   534  		if len(frames) > 0 {
   535  			x.file = frames[0].File
   536  			x.line = frames[0].Line
   537  		}
   538  		sp.insts[addr] = x
   539  
   540  		sp.addStack(addr, frames)
   541  	}
   542  }
   543  
   544  // splitIntoRanges converts the set of addresses we are interested in into a set of address
   545  // ranges to disassemble. It also returns the set of addresses found that did not have an
   546  // associated object file and were therefore not added to an address range.
   547  func (sp *sourcePrinter) splitIntoRanges(prof *profile.Profile, addrMap map[uint64]addrInfo, flat map[uint64]int64) ([]addressRange, []uint64) {
   548  	// Partition addresses into two sets: ones with a known object file, and ones without.
   549  	var addrs, unprocessed []uint64
   550  	for addr, info := range addrMap {
   551  		if info.obj != nil {
   552  			addrs = append(addrs, addr)
   553  		} else {
   554  			unprocessed = append(unprocessed, addr)
   555  		}
   556  	}
   557  	slices.Sort(addrs)
   558  
   559  	const expand = 500 // How much to expand range to pick up nearby addresses.
   560  	var result []addressRange
   561  	for i, n := 0, len(addrs); i < n; {
   562  		begin, end := addrs[i], addrs[i]
   563  		sum := flat[begin]
   564  		i++
   565  
   566  		info := addrMap[begin]
   567  		m := info.loc.Mapping
   568  		obj := info.obj // Non-nil because of the partitioning done above.
   569  
   570  		// Find following addresses that are close enough to addrs[i].
   571  		for i < n && addrs[i] <= end+2*expand && addrs[i] < m.Limit {
   572  			// When we expand ranges by "expand" on either side, the ranges
   573  			// for addrs[i] and addrs[i-1] will merge.
   574  			end = addrs[i]
   575  			sum += flat[end]
   576  			i++
   577  		}
   578  		if m.Start-begin >= expand {
   579  			begin -= expand
   580  		} else {
   581  			begin = m.Start
   582  		}
   583  		if m.Limit-end >= expand {
   584  			end += expand
   585  		} else {
   586  			end = m.Limit
   587  		}
   588  
   589  		result = append(result, addressRange{begin, end, obj, m, sum})
   590  	}
   591  	return result, unprocessed
   592  }
   593  
   594  func (sp *sourcePrinter) initSamples(flat, cum map[uint64]int64) {
   595  	for addr, inst := range sp.insts {
   596  		// Move all samples that were assigned to the middle of an instruction to the
   597  		// beginning of that instruction. This takes care of samples that were recorded
   598  		// against pc+1.
   599  		instEnd := addr + uint64(inst.length)
   600  		for p := addr; p < instEnd; p++ {
   601  			inst.flat += flat[p]
   602  			inst.cum += cum[p]
   603  		}
   604  		sp.insts[addr] = inst
   605  	}
   606  }
   607  
   608  func (sp *sourcePrinter) generate(maxFiles int, rpt *Report) WebListData {
   609  	// Finalize per-file counts.
   610  	for _, file := range sp.files {
   611  		seen := map[uint64]bool{}
   612  		for _, line := range file.lines {
   613  			for _, x := range line {
   614  				if seen[x.addr] {
   615  					// Same address can be displayed multiple times in a file
   616  					// (e.g., if we show multiple inlined functions).
   617  					// Avoid double-counting samples in this case.
   618  					continue
   619  				}
   620  				seen[x.addr] = true
   621  				inst := sp.insts[x.addr]
   622  				file.cum += inst.cum
   623  				file.flat += inst.flat
   624  			}
   625  		}
   626  	}
   627  
   628  	// Get sorted list of files to print.
   629  	var files []*sourceFile
   630  	for _, f := range sp.files {
   631  		files = append(files, f)
   632  	}
   633  	order := func(i, j int) bool { return files[i].flat > files[j].flat }
   634  	if maxFiles < 0 {
   635  		// Order by name for compatibility with old code.
   636  		order = func(i, j int) bool { return files[i].fname < files[j].fname }
   637  		maxFiles = len(files)
   638  	}
   639  	sort.Slice(files, order)
   640  	result := WebListData{
   641  		Total: rpt.formatValue(rpt.total),
   642  	}
   643  	for i, f := range files {
   644  		if i < maxFiles {
   645  			result.Files = append(result.Files, sp.generateFile(f, rpt))
   646  		}
   647  	}
   648  	return result
   649  }
   650  
   651  func (sp *sourcePrinter) generateFile(f *sourceFile, rpt *Report) WebListFile {
   652  	var result WebListFile
   653  	for _, fn := range sp.functions(f) {
   654  		if fn.cum == 0 {
   655  			continue
   656  		}
   657  
   658  		listfn := WebListFunc{
   659  			Name:       fn.name,
   660  			File:       f.fname,
   661  			Flat:       rpt.formatValue(fn.flat),
   662  			Cumulative: rpt.formatValue(fn.cum),
   663  			Percent:    measurement.Percentage(fn.cum, rpt.total),
   664  		}
   665  		var asm []assemblyInstruction
   666  		for l := fn.begin; l < fn.end; l++ {
   667  			lineContents, ok := sp.reader.line(f.fname, l)
   668  			if !ok {
   669  				if len(f.lines[l]) == 0 {
   670  					// Outside of range of valid lines and nothing to print.
   671  					continue
   672  				}
   673  				if l == 0 {
   674  					// Line number 0 shows up if line number is not known.
   675  					lineContents = "<instructions with unknown line numbers>"
   676  				} else {
   677  					// Past end of file, but have data to print.
   678  					lineContents = "???"
   679  				}
   680  			}
   681  
   682  			// Make list of assembly instructions.
   683  			asm = asm[:0]
   684  			var flatSum, cumSum int64
   685  			var lastAddr uint64
   686  			for _, inst := range f.lines[l] {
   687  				addr := inst.addr
   688  				x := sp.insts[addr]
   689  				flatSum += x.flat
   690  				cumSum += x.cum
   691  				startsBlock := (addr != lastAddr+uint64(sp.insts[lastAddr].length))
   692  				lastAddr = addr
   693  
   694  				// divisors already applied, so leave flatDiv,cumDiv as 0
   695  				asm = append(asm, assemblyInstruction{
   696  					address:     x.objAddr,
   697  					instruction: x.disasm,
   698  					function:    fn.name,
   699  					file:        x.file,
   700  					line:        x.line,
   701  					flat:        x.flat,
   702  					cum:         x.cum,
   703  					startsBlock: startsBlock,
   704  					inlineCalls: inst.stack,
   705  				})
   706  			}
   707  
   708  			listfn.Lines = append(listfn.Lines, makeWebListLine(l, flatSum, cumSum, lineContents, asm, sp.reader, rpt))
   709  		}
   710  
   711  		result.Funcs = append(result.Funcs, listfn)
   712  	}
   713  	return result
   714  }
   715  
   716  // functions splits apart the lines to show in a file into a list of per-function ranges.
   717  func (sp *sourcePrinter) functions(f *sourceFile) []sourceFunction {
   718  	var funcs []sourceFunction
   719  
   720  	// Get interesting lines in sorted order.
   721  	lines := make([]int, 0, len(f.lines))
   722  	for l := range f.lines {
   723  		lines = append(lines, l)
   724  	}
   725  	sort.Ints(lines)
   726  
   727  	// Merge adjacent lines that are in same function and not too far apart.
   728  	const mergeLimit = 20
   729  	for _, l := range lines {
   730  		name := f.funcName[l]
   731  		if pretty, ok := sp.prettyNames[name]; ok {
   732  			// Use demangled name if available.
   733  			name = pretty
   734  		}
   735  
   736  		fn := sourceFunction{name: name, begin: l, end: l + 1}
   737  		for _, x := range f.lines[l] {
   738  			inst := sp.insts[x.addr]
   739  			fn.flat += inst.flat
   740  			fn.cum += inst.cum
   741  		}
   742  
   743  		// See if we should merge into preceding function.
   744  		if len(funcs) > 0 {
   745  			last := funcs[len(funcs)-1]
   746  			if l-last.end < mergeLimit && last.name == name {
   747  				last.end = l + 1
   748  				last.flat += fn.flat
   749  				last.cum += fn.cum
   750  				funcs[len(funcs)-1] = last
   751  				continue
   752  			}
   753  		}
   754  
   755  		// Add new function.
   756  		funcs = append(funcs, fn)
   757  	}
   758  
   759  	// Expand function boundaries to show neighborhood.
   760  	const expand = 5
   761  	for i, f := range funcs {
   762  		if i == 0 {
   763  			// Extend backwards, stopping at line number 1, but do not disturb 0
   764  			// since that is a special line number that can show up when addr2line
   765  			// cannot determine the real line number.
   766  			if f.begin > expand {
   767  				f.begin -= expand
   768  			} else if f.begin > 1 {
   769  				f.begin = 1
   770  			}
   771  		} else {
   772  			// Find gap from predecessor and divide between predecessor and f.
   773  			halfGap := min((f.begin-funcs[i-1].end)/2, expand)
   774  			funcs[i-1].end += halfGap
   775  			f.begin -= halfGap
   776  		}
   777  		funcs[i] = f
   778  	}
   779  
   780  	// Also extend the ending point of the last function.
   781  	if len(funcs) > 0 {
   782  		funcs[len(funcs)-1].end += expand
   783  	}
   784  
   785  	return funcs
   786  }
   787  
   788  // objectFile return the object for the specified mapping, opening it if necessary.
   789  // It returns nil on error.
   790  func (sp *sourcePrinter) objectFile(m *profile.Mapping) plugin.ObjFile {
   791  	if m == nil {
   792  		return nil
   793  	}
   794  	if object, ok := sp.objects[m.File]; ok {
   795  		return object // May be nil if we detected an error earlier.
   796  	}
   797  	object, err := sp.objectTool.Open(m.File, m.Start, m.Limit, m.Offset, m.KernelRelocationSymbol)
   798  	if err != nil {
   799  		object = nil
   800  	}
   801  	sp.objects[m.File] = object // Cache even on error.
   802  	return object
   803  }
   804  
   805  // makeWebListLine returns the contents of a single line in a web listing. This includes
   806  // the source line and the corresponding assembly.
   807  func makeWebListLine(lineNo int, flat, cum int64, lineContents string,
   808  	assembly []assemblyInstruction, reader *sourceReader, rpt *Report) WebListLine {
   809  	line := WebListLine{
   810  		SrcLine:    lineContents,
   811  		Line:       lineNo,
   812  		Flat:       valueOrDot(flat, rpt),
   813  		Cumulative: valueOrDot(cum, rpt),
   814  	}
   815  
   816  	if len(assembly) == 0 {
   817  		line.HTMLClass = "nop"
   818  		return line
   819  	}
   820  
   821  	nestedInfo := false
   822  	line.HTMLClass = "deadsrc"
   823  	for _, an := range assembly {
   824  		if len(an.inlineCalls) > 0 || an.instruction != synthAsm {
   825  			nestedInfo = true
   826  			line.HTMLClass = "livesrc"
   827  		}
   828  	}
   829  
   830  	if nestedInfo {
   831  		srcIndent := indentation(lineContents)
   832  		line.Instructions = makeWebListInstructions(srcIndent, assembly, reader, rpt)
   833  	}
   834  	return line
   835  }
   836  
   837  func makeWebListInstructions(srcIndent int, assembly []assemblyInstruction, reader *sourceReader, rpt *Report) []WebListInstruction {
   838  	var result []WebListInstruction
   839  	var curCalls []callID
   840  	for i, an := range assembly {
   841  		var fileline string
   842  		if an.file != "" {
   843  			fileline = fmt.Sprintf("%s:%d", template.HTMLEscapeString(filepath.Base(an.file)), an.line)
   844  		}
   845  		text := strings.Repeat(" ", srcIndent+4+4*len(an.inlineCalls)) + an.instruction
   846  		inst := WebListInstruction{
   847  			NewBlock:   (an.startsBlock && i != 0),
   848  			Flat:       valueOrDot(an.flat, rpt),
   849  			Cumulative: valueOrDot(an.cum, rpt),
   850  			Synthetic:  (an.instruction == synthAsm),
   851  			Address:    an.address,
   852  			Disasm:     rightPad(text, 80),
   853  			FileLine:   fileline,
   854  		}
   855  
   856  		// Add inlined call context.
   857  		for j, c := range an.inlineCalls {
   858  			if j < len(curCalls) && curCalls[j] == c {
   859  				// Skip if same as previous instruction.
   860  				continue
   861  			}
   862  			curCalls = nil
   863  			fline, ok := reader.line(c.file, c.line)
   864  			if !ok {
   865  				fline = ""
   866  			}
   867  			srcCode := strings.Repeat(" ", srcIndent+4+4*j) + strings.TrimSpace(fline)
   868  			inst.InlinedCalls = append(inst.InlinedCalls, WebListCall{
   869  				SrcLine:  rightPad(srcCode, 80),
   870  				FileBase: filepath.Base(c.file),
   871  				Line:     c.line,
   872  			})
   873  		}
   874  		curCalls = an.inlineCalls
   875  
   876  		result = append(result, inst)
   877  	}
   878  	return result
   879  }
   880  
   881  // getSourceFromFile collects the sources of a function from a source
   882  // file and annotates it with the samples in fns. Returns the sources
   883  // as nodes, using the info.name field to hold the source code.
   884  func getSourceFromFile(file string, reader *sourceReader, fns graph.Nodes, start, end int) (graph.Nodes, string, error) {
   885  	lineNodes := make(map[int]graph.Nodes)
   886  
   887  	// Collect source coordinates from profile.
   888  	const margin = 5 // Lines before first/after last sample.
   889  	if start == 0 {
   890  		if fns[0].Info.StartLine != 0 {
   891  			start = fns[0].Info.StartLine
   892  		} else {
   893  			start = fns[0].Info.Lineno - margin
   894  		}
   895  	} else {
   896  		start -= margin
   897  	}
   898  	if end == 0 {
   899  		end = fns[0].Info.Lineno
   900  	}
   901  	end += margin
   902  	for _, n := range fns {
   903  		lineno := n.Info.Lineno
   904  		nodeStart := n.Info.StartLine
   905  		if nodeStart == 0 {
   906  			nodeStart = lineno - margin
   907  		}
   908  		nodeEnd := lineno + margin
   909  		if nodeStart < start {
   910  			start = nodeStart
   911  		} else if nodeEnd > end {
   912  			end = nodeEnd
   913  		}
   914  		lineNodes[lineno] = append(lineNodes[lineno], n)
   915  	}
   916  	if start < 1 {
   917  		start = 1
   918  	}
   919  
   920  	var src graph.Nodes
   921  	for lineno := start; lineno <= end; lineno++ {
   922  		line, ok := reader.line(file, lineno)
   923  		if !ok {
   924  			break
   925  		}
   926  		flat, cum := lineNodes[lineno].Sum()
   927  		src = append(src, &graph.Node{
   928  			Info: graph.NodeInfo{
   929  				Name:   strings.TrimRight(line, "\n"),
   930  				Lineno: lineno,
   931  			},
   932  			Flat: flat,
   933  			Cum:  cum,
   934  		})
   935  	}
   936  	if err := reader.fileError(file); err != nil {
   937  		return nil, file, err
   938  	}
   939  	return src, file, nil
   940  }
   941  
   942  // sourceReader provides access to source code with caching of file contents.
   943  type sourceReader struct {
   944  	// searchPath is a filepath.ListSeparator-separated list of directories where
   945  	// source files should be searched.
   946  	searchPath string
   947  
   948  	// trimPath is a filepath.ListSeparator-separated list of paths to trim.
   949  	trimPath string
   950  
   951  	// files maps from path name to a list of lines.
   952  	// files[*][0] is unused since line numbering starts at 1.
   953  	files map[string][]string
   954  
   955  	// errors collects errors encountered per file. These errors are
   956  	// consulted before returning out of these module.
   957  	errors map[string]error
   958  }
   959  
   960  func newSourceReader(searchPath, trimPath string) *sourceReader {
   961  	return &sourceReader{
   962  		searchPath,
   963  		trimPath,
   964  		make(map[string][]string),
   965  		make(map[string]error),
   966  	}
   967  }
   968  
   969  func (reader *sourceReader) fileError(path string) error {
   970  	return reader.errors[path]
   971  }
   972  
   973  // line returns the line numbered "lineno" in path, or _,false if lineno is out of range.
   974  func (reader *sourceReader) line(path string, lineno int) (string, bool) {
   975  	lines, ok := reader.files[path]
   976  	if !ok {
   977  		// Read and cache file contents.
   978  		lines = []string{""} // Skip 0th line
   979  		f, err := openSourceFile(path, reader.searchPath, reader.trimPath)
   980  		if err != nil {
   981  			reader.errors[path] = err
   982  		} else {
   983  			s := bufio.NewScanner(f)
   984  			for s.Scan() {
   985  				lines = append(lines, s.Text())
   986  			}
   987  			f.Close()
   988  			if s.Err() != nil {
   989  				reader.errors[path] = err
   990  			}
   991  		}
   992  		reader.files[path] = lines
   993  	}
   994  	if lineno <= 0 || lineno >= len(lines) {
   995  		return "", false
   996  	}
   997  	return lines[lineno], true
   998  }
   999  
  1000  // openSourceFile opens a source file from a name encoded in a profile. File
  1001  // names in a profile after can be relative paths, so search them in each of
  1002  // the paths in searchPath and their parents. In case the profile contains
  1003  // absolute paths, additional paths may be configured to trim from the source
  1004  // paths in the profile. This effectively turns the path into a relative path
  1005  // searching it using searchPath as usual).
  1006  func openSourceFile(path, searchPath, trim string) (*os.File, error) {
  1007  	path = trimPath(path, trim, searchPath)
  1008  	// If file is still absolute, require file to exist.
  1009  	if filepath.IsAbs(path) {
  1010  		f, err := os.Open(path)
  1011  		return f, err
  1012  	}
  1013  	// Scan each component of the path.
  1014  	for _, dir := range filepath.SplitList(searchPath) {
  1015  		// Search up for every parent of each possible path.
  1016  		for {
  1017  			filename := filepath.Join(dir, path)
  1018  			if f, err := os.Open(filename); err == nil {
  1019  				return f, nil
  1020  			}
  1021  			parent := filepath.Dir(dir)
  1022  			if parent == dir {
  1023  				break
  1024  			}
  1025  			dir = parent
  1026  		}
  1027  	}
  1028  
  1029  	return nil, fmt.Errorf("could not find file %s on path %s", path, searchPath)
  1030  }
  1031  
  1032  // trimPath cleans up a path by removing prefixes that are commonly
  1033  // found on profiles plus configured prefixes.
  1034  // TODO(aalexand): Consider optimizing out the redundant work done in this
  1035  // function if it proves to matter.
  1036  func trimPath(path, trimPath, searchPath string) string {
  1037  	// Keep path variable intact as it's used below to form the return value.
  1038  	sPath, searchPath := filepath.ToSlash(path), filepath.ToSlash(searchPath)
  1039  	if trimPath == "" {
  1040  		// If the trim path is not configured, try to guess it heuristically:
  1041  		// search for basename of each search path in the original path and, if
  1042  		// found, strip everything up to and including the basename. So, for
  1043  		// example, given original path "/some/remote/path/my-project/foo/bar.c"
  1044  		// and search path "/my/local/path/my-project" the heuristic will return
  1045  		// "/my/local/path/my-project/foo/bar.c".
  1046  		for _, dir := range filepath.SplitList(searchPath) {
  1047  			want := "/" + filepath.Base(dir) + "/"
  1048  			if found := strings.Index(sPath, want); found != -1 {
  1049  				return path[found+len(want):]
  1050  			}
  1051  		}
  1052  	}
  1053  	// Trim configured trim prefixes.
  1054  	trimPaths := append(filepath.SplitList(filepath.ToSlash(trimPath)), "/proc/self/cwd/./", "/proc/self/cwd/")
  1055  	for _, trimPath := range trimPaths {
  1056  		if !strings.HasSuffix(trimPath, "/") {
  1057  			trimPath += "/"
  1058  		}
  1059  		if strings.HasPrefix(sPath, trimPath) {
  1060  			return path[len(trimPath):]
  1061  		}
  1062  	}
  1063  	return path
  1064  }
  1065  
  1066  func indentation(line string) int {
  1067  	column := 0
  1068  	for _, c := range line {
  1069  		if c == ' ' {
  1070  			column++
  1071  		} else if c == '\t' {
  1072  			column++
  1073  			for column%8 != 0 {
  1074  				column++
  1075  			}
  1076  		} else {
  1077  			break
  1078  		}
  1079  	}
  1080  	return column
  1081  }
  1082  
  1083  // rightPad pads the input with spaces on the right-hand-side to make it have
  1084  // at least width n. It treats tabs as enough spaces that lead to the next
  1085  // 8-aligned tab-stop.
  1086  func rightPad(s string, n int) string {
  1087  	var str strings.Builder
  1088  
  1089  	// Convert tabs to spaces as we go so padding works regardless of what prefix
  1090  	// is placed before the result.
  1091  	column := 0
  1092  	for _, c := range s {
  1093  		column++
  1094  		if c == '\t' {
  1095  			str.WriteRune(' ')
  1096  			for column%8 != 0 {
  1097  				column++
  1098  				str.WriteRune(' ')
  1099  			}
  1100  		} else {
  1101  			str.WriteRune(c)
  1102  		}
  1103  	}
  1104  	for column < n {
  1105  		column++
  1106  		str.WriteRune(' ')
  1107  	}
  1108  	return str.String()
  1109  }
  1110  
  1111  func canonicalizeFileName(fname string) string {
  1112  	fname = strings.TrimPrefix(fname, "/proc/self/cwd/")
  1113  	fname = strings.TrimPrefix(fname, "./")
  1114  	return filepath.Clean(fname)
  1115  }
  1116  

View as plain text