Thanks to visit codestin.com
Credit goes to github.com

Skip to content

gh-96143: Allow Linux perf profiler to see Python calls #96123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 50 commits into from
Aug 30, 2022
Merged
Changes from 1 commit
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
2a918e4
Allow Linux perf profiler to see Python calls
pablogsal Aug 7, 2022
cea1420
Add test
pablogsal Aug 20, 2022
4107c53
Update PCbuild/_freeze_module.vcxproj.filters
pablogsal Aug 20, 2022
5e34e66
munmap pages on shutdown, keep FILE open
tiran Aug 21, 2022
a26a850
Fix tests
pablogsal Aug 21, 2022
8170b24
Skip tests if sanitizer is active
pablogsal Aug 21, 2022
9df1c93
Add ARM64 code generated by aarch64-linux-gnu-gcc
tiran Aug 21, 2022
d8f396d
Address review comments
pablogsal Aug 21, 2022
d35c5d7
Secure fopen, use unraisable, continue on error
tiran Aug 22, 2022
2664b12
cleanup resources, set to uninit
tiran Aug 22, 2022
e6c365a
Allow to set custom callbacks
pablogsal Aug 22, 2022
5513fb1
Add comment to asm file
pablogsal Aug 22, 2022
76c7dc0
fixup! Merge pull request #36 from tiran/perf-file
pablogsal Aug 22, 2022
a545b3c
Add comments to the perf_trampoline file and format file
pablogsal Aug 22, 2022
5130c8d
Correct News entry
pablogsal Aug 22, 2022
991366b
Update Lib/test/test_perf_profiler.py
pablogsal Aug 22, 2022
0a0e53d
Rename perf macro
pablogsal Aug 22, 2022
7ea3371
Fix some typos
pablogsal Aug 22, 2022
680db66
Improve perf profiler tests
tiran Aug 22, 2022
1263a29
Add guard for initialization
pablogsal Aug 22, 2022
a42bde5
Add acks
pablogsal Aug 22, 2022
b780d2a
Initialize perf file lazily
pablogsal Aug 22, 2022
04bf416
Address review comments
pablogsal Aug 22, 2022
7558df2
Complain if there is already a evaluator frame when deactivating/acti…
pablogsal Aug 22, 2022
d1ebc88
Fix some errors on CI
pablogsal Aug 22, 2022
a83a31b
Reorder arguments to speed up trampoline
tiran Aug 22, 2022
0febd84
Preserve frame pointer
pablogsal Aug 22, 2022
dc5a6a5
Support perf backend and better handle forks
pablogsal Aug 22, 2022
be72b92
Fix more fork problems
pablogsal Aug 22, 2022
b5739f4
Update Lib/test/test_perf_profiler.py
pablogsal Aug 22, 2022
04c0c14
Handle missing backends
pablogsal Aug 22, 2022
e810ce6
Update Lib/test/test_perf_profiler.py
pablogsal Aug 22, 2022
bc8bf4e
clean up perf files
pablogsal Aug 22, 2022
0252845
Update Misc/NEWS.d/next/Core and Builtins/2022-08-20-18-36-40.gh-issu…
pablogsal Aug 22, 2022
264bed7
Test fork support, fix some fork problems and improve test file
pablogsal Aug 23, 2022
a31a498
Add more tests
pablogsal Aug 23, 2022
f591e8d
Update Objects/perf_trampoline.c
pablogsal Aug 23, 2022
0af2a08
make argument mandatory
pablogsal Aug 23, 2022
861ae09
Use struct for perf callbacks
tiran Aug 23, 2022
3058cf0
Rename macro to PY_HAVE_PERF_TRAMPOLINE
tiran Aug 23, 2022
07ee991
Merge pull request #39 from tiran/perf_callback_struct
pablogsal Aug 23, 2022
be612a9
Allow gdb to unwind
pablogsal Aug 23, 2022
f4e3fff
Merge remote-tracking branch 'upstream/main' into perf
pablogsal Aug 25, 2022
c27f8b1
Add docs
pablogsal Aug 25, 2022
e27a2c4
fixup! Add docs
pablogsal Aug 25, 2022
81c7f4b
fixup! fixup! Add docs
pablogsal Aug 25, 2022
ef0650b
Update sys API names in the NEWS entry.
gpshead Aug 29, 2022
d8932d2
Add environment variable
pablogsal Aug 29, 2022
0f303ff
Merge branch 'main' into perf
pablogsal Aug 29, 2022
e3f846e
Document the env var and the -X option
pablogsal Aug 29, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Secure fopen, use unraisable, continue on error
  • Loading branch information
tiran authored and pablogsal committed Aug 23, 2022
commit d35c5d701429f89165a4cfa7fa80f5f0cb7544ab
53 changes: 42 additions & 11 deletions Objects/perf_trampoline.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#ifdef HAVE_PERF_TRAMPOLINE

#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
Expand All @@ -29,6 +30,12 @@ struct code_arena_st {

typedef struct code_arena_st code_arena_t;

typedef enum {
PERF_STATUS_FAILED = -1,
PERF_STATUS_NO_INIT = 0,
} perf_status_t;

static perf_status_t perf_status = PERF_STATUS_NO_INIT;
static Py_ssize_t extra_code_index = -1;
static code_arena_t *code_arena;
static FILE *perf_map_file;
Expand All @@ -46,7 +53,10 @@ new_code_arena(void)
-1, // fd (not used here)
0); // offset (not used here)
if (!memory) {
Py_FatalError("Failed to allocate new code arena");
PyErr_SetFromErrno(PyExc_OSError);
_PyErr_WriteUnraisableMsg(
"Failed to create new mmap for perf trampoline", NULL);
perf_status = PERF_STATUS_FAILED;
return -1;
}
void *start = &_Py_trampoline_func_start;
Expand All @@ -57,12 +67,19 @@ new_code_arena(void)
for (size_t i = 0; i < n_copies; i++) {
memcpy(memory + i * code_size, start, code_size * sizeof(char));
}

mprotect(memory, mem_size, PROT_READ | PROT_EXEC);
// Some systems may prevent us from creating executable code on the fly.
int res = mprotect(memory, mem_size, PROT_READ | PROT_EXEC);
if (res == -1) {
PyErr_SetFromErrno(PyExc_OSError);
_PyErr_WriteUnraisableMsg(
"Failed to set mmap for perf trampoline to PROT_READ | PROT_EXEC", NULL);
}

code_arena_t *new_arena = PyMem_RawCalloc(1, sizeof(code_arena_t));
if (new_arena == NULL) {
Py_FatalError("Failed to allocate new code arena struct");
PyErr_NoMemory();
_PyErr_WriteUnraisableMsg(
"Failed to allocate new code arena struct", NULL);
return -1;
}

Expand Down Expand Up @@ -107,7 +124,6 @@ compile_trampoline(void)
return NULL;
}
}

assert(code_arena->size_left <= code_arena->size);
return code_arena_new_code(code_arena);
}
Expand All @@ -120,11 +136,23 @@ perf_map_get_file(void)
}
char filename[100];
pid_t pid = getpid();
// TODO: %d is incorrect if pid_t is long long
snprintf(filename, sizeof(filename), "/tmp/perf-%d.map", pid);
perf_map_file = fopen(filename, "a");
// Location and file name of perf map is hard-coded in perf tool.
// Use exclusive create flag wit nofollow to prevent symlink attacks.
int flags = O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC;
snprintf(filename, sizeof(filename)-1, "/tmp/perf-%jd.map", (intmax_t)pid);
int fd = open(filename, flags, 0600);
if (fd == -1) {
perf_status = PERF_STATUS_FAILED;
PyErr_SetFromErrnoWithFilename(PyExc_OSError, filename);
_PyErr_WriteUnraisableMsg("Failed to create perf map file", NULL);
return NULL;
}
perf_map_file = fdopen(fd, "w");
if (!perf_map_file) {
_Py_FatalErrorFormat(__func__, "Couldn't open %s: errno(%d)", filename, errno);
perf_status = PERF_STATUS_FAILED;
PyErr_SetFromErrnoWithFilename(PyExc_OSError, filename);
_PyErr_WriteUnraisableMsg("Failed to create perf map file handle", NULL);
close(fd);
return NULL;
}
return perf_map_file;
Expand Down Expand Up @@ -154,20 +182,23 @@ static PyObject *
py_trampoline_evaluator(PyThreadState *ts, _PyInterpreterFrame *frame,
int throw)
{
if (perf_status == PERF_STATUS_FAILED) {
return _PyEval_EvalFrameDefault(ts, frame, throw);
}
PyCodeObject *co = frame->f_code;
py_trampoline f = NULL;
_PyCode_GetExtra((PyObject *)co, extra_code_index, (void **)&f);
if (f == NULL) {
FILE *pfile = perf_map_get_file();
if (pfile == NULL) {
return NULL;
return _PyEval_EvalFrameDefault(ts, frame, throw);
}
if (extra_code_index == -1) {
extra_code_index = _PyEval_RequestCodeExtraIndex(NULL);
}
py_trampoline new_trampoline = compile_trampoline();
if (new_trampoline == NULL) {
return NULL;
return _PyEval_EvalFrameDefault(ts, frame, throw);
}
perf_map_write_entry(pfile, new_trampoline, code_arena->code_size,
PyUnicode_AsUTF8(co->co_qualname),
Expand Down