Skip to contentSkip to conversation

plugins: add plugin API to get args passed to binary

PATCH In review demin.han 3 participants 4 messages Nov 1, 2024 – Jan 10, 2025

lore
Share & tools
lore

demin.han

author
#0 lore raw

PATCH 1/1 plugins: add plugin API to get args passed to binary

Why we need args?
When plugin outputs log files, only binary path can't distinguish multiple runs if the binary passed with different args.
This is bad for CI using plugin.

---
 include/qemu/qemu-plugin.h   | 11 +++++++++++
 plugins/api.c                | 16 ++++++++++++++++
 plugins/qemu-plugins.symbols |  1 +
 3 files changed, 28 insertions(+)
include/qemu/qemu-plugin.h +11 -0
index 622c9a0232..daf75c9f5a 100644
@@ -837,6 +837,17 @@ bool qemu_plugin_bool_parse(const char *name, const char *val, bool *ret);
 QEMU_PLUGIN_API
 const char *qemu_plugin_path_to_binary(void);
 
+/**
+ * qemu_plugin_argv_to_binary() - argv to binary file being executed
+ *
+ * Return a string array representing the argv to the binary. For user-mode
+ * this is the main executable's argv. For system emulation we currently
+ * return NULL. The user should g_free() the string array once no longer
+ * needed.
+ */
+QEMU_PLUGIN_API
+const char **qemu_plugin_argv_to_binary(void);
+
 /**
  * qemu_plugin_start_code() - returns start of text segment
  *
plugins/api.c +16 -0
index 24ea64e2de..fa2735db03 100644
@@ -485,6 +485,22 @@ const char *qemu_plugin_path_to_binary(void)
     return path;
 }
 
+const char **qemu_plugin_argv_to_binary(void)
+{
+    const char **argv = NULL;
+#ifdef CONFIG_USER_ONLY
+    int i, argc;
+    TaskState *ts = get_task_state(current_cpu);
+    argc = ts->bprm->argc;
+    argv = g_malloc(sizeof(char *) * (argc + 1));
+    for (i = 0; i < argc; ++i) {
+        argv[i] = g_strdup(ts->bprm->argv[i]);
+    }
+    argv[argc] = NULL;
+#endif
+    return argv;
+}
+
 uint64_t qemu_plugin_start_code(void)
 {
     uint64_t start = 0;
plugins/qemu-plugins.symbols +1 -0
index 032661f9ea..532582effe 100644
@@ -1,4 +1,5 @@
 {
+  qemu_plugin_argv_to_binary;
   qemu_plugin_bool_parse;
   qemu_plugin_end_code;
   qemu_plugin_entry_code;
Signature · 1 line
2.46.1
#1 lore raw

Hi Demin,

thanks for your contribution.

Quoted 5 lines · demin.han

Why we need args?
When plugin outputs log files, only binary path can't distinguish multiple runs if the binary passed with different args.
This is bad for CI using plugin.

Can it be solved simply by encoding this in name of log file from the CI run script?

$ cmd="/usr/bin/echo Hello world"
$ out_file="$(echo "$cmd" | sed -e 's/\s/_/').log"
$ qemu -plugin... -d plugin -D "$out_file" $cmd

I can see some good points to add this new API, but for the use case presented in commit message, I'm not sure to see what it solves.

Quoted 61 lines
---
  include/qemu/qemu-plugin.h   | 11 +++++++++++
  plugins/api.c                | 16 ++++++++++++++++
  plugins/qemu-plugins.symbols |  1 +
  3 files changed, 28 insertions(+)
include/qemu/qemu-plugin.h +11 -0
index 622c9a0232..daf75c9f5a 100644
@@ -837,6 +837,17 @@ bool qemu_plugin_bool_parse(const char *name, const char *val, bool *ret);
  QEMU_PLUGIN_API
  const char *qemu_plugin_path_to_binary(void);
 
+/**
+ * qemu_plugin_argv_to_binary() - argv to binary file being executed
+ *
+ * Return a string array representing the argv to the binary. For user-mode
+ * this is the main executable's argv. For system emulation we currently
+ * return NULL. The user should g_free() the string array once no longer
+ * needed.
+ */
+QEMU_PLUGIN_API
+const char **qemu_plugin_argv_to_binary(void);
+
  /**
   * qemu_plugin_start_code() - returns start of text segment
   *
plugins/api.c +16 -0
index 24ea64e2de..fa2735db03 100644
@@ -485,6 +485,22 @@ const char *qemu_plugin_path_to_binary(void)
      return path;
  }
 
+const char **qemu_plugin_argv_to_binary(void)
+{
+    const char **argv = NULL;
+#ifdef CONFIG_USER_ONLY
+    int i, argc;
+    TaskState *ts = get_task_state(current_cpu);
+    argc = ts->bprm->argc;
+    argv = g_malloc(sizeof(char *) * (argc + 1));
+    for (i = 0; i < argc; ++i) {
+        argv[i] = g_strdup(ts->bprm->argv[i]);
+    }
+    argv[argc] = NULL;
+#endif
+    return argv;
+}
+
  uint64_t qemu_plugin_start_code(void)
  {
      uint64_t start = 0;
plugins/qemu-plugins.symbols +0 -0
index 032661f9ea..532582effe 100644

plugins/qemu-plugins.symbols line 5

Quoted 5 lines
plugins/qemu-plugins.symbols +1 -0
@@ -1,4 +1,5 @@
  {
+  qemu_plugin_argv_to_binary;
    qemu_plugin_bool_parse;
    qemu_plugin_end_code;
    qemu_plugin_entry_code;

Regards,
Pierrick

#2 lore raw
In reply to demin.han
Quoted 4 lines · demin.han

Why we need args?
When plugin outputs log files, only binary path can't distinguish multiple runs if the binary passed with different args.
This is bad for CI using plugin.

New APIs should come with an example use case for testing. However for this use case why isn't the plugin using getpid() or gettid() a suitable solution?

Some additional comments bellow...

Quoted 12 lines
---
 include/qemu/qemu-plugin.h   | 11 +++++++++++
 plugins/api.c                | 16 ++++++++++++++++
 plugins/qemu-plugins.symbols |  1 +
 3 files changed, 28 insertions(+)
include/qemu/qemu-plugin.h +0 -0
index 622c9a0232..daf75c9f5a 100644

include/qemu/qemu-plugin.h bool qemu_plugin_bool_parse(const char *name, const char *val, bool *ret); · line 845

Quoted 9 lines
include/qemu/qemu-plugin.h +6 -0
@@ -837,6 +837,17 @@ bool qemu_plugin_bool_parse(const char *name, const char *val, bool *ret);
 QEMU_PLUGIN_API
 const char *qemu_plugin_path_to_binary(void);
 
+/**
+ * qemu_plugin_argv_to_binary() - argv to binary file being executed
+ *
+ * Return a string array representing the argv to the binary. For user-mode
+ * this is the main executable's argv. For system emulation we currently
+ * return NULL.

Maybe more explicit:

qemu_plugin_get_user_argv() and be clear it is user mode only.

Although I suspect a qemu_plugin_get_user_env() might be a more useful helper depending on the use case.

Quoted 15 lines

The user should g_free() the string array once no longer
+ * needed.
+ */

include/qemu/qemu-plugin.h +3 -0
+QEMU_PLUGIN_API
+const char **qemu_plugin_argv_to_binary(void);
+
 /**
  * qemu_plugin_start_code() - returns start of text segment
  *
plugins/api.c +0 -0
index 24ea64e2de..fa2735db03 100644
@@ -485,6 +485,22 @@ const char *qemu_plugin_path_to_binary(void)
     return path;

plugins/api.c const char *qemu_plugin_path_to_binary(void) · line 495

Quoted 10 lines
plugins/api.c +8 -0
 }
 
+const char **qemu_plugin_argv_to_binary(void)
+{
+    const char **argv = NULL;
+#ifdef CONFIG_USER_ONLY
+    int i, argc;
+    TaskState *ts = get_task_state(current_cpu);
+    argc = ts->bprm->argc;
+    argv = g_malloc(sizeof(char *) * (argc + 1));

argv = g_new0(char *, argc + 1);

plugins/api.c const char *qemu_plugin_path_to_binary(void) · line 499

Quoted 4 lines
plugins/api.c +4 -0
+    for (i = 0; i < argc; ++i) {
+        argv[i] = g_strdup(ts->bprm->argv[i]);
+    }
+    argv[argc] = NULL;

Allows you to drop this as well.

Quoted 12 lines
plugins/api.c +4 -0
+#endif
+    return argv;
+}
+
 uint64_t qemu_plugin_start_code(void)
 {
     uint64_t start = 0;
plugins/qemu-plugins.symbols +0 -0
index 032661f9ea..532582effe 100644

plugins/qemu-plugins.symbols line 5

Quoted 5 lines
plugins/qemu-plugins.symbols +1 -0
@@ -1,4 +1,5 @@
 {
+  qemu_plugin_argv_to_binary;
   qemu_plugin_bool_parse;
   qemu_plugin_end_code;
   qemu_plugin_entry_code;

You can drop the symbols now we autogenerate them.

Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
author
#3 lore raw

Hi,

Quoted 10 lines

-----Original Message-----
From: Alex Bennée <[email protected]>
Sent: 2025年1月9日 19:59
To: Demin Han <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH] plugins: add plugin API to get args passed to binary

"demin.han" <[email protected]> writes:

Quoted 7 lines
Quoted 4 lines

Why we need args?
When plugin outputs log files, only binary path can't distinguish multiple runs if the binary passed with different args. This is bad for CI using plugin.

New APIs should come with an example use case for testing. However for this use case why isn't the plugin using getpid() or gettid() a suitable solution?

We want a meaning and unique name for plugin output. For example ./a arg0 arg1 should have a_arg0_arg1.log
./a arg2 arg3 should have a_arg2_arg3.log
I think getpid() and getid() is not unique

This case can be solved by using a shell wrapper, extract args and pass them to plugin suggested by Pierrick Bouvier

Regards,
Demin