|
| 1 | +/* ********************************************************** |
| 2 | + * Copyright (c) 2012 Google, Inc. All rights reserved. |
| 3 | + * **********************************************************/ |
| 4 | + |
| 5 | +/* Trace and basic block count. */ |
| 6 | + |
| 7 | +#include "dr_api.h" |
| 8 | + |
| 9 | +/* Forward decls. */ |
| 10 | +static void event_exit(void); |
| 11 | + |
| 12 | +static dr_emit_flags_t |
| 13 | +event_basic_block(void *drcontext, void *tag, |
| 14 | + instrlist_t *bb, bool for_trace, |
| 15 | + bool translating); |
| 16 | + |
| 17 | +static dr_emit_flags_t |
| 18 | +event_trace(void *drcontext, void *tag, instrlist_t *trace, |
| 19 | + bool translating); |
| 20 | + |
| 21 | +/* Exported entry point. */ |
| 22 | +DR_EXPORT void |
| 23 | +dr_init(client_id_t id) |
| 24 | +{ |
| 25 | + /* Can't use libc printf: conflict with app! */ |
| 26 | + dr_printf("Client 'bbcount' initializing\n"); |
| 27 | + /* Register events. */ |
| 28 | + dr_register_bb_event(event_basic_block); |
| 29 | + dr_register_trace_event(event_trace); |
| 30 | + dr_register_exit_event(event_exit); |
| 31 | +} |
| 32 | + |
| 33 | +/************************************************************ |
| 34 | + * Basic block counting. |
| 35 | + */ |
| 36 | + |
| 37 | +static uint bbs_instrumented; |
| 38 | +static uint bbs_executed; |
| 39 | + |
| 40 | +/* Basic block execution callback, aka Pin's "analysis" callback. */ |
| 41 | +static void bb_count(void) { bbs_executed++; } |
| 42 | + |
| 43 | +/* Basic block instrumentation callback. */ |
| 44 | +static dr_emit_flags_t |
| 45 | +event_basic_block(void *drcontext, void *tag, instrlist_t *bb, |
| 46 | + bool for_trace, bool translating) |
| 47 | +{ |
| 48 | + /* bb event called again for tracing and translating, don't count those. */ |
| 49 | + if (!for_trace && !translating) |
| 50 | + bbs_instrumented++; |
| 51 | + if (!for_trace) |
| 52 | + dr_insert_clean_call(drcontext, bb, instrlist_first(bb), |
| 53 | + (void *)bb_count, false/*fpstate*/, 0/*num args*/); |
| 54 | + return DR_EMIT_DEFAULT; |
| 55 | +} |
| 56 | + |
| 57 | +/************************************************************ |
| 58 | + * Trace counting. |
| 59 | + */ |
| 60 | + |
| 61 | +static uint traces_instrumented; |
| 62 | +static uint traces_executed; |
| 63 | + |
| 64 | +/* Trace execution callback, aka Pin's "analysis" callback. */ |
| 65 | +static void trace_count(void) { traces_executed++; } |
| 66 | + |
| 67 | +/* Trace instrumentation callback. */ |
| 68 | +static dr_emit_flags_t |
| 69 | +event_trace(void *drcontext, void *tag, instrlist_t *trace, bool translating) |
| 70 | +{ |
| 71 | + /* trace event called again for translating, don't count those. */ |
| 72 | + if (!translating) |
| 73 | + traces_instrumented++; |
| 74 | + dr_insert_clean_call(drcontext, trace, instrlist_first(trace), |
| 75 | + (void *)trace_count, false/*fpstate*/, 0/*num args*/); |
| 76 | + return DR_EMIT_DEFAULT; |
| 77 | +} |
| 78 | + |
| 79 | +static void |
| 80 | +event_exit(void) |
| 81 | +{ |
| 82 | + dr_messagebox("Instrumentation results:\n" |
| 83 | + "%10u bbs instrumented\n" |
| 84 | + "%10u bbs executed\n" |
| 85 | + "%10u traces instrumented\n" |
| 86 | + "%10u traces executed\n", |
| 87 | + bbs_instrumented, |
| 88 | + bbs_executed, |
| 89 | + traces_instrumented, |
| 90 | + traces_executed); |
| 91 | +} |
0 commit comments