njn | c953984 | 2002-10-02 13:26:35 +0000 | [diff] [blame] | 1 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2 | /*--------------------------------------------------------------------*/ |
nethercote | 137bc55 | 2003-11-14 17:47:54 +0000 | [diff] [blame] | 3 | /*--- Simple tool for counting UInstrs, using a C helper. ---*/ |
njn25 | cac76cb | 2002-09-23 11:21:57 +0000 | [diff] [blame] | 4 | /*--- lk_main.c ---*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 5 | /*--------------------------------------------------------------------*/ |
| 6 | |
| 7 | /* |
nethercote | 137bc55 | 2003-11-14 17:47:54 +0000 | [diff] [blame] | 8 | This file is part of Lackey, an example Valgrind tool that does |
njn | c953984 | 2002-10-02 13:26:35 +0000 | [diff] [blame] | 9 | some simple program measurement. |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 10 | |
njn | 5361242 | 2005-03-12 16:22:54 +0000 | [diff] [blame] | 11 | Copyright (C) 2002-2005 Nicholas Nethercote |
njn | 2bc1012 | 2005-05-08 02:10:27 +0000 | [diff] [blame] | 12 | njn@valgrind.org |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 13 | |
| 14 | This program is free software; you can redistribute it and/or |
| 15 | modify it under the terms of the GNU General Public License as |
| 16 | published by the Free Software Foundation; either version 2 of the |
| 17 | License, or (at your option) any later version. |
| 18 | |
| 19 | This program is distributed in the hope that it will be useful, but |
| 20 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 22 | General Public License for more details. |
| 23 | |
| 24 | You should have received a copy of the GNU General Public License |
| 25 | along with this program; if not, write to the Free Software |
| 26 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 27 | 02111-1307, USA. |
| 28 | |
| 29 | The GNU General Public License is contained in the file COPYING. |
| 30 | */ |
| 31 | |
nethercote | 4606320 | 2004-09-02 08:51:43 +0000 | [diff] [blame] | 32 | #include "tool.h" |
njn | 43b9a8a | 2005-05-10 04:37:01 +0000 | [diff] [blame^] | 33 | #include "pub_tool_tooliface.h" |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 34 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 35 | /* Nb: use ULongs because the numbers can get very big */ |
| 36 | static ULong n_dlrr_calls = 0; |
| 37 | static ULong n_BBs = 0; |
| 38 | static ULong n_UInstrs = 0; |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 39 | static ULong n_guest_instrs = 0; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 40 | static ULong n_Jccs = 0; |
| 41 | static ULong n_Jccs_untaken = 0; |
| 42 | |
| 43 | static void add_one_dlrr_call(void) |
| 44 | { |
| 45 | n_dlrr_calls++; |
| 46 | } |
| 47 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 48 | /* See comment above lk_instrument for reason why n_machine_instrs is |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 49 | incremented here. */ |
| 50 | static void add_one_BB(void) |
| 51 | { |
| 52 | n_BBs++; |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 53 | n_guest_instrs++; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | static void add_one_UInstr(void) |
| 57 | { |
| 58 | n_UInstrs++; |
| 59 | } |
| 60 | |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 61 | static void add_one_guest_instr(void) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 62 | { |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 63 | n_guest_instrs++; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | static void add_one_Jcc(void) |
| 67 | { |
| 68 | n_Jccs++; |
| 69 | } |
| 70 | |
| 71 | static void add_one_Jcc_untaken(void) |
| 72 | { |
| 73 | n_Jccs_untaken++; |
| 74 | } |
| 75 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 76 | static void lk_post_clo_init(void) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 77 | { |
| 78 | } |
| 79 | |
nethercote | 7e08f06 | 2004-10-20 10:37:00 +0000 | [diff] [blame] | 80 | /* Note: machine instructions are marked by an INCEIP at the end of each one, |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 81 | except for the final one in the basic block which ends in an |
| 82 | unconditional JMP. Sometimes the final unconditional JMP is preceded by |
| 83 | a conditional JMP (Jcc), and thus it isn't reached. Eg: |
| 84 | |
| 85 | <code a> |
| 86 | INCEIP ... |
| 87 | |
| 88 | <code b> |
| 89 | Jcc ... |
| 90 | JMP ... (will not be reached if Jcc succeeds) |
| 91 | |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 92 | If we simplemindedly added calls to add_one_guest_instr() before INCEIPs |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 93 | and unconditional JMPs, we'd sometimes miss the final call (when a |
nethercote | 7e08f06 | 2004-10-20 10:37:00 +0000 | [diff] [blame] | 94 | preceding conditional JMP succeeds), underestimating the machine instruction |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 95 | count. |
| 96 | |
| 97 | <code a> |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 98 | call add_one_guest_instr() |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 99 | INCEIP ... |
| 100 | |
| 101 | <code b> |
| 102 | Jcc ... |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 103 | call add_one_guest_instr() |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 104 | JMP ... |
| 105 | |
| 106 | Instead we add a call before each INCEIP, and also one at the start of the |
| 107 | block, but not one at the end, viz: |
| 108 | |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 109 | call add_one_guest_instr() |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 110 | |
| 111 | <code a> |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 112 | call add_one_guest_instr() |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 113 | INCEIP ... |
| 114 | |
| 115 | <code b> |
| 116 | Jcc ... |
| 117 | JMP ... |
| 118 | |
| 119 | Which gives us the right answer. And just to avoid two C calls, we fold |
| 120 | the basic-block-beginning call in with add_one_BB(). Phew. |
| 121 | */ |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 122 | static IRBB* lk_instrument(IRBB* bb_in, VexGuestLayout* layout, |
| 123 | IRType gWordTy, IRType hWordTy ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 124 | { |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 125 | IRDirty* di; |
| 126 | Int i; |
sewardj | d54babf | 2005-03-21 00:55:49 +0000 | [diff] [blame] | 127 | IRBB* bb; |
| 128 | |
| 129 | if (gWordTy != hWordTy) { |
| 130 | /* We don't currently support this case. */ |
| 131 | VG_(tool_panic)("host/guest word size mismatch"); |
| 132 | } |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 133 | |
| 134 | /* Set up BB */ |
sewardj | d54babf | 2005-03-21 00:55:49 +0000 | [diff] [blame] | 135 | bb = emptyIRBB(); |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 136 | bb->tyenv = dopyIRTypeEnv(bb_in->tyenv); |
| 137 | bb->next = dopyIRExpr(bb_in->next); |
| 138 | bb->jumpkind = bb_in->jumpkind; |
| 139 | |
| 140 | #if 0 |
| 141 | /* We need to know the entry point for this bb to do this. In any |
| 142 | case it's pretty meaningless in the presence of bb chasing since |
| 143 | we may enter this function part way through an IRBB. */ |
| 144 | /* Count call to dlrr(), if this BB is dlrr()'s entry point */ |
| 145 | if (VG_(get_fnname_if_entry)(orig_addr, fnname, 100) && |
| 146 | 0 == VG_(strcmp)(fnname, "_dl_runtime_resolve")) |
| 147 | { |
| 148 | addStmtToIRBB( |
| 149 | bb, |
| 150 | IRStmt_Dirty( |
| 151 | unsafeIRDirty_0_N( "add_one_dlrr_call", mkIRExprVec_0() ) |
| 152 | )); |
| 153 | } |
| 154 | #endif |
| 155 | |
| 156 | /* Count this basic block */ |
| 157 | di = unsafeIRDirty_0_N( 0, "add_one_BB", &add_one_BB, mkIRExprVec_0() ); |
| 158 | addStmtToIRBB( bb, IRStmt_Dirty(di) ); |
| 159 | |
| 160 | for (i = 0; i < bb_in->stmts_used; i++) { |
| 161 | IRStmt* st = bb_in->stmts[i]; |
| 162 | if (!st) continue; |
| 163 | |
| 164 | switch (st->tag) { |
| 165 | case Ist_Exit: |
| 166 | /* Count Jcc */ |
| 167 | addStmtToIRBB( |
| 168 | bb, |
| 169 | IRStmt_Dirty( |
| 170 | unsafeIRDirty_0_N( 0, "add_one_Jcc", &add_one_Jcc, |
| 171 | mkIRExprVec_0() ) |
| 172 | )); |
| 173 | addStmtToIRBB( bb, dopyIRStmt(st) ); |
| 174 | /* Count non-taken Jcc */ |
| 175 | addStmtToIRBB( |
| 176 | bb, |
| 177 | IRStmt_Dirty( |
| 178 | unsafeIRDirty_0_N( 0, "add_one_Jcc_untaken", &add_one_Jcc_untaken, |
| 179 | mkIRExprVec_0() ) |
| 180 | )); |
| 181 | break; |
| 182 | |
| 183 | default: |
sewardj | bd598e1 | 2005-01-07 12:10:21 +0000 | [diff] [blame] | 184 | addStmtToIRBB( bb, st ); |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | |
| 188 | return bb; |
| 189 | |
| 190 | |
| 191 | #if 0 |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 192 | UCodeBlock* cb; |
| 193 | Int i; |
| 194 | UInstr* u; |
| 195 | Char fnname[100]; |
| 196 | |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 197 | cb = VG_(setup_UCodeBlock)(cb_in); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 198 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 199 | /* Count basic block */ |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 200 | VG_(call_helper_0_0)(cb, (Addr) & add_one_BB); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 201 | |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 202 | for (i = 0; i < VG_(get_num_instrs)(cb_in); i++) { |
| 203 | u = VG_(get_instr)(cb_in, i); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 204 | |
| 205 | switch (u->opcode) { |
sewardj | 7a5ebcf | 2002-11-13 22:42:13 +0000 | [diff] [blame] | 206 | case NOP: case LOCK: case CALLM_S: case CALLM_E: |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 207 | break; |
| 208 | |
| 209 | case INCEIP: |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 210 | /* Count x86 instr */ |
| 211 | VG_(call_helper_0_0)(cb, (Addr) & add_one_x86_instr); |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 212 | VG_(copy_UInstr)(cb, u); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 213 | break; |
| 214 | |
| 215 | case JMP: |
| 216 | if (u->cond != CondAlways) { |
| 217 | /* Count Jcc */ |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 218 | VG_(call_helper_0_0)(cb, (Addr) & add_one_Jcc); |
| 219 | VG_(copy_UInstr)(cb, u); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 220 | /* Count non-taken Jcc */ |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 221 | VG_(call_helper_0_0)(cb, (Addr) & add_one_Jcc_untaken); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 222 | } else { |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 223 | VG_(copy_UInstr)(cb, u); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 224 | } |
| 225 | break; |
sewardj | 3d7c9c8 | 2003-03-26 21:08:13 +0000 | [diff] [blame] | 226 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 227 | default: |
| 228 | /* Count UInstr */ |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 229 | VG_(call_helper_0_0)(cb, (Addr) & add_one_UInstr); |
| 230 | VG_(copy_UInstr)(cb, u); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 231 | break; |
| 232 | } |
| 233 | } |
| 234 | |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 235 | VG_(free_UCodeBlock)(cb_in); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 236 | return cb; |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 237 | #endif |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 238 | } |
| 239 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 240 | static void lk_fini(Int exitcode) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 241 | { |
| 242 | VG_(message)(Vg_UserMsg, |
| 243 | "Counted %d calls to _dl_runtime_resolve()", n_dlrr_calls); |
| 244 | |
| 245 | VG_(message)(Vg_UserMsg, ""); |
| 246 | VG_(message)(Vg_UserMsg, "Executed:"); |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 247 | VG_(message)(Vg_UserMsg, " BBs: %u", n_BBs); |
| 248 | VG_(message)(Vg_UserMsg, " guest instrs: %u", n_guest_instrs); |
| 249 | VG_(message)(Vg_UserMsg, " UInstrs: %u", n_UInstrs); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 250 | |
| 251 | VG_(message)(Vg_UserMsg, ""); |
| 252 | VG_(message)(Vg_UserMsg, "Jccs:"); |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 253 | VG_(message)(Vg_UserMsg, " total: %u", n_Jccs); |
| 254 | VG_(message)(Vg_UserMsg, " %% taken: %u%%", |
| 255 | (n_Jccs - n_Jccs_untaken)*100 / |
| 256 | (n_Jccs ? n_Jccs : 1)); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 257 | |
| 258 | VG_(message)(Vg_UserMsg, ""); |
| 259 | VG_(message)(Vg_UserMsg, "Ratios:"); |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 260 | VG_(message)(Vg_UserMsg, " guest instrs : BB = %3u : 10", |
| 261 | 10 * n_guest_instrs / n_BBs); |
| 262 | VG_(message)(Vg_UserMsg, " UInstrs : BB = %3u : 10", |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 263 | 10 * n_UInstrs / n_BBs); |
sewardj | 9f649aa | 2004-11-22 20:38:40 +0000 | [diff] [blame] | 264 | VG_(message)(Vg_UserMsg, " UInstrs : x86_instr = %3u : 10", |
| 265 | 10 * n_UInstrs / n_guest_instrs); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 266 | |
njn | 7d9f94d | 2003-04-22 21:41:40 +0000 | [diff] [blame] | 267 | VG_(message)(Vg_UserMsg, ""); |
| 268 | VG_(message)(Vg_UserMsg, "Exit code: %d", exitcode); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 269 | } |
| 270 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 271 | static void lk_pre_clo_init(void) |
| 272 | { |
| 273 | VG_(details_name) ("Lackey"); |
| 274 | VG_(details_version) (NULL); |
| 275 | VG_(details_description) ("an example Valgrind tool"); |
| 276 | VG_(details_copyright_author)( |
| 277 | "Copyright (C) 2002-2005, and GNU GPL'd, by Nicholas Nethercote."); |
| 278 | VG_(details_bug_reports_to) (VG_BUGS_TO); |
| 279 | VG_(details_avg_translation_sizeB) ( 175 ); |
| 280 | |
| 281 | VG_(basic_tool_funcs) (lk_post_clo_init, |
| 282 | lk_instrument, |
| 283 | lk_fini); |
| 284 | } |
| 285 | |
| 286 | VG_DETERMINE_INTERFACE_VERSION(lk_pre_clo_init, 0) |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 287 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 288 | /*--------------------------------------------------------------------*/ |
njn25 | cac76cb | 2002-09-23 11:21:57 +0000 | [diff] [blame] | 289 | /*--- end lk_main.c ---*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 290 | /*--------------------------------------------------------------------*/ |
| 291 | |