...

Source file src/runtime/pprof/proto.go

Documentation: runtime/pprof

     1  // Copyright 2016 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package pprof
     6  
     7  import (
     8  	"bytes"
     9  	"compress/gzip"
    10  	"fmt"
    11  	"internal/abi"
    12  	"io"
    13  	"os"
    14  	"runtime"
    15  	"strconv"
    16  	"strings"
    17  	"time"
    18  	"unsafe"
    19  )
    20  
    21  // lostProfileEvent is the function to which lost profiling
    22  // events are attributed.
    23  // (The name shows up in the pprof graphs.)
    24  func lostProfileEvent() { lostProfileEvent() }
    25  
    26  // A profileBuilder writes a profile incrementally from a
    27  // stream of profile samples delivered by the runtime.
    28  type profileBuilder struct {
    29  	start      time.Time
    30  	end        time.Time
    31  	havePeriod bool
    32  	period     int64
    33  	m          profMap
    34  
    35  	// encoding state
    36  	w         io.Writer
    37  	zw        *gzip.Writer
    38  	pb        protobuf
    39  	strings   []string
    40  	stringMap map[string]int
    41  	locs      map[uintptr]locInfo // list of locInfo starting with the given PC.
    42  	funcs     map[string]int      // Package path-qualified function name to Function.ID
    43  	mem       []memMap
    44  	deck      pcDeck
    45  }
    46  
    47  type memMap struct {
    48  	// initialized as reading mapping
    49  	start         uintptr
    50  	end           uintptr
    51  	offset        uint64
    52  	file, buildID string
    53  
    54  	funcs symbolizeFlag
    55  	fake  bool // map entry was faked; /proc/self/maps wasn't available
    56  }
    57  
    58  // symbolizeFlag keeps track of symbolization result.
    59  //
    60  //	0                  : no symbol lookup was performed
    61  //	1<<0 (lookupTried) : symbol lookup was performed
    62  //	1<<1 (lookupFailed): symbol lookup was performed but failed
    63  type symbolizeFlag uint8
    64  
    65  const (
    66  	lookupTried  symbolizeFlag = 1 << iota
    67  	lookupFailed symbolizeFlag = 1 << iota
    68  )
    69  
    70  const (
    71  	// message Profile
    72  	tagProfile_SampleType        = 1  // repeated ValueType
    73  	tagProfile_Sample            = 2  // repeated Sample
    74  	tagProfile_Mapping           = 3  // repeated Mapping
    75  	tagProfile_Location          = 4  // repeated Location
    76  	tagProfile_Function          = 5  // repeated Function
    77  	tagProfile_StringTable       = 6  // repeated string
    78  	tagProfile_DropFrames        = 7  // int64 (string table index)
    79  	tagProfile_KeepFrames        = 8  // int64 (string table index)
    80  	tagProfile_TimeNanos         = 9  // int64
    81  	tagProfile_DurationNanos     = 10 // int64
    82  	tagProfile_PeriodType        = 11 // ValueType (really optional string???)
    83  	tagProfile_Period            = 12 // int64
    84  	tagProfile_Comment           = 13 // repeated int64
    85  	tagProfile_DefaultSampleType = 14 // int64
    86  
    87  	// message ValueType
    88  	tagValueType_Type = 1 // int64 (string table index)
    89  	tagValueType_Unit = 2 // int64 (string table index)
    90  
    91  	// message Sample
    92  	tagSample_Location = 1 // repeated uint64
    93  	tagSample_Value    = 2 // repeated int64
    94  	tagSample_Label    = 3 // repeated Label
    95  
    96  	// message Label
    97  	tagLabel_Key = 1 // int64 (string table index)
    98  	tagLabel_Str = 2 // int64 (string table index)
    99  	tagLabel_Num = 3 // int64
   100  
   101  	// message Mapping
   102  	tagMapping_ID              = 1  // uint64
   103  	tagMapping_Start           = 2  // uint64
   104  	tagMapping_Limit           = 3  // uint64
   105  	tagMapping_Offset          = 4  // uint64
   106  	tagMapping_Filename        = 5  // int64 (string table index)
   107  	tagMapping_BuildID         = 6  // int64 (string table index)
   108  	tagMapping_HasFunctions    = 7  // bool
   109  	tagMapping_HasFilenames    = 8  // bool
   110  	tagMapping_HasLineNumbers  = 9  // bool
   111  	tagMapping_HasInlineFrames = 10 // bool
   112  
   113  	// message Location
   114  	tagLocation_ID        = 1 // uint64
   115  	tagLocation_MappingID = 2 // uint64
   116  	tagLocation_Address   = 3 // uint64
   117  	tagLocation_Line      = 4 // repeated Line
   118  
   119  	// message Line
   120  	tagLine_FunctionID = 1 // uint64
   121  	tagLine_Line       = 2 // int64
   122  
   123  	// message Function
   124  	tagFunction_ID         = 1 // uint64
   125  	tagFunction_Name       = 2 // int64 (string table index)
   126  	tagFunction_SystemName = 3 // int64 (string table index)
   127  	tagFunction_Filename   = 4 // int64 (string table index)
   128  	tagFunction_StartLine  = 5 // int64
   129  )
   130  
   131  // stringIndex adds s to the string table if not already present
   132  // and returns the index of s in the string table.
   133  func (b *profileBuilder) stringIndex(s string) int64 {
   134  	id, ok := b.stringMap[s]
   135  	if !ok {
   136  		id = len(b.strings)
   137  		b.strings = append(b.strings, s)
   138  		b.stringMap[s] = id
   139  	}
   140  	return int64(id)
   141  }
   142  
   143  func (b *profileBuilder) flush() {
   144  	const dataFlush = 4096
   145  	if b.pb.nest == 0 && len(b.pb.data) > dataFlush {
   146  		b.zw.Write(b.pb.data)
   147  		b.pb.data = b.pb.data[:0]
   148  	}
   149  }
   150  
   151  // pbValueType encodes a ValueType message to b.pb.
   152  func (b *profileBuilder) pbValueType(tag int, typ, unit string) {
   153  	start := b.pb.startMessage()
   154  	b.pb.int64(tagValueType_Type, b.stringIndex(typ))
   155  	b.pb.int64(tagValueType_Unit, b.stringIndex(unit))
   156  	b.pb.endMessage(tag, start)
   157  }
   158  
   159  // pbSample encodes a Sample message to b.pb.
   160  func (b *profileBuilder) pbSample(values []int64, locs []uint64, labels func()) {
   161  	start := b.pb.startMessage()
   162  	b.pb.int64s(tagSample_Value, values)
   163  	b.pb.uint64s(tagSample_Location, locs)
   164  	if labels != nil {
   165  		labels()
   166  	}
   167  	b.pb.endMessage(tagProfile_Sample, start)
   168  	b.flush()
   169  }
   170  
   171  // pbLabel encodes a Label message to b.pb.
   172  func (b *profileBuilder) pbLabel(tag int, key, str string, num int64) {
   173  	start := b.pb.startMessage()
   174  	b.pb.int64Opt(tagLabel_Key, b.stringIndex(key))
   175  	b.pb.int64Opt(tagLabel_Str, b.stringIndex(str))
   176  	b.pb.int64Opt(tagLabel_Num, num)
   177  	b.pb.endMessage(tag, start)
   178  }
   179  
   180  // pbLine encodes a Line message to b.pb.
   181  func (b *profileBuilder) pbLine(tag int, funcID uint64, line int64) {
   182  	start := b.pb.startMessage()
   183  	b.pb.uint64Opt(tagLine_FunctionID, funcID)
   184  	b.pb.int64Opt(tagLine_Line, line)
   185  	b.pb.endMessage(tag, start)
   186  }
   187  
   188  // pbMapping encodes a Mapping message to b.pb.
   189  func (b *profileBuilder) pbMapping(tag int, id, base, limit, offset uint64, file, buildID string, hasFuncs bool) {
   190  	start := b.pb.startMessage()
   191  	b.pb.uint64Opt(tagMapping_ID, id)
   192  	b.pb.uint64Opt(tagMapping_Start, base)
   193  	b.pb.uint64Opt(tagMapping_Limit, limit)
   194  	b.pb.uint64Opt(tagMapping_Offset, offset)
   195  	b.pb.int64Opt(tagMapping_Filename, b.stringIndex(file))
   196  	b.pb.int64Opt(tagMapping_BuildID, b.stringIndex(buildID))
   197  	// TODO: we set HasFunctions if all symbols from samples were symbolized (hasFuncs).
   198  	// Decide what to do about HasInlineFrames and HasLineNumbers.
   199  	// Also, another approach to handle the mapping entry with
   200  	// incomplete symbolization results is to dupliace the mapping
   201  	// entry (but with different Has* fields values) and use
   202  	// different entries for symbolized locations and unsymbolized locations.
   203  	if hasFuncs {
   204  		b.pb.bool(tagMapping_HasFunctions, true)
   205  	}
   206  	b.pb.endMessage(tag, start)
   207  }
   208  
   209  func allFrames(addr uintptr) ([]runtime.Frame, symbolizeFlag) {
   210  	// Expand this one address using CallersFrames so we can cache
   211  	// each expansion. In general, CallersFrames takes a whole
   212  	// stack, but in this case we know there will be no skips in
   213  	// the stack and we have return PCs anyway.
   214  	frames := runtime.CallersFrames([]uintptr{addr})
   215  	frame, more := frames.Next()
   216  	if frame.Function == "runtime.goexit" {
   217  		// Short-circuit if we see runtime.goexit so the loop
   218  		// below doesn't allocate a useless empty location.
   219  		return nil, 0
   220  	}
   221  
   222  	symbolizeResult := lookupTried
   223  	if frame.PC == 0 || frame.Function == "" || frame.File == "" || frame.Line == 0 {
   224  		symbolizeResult |= lookupFailed
   225  	}
   226  
   227  	if frame.PC == 0 {
   228  		// If we failed to resolve the frame, at least make up
   229  		// a reasonable call PC. This mostly happens in tests.
   230  		frame.PC = addr - 1
   231  	}
   232  	ret := []runtime.Frame{frame}
   233  	for frame.Function != "runtime.goexit" && more == true {
   234  		frame, more = frames.Next()
   235  		ret = append(ret, frame)
   236  	}
   237  	return ret, symbolizeResult
   238  }
   239  
   240  type locInfo struct {
   241  	// location id assigned by the profileBuilder
   242  	id uint64
   243  
   244  	// sequence of PCs, including the fake PCs returned by the traceback
   245  	// to represent inlined functions
   246  	// https://github.com/golang/go/blob/d6f2f833c93a41ec1c68e49804b8387a06b131c5/src/runtime/traceback.go#L347-L368
   247  	pcs []uintptr
   248  
   249  	// firstPCFrames and firstPCSymbolizeResult hold the results of the
   250  	// allFrames call for the first (leaf-most) PC this locInfo represents
   251  	firstPCFrames          []runtime.Frame
   252  	firstPCSymbolizeResult symbolizeFlag
   253  }
   254  
   255  // newProfileBuilder returns a new profileBuilder.
   256  // CPU profiling data obtained from the runtime can be added
   257  // by calling b.addCPUData, and then the eventual profile
   258  // can be obtained by calling b.finish.
   259  func newProfileBuilder(w io.Writer) *profileBuilder {
   260  	zw, _ := gzip.NewWriterLevel(w, gzip.BestSpeed)
   261  	b := &profileBuilder{
   262  		w:         w,
   263  		zw:        zw,
   264  		start:     time.Now(),
   265  		strings:   []string{""},
   266  		stringMap: map[string]int{"": 0},
   267  		locs:      map[uintptr]locInfo{},
   268  		funcs:     map[string]int{},
   269  	}
   270  	b.readMapping()
   271  	return b
   272  }
   273  
   274  // addCPUData adds the CPU profiling data to the profile.
   275  //
   276  // The data must be a whole number of records, as delivered by the runtime.
   277  // len(tags) must be equal to the number of records in data.
   278  func (b *profileBuilder) addCPUData(data []uint64, tags []unsafe.Pointer) error {
   279  	if !b.havePeriod {
   280  		// first record is period
   281  		if len(data) < 3 {
   282  			return fmt.Errorf("truncated profile")
   283  		}
   284  		if data[0] != 3 || data[2] == 0 {
   285  			return fmt.Errorf("malformed profile")
   286  		}
   287  		// data[2] is sampling rate in Hz. Convert to sampling
   288  		// period in nanoseconds.
   289  		b.period = 1e9 / int64(data[2])
   290  		b.havePeriod = true
   291  		data = data[3:]
   292  		// Consume tag slot. Note that there isn't a meaningful tag
   293  		// value for this record.
   294  		tags = tags[1:]
   295  	}
   296  
   297  	// Parse CPU samples from the profile.
   298  	// Each sample is 3+n uint64s:
   299  	//	data[0] = 3+n
   300  	//	data[1] = time stamp (ignored)
   301  	//	data[2] = count
   302  	//	data[3:3+n] = stack
   303  	// If the count is 0 and the stack has length 1,
   304  	// that's an overflow record inserted by the runtime
   305  	// to indicate that stack[0] samples were lost.
   306  	// Otherwise the count is usually 1,
   307  	// but in a few special cases like lost non-Go samples
   308  	// there can be larger counts.
   309  	// Because many samples with the same stack arrive,
   310  	// we want to deduplicate immediately, which we do
   311  	// using the b.m profMap.
   312  	for len(data) > 0 {
   313  		if len(data) < 3 || data[0] > uint64(len(data)) {
   314  			return fmt.Errorf("truncated profile")
   315  		}
   316  		if data[0] < 3 || tags != nil && len(tags) < 1 {
   317  			return fmt.Errorf("malformed profile")
   318  		}
   319  		if len(tags) < 1 {
   320  			return fmt.Errorf("mismatched profile records and tags")
   321  		}
   322  		count := data[2]
   323  		stk := data[3:data[0]]
   324  		data = data[data[0]:]
   325  		tag := tags[0]
   326  		tags = tags[1:]
   327  
   328  		if count == 0 && len(stk) == 1 {
   329  			// overflow record
   330  			count = uint64(stk[0])
   331  			stk = []uint64{
   332  				// gentraceback guarantees that PCs in the
   333  				// stack can be unconditionally decremented and
   334  				// still be valid, so we must do the same.
   335  				uint64(abi.FuncPCABIInternal(lostProfileEvent) + 1),
   336  			}
   337  		}
   338  		b.m.lookup(stk, tag).count += int64(count)
   339  	}
   340  
   341  	if len(tags) != 0 {
   342  		return fmt.Errorf("mismatched profile records and tags")
   343  	}
   344  	return nil
   345  }
   346  
   347  // build completes and returns the constructed profile.
   348  func (b *profileBuilder) build() {
   349  	b.end = time.Now()
   350  
   351  	b.pb.int64Opt(tagProfile_TimeNanos, b.start.UnixNano())
   352  	if b.havePeriod { // must be CPU profile
   353  		b.pbValueType(tagProfile_SampleType, "samples", "count")
   354  		b.pbValueType(tagProfile_SampleType, "cpu", "nanoseconds")
   355  		b.pb.int64Opt(tagProfile_DurationNanos, b.end.Sub(b.start).Nanoseconds())
   356  		b.pbValueType(tagProfile_PeriodType, "cpu", "nanoseconds")
   357  		b.pb.int64Opt(tagProfile_Period, b.period)
   358  	}
   359  
   360  	values := []int64{0, 0}
   361  	var locs []uint64
   362  
   363  	for e := b.m.all; e != nil; e = e.nextAll {
   364  		values[0] = e.count
   365  		values[1] = e.count * b.period
   366  
   367  		var labels func()
   368  		if e.tag != nil {
   369  			labels = func() {
   370  				for k, v := range *(*labelMap)(e.tag) {
   371  					b.pbLabel(tagSample_Label, k, v, 0)
   372  				}
   373  			}
   374  		}
   375  
   376  		locs = b.appendLocsForStack(locs[:0], e.stk)
   377  
   378  		b.pbSample(values, locs, labels)
   379  	}
   380  
   381  	for i, m := range b.mem {
   382  		hasFunctions := m.funcs == lookupTried // lookupTried but not lookupFailed
   383  		b.pbMapping(tagProfile_Mapping, uint64(i+1), uint64(m.start), uint64(m.end), m.offset, m.file, m.buildID, hasFunctions)
   384  	}
   385  
   386  	// TODO: Anything for tagProfile_DropFrames?
   387  	// TODO: Anything for tagProfile_KeepFrames?
   388  
   389  	b.pb.strings(tagProfile_StringTable, b.strings)
   390  	b.zw.Write(b.pb.data)
   391  	b.zw.Close()
   392  }
   393  
   394  // appendLocsForStack appends the location IDs for the given stack trace to the given
   395  // location ID slice, locs. The addresses in the stack are return PCs or 1 + the PC of
   396  // an inline marker as the runtime traceback function returns.
   397  //
   398  // It may emit to b.pb, so there must be no message encoding in progress.
   399  func (b *profileBuilder) appendLocsForStack(locs []uint64, stk []uintptr) (newLocs []uint64) {
   400  	b.deck.reset()
   401  
   402  	// The last frame might be truncated. Recover lost inline frames.
   403  	stk = runtime_expandFinalInlineFrame(stk)
   404  
   405  	for len(stk) > 0 {
   406  		addr := stk[0]
   407  		if l, ok := b.locs[addr]; ok {
   408  			// When generating code for an inlined function, the compiler adds
   409  			// NOP instructions to the outermost function as a placeholder for
   410  			// each layer of inlining. When the runtime generates tracebacks for
   411  			// stacks that include inlined functions, it uses the addresses of
   412  			// those NOPs as "fake" PCs on the stack as if they were regular
   413  			// function call sites. But if a profiling signal arrives while the
   414  			// CPU is executing one of those NOPs, its PC will show up as a leaf
   415  			// in the profile with its own Location entry. So, always check
   416  			// whether addr is a "fake" PC in the context of the current call
   417  			// stack by trying to add it to the inlining deck before assuming
   418  			// that the deck is complete.
   419  			if len(b.deck.pcs) > 0 {
   420  				if added := b.deck.tryAdd(addr, l.firstPCFrames, l.firstPCSymbolizeResult); added {
   421  					stk = stk[1:]
   422  					continue
   423  				}
   424  			}
   425  
   426  			// first record the location if there is any pending accumulated info.
   427  			if id := b.emitLocation(); id > 0 {
   428  				locs = append(locs, id)
   429  			}
   430  
   431  			// then, record the cached location.
   432  			locs = append(locs, l.id)
   433  
   434  			// Skip the matching pcs.
   435  			//
   436  			// Even if stk was truncated due to the stack depth
   437  			// limit, expandFinalInlineFrame above has already
   438  			// fixed the truncation, ensuring it is long enough.
   439  			stk = stk[len(l.pcs):]
   440  			continue
   441  		}
   442  
   443  		frames, symbolizeResult := allFrames(addr)
   444  		if len(frames) == 0 { // runtime.goexit.
   445  			if id := b.emitLocation(); id > 0 {
   446  				locs = append(locs, id)
   447  			}
   448  			stk = stk[1:]
   449  			continue
   450  		}
   451  
   452  		if added := b.deck.tryAdd(addr, frames, symbolizeResult); added {
   453  			stk = stk[1:]
   454  			continue
   455  		}
   456  		// add failed because this addr is not inlined with the
   457  		// existing PCs in the deck. Flush the deck and retry handling
   458  		// this pc.
   459  		if id := b.emitLocation(); id > 0 {
   460  			locs = append(locs, id)
   461  		}
   462  
   463  		// check cache again - previous emitLocation added a new entry
   464  		if l, ok := b.locs[addr]; ok {
   465  			locs = append(locs, l.id)
   466  			stk = stk[len(l.pcs):] // skip the matching pcs.
   467  		} else {
   468  			b.deck.tryAdd(addr, frames, symbolizeResult) // must succeed.
   469  			stk = stk[1:]
   470  		}
   471  	}
   472  	if id := b.emitLocation(); id > 0 { // emit remaining location.
   473  		locs = append(locs, id)
   474  	}
   475  	return locs
   476  }
   477  
   478  // Here's an example of how Go 1.17 writes out inlined functions, compiled for
   479  // linux/amd64. The disassembly of main.main shows two levels of inlining: main
   480  // calls b, b calls a, a does some work.
   481  //
   482  //   inline.go:9   0x4553ec  90              NOPL                 // func main()    { b(v) }
   483  //   inline.go:6   0x4553ed  90              NOPL                 // func b(v *int) { a(v) }
   484  //   inline.go:5   0x4553ee  48c7002a000000  MOVQ $0x2a, 0(AX)    // func a(v *int) { *v = 42 }
   485  //
   486  // If a profiling signal arrives while executing the MOVQ at 0x4553ee (for line
   487  // 5), the runtime will report the stack as the MOVQ frame being called by the
   488  // NOPL at 0x4553ed (for line 6) being called by the NOPL at 0x4553ec (for line
   489  // 9).
   490  //
   491  // The role of pcDeck is to collapse those three frames back into a single
   492  // location at 0x4553ee, with file/line/function symbolization info representing
   493  // the three layers of calls. It does that via sequential calls to pcDeck.tryAdd
   494  // starting with the leaf-most address. The fourth call to pcDeck.tryAdd will be
   495  // for the caller of main.main. Because main.main was not inlined in its caller,
   496  // the deck will reject the addition, and the fourth PC on the stack will get
   497  // its own location.
   498  
   499  // pcDeck is a helper to detect a sequence of inlined functions from
   500  // a stack trace returned by the runtime.
   501  //
   502  // The stack traces returned by runtime's trackback functions are fully
   503  // expanded (at least for Go functions) and include the fake pcs representing
   504  // inlined functions. The profile proto expects the inlined functions to be
   505  // encoded in one Location message.
   506  // https://github.com/google/pprof/blob/5e965273ee43930341d897407202dd5e10e952cb/proto/profile.proto#L177-L184
   507  //
   508  // Runtime does not directly expose whether a frame is for an inlined function
   509  // and looking up debug info is not ideal, so we use a heuristic to filter
   510  // the fake pcs and restore the inlined and entry functions. Inlined functions
   511  // have the following properties:
   512  //
   513  //	Frame's Func is nil (note: also true for non-Go functions), and
   514  //	Frame's Entry matches its entry function frame's Entry (note: could also be true for recursive calls and non-Go functions), and
   515  //	Frame's Name does not match its entry function frame's name (note: inlined functions cannot be directly recursive).
   516  //
   517  // As reading and processing the pcs in a stack trace one by one (from leaf to the root),
   518  // we use pcDeck to temporarily hold the observed pcs and their expanded frames
   519  // until we observe the entry function frame.
   520  type pcDeck struct {
   521  	pcs             []uintptr
   522  	frames          []runtime.Frame
   523  	symbolizeResult symbolizeFlag
   524  
   525  	// firstPCFrames indicates the number of frames associated with the first
   526  	// (leaf-most) PC in the deck
   527  	firstPCFrames int
   528  	// firstPCSymbolizeResult holds the results of the allFrames call for the
   529  	// first (leaf-most) PC in the deck
   530  	firstPCSymbolizeResult symbolizeFlag
   531  }
   532  
   533  func (d *pcDeck) reset() {
   534  	d.pcs = d.pcs[:0]
   535  	d.frames = d.frames[:0]
   536  	d.symbolizeResult = 0
   537  	d.firstPCFrames = 0
   538  	d.firstPCSymbolizeResult = 0
   539  }
   540  
   541  // tryAdd tries to add the pc and Frames expanded from it (most likely one,
   542  // since the stack trace is already fully expanded) and the symbolizeResult
   543  // to the deck. If it fails the caller needs to flush the deck and retry.
   544  func (d *pcDeck) tryAdd(pc uintptr, frames []runtime.Frame, symbolizeResult symbolizeFlag) (success bool) {
   545  	if existing := len(d.frames); existing > 0 {
   546  		// 'd.frames' are all expanded from one 'pc' and represent all
   547  		// inlined functions so we check only the last one.
   548  		newFrame := frames[0]
   549  		last := d.frames[existing-1]
   550  		if last.Func != nil { // the last frame can't be inlined. Flush.
   551  			return false
   552  		}
   553  		if last.Entry == 0 || newFrame.Entry == 0 { // Possibly not a Go function. Don't try to merge.
   554  			return false
   555  		}
   556  
   557  		if last.Entry != newFrame.Entry { // newFrame is for a different function.
   558  			return false
   559  		}
   560  		if last.Function == newFrame.Function { // maybe recursion.
   561  			return false
   562  		}
   563  	}
   564  	d.pcs = append(d.pcs, pc)
   565  	d.frames = append(d.frames, frames...)
   566  	d.symbolizeResult |= symbolizeResult
   567  	if len(d.pcs) == 1 {
   568  		d.firstPCFrames = len(d.frames)
   569  		d.firstPCSymbolizeResult = symbolizeResult
   570  	}
   571  	return true
   572  }
   573  
   574  // emitLocation emits the new location and function information recorded in the deck
   575  // and returns the location ID encoded in the profile protobuf.
   576  // It emits to b.pb, so there must be no message encoding in progress.
   577  // It resets the deck.
   578  func (b *profileBuilder) emitLocation() uint64 {
   579  	if len(b.deck.pcs) == 0 {
   580  		return 0
   581  	}
   582  	defer b.deck.reset()
   583  
   584  	addr := b.deck.pcs[0]
   585  	firstFrame := b.deck.frames[0]
   586  
   587  	// We can't write out functions while in the middle of the
   588  	// Location message, so record new functions we encounter and
   589  	// write them out after the Location.
   590  	type newFunc struct {
   591  		id         uint64
   592  		name, file string
   593  	}
   594  	newFuncs := make([]newFunc, 0, 8)
   595  
   596  	id := uint64(len(b.locs)) + 1
   597  	b.locs[addr] = locInfo{
   598  		id:                     id,
   599  		pcs:                    append([]uintptr{}, b.deck.pcs...),
   600  		firstPCSymbolizeResult: b.deck.firstPCSymbolizeResult,
   601  		firstPCFrames:          append([]runtime.Frame{}, b.deck.frames[:b.deck.firstPCFrames]...),
   602  	}
   603  
   604  	start := b.pb.startMessage()
   605  	b.pb.uint64Opt(tagLocation_ID, id)
   606  	b.pb.uint64Opt(tagLocation_Address, uint64(firstFrame.PC))
   607  	for _, frame := range b.deck.frames {
   608  		// Write out each line in frame expansion.
   609  		funcID := uint64(b.funcs[frame.Function])
   610  		if funcID == 0 {
   611  			funcID = uint64(len(b.funcs)) + 1
   612  			b.funcs[frame.Function] = int(funcID)
   613  			newFuncs = append(newFuncs, newFunc{funcID, frame.Function, frame.File})
   614  		}
   615  		b.pbLine(tagLocation_Line, funcID, int64(frame.Line))
   616  	}
   617  	for i := range b.mem {
   618  		if b.mem[i].start <= addr && addr < b.mem[i].end || b.mem[i].fake {
   619  			b.pb.uint64Opt(tagLocation_MappingID, uint64(i+1))
   620  
   621  			m := b.mem[i]
   622  			m.funcs |= b.deck.symbolizeResult
   623  			b.mem[i] = m
   624  			break
   625  		}
   626  	}
   627  	b.pb.endMessage(tagProfile_Location, start)
   628  
   629  	// Write out functions we found during frame expansion.
   630  	for _, fn := range newFuncs {
   631  		start := b.pb.startMessage()
   632  		b.pb.uint64Opt(tagFunction_ID, fn.id)
   633  		b.pb.int64Opt(tagFunction_Name, b.stringIndex(fn.name))
   634  		b.pb.int64Opt(tagFunction_SystemName, b.stringIndex(fn.name))
   635  		b.pb.int64Opt(tagFunction_Filename, b.stringIndex(fn.file))
   636  		b.pb.endMessage(tagProfile_Function, start)
   637  	}
   638  
   639  	b.flush()
   640  	return id
   641  }
   642  
   643  // readMapping reads /proc/self/maps and writes mappings to b.pb.
   644  // It saves the address ranges of the mappings in b.mem for use
   645  // when emitting locations.
   646  func (b *profileBuilder) readMapping() {
   647  	data, _ := os.ReadFile("/proc/self/maps")
   648  	parseProcSelfMaps(data, b.addMapping)
   649  	if len(b.mem) == 0 { // pprof expects a map entry, so fake one.
   650  		b.addMappingEntry(0, 0, 0, "", "", true)
   651  		// TODO(hyangah): make addMapping return *memMap or
   652  		// take a memMap struct, and get rid of addMappingEntry
   653  		// that takes a bunch of positional arguments.
   654  	}
   655  }
   656  
   657  var space = []byte(" ")
   658  var newline = []byte("\n")
   659  
   660  func parseProcSelfMaps(data []byte, addMapping func(lo, hi, offset uint64, file, buildID string)) {
   661  	// $ cat /proc/self/maps
   662  	// 00400000-0040b000 r-xp 00000000 fc:01 787766                             /bin/cat
   663  	// 0060a000-0060b000 r--p 0000a000 fc:01 787766                             /bin/cat
   664  	// 0060b000-0060c000 rw-p 0000b000 fc:01 787766                             /bin/cat
   665  	// 014ab000-014cc000 rw-p 00000000 00:00 0                                  [heap]
   666  	// 7f7d76af8000-7f7d7797c000 r--p 00000000 fc:01 1318064                    /usr/lib/locale/locale-archive
   667  	// 7f7d7797c000-7f7d77b36000 r-xp 00000000 fc:01 1180226                    /lib/x86_64-linux-gnu/libc-2.19.so
   668  	// 7f7d77b36000-7f7d77d36000 ---p 001ba000 fc:01 1180226                    /lib/x86_64-linux-gnu/libc-2.19.so
   669  	// 7f7d77d36000-7f7d77d3a000 r--p 001ba000 fc:01 1180226                    /lib/x86_64-linux-gnu/libc-2.19.so
   670  	// 7f7d77d3a000-7f7d77d3c000 rw-p 001be000 fc:01 1180226                    /lib/x86_64-linux-gnu/libc-2.19.so
   671  	// 7f7d77d3c000-7f7d77d41000 rw-p 00000000 00:00 0
   672  	// 7f7d77d41000-7f7d77d64000 r-xp 00000000 fc:01 1180217                    /lib/x86_64-linux-gnu/ld-2.19.so
   673  	// 7f7d77f3f000-7f7d77f42000 rw-p 00000000 00:00 0
   674  	// 7f7d77f61000-7f7d77f63000 rw-p 00000000 00:00 0
   675  	// 7f7d77f63000-7f7d77f64000 r--p 00022000 fc:01 1180217                    /lib/x86_64-linux-gnu/ld-2.19.so
   676  	// 7f7d77f64000-7f7d77f65000 rw-p 00023000 fc:01 1180217                    /lib/x86_64-linux-gnu/ld-2.19.so
   677  	// 7f7d77f65000-7f7d77f66000 rw-p 00000000 00:00 0
   678  	// 7ffc342a2000-7ffc342c3000 rw-p 00000000 00:00 0                          [stack]
   679  	// 7ffc34343000-7ffc34345000 r-xp 00000000 00:00 0                          [vdso]
   680  	// ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
   681  
   682  	var line []byte
   683  	// next removes and returns the next field in the line.
   684  	// It also removes from line any spaces following the field.
   685  	next := func() []byte {
   686  		var f []byte
   687  		f, line, _ = bytes.Cut(line, space)
   688  		line = bytes.TrimLeft(line, " ")
   689  		return f
   690  	}
   691  
   692  	for len(data) > 0 {
   693  		line, data, _ = bytes.Cut(data, newline)
   694  		addr := next()
   695  		loStr, hiStr, ok := strings.Cut(string(addr), "-")
   696  		if !ok {
   697  			continue
   698  		}
   699  		lo, err := strconv.ParseUint(loStr, 16, 64)
   700  		if err != nil {
   701  			continue
   702  		}
   703  		hi, err := strconv.ParseUint(hiStr, 16, 64)
   704  		if err != nil {
   705  			continue
   706  		}
   707  		perm := next()
   708  		if len(perm) < 4 || perm[2] != 'x' {
   709  			// Only interested in executable mappings.
   710  			continue
   711  		}
   712  		offset, err := strconv.ParseUint(string(next()), 16, 64)
   713  		if err != nil {
   714  			continue
   715  		}
   716  		next()          // dev
   717  		inode := next() // inode
   718  		if line == nil {
   719  			continue
   720  		}
   721  		file := string(line)
   722  
   723  		// Trim deleted file marker.
   724  		deletedStr := " (deleted)"
   725  		deletedLen := len(deletedStr)
   726  		if len(file) >= deletedLen && file[len(file)-deletedLen:] == deletedStr {
   727  			file = file[:len(file)-deletedLen]
   728  		}
   729  
   730  		if len(inode) == 1 && inode[0] == '0' && file == "" {
   731  			// Huge-page text mappings list the initial fragment of
   732  			// mapped but unpopulated memory as being inode 0.
   733  			// Don't report that part.
   734  			// But [vdso] and [vsyscall] are inode 0, so let non-empty file names through.
   735  			continue
   736  		}
   737  
   738  		// TODO: pprof's remapMappingIDs makes two adjustments:
   739  		// 1. If there is an /anon_hugepage mapping first and it is
   740  		// consecutive to a next mapping, drop the /anon_hugepage.
   741  		// 2. If start-offset = 0x400000, change start to 0x400000 and offset to 0.
   742  		// There's no indication why either of these is needed.
   743  		// Let's try not doing these and see what breaks.
   744  		// If we do need them, they would go here, before we
   745  		// enter the mappings into b.mem in the first place.
   746  
   747  		buildID, _ := elfBuildID(file)
   748  		addMapping(lo, hi, offset, file, buildID)
   749  	}
   750  }
   751  
   752  func (b *profileBuilder) addMapping(lo, hi, offset uint64, file, buildID string) {
   753  	b.addMappingEntry(lo, hi, offset, file, buildID, false)
   754  }
   755  
   756  func (b *profileBuilder) addMappingEntry(lo, hi, offset uint64, file, buildID string, fake bool) {
   757  	b.mem = append(b.mem, memMap{
   758  		start:   uintptr(lo),
   759  		end:     uintptr(hi),
   760  		offset:  offset,
   761  		file:    file,
   762  		buildID: buildID,
   763  		fake:    fake,
   764  	})
   765  }
   766  

View as plain text