Skip to contentSkip to conversation

Enable plugin support on msys2/mingw

PATCH Superseded Yonggang Luo 3 participants 9 messages Sep 26, 2020 – Sep 27, 2020

Subsystems (2)

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

cover Enable plugin support on msys2/mingw

Add this feature on msys2/mingw by using glib provided cross-platform dlsym f= unctional.

Yonggang Luo (4):
plugins: Fixes a issue when dlsym failed, the handle not closed.
plugin: Fixes compiling errors on msys2/mingw
Getting qemu-plugin works under win32.
cirrus: Enable plugin in cirrus for windows

 .cirrus.yml                  |   3 +-
 Makefile                     |   1 -
 configure                    |  71 --------------
 contrib/plugins/hotblocks.c  |   4 +-
 contrib/plugins/hotpages.c   |   2 +
 contrib/plugins/howvec.c     |   2 +
 contrib/plugins/lockstep.c   |   2 +
 include/qemu/qemu-plugin.h   | 184 ++++++++++++++++++++++++++---------
 meson.build                  |   6 +-
 plugins/api.c                |  65 +++++++------
 plugins/core.c               |   7 +-
 plugins/loader.c             |  21 ++++
 plugins/meson.build          |  10 +-
 plugins/qemu-plugins.symbols |  40 --------
 tests/plugin/bb.c            |   6 +-
 tests/plugin/empty.c         |   2 +
 tests/plugin/insn.c          |   2 +
 tests/plugin/mem.c           |   2 +
 18 files changed, 224 insertions(+), 206 deletions(-)
 delete mode 100644 plugins/qemu-plugins.symbols

--=20
2.28.0.windows.1

#1 lore raw

PATCH 1/4 plugins: Fixes a issue when dlsym failed, the handle not closed.

---
 plugins/loader.c | 1 +
 1 file changed, 1 insertion(+)
plugins/loader.c +1 -0
index 685d334e1a..8ac5dbc20f 100644
@@ -235,6 +235,7 @@ static int plugin_load(struct qemu_plugin_desc *desc, const qemu_info_t *info)
     return rc;
 
  err_symbol:
+    g_module_close(ctx->handle);
  err_dlopen:
     qemu_vfree(ctx);
     return 1;
Signature · 1 line
2.28.0.windows.1
Reviewed-by on v1 1/4
#2 lore raw

Re: [PATCH 1/4] plugins: Fixes a issue when dlsym failed, the handle not closed.

Quoted 1 line · Yonggang Luo

Superfluous trailing '.' in patch subject.

Quoted 17 lines
---
 plugins/loader.c | 1 +
 1 file changed, 1 insertion(+)
plugins/loader.c +1 -0
index 685d334e1a..8ac5dbc20f 100644
@@ -235,6 +235,7 @@ static int plugin_load(struct qemu_plugin_desc *desc, const qemu_info_t *info)
     return rc;
 
  err_symbol:
+    g_module_close(ctx->handle);
  err_dlopen:
     qemu_vfree(ctx);
     return 1;
#3 lore raw

PATCH 2/4 plugin: Fixes compiling errors on msys2/mingw

In reply to Yonggang Luo
---
 contrib/plugins/hotblocks.c | 2 +-
 tests/plugin/bb.c           | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)
contrib/plugins/hotblocks.c +1 -1
index 3942a2ca54..37435a3fc7 100644
@@ -102,7 +102,7 @@ static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb)
 {
     ExecCount *cnt;
     uint64_t pc = qemu_plugin_tb_vaddr(tb);
-    unsigned long insns = qemu_plugin_tb_n_insns(tb);
+    size_t insns = qemu_plugin_tb_n_insns(tb);
     uint64_t hash = pc ^ insns;
 
     g_mutex_lock(&lock);
tests/plugin/bb.c +2 -2
index e4cc7fdd6e..ef784be78f 100644
@@ -72,7 +72,7 @@ static void vcpu_tb_exec(unsigned int cpu_index, void *udata)
     CPUCount *count = max_cpus ?
         g_ptr_array_index(counts, cpu_index) : &inline_count;
 
-    unsigned long n_insns = (unsigned long)udata;
+    uint64_t n_insns = (uint64_t)udata;
     g_mutex_lock(&count->lock);
     count->insn_count += n_insns;
     count->bb_count++;
@@ -81,7 +81,7 @@ static void vcpu_tb_exec(unsigned int cpu_index, void *udata)
 
 static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb)
 {
-    unsigned long n_insns = qemu_plugin_tb_n_insns(tb);
+    size_t n_insns = qemu_plugin_tb_n_insns(tb);
 
     if (do_inline) {
         qemu_plugin_register_vcpu_tb_exec_inline(tb, QEMU_PLUGIN_INLINE_ADD_U64,
Signature · 1 line
2.28.0.windows.1
Reviewed-by on v1 2/4
#4 lore raw

Re: [PATCH 2/4] plugin: Fixes compiling errors on msys2/mingw

Quoted 1 line · Yonggang Luo
Quoted 41 lines
---
 contrib/plugins/hotblocks.c | 2 +-
 tests/plugin/bb.c           | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)
contrib/plugins/hotblocks.c +1 -1
index 3942a2ca54..37435a3fc7 100644
@@ -102,7 +102,7 @@ static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb)
 {
     ExecCount *cnt;
     uint64_t pc = qemu_plugin_tb_vaddr(tb);
-    unsigned long insns = qemu_plugin_tb_n_insns(tb);
+    size_t insns = qemu_plugin_tb_n_insns(tb);
     uint64_t hash = pc ^ insns;
 
     g_mutex_lock(&lock);
tests/plugin/bb.c +2 -2
index e4cc7fdd6e..ef784be78f 100644
@@ -72,7 +72,7 @@ static void vcpu_tb_exec(unsigned int cpu_index, void *udata)
     CPUCount *count = max_cpus ?
         g_ptr_array_index(counts, cpu_index) : &inline_count;
 
-    unsigned long n_insns = (unsigned long)udata;
+    uint64_t n_insns = (uint64_t)udata;
     g_mutex_lock(&count->lock);
     count->insn_count += n_insns;
     count->bb_count++;
@@ -81,7 +81,7 @@ static void vcpu_tb_exec(unsigned int cpu_index, void *udata)
 
 static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb)
 {
-    unsigned long n_insns = qemu_plugin_tb_n_insns(tb);
+    size_t n_insns = qemu_plugin_tb_n_insns(tb);
 
     if (do_inline) {
         qemu_plugin_register_vcpu_tb_exec_inline(tb, QEMU_PLUGIN_INLINE_ADD_U64,
#5 lore raw

Re: [PATCH 2/4] plugin: Fixes compiling errors on msys2/mingw

In reply to Yonggang Luo

tests/plugin/bb.c static void vcpu_tb_exec(unsigned int cpu_index, void *udata) · line 75

Quoted 6 lines · Yonggang Luo
tests/plugin/bb.c +1 -1
@@ -72,7 +72,7 @@ static void vcpu_tb_exec(unsigned int cpu_index, void *udata)
     CPUCount *count = max_cpus ?
         g_ptr_array_index(counts, cpu_index) : &inline_count;
 
-    unsigned long n_insns = (unsigned long)udata;
+    uint64_t n_insns = (uint64_t)udata;

This needs to be uintptr_t.

r~

#6 lore raw

Re: [PATCH 2/4] plugin: Fixes compiling errors on msys2/mingw

Quoted 9 lines · Richard Henderson

tests/plugin/bb.c static void vcpu_tb_exec(unsigned int cpu_index, void *udata) · line 75

Quoted 6 lines · Yonggang Luo
tests/plugin/bb.c +1 -1
@@ -72,7 +72,7 @@ static void vcpu_tb_exec(unsigned int cpu_index, void *udata)
     CPUCount *count = max_cpus ?
         g_ptr_array_index(counts, cpu_index) : &inline_count;
 
-    unsigned long n_insns = (unsigned long)udata;
+    uint64_t n_insns = (uint64_t)udata;

This needs to be uintptr_t.

Oops good catch.

Quoted 3 lines

r~

#7 lore raw

PATCH 3/4 Getting qemu-plugin works under win32.

In reply to Yonggang Luo
---
 Makefile                     |   1 -
 configure                    |  71 --------------
 contrib/plugins/hotblocks.c  |   2 +
 contrib/plugins/hotpages.c   |   2 +
 contrib/plugins/howvec.c     |   2 +
 contrib/plugins/lockstep.c   |   2 +
 include/qemu/qemu-plugin.h   | 184 ++++++++++++++++++++++++++---------
 meson.build                  |   6 +-
 plugins/api.c                |  65 +++++++------
 plugins/core.c               |   7 +-
 plugins/loader.c             |  20 ++++
 plugins/meson.build          |  10 +-
 plugins/qemu-plugins.symbols |  40 --------
 tests/plugin/bb.c            |   2 +
 tests/plugin/empty.c         |   2 +
 tests/plugin/insn.c          |   2 +
 tests/plugin/mem.c           |   2 +
 17 files changed, 218 insertions(+), 202 deletions(-)
 delete mode 100644 plugins/qemu-plugins.symbols
Makefile +0 -1
index 897246d77c..3dd74205b5 100644
@@ -105,7 +105,6 @@ config-host.mak: $(SRC_PATH)/configure $(SRC_PATH)/pc-bios $(SRC_PATH)/VERSION
 
 # Force configure to re-run if the API symbols are updated
 ifeq ($(CONFIG_PLUGIN),y)
-config-host.mak: $(SRC_PATH)/plugins/qemu-plugins.symbols
 
 .PHONY: plugins
 plugins:
configure +0 -71

83 lines not on this page · raw message

contrib/plugins/hotblocks.c +2 -0
index 37435a3fc7..286ebdd497 100644
@@ -13,6 +13,7 @@
 #include <stdio.h>
 #include <glib.h>
 
+#define QEMU_PLUGIN_IMPLEMENTATION
 #include <qemu-plugin.h>
 
 QEMU_PLUGIN_EXPORT int qemu_plugin_version = QEMU_PLUGIN_VERSION;
@@ -136,6 +137,7 @@ int qemu_plugin_install(qemu_plugin_id_t id, const qemu_info_t *info,
     if (argc && strcmp(argv[0], "inline") == 0) {
         do_inline = true;
     }
+    qemu_plugin_init(info);
 
     plugin_init();
 
contrib/plugins/hotpages.c +2 -0
index ecd6c18732..941554439b 100644
@@ -16,6 +16,7 @@
 #include <stdio.h>
 #include <glib.h>
 
+#define QEMU_PLUGIN_IMPLEMENTATION
 #include <qemu-plugin.h>
 
 QEMU_PLUGIN_EXPORT int qemu_plugin_version = QEMU_PLUGIN_VERSION;
@@ -166,6 +167,7 @@ int qemu_plugin_install(qemu_plugin_id_t id, const qemu_info_t *info,
                         int argc, char **argv)
 {
     int i;
+    qemu_plugin_init(info);
 
     for (i = 0; i < argc; i++) {
         char *opt = argv[i];
contrib/plugins/howvec.c +2 -0
index 3b9a6939f2..d832be9bef 100644
@@ -18,6 +18,7 @@
 #include <stdio.h>
 #include <glib.h>
 
+#define QEMU_PLUGIN_IMPLEMENTATION
 #include <qemu-plugin.h>
 
 QEMU_PLUGIN_EXPORT int qemu_plugin_version = QEMU_PLUGIN_VERSION;
@@ -320,6 +321,7 @@ QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
                                            int argc, char **argv)
 {
     int i;
+    qemu_plugin_init(info);
 
     /* Select a class table appropriate to the guest architecture */
     for (i = 0; i < ARRAY_SIZE(class_tables); i++) {
contrib/plugins/lockstep.c +2 -0
index a696673dff..5090aa8b10 100644
@@ -31,6 +31,7 @@
 #include <stdio.h>
 #include <errno.h>
 
+#define QEMU_PLUGIN_IMPLEMENTATION
 #include <qemu-plugin.h>
 
 QEMU_PLUGIN_EXPORT int qemu_plugin_version = QEMU_PLUGIN_VERSION;
@@ -316,6 +317,7 @@ QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
                                            int argc, char **argv)
 {
     int i;
+    qemu_plugin_init(info);
 
     if (!argc || !argv[0]) {
         qemu_plugin_outs("Need a socket path to talk to other instance.");
include/qemu/qemu-plugin.h +139 -45

360 lines not on this page · raw message

meson.build +5 -1
index 822aac0825..d79ff96bf7 100644
@@ -243,7 +243,11 @@ endif
 rt = cc.find_library('rt', required: false)
 libdl = not_found
 if 'CONFIG_PLUGIN' in config_host
-  libdl = cc.find_library('dl', required: true)
+  if targetos == 'windows'
+    libdl = declare_dependency(compile_args: [], link_args: [])
+  else
+    libdl = cc.find_library('dl', required: true)
+  endif
 endif
 libiscsi = not_found
 if 'CONFIG_LIBISCSI' in config_host
plugins/api.c +33 -32

239 lines not on this page · raw message

plugins/core.c +4 -3
index 51bfc94787..93a4b83a65 100644
@@ -12,6 +12,7 @@
  * SPDX-License-Identifier: GPL-2.0-or-later
  */
 #include "qemu/osdep.h"
+#define QEMU_PLUGIN_API_IMPLEMENTATION
 #include "qemu/error-report.h"
 #include "qemu/config-file.h"
 #include "qapi/error.h"
@@ -233,7 +234,7 @@ static void plugin_vcpu_for_each(gpointer k, gpointer v, gpointer udata)
     args->cb(args->ctx->id, cpu_index);
 }
 
-void qemu_plugin_vcpu_for_each(qemu_plugin_id_t id,
+QEMU_PLUGIN_EXPORT void qemu_plugin_vcpu_for_each(qemu_plugin_id_t id,
                                qemu_plugin_vcpu_simple_cb_t cb)
 {
     struct plugin_for_each_args args;
@@ -384,13 +385,13 @@ void qemu_plugin_vcpu_resume_cb(CPUState *cpu)
     plugin_vcpu_cb__simple(cpu, QEMU_PLUGIN_EV_VCPU_RESUME);
 }
 
-void qemu_plugin_register_vcpu_idle_cb(qemu_plugin_id_t id,
+QEMU_PLUGIN_EXPORT void qemu_plugin_register_vcpu_idle_cb(qemu_plugin_id_t id,
                                        qemu_plugin_vcpu_simple_cb_t cb)
 {
     plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_IDLE, cb);
 }
 
-void qemu_plugin_register_vcpu_resume_cb(qemu_plugin_id_t id,
+QEMU_PLUGIN_EXPORT void qemu_plugin_register_vcpu_resume_cb(qemu_plugin_id_t id,
                                          qemu_plugin_vcpu_simple_cb_t cb)
 {
     plugin_register_cb(id, QEMU_PLUGIN_EV_VCPU_RESUME, cb);
plugins/loader.c +20 -0
index 8ac5dbc20f..c1d52dacc8 100644
@@ -254,6 +254,17 @@ static void plugin_desc_free(struct qemu_plugin_desc *desc)
     g_free(desc);
 }
 
+static void *qemu_plugin_global_dlsym(void* context, const char *name)
+{
+    GModule *global_handle = context;
+    gpointer sym = NULL;
+    if (!g_module_symbol(global_handle, name, &sym)) {
+        error_report("%s: %s", __func__, g_module_error());
+        return NULL;
+    }
+    return sym;
+}
+
 /**
  * qemu_plugin_load_list - load a list of plugins
  * @head: head of the list of descriptors of the plugins to be loaded
@@ -267,6 +278,7 @@ int qemu_plugin_load_list(QemuPluginList *head)
 {
     struct qemu_plugin_desc *desc, *next;
     g_autofree qemu_info_t *info = g_new0(qemu_info_t, 1);
+    GModule *global_handle = NULL;
 
     info->target_name = TARGET_NAME;
     info->version.min = QEMU_PLUGIN_MIN_VERSION;
@@ -276,6 +288,12 @@ int qemu_plugin_load_list(QemuPluginList *head)
     info->system_emulation = true;
     info->system.smp_vcpus = ms->smp.cpus;
     info->system.max_vcpus = ms->smp.max_cpus;
+    global_handle = g_module_open(NULL, G_MODULE_BIND_LOCAL);
+    if (global_handle == NULL) {
+        goto err_dlopen;
+    }
+    info->dlsym = qemu_plugin_global_dlsym;
+    info->context = (void*)global_handle;
 #else
     info->system_emulation = false;
 #endif
@@ -289,6 +307,8 @@ int qemu_plugin_load_list(QemuPluginList *head)
         }
         QTAILQ_REMOVE(head, desc, entry);
     }
+
+err_dlopen:
     return 0;
 }
 
plugins/meson.build +1 -9
index e77723010e..16af5d7ca3 100644
@@ -1,13 +1,5 @@
-if 'CONFIG_HAS_LD_DYNAMIC_LIST' in config_host
-  plugin_ldflags = ['-Wl,--dynamic-list=' + (meson.build_root() / 'qemu-plugins-ld.symbols')]
-elif 'CONFIG_HAS_LD_EXPORTED_SYMBOLS_LIST' in config_host
-  plugin_ldflags = ['-Wl,-exported_symbols_list,' + (meson.build_root() / 'qemu-plugins-ld64.symbols')]
-else
-  plugin_ldflags = []
-endif
-
 specific_ss.add(when: 'CONFIG_PLUGIN', if_true: [files(
   'loader.c',
   'core.c',
   'api.c',
-), declare_dependency(link_args: plugin_ldflags)])
+), declare_dependency(link_args: [], compile_args: ['-Wno-missing-prototypes'])])
plugins/qemu-plugins.symbols deleted +0 -40
deleted file mode 100644
index 4bdb381f48..0000000000
@@ -1,40 +0,0 @@
-{
-  qemu_plugin_uninstall;
-  qemu_plugin_reset;
-  qemu_plugin_register_vcpu_init_cb;
-  qemu_plugin_register_vcpu_exit_cb;
-  qemu_plugin_register_vcpu_idle_cb;
-  qemu_plugin_register_vcpu_resume_cb;
-  qemu_plugin_register_vcpu_insn_exec_cb;
-  qemu_plugin_register_vcpu_insn_exec_inline;
-  qemu_plugin_register_vcpu_mem_cb;
-  qemu_plugin_register_vcpu_mem_haddr_cb;
-  qemu_plugin_register_vcpu_mem_inline;
-  qemu_plugin_ram_addr_from_host;
-  qemu_plugin_register_vcpu_tb_trans_cb;
-  qemu_plugin_register_vcpu_tb_exec_cb;
-  qemu_plugin_register_vcpu_tb_exec_inline;
-  qemu_plugin_register_flush_cb;
-  qemu_plugin_register_vcpu_syscall_cb;
-  qemu_plugin_register_vcpu_syscall_ret_cb;
-  qemu_plugin_register_atexit_cb;
-  qemu_plugin_tb_n_insns;
-  qemu_plugin_tb_get_insn;
-  qemu_plugin_tb_vaddr;
-  qemu_plugin_insn_data;
-  qemu_plugin_insn_size;
-  qemu_plugin_insn_vaddr;
-  qemu_plugin_insn_haddr;
-  qemu_plugin_insn_disas;
-  qemu_plugin_mem_size_shift;
-  qemu_plugin_mem_is_sign_extended;
-  qemu_plugin_mem_is_big_endian;
-  qemu_plugin_mem_is_store;
-  qemu_plugin_get_hwaddr;
-  qemu_plugin_hwaddr_is_io;
-  qemu_plugin_hwaddr_to_raddr;
-  qemu_plugin_vcpu_for_each;
-  qemu_plugin_n_vcpus;
-  qemu_plugin_n_max_vcpus;
-  qemu_plugin_outs;
-};
tests/plugin/bb.c +2 -0
index ef784be78f..5ee0bce39d 100644
@@ -12,6 +12,7 @@
 #include <stdio.h>
 #include <glib.h>
 
+#define QEMU_PLUGIN_IMPLEMENTATION
 #include <qemu-plugin.h>
 
 QEMU_PLUGIN_EXPORT int qemu_plugin_version = QEMU_PLUGIN_VERSION;
@@ -101,6 +102,7 @@ QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
                                            int argc, char **argv)
 {
     int i;
+    qemu_plugin_init(info);
 
     for (i = 0; i < argc; i++) {
         char *opt = argv[i];
tests/plugin/empty.c +2 -0
index 8fa6bacd93..9e1e683fd6 100644
@@ -11,6 +11,7 @@
 #include <unistd.h>
 #include <stdio.h>
 
+#define QEMU_PLUGIN_IMPLEMENTATION
 #include <qemu-plugin.h>
 
 QEMU_PLUGIN_EXPORT int qemu_plugin_version = QEMU_PLUGIN_VERSION;
@@ -27,6 +28,7 @@ QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
                                            const qemu_info_t *info,
                                            int argc, char **argv)
 {
+    qemu_plugin_init(info);
     qemu_plugin_register_vcpu_tb_trans_cb(id, vcpu_tb_trans);
     return 0;
 }
tests/plugin/insn.c +2 -0
index a9a6e41237..ea848036bc 100644
@@ -12,6 +12,7 @@
 #include <stdio.h>
 #include <glib.h>
 
+#define QEMU_PLUGIN_IMPLEMENTATION
 #include <qemu-plugin.h>
 
 QEMU_PLUGIN_EXPORT int qemu_plugin_version = QEMU_PLUGIN_VERSION;
@@ -52,6 +53,7 @@ QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
                                            const qemu_info_t *info,
                                            int argc, char **argv)
 {
+    qemu_plugin_init(info);
     if (argc && !strcmp(argv[0], "inline")) {
         do_inline = true;
     }
tests/plugin/mem.c +2 -0
index 4725bd851d..ba3ce3a713 100644
@@ -12,6 +12,7 @@
 #include <stdio.h>
 #include <glib.h>
 
+#define QEMU_PLUGIN_IMPLEMENTATION
 #include <qemu-plugin.h>
 
 QEMU_PLUGIN_EXPORT int qemu_plugin_version = QEMU_PLUGIN_VERSION;
@@ -73,6 +74,7 @@ QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
                                            const qemu_info_t *info,
                                            int argc, char **argv)
 {
+    qemu_plugin_init(info);
     if (argc) {
         if (argc >= 3) {
             if (!strcmp(argv[2], "haddr")) {
Signature · 1 line
2.28.0.windows.1
#8 lore raw

PATCH 4/4 cirrus: Enable plugin in cirrus for windows

In reply to Yonggang Luo
---
 .cirrus.yml | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
.cirrus.yml +2 -1
index 86ea33926a..5363142373 100644
@@ -104,7 +104,8 @@ windows_msys2_task:
     - C:\tools\msys64\usr\bin\bash.exe -lc "mkdir build"
     - C:\tools\msys64\usr\bin\bash.exe -lc "cd build && ../configure
       --python=python3 --ninja=ninja
-      --target-list-exclude=i386-softmmu,arm-softmmu,ppc-softmmu,mips-softmmu"
+      --target-list-exclude=i386-softmmu,arm-softmmu,ppc-softmmu,mips-softmmu
+      --enable-plugins"
     - C:\tools\msys64\usr\bin\bash.exe -lc "cd build && make -j$NUMBER_OF_PROCESSORS"
   test_script:
     - C:\tools\msys64\usr\bin\bash.exe -lc "cd build && make V=1 check"
Signature · 1 line
2.28.0.windows.1