From patchwork Mon Oct 31 20:29:46 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Weil X-Patchwork-Id: 122937 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [140.186.70.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id AD8C8B6F81 for ; Tue, 1 Nov 2011 07:30:58 +1100 (EST) Received: from localhost ([::1]:53551 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RKyVT-0006Y6-7z for incoming@patchwork.ozlabs.org; Mon, 31 Oct 2011 16:30:55 -0400 Received: from eggs.gnu.org ([140.186.70.92]:48161) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RKyVN-0006Xx-NP for qemu-devel@nongnu.org; Mon, 31 Oct 2011 16:30:50 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RKyVM-0003OB-PD for qemu-devel@nongnu.org; Mon, 31 Oct 2011 16:30:49 -0400 Received: from v220110690675601.yourvserver.net ([78.47.199.172]:40186) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RKyVM-0003M9-Kd for qemu-devel@nongnu.org; Mon, 31 Oct 2011 16:30:48 -0400 Received: from localhost (v220110690675601.yourvserver.net.local [127.0.0.1]) by v220110690675601.yourvserver.net (Postfix) with ESMTP id E59BC72834F7; Mon, 31 Oct 2011 21:30:22 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at weilnetz.de Received: from v220110690675601.yourvserver.net ([127.0.0.1]) by localhost (v220110690675601.yourvserver.net [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id aunbbEVQJnDM; Mon, 31 Oct 2011 21:29:51 +0100 (CET) Received: by v220110690675601.yourvserver.net (Postfix, from userid 1000) id DDF1A7283506; Mon, 31 Oct 2011 21:29:51 +0100 (CET) From: Stefan Weil To: qemu-devel@nongnu.org Date: Mon, 31 Oct 2011 21:29:46 +0100 Message-Id: <1320092986-2490-1-git-send-email-sw@weilnetz.de> X-Mailer: git-send-email 1.7.2.5 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 3) X-Received-From: 78.47.199.172 Cc: Stefan Weil , Anthony Liguori , Avi Kivity Subject: [Qemu-devel] [PATCH v2] Support running QEMU on Valgrind X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Valgrind is a tool which can automatically detect many kinds of bugs. Running QEMU on Valgrind with x86_64 hosts was not possible because Valgrind aborts when memalign is called with an alignment larger than 1 MiB. QEMU normally uses 2 MiB on Linux x86_64. Now the alignment is reduced to the page size when QEMU is running on Valgrind. v2: Instead of using the macro RUNNING_ON_VALGRIND from valgrind.h, the patch now uses a hack from libvirt which tests for the pre-loaded vgpreload_*.so shared libraries. This avoids the need for valgrind.h. Signed-off-by: Stefan Weil --- oslib-posix.c | 22 +++++++++++++++++++--- 1 files changed, 19 insertions(+), 3 deletions(-) diff --git a/oslib-posix.c b/oslib-posix.c index dbc8ee8..6f29762 100644 --- a/oslib-posix.c +++ b/oslib-posix.c @@ -36,8 +36,11 @@ extern int daemon(int, int); #endif #if defined(__linux__) && defined(__x86_64__) - /* Use 2MB alignment so transparent hugepages can be used by KVM */ + /* Use 2 MiB alignment so transparent hugepages can be used by KVM. + Valgrind does not support alignments larger than 1 MiB, + therefore we need special code which handles running on Valgrind. */ # define QEMU_VMALLOC_ALIGN (512 * 4096) +# define CONFIG_VALGRIND #else # define QEMU_VMALLOC_ALIGN getpagesize() #endif @@ -47,7 +50,11 @@ extern int daemon(int, int); #include "trace.h" #include "qemu_socket.h" - +#if defined(CONFIG_VALGRIND) +static int running_on_valgrind = -1; +#else +# define running_on_valgrind 0 +#endif int qemu_daemon(int nochdir, int noclose) { @@ -89,7 +96,16 @@ void *qemu_vmalloc(size_t size) void *ptr; size_t align = QEMU_VMALLOC_ALIGN; - if (size < align) { +#if defined(CONFIG_VALGRIND) + if (running_on_valgrind < 0) { + /* First call, test whether we are running on Valgrind. + This is a substitute for RUNNING_ON_VALGRIND from valgrind.h. */ + const char *ld = getenv("LD_PRELOAD"); + running_on_valgrind = (ld != NULL && strstr(ld, "vgpreload")); + } +#endif + + if (size < align || running_on_valgrind) { align = getpagesize(); } ptr = qemu_memalign(align, size);