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_X86_64 62
114+ # define EM_ARM 40
115+ # define EM_AARCH64 183
116+ # define EM_RISCV 243
117+ #endif
118+
105119/* Convenient access to the global trampoline API state */
106120#define trampoline_api _PyRuntime.ceval.perf.trampoline_api
107121
@@ -195,7 +209,7 @@ struct BaseEvent {
195209typedef struct {
196210 struct BaseEvent base ; // Common event header
197211 uint32_t process_id ; // Process ID where code was generated
198- uint32_t thread_id ; // Thread ID where code was generated
212+ uint64_t thread_id ; // Thread ID where code was generated
199213 uint64_t vma ; // Virtual memory address where code is loaded
200214 uint64_t code_address ; // Address of the actual machine code
201215 uint64_t code_size ; // Size of the machine code in bytes
@@ -1166,7 +1180,11 @@ static void perf_map_jit_write_entry(void *state, const void *code_addr,
11661180 ev .base .size = sizeof (ev ) + (name_length + 1 ) + size ;
11671181 ev .base .time_stamp = get_current_monotonic_ticks ();
11681182 ev .process_id = getpid ();
1183+ #if defined(__APPLE__ )
1184+ pthread_threadid_np (NULL , & ev .thread_id );
1185+ #else
11691186 ev .thread_id = syscall (SYS_gettid ); // Get thread ID via system call
1187+ #endif
11701188 ev .vma = base ; // Virtual memory address
11711189 ev .code_address = base ; // Same as VMA for our use case
11721190 ev .code_size = size ;
@@ -1262,4 +1280,4 @@ _PyPerf_Callbacks _Py_perfmap_jit_callbacks = {
12621280 & perf_map_jit_fini , // Cleanup function
12631281};
12641282
1265- #endif /* PY_HAVE_PERF_TRAMPOLINE */
1283+ #endif /* PY_HAVE_PERF_TRAMPOLINE */
0 commit comments