Patchwork introduce environment variables for all qemu-user options

login
register
mail settings
Submitter Johannes Schauer
Date July 31, 2011, 11:51 a.m.
Message ID <1312113090-23571-1-git-send-email-j.schauer@email.de>
Download mbox | patch
Permalink /patch/107599/
State New
Headers show

Comments

Johannes Schauer - July 31, 2011, 11:51 a.m.
From: Johannes Schauer <j.schauer@email.de>

A first try to introduce a generic setup for mapping environment variables to
command line options.

I'm afraid to code something for platforms I can't do runtime tests on, so this 
is only for linux-user for now.

Signed-off-by: Johannes Schauer <j.schauer@email.de>
---
 linux-user/main.c |  147 +++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 144 insertions(+), 3 deletions(-)
Peter Maydell - July 31, 2011, 12:12 p.m.
On 31 July 2011 12:51,  <j.schauer@email.de> wrote:
> +    if ((r = getenv("QEMU_STACK_SIZE")) != NULL) {
> +        guest_stack_size = strtoul(r, (char **)&r, 0);
> +        if (guest_stack_size == 0)
> +            usage();
> +        if (*r == 'M')
> +            guest_stack_size *= 1024 * 1024;
> +        else if (*r == 'k' || *r == 'K')
> +            guest_stack_size *= 1024;
> +    }

[etc]

This is all basically duplicating the existing command line
argument parsing code, which (a) makes it very easy for the
two to drift out of sync in future and (b) means command line
options added in future might end up without a corresponding
environment variable.

I think it would be much nicer to have this be table-driven,
to avoid the code duplication. It ought to be possible to
derive most of the extra --help text from the table too.

-- PMM

Patch

diff --git a/linux-user/main.c b/linux-user/main.c
index dbba8be..fb986e3 100644
--- a/linux-user/main.c
+++ b/linux-user/main.c
@@ -2640,26 +2640,51 @@  static void usage(void)
            "-E var=value      sets/modifies targets environment variable(s)\n"
            "-U var            unsets targets environment variable(s)\n"
            "-0 argv0          forces target process argv[0] to be argv0\n"
+           "-r uname          set qemu uname release string\n"
 #if defined(CONFIG_USE_GUEST_BASE)
            "-B address        set guest_base address to address\n"
            "-R size           reserve size bytes for guest virtual address space\n"
 #endif
            "\n"
+           "Standard environment variables:\n"
+           "QEMU_GDB          see the -g option\n"
+           "QEMU_LD_PREFIX    see the -L option\n"
+           "QEMU_STACK_SIZE   see the -s option\n"
+           "QEMU_CPU          see the -cpu option\n"
+           "QEMU_SET_ENV      see the -E option, comma separated list of arguments\n"
+           "QEMU_UNSET_ENV    see the -U option, comma separated list of arguments\n"
+           "QEMU_ARGV0        see the -0 option\n"
+           "QEMU_UNAME        see the -r option\n"
+#if defined(CONFIG_USE_GUEST_BASE)
+           "QEMU_GUEST_BASE   see the -B option\n"
+           "QEMU_RESERVED_VA  see the -R option\n"
+#endif
+           "\n"
            "Debug options:\n"
            "-d options   activate log (logfile=%s)\n"
            "-p pagesize  set the host page size to 'pagesize'\n"
            "-singlestep  always run in singlestep mode\n"
            "-strace      log system calls\n"
            "\n"
-           "Environment variables:\n"
-           "QEMU_STRACE       Print system calls and arguments similar to the\n"
-           "                  'strace' program.  Enable by setting to any value.\n"
+           "Debug environment variables:\n"
+           "QEMU_LOG          see the -d option\n"
+           "QEMU_PAGESIZE     see the -p option\n"
+           "QEMU_SINGLESTEP   see the -singlestep option\n"
+           "QEMU_STRACE       see the -strace option\n"
+           "\n"
            "You can use -E and -U options to set/unset environment variables\n"
            "for target process.  It is possible to provide several variables\n"
            "by repeating the option.  For example:\n"
            "    -E var1=val2 -E var2=val2 -U LD_PRELOAD -U LD_DEBUG\n"
            "Note that if you provide several changes to single variable\n"
            "last change will stay in effect.\n"
+           "Using the environment variables QEMU_SET_ENV and QEMU_UNSET_ENV\n"
+           "to set/unset environment variables for target process is\n"
+           "possible by a comma separated list of values in getsubopt(3)\n"
+           "style. For example:\n"
+           "    QEMU_SET_ENV=var1=val2,var2=val2 QEMU_UNSET_ENV=LD_PRELOAD,LD_DEBUG\n"
+           "Note that if you provide several changes to single variable\n"
+           "last change will stay in effect.\n"
            ,
            TARGET_ARCH,
            interp_prefix,
@@ -2758,6 +2783,122 @@  int main(int argc, char **argv, char **envp)
     cpudef_setup(); /* parse cpu definitions in target config file (TBD) */
 #endif
 
+    if ((r = getenv("QEMU_LOG")) != NULL) {
+        int mask;
+        const CPULogItem *item;
+        mask = cpu_str_to_log_mask(r);
+        if (!mask) {
+            printf("Log items (comma separated):\n");
+            for(item = cpu_log_items; item->mask != 0; item++) {
+                printf("%-10s %s\n", item->name, item->help);
+            }
+            exit(1);
+        }
+        cpu_set_log(mask);
+    }
+    if ((r = getenv("QEMU_SET_ENV")) != NULL) {
+        char *p, *token;
+        p = strdup(r);
+        while ((token = strsep(&p, ",")) != NULL) {
+            if (envlist_setenv(envlist, token) != 0)
+                usage();
+        }
+        free(p);
+    }
+    if ((r = getenv("QEMU_UNSET_ENV")) != NULL) {
+        char *p, *token;
+        p = strdup(r);
+        while ((token = strsep(&p, ",")) != NULL) {
+            if (envlist_unsetenv(envlist, token) != 0)
+                usage();
+        }
+        free(p);
+    }
+    if ((r = getenv("QEMU_ARGV0")) != NULL) {
+        argv0 = strdup(r);
+    }
+    if ((r = getenv("QEMU_STACK_SIZE")) != NULL) {
+        guest_stack_size = strtoul(r, (char **)&r, 0);
+        if (guest_stack_size == 0)
+            usage();
+        if (*r == 'M')
+            guest_stack_size *= 1024 * 1024;
+        else if (*r == 'k' || *r == 'K')
+            guest_stack_size *= 1024;
+    }
+    if ((r = getenv("QEMU_LD_PREFIX")) != NULL) {
+        interp_prefix = strdup(r);
+    }
+    if ((r = getenv("QEMU_PAGESIZE")) != NULL) {
+        qemu_host_page_size = atoi(r);
+        if (qemu_host_page_size == 0 ||
+            (qemu_host_page_size & (qemu_host_page_size - 1)) != 0) {
+            fprintf(stderr, "page size must be a power of two\n");
+            exit(1);
+        }
+    }
+    if ((r = getenv("QEMU_GDB")) != NULL) {
+        gdbstub_port = atoi(r);
+    }
+    if ((r = getenv("QEMU_UNAME")) != NULL) {
+        qemu_uname_release = strdup(r);
+    }
+    if ((r = getenv("QEMU_CPU")) != NULL) {
+        cpu_model = strdup(r);
+        if (cpu_model == NULL || strcmp(cpu_model, "?") == 0) {
+/* XXX: implement xxx_cpu_list for targets that still miss it */
+#if defined(cpu_list_id)
+            cpu_list_id(stdout, &fprintf, "");
+#elif defined(cpu_list)
+            cpu_list(stdout, &fprintf); /* deprecated */
+#endif
+            exit(1);
+        }
+    }
+#if defined(CONFIG_USE_GUEST_BASE)
+    if ((r = getenv("QEMU_GUEST_BASE")) != NULL) {
+       guest_base = strtol(r, NULL, 0);
+       have_guest_base = 1;
+    }
+    if ((r = getenv("QEMU_QEMU_RESERVED_VA")) != NULL) {
+        char *p;
+        int shift = 0;
+        reserved_va = strtoul(r, &p, 0);
+        switch (*p) {
+        case 'k':
+        case 'K':
+            shift = 10;
+            break;
+        case 'M':
+            shift = 20;
+            break;
+        case 'G':
+            shift = 30;
+            break;
+        }
+        if (shift) {
+            unsigned long unshifted = reserved_va;
+            p++;
+            reserved_va <<= shift;
+            if (((reserved_va >> shift) != unshifted)
+#if HOST_LONG_BITS > TARGET_VIRT_ADDR_SPACE_BITS
+                || (reserved_va > (1ul << TARGET_VIRT_ADDR_SPACE_BITS))
+#endif
+                ) {
+                fprintf(stderr, "Reserved virtual address too big\n");
+                exit(1);
+            }
+        }
+        if (*p) {
+            fprintf(stderr, "Unrecognised -R size suffix '%s'\n", p);
+            exit(1);
+        }
+    }
+#endif
+    if ((r = getenv("QEMU_SINGLESTEP")) != NULL) {
+        singlestep = 1;
+    }
+
     optind = 1;
     for(;;) {
         if (optind >= argc)