美文网首页
Golang标准库——debug/dwarf

Golang标准库——debug/dwarf

作者: DevilRoshan | 来源:发表于2020-09-14 01:19 被阅读0次

dwarf

dwarf包可以访问从可执行文件加载的DWARF调试信息。http://dwarfstd.org/doc/dwarf-2.0.0.pdf

主要是为unix下的调试器提供必要的调试信息,例如PC地址对应的文件名,行号等,以便于源码级调试。

Variables

var ErrUnknownPC = errors.New("ErrUnknownPC")

ErrUnknownPC是LineReader.ScanPC返回的错误,当查找PC未被行表中的任何条目覆盖时。

type AddrType

type AddrType struct {
    BasicType
}

AddrType表示机器地址类型。

type ArrayType

type ArrayType struct {
    CommonType
    Type          Type
    StrideBitSize int64 // if > 0, number of bits to hold each element
    Count         int64 // if == -1, an incomplete array, like char x[].
}

ArrayType表示固定大小的数组类型。

func (*ArrayType) Size

func (t *ArrayType) Size() int64

func (*ArrayType) String

func (t *ArrayType) String() string

type Attr

type Attr uint32

Attr标识DWARF条目的字段中的属性类型。

const (
    AttrSibling        Attr = 0x01
    AttrLocation       Attr = 0x02
    AttrName           Attr = 0x03
    AttrOrdering       Attr = 0x09
    AttrByteSize       Attr = 0x0B
    AttrBitOffset      Attr = 0x0C
    AttrBitSize        Attr = 0x0D
    AttrStmtList       Attr = 0x10
    AttrLowpc          Attr = 0x11
    AttrHighpc         Attr = 0x12
    AttrLanguage       Attr = 0x13
    AttrDiscr          Attr = 0x15
    AttrDiscrValue     Attr = 0x16
    AttrVisibility     Attr = 0x17
    AttrImport         Attr = 0x18
    AttrStringLength   Attr = 0x19
    AttrCommonRef      Attr = 0x1A
    AttrCompDir        Attr = 0x1B
    AttrConstValue     Attr = 0x1C
    AttrContainingType Attr = 0x1D
    AttrDefaultValue   Attr = 0x1E
    AttrInline         Attr = 0x20
    AttrIsOptional     Attr = 0x21
    AttrLowerBound     Attr = 0x22
    AttrProducer       Attr = 0x25
    AttrPrototyped     Attr = 0x27
    AttrReturnAddr     Attr = 0x2A
    AttrStartScope     Attr = 0x2C
    AttrStrideSize     Attr = 0x2E
    AttrUpperBound     Attr = 0x2F
    AttrAbstractOrigin Attr = 0x31
    AttrAccessibility  Attr = 0x32
    AttrAddrClass      Attr = 0x33
    AttrArtificial     Attr = 0x34
    AttrBaseTypes      Attr = 0x35
    AttrCalling        Attr = 0x36
    AttrCount          Attr = 0x37
    AttrDataMemberLoc  Attr = 0x38
    AttrDeclColumn     Attr = 0x39
    AttrDeclFile       Attr = 0x3A
    AttrDeclLine       Attr = 0x3B
    AttrDeclaration    Attr = 0x3C
    AttrDiscrList      Attr = 0x3D
    AttrEncoding       Attr = 0x3E
    AttrExternal       Attr = 0x3F
    AttrFrameBase      Attr = 0x40
    AttrFriend         Attr = 0x41
    AttrIdentifierCase Attr = 0x42
    AttrMacroInfo      Attr = 0x43
    AttrNamelistItem   Attr = 0x44
    AttrPriority       Attr = 0x45
    AttrSegment        Attr = 0x46
    AttrSpecification  Attr = 0x47
    AttrStaticLink     Attr = 0x48
    AttrType           Attr = 0x49
    AttrUseLocation    Attr = 0x4A
    AttrVarParam       Attr = 0x4B
    AttrVirtuality     Attr = 0x4C
    AttrVtableElemLoc  Attr = 0x4D
    AttrAllocated      Attr = 0x4E
    AttrAssociated     Attr = 0x4F
    AttrDataLocation   Attr = 0x50
    AttrStride         Attr = 0x51
    AttrEntrypc        Attr = 0x52
    AttrUseUTF8        Attr = 0x53
    AttrExtension      Attr = 0x54
    AttrRanges         Attr = 0x55
    AttrTrampoline     Attr = 0x56
    AttrCallColumn     Attr = 0x57
    AttrCallFile       Attr = 0x58
    AttrCallLine       Attr = 0x59
    AttrDescription    Attr = 0x5A
)

func (Attr) GoString

func (a Attr) GoString() string

func (Attr) String

func (a Attr) String() string

type BasicType

type BasicType struct {
    CommonType
    BitSize   int64
    BitOffset int64
}

BasicType包含所有基本类型共有的字段。

func (*BasicType) Basic

func (b *BasicType) Basic() *BasicType

func (*BasicType) String

func (t *BasicType) String() string

type BoolType

type BoolType struct {
    BasicType
}

BoolType表示布尔类型。

type CharType

type CharType struct {
    BasicType
}

CharType表示签名字符类型。

type Class

type Class int

类别是属性值的DWARF 4类别。

通常,给定属性的值可以采用DWARF定义的几种可能的类别之一,每种类别都会导致对该属性的解释稍有不同。

DWARF版本4与以前版本的DWARF相比,可以更好地区分属性值类。 读者将把较粗的类从早期版本的DWARF消除为适当的DWARF 4类。 例如,DWARF 2对常量以及所有类型的节偏移使用“ constant”,但是读者会规范化DWARF 2文件中的属性,这些属性引用节偏移到Class * Ptr类之一,即使这些类仅仅是 在DWARF 3中定义。

const (
    // ClassUnknown represents values of unknown DWARF class.
    ClassUnknown Class = iota

    // ClassAddress represents values of type uint64 that are
    // addresses on the target machine.
    ClassAddress

    // ClassBlock represents values of type []byte whose
    // interpretation depends on the attribute.
    ClassBlock

    // ClassConstant represents values of type int64 that are
    // constants. The interpretation of this constant depends on
    // the attribute.
    ClassConstant

    // ClassExprLoc represents values of type []byte that contain
    // an encoded DWARF expression or location description.
    ClassExprLoc

    // ClassFlag represents values of type bool.
    ClassFlag

    // ClassLinePtr represents values that are an int64 offset
    // into the "line" section.
    ClassLinePtr

    // ClassLocListPtr represents values that are an int64 offset
    // into the "loclist" section.
    ClassLocListPtr

    // ClassMacPtr represents values that are an int64 offset into
    // the "mac" section.
    ClassMacPtr

    // ClassMacPtr represents values that are an int64 offset into
    // the "rangelist" section.
    ClassRangeListPtr

    // ClassReference represents values that are an Offset offset
    // of an Entry in the info section (for use with Reader.Seek).
    // The DWARF specification combines ClassReference and
    // ClassReferenceSig into class "reference".
    ClassReference

    // ClassReferenceSig represents values that are a uint64 type
    // signature referencing a type Entry.
    ClassReferenceSig

    // ClassString represents values that are strings. If the
    // compilation unit specifies the AttrUseUTF8 flag (strongly
    // recommended), the string value will be encoded in UTF-8.
    // Otherwise, the encoding is unspecified.
    ClassString

    // ClassReferenceAlt represents values of type int64 that are
    // an offset into the DWARF "info" section of an alternate
    // object file.
    ClassReferenceAlt

    // ClassStringAlt represents values of type int64 that are an
    // offset into the DWARF string section of an alternate object
    // file.
    ClassStringAlt
)

func (Class) GoString

func (i Class) GoString() string

func (Class) [String

func (i Class) String() string

type CommonType

type CommonType struct {
    ByteSize int64  // size of value of this type, in bytes
    Name     string // name that can be used to refer to type
}

CommonType包含多种类型共有的字段。 如果字段未知或不适用于给定类型,则使用零值。

func (*CommonType) Common

func (c *CommonType) Common() *CommonType

func (*CommonType) Size

func (c *CommonType) Size() int64

type ComplexType

type ComplexType struct {
    BasicType
}

ComplexType表示复杂的浮点类型。

type Data

type Data struct {
    // contains filtered or unexported fields
}

数据表示从可执行文件(例如,ELF或Mach-O可执行文件)加载的DWARF调试信息。

func New

func New(abbrev, aranges, frame, info, line, pubnames, ranges, str []byte) (*Data, error)

New返回从给定参数初始化的新Data对象。 客户端通常应使用相应软件包debug / elf,debug / macho或debug / pe的File类型的DWARF方法,而不是直接调用此函数。

[] byte参数是来自目标文件中相应调试部分的数据; 例如,对于ELF对象,abbrev是“ .debug_abbrev”部分的内容。

func (*Data) AddTypes

func (d *Data) AddTypes(name string, types []byte) error

AddTypes将向DWARF数据添加一个.debug_types部分。 具有DWARF版本4调试信息的典型对象将具有多个.debug_types部分。 该名称仅用于错误报告,用于将一个.debug_types部分与另一个部分区分开。

func (*Data) LineReader

func (d *Data) LineReader(cu *Entry) (*LineReader, error)

LineReader为编译单元cu的行表返回一个新的阅读器,该表必须是带有TagCompileUnit标签的Entry。

如果此编译单元没有线表,则返回nil,nil。

func (*Data) Ranges

func (d *Data) Ranges(e *Entry) ([][2]uint64, error)

范围返回由e(一组[低,高]对)覆盖的PC范围。 仅某些条目类型(例如TagCompileUnit或TagSubprogram)具有PC范围;只有某些条目类型才具有PC范围。 对于其他人,这将返回nil且没有错误。

func (*Data) Reader

func (d *Data) Reader() *Reader

Reader返回一个新的Data Reader。 阅读器位于DWARF“信息”部分的字节偏移量0处。

func (*Data) Type

func (d *Data) Type(off Offset) (Type, error)

在DWARF“信息”部分中,类型读取关闭时的类型。

type DecodeError

type DecodeError struct {
    Name   string
    Offset Offset
    Err    string
}

func (DecodeError) Error

func (e DecodeError) Error() string

type DotDotDotType

type DotDotDotType struct {
    CommonType
}

DotDotDotType表示可变参数...函数参数。

func (*DotDotDotType) String

func (t *DotDotDotType) String() string

type Entry

type Entry struct {
    Offset   Offset // offset of Entry in DWARF info
    Tag      Tag    // tag (kind of Entry)
    Children bool   // whether Entry is followed by children
    Field    []Field
}

entry是一系列属性/值对。

func (*Entry) AttrField

func (e *Entry) AttrField(a Attr) *Field

AttrField返回与Entry中的属性Attr关联的Field,如果没有这样的属性,则返回nil。

func (*Entry) Val

func (e *Entry) Val(a Attr) interface{}

Val返回与Entry中的属性Attr关联的值,如果没有这样的属性,则返回nil。

一个常见的习惯用法是将nil return的检查与该值具有预期的动态类型的检查合并,如:

v, ok := e.Val(AttrSibling).(int64)

type EnumType

type EnumType struct {
    CommonType
    EnumName string
    Val      []*EnumValue
}

一个EnumType代表一个枚举类型。 它的本机整数类型的唯一指示是其ByteSize(在CommonType内部)。

func (*EnumType) String

func (t *EnumType) String() string

type EnumValue

type EnumValue struct {
    Name string
    Val  int64
}

一个EnumValue代表一个枚举值。

type Field

type Field struct {
    Attr  Attr
    Val   interface{}
    Class Class
}

Field是条目中的单个属性/值对。

Val可以是DWARF定义的几个“属性类”之一。 每个类对应的Go类型是:

DWARF class       Go type        Class
-----------       -------        -----
address           uint64         ClassAddress
block             []byte         ClassBlock
constant          int64          ClassConstant
flag              bool           ClassFlag
reference
  to info         dwarf.Offset   ClassReference
  to type unit    uint64         ClassReferenceSig
string            string         ClassString
exprloc           []byte         ClassExprLoc
lineptr           int64          ClassLinePtr
loclistptr        int64          ClassLocListPtr
macptr            int64          ClassMacPtr
rangelistptr      int64          ClassRangeListPtr

对于无法识别或供应商定义的属性,Class可以为ClassUnknown。

type FloatType

type FloatType struct {
    BasicType
}

FloatType表示浮点类型。

type FuncType

type FuncType struct {
    CommonType
    ReturnType Type
    ParamType  []Type
}

FuncType表示函数类型。

func (*FuncType) String

func (t *FuncType) String() string

type IntType

type IntType struct {
    BasicType
}

IntType表示有符号整数类型。

type LineEntry

type LineEntry struct {
    // Address is the program-counter value of a machine
    // instruction generated by the compiler. This LineEntry
    // applies to each instruction from Address to just before the
    // Address of the next LineEntry.
    Address uint64

    // OpIndex is the index of an operation within a VLIW
    // instruction. The index of the first operation is 0. For
    // non-VLIW architectures, it will always be 0. Address and
    // OpIndex together form an operation pointer that can
    // reference any individual operation within the instruction
    // stream.
    OpIndex int

    // File is the source file corresponding to these
    // instructions.
    File *LineFile

    // Line is the source code line number corresponding to these
    // instructions. Lines are numbered beginning at 1. It may be
    // 0 if these instructions cannot be attributed to any source
    // line.
    Line int

    // Column is the column number within the source line of these
    // instructions. Columns are numbered beginning at 1. It may
    // be 0 to indicate the "left edge" of the line.
    Column int

    // IsStmt indicates that Address is a recommended breakpoint
    // location, such as the beginning of a line, statement, or a
    // distinct subpart of a statement.
    IsStmt bool

    // BasicBlock indicates that Address is the beginning of a
    // basic block.
    BasicBlock bool

    // PrologueEnd indicates that Address is one (of possibly
    // many) PCs where execution should be suspended for a
    // breakpoint on entry to the containing function.
    //
    // Added in DWARF 3.
    PrologueEnd bool

    // EpilogueBegin indicates that Address is one (of possibly
    // many) PCs where execution should be suspended for a
    // breakpoint on exit from this function.
    //
    // Added in DWARF 3.
    EpilogueBegin bool

    // ISA is the instruction set architecture for these
    // instructions. Possible ISA values should be defined by the
    // applicable ABI specification.
    //
    // Added in DWARF 3.
    ISA int

    // Discriminator is an arbitrary integer indicating the block
    // to which these instructions belong. It serves to
    // distinguish among multiple blocks that may all have with
    // the same source file, line, and column. Where only one
    // block exists for a given source position, it should be 0.
    //
    // Added in DWARF 3.
    Discriminator int

    // EndSequence indicates that Address is the first byte after
    // the end of a sequence of target machine instructions. If it
    // is set, only this and the Address field are meaningful. A
    // line number table may contain information for multiple
    // potentially disjoint instruction sequences. The last entry
    // in a line table should always have EndSequence set.
    EndSequence bool
}

LineEntry是DWARF线表中的一行。

type LineFile

type LineFile struct {
    Name   string
    Mtime  uint64 // Implementation defined modification time, or 0 if unknown
    Length int    // File length, or 0 if unknown
}

LineFile是DWARF线表条目引用的源文件。

type LineReader

type LineReader struct {
    // contains filtered or unexported fields
}

LineReader从DWARF“行”部分读取单个编译单元的LineEntry结构序列。 LineEntries以PC递增的顺序发生,并且每个LineEntry都将来自该LineEntry PC的指令的元数据提供给下一个LineEntry PC之前的指令。 最后一个条目将设置其EndSequence字段。

func (*LineReader) Next

func (r *LineReader) Next(entry *LineEntry) error

Next将* entry设置到此行表中的下一行,然后移动到下一行。 如果没有更多条目,并且该行表已正确终止,则返回io.EOF。

行始终按增加entry.Address的顺序排列,但entry.Line可以前进或后退。

func (*LineReader) Reset

func (r *LineReader) Reset()

重置将线表阅读器重新定位在线表的开头。

func (*LineReader) Seek

func (r *LineReader) Seek(pos LineReaderPos)

Seek将线表读取器还原到Tell所返回的位置。

此行表上对Tell的调用必须已返回参数pos。

func (*LineReader) SeekPC

func (r *LineReader) SeekPC(pc uint64, entry *LineEntry) error

SeekPC将* entry设置为包含pc的LineEntry,并将阅读器放在行表中的下一个条目上。 如有必要,这将向后寻找PC。

如果该行表中的任何条目均未包含pc,则SeekPC返回ErrUnknownPC。 在这种情况下,*输入和最终搜索位置未指定。

请注意,DWARF线表仅允许顺序向前扫描。 因此,在最坏的情况下,这花费的时间与线表的大小成线性关系。 如果呼叫者希望重复进行快速的PC查找,则应建立适当的线表索引。

func (*LineReader) Tell

func (r *LineReader) Tell() LineReaderPos

Tell返回折线表中的当前位置。

type LineReaderPos

type LineReaderPos struct {
    // contains filtered or unexported fields
}

LineReaderPos表示线表中的位置。

type Offset

type Offset uint32

偏移量表示DWARF信息中条目的位置。 (请参阅Reader.Seek。)

type PtrType

type PtrType struct {
    CommonType
    Type Type
}

PtrType表示指针类型。

func (*PtrType) String

func (t *PtrType) String() string

type QualType

type QualType struct {
    CommonType
    Qual string
    Type Type
}

QualType表示具有C / C ++“ const”,“ restrict”或“ volatile”限定符的类型。

func (*QualType) Size

func (t *QualType) Size() int64

func (*QualType) String

func (t *QualType) String() string

type Reader

type Reader struct {
    // contains filtered or unexported fields
}

读取器允许从DWARF“信息”部分读取条目结构。 Entry结构排列在树中。 Reader的Next函数从树的预遍历中返回连续的条目。 如果条目具有子项,则其“子项”字段为true,并且子项跟随,并以标签0的条目终止。

func (*Reader) AddressSize

func (r *Reader) AddressSize() int

AddressSize返回当前编译单元中地址的大小(以字节为单位)。

func (*Reader) Next

func (r *Reader) Next() (*Entry, error)

Next从编码的条目流中读取下一个条目。 它返回nil,到达部分末尾时返回nil。 如果当前偏移量无效或该偏移量处的数据无法解码为有效条目,则它将返回错误。

func (*Reader) Seek

func (r *Reader) Seek(off Offset)

在编码的输入流中,Seek将Reader的位置偏移。 偏移0可用于表示第一个输入。

func (*Reader) SeekPC

func (r *Reader) SeekPC(pc uint64) (*Entry, error)

SeekPC返回包含pc的编译单元的Entry,并定位阅读器以读取该单元的子代。 如果pc未被任何单元覆盖,则SeekPC返回ErrUnknownPC,并且读取器的位置未定义。

因为编译单元可以描述可执行文件的多个区域,所以在最坏的情况下,SeekPC必须搜索所有编译单元中的所有范围。 每次对SeekPC的调用都会在上一个调用的编译单元开始搜索,因此通常,如果对PC进行排序,查找一系列PC会更快。 如果呼叫者希望重复进行快速PC查找,则应使用Ranges方法建立适当的索引。

func (*Reader) SkipChildren

func (r *Reader) SkipChildren()

SkipChildren跳过与Next返回的最后一个条目关联的子条目。 如果该条目没有子对象,或者未调用Next,则SkipChildren是禁止操作的对象。

type StructField

type StructField struct {
    Name       string
    Type       Type
    ByteOffset int64
    ByteSize   int64
    BitOffset  int64 // within the ByteSize bytes at ByteOffset
    BitSize    int64 // zero if not a bit field
}

StructField表示结构,联合或C ++类类型的字段。

type StructType

type StructType struct {
    CommonType
    StructName string
    Kind       string // "struct", "union", or "class".
    Field      []*StructField
    Incomplete bool // if true, struct, union, class is declared but not defined
}

StructType表示结构,联合或C ++类类型。

func (*StructType) Defn

func (t *StructType) Defn() string

func (*StructType) String

func (t *StructType) String() string

type Tag

type Tag uint32

标签是条目的分类(类型)。

const (
    TagArrayType              Tag = 0x01
    TagClassType              Tag = 0x02
    TagEntryPoint             Tag = 0x03
    TagEnumerationType        Tag = 0x04
    TagFormalParameter        Tag = 0x05
    TagImportedDeclaration    Tag = 0x08
    TagLabel                  Tag = 0x0A
    TagLexDwarfBlock          Tag = 0x0B
    TagMember                 Tag = 0x0D
    TagPointerType            Tag = 0x0F
    TagReferenceType          Tag = 0x10
    TagCompileUnit            Tag = 0x11
    TagStringType             Tag = 0x12
    TagStructType             Tag = 0x13
    TagSubroutineType         Tag = 0x15
    TagTypedef                Tag = 0x16
    TagUnionType              Tag = 0x17
    TagUnspecifiedParameters  Tag = 0x18
    TagVariant                Tag = 0x19
    TagCommonDwarfBlock       Tag = 0x1A
    TagCommonInclusion        Tag = 0x1B
    TagInheritance            Tag = 0x1C
    TagInlinedSubroutine      Tag = 0x1D
    TagModule                 Tag = 0x1E
    TagPtrToMemberType        Tag = 0x1F
    TagSetType                Tag = 0x20
    TagSubrangeType           Tag = 0x21
    TagWithStmt               Tag = 0x22
    TagAccessDeclaration      Tag = 0x23
    TagBaseType               Tag = 0x24
    TagCatchDwarfBlock        Tag = 0x25
    TagConstType              Tag = 0x26
    TagConstant               Tag = 0x27
    TagEnumerator             Tag = 0x28
    TagFileType               Tag = 0x29
    TagFriend                 Tag = 0x2A
    TagNamelist               Tag = 0x2B
    TagNamelistItem           Tag = 0x2C
    TagPackedType             Tag = 0x2D
    TagSubprogram             Tag = 0x2E
    TagTemplateTypeParameter  Tag = 0x2F
    TagTemplateValueParameter Tag = 0x30
    TagThrownType             Tag = 0x31
    TagTryDwarfBlock          Tag = 0x32
    TagVariantPart            Tag = 0x33
    TagVariable               Tag = 0x34
    TagVolatileType           Tag = 0x35
    // The following are new in DWARF 3.
    TagDwarfProcedure  Tag = 0x36
    TagRestrictType    Tag = 0x37
    TagInterfaceType   Tag = 0x38
    TagNamespace       Tag = 0x39
    TagImportedModule  Tag = 0x3A
    TagUnspecifiedType Tag = 0x3B
    TagPartialUnit     Tag = 0x3C
    TagImportedUnit    Tag = 0x3D
    TagMutableType     Tag = 0x3E // Later removed from DWARF.
    TagCondition       Tag = 0x3F
    TagSharedType      Tag = 0x40
    // The following are new in DWARF 4.
    TagTypeUnit            Tag = 0x41
    TagRvalueReferenceType Tag = 0x42
    TagTemplateAlias       Tag = 0x43
)

func (Tag) GoString

func (t Tag) GoString() string

func (Tag) String

func (t Tag) String() string

type Type

type Type interface {
    Common() *CommonType
    String() string
    Size() int64
}

Type通常表示一个指向任何特定Type结构(CharType,StructType等)的指针。

type TypedefType

type TypedefType struct {
    CommonType
    Type Type
}

TypedefType表示命名类型。

func (*TypedefType) Size

func (t *TypedefType) Size() int64

func (*TypedefType) String

func (t *TypedefType) String() string

type UcharType

type UcharType struct {
    BasicType
}

UcharType表示无符号字符类型。

type UintType

type UintType struct {
    BasicType
}

UintType表示无符号整数类型。

type UnspecifiedType

type UnspecifiedType struct {
    BasicType
}

UnspecifiedType表示隐式,未知,模棱两可或不存在的类型。

type VoidType

type VoidType struct {
    CommonType
}

VoidType表示C void类型。

func (*VoidType) String

func (t *VoidType) String() string

相关文章

网友评论

      本文标题:Golang标准库——debug/dwarf

      本文链接:https://www.haomeiwen.com/subject/elraektx.html