Received: from eggs.gnu.org ([208.118.235.92]:52322) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1US6JD-000654-Cm for qemu-devel@nongnu.org; Tue, 16 Apr 2013 09:52:38 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1US6ID-0003h7-KT for qemu-devel@nongnu.org; Tue, 16 Apr 2013 09:51:36 -0400 Received: from roura.ac.upc.es ([147.83.33.10]:44909) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1US6IC-0003gd-VD for qemu-devel@nongnu.org; Tue, 16 Apr 2013 09:51:29 -0400 Received: from gw.ac.upc.edu (gw.ac.upc.es [147.83.30.3]) by roura.ac.upc.es (8.13.8/8.13.8) with ESMTP id r3GDpSkZ030925 for ; Tue, 16 Apr 2013 15:51:28 +0200 Received: from localhost (unknown [84.88.51.85]) by gw.ac.upc.edu (Postfix) with ESMTP id E6E646B01C9 for ; Tue, 16 Apr 2013 15:51:27 +0200 (CEST) From: =?utf-8?b?TGx1w61z?= Vilanova Date: Tue, 16 Apr 2013 15:51:27 +0200 Message-Id: <20130416135126.21588.39829.stgit@fimbulvetr.bsc.es> In-Reply-To: <20130416134949.21588.80064.stgit@fimbulvetr.bsc.es> References: <20130416134949.21588.80064.stgit@fimbulvetr.bsc.es> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Subject: [Qemu-devel] [PATCH v2 17/23] instrument: Add commandline options to start with an instrumentation library List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Add commandline options to control initial loading of dynamic instrumenta= tion library. Signed-off-by: Llu=C3=ADs Vilanova --- bsd-user/main.c | 25 +++++++++++ bsd-user/syscall.c | 5 ++ instrument/Makefile.objs | 2 + instrument/cmdline.c | 101 ++++++++++++++++++++++++++++++++++++++++= ++++++ instrument/cmdline.h | 52 ++++++++++++++++++++++++ linux-user/main.c | 30 ++++++++++++++ linux-user/syscall.c | 4 ++ qemu-options.hx | 18 ++++++++ vl.c | 41 +++++++++++++++++++ 9 files changed, 278 insertions(+) create mode 100644 instrument/cmdline.c create mode 100644 instrument/cmdline.h diff --git a/bsd-user/main.c b/bsd-user/main.c index cc84981..489168a 100644 --- a/bsd-user/main.c +++ b/bsd-user/main.c @@ -33,6 +33,8 @@ #include "tcg.h" #include "qemu/timer.h" #include "qemu/envlist.h" +#include "instrument/cmdline.h" + =20 int singlestep; #if defined(CONFIG_USE_GUEST_BASE) @@ -688,6 +690,15 @@ static void usage(void) #endif "-bsd type select emulated BSD type FreeBSD/NetBSD/Op= enBSD (default)\n" "\n" +#if defined(CONFIG_TRACE_INSTRUMENT) + "Tracing options:\n" +#if defined(CONFIG_TRACE_INSTRUMENT_DYNAMIC) + "-instr path load a dynamic trace instrumentation libra= ry\n" +#endif + "-instr-arg string\n" + " argument to dynamic trace instrumentation = library (can be given multiple times)\n" + "\n" +#endif "Debug options:\n" "-d item1[,...] enable logging of specified items\n" " (use '-d help' for a list of log items)\n" @@ -743,6 +754,9 @@ int main(int argc, char **argv) char **target_environ, **wrk; envlist_t *envlist =3D NULL; bsd_type =3D target_openbsd; + char *instrument_path =3D NULL; + int instrument_argc =3D 0; + char **instrument_argv =3D NULL; =20 if (argc <=3D 1) usage(); @@ -852,6 +866,14 @@ int main(int argc, char **argv) singlestep =3D 1; } else if (!strcmp(r, "strace")) { do_strace =3D 1; +#if defined(CONFIG_TRACE_INSTRUMENT_DYNAMIC) + } else if (!strcmp(r, "instr")) { + instrument_path =3D argv[optind++]; +#endif +#if defined(CONFIG_TRACE_INSTRUMENT) + } else if (!strcmp(r, "instr-arg")) { + instr_parse_args(argv[optind++], &instrument_argc, &instrume= nt_argv); +#endif } else { usage(); @@ -1135,6 +1157,9 @@ int main(int argc, char **argv) gdbserver_start (gdbstub_port); gdb_handlesig(env, 0); } + + instr_init(instrument_path, instrument_argc, instrument_argv); + cpu_loop(env); /* never exits */ return 0; diff --git a/bsd-user/syscall.c b/bsd-user/syscall.c index 18b43f1..dbb6816 100644 --- a/bsd-user/syscall.c +++ b/bsd-user/syscall.c @@ -35,6 +35,8 @@ =20 #include "qemu.h" #include "qemu-common.h" +#include "instrument/cmdline.h" + =20 //#define DEBUG =20 @@ -334,6 +336,7 @@ abi_long do_freebsd_syscall(void *cpu_env, int num, a= bi_long arg1, _mcleanup(); #endif gdb_exit(cpu_env, arg1); + instr_fini(); /* XXX: should free thread stack and CPU env */ _exit(arg1); ret =3D 0; /* avoid warning */ @@ -429,6 +432,7 @@ abi_long do_netbsd_syscall(void *cpu_env, int num, ab= i_long arg1, _mcleanup(); #endif gdb_exit(cpu_env, arg1); + instr_fini(); /* XXX: should free thread stack and CPU env */ _exit(arg1); ret =3D 0; /* avoid warning */ @@ -501,6 +505,7 @@ abi_long do_openbsd_syscall(void *cpu_env, int num, a= bi_long arg1, _mcleanup(); #endif gdb_exit(cpu_env, arg1); + instr_fini(); /* XXX: should free thread stack and CPU env */ _exit(arg1); ret =3D 0; /* avoid warning */ diff --git a/instrument/Makefile.objs b/instrument/Makefile.objs index e3d4e9b..453330f 100644 --- a/instrument/Makefile.objs +++ b/instrument/Makefile.objs @@ -65,6 +65,8 @@ endif ###################################################################### # Control code =20 +target-obj-y +=3D cmdline.o + target-obj-y +=3D control.o =20 common-obj-$(CONFIG_SOFTMMU) +=3D hmp.o diff --git a/instrument/cmdline.c b/instrument/cmdline.c new file mode 100644 index 0000000..0f52f60 --- /dev/null +++ b/instrument/cmdline.c @@ -0,0 +1,101 @@ +/* + * Control dynamic trace instrumentation during program (de)initializati= on. + * + * Copyright (C) 2012-2013 Llu=C3=ADs Vilanova + * + * This work is licensed under the terms of the GNU GPL, version 2 or la= ter. + * See the COPYING file in the top-level directory. + */ + +#include "instrument/cmdline.h" + +#include + +#include "qemu-common.h" +#include "instrument/control.h" + +#if defined(CONFIG_TRACE_INSTRUMENT_STATIC) +#include "instrument/qemu-instr/events.h" +#endif + + +static bool loaded; + + +void instr_parse_args(const char *args, int *argc, const char ***argv) +{ + *argv =3D realloc(*argv, sizeof(**argv) * (*argc + 1)); + (*argv)[*argc] =3D args; + (*argc)++; +} + +void instr_init(const char *path, int argc, const char **argv) +{ + if (atexit(instr_fini) !=3D 0) { + fprintf(stderr, "error: atexit: %s\n", strerror(errno)); + abort(); + } + + if (path =3D=3D NULL) { +#if defined(CONFIG_TRACE_INSTRUMENT_STATIC) + loaded =3D true; + qi_init(argc, argv); +#endif + return; + } + + InstrLoadError err =3D instr_load(path, argc, argv); + switch (err) { + case INSTR_LOAD_OK: + loaded =3D true; + return; + case INSTR_LOAD_UNAVAILABLE: + fprintf(stderr, "error: instrument: not available\n"); + break; + case INSTR_LOAD_LOADED: + fprintf(stderr, "error: instrument: already loaded\n"); + break; + case INSTR_LOAD_DL: +#if defined(CONFIG_TRACE_INSTRUMENT_DYNAMIC) + fprintf(stderr, "error: instrument: error loading library: %s\n"= , dlerror()); +#else + abort(); +#endif + break; + } + + exit(1); +} + +void instr_fini(void) +{ + if (!loaded) { + return; + } + +#if defined(CONFIG_TRACE_INSTRUMENT_STATIC) + qi_fini(); + return; +#endif + + InstrUnloadError err =3D instr_unload(); + switch (err) { + case INSTR_UNLOAD_OK: + return; + case INSTR_UNLOAD_UNAVAILABLE: + fprintf(stderr, "error: not available\n"); + break; + case INSTR_UNLOAD_UNLOADED: + /* the user might have already unloaded it */ + return; + case INSTR_UNLOAD_DL: +#if defined(CONFIG_TRACE_INSTRUMENT_DYNAMIC) + fprintf(stderr, "error: error unloading library: %s\n", dlerror(= )); +#else + abort(); +#endif + break; + } + + exit(1); +} diff --git a/instrument/cmdline.h b/instrument/cmdline.h new file mode 100644 index 0000000..b096793 --- /dev/null +++ b/instrument/cmdline.h @@ -0,0 +1,52 @@ +/* + * Control dynamic trace instrumentation during program (de)initializati= on. + * + * Copyright (C) 2012-2013 Llu=C3=ADs Vilanova + * + * This work is licensed under the terms of the GNU GPL, version 2 or la= ter. + * See the COPYING file in the top-level directory. + */ + +#ifndef INSTRUMENT__CMDLINE_H +#define INSTRUMENT__CMDLINE_H + +/** + * instr_parse_args: + * @args: Input arguments. + * @argc: Output argument count. + * @argv: Output argument array. + * + * Adds element @args into the argument array. + */ +void instr_parse_args(const char *args, int *argc, const char ***argv); + +/** + * instr_init: + * @path: Path to dynamic trace instrumentation library. + * @argc: Number of arguments to the library's #qi_init routine. + * @argv: Arguments to the library's #qi_init routine. + * + * Load and initialize the given instrumentation library. + * + * Automatically installs #instr_fini as an atexit callback. + * + * If path is %NULL and we're running with static instrumentation, the l= ibrary + * is not loaded but just initialized. + * + * Pre-condition: There is no library already loaded. + */ +void instr_init(const char *path, int argc, const char **argv); + +/** + * instr_fini: + * + * Deinitialize and unload the current instrumentation library. + * + * If we're running with static instrumentation, the library is not unlo= aded but + * just deinitialized. + * + * Pre-condition: There is an already loaded library. + */ +void instr_fini(void); + +#endif /* INSTRUMENT__CMDLINE_H */ diff --git a/linux-user/main.c b/linux-user/main.c index 4e92a0b..9216e69 100644 --- a/linux-user/main.c +++ b/linux-user/main.c @@ -34,6 +34,8 @@ #include "qemu/timer.h" #include "qemu/envlist.h" #include "elf.h" +#include "instrument/cmdline.h" + =20 char *exec_path; =20 @@ -3327,6 +3329,23 @@ static void handle_arg_reserved_va(const char *arg= ) } #endif =20 +static const char *instrument_path; +#if defined(CONFIG_TRACE_INSTRUMENT_DYNAMIC) +static void handle_arg_instrument(const char *arg) +{ + instrument_path =3D arg; +} +#endif + +static int instrument_argc =3D 0; +static const char **instrument_argv =3D NULL; +#if defined(CONFIG_TRACE_INSTRUMENT) +static void handle_arg_instrument_arg(const char *arg) +{ + instr_parse_args(arg, &instrument_argc, &instrument_argv); +} +#endif + static void handle_arg_singlestep(const char *arg) { singlestep =3D 1; @@ -3378,6 +3397,14 @@ static const struct qemu_argument arg_table[] =3D = { {"R", "QEMU_RESERVED_VA", true, handle_arg_reserved_va, "size", "reserve 'size' bytes for guest virtual address space= "}, #endif +#if defined(CONFIG_TRACE_INSTRUMENT_DYNAMIC) + {"instr", "QEMU_INSTR", true, handle_arg_instrument, + "path", "load a dynamic trace instrumentation library"}, +#endif +#if defined(CONFIG_TRACE_INSTRUMENT) + {"instr-arg", "QEMU_INSTR_ARGS", true, handle_arg_instrument_arg, + "string", "argument to dynamic trace instrumentation library (c= an be given multiple times)"}, +#endif {"d", "QEMU_LOG", true, handle_arg_log, "item[,...]", "enable logging of specified items " "(use '-d help' for a list of items)"}, @@ -4071,6 +4098,9 @@ int main(int argc, char **argv, char **envp) } gdb_handlesig(env, 0); } + + instr_init(instrument_path, instrument_argc, instrument_argv); + cpu_loop(env); /* never exits */ return 0; diff --git a/linux-user/syscall.c b/linux-user/syscall.c index 1f07621..d55af6c 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -109,6 +109,8 @@ int __clone2(int (*fn)(void *), void *child_stack_bas= e, #include "cpu-uname.h" =20 #include "qemu.h" +#include "instrument/cmdline.h" + =20 #if defined(CONFIG_USE_NPTL) #define CLONE_NPTL_FLAGS2 (CLONE_SETTLS | \ @@ -5248,6 +5250,7 @@ abi_long do_syscall(void *cpu_env, int num, abi_lon= g arg1, _mcleanup(); #endif gdb_exit(cpu_env, arg1); + instr_fini(); _exit(arg1); ret =3D 0; /* avoid warning */ break; @@ -7028,6 +7031,7 @@ abi_long do_syscall(void *cpu_env, int num, abi_lon= g arg1, _mcleanup(); #endif gdb_exit(cpu_env, arg1); + instr_fini(); ret =3D get_errno(exit_group(arg1)); break; #endif diff --git a/qemu-options.hx b/qemu-options.hx index 7cd6002..0f36c1c 100644 --- a/qemu-options.hx +++ b/qemu-options.hx @@ -3021,6 +3021,24 @@ the @var{simple} tracing backend. @end table ETEXI =20 +DEF("instr", HAS_ARG, QEMU_OPTION_instr, + "-instr file=3D[,arg=3D]\n" + " load an instrumentation library\n", + QEMU_ARCH_ALL) +STEXI +@item -instr file=3D@var{file}[,arg=3D@var{string}] +@findex -instr + +Load a dynamic trace instrumentation library. + +@table @option +@item file=3D@var{file} +Load the given dynamic trace instrumentation library. +@item arg=3D@var{string} +String argument passed as to the library's @code{qi_init} routine (can b= e given multiple times). +@end table +ETEXI + HXCOMM Internal use DEF("qtest", HAS_ARG, QEMU_OPTION_qtest, "", QEMU_ARCH_ALL) DEF("qtest-log", HAS_ARG, QEMU_OPTION_qtest_log, "", QEMU_ARCH_ALL) diff --git a/vl.c b/vl.c index d694a90..e773bb4 100644 --- a/vl.c +++ b/vl.c @@ -163,6 +163,7 @@ int main(int argc, char **argv) =20 #include "trace.h" #include "trace/control.h" +#include "instrument/cmdline.h" #include "qemu/queue.h" #include "sysemu/cpus.h" #include "sysemu/arch_init.h" @@ -352,6 +353,22 @@ static QemuOptsList qemu_trace_opts =3D { }, }; =20 +static QemuOptsList qemu_instr_opts =3D { + .name =3D "instr", + .implied_opt_name =3D "instr", + .head =3D QTAILQ_HEAD_INITIALIZER(qemu_instr_opts.head), + .desc =3D { + { + .name =3D "file", + .type =3D QEMU_OPT_STRING, + },{ + .name =3D "arg", + .type =3D QEMU_OPT_STRING, + }, + { /* end of list */ } + }, +}; + static QemuOptsList qemu_option_rom_opts =3D { .name =3D "option-rom", .implied_opt_name =3D "romfile", @@ -2883,6 +2900,9 @@ int main(int argc, char **argv, char **envp) }; const char *trace_events =3D NULL; const char *trace_file =3D NULL; + const char *instrument_path =3D NULL; + int instrument_argc =3D 0; + const char **instrument_argv =3D NULL; =20 atexit(qemu_run_exit_notifiers); error_set_progname(argv[0]); @@ -2908,6 +2928,7 @@ int main(int argc, char **argv, char **envp) qemu_add_opts(&qemu_global_opts); qemu_add_opts(&qemu_mon_opts); qemu_add_opts(&qemu_trace_opts); + qemu_add_opts(&qemu_instr_opts); qemu_add_opts(&qemu_option_rom_opts); qemu_add_opts(&qemu_machine_opts); qemu_add_opts(&qemu_boot_opts); @@ -3820,6 +3841,24 @@ int main(int argc, char **argv, char **envp) trace_file =3D qemu_opt_get(opts, "file"); break; } + case QEMU_OPTION_instr: + { + olist =3D qemu_find_opts("instr"); + if (!olist) { + exit(1); + } + opts =3D qemu_opts_parse(olist, optarg, 0); + if (!opts) { + exit(1); + } + const char * path =3D qemu_opt_get(opts, "file"); + if (path !=3D NULL) { + instrument_path =3D path; + } + instr_parse_args(qemu_opt_get(opts, "arg"), + &instrument_argc, &instrument_argv); + break; + } case QEMU_OPTION_readconfig: { int ret =3D qemu_read_config_file(optarg); @@ -4414,6 +4453,8 @@ int main(int argc, char **argv, char **envp) } } =20 + instr_init(instrument_path, instrument_argc, instrument_argv); + if (incoming) { Error *local_err =3D NULL; qemu_start_incoming_migration(incoming, &local_err);