@@ -48,50 +48,38 @@ const elfErrorBadMagicNumber = "bad magic number"
4848
4949func linuxThreadsFromNotes (p * process , notes []* note , machineType elf.Machine ) proc.Thread {
5050 var currentThread proc.Thread
51- var lastThreadAMD * linuxAMD64Thread
52- var lastThreadARM * linuxARM64Thread
53- var lastThreadRISCV * linuxRISCV64Thread
51+ var lastThread osThread
5452
5553 for _ , note := range notes {
5654 switch note .Type {
5755 case elf .NT_PRSTATUS :
58- if machineType == _EM_X86_64 {
56+ switch machineType {
57+ case _EM_X86_64 :
5958 t := note .Desc .(* linuxPrStatusAMD64 )
60- lastThreadAMD = & linuxAMD64Thread {linutil.AMD64Registers {Regs : & t .Reg }, t }
61- p .Threads [int (t .Pid )] = & thread {lastThreadAMD , p , proc.CommonThread {}}
62- if currentThread == nil {
63- currentThread = p .Threads [int (t .Pid )]
64- }
65- } else if machineType == _EM_AARCH64 {
59+ lastThread = & linuxAMD64Thread {linutil.AMD64Registers {Regs : & t .Reg }, t }
60+ case _EM_AARCH64 :
6661 t := note .Desc .(* linuxPrStatusARM64 )
67- lastThreadARM = & linuxARM64Thread {linutil.ARM64Registers {Regs : & t .Reg }, t }
68- p .Threads [int (t .Pid )] = & thread {lastThreadARM , p , proc.CommonThread {}}
69- if currentThread == nil {
70- currentThread = p .Threads [int (t .Pid )]
71- }
72- } else if machineType == _EM_RISCV {
62+ lastThread = & linuxARM64Thread {linutil.ARM64Registers {Regs : & t .Reg }, t }
63+ case _EM_RISCV :
7364 t := note .Desc .(* linuxPrStatusRISCV64 )
74- lastThreadRISCV = & linuxRISCV64Thread {linutil.RISCV64Registers {Regs : & t .Reg }, t }
75- p .Threads [int (t .Pid )] = & thread {lastThreadRISCV , p , proc.CommonThread {}}
76- if currentThread == nil {
77- currentThread = p .Threads [int (t .Pid )]
78- }
65+ lastThread = & linuxRISCV64Thread {linutil.RISCV64Registers {Regs : & t .Reg }, t }
66+ default :
67+ continue
68+ }
69+ p .Threads [lastThread .ThreadID ()] = & thread {lastThread , p , proc.CommonThread {}}
70+ if currentThread == nil {
71+ currentThread = p .Threads [lastThread .ThreadID ()]
7972 }
8073 case _NT_FPREGSET :
81- if machineType == _EM_AARCH64 {
82- if lastThreadARM != nil {
83- lastThreadARM .regs .Fpregs = note .Desc .(* linutil.ARM64PtraceFpRegs ).Decode ()
84- }
85- } else if machineType == _EM_RISCV {
86- if lastThreadRISCV != nil {
87- lastThreadRISCV .regs .Fpregs = note .Desc .(* linutil.RISCV64PtraceFpRegs ).Decode ()
88- }
74+ switch th := lastThread .(type ) {
75+ case * linuxARM64Thread :
76+ th .regs .Fpregs = note .Desc .(* linutil.ARM64PtraceFpRegs ).Decode ()
77+ case * linuxRISCV64Thread :
78+ th .regs .Fpregs = note .Desc .(* linutil.RISCV64PtraceFpRegs ).Decode ()
8979 }
9080 case _NT_X86_XSTATE :
91- if machineType == _EM_X86_64 {
92- if lastThreadAMD != nil {
93- lastThreadAMD .regs .Fpregs = note .Desc .(* amd64util.AMD64Xstate ).Decode ()
94- }
81+ if lastThread != nil {
82+ lastThread .(* linuxAMD64Thread ).regs .Fpregs = note .Desc .(* amd64util.AMD64Xstate ).Decode ()
9583 }
9684 case elf .NT_PRPSINFO :
9785 p .pid = int (note .Desc .(* linuxPrPsInfo ).Pid )
@@ -100,6 +88,12 @@ func linuxThreadsFromNotes(p *process, notes []*note, machineType elf.Machine) p
10088 return currentThread
10189}
10290
91+ var supportedLinuxMachines = map [elf.Machine ]string {
92+ _EM_X86_64 : "amd64" ,
93+ _EM_AARCH64 : "arm64" ,
94+ _EM_RISCV : "riscv64" ,
95+ }
96+
10397// readLinuxOrPlatformIndependentCore reads a core file from corePath
10498// corresponding to the executable at exePath. For details on the Linux ELF
10599// core format, see:
@@ -155,17 +149,10 @@ func readLinuxOrPlatformIndependentCore(corePath, exePath string) (*process, pro
155149 return nil , nil , err
156150 }
157151 bi = proc .NewBinaryInfo (goos , goarch )
152+ } else if goarch , ok := supportedLinuxMachines [machineType ]; ok {
153+ bi = proc .NewBinaryInfo ("linux" , goarch )
158154 } else {
159- switch machineType {
160- case _EM_X86_64 :
161- bi = proc .NewBinaryInfo ("linux" , "amd64" )
162- case _EM_AARCH64 :
163- bi = proc .NewBinaryInfo ("linux" , "arm64" )
164- case _EM_RISCV :
165- bi = proc .NewBinaryInfo ("linux" , "riscv64" )
166- default :
167- return nil , nil , errors .New ("unsupported machine type" )
168- }
155+ return nil , nil , errors .New ("unsupported machine type" )
169156 }
170157
171158 entryPoint := findEntryPoint (notes , bi .Arch .PtrSize ())
@@ -202,36 +189,36 @@ type linuxRISCV64Thread struct {
202189 t * linuxPrStatusRISCV64
203190}
204191
205- func (t * linuxAMD64Thread ) registers () (proc.Registers , error ) {
192+ func (t * linuxAMD64Thread ) Registers () (proc.Registers , error ) {
206193 var r linutil.AMD64Registers
207194 r .Regs = t .regs .Regs
208195 r .Fpregs = t .regs .Fpregs
209196 return & r , nil
210197}
211198
212- func (t * linuxARM64Thread ) registers () (proc.Registers , error ) {
199+ func (t * linuxARM64Thread ) Registers () (proc.Registers , error ) {
213200 var r linutil.ARM64Registers
214201 r .Regs = t .regs .Regs
215202 r .Fpregs = t .regs .Fpregs
216203 return & r , nil
217204}
218205
219- func (t * linuxRISCV64Thread ) registers () (proc.Registers , error ) {
206+ func (t * linuxRISCV64Thread ) Registers () (proc.Registers , error ) {
220207 var r linutil.RISCV64Registers
221208 r .Regs = t .regs .Regs
222209 r .Fpregs = t .regs .Fpregs
223210 return & r , nil
224211}
225212
226- func (t * linuxAMD64Thread ) pid () int {
213+ func (t * linuxAMD64Thread ) ThreadID () int {
227214 return int (t .t .Pid )
228215}
229216
230- func (t * linuxARM64Thread ) pid () int {
217+ func (t * linuxARM64Thread ) ThreadID () int {
231218 return int (t .t .Pid )
232219}
233220
234- func (t * linuxRISCV64Thread ) pid () int {
221+ func (t * linuxRISCV64Thread ) ThreadID () int {
235222 return int (t .t .Pid )
236223}
237224
@@ -360,19 +347,12 @@ func readNote(r io.ReadSeeker, machineType elf.Machine) (*note, error) {
360347 note .Desc = desc
361348 case _NT_FPREGSET :
362349 if machineType == _EM_AARCH64 {
363- fpregs := & linutil.ARM64PtraceFpRegs {}
364- rdr := bytes .NewReader (desc [:_ARM_FP_HEADER_START ])
365- if err := binary .Read (rdr , binary .LittleEndian , fpregs .Byte ()); err != nil {
366- return nil , err
367- }
368- note .Desc = fpregs
350+ err = readFpregsetNote (note , & linutil.ARM64PtraceFpRegs {}, desc [:_ARM_FP_HEADER_START ])
369351 } else if machineType == _EM_RISCV {
370- fpregs := & linutil.RISCV64PtraceFpRegs {}
371- rdr := bytes .NewReader (desc )
372- if err := binary .Read (rdr , binary .LittleEndian , fpregs .Byte ()); err != nil {
373- return nil , err
374- }
375- note .Desc = fpregs
352+ err = readFpregsetNote (note , & linutil.RISCV64PtraceFpRegs {}, desc )
353+ }
354+ if err != nil {
355+ return nil , err
376356 }
377357 }
378358 if err := skipPadding (r , 4 ); err != nil {
@@ -381,6 +361,15 @@ func readNote(r io.ReadSeeker, machineType elf.Machine) (*note, error) {
381361 return note , nil
382362}
383363
364+ func readFpregsetNote (note * note , fpregs interface { Byte () []byte }, desc []byte ) error {
365+ rdr := bytes .NewReader (desc )
366+ if err := binary .Read (rdr , binary .LittleEndian , fpregs .Byte ()); err != nil {
367+ return err
368+ }
369+ note .Desc = fpregs
370+ return nil
371+ }
372+
384373// skipPadding moves r to the next multiple of pad.
385374func skipPadding (r io.ReadSeeker , pad int64 ) error {
386375 pos , err := r .Seek (0 , io .SeekCurrent )
0 commit comments