Reputation: 3200
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.
Upvotes: 1
Views: 477
Reputation: 3200
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
}
Upvotes: 1