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 | |
sewardj | 9ebd6e0 | 2007-01-08 06:01:59 +0000 | [diff] [blame] | 10 | Copyright (C) 2002-2007 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 | // |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 33 | // There are four kinds of instrumentation it can do. They can be turned |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame] | 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. |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 41 | // * --trace-superblocks=yes: |
| 42 | // trace all superblock entries. Mostly of interest |
| 43 | // to the Valgrind developers. |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame] | 44 | // |
| 45 | // The code for each kind of instrumentation is guarded by a clo_* variable: |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 46 | // clo_basic_counts, clo_detailed_counts, clo_trace_mem and clo_trace_sbs. |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame] | 47 | // |
| 48 | // If you want to modify any of the instrumentation code, look for the code |
| 49 | // that is guarded by the relevant clo_* variable (eg. clo_trace_mem) |
| 50 | // If you're not interested in the other kinds of instrumentation you can |
| 51 | // remove them. If you want to do more complex modifications, please read |
| 52 | // VEX/pub/libvex_ir.h to understand the intermediate representation. |
| 53 | // |
| 54 | // |
| 55 | // Specific Details about --trace-mem=yes |
| 56 | // -------------------------------------- |
njn | eaf0ca9 | 2006-04-09 01:23:29 +0000 | [diff] [blame] | 57 | // Lackey's --trace-mem code is a good starting point for building Valgrind |
| 58 | // tools that act on memory loads and stores. It also could be used as is, |
| 59 | // with its output used as input to a post-mortem processing step. However, |
| 60 | // because memory traces can be very large, online analysis is generally |
| 61 | // better. |
njn | fd73ebb | 2005-12-30 22:39:58 +0000 | [diff] [blame] | 62 | // |
njn | eaf0ca9 | 2006-04-09 01:23:29 +0000 | [diff] [blame] | 63 | // It prints memory data access traces that look like this: |
| 64 | // |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 65 | // I 0023C790,2 # instruction read at 0x0023C790 of size 2 |
| 66 | // I 0023C792,5 |
| 67 | // S BE80199C,4 # data store at 0xBE80199C of size 4 |
| 68 | // I 0025242B,3 |
| 69 | // L BE801950,4 # data load at 0xBE801950 of size 4 |
| 70 | // I 0023D476,7 |
| 71 | // M 0025747C,1 # data modify at 0x0025747C of size 1 |
| 72 | // I 0023DC20,2 |
| 73 | // L 00254962,1 |
| 74 | // L BE801FB3,1 |
| 75 | // I 00252305,1 |
| 76 | // L 00254AEB,1 |
| 77 | // S 00257998,1 |
njn | eaf0ca9 | 2006-04-09 01:23:29 +0000 | [diff] [blame] | 78 | // |
| 79 | // Every instruction executed has an "instr" event representing it. |
| 80 | // Instructions that do memory accesses are followed by one or more "load", |
| 81 | // "store" or "modify" events. Some instructions do more than one load or |
| 82 | // store, as in the last two examples in the above trace. |
| 83 | // |
| 84 | // Here are some examples of x86 instructions that do different combinations |
| 85 | // of loads, stores, and modifies. |
| 86 | // |
| 87 | // Instruction Memory accesses Event sequence |
| 88 | // ----------- --------------- -------------- |
| 89 | // add %eax, %ebx No loads or stores instr |
| 90 | // |
| 91 | // movl (%eax), %ebx loads (%eax) instr, load |
| 92 | // |
| 93 | // movl %eax, (%ebx) stores (%ebx) instr, store |
| 94 | // |
| 95 | // incl (%ecx) modifies (%ecx) instr, modify |
| 96 | // |
| 97 | // cmpsb loads (%esi), loads(%edi) instr, load, load |
| 98 | // |
| 99 | // call*l (%edx) loads (%edx), stores -4(%esp) instr, load, store |
| 100 | // pushl (%edx) loads (%edx), stores -4(%esp) instr, load, store |
| 101 | // movsw loads (%esi), stores (%edi) instr, load, store |
| 102 | // |
| 103 | // Instructions using x86 "rep" prefixes are traced as if they are repeated |
| 104 | // N times. |
| 105 | // |
| 106 | // Lackey with --trace-mem gives good traces, but they are not perfect, for |
| 107 | // the following reasons: |
| 108 | // |
| 109 | // - It does not trace into the OS kernel, so system calls and other kernel |
| 110 | // operations (eg. some scheduling and signal handling code) are ignored. |
| 111 | // |
njn | e8d217d | 2006-10-18 23:46:26 +0000 | [diff] [blame] | 112 | // - It could model loads and stores done at the system call boundary using |
| 113 | // the pre_mem_read/post_mem_write events. For example, if you call |
| 114 | // fstat() you know that the passed in buffer has been written. But it |
| 115 | // currently does not do this. |
| 116 | // |
| 117 | // - Valgrind replaces some code (not much) with its own, notably parts of |
| 118 | // code for scheduling operations and signal handling. This code is not |
| 119 | // traced. |
njn | eaf0ca9 | 2006-04-09 01:23:29 +0000 | [diff] [blame] | 120 | // |
| 121 | // - There is no consideration of virtual-to-physical address mapping. |
| 122 | // This may not matter for many purposes. |
| 123 | // |
| 124 | // - Valgrind modifies the instruction stream in some very minor ways. For |
| 125 | // example, on x86 the bts, btc, btr instructions are incorrectly |
| 126 | // considered to always touch memory (this is a consequence of these |
| 127 | // instructions being very difficult to simulate). |
| 128 | // |
| 129 | // - Valgrind tools layout memory differently to normal programs, so the |
| 130 | // addresses you get will not be typical. Thus Lackey (and all Valgrind |
| 131 | // tools) is suitable for getting relative memory traces -- eg. if you |
| 132 | // want to analyse locality of memory accesses -- but is not good if |
| 133 | // absolute addresses are important. |
| 134 | // |
| 135 | // Despite all these warnings, Dullard's results should be good enough for a |
| 136 | // wide range of purposes. For example, Cachegrind shares all the above |
| 137 | // shortcomings and it is still useful. |
njn | 9dd7277 | 2006-03-11 06:48:20 +0000 | [diff] [blame] | 138 | // |
njn | fd73ebb | 2005-12-30 22:39:58 +0000 | [diff] [blame] | 139 | // For further inspiration, you should look at cachegrind/cg_main.c which |
njn | eaf0ca9 | 2006-04-09 01:23:29 +0000 | [diff] [blame] | 140 | // uses the same basic technique for tracing memory accesses, but also groups |
| 141 | // events together for processing into twos and threes so that fewer C calls |
| 142 | // are made and things run faster. |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 143 | // |
| 144 | // Specific Details about --trace-superblocks=yes |
| 145 | // ---------------------------------------------- |
| 146 | // Valgrind splits code up into single entry, multiple exit blocks |
| 147 | // known as superblocks. By itself, --trace-superblocks=yes just |
| 148 | // prints a message as each superblock is run: |
| 149 | // |
| 150 | // SB 04013170 |
| 151 | // SB 04013177 |
| 152 | // SB 04013173 |
| 153 | // SB 04013177 |
| 154 | // |
| 155 | // The hex number is the address of the first instruction in the |
| 156 | // superblock. You can see the relationship more obviously if you use |
| 157 | // --trace-superblocks=yes and --trace-mem=yes together. Then a "SB" |
| 158 | // message at address X is immediately followed by an "instr:" message |
| 159 | // for that address, as the first instruction in the block is |
| 160 | // executed, for example: |
| 161 | // |
| 162 | // SB 04014073 |
| 163 | // I 04014073,3 |
| 164 | // L 7FEFFF7F8,8 |
| 165 | // I 04014076,4 |
| 166 | // I 0401407A,3 |
| 167 | // I 0401407D,3 |
| 168 | // I 04014080,3 |
| 169 | // I 04014083,6 |
| 170 | |
njn | fd73ebb | 2005-12-30 22:39:58 +0000 | [diff] [blame] | 171 | |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 172 | #include "pub_tool_basics.h" |
njn | 43b9a8a | 2005-05-10 04:37:01 +0000 | [diff] [blame] | 173 | #include "pub_tool_tooliface.h" |
njn | f39e9a3 | 2005-06-12 02:43:17 +0000 | [diff] [blame] | 174 | #include "pub_tool_libcassert.h" |
njn | 36a20fa | 2005-06-03 03:08:39 +0000 | [diff] [blame] | 175 | #include "pub_tool_libcprint.h" |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 176 | #include "pub_tool_debuginfo.h" |
| 177 | #include "pub_tool_libcbase.h" |
| 178 | #include "pub_tool_options.h" |
sewardj | 5fed8c0 | 2005-12-23 12:56:11 +0000 | [diff] [blame] | 179 | #include "pub_tool_machine.h" // VG_(fnptr_to_fnentry) |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 180 | |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame] | 181 | /*------------------------------------------------------------*/ |
| 182 | /*--- Command line options ---*/ |
| 183 | /*------------------------------------------------------------*/ |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 184 | |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame] | 185 | /* Command line options controlling instrumentation kinds, as described at |
| 186 | * the top of this file. */ |
| 187 | static Bool clo_basic_counts = True; |
| 188 | static Bool clo_detailed_counts = False; |
| 189 | static Bool clo_trace_mem = False; |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 190 | static Bool clo_trace_sbs = False; |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 191 | |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame] | 192 | /* The name of the function of which the number of calls (under |
| 193 | * --basic-counts=yes) is to be counted, with default. Override with command |
| 194 | * line option --fnname. */ |
| 195 | static Char* clo_fnname = "_dl_runtime_resolve"; |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 196 | |
| 197 | static Bool lk_process_cmd_line_option(Char* arg) |
| 198 | { |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame] | 199 | VG_STR_CLO(arg, "--fnname", clo_fnname) |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 200 | else VG_BOOL_CLO(arg, "--basic-counts", clo_basic_counts) |
| 201 | else VG_BOOL_CLO(arg, "--detailed-counts", clo_detailed_counts) |
| 202 | else VG_BOOL_CLO(arg, "--trace-mem", clo_trace_mem) |
| 203 | else VG_BOOL_CLO(arg, "--trace-superblocks", clo_trace_sbs) |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 204 | else |
| 205 | return False; |
| 206 | |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame] | 207 | tl_assert(clo_fnname); |
| 208 | tl_assert(clo_fnname[0]); |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 209 | return True; |
| 210 | } |
| 211 | |
| 212 | static void lk_print_usage(void) |
| 213 | { |
| 214 | VG_(printf)( |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame] | 215 | " --basic-counts=no|yes count instructions, jumps, etc. [no]\n" |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 216 | " --detailed-counts=no|yes count loads, stores and alu ops [no]\n" |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame] | 217 | " --trace-mem=no|yes trace all loads and stores [no]\n" |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 218 | " --trace-superblocks=no|yes trace all superblock entries [no]\n" |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame] | 219 | " --fnname=<name> count calls to <name> (only used if\n" |
| 220 | " --basic-count=yes) [_dl_runtime_resolve]\n" |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 221 | ); |
| 222 | } |
| 223 | |
| 224 | static void lk_print_debug_usage(void) |
| 225 | { |
njn | eaf0ca9 | 2006-04-09 01:23:29 +0000 | [diff] [blame] | 226 | VG_(printf)( |
| 227 | " (none)\n" |
| 228 | ); |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 229 | } |
| 230 | |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame] | 231 | /*------------------------------------------------------------*/ |
njn | eaf0ca9 | 2006-04-09 01:23:29 +0000 | [diff] [blame] | 232 | /*--- Stuff for --basic-counts ---*/ |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame] | 233 | /*------------------------------------------------------------*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 234 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 235 | /* Nb: use ULongs because the numbers can get very big */ |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 236 | static ULong n_func_calls = 0; |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 237 | static ULong n_SBs_entered = 0; |
| 238 | static ULong n_SBs_completed = 0; |
njn | eaf0ca9 | 2006-04-09 01:23:29 +0000 | [diff] [blame] | 239 | static ULong n_IRStmts = 0; |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 240 | static ULong n_guest_instrs = 0; |
| 241 | static ULong n_Jccs = 0; |
| 242 | static ULong n_Jccs_untaken = 0; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 243 | |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 244 | static void add_one_func_call(void) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 245 | { |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 246 | n_func_calls++; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 247 | } |
| 248 | |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 249 | static void add_one_SB_entered(void) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 250 | { |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 251 | n_SBs_entered++; |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 252 | } |
| 253 | |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 254 | static void add_one_SB_completed(void) |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 255 | { |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 256 | n_SBs_completed++; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 257 | } |
| 258 | |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 259 | static void add_one_IRStmt(void) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 260 | { |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 261 | n_IRStmts++; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 262 | } |
| 263 | |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 264 | static void add_one_guest_instr(void) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 265 | { |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 266 | n_guest_instrs++; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | static void add_one_Jcc(void) |
| 270 | { |
| 271 | n_Jccs++; |
| 272 | } |
| 273 | |
| 274 | static void add_one_Jcc_untaken(void) |
| 275 | { |
| 276 | n_Jccs_untaken++; |
| 277 | } |
| 278 | |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame] | 279 | /*------------------------------------------------------------*/ |
njn | eaf0ca9 | 2006-04-09 01:23:29 +0000 | [diff] [blame] | 280 | /*--- Stuff for --detailed-counts ---*/ |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame] | 281 | /*------------------------------------------------------------*/ |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 282 | |
| 283 | /* --- Operations --- */ |
| 284 | |
| 285 | typedef enum { OpLoad=0, OpStore=1, OpAlu=2 } Op; |
| 286 | |
| 287 | #define N_OPS 3 |
| 288 | |
| 289 | |
| 290 | /* --- Types --- */ |
| 291 | |
| 292 | #define N_TYPES 9 |
| 293 | |
| 294 | static Int type2index ( IRType ty ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 295 | { |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 296 | switch (ty) { |
| 297 | case Ity_I1: return 0; |
| 298 | case Ity_I8: return 1; |
| 299 | case Ity_I16: return 2; |
| 300 | case Ity_I32: return 3; |
| 301 | case Ity_I64: return 4; |
| 302 | case Ity_I128: return 5; |
| 303 | case Ity_F32: return 6; |
| 304 | case Ity_F64: return 7; |
| 305 | case Ity_V128: return 8; |
| 306 | default: tl_assert(0); break; |
| 307 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 308 | } |
| 309 | |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 310 | static HChar* nameOfTypeIndex ( IRType ty ) |
| 311 | { |
| 312 | switch (ty) { |
| 313 | case 0: return "I1"; break; |
| 314 | case 1: return "I8"; break; |
| 315 | case 2: return "I16"; break; |
| 316 | case 3: return "I32"; break; |
| 317 | case 4: return "I64"; break; |
| 318 | case 5: return "I128"; break; |
| 319 | case 6: return "F32"; break; |
| 320 | case 7: return "F64"; break; |
| 321 | case 8: return "V128"; break; |
| 322 | default: tl_assert(0); break; |
| 323 | } |
| 324 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 325 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 326 | |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 327 | /* --- Counts --- */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 328 | |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 329 | static ULong detailCounts[N_OPS][N_TYPES]; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 330 | |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 331 | /* The helper that is called from the instrumented code. */ |
| 332 | static VG_REGPARM(1) |
| 333 | void increment_detail(ULong* detail) |
| 334 | { |
| 335 | (*detail)++; |
| 336 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 337 | |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 338 | /* A helper that adds the instrumentation for a detail. */ |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 339 | static void instrument_detail(IRSB* sb, Op op, IRType type) |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 340 | { |
| 341 | IRDirty* di; |
| 342 | IRExpr** argv; |
| 343 | const UInt typeIx = type2index(type); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 344 | |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 345 | tl_assert(op < N_OPS); |
| 346 | tl_assert(typeIx < N_TYPES); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 347 | |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 348 | argv = mkIRExprVec_1( mkIRExpr_HWord( (HWord)&detailCounts[op][typeIx] ) ); |
sewardj | 5fed8c0 | 2005-12-23 12:56:11 +0000 | [diff] [blame] | 349 | di = unsafeIRDirty_0_N( 1, "increment_detail", |
| 350 | VG_(fnptr_to_fnentry)( &increment_detail ), |
| 351 | argv); |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 352 | addStmtToIRSB( sb, IRStmt_Dirty(di) ); |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 353 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 354 | |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 355 | /* Summarize and print the details. */ |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 356 | static void print_details ( void ) |
| 357 | { |
| 358 | Int typeIx; |
| 359 | VG_(message)(Vg_UserMsg, |
| 360 | " Type Loads Stores AluOps"); |
| 361 | VG_(message)(Vg_UserMsg, |
| 362 | " -------------------------------------------"); |
| 363 | for (typeIx = 0; typeIx < N_TYPES; typeIx++) { |
| 364 | VG_(message)(Vg_UserMsg, |
| 365 | " %4s %,12llu %,12llu %,12llu", |
| 366 | nameOfTypeIndex( typeIx ), |
| 367 | detailCounts[OpLoad ][typeIx], |
| 368 | detailCounts[OpStore][typeIx], |
| 369 | detailCounts[OpAlu ][typeIx] |
| 370 | ); |
| 371 | } |
| 372 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 373 | |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 374 | |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame] | 375 | /*------------------------------------------------------------*/ |
njn | eaf0ca9 | 2006-04-09 01:23:29 +0000 | [diff] [blame] | 376 | /*--- Stuff for --trace-mem ---*/ |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame] | 377 | /*------------------------------------------------------------*/ |
njn | fd73ebb | 2005-12-30 22:39:58 +0000 | [diff] [blame] | 378 | |
njn | eaf0ca9 | 2006-04-09 01:23:29 +0000 | [diff] [blame] | 379 | #define MAX_DSIZE 512 |
| 380 | |
| 381 | typedef |
| 382 | IRExpr |
| 383 | IRAtom; |
| 384 | |
| 385 | typedef |
| 386 | enum { Event_Ir, Event_Dr, Event_Dw, Event_Dm } |
| 387 | EventKind; |
| 388 | |
| 389 | typedef |
| 390 | struct { |
| 391 | EventKind ekind; |
| 392 | IRAtom* addr; |
| 393 | Int size; |
| 394 | } |
| 395 | Event; |
| 396 | |
| 397 | /* Up to this many unnotified events are allowed. Must be at least two, |
| 398 | so that reads and writes to the same address can be merged into a modify. |
| 399 | Beyond that, larger numbers just potentially induce more spilling due to |
| 400 | extending live ranges of address temporaries. */ |
| 401 | #define N_EVENTS 4 |
| 402 | |
| 403 | /* Maintain an ordered list of memory events which are outstanding, in |
| 404 | the sense that no IR has yet been generated to do the relevant |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 405 | helper calls. The SB is scanned top to bottom and memory events |
njn | eaf0ca9 | 2006-04-09 01:23:29 +0000 | [diff] [blame] | 406 | are added to the end of the list, merging with the most recent |
| 407 | notified event where possible (Dw immediately following Dr and |
| 408 | having the same size and EA can be merged). |
| 409 | |
| 410 | This merging is done so that for architectures which have |
| 411 | load-op-store instructions (x86, amd64), the instr is treated as if |
| 412 | it makes just one memory reference (a modify), rather than two (a |
| 413 | read followed by a write at the same address). |
| 414 | |
| 415 | At various points the list will need to be flushed, that is, IR |
| 416 | generated from it. That must happen before any possible exit from |
| 417 | the block (the end, or an IRStmt_Exit). Flushing also takes place |
| 418 | when there is no space to add a new event. |
| 419 | |
| 420 | If we require the simulation statistics to be up to date with |
| 421 | respect to possible memory exceptions, then the list would have to |
| 422 | be flushed before each memory reference. That's a pain so we don't |
| 423 | bother. |
| 424 | |
| 425 | Flushing the list consists of walking it start to end and emitting |
| 426 | instrumentation IR for each event, in the order in which they |
| 427 | appear. */ |
| 428 | |
| 429 | static Event events[N_EVENTS]; |
| 430 | static Int events_used = 0; |
| 431 | |
| 432 | |
| 433 | static VG_REGPARM(2) void trace_instr(Addr addr, SizeT size) |
| 434 | { |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 435 | VG_(printf)("I %08lx,%d\n", addr, size); |
njn | eaf0ca9 | 2006-04-09 01:23:29 +0000 | [diff] [blame] | 436 | } |
| 437 | |
njn | fd73ebb | 2005-12-30 22:39:58 +0000 | [diff] [blame] | 438 | static VG_REGPARM(2) void trace_load(Addr addr, SizeT size) |
| 439 | { |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 440 | VG_(printf)(" L %08lx,%d\n", addr, size); |
njn | fd73ebb | 2005-12-30 22:39:58 +0000 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | static VG_REGPARM(2) void trace_store(Addr addr, SizeT size) |
| 444 | { |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 445 | VG_(printf)(" S %08lx,%d\n", addr, size); |
njn | eaf0ca9 | 2006-04-09 01:23:29 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | static VG_REGPARM(2) void trace_modify(Addr addr, SizeT size) |
| 449 | { |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 450 | VG_(printf)(" M %08lx,%d\n", addr, size); |
njn | eaf0ca9 | 2006-04-09 01:23:29 +0000 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 454 | static void flushEvents(IRSB* sb) |
njn | eaf0ca9 | 2006-04-09 01:23:29 +0000 | [diff] [blame] | 455 | { |
| 456 | Int i; |
| 457 | Char* helperName; |
| 458 | void* helperAddr; |
| 459 | IRExpr** argv; |
| 460 | IRDirty* di; |
| 461 | Event* ev; |
| 462 | |
| 463 | for (i = 0; i < events_used; i++) { |
| 464 | |
| 465 | ev = &events[i]; |
| 466 | |
| 467 | // Decide on helper fn to call and args to pass it. |
| 468 | switch (ev->ekind) { |
| 469 | case Event_Ir: helperName = "trace_instr"; |
| 470 | helperAddr = trace_instr; break; |
| 471 | |
| 472 | case Event_Dr: helperName = "trace_load"; |
| 473 | helperAddr = trace_load; break; |
| 474 | |
| 475 | case Event_Dw: helperName = "trace_store"; |
| 476 | helperAddr = trace_store; break; |
| 477 | |
| 478 | case Event_Dm: helperName = "trace_modify"; |
| 479 | helperAddr = trace_modify; break; |
| 480 | default: |
| 481 | tl_assert(0); |
| 482 | } |
| 483 | |
| 484 | // Add the helper. |
| 485 | argv = mkIRExprVec_2( ev->addr, mkIRExpr_HWord( ev->size ) ); |
| 486 | di = unsafeIRDirty_0_N( /*regparms*/2, |
| 487 | helperName, VG_(fnptr_to_fnentry)( helperAddr ), |
| 488 | argv ); |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 489 | addStmtToIRSB( sb, IRStmt_Dirty(di) ); |
njn | eaf0ca9 | 2006-04-09 01:23:29 +0000 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | events_used = 0; |
| 493 | } |
| 494 | |
| 495 | // WARNING: If you aren't interested in instruction reads, you can omit the |
| 496 | // code that adds calls to trace_instr() in flushEvents(). However, you |
| 497 | // must still call this function, addEvent_Ir() -- it is necessary to add |
| 498 | // the Ir events to the events list so that merging of paired load/store |
| 499 | // events into modify events works correctly. |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 500 | static void addEvent_Ir ( IRSB* sb, IRAtom* iaddr, UInt isize ) |
njn | eaf0ca9 | 2006-04-09 01:23:29 +0000 | [diff] [blame] | 501 | { |
| 502 | Event* evt; |
| 503 | tl_assert( (VG_MIN_INSTR_SZB <= isize && isize <= VG_MAX_INSTR_SZB) |
| 504 | || VG_CLREQ_SZB == isize ); |
| 505 | if (events_used == N_EVENTS) |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 506 | flushEvents(sb); |
njn | eaf0ca9 | 2006-04-09 01:23:29 +0000 | [diff] [blame] | 507 | tl_assert(events_used >= 0 && events_used < N_EVENTS); |
| 508 | evt = &events[events_used]; |
| 509 | evt->ekind = Event_Ir; |
| 510 | evt->addr = iaddr; |
| 511 | evt->size = isize; |
| 512 | events_used++; |
| 513 | } |
| 514 | |
| 515 | static |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 516 | void addEvent_Dr ( IRSB* sb, IRAtom* daddr, Int dsize ) |
njn | eaf0ca9 | 2006-04-09 01:23:29 +0000 | [diff] [blame] | 517 | { |
| 518 | Event* evt; |
| 519 | tl_assert(isIRAtom(daddr)); |
| 520 | tl_assert(dsize >= 1 && dsize <= MAX_DSIZE); |
| 521 | if (events_used == N_EVENTS) |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 522 | flushEvents(sb); |
njn | eaf0ca9 | 2006-04-09 01:23:29 +0000 | [diff] [blame] | 523 | tl_assert(events_used >= 0 && events_used < N_EVENTS); |
| 524 | evt = &events[events_used]; |
| 525 | evt->ekind = Event_Dr; |
| 526 | evt->addr = daddr; |
| 527 | evt->size = dsize; |
| 528 | events_used++; |
| 529 | } |
| 530 | |
| 531 | static |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 532 | void addEvent_Dw ( IRSB* sb, IRAtom* daddr, Int dsize ) |
njn | eaf0ca9 | 2006-04-09 01:23:29 +0000 | [diff] [blame] | 533 | { |
| 534 | Event* lastEvt; |
| 535 | Event* evt; |
| 536 | tl_assert(isIRAtom(daddr)); |
| 537 | tl_assert(dsize >= 1 && dsize <= MAX_DSIZE); |
| 538 | |
| 539 | // Is it possible to merge this write with the preceding read? |
| 540 | lastEvt = &events[events_used-1]; |
| 541 | if (events_used > 0 |
| 542 | && lastEvt->ekind == Event_Dr |
| 543 | && lastEvt->size == dsize |
| 544 | && eqIRAtom(lastEvt->addr, daddr)) |
| 545 | { |
| 546 | lastEvt->ekind = Event_Dm; |
| 547 | return; |
| 548 | } |
| 549 | |
| 550 | // No. Add as normal. |
| 551 | if (events_used == N_EVENTS) |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 552 | flushEvents(sb); |
njn | eaf0ca9 | 2006-04-09 01:23:29 +0000 | [diff] [blame] | 553 | tl_assert(events_used >= 0 && events_used < N_EVENTS); |
| 554 | evt = &events[events_used]; |
| 555 | evt->ekind = Event_Dw; |
| 556 | evt->size = dsize; |
| 557 | evt->addr = daddr; |
| 558 | events_used++; |
njn | fd73ebb | 2005-12-30 22:39:58 +0000 | [diff] [blame] | 559 | } |
| 560 | |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame] | 561 | |
| 562 | /*------------------------------------------------------------*/ |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 563 | /*--- Stuff for --trace-superblocks ---*/ |
| 564 | /*------------------------------------------------------------*/ |
| 565 | |
| 566 | static void trace_superblock(Addr addr) |
| 567 | { |
| 568 | VG_(printf)("SB %08lx\n", addr); |
| 569 | } |
| 570 | |
| 571 | |
| 572 | /*------------------------------------------------------------*/ |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame] | 573 | /*--- Basic tool functions ---*/ |
| 574 | /*------------------------------------------------------------*/ |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 575 | |
| 576 | static void lk_post_clo_init(void) |
| 577 | { |
| 578 | Int op, tyIx; |
| 579 | |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame] | 580 | if (clo_detailed_counts) { |
| 581 | for (op = 0; op < N_OPS; op++) |
| 582 | for (tyIx = 0; tyIx < N_TYPES; tyIx++) |
| 583 | detailCounts[op][tyIx] = 0; |
| 584 | } |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 585 | } |
| 586 | |
sewardj | 4ba057c | 2005-10-18 12:04:18 +0000 | [diff] [blame] | 587 | static |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 588 | IRSB* lk_instrument ( VgCallbackClosure* closure, |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 589 | IRSB* sbIn, |
sewardj | 461df9c | 2006-01-17 02:06:39 +0000 | [diff] [blame] | 590 | VexGuestLayout* layout, |
| 591 | VexGuestExtents* vge, |
| 592 | IRType gWordTy, IRType hWordTy ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 593 | { |
njn | eaf0ca9 | 2006-04-09 01:23:29 +0000 | [diff] [blame] | 594 | IRDirty* di; |
| 595 | Int i; |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 596 | IRSB* sbOut; |
njn | eaf0ca9 | 2006-04-09 01:23:29 +0000 | [diff] [blame] | 597 | Char fnname[100]; |
| 598 | IRType type; |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 599 | IRTypeEnv* tyenv = sbIn->tyenv; |
sewardj | d54babf | 2005-03-21 00:55:49 +0000 | [diff] [blame] | 600 | |
| 601 | if (gWordTy != hWordTy) { |
| 602 | /* We don't currently support this case. */ |
| 603 | VG_(tool_panic)("host/guest word size mismatch"); |
| 604 | } |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 605 | |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 606 | /* Set up SB */ |
| 607 | sbOut = deepCopyIRSBExceptStmts(sbIn); |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 608 | |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 609 | // Copy verbatim any IR preamble preceding the first IMark |
| 610 | i = 0; |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 611 | while (i < sbIn->stmts_used && sbIn->stmts[i]->tag != Ist_IMark) { |
| 612 | addStmtToIRSB( sbOut, sbIn->stmts[i] ); |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 613 | i++; |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 614 | } |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 615 | |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame] | 616 | if (clo_basic_counts) { |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 617 | /* Count this superblock. */ |
| 618 | di = unsafeIRDirty_0_N( 0, "add_one_SB_entered", |
| 619 | VG_(fnptr_to_fnentry)( &add_one_SB_entered ), |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame] | 620 | mkIRExprVec_0() ); |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 621 | addStmtToIRSB( sbOut, IRStmt_Dirty(di) ); |
| 622 | } |
| 623 | |
| 624 | if (clo_trace_sbs) { |
| 625 | /* Print this superblock's address. */ |
| 626 | di = unsafeIRDirty_0_N( |
| 627 | 0, "trace_superblock", |
| 628 | VG_(fnptr_to_fnentry)( &trace_superblock ), |
| 629 | mkIRExprVec_1( mkIRExpr_HWord( vge->base[0] ) ) |
| 630 | ); |
| 631 | addStmtToIRSB( sbOut, IRStmt_Dirty(di) ); |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame] | 632 | } |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 633 | |
njn | eaf0ca9 | 2006-04-09 01:23:29 +0000 | [diff] [blame] | 634 | if (clo_trace_mem) { |
| 635 | events_used = 0; |
| 636 | } |
| 637 | |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 638 | for (/*use current i*/; i < sbIn->stmts_used; i++) { |
| 639 | IRStmt* st = sbIn->stmts[i]; |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 640 | if (!st || st->tag == Ist_NoOp) continue; |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 641 | |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame] | 642 | if (clo_basic_counts) { |
| 643 | /* Count one VEX statement. */ |
| 644 | di = unsafeIRDirty_0_N( 0, "add_one_IRStmt", |
| 645 | VG_(fnptr_to_fnentry)( &add_one_IRStmt ), |
| 646 | mkIRExprVec_0() ); |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 647 | addStmtToIRSB( sbOut, IRStmt_Dirty(di) ); |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame] | 648 | } |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 649 | |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 650 | switch (st->tag) { |
njn | eaf0ca9 | 2006-04-09 01:23:29 +0000 | [diff] [blame] | 651 | case Ist_NoOp: |
| 652 | case Ist_AbiHint: |
| 653 | case Ist_Put: |
| 654 | case Ist_PutI: |
| 655 | case Ist_MFence: |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 656 | addStmtToIRSB( sbOut, st ); |
njn | eaf0ca9 | 2006-04-09 01:23:29 +0000 | [diff] [blame] | 657 | break; |
| 658 | |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 659 | case Ist_IMark: |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame] | 660 | if (clo_basic_counts) { |
| 661 | /* Count guest instruction. */ |
| 662 | di = unsafeIRDirty_0_N( 0, "add_one_guest_instr", |
| 663 | VG_(fnptr_to_fnentry)( &add_one_guest_instr ), |
| 664 | mkIRExprVec_0() ); |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 665 | addStmtToIRSB( sbOut, IRStmt_Dirty(di) ); |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame] | 666 | |
| 667 | /* An unconditional branch to a known destination in the |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 668 | * guest's instructions can be represented, in the IRSB to |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame] | 669 | * instrument, by the VEX statements that are the |
| 670 | * translation of that known destination. This feature is |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 671 | * called 'SB chasing' and can be influenced by command |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame] | 672 | * line option --vex-guest-chase-thresh. |
| 673 | * |
| 674 | * To get an accurate count of the calls to a specific |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 675 | * function, taking SB chasing into account, we need to |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame] | 676 | * check for each guest instruction (Ist_IMark) if it is |
| 677 | * the entry point of a function. |
| 678 | */ |
| 679 | tl_assert(clo_fnname); |
| 680 | tl_assert(clo_fnname[0]); |
| 681 | if (VG_(get_fnname_if_entry)(st->Ist.IMark.addr, |
| 682 | fnname, sizeof(fnname)) |
| 683 | && 0 == VG_(strcmp)(fnname, clo_fnname)) { |
| 684 | di = unsafeIRDirty_0_N( |
| 685 | 0, "add_one_func_call", |
| 686 | VG_(fnptr_to_fnentry)( &add_one_func_call ), |
| 687 | mkIRExprVec_0() ); |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 688 | addStmtToIRSB( sbOut, IRStmt_Dirty(di) ); |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame] | 689 | } |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 690 | } |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame] | 691 | if (clo_trace_mem) { |
njn | eaf0ca9 | 2006-04-09 01:23:29 +0000 | [diff] [blame] | 692 | // WARNING: do not remove this function call, even if you |
| 693 | // aren't interested in instruction reads. See the comment |
| 694 | // above the function itself for more detail. |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 695 | addEvent_Ir( sbOut, mkIRExpr_HWord( (HWord)st->Ist.IMark.addr ), |
njn | eaf0ca9 | 2006-04-09 01:23:29 +0000 | [diff] [blame] | 696 | st->Ist.IMark.len ); |
njn | fd73ebb | 2005-12-30 22:39:58 +0000 | [diff] [blame] | 697 | } |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 698 | addStmtToIRSB( sbOut, st ); |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 699 | break; |
| 700 | |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 701 | case Ist_WrTmp: |
njn | fd73ebb | 2005-12-30 22:39:58 +0000 | [diff] [blame] | 702 | // Add a call to trace_load() if --trace-mem=yes. |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame] | 703 | if (clo_trace_mem) { |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 704 | IRExpr* data = st->Ist.WrTmp.data; |
njn | fd73ebb | 2005-12-30 22:39:58 +0000 | [diff] [blame] | 705 | if (data->tag == Iex_Load) { |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 706 | addEvent_Dr( sbOut, data->Iex.Load.addr, |
njn | eaf0ca9 | 2006-04-09 01:23:29 +0000 | [diff] [blame] | 707 | sizeofIRType(data->Iex.Load.ty) ); |
njn | fd73ebb | 2005-12-30 22:39:58 +0000 | [diff] [blame] | 708 | } |
| 709 | } |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame] | 710 | if (clo_detailed_counts) { |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 711 | IRExpr* expr = st->Ist.WrTmp.data; |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 712 | type = typeOfIRExpr(sbOut->tyenv, expr); |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 713 | tl_assert(type != Ity_INVALID); |
| 714 | switch (expr->tag) { |
| 715 | case Iex_Load: |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 716 | instrument_detail( sbOut, OpLoad, type ); |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 717 | break; |
| 718 | case Iex_Unop: |
| 719 | case Iex_Binop: |
sewardj | e91cea7 | 2006-02-08 19:32:02 +0000 | [diff] [blame] | 720 | case Iex_Triop: |
| 721 | case Iex_Qop: |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 722 | case Iex_Mux0X: |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 723 | instrument_detail( sbOut, OpAlu, type ); |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 724 | break; |
| 725 | default: |
| 726 | break; |
| 727 | } |
| 728 | } |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 729 | addStmtToIRSB( sbOut, st ); |
njn | eaf0ca9 | 2006-04-09 01:23:29 +0000 | [diff] [blame] | 730 | break; |
| 731 | |
| 732 | case Ist_Store: |
| 733 | if (clo_trace_mem) { |
| 734 | IRExpr* data = st->Ist.Store.data; |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 735 | addEvent_Dw( sbOut, st->Ist.Store.addr, |
njn | eaf0ca9 | 2006-04-09 01:23:29 +0000 | [diff] [blame] | 736 | sizeofIRType(typeOfIRExpr(tyenv, data)) ); |
| 737 | } |
| 738 | if (clo_detailed_counts) { |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 739 | type = typeOfIRExpr(sbOut->tyenv, st->Ist.Store.data); |
njn | eaf0ca9 | 2006-04-09 01:23:29 +0000 | [diff] [blame] | 740 | tl_assert(type != Ity_INVALID); |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 741 | instrument_detail( sbOut, OpStore, type ); |
njn | eaf0ca9 | 2006-04-09 01:23:29 +0000 | [diff] [blame] | 742 | } |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 743 | addStmtToIRSB( sbOut, st ); |
njn | eaf0ca9 | 2006-04-09 01:23:29 +0000 | [diff] [blame] | 744 | break; |
| 745 | |
| 746 | case Ist_Dirty: { |
| 747 | Int dsize; |
| 748 | IRDirty* d = st->Ist.Dirty.details; |
| 749 | if (d->mFx != Ifx_None) { |
| 750 | // This dirty helper accesses memory. Collect the details. |
| 751 | tl_assert(d->mAddr != NULL); |
| 752 | tl_assert(d->mSize != 0); |
| 753 | dsize = d->mSize; |
| 754 | if (d->mFx == Ifx_Read || d->mFx == Ifx_Modify) |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 755 | addEvent_Dr( sbOut, d->mAddr, dsize ); |
njn | eaf0ca9 | 2006-04-09 01:23:29 +0000 | [diff] [blame] | 756 | if (d->mFx == Ifx_Write || d->mFx == Ifx_Modify) |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 757 | addEvent_Dw( sbOut, d->mAddr, dsize ); |
njn | eaf0ca9 | 2006-04-09 01:23:29 +0000 | [diff] [blame] | 758 | } else { |
| 759 | tl_assert(d->mAddr == NULL); |
| 760 | tl_assert(d->mSize == 0); |
| 761 | } |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 762 | addStmtToIRSB( sbOut, st ); |
njn | eaf0ca9 | 2006-04-09 01:23:29 +0000 | [diff] [blame] | 763 | break; |
| 764 | } |
| 765 | |
| 766 | case Ist_Exit: |
| 767 | if (clo_basic_counts) { |
| 768 | /* Count Jcc */ |
| 769 | di = unsafeIRDirty_0_N( 0, "add_one_Jcc", |
| 770 | VG_(fnptr_to_fnentry)( &add_one_Jcc ), |
| 771 | mkIRExprVec_0() ); |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 772 | addStmtToIRSB( sbOut, IRStmt_Dirty(di) ); |
njn | eaf0ca9 | 2006-04-09 01:23:29 +0000 | [diff] [blame] | 773 | } |
| 774 | if (clo_trace_mem) { |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 775 | flushEvents(sbOut); |
njn | eaf0ca9 | 2006-04-09 01:23:29 +0000 | [diff] [blame] | 776 | } |
| 777 | |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 778 | addStmtToIRSB( sbOut, st ); // Original statement |
njn | eaf0ca9 | 2006-04-09 01:23:29 +0000 | [diff] [blame] | 779 | |
| 780 | if (clo_basic_counts) { |
| 781 | /* Count non-taken Jcc */ |
| 782 | di = unsafeIRDirty_0_N( 0, "add_one_Jcc_untaken", |
| 783 | VG_(fnptr_to_fnentry)( |
| 784 | &add_one_Jcc_untaken ), |
| 785 | mkIRExprVec_0() ); |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 786 | addStmtToIRSB( sbOut, IRStmt_Dirty(di) ); |
njn | eaf0ca9 | 2006-04-09 01:23:29 +0000 | [diff] [blame] | 787 | } |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 788 | break; |
| 789 | |
| 790 | default: |
njn | eaf0ca9 | 2006-04-09 01:23:29 +0000 | [diff] [blame] | 791 | tl_assert(0); |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 792 | } |
| 793 | } |
| 794 | |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame] | 795 | if (clo_basic_counts) { |
| 796 | /* Count this basic block. */ |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 797 | di = unsafeIRDirty_0_N( 0, "add_one_SB_completed", |
| 798 | VG_(fnptr_to_fnentry)( &add_one_SB_completed ), |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame] | 799 | mkIRExprVec_0() ); |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 800 | addStmtToIRSB( sbOut, IRStmt_Dirty(di) ); |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame] | 801 | } |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 802 | |
njn | eaf0ca9 | 2006-04-09 01:23:29 +0000 | [diff] [blame] | 803 | if (clo_trace_mem) { |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 804 | /* At the end of the sbIn. Flush outstandings. */ |
| 805 | flushEvents(sbOut); |
njn | eaf0ca9 | 2006-04-09 01:23:29 +0000 | [diff] [blame] | 806 | } |
| 807 | |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 808 | return sbOut; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 809 | } |
| 810 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 811 | static void lk_fini(Int exitcode) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 812 | { |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 813 | char percentify_buf[4]; /* Two digits, '%' and 0. */ |
| 814 | const int percentify_size = sizeof(percentify_buf); |
| 815 | const int percentify_decs = 0; |
| 816 | |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame] | 817 | tl_assert(clo_fnname); |
| 818 | tl_assert(clo_fnname[0]); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 819 | |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame] | 820 | if (clo_basic_counts) { |
| 821 | VG_(message)(Vg_UserMsg, |
| 822 | "Counted %,llu calls to %s()", n_func_calls, clo_fnname); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 823 | |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame] | 824 | VG_(message)(Vg_UserMsg, ""); |
| 825 | VG_(message)(Vg_UserMsg, "Jccs:"); |
| 826 | VG_(message)(Vg_UserMsg, " total: %,llu", n_Jccs); |
| 827 | VG_(percentify)((n_Jccs - n_Jccs_untaken), (n_Jccs ? n_Jccs : 1), |
| 828 | percentify_decs, percentify_size, percentify_buf); |
| 829 | VG_(message)(Vg_UserMsg, " taken: %,llu (%s)", |
| 830 | (n_Jccs - n_Jccs_untaken), percentify_buf); |
| 831 | |
| 832 | VG_(message)(Vg_UserMsg, ""); |
| 833 | VG_(message)(Vg_UserMsg, "Executed:"); |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 834 | VG_(message)(Vg_UserMsg, " SBs entered: %,llu", n_SBs_entered); |
| 835 | VG_(message)(Vg_UserMsg, " SBs completed: %,llu", n_SBs_completed); |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame] | 836 | VG_(message)(Vg_UserMsg, " guest instrs: %,llu", n_guest_instrs); |
| 837 | VG_(message)(Vg_UserMsg, " IRStmts: %,llu", n_IRStmts); |
| 838 | |
| 839 | VG_(message)(Vg_UserMsg, ""); |
| 840 | VG_(message)(Vg_UserMsg, "Ratios:"); |
sewardj | 5d1c901 | 2007-02-12 08:42:13 +0000 | [diff] [blame^] | 841 | tl_assert(n_SBs_entered); // Paranoia time. |
| 842 | VG_(message)(Vg_UserMsg, " guest instrs : SB entered = %3u : 10", |
| 843 | 10 * n_guest_instrs / n_SBs_entered); |
| 844 | VG_(message)(Vg_UserMsg, " IRStmts : SB entered = %3u : 10", |
| 845 | 10 * n_IRStmts / n_SBs_entered); |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame] | 846 | tl_assert(n_guest_instrs); // Paranoia time. |
| 847 | VG_(message)(Vg_UserMsg, " IRStmts : guest instr = %3u : 10", |
| 848 | 10 * n_IRStmts / n_guest_instrs); |
| 849 | } |
| 850 | |
| 851 | if (clo_detailed_counts) { |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 852 | VG_(message)(Vg_UserMsg, ""); |
| 853 | VG_(message)(Vg_UserMsg, "IR-level counts by type:"); |
| 854 | print_details(); |
| 855 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 856 | |
njn | d99644d | 2006-04-07 11:52:55 +0000 | [diff] [blame] | 857 | if (clo_basic_counts) { |
| 858 | VG_(message)(Vg_UserMsg, ""); |
| 859 | VG_(message)(Vg_UserMsg, "Exit code: %d", exitcode); |
| 860 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 861 | } |
| 862 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 863 | static void lk_pre_clo_init(void) |
| 864 | { |
| 865 | VG_(details_name) ("Lackey"); |
| 866 | VG_(details_version) (NULL); |
| 867 | VG_(details_description) ("an example Valgrind tool"); |
| 868 | VG_(details_copyright_author)( |
sewardj | 9ebd6e0 | 2007-01-08 06:01:59 +0000 | [diff] [blame] | 869 | "Copyright (C) 2002-2007, and GNU GPL'd, by Nicholas Nethercote."); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 870 | VG_(details_bug_reports_to) (VG_BUGS_TO); |
sewardj | 4082356 | 2006-10-17 02:21:17 +0000 | [diff] [blame] | 871 | VG_(details_avg_translation_sizeB) ( 200 ); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 872 | |
| 873 | VG_(basic_tool_funcs) (lk_post_clo_init, |
| 874 | lk_instrument, |
| 875 | lk_fini); |
sewardj | 7a26f02 | 2005-11-01 17:52:34 +0000 | [diff] [blame] | 876 | VG_(needs_command_line_options)(lk_process_cmd_line_option, |
| 877 | lk_print_usage, |
| 878 | lk_print_debug_usage); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 879 | } |
| 880 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 881 | VG_DETERMINE_INTERFACE_VERSION(lk_pre_clo_init) |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 882 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 883 | /*--------------------------------------------------------------------*/ |
njn25 | cac76cb | 2002-09-23 11:21:57 +0000 | [diff] [blame] | 884 | /*--- end lk_main.c ---*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 885 | /*--------------------------------------------------------------------*/ |