{"id":813320,"url":"http://patchwork.ozlabs.org/api/1.2/patches/813320/?format=json","web_url":"http://patchwork.ozlabs.org/project/qemu-devel/patch/150529860043.10902.2542204905322127747.stgit@frigg.lan/","project":{"id":14,"url":"http://patchwork.ozlabs.org/api/1.2/projects/14/?format=json","name":"QEMU Development","link_name":"qemu-devel","list_id":"qemu-devel.nongnu.org","list_email":"qemu-devel@nongnu.org","web_url":"","scm_url":"","webscm_url":"","list_archive_url":"","list_archive_url_format":"","commit_url_format":""},"msgid":"<150529860043.10902.2542204905322127747.stgit@frigg.lan>","list_archive_url":null,"date":"2017-09-13T10:30:00","name":"[v6,09/22] instrument: Add basic control interface","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"5788480075a1b83c9fefe392036a622104be0d0a","submitter":{"id":9099,"url":"http://patchwork.ozlabs.org/api/1.2/people/9099/?format=json","name":"Lluís Vilanova","email":"vilanova@ac.upc.edu"},"delegate":null,"mbox":"http://patchwork.ozlabs.org/project/qemu-devel/patch/150529860043.10902.2542204905322127747.stgit@frigg.lan/mbox/","series":[{"id":2857,"url":"http://patchwork.ozlabs.org/api/1.2/series/2857/?format=json","web_url":"http://patchwork.ozlabs.org/project/qemu-devel/list/?series=2857","date":"2017-09-13T09:53:43","name":"instrument: Add basic event instrumentation","version":6,"mbox":"http://patchwork.ozlabs.org/series/2857/mbox/"}],"comments":"http://patchwork.ozlabs.org/api/patches/813320/comments/","check":"pending","checks":"http://patchwork.ozlabs.org/api/patches/813320/checks/","tags":{},"related":[],"headers":{"Return-Path":"<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>","X-Original-To":"incoming@patchwork.ozlabs.org","Delivered-To":"patchwork-incoming@bilbo.ozlabs.org","Authentication-Results":"ozlabs.org;\n\tspf=pass (mailfrom) smtp.mailfrom=nongnu.org\n\t(client-ip=2001:4830:134:3::11; helo=lists.gnu.org;\n\tenvelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org;\n\treceiver=<UNKNOWN>)","Received":["from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11])\n\t(using TLSv1 with cipher AES256-SHA (256/256 bits))\n\t(No client certificate requested)\n\tby ozlabs.org (Postfix) with ESMTPS id 3xsdGl3D65z9s7M\n\tfor <incoming@patchwork.ozlabs.org>;\n\tWed, 13 Sep 2017 20:30:39 +1000 (AEST)","from localhost ([::1]:41399 helo=lists.gnu.org)\n\tby lists.gnu.org with esmtp (Exim 4.71) (envelope-from\n\t<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>)\n\tid 1ds4wH-0007S1-Gn\n\tfor incoming@patchwork.ozlabs.org; Wed, 13 Sep 2017 06:30:37 -0400","from eggs.gnu.org ([2001:4830:134:3::10]:51945)\n\tby lists.gnu.org with esmtp (Exim 4.71)\n\t(envelope-from <vilanova@ac.upc.edu>) id 1ds4vw-0007Rd-NH\n\tfor qemu-devel@nongnu.org; Wed, 13 Sep 2017 06:30:18 -0400","from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)\n\t(envelope-from <vilanova@ac.upc.edu>) id 1ds4vr-0002UV-L3\n\tfor qemu-devel@nongnu.org; Wed, 13 Sep 2017 06:30:16 -0400","from roura.ac.upc.es ([147.83.33.10]:48966)\n\tby eggs.gnu.org with esmtp (Exim 4.71)\n\t(envelope-from <vilanova@ac.upc.edu>) id 1ds4vr-0002TO-38\n\tfor qemu-devel@nongnu.org; Wed, 13 Sep 2017 06:30:11 -0400","from correu-2.ac.upc.es (correu-2.ac.upc.es [147.83.30.92])\n\tby roura.ac.upc.es (8.13.8/8.13.8) with ESMTP id v8DAU7et008742;\n\tWed, 13 Sep 2017 12:30:07 +0200","from localhost (unknown [132.68.137.204])\n\tby correu-2.ac.upc.es (Postfix) with ESMTPSA id C89FF39C;\n\tWed, 13 Sep 2017 12:30:01 +0200 (CEST)"],"From":"=?utf-8?b?TGx1w61z?= Vilanova <vilanova@ac.upc.edu>","To":"qemu-devel@nongnu.org","Date":"Wed, 13 Sep 2017 13:30:00 +0300","Message-Id":"<150529860043.10902.2542204905322127747.stgit@frigg.lan>","X-Mailer":"git-send-email 2.14.1","In-Reply-To":"<150529642278.10902.18234057937634437857.stgit@frigg.lan>","References":"<150529642278.10902.18234057937634437857.stgit@frigg.lan>","User-Agent":"StGit/0.18","MIME-Version":"1.0","Content-Type":"text/plain; charset=\"utf-8\"","Content-Transfer-Encoding":"quoted-printable","X-MIME-Autoconverted":"from 8bit to quoted-printable by roura.ac.upc.es id\n\tv8DAU7et008742","X-detected-operating-system":"by eggs.gnu.org: GNU/Linux 2.6.x [fuzzy]","X-Received-From":"147.83.33.10","Subject":"[Qemu-devel] [PATCH v6 09/22] instrument: Add basic control\n\tinterface","X-BeenThere":"qemu-devel@nongnu.org","X-Mailman-Version":"2.1.21","Precedence":"list","List-Id":"<qemu-devel.nongnu.org>","List-Unsubscribe":"<https://lists.nongnu.org/mailman/options/qemu-devel>,\n\t<mailto:qemu-devel-request@nongnu.org?subject=unsubscribe>","List-Archive":"<http://lists.nongnu.org/archive/html/qemu-devel/>","List-Post":"<mailto:qemu-devel@nongnu.org>","List-Help":"<mailto:qemu-devel-request@nongnu.org?subject=help>","List-Subscribe":"<https://lists.nongnu.org/mailman/listinfo/qemu-devel>,\n\t<mailto:qemu-devel-request@nongnu.org?subject=subscribe>","Cc":"Markus Armbruster <armbru@redhat.com>, \"Emilio G. Cota\" <cota@braap.org>,\n\tStefan Hajnoczi <stefanha@redhat.com>,\n\tPaolo Bonzini <pbonzini@redhat.com>, =?utf-8?q?Llu=C3=ADs_Vilanova?=\n\t<vilanova@ac.upc.edu>","Errors-To":"qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org","Sender":"\"Qemu-devel\"\n\t<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>"},"content":"Signed-off-by: Lluís Vilanova <vilanova@ac.upc.edu>\n---\n Makefile                        |    4 +++\n configure                       |    1 +\n include/qemu/compiler.h         |   19 ++++++++++++++++\n instrument/Makefile.objs        |    1 +\n instrument/control.c            |   28 ++++++++++++++++++++++++\n instrument/control.h            |   44 +++++++++++++++++++++++++++++++++++++\n instrument/control.inc.h        |   25 +++++++++++++++++++++\n instrument/error.h              |   28 ++++++++++++++++++++++++\n instrument/events.h             |   37 +++++++++++++++++++++++++++++++\n instrument/events.inc.h         |   11 +++++++++\n instrument/load.c               |   13 +++++++++++\n instrument/qemu-instr/control.h |   46 +++++++++++++++++++++++++++++++++++++++\n stubs/instrument.c              |    4 +++\n 13 files changed, 261 insertions(+)\n create mode 100644 instrument/control.c\n create mode 100644 instrument/control.h\n create mode 100644 instrument/control.inc.h\n create mode 100644 instrument/error.h\n create mode 100644 instrument/events.h\n create mode 100644 instrument/events.inc.h\n create mode 100644 instrument/qemu-instr/control.h","diff":"diff --git a/Makefile b/Makefile\nindex 3861b3f49c..c3d9a4bcd9 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -599,6 +599,10 @@ ifdef CONFIG_VIRTFS\n \t$(INSTALL_DIR) \"$(DESTDIR)$(mandir)/man1\"\n \t$(INSTALL_DATA) fsdev/virtfs-proxy-helper.1 \"$(DESTDIR)$(mandir)/man1\"\n endif\n+ifdef CONFIG_INSTRUMENT\n+\t$(INSTALL_DIR) \"$(DESTDIR)$(includedir)/qemu-instr/\"\n+\t$(INSTALL_DATA) $(SRC_PATH)/instrument/qemu-instr/control.h \"$(DESTDIR)$(includedir)/qemu-instr/\"\n+endif\n \n install-datadir:\n \t$(INSTALL_DIR) \"$(DESTDIR)$(qemu_datadir)\"\ndiff --git a/configure b/configure\nindex 5175151317..18810eae84 100755\n--- a/configure\n+++ b/configure\n@@ -6030,6 +6030,7 @@ if test \"$instrument\" = \"yes\"; then\n   LIBS=\"-ldl $LIBS\"\n   echo \"CONFIG_INSTRUMENT=y\" >> $config_host_mak\n fi\n+QEMU_INCLUDES=\"-I\\$(SRC_PATH)/instrument $QEMU_INCLUDES\"\n \n if test \"$rdma\" = \"yes\" ; then\n   echo \"CONFIG_RDMA=y\" >> $config_host_mak\ndiff --git a/include/qemu/compiler.h b/include/qemu/compiler.h\nindex 340e5fdc09..e86bd34e2c 100644\n--- a/include/qemu/compiler.h\n+++ b/include/qemu/compiler.h\n@@ -111,4 +111,23 @@\n #define GCC_FMT_ATTR(n, m)\n #endif\n \n+/*\n+ * Export symbol to dlopen()'ed libraries'.\n+ *\n+ * This code is taken from http://gcc.gnu.org/wiki/Visibility.\n+ */\n+#if defined _WIN32 || defined __CYGWIN__\n+  #ifdef __GNUC__\n+    #define SYM_PUBLIC __attribute__ ((dllimport))\n+  #else\n+    #define SYM_PUBLIC __declspec(dllimport)\n+  #endif\n+#else\n+  #if __GNUC__ >= 4\n+    #define SYM_PUBLIC __attribute__ ((visibility(\"default\")))\n+  #else\n+    #define SYM_PUBLIC\n+  #endif\n+#endif\n+\n #endif /* COMPILER_H */\ndiff --git a/instrument/Makefile.objs b/instrument/Makefile.objs\nindex 7bf4e27e3c..ec76b2080b 100644\n--- a/instrument/Makefile.objs\n+++ b/instrument/Makefile.objs\n@@ -3,3 +3,4 @@\n target-obj-$(CONFIG_INSTRUMENT) += cmdline.o\n target-obj-$(CONFIG_INSTRUMENT) += load.o\n target-obj-$(CONFIG_INSTRUMENT) += qmp.o\n+target-obj-$(CONFIG_INSTRUMENT) += control.o\ndiff --git a/instrument/control.c b/instrument/control.c\nnew file mode 100644\nindex 0000000000..3630d6b3be\n--- /dev/null\n+++ b/instrument/control.c\n@@ -0,0 +1,28 @@\n+/*\n+ * Control instrumentation during program (de)initialization.\n+ *\n+ * Copyright (C) 2012-2017 Lluís Vilanova <vilanova@ac.upc.edu>\n+ *\n+ * This work is licensed under the terms of the GNU GPL, version 2 or later.\n+ * See the COPYING file in the top-level directory.\n+ */\n+\n+#include \"instrument/control.h\"\n+#include \"instrument/error.h\"\n+#include \"instrument/events.h\"\n+#include \"instrument/load.h\"\n+#include \"instrument/qemu-instr/control.h\"\n+#include \"qemu/compiler.h\"\n+\n+__thread InstrState instr_cur_state;\n+\n+\n+qi_fini_fn instr_event__fini_fn;\n+void *instr_event__fini_data;\n+\n+SYM_PUBLIC void qi_set_fini(qi_fini_fn fn, void *data)\n+{\n+    ERROR_IF(!instr_get_state(), \"called outside instrumentation\");\n+    instr_set_event(fini_fn, fn);\n+    instr_set_event(fini_data, data);\n+}\ndiff --git a/instrument/control.h b/instrument/control.h\nnew file mode 100644\nindex 0000000000..f2b085f69b\n--- /dev/null\n+++ b/instrument/control.h\n@@ -0,0 +1,44 @@\n+/*\n+ * Control instrumentation during program (de)initialization.\n+ *\n+ * Copyright (C) 2012-2017 Lluís Vilanova <vilanova@ac.upc.edu>\n+ *\n+ * This work is licensed under the terms of the GNU GPL, version 2 or later.\n+ * See the COPYING file in the top-level directory.\n+ */\n+\n+#ifndef INSTRUMENT__CONTROL_H\n+#define INSTRUMENT__CONTROL_H\n+\n+\n+/**\n+ * InstrState:\n+ * @INSTR_STATE_DISABLE: Intrumentation API not available.\n+ * @INSTR_STATE_ENABLE: Intrumentation API available.\n+ *\n+ * Instrumentation state of current host thread. Used to ensure instrumentation\n+ * clients use QEMU's API only in expected points.\n+ */\n+typedef enum {\n+    INSTR_STATE_DISABLE,\n+    INSTR_STATE_ENABLE,\n+} InstrState;\n+\n+/**\n+ * instr_set_state:\n+ *\n+ * Set the instrumentation state of the current host thread.\n+ */\n+static inline void instr_set_state(InstrState state);\n+\n+/**\n+ * instr_get_state:\n+ *\n+ * Get the instrumentation state of the current host thread.\n+ */\n+static inline InstrState instr_get_state(void);\n+\n+\n+#include \"instrument/control.inc.h\"\n+\n+#endif  /* INSTRUMENT__CONTROL_H */\ndiff --git a/instrument/control.inc.h b/instrument/control.inc.h\nnew file mode 100644\nindex 0000000000..0f649f4caa\n--- /dev/null\n+++ b/instrument/control.inc.h\n@@ -0,0 +1,25 @@\n+/*\n+ * Control instrumentation during program (de)initialization.\n+ *\n+ * Copyright (C) 2012-2017 Lluís Vilanova <vilanova@ac.upc.edu>\n+ *\n+ * This work is licensed under the terms of the GNU GPL, version 2 or later.\n+ * See the COPYING file in the top-level directory.\n+ */\n+\n+#include \"qemu/atomic.h\"\n+#include \"qemu/compiler.h\"\n+#include <stdbool.h>\n+\n+\n+extern __thread InstrState instr_cur_state;\n+\n+static inline void instr_set_state(InstrState state)\n+{\n+    atomic_store_release(&instr_cur_state, state);\n+}\n+\n+static inline InstrState instr_get_state(void)\n+{\n+    return atomic_load_acquire(&instr_cur_state);\n+}\ndiff --git a/instrument/error.h b/instrument/error.h\nnew file mode 100644\nindex 0000000000..f8d1dd4b16\n--- /dev/null\n+++ b/instrument/error.h\n@@ -0,0 +1,28 @@\n+/*\n+ * Helpers for controlling errors in instrumentation libraries.\n+ *\n+ * Copyright (C) 2012-2017 Lluís Vilanova <vilanova@ac.upc.edu>\n+ *\n+ * This work is licensed under the terms of the GNU GPL, version 2 or later.\n+ * See the COPYING file in the top-level directory.\n+ */\n+\n+#ifndef INSTRUMENT_ERROR_H\n+#define INSTRUMENT_ERROR_H\n+\n+#include \"qemu/osdep.h\"\n+#include \"qemu/error-report.h\"\n+\n+\n+#define _ERROR(msg, args...)                            \\\n+    do {                                                \\\n+        error_report(\"%s:\" msg, __func__, ##args);      \\\n+    } while (0)\n+\n+#define ERROR_IF(cond, msg, args...) \\\n+    if (unlikely(cond)) {            \\\n+        _ERROR(msg, ##args);         \\\n+        return;                      \\\n+    }\n+\n+#endif  /* INSTRUMENT_ERROR_H */\ndiff --git a/instrument/events.h b/instrument/events.h\nnew file mode 100644\nindex 0000000000..82ad0bd827\n--- /dev/null\n+++ b/instrument/events.h\n@@ -0,0 +1,37 @@\n+/*\n+ * Internal API for triggering instrumentation events.\n+ *\n+ * Copyright (C) 2017 Lluís Vilanova <vilanova@ac.upc.edu>\n+ *\n+ * This work is licensed under the terms of the GNU GPL, version 2 or later.\n+ * See the COPYING file in the top-level directory.\n+ */\n+\n+#ifndef INSTRUMENT__EVENTS_H\n+#define INSTRUMENT__EVENTS_H\n+\n+#include \"instrument/qemu-instr/control.h\"\n+\n+/**\n+ * instr_get_event:\n+ *\n+ * Get value set by instrumentation library.\n+ */\n+#define instr_get_event(name)                   \\\n+    atomic_load_acquire(&instr_event__ ## name)\n+\n+/**\n+ * instr_get_event:\n+ *\n+ * Set value from instrumentation library.\n+ */\n+#define instr_set_event(name, fn)               \\\n+    atomic_store_release(&instr_event__ ## name, fn)\n+\n+\n+extern qi_fini_fn instr_event__fini_fn;\n+extern void *instr_event__fini_data;\n+\n+#include \"instrument/events.inc.h\"\n+\n+#endif  /* INSTRUMENT__EVENTS_H */\ndiff --git a/instrument/events.inc.h b/instrument/events.inc.h\nnew file mode 100644\nindex 0000000000..8b1ce7fcb2\n--- /dev/null\n+++ b/instrument/events.inc.h\n@@ -0,0 +1,11 @@\n+/*\n+ * Internal API for triggering instrumentation events.\n+ *\n+ * Copyright (C) 2017 Lluís Vilanova <vilanova@ac.upc.edu>\n+ *\n+ * This work is licensed under the terms of the GNU GPL, version 2 or later.\n+ * See the COPYING file in the top-level directory.\n+ */\n+\n+\n+\ndiff --git a/instrument/load.c b/instrument/load.c\nindex af98f4ce38..a01d66a4d4 100644\n--- a/instrument/load.c\n+++ b/instrument/load.c\n@@ -11,6 +11,8 @@\n #include \"qemu-common.h\"\n \n #include <dlfcn.h>\n+#include \"instrument/control.h\"\n+#include \"instrument/events.h\"\n #include \"instrument/load.h\"\n #include \"qemu/config-file.h\"\n #include \"qemu/error-report.h\"\n@@ -96,8 +98,11 @@ InstrLoadError instr_load(const char *path, int argc, const char **argv,\n         res = INSTR_LOAD_DLERROR;\n         goto err;\n     }\n+    instr_set_event(fini_fn, NULL);\n \n+    instr_set_state(INSTR_STATE_ENABLE);\n     main_res = main_cb(argc, argv);\n+    instr_set_state(INSTR_STATE_DISABLE);\n \n     if (main_res != 0) {\n         res = INSTR_LOAD_ERROR;\n@@ -126,6 +131,14 @@ InstrUnloadError instr_unload(const char *id)\n         goto out;\n     }\n \n+    qi_fini_fn fini_fn = instr_get_event(fini_fn);\n+    if (fini_fn) {\n+        void *fini_data = instr_get_event(fini_data);\n+        fini_fn(fini_data);\n+    }\n+\n+    instr_set_event(fini_fn, NULL);\n+\n     /* this should never fail */\n     if (dlclose(handle->dlhandle) < 0) {\n         res = INSTR_UNLOAD_DLERROR;\ndiff --git a/instrument/qemu-instr/control.h b/instrument/qemu-instr/control.h\nnew file mode 100644\nindex 0000000000..b841afaa31\n--- /dev/null\n+++ b/instrument/qemu-instr/control.h\n@@ -0,0 +1,46 @@\n+/*\n+ * Main instrumentation interface for QEMU.\n+ *\n+ * Copyright (C) 2012-2017 Lluís Vilanova <vilanova@ac.upc.edu>\n+ *\n+ * This work is licensed under the terms of the GNU GPL, version 2 or later.\n+ * See the COPYING file in the top-level directory.\n+ */\n+\n+#ifndef QI__CONTROL_H\n+#define QI__CONTROL_H\n+\n+#ifdef __cplusplus\n+extern \"C\" {\n+#endif\n+\n+#include <stdbool.h>\n+#include <stddef.h>\n+\n+\n+/**\n+ * SECTION:control\n+ * @section_id: qi-control\n+ * @title: Event control API for QEMU event instrumentation\n+ */\n+\n+typedef void (*qi_fini_fn)(void *arg);\n+\n+/**\n+ * qi_set_fini:\n+ * @fn: Finalization function.\n+ * @data: Argument to pass to the finalization function.\n+ *\n+ * Set the function to call when finalizing (unloading) the instrumentation\n+ * library.\n+ *\n+ * NOTE: Calls to printf() might not be shown if the library is unloaded when\n+ *       QEMU terminates.\n+ */\n+void qi_set_fini(qi_fini_fn fn, void *data);\n+\n+#ifdef __cplusplus\n+}\n+#endif\n+\n+#endif  /* QI__CONTROL_H */\ndiff --git a/stubs/instrument.c b/stubs/instrument.c\nindex 292a2cdf26..560844469c 100644\n--- a/stubs/instrument.c\n+++ b/stubs/instrument.c\n@@ -10,6 +10,7 @@\n #include \"qemu/osdep.h\"\n \n #include \"instrument/cmdline.h\"\n+#include \"instrument/control.h\"\n #include \"qapi/error.h\"\n #include \"qapi/qmp/qerror.h\"\n \n@@ -38,3 +39,6 @@ void qmp_instr_unload(const char *id, Error **errp)\n {\n     error_setg(errp, QERR_UNSUPPORTED);\n }\n+\n+\n+__thread InstrState instr_cur_state;\n","prefixes":["v6","09/22"]}