Skip to content

Commit 5d5af8b

Browse files
committed
Make core reader more robust to missing files.
1 parent 8e60c9a commit 5d5af8b

File tree

1 file changed

+9
-15
lines changed

1 file changed

+9
-15
lines changed

core/read.go

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,6 @@ func Core(coreFile, base string) (*Process, error) {
2828
return nil, err
2929
}
3030

31-
// Double-check that we have complete data available for all mappings.
32-
for _, m := range p.maps {
33-
if m.f == nil {
34-
return nil, fmt.Errorf("incomplete mapping %x %x", m.min, m.max)
35-
}
36-
}
37-
3831
// Sort then merge mappings, just to clean up a bit.
3932
sort.Slice(p.maps, func(i, j int) bool {
4033
return p.maps[i].min < p.maps[j].min
@@ -56,6 +49,11 @@ func Core(coreFile, base string) (*Process, error) {
5649

5750
// Memory map all the mappings.
5851
for _, m := range maps {
52+
if m.f == nil {
53+
// Pretend this is read-as-zero.
54+
m.contents = make([]byte, m.max.Sub(m.min))
55+
continue
56+
}
5957
var err error
6058
m.contents, err = syscall.Mmap(int(m.f.Fd()), m.off, int(m.max.Sub(m.min)), syscall.PROT_READ, syscall.MAP_SHARED)
6159
if err != nil {
@@ -151,9 +149,6 @@ func (p *Process) readLoad(f *os.File, e *elf.File, prog *elf.Prog) error {
151149
}
152150
if prog.Flags&elf.PF_W != 0 {
153151
perm |= Write
154-
if prog.Filesz != prog.Memsz {
155-
return fmt.Errorf("Data at address %x is not complete. The core has %x bytes, we need %x bytes", min, prog.Filesz, prog.Memsz)
156-
}
157152
}
158153
if prog.Flags&elf.PF_X != 0 {
159154
perm |= Exec
@@ -171,6 +166,7 @@ func (p *Process) readLoad(f *os.File, e *elf.File, prog *elf.Prog) error {
171166
if prog.Filesz < uint64(m.max.Sub(m.min)) {
172167
// We only have partial data for this mapping in the core file.
173168
// Trim the mapping and allocate an anonymous mapping for the remainder.
169+
// The remainder will be read-as-zero.
174170
m2 := &Mapping{min: m.min.Add(int64(prog.Filesz)), max: m.max, perm: m.perm}
175171
m.max = m2.min
176172
p.maps = append(p.maps, m2)
@@ -247,10 +243,8 @@ func (p *Process) readNTFile(f *os.File, e *elf.File, desc []byte) error {
247243

248244
backing, err := os.Open(filepath.Join(p.base, name))
249245
if err != nil {
250-
// Can't find mapped file.
251-
// TODO: if we debug on a different machine,
252-
// provide a way to map from core's file spec to a real file.
253-
return fmt.Errorf("can't open mapped file: %v\n", err)
246+
fmt.Fprintf(os.Stderr, "Missing data for addresses [%x %x] because of failure to %s. Assuming all zero.\n", min, max, err)
247+
// backing==nil means treat as all zero.
254248
}
255249

256250
// TODO: this is O(n^2). Shouldn't be a big problem in practice.
@@ -294,7 +288,7 @@ func (p *Process) readNTFile(f *os.File, e *elf.File, desc []byte) error {
294288
}
295289

296290
}
297-
if !found {
291+
if !found && backing != nil {
298292
p.exec = append(p.exec, m.f)
299293
}
300294
}

0 commit comments

Comments
 (0)