Skip to content
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

fix: Better macOS availability checks #524

Merged
merged 3 commits into from
Apr 21, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
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
9 changes: 3 additions & 6 deletions src/unwinder/sentry_unwinder_libbacktrace.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,15 @@
# 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(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) {
Expand Down