[6ee980]: libopagent / opagent.c Maximize Restore History

Download this file

opagent.c    436 lines (392 with data), 11.6 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
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/**
* @file opagent.c
* Interface to report symbol names and dynamically generated code to Oprofile
*
* @remark Copyright 2007 OProfile authors
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* @author Jens Wilke
* @Modifications Daniel Hansel
*
* Copyright IBM Corporation 2007
*
*/
/******************************************************************
* ATTENTION:
* When adding new functions to this interface, you MUST update
* opagent_symbols.ver.
*
* If a change is made to an existing exported function, perform the
* the following steps. As an example, assume op_open_agent()
* is being updated to include a 'dump_code' parameter.
* 1. Update the opagent.ver file with a new version node, and
* add the op_open_agent to it. Note that op_open_agent
* is also still declared in the original version node.
* 2. Add '__asm__(".symver <blah>") directives to this .c source file.
* For this example, the directives would be as follows:
* __asm__(".symver op_open_agent_1_0,op_open_agent@OPAGENT_1.0");
* __asm__(".symver op_open_agent_2_0,op_open_agent@@OPAGENT_2.0");
* 3. Update the declaration of op_open_agent in the header file with
* the additional parameter.
* 4. Change the name of the original op_open_agent to "op_open_agent_1_0"
* in this .c source file.
* 5. Add the new op_open_agent_2_0(int dump_code) function in this
* .c source file.
*
* See libopagent/Makefile.am for more information.
*******************************************************************/
#include "config.h"
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdint.h>
#include <limits.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include <bfd.h>
#include "opagent.h"
#include "op_config.h"
#include "jitdump.h"
// Declare BFD-related global variables.
static char * _bfd_target_name;
static int _bfd_arch;
static unsigned int _bfd_mach;
// Define BFD-related global variables.
static int define_bfd_vars(void)
{
bfd * bfd;
bfd_boolean r;
int len;
#define MAX_PATHLENGTH 2048
char mypath[MAX_PATHLENGTH];
len = readlink("/proc/self/exe", mypath, sizeof(mypath));
if (len < 0) {
fprintf(stderr, "libopagent: readlink /proc/self/exe failed\n");
return -1;
}
if (len >= MAX_PATHLENGTH) {
fprintf(stderr, "libopagent: readlink /proc/self/exe returned"
" path length longer than %d.\n", MAX_PATHLENGTH);
return -1;
}
mypath[len] = '\0';
bfd_init();
bfd = bfd_openr(mypath, NULL);
if (bfd == NULL) {
bfd_perror("bfd_openr error. Cannot get required BFD info");
return -1;
}
r = bfd_check_format(bfd, bfd_object);
if (!r) {
bfd_perror("bfd_get_arch error. Cannot get required BFD info");
return -1;
}
_bfd_target_name = bfd->xvec->name;
_bfd_arch = bfd_get_arch(bfd);
_bfd_mach = bfd_get_mach(bfd);
return 0;
}
/**
* Define the version of the opagent library.
*/
#define OP_MAJOR_VERSION 1
#define OP_MINOR_VERSION 0
#define TMP_OPROFILE_DIR "/tmp/.oprofile"
#define JITDUMP_DIR TMP_OPROFILE_DIR "/jitdump"
#define MSG_MAXLEN 20
op_agent_t op_open_agent(void)
{
char pad_bytes[7] = {0, 0, 0, 0, 0, 0, 0};
int pad_cnt;
char dump_path[PATH_MAX];
char err_msg[PATH_MAX + 16];
struct stat dirstat;
int rc;
struct jitheader header;
int fd;
struct timeval tv;
FILE * dumpfile = NULL;
/* Coverity complains about 'time-of-check-time-of-use' race if we do stat() on
* a file (or directory) and then open or create it afterwards. So instead,
* we'll try to open it and see what happens.
*/
int create_dir = 0;
DIR * dir1 = opendir(TMP_OPROFILE_DIR);
if (!dir1) {
if (errno == ENOENT) {
create_dir = 1;
} else if (errno == ENOTDIR) {
fprintf(stderr, "Error: Creation of directory %s failed. File exists where directory is expected.\n",
TMP_OPROFILE_DIR);
return NULL;
}
} else {
closedir(dir1);
}
if (create_dir) {
create_dir = 0;
rc = mkdir(TMP_OPROFILE_DIR, S_IRWXU | S_IRWXG | S_IRWXO);
if (rc && (errno != EEXIST)) {
fprintf(stderr, "Error trying to create %s dir.\n", TMP_OPROFILE_DIR);
return NULL;
}
}
dir1 = opendir(JITDUMP_DIR);
if (!dir1) {
if (errno == ENOENT) {
create_dir = 1;
} else if (errno == ENOTDIR) {
fprintf(stderr, "Error: Creation of directory %s failed. File exists where directory is expected.\n",
JITDUMP_DIR);
return NULL;
}
} else {
closedir(dir1);
}
if (create_dir) {
rc = mkdir(JITDUMP_DIR, S_IRWXU | S_IRWXG | S_IRWXO);
if (rc && (errno != EEXIST)) {
fprintf(stderr, "Error trying to create %s dir.\n", JITDUMP_DIR);
return NULL;
}
}
snprintf(dump_path, PATH_MAX, "%s/%i.dump", JITDUMP_DIR, getpid());
snprintf(err_msg, PATH_MAX + 16, "Error opening %s\n", dump_path);
// make the dump file only accessible for the user for security reason.
fd = creat(dump_path, S_IRUSR|S_IWUSR);
if (fd == -1) {
fprintf(stderr, "%s\n", err_msg);
return NULL;
}
dumpfile = fdopen(fd, "w");
if (!dumpfile) {
fprintf(stderr, "%s\n", err_msg);
close(fd);
return NULL;
}
if (define_bfd_vars()) {
fclose(dumpfile);
return NULL;
}
header.magic = JITHEADER_MAGIC;
header.version = JITHEADER_VERSION;
header.totalsize = sizeof(header) + strlen(_bfd_target_name) + 1;
/* calculate amount of padding '\0' */
pad_cnt = PADDING_8ALIGNED(header.totalsize);
header.totalsize += pad_cnt;
header.bfd_arch = _bfd_arch;
header.bfd_mach = _bfd_mach;
if (gettimeofday(&tv, NULL)) {
fclose(dumpfile);
fprintf(stderr, "gettimeofday failed\n");
return NULL;
}
header.timestamp = tv.tv_sec;
snprintf(err_msg, PATH_MAX + 16, "Error writing to %s", dump_path);
if (!fwrite(&header, sizeof(header), 1, dumpfile)) {
fclose(dumpfile);
fprintf(stderr, "%s\n", err_msg);
return NULL;
}
if (!fwrite(_bfd_target_name, strlen(_bfd_target_name) + 1, 1,
dumpfile)) {
fclose(dumpfile);
fprintf(stderr, "%s\n", err_msg);
return NULL;
}
/* write padding '\0' if necessary */
if (pad_cnt && !fwrite(pad_bytes, pad_cnt, 1, dumpfile)) {
fclose(dumpfile);
fprintf(stderr, "%s\n", err_msg);
return NULL;
}
fflush(dumpfile);
return (op_agent_t)dumpfile;
}
int op_close_agent(op_agent_t hdl)
{
struct jr_code_close rec;
struct timeval tv;
FILE * dumpfile = (FILE *) hdl;
if (!dumpfile) {
errno = EINVAL;
return -1;
}
rec.id = JIT_CODE_CLOSE;
rec.total_size = sizeof(rec);
if (gettimeofday(&tv, NULL)) {
fprintf(stderr, "gettimeofday failed\n");
return -1;
}
rec.timestamp = tv.tv_sec;
if (!fwrite(&rec, sizeof(rec), 1, dumpfile))
return -1;
fclose(dumpfile);
dumpfile = NULL;
return 0;
}
int op_write_native_code(op_agent_t hdl, char const * symbol_name,
uint64_t vma, void const * code, unsigned int const size)
{
struct jr_code_load rec;
struct timeval tv;
size_t sz_symb_name;
char pad_bytes[7] = { 0, 0, 0, 0, 0, 0, 0 };
size_t padding_count;
FILE * dumpfile = (FILE *) hdl;
if (!dumpfile) {
errno = EINVAL;
fprintf(stderr, "Invalid hdl argument\n");
return -1;
}
sz_symb_name = strlen(symbol_name) + 1;
rec.id = JIT_CODE_LOAD;
rec.code_size = size;
rec.vma = vma;
rec.code_addr = (u64) (uintptr_t) code;
rec.total_size = code ? sizeof(rec) + sz_symb_name + size :
sizeof(rec) + sz_symb_name;
/* calculate amount of padding '\0' */
padding_count = PADDING_8ALIGNED(rec.total_size);
rec.total_size += padding_count;
if (gettimeofday(&tv, NULL)) {
fprintf(stderr, "gettimeofday failed\n");
return -1;
}
rec.timestamp = tv.tv_sec;
/* locking makes sure that we continuously write this record, if
* we are called within a multi-threaded context */
flockfile(dumpfile);
/* Write record, symbol name, code (optionally), and (if necessary)
* additonal padding \0 bytes.
*/
if (fwrite_unlocked(&rec, sizeof(rec), 1, dumpfile) &&
fwrite_unlocked(symbol_name, sz_symb_name, 1, dumpfile)) {
if (code)
fwrite_unlocked(code, size, 1, dumpfile);
if (padding_count)
fwrite_unlocked(pad_bytes, padding_count, 1, dumpfile);
/* Always flush to ensure conversion code to elf will see
* data as soon as possible */
fflush_unlocked(dumpfile);
funlockfile(dumpfile);
return 0;
}
fflush_unlocked(dumpfile);
funlockfile(dumpfile);
return -1;
}
int op_write_debug_line_info(op_agent_t hdl, void const * code,
size_t nr_entry,
struct debug_line_info const * compile_map)
{
struct jr_code_debug_info rec;
long cur_pos, last_pos;
struct timeval tv;
size_t i;
size_t padding_count;
char padd_bytes[7] = {0, 0, 0, 0, 0, 0, 0};
int rc = -1;
FILE * dumpfile = (FILE *) hdl;
if (!dumpfile) {
errno = EINVAL;
fprintf(stderr, "Invalid hdl argument\n");
return -1;
}
/* write nothing if no entries are provided */
if (nr_entry == 0)
return 0;
rec.id = JIT_CODE_DEBUG_INFO;
rec.code_addr = (uint64_t)(uintptr_t)code;
/* will be fixed after writing debug line info */
rec.total_size = 0;
rec.nr_entry = nr_entry;
if (gettimeofday(&tv, NULL)) {
fprintf(stderr, "gettimeofday failed\n");
return -1;
}
rec.timestamp = tv.tv_sec;
flockfile(dumpfile);
if ((cur_pos = ftell(dumpfile)) == -1l)
goto error;
if (!fwrite_unlocked(&rec, sizeof(rec), 1, dumpfile))
goto error;
for (i = 0; i < nr_entry; ++i) {
if (!fwrite_unlocked(&compile_map[i].vma,
sizeof(compile_map[i].vma), 1,
dumpfile) ||
!fwrite_unlocked(&compile_map[i].lineno,
sizeof(compile_map[i].lineno), 1,
dumpfile) ||
!fwrite_unlocked(compile_map[i].filename,
strlen(compile_map[i].filename) + 1, 1,
dumpfile))
goto error;
}
if ((last_pos = ftell(dumpfile)) == -1l)
goto error;
rec.total_size = last_pos - cur_pos;
padding_count = PADDING_8ALIGNED(rec.total_size);
rec.total_size += padding_count;
if (padding_count && !fwrite(padd_bytes, padding_count, 1, dumpfile))
goto error;
if (fseek(dumpfile, cur_pos, SEEK_SET) == -1l)
goto error;
if (!fwrite_unlocked(&rec, sizeof(rec), 1, dumpfile))
goto error;
if (fseek(dumpfile, last_pos + padding_count, SEEK_SET) == -1)
goto error;
rc = 0;
error:
fflush_unlocked(dumpfile);
funlockfile(dumpfile);
return rc;
}
int op_unload_native_code(op_agent_t hdl, uint64_t vma)
{
struct jr_code_unload rec;
struct timeval tv;
FILE * dumpfile = (FILE *) hdl;
if (!dumpfile) {
errno = EINVAL;
fprintf(stderr, "Invalid hdl argument\n");
return -1;
}
rec.id = JIT_CODE_UNLOAD;
rec.vma = vma;
rec.total_size = sizeof(rec);
if (gettimeofday(&tv, NULL)) {
fprintf(stderr, "gettimeofday failed\n");
return -1;
}
rec.timestamp = tv.tv_sec;
if (!fwrite(&rec, sizeof(rec), 1, dumpfile))
return -1;
fflush(dumpfile);
return 0;
}
int op_major_version(void)
{
return OP_MAJOR_VERSION;
}
int op_minor_version(void)
{
return OP_MINOR_VERSION;
}