mirror of
				https://github.com/go-delve/delve.git
				synced 2025-10-31 02:36:18 +08:00 
			
		
		
		
	Read string type from memory
This commit is contained in:
		| @ -12,6 +12,7 @@ import ( | |||||||
| 	"os" | 	"os" | ||||||
| 	"strconv" | 	"strconv" | ||||||
| 	"syscall" | 	"syscall" | ||||||
|  | 	"unsafe" | ||||||
|  |  | ||||||
| 	"github.com/derekparker/dbg/dwarf/frame" | 	"github.com/derekparker/dbg/dwarf/frame" | ||||||
| 	"github.com/derekparker/dbg/dwarf/line" | 	"github.com/derekparker/dbg/dwarf/line" | ||||||
| @ -376,6 +377,8 @@ func (dbp *DebuggedProcess) extractValue(instructions []byte, typ interface{}) ( | |||||||
| 	offset := uintptr(int64(regs.Rsp) + off) | 	offset := uintptr(int64(regs.Rsp) + off) | ||||||
|  |  | ||||||
| 	switch typ.(type) { | 	switch typ.(type) { | ||||||
|  | 	case *dwarf.StructType: | ||||||
|  | 		return dbp.readString(offset) | ||||||
| 	case *dwarf.IntType: | 	case *dwarf.IntType: | ||||||
| 		return dbp.readInt(offset) | 		return dbp.readInt(offset) | ||||||
| 	case *dwarf.FloatType: | 	case *dwarf.FloatType: | ||||||
| @ -385,6 +388,25 @@ func (dbp *DebuggedProcess) extractValue(instructions []byte, typ interface{}) ( | |||||||
| 	return "", fmt.Errorf("could not find value for type %s", typ) | 	return "", fmt.Errorf("could not find value for type %s", typ) | ||||||
| } | } | ||||||
|  |  | ||||||
|  | func (dbp *DebuggedProcess) readString(addr uintptr) (string, error) { | ||||||
|  | 	val, err := dbp.readMemory(addr, 8) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return "", err | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// deref the pointer to the string | ||||||
|  | 	addr = uintptr(binary.LittleEndian.Uint64(val)) | ||||||
|  | 	val, err = dbp.readMemory(addr, 16) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return "", err | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	i := bytes.IndexByte(val, 0x0) | ||||||
|  | 	val = val[:i] | ||||||
|  | 	str := *(*string)(unsafe.Pointer(&val)) | ||||||
|  | 	return str, nil | ||||||
|  | } | ||||||
|  |  | ||||||
| func (dbp *DebuggedProcess) readInt(addr uintptr) (string, error) { | func (dbp *DebuggedProcess) readInt(addr uintptr) (string, error) { | ||||||
| 	val, err := dbp.readMemory(addr, 8) | 	val, err := dbp.readMemory(addr, 8) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
|  | |||||||
| @ -231,6 +231,7 @@ func TestVariableEvaluation(t *testing.T) { | |||||||
| 		value   string | 		value   string | ||||||
| 		varType string | 		varType string | ||||||
| 	}{ | 	}{ | ||||||
|  | 		{"a1", "foo", "struct string"}, | ||||||
| 		{"a2", "6", "int"}, | 		{"a2", "6", "int"}, | ||||||
| 		{"a3", "7.23", "float64"}, | 		{"a3", "7.23", "float64"}, | ||||||
| 	} | 	} | ||||||
| @ -257,7 +258,7 @@ func TestVariableEvaluation(t *testing.T) { | |||||||
| 			} | 			} | ||||||
|  |  | ||||||
| 			if variable.Value != tc.value { | 			if variable.Value != tc.value { | ||||||
| 				t.Fatalf("Expected %s got %s\n", tc.value, variable.Value) | 				t.Fatalf("Expected %#v got %#v\n", tc.value, variable.Value) | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
| 	}) | 	}) | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user
	 Derek Parker
					Derek Parker