diff options
author | Peter Lieven <pl@kamp.de> | 2016-09-27 11:58:45 +0200 |
---|---|---|
committer | Kevin Wolf <kwolf@redhat.com> | 2016-09-29 14:13:39 +0200 |
commit | 7d992e4d5a95d0b21c6c33bd32cef8671805e39b (patch) | |
tree | 3d99c261cb5f7056970c9a7c158c08b2fc7a8d06 /util | |
parent | 2f4aa2329945c741e10fd5b6631e99a4be25ea60 (diff) | |
download | qemu-7d992e4d5a95d0b21c6c33bd32cef8671805e39b.zip qemu-7d992e4d5a95d0b21c6c33bd32cef8671805e39b.tar.gz qemu-7d992e4d5a95d0b21c6c33bd32cef8671805e39b.tar.bz2 |
oslib-posix: add a configure switch to debug stack usage
this adds a knob to track the maximum stack usage of stacks
created by qemu_alloc_stack.
Signed-off-by: Peter Lieven <pl@kamp.de>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
Diffstat (limited to 'util')
-rw-r--r-- | util/oslib-posix.c | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/util/oslib-posix.c b/util/oslib-posix.c index d950c34..aaec189 100644 --- a/util/oslib-posix.c +++ b/util/oslib-posix.c @@ -50,6 +50,10 @@ #include "qemu/mmap-alloc.h" +#ifdef CONFIG_DEBUG_STACK_USAGE +#include "qemu/error-report.h" +#endif + int qemu_get_thread_id(void) { #if defined(__linux__) @@ -503,6 +507,9 @@ pid_t qemu_fork(Error **errp) void *qemu_alloc_stack(size_t *sz) { void *ptr, *guardpage; +#ifdef CONFIG_DEBUG_STACK_USAGE + void *ptr2; +#endif size_t pagesz = getpagesize(); #ifdef _SC_THREAD_STACK_MIN /* avoid stacks smaller than _SC_THREAD_STACK_MIN */ @@ -534,10 +541,38 @@ void *qemu_alloc_stack(size_t *sz) abort(); } +#ifdef CONFIG_DEBUG_STACK_USAGE + for (ptr2 = ptr + pagesz; ptr2 < ptr + *sz; ptr2 += sizeof(uint32_t)) { + *(uint32_t *)ptr2 = 0xdeadbeaf; + } +#endif + return ptr; } +#ifdef CONFIG_DEBUG_STACK_USAGE +static __thread unsigned int max_stack_usage; +#endif + void qemu_free_stack(void *stack, size_t sz) { +#ifdef CONFIG_DEBUG_STACK_USAGE + unsigned int usage; + void *ptr; + + for (ptr = stack + getpagesize(); ptr < stack + sz; + ptr += sizeof(uint32_t)) { + if (*(uint32_t *)ptr != 0xdeadbeaf) { + break; + } + } + usage = sz - (uintptr_t) (ptr - stack); + if (usage > max_stack_usage) { + error_report("thread %d max stack usage increased from %u to %u", + qemu_get_thread_id(), max_stack_usage, usage); + max_stack_usage = usage; + } +#endif + munmap(stack, sz); } |