github.com/undoio/delve@v1.9.0/pkg/dwarf/line/state_machine_test.go (about)

     1  package line
     2  
     3  import (
     4  	"bytes"
     5  	"compress/gzip"
     6  	"debug/dwarf"
     7  	"debug/macho"
     8  	"encoding/binary"
     9  	"fmt"
    10  	"io"
    11  	"io/ioutil"
    12  	"os"
    13  	"runtime"
    14  	"testing"
    15  
    16  	"github.com/undoio/delve/pkg/dwarf/util"
    17  )
    18  
    19  func slurpGzip(path string) ([]byte, error) {
    20  	fh, err := os.Open(path)
    21  	if err != nil {
    22  		return nil, err
    23  	}
    24  	defer fh.Close()
    25  	gzin, err := gzip.NewReader(fh)
    26  	if err != nil {
    27  		return nil, err
    28  	}
    29  	defer gzin.Close()
    30  	return ioutil.ReadAll(gzin)
    31  }
    32  
    33  func TestGrafana(t *testing.T) {
    34  	// Compares a full execution of our state machine on the debug_line section
    35  	// of grafana to the output generated using debug/dwarf.LineReader on the
    36  	// same section.
    37  
    38  	if runtime.GOOS == "windows" {
    39  		t.Skip("filepath.Join ruins this test on windows")
    40  	}
    41  	debugBytes, err := slurpGzip("_testdata/debug.grafana.debug.gz")
    42  	if err != nil {
    43  		t.Fatal(err)
    44  	}
    45  	exe, err := macho.NewFile(bytes.NewReader(debugBytes))
    46  	if err != nil {
    47  		t.Fatal(err)
    48  	}
    49  
    50  	sec := exe.Section("__debug_line")
    51  	debugLineBytes, err := sec.Data()
    52  	if err != nil {
    53  		t.Fatal(err)
    54  	}
    55  
    56  	data, err := exe.DWARF()
    57  	if err != nil {
    58  		t.Fatal(err)
    59  	}
    60  
    61  	debugLineBuffer := bytes.NewBuffer(debugLineBytes)
    62  	rdr := data.Reader()
    63  	for {
    64  		e, err := rdr.Next()
    65  		if err != nil {
    66  			t.Fatal(err)
    67  		}
    68  		if e == nil {
    69  			break
    70  		}
    71  		rdr.SkipChildren()
    72  		if e.Tag != dwarf.TagCompileUnit {
    73  			continue
    74  		}
    75  		cuname, _ := e.Val(dwarf.AttrName).(string)
    76  
    77  		lineInfo := Parse(e.Val(dwarf.AttrCompDir).(string), debugLineBuffer, nil, t.Logf, 0, false, 8)
    78  		lineInfo.endSeqIsValid = true
    79  		sm := newStateMachine(lineInfo, lineInfo.Instructions, 8)
    80  
    81  		lnrdr, err := data.LineReader(e)
    82  		if err != nil {
    83  			t.Fatal(err)
    84  		}
    85  
    86  		checkCompileUnit(t, cuname, lnrdr, sm)
    87  	}
    88  }
    89  
    90  func checkCompileUnit(t *testing.T, cuname string, lnrdr *dwarf.LineReader, sm *StateMachine) {
    91  	var lne dwarf.LineEntry
    92  	for {
    93  		if err := sm.next(); err != nil {
    94  			if err != io.EOF {
    95  				t.Fatalf("state machine next error: %v", err)
    96  			}
    97  			break
    98  		}
    99  		if !sm.valid {
   100  			continue
   101  		}
   102  
   103  		err := lnrdr.Next(&lne)
   104  		if err == io.EOF {
   105  			t.Fatalf("line reader ended before our state machine for compile unit %s", cuname)
   106  		}
   107  		if err != nil {
   108  			t.Fatal(err)
   109  		}
   110  
   111  		tgt := fmt.Sprintf("%#x %s:%d isstmt:%v prologue_end:%v epilogue_begin:%v", lne.Address, lne.File.Name, lne.Line, lne.IsStmt, lne.PrologueEnd, lne.EpilogueBegin)
   112  
   113  		out := fmt.Sprintf("%#x %s:%d isstmt:%v prologue_end:%v epilogue_begin:%v", sm.address, sm.file, sm.line, sm.isStmt, sm.prologueEnd, sm.epilogueBegin)
   114  		if out != tgt {
   115  			t.Errorf("mismatch:\n")
   116  			t.Errorf("got:\t%s\n", out)
   117  			t.Errorf("expected:\t%s\n", tgt)
   118  			t.Fatal("previous error")
   119  		}
   120  	}
   121  
   122  	err := lnrdr.Next(&lne)
   123  	if err != io.EOF {
   124  		t.Fatalf("state machine ended before the line reader for compile unit %s", cuname)
   125  	}
   126  }
   127  
   128  func TestMultipleSequences(t *testing.T) {
   129  	// Check that our state machine (specifically PCToLine and AllPCsBetween)
   130  	// are correct when dealing with units containing more than one sequence.
   131  
   132  	const thefile = "thefile.go"
   133  
   134  	instr := bytes.NewBuffer(nil)
   135  	ptrSize := ptrSizeByRuntimeArch()
   136  
   137  	write_DW_LNE_set_address := func(addr uint64) {
   138  		instr.WriteByte(0)
   139  		util.EncodeULEB128(instr, 9) // 1 + ptr_size
   140  		instr.WriteByte(DW_LINE_set_address)
   141  		util.WriteUint(instr, binary.LittleEndian, ptrSize, addr)
   142  	}
   143  
   144  	write_DW_LNS_copy := func() {
   145  		instr.WriteByte(DW_LNS_copy)
   146  	}
   147  
   148  	write_DW_LNS_advance_pc := func(off uint64) {
   149  		instr.WriteByte(DW_LNS_advance_pc)
   150  		util.EncodeULEB128(instr, off)
   151  	}
   152  
   153  	write_DW_LNS_advance_line := func(off int64) {
   154  		instr.WriteByte(DW_LNS_advance_line)
   155  		util.EncodeSLEB128(instr, off)
   156  	}
   157  
   158  	write_DW_LNE_end_sequence := func() {
   159  		instr.WriteByte(0)
   160  		util.EncodeULEB128(instr, 1)
   161  		instr.WriteByte(DW_LINE_end_sequence)
   162  	}
   163  
   164  	write_DW_LNE_set_address(0x400000)
   165  	write_DW_LNS_copy() // thefile.go:1 0x400000
   166  	write_DW_LNS_advance_pc(0x2)
   167  	write_DW_LNS_advance_line(1)
   168  	write_DW_LNS_copy() // thefile.go:2 0x400002
   169  	write_DW_LNS_advance_pc(0x2)
   170  	write_DW_LNS_advance_line(1)
   171  	write_DW_LNS_copy() // thefile.go:3 0x400004
   172  	write_DW_LNS_advance_pc(0x2)
   173  	write_DW_LNE_end_sequence() // thefile.go:3 ends the byte before 0x400006
   174  
   175  	write_DW_LNE_set_address(0x600000)
   176  	write_DW_LNS_advance_line(10)
   177  	write_DW_LNS_copy() // thefile.go:11 0x600000
   178  	write_DW_LNS_advance_pc(0x2)
   179  	write_DW_LNS_advance_line(1)
   180  	write_DW_LNS_copy() // thefile.go:12 0x600002
   181  	write_DW_LNS_advance_pc(0x2)
   182  	write_DW_LNS_advance_line(1)
   183  	write_DW_LNS_copy() // thefile.go:13 0x600004
   184  	write_DW_LNS_advance_pc(0x2)
   185  	write_DW_LNE_end_sequence() // thefile.go:13 ends the byte before 0x600006
   186  
   187  	write_DW_LNE_set_address(0x500000)
   188  	write_DW_LNS_advance_line(20)
   189  	write_DW_LNS_copy() // thefile.go:21 0x500000
   190  	write_DW_LNS_advance_pc(0x2)
   191  	write_DW_LNS_advance_line(1)
   192  	write_DW_LNS_copy() // thefile.go:22 0x500002
   193  	write_DW_LNS_advance_pc(0x2)
   194  	write_DW_LNS_advance_line(1)
   195  	write_DW_LNS_copy() // thefile.go:23 0x500004
   196  	write_DW_LNS_advance_pc(0x2)
   197  	write_DW_LNE_end_sequence() // thefile.go:23 ends the byte before 0x500006
   198  
   199  	lines := &DebugLineInfo{
   200  		Prologue: &DebugLinePrologue{
   201  			UnitLength:     1,
   202  			Version:        2,
   203  			MinInstrLength: 1,
   204  			InitialIsStmt:  1,
   205  			LineBase:       -3,
   206  			LineRange:      12,
   207  			OpcodeBase:     13,
   208  			StdOpLengths:   []uint8{0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1},
   209  		},
   210  		IncludeDirs:  []string{},
   211  		FileNames:    []*FileEntry{&FileEntry{Path: thefile}},
   212  		Instructions: instr.Bytes(),
   213  		ptrSize:      ptrSize,
   214  	}
   215  
   216  	// Test that PCToLine is correct for all three sequences
   217  
   218  	for _, testCase := range []struct {
   219  		pc   uint64
   220  		line int
   221  	}{
   222  		{0x400000, 1},
   223  		{0x400002, 2},
   224  		{0x400004, 3},
   225  
   226  		{0x500000, 21},
   227  		{0x500002, 22},
   228  		{0x500004, 23},
   229  
   230  		{0x600000, 11},
   231  		{0x600002, 12},
   232  		{0x600004, 13},
   233  	} {
   234  		sm := newStateMachine(lines, lines.Instructions, lines.ptrSize)
   235  		file, curline, ok := sm.PCToLine(testCase.pc)
   236  		if !ok {
   237  			t.Fatalf("Could not find %#x", testCase.pc)
   238  		}
   239  		if file != thefile {
   240  			t.Fatalf("Wrong file returned for %#x %q", testCase.pc, file)
   241  		}
   242  		if curline != testCase.line {
   243  			t.Errorf("Wrong line returned for %#x: got %d expected %d", testCase.pc, curline, testCase.line)
   244  		}
   245  
   246  	}
   247  
   248  	// Test that AllPCsBetween is correct for all three sequences
   249  	for _, testCase := range []struct {
   250  		start, end uint64
   251  		tgt        []uint64
   252  	}{
   253  		{0x400000, 0x400005, []uint64{0x400000, 0x400002, 0x400004}},
   254  		{0x500000, 0x500005, []uint64{0x500000, 0x500002, 0x500004}},
   255  		{0x600000, 0x600005, []uint64{0x600000, 0x600002, 0x600004}},
   256  	} {
   257  		out, err := lines.AllPCsBetween(testCase.start, testCase.end, "", -1)
   258  		if err != nil {
   259  			t.Fatalf("AllPCsBetween(%#x, %#x): %v", testCase.start, testCase.end, err)
   260  		}
   261  
   262  		if len(out) != len(testCase.tgt) {
   263  			t.Errorf("AllPCsBetween(%#x, %#x): expected: %#x got: %#x", testCase.start, testCase.end, testCase.tgt, out)
   264  		}
   265  	}
   266  }