Skip to content

Commit

Permalink
i#7113 instr decode cache: move module read into raw2trace_shared
Browse files Browse the repository at this point in the history
This is in preparation for further changes that include the module mapper logic in a new library (drmemtrace_instr_decode_cache) that provides an instr decode cache (instr_decode_cache_t). To avoid having to pull in the whole drmemtrace_raw2trace implementation into drmemtrace_instr_decode_cache, which will end up including it in the invariant checker and everything that depends on, we separate out the module mapper into raw2trace_shared which is intended for such cases.

Issue: #7113
  • Loading branch information
abhinav92003 committed Dec 11, 2024
1 parent 64ac31b commit e9177bc
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 32 deletions.
15 changes: 10 additions & 5 deletions clients/drcachesim/tools/opcode_mix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,18 @@ opcode_mix_t::initialize()
return "";
// Legacy trace support where binaries are needed.
// We do not support non-module code for such traces.
std::string error = directory_.initialize_module_file(module_file_path_);
if (!error.empty())
return "Failed to initialize directory: " + error;
file_t modfile;
char *modfile_bytes;
std::string error = read_module_file_bytes(module_file_path_, modfile, modfile_bytes);
if (!error.empty()) {
return "Failed to read module file: " + error;
}
module_mapper_ =
module_mapper_t::create(directory_.modfile_bytes_, nullptr, nullptr, nullptr,
nullptr, knob_verbose_, knob_alt_module_dir_);
module_mapper_t::create(modfile_bytes, nullptr, nullptr, nullptr, nullptr,
knob_verbose_, knob_alt_module_dir_);
module_mapper_->get_loaded_modules();
delete[] modfile_bytes;
dr_close_file(modfile);
error = module_mapper_->get_last_error();
if (!error.empty())
return "Failed to load binaries: " + error;
Expand Down
28 changes: 13 additions & 15 deletions clients/drcachesim/tools/view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,24 +124,22 @@ view_t::initialize_stream(memtrace_stream_t *serial_stream)
if (module_file_path_.empty()) {
has_modules_ = false;
} else {
std::string error = directory_.initialize_module_file(module_file_path_);
file_t modfile;
char *modfile_bytes;
std::string error =
read_module_file_bytes(module_file_path_, modfile, modfile_bytes);
if (error.empty()) {
module_mapper_ =
module_mapper_t::create(modfile_bytes, nullptr, nullptr, nullptr, nullptr,
knob_verbose_, knob_alt_module_dir_);
module_mapper_->get_loaded_modules();
delete[] modfile_bytes;
dr_close_file(modfile);
error = module_mapper_->get_last_error();
}
if (!error.empty())
has_modules_ = false;
}
if (!has_modules_) {
// Continue but omit disassembly to support cases where binaries are
// not available and OFFLINE_FILE_TYPE_ENCODINGS is not present.
return "";
}
// Legacy trace support where binaries are needed.
// We do not support non-module code for such traces.
module_mapper_ =
module_mapper_t::create(directory_.modfile_bytes_, nullptr, nullptr, nullptr,
nullptr, knob_verbose_, knob_alt_module_dir_);
module_mapper_->get_loaded_modules();
std::string error = module_mapper_->get_last_error();
if (!error.empty())
return "Failed to load binaries: " + error;
return "";
}

Expand Down
15 changes: 3 additions & 12 deletions clients/drcachesim/tracer/raw2trace_directory.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* **********************************************************
* Copyright (c) 2017-2023 Google, Inc. All rights reserved.
* Copyright (c) 2017-2024 Google, Inc. All rights reserved.
* **********************************************************/

/*
Expand Down Expand Up @@ -55,6 +55,7 @@
#include "directory_iterator.h"
#include "dr_api.h" // Must be after windows.h.
#include "raw2trace_directory.h" // Includes dr_api.h which must be after windows.h.
#include "raw2trace_shared.h"
#include "reader.h"
#include "raw2trace.h"
#include "record_file_reader.h"
Expand Down Expand Up @@ -435,17 +436,7 @@ raw2trace_directory_t::open_cpu_schedule_file()
std::string
raw2trace_directory_t::read_module_file(const std::string &modfilename)
{
modfile_ = dr_open_file(modfilename.c_str(), DR_FILE_READ);
if (modfile_ == INVALID_FILE)
return "Failed to open module file " + modfilename;
uint64 modfile_size;
if (!dr_file_size(modfile_, &modfile_size))
return "Failed to get module file size: " + modfilename;
size_t modfile_size_ = (size_t)modfile_size;
modfile_bytes_ = new char[modfile_size_];
if (dr_read_file(modfile_, modfile_bytes_, modfile_size_) < (ssize_t)modfile_size_)
return "Didn't read whole module file " + modfilename;
return "";
return read_module_file_bytes(modfilename, modfile_, modfile_bytes_);
}

bool
Expand Down
25 changes: 25 additions & 0 deletions clients/drcachesim/tracer/raw2trace_shared.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,5 +143,30 @@ drmemtrace_get_timestamp_from_offline_trace(const void *trace, size_t trace_size
return DRMEMTRACE_SUCCESS;
}

std::string
read_module_file_bytes(const std::string &modfilename, file_t &modfile,
char *&modfile_bytes)
{
modfile = dr_open_file(modfilename.c_str(), DR_FILE_READ);
if (modfile == INVALID_FILE)
return "Failed to open module file " + modfilename;
uint64 modfile_size;
if (!dr_file_size(modfile, &modfile_size)) {
dr_close_file(modfile);
modfile = INVALID_FILE;
return "Failed to get module file size: " + modfilename;
}
size_t modfile_size_ = (size_t)modfile_size;
modfile_bytes = new char[modfile_size_];
if (dr_read_file(modfile, modfile_bytes, modfile_size_) < (ssize_t)modfile_size_) {
dr_close_file(modfile);
delete[] modfile_bytes;
modfile = INVALID_FILE;
modfile_bytes = nullptr;
return "Didn't read whole module file " + modfilename;
}
return "";
}

} // namespace drmemtrace
} // namespace dynamorio
12 changes: 12 additions & 0 deletions clients/drcachesim/tracer/raw2trace_shared.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,18 @@ class memref_counter_t : public reader_t {
std::list<trace_entry_t> list_;
};

/**
* Reads the module file at the given \p modfilename path. Returns an
* empty string if successful, or the error string if not.
*
* If successful, the returned \p modfile must be closed using
* \p dr_close_file, and the returned \p modefilebytes must be freed
* using a delete[].
*/
std::string
read_module_file_bytes(const std::string &modfilename, file_t &modfile,
char *&modfile_bytes);

} // namespace drmemtrace
} // namespace dynamorio

Expand Down

0 comments on commit e9177bc

Please sign in to comment.