[Reflection] Add API for inspecting async task allocation slabs.

We remove the existing `swift_reflection_iterateAsyncTaskAllocations` API that attempts to provide all necessary information about a tasks's allocations starting from the task. Instead, we split it into two pieces: `swift_reflection_asyncTaskSlabPointer` to get the first slab for a task, and `+swift_reflection_asyncTaskSlabAllocations` to get the allocations in a slab, and a pointer to the next slab.

We also add a dummy metadata pointer to the beginning of each slab. This allows tools to identify slab allocations on the heap without needing to locate every single async task object. They can then use `swift_reflection_asyncTaskSlabAllocations` on such allocations to find out about the contents.

rdar://82549631
This commit is contained in:
Mike Ash
2021-11-04 11:39:51 -04:00
parent 127874edea
commit 7c7dc5d5b3
13 changed files with 200 additions and 91 deletions

View File

@@ -658,22 +658,39 @@ int reflectEnumValue(SwiftReflectionContextRef RC,
}
static void
asyncTaskIterationCallback(swift_reflection_ptr_t AllocationPtr, unsigned Count,
swift_async_task_allocation_chunk_t Chunks[],
void *ContextPtr) {
printf(" Allocation block %#" PRIx64 "\n", (uint64_t)AllocationPtr);
for (unsigned i = 0; i < Count; i++)
printf(" Chunk at %#" PRIx64 " length %u kind %u\n",
(uint64_t)Chunks[i].Start, Chunks[i].Length, Chunks[i].Kind);
}
int reflectAsyncTask(SwiftReflectionContextRef RC,
const PipeMemoryReader Pipe) {
uintptr_t AsyncTaskInstance = PipeMemoryReader_receiveInstanceAddress(&Pipe);
printf("Async task %#" PRIx64 "\n", (uint64_t)AsyncTaskInstance);
swift_reflection_iterateAsyncTaskAllocations(
RC, AsyncTaskInstance, asyncTaskIterationCallback, NULL);
swift_async_task_slab_return_t SlabPtrResult =
swift_reflection_asyncTaskSlabPointer(RC, AsyncTaskInstance);
if (SlabPtrResult.Error) {
printf("swift_reflection_asyncTaskSlabPointer failed: %s\n",
SlabPtrResult.Error);
} else {
swift_reflection_ptr_t SlabPtr = SlabPtrResult.SlabPtr;
while (SlabPtr) {
printf(" Slab pointer %#" PRIx64 "\n", (uint64_t)SlabPtr);
swift_async_task_slab_allocations_return_t AllocationsResult =
swift_reflection_asyncTaskSlabAllocations(RC, SlabPtr);
if (AllocationsResult.Error) {
printf("swift_reflection_asyncTaskSlabAllocations failed: %s\n",
AllocationsResult.Error);
SlabPtr = 0;
} else {
printf(" Slab size %" PRIu64 "\n",
(uint64_t)AllocationsResult.SlabSize);
for (unsigned i = 0; i < AllocationsResult.ChunkCount; i++) {
swift_async_task_allocation_chunk_t Chunk =
AllocationsResult.Chunks[i];
printf(" Chunk at %#" PRIx64 " length %u kind %u\n",
(uint64_t)Chunk.Start, Chunk.Length, Chunk.Kind);
}
SlabPtr = AllocationsResult.NextSlab;
}
}
}
printf("\n\n");
PipeMemoryReader_sendDoneMessage(&Pipe);