Skip to content

Commit

Permalink
fix: Better macOS availability checks (#524)
Browse files Browse the repository at this point in the history
This should allow building on older macOS versions as well as
running on older versions by fixing the usage of __builtin_available,
and adding a different clock source for older macOS versions.
  • Loading branch information
Swatinem authored Apr 21, 2021
1 parent 1d62fc5 commit b8c8792
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
- Build fixes for PPC and universal macOS.
- Fixes to build using musl libc.
- Correctness fixes around printf and strftime usage.
- Allow building and running on older macOS versions.

**Internal**:

Expand Down
4 changes: 4 additions & 0 deletions src/sentry_boot.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
# include <windows.h>
#endif

#ifndef __has_builtin
# define __has_builtin(x) 0
#endif

#include <sentry.h>
#include <stdbool.h>

Expand Down
8 changes: 4 additions & 4 deletions src/sentry_utils.c
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
// According to http://lua-users.org/lists/lua-l/2016-04/msg00216.html we can
// use `stdtod_l` on all platforms when defining `_GNU_SOURCE`.

#define _GNU_SOURCE
#include "sentry_boot.h"

#include "sentry_utils.h"
#include "sentry_alloc.h"
#include "sentry_core.h"
#include "sentry_string.h"
#include "sentry_sync.h"
#include "sentry_utils.h"
#include <locale.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#ifdef SENTRY_PLATFORM_MACOS
#ifdef SENTRY_PLATFORM_DARWIN
# include <xlocale.h>
#elif defined(SENTRY_PLATFORM_LINUX) && !defined(SENTRY_PLATFORM_ANDROID)
# include "../vendor/stb_sprintf.h"
Expand Down Expand Up @@ -493,7 +493,7 @@ sentry__snprintf_c(char *buf, size_t buf_size, const char *fmt, ...)
rv = _vsnprintf_l(buf, buf_size, fmt, c_locale(), args);
#elif defined(SENTRY_PLATFORM_ANDROID) || defined(SENTRY_PLATFORM_IOS)
rv = vsnprintf(buf, buf_size, fmt, args);
#elif defined(SENTRY_PLATFORM_MACOS)
#elif defined(SENTRY_PLATFORM_DARWIN)
rv = vsnprintf_l(buf, buf_size, c_locale(), fmt, args);
#else
rv = stbsp_vsnprintf(buf, buf_size, fmt, args);
Expand Down
23 changes: 23 additions & 0 deletions src/sentry_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

#include "sentry_boot.h"

#ifdef SENTRY_PLATFORM_DARWIN
# include <mach/clock.h>
# include <mach/mach.h>
#endif
#ifdef SENTRY_PLATFORM_WINDOWS
# include <winnt.h>
#else
Expand Down Expand Up @@ -145,6 +149,25 @@ sentry__monotonic_time(void)
LARGE_INTEGER qpc_counter;
QueryPerformanceCounter(&qpc_counter);
return qpc_counter.QuadPart * 1000 / qpc_frequency.QuadPart;
#elif defined(SENTRY_PLATFORM_DARWIN)

// try `clock_gettime` first if available,
// fall back to `host_get_clock_service` otherwise
# if defined(MAC_OS_X_VERSION_10_12) && __has_builtin(__builtin_available)
if (__builtin_available(macOS 10.12, *)) {
struct timespec tv;
return (clock_gettime(CLOCK_MONOTONIC, &tv) == 0)
? (uint64_t)tv.tv_sec * 1000 + tv.tv_nsec / 1000000
: 0;
}
# endif

clock_serv_t cclock;
mach_timespec_t mts;
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
clock_get_time(cclock, &mts);
mach_port_deallocate(mach_task_self(), cclock);
return (uint64_t)mts.tv_sec * 1000 + mts.tv_nsec / 1000000;
#else
struct timespec tv;
return (clock_gettime(CLOCK_MONOTONIC, &tv) == 0)
Expand Down
14 changes: 6 additions & 8 deletions src/unwinder/sentry_unwinder_libbacktrace.c
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
#include "sentry_boot.h"

#if defined(SENTRY_PLATFORM_MACOS) || defined(__GLIBC__)
#if defined(SENTRY_PLATFORM_DARWIN) || defined(__GLIBC__)
# include <execinfo.h>
#endif

#ifndef __has_builtin
# define __has_builtin(x) 0
#endif

size_t
sentry__unwind_stack_libbacktrace(
void *addr, const sentry_ucontext_t *uctx, void **ptrs, size_t max_frames)
{
if (addr) {
#if defined(SENTRY_PLATFORM_MACOS) && __has_builtin(__builtin_available)
if (__builtin_available(macOS 10.14, *))
#if defined(SENTRY_PLATFORM_MACOS) && defined(MAC_OS_X_VERSION_10_14) \
&& __has_builtin(__builtin_available)
if (__builtin_available(macOS 10.14, *)) {
return (size_t)backtrace_from_fp(addr, ptrs, (int)max_frames);
}
#endif
return 0;
} else if (uctx) {
return 0;
}
#if defined(SENTRY_PLATFORM_MACOS) || defined(__GLIBC__)
#if defined(SENTRY_PLATFORM_DARWIN) || defined(__GLIBC__)
return (size_t)backtrace(ptrs, (int)max_frames);
#else
(void)ptrs;
Expand Down

0 comments on commit b8c8792

Please sign in to comment.