Skip to content

Commit bf90f33

Browse files
committed
pkg/proc/core: Clean up some repetitive code
Cleans up repetitive code in pkg/proc/core and also adds coredumpctl support to our test suite.
1 parent 05dc760 commit bf90f33

File tree

5 files changed

+65
-81
lines changed

5 files changed

+65
-81
lines changed

pkg/proc/core/core.go

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -168,14 +168,14 @@ type process struct {
168168

169169
// thread represents a thread in the core file being debugged.
170170
type thread struct {
171-
th osThread
171+
osThread
172172
p *process
173173
common proc.CommonThread
174174
}
175175

176176
type osThread interface {
177-
registers() (proc.Registers, error)
178-
pid() int
177+
Registers() (proc.Registers, error)
178+
ThreadID() int
179179
}
180180

181181
var (
@@ -322,7 +322,7 @@ func (t *thread) ProcessMemory() proc.MemoryReadWriter {
322322
// Location returns the location of this thread based on
323323
// the value of the instruction pointer register.
324324
func (t *thread) Location() (*proc.Location, error) {
325-
regs, err := t.th.registers()
325+
regs, err := t.Registers()
326326
if err != nil {
327327
return nil, err
328328
}
@@ -338,16 +338,6 @@ func (t *thread) Breakpoint() *proc.BreakpointState {
338338
return &proc.BreakpointState{}
339339
}
340340

341-
// ThreadID returns the ID for this thread.
342-
func (t *thread) ThreadID() int {
343-
return t.th.pid()
344-
}
345-
346-
// Registers returns the current value of the registers for this thread.
347-
func (t *thread) Registers() (proc.Registers, error) {
348-
return t.th.registers()
349-
}
350-
351341
// RestoreRegisters will only return an error for core files,
352342
// you cannot change register values for core files.
353343
func (t *thread) RestoreRegisters(proc.Registers) error {

pkg/proc/core/core_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,13 @@ func withCoreFile(t *testing.T, name, args string) *proc.TargetGroup {
213213
case err != nil || len(cores) > 1:
214214
t.Fatalf("Got %v, wanted one file named core* in %v", cores, tempDir)
215215
case len(cores) == 0:
216-
t.Skipf("core file was not produced, could not run test")
217-
return nil
216+
cores = []string{fix.Path + ".dump"}
217+
err := exec.Command("coredumpctl", "--output="+cores[0], "dump", fix.Path).Run()
218+
if err != nil {
219+
t.Skipf("core file was not produced, could not run test, coredumpctl error: %v", err)
220+
return nil
221+
}
222+
test.PathsToRemove = append(test.PathsToRemove, cores[0])
218223
}
219224
corePath := cores[0]
220225

pkg/proc/core/delve_core.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,11 @@ type delveThread struct {
130130
regs *delveRegisters
131131
}
132132

133-
func (th *delveThread) pid() int {
133+
func (th *delveThread) ThreadID() int {
134134
return int(th.id)
135135
}
136136

137-
func (th *delveThread) registers() (proc.Registers, error) {
137+
func (th *delveThread) Registers() (proc.Registers, error) {
138138
return th.regs, nil
139139
}
140140

pkg/proc/core/linux_core.go

Lines changed: 50 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -48,50 +48,38 @@ const elfErrorBadMagicNumber = "bad magic number"
4848

4949
func 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.
385374
func skipPadding(r io.ReadSeeker, pad int64) error {
386375
pos, err := r.Seek(0, io.SeekCurrent)

pkg/proc/core/windows_amd64_minidump.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ type windowsAMD64Thread struct {
5757
th *minidump.Thread
5858
}
5959

60-
func (th *windowsAMD64Thread) pid() int {
60+
func (th *windowsAMD64Thread) ThreadID() int {
6161
return int(th.th.ID)
6262
}
6363

64-
func (th *windowsAMD64Thread) registers() (proc.Registers, error) {
64+
func (th *windowsAMD64Thread) Registers() (proc.Registers, error) {
6565
return winutil.NewAMD64Registers(&th.th.Context, th.th.TEB), nil
6666
}

0 commit comments

Comments
 (0)