Skip to content

Commit

Permalink
fix: read filepath from dwarf line
Browse files Browse the repository at this point in the history
Closes: #67
  • Loading branch information
Zxilly committed Jun 10, 2024
1 parent 183152e commit eb450cb
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 18 deletions.
6 changes: 1 addition & 5 deletions .github/codecov.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
codecov:
allow_pseudo_compare: true
allow_coverage_offsets: true

coverage:
status:
patch:
Expand All @@ -12,11 +13,6 @@ coverage:
threshold: 5%
target: 70%

ignore:
- "internal/wrapper/*.go"
- "internal/knowninfo/dwarf_info.go"
- "cmd/gsa/*.go"

comment:
layout: "condensed_header, condensed_files, diff, flags, condensed_footer"
behavior: default
Expand Down
38 changes: 25 additions & 13 deletions internal/knowninfo/dwarf.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,20 @@ func (k *KnownInfo) TryLoadDwarf() bool {

loadCompilerUnit := func(cuEntry *dwarf.Entry, pendingEntry []*dwarf.Entry) {
var pkg *entity.Package
var filePath string

cuLang, _ := cuEntry.Val(dwarf.AttrLanguage).(int64)
cuName, _ := cuEntry.Val(dwarf.AttrName).(string)

lr, err := d.LineReader(cuEntry)
if err != nil {
slog.Warn(fmt.Sprintf("Failed to read DWARF line: %v", err))
return
}
var files []*dwarf.LineFile
if lr != nil {
files = lr.Files()
}

if cuLang == DwLangGo {
// if we have load it with pclntab?
pkg = k.Deps.Trie.Get(cuName)
Expand All @@ -86,7 +95,6 @@ func (k *KnownInfo) TryLoadDwarf() bool {
pkg.Name = cuName
}
pkg.DwarfEntry = cuEntry
filePath = "<unknown>"
typ := entity.PackageTypeVendor
if cuName == "main" {
typ = entity.PackageTypeMain
Expand All @@ -103,15 +111,6 @@ func (k *KnownInfo) TryLoadDwarf() bool {
pkg.Type = entity.PackageTypeCGO
langPackages[pkgName] = pkg
}

compDirAny := cuEntry.Val(dwarf.AttrCompDir)
if compDirAny == nil {
slog.Warn("Failed to load DWARF: no compDir")
return
}
compDir := compDirAny.(string)

filePath = fmt.Sprintf("%s/%s", compDir, cuName)
}

for _, subEntry := range pendingEntry {
Expand Down Expand Up @@ -149,8 +148,9 @@ func (k *KnownInfo) TryLoadDwarf() bool {
}

if len(ranges) == 0 {
// fixme: maybe compiler optimization it?
// fixme: maybe compiler optimize it?
// example: sqlite3 simpleDestroy
slog.Debug(fmt.Sprintf("Failed to load DWARF function size, no range: %s", subEntryName))
continue
}

Expand All @@ -166,6 +166,18 @@ func (k *KnownInfo) TryLoadDwarf() bool {
}
}

filename := "<autogenerated>"
if subEntry.Val(dwarf.AttrTrampoline) == nil {
fileIndexAny := subEntry.Val(dwarf.AttrDeclFile)
if fileIndexAny == nil {
slog.Warn(fmt.Sprintf("Failed to load DWARF function file: %s", subEntryName))
continue
}
fileIndex := fileIndexAny.(int64)
file := files[fileIndex]
filename = file.Name
}

fn := &entity.Function{
Name: subEntryName,
Addr: addr,
Expand All @@ -175,7 +187,7 @@ func (k *KnownInfo) TryLoadDwarf() bool {
PclnSize: entity.NewEmptyPclnSymbolSize(),
}

added := pkg.AddFuncIfNotExists(filePath, fn)
added := pkg.AddFuncIfNotExists(filename, fn)

if added {
k.KnownAddr.Text.Insert(&entity.Addr{
Expand Down
83 changes: 83 additions & 0 deletions internal/knowninfo/dwarf_info_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package knowninfo

import (
"strconv"
"testing"
)

func TestLanguageString(t *testing.T) {
tests := []struct {
input Language
expected string
}{
{DwLangC89, "C89"},
{DwLangC, "C"},
{DwLangAda83, "Ada83"},
{DwLangCPP, "C++"},
{DwLangCobol74, "Cobol74"},
{DwLangCobol85, "Cobol85"},
{DwLangFortran77, "Fortran77"},
{DwLangFortran90, "Fortran90"},
{DwLangPascal83, "Pascal83"},
{DwLangModula2, "Modula2"},
{DwLangJava, "Java"},
{DwLangC99, "C99"},
{DwLangAda95, "Ada95"},
{DwLangFortran95, "Fortran95"},
{DwLangPLI, "PLI"},
{DwLangObjC, "ObjC"},
{DwLangObjCPP, "ObjC++"},
{DwLangUPC, "UPC"},
{DwLangD, "D"},
{DwLangPython, "Python"},
{DwLangOpenCL, "OpenCL"},
{DwLangGo, "Go"},
{DwLangModula3, "Modula3"},
{DwLangHaskell, "Haskell"},
{DwLangCPP03, "C++03"},
{DwLangCPP11, "C++11"},
{DwLangOCaml, "OCaml"},
{DwLangRust, "Rust"},
{DwLangC11, "C11"},
{DwLangSwift, "Swift"},
{DwLangJulia, "Julia"},
{DwLangDylan, "Dylan"},
{DwLangCPP14, "C++14"},
{DwLangFortran03, "Fortran03"},
{DwLangFortran08, "Fortran08"},
{DwLangRenderScript, "RenderScript"},
{DwLangBLISS, "BLISS"},
{DwLangKotlin, "Kotlin"},
{DwLangZig, "Zig"},
{DwLangCrystal, "Crystal"},
{DwLangCPP17, "C++17"},
{DwLangCPP20, "C++20"},
{DwLangC17, "C17"},
{DwLangFortran18, "Fortran18"},
{DwLangAda2005, "Ada2005"},
{DwLangAda2012, "Ada2012"},
{DwLangHIP, "HIP"},
{DwLangAssembly, "Assembly"},
{DwLangCSharp, "C#"},
{DwLangMojo, "Mojo"},
{DwLangGLSL, "GLSL"},
{DwLangGLSLES, "GLSLES"},
{DwLangHLSL, "HLSL"},
{DwLangOpenCLCPP, "OpenCL++"},
{DwLangCPPForOpenCL, "C++ForOpenCL"},
{DwLangSYCL, "SYCL"},
{DwLangRuby, "Ruby"},
{DwLangMove, "Move"},
{DwLangHylo, "Hylo"},
{Language(0x9999), "Language(39321)"}, // Test case for an unknown language constant
}

for _, tt := range tests {
t.Run(strconv.Itoa(int(tt.input)), func(t *testing.T) {
result := LanguageString(tt.input)
if result != tt.expected {
t.Errorf("LanguageString(%v) = %v; want %v", tt.input, result, tt.expected)
}
})
}
}

0 comments on commit eb450cb

Please sign in to comment.