Skip to content

Commit

Permalink
i#6495 syscall templates: Inject kernel traces in raw2trace (#6496)
Browse files Browse the repository at this point in the history
Adds support to raw2trace to read system call kernel trace templates
from the given file and inject them at the corresponding system call
number marker during raw2trace conversion of user-space traces.

Adds a new offline file type
OFFLINE_FILE_TYPE_KERNEL_SYSCALL_TRACE_TEMPLATES used by such system
call trace template files.

Adds the burst_syscall_inject.cpp test that writes a sample system call
template file and verifies that it can be processed by the analyzer
framework; further, it collects a user-space trace for an app function
that makes some system calls, then performs raw2trace conversion with
system call template injection using the above-mentioned file. It then
verifies that the trace templates are injected properly in the resulting
trace. The test also runs basic_counts on the resulting trace to ensure
it can be processed by the analyzer framework.

Extracts raw2trace logic to write the essential header entries (that is,
the entries that are expected by the analyzer framework in the trace)
into a separate public API that can be used by other libraries (such as
the new burst_syscall_inject test or any other library writing the
syscall trace template file) to write valid header entries.

The sample system call template file written by burst_syscall_inject.cpp
also serves as documentation for the expected format of
OFFLINE_FILE_TYPE_KERNEL_SYSCALL_TRACE_TEMPLATES files.

More aspects of system call kernel trace injection is left to future
work: supporting multiple trace templates per system call (e.g.
differentiated by system call arguments or return value), or other
adjustment to the template at injection time.

Issue: #6495
  • Loading branch information
abhinav92003 authored Dec 9, 2023
1 parent 8eba754 commit 43cbfc3
Show file tree
Hide file tree
Showing 17 changed files with 818 additions and 33 deletions.
4 changes: 4 additions & 0 deletions api/docs/release.dox
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ changes:
refers to timestamps and direct switches, which is what most users should want.

Further non-compatibility-affecting changes include:
- Added raw2trace support to inject system call kernel trace templates collected from
elsewhere (e.g., QEMU, Gem5) into the user-space drmemtrace traces at the
corresponding system call number marker. This is done by specifying the path to the
template file via the new -syscall_template_file option.
- Added a new scheme for the modoffs field in the PC trace entry which allows L0
filtering of non-module code; see
#dynamorio::drmemtrace::ENCODING_FILE_TYPE_SEPARATE_NON_MOD_INSTRS. Also added
Expand Down
11 changes: 11 additions & 0 deletions clients/drcachesim/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ set(raw2trace_srcs
tracer/instru_offline.cpp
reader/reader.cpp
common/trace_entry.cpp
reader/record_file_reader.cpp
${zlib_reader}
)
if (libsnappy)
set(raw2trace_srcs ${raw2trace_srcs}
Expand Down Expand Up @@ -1022,6 +1024,15 @@ if (BUILD_TESTS)
target_link_libraries(tool.drcacheoff.burst_maps test_helpers)
add_win32_flags(tool.drcacheoff.burst_maps)
endif ()
if (LINUX)
add_executable(tool.drcacheoff.burst_syscall_inject tests/burst_syscall_inject.cpp)
configure_DynamoRIO_static(tool.drcacheoff.burst_syscall_inject)
use_DynamoRIO_static_client(tool.drcacheoff.burst_syscall_inject drmemtrace_static)
target_link_libraries(tool.drcacheoff.burst_syscall_inject drmemtrace_raw2trace
drmemtrace_analyzer test_helpers drmemtrace_basic_counts)
add_win32_flags(tool.drcacheoff.burst_syscall_inject)
use_DynamoRIO_drmemtrace_tracer(tool.drcacheoff.burst_syscall_inject)
endif ()

if (UNIX)
if (X86 AND NOT APPLE) # This test is x86-specific.
Expand Down
6 changes: 4 additions & 2 deletions clients/drcachesim/analyzer_multi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ analyzer_multi_t::analyzer_multi_t()
if (needs_processing) {
raw2trace_directory_t dir(op_verbose.get_value());
std::string dir_err =
dir.initialize(op_indir.get_value(), "", op_trace_compress.get_value());
dir.initialize(op_indir.get_value(), "", op_trace_compress.get_value(),
op_syscall_template_file.get_value());
if (!dir_err.empty()) {
success_ = false;
error_string_ = "Directory setup failed: " + dir_err;
Expand All @@ -171,7 +172,8 @@ analyzer_multi_t::analyzer_multi_t()
dir.encoding_file_, dir.serial_schedule_file_, dir.cpu_schedule_file_,
nullptr, op_verbose.get_value(), op_jobs.get_value(),
op_alt_module_dir.get_value(), op_chunk_instr_count.get_value(),
dir.in_kfiles_map_, dir.kcoredir_, dir.kallsymsdir_);
dir.in_kfiles_map_, dir.kcoredir_, dir.kallsymsdir_,
std::move(dir.syscall_template_file_reader_));
std::string error = raw2trace.do_conversion();
if (!error.empty()) {
success_ = false;
Expand Down
7 changes: 7 additions & 0 deletions clients/drcachesim/common/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -879,5 +879,12 @@ droption_t<uint64_t>
"A letter is printed every N instrs",
"A letter is printed every N instrs or N waits");

droption_t<std::string> op_syscall_template_file(
DROPTION_SCOPE_FRONTEND, "syscall_template_file", "",
"Path to the file that contains system call trace templates.",
"Path to the file that contains system call trace templates. "
"If set, system call traces will be injected from the file "
"into the resulting trace.");

} // namespace drmemtrace
} // namespace dynamorio
1 change: 1 addition & 0 deletions clients/drcachesim/common/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ extern dynamorio::droption::droption_t<std::string> op_replay_file;
extern dynamorio::droption::droption_t<std::string> op_cpu_schedule_file;
#endif
extern dynamorio::droption::droption_t<uint64_t> op_schedule_stats_print_every;
extern dynamorio::droption::droption_t<std::string> op_syscall_template_file;

} // namespace drmemtrace
} // namespace dynamorio
Expand Down
21 changes: 20 additions & 1 deletion clients/drcachesim/common/trace_entry.h
Original file line number Diff line number Diff line change
Expand Up @@ -895,7 +895,10 @@ typedef enum {
OFFLINE_FILE_TYPE_BLOCKING_SYSCALLS = 0x800,
/**
* Kernel traces of syscalls are included.
* The included kernel traces are in the Intel® Processor Trace format.
* The included kernel traces are provided either by the -syscall_template_file to
* raw2trace (see #OFFLINE_FILE_TYPE_KERNEL_SYSCALL_TRACE_TEMPLATES), or on x86 using
* the -enable_kernel_tracing option that uses Intel® Processor Trace to collect a
* trace for system call execution.
*/
OFFLINE_FILE_TYPE_KERNEL_SYSCALLS = 0x1000,
/**
Expand All @@ -906,6 +909,22 @@ typedef enum {
* The initial part can be used by a simulator for warmup.
*/
OFFLINE_FILE_TYPE_BIMODAL_FILTERED_WARMUP = 0x2000,
/**
* Indicates an offline trace that contains trace templates for some system calls.
* The individual traces are separated by a #TRACE_MARKER_TYPE_SYSCALL marker which
* also specifies what system call the following trace belongs to. This file can be
* used with -syscall_template_file to raw2trace to create a
* #OFFLINE_FILE_TYPE_KERNEL_SYSCALLS trace. See the sample file written by the
* burst_syscall_inject.cpp test for more details on the expected format for the
* system call template file.
* TODO i#6495: Add support for reading a zipfile where each trace template is in
* a separate component. This will make it easier to manually append, update, or
* inspect the individual templates, and also allow streaming the component with the
* required template when needed instead of reading the complete file into memory
* ahead of time. Note that we may drop support for non-zipfile template files in
* the future.
*/
OFFLINE_FILE_TYPE_KERNEL_SYSCALL_TRACE_TEMPLATES = 0x4000,
} offline_file_type_t;

static inline const char *
Expand Down
5 changes: 3 additions & 2 deletions clients/drcachesim/reader/reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -261,12 +261,14 @@ class reader_t : public std::iterator<std::input_iterator_tag, memref_t>,
std::queue<trace_entry_t> queue_;
trace_entry_t entry_copy_; // For use in returning a queue entry.

private:
struct encoding_info_t {
size_t size = 0;
unsigned char bits[MAX_ENCODING_LENGTH];
};

std::unordered_map<addr_t, encoding_info_t> encodings_;

private:
memref_t cur_ref_;
memref_tid_t cur_tid_ = 0;
memref_pid_t cur_pid_ = 0;
Expand All @@ -277,7 +279,6 @@ class reader_t : public std::iterator<std::input_iterator_tag, memref_t>,
std::unordered_map<memref_tid_t, memref_pid_t> tid2pid_;
bool expect_no_encodings_ = true;
encoding_info_t last_encoding_;
std::unordered_map<addr_t, encoding_info_t> encodings_;
addr_t last_branch_target_ = 0;
};

Expand Down
6 changes: 5 additions & 1 deletion clients/drcachesim/reader/record_file_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,12 @@ template <>
bool
record_file_reader_t<std::ifstream>::read_next_entry()
{
if (!input_file_->read((char *)&cur_entry_, sizeof(cur_entry_)))
if (!input_file_->read((char *)&cur_entry_, sizeof(cur_entry_))) {
if (input_file_->eof()) {
eof_ = true;
}
return false;
}
VPRINT(this, 4, "Read from file: type=%s (%d), size=%d, addr=%zu\n",
trace_type_names[cur_entry_.type], cur_entry_.type, cur_entry_.size,
cur_entry_.addr);
Expand Down
Loading

0 comments on commit 43cbfc3

Please sign in to comment.