* Copyright (c) 2023 Google, Inc. All rights reserved.
* **********************************************************/
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of Google, Inc. nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL VMWARE, INC. OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/
*/
#ifndef _SYSCALL_PT_TRACE_
#define _SYSCALL_PT_TRACE_ 1
#include <cstddef>
#include <string>
#include "dr_api.h"
#include "drmemtrace.h"
#include "drpttracer.h"
#include "trace_entry.h"
#include "utils.h"
namespace dynamorio {
namespace drmemtrace {
* This can ensure the pttracer handle is cleaned up when it is out of scope.
*/
struct drpttracer_handle_autoclean_t {
public:
drpttracer_handle_autoclean_t(void *drcontext, void *handle)
: drcontext { drcontext }
, handle { handle }
{
}
~drpttracer_handle_autoclean_t()
{
reset();
}
void
reset()
{
ASSERT(drcontext != nullptr, "invalid drcontext");
if (handle != nullptr) {
drpttracer_destroy_handle(drcontext, handle);
handle = nullptr;
}
}
void *drcontext = nullptr;
void *handle = nullptr;
};
* This can ensure the pttracer handle is cleaned up when it is out of scope.
*/
struct drpttracer_output_autoclean_t {
public:
drpttracer_output_autoclean_t(void *drcontext, drpttracer_output_t *data)
: drcontext { drcontext }
, data { data }
{
}
~drpttracer_output_autoclean_t()
{
ASSERT(drcontext != nullptr, "invalid drcontext");
if (data != nullptr) {
void *drcontext = dr_get_current_drcontext();
drpttracer_destroy_output(drcontext, data);
data = nullptr;
}
}
void *drcontext = nullptr;
drpttracer_output_t *data = nullptr;
};
#define INVALID_SYSNUM -1
*/
class syscall_pt_trace_t {
public:
syscall_pt_trace_t();
~syscall_pt_trace_t();
* The instance will dump the kernel PT trace for every syscall. So the caller must
* pass in the output directory and the file write function.
*/
bool
init(void *drcontext, char *pt_dir_name, drmemtrace_open_file_func_t open_file_func,
drmemtrace_write_file_func_t write_file_func,
drmemtrace_close_file_func_t close_file_func);
bool
start_syscall_pt_trace(IN int sysnum);
bool
stop_syscall_pt_trace();
int
get_cur_recording_sysnum()
{
return cur_recording_sysnum_;
}
*/
int
get_traced_syscall_idx()
{
return traced_syscall_idx_;
}
* It can be used to filter out the syscalls that are not interesting or not
* supported.
*/
static bool
is_syscall_pt_trace_enabled(IN int sysnum);
private:
bool
metadata_dump(pt_metadata_t metadata);
bool
trace_data_dump(drpttracer_output_autoclean_t &output);
drmemtrace_open_file_func_t open_file_func_;
drmemtrace_write_file_func_t write_file_func_;
drmemtrace_close_file_func_t close_file_func_;
* function should be called only once per thread.
*/
bool is_initialized_;
drpttracer_handle_autoclean_t pttracer_handle_;
* data for each system call. The buffer will be updated when stop_syscall_pt_trace()
* is invoked.
*/
drpttracer_output_autoclean_t pttracer_output_buffer_;
int traced_syscall_idx_;
int cur_recording_sysnum_;
bool is_dumping_metadata_;
* We need ensure pass the same context to all drpttracer's APIs.
*/
void *drcontext_;
* It is a per-thread file. It stores the PT trace data and metadata for
* every syscall in the current thread.
*/
file_t output_file_;
};
}
}
#endif