Repository navigation
Expand file tree
/
Copy pathjitdump.c
More file actions
269 lines (228 loc) · 6.61 KB
/
Copy pathjitdump.c
File metadata and controls
269 lines (228 loc) · 6.61 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
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
#include "mudlle-config.h"
#include "jitdump.h"
#if !defined NOCOMPILER && defined __linux__
#define _GNU_SOURCE /* for gettid() */
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <sys/mman.h>
#include "dwarf.h"
#include "elf.h"
#include "mvalues.h"
#include "strbuf.h"
#include "utils.h"
/* https://github.com/torvalds/linux/blob/master/tools/perf/Documentation/
jitdump-specification.txt */
#define JITDUMP_HEADER_MAGIC 0x4a695444 /* "JiTD" */
#define JITDUMP_HEADER_VERSION 1
struct jitdump_header {
uint32_t magic;
uint32_t version;
uint32_t total_size;
uint32_t elf_mach;
uint32_t pad1;
uint32_t pid;
uint64_t timestamp;
uint64_t flags;
};
enum jit_record_type {
JIT_CODE_LOAD,
JIT_CODE_MOVE,
JIT_CODE_DEBUG_INFO,
JIT_CODE_CLOSE,
};
struct jitdump_record_header {
enum jit_record_type id : 32;
uint32_t total_size;
uint64_t timestamp;
};
struct jitdump_load_record {
struct jitdump_record_header h;
uint32_t pid;
uint32_t tid;
uint64_t vma;
uint64_t code_addr;
uint64_t code_size;
uint64_t code_index;
/* followed by NUL-terminated name */
};
struct jitdump_move_record {
struct jitdump_record_header h;
uint32_t pid;
uint32_t tid;
uint64_t vma;
uint64_t old_code_addr;
uint64_t new_code_addr;
uint64_t code_size;
uint64_t code_index;
};
struct jitdump_debug_entry {
uint64_t addr;
int lineno; /* source line number starting at 1 */
int discrim; /* column discriminator, 0 is default */
/* followed by NUL-terminated filename; \xff\0 if same as previous entry */
};
struct jitdump_debug_info_record {
struct jitdump_record_header h;
uint64_t code_addr;
uint64_t nr_entry;
/* followed by struct jitdump_debug_entry */
};
static FILE *jitdump_file;
static void *jitdump_mmap;
static struct strbuf jitdump_path;
static uint64_t jitdump_timestamp(void)
{
struct timespec ts;
if (clock_gettime(CLOCK_MONOTONIC, &ts))
{
perror("clock_gettime()");
abort();
}
return (uint64_t)ts.tv_sec * 1000000000 + ts.tv_nsec;
}
void start_jitdump(const char *dstdir, const char *srcpath)
{
if (dstdir == NULL)
dstdir = "";
struct strbuf sb = sb_initf("%s%sjit-%ld.dump", dstdir,
*dstdir ? "/" : "", (long)getpid());
jitdump_file = fopen(sb_str(&sb), "w+");
if (jitdump_file == NULL)
{
struct strbuf sberr = sb_initf("fopen(\"%s\")", sb_str(&sb));
perror(sb_str(&sberr));
sb_free(&sberr);
sb_free(&sb);
return;
}
sb_free(&sb);
sb_empty(&jitdump_path);
sb_addstr(&jitdump_path, srcpath);
jitdump_mmap = mmap(NULL, pagesize, PROT_READ | PROT_EXEC,
MAP_PRIVATE, fileno(jitdump_file), 0);
if (jitdump_mmap == MAP_FAILED)
{
perror("mmap()");
stop_jitdump();
return;
}
struct jitdump_header header = {
.magic = JITDUMP_HEADER_MAGIC,
.version = JITDUMP_HEADER_VERSION,
.total_size = sizeof header,
.elf_mach = EM_MACHINE,
.pid = getpid(),
.timestamp = jitdump_timestamp(),
};
fwrite(&header, sizeof header, 1, jitdump_file);
fflush(jitdump_file);
}
void stop_jitdump(void)
{
sb_free(&jitdump_path);
if (jitdump_mmap != NULL && jitdump_mmap != MAP_FAILED)
{
munmap(jitdump_mmap, pagesize);
jitdump_mmap = NULL;
}
if (jitdump_file == NULL)
return;
fclose(jitdump_file);
jitdump_file = NULL;
}
static void jitdump_debug_info(struct mcode *mcode)
{
size_t nstates;
struct lni_state *states = line_number_info(mcode->code.linenos, &nstates);
if (states == NULL)
return;
size_t namesize = string_len(code_filename(&mcode->code)) + 1;
struct jitdump_debug_info_record r = {
.h = {
.id = JIT_CODE_DEBUG_INFO,
.total_size = (sizeof r
+ nstates * sizeof (struct jitdump_debug_entry)
+ nstates * sb_len(&jitdump_path)
+ nstates * namesize),
.timestamp = jitdump_timestamp()
},
.code_addr = (ulong)&mcode->mcode,
.nr_entry = nstates
};
fwrite(&r, sizeof r, 1, jitdump_file);
for (size_t i = 0; i < nstates; ++i)
{
/* it looks like a bug in linux/tools/perf/util/genelf.c that one has to
add sizeof (struct elf_file_hdr) below */
struct jitdump_debug_entry e = {
.addr = ((ulong)&mcode->mcode
+ sizeof (struct elf_file_hdr)
+ states[i].addr),
.lineno = states[i].line,
};
fwrite(&e, sizeof e, 1, jitdump_file);
fwrite(sb_str(&jitdump_path), sb_len(&jitdump_path), 1, jitdump_file);
fwrite(code_filename(&mcode->code)->str, namesize, 1, jitdump_file);
}
free(states);
fflush(jitdump_file);
}
void jitdump_load(struct mcode *mcode)
{
if (jitdump_file == NULL)
return;
jitdump_debug_info(mcode);
static const char fn_str[] = "fn";
const char *name = (mcode->code.varname
? mcode->code.varname->str
: fn_str);
size_t namesize = (mcode->code.varname
? string_len(mcode->code.varname) + 1
: sizeof fn_str);
struct jitdump_load_record r = {
.h = {
.id = JIT_CODE_LOAD,
.total_size = sizeof r + namesize + mcode->code_length,
.timestamp = jitdump_timestamp()
},
.pid = getpid(),
.tid = gettid(),
.vma = (ulong)&mcode->mcode,
.code_addr = (ulong)&mcode->mcode,
.code_size = mcode->code_length,
.code_index = mcode->sequence
};
fwrite(&r, sizeof r, 1, jitdump_file);
fwrite(name, namesize, 1, jitdump_file);
fwrite(mcode->mcode, mcode->code_length, 1, jitdump_file);
fflush(jitdump_file);
}
/* note that 'new_mcode' might not have the moved object yet */
void jitdump_move(struct mcode *old_mcode, struct mcode *new_mcode)
{
if (jitdump_file == NULL)
return;
struct jitdump_move_record r = {
.h = {
.id = JIT_CODE_MOVE,
.total_size = sizeof r,
.timestamp = jitdump_timestamp()
},
.pid = getpid(),
.tid = gettid(),
.vma = (ulong)&new_mcode->mcode,
.old_code_addr = (ulong)old_mcode->mcode,
.new_code_addr = (ulong)&new_mcode->mcode,
.code_size = old_mcode->code_length,
.code_index = old_mcode->sequence
};
fwrite(&r, sizeof r, 1, jitdump_file);
fflush(jitdump_file);
}
#else /* NOCOMPILER || !__linux__ */
void start_jitdump(const char *dstdir, const char *srcpath) { }
void stop_jitdump(void) { }
void jitdump_load(struct mcode *mcode) { }
void jitdump_move(struct mcode *old_mcode, struct mcode *new_mcode) { }
#endif /* NOCOMPILER || !__linux__ */