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

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 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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序