static char * read_module_list(char *buf, void ***tables, uint *num_mods) { char path[MAXIMUM_PATH]; uint i; uint version; PRINT(3, "Reading module table...\n"); /* versione number */ PRINT(4, "Reading version number"); if (dr_sscanf(buf, "DRCOV VERSION: %u\n", &version) != 1 && version != DRCOV_VERSION) { WARN(2, "Failed to read version number"); return NULL; } buf = move_to_next_line(buf); /* module table header */ PRINT(4, "Reading Module Table Header\n"); if (dr_sscanf(buf, "Module Table: %d\n", num_mods) != 1) { WARN(2, "Failed to read module table"); return NULL; } buf = move_to_next_line(buf); /* module lists */ PRINT(4, "Reading Module Lists\n"); *tables = calloc(*num_mods, sizeof(*tables)); for (i = 0; i < *num_mods; i++) { uint mod_id; uint64 mod_size; void *bb_table; /* assuming the string is something like: "0, 2207744, /bin/ls" */ /* XXX: i#1143: we do not use dr_sscanf since it does not support %[] */ if (sscanf(buf, " %u, %"INT64_FORMAT"u, %[^\n\r]", &mod_id, &mod_size, path) != 3) ASSERT(false, "Failed to read module table"); buf = move_to_next_line(buf); PRINT(5, "Module: %u, "PFX", %s\n", mod_id, (ptr_uint_t)mod_size, path); bb_table = hashtable_lookup(&module_htable, path); if (bb_table == NULL) { if (mod_size >= UINT_MAX) ASSERT(false, "module size is too large"); if (strstr(path, "<unknown>") != NULL || (mod_filter != NULL && strstr(path, mod_filter) == NULL)) bb_table = BB_TABLE_IGNORE; else bb_table = bb_table_create((uint)mod_size); PRINT(4, "Create bb table "PFX" for module %s\n", (ptr_uint_t)bb_table, path); num_module_htable_entries++; if (!hashtable_add(&module_htable, path, bb_table)) ASSERT(false, "Failed to add new module"); } (*tables)[i] = bb_table; } return buf; }
static void options_init(client_id_t id) { const char *opstr = dr_get_options(id); const char *s; char token[OPTION_MAX_LENGTH]; /* default values */ dr_snprintf(options.logdir, BUFFER_SIZE_ELEMENTS(options.logdir), "."); for (s = dr_get_token(opstr, token, BUFFER_SIZE_ELEMENTS(token)); s != NULL; s = dr_get_token(s, token, BUFFER_SIZE_ELEMENTS(token))) { if (strcmp(token, "-logdir") == 0) { s = dr_get_token(s, options.logdir, BUFFER_SIZE_ELEMENTS(options.logdir)); USAGE_CHECK(s != NULL, "missing logdir path"); } else if (strcmp(token, "-verbose") == 0) { s = dr_get_token(s, token, BUFFER_SIZE_ELEMENTS(token)); USAGE_CHECK(s != NULL, "missing -verbose number"); if (s != NULL) { int res = dr_sscanf(token, "%u", &verbose); USAGE_CHECK(res == 1, "invalid -verbose number"); } } else if (strcmp(token, "-symcache_path") == 0) { s = dr_get_token(s, options.sympath, BUFFER_SIZE_ELEMENTS(options.sympath)); USAGE_CHECK(s != NULL, "missing symcache dir path"); ALERT(2, "<drstrace symbol source is %s>\n", options.sympath); } else { ALERT(0, "UNRECOGNIZED OPTION: \"%s\"\n", token); USAGE_CHECK(false, "invalid option"); } } }
static bool parse_commandline_args(const char * args) { client_arg = (client_arg_t *)dr_global_alloc(sizeof(client_arg_t)); if (dr_sscanf(args, "%s", &client_arg->filter_filename) != 1){ return false; } return true; }
static bool parse_commandline_args (const char * args) { client_arg = (client_arg_t *)dr_global_alloc(sizeof(client_arg_t)); if(dr_sscanf(args,"%s %d %s %s",&client_arg->filter_filename, &client_arg->filter_mode, &client_arg->output_folder, &client_arg->extra_info)!=4){ return false; } return true; }
/* Sets modcache->has_debug_info. * No lock is needed as we assume the caller hasn't exposed modcache outside this * thread yet. */ static bool symcache_read_symfile(const module_data_t *mod, const char *modname, mod_cache_t *modcache) { hashtable_t *symtable = &modcache->table; bool res = false; const char *line, *next_line; char symbol[MAX_SYMLEN]; size_t offs; uint64 map_size; size_t actual_size; bool ok; void *map = NULL; char symfile[MAXIMUM_PATH]; file_t f; symcache_get_filename(modname, symfile, BUFFER_SIZE_ELEMENTS(symfile)); f = dr_open_file(symfile, DR_FILE_READ); if (f == INVALID_FILE) goto symcache_read_symfile_done; LOG(2, "processing symbol cache file for %s\n", modname); /* we avoid having to do our own buffering by just mapping the whole file */ ok = dr_file_size(f, &map_size); if (ok) { actual_size = (size_t) map_size; ASSERT(actual_size == map_size, "file size too large"); map = dr_map_file(f, &actual_size, 0, NULL, DR_MEMPROT_READ, 0); } if (!ok || map == NULL || actual_size < map_size) { NOTIFY_ERROR("Error mapping symcache file for %s"NL, modname); goto symcache_read_symfile_done; } if (strncmp((char *)map, SYMCACHE_FILE_HEADER, strlen(SYMCACHE_FILE_HEADER)) != 0) { WARN("WARNING: symbol cache file is corrupted\n"); goto symcache_read_symfile_done; } /* i#1057: We use dr_sscanf() because sscanf() from ntdll will call strlen() * and read off the end of the mapped file if it doesn't hit a null. */ if (dr_sscanf((char *)map + strlen(SYMCACHE_FILE_HEADER) + 1, "%d", (uint *)&offs) != 1 || /* neither forward nor backward compatible */ offs != SYMCACHE_VERSION) { WARN("WARNING: symbol cache file has wrong version\n"); goto symcache_read_symfile_done; } line = strchr((char *) map, '\n'); if (line != NULL) line++; if (line != NULL) { /* Module consistency checks */ uint cache_file_size; uint64 module_file_size; uint timestamp; #ifdef WINDOWS version_number_t file_version; version_number_t product_version; uint checksum; size_t module_internal_size; if (dr_sscanf(line, "%u,"UINT64_FORMAT_STRING","UINT64_FORMAT_STRING"," UINT64_FORMAT_STRING",%u,%u,%lu", &cache_file_size, &module_file_size, &file_version.version, &product_version.version, &checksum, ×tamp, &module_internal_size) != 7) { WARN("WARNING: %s symbol cache file has bad consistency header\n", modname); goto symcache_read_symfile_done; } if (module_file_size != modcache->module_file_size || file_version.version != modcache->file_version.version || product_version.version != modcache->product_version.version || checksum != modcache->checksum || timestamp != modcache->timestamp || module_internal_size != modcache->module_internal_size) { LOG(1, "module version mismatch: %s symbol cache file is stale\n", modname); LOG(2, "\t"UINT64_FORMAT_STRING" vs "UINT64_FORMAT_STRING", " UINT64_FORMAT_STRING" vs "UINT64_FORMAT_STRING", " UINT64_FORMAT_STRING" vs "UINT64_FORMAT_STRING", " "%u vs %u, %u vs %u, %lu vs %lu\n", module_file_size, modcache->module_file_size, file_version.version, modcache->file_version.version, product_version.version, modcache->product_version.version, checksum, modcache->checksum, timestamp, modcache->timestamp, module_internal_size, modcache->module_internal_size); goto symcache_read_symfile_done; } #elif defined(LINUX) if (dr_sscanf(line, "%u,"UINT64_FORMAT_STRING",%u", &cache_file_size, &module_file_size, ×tamp) != 3) { WARN("WARNING: %s symbol cache file has bad consistency header\n", modname); goto symcache_read_symfile_done; } if (module_file_size != modcache->module_file_size || timestamp != modcache->timestamp) { LOG(1, "module version mismatch: %s symbol cache file is stale\n", modname); goto symcache_read_symfile_done; } #elif defined(MACOS) uint current_version; uint compatibility_version; byte uuid[16]; /* XXX: if dr_sscanf supported %n maybe we could split these into * separate scans on the same string and share code w/ Linux. */ if (dr_sscanf(line, "%u,"UINT64_FORMAT_STRING",%u,%u,%u,%x,%x,%x,%x", &cache_file_size, &module_file_size, ×tamp, ¤t_version, &compatibility_version, (uint*)(&uuid[0]), (uint*)(&uuid[4]), (uint*)(&uuid[8]), (uint*)(&uuid[12])) != 9) { WARN("WARNING: %s symbol cache file has bad consistency header B\n", modname); goto symcache_read_symfile_done; } if (current_version != modcache->current_version || compatibility_version != modcache->compatibility_version || memcmp(uuid, modcache->uuid, sizeof(uuid)) != 0) { LOG(1, "module version mismatch: %s symbol cache file is stale\n", modname); goto symcache_read_symfile_done; } #endif /* We could go further w/ CRC or even MD5 but not worth it for dev tool */ if (cache_file_size != (uint)map_size) { WARN("WARNING: %s symbol cache file is corrupted: map=%d vs file=%d\n", modname, (uint)map_size, cache_file_size); goto symcache_read_symfile_done; } } line = strchr(line, '\n'); if (line != NULL) line++; if (line != NULL) { uint has_debug_info; if (dr_sscanf(line, "%u", &has_debug_info) != 1) { WARN("WARNING: %s symbol cache file has bad consistency header\n", modname); goto symcache_read_symfile_done; } if (has_debug_info) { /* We assume that the current availability of debug info doesn't matter */ modcache->has_debug_info = true; } else { /* We delay the costly check for symbols until we've read the symcache * b/c if its entry indicates symbols we don't need to look */ if (module_has_symbols(mod)) { LOG(1, "module now has debug info: %s symbol cache is stale\n", modname); goto symcache_read_symfile_done; } } } line = strchr(line, '\n'); if (line != NULL) line++; symbol[0] = '\0'; for (; line != NULL && line < ((char *)map) + map_size; line = next_line) { const char *comma = strchr(line, ','); const char *newline = strchr(line, '\n'); size_t symlen = (comma != NULL ? comma - line : 0); if (newline == NULL) { next_line = ((char *)map) + map_size + 1; /* handle EOF w/o trailing \n */ } else { next_line = newline + 1; } if (symlen > 0 && symlen < MAX_SYMLEN) { strncpy(symbol, line, symlen); symbol[symlen] = '\0'; } if (comma != NULL && symlen < MAX_SYMLEN && symbol[0] != '\0' && dr_sscanf(comma, ",0x%x", (uint *)&offs) == 1) { #ifdef WINDOWS /* Guard against corrupted files that cause DrMem to crash (i#1465) */ if (offs >= modcache->module_internal_size) { /* This one we want to know about */ NOTIFY("SYMCACHE ERROR: %s file has too-large entry "PIFX" for %s"NL, modname, offs, symbol); goto symcache_read_symfile_done; } #endif symcache_symbol_add(modname, symtable, symbol, offs); } else { WARN("WARNING: malformed symbol cache line \"%.*s\"\n", next_line - line - 1, line); /* We abort in case there were two dueling writes to the file * and it somehow got past the self-consistency check, * putting a header in the middle of the file, and we can't * trust subsequent lines since they may belong to a different * version of the module */ break; /* res should still be true */ } } res = true; symcache_read_symfile_done: if (map != NULL) dr_unmap_file(map, actual_size); if (f != INVALID_FILE) dr_close_file(f); if (!res) modcache->has_debug_info = module_has_symbols(mod); return res; }
/* file I/O */ void md_read_from_file (module_t * head, file_t file, bool extra_info){ uint64 map_size; size_t actual_size; bool ok; void * map = NULL; char * line; /*loop variables*/ int i; int j; /* linked list structure specific variables */ int no_modules; int no_instructions; unsigned int addr; char module_name[MAX_STRING_LENGTH]; /* for filling up the linked list data structure */ module_t * elem; ok = dr_file_size(file,&map_size); if(ok){ actual_size = (size_t)map_size; DR_ASSERT(actual_size == map_size); map = dr_map_file(file, &actual_size, 0, NULL, DR_MEMPROT_READ, 0); } dr_sscanf((char *)map,"%d\n",&no_modules); //dr_printf("%d\n",no_modules); //debug line = (char *)map; for(i=0;i<no_modules;i++){ line = strchr(line,'\n'); line++; //start of the next line //dr_sscanf(line,"%[^\t\n]\n",module_name); dr_get_token(line,module_name,MAX_STRING_LENGTH); //getFinalName(module_name); //dr_printf("%s\n",module_name); //debug line = strchr(line,'\n'); line++; //start of the next line dr_sscanf(line,"%d\n",&no_instructions); //dr_printf("%d\n",no_instructions); //debug //create a new element elem = new_elem(module_name,no_instructions+2); head->next = elem; head = elem; for(j=0;j<no_instructions;j++){ line = strchr(line,'\n'); line++; //start of the next line dr_sscanf(line,"%u\n",&addr); //dr_printf(line,"%x\n",addr); //debug add_bb_to_list(head->bbs,addr, extra_info, head->size_bbs); } } }
drcovlib_status_t drmodtrack_offline_read(file_t file, const char **map, OUT void **handle, OUT uint *num_mods) { module_read_info_t *info = NULL; uint i; uint64 file_size; size_t map_size = 0; const char *buf, *map_start; uint version; if (handle == NULL || num_mods == NULL) return DRCOVLIB_ERROR_INVALID_PARAMETER; if (file == INVALID_FILE) { if (map == NULL) return DRCOVLIB_ERROR_INVALID_PARAMETER; map_start = *map; } else { if (!dr_file_size(file, &file_size)) return DRCOVLIB_ERROR_INVALID_PARAMETER; map_size = (size_t)file_size; map_start = (char *) dr_map_file(file, &map_size, 0, NULL, DR_MEMPROT_READ, 0); if (map_start == NULL || map_size < file_size) return DRCOVLIB_ERROR_INVALID_PARAMETER; /* assume bad perms or sthg */ } if (map_start == NULL) return DRCOVLIB_ERROR_INVALID_PARAMETER; buf = map_start; /* Module table header, handling the pre-versioning legacy format. */ if (dr_sscanf(buf, "Module Table: %u\n", num_mods) == 1) version = 1; else if (dr_sscanf(buf, "Module Table: version %u, count %u\n", &version, num_mods) != 2 || version != MODULE_FILE_VERSION) goto read_error; buf = move_to_next_line(buf); if (version > 1) { // Skip header line buf = move_to_next_line(buf); } info = (module_read_info_t *)dr_global_alloc(sizeof(*info)); if (file != INVALID_FILE) { info->map = map_start; info->map_size = map_size; } else info->map = NULL; info->num_mods = *num_mods; info->mod = (module_read_entry_t *)dr_global_alloc(*num_mods * sizeof(*info->mod)); /* module lists */ for (i = 0; i < *num_mods; i++) { uint mod_id; if (version == 1) { if (dr_sscanf(buf, " %u, %" INT64_FORMAT"u, %[^\n\r]", &mod_id, &info->mod[i].size, info->mod[i].path) != 3 || mod_id != i) goto read_error; } else { app_pc end, entry; #ifdef WINDOWS uint checksum, timestamp; if (dr_sscanf(buf, " %u, "PIFX", "PIFX", "PIFX", 0x%x, 0x%x, %[^\n\r]", &mod_id, &info->mod[i].base, &end, &entry, &checksum, ×tamp, info->mod[i].path) != 7 || mod_id != i) goto read_error; #else if (dr_sscanf(buf, " %u, "PIFX", "PIFX", "PIFX", %[^\n\r]", &mod_id, &info->mod[i].base, &end, &entry, info->mod[i].path) != 5 || mod_id != i) goto read_error; #endif info->mod[i].size = end - info->mod[i].base; } buf = move_to_next_line(buf); } if (file == INVALID_FILE) *map = buf; *handle = (void *)info; return DRCOVLIB_SUCCESS; read_error: if (info != NULL) { dr_global_free(info->mod, *num_mods * sizeof(*info->mod)); dr_global_free(info, sizeof(*info)); } if (file != INVALID_FILE) dr_unmap_file((char *)map_start, map_size); return DRCOVLIB_ERROR; }