This is a hack to allow us to name malloc'd buffers that are backed by anonymous mmap allocations.
Quite useful in my debugging, probably not quite appropriate for upstream unless we can figure out a more robust method for identifying when we get an mmap'd page from malloc and adjusting the pointer offset accordingly - I am currently subtracting a fixed 16 bytes because that "works on my system", and I have no clue if that's appropriate everywhere or not. At least this way, my trick is published to the list for posterity. Signed-off-by: John Snow <[email protected]> --- include/qemu/bitmap.h | 6 ++++++ include/qemu/mmap-vma.h | 3 +++ hw/core/loader.c | 3 +++ hw/i386/x86-common.c | 3 +++ system/device_tree.c | 2 ++ system/physmem.c | 8 ++++++-- util/mmap-vma.c | 23 +++++++++++++++++++++++ 7 files changed, 46 insertions(+), 2 deletions(-) diff --git a/include/qemu/bitmap.h b/include/qemu/bitmap.h index 0044333cb51..6681c87737f 100644 --- a/include/qemu/bitmap.h +++ b/include/qemu/bitmap.h @@ -104,6 +104,12 @@ static inline unsigned long *bitmap_try_new(long nbits) return g_try_new0(unsigned long, nelem); } +static inline size_t bitmap_size(long nbits) +{ + long nelem = BITS_TO_LONGS(nbits); + return sizeof(unsigned long) * nelem; +} + static inline unsigned long *bitmap_new(long nbits) { long nelem = BITS_TO_LONGS(nbits); diff --git a/include/qemu/mmap-vma.h b/include/qemu/mmap-vma.h index 763ea5a5cab..4646657022c 100644 --- a/include/qemu/mmap-vma.h +++ b/include/qemu/mmap-vma.h @@ -15,4 +15,7 @@ int qemu_name_vma(void *ptr, size_t sz, const char *fmt, ...) G_GNUC_PRINTF(3, 4); +int qemu_name_buffer(void *ptr, size_t sz, const char *fmt, ...) + G_GNUC_PRINTF(3, 4); + #endif diff --git a/hw/core/loader.c b/hw/core/loader.c index 5cbfba0a86d..cc0e0d6979a 100644 --- a/hw/core/loader.c +++ b/hw/core/loader.c @@ -45,6 +45,7 @@ #include "qemu/osdep.h" #include "qemu/datadir.h" #include "qemu/error-report.h" +#include "qemu/mmap-vma.h" #include "qapi/error.h" #include "qapi/qapi-commands-machine.h" #include "qapi/type-helpers.h" @@ -1111,6 +1112,8 @@ ssize_t rom_add_file(const char *file, const char *fw_dir, goto err; } + qemu_name_buffer(rom->data, size, "%s", file); + if (fw_dir) { rom->fw_dir = g_strdup(fw_dir); rom->fw_file = g_strdup(file); diff --git a/hw/i386/x86-common.c b/hw/i386/x86-common.c index dcd4fa8eb79..608c4cf4202 100644 --- a/hw/i386/x86-common.c +++ b/hw/i386/x86-common.c @@ -25,6 +25,7 @@ #include "qemu/cutils.h" #include "qemu/units.h" #include "qemu/datadir.h" +#include "qemu/mmap-vma.h" #include "qapi/error.h" #include "system/numa.h" #include "system/system.h" @@ -914,6 +915,8 @@ void x86_load_linux(X86MachineState *x86ms, setup = g_malloc(setup_size); kernel = g_malloc(kernel_size); + qemu_name_buffer(kernel, kernel_size, "%s", kernel_filename); + fseek(f, 0, SEEK_SET); if (fread(setup, 1, setup_size, f) != setup_size) { fprintf(stderr, "fread() failed\n"); diff --git a/system/device_tree.c b/system/device_tree.c index 48365435ea0..816874fb299 100644 --- a/system/device_tree.c +++ b/system/device_tree.c @@ -27,6 +27,7 @@ #include "hw/core/loader.h" #include "hw/core/boards.h" #include "qemu/config-file.h" +#include "qemu/mmap-vma.h" #include "qapi/qapi-commands-machine.h" #include "qobject/qdict.h" @@ -41,6 +42,7 @@ void *create_device_tree(int *sizep) *sizep = FDT_MAX_SIZE; fdt = g_malloc0(FDT_MAX_SIZE); + qemu_name_buffer(fdt, FDT_MAX_SIZE, "device_tree"); ret = fdt_create(fdt, FDT_MAX_SIZE); if (ret < 0) { goto fail; diff --git a/system/physmem.c b/system/physmem.c index 202e3a5cd00..dc0e9540919 100644 --- a/system/physmem.c +++ b/system/physmem.c @@ -26,6 +26,7 @@ #include "qemu/hbitmap.h" #include "qemu/madvise.h" #include "qemu/lockable.h" +#include "qemu/mmap-vma.h" #ifdef CONFIG_TCG #include "accel/tcg/cpu-ops.h" @@ -2105,7 +2106,7 @@ void qemu_ram_msync(RAMBlock *block, ram_addr_t start, ram_addr_t length) } /* Called with ram_list.mutex held */ -static void dirty_memory_extend(ram_addr_t new_ram_size) +static void dirty_memory_extend(ram_addr_t new_ram_size, const char *nickname) { unsigned int old_num_blocks = ram_list.num_dirty_blocks; unsigned int new_num_blocks = DIV_ROUND_UP(new_ram_size, @@ -2133,6 +2134,9 @@ static void dirty_memory_extend(ram_addr_t new_ram_size) for (j = old_num_blocks; j < new_num_blocks; j++) { new_blocks->blocks[j] = bitmap_new(DIRTY_MEMORY_BLOCK_SIZE); + qemu_name_buffer(new_blocks->blocks[j], + bitmap_size(DIRTY_MEMORY_BLOCK_SIZE), + "bitmap<%s>", nickname); } qatomic_rcu_set(&ram_list.dirty_memory[i], new_blocks); @@ -2254,7 +2258,7 @@ static void ram_block_add(RAMBlock *new_block, Error **errp) } ram_size = (new_block->offset + new_block->max_length) >> TARGET_PAGE_BITS; - dirty_memory_extend(ram_size); + dirty_memory_extend(ram_size, new_block->mr->name); /* Keep the list sorted from biggest to smallest block. Unlike QTAILQ, * QLIST (which has an RCU-friendly variant) does not have insertion at * tail, so save the last element in last_block. diff --git a/util/mmap-vma.c b/util/mmap-vma.c index 46ea98f1134..083400a9c2e 100644 --- a/util/mmap-vma.c +++ b/util/mmap-vma.c @@ -73,3 +73,26 @@ int qemu_name_vma(void *ptr, size_t sz, const char *fmt, ...) return rc; } + +G_GNUC_PRINTF(3, 4) +int qemu_name_buffer(void *ptr, size_t sz, const char *fmt, ...) +{ + /* + * this is hacky as hell, but ptmalloc will occasionally use + * mmap() for large buffers - when it does so, you get a pointer + * to 16 bytes after the actual mmap. This function compensates + * for that to guess at what the real mmap allocation was. + * + * This can be used unconditionally whether or not the allocation + * was actually mmap'd - if it wasn't, this just fails. Yes, it's + * still very ugly. + */ + va_list args; + size_t pagesz = qemu_real_host_page_size(); + + ptr = ptr - 0x10; + sz = ROUND_UP(sz + 0x10, pagesz); + + va_start(args, fmt); + return qemu_vname_vma(ptr, sz, fmt, args); +} -- 2.56.1
