6#if defined(DART_HOST_OS_MACOS)
11#include <CoreFoundation/CoreFoundation.h>
14#include <crt_externs.h>
18#include <mach-o/dyld.h>
21#include <sys/resource.h>
22#include <sys/sysctl.h>
24#include <sys/utsname.h>
36const char* Platform::executable_name_ =
nullptr;
37int Platform::script_index_ = 1;
38char** Platform::argv_ =
nullptr;
40static const char* strcode(
int si_signo,
int si_code) {
41#define CASE(signo, code) \
42 if (si_signo == signo && si_code == code) return #code;
44 CASE(SIGILL, ILL_ILLOPC);
45 CASE(SIGILL, ILL_ILLOPN);
46 CASE(SIGILL, ILL_ILLADR);
47 CASE(SIGILL, ILL_ILLTRP);
48 CASE(SIGILL, ILL_PRVOPC);
49 CASE(SIGILL, ILL_PRVREG);
50 CASE(SIGILL, ILL_COPROC);
51 CASE(SIGILL, ILL_BADSTK);
52 CASE(SIGSEGV, SEGV_MAPERR);
53 CASE(SIGSEGV, SEGV_ACCERR);
54 CASE(SIGBUS, BUS_ADRALN);
55 CASE(SIGBUS, BUS_ADRERR);
56 CASE(SIGBUS, BUS_OBJERR);
57 CASE(SIGTRAP, TRAP_BRKPT);
58 CASE(SIGTRAP, TRAP_TRACE);
63static void segv_handler(
int signal, siginfo_t* siginfo,
void* context) {
65 "\n===== CRASH =====\n"
66 "si_signo=%s(%d), si_code=%s(%d), si_addr=%p\n",
67 strsignal(siginfo->si_signo), siginfo->si_signo,
68 strcode(siginfo->si_signo, siginfo->si_code), siginfo->si_code,
79 struct sigaction act = {};
80 act.sa_handler = SIG_IGN;
81 if (sigaction(SIGPIPE, &act,
nullptr) != 0) {
82 perror(
"Setting signal handler failed");
90 sigemptyset(&signal_mask);
91 sigaddset(&signal_mask, SIGTTOU);
92 if (sigprocmask(SIG_BLOCK, &signal_mask,
nullptr) < 0) {
93 perror(
"Setting signal handler failed");
97 act.sa_flags = SA_SIGINFO;
98 act.sa_sigaction = &segv_handler;
99 if (sigemptyset(&act.sa_mask) != 0) {
100 perror(
"sigemptyset() failed.");
103 if (sigaddset(&act.sa_mask, SIGPROF) != 0) {
104 perror(
"sigaddset() failed");
107 if (sigaction(SIGSEGV, &act,
nullptr) != 0) {
108 perror(
"sigaction() failed.");
111 if (sigaction(SIGBUS, &act,
nullptr) != 0) {
112 perror(
"sigaction() failed.");
115 if (sigaction(SIGTRAP, &act,
nullptr) != 0) {
116 perror(
"sigaction() failed.");
119 if (sigaction(SIGILL, &act,
nullptr) != 0) {
120 perror(
"sigaction() failed.");
128 size_t cpus_length =
sizeof(cpus);
129 if (sysctlbyname(
"hw.logicalcpu", &cpus, &cpus_length,
nullptr, 0) == 0) {
133 return sysconf(_SC_NPROCESSORS_ONLN);
150static const char* GetLocaleName() {
151 CFLocaleRef locale = CFLocaleCopyCurrent();
152 CFStringRef locale_string = CFLocaleGetIdentifier(locale);
153 CFIndex
len = CFStringGetLength(locale_string);
155 CFStringGetMaximumSizeForEncoding(
len, kCFStringEncodingUTF8) + 1;
159 CFStringGetCString(locale_string,
result, max_len, kCFStringEncodingUTF8);
167static const char* GetPreferredLanguageName() {
168 CFArrayRef languages = CFLocaleCopyPreferredLanguages();
169 CFIndex languages_length = CFArrayGetCount(languages);
170 if (languages_length < 1) {
171 CFRelease(languages);
175 reinterpret_cast<CFTypeRef
>(CFArrayGetValueAtIndex(languages, 0));
176 CFTypeID item_type = CFGetTypeID(item);
177 ASSERT(item_type == CFStringGetTypeID());
178 CFStringRef language =
reinterpret_cast<CFStringRef
>(item);
179 CFIndex
len = CFStringGetLength(language);
181 CFStringGetMaximumSizeForEncoding(
len, kCFStringEncodingUTF8) + 1;
185 CFStringGetCString(language,
result, max_len, kCFStringEncodingUTF8);
186 CFRelease(languages);
196 const char* preferred_language = GetPreferredLanguageName();
197 return (preferred_language !=
nullptr) ? preferred_language : GetLocaleName();
201 return gethostname(
buffer, buffer_length) == 0;
221 char** environ = *(_NSGetEnviron());
223 char** tmp = environ;
224 while (*(tmp++) !=
nullptr) {
230 for (intptr_t current = 0; current <
i; current++) {
231 result[current] = environ[current];
238 return executable_name_;
243 uint32_t path_size = 0;
244 if (_NSGetExecutablePath(
nullptr, &path_size) == 0) {
249 if (_NSGetExecutablePath(
path, &path_size) != 0) {
259 uint32_t path_size = 0;
260 if (_NSGetExecutablePath(
nullptr, &path_size) == 0) {
263 if (path_size > result_size) {
266 if (_NSGetExecutablePath(
result, &path_size) != 0) {
273 pthread_setname_np(
name);
275#if !defined(DART_HOST_OS_IOS)
279 class ScopedDLHandle :
public ValueObject {
281 explicit ScopedDLHandle(
void* handle) :
handle_(handle) {}
292 class ScopedCFStringRef :
public ValueObject {
294 explicit ScopedCFStringRef(
const char*
s)
295 : ref_(CFStringCreateWithCString(nullptr, (
s), kCFStringEncodingUTF8)) {
297 ~ScopedCFStringRef() {
298 if (ref_ !=
nullptr) CFRelease(ref_);
300 CFStringRef
get() {
return ref_; }
307 ScopedDLHandle application_services_handle(
308 dlopen(
"/System/Library/Frameworks/ApplicationServices.framework/"
309 "Versions/A/ApplicationServices",
310 RTLD_LAZY | RTLD_LOCAL));
311 if (application_services_handle.get() ==
nullptr)
return;
313 ScopedCFStringRef launch_services_bundle_name(
"com.apple.LaunchServices");
314 CFBundleRef launch_services_bundle =
315 CFBundleGetBundleWithIdentifier(launch_services_bundle_name.get());
316 if (launch_services_bundle ==
nullptr)
return;
318#define GET_FUNC(name, cstr) \
319 ScopedCFStringRef name##_id(cstr); \
320 *reinterpret_cast<void**>(&name) = CFBundleGetFunctionPointerForName( \
321 launch_services_bundle, name##_id.get()); \
322 if (name == nullptr) return;
324#define GET_DATA(name, cstr) \
325 ScopedCFStringRef name##_id(cstr); \
326 *reinterpret_cast<void**>(&name) = \
327 CFBundleGetDataPointerForName(launch_services_bundle, name##_id.get()); \
328 if (name == nullptr) return;
330 CFTypeRef (*_LSGetCurrentApplicationASN)(void);
331 GET_FUNC(_LSGetCurrentApplicationASN,
"_LSGetCurrentApplicationASN");
333 OSStatus (*_LSSetApplicationInformationItem)(
int, CFTypeRef, CFStringRef,
334 CFStringRef, CFDictionaryRef*);
335 GET_FUNC(_LSSetApplicationInformationItem,
336 "_LSSetApplicationInformationItem");
338 CFDictionaryRef (*_LSApplicationCheckIn)(
int, CFDictionaryRef);
339 GET_FUNC(_LSApplicationCheckIn,
"_LSApplicationCheckIn");
341 void (*_LSSetApplicationLaunchServicesServerConnectionStatus)(uint64_t,
343 GET_FUNC(_LSSetApplicationLaunchServicesServerConnectionStatus,
344 "_LSSetApplicationLaunchServicesServerConnectionStatus");
346 CFStringRef* _kLSDisplayNameKey;
347 GET_DATA(_kLSDisplayNameKey,
"_kLSDisplayNameKey");
348 if (*_kLSDisplayNameKey ==
nullptr)
return;
350 _LSSetApplicationLaunchServicesServerConnectionStatus(0,
nullptr);
352 _LSApplicationCheckIn(-2, CFBundleGetInfoDictionary(CFBundleGetMainBundle()));
355 asn = _LSGetCurrentApplicationASN();
356 if (asn ==
nullptr)
return;
358 ScopedCFStringRef cf_name(
name);
359 _LSSetApplicationInformationItem(-2, asn, *_kLSDisplayNameKey, cf_name.get(),
379 rlimit limit = {
static_cast<rlim_t
>(
value),
static_cast<rlim_t
>(
value)};
380 setrlimit(RLIMIT_CORE, &limit);
static void PrintErr(const char *format,...) PRINTF_ATTRIBUTE(1
static void RestoreConfig()
static char * ScopedCopyCString(const char *str)
static char * ScopedCString(intptr_t length)
static const char * GetCanonicalPath(Namespace *namespc, const char *path, char *dest=nullptr, int dest_size=0)
#define CASE(Arity, Mask, Name, Args, Result)
std::string NSProcessInfoOperatingSystemVersionString()
DART_EXPORT void Dart_PrepareToAbort()
DART_EXPORT uint8_t * Dart_ScopeAllocate(intptr_t size)
DART_EXPORT void Dart_DumpNativeStackTrace(void *context)
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir path
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace buffer
const myers::Point & get(const myers::Segment &)