{"id":813331,"url":"http://patchwork.ozlabs.org/api/1.2/patches/813331/?format=json","web_url":"http://patchwork.ozlabs.org/project/qemu-devel/patch/150529932725.10902.6569724074050775920.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":"<150529932725.10902.6569724074050775920.stgit@frigg.lan>","list_archive_url":null,"date":"2017-09-13T10:42:07","name":"[v6,12/22] instrument: Add event 'guest_cpu_enter'","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"d21fa3c9729094831679f75def23a3f196496b9e","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/150529932725.10902.6569724074050775920.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/813331/comments/","check":"pending","checks":"http://patchwork.ozlabs.org/api/patches/813331/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 3xsdY375mKz9s5L\n\tfor <incoming@patchwork.ozlabs.org>;\n\tWed, 13 Sep 2017 20:43:03 +1000 (AEST)","from localhost ([::1]:41476 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 1ds58I-0001AK-2c\n\tfor incoming@patchwork.ozlabs.org; Wed, 13 Sep 2017 06:43:02 -0400","from eggs.gnu.org ([2001:4830:134:3::10]:59267)\n\tby lists.gnu.org with esmtp (Exim 4.71)\n\t(envelope-from <vilanova@ac.upc.edu>) id 1ds57e-00016g-F1\n\tfor qemu-devel@nongnu.org; Wed, 13 Sep 2017 06:42:24 -0400","from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)\n\t(envelope-from <vilanova@ac.upc.edu>) id 1ds57a-0005Qz-ER\n\tfor qemu-devel@nongnu.org; Wed, 13 Sep 2017 06:42:22 -0400","from roura.ac.upc.es ([147.83.33.10]:35527)\n\tby eggs.gnu.org with esmtp (Exim 4.71)\n\t(envelope-from <vilanova@ac.upc.edu>) id 1ds57a-0005PN-0R\n\tfor qemu-devel@nongnu.org; Wed, 13 Sep 2017 06:42:18 -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 v8DAgEQE009095;\n\tWed, 13 Sep 2017 12:42:14 +0200","from localhost (unknown [132.68.137.204])\n\tby correu-2.ac.upc.es (Postfix) with ESMTPSA id 79EA0654;\n\tWed, 13 Sep 2017 12:42:08 +0200 (CEST)"],"From":"=?utf-8?b?TGx1w61z?= Vilanova <vilanova@ac.upc.edu>","To":"qemu-devel@nongnu.org","Date":"Wed, 13 Sep 2017 13:42:07 +0300","Message-Id":"<150529932725.10902.6569724074050775920.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\tv8DAgEQE009095","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 12/22] instrument: Add event\n\t'guest_cpu_enter'","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 instrument/control.c            |    9 ++++++++\n instrument/events.h             |    5 ++++\n instrument/events.inc.h         |   11 +++++++++\n instrument/load.c               |    9 ++++++++\n instrument/qemu-instr/control.h |   46 +++++++++++++++++++++++++++++++++++++++\n stubs/instrument.c              |    1 +\n trace/control-target.c          |    2 ++\n 7 files changed, 83 insertions(+)","diff":"diff --git a/instrument/control.c b/instrument/control.c\nindex 8cf2b4f967..c4b3ca0440 100644\n--- a/instrument/control.c\n+++ b/instrument/control.c\n@@ -49,3 +49,12 @@ SYM_PUBLIC void qi_set_fini(qi_fini_fn fn, void *data)\n     instr_set_event(fini_fn, fn);\n     instr_set_event(fini_data, data);\n }\n+\n+\n+void (*instr_event__guest_cpu_enter)(QICPU vcpu);\n+\n+SYM_PUBLIC void qi_event_set_guest_cpu_enter(void (*fn)(QICPU vcpu))\n+{\n+    ERROR_IF(!instr_get_state(), \"called outside instrumentation\");\n+    instr_set_event(guest_cpu_enter, fn);\n+}\ndiff --git a/instrument/events.h b/instrument/events.h\nindex 82ad0bd827..947f120aa9 100644\n--- a/instrument/events.h\n+++ b/instrument/events.h\n@@ -11,6 +11,7 @@\n #define INSTRUMENT__EVENTS_H\n \n #include \"instrument/qemu-instr/control.h\"\n+#include \"instrument/qemu-instr/types.h\"\n \n /**\n  * instr_get_event:\n@@ -32,6 +33,10 @@\n extern qi_fini_fn instr_event__fini_fn;\n extern void *instr_event__fini_data;\n \n+extern void (*instr_event__guest_cpu_enter)(QICPU vcpu);\n+static inline void instr_guest_cpu_enter(CPUState *vcpu);\n+\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\nindex 8b1ce7fcb2..e3f8024716 100644\n--- a/instrument/events.inc.h\n+++ b/instrument/events.inc.h\n@@ -7,5 +7,16 @@\n  * See the COPYING file in the top-level directory.\n  */\n \n+#include \"instrument/control.h\"\n \n \n+static inline void instr_guest_cpu_enter(CPUState *vcpu)\n+{\n+    void (*cb)(QICPU vcpu) = instr_get_event(guest_cpu_enter);\n+    if (cb) {\n+        QICPU vcpu_ = instr_cpu_to_qicpu(vcpu);\n+        instr_set_state(INSTR_STATE_ENABLE);\n+        (*cb)(vcpu_);\n+        instr_set_state(INSTR_STATE_DISABLE);\n+    }\n+}\ndiff --git a/instrument/load.c b/instrument/load.c\nindex a01d66a4d4..218bca74b2 100644\n--- a/instrument/load.c\n+++ b/instrument/load.c\n@@ -11,6 +11,7 @@\n #include \"qemu-common.h\"\n \n #include <dlfcn.h>\n+#include \"exec/cpu-common.h\"\n #include \"instrument/control.h\"\n #include \"instrument/events.h\"\n #include \"instrument/load.h\"\n@@ -109,6 +110,13 @@ InstrLoadError instr_load(const char *path, int argc, const char **argv,\n         goto err;\n     }\n \n+    cpu_list_lock();\n+    CPUState *cpu;\n+    CPU_FOREACH(cpu) {\n+        instr_guest_cpu_enter(cpu);\n+    }\n+    cpu_list_unlock();\n+\n     res = INSTR_LOAD_OK;\n     goto out;\n \n@@ -138,6 +146,7 @@ InstrUnloadError instr_unload(const char *id)\n     }\n \n     instr_set_event(fini_fn, NULL);\n+    instr_set_event(guest_cpu_enter, NULL);\n \n     /* this should never fail */\n     if (dlclose(handle->dlhandle) < 0) {\ndiff --git a/instrument/qemu-instr/control.h b/instrument/qemu-instr/control.h\nindex b841afaa31..f61e7a2b6e 100644\n--- a/instrument/qemu-instr/control.h\n+++ b/instrument/qemu-instr/control.h\n@@ -16,6 +16,7 @@ extern \"C\" {\n \n #include <stdbool.h>\n #include <stddef.h>\n+#include <qemu-instr/types.h>\n \n \n /**\n@@ -39,6 +40,51 @@ typedef void (*qi_fini_fn)(void *arg);\n  */\n void qi_set_fini(qi_fini_fn fn, void *data);\n \n+\n+/*\n+ * Set callbacks for available events. Each event has a short description and\n+ * various indicators of when it can be triggered:\n+ *\n+ * - Mode :: user\n+ *   Triggered in QEMU user application emulation (e.g., linux-user).\n+ *\n+ * - Mode :: softmmy\n+ *   Triggered in QEMU full-system emulation.\n+ *\n+ *\n+ * - Targets :: all\n+ *   Triggered on all targets, both using TCG or native hardware virtualization\n+ *   (e.g., KVM).\n+ *\n+ * - Targets :: TCG(<arch>)\n+ *   Triggered on the given guest target architectures when executing with TCG\n+ *   (no native hardware virtualization).\n+ *\n+ *\n+ * - Time :: exec\n+ *   Triggered when the guest executes the described operation.\n+ *\n+ * - Time :: trans\n+ *   Triggered when QEMU translates a guest operation. This is only available\n+ *   when executing with TCG. Guest instructions are decompiled and translated\n+ *   into the intermediate TCG language (when \"Time: trans\" events are\n+ *   triggered). Then, the TCG compiler translates TCG code into the native host\n+ *   code that QEMU will execute to emulate the guest (when \"Time: exec\" events\n+ *   are triggered). As QEMU uses a cache of translated code, the same\n+ *   instruction might be translated more than once (when the cache overflows).\n+ */\n+\n+/*\n+ * Hot-plug a new virtual (guest) CPU.\n+ *\n+ * Also triggered on each CPU when an instrumentation library is loaded.\n+ *\n+ * Mode: user, softmmu\n+ * Targets: all\n+ * Time: exec\n+ */\n+void qi_event_set_guest_cpu_enter(void (*fn)(QICPU vcpu));\n+\n #ifdef __cplusplus\n }\n #endif\ndiff --git a/stubs/instrument.c b/stubs/instrument.c\nindex 560844469c..abed583a56 100644\n--- a/stubs/instrument.c\n+++ b/stubs/instrument.c\n@@ -42,3 +42,4 @@ void qmp_instr_unload(const char *id, Error **errp)\n \n \n __thread InstrState instr_cur_state;\n+void (*instr_event__guest_cpu_enter)(QICPU *vcpu);\ndiff --git a/trace/control-target.c b/trace/control-target.c\nindex 706b2cee9d..f22688bcd5 100644\n--- a/trace/control-target.c\n+++ b/trace/control-target.c\n@@ -9,6 +9,7 @@\n \n #include \"qemu/osdep.h\"\n #include \"cpu.h\"\n+#include \"instrument/events.h\"\n #include \"trace-root.h\"\n #include \"trace/control.h\"\n #include \"translate-all.h\"\n@@ -146,5 +147,6 @@ void trace_init_vcpu(CPUState *vcpu)\n             }\n         }\n     }\n+    instr_guest_cpu_enter(vcpu);\n     trace_guest_cpu_enter(vcpu);\n }\n","prefixes":["v6","12/22"]}