OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (c) 2011 The Native Client Authors. All rights reserved. | |
3 * Use of this source code is governed by a BSD-style license that can be | |
4 * found in the LICENSE file. | |
5 */ | |
6 | |
7 /* | |
8 * nc_opcode_histogram.c - Collects histogram information while validating. | |
9 */ | |
10 | |
11 #include <stdio.h> | |
12 | |
13 #include "native_client/src/trusted/validator/x86/ncval_reg_sfi/nc_opcode_histog
ram.h" | |
14 | |
15 #include "native_client/src/include/portability_io.h" | |
16 #include "native_client/src/shared/platform/nacl_log.h" | |
17 #include "native_client/src/trusted/validator/x86/decoder/nc_inst_iter.h" | |
18 #include "native_client/src/trusted/validator/x86/decoder/nc_inst_state.h" | |
19 #include "native_client/src/trusted/validator/x86/decoder/nc_inst_state_internal
.h" | |
20 #include "native_client/src/trusted/validator/x86/ncval_reg_sfi/ncvalidate_iter.
h" | |
21 #include "native_client/src/trusted/validator/x86/ncval_reg_sfi/ncvalidate_iter_
internal.h" | |
22 | |
23 Bool NACL_FLAGS_opcode_histogram = FALSE; | |
24 | |
25 void NaClOpcodeHistogramInitialize(NaClValidatorState* state) { | |
26 int i; | |
27 if (!state->print_opcode_histogram) return; | |
28 for (i = 0; i < 256; ++i) { | |
29 state->opcode_histogram.opcode_histogram[i] = 0; | |
30 } | |
31 } | |
32 | |
33 void NaClOpcodeHistogramRecord(NaClValidatorState* state) { | |
34 NaClInstState* inst_state = state->cur_inst_state; | |
35 const NaClInst* inst = state->cur_inst; | |
36 if ((inst->name != InstInvalid) && | |
37 (inst_state->num_opcode_bytes > 0)) { | |
38 state->opcode_histogram.opcode_histogram[ | |
39 inst_state->bytes.byte[inst_state->num_prefix_bytes]]++; | |
40 } | |
41 } | |
42 | |
43 #define LINE_SIZE 1024 | |
44 | |
45 void NaClOpcodeHistogramPrintStats(NaClValidatorState* state) { | |
46 int i; | |
47 char line[LINE_SIZE]; | |
48 int line_size = LINE_SIZE; | |
49 int printed_in_this_row = 0; | |
50 NaClValidatorMessage(LOG_INFO, state, "Opcode Histogram:\n"); | |
51 for (i = 0; i < 256; ++i) { | |
52 if (0 != state->opcode_histogram.opcode_histogram[i]) { | |
53 if (line_size < LINE_SIZE) { | |
54 line_size -= SNPRINTF(line, line_size, "%"NACL_PRId32"\t0x%02x\t", | |
55 state->opcode_histogram.opcode_histogram[i], i); | |
56 } | |
57 ++printed_in_this_row; | |
58 if (printed_in_this_row > 3) { | |
59 printed_in_this_row = 0; | |
60 NaClValidatorMessage(LOG_INFO, state, "%s\n", line); | |
61 line_size = LINE_SIZE; | |
62 } | |
63 } | |
64 } | |
65 if (0 != printed_in_this_row) { | |
66 NaClValidatorMessage(LOG_INFO, state, "%s\n", line); | |
67 } | |
68 } | |
OLD | NEW |