about summary refs log tree commit diff
path: root/frida_mode/src/stats/stats_arm64.c
blob: ea283dbeccacc22ded0c4bb249701ae2f8ecda92 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include <sys/mman.h>

#include "frida-gumjs.h"

#include "ranges.h"
#include "shm.h"
#include "stats.h"
#include "util.h"

#define MICRO_TO_SEC 1000000

#if defined(__aarch64__)

typedef struct {

  guint64 num_blocks;
  guint64 num_instructions;

  guint64 num_eob;
  guint64 num_reloc;

  guint64 num_adr;
  guint64 num_adrp;

  guint64 num_b;
  guint64 num_bcc;
  guint64 num_bl;
  guint64 num_br;

  guint64 num_cbz;
  guint64 num_cbnz;

  guint64 num_ldr;
  guint64 num_ldrsw;

  guint64 num_ret;

  guint64 num_tbz;
  guint64 num_tbnz;

} stats_data_arch_t;

static stats_data_arch_t *stats_data_arch = NULL;

void starts_arch_init(void) {

  stats_data_arch = shm_create(sizeof(stats_data_arch_t));

}

static void stats_write_arch_stat(char *label, guint64 value, guint64 total) {

  stats_print("%-30s ", label);
  stats_print("%10" G_GINT64_MODIFIER "u ", value);
  if (total == 0) {

    stats_print("(--.--%%), ");

  } else {

    stats_print("(%5.2f%%) ", ((float)value * 100) / total);

  }

  stats_print("\n");

}

static void stats_write_arch_stat_delta(char *label, guint64 prev_value,
                                        guint64 curr_value, guint elapsed,
                                        guint64 prev_total,
                                        guint64 curr_total) {

  guint64 delta = curr_value - prev_value;
  guint64 delta_total = curr_total - prev_total;
  guint64 per_sec = delta / elapsed;

  stats_print("%-30s ", label);

  stats_print("%10" G_GINT64_MODIFIER "u ", curr_value);
  if (curr_total == 0) {

    stats_print("(--.--%%), ");

  } else {

    stats_print("(%5.2f%%) ", ((float)curr_value * 100) / curr_total);

  }

  stats_print("%10" G_GINT64_MODIFIER "u ", delta);
  if (delta_total == 0) {

    stats_print("(--.--%%), ");

  } else {

    stats_print("(%5.2f%%) ", ((float)delta * 100) / delta_total);

  }

  stats_print("[%10" G_GINT64_MODIFIER "u/s]", per_sec);
  stats_print("\n");

}

void stats_write_arch(stats_data_t *data) {

  guint elapsed =
      (data->curr.stats_time - data->prev.stats_time) / MICRO_TO_SEC;
  stats_print("%-30s %10s %19s\n", "Transitions", "cumulative", "delta");
  stats_print("%-30s %10s %19s\n", "-----------", "----------", "-----");
  stats_print(
      "%-30s %10" G_GINT64_MODIFIER "u %-8s %10" G_GINT64_MODIFIER "u\n",
      "total", data->curr.total, "", data->curr.total - data->prev.total);
  stats_write_arch_stat_delta("call_imm", data->prev.call_imm,
                              data->curr.call_imm, elapsed, data->prev.total,
                              data->curr.total);
  stats_write_arch_stat_delta("call_reg", data->prev.call_reg,
                              data->curr.call_reg, elapsed, data->prev.total,
                              data->curr.total);
  stats_write_arch_stat_delta("excluded_call_reg", data->prev.excluded_call_reg,
                              data->curr.excluded_call_reg, elapsed,
                              data->prev.total, data->curr.total);
  stats_write_arch_stat_delta("ret", data->prev.ret, data->curr.ret, elapsed,
                              data->prev.total, data->curr.total);
  stats_write_arch_stat_delta("post_call_invoke", data->prev.post_call_invoke,
                              data->curr.post_call_invoke, elapsed,
                              data->prev.total, data->curr.total);
  stats_write_arch_stat_delta("excluded_call_imm", data->prev.excluded_call_imm,
                              data->curr.excluded_call_imm, elapsed,
                              data->prev.total, data->curr.total);
  stats_write_arch_stat_delta("jmp_imm", data->prev.jmp_imm, data->curr.jmp_imm,
                              elapsed, data->prev.total, data->curr.total);
  stats_write_arch_stat_delta("jmp_reg", data->prev.jmp_reg, data->curr.jmp_reg,
                              elapsed, data->prev.total, data->curr.total);
  stats_write_arch_stat_delta("jmp_cond_cc", data->prev.jmp_cond_cc,
                              data->curr.jmp_cond_cc, elapsed, data->prev.total,
                              data->curr.total);
  stats_write_arch_stat_delta("jmp_cond_cbz", data->prev.jmp_cond_cbz,
                              data->curr.jmp_cond_cbz, elapsed,
                              data->prev.total, data->curr.total);
  stats_write_arch_stat_delta("jmp_cond_cbnz", data->prev.jmp_cond_cbnz,
                              data->curr.jmp_cond_cbnz, elapsed,
                              data->prev.total, data->curr.total);
  stats_write_arch_stat_delta("jmp_cond_tbz", data->prev.jmp_cond_tbz,
                              data->curr.jmp_cond_tbz, elapsed,
                              data->prev.total, data->curr.total);
  stats_write_arch_stat_delta("jmp_cond_tbnz", data->prev.jmp_cond_tbnz,
                              data->curr.jmp_cond_tbnz, elapsed,
                              data->prev.total, data->curr.total);
  stats_write_arch_stat_delta("jmp_continuation", data->prev.jmp_continuation,
                              data->curr.jmp_continuation, elapsed,
                              data->prev.total, data->curr.total);
  stats_print("\n");
  stats_print("\n");

  stats_print("Instrumentation\n");
  stats_print("---------------\n");
  stats_print("%-30s %10" G_GINT64_MODIFIER "u\n", "Instructions",
              stats_data_arch->num_instructions);
  stats_print("%-30s %10" G_GINT64_MODIFIER "u\n", "Blocks",
              stats_data_arch->num_blocks);

  if (stats_data_arch->num_blocks != 0) {

    stats_print(
        "%-30s %10" G_GINT64_MODIFIER "u\n", "Avg Instructions / Block ",
        stats_data_arch->num_instructions / stats_data_arch->num_blocks);

  }

  stats_print("\n");
  stats_print("\n");

  guint64 num_instructions = stats_data_arch->num_instructions;

  stats_print("EOB Instructions\n");
  stats_print("----------------\n");
  stats_write_arch_stat("Total", stats_data_arch->num_eob, num_instructions);
  stats_write_arch_stat("B", stats_data_arch->num_b, num_instructions);
  stats_write_arch_stat("Bcc", stats_data_arch->num_bcc, num_instructions);
  stats_write_arch_stat("BL", stats_data_arch->num_bl, num_instructions);
  stats_write_arch_stat("BR", stats_data_arch->num_br, num_instructions);
  stats_write_arch_stat("CBZ", stats_data_arch->num_cbz, num_instructions);
  stats_write_arch_stat("CBNZ", stats_data_arch->num_cbnz, num_instructions);
  stats_write_arch_stat("RET", stats_data_arch->num_ret, num_instructions);
  stats_write_arch_stat("TBZ", stats_data_arch->num_tbz, num_instructions);
  stats_write_arch_stat("TBNZ", stats_data_arch->num_tbnz, num_instructions);
  stats_print("\n");
  stats_print("\n");

  stats_print("Relocated Instructions\n");
  stats_print("----------------------\n");
  stats_write_arch_stat("Total", stats_data_arch->num_reloc, num_instructions);

  stats_write_arch_stat("ADR", stats_data_arch->num_adr, num_instructions);
  stats_write_arch_stat("ADRP", stats_data_arch->num_adrp, num_instructions);
  stats_write_arch_stat("LDR", stats_data_arch->num_ldr, num_instructions);
  stats_write_arch_stat("LDRSW", stats_data_arch->num_ldrsw, num_instructions);

  stats_print("\n");
  stats_print("\n");

}

void stats_collect_arch(const cs_insn *instr, gboolean begin) {

  if (stats_data_arch == NULL) { return; }
  if (begin) { stats_data_arch->num_blocks++; }
  stats_data_arch->num_instructions++;

  switch (instr->id) {

    case ARM64_INS_ADR:
      stats_data_arch->num_adr++;
      stats_data_arch->num_reloc++;
      break;

    case ARM64_INS_ADRP:
      stats_data_arch->num_adrp++;
      stats_data_arch->num_reloc++;
      break;

    case ARM64_INS_B:
      switch (instr->detail->arm64.cc) {

        case ARM64_CC_INVALID:
        case ARM64_CC_AL:
        case ARM64_CC_NV:
          stats_data_arch->num_b++;
          break;
        default:
          stats_data_arch->num_bcc++;
          break;

      }

      stats_data_arch->num_eob++;
      break;

    case ARM64_INS_BR:
    case ARM64_INS_BRAA:
    case ARM64_INS_BRAAZ:
    case ARM64_INS_BRAB:
    case ARM64_INS_BRABZ:
      stats_data_arch->num_br++;
      stats_data_arch->num_eob++;
      break;

    case ARM64_INS_BL:
    case ARM64_INS_BLR:
    case ARM64_INS_BLRAA:
    case ARM64_INS_BLRAAZ:
    case ARM64_INS_BLRAB:
    case ARM64_INS_BLRABZ:
      stats_data_arch->num_bl++;
      stats_data_arch->num_eob++;
      break;

    case ARM64_INS_CBZ:
      stats_data_arch->num_cbz++;
      stats_data_arch->num_eob++;
      break;

    case ARM64_INS_CBNZ:
      stats_data_arch->num_cbnz++;
      stats_data_arch->num_eob++;
      break;

    case ARM64_INS_LDR:
      stats_data_arch->num_ldr++;
      stats_data_arch->num_reloc++;
      break;

    case ARM64_INS_LDRSW:
      stats_data_arch->num_ldrsw++;
      stats_data_arch->num_reloc++;
      break;

    case ARM64_INS_RET:
    case ARM64_INS_RETAA:
    case ARM64_INS_RETAB:
      stats_data_arch->num_ret++;
      stats_data_arch->num_eob++;
      break;

    case ARM64_INS_TBZ:
      stats_data_arch->num_tbz++;
      stats_data_arch->num_eob++;
      break;

    case ARM64_INS_TBNZ:
      stats_data_arch->num_tbnz++;
      stats_data_arch->num_eob++;
      break;

    default:
      break;

  }

}

#endif