Patchwork [v5,02/10] trace: avoid conditional code compilation during option parsing

login
register
mail settings
Submitter =?utf-8?Q?Llu=C3=ADs?=
Date June 28, 2011, 4:53 p.m.
Message ID <20110628165308.23482.66207.stgit@ginnungagap.bsc.es>
Download mbox | patch
Permalink /patch/102425/
State New
Headers show

Comments

=?utf-8?Q?Llu=C3=ADs?= - June 28, 2011, 4:53 p.m.
Instead of conditionally compiling option support, perform checks and issue the
corresponding error messages.

Signed-off-by: Lluís Vilanova <vilanova@ac.upc.edu>
---
 configure       |    4 +++-
 qemu-config.c   |    4 ----
 qemu-options.hx |    6 ++++--
 vl.c            |   17 +++++++++++++----
 4 files changed, 20 insertions(+), 11 deletions(-)
Stefan Hajnoczi - July 15, 2011, 9:41 a.m.
On Tue, Jun 28, 2011 at 06:53:08PM +0200, Lluís wrote:
> diff --git a/vl.c b/vl.c
> index b2f41fd..b766dc7 100644
> --- a/vl.c
> +++ b/vl.c
> @@ -2861,14 +2861,23 @@ int main(int argc, char **argv, char **envp)
>                  }
>                  xen_mode = XEN_ATTACH;
>                  break;
> -#ifdef CONFIG_SIMPLE_TRACE
>              case QEMU_OPTION_trace:
>                  opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
> -                if (opts) {
> -                    trace_file = qemu_opt_get(opts, "file");
> +                if (!opts) {
> +                    exit(1);
>                  }
> -                break;
> +#if defined(CONFIG_TRACE_NOP)
> +                fprintf(stderr, "qemu: option \"-%s\" is not supported by this tracing backend\n", popt->name);
> +                exit(1);
>  #endif
> +                trace_file = qemu_opt_get(opts, "file");
> +#if !defined(CONFIG_SIMPLE_TRACE)
> +                if (trace_file) {
> +                    fprintf(stderr, "qemu: suboption \"-%s file\" is not supported by this tracing backend\n", popt->name);
> +                    exit(1);
> +                }
> +#endif

This doesn't account for other trace backends.  For example with dtrace
or ust the option would be compiled in and not rejected at runtime.

Instead of checking all possible trace backend CONFIG_* we could always
compile the -trace option in.  Specific backends can validate their
options (if used).  This approach will not reject unused options but
it's simple and doesn't use #ifdefs.

Stefan

Patch

diff --git a/configure b/configure
index 88159ac..e41dcca 100755
--- a/configure
+++ b/configure
@@ -2942,7 +2942,9 @@  bsd)
 esac
 
 echo "TRACE_BACKEND=$trace_backend" >> $config_host_mak
-if test "$trace_backend" = "simple"; then
+if test "$trace_backend" = "nop"; then
+  echo "CONFIG_TRACE_NOP=y" >> $config_host_mak
+elif test "$trace_backend" = "simple"; then
   echo "CONFIG_SIMPLE_TRACE=y" >> $config_host_mak
 fi
 # Set the appropriate trace file.
diff --git a/qemu-config.c b/qemu-config.c
index c63741c..d187dc5 100644
--- a/qemu-config.c
+++ b/qemu-config.c
@@ -296,7 +296,6 @@  static QemuOptsList qemu_mon_opts = {
     },
 };
 
-#ifdef CONFIG_SIMPLE_TRACE
 static QemuOptsList qemu_trace_opts = {
     .name = "trace",
     .implied_opt_name = "trace",
@@ -309,7 +308,6 @@  static QemuOptsList qemu_trace_opts = {
         { /* end of list */ }
     },
 };
-#endif
 
 static QemuOptsList qemu_cpudef_opts = {
     .name = "cpudef",
@@ -479,9 +477,7 @@  static QemuOptsList *vm_config_groups[32] = {
     &qemu_global_opts,
     &qemu_mon_opts,
     &qemu_cpudef_opts,
-#ifdef CONFIG_SIMPLE_TRACE
     &qemu_trace_opts,
-#endif
     &qemu_option_rom_opts,
     &qemu_machine_opts,
     NULL,
diff --git a/qemu-options.hx b/qemu-options.hx
index 37e54ee..b691e13 100644
--- a/qemu-options.hx
+++ b/qemu-options.hx
@@ -2393,17 +2393,19 @@  Normally QEMU loads a configuration file from @var{sysconfdir}/qemu.conf and
 @var{sysconfdir}/target-@var{ARCH}.conf on startup.  The @code{-nodefconfig}
 option will prevent QEMU from loading these configuration files at startup.
 ETEXI
-#ifdef CONFIG_SIMPLE_TRACE
 DEF("trace", HAS_ARG, QEMU_OPTION_trace,
     "-trace\n"
     "                Specify a trace file to log traces to\n",
     QEMU_ARCH_ALL)
 STEXI
+HXCOMM This line is not accurate, as the option is backend-specific but HX does
+HXCOMM not support conditional compilation of text.
 @item -trace
 @findex -trace
 Specify a trace file to log output traces to.
+
+This option is available only when using the @var{simple} tracing backend.
 ETEXI
-#endif
 
 HXCOMM This is the last statement. Insert new options before this line!
 STEXI
diff --git a/vl.c b/vl.c
index b2f41fd..b766dc7 100644
--- a/vl.c
+++ b/vl.c
@@ -2861,14 +2861,23 @@  int main(int argc, char **argv, char **envp)
                 }
                 xen_mode = XEN_ATTACH;
                 break;
-#ifdef CONFIG_SIMPLE_TRACE
             case QEMU_OPTION_trace:
                 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
-                if (opts) {
-                    trace_file = qemu_opt_get(opts, "file");
+                if (!opts) {
+                    exit(1);
                 }
-                break;
+#if defined(CONFIG_TRACE_NOP)
+                fprintf(stderr, "qemu: option \"-%s\" is not supported by this tracing backend\n", popt->name);
+                exit(1);
 #endif
+                trace_file = qemu_opt_get(opts, "file");
+#if !defined(CONFIG_SIMPLE_TRACE)
+                if (trace_file) {
+                    fprintf(stderr, "qemu: suboption \"-%s file\" is not supported by this tracing backend\n", popt->name);
+                    exit(1);
+                }
+#endif
+                break;
             case QEMU_OPTION_readconfig:
                 {
                     int ret = qemu_read_config_file(optarg);