Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ if(UNIX AND NOT APPLE)
endif()
endif()

if(APPLE)
check_support(HAS_MACH_VM has_mach_vm.cpp "" "" "")
endif()

# =============================================== Autoconfig unwinding ===============================================
# Unwind back-ends
if(
Expand Down Expand Up @@ -340,6 +344,10 @@ if(HAS_DLADDR1)
target_compile_definitions(${target_name} PUBLIC CPPTRACE_HAS_DLADDR1)
endif()

if(HAS_MACH_VM)
target_compile_definitions(${target_name} PUBLIC HAS_MACH_VM)
endif()

# Symbols
if(CPPTRACE_GET_SYMBOLS_WITH_LIBBACKTRACE)
if(NOT HAS_BACKTRACE)
Expand Down
23 changes: 23 additions & 0 deletions cmake/has_mach_vm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <mach/mach.h>
#include <mach/mach_vm.h>
#include <cstdint>

int main() {
mach_vm_size_t vmsize;
uintptr_t addr = reinterpret_cast<uintptr_t>(&vmsize);
uintptr_t page_addr = addr & ~(4096 - 1);
mach_vm_address_t address = (mach_vm_address_t)page_addr;
vm_region_basic_info_data_t info;
mach_msg_type_number_t info_count =
sizeof(size_t) == 8 ? VM_REGION_BASIC_INFO_COUNT_64 : VM_REGION_BASIC_INFO_COUNT;
memory_object_name_t object;
mach_vm_region(
mach_task_self(),
&address,
&vmsize,
VM_REGION_BASIC_INFO,
(vm_region_info_t)&info,
&info_count,
&object
);
}
17 changes: 15 additions & 2 deletions src/from_current.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
#include <unistd.h>
#if IS_APPLE
#include <mach/mach.h>
#include <mach/mach_vm.h>
#ifdef HAS_MACH_VM
#include <mach/mach_vm.h>
#endif
#else
#include <fstream>
#include <iomanip>
Expand Down Expand Up @@ -112,13 +114,24 @@ namespace cpptrace {
#if IS_APPLE
int get_page_protections(void* page) {
// https://stackoverflow.com/a/12627784/15675011
#ifdef HAS_MACH_VM
mach_vm_size_t vmsize;
mach_vm_address_t address = (mach_vm_address_t)page;
#else
vm_size_t vmsize;
vm_address_t address = (vm_address_t)page;
#endif
vm_region_basic_info_data_t info;
mach_msg_type_number_t info_count =
sizeof(size_t) == 8 ? VM_REGION_BASIC_INFO_COUNT_64 : VM_REGION_BASIC_INFO_COUNT;
memory_object_name_t object;
kern_return_t status = mach_vm_region(
kern_return_t status =
#ifdef HAS_MACH_VM
mach_vm_region
#else
vm_region_64
#endif
(
mach_task_self(),
&address,
&vmsize,
Expand Down
Loading