Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions api/samples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ add_sample_client(div "div.c" "drmgr")
add_sample_client(empty "empty.c" "")
add_sample_client(memtrace_simple "memtrace_simple.c;utils.c" "drmgr;drreg;drutil;drx")
add_sample_client(memval_simple "memval_simple.c;utils.c" "drmgr;drreg;drutil;drx")
add_sample_client(instrace_simple "instrace_simple.c;utils.c" "drmgr;drreg;drx")
add_sample_client(instrace_simple "instrace_simple.c;utils.c" "drmgr;drreg;drutil;drx")
add_sample_client(opcode_count "opcode_count.cpp" "drmgr;drreg;drx;droption")
if (X86 OR RISCV64) # TODO i#1551, #1569: Port to AArchXX.
add_sample_client(cbr "cbr.c" "drmgr")
Expand All @@ -253,9 +253,9 @@ if (X86) # XXX i#1551, i#1569, i#3544: port to ARM/AArch64/RISCV64
"drcontainers;drmgr;drreg;drutil;drx")
_DR_append_property_list(TARGET memtrace_x86_text COMPILE_DEFINITIONS "OUTPUT_TEXT")
add_sample_client(instrace_x86_binary "instrace_x86.c;utils.c"
"drcontainers;drmgr;drreg;drx")
"drcontainers;drmgr;drreg;drutil;drx")
add_sample_client(instrace_x86_text "instrace_x86.c;utils.c"
"drcontainers;drmgr;drreg;drx")
"drcontainers;drmgr;drreg;drutil;drx")
_DR_append_property_list(TARGET instrace_x86_text COMPILE_DEFINITIONS "OUTPUT_TEXT")
add_sample_client(prefetch "prefetch.c" "drmgr")
if (NOT WIN32)
Expand Down
15 changes: 15 additions & 0 deletions api/samples/instrace_simple.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
#include "dr_api.h"
#include "drmgr.h"
#include "drreg.h"
#include "drutil.h"
#include "utils.h"

/* Each ins_ref_t describes an executed instruction. */
Expand Down Expand Up @@ -198,6 +199,16 @@ instrument_instr(void *drcontext, instrlist_t *ilist, instr_t *where)
DR_ASSERT(false);
}

static dr_emit_flags_t
event_bb(void *drcontext, void *tag, instrlist_t *bb, bool for_trace, bool translating)
{
instr_t *first_app = instrlist_first_app(bb);
if (first_app == NULL || !drmgr_is_first_instr(drcontext, first_app))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this condition required?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check ensures REP expansion runs only once per application basic block, avoiding duplicate expansion during re-instrumentation or trace construction.

return DR_EMIT_DEFAULT;
DR_ASSERT(drutil_expand_rep_string(drcontext, bb));
return DR_EMIT_DEFAULT;
}

/* For each app instr, we insert inline code to fill the buffer. */
static dr_emit_flags_t
event_app_instruction(void *drcontext, void *tag, instrlist_t *bb, instr_t *instr,
Expand Down Expand Up @@ -288,11 +299,13 @@ event_exit(void)
if (!drmgr_unregister_tls_field(tls_idx) ||
!drmgr_unregister_thread_init_event(event_thread_init) ||
!drmgr_unregister_thread_exit_event(event_thread_exit) ||
!drmgr_unregister_bb_app2app_event(event_bb) ||
!drmgr_unregister_bb_insertion_event(event_app_instruction) ||
drreg_exit() != DRREG_SUCCESS)
DR_ASSERT(false);

dr_mutex_destroy(mutex);
drutil_exit();
drmgr_exit();
}

Expand All @@ -305,11 +318,13 @@ dr_client_main(client_id_t id, int argc, const char *argv[])
"http://dynamorio.org/issues");
if (!drmgr_init() || drreg_init(&ops) != DRREG_SUCCESS)
DR_ASSERT(false);
drutil_init();

/* register events */
drmgr_register_exit_event(event_exit);
if (!drmgr_register_thread_init_event(event_thread_init) ||
!drmgr_register_thread_exit_event(event_thread_exit) ||
!drmgr_register_bb_app2app_event(event_bb, NULL) ||
!drmgr_register_bb_instrumentation_event(NULL /*analysis_func*/,
event_app_instruction, NULL))
DR_ASSERT(false);
Expand Down
18 changes: 18 additions & 0 deletions api/samples/instrace_x86.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
#include "dr_api.h"
#include "drmgr.h"
#include "drreg.h"
#include "drutil.h"
#include "utils.h"

/* Each ins_ref_t describes an executed instruction. */
Expand Down Expand Up @@ -99,6 +100,9 @@ event_thread_init(void *drcontext);
static void
event_thread_exit(void *drcontext);

static dr_emit_flags_t
event_bb(void *drcontext, void *tag, instrlist_t *bb, bool for_trace, bool translating);

static dr_emit_flags_t
event_bb_insert(void *drcontext, void *tag, instrlist_t *bb, instr_t *instr,
bool for_trace, bool translating, void *user_data);
Expand Down Expand Up @@ -130,11 +134,13 @@ dr_client_main(client_id_t id, int argc, const char *argv[])
page_size = dr_page_size();
if (!drmgr_init() || drreg_init(&ops) != DRREG_SUCCESS)
DR_ASSERT(false);
drutil_init();
client_id = id;
mutex = dr_mutex_create();
drmgr_register_exit_event(event_exit);
if (!drmgr_register_thread_init_event(event_thread_init) ||
!drmgr_register_thread_exit_event(event_thread_exit) ||
!drmgr_register_bb_app2app_event(event_bb, NULL) ||
!drmgr_register_bb_instrumentation_event(NULL /*analysis func*/, event_bb_insert,
&priority)) {
/* something is wrong: can't continue */
Expand Down Expand Up @@ -176,11 +182,13 @@ event_exit()
if (!drmgr_unregister_tls_field(tls_index) ||
!drmgr_unregister_thread_init_event(event_thread_init) ||
!drmgr_unregister_thread_exit_event(event_thread_exit) ||
!drmgr_unregister_bb_app2app_event(event_bb) ||
!drmgr_unregister_bb_insertion_event(event_bb_insert) ||
drreg_exit() != DRREG_SUCCESS)
DR_ASSERT(false);

dr_mutex_destroy(mutex);
drutil_exit();
drmgr_exit();
}

Expand Down Expand Up @@ -240,6 +248,16 @@ event_thread_exit(void *drcontext)
dr_thread_free(drcontext, data, sizeof(per_thread_t));
}

static dr_emit_flags_t
event_bb(void *drcontext, void *tag, instrlist_t *bb, bool for_trace, bool translating)
{
instr_t *first_app = instrlist_first_app(bb);
if (first_app == NULL || !drmgr_is_first_instr(drcontext, first_app))
return DR_EMIT_DEFAULT;
DR_ASSERT(drutil_expand_rep_string(drcontext, bb));
return DR_EMIT_DEFAULT;
}

/* event_bb_insert calls instrument_instr to instrument every
* application memory reference.
*/
Expand Down
Loading