From patchwork Thu Dec 20 01:24:37 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Viktor Prutyanov X-Patchwork-Id: 10738439 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 8326A1399 for ; Thu, 20 Dec 2018 01:28:59 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6F6EE2860B for ; Thu, 20 Dec 2018 01:28:59 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5ED762861D; Thu, 20 Dec 2018 01:28:59 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.7 required=2.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id EB3152860B for ; Thu, 20 Dec 2018 01:28:58 +0000 (UTC) Received: from localhost ([::1]:34860 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gZn8z-0000Ln-VV for patchwork-qemu-devel@patchwork.kernel.org; Wed, 19 Dec 2018 20:28:58 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:46567) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gZn5U-0005mH-4C for qemu-devel@nongnu.org; Wed, 19 Dec 2018 20:25:21 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gZn5T-0002wG-6S for qemu-devel@nongnu.org; Wed, 19 Dec 2018 20:25:19 -0500 Received: from mail-lf1-x144.google.com ([2a00:1450:4864:20::144]:33526) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gZn5Q-0002fI-EH for qemu-devel@nongnu.org; Wed, 19 Dec 2018 20:25:17 -0500 Received: by mail-lf1-x144.google.com with SMTP id i26so77351lfc.0 for ; Wed, 19 Dec 2018 17:25:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=phystech-edu.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=JAyA7BqK1HhsNSgFJs4dpakarkygPGi9jp2igGrUPWU=; b=xUtc2BoxZaPesH6MXHWpQzPzcFtLsi8JQngBR9d2tMlAlLRmjwFpu+A/yhnWEVD82Y cxOZexYtQDkjb/nYnhV5OwGQCEdOwu+pDEgvNfZ2ROYmL2y/xinyeCvO60gs3VBUDPrr /yzEOHG0iD8zjcPwOsNIKiuAiodDNETLlte7zEZC8Ag1/r1JZeN6lADvJWEO9w85SI1n XVeDwbvb6r+kxcdVqSyNTvNI3/KMqKH22fuyNaqGVr10+Gp1UU+EA4a5OcjUDdJSevD7 Ov+MBJHa65NOuI26N6RszJQIwDE1bVi/Q3Xyu4r82JDPyO4HBWZGuNkssbffqiZRQjCi 3ZxA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=JAyA7BqK1HhsNSgFJs4dpakarkygPGi9jp2igGrUPWU=; b=IQu7XpJ7cm3d7OeKX9ib5TLu8nLa2puDVNPyUIz15RoPZ4/yJuwmZeivAGrMZkJYrY w7Vw46adC+NoUveS0tatwHK1keu8FBSlr/4rzQiQYIHVIuFqvChXI/2Fx4McqpXoV+u5 i8YUWkWikn8xD6rsTwzorLq55uzq4L9Yk5V+yKbPzN2h/jtI/TfwSWJvdTfigOLvpVTc W6g16kdd7P/f+kCzng9RIW9WGIX1QM+dYvmhh9rgIEpo62r5c5b/Zm2jPjq65xsr5L32 k8JdeAHGz0KrlqRTSmpfhr8lC5lIX6WVtwr+S3p67jkoT6ZrOsAxtD7/kDnwZdaIEDxi 6gYw== X-Gm-Message-State: AA+aEWYNZKDcF9nRbWP1HT3MjOfhWyX8+5NThR33ascrCbFwcuI6VYFl 5mPSTpsNtL4A8gd5ZJmkGnIoTQ== X-Google-Smtp-Source: AFSGD/VszncSqyae53o4xwsme3d9qRx8znjhi7T9SX5bwSMVzo9+HLMc59spgHXbGNHKU/gMISxucg== X-Received: by 2002:a19:9904:: with SMTP id b4mr13266364lfe.95.1545269105243; Wed, 19 Dec 2018 17:25:05 -0800 (PST) Received: from localhost.localdomain ([93.175.11.132]) by smtp.gmail.com with ESMTPSA id z7-v6sm3737540lji.42.2018.12.19.17.25.04 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 19 Dec 2018 17:25:04 -0800 (PST) From: Viktor Prutyanov To: pbonzini@redhat.com, rkagan@virtuozzo.com Date: Thu, 20 Dec 2018 04:24:37 +0300 Message-Id: <20181220012441.13694-3-viktor.prutyanov@phystech.edu> X-Mailer: git-send-email 2.17.2 In-Reply-To: <20181220012441.13694-1-viktor.prutyanov@phystech.edu> References: <20181220012441.13694-1-viktor.prutyanov@phystech.edu> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::144 Subject: [Qemu-devel] [PATCH v2 2/6] contrib/elf2dmp: use GLib in ELF processing X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: qemu-devel@nongnu.org, Viktor Prutyanov Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP Replace POSIX mmap with GLib g_mapped_file_new in ELF processing module to make elf2dmp cross-platform. Signed-off-by: Viktor Prutyanov --- contrib/elf2dmp/qemu_elf.c | 27 ++++++++------------------- contrib/elf2dmp/qemu_elf.h | 2 +- 2 files changed, 9 insertions(+), 20 deletions(-) diff --git a/contrib/elf2dmp/qemu_elf.c b/contrib/elf2dmp/qemu_elf.c index e9c0d2534a..0db7816586 100644 --- a/contrib/elf2dmp/qemu_elf.c +++ b/contrib/elf2dmp/qemu_elf.c @@ -120,25 +120,17 @@ static void exit_states(QEMU_Elf *qe) int QEMU_Elf_init(QEMU_Elf *qe, const char *filename) { + GError *gerr = NULL; int err = 0; - struct stat st; - qe->fd = open(filename, O_RDONLY, 0); - if (qe->fd == -1) { - eprintf("Failed to open ELF dump file \'%s\'\n", filename); + qe->gmf = g_mapped_file_new(filename, TRUE, &gerr); + if (gerr) { + eprintf("Failed to map ELF dump file \'%s\'\n", filename); return 1; } - fstat(qe->fd, &st); - qe->size = st.st_size; - - qe->map = mmap(NULL, qe->size, PROT_READ | PROT_WRITE, - MAP_PRIVATE, qe->fd, 0); - if (qe->map == MAP_FAILED) { - eprintf("Failed to map ELF file\n"); - err = 1; - goto out_fd; - } + qe->map = g_mapped_file_get_contents(qe->gmf); + qe->size = g_mapped_file_get_length(qe->gmf); if (init_states(qe)) { eprintf("Failed to extract QEMU CPU states\n"); @@ -149,9 +141,7 @@ int QEMU_Elf_init(QEMU_Elf *qe, const char *filename) return 0; out_unmap: - munmap(qe->map, qe->size); -out_fd: - close(qe->fd); + g_mapped_file_unref(qe->gmf); return err; } @@ -159,6 +149,5 @@ out_fd: void QEMU_Elf_exit(QEMU_Elf *qe) { exit_states(qe); - munmap(qe->map, qe->size); - close(qe->fd); + g_mapped_file_unref(qe->gmf); } diff --git a/contrib/elf2dmp/qemu_elf.h b/contrib/elf2dmp/qemu_elf.h index 19d1299954..fc69606d00 100644 --- a/contrib/elf2dmp/qemu_elf.h +++ b/contrib/elf2dmp/qemu_elf.h @@ -34,7 +34,7 @@ typedef struct QEMUCPUState { int is_system(QEMUCPUState *s); typedef struct QEMU_Elf { - int fd; + GMappedFile *gmf; size_t size; void *map; QEMUCPUState **state;