From ce83862d80c07f53bf599d9e46c702c284b97bb1 Mon Sep 17 00:00:00 2001 From: aarzilli Date: Sat, 19 Aug 2017 13:06:20 +0200 Subject: [PATCH] pkg/dwarf/line: support DW_LNE_define_file --- pkg/dwarf/line/line_parser.go | 33 +++++++++++++++++++++------------ pkg/dwarf/line/state_machine.go | 23 ++++++++++++++--------- 2 files changed, 35 insertions(+), 21 deletions(-) diff --git a/pkg/dwarf/line/line_parser.go b/pkg/dwarf/line/line_parser.go index da4b3833..27cfbcbd 100644 --- a/pkg/dwarf/line/line_parser.go +++ b/pkg/dwarf/line/line_parser.go @@ -114,23 +114,32 @@ func parseIncludeDirs(info *DebugLineInfo, buf *bytes.Buffer) { func parseFileEntries(info *DebugLineInfo, buf *bytes.Buffer) { for { - entry := new(FileEntry) - - entry.Path, _ = util.ParseString(buf) + entry := readFileEntry(info, buf, true) if entry.Path == "" { break } - entry.DirIdx, _ = util.DecodeULEB128(buf) - entry.LastModTime, _ = util.DecodeULEB128(buf) - entry.Length, _ = util.DecodeULEB128(buf) - if !filepath.IsAbs(entry.Path) { - if entry.DirIdx >= 0 && entry.DirIdx < uint64(len(info.IncludeDirs)) { - entry.Path = filepath.Join(info.IncludeDirs[entry.DirIdx], entry.Path) - } - } - info.FileNames = append(info.FileNames, entry) info.Lookup[entry.Path] = entry } } + +func readFileEntry(info *DebugLineInfo, buf *bytes.Buffer, exitOnEmptyPath bool) *FileEntry { + entry := new(FileEntry) + + entry.Path, _ = util.ParseString(buf) + if entry.Path == "" && exitOnEmptyPath { + return entry + } + + entry.DirIdx, _ = util.DecodeULEB128(buf) + entry.LastModTime, _ = util.DecodeULEB128(buf) + entry.Length, _ = util.DecodeULEB128(buf) + if !filepath.IsAbs(entry.Path) { + if entry.DirIdx >= 0 && entry.DirIdx < uint64(len(info.IncludeDirs)) { + entry.Path = filepath.Join(info.IncludeDirs[entry.DirIdx], entry.Path) + } + } + + return entry +} diff --git a/pkg/dwarf/line/state_machine.go b/pkg/dwarf/line/state_machine.go index b7ecbef0..188d94b8 100644 --- a/pkg/dwarf/line/state_machine.go +++ b/pkg/dwarf/line/state_machine.go @@ -38,6 +38,8 @@ type StateMachine struct { buf *bytes.Buffer // remaining instructions opcodes []opcodefn + definedFiles []*FileEntry // files defined with DW_LINE_define_file + lastAddress uint64 lastFile string lastLine int @@ -335,7 +337,16 @@ func advanceline(sm *StateMachine, buf *bytes.Buffer) { func setfile(sm *StateMachine, buf *bytes.Buffer) { i, _ := util.DecodeULEB128(buf) - sm.file = sm.dbl.FileNames[i-1].Path + if i-1 < uint64(len(sm.dbl.FileNames)) { + sm.file = sm.dbl.FileNames[i-1].Path + } else { + j := (i - 1) - uint64(len(sm.dbl.FileNames)) + if j < uint64(len(sm.definedFiles)) { + sm.file = sm.definedFiles[j].Path + } else { + sm.file = "" + } + } } func setcolumn(sm *StateMachine, buf *bytes.Buffer) { @@ -376,12 +387,6 @@ func setaddress(sm *StateMachine, buf *bytes.Buffer) { } func definefile(sm *StateMachine, buf *bytes.Buffer) { - var ( - _, _ = util.ParseString(buf) - _, _ = util.DecodeULEB128(buf) - _, _ = util.DecodeULEB128(buf) - _, _ = util.DecodeULEB128(buf) - ) - - // Don't do anything here yet. + entry := readFileEntry(sm.dbl, sm.buf, false) + sm.definedFiles = append(sm.definedFiles, entry) } -- GitLab