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
}