| 1 |
#include "vmp_stack.h" |
| 2 |
|
| 3 |
#ifndef _GNU_SOURCE |
| 4 |
#define _GNU_SOURCE |
| 5 |
#endif |
| 6 |
|
| 7 |
#include <string.h> |
| 8 |
#include <stdio.h> |
| 9 |
#include <stdlib.h> |
| 10 |
#include <sys/types.h> |
| 11 |
#include <stddef.h> |
| 12 |
#include <assert.h> |
| 13 |
|
| 14 |
#include "vmprof.h" |
| 15 |
#include "compat.h" |
| 16 |
|
| 17 |
#ifdef VMP_SUPPORTS_NATIVE_PROFILING |
| 18 |
|
| 19 |
#if defined(VMPROF_LINUX) || defined(VMPROF_BSD) |
| 20 |
#include "unwind/vmprof_unwind.h" |
| 21 |
typedef mcontext_t unw_context_t; |
| 22 |
|
| 23 |
|
| 24 |
static int (*unw_get_reg)(unw_cursor_t*, int, unw_word_t*) = NULL; |
| 25 |
static int (*unw_step)(unw_cursor_t*) = NULL; |
| 26 |
static int (*unw_init_local)(unw_cursor_t *, unw_context_t *) = NULL; |
| 27 |
static int (*unw_get_proc_info)(unw_cursor_t *, unw_proc_info_t *) = NULL; |
| 28 |
static int (*unw_get_proc_name)(unw_cursor_t *, char *, size_t, unw_word_t*) = NULL; |
| 29 |
static int (*unw_is_signal_frame)(unw_cursor_t *) = NULL; |
| 30 |
static int (*unw_getcontext)(unw_context_t *) = NULL; |
| 31 |
#else |
| 32 |
#define UNW_LOCAL_ONLY |
| 33 |
#include <libunwind.h> |
| 34 |
#endif |
| 35 |
|
| 36 |
#endif |
| 37 |
|
| 38 |
#ifdef __APPLE__ |
| 39 |
#include <mach/mach.h> |
| 40 |
#include <mach/mach_vm.h> |
| 41 |
#include <mach/message.h> |
| 42 |
#include <mach/kern_return.h> |
| 43 |
#include <mach/task_info.h> |
| 44 |
#include <sys/types.h> |
| 45 |
#include <unistd.h> |
| 46 |
#include <dlfcn.h> |
| 47 |
#elif defined(__unix__) |
| 48 |
#include <dlfcn.h> |
| 49 |
#endif |
| 50 |
|
| 51 |
#ifdef PYPY_JIT_CODEMAP |
| 52 |
void *pypy_find_codemap_at_addr(long addr, long *start_addr); |
| 53 |
#endif |
| 54 |
|
| 55 |
int _per_loop(void) { |
| 56 |
|
| 57 |
#ifdef RPYTHON_VMPROF |
| 58 |
return 2; |
| 59 |
#else |
| 60 |
if (vmp_profiles_python_lines()) { |
| 61 |
return 2; |
| 62 |
} |
| 63 |
return 1; |
| 64 |
#endif |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
#ifdef PY_TEST |
| 69 |
|
| 70 |
PY_EVAL_RETURN_T * vmprof_eval(PY_STACK_FRAME_T *f, int throwflag) { return NULL; } |
| 71 |
#endif |
| 72 |
|
| 73 |
#ifdef VMP_SUPPORTS_NATIVE_PROFILING |
| 74 |
static intptr_t *vmp_ranges = NULL; |
| 75 |
static ssize_t vmp_range_count = 0; |
| 76 |
static int vmp_native_traces_enabled = 0; |
| 77 |
#endif |
| 78 |
static int _vmp_profiles_lines = 0; |
| 79 |
|
| 80 |
void vmp_profile_lines(int lines) { |
| 81 |
_vmp_profiles_lines = lines; |
| 82 |
} |
| 83 |
int vmp_profiles_python_lines(void) { |
| 84 |
return _vmp_profiles_lines; |
| 85 |
} |
| 86 |
|
| 87 |
#if PY_VERSION_HEX >= 0x030B0000 |
| 88 |
static _PyInterpreterFrame * _write_python_stack_entry(_PyInterpreterFrame * frame, void ** result, int * depth, int max_depth) |
| 89 |
{ |
| 90 |
#ifndef RPYTHON_VMPROF |
| 91 |
if (vmp_profiles_python_lines()) { |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
result[*depth] = (void*) (int64_t) _PyInterpreterFrame_GetLine(frame); |
| 102 |
*depth = *depth + 1; |
| 103 |
} |
| 104 |
PyCodeObject* frame_code = unsafe_PyInterpreterFrame_GetCode(frame); |
| 105 |
result[*depth] = (void*)CODE_ADDR_TO_UID(frame_code); |
| 106 |
|
| 107 |
*depth = *depth + 1; |
| 108 |
#else |
| 109 |
|
| 110 |
if (frame->kind == VMPROF_CODE_TAG) { |
| 111 |
int n = *depth; |
| 112 |
result[n++] = (void*)frame->kind; |
| 113 |
result[n++] = (void*)frame->value; |
| 114 |
*depth = n; |
| 115 |
} |
| 116 |
#ifdef PYPY_JIT_CODEMAP |
| 117 |
else if (frame->kind == VMPROF_JITTED_TAG) { |
| 118 |
intptr_t pc = ((intptr_t*)(frame->value - sizeof(intptr_t)))[0]; |
| 119 |
*depth = vmprof_write_header_for_jit_addr((intptr_t*)result, *depth, pc, max_depth); |
| 120 |
} |
| 121 |
#endif |
| 122 |
|
| 123 |
|
| 124 |
#endif |
| 125 |
|
| 126 |
return unsafe_PyInterpreterFrame_GetBack(frame); |
| 127 |
} |
| 128 |
#else |
| 129 |
|
| 130 |
static PY_STACK_FRAME_T * _write_python_stack_entry(PY_STACK_FRAME_T * frame, void ** result, int * depth, int max_depth) |
| 131 |
{ |
| 132 |
#ifndef RPYTHON_VMPROF |
| 133 |
if (vmp_profiles_python_lines()) { |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
result[*depth] = (void*) (int64_t) PyFrame_GetLineNumber(frame); |
| 144 |
*depth = *depth + 1; |
| 145 |
} |
| 146 |
PyCodeObject* frame_code = FRAME_CODE(frame); |
| 147 |
result[*depth] = (void*)CODE_ADDR_TO_UID(frame_code); |
| 148 |
Py_DECREF(frame_code); |
| 149 |
*depth = *depth + 1; |
| 150 |
#else |
| 151 |
|
| 152 |
if (frame->kind == VMPROF_CODE_TAG) { |
| 153 |
int n = *depth; |
| 154 |
result[n++] = (void*)frame->kind; |
| 155 |
result[n++] = (void*)frame->value; |
| 156 |
*depth = n; |
| 157 |
} |
| 158 |
#ifdef PYPY_JIT_CODEMAP |
| 159 |
else if (frame->kind == VMPROF_JITTED_TAG) { |
| 160 |
intptr_t pc = ((intptr_t*)(frame->value - sizeof(intptr_t)))[0]; |
| 161 |
*depth = vmprof_write_header_for_jit_addr((intptr_t*)result, *depth, pc, max_depth); |
| 162 |
} |
| 163 |
#endif |
| 164 |
|
| 165 |
|
| 166 |
#endif |
| 167 |
|
| 168 |
return FRAME_STEP(frame); |
| 169 |
} |
| 170 |
|
| 171 |
#endif |
| 172 |
|
| 173 |
#if PY_VERSION_HEX >= 0x030B0000 |
| 174 |
int vmp_walk_and_record_python_stack_only(_PyInterpreterFrame *frame, void ** result, |
| 175 |
int max_depth, int depth, intptr_t pc) |
| 176 |
{ |
| 177 |
while ((depth + _per_loop()) <= max_depth && frame) { |
| 178 |
frame = _write_python_stack_entry(frame, result, &depth, max_depth); |
| 179 |
} |
| 180 |
return depth; |
| 181 |
} |
| 182 |
#else |
| 183 |
int vmp_walk_and_record_python_stack_only(PY_STACK_FRAME_T *frame, void ** result, |
| 184 |
int max_depth, int depth, intptr_t pc) |
| 185 |
{ |
| 186 |
while ((depth + _per_loop()) <= max_depth && frame) { |
| 187 |
frame = _write_python_stack_entry(frame, result, &depth, max_depth); |
| 188 |
} |
| 189 |
return depth; |
| 190 |
} |
| 191 |
#endif |
| 192 |
|
| 193 |
#ifdef VMP_SUPPORTS_NATIVE_PROFILING |
| 194 |
int _write_native_stack(void* addr, void ** result, int depth, int max_depth) { |
| 195 |
#ifdef RPYTHON_VMPROF |
| 196 |
if (depth + 2 >= max_depth) { |
| 197 |
|
| 198 |
return depth; |
| 199 |
} |
| 200 |
result[depth++] = (void*)VMPROF_NATIVE_TAG; |
| 201 |
#else |
| 202 |
if (vmp_profiles_python_lines()) { |
| 203 |
if (depth + 2 >= max_depth) { |
| 204 |
|
| 205 |
return depth; |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
result[depth++] = 0; |
| 210 |
} |
| 211 |
#endif |
| 212 |
result[depth++] = addr; |
| 213 |
return depth; |
| 214 |
} |
| 215 |
#endif |
| 216 |
|
| 217 |
#if PY_VERSION_HEX >= 0x030B0000 |
| 218 |
int vmp_walk_and_record_stack(_PyInterpreterFrame *frame, void ** result, |
| 219 |
int max_depth, int signal, intptr_t pc) { |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
#ifdef VMP_SUPPORTS_NATIVE_PROFILING |
| 242 |
void * func_addr; |
| 243 |
unw_cursor_t cursor; |
| 244 |
unw_context_t uc; |
| 245 |
unw_proc_info_t pip; |
| 246 |
int ret; |
| 247 |
|
| 248 |
if (vmp_native_enabled() == 0) { |
| 249 |
return vmp_walk_and_record_python_stack_only(frame, result, max_depth, 0, pc); |
| 250 |
} |
| 251 |
|
| 252 |
ret = unw_getcontext(&uc); |
| 253 |
if (ret < 0) { |
| 254 |
|
| 255 |
#if DEBUG |
| 256 |
fprintf(stderr, "WARNING: unw_getcontext did not retreive context, switching to python profiling mode \n"); |
| 257 |
#endif |
| 258 |
vmp_native_disable(); |
| 259 |
return vmp_walk_and_record_python_stack_only(frame, result, max_depth, 0, pc); |
| 260 |
} |
| 261 |
ret = unw_init_local(&cursor, &uc); |
| 262 |
if (ret < 0) { |
| 263 |
|
| 264 |
#if DEBUG |
| 265 |
fprintf(stderr, "WARNING: unw_init_local did not succeed, switching to python profiling mode \n"); |
| 266 |
#endif |
| 267 |
vmp_native_disable(); |
| 268 |
return vmp_walk_and_record_python_stack_only(frame, result, max_depth, 0, pc); |
| 269 |
} |
| 270 |
|
| 271 |
if (signal < 0) { |
| 272 |
while (signal < 0) { |
| 273 |
int err = unw_step(&cursor); |
| 274 |
if (err <= 0) { |
| 275 |
#if DEBUG |
| 276 |
fprintf(stderr, "WARNING: did not find signal frame, skipping sample\n"); |
| 277 |
#endif |
| 278 |
return 0; |
| 279 |
} |
| 280 |
signal++; |
| 281 |
} |
| 282 |
} else { |
| 283 |
#ifdef VMPROF_LINUX |
| 284 |
while (signal) { |
| 285 |
int is_signal_frame = unw_is_signal_frame(&cursor); |
| 286 |
if (is_signal_frame) { |
| 287 |
unw_step(&cursor); |
| 288 |
break; |
| 289 |
} |
| 290 |
int err = unw_step(&cursor); |
| 291 |
if (err <= 0) { |
| 292 |
#if DEBUG |
| 293 |
fprintf(stderr,"WARNING: did not find signal frame, skipping sample\n"); |
| 294 |
#endif |
| 295 |
return 0; |
| 296 |
} |
| 297 |
} |
| 298 |
#else |
| 299 |
|
| 300 |
if (signal) { |
| 301 |
unw_step(&cursor); |
| 302 |
|
| 303 |
unw_step(&cursor); |
| 304 |
unw_step(&cursor); |
| 305 |
unw_step(&cursor); |
| 306 |
} |
| 307 |
#endif |
| 308 |
} |
| 309 |
|
| 310 |
int depth = 0; |
| 311 |
|
| 312 |
while ((depth + _per_loop()) <= max_depth) { |
| 313 |
unw_get_proc_info(&cursor, &pip); |
| 314 |
|
| 315 |
func_addr = (void*)pip.start_ip; |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
#ifdef PYPY_JIT_CODEMAP |
| 334 |
long start_addr = 0; |
| 335 |
unw_word_t rip = 0; |
| 336 |
if (unw_get_reg(&cursor, UNW_REG_IP, &rip) < 0) { |
| 337 |
return 0; |
| 338 |
} |
| 339 |
#endif |
| 340 |
|
| 341 |
if (IS_VMPROF_EVAL((void*)pip.start_ip)) { |
| 342 |
|
| 343 |
return vmp_walk_and_record_python_stack_only(frame, result, max_depth, depth, pc); |
| 344 |
#ifdef PYPY_JIT_CODEMAP |
| 345 |
} else if (pypy_find_codemap_at_addr(rip, &start_addr) != NULL) { |
| 346 |
depth = vmprof_write_header_for_jit_addr((intptr_t*)result, depth, pc, max_depth); |
| 347 |
return vmp_walk_and_record_python_stack_only(frame, result, max_depth, depth, pc); |
| 348 |
#endif |
| 349 |
} else { |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
if (func_addr != 0x0) { |
| 354 |
depth = _write_native_stack((void*)(((uint64_t)func_addr) | 0x1), result, depth, max_depth); |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
int err = unw_step(&cursor); |
| 359 |
if (err == 0) { |
| 360 |
break; |
| 361 |
} else if (err < 0) { |
| 362 |
|
| 363 |
return vmp_walk_and_record_python_stack_only(frame, result, max_depth, 0, pc); |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
#endif |
| 369 |
return vmp_walk_and_record_python_stack_only(frame, result, max_depth, 0, pc); |
| 370 |
} |
| 371 |
#else |
| 372 |
int vmp_walk_and_record_stack(PY_STACK_FRAME_T *frame, void ** result, |
| 373 |
int max_depth, int signal, intptr_t pc) { |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
#ifdef VMP_SUPPORTS_NATIVE_PROFILING |
| 396 |
void * func_addr; |
| 397 |
unw_cursor_t cursor; |
| 398 |
unw_context_t uc; |
| 399 |
unw_proc_info_t pip; |
| 400 |
int ret; |
| 401 |
|
| 402 |
if (vmp_native_enabled() == 0) { |
| 403 |
return vmp_walk_and_record_python_stack_only(frame, result, max_depth, 0, pc); |
| 404 |
} |
| 405 |
|
| 406 |
ret = unw_getcontext(&uc); |
| 407 |
if (ret < 0) { |
| 408 |
|
| 409 |
#if DEBUG |
| 410 |
fprintf(stderr, "WARNING: unw_getcontext did not retreive context, switching to python profiling mode \n"); |
| 411 |
#endif |
| 412 |
vmp_native_disable(); |
| 413 |
return vmp_walk_and_record_python_stack_only(frame, result, max_depth, 0, pc); |
| 414 |
} |
| 415 |
ret = unw_init_local(&cursor, &uc); |
| 416 |
if (ret < 0) { |
| 417 |
|
| 418 |
#if DEBUG |
| 419 |
fprintf(stderr, "WARNING: unw_init_local did not succeed, switching to python profiling mode \n"); |
| 420 |
#endif |
| 421 |
vmp_native_disable(); |
| 422 |
return vmp_walk_and_record_python_stack_only(frame, result, max_depth, 0, pc); |
| 423 |
} |
| 424 |
|
| 425 |
if (signal < 0) { |
| 426 |
while (signal < 0) { |
| 427 |
int err = unw_step(&cursor); |
| 428 |
if (err <= 0) { |
| 429 |
#if DEBUG |
| 430 |
fprintf(stderr, "WARNING: did not find signal frame, skipping sample\n"); |
| 431 |
#endif |
| 432 |
return 0; |
| 433 |
} |
| 434 |
signal++; |
| 435 |
} |
| 436 |
} else { |
| 437 |
#ifdef VMPROF_LINUX |
| 438 |
while (signal) { |
| 439 |
int is_signal_frame = unw_is_signal_frame(&cursor); |
| 440 |
if (is_signal_frame) { |
| 441 |
unw_step(&cursor); |
| 442 |
break; |
| 443 |
} |
| 444 |
int err = unw_step(&cursor); |
| 445 |
if (err <= 0) { |
| 446 |
#if DEBUG |
| 447 |
fprintf(stderr,"WARNING: did not find signal frame, skipping sample\n"); |
| 448 |
#endif |
| 449 |
return 0; |
| 450 |
} |
| 451 |
} |
| 452 |
#else |
| 453 |
|
| 454 |
if (signal) { |
| 455 |
unw_step(&cursor); |
| 456 |
|
| 457 |
unw_step(&cursor); |
| 458 |
unw_step(&cursor); |
| 459 |
unw_step(&cursor); |
| 460 |
} |
| 461 |
#endif |
| 462 |
} |
| 463 |
|
| 464 |
int depth = 0; |
| 465 |
|
| 466 |
while ((depth + _per_loop()) <= max_depth) { |
| 467 |
unw_get_proc_info(&cursor, &pip); |
| 468 |
|
| 469 |
func_addr = (void*)pip.start_ip; |
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
#ifdef PYPY_JIT_CODEMAP |
| 488 |
long start_addr = 0; |
| 489 |
unw_word_t rip = 0; |
| 490 |
if (unw_get_reg(&cursor, UNW_REG_IP, &rip) < 0) { |
| 491 |
return 0; |
| 492 |
} |
| 493 |
#endif |
| 494 |
|
| 495 |
if (IS_VMPROF_EVAL((void*)pip.start_ip)) { |
| 496 |
|
| 497 |
return vmp_walk_and_record_python_stack_only(frame, result, max_depth, depth, pc); |
| 498 |
#ifdef PYPY_JIT_CODEMAP |
| 499 |
} else if (pypy_find_codemap_at_addr(rip, &start_addr) != NULL) { |
| 500 |
depth = vmprof_write_header_for_jit_addr((intptr_t*)result, depth, pc, max_depth); |
| 501 |
return vmp_walk_and_record_python_stack_only(frame, result, max_depth, depth, pc); |
| 502 |
#endif |
| 503 |
} else { |
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
if (func_addr != 0x0) { |
| 508 |
depth = _write_native_stack((void*)(((uint64_t)func_addr) | 0x1), result, depth, max_depth); |
| 509 |
} |
| 510 |
} |
| 511 |
|
| 512 |
int err = unw_step(&cursor); |
| 513 |
if (err == 0) { |
| 514 |
break; |
| 515 |
} else if (err < 0) { |
| 516 |
|
| 517 |
return vmp_walk_and_record_python_stack_only(frame, result, max_depth, 0, pc); |
| 518 |
} |
| 519 |
} |
| 520 |
|
| 521 |
|
| 522 |
#endif |
| 523 |
return vmp_walk_and_record_python_stack_only(frame, result, max_depth, 0, pc); |
| 524 |
} |
| 525 |
#endif |
| 526 |
|
| 527 |
int vmp_native_enabled(void) { |
| 528 |
#ifdef VMP_SUPPORTS_NATIVE_PROFILING |
| 529 |
return vmp_native_traces_enabled; |
| 530 |
#else |
| 531 |
return 0; |
| 532 |
#endif |
| 533 |
} |
| 534 |
|
| 535 |
#ifdef VMP_SUPPORTS_NATIVE_PROFILING |
| 536 |
int _ignore_symbols_from_path(const char * name) { |
| 537 |
|
| 538 |
|
| 539 |
#ifdef RPYTHON_VMPROF |
| 540 |
if (strstr(name, "libpypy-c.so") != NULL |
| 541 |
|| strstr(name, "pypy-c") != NULL) { |
| 542 |
return 1; |
| 543 |
} |
| 544 |
#else |
| 545 |
|
| 546 |
if (strstr(name, "python") != NULL && |
| 547 |
# ifdef __unix__ |
| 548 |
strstr(name, ".so\n") == NULL |
| 549 |
# elif defined(__APPLE__) |
| 550 |
strstr(name, ".so") == NULL |
| 551 |
# endif |
| 552 |
) { |
| 553 |
return 1; |
| 554 |
} |
| 555 |
#endif |
| 556 |
return 0; |
| 557 |
} |
| 558 |
|
| 559 |
int _reset_vmp_ranges(void) { |
| 560 |
|
| 561 |
int max_count = 10; |
| 562 |
vmp_range_count = 0; |
| 563 |
if (vmp_ranges != NULL) { free(vmp_ranges); } |
| 564 |
vmp_ranges = malloc(max_count * sizeof(intptr_t)); |
| 565 |
return max_count; |
| 566 |
} |
| 567 |
|
| 568 |
|
| 569 |
int _resize_ranges(intptr_t ** cursor, int max_count) { |
| 570 |
ptrdiff_t diff = (*cursor - vmp_ranges); |
| 571 |
if (diff + 2 > max_count) { |
| 572 |
max_count *= 2; |
| 573 |
vmp_ranges = realloc(vmp_ranges, max_count*sizeof(intptr_t)); |
| 574 |
*cursor = vmp_ranges + diff; |
| 575 |
} |
| 576 |
return max_count; |
| 577 |
} |
| 578 |
|
| 579 |
intptr_t * _add_to_range(intptr_t * cursor, intptr_t start, intptr_t end) { |
| 580 |
if (cursor[0] == start) { |
| 581 |
|
| 582 |
|
| 583 |
cursor[0] = end; |
| 584 |
} else { |
| 585 |
if (cursor != vmp_ranges) { |
| 586 |
|
| 587 |
cursor++; |
| 588 |
} |
| 589 |
cursor[0] = start; |
| 590 |
cursor[1] = end; |
| 591 |
vmp_range_count += 2; |
| 592 |
cursor++; |
| 593 |
} |
| 594 |
return cursor; |
| 595 |
} |
| 596 |
|
| 597 |
#ifdef __unix__ |
| 598 |
int vmp_read_vmaps(const char * fname) { |
| 599 |
|
| 600 |
FILE * fd = fopen(fname, "rb"); |
| 601 |
if (fd == NULL) { |
| 602 |
return 0; |
| 603 |
} |
| 604 |
char * saveptr = NULL; |
| 605 |
char * line = NULL; |
| 606 |
char * he = NULL; |
| 607 |
char * name; |
| 608 |
char *start_hex = NULL, *end_hex = NULL; |
| 609 |
size_t n = 0; |
| 610 |
ssize_t size; |
| 611 |
intptr_t start, end; |
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
int max_count = _reset_vmp_ranges(); |
| 621 |
intptr_t * cursor = vmp_ranges; |
| 622 |
cursor[0] = -1; |
| 623 |
while ((size = getline(&line, &n, fd)) >= 0) { |
| 624 |
assert(line != NULL); |
| 625 |
start_hex = strtok_r(line, "-", &saveptr); |
| 626 |
if (start_hex == NULL) { continue; } |
| 627 |
start = strtoll(start_hex, &he, 16); |
| 628 |
end_hex = strtok_r(NULL, " ", &saveptr); |
| 629 |
if (end_hex == NULL) { continue; } |
| 630 |
end = strtoll(end_hex, &he, 16); |
| 631 |
|
| 632 |
strtok_r(NULL, " ", &saveptr); |
| 633 |
strtok_r(NULL, " ", &saveptr); |
| 634 |
strtok_r(NULL, " ", &saveptr); |
| 635 |
strtok_r(NULL, " ", &saveptr); |
| 636 |
|
| 637 |
name = saveptr; |
| 638 |
if (_ignore_symbols_from_path(name)) { |
| 639 |
max_count = _resize_ranges(&cursor, max_count); |
| 640 |
cursor = _add_to_range(cursor, start, end); |
| 641 |
} |
| 642 |
free(line); |
| 643 |
line = NULL; |
| 644 |
n = 0; |
| 645 |
} |
| 646 |
|
| 647 |
fclose(fd); |
| 648 |
return 1; |
| 649 |
} |
| 650 |
#endif |
| 651 |
|
| 652 |
#ifdef __APPLE__ |
| 653 |
int vmp_read_vmaps(const char * fname) { |
| 654 |
kern_return_t kr; |
| 655 |
task_t task; |
| 656 |
mach_vm_address_t addr; |
| 657 |
mach_vm_size_t vmsize; |
| 658 |
vm_region_top_info_data_t topinfo; |
| 659 |
mach_msg_type_number_t count; |
| 660 |
memory_object_name_t obj; |
| 661 |
int ret = 0; |
| 662 |
pid_t pid; |
| 663 |
|
| 664 |
pid = getpid(); |
| 665 |
kr = task_for_pid(mach_task_self(), pid, &task); |
| 666 |
if (kr != KERN_SUCCESS) { |
| 667 |
goto teardown; |
| 668 |
} |
| 669 |
|
| 670 |
addr = 0; |
| 671 |
int max_count = _reset_vmp_ranges(); |
| 672 |
intptr_t * cursor = vmp_ranges; |
| 673 |
cursor[0] = -1; |
| 674 |
|
| 675 |
do { |
| 676 |
|
| 677 |
count = VM_REGION_TOP_INFO_COUNT; |
| 678 |
vmsize = 0; |
| 679 |
kr = mach_vm_region(task, &addr, &vmsize, VM_REGION_TOP_INFO, |
| 680 |
(vm_region_info_t)&topinfo, &count, &obj); |
| 681 |
if (kr == KERN_SUCCESS) { |
| 682 |
vm_address_t start = (vm_address_t)addr, end = (vm_address_t)(addr + vmsize); |
| 683 |
|
| 684 |
Dl_info info; |
| 685 |
if (dladdr((const void*)start, &info) == 0) { |
| 686 |
|
| 687 |
addr += vmsize; |
| 688 |
continue; |
| 689 |
} |
| 690 |
if (_ignore_symbols_from_path(info.dli_fname)) { |
| 691 |
|
| 692 |
max_count = _resize_ranges(&cursor, max_count); |
| 693 |
cursor = _add_to_range(cursor, start, end); |
| 694 |
} |
| 695 |
addr = addr + vmsize; |
| 696 |
} else if (kr != KERN_INVALID_ADDRESS) { |
| 697 |
goto teardown; |
| 698 |
} |
| 699 |
} while (kr == KERN_SUCCESS); |
| 700 |
|
| 701 |
ret = 1; |
| 702 |
|
| 703 |
teardown: |
| 704 |
if (task != MACH_PORT_NULL) { |
| 705 |
mach_port_deallocate(mach_task_self(), task); |
| 706 |
} |
| 707 |
return ret; |
| 708 |
} |
| 709 |
#endif |
| 710 |
|
| 711 |
static const char * vmprof_error = NULL; |
| 712 |
static void * libhandle = NULL; |
| 713 |
|
| 714 |
#ifdef VMPROF_LINUX |
| 715 |
#include <link.h> |
| 716 |
#define LIBUNWIND "libunwind.so" |
| 717 |
#ifdef __i386__ |
| 718 |
#define PREFIX "x86" |
| 719 |
#define LIBUNWIND_SUFFIX "" |
| 720 |
#elif __x86_64__ |
| 721 |
#define PREFIX "x86_64" |
| 722 |
#define LIBUNWIND_SUFFIX "-x86_64" |
| 723 |
#elif __powerpc64__ |
| 724 |
#define PREFIX "ppc64" |
| 725 |
#define LIBUNWIND_SUFFIX "-ppc64" |
| 726 |
#endif |
| 727 |
#define U_PREFIX "_U" |
| 728 |
#define UL_PREFIX "_UL" |
| 729 |
#endif |
| 730 |
|
| 731 |
int vmp_native_enable(void) { |
| 732 |
#ifdef VMPROF_LINUX |
| 733 |
void * oldhandle = NULL; |
| 734 |
struct link_map * map = NULL; |
| 735 |
if (libhandle == NULL) { |
| 736 |
|
| 737 |
libhandle = dlopen(NULL, RTLD_NOW); |
| 738 |
if (libhandle != NULL) { |
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
if (dlinfo(libhandle, RTLD_DI_LINKMAP, &map) != 0) { |
| 743 |
(void)dlclose(libhandle); |
| 744 |
libhandle = NULL; |
| 745 |
goto bail_out; |
| 746 |
} |
| 747 |
|
| 748 |
do { |
| 749 |
if (strstr(map->l_name, ".libs_vmprof/libunwind" LIBUNWIND_SUFFIX) != NULL) { |
| 750 |
oldhandle = libhandle; |
| 751 |
libhandle = dlopen(map->l_name, RTLD_LAZY|RTLD_LOCAL); |
| 752 |
(void)dlclose(oldhandle); |
| 753 |
oldhandle = NULL; |
| 754 |
goto loaded_libunwind; |
| 755 |
} |
| 756 |
map = map->l_next; |
| 757 |
} while (map != NULL); |
| 758 |
|
| 759 |
(void)dlclose(libhandle); |
| 760 |
libhandle = NULL; |
| 761 |
} |
| 762 |
|
| 763 |
|
| 764 |
if ((libhandle = dlopen(LIBUNWIND, RTLD_LAZY | RTLD_LOCAL)) == NULL) { |
| 765 |
goto bail_out; |
| 766 |
} |
| 767 |
loaded_libunwind: |
| 768 |
if ((unw_get_reg = dlsym(libhandle, UL_PREFIX PREFIX "_get_reg")) == NULL) { |
| 769 |
goto bail_out; |
| 770 |
} |
| 771 |
if ((unw_get_proc_info = dlsym(libhandle, UL_PREFIX PREFIX "_get_proc_info")) == NULL){ |
| 772 |
goto bail_out; |
| 773 |
} |
| 774 |
if ((unw_get_proc_name = dlsym(libhandle, UL_PREFIX PREFIX "_get_proc_name")) == NULL){ |
| 775 |
goto bail_out; |
| 776 |
} |
| 777 |
if ((unw_init_local = dlsym(libhandle, UL_PREFIX PREFIX "_init_local")) == NULL) { |
| 778 |
goto bail_out; |
| 779 |
} |
| 780 |
if ((unw_step = dlsym(libhandle, UL_PREFIX PREFIX "_step")) == NULL) { |
| 781 |
goto bail_out; |
| 782 |
} |
| 783 |
if ((unw_is_signal_frame = dlsym(libhandle, UL_PREFIX PREFIX "_is_signal_frame")) == NULL) { |
| 784 |
goto bail_out; |
| 785 |
} |
| 786 |
#if __powerpc64__ |
| 787 |
|
| 788 |
#define U_PREFIX |
| 789 |
#define PREFIX |
| 790 |
#define USCORE |
| 791 |
#else |
| 792 |
#define USCORE "_" |
| 793 |
#endif |
| 794 |
if ((unw_getcontext = dlsym(libhandle, U_PREFIX PREFIX USCORE "getcontext")) == NULL) { |
| 795 |
goto bail_out; |
| 796 |
} |
| 797 |
} |
| 798 |
#endif |
| 799 |
|
| 800 |
vmp_native_traces_enabled = 1; |
| 801 |
return 1; |
| 802 |
|
| 803 |
#ifdef VMPROF_LINUX |
| 804 |
bail_out: |
| 805 |
vmprof_error = dlerror(); |
| 806 |
fprintf(stderr, "could not load libunwind at runtime. error: %s\n", vmprof_error); |
| 807 |
vmp_native_traces_enabled = 0; |
| 808 |
return 0; |
| 809 |
#endif |
| 810 |
} |
| 811 |
|
| 812 |
void vmp_native_disable(void) { |
| 813 |
|
| 814 |
if (libhandle != NULL) { |
| 815 |
if (dlclose(libhandle)) { |
| 816 |
vmprof_error = dlerror(); |
| 817 |
#if DEBUG |
| 818 |
fprintf(stderr, "could not close libunwind at runtime. error: %s\n", vmprof_error); |
| 819 |
#endif |
| 820 |
} |
| 821 |
libhandle = NULL; |
| 822 |
} |
| 823 |
|
| 824 |
vmp_native_traces_enabled = 0; |
| 825 |
if (vmp_ranges != NULL) { |
| 826 |
free(vmp_ranges); |
| 827 |
vmp_ranges = NULL; |
| 828 |
} |
| 829 |
vmp_range_count = 0; |
| 830 |
} |
| 831 |
|
| 832 |
int vmp_ignore_ip(intptr_t ip) { |
| 833 |
if (vmp_range_count == 0) { |
| 834 |
return 0; |
| 835 |
} |
| 836 |
int i = vmp_binary_search_ranges(ip, vmp_ranges, (int)vmp_range_count); |
| 837 |
if (i == -1) { |
| 838 |
return 0; |
| 839 |
} |
| 840 |
|
| 841 |
assert((i & 1) == 0 && "returned index MUST be even"); |
| 842 |
|
| 843 |
intptr_t v = vmp_ranges[i]; |
| 844 |
intptr_t v2 = vmp_ranges[i+1]; |
| 845 |
return v <= ip && ip <= v2; |
| 846 |
} |
| 847 |
|
| 848 |
int vmp_binary_search_ranges(intptr_t ip, intptr_t * l, int count) { |
| 849 |
intptr_t * r = l + count; |
| 850 |
intptr_t * ol = l; |
| 851 |
intptr_t * or = r-1; |
| 852 |
while (1) { |
| 853 |
ptrdiff_t i = (r-l)/2; |
| 854 |
if (i == 0) { |
| 855 |
if (l == ol && *l > ip) { |
| 856 |
|
| 857 |
return -1; |
| 858 |
} else if (l == or && *l < ip) { |
| 859 |
|
| 860 |
return -1; |
| 861 |
} else { |
| 862 |
|
| 863 |
i = l - ol; |
| 864 |
if ((i & 1) == 1) { |
| 865 |
return (int)i-1; |
| 866 |
} |
| 867 |
return (int)i; |
| 868 |
} |
| 869 |
} |
| 870 |
intptr_t * m = l + i; |
| 871 |
if (ip < *m) { |
| 872 |
r = m; |
| 873 |
} else { |
| 874 |
l = m; |
| 875 |
} |
| 876 |
} |
| 877 |
return -1; |
| 878 |
} |
| 879 |
|
| 880 |
int vmp_ignore_symbol_count(void) { |
| 881 |
return (int)vmp_range_count; |
| 882 |
} |
| 883 |
|
| 884 |
intptr_t * vmp_ignore_symbols(void) { |
| 885 |
return vmp_ranges; |
| 886 |
} |
| 887 |
|
| 888 |
void vmp_set_ignore_symbols(intptr_t * symbols, int count) { |
| 889 |
vmp_ranges = symbols; |
| 890 |
vmp_range_count = count; |
| 891 |
} |
| 892 |
#endif |