njn | c953984 | 2002-10-02 13:26:35 +0000 | [diff] [blame] | 1 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2 | /*--------------------------------------------------------------------*/ |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame^] | 3 | /*--- An example Valgrind tool. lk_main.c ---*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 4 | /*--------------------------------------------------------------------*/ |
| 5 | |
| 6 | /* |
nethercote | 137bc55 | 2003-11-14 17:47:54 +0000 | [diff] [blame] | 7 | This file is part of Lackey, an example Valgrind tool that does |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame^] | 8 | some simple program measurement and tracing. |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 9 | |
njn | 5361242 | 2005-03-12 16:22:54 +0000 | [diff] [blame] | 10 | Copyright (C) 2002-2005 Nicholas Nethercote |
njn | 2bc1012 | 2005-05-08 02:10:27 +0000 | [diff] [blame] | 11 | njn@valgrind.org |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 12 | |
| 13 | This program is free software; you can redistribute it and/or |
| 14 | modify it under the terms of the GNU General Public License as |
| 15 | published by the Free Software Foundation; either version 2 of the |
| 16 | License, or (at your option) any later version. |
| 17 | |
| 18 | This program is distributed in the hope that it will be useful, but |
| 19 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 21 | General Public License for more details. |
| 22 | |
| 23 | You should have received a copy of the GNU General Public License |
| 24 | along with this program; if not, write to the Free Software |
| 25 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 26 | 02111-1307, USA. |
| 27 | |
| 28 | The GNU General Public License is contained in the file COPYING. |
| 29 | */ |
| 30 | |
njn | fd73ebb | 2005-12-30 22:39:58 +0000 | [diff] [blame] | 31 | // This tool shows how to do some basic instrumentation. |
| 32 | // |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame^] | 33 | // There are three kinds of instrumentation it can do. They can be turned |
| 34 | // on/off independently with command line options: |
| 35 | // |
| 36 | // * --basic-counts : do basic counts, eg. number of instructions |
| 37 | // executed, jumps executed, etc. |
| 38 | // * --detailed-counts: do more detailed counts: number of loads, stores |
| 39 | // and ALU operations of different sizes. |
| 40 | // * --trace-mem=yes: trace all (data) memory accesses. |
| 41 | // |
| 42 | // The code for each kind of instrumentation is guarded by a clo_* variable: |
| 43 | // clo_basic_counts, clo_detailed_counts and clo_trace_mem. |
| 44 | // |
| 45 | // If you want to modify any of the instrumentation code, look for the code |
| 46 | // that is guarded by the relevant clo_* variable (eg. clo_trace_mem) |
| 47 | // If you're not interested in the other kinds of instrumentation you can |
| 48 | // remove them. If you want to do more complex modifications, please read |
| 49 | // VEX/pub/libvex_ir.h to understand the intermediate representation. |
| 50 | // |
| 51 | // |
| 52 | // Specific Details about --trace-mem=yes |
| 53 | // -------------------------------------- |
| 54 | // The address trace produced by --trace-mem=yes is good, but not perfect; |
| 55 | // see Section 3.3.7 of Nicholas Nethercote's PhD dissertation "Dynamic |
| 56 | // Binary Analysis and Instrumentation", 2004, for details about the few |
| 57 | // loads and stores that it misses, and other caveats about the accuracy of |
| 58 | // the address trace. |
njn | fd73ebb | 2005-12-30 22:39:58 +0000 | [diff] [blame] | 59 | // |
njn | 9dd7277 | 2006-03-11 06:48:20 +0000 | [diff] [blame] | 60 | // [Actually, the traces aren't quite right because instructions that modify |
| 61 | // a memory location are treated like a load followed by a store.] |
| 62 | // |
njn | fd73ebb | 2005-12-30 22:39:58 +0000 | [diff] [blame] | 63 | // For further inspiration, you should look at cachegrind/cg_main.c which |
| 64 | // handles memory accesses in a more sophisticated way -- it groups them |
| 65 | // together for processing into twos and threes so that fewer C calls are |
| 66 | // made and things run faster. |
| 67 | |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 68 | #include "pub_tool_basics.h" |
njn | 43b9a8a | 2005-05-10 04:37:01 +0000 | [diff] [blame] | 69 | #include "pub_tool_tooliface.h" |
njn | f39e9a3 | 2005-06-12 02:43:17 +0000 | [diff] [blame] | 70 | #include "pub_tool_libcassert.h" |
njn | 36a20fa | 2005-06-03 03:08:39 +0000 | [diff] [blame] | 71 | #include "pub_tool_libcprint.h" |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 72 | #include "pub_tool_debuginfo.h" |
| 73 | #include "pub_tool_libcbase.h" |
| 74 | #include "pub_tool_options.h" |
sewardj | 5fed8c0 | 2005-12-23 12:56:11 +0000 | [diff] [blame] | 75 | #include "pub_tool_machine.h" // VG_(fnptr_to_fnentry) |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 76 | |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame^] | 77 | /*------------------------------------------------------------*/ |
| 78 | /*--- Command line options ---*/ |
| 79 | /*------------------------------------------------------------*/ |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 80 | |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame^] | 81 | /* Command line options controlling instrumentation kinds, as described at |
| 82 | * the top of this file. */ |
| 83 | static Bool clo_basic_counts = True; |
| 84 | static Bool clo_detailed_counts = False; |
| 85 | static Bool clo_trace_mem = False; |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 86 | |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame^] | 87 | /* The name of the function of which the number of calls (under |
| 88 | * --basic-counts=yes) is to be counted, with default. Override with command |
| 89 | * line option --fnname. */ |
| 90 | static Char* clo_fnname = "_dl_runtime_resolve"; |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 91 | |
| 92 | static Bool lk_process_cmd_line_option(Char* arg) |
| 93 | { |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame^] | 94 | VG_STR_CLO(arg, "--fnname", clo_fnname) |
| 95 | else VG_BOOL_CLO(arg, "--basic-counts", clo_basic_counts) |
| 96 | else VG_BOOL_CLO(arg, "--detailed-counts", clo_detailed_counts) |
| 97 | else VG_BOOL_CLO(arg, "--trace-mem", clo_trace_mem) |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 98 | else |
| 99 | return False; |
| 100 | |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame^] | 101 | tl_assert(clo_fnname); |
| 102 | tl_assert(clo_fnname[0]); |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 103 | return True; |
| 104 | } |
| 105 | |
| 106 | static void lk_print_usage(void) |
| 107 | { |
| 108 | VG_(printf)( |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame^] | 109 | " --basic-counts=no|yes count instructions, jumps, etc. [no]\n" |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 110 | " --detailed-counts=no|yes count loads, stores and alu ops [no]\n" |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame^] | 111 | " --trace-mem=no|yes trace all loads and stores [no]\n" |
| 112 | " --fnname=<name> count calls to <name> (only used if\n" |
| 113 | " --basic-count=yes) [_dl_runtime_resolve]\n" |
| 114 | |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 115 | ); |
| 116 | } |
| 117 | |
| 118 | static void lk_print_debug_usage(void) |
| 119 | { |
| 120 | } |
| 121 | |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame^] | 122 | /*------------------------------------------------------------*/ |
| 123 | /*--- Data and helpers for --basic-counts ---*/ |
| 124 | /*------------------------------------------------------------*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 125 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 126 | /* Nb: use ULongs because the numbers can get very big */ |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 127 | static ULong n_func_calls = 0; |
| 128 | static ULong n_BBs_entered = 0; |
| 129 | static ULong n_BBs_completed = 0; |
| 130 | static ULong n_IRStmts = 0; |
| 131 | static ULong n_guest_instrs = 0; |
| 132 | static ULong n_Jccs = 0; |
| 133 | static ULong n_Jccs_untaken = 0; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 134 | |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 135 | static void add_one_func_call(void) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 136 | { |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 137 | n_func_calls++; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 138 | } |
| 139 | |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 140 | static void add_one_BB_entered(void) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 141 | { |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 142 | n_BBs_entered++; |
| 143 | } |
| 144 | |
| 145 | static void add_one_BB_completed(void) |
| 146 | { |
| 147 | n_BBs_completed++; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 148 | } |
| 149 | |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 150 | static void add_one_IRStmt(void) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 151 | { |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 152 | n_IRStmts++; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 153 | } |
| 154 | |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 155 | static void add_one_guest_instr(void) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 156 | { |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 157 | n_guest_instrs++; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | static void add_one_Jcc(void) |
| 161 | { |
| 162 | n_Jccs++; |
| 163 | } |
| 164 | |
| 165 | static void add_one_Jcc_untaken(void) |
| 166 | { |
| 167 | n_Jccs_untaken++; |
| 168 | } |
| 169 | |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame^] | 170 | /*------------------------------------------------------------*/ |
| 171 | /*--- Data and helpers for --detailed-counts ---*/ |
| 172 | /*------------------------------------------------------------*/ |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 173 | |
| 174 | /* --- Operations --- */ |
| 175 | |
| 176 | typedef enum { OpLoad=0, OpStore=1, OpAlu=2 } Op; |
| 177 | |
| 178 | #define N_OPS 3 |
| 179 | |
| 180 | |
| 181 | /* --- Types --- */ |
| 182 | |
| 183 | #define N_TYPES 9 |
| 184 | |
| 185 | static Int type2index ( IRType ty ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 186 | { |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 187 | switch (ty) { |
| 188 | case Ity_I1: return 0; |
| 189 | case Ity_I8: return 1; |
| 190 | case Ity_I16: return 2; |
| 191 | case Ity_I32: return 3; |
| 192 | case Ity_I64: return 4; |
| 193 | case Ity_I128: return 5; |
| 194 | case Ity_F32: return 6; |
| 195 | case Ity_F64: return 7; |
| 196 | case Ity_V128: return 8; |
| 197 | default: tl_assert(0); break; |
| 198 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 199 | } |
| 200 | |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 201 | static HChar* nameOfTypeIndex ( IRType ty ) |
| 202 | { |
| 203 | switch (ty) { |
| 204 | case 0: return "I1"; break; |
| 205 | case 1: return "I8"; break; |
| 206 | case 2: return "I16"; break; |
| 207 | case 3: return "I32"; break; |
| 208 | case 4: return "I64"; break; |
| 209 | case 5: return "I128"; break; |
| 210 | case 6: return "F32"; break; |
| 211 | case 7: return "F64"; break; |
| 212 | case 8: return "V128"; break; |
| 213 | default: tl_assert(0); break; |
| 214 | } |
| 215 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 216 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 217 | |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 218 | /* --- Counts --- */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 219 | |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 220 | static ULong detailCounts[N_OPS][N_TYPES]; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 221 | |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 222 | /* The helper that is called from the instrumented code. */ |
| 223 | static VG_REGPARM(1) |
| 224 | void increment_detail(ULong* detail) |
| 225 | { |
| 226 | (*detail)++; |
| 227 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 228 | |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 229 | /* A helper that adds the instrumentation for a detail. */ |
| 230 | static void instrument_detail(IRBB* bb, Op op, IRType type) |
| 231 | { |
| 232 | IRDirty* di; |
| 233 | IRExpr** argv; |
| 234 | const UInt typeIx = type2index(type); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 235 | |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 236 | tl_assert(op < N_OPS); |
| 237 | tl_assert(typeIx < N_TYPES); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 238 | |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 239 | argv = mkIRExprVec_1( mkIRExpr_HWord( (HWord)&detailCounts[op][typeIx] ) ); |
sewardj | 5fed8c0 | 2005-12-23 12:56:11 +0000 | [diff] [blame] | 240 | di = unsafeIRDirty_0_N( 1, "increment_detail", |
| 241 | VG_(fnptr_to_fnentry)( &increment_detail ), |
| 242 | argv); |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 243 | addStmtToIRBB( bb, IRStmt_Dirty(di) ); |
| 244 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 245 | |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 246 | /* Summarize and print the details. */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 247 | |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 248 | static void print_details ( void ) |
| 249 | { |
| 250 | Int typeIx; |
| 251 | VG_(message)(Vg_UserMsg, |
| 252 | " Type Loads Stores AluOps"); |
| 253 | VG_(message)(Vg_UserMsg, |
| 254 | " -------------------------------------------"); |
| 255 | for (typeIx = 0; typeIx < N_TYPES; typeIx++) { |
| 256 | VG_(message)(Vg_UserMsg, |
| 257 | " %4s %,12llu %,12llu %,12llu", |
| 258 | nameOfTypeIndex( typeIx ), |
| 259 | detailCounts[OpLoad ][typeIx], |
| 260 | detailCounts[OpStore][typeIx], |
| 261 | detailCounts[OpAlu ][typeIx] |
| 262 | ); |
| 263 | } |
| 264 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 265 | |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 266 | |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame^] | 267 | /*------------------------------------------------------------*/ |
| 268 | /*--- Data and helpers for --trace-mem ---*/ |
| 269 | /*------------------------------------------------------------*/ |
njn | fd73ebb | 2005-12-30 22:39:58 +0000 | [diff] [blame] | 270 | |
| 271 | static VG_REGPARM(2) void trace_load(Addr addr, SizeT size) |
| 272 | { |
| 273 | VG_(printf)("load : %p, %d\n", addr, size); |
| 274 | } |
| 275 | |
| 276 | static VG_REGPARM(2) void trace_store(Addr addr, SizeT size) |
| 277 | { |
| 278 | VG_(printf)("store: %p, %d\n", addr, size); |
| 279 | } |
| 280 | |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame^] | 281 | |
| 282 | /*------------------------------------------------------------*/ |
| 283 | /*--- Basic tool functions ---*/ |
| 284 | /*------------------------------------------------------------*/ |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 285 | |
| 286 | static void lk_post_clo_init(void) |
| 287 | { |
| 288 | Int op, tyIx; |
| 289 | |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame^] | 290 | if (clo_detailed_counts) { |
| 291 | for (op = 0; op < N_OPS; op++) |
| 292 | for (tyIx = 0; tyIx < N_TYPES; tyIx++) |
| 293 | detailCounts[op][tyIx] = 0; |
| 294 | } |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 295 | } |
| 296 | |
sewardj | 4ba057c | 2005-10-18 12:04:18 +0000 | [diff] [blame] | 297 | static |
sewardj | 461df9c | 2006-01-17 02:06:39 +0000 | [diff] [blame] | 298 | IRBB* lk_instrument ( VgCallbackClosure* closure, |
| 299 | IRBB* bb_in, |
| 300 | VexGuestLayout* layout, |
| 301 | VexGuestExtents* vge, |
| 302 | IRType gWordTy, IRType hWordTy ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 303 | { |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 304 | IRDirty* di; |
| 305 | Int i; |
sewardj | d54babf | 2005-03-21 00:55:49 +0000 | [diff] [blame] | 306 | IRBB* bb; |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 307 | Char fnname[100]; |
| 308 | IRType type; |
njn | fd73ebb | 2005-12-30 22:39:58 +0000 | [diff] [blame] | 309 | IRExpr** argv; |
| 310 | IRExpr* addr_expr; |
| 311 | IRExpr* size_expr; |
sewardj | d54babf | 2005-03-21 00:55:49 +0000 | [diff] [blame] | 312 | |
| 313 | if (gWordTy != hWordTy) { |
| 314 | /* We don't currently support this case. */ |
| 315 | VG_(tool_panic)("host/guest word size mismatch"); |
| 316 | } |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 317 | |
| 318 | /* Set up BB */ |
sewardj | d54babf | 2005-03-21 00:55:49 +0000 | [diff] [blame] | 319 | bb = emptyIRBB(); |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 320 | bb->tyenv = dopyIRTypeEnv(bb_in->tyenv); |
| 321 | bb->next = dopyIRExpr(bb_in->next); |
| 322 | bb->jumpkind = bb_in->jumpkind; |
| 323 | |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 324 | // Copy verbatim any IR preamble preceding the first IMark |
| 325 | i = 0; |
| 326 | while (i < bb_in->stmts_used && bb_in->stmts[i]->tag != Ist_IMark) { |
| 327 | addStmtToIRBB( bb, bb_in->stmts[i] ); |
| 328 | i++; |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 329 | } |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 330 | |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame^] | 331 | if (clo_basic_counts) { |
| 332 | /* Count this basic block. */ |
| 333 | di = unsafeIRDirty_0_N( 0, "add_one_BB_entered", |
| 334 | VG_(fnptr_to_fnentry)( &add_one_BB_entered ), |
| 335 | mkIRExprVec_0() ); |
| 336 | addStmtToIRBB( bb, IRStmt_Dirty(di) ); |
| 337 | } |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 338 | |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 339 | for (/*use current i*/; i < bb_in->stmts_used; i++) { |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 340 | IRStmt* st = bb_in->stmts[i]; |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 341 | if (!st || st->tag == Ist_NoOp) continue; |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 342 | |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame^] | 343 | if (clo_basic_counts) { |
| 344 | /* Count one VEX statement. */ |
| 345 | di = unsafeIRDirty_0_N( 0, "add_one_IRStmt", |
| 346 | VG_(fnptr_to_fnentry)( &add_one_IRStmt ), |
| 347 | mkIRExprVec_0() ); |
| 348 | addStmtToIRBB( bb, IRStmt_Dirty(di) ); |
| 349 | } |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 350 | |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 351 | switch (st->tag) { |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 352 | case Ist_IMark: |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame^] | 353 | if (clo_basic_counts) { |
| 354 | /* Count guest instruction. */ |
| 355 | di = unsafeIRDirty_0_N( 0, "add_one_guest_instr", |
| 356 | VG_(fnptr_to_fnentry)( &add_one_guest_instr ), |
| 357 | mkIRExprVec_0() ); |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 358 | addStmtToIRBB( bb, IRStmt_Dirty(di) ); |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame^] | 359 | |
| 360 | /* An unconditional branch to a known destination in the |
| 361 | * guest's instructions can be represented, in the IRBB to |
| 362 | * instrument, by the VEX statements that are the |
| 363 | * translation of that known destination. This feature is |
| 364 | * called 'BB chasing' and can be influenced by command |
| 365 | * line option --vex-guest-chase-thresh. |
| 366 | * |
| 367 | * To get an accurate count of the calls to a specific |
| 368 | * function, taking BB chasing into account, we need to |
| 369 | * check for each guest instruction (Ist_IMark) if it is |
| 370 | * the entry point of a function. |
| 371 | */ |
| 372 | tl_assert(clo_fnname); |
| 373 | tl_assert(clo_fnname[0]); |
| 374 | if (VG_(get_fnname_if_entry)(st->Ist.IMark.addr, |
| 375 | fnname, sizeof(fnname)) |
| 376 | && 0 == VG_(strcmp)(fnname, clo_fnname)) { |
| 377 | di = unsafeIRDirty_0_N( |
| 378 | 0, "add_one_func_call", |
| 379 | VG_(fnptr_to_fnentry)( &add_one_func_call ), |
| 380 | mkIRExprVec_0() ); |
| 381 | addStmtToIRBB( bb, IRStmt_Dirty(di) ); |
| 382 | } |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 383 | } |
| 384 | addStmtToIRBB( bb, st ); |
| 385 | break; |
| 386 | |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 387 | case Ist_Exit: |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame^] | 388 | if (clo_basic_counts) { |
| 389 | /* Count Jcc */ |
| 390 | di = unsafeIRDirty_0_N( 0, "add_one_Jcc", |
| 391 | VG_(fnptr_to_fnentry)( &add_one_Jcc ), |
| 392 | mkIRExprVec_0() ); |
| 393 | addStmtToIRBB( bb, IRStmt_Dirty(di) ); |
| 394 | } |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 395 | |
| 396 | addStmtToIRBB( bb, st ); |
| 397 | |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame^] | 398 | if (clo_basic_counts) { |
| 399 | /* Count non-taken Jcc */ |
| 400 | di = unsafeIRDirty_0_N( 0, "add_one_Jcc_untaken", |
| 401 | VG_(fnptr_to_fnentry)( |
| 402 | &add_one_Jcc_untaken ), |
| 403 | mkIRExprVec_0() ); |
| 404 | addStmtToIRBB( bb, IRStmt_Dirty(di) ); |
| 405 | } |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 406 | break; |
| 407 | |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 408 | case Ist_Store: |
njn | fd73ebb | 2005-12-30 22:39:58 +0000 | [diff] [blame] | 409 | // Add a call to trace_store() if --trace-mem=yes. |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame^] | 410 | if (clo_trace_mem) { |
njn | fd73ebb | 2005-12-30 22:39:58 +0000 | [diff] [blame] | 411 | addr_expr = st->Ist.Store.addr; |
| 412 | size_expr = mkIRExpr_HWord( |
| 413 | sizeofIRType( |
| 414 | typeOfIRExpr(bb->tyenv, st->Ist.Store.data))); |
| 415 | argv = mkIRExprVec_2( addr_expr, size_expr ); |
| 416 | di = unsafeIRDirty_0_N( /*regparms*/2, |
| 417 | "trace_store", |
| 418 | VG_(fnptr_to_fnentry)( trace_store ), |
| 419 | argv ); |
| 420 | addStmtToIRBB( bb, IRStmt_Dirty(di) ); |
| 421 | } |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame^] | 422 | if (clo_detailed_counts) { |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 423 | type = typeOfIRExpr(bb->tyenv, st->Ist.Store.data); |
| 424 | tl_assert(type != Ity_INVALID); |
| 425 | instrument_detail( bb, OpStore, type ); |
| 426 | } |
| 427 | addStmtToIRBB( bb, st ); |
| 428 | break; |
| 429 | |
| 430 | case Ist_Tmp: |
njn | fd73ebb | 2005-12-30 22:39:58 +0000 | [diff] [blame] | 431 | // Add a call to trace_load() if --trace-mem=yes. |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame^] | 432 | if (clo_trace_mem) { |
njn | fd73ebb | 2005-12-30 22:39:58 +0000 | [diff] [blame] | 433 | IRExpr* data = st->Ist.Tmp.data; |
| 434 | if (data->tag == Iex_Load) { |
| 435 | addr_expr = data->Iex.Load.addr; |
| 436 | size_expr = mkIRExpr_HWord( sizeofIRType(data->Iex.Load.ty) ); |
| 437 | argv = mkIRExprVec_2( addr_expr, size_expr ); |
| 438 | di = unsafeIRDirty_0_N( /*regparms*/2, |
| 439 | "trace_load", |
| 440 | VG_(fnptr_to_fnentry)( trace_load ), |
| 441 | argv ); |
| 442 | addStmtToIRBB( bb, IRStmt_Dirty(di) ); |
| 443 | } |
| 444 | } |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame^] | 445 | if (clo_detailed_counts) { |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 446 | IRExpr* expr = st->Ist.Tmp.data; |
| 447 | type = typeOfIRExpr(bb->tyenv, expr); |
| 448 | tl_assert(type != Ity_INVALID); |
| 449 | switch (expr->tag) { |
| 450 | case Iex_Load: |
| 451 | instrument_detail( bb, OpLoad, type ); |
| 452 | break; |
| 453 | case Iex_Unop: |
| 454 | case Iex_Binop: |
sewardj | e91cea7 | 2006-02-08 19:32:02 +0000 | [diff] [blame] | 455 | case Iex_Triop: |
| 456 | case Iex_Qop: |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 457 | case Iex_Mux0X: |
| 458 | instrument_detail( bb, OpAlu, type ); |
| 459 | break; |
| 460 | default: |
| 461 | break; |
| 462 | } |
| 463 | } |
| 464 | addStmtToIRBB( bb, st ); |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 465 | break; |
| 466 | |
| 467 | default: |
sewardj | bd598e1 | 2005-01-07 12:10:21 +0000 | [diff] [blame] | 468 | addStmtToIRBB( bb, st ); |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 469 | } |
| 470 | } |
| 471 | |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame^] | 472 | if (clo_basic_counts) { |
| 473 | /* Count this basic block. */ |
| 474 | di = unsafeIRDirty_0_N( 0, "add_one_BB_completed", |
| 475 | VG_(fnptr_to_fnentry)( &add_one_BB_completed ), |
| 476 | mkIRExprVec_0() ); |
| 477 | addStmtToIRBB( bb, IRStmt_Dirty(di) ); |
| 478 | } |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 479 | |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 480 | return bb; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 481 | } |
| 482 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 483 | static void lk_fini(Int exitcode) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 484 | { |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 485 | char percentify_buf[4]; /* Two digits, '%' and 0. */ |
| 486 | const int percentify_size = sizeof(percentify_buf); |
| 487 | const int percentify_decs = 0; |
| 488 | |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame^] | 489 | tl_assert(clo_fnname); |
| 490 | tl_assert(clo_fnname[0]); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 491 | |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame^] | 492 | if (clo_basic_counts) { |
| 493 | VG_(message)(Vg_UserMsg, |
| 494 | "Counted %,llu calls to %s()", n_func_calls, clo_fnname); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 495 | |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame^] | 496 | VG_(message)(Vg_UserMsg, ""); |
| 497 | VG_(message)(Vg_UserMsg, "Jccs:"); |
| 498 | VG_(message)(Vg_UserMsg, " total: %,llu", n_Jccs); |
| 499 | VG_(percentify)((n_Jccs - n_Jccs_untaken), (n_Jccs ? n_Jccs : 1), |
| 500 | percentify_decs, percentify_size, percentify_buf); |
| 501 | VG_(message)(Vg_UserMsg, " taken: %,llu (%s)", |
| 502 | (n_Jccs - n_Jccs_untaken), percentify_buf); |
| 503 | |
| 504 | VG_(message)(Vg_UserMsg, ""); |
| 505 | VG_(message)(Vg_UserMsg, "Executed:"); |
| 506 | VG_(message)(Vg_UserMsg, " BBs entered: %,llu", n_BBs_entered); |
| 507 | VG_(message)(Vg_UserMsg, " BBs completed: %,llu", n_BBs_completed); |
| 508 | VG_(message)(Vg_UserMsg, " guest instrs: %,llu", n_guest_instrs); |
| 509 | VG_(message)(Vg_UserMsg, " IRStmts: %,llu", n_IRStmts); |
| 510 | |
| 511 | VG_(message)(Vg_UserMsg, ""); |
| 512 | VG_(message)(Vg_UserMsg, "Ratios:"); |
| 513 | tl_assert(n_BBs_entered); // Paranoia time. |
| 514 | VG_(message)(Vg_UserMsg, " guest instrs : BB entered = %3u : 10", |
| 515 | 10 * n_guest_instrs / n_BBs_entered); |
| 516 | VG_(message)(Vg_UserMsg, " IRStmts : BB entered = %3u : 10", |
| 517 | 10 * n_IRStmts / n_BBs_entered); |
| 518 | tl_assert(n_guest_instrs); // Paranoia time. |
| 519 | VG_(message)(Vg_UserMsg, " IRStmts : guest instr = %3u : 10", |
| 520 | 10 * n_IRStmts / n_guest_instrs); |
| 521 | } |
| 522 | |
| 523 | if (clo_detailed_counts) { |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 524 | VG_(message)(Vg_UserMsg, ""); |
| 525 | VG_(message)(Vg_UserMsg, "IR-level counts by type:"); |
| 526 | print_details(); |
| 527 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 528 | |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame^] | 529 | if (clo_basic_counts) { |
| 530 | VG_(message)(Vg_UserMsg, ""); |
| 531 | VG_(message)(Vg_UserMsg, "Exit code: %d", exitcode); |
| 532 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 533 | } |
| 534 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 535 | static void lk_pre_clo_init(void) |
| 536 | { |
| 537 | VG_(details_name) ("Lackey"); |
| 538 | VG_(details_version) (NULL); |
| 539 | VG_(details_description) ("an example Valgrind tool"); |
| 540 | VG_(details_copyright_author)( |
| 541 | "Copyright (C) 2002-2005, and GNU GPL'd, by Nicholas Nethercote."); |
| 542 | VG_(details_bug_reports_to) (VG_BUGS_TO); |
| 543 | VG_(details_avg_translation_sizeB) ( 175 ); |
| 544 | |
| 545 | VG_(basic_tool_funcs) (lk_post_clo_init, |
| 546 | lk_instrument, |
| 547 | lk_fini); |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 548 | VG_(needs_command_line_options)(lk_process_cmd_line_option, |
| 549 | lk_print_usage, |
| 550 | lk_print_debug_usage); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 551 | } |
| 552 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 553 | VG_DETERMINE_INTERFACE_VERSION(lk_pre_clo_init) |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 554 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 555 | /*--------------------------------------------------------------------*/ |
njn25 | cac76cb | 2002-09-23 11:21:57 +0000 | [diff] [blame] | 556 | /*--- end lk_main.c ---*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 557 | /*--------------------------------------------------------------------*/ |