{"id":810744,"url":"http://patchwork.ozlabs.org/api/patches/810744/?format=json","web_url":"http://patchwork.ozlabs.org/project/qemu-devel/patch/150472074219.24907.5510718414753398145.stgit@frigg.lan/","project":{"id":14,"url":"http://patchwork.ozlabs.org/api/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":"<150472074219.24907.5510718414753398145.stgit@frigg.lan>","list_archive_url":null,"date":"2017-09-06T17:59:02","name":"[v4,09/20] instrument: Add basic control interface","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"91a14a64c47bb4a3393862b2cc44bc5a3144e12b","submitter":{"id":9099,"url":"http://patchwork.ozlabs.org/api/people/9099/?format=json","name":"Lluís Vilanova","email":"vilanova@ac.upc.edu"},"delegate":null,"mbox":"http://patchwork.ozlabs.org/project/qemu-devel/patch/150472074219.24907.5510718414753398145.stgit@frigg.lan/mbox/","series":[{"id":1859,"url":"http://patchwork.ozlabs.org/api/series/1859/?format=json","web_url":"http://patchwork.ozlabs.org/project/qemu-devel/list/?series=1859","date":"2017-09-06T17:22:41","name":"instrument: Add basic event instrumentation","version":4,"mbox":"http://patchwork.ozlabs.org/series/1859/mbox/"}],"comments":"http://patchwork.ozlabs.org/api/patches/810744/comments/","check":"pending","checks":"http://patchwork.ozlabs.org/api/patches/810744/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 3xnWZL08bxz9t2d\n\tfor <incoming@patchwork.ozlabs.org>;\n\tThu,  7 Sep 2017 03:59:54 +1000 (AEST)","from localhost ([::1]:37348 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 1dpecC-0003Td-41\n\tfor incoming@patchwork.ozlabs.org; Wed, 06 Sep 2017 13:59:52 -0400","from eggs.gnu.org ([2001:4830:134:3::10]:44471)\n\tby lists.gnu.org with esmtp (Exim 4.71)\n\t(envelope-from <vilanova@ac.upc.edu>) id 1dpebf-0003RH-96\n\tfor qemu-devel@nongnu.org; Wed, 06 Sep 2017 13:59:21 -0400","from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)\n\t(envelope-from <vilanova@ac.upc.edu>) id 1dpebZ-0007p4-KB\n\tfor qemu-devel@nongnu.org; Wed, 06 Sep 2017 13:59:19 -0400","from roura.ac.upc.es ([147.83.33.10]:38567)\n\tby eggs.gnu.org with esmtp (Exim 4.71)\n\t(envelope-from <vilanova@ac.upc.edu>) id 1dpebZ-0007no-0L\n\tfor qemu-devel@nongnu.org; Wed, 06 Sep 2017 13:59:13 -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 v86Hx9Jr004576;\n\tWed, 6 Sep 2017 19:59:09 +0200","from localhost (unknown [31.210.187.58])\n\tby correu-2.ac.upc.es (Postfix) with ESMTPSA id ACAA5323;\n\tWed,  6 Sep 2017 19:59:03 +0200 (CEST)"],"From":"=?utf-8?b?TGx1w61z?= Vilanova <vilanova@ac.upc.edu>","To":"qemu-devel@nongnu.org","Date":"Wed,  6 Sep 2017 20:59:02 +0300","Message-Id":"<150472074219.24907.5510718414753398145.stgit@frigg.lan>","X-Mailer":"git-send-email 2.14.1","In-Reply-To":"<150471856141.24907.274176769201097378.stgit@frigg.lan>","References":"<150471856141.24907.274176769201097378.stgit@frigg.lan>","User-Agent":"StGit/0.17.1-dirty","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\tv86Hx9Jr004576","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 v4 09/20] 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":"Paolo Bonzini <pbonzini@redhat.com>, \"Emilio G. Cota\" <cota@braap.org>,\n\t=?utf-8?q?Llu=C3=ADs_Vilanova?= <vilanova@ac.upc.edu>,\n\tStefan Hajnoczi <stefanha@redhat.com>","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                           |    5 +++\n configure                          |    1 +\n instrument/Makefile.objs           |    2 +\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    |   43 +++++++++++++++++++++++++++\n instrument/qemu-instr/visibility.h |   58 ++++++++++++++++++++++++++++++++++++\n stubs/Makefile.objs                |    1 +\n stubs/instrument.c                 |   13 ++++++++\n 14 files changed, 309 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\n create mode 100644 instrument/qemu-instr/visibility.h\n create mode 100644 stubs/instrument.c","diff":"diff --git a/Makefile b/Makefile\nindex 81447b1f08..6171661458 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -589,6 +589,11 @@ 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+\t$(INSTALL_DATA) $(SRC_PATH)/instrument/qemu-instr/visibility.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 05bd7b1950..3673fc9058 100755\n--- a/configure\n+++ b/configure\n@@ -6038,6 +6038,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/instrument/Makefile.objs b/instrument/Makefile.objs\nindex 13a8f60431..9b7e1c03aa 100644\n--- a/instrument/Makefile.objs\n+++ b/instrument/Makefile.objs\n@@ -3,3 +3,5 @@\n target-obj-y += cmdline.o\n target-obj-$(CONFIG_INSTRUMENT) += load.o\n target-obj-y += qmp.o\n+\n+target-obj-$(CONFIG_INSTRUMENT) += control.o\ndiff --git a/instrument/control.c b/instrument/control.c\nnew file mode 100644\nindex 0000000000..2c2781beeb\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 \"instrument/qemu-instr/visibility.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+QI_VPUBLIC 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 a57401102a..e180f03429 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@@ -105,8 +107,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@@ -136,6 +141,14 @@ InstrUnloadError instr_unload(int64_t handle_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..f6e289daa0\n--- /dev/null\n+++ b/instrument/qemu-instr/control.h\n@@ -0,0 +1,43 @@\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+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/instrument/qemu-instr/visibility.h b/instrument/qemu-instr/visibility.h\nnew file mode 100644\nindex 0000000000..305dddf7d8\n--- /dev/null\n+++ b/instrument/qemu-instr/visibility.h\n@@ -0,0 +1,58 @@\n+/*\n+ * Macros for symbol visibility.\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 of QEMU.\n+ */\n+\n+#ifndef QI__VISIBILITY_H\n+#define QI__VISIBILITY_H\n+\n+#ifdef __cplusplus\n+extern \"C\" {\n+#endif\n+\n+/**\n+ * SECTION:visibility\n+ * @section_id: qi-visibility\n+ * @title: Symbol visibility\n+ *\n+ * This code is taken from http://gcc.gnu.org/wiki/Visibility.\n+ */\n+\n+/**\n+ * QI_VPUBLIC:\n+ *\n+ * Make an element public to user's instrumentation code.\n+ */\n+\n+/**\n+ * QI_VLOCAL:\n+ *\n+ * Make an element not visible to user's instrumentation code.\n+ */\n+\n+#if defined _WIN32 || defined __CYGWIN__\n+  #ifdef __GNUC__\n+    #define QI_VPUBLIC __attribute__ ((dllimport))\n+  #else\n+    #define QI_VPUBLIC __declspec(dllimport)\n+  #endif\n+  #define QI_VLOCAL\n+#else\n+  #if __GNUC__ >= 4\n+    #define QI_VPUBLIC __attribute__ ((visibility (\"default\")))\n+    #define QI_VLOCAL  __attribute__ ((visibility (\"hidden\")))\n+  #else\n+    #define QI_VPUBLIC\n+    #define QI_VLOCAL\n+  #endif\n+#endif\n+\n+#ifdef __cplusplus\n+}\n+#endif\n+\n+#endif  /* QI__VISIBILITY_H */\ndiff --git a/stubs/Makefile.objs b/stubs/Makefile.objs\nindex e69c217aff..3aaec2d9dd 100644\n--- a/stubs/Makefile.objs\n+++ b/stubs/Makefile.objs\n@@ -13,6 +13,7 @@ stub-obj-y += error-printf.o\n stub-obj-y += fdset.o\n stub-obj-y += gdbstub.o\n stub-obj-y += get-vm-name.o\n+stub-obj-y += instrument.o\n stub-obj-y += iothread.o\n stub-obj-y += iothread-lock.o\n stub-obj-y += is-daemonized.o\ndiff --git a/stubs/instrument.c b/stubs/instrument.c\nnew file mode 100644\nindex 0000000000..6731710fd5\n--- /dev/null\n+++ b/stubs/instrument.c\n@@ -0,0 +1,13 @@\n+/*\n+ * Instrumentation placeholders.\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+#include \"instrument/control.h\"\n+\n+\n+__thread InstrState instr_cur_state;\n","prefixes":["v4","09/20"]}