douhui0975
douhui0975
2016-05-16 09:52
采纳率: 100%
浏览 133
已采纳

golang从编译文件中获取所有所有文件路径

I am trying to fetch all source files that where used during compilation. Function runtime.Caller() and panic with its stacktrace show this info.

I need something like this

func fetchUsedFiles() []string {
    ....
}
func main() {
    log.Println("Used Files",fetchUsedFiles())
}

UPD:

All what I need is read somehow "runtime.firstmoduledata" variable content. But it is not exported variable.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanlin6989
    duanlin6989 2016-05-18 07:27
    已采纳

    Actually I have combined VonC answer and my runtime approcah.

    All needed data stored at runtime.firstmoduledata - struct but they are not exported. To read data from there I read my executable file again (implemented only for ELF), find this symbol and do the other part like runtime.Caller() function. This method is not portable and can be broken by changes in runtime library but it works.

    selfReflect function uses copies of private types from runtime lib

    https://github.com/martende/restartable/blob/master/restartable.go#L208

    func selfReflect(filename string) ([]string,error) {
        f,err := elf.Open(filename)
        if err != nil {
            return nil,err
        }
        defer f.Close()
        syms,err := f.Symbols()
        if err != nil {
            return nil,err
        }
        var modSym elf.Symbol
        var modSymFound = false
        for _,v := range syms {
            if v.Name == "runtime.firstmoduledata" {
                modSym = v
                modSymFound = true
                break
            }
        }
        if ! modSymFound {
            return nil,errors.New("elfparse:nosym")
        }
    
        var datap = (*moduledata)(unsafe.Pointer(uintptr(modSym.Value)))
    
        files := make([]string,0)
        for i := range datap.filetab {
            bp := &datap.pclntable[datap.filetab[i]]
            file := C.GoString( (*C.char) (unsafe.Pointer(bp))  )
            if file != "<autogenerated>" && file != "@" {
                if _, err := os.Stat(file); err == nil {
                    files = append(files ,file)
                }
            }
        }
    
    
        return files,nil
    }
    
    点赞 评论

相关推荐