drcovlib_status_t drmodtrack_lookup(void *drcontext, app_pc pc, OUT uint *mod_index, OUT app_pc *mod_base) { per_thread_t *data = (per_thread_t *)drmgr_get_tls_field(drcontext, tls_idx); module_entry_t *entry; int i; /* We assume we never change an entry's data field, even on unload, * and thus it is ok to check its value without a lock. */ /* lookup thread module cache */ for (i = 0; i < NUM_THREAD_MODULE_CACHE; i++) { entry = data->cache[i]; if (pc_is_in_module(entry, pc)) { if (i > 0) { thread_module_cache_adjust(data->cache, entry, i, NUM_THREAD_MODULE_CACHE); } lookup_helper_set_fields(entry, mod_index, mod_base); return DRCOVLIB_SUCCESS; } } /* lookup global module cache */ /* we use a direct map cache, so it is ok to access it without lock */ for (i = 0; i < NUM_GLOBAL_MODULE_CACHE; i++) { entry = module_table.cache[i]; if (pc_is_in_module(entry, pc)) { lookup_helper_set_fields(entry, mod_index, mod_base); return DRCOVLIB_SUCCESS; } } /* lookup module table */ entry = NULL; drvector_lock(&module_table.vector); for (i = module_table.vector.entries - 1; i >= 0; i--) { entry = drvector_get_entry(&module_table.vector, i); ASSERT(entry != NULL, "fail to get module entry"); if (pc_is_in_module(entry, pc)) { global_module_cache_add(module_table.cache, entry); thread_module_cache_add(data->cache, NUM_THREAD_MODULE_CACHE, entry); break; } entry = NULL; } if (entry != NULL) lookup_helper_set_fields(entry, mod_index, mod_base); drvector_unlock(&module_table.vector); return entry == NULL ? DRCOVLIB_ERROR_NOT_FOUND : DRCOVLIB_SUCCESS; }
module_entry_t * module_table_lookup(module_entry_t **cache, int cache_size, module_table_t *table, app_pc pc) { module_entry_t *entry; int i; /* We assume we never change an entry's data field, even on unload, * and thus it is ok to check its value without a lock. */ /* lookup thread module cache */ if (cache != NULL) { for (i = 0; i < cache_size; i++) { entry = cache[i]; if (pc_is_in_module(entry, pc)) { if (i > 0) thread_module_cache_adjust(cache, entry, i, cache_size); return entry; } } } /* lookup global module cache */ /* we use a direct map cache, so it is ok to access it without lock */ for (i = 0; i < NUM_GLOBAL_MODULE_CACHE; i++) { entry = table->cache[i]; if (pc_is_in_module(entry, pc)) return entry; } /* lookup module table */ entry = NULL; drvector_lock(&table->vector); for (i = table->vector.entries - 1; i >= 0; i--) { entry = drvector_get_entry(&table->vector, i); ASSERT(entry != NULL, "fail to get module entry"); if (pc_is_in_module(entry, pc)) { global_module_cache_add(table->cache, entry); if (cache != NULL) thread_module_cache_add(cache, cache_size, entry); break; } entry = NULL; } drvector_unlock(&table->vector); return entry; }
static inline void thread_module_cache_add(module_entry_t **cache, uint cache_size, module_entry_t *entry) { thread_module_cache_adjust(cache, entry, cache_size - 1, cache_size); }