qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Balamuruhan S <bala24@linux.ibm.com>
To: qemu-devel@nongnu.org
Cc: maddy@linux.vnet.ibm.com, Balamuruhan S <bala24@linux.ibm.com>,
	anju@linux.vnet.ibm.com, clg@kaod.org, hari@linux.vnet.ibm.com,
	pbonzini@redhat.com, david@gibson.dropbear.id.au
Subject: [Qemu-devel] [RFC PATCH 1/6] utils/python_api: add scripting interface for Qemu with python lib
Date: Wed,  7 Aug 2019 12:44:40 +0530	[thread overview]
Message-ID: <20190807071445.4109-2-bala24@linux.ibm.com> (raw)
In-Reply-To: <20190807071445.4109-1-bala24@linux.ibm.com>

Adds scripting interface with python library to call functions in
python modules from Qemu that can be used to feed input externally
and without recompiling Qemu that can be used for early development,
testing and can be extended to abstract some of Qemu code out to a
python script to ease maintenance.

Signed-off-by: Balamuruhan S <bala24@linux.ibm.com>
---
 configure                   |  10 +++++
 include/sysemu/python_api.h |  30 +++++++++++++
 util/Makefile.objs          |   1 +
 util/python_api.c           | 100 ++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 141 insertions(+)
 create mode 100644 include/sysemu/python_api.h
 create mode 100644 util/python_api.c

diff --git a/configure b/configure
index 714e7fb6a1..fddddcc879 100755
--- a/configure
+++ b/configure
@@ -1866,6 +1866,11 @@ fi
 # Preserve python version since some functionality is dependent on it
 python_version=$($python -V 2>&1 | sed -e 's/Python\ //')
 
+# Python config to be used for CFLAGS and LDFLAGS
+if ! [ -z "$python" ]; then
+    python_config="$python-config"
+fi
+
 # Suppress writing compiled files
 python="$python -B"
 
@@ -6304,6 +6309,11 @@ echo_version() {
     fi
 }
 
+if ! [ -z "$python_config" ]; then
+    QEMU_CFLAGS="$QEMU_CFLAGS $($python_config --includes)"
+    QEMU_LDFLAGS="$QEMU_LDFLAGS $($python_config --ldflags)"
+fi
+
 # prepend pixman and ftd flags after all config tests are done
 QEMU_CFLAGS="$pixman_cflags $fdt_cflags $QEMU_CFLAGS"
 QEMU_LDFLAGS="$fdt_ldflags $QEMU_LDFLAGS"
diff --git a/include/sysemu/python_api.h b/include/sysemu/python_api.h
new file mode 100644
index 0000000000..ff02d58377
--- /dev/null
+++ b/include/sysemu/python_api.h
@@ -0,0 +1,30 @@
+#ifndef _PPC_PNV_PYTHON_H
+#define _PPC_PNV_PYTHON_H
+
+#include <stdbool.h>
+#include <Python.h>
+
+extern PyObject *python_callback(const char *abs_module_path, const char *mod,
+                                 const char *func, char *args[],
+                                 const int nargs);
+
+extern uint64_t python_callback_int(const char *abs_module_path,
+                                    const char *mod,
+                                    const char *func, char *args[],
+                                    const int nargs);
+
+extern char *python_callback_str(const char *abs_module_path, const char *mod,
+                                 const char *func, char *args[],
+                                 const int nargs);
+
+extern bool python_callback_bool(const char *abs_module_path, const char *mod,
+                                 const char *func, char *args[],
+                                 const int nargs);
+
+extern void python_args_init_cast_int(char *args[], int arg, int pos);
+
+extern void python_args_init_cast_long(char *args[], uint64_t arg, int pos);
+
+extern void python_args_clean(char *args[], int nargs);
+
+#endif
diff --git a/util/Makefile.objs b/util/Makefile.objs
index 41bf59d127..05851c94a7 100644
--- a/util/Makefile.objs
+++ b/util/Makefile.objs
@@ -50,6 +50,7 @@ util-obj-y += range.o
 util-obj-y += stats64.o
 util-obj-y += systemd.o
 util-obj-y += iova-tree.o
+util-obj-y += python_api.o
 util-obj-$(CONFIG_INOTIFY1) += filemonitor-inotify.o
 util-obj-$(CONFIG_LINUX) += vfio-helpers.o
 util-obj-$(CONFIG_POSIX) += drm.o
diff --git a/util/python_api.c b/util/python_api.c
new file mode 100644
index 0000000000..854187e00f
--- /dev/null
+++ b/util/python_api.c
@@ -0,0 +1,100 @@
+#include "sysemu/python_api.h"
+#include "qemu/osdep.h"
+
+PyObject *python_callback(const char *abs_module_path, const char *mod,
+                          const char *func, char *args[], const int nargs)
+{
+    PyObject *mod_name, *module, *mod_ref, *function, *arguments;
+    PyObject *result = 0;
+    PyObject *value = NULL;
+
+    /* Set PYTHONPATH to absolute module path directory */
+    if (!abs_module_path)
+        abs_module_path = ".";
+    setenv("PYTHONPATH", abs_module_path, 1);
+
+    /* Initialize the Python Interpreter */
+    Py_Initialize();
+    mod_name = PyUnicode_FromString(mod);
+    /* Import module object */
+    module = PyImport_Import(mod_name);
+    if (!module) {
+        PyErr_Print();
+        fprintf(stderr, "Failed to load \"%s\"\n", mod);
+        exit(EXIT_FAILURE);
+    }
+    mod_ref = PyModule_GetDict(module);
+    function = PyDict_GetItemString(mod_ref, func);
+    if (function && PyCallable_Check(function)) {
+        arguments = PyTuple_New(nargs);
+        for (int i = 0; i < nargs; i++) {
+            value = PyUnicode_FromString(args[i]);
+            if (!value) {
+                Py_DECREF(arguments);
+                Py_DECREF(module);
+                fprintf(stderr, "Cannot convert argument\n");
+                exit(EXIT_FAILURE);
+            }
+            PyTuple_SetItem(arguments, i, value);
+        }
+        PyErr_Print();
+        result = PyObject_CallObject(function, arguments);
+        PyErr_Print();
+    }
+    else {
+        if (PyErr_Occurred())
+            PyErr_Print();
+        fprintf(stderr, "Cannot find function \"%s\"\n", func);
+        exit(EXIT_FAILURE);
+    }
+    /* Clean up */
+    Py_DECREF(value);
+    Py_DECREF(module);
+    Py_DECREF(mod_name);
+    /* Finish the Python Interpreter */
+    Py_Finalize();
+    return result;
+}
+
+uint64_t python_callback_int(const char *abs_module_path, const char *mod,
+                             const char *func, char *args[], const int nargs)
+{
+    PyObject *result;
+    result = python_callback(abs_module_path, mod, func, args, nargs);
+    return PyLong_AsLong(result);
+}
+
+char *python_callback_str(const char *abs_module_path, const char *mod,
+                          const char *func, char *args[], const int nargs)
+{
+    PyObject *result;
+    result = python_callback(abs_module_path, mod, func, args, nargs);
+    return PyUnicode_AsUTF8(result);
+}
+
+bool python_callback_bool(const char *abs_module_path, const char *mod,
+                          const char *func, char *args[], const int nargs)
+{
+    PyObject *result;
+    result = python_callback(abs_module_path, mod, func, args, nargs);
+    return (result == Py_True);
+}
+
+void python_args_init_cast_int(char *args[], int arg, int pos)
+{
+    args[pos]= malloc(sizeof(int));
+    sprintf(args[pos], "%d", arg);
+}
+
+void python_args_init_cast_long(char *args[], uint64_t arg, int pos)
+{
+    args[pos]= g_malloc(sizeof(uint64_t) * 2);
+    sprintf(args[pos], "%lx", arg);
+}
+
+void python_args_clean(char *args[], int nargs)
+{
+    for (int i = 0; i < nargs; i++) {
+        g_free(args[i]);
+    }
+}
-- 
2.14.5



  reply	other threads:[~2019-08-07  7:17 UTC|newest]

Thread overview: 45+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-08-07  7:14 [Qemu-devel] [RFC PATCH 0/6] Enhancing Qemu MMIO emulation with scripting interface Balamuruhan S
2019-08-07  7:14 ` Balamuruhan S [this message]
2019-08-07 10:20   ` [Qemu-devel] [RFC PATCH 1/6] utils/python_api: add scripting interface for Qemu with python lib Philippe Mathieu-Daudé
2019-08-08 10:10     ` Stefan Hajnoczi
2019-08-08 10:33       ` Philippe Mathieu-Daudé
2019-08-08 10:53       ` Daniel P. Berrangé
2019-08-09  8:46         ` Stefan Hajnoczi
2019-08-12  4:53           ` Balamuruhan S
2019-08-08 10:09   ` Stefan Hajnoczi
2019-08-11  6:39     ` Balamuruhan S
2019-08-08 10:49   ` Daniel P. Berrangé
2019-08-08 12:45     ` Philippe Mathieu-Daudé
2019-08-09  4:39       ` David Gibson
2019-08-12  4:45       ` Balamuruhan S
2019-08-07  7:14 ` [Qemu-devel] [RFC PATCH 2/6] hw/ppc/pnv_xscom: extend xscom to use python interface Balamuruhan S
2019-08-08  9:04   ` Cédric Le Goater
2019-08-07  7:14 ` [Qemu-devel] [RFC PATCH 3/6] hw/ppc/pnv_homer: add homer/occ common area emulation for PowerNV Balamuruhan S
2019-08-07  7:54   ` Cédric Le Goater
2019-08-07 10:07     ` Balamuruhan S
2019-08-08  8:32       ` Cédric Le Goater
2019-08-09  4:44     ` David Gibson
2019-08-11  6:34       ` Balamuruhan S
2019-08-07  7:14 ` [Qemu-devel] [RFC PATCH 4/6] hw/ppc/pnv: initialize and realize homer/occ common area Balamuruhan S
2019-08-07  7:59   ` Cédric Le Goater
2019-08-07 10:12     ` Balamuruhan S
2019-08-08  8:46       ` Cédric Le Goater
2019-08-09  4:45   ` David Gibson
2019-08-07  7:14 ` [Qemu-devel] [RFC PATCH 5/6] hw/ppc/pnv_xscom: retrieve homer/occ base address from PBA BARs Balamuruhan S
2019-08-07  8:01   ` Cédric Le Goater
2019-08-07 10:22     ` Balamuruhan S
2019-08-09  4:45   ` David Gibson
2019-08-07  7:14 ` [Qemu-devel] [RFC PATCH 6/6] hw/ppc/pnv_homer: add python interface support for homer/occ common area Balamuruhan S
2019-08-07 10:27   ` Philippe Mathieu-Daudé
2019-08-11  6:05     ` Balamuruhan S
2019-08-09  4:46   ` David Gibson
2019-08-11  6:19     ` Balamuruhan S
2019-08-07  7:33 ` [Qemu-devel] [RFC PATCH 0/6] Enhancing Qemu MMIO emulation with scripting interface no-reply
2019-08-07  8:15 ` Cédric Le Goater
2019-08-07 10:16   ` Balamuruhan S
2019-08-09  4:49   ` David Gibson
2019-08-12  5:07     ` Balamuruhan S
2019-08-07  8:51 ` no-reply
2019-08-07  9:18 ` no-reply
2019-08-08 10:25 ` Stefan Hajnoczi
2019-08-12  6:03   ` Balamuruhan S

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190807071445.4109-2-bala24@linux.ibm.com \
    --to=bala24@linux.ibm.com \
    --cc=anju@linux.vnet.ibm.com \
    --cc=clg@kaod.org \
    --cc=david@gibson.dropbear.id.au \
    --cc=hari@linux.vnet.ibm.com \
    --cc=maddy@linux.vnet.ibm.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).