Source file src/cmd/compile/internal/dwarfgen/dwarf.go

     1  // Copyright 2011 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 dwarfgen
     6  
     7  import (
     8  	"bytes"
     9  	"flag"
    10  	"fmt"
    11  	"internal/buildcfg"
    12  	"slices"
    13  	"sort"
    14  	"strings"
    15  
    16  	"cmd/compile/internal/base"
    17  	"cmd/compile/internal/ir"
    18  	"cmd/compile/internal/reflectdata"
    19  	"cmd/compile/internal/ssa"
    20  	"cmd/compile/internal/ssagen"
    21  	"cmd/compile/internal/typecheck"
    22  	"cmd/compile/internal/types"
    23  	"cmd/internal/dwarf"
    24  	"cmd/internal/obj"
    25  	"cmd/internal/objabi"
    26  	"cmd/internal/src"
    27  )
    28  
    29  func Info(ctxt *obj.Link, fnsym *obj.LSym, infosym *obj.LSym, curfn obj.Func) (scopes []dwarf.Scope, inlcalls dwarf.InlCalls) {
    30  	fn := curfn.(*ir.Func)
    31  
    32  	if fn.Nname != nil {
    33  		expect := fn.Linksym()
    34  		if fnsym.ABI() == obj.ABI0 {
    35  			expect = fn.LinksymABI(obj.ABI0)
    36  		}
    37  		if fnsym != expect {
    38  			base.Fatalf("unexpected fnsym: %v != %v", fnsym, expect)
    39  		}
    40  	}
    41  
    42  	// Back when there were two different *Funcs for a function, this code
    43  	// was not consistent about whether a particular *Node being processed
    44  	// was an ODCLFUNC or ONAME node. Partly this is because inlined function
    45  	// bodies have no ODCLFUNC node, which was it's own inconsistency.
    46  	// In any event, the handling of the two different nodes for DWARF purposes
    47  	// was subtly different, likely in unintended ways. CL 272253 merged the
    48  	// two nodes' Func fields, so that code sees the same *Func whether it is
    49  	// holding the ODCLFUNC or the ONAME. This resulted in changes in the
    50  	// DWARF output. To preserve the existing DWARF output and leave an
    51  	// intentional change for a future CL, this code does the following when
    52  	// fn.Op == ONAME:
    53  	//
    54  	// 1. Disallow use of createComplexVars in createDwarfVars.
    55  	//    It was not possible to reach that code for an ONAME before,
    56  	//    because the DebugInfo was set only on the ODCLFUNC Func.
    57  	//    Calling into it in the ONAME case causes an index out of bounds panic.
    58  	//
    59  	// 2. Do not populate apdecls. fn.Func.Dcl was in the ODCLFUNC Func,
    60  	//    not the ONAME Func. Populating apdecls for the ONAME case results
    61  	//    in selected being populated after createSimpleVars is called in
    62  	//    createDwarfVars, and then that causes the loop to skip all the entries
    63  	//    in dcl, meaning that the RecordAutoType calls don't happen.
    64  	//
    65  	// These two adjustments keep toolstash -cmp working for now.
    66  	// Deciding the right answer is, as they say, future work.
    67  	//
    68  	// We can tell the difference between the old ODCLFUNC and ONAME
    69  	// cases by looking at the infosym.Name. If it's empty, DebugInfo is
    70  	// being called from (*obj.Link).populateDWARF, which used to use
    71  	// the ODCLFUNC. If it's non-empty (the name will end in $abstract),
    72  	// DebugInfo is being called from (*obj.Link).DwarfAbstractFunc,
    73  	// which used to use the ONAME form.
    74  	isODCLFUNC := infosym.Name == ""
    75  
    76  	var apdecls []*ir.Name
    77  	// Populate decls for fn.
    78  	if isODCLFUNC {
    79  		for _, n := range fn.Dcl {
    80  			if n.Op() != ir.ONAME { // might be OTYPE or OLITERAL
    81  				continue
    82  			}
    83  			switch n.Class {
    84  			case ir.PAUTO:
    85  				if !n.Used() {
    86  					// Text == nil -> generating abstract function
    87  					if fnsym.Func().Text != nil {
    88  						base.Fatalf("debuginfo unused node (AllocFrame should truncate fn.Func.Dcl)")
    89  					}
    90  					continue
    91  				}
    92  			case ir.PPARAM, ir.PPARAMOUT:
    93  			default:
    94  				continue
    95  			}
    96  			if !shouldEmitDwarfVar(n) {
    97  				continue
    98  			}
    99  			apdecls = append(apdecls, n)
   100  			if n.Type().Kind() == types.TSSA {
   101  				// Can happen for TypeInt128 types. This only happens for
   102  				// spill locations, so not a huge deal.
   103  				continue
   104  			}
   105  			fnsym.Func().RecordAutoType(reflectdata.TypeLinksym(n.Type()))
   106  		}
   107  	}
   108  
   109  	var closureVars map[*ir.Name]int64
   110  	if fn.Needctxt() {
   111  		closureVars = make(map[*ir.Name]int64)
   112  		csiter := typecheck.NewClosureStructIter(fn.ClosureVars)
   113  		for {
   114  			n, _, offset := csiter.Next()
   115  			if n == nil {
   116  				break
   117  			}
   118  			closureVars[n] = offset
   119  			if n.Heapaddr != nil {
   120  				closureVars[n.Heapaddr] = offset
   121  			}
   122  		}
   123  	}
   124  
   125  	decls, dwarfVars := createDwarfVars(fnsym, isODCLFUNC, fn, apdecls, closureVars)
   126  
   127  	// For each type referenced by the functions auto vars but not
   128  	// already referenced by a dwarf var, attach an R_USETYPE relocation to
   129  	// the function symbol to insure that the type included in DWARF
   130  	// processing during linking.
   131  	// Do the same with R_USEIFACE relocations from the function symbol for the
   132  	// same reason.
   133  	// All these R_USETYPE relocations are only looked at if the function
   134  	// survives deadcode elimination in the linker.
   135  	typesyms := []*obj.LSym{}
   136  	for t := range fnsym.Func().Autot {
   137  		typesyms = append(typesyms, t)
   138  	}
   139  	for i := range fnsym.R {
   140  		if fnsym.R[i].Type == objabi.R_USEIFACE && !strings.HasPrefix(fnsym.R[i].Sym.Name, "go:itab.") {
   141  			// Types referenced through itab will be referenced from somewhere else
   142  			typesyms = append(typesyms, fnsym.R[i].Sym)
   143  		}
   144  	}
   145  	slices.SortFunc(typesyms, func(a, b *obj.LSym) int {
   146  		return strings.Compare(a.Name, b.Name)
   147  	})
   148  	var lastsym *obj.LSym
   149  	for _, sym := range typesyms {
   150  		if sym == lastsym {
   151  			continue
   152  		}
   153  		lastsym = sym
   154  		infosym.AddRel(ctxt, obj.Reloc{Type: objabi.R_USETYPE, Sym: sym})
   155  	}
   156  	fnsym.Func().Autot = nil
   157  
   158  	var varScopes []ir.ScopeID
   159  	for _, decl := range decls {
   160  		pos := declPos(decl)
   161  		varScopes = append(varScopes, findScope(fn.Marks, pos))
   162  	}
   163  
   164  	scopes = assembleScopes(fnsym, fn, dwarfVars, varScopes)
   165  	if base.Flag.GenDwarfInl > 0 {
   166  		inlcalls = assembleInlines(fnsym, dwarfVars)
   167  	}
   168  	return scopes, inlcalls
   169  }
   170  
   171  func declPos(decl *ir.Name) src.XPos {
   172  	return decl.Canonical().Pos()
   173  }
   174  
   175  // createDwarfVars process fn, returning a list of DWARF variables and the
   176  // Nodes they represent.
   177  func createDwarfVars(fnsym *obj.LSym, complexOK bool, fn *ir.Func, apDecls []*ir.Name, closureVars map[*ir.Name]int64) ([]*ir.Name, []*dwarf.Var) {
   178  	// Collect a raw list of DWARF vars.
   179  	var vars []*dwarf.Var
   180  	var decls []*ir.Name
   181  
   182  	// Build a VarID lookup map for SSA debug info if available.
   183  	var debug *ssa.FuncDebug
   184  	var varIDMap map[*ir.Name]ssa.VarID
   185  	if fn.DebugInfo != nil {
   186  		debug = fn.DebugInfo.(*ssa.FuncDebug)
   187  		varIDMap = make(map[*ir.Name]ssa.VarID, len(debug.Vars))
   188  		for i, n := range debug.Vars {
   189  			varIDMap[n] = ssa.VarID(i)
   190  		}
   191  	}
   192  	canUseComplex := complexOK && debug != nil
   193  
   194  	// markVarSeen marks a variable and all its associated slot names as seen.
   195  	// This is needed because decomposed variables may have slots whose ir.Name
   196  	// differs from the variable itself (e.g., PAUTO vs PPARAMOUT for the same
   197  	// logical variable). Without this, the dcl loop could create duplicate
   198  	// conservative entries for names that are already covered by a complex var.
   199  	seen := make(map[*ir.Name]bool)
   200  	markVarSeen := func(n *ir.Name, varID ssa.VarID) {
   201  		seen[n] = true
   202  		if debug != nil && int(varID) < len(debug.VarSlots) {
   203  			for _, slot := range debug.VarSlots[varID] {
   204  				seen[debug.Slots[slot].N] = true
   205  			}
   206  		}
   207  	}
   208  
   209  	// Unified loop: for each variable in apDecls, try createComplexVar
   210  	// (SSA debug info) first, then fall back to createSimpleVar.
   211  	for _, n := range apDecls {
   212  		if !shouldEmitDwarfVar(n) {
   213  			continue
   214  		}
   215  		if canUseComplex {
   216  			if vid, ok := varIDMap[n]; ok {
   217  				if dvar := createComplexVar(fnsym, fn, vid, closureVars); dvar != nil {
   218  					decls = append(decls, n)
   219  					vars = append(vars, dvar)
   220  					markVarSeen(n, vid)
   221  					continue
   222  				}
   223  			}
   224  		}
   225  		seen[n] = true
   226  		decls = append(decls, n)
   227  		vars = append(vars, createSimpleVar(fnsym, n, closureVars))
   228  	}
   229  
   230  	// Add SSA-tracked vars not in apDecls.
   231  	if canUseComplex {
   232  		for i, n := range debug.Vars {
   233  			if seen[n] {
   234  				continue
   235  			}
   236  			if !shouldEmitDwarfVar(n) {
   237  				continue
   238  			}
   239  			if dvar := createComplexVar(fnsym, fn, ssa.VarID(i), closureVars); dvar != nil {
   240  				decls = append(decls, n)
   241  				vars = append(vars, dvar)
   242  				markVarSeen(n, ssa.VarID(i))
   243  			}
   244  		}
   245  	}
   246  
   247  	// Recover zero-sized variables eliminated by the stackframe pass.
   248  	if debug != nil {
   249  		for _, n := range debug.OptDcl {
   250  			if seen[n] {
   251  				continue
   252  			}
   253  			if n.Class != ir.PAUTO {
   254  				continue
   255  			}
   256  			types.CalcSize(n.Type())
   257  			if n.Type().Size() == 0 {
   258  				decls = append(decls, n)
   259  				vars = append(vars, createSimpleVar(fnsym, n, closureVars))
   260  				vars[len(vars)-1].StackOffset = 0
   261  				fnsym.Func().RecordAutoType(reflectdata.TypeLinksym(n.Type()))
   262  				seen[n] = true
   263  			}
   264  		}
   265  	}
   266  
   267  	// For inlined functions or functions with register output params,
   268  	// collect additional declarations that may not be in apDecls.
   269  	dcl := apDecls
   270  	if fnsym.WasInlined() {
   271  		dcl = preInliningDcls(fnsym)
   272  	} else if debug != nil {
   273  		// The backend's stackframe pass prunes away entries from the
   274  		// fn's Dcl list, including PARAMOUT nodes that correspond to
   275  		// output params passed in registers. Add back in these
   276  		// entries here so that we can process them properly during
   277  		// DWARF-gen. See issue 48573 for more details.
   278  		for _, n := range debug.RegOutputParams {
   279  			if !ssa.IsVarWantedForDebug(n) {
   280  				continue
   281  			}
   282  			if n.Class != ir.PPARAMOUT || !n.IsOutputParamInRegisters() {
   283  				base.Fatalf("invalid ir.Name on debugInfo.RegOutputParams list")
   284  			}
   285  			dcl = append(dcl, n)
   286  		}
   287  	}
   288  
   289  	// Process remaining variables not yet handled. For each variable,
   290  	// try createComplexVar first, then fall back to createSimpleVar
   291  	// for non-SSA-able params, or createConservativeVar for the rest.
   292  	for _, n := range dcl {
   293  		if seen[n] {
   294  			continue
   295  		}
   296  		if !shouldEmitDwarfVar(n) {
   297  			continue
   298  		}
   299  		seen[n] = true
   300  		if canUseComplex {
   301  			if vid, ok := varIDMap[n]; ok {
   302  				if dvar := createComplexVar(fnsym, fn, vid, closureVars); dvar != nil {
   303  					decls = append(decls, n)
   304  					vars = append(vars, dvar)
   305  					continue
   306  				}
   307  			}
   308  		}
   309  		if n.Class == ir.PPARAM && !ssa.CanSSA(n.Type()) {
   310  			decls = append(decls, n)
   311  			vars = append(vars, createSimpleVar(fnsym, n, closureVars))
   312  			continue
   313  		}
   314  		decls = append(decls, n)
   315  		vars = append(vars, createConservativeVar(fnsym, fn, n, closureVars))
   316  	}
   317  
   318  	// Sort decls and vars.
   319  	sortDeclsAndVars(fn, decls, vars)
   320  
   321  	return decls, vars
   322  }
   323  
   324  // createConservativeVar creates a DWARF variable with a conservative location
   325  // description. This is used for variables that were optimized away or otherwise
   326  // don't have precise location info. The intent is to communicate that "yes,
   327  // there is a variable named X in this function, but no, I don't have enough
   328  // information to reliably report its contents."
   329  // For heap-escaped variables, a location list is created that describes
   330  // dereferencing the pointer at the stack offset.
   331  func createConservativeVar(fnsym *obj.LSym, fn *ir.Func, n *ir.Name, closureVars map[*ir.Name]int64) *dwarf.Var {
   332  	typename := dwarf.InfoPrefix + types.TypeSymName(n.Type())
   333  	tag := dwarf.DW_TAG_variable
   334  	isReturnValue := (n.Class == ir.PPARAMOUT)
   335  	if n.Class == ir.PPARAM || n.Class == ir.PPARAMOUT {
   336  		tag = dwarf.DW_TAG_formal_parameter
   337  	}
   338  	inlIndex := 0
   339  	if base.Flag.GenDwarfInl > 1 {
   340  		if n.InlFormal() || n.InlLocal() {
   341  			inlIndex = posInlIndex(n.Pos()) + 1
   342  			if n.InlFormal() {
   343  				tag = dwarf.DW_TAG_formal_parameter
   344  			}
   345  		}
   346  	}
   347  	declpos := base.Ctxt.InnermostPos(n.Pos())
   348  	dvar := &dwarf.Var{
   349  		Name:          n.Sym().Name,
   350  		IsReturnValue: isReturnValue,
   351  		Tag:           tag,
   352  		WithLoclist:   true,
   353  		StackOffset:   int32(n.FrameOffset()),
   354  		Type:          base.Ctxt.Lookup(typename),
   355  		DeclFile:      declpos.RelFilename(),
   356  		DeclLine:      declpos.RelLine(),
   357  		DeclCol:       declpos.RelCol(),
   358  		InlIndex:      int32(inlIndex),
   359  		ChildIndex:    -1,
   360  		DictIndex:     n.DictIndex,
   361  		ClosureOffset: closureOffset(n, closureVars),
   362  	}
   363  	if n.Esc() == ir.EscHeap && n.Heapaddr != nil {
   364  		// The variable was promoted to the heap and has a known heap
   365  		// address, so describe its location by dereferencing the pointer
   366  		// stored at its stack offset. A heap-escaped variable may have no
   367  		// Heapaddr if it was declared in unreachable code: escape analysis
   368  		// marks it as heap-allocated, but SSA generation skips the dead
   369  		// declaration and never allocates the address. In that case fall
   370  		// through and emit a conservative variable with no location list.
   371  		debug := fn.DebugInfo.(*ssa.FuncDebug)
   372  		list := createHeapDerefLocationList(n, debug.EntryID)
   373  		dvar.PutLocationList = func(listSym, startPC dwarf.Sym) {
   374  			debug.PutLocationList(list, base.Ctxt, listSym.(*obj.LSym), startPC.(*obj.LSym))
   375  		}
   376  	}
   377  	// Record go type to ensure that it gets emitted by the linker.
   378  	fnsym.Func().RecordAutoType(reflectdata.TypeLinksym(n.Type()))
   379  	return dvar
   380  }
   381  
   382  // sortDeclsAndVars sorts the decl and dwarf var lists according to
   383  // parameter declaration order, so as to insure that when a subprogram
   384  // DIE is emitted, its parameter children appear in declaration order.
   385  // Prior to the advent of the register ABI, sorting by frame offset
   386  // would achieve this; with the register we now need to go back to the
   387  // original function signature.
   388  func sortDeclsAndVars(fn *ir.Func, decls []*ir.Name, vars []*dwarf.Var) {
   389  	paramOrder := make(map[*ir.Name]int)
   390  	idx := 1
   391  	for _, f := range fn.Type().RecvParamsResults() {
   392  		if n, ok := f.Nname.(*ir.Name); ok {
   393  			paramOrder[n] = idx
   394  			idx++
   395  		}
   396  	}
   397  	sort.Stable(varsAndDecls{decls, vars, paramOrder})
   398  }
   399  
   400  type varsAndDecls struct {
   401  	decls      []*ir.Name
   402  	vars       []*dwarf.Var
   403  	paramOrder map[*ir.Name]int
   404  }
   405  
   406  func (v varsAndDecls) Len() int {
   407  	return len(v.decls)
   408  }
   409  
   410  func (v varsAndDecls) Less(i, j int) bool {
   411  	nameLT := func(ni, nj *ir.Name) bool {
   412  		oi, foundi := v.paramOrder[ni]
   413  		oj, foundj := v.paramOrder[nj]
   414  		if foundi {
   415  			if foundj {
   416  				return oi < oj
   417  			} else {
   418  				return true
   419  			}
   420  		}
   421  		return false
   422  	}
   423  	return nameLT(v.decls[i], v.decls[j])
   424  }
   425  
   426  func (v varsAndDecls) Swap(i, j int) {
   427  	v.vars[i], v.vars[j] = v.vars[j], v.vars[i]
   428  	v.decls[i], v.decls[j] = v.decls[j], v.decls[i]
   429  }
   430  
   431  // Given a function that was inlined at some point during the
   432  // compilation, return a sorted list of nodes corresponding to the
   433  // autos/locals in that function prior to inlining. If this is a
   434  // function that is not local to the package being compiled, then the
   435  // names of the variables may have been "versioned" to avoid conflicts
   436  // with local vars; disregard this versioning when sorting.
   437  func preInliningDcls(fnsym *obj.LSym) []*ir.Name {
   438  	fn := base.Ctxt.DwFixups.GetPrecursorFunc(fnsym).(*ir.Func)
   439  	var rdcl []*ir.Name
   440  	for _, n := range fn.Inl.Dcl {
   441  		if n.Sym().Name[0] == '.' || !shouldEmitDwarfVarSafe(n) {
   442  			continue
   443  		}
   444  		rdcl = append(rdcl, n)
   445  	}
   446  	return rdcl
   447  }
   448  
   449  func createSimpleVar(fnsym *obj.LSym, n *ir.Name, closureVars map[*ir.Name]int64) *dwarf.Var {
   450  	var tag int
   451  	var offs int64
   452  
   453  	localAutoOffset := func() int64 {
   454  		offs = n.FrameOffset()
   455  		if base.Ctxt.Arch.FixedFrameSize == 0 {
   456  			offs -= int64(types.PtrSize)
   457  		}
   458  		if buildcfg.FramePointerEnabled {
   459  			offs -= int64(types.PtrSize)
   460  		}
   461  		return offs
   462  	}
   463  
   464  	switch n.Class {
   465  	case ir.PAUTO:
   466  		offs = localAutoOffset()
   467  		tag = dwarf.DW_TAG_variable
   468  	case ir.PPARAM, ir.PPARAMOUT:
   469  		tag = dwarf.DW_TAG_formal_parameter
   470  		if n.IsOutputParamInRegisters() {
   471  			offs = localAutoOffset()
   472  		} else {
   473  			offs = n.FrameOffset() + base.Ctxt.Arch.FixedFrameSize
   474  		}
   475  
   476  	default:
   477  		base.Fatalf("createSimpleVar unexpected class %v for node %v", n.Class, n)
   478  	}
   479  
   480  	typename := dwarf.InfoPrefix + types.TypeSymName(n.Type())
   481  	delete(fnsym.Func().Autot, reflectdata.TypeLinksym(n.Type()))
   482  	inlIndex := 0
   483  	if base.Flag.GenDwarfInl > 1 {
   484  		if n.InlFormal() || n.InlLocal() {
   485  			inlIndex = posInlIndex(n.Pos()) + 1
   486  			if n.InlFormal() {
   487  				tag = dwarf.DW_TAG_formal_parameter
   488  			}
   489  		}
   490  	}
   491  	declpos := base.Ctxt.InnermostPos(declPos(n))
   492  	return &dwarf.Var{
   493  		Name:          n.Sym().Name,
   494  		IsReturnValue: n.Class == ir.PPARAMOUT,
   495  		IsInlFormal:   n.InlFormal(),
   496  		Tag:           tag,
   497  		StackOffset:   int32(offs),
   498  		Type:          base.Ctxt.Lookup(typename),
   499  		DeclFile:      declpos.RelFilename(),
   500  		DeclLine:      declpos.RelLine(),
   501  		DeclCol:       declpos.RelCol(),
   502  		InlIndex:      int32(inlIndex),
   503  		ChildIndex:    -1,
   504  		DictIndex:     n.DictIndex,
   505  		ClosureOffset: closureOffset(n, closureVars),
   506  	}
   507  }
   508  
   509  // createComplexVar builds a single DWARF variable entry and location list.
   510  func createComplexVar(fnsym *obj.LSym, fn *ir.Func, varID ssa.VarID, closureVars map[*ir.Name]int64) *dwarf.Var {
   511  	debug := fn.DebugInfo.(*ssa.FuncDebug)
   512  	n := debug.Vars[varID]
   513  
   514  	var tag int
   515  	switch n.Class {
   516  	case ir.PAUTO:
   517  		tag = dwarf.DW_TAG_variable
   518  	case ir.PPARAM, ir.PPARAMOUT:
   519  		tag = dwarf.DW_TAG_formal_parameter
   520  	default:
   521  		return nil
   522  	}
   523  
   524  	gotype := reflectdata.TypeLinksym(n.Type())
   525  	delete(fnsym.Func().Autot, gotype)
   526  	typename := dwarf.InfoPrefix + gotype.Name[len("type:"):]
   527  	inlIndex := 0
   528  	if base.Flag.GenDwarfInl > 1 {
   529  		if n.InlFormal() || n.InlLocal() {
   530  			inlIndex = posInlIndex(n.Pos()) + 1
   531  			if n.InlFormal() {
   532  				tag = dwarf.DW_TAG_formal_parameter
   533  			}
   534  		}
   535  	}
   536  	declpos := base.Ctxt.InnermostPos(n.Pos())
   537  	dvar := &dwarf.Var{
   538  		Name:          n.Sym().Name,
   539  		IsReturnValue: n.Class == ir.PPARAMOUT,
   540  		IsInlFormal:   n.InlFormal(),
   541  		Tag:           tag,
   542  		WithLoclist:   true,
   543  		Type:          base.Ctxt.Lookup(typename),
   544  		// The stack offset is used as a sorting key, so for decomposed
   545  		// variables just give it the first one. It's not used otherwise.
   546  		// This won't work well if the first slot hasn't been assigned a stack
   547  		// location, but it's not obvious how to do better.
   548  		StackOffset:   ssagen.StackOffset(debug.Slots[debug.VarSlots[varID][0]]),
   549  		DeclFile:      declpos.RelFilename(),
   550  		DeclLine:      declpos.RelLine(),
   551  		DeclCol:       declpos.RelCol(),
   552  		InlIndex:      int32(inlIndex),
   553  		ChildIndex:    -1,
   554  		DictIndex:     n.DictIndex,
   555  		ClosureOffset: closureOffset(n, closureVars),
   556  	}
   557  	list := debug.LocationLists[varID]
   558  	if len(list) != 0 {
   559  		dvar.PutLocationList = func(listSym, startPC dwarf.Sym) {
   560  			debug.PutLocationList(list, base.Ctxt, listSym.(*obj.LSym), startPC.(*obj.LSym))
   561  		}
   562  	}
   563  	return dvar
   564  }
   565  
   566  // createHeapDerefLocationList creates a location list for a heap-escaped variable
   567  // that describes "dereference pointer at stack offset"
   568  func createHeapDerefLocationList(n *ir.Name, entryID ssa.ID) []ssa.LocListEntry {
   569  	// Get the stack offset where the heap pointer is stored
   570  	heapPtrOffset := n.Heapaddr.FrameOffset()
   571  	if base.Ctxt.Arch.FixedFrameSize == 0 {
   572  		heapPtrOffset -= int64(types.PtrSize)
   573  	}
   574  	if buildcfg.FramePointerEnabled {
   575  		heapPtrOffset -= int64(types.PtrSize)
   576  	}
   577  
   578  	// Create a location expression: DW_OP_fbreg <offset> DW_OP_deref
   579  	var expr []byte
   580  	expr = append(expr, dwarf.DW_OP_fbreg)
   581  	expr = dwarf.AppendSleb128(expr, heapPtrOffset)
   582  	expr = append(expr, dwarf.DW_OP_deref)
   583  
   584  	return []ssa.LocListEntry{{
   585  		StartBlock: entryID,
   586  		StartValue: ssa.BlockStart.ID,
   587  		EndBlock:   entryID,
   588  		EndValue:   ssa.FuncEnd.ID,
   589  		Expr:       expr,
   590  	}}
   591  }
   592  
   593  // RecordFlags records the specified command-line flags to be placed
   594  // in the DWARF info.
   595  func RecordFlags(flags ...string) {
   596  	if base.Ctxt.Pkgpath == "" {
   597  		base.Fatalf("missing pkgpath")
   598  	}
   599  
   600  	type BoolFlag interface {
   601  		IsBoolFlag() bool
   602  	}
   603  	type CountFlag interface {
   604  		IsCountFlag() bool
   605  	}
   606  	var cmd bytes.Buffer
   607  	for _, name := range flags {
   608  		f := flag.Lookup(name)
   609  		if f == nil {
   610  			continue
   611  		}
   612  		getter := f.Value.(flag.Getter)
   613  		if getter.String() == f.DefValue {
   614  			// Flag has default value, so omit it.
   615  			continue
   616  		}
   617  		if bf, ok := f.Value.(BoolFlag); ok && bf.IsBoolFlag() {
   618  			val, ok := getter.Get().(bool)
   619  			if ok && val {
   620  				fmt.Fprintf(&cmd, " -%s", f.Name)
   621  				continue
   622  			}
   623  		}
   624  		if cf, ok := f.Value.(CountFlag); ok && cf.IsCountFlag() {
   625  			val, ok := getter.Get().(int)
   626  			if ok && val == 1 {
   627  				fmt.Fprintf(&cmd, " -%s", f.Name)
   628  				continue
   629  			}
   630  		}
   631  		fmt.Fprintf(&cmd, " -%s=%v", f.Name, getter.Get())
   632  	}
   633  
   634  	// Adds flag to producer string signaling whether regabi is turned on or
   635  	// off.
   636  	// Once regabi is turned on across the board and the relative GOEXPERIMENT
   637  	// knobs no longer exist this code should be removed.
   638  	if buildcfg.Experiment.RegabiArgs {
   639  		cmd.Write([]byte(" regabi"))
   640  	}
   641  
   642  	if cmd.Len() == 0 {
   643  		return
   644  	}
   645  	s := base.Ctxt.Lookup(dwarf.CUInfoPrefix + "producer." + base.Ctxt.Pkgpath)
   646  	s.Type = objabi.SDWARFCUINFO
   647  	// Sometimes (for example when building tests) we can link
   648  	// together two package main archives. So allow dups.
   649  	s.Set(obj.AttrDuplicateOK, true)
   650  	base.Ctxt.Data = append(base.Ctxt.Data, s)
   651  	s.P = cmd.Bytes()[1:]
   652  }
   653  
   654  // RecordPackageName records the name of the package being
   655  // compiled, so that the linker can save it in the compile unit's DIE.
   656  func RecordPackageName() {
   657  	s := base.Ctxt.Lookup(dwarf.CUInfoPrefix + "packagename." + base.Ctxt.Pkgpath)
   658  	s.Type = objabi.SDWARFCUINFO
   659  	// Sometimes (for example when building tests) we can link
   660  	// together two package main archives. So allow dups.
   661  	s.Set(obj.AttrDuplicateOK, true)
   662  	base.Ctxt.Data = append(base.Ctxt.Data, s)
   663  	s.P = []byte(types.LocalPkg.Name)
   664  }
   665  
   666  // shouldEmitDwarfVar reports whether n should have a DWARF variable entry.
   667  // This consolidates filtering that was previously spread across IR (AutoTemp),
   668  // SSA (IsVarWantedForDebug), and dwarfgen (symbol name checks).
   669  func shouldEmitDwarfVar(n *ir.Name) bool {
   670  	if ir.IsAutoTmp(n) {
   671  		return false
   672  	}
   673  	return shouldEmitDwarfVarSafe(n)
   674  }
   675  
   676  // shouldEmitDwarfVarSafe is like shouldEmitDwarfVar but omits the ir.IsAutoTmp
   677  // check, making it safe to call during parallel compilation on shared ir.Name
   678  // nodes (e.g., in preInliningDcls). ir.IsAutoTmp reads the mutable flags bitset,
   679  // which can race with other goroutines writing different flags during compilation.
   680  // Auto temps have names starting with "." so callers must filter those separately.
   681  func shouldEmitDwarfVarSafe(n *ir.Name) bool {
   682  	if !ssa.IsVarWantedForDebug(n) {
   683  		return false
   684  	}
   685  	if n.Sym().Name == "_" {
   686  		return false
   687  	}
   688  	if n.Type().IsUntyped() {
   689  		return false
   690  	}
   691  	return true
   692  }
   693  
   694  func closureOffset(n *ir.Name, closureVars map[*ir.Name]int64) int64 {
   695  	return closureVars[n]
   696  }
   697  

View as plain text