-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstrument.hh
More file actions
86 lines (80 loc) · 2.32 KB
/
instrument.hh
File metadata and controls
86 lines (80 loc) · 2.32 KB
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
#include <stdint.h> /* *int*_t */
#include <stdlib.h> /* malloc, free */
#include <glib.h> /* GString */
#include <stdio.h>
class Instru_Inst {
#define PATTERN1 " %lx %hhd"
#define PATTERN2 " %02hhx"
public:
uint64_t addr;
uint8_t n_bytes;
uint8_t *bytes;
Instru_Inst(uint8_t n_bytes) {
this->n_bytes = n_bytes;
bytes = (uint8_t *)malloc(sizeof(uint8_t) * n_bytes);
}
Instru_Inst(FILE *file) {
if (EOF == fscanf(file, PATTERN1, &addr, &n_bytes)) {
perror("fscanf"); exit(-1);
}
bytes = (uint8_t *)malloc(sizeof(uint8_t) * n_bytes);
for (int i=0; i<n_bytes; i++) {
if (EOF == fscanf(file, PATTERN2, bytes+i)) {
perror("fscanf"); exit(-1);
}
}
if (EOF == fscanf(file, "\n")) {
perror("fscanf"); exit(-1);
}
}
~Instru_Inst(void) {if (bytes) {
free(bytes);
bytes = NULL;
}}
GString *tostr(void) {
GString *str = g_string_new(NULL);
g_string_append_printf(str, PATTERN1, addr, n_bytes);
for (int i=0; i<n_bytes; i++) {
g_string_append_printf(str, PATTERN2, bytes[i]);
}
g_string_append_printf(str, "\n");
return str;
}
#undef PATTERN1
#undef PATTERN2
};
class Instru_TBCount {
#define PATTERN "%ld %lx %d\n"
public:
uint64_t count;
uint64_t vaddr;
uint32_t n_insns;
Instru_Inst **insns;
Instru_TBCount(uint32_t n_insns) {
this->n_insns = n_insns;
insns = (Instru_Inst **)malloc(sizeof(Instru_Inst *) * n_insns);
}
Instru_TBCount(FILE *file) {
if (EOF == fscanf(file, PATTERN, &count, &vaddr, &n_insns)) {
perror("fscanf"); exit(-1);
}
insns = (Instru_Inst **)malloc(sizeof(Instru_Inst *) * n_insns);
for (int i=0; i<n_insns; i++)
insns[i] = new Instru_Inst(file);
}
~Instru_TBCount(void) {if (insns) {
for (int i=0; i<n_insns; i++) {
delete insns[i];
}
free(insns);
insns = NULL;
}}
GString *tostr(void) {
GString *str = g_string_new(NULL);
g_string_append_printf(str, PATTERN, count, vaddr, n_insns);
for (int i=0; i<n_insns; i++)
g_string_append(str, insns[i]->tostr()->str);
return str;
}
#undef PATTERN
};