23#include "flutter/runtime/ptrace_check.h"
25#if TRACING_CHECKS_NECESSARY
27#include <sys/sysctl.h>
32#include "flutter/fml/build_config.h"
36static_assert(FML_OS_IOS,
"This translation unit is iOS specific.");
37static_assert(FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG,
38 "This translation unit must only be compiled in the debug "
40 "contains private API usage.");
44extern "C" int ptrace(
int request, pid_t pid, caddr_t
addr,
int data);
48static bool IsLaunchedByFlutterCLI(
const Settings& vm_settings) {
53 return vm_settings.enable_checked_mode;
56static bool IsLaunchedByXcode() {
59 const pid_t
self = getpid();
60 int mib[5] = {CTL_KERN, KERN_PROC, KERN_PROC_PID,
self, 0};
62 auto proc = std::make_unique<struct kinfo_proc>();
63 size_t proc_size =
sizeof(
struct kinfo_proc);
64 if (::sysctl(mib, 4, proc.get(), &proc_size,
nullptr, 0) < 0) {
65 FML_LOG(
ERROR) <<
"Could not execute sysctl() to get current process info: "
70 return proc->kp_proc.p_flag & P_TRACED;
73static bool EnableTracingManually(
const Settings& vm_settings) {
74 if (::ptrace(PT_TRACE_ME, 0,
nullptr, 0) == -1) {
75 FML_LOG(
ERROR) <<
"Could not call ptrace(PT_TRACE_ME): " << strerror(errno);
80 if (::ptrace(PT_SIGEXC, 0,
nullptr, 0) == -1) {
81 FML_LOG(
ERROR) <<
"Could not call ptrace(PT_SIGEXC): " << strerror(errno);
90 size_t maxproc_size =
sizeof(size_t);
91 const int sysctl_result =
92 ::sysctlbyname(
"kern.maxproc", &maxproc, &maxproc_size,
nullptr, 0);
93 if (sysctl_result < 0) {
95 <<
"Could not execute sysctl() to determine process count limit: "
100 const char* warning =
101 "Launching a debug-mode app from the home screen may cause problems.\n"
102 "Please compile a profile-/release-build, launch your app via \"flutter "
103 "run\", or see https://github.com/flutter/flutter/wiki/"
104 "PID-leak-in-iOS-debug-builds-launched-from-home-screen for details.";
106 if (vm_settings.verbose_logging
109 || getpid() % (maxproc / 10) == 0)
117static bool EnableTracingIfNecessaryOnce(
const Settings& vm_settings) {
118 if (IsLaunchedByFlutterCLI(vm_settings)) {
122 if (IsLaunchedByXcode()) {
126 return EnableTracingManually(vm_settings);
131bool EnableTracingIfNecessaryImpl(
const Settings& vm_settings) {
132 static std::once_flag tracing_flag;
134 std::call_once(tracing_flag, [&vm_settings]() {
135 sTracingResult = EnableTracingIfNecessaryOnce(vm_settings)
143 return sTracingResult;
#define FML_LOG(severity)
std::shared_ptr< const fml::Mapping > data