mirror of
				https://github.com/go-delve/delve.git
				synced 2025-10-31 18:57:18 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			104 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			104 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package frame
 | |
| 
 | |
| import (
 | |
| 	"bytes"
 | |
| 	"debug/elf"
 | |
| 	"os"
 | |
| 	"path/filepath"
 | |
| 	"testing"
 | |
| )
 | |
| 
 | |
| func grabDebugFrameSection(fp string, t *testing.T) []byte {
 | |
| 	p, err := filepath.Abs(fp)
 | |
| 	if err != nil {
 | |
| 		t.Fatal(err)
 | |
| 	}
 | |
| 
 | |
| 	f, err := os.Open(p)
 | |
| 	if err != nil {
 | |
| 		t.Fatal(err)
 | |
| 	}
 | |
| 
 | |
| 	ef, err := elf.NewFile(f)
 | |
| 	if err != nil {
 | |
| 		t.Fatal(err)
 | |
| 	}
 | |
| 
 | |
| 	data, err := ef.Section(".debug_frame").Data()
 | |
| 	if err != nil {
 | |
| 		t.Fatal(err)
 | |
| 	}
 | |
| 
 | |
| 	return data
 | |
| }
 | |
| 
 | |
| func TestDecodeULEB128(t *testing.T) {
 | |
| 	var leb128 = bytes.NewBuffer([]byte{0xE5, 0x8E, 0x26})
 | |
| 
 | |
| 	n, c := decodeULEB128(leb128)
 | |
| 	if n != 624485 {
 | |
| 		t.Fatal("Number was not decoded properly, got: ", n, c)
 | |
| 	}
 | |
| }
 | |
| 
 | |
| func TestDecodeSLEB128(t *testing.T) {
 | |
| 	sleb128 := bytes.NewBuffer([]byte{0x9b, 0xf1, 0x59})
 | |
| 
 | |
| 	n, c := decodeSLEB128(sleb128)
 | |
| 	if n != -624485 {
 | |
| 		t.Fatal("Number was not decoded properly, got: ", n, c)
 | |
| 	}
 | |
| }
 | |
| 
 | |
| func TestParseString(t *testing.T) {
 | |
| 	bstr := bytes.NewBuffer([]byte{'h', 'i', 0x0, 0xFF, 0xCC})
 | |
| 	str, _ := parseString(bstr)
 | |
| 
 | |
| 	if str != "hi" {
 | |
| 		t.Fatal("String was not parsed correctly")
 | |
| 	}
 | |
| }
 | |
| 
 | |
| func TestParse(t *testing.T) {
 | |
| 	data := grabDebugFrameSection("../_fixtures/testprog", t)
 | |
| 	ce := Parse(data)[0]
 | |
| 
 | |
| 	if ce.Length != 16 {
 | |
| 		t.Fatal("Length was not parsed correctly")
 | |
| 	}
 | |
| 
 | |
| 	if ce.CIE_id != 0xffffffff {
 | |
| 		t.Fatal("CIE id was not parsed correctly")
 | |
| 	}
 | |
| 
 | |
| 	if ce.Version != 0x3 {
 | |
| 		t.Fatalf("Version was not parsed correctly expected %#v got %#v, data was %#v", 0x3, ce.Version, data[0:40])
 | |
| 	}
 | |
| 
 | |
| 	if ce.Augmentation != "" {
 | |
| 		t.Fatal("Augmentation was not parsed correctly")
 | |
| 	}
 | |
| 
 | |
| 	if ce.CodeAlignmentFactor != 0x1 {
 | |
| 		t.Fatal("Code Alignment Factor was not parsed correctly")
 | |
| 	}
 | |
| 
 | |
| 	if ce.DataAlignmentFactor != -4 {
 | |
| 		t.Fatalf("Data Alignment Factor was not parsed correctly got %#v", ce.DataAlignmentFactor)
 | |
| 	}
 | |
| 
 | |
| 	fe := ce.FrameDescriptorEntries[0]
 | |
| 
 | |
| 	if fe.Length != 32 {
 | |
| 		t.Fatal("Length was not parsed correctly")
 | |
| 	}
 | |
| 
 | |
| 	if fe.CIE_pointer != ce {
 | |
| 		t.Fatal("Frame entry does not point to parent CIE")
 | |
| 	}
 | |
| 
 | |
| 	if fe.InitialLocation != 0x400c00 {
 | |
| 		t.Fatalf("Initial location not parsed correctly, got %#v", fe.InitialLocation)
 | |
| 	}
 | |
| }
 | 
