This is an archive of the discontinued LLVM Phabricator instance.

[lldb] [test/Register] Initial tests for regsets in core dumps [WIP]
ClosedPublic

Authored by mgorny on Nov 23 2020, 6:55 AM.

Details

Summary

Add initial tests for reading register sets from core dumps. This
includes a C++ program to write registers and dump core, resulting core
dumps for Linux, FreeBSD and NetBSD, and the tests to verify them.

The tests are split into generic part, verifying user-specified register
values, and coredump-specific tests that verify memory addresses that
differ for every dump.

At this moment, all platforms support GPRs and FPRs up to XMM for amd64
target. The i386 target does not work on NetBSD at all, and is missing
FPRs entirely on FreeBSD.

Diff Detail

Event Timeline

mgorny created this revision.Nov 23 2020, 6:55 AM
mgorny requested review of this revision.Nov 23 2020, 6:55 AM

I can't tell the core sizes from the diff, but my recollection is that even non-freebsd core files are larger than one would like. Even with linux, ones has to go to great lengths to get it to create a small one.

I'd recommend manually splicing the core to only include the data relevant for the tests. I just tried the process on a random core file and it was pretty straight-forward. What I did was:

  • change the e_phnum to one (the NOTE segment was the first segment in the headers)
  • delete everything after the end of the NOTE segment (the NOTE segment data was the first)

Lldb was perfectly happy to open the resulting core file, but I've reduced its size by more than 99%. The file still contains remnants of the discarded program headers, but they are harmless.

It looks like a similar approach could be applied to NetBSD core files. It seems NetBSD places the NOTE segment last in the program headers (not in the file), so one would also need to adjust e_phoff to point to it. If FreeBSD puts the NOTE segment last in the file, then one would also need to adjust p_offset in the program header, and splice data out of the middle of the file, which is slightly trickier, but not impossible. (I've also successfully spliced a single note (NT_FILE) out of the note segment, and it was relatively straight-forward, but probably unnecessary).

Come to think of it, given the number of core files you have amassed, it might not be unreasonable to turn this process into a shell script (in python, in shell with readelf+grep+dd, or whatever). Then we could check that in somewhere and ask people to run it whenever they want to contribute a core file (for register reading purposes, at least).

mgorny updated this revision to Diff 338527.Apr 19 2021, 8:14 AM
mgorny edited the summary of this revision. (Show Details)

Now includes a small-ish Python scripts to strip unnecessary data from coredumps. For now, it just strips everything but the PT_NOTE segment. Headers aren't modified, so holes are inserted before PT_NOTE segments, and the file is truncated after the last one. It uses the pyelftools library to portably read ELF headers.

New sizes:

$ ls -lh *.core
-rw------- 1 mgorny mgorny  13K 04-19 17:11 x86-32-freebsd.core
-rw------- 1 mgorny mgorny 5,1K 04-19 17:11 x86-32-linux.core
-rw------- 1 mgorny mgorny 2,7K 04-19 17:11 x86-32-netbsd.core
-rw------- 1 mgorny mgorny  15K 04-19 17:11 x86-64-freebsd.core
-rw------- 1 mgorny mgorny 6,6K 04-19 17:11 x86-64-linux.core
-rw------- 1 mgorny mgorny 5,2K 04-19 17:11 x86-64-netbsd.core
labath accepted this revision.Apr 21 2021, 8:04 AM

I like this.

I should note that, in the mean time, obj2yaml support for program headers has been greatly improved, to the point where one may actually be able to generate a reasonably-looking core file from yaml. However, obj2yaml support is lagging behind a bit, so one could not produce an initial yaml file and would have to start from scratch instead. I would love to get the missing bits of functionality into yaml2obj so this could all be yaml-ified, but I think this is pretty cool nonetheless...

This revision is now accepted and ready to land.Apr 21 2021, 8:04 AM

I'll try to keep this in mind. I expect to have some vacation time ~1 month from now, so maybe then ;-).

Herald added a project: Restricted Project. · View Herald TranscriptApr 21 2021, 10:41 AM