6666#ifdef PY_HAVE_PERF_TRAMPOLINE
6767
6868/* Standard library includes for perf jitdump implementation */
69- #include <elf.h> // ELF architecture constants
69+ #if defined(__linux__ )
70+ # include <elf.h> // ELF architecture constants
71+ #endif
7072#include <fcntl.h> // File control operations
7173#include <stdio.h> // Standard I/O operations
7274#include <stdlib.h> // Standard library functions
7375#include <sys/mman.h> // Memory mapping functions (mmap)
7476#include <sys/types.h> // System data types
7577#include <unistd.h> // System calls (sysconf, getpid)
7678#include <sys/time.h> // Time functions (gettimeofday)
77- #include <sys/syscall.h> // System call interface
79+ #if defined(__linux__ )
80+ # include <sys/syscall.h> // System call interface
81+ #endif
7882
7983// =============================================================================
8084// CONSTANTS AND CONFIGURATION
102106 */
103107#define PERF_JIT_CODE_PADDING 0x100
104108
109+
110+ /* These constants are defined inside <elf.h>, which we can't use outside of linux. */
111+ #if !defined(__linux__ )
112+ # define EM_386 3
113+ # define EM_ARM 40
114+ # define EM_AARCH64 183
115+ # define EM_RISCV 243
116+ #endif
117+
105118/* Convenient access to the global trampoline API state */
106119#define trampoline_api _PyRuntime.ceval.perf.trampoline_api
107120
@@ -195,7 +208,7 @@ struct BaseEvent {
195208typedef struct {
196209 struct BaseEvent base ; // Common event header
197210 uint32_t process_id ; // Process ID where code was generated
198- uint32_t thread_id ; // Thread ID where code was generated
211+ uint64_t thread_id ; // Thread ID where code was generated
199212 uint64_t vma ; // Virtual memory address where code is loaded
200213 uint64_t code_address ; // Address of the actual machine code
201214 uint64_t code_size ; // Size of the machine code in bytes
@@ -1166,7 +1179,11 @@ static void perf_map_jit_write_entry(void *state, const void *code_addr,
11661179 ev .base .size = sizeof (ev ) + (name_length + 1 ) + size ;
11671180 ev .base .time_stamp = get_current_monotonic_ticks ();
11681181 ev .process_id = getpid ();
1182+ #if defined(__APPLE__ )
1183+ pthread_threadid_np (NULL , & ev .thread_id );
1184+ #else
11691185 ev .thread_id = syscall (SYS_gettid ); // Get thread ID via system call
1186+ #endif
11701187 ev .vma = base ; // Virtual memory address
11711188 ev .code_address = base ; // Same as VMA for our use case
11721189 ev .code_size = size ;
@@ -1262,4 +1279,4 @@ _PyPerf_Callbacks _Py_perfmap_jit_callbacks = {
12621279 & perf_map_jit_fini , // Cleanup function
12631280};
12641281
1265- #endif /* PY_HAVE_PERF_TRAMPOLINE */
1282+ #endif /* PY_HAVE_PERF_TRAMPOLINE */
0 commit comments