forked from andikleen/simple-pt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dtools.c
154 lines (143 loc) · 4.48 KB
/
dtools.c
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
/* Decoding tools */
/*
* Copyright (c) 2015, Intel Corporation
* Author: Andi Kleen
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <intel-pt.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <ctype.h>
#include <cpuid.h>
#include "dtools.h"
#include "map.h"
#include "elf.h"
#include "symtab.h"
double tsc_freq;
struct pt_insn_decoder *init_decoder(char *fn, struct pt_config *config)
{
unsigned zero = 0;
pt_cpu_errata(&config->errata, &config->cpu);
/* When no bit is set, set all, as libipt does not keep up with newer
* CPUs otherwise.
*/
if (!memcmp(&config->errata, &zero, 4))
memset(&config->errata,0xff, sizeof(config->errata));
size_t len;
unsigned char *map = mapfile(fn, &len);
if (!map) {
fprintf(stderr, "Cannot open PT file %s: %s\n", fn, strerror(errno));
exit(1);
}
config->begin = map;
config->end = map + len;
struct pt_insn_decoder *decoder = pt_insn_alloc_decoder(config);
if (!decoder) {
fprintf(stderr, "Cannot create PT decoder\n");
return NULL;
}
return decoder;
}
/* Sideband format:
meta family num
meta model num
meta stepping num
meta mtc_freq num-1
meta num_freq num
meta tsc_ratio eax ebx
timestamp pid cr3 load-address off-in-file path-to-binary[:codebin]
*/
void load_sideband(char *fn, struct pt_image *image, struct pt_config *config)
{
FILE *f = fopen(fn, "r");
if (!f) {
fprintf(stderr, "Cannot open %s: %s\n", fn, strerror(errno));
exit(1);
}
char *line = NULL;
size_t linelen = 0;
int lineno = 1;
for (;getline(&line, &linelen, f) > 0; lineno++) {
uint64_t cr3, addr, off, len;
unsigned pid;
double ts;
int n;
if (!strncmp(line, "meta", 4)) {
if (sscanf(line, "meta tsc_ratio %u %u",
&config->cpuid_0x15_eax,
&config->cpuid_0x15_ebx) == 2) {
/* ok */
} else if (sscanf(line, "meta family %hu",
&config->cpu.family) == 1) {
config->cpu.vendor = pcv_intel;
} else if (sscanf(line, "meta model %hhu",
&config->cpu.model) == 1) {
/* ok */
} else if (sscanf(line, "meta stepping %hhu",
&config->cpu.stepping) == 1) {
} else if (sscanf(line, "meta mtc_freq %hhu",
&config->mtc_freq) == 1) {
if (config->mtc_freq)
config->mtc_freq--;
} else if (sscanf(line, "meta nom_freq %hhu",
&config->nom_freq) == 1) {
tsc_freq = config->nom_freq / 10.0;
} else {
fprintf(stderr, "%s:%d: Unknown meta statement\n", fn, lineno);
}
continue;
}
if (sscanf(line, "%lf %u %lx %lx %lx %lx %n", &ts, &pid, &cr3, &addr, &off, &len, &n) != 6) {
/* Handle old format */
if (sscanf(line, "%lf %u %lx %lx %lx %n", &ts, &pid, &cr3, &addr, &off, &n) == 5) {
len = 0;
} else {
fprintf(stderr, "%s:%d: Parse error\n", fn, lineno);
continue;
}
}
if (ts == 0 && !seen_cr3(cr3))
continue;
while (isspace(line[n]))
n++;
/* timestamp ignored for now. could later be used to distinguish
reused CR3s or reused address space. */
/* pid ignored for now. should use in decoding. */
char *p = strchr(line + n, '\n');
if (p) {
*p = 0;
while (--p >= line + n && isspace(*p))
*p = 0;
}
if (read_elf(line + n, image, addr, cr3, off, len)) {
fprintf(stderr, "Cannot read %s: %s\n", line + n, strerror(errno));
}
}
free(line);
fclose(f);
}