diff --git a/linux-user/main.c b/linux-user/main.c index ea84c2de5f..e13252a007 100644 --- a/linux-user/main.c +++ b/linux-user/main.c @@ -381,30 +381,11 @@ static void handle_arg_latx_unlink(const char *arg) options_parse_latx_unlink(arg); } -static void handle_arg_latx_em_debug(const char *arg) -{ - option_em_debug = 1; -} - static void handle_arg_latx_trace(const char *arg) { options_parse_trace(arg); } -static void handle_arg_latx_check(const char *arg) -{ - option_check = 1; -} - -static void handle_arg_latx_tb_dump(const char *arg) -{ - option_dump_all_tb = 1; -} - -static void handle_arg_latx_disassemble_trace_cmp(const char *arg) -{ - options_parse_latx_disassemble_trace_cmp(arg); -} #endif @@ -810,7 +791,7 @@ struct qemu_argument { static const struct qemu_argument arg_table[] = { #ifdef CONFIG_LATX - {"latx-optimize", "LAT_OPTIMIZE", false, handle_arg_optimize, + {"latx-optimize", "LATX_OPTIMIZE", false, handle_arg_optimize, "", "specify enabled optimize type"}, {"latx-smc", "LATX_SMC", true, handle_arg_latx_smc, "", "smc strategy: 0 (page) 1 (tb) 2(+shmm) 6(+helper,default)"}, @@ -889,17 +870,8 @@ static const struct qemu_argument arg_table[] = { "", "enable all exception bit in fcsr"}, {"latx-unlink", "LATX_UNLINK", true, handle_arg_latx_unlink, "", "unlink_count[,cpu_index]"}, - {"latx-em-debug", "", false, handle_arg_latx_em_debug, - "", ""}, {"latx-trace", "", true, handle_arg_latx_trace, "bitmap", "LATX-trace-TB-execution: 2 bits each for TB,ir1,ir2"}, - {"latx-check", "", false, handle_arg_latx_check, - "", "LATX-enable-check"}, - {"latx-tb-dump", "", false, handle_arg_latx_tb_dump, - "", "LATX dump all the run times of all TB"}, - {"latx-disassemble-trace-cmp", "LATX_DISASSEMBLE_TRACE_CMP", - true, handle_arg_latx_disassemble_trace_cmp, - "", "LATX Compare different disassemble."}, #endif {"h", "", false, handle_arg_help, "", "print this help"}, @@ -1033,12 +1005,36 @@ static void usage(int exitcode) } #endif -static int parse_args(int argc, char **argv) +struct ParseOption { + const char *opt_name; + const char *opt_arg; +}; + +static struct ParseOption parse_options[128]; +static int arg_count = 0; + +void find_option(const char* key, const char* val) +{ +#define ENVFUN(NAME, name) \ + else if (!strcmp(key, #NAME)) { \ + name(val); \ + } + if (0); + ENVS + else { + printf("no option %s\n", key); + } +} + +static void options_set(void) { + /* set latx.conf */ + conf_init(exec_path); + const char *r; - int optind; const struct qemu_argument *arginfo; + /* set env */ for (arginfo = arg_table; arginfo->handle_opt != NULL; arginfo++) { if (arginfo->env == NULL) { continue; @@ -1050,6 +1046,27 @@ static int parse_args(int argc, char **argv) } } + /* set argv */ + for (int i = 0; i < arg_count; i++) { + for (arginfo = arg_table; arginfo->handle_opt != NULL; arginfo++) { + if (!strcmp(parse_options[i].opt_name, arginfo->argv)) { + if (arginfo->has_arg) { + arginfo->handle_opt(parse_options[i].opt_arg); + } else { + arginfo->handle_opt(NULL); + } + break; + } + } + } +} + +static int parse_args(int argc, char **argv) +{ + const char *r; + int optind; + const struct qemu_argument *arginfo; + optind = 1; for (;;) { if (optind >= argc) { @@ -1077,11 +1094,14 @@ static int parse_args(int argc, char **argv) "missing argument for option '%s'\n", r); exit(EXIT_FAILURE); } - arginfo->handle_opt(argv[optind]); + parse_options[arg_count].opt_name = arginfo->argv; + parse_options[arg_count].opt_arg = argv[optind]; optind++; } else { - arginfo->handle_opt(NULL); + parse_options[arg_count].opt_name = arginfo->argv; + parse_options[arg_count].opt_arg = NULL; } + arg_count++; break; } } @@ -1151,6 +1171,7 @@ int main(int argc, char **argv, char **envp) } optind = parse_args(argc, argv); + options_set(); error_init(argv[0]); module_call_init(MODULE_INIT_TRACE); diff --git a/target/i386/latx/include/latx-options.h b/target/i386/latx/include/latx-options.h index 322878370e..966d075729 100644 --- a/target/i386/latx/include/latx-options.h +++ b/target/i386/latx/include/latx-options.h @@ -6,7 +6,6 @@ #include "latx-disassemble-trace.h" #include "latx-debug.h" -extern int option_em_debug; #ifdef CONFIG_LATX_INSTS_PATTERN extern int option_instptn; #endif @@ -29,10 +28,7 @@ extern int option_dump_ir2; extern int option_dump_profile; extern int option_trace_tb; extern int option_trace_ir1; -extern int option_check; extern int option_enable_fcsr_exc; -extern int option_dump_all_tb; -extern int option_latx_disassemble_trace_cmp; extern int option_jr_ra; #define SMC_ILL_INST 0x1 /* ld.w $a1,$zero,0 */ @@ -92,6 +88,121 @@ extern unsigned long long counter_tb_tr; extern unsigned long long counter_ir1_tr; extern unsigned long long counter_mips_tr; +#ifdef CONFIG_LATX +#define ENVSUP_LATX \ + ENVFUN(LATX_OPTIMIZE, handle_arg_optimize) \ + ENVFUN(LATX_SMC, handle_arg_latx_smc) \ + ENVFUN(LATX_CLOSE_PARALLEL, handle_arg_latx_parallel) \ + ENVFUN(LATX_SOFTFPU, handle_arg_latx_softfpu) \ + ENVFUN(LATX_SOFTFPU_FAST, handle_arg_latx_softfpu_fast) \ + ENVFUN(LATX_PRLIMIT, handle_arg_latx_prlimit) \ + ENVFUN(LATX_ROUNDING_OPT, handle_arg_latx_rounding) \ + ENVFUN(LATX_CVT_OPT, handle_arg_latx_cvt_opt) \ + ENVFUN(LATX_FPUTAG, handle_arg_latx_fputag) \ + ENVFUN(SAVE_XMM, handle_arg_save_xmm) \ + ENVFUN(LATX_JRRA, handle_arg_latx_jrra) \ + ENVFUN(LATX_IMM_REG, handle_arg_latx_imm_reg) \ + ENVFUN(LATX_MT, handle_arg_latx_mem_test) \ + ENVFUN(LATX_REAL_MAPS, handle_arg_latx_real_maps) \ + ENVFUN(LATX_MONITOR_SHARED_MEM, handle_arg_latx_monitor_shared_mem) \ + ENVFUN(LATX_ANONYM, handle_arg_latx_anonym) \ + ENVFUN(LATX_MMAP_START, handle_arg_mmap_start) \ + ENVFUN(LATX_MMAP_FIXED, handle_arg_mmap_fixed) \ + ENVFUN(LATX_UNIMP_DUMP, handle_arg_latx_unimp_dump) +#else +#define ENVSUP_LATX +#endif + +#if defined(CONFIG_LATX) && defined(CONFIG_LATX_AVX_OPT) +#define ENVSUP_AVX \ + ENVFUN(LATX_AVX_CPUID, handle_arg_latx_avx_cpuid) +#else +#define ENVSUP_AVX +#endif + +#if defined(CONFIG_LATX) && defined(CONFIG_LATX_KZT) +#define ENVSUP_KZT \ + ENVFUN(LATX_KZT, handle_arg_latx_kzt) +#else +#define ENVSUP_KZT +#endif + +#if defined(CONFIG_LATX) && defined(CONFIG_LATX_AOT) +#define ENVSUP_AOT \ + ENVFUN(LATX_AOT, handle_arg_latx_aot) \ + ENVFUN(LAT_AOT_FILE_SIZE, handle_arg_lat_aot_file_size) \ + ENVFUN(LAT_AOT_LEFT_FILE_SIZE, handle_arg_lat_aot_left_file_size) \ + ENVFUN(LATX_AOT_WINE_PEFILES_CACHE, handle_arg_latx_aot_wine_pefiles_cache) +#else +#define ENVSUP_AOT +#endif + +#if defined(CONFIG_LATX_DEBUG) || defined(CONFIG_DEBUG_TCG) +#define ENVSUP_DEBUG \ + ENVFUN(LAT_GDB, handle_arg_gdb) \ + ENVFUN(LAT_STACK_SIZE, handle_arg_stack_size) \ + ENVFUN(LAT_CPU, handle_arg_cpu) \ + ENVFUN(LAT_SET_ENV, handle_arg_set_env) \ + ENVFUN(LAT_UNSET_ENV, handle_arg_unset_env) \ + ENVFUN(LAT_ARGV0, handle_arg_argv0) \ + ENVFUN(LAT_UNAME, handle_arg_uname) \ + ENVFUN(LAT_GUEST_BASE, handle_arg_guest_base) \ + ENVFUN(LAT_RESERVED_VA, handle_arg_reserved_va) \ + ENVFUN(LAT_LOG, handle_arg_log) \ + ENVFUN(LAT_IMM_SKIP_PC, handle_arg_imm_skip_pc) \ + ENVFUN(LAT_DFILTER, handle_arg_dfilter) \ + ENVFUN(LAT_LOG_FILENAME, handle_arg_log_filename) \ + ENVFUN(LAT_PAGESIZE, handle_arg_pagesize) \ + ENVFUN(LAT_SINGLESTEP, handle_arg_singlestep) \ + ENVFUN(LAT_STRACE, handle_arg_strace) \ + ENVFUN(LAT_STRACE_ERROR, handle_arg_strace_error) \ + ENVFUN(LAT_RAND_SEED, handle_arg_seed) \ + ENVFUN(LAT_TRACE, handle_arg_trace) \ + ENVFUN(LAT_LD_PREFIX, handle_arg_ld_prefix) \ + ENVFUN(LAT_VERSION, handle_arg_version) +#else +#define ENVSUP_DEBUG +#endif + +#if (defined(CONFIG_LATX_DEBUG) || defined(CONFIG_DEBUG_TCG)) && defined(CONFIG_LATX) +#define ENVSUP_LATX_DEBUG \ + ENVFUN(LATX_BEGIN_TRACE, handle_range_trace_begin) \ + ENVFUN(LATX_END_TRACE, handle_range_trace_end) \ + ENVFUN(LATX_DUMP, handle_arg_latx_print) \ + ENVFUN(LATX_SHOW_TB, handle_arg_latx_show_tb) \ + ENVFUN(LATX_TRACE_MEM, handle_arg_latx_trace_mem) \ + ENVFUN(LATX_BREAK_INSN, handle_arg_latx_break_insn) \ + ENVFUN(LATX_DEBUG_LATIVE, handle_arg_debug_lative) \ + ENVFUN(LATX_ENABLE_FCSR_EXC, handle_arg_enable_fcsr_exc) \ + ENVFUN(LATX_UNLINK, handle_arg_latx_unlink) +#else +#define ENVSUP_LATX_DEBUG +#endif + +#if (defined(CONFIG_LATX_DEBUG) || defined(CONFIG_DEBUG_TCG)) && defined(CONFIG_PLUGIN) +#define ENVSUP_PLUGIN \ + ENVFUN(LAT_PLUGIN, handle_arg_plugin) +#else +#define ENVSUP_PLUGIN +#endif + +#if defined(TARGET_XTENSA) +#define ENVSUP_XTENSA \ + ENVFUN(QEMU_XTENSA_ABI_CALL0, handle_arg_abi_call0) +#else +#define ENVSUP_XTENSA +#endif + +#define ENVS \ + ENVSUP_LATX \ + ENVSUP_AVX \ + ENVSUP_KZT \ + ENVSUP_AOT \ + ENVSUP_DEBUG \ + ENVSUP_LATX_DEBUG \ + ENVSUP_PLUGIN \ + ENVSUP_XTENSA + void options_init(void); void options_parse_opt(const char *opt); void options_parse_imm_reg(const char *bits); @@ -104,4 +215,10 @@ void options_parse_latx_unlink(const char *arg); void options_parse_trace(const char *bits); uint8 options_to_save(void); void options_parse_latx_disassemble_trace_cmp(const char *args); +void conf_init(const char *program); +void load_conf_file(const char *file, const char* program); +char* trim_name(const char *program); +void find_option(const char *name, const char *val); +int option_line_init(char *line, char **name, char **value); +char* trim(char *s); #endif diff --git a/target/i386/latx/latx-config.c b/target/i386/latx/latx-config.c index 751d797ec2..7c3b2ead4c 100644 --- a/target/i386/latx/latx-config.c +++ b/target/i386/latx/latx-config.c @@ -504,15 +504,11 @@ void latx_guest_stack_init(CPUArchState *env) #endif void latx_dt_init(void) { -#ifdef CONFIG_LATX_DEBUG - disassemble_trace_init(TARGET_ABI_BITS, option_latx_disassemble_trace_cmp); -#else #ifdef CONFIG_LATX_CAPSTONE_GIT gitcapstone_init(TARGET_ABI_BITS); #else lacapstone_init(TARGET_ABI_BITS); #endif -#endif } void latx_init_fpu_regs(CPUArchState *env) diff --git a/target/i386/latx/latx-disassemble-trace/latx-disassemble-trace.c b/target/i386/latx/latx-disassemble-trace/latx-disassemble-trace.c index e184a6e2a9..a3c3b575ae 100644 --- a/target/i386/latx/latx-disassemble-trace/latx-disassemble-trace.c +++ b/target/i386/latx/latx-disassemble-trace/latx-disassemble-trace.c @@ -7,52 +7,6 @@ int (*la_disa_v2)(const uint8_t *code, size_t code_size, uint64_t address, size_t count, struct la_dt_insn **insn, int ir1_num, void *pir1_base, int mode) = NULL; -void disassemble_trace_init(int abi_bits, int args) -{ - dt_mode = abi_bits; - switch (args & 0xffff) { - case 0: - break; - case OPT_V1NEXTCAPSTONE: - la_disa_v1 = &gitcapstone_get; - break; - case OPT_V1LACAPSTONE: - la_disa_v1 = &gitcapstone_get; - break; - case OPT_V1LAXED: - la_disa_v1 = &laxed_get; - break; - case OPT_V1LAZYDIS: - la_disa_v1 = &lazydis_get; - break; - default: - lsassertm(0, "invalid V1."); - } - switch (args & 0xffff0000) { - case 0: - break; - case OPT_V2LACAPSTONE: - la_disa_v2 = &gitcapstone_get; - break; - case OPT_V2NEXTCAPSTONE: - la_disa_v2 = &gitcapstone_get; - break; - case OPT_V2LAXED: - la_disa_v2 = &laxed_get; - break; - case OPT_V2LAZYDIS: - la_disa_v2 = &lazydis_get; - break; - default: - dtassert(0); - } - if (la_disa_v2) { - disassemble_trace_cmp = &disassemble_trace_loop; - } - gitcapstone_init(abi_bits); - laxed_init(abi_bits); - lazydis_init(abi_bits); -} static int64_t imm_cast(int64_t imm, uint32_t size) { switch (size) { diff --git a/target/i386/latx/latx-options.c b/target/i386/latx/latx-options.c index 6d70bda9f6..4d5a573dec 100644 --- a/target/i386/latx/latx-options.c +++ b/target/i386/latx/latx-options.c @@ -57,10 +57,6 @@ int option_dump_ir2; int option_dump_profile; int option_trace_tb; int option_trace_ir1; -int option_check; -int option_em_debug; -int option_dump_all_tb; -int option_latx_disassemble_trace_cmp; int option_debug_lative; int option_aot; int option_load_aot; @@ -100,6 +96,94 @@ unsigned long long counter_tb_tr; unsigned long long counter_ir1_tr; unsigned long long counter_mips_tr; +char* trim(char *s) +{ + while (isspace((unsigned char)*s)) s++; + char *end = s + strlen(s) - 1; + while (isspace((unsigned char)*end)) end--; + end[1] = '\0'; + return s; +} + +// find env var and value +int option_line_init(char *line, char **name, char **value) +{ + while (isspace((unsigned char)*line)) line++; + if (*line == '#' || *line == '\0') { return false; } + char *eq = strchr(line, '='); + if (eq == NULL) { return false; } + *eq = '\0'; + *name = trim(line); + *value = trim(eq + 1); + return true; +} + +char* trim_name(const char *program) +{ + size_t len = strlen(program); + char *name = malloc(len + 1); + len = strlen(program); + name = malloc(len + 1); + memcpy(name, program, len); + name[len] = '\0'; + while (*name && !isalnum((unsigned char)*name) ) { + ++name; + } + return name; +} + +void load_conf_file(const char *file, const char *program) +{ + FILE *fp = fopen(file, "r"); + if (!program || !fp) + return; + + char *pro_name = trim_name(program); + char *line = NULL, *name = NULL; + size_t len = 0; + int flag = 0; + + while(getline(&line, &len, fp) != -1) { + char *option_name, *option_value; + // env var + if(flag != 1 && option_line_init(line, &option_name, &option_value)) { + find_option(option_name, option_value); + } + // guest name + if (line[0] == '[' && strchr(line, ']')) { + if (flag == 2) + break; + size_t size = strchr(line, ']') - strchr(line, '[') - 1; + name = malloc(size + 1); + memcpy(name, line + 1,size); + name[size] = '\0'; + if (!strcmp(name, pro_name)) { + flag = 2; + } + else {flag = 1;} + } + } + free(line); +} + +void conf_init(const char *program) +{ + char path[PATH_MAX]; + + const char *home_path = getenv("HOME"); +#ifdef TARGET_X86_64 + /* load /etc/latx-*.conf */ + load_conf_file("/etc/latx-x86_64.conf", program); + snprintf(path, PATH_MAX, "%s/.config/latx-x86_64.conf", home_path); +#else + load_conf_file("/etc/latx-i386.conf", program); + snprintf(path, PATH_MAX, "%s/.config/latx-i386.conf", home_path); +#endif + + /* load ~/.config/latx-*.conf */ + load_conf_file(path, program); +} + void options_init(void) { option_debug_lative = 0; @@ -110,9 +194,6 @@ void options_init(void) option_dump = 0; option_trace_tb = 0; option_trace_ir1 = 0; - option_check = 0; - option_dump_all_tb = 0; - option_latx_disassemble_trace_cmp = 0; option_enable_lasx = 1; counter_tb_exec = 0; @@ -346,49 +427,6 @@ void options_parse_trace(const char *bits) } } -void options_parse_latx_disassemble_trace_cmp(const char *args) -{ - if (!args) { - return; - } - char *findSpil = strstr(args, ":"); - if (!findSpil) { - lsassertm(0, "Can't find \':\' from args.\n"); - return; - } - char strtmp[100] = {0}; - option_latx_disassemble_trace_cmp = 0; - lsassert(findSpil - args <= 100); - strncpy(strtmp, args, findSpil - args); - if (!strcmp(strtmp, "lacapstone")) { - option_latx_disassemble_trace_cmp |= OPT_V1LACAPSTONE; - } else if (!strcmp(strtmp, "nextcapstone")) { - option_latx_disassemble_trace_cmp |= OPT_V1NEXTCAPSTONE; - } else if (!strcmp(strtmp, "laxed")) { - option_latx_disassemble_trace_cmp |= OPT_V1LAXED; - } else if (!strcmp(strtmp, "lazydis")) { - option_latx_disassemble_trace_cmp |= OPT_V1LAZYDIS; - } else { - lsassertm(0, "V1 must be lacapstone, lazydis, " - "nextcapstone or laxed, but this V1=%s\n", strtmp); - return; - } - strncpy(strtmp, findSpil + 1, 99); - if (!strcmp(strtmp, "lacapstone")) { - option_latx_disassemble_trace_cmp |= OPT_V2LACAPSTONE; - } else if (!strcmp(strtmp, "nextcapstone")) { - option_latx_disassemble_trace_cmp |= OPT_V2NEXTCAPSTONE; - } else if (!strcmp(strtmp, "laxed")) { - option_latx_disassemble_trace_cmp |= OPT_V2LAXED; - } else if (!strcmp(strtmp, "lazydis")) { - option_latx_disassemble_trace_cmp |= OPT_V2LAZYDIS; - } else if (strlen(strtmp) == 0) { - /*Only want to choose la_disa_v1, No cmp.*/ - } else { - lsassertm(0, "V2 must be lacapstone, lazydis, " - "nextcapstone, laxed or NULL, but this V2=%s\n", strtmp); - } -} uint8 options_to_save(void) { uint8 option_bitmap = 0;