MCPcopy Create free account
hub / github.com/google/pprof / getSourceFromFile

Function getSourceFromFile

internal/report/source.go:884–940  ·  view source on GitHub ↗

getSourceFromFile collects the sources of a function from a source file and annotates it with the samples in fns. Returns the sources as nodes, using the info.name field to hold the source code.

(file string, reader *sourceReader, fns graph.Nodes, start, end int)

Source from the content-addressed store, hash-verified

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.
884func 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

Callers 1

printSourceFunction · 0.85

Calls 3

lineMethod · 0.80
fileErrorMethod · 0.80
SumMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…