/* Disable all functions of a trace * @param trace trace name * @see etrace_funcsettatus */ int etrace_funcdisableall(char *trace) { u_int index; int keynbr; char **keys; hash_t *table; PROFILER_IN(__FILE__, __FUNCTION__, __LINE__); if (!trace) trace = ETRACE_TYPE_DEFAULT; etrace_init_trace(); keys = hash_get_keys(&traces_table, &keynbr); if (keys) { for (index = 0; index < keynbr; index++) { table = (hash_t *) hash_get(&traces_table, keys[index]); if (table) etrace_funcsetstatus(table, 0); } hash_free_keys(keys); } PROFILER_ROUT(__FILE__, __FUNCTION__, __LINE__, 0); }
/** * Print dependence * @param obj */ static int revm_dolist_dep(elfshobj_t *obj) { elfshobj_t *actual; char logbuf[20]; char **keys; int keynbr; int index; PROFILER_IN(__FILE__, __FUNCTION__, __LINE__); if (!obj) PROFILER_ERR(__FILE__, __FUNCTION__, __LINE__, "Invalid argument", -1); if (hash_size(&obj->child_hash)) { keys = hash_get_keys(&obj->child_hash, &keynbr); revm_output("DEPS = ["); for (index = 0; index < keynbr; index++) { actual = hash_get(&obj->child_hash, keys[index]); snprintf(logbuf, sizeof(logbuf), "%s%u", (index == 0 ? "" : ","), actual->id); revm_output(logbuf); } revm_output("]"); } PROFILER_ROUT(__FILE__, __FUNCTION__, __LINE__, 0); }
/** * match_breakpoints_files * Check the Tracer#breakpoints_cache if any breakpoints match the given * tracepoint_path. Return 1 if found. Otherwise 0; */ static int match_breakpoints_files(VALUE self, VALUE tracepoint_path) { int i; char *c_tracepoint_path; VALUE path_breakpoints_hash; VALUE breakpoints_paths; VALUE *c_breakpoints_paths; int breakpoints_paths_len; // Return 0 if the given path is Qnil if(!RTEST(tracepoint_path)) { return 0; } c_tracepoint_path = rb_string_value_cstr(&tracepoint_path); path_breakpoints_hash = rb_iv_get(self, "@breakpoints_cache"); breakpoints_paths = hash_get_keys(path_breakpoints_hash); c_breakpoints_paths = RARRAY_PTR(breakpoints_paths); breakpoints_paths_len = RARRAY_LEN(breakpoints_paths); for (i = 0; i < breakpoints_paths_len; i++) { VALUE breakpoint_path = c_breakpoints_paths[i]; char *c_breakpoint_path = rb_string_value_cstr(&breakpoint_path); if (strcmp(c_tracepoint_path, c_breakpoint_path) == 0) { return 1; } } return 0; }
/** * Display the content of a hash table * @param name Hash table name to display */ int revm_table_display_content(char *name) { hash_t *h; char **keys; int keynbr; int index; char logbuf[BUFSIZ]; PROFILER_IN(__FILE__, __FUNCTION__, __LINE__); h = hash_get(hash_hash, name); if (!h) PROFILER_ERR(__FILE__, __FUNCTION__, __LINE__, "Invalid requested hash parameter", -1); /* Empty hash */ keys = hash_get_keys(h, &keynbr); if (!keynbr) { revm_output(" [*] Hash table is empty \n\n"); PROFILER_ROUT(__FILE__, __FUNCTION__, __LINE__, 0); } /* Display pointers */ for (index = 0; index < keynbr; index++) revm_table_display_element(h, keys[index], 0); snprintf(logbuf, sizeof(logbuf), "\n [*] Displayed %u entries of table %s \n\n", keynbr, name); revm_output(logbuf); hash_free_keys(keys); PROFILER_ROUT(__FILE__, __FUNCTION__, __LINE__, 0); }
/** * Print types matching a regular expression * * @param regex * @return */ int revm_type_print_regex(char *regex) { regex_t rx; int keynbr; char **keys; int index; int match; char buf[50]; PROFILER_IN(__FILE__, __FUNCTION__, __LINE__); if (regcomp(&rx, regex, REG_EXTENDED) < 0) PROFILER_ERR(__FILE__, __FUNCTION__, __LINE__, "Failed to compute regex", -1); keys = hash_get_keys(&types_hash, &keynbr); for (match = index = 0; index < keynbr; index++) { if (!regexec(&rx, keys[index], 0, 0, 0)) { revm_type_print(keys[index], 0); match++; } } snprintf(buf, sizeof(buf), " [*] Matched %u types \n\n", match); revm_output(buf); PROFILER_ROUT(__FILE__, __FUNCTION__, __LINE__, 0); }
/** * Get the parent object of a breakpoint. * Thats needed for the mprotect stuff inside the breakpoint handler * @param addr * @return */ elfshobj_t *e2dbg_get_parent_object(eresi_Addr addr) { elfsh_Phdr *cur; elfshobj_t *curfile; elfshsect_t *cursect; char **keys; int index; int keynbr; PROFILER_IN(__FILE__, __FUNCTION__, __LINE__); keys = hash_get_keys(&world.curjob->loaded, &keynbr); for (index = 0; index < keynbr; index++) { curfile = hash_get(&world.curjob->loaded, keys[index]); cursect = elfsh_get_parent_section(curfile, addr, NULL); if (cursect) { cur = elfsh_get_parent_segment(curfile, cursect); if (cur) PROFILER_ROUT(__FILE__, __FUNCTION__, __LINE__, curfile); } } /* Parent object not found */ PROFILER_ERR(__FILE__, __FUNCTION__, __LINE__, "Unable to get parent object addr", (NULL)); }
char *edfmt_srcline_get(char *buf, eresi_Addr addr) { hash_t *htable; int keynbr; char **keys; int index; edfmtfunc_t *lfunc; PROFILER_IN(__FILE__, __FUNCTION__, __LINE__); if (!uniinfo) PROFILER_ERR(__FILE__, __FUNCTION__, __LINE__, "Global pointer uninitialise", NULL); /* Setup hash table depending of current scope */ htable = API_GET_FROM_SCOPE(hfunc); keys = hash_get_keys(htable, &keynbr); buf = NULL; if (keys) { for (index = 0; index < keynbr; index++) { lfunc = (edfmtfunc_t *) hash_get(htable, keys[index]); if (lfunc->start <= addr && lfunc->end >= addr) { // FIXME: srcLine is an integer! need to open file and read line! //buf = lfunc->srcLine; buf = "READ FILE!"; PROFILER_ROUT(__FILE__, __FUNCTION__, __LINE__, buf); } } } PROFILER_ROUT(__FILE__, __FUNCTION__, __LINE__, buf); }
/** * Return an element of this hash * The choice is non-deterministic. * * @param hash * @return */ void* hash_get_one(hash_t *hash) { char **keys; int index; if (!hash || !hash_size(hash)) return (NULL); keys = hash_get_keys(hash, &index); return (hash_get(hash, keys[0])); }
/** * @brief Return the only element of this hash . * @param hash Hash table. * @return NULL on error. */ void *hash_get_single(hash_t *hash) { char **keys; int idx; if (!hash || hash_size(hash) != 1) return (NULL); keys = hash_get_keys(hash, &idx); return (hash_get(hash, keys[0])); }
/** * Display the content of all hash tables that match the regex * @param tableregx Regular expression matching table names * @param elemregx Regular expression matching element keys */ static int revm_table_display_regx2(char *tableregx, char *elemregx) { regex_t rx, ex; int keynbr, keynbr2; char **keys, **keys2; int index, index2; hash_t *cur; u_int match; char logbuf[BUFSIZ]; PROFILER_IN(__FILE__, __FUNCTION__, __LINE__); if (regcomp(&rx, tableregx, REG_EXTENDED) < 0) PROFILER_ERR(__FILE__, __FUNCTION__, __LINE__, "Failed to compute regex", -1); if (regcomp(&ex, elemregx, REG_EXTENDED) < 0) PROFILER_ERR(__FILE__, __FUNCTION__, __LINE__, "Failed to compute regex", -1); /* Look for matching tables */ keys = hash_get_keys(hash_hash, &keynbr); for (match = index = 0; index < keynbr; index++) if (!regexec(&rx, keys[index], 0, 0, 0)) { cur = hash_get(hash_hash, keys[index]); //fprintf(stderr, "MATCHED TABLE %s of %u elems \n", cur->name, cur->elmnbr); keys2 = hash_get_keys(cur, &keynbr2); for (index2 = 0; index2 < keynbr2; index2++) if (!regexec(&ex, keys2[index2], 0, 0, 0)) { match++; revm_table_display_element(cur, keys2[index2], 1); } } hash_free_keys(keys); hash_free_keys(keys2); snprintf(logbuf, sizeof(logbuf), "\n [*] Matched %u entries in all tables\n\n", match); revm_output(logbuf); PROFILER_ROUT(__FILE__, __FUNCTION__, __LINE__, 0); }
// Returns array of values int histo_get_features(int *numbers, int *freq, int plot) { //hash_display(); hash_get_keys(numbers, count); //hash_get_values(freq, count); int i; for(i=0; i<count; i++) { //printf("Number %d shown %d times\n",keys[i],hash_get(keys[i])); freq[i] = hash_get(numbers[i]); if (plot) printf("%d %d\n",numbers[i], freq[i]); } return 0; }
/** * match_breakpoints * Check the Tracer#breakpoints_cache for any matching breakpoints of given * file path and line number. * * Return a Ruby array of breakpoints found. Qtrue if no match found, but this * file contains at least one breakpoint. Qnil if event triggered in a file * that doesn't contain any breakpoints. */ static VALUE match_breakpoints(VALUE self, const char *c_trace_path, int c_trace_lineno) { int i, j; VALUE path_breakpoints_hash = rb_iv_get(self, "@breakpoints_cache"); VALUE breakpoints_paths = hash_get_keys(path_breakpoints_hash); VALUE *c_breakpoints_paths = RARRAY_PTR(breakpoints_paths); int breakpoints_paths_len = RARRAY_LEN(breakpoints_paths); VALUE path_match = Qnil; // Check the file paths of @breakpoints_cache for (i = 0; i < breakpoints_paths_len; i++) { VALUE breakpoint_path = c_breakpoints_paths[i]; char *c_breakpoint_path = rb_string_value_cstr(&breakpoint_path); // Found matching file path, keep going and check for the line numbers if (strcmp(c_trace_path, c_breakpoint_path) == 0) { VALUE line_breakpoint_hash = rb_hash_aref(path_breakpoints_hash, breakpoint_path); VALUE breakpoints_lines = hash_get_keys(line_breakpoint_hash); VALUE *c_breakpoints_lines = RARRAY_PTR(breakpoints_lines); int breakpoints_lines_len = RARRAY_LEN(breakpoints_lines); path_match = Qtrue; // Found matching breakpoints. Return the cached breakpoints array for (j = 0; j < breakpoints_lines_len; j++) { VALUE breakpoint_lineno = c_breakpoints_lines[j]; int c_breakpoint_lineno = NUM2INT(breakpoint_lineno); if (c_trace_lineno == c_breakpoint_lineno) { return rb_hash_aref(line_breakpoint_hash, breakpoint_lineno); } } } } return path_match; }
/** * Detail listing of a trace * @param table trace hash table * @param name trace name * @see traces_list */ int traces_list_detail(hash_t *table, char *name) { char **keys; u_int index; int keynbr; trace_t *entrie; char buf[BUFSIZ]; PROFILER_IN(__FILE__, __FUNCTION__, __LINE__); snprintf(buf, BUFSIZ - 1, " ~> %s: %s\n", aspectworld.colorfieldstr("Trace name"), aspectworld.colorstr(name)); aspectworld.profile(buf); keys = hash_get_keys(table, &keynbr); if (keys && keynbr > 0) { aspectworld.profile("\n"); for (index = 0; index < keynbr; index++) { entrie = (trace_t *) hash_get(table, keys[index]); if (entrie) { snprintf(buf, BUFSIZ - 1, " %s: %s %s: %s\n", aspectworld.colorfieldstr("Function name"), aspectworld.colorstr_fmt("%-15s", entrie->funcname), aspectworld.colorfieldstr("status"), aspectworld.colortypestr(entrie->enable ? "enabled" : "disabled")); aspectworld.profile(buf); aspectworld.endline(); } } aspectworld.profile("\n"); hash_free_keys(keys); } else { aspectworld.profile("\n [*] No function in this trace\n\n"); } PROFILER_ROUT(__FILE__, __FUNCTION__, __LINE__, 0); }
/* Intersect hash tables in the first one */ int hash_inter(hash_t *dst, hash_t *src) { char **keys; int keynbr; int idx; char *curkey; int ret; if (!src || !dst || src->elmnbr == 0 || dst->elmnbr == 0) return (0); keys = hash_get_keys(dst, &keynbr); for (ret = idx = 0; idx < keynbr; idx++) { curkey = keys[idx]; if (!hash_get(src, curkey)) ret += hash_del(dst, curkey); } return ret; }
/** * Destroy a hash table. * @param hash Pointer to the hash to destroy */ void hash_destroy(hash_t *hash) { char **keys; int idx; int keynbr; PROFILER_IN(__FILE__, __FUNCTION__, __LINE__); /* We should not destroy the elements as they might be in other hashes */ keys = hash_get_keys(hash, &keynbr); for (idx = 0; idx < keynbr; idx++) if (keys[idx]) XFREE(__FILE__, __FUNCTION__, __LINE__, keys[idx]); if (keys) hash_free_keys(keys); hash_del(hash_hash, hash->name); XFREE(__FILE__, __FUNCTION__, __LINE__, hash->ent); PROFILER_OUT(__FILE__, __FUNCTION__, __LINE__); }
/** * Display the content of a hash table */ static void revm_tables_display() { char **keys; int keynbr; int index; hash_t *cur; PROFILER_IN(__FILE__, __FUNCTION__, __LINE__); revm_output(" .:: Registered tables \n\n"); keys = hash_get_keys(hash_hash, &keynbr); for (index = 0; index < keynbr; index++) { cur = hash_get(hash_hash, keys[index]); revm_table_display(cur, keys[index]); } hash_free_keys(keys); revm_output("\n Type 'help tables' for more table details.\n\n"); PROFILER_OUT(__FILE__, __FUNCTION__, __LINE__); }
/** * Display the content of all hash tables that match the regex */ static int revm_list_display_regx(char *regx) { regex_t rx; int keynbr; char **keys; int index; int match; char buf[50]; char *lastmatch; list_t *cur; PROFILER_IN(__FILE__, __FUNCTION__, __LINE__); if (regcomp(&rx, regx, REG_EXTENDED) < 0) PROFILER_ERR(__FILE__, __FUNCTION__, __LINE__, "Failed to compute regex", -1); /* Look for matching tables */ keys = hash_get_keys(hash_lists, &keynbr); for (lastmatch = NULL, match = index = 0; index < keynbr; index++) { if (!regexec(&rx, keys[index], 0, 0, 0)) { cur = hash_get(hash_lists, keys[index]); revm_list_display(cur, keys[index]); match++; lastmatch = keys[index]; } } /* Print the content of the table if we had a unique match */ if (match == 1) revm_list_display_content(lastmatch); else { snprintf(buf, sizeof(buf), "\n [*] Matched %u list%c \n\n", match, (match > 1 ? 's' : ' ')); revm_output(buf); } hash_free_keys(keys); PROFILER_ROUT(__FILE__, __FUNCTION__, __LINE__, 0); }
/* Find breakpoint by ID */ elfshbp_t *e2dbg_breakpoint_from_id(uint32_t bpid) { elfshbp_t *cur; int index; char **keys; int keynbr; PROFILER_IN(__FILE__, __FUNCTION__, __LINE__); keys = hash_get_keys(&e2dbgworld.bp, &keynbr); for (index = 0; index < keynbr; index++) { cur = hash_get(&e2dbgworld.bp, keys[index]); if (cur->id == bpid) PROFILER_ROUT(__FILE__, __FUNCTION__, __LINE__, cur); } hash_free_keys(keys); PROFILER_ERR(__FILE__, __FUNCTION__, __LINE__, "Unable to find breakpoing by ID", NULL); }
/** * Switch to the next workspace * */ int revm_workspace_next() { u_int index, entrie; char **keys; int keynbr; revmjob_t *curjob; PROFILER_IN(__FILE__, __FUNCTION__, __LINE__); keys = hash_get_keys(&world.jobs, &keynbr); if (keynbr <= 1) PROFILER_ROUT(__FILE__, __FUNCTION__, __LINE__, 0); /* Search the current index */ for (index = 0; index < keynbr; index++) { curjob = hash_get(&world.jobs, keys[index]); if (revm_own_job(curjob) && curjob == world.curjob) { entrie = index; break; } } /* Search the next entrie */ for (entrie = (entrie+1) % keynbr; entrie < keynbr; entrie = (entrie+1) % keynbr) { curjob = hash_get(&world.jobs, keys[entrie]); if (revm_own_job(curjob)) { /* If we found the current job, we made a loop, so we break */ if (curjob == world.curjob) break; revm_switch_job(curjob); PROFILER_ROUT(__FILE__, __FUNCTION__, __LINE__, 1); } } PROFILER_ERR(__FILE__, __FUNCTION__, __LINE__, "Unable to find workspace to switch on", -1); }
/** * Change the status of a whole trace * @param table trace hash table * @param status new status (0 = disable, 1 = enable) * @see etrace_funcenableall * @see etrace_funcdisableall */ int etrace_funcsetstatus(hash_t *table, int status) { u_int index; int keynbr; char **keys; trace_t *ret_trace; PROFILER_IN(__FILE__, __FUNCTION__, __LINE__); if (!table) PROFILER_ERR(__FILE__, __FUNCTION__, __LINE__, "Invalid parameters", -1); etrace_init_trace(); keys = hash_get_keys(table, &keynbr); if (keys) { for (index = 0; index < keynbr; index++) { ret_trace = (trace_t *) hash_get(table, keys[index]); if (ret_trace) { if (ret_trace->enable != status) { if (ret_trace->enable == 1) trace_enabled_count--; else trace_enabled_count++; } ret_trace->enable = status; } } hash_free_keys(keys); } PROFILER_ROUT(__FILE__, __FUNCTION__, __LINE__, 0); }
/** * @brief Display str on all term * @ingroup io */ int revm_output_bcast(char *str) { int index; int ret = 0; revmjob_t *old; char **keys; int keynbr; PROFILER_IN(__FILE__, __FUNCTION__, __LINE__); /* Saving current output parameters */ old = world.curjob; /* Net's outputs */ if (world.state.revm_net) { keys = hash_get_keys(&world.jobs, &keynbr); for (index = 0; index < keynbr; index++) { old = hash_get(&world.jobs, keys[index]); if (!strcmp(keys[index], "local") || !strcmp(keys[index], "net_init") || !strncmp(keys[index], "DUMP", 4) || !old->ws.active); continue; world.curjob = old; ret |= revm_output(str); revm_flush(); } } /* stdout */ if (world.state.revm_mode != REVM_STATE_CMDLINE) { world.curjob = hash_get(&world.jobs, "local"); ret |= revm_output(str); } /* Restore */ world.curjob = old; PROFILER_ROUT(__FILE__, __FUNCTION__, __LINE__, (ret)); }
/** * Display the content of all hash tables that match the regex * @param tableregx Regular expression matching table names * @param elemregx Regular expression matching element keys */ static int revm_list_display_regx2(char *tableregx, char *elemregx) { regex_t rx, ex; int keynbr; char **keys; int index; list_t *cur; listent_t *curent; u_int match; char logbuf[BUFSIZ]; PROFILER_IN(__FILE__, __FUNCTION__, __LINE__); if (regcomp(&rx, tableregx, REG_EXTENDED) < 0) PROFILER_ERR(__FILE__, __FUNCTION__, __LINE__, "Failed to compute regex", -1); if (regcomp(&ex, elemregx, REG_EXTENDED) < 0) PROFILER_ERR(__FILE__, __FUNCTION__, __LINE__, "Failed to compute regex", -1); /* Look for matching tables */ keys = hash_get_keys(hash_lists, &keynbr); for (match = index = 0; index < keynbr; index++) if (!regexec(&rx, keys[index], 0, 0, 0)) { cur = hash_get(hash_lists, keys[index]); for (curent = cur->head; curent; curent = curent->next) if (!regexec(&ex, curent->key, 0, 0, 0)) { match++; revm_list_display_element(cur, curent->key, 1); } } hash_free_keys(keys); snprintf(logbuf, sizeof(logbuf), "\n [*] Matched %u entries in all lists \n\n", match); revm_output(logbuf); PROFILER_ROUT(__FILE__, __FUNCTION__, __LINE__, 0); }
/** * Print available types * * @return */ int revm_type_prints() { int keynbr; char **keys; int index; int total; char buf[BUFSIZ]; PROFILER_IN(__FILE__, __FUNCTION__, __LINE__); if (!types_hash.elmnbr) { revm_output("\n .:: No registered types\n\n"); PROFILER_ROUT(__FILE__, __FUNCTION__, __LINE__, 0); } revm_output("\n .:: Available meta-language types \n\n"); keys = hash_get_keys(&types_hash, &keynbr); for (total = index = 0; index < keynbr; index++) total += revm_type_print(keys[index], 1); snprintf(buf, sizeof(buf), "\n .:: Found %u registered types\n\n", total); revm_output(buf); PROFILER_ROUT(__FILE__, __FUNCTION__, __LINE__, 0); }
int cmd_configure() { configitem_t *ci; int cnt; int idx; char **tab; PROFILER_IN(__FILE__, __FUNCTION__, __LINE__); /* We list the configuration */ if (world.curjob->curcmd->argc < 2) { tab = hash_get_keys(&aspectworld.config_hash, &cnt); printf(" [*] Configure parameters\n\n"); for (idx = 0; idx < cnt; idx++) { ci = hash_get(&aspectworld.config_hash, tab[idx]); if (ci->type == CONFIG_TYPE_INT) printf(" [+] (%2s) %-30s : %d\n", (ci->mode == CONFIG_MODE_RW) ? "RW" : "RO", ci->name, ci->val); if (ci->type == CONFIG_TYPE_STR) printf(" [+] (%2s) %-30s : %s\n", (ci->mode == CONFIG_MODE_RW) ? "RW" : "RO", ci->name, (char *) ci->data); } printf("\n"); } /* We change a configuration option */ else { ci = hash_get(&aspectworld.config_hash, world.curjob->curcmd->param[0]); if (ci != NULL) { if (ci->mode == CONFIG_MODE_RO) PROFILER_ERR(__FILE__, __FUNCTION__, __LINE__, "Cannot update a READONLY key", (-1)); if (ci->type == CONFIG_TYPE_INT) config_update_key(ci->name, (void *) atoi(world.curjob->curcmd->param[1])); if (ci->type == CONFIG_TYPE_STR) config_update_key(ci->name, (void *) world.curjob->curcmd->param[1]); printf(" [*] setting new configuration value for %s to %s\n\n", world.curjob->curcmd->param[0], world.curjob->curcmd->param[1]); } else PROFILER_ERR(__FILE__, __FUNCTION__, __LINE__, "Configuration key not found.", (-1)); } PROFILER_ROUT(__FILE__, __FUNCTION__, __LINE__, 0); }
/** * Resolve symbol in one file or all (mapped) if we are in e2dbg * Runtime compatible. * @param file * @param addr * @param roffset * @return */ char *revm_resolve(elfshobj_t *file, eresi_Addr addr, elfsh_SAddr *roffset) { int index; elfshobj_t *actual; char *name = NULL; char *dname = NULL; elfsh_SAddr offset = 0; elfsh_SAddr doffset = 0; char *bestname = NULL; elfsh_SAddr bestoffset; elfshobj_t *bestfile; char buf[BUFSIZ]; char *str; char **keys; int keynbr; PROFILER_IN(__FILE__, __FUNCTION__, __LINE__); if (!file) PROFILER_ERR(__FILE__, __FUNCTION__, __LINE__, "Invalid NULL argument", NULL); actual = file; name = elfsh_reverse_symbol(actual, addr, &offset); dname = elfsh_reverse_dynsymbol(actual, addr, &doffset); #if __DEBUG_RESOLVE__ printf("[elfsh:resolve] First found file : %s name = %s:%d / dname = %s:%d ("XFMT") \n", actual->name, name, offset, dname, doffset, addr); #endif if (!name || (dname && !strcmp(name, ELFSH_SECTION_NAME_PLT)) || (offset < 0) || (dname && doffset < offset && doffset >= 0)) { name = dname; offset = doffset; } else if (name && dname && doffset == offset) name = dname; bestname = name; bestoffset = offset; bestfile = actual; /* Find the best symbol by searching in all the objects of the process */ if (e2dbg_presence_get()) { keys = hash_get_keys(&world.curjob->loaded, &keynbr); for (index = 0; index < keynbr; index++) { actual = hash_get(&world.curjob->loaded, keys[index]); if (!actual->linkmap) continue; name = elfsh_reverse_symbol(actual, addr, &offset); dname = elfsh_reverse_dynsymbol(actual, addr, &doffset); if (!name || (offset < 0) || (dname && doffset < offset && doffset >= 0)) { name = dname; offset = doffset; } if (!bestname || (bestoffset < 0) || (name && (offset < bestoffset) && offset >= 0)) { bestname = name; bestoffset = offset; bestfile = actual; #if __DEBUG_RESOLVE__ fprintf(stderr, "[elfsh:resolve] Changed best : file %s name %s %d\n", actual->name, name, offset); #endif } } } #if __DEBUG_RESOLVE__ printf("[elfsh:resolve] BEST name %s %d\n", bestname, bestoffset); #endif if (roffset) *roffset = bestoffset; if (bestname == NULL) PROFILER_ERR(__FILE__, __FUNCTION__, __LINE__, "Unable to resolve best name", (NULL)); if (elfsh_is_runtime_mode()) { str = revm_basename(bestfile->name); snprintf(buf, BUFSIZ, "%s@%s", bestname, (str ? str : "CORRUPTED")); } else snprintf(buf, BUFSIZ, "%s", bestname); PROFILER_ROUT(__FILE__, __FUNCTION__, __LINE__, strdup(buf)); }
PROFILER_ROUT(__FILE__, __FUNCTION__, __LINE__, 0); } /* This function reflect a data structure */ int revm_type_reflect(hash_t *hash, char *typename) { char **keys; int index; int nbr; void *data; char logbuf[BUFSIZ]; revmexpr_t *result; PROFILER_IN(__FILE__, __FUNCTION__, __LINE__); keys = hash_get_keys(hash, &nbr); for (index = 0; index < nbr; index++) { data = hash_get(hash, keys[index]); snprintf(logbuf, sizeof(logbuf), "%c%s_%s", REVM_VAR_PREFIX, typename, keys[index]); result = revm_inform_type_addr(typename, logbuf, (eresi_Addr) data, NULL, 0, 1); if (!result) PROFILER_ERR(__FILE__, __FUNCTION__, __LINE__, "Failed to reflect data type", -1); #if __DEBUG_REFLECT__ fprintf(stderr, "SUCCESS TO REFLECT %s OBJECT OF KEY %s \n", typename, keys[index]); revm_expr_print(result, 0); #endif
/** * List all the loaded objects */ int cmd_dolist() { elfshobj_t *actual; int index; char *time; char *nl; char c; char c2; char logbuf[BUFSIZ]; char optbuf[BUFSIZ]; char **keys; int keynbr; PROFILER_IN(__FILE__, __FUNCTION__, __LINE__); index = 1; /* Private descriptors */ if (hash_size(&world.curjob->loaded)) { revm_output(" .::. Static Working files .::. \n"); keys = hash_get_keys(&world.curjob->loaded, &keynbr); for (index = 0; index < keynbr; index++) { actual = hash_get(&world.curjob->loaded, keys[index]); time = ctime(&actual->loadtime); nl = strchr(time, '\n'); if (nl) *nl = 0x00; c = (world.curjob->curfile == actual ? '*' : ' '); c2 = ((actual->linkmap || actual->rhdr.base) ? 'M' : ' '); if (elfsh_is_runtime_mode()) snprintf(optbuf, BUFSIZ, "(" XFMT ")", actual->rhdr.base); else snprintf(optbuf, BUFSIZ, "%s", ""); snprintf(logbuf, BUFSIZ - 1, " %s %c%c %s ID: %10u %s %-31s ", time, c, c2, optbuf, actual->id, elfsh_get_objtype(actual->hdr) == ET_REL ? "ET_REL " : elfsh_get_objtype(actual->hdr) == ET_DYN ? "ET_DYN " : elfsh_get_objtype(actual->hdr) == ET_EXEC ? "ET_EXEC" : elfsh_get_objtype(actual->hdr) == ET_CORE ? "ET_CORE" : "UNKNOWN", actual->name); revm_output(logbuf); revm_dolist_dep(actual); revm_output("\n"); /* printf("-> Hashes for object : PAR[%u] ROOT[%u] CHILD[%u] \n", hash_size(&actual->parent_hash), hash_size(&actual->root_hash), hash_size(&actual->child_hash)); */ } } /* Shared descriptors */ if (hash_size(&world.shared_hash)) { revm_output("\n .::. Shared Working files .::. \n"); keys = hash_get_keys(&world.shared_hash, &keynbr); for (index = 0; index < keynbr; index++) { actual = hash_get(&world.shared_hash, keys[index]); time = ctime(&actual->loadtime); nl = strchr(time, '\n'); if (nl) *nl = 0x00; c = (world.curjob->curfile == actual ? '*' : ' '); c2 = (actual->linkmap ? 'L' : ' '); if (elfsh_is_runtime_mode()) snprintf(optbuf, BUFSIZ, "(" XFMT ")", actual->rhdr.base); else snprintf(optbuf, BUFSIZ, "%s", ""); snprintf(logbuf, BUFSIZ - 1, " [%02u] %s %c%c %s ID: %02u %-31s \n", index + 1, time, c, c2, optbuf, actual->id, actual->name); revm_output(logbuf); } } if (!hash_size(&world.curjob->loaded) && !hash_size(&world.shared_hash)) revm_output(" [*] No loaded file\n"); revm_output("\n"); revm_modlist(); revm_output("\n"); PROFILER_ROUT(__FILE__, __FUNCTION__, __LINE__, 0); }
/** Main function. Parses options and calls helper function for any heavy lifting. */ int main (int argc, char *argv[]) { int input_type=FILEDATA; int output_type=MIMETYPE; const char *mimetype; char *output=0; int i; hash_table_t launch_hash; locale_init(); /* Parse options */ while( 1 ) { static struct option long_options[] = { { "input-file-data", no_argument, 0, 't' } , { "input-filename", no_argument, 0, 'f' } , { "input-mime", no_argument, 0, 'i' } , { "output-mime", no_argument, 0, 'm' } , { "output-description", no_argument, 0, 'd' } , { "output-action", no_argument, 0, 'a' } , { "help", no_argument, 0, 'h' } , { "version", no_argument, 0, 'v' } , { "launch", no_argument, 0, 'l' } , { 0, 0, 0, 0 } } ; int opt_index = 0; int opt = getopt_long( argc, argv, GETOPT_STRING, long_options, &opt_index ); if( opt == -1 ) break; switch( opt ) { case 0: break; case 't': input_type=FILEDATA; break; case 'f': input_type=FILENAME; break; case 'i': input_type=MIMETYPE; break; case 'm': output_type=MIMETYPE; break; case 'd': output_type=DESCRIPTION; break; case 'a': output_type=ACTION; break; case 'l': output_type=LAUNCH; break; case 'h': print_help( argv[0], 1 ); exit(0); case 'v': printf( _("%s, version %s\n"), MIMEDB, PACKAGE_VERSION ); exit( 0 ); case '?': return 1; } } if( ( output_type == LAUNCH )&&(input_type==MIMETYPE)) { fprintf( stderr, _("%s: Can not launch a mimetype\n"), MIMEDB ); print_help( argv[0], 2 ); exit(1); } if( output_type == LAUNCH ) hash_init( &launch_hash, &hash_str_func, &hash_str_cmp ); /* Loop over all non option arguments and do the specified lookup */ //fprintf( stderr, "Input %d, output %d\n", input_type, output_type ); for (i = optind; (i < argc)&&(!error); i++) { /* Convert from filename to mimetype, if needed */ if( input_type == FILENAME ) { mimetype = xdg_mime_get_mime_type_from_file_name(argv[i]); } else if( input_type == FILEDATA ) { mimetype = xdg_mime_get_mime_type_for_file(argv[i]); } else mimetype = xdg_mime_is_valid_mime_type(argv[i])?argv[i]:0; mimetype = xdg_mime_unalias_mime_type (mimetype); if( !mimetype ) { fprintf( stderr, _( "%s: Could not parse mimetype from argument '%s'\n"), MIMEDB, argv[i] ); error=1; return 1; } /* Convert from mimetype to whatever, if needed */ switch( output_type ) { case MIMETYPE: { output = (char *)mimetype; break; } case DESCRIPTION: { output = get_description( mimetype ); if( !output ) output = strdup( _("Unknown") ); break; } case ACTION: { output = get_action( mimetype ); break; } case LAUNCH: { /* There may be more files using the same launcher, we add them all up in little array_list_ts and launched them together after all the arguments have been parsed. */ array_list_t *l= (array_list_t *)hash_get( &launch_hash, mimetype ); output = 0; if( !l ) { l = my_malloc( sizeof( array_list_t ) ); if( l == 0 ) { break; } al_init( l ); hash_put( &launch_hash, mimetype, l ); } al_push( l, argv[i] ); } } /* Print the glorious result */ if( output ) { printf( "%s\n", output ); if( output != mimetype ) free( output ); } output = 0; } /* Perform the actual launching */ if( output_type == LAUNCH && !error ) { int i; array_list_t mimes; al_init( &mimes ); hash_get_keys( &launch_hash, &mimes ); for( i=0; i<al_get_count( &mimes ); i++ ) { char *mimetype = (char *)al_get( &mimes, i ); array_list_t *files = (array_list_t *)hash_get( &launch_hash, mimetype ); if( !files ) { fprintf( stderr, _( "%s: Unknown error\n"), MIMEDB ); error=1; break; } char *launcher = get_action( mimetype ); if( launcher ) { launch( launcher, files, 0 ); free( launcher ); } } hash_foreach( &launch_hash, &clear_entry ); hash_destroy( &launch_hash ); al_destroy( &mimes ); } if( launch_buff ) free( launch_buff ); if( start_re ) { regfree( start_re ); regfree( stop_re ); free( start_re ); free( stop_re ); } xdg_mime_shutdown(); return error; }
/** * Need doxygen comment */ int cmd_workspace() { revmjob_t *job; u_int idx; u_int index; char logbuf[BUFSIZ]; char *nl; char *time; elfshobj_t *obj; char **keys; int keynbr; char **loadedkeys; int loadedkeynbr; PROFILER_IN(__FILE__, __FUNCTION__, __LINE__); //printf("workspace argc %u \n", world.curjob->curcmd->argc); switch (world.curjob->curcmd->argc) { /* $ workspace */ case 0: revm_output(" .::. Workspaces .::. \n"); keys = hash_get_keys(&world.jobs, &keynbr); for (index = 0; index < keynbr; index++) { job = (revmjob_t *) hash_get(&world.jobs, keys[index]); if (revm_own_job(job)) { time = ctime(&job->ws.createtime); nl = strchr(time, '\n'); if (nl) *nl = 0x00; snprintf(logbuf, BUFSIZ - 1, " [%s] %s %c \n", keys[index], time, (job->ws.active ? '*' : ' ')); revm_output(logbuf); if (hash_size(&job->loaded)) { loadedkeys = hash_get_keys(&job->loaded, &loadedkeynbr); for (idx = 0; idx < loadedkeynbr; idx++) { obj = hash_get(&job->loaded, loadedkeys[idx]); snprintf(logbuf, BUFSIZ - 1, " \t %c %s \n", (job->curfile == obj ? '*' : ' '), obj->name); revm_output(logbuf); } } else { snprintf(logbuf, BUFSIZ - 1, " \t No files\n"); revm_output(logbuf); } } } revm_output("\n"); PROFILER_ROUT(__FILE__, __FUNCTION__, __LINE__, 0); /* $ workspace name */ case 1: PROFILER_ROUT(__FILE__, __FUNCTION__, __LINE__, revm_create_new_workspace(revm_get_cur_job_parameter(0))); /* Unknown command format */ default: PROFILER_ERR(__FILE__, __FUNCTION__, __LINE__, "Wrong arg number", -1); } }
/** * Enumerate each trace and detail every functions * @param name not use * @param optarg not use * @see traces_list_detail */ int traces_list(elfshobj_t *file, char *name, char **optarg) { char **keys = NULL; u_int index; int keynbr; hash_t *subtable; char funcreg[256]; size_t len; char buf[BUFSIZ]; PROFILER_IN(__FILE__, __FUNCTION__, __LINE__); aspectworld.profile(" .: Trace list :.\n"); if (traces_table.ent) keys = hash_get_keys(&traces_table, &keynbr); if (keys) { if (keynbr > 0) aspectworld.profile("\n"); for (index = 0; index < keynbr; index++) { subtable = hash_get(&traces_table, keys[index]); if (subtable) traces_list_detail(subtable, keys[index]); } hash_free_keys(keys); } else { aspectworld.profile("\n [*] No tracing table available\n\n"); } aspectworld.profile(" .: Exclude list :.\n"); /* Reset */ keynbr = 0; keys = NULL; /* Print exclude list */ if (exclude_table.ent) keys = hash_get_keys(&exclude_table, &keynbr); if (keys) { if (exclude_table.ent) aspectworld.profile("\n"); for (index = 0; index < keynbr; index++) { len = strlen(keys[index]); snprintf(funcreg, 255, "%s%s%s", keys[index][0] != '^' ? "^" : "", keys[index], keys[index][len-1] != '$' ? "$" : ""); snprintf(buf, BUFSIZ - 1, " %s %s %s %s %s\n", aspectworld.colornumber("[%02u]", index+1), aspectworld.colorfieldstr("name:"), aspectworld.colorstr_fmt("%-15s", keys[index]), aspectworld.colorfieldstr("regex:"), aspectworld.colorstr(funcreg)); aspectworld.profile(buf); aspectworld.endline(); } hash_free_keys(keys); } else { aspectworld.profile("\n [*] No exclude table available\n"); } aspectworld.profile("\n"); PROFILER_ROUT(__FILE__, __FUNCTION__, __LINE__, 0); }