File: dwarf.go

package info (click to toggle)
golang-golang-x-debug 0.0~git20160621.0.fb50892-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 844 kB
  • sloc: ansic: 82; asm: 46; makefile: 4
file content (100 lines) | stat: -rw-r--r-- 2,433 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package server

import (
	"errors"
	"fmt"

	"golang.org/x/debug/dwarf"
)

func (s *Server) functionStartAddress(name string) (uint64, error) {
	entry, err := s.dwarfData.LookupFunction(name)
	if err != nil {
		return 0, err
	}
	addrAttr := entry.Val(dwarf.AttrLowpc)
	if addrAttr == nil {
		return 0, fmt.Errorf("symbol %q has no LowPC attribute", name)
	}
	addr, ok := addrAttr.(uint64)
	if !ok {
		return 0, fmt.Errorf("symbol %q has non-uint64 LowPC attribute", name)
	}
	return addr, nil
}

// evalLocation parses a DWARF location description encoded in v.  It works for
// cases where the variable is stored at an offset from the Canonical Frame
// Address.  The return value is this offset.
// TODO: a more general location-description-parsing function.
func evalLocation(v []uint8) (int64, error) {
	// Some DWARF constants.
	const (
		opConsts       = 0x11
		opPlus         = 0x22
		opCallFrameCFA = 0x9C
	)
	if len(v) == 0 {
		return 0, errors.New("empty location specifier")
	}
	if v[0] != opCallFrameCFA {
		return 0, errors.New("unsupported location specifier")
	}
	if len(v) == 1 {
		// The location description was just DW_OP_call_frame_cfa, so the location is exactly the CFA.
		return 0, nil
	}
	if v[1] != opConsts {
		return 0, errors.New("unsupported location specifier")
	}
	offset, v, err := sleb128(v[2:])
	if err != nil {
		return 0, err
	}
	if len(v) == 1 && v[0] == opPlus {
		// The location description was DW_OP_call_frame_cfa, DW_OP_consts <offset>, DW_OP_plus.
		// So return the offset.
		return offset, nil
	}
	return 0, errors.New("unsupported location specifier")
}

func uleb128(v []uint8) (u uint64) {
	var shift uint
	for _, x := range v {
		u |= (uint64(x) & 0x7F) << shift
		shift += 7
		if x&0x80 == 0 {
			break
		}
	}
	return u
}

// sleb128 parses a signed integer encoded with sleb128 at the start of v, and
// returns the integer and the remainder of v.
func sleb128(v []uint8) (s int64, rest []uint8, err error) {
	var shift uint
	var sign int64 = -1
	var i int
	var x uint8
	for i, x = range v {
		s |= (int64(x) & 0x7F) << shift
		shift += 7
		sign <<= 7
		if x&0x80 == 0 {
			if x&0x40 != 0 {
				s |= sign
			}
			break
		}
	}
	if i == len(v) {
		return 0, nil, errors.New("truncated sleb128")
	}
	return s, v[i+1:], nil
}