From nobody Fri Nov 7 12:58:05 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1538755059192684.3299304733983; Fri, 5 Oct 2018 08:57:39 -0700 (PDT) Received: from localhost ([::1]:35739 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g8STy-0007fV-0h for importer@patchew.org; Fri, 05 Oct 2018 11:57:38 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:57107) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g8SMF-0006fM-Fj for qemu-devel@nongnu.org; Fri, 05 Oct 2018 11:49:41 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1g8SM9-0006Ft-Uw for qemu-devel@nongnu.org; Fri, 05 Oct 2018 11:49:39 -0400 Received: from mail-wm1-x343.google.com ([2a00:1450:4864:20::343]:51342) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1g8SM9-00066X-AT for qemu-devel@nongnu.org; Fri, 05 Oct 2018 11:49:33 -0400 Received: by mail-wm1-x343.google.com with SMTP id 143-v6so2304750wmf.1 for ; Fri, 05 Oct 2018 08:49:26 -0700 (PDT) Received: from zen.linaro.local ([81.128.185.34]) by smtp.gmail.com with ESMTPSA id u191-v6sm2598725wmd.31.2018.10.05.08.49.15 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 05 Oct 2018 08:49:22 -0700 (PDT) Received: from zen.linaroharston (localhost [127.0.0.1]) by zen.linaro.local (Postfix) with ESMTP id D316B3E072C; Fri, 5 Oct 2018 16:49:11 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=CSyyGIpdIo/1QHsx/c6muSCJsLQTSUi76xVf3LhjqPQ=; b=GZls9IzR1pQJDLPov7ccz4StWutX1Hqea/jlRyIwJjmFhxzyEa4/8yq6C/kutzXzNS W8whXd8n37bpXdGFw2RWmByPcpauXAHLCJkbfScYRnpBRswT7UPnYrkefSIL9twhAHhW osj5z8DQ0XvcfV9d+SlCkbcprqX8f7lkmW8lw= 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:mime-version:content-transfer-encoding; bh=CSyyGIpdIo/1QHsx/c6muSCJsLQTSUi76xVf3LhjqPQ=; b=rjgcmEbVlE1oAYaTbVamEFpTdN9fPa9Efu52CFQjWqMPDUxRk5/3PmJjpIHQi/J3ge MriR5rEHeo7IDhq6Z4uCfIN69/s/1tDhQOtuqYduQW2EhbgxfBHfiJpOXuQNeS/7KfX5 Hq94ioblUtf5jCxBJU+9LCsit4xugQNXwYY8hpoQFuden8qTbYeCQNKLYVYgRc+fA+by v5mMYv1NUtL7LBUxSGzVssb+i219ZQ6y/b7SL2Dr0HON/6XqE76IuhhWlp13Qu/BAfqq ryCnIrZR+N7Z7nzBUII9EQlWATfRcqjJS703nGkX8B2zmSlhuIan4wf7vcjkcw7akxke ElsA== X-Gm-Message-State: ABuFfojcnYhbW3amn1SZlVmavrv3j9m0Ekuhm++mvNg6fqmPXrVhLE1h ir/Hdebp2H1BvdFauAbp2RwScg== X-Google-Smtp-Source: ACcGV638YoBqGLIA9xeH+Nb7hOGrLeFQzKju0F9EpSwfIiMnbplq1HwGXPwLMd7udDgAz77IbHMVWA== X-Received: by 2002:a1c:9fd5:: with SMTP id i204-v6mr8596286wme.88.1538754565876; Fri, 05 Oct 2018 08:49:25 -0700 (PDT) From: =?UTF-8?q?Alex=20Benn=C3=A9e?= To: qemu-devel@nongnu.org Date: Fri, 5 Oct 2018 16:49:03 +0100 Message-Id: <20181005154910.3099-15-alex.bennee@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181005154910.3099-1-alex.bennee@linaro.org> References: <20181005154910.3099-1-alex.bennee@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::343 Subject: [Qemu-devel] [RFC PATCH 14/21] trace: add support for plugin infrastructure 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: Pavel.Dovgaluk@ispras.ru, cota@braap.org, Stefan Hajnoczi , Paolo Bonzini , =?UTF-8?q?Alex=20Benn=C3=A9e?= , vilanova@ac.upc.edu Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDMRC_1 RDKM_2 RSF_0 Z_629925259 SPT_0 From: Pavel Dovgalyuk This patch adds support for dynamically loaded plugins. Every plugin is a dynamic library with a set of optional exported functions that will be called from QEMU. Signed-off-by: Pavel Dovgalyuk [AJB: moved to using trace point API] Signed-off-by: Alex Benn=C3=A9e --- ajb - moved to tracepoint interface (bind_to_tracepoints) - now in trace/ and include/plugins/ --- include/plugins/plugins.h | 14 ++++ include/qemu/plugins.h | 18 +++++ qemu-options.hx | 10 +++ trace/Makefile.objs | 1 + trace/plugins.c | 139 ++++++++++++++++++++++++++++++++++++++ vl.c | 8 +++ 6 files changed, 190 insertions(+) create mode 100644 include/plugins/plugins.h create mode 100644 include/qemu/plugins.h create mode 100644 trace/plugins.c diff --git a/include/plugins/plugins.h b/include/plugins/plugins.h new file mode 100644 index 0000000000..54ac53cb32 --- /dev/null +++ b/include/plugins/plugins.h @@ -0,0 +1,14 @@ +#ifndef PLUGINS_INTERFACE_H +#define PLUGINS_INTERFACE_H + +#include +#include + +/* Mandatory Plugin interfaces */ + +bool plugin_init(const char *args); +char *plugin_status(void); + +/* Other optional hooks are defined by available trace-points */ + +#endif /* PLUGINS_INTERFACE_H */ diff --git a/include/qemu/plugins.h b/include/qemu/plugins.h new file mode 100644 index 0000000000..c86bb7ae67 --- /dev/null +++ b/include/qemu/plugins.h @@ -0,0 +1,18 @@ +#ifndef PLUGINS_H +#define PLUGINS_H + +#ifdef CONFIG_TRACE_PLUGIN + +void qemu_plugin_parse_cmd_args(const char *optarg); +void qemu_plugin_load(const char *filename, const char *args); +void qemu_plugins_init(void); + +#else + +static inline void qemu_plugin_parse_cmd_args(const char *optarg) { } +static inline void qemu_plugin_load(const char *filename, const char *args= ) { } +static inline void qemu_plugins_init(void) { } + +#endif + +#endif /* PLUGINS_H */ diff --git a/qemu-options.hx b/qemu-options.hx index f139459e80..87206dd79f 100644 --- a/qemu-options.hx +++ b/qemu-options.hx @@ -3892,6 +3892,16 @@ STEXI Enable synchronization profiling. ETEXI =20 +#ifdef CONFIG_TRACE_PLUGIN +DEF("plugin", HAS_ARG, QEMU_OPTION_plugin, \ + "-plugin file=3D[,args=3D] load plugin with <= args>\n", QEMU_ARCH_ALL) +STEXI +@item -plugin file=3D@var{file}[,args=3D@var{args}] +@findex -plugin +Load @var{file} plugin passing @var{args} arguments. +ETEXI +#endif + STEXI @end table ETEXI diff --git a/trace/Makefile.objs b/trace/Makefile.objs index afd571c3ec..4977f654a5 100644 --- a/trace/Makefile.objs +++ b/trace/Makefile.objs @@ -54,6 +54,7 @@ $(obj)/generated-tcg-tracers.h-timestamp: $(SRC_PATH)/tra= ce-events $(BUILD_DIR)/ =20 util-obj-$(CONFIG_TRACE_SIMPLE) +=3D simple.o util-obj-$(CONFIG_TRACE_FTRACE) +=3D ftrace.o +util-obj-$(CONFIG_TRACE_PLUGIN) +=3D plugins.o util-obj-y +=3D control.o target-obj-y +=3D control-target.o util-obj-y +=3D qmp.o diff --git a/trace/plugins.c b/trace/plugins.c new file mode 100644 index 0000000000..25aec2ff70 --- /dev/null +++ b/trace/plugins.c @@ -0,0 +1,139 @@ +/* + * Plugin Support + * + * + * Copyright (c) 2018 Pavel Dovgalyuk + * Copyright (c) 2018 Alex Benn=C3=A9e + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#include "qemu/osdep.h" +#include +#include "qemu-common.h" +#include "qemu/error-report.h" +#include "qemu/plugins.h" +#include "qemu/queue.h" +#include "qemu/option.h" +#include "trace/control.h" + +typedef bool (*PluginInitFunc)(const char *); +typedef char * (*PluginStatusFunc)(void); + +typedef struct QemuPluginInfo { + const char *filename; + const char *args; + GModule *g_module; + + PluginInitFunc init; + PluginStatusFunc status; + + GPtrArray *events; + + QLIST_ENTRY(QemuPluginInfo) next; +} QemuPluginInfo; + +static QLIST_HEAD(, QemuPluginInfo) qemu_plugins + =3D QLIST_HEAD_INITIALIZER(qemu_plugins); + +static QemuOptsList qemu_plugin_opts =3D { + .name =3D "plugin", + .head =3D QTAILQ_HEAD_INITIALIZER(qemu_plugin_opts.head), + .desc =3D { + { + .name =3D "file", + .type =3D QEMU_OPT_STRING, + },{ + .name =3D "args", + .type =3D QEMU_OPT_STRING, + }, + { /* end of list */ } + }, +}; + +void qemu_plugin_parse_cmd_args(const char *optarg) +{ + QemuOpts *opts =3D qemu_opts_parse_noisily(&qemu_plugin_opts, optarg, = false); + qemu_plugin_load(qemu_opt_get(opts, "file"), + qemu_opt_get(opts, "args")); +} + +static int bind_to_tracepoints(GModule *g_module, GPtrArray *events) +{ + int count =3D 0; + TraceEventIter iter; + TraceEvent *ev; + + trace_event_iter_init(&iter, "*"); + while ((ev =3D trace_event_iter_next(&iter)) !=3D NULL) { + const char *name =3D trace_event_get_name(ev); + gpointer fn; + + if (g_module_symbol(g_module, name, &fn)) { + ev->plugin =3D (uintptr_t) fn; + trace_event_set_state_dynamic(ev, true); + count++; + } + } + + return count; +} + +void qemu_plugin_load(const char *filename, const char *args) +{ + GModule *g_module; + QemuPluginInfo *info =3D NULL; + if (!filename) { + error_report("plugin name was not specified"); + return; + } + g_module =3D g_module_open(filename, + G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL); + if (!g_module) { + error_report("can't load plugin '%s'", filename); + return; + } + info =3D g_new0(QemuPluginInfo, 1); + info->filename =3D g_strdup(filename); + info->g_module =3D g_module; + + if (!g_module_symbol(g_module, "plugin_init", + (gpointer *) &info->init)) { + error_report("all plugins must provide a plugin_init hook"); + return; + } + + if (!g_module_symbol(g_module, "plugin_status", + (gpointer *) &info->status)) { + error_report("all plugins must provide a plugin_status hook"); + return; + } + + /* OK we can now see how many events might have bindings */ + info->events =3D g_ptr_array_new(); + + if (bind_to_tracepoints(g_module, info->events) < 0) { + error_report("failed to bind any events"); + return; + } + + /* Save the args, we will initialise later on once everything is + set up */ + if (args) { + info->args =3D g_strdup(args); + } + + QLIST_INSERT_HEAD(&qemu_plugins, info, next); + + return; +} + +void qemu_plugins_init(void) +{ + QemuPluginInfo *info; + QLIST_FOREACH(info, &qemu_plugins, next) { + if (info->init) { + info->init(info->args); + } + } +} diff --git a/vl.c b/vl.c index 795e025445..b46ef5032f 100644 --- a/vl.c +++ b/vl.c @@ -130,6 +130,7 @@ int main(int argc, char **argv) #include "qapi/qapi-commands-run-state.h" #include "qapi/qmp/qerror.h" #include "sysemu/iothread.h" +#include "qemu/plugins.h" =20 #define MAX_VIRTIO_CONSOLES 1 =20 @@ -3884,6 +3885,11 @@ int main(int argc, char **argv, char **envp) case QEMU_OPTION_enable_sync_profile: qsp_enable(); break; +#ifdef CONFIG_TRACE_PLUGIN + case QEMU_OPTION_plugin: + qemu_plugin_parse_cmd_args(optarg); + break; +#endif case QEMU_OPTION_nouserconfig: /* Nothing to be parsed here. Especially, do not error out= below. */ break; @@ -4449,6 +4455,8 @@ int main(int argc, char **argv, char **envp) } parse_numa_opts(current_machine); =20 + qemu_plugins_init(); + /* do monitor/qmp handling at preconfig state if requested */ main_loop(); =20 --=20 2.17.1