contrib/elf2dmp: Use GPtrArray

This removes the need to enumarate QEMUCPUState twice and saves code.

Signed-off-by: Akihiko Odaki <akihiko.odaki@daynix.com>
Reviewed-by: Peter Maydell <peter.maydell@linaro.org>
Tested-by: Viktor Prutyanov <viktor.prutyanov@phystech.edu>
Message-id: 20240307-elf2dmp-v4-17-4f324ad4d99d@daynix.com
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
This commit is contained in:
Akihiko Odaki 2024-03-07 19:21:00 +09:00 committed by Peter Maydell
parent a2de23c6e3
commit 0c94e32dff

View File

@ -66,7 +66,7 @@ static bool init_states(QEMU_Elf *qe)
Elf64_Nhdr *start = (void *)((uint8_t *)qe->map + phdr[0].p_offset); Elf64_Nhdr *start = (void *)((uint8_t *)qe->map + phdr[0].p_offset);
Elf64_Nhdr *end = (void *)((uint8_t *)start + phdr[0].p_memsz); Elf64_Nhdr *end = (void *)((uint8_t *)start + phdr[0].p_memsz);
Elf64_Nhdr *nhdr; Elf64_Nhdr *nhdr;
size_t cpu_nr = 0; GPtrArray *states;
if (phdr[0].p_type != PT_NOTE) { if (phdr[0].p_type != PT_NOTE) {
eprintf("Failed to find PT_NOTE\n"); eprintf("Failed to find PT_NOTE\n");
@ -74,38 +74,29 @@ static bool init_states(QEMU_Elf *qe)
} }
qe->has_kernel_gs_base = 1; qe->has_kernel_gs_base = 1;
states = g_ptr_array_new();
for (nhdr = start; nhdr < end; nhdr = nhdr_get_next(nhdr)) { for (nhdr = start; nhdr < end; nhdr = nhdr_get_next(nhdr)) {
if (!strcmp(nhdr_get_name(nhdr), QEMU_NOTE_NAME)) { if (!strcmp(nhdr_get_name(nhdr), QEMU_NOTE_NAME)) {
QEMUCPUState *state = nhdr_get_desc(nhdr); QEMUCPUState *state = nhdr_get_desc(nhdr);
if (state->size < sizeof(*state)) { if (state->size < sizeof(*state)) {
eprintf("CPU #%zu: QEMU CPU state size %u doesn't match\n", eprintf("CPU #%u: QEMU CPU state size %u doesn't match\n",
cpu_nr, state->size); states->len, state->size);
/* /*
* We assume either every QEMU CPU state has KERNEL_GS_BASE or * We assume either every QEMU CPU state has KERNEL_GS_BASE or
* no one has. * no one has.
*/ */
qe->has_kernel_gs_base = 0; qe->has_kernel_gs_base = 0;
} }
cpu_nr++; g_ptr_array_add(states, state);
} }
} }
printf("%zu CPU states has been found\n", cpu_nr); printf("%u CPU states has been found\n", states->len);
qe->state = g_new(QEMUCPUState*, cpu_nr); qe->state_nr = states->len;
qe->state = (void *)g_ptr_array_free(states, FALSE);
cpu_nr = 0;
for (nhdr = start; nhdr < end; nhdr = nhdr_get_next(nhdr)) {
if (!strcmp(nhdr_get_name(nhdr), QEMU_NOTE_NAME)) {
qe->state[cpu_nr] = nhdr_get_desc(nhdr);
cpu_nr++;
}
}
qe->state_nr = cpu_nr;
return true; return true;
} }