检测是否正在从Xcode运行Swift应用程序 [英] Detect if Swift app is being run from Xcode
问题描述
我想以编程方式确定iOS应用程序是直接从XCode运行(在模拟器中还是在系留设备上)。
我已经尝试了这里描述的-D DEBUG解决方案,但是当我从Xcode断开连接并重新连接时运行应用程序,它仍然认为它处于调试模式。
我认为我正在寻找的是一个Swift版本的此功能
I would like to programmatically determine if the iOS app is being run directly from XCode (either in the simulator or on a tethered device). I've tried the -D DEBUG solution described here, but when I then disconnect from Xcode and re-run the app, it still thinks it's in debug mode. I think what I'm looking for is a Swift version of this function
#include <assert.h>
#include <stdbool.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/sysctl.h>
static bool AmIBeingDebugged(void)
// Returns true if the current process is being debugged (either
// running under the debugger or has a debugger attached post facto).
{
int junk;
int mib[4];
struct kinfo_proc info;
size_t size;
// Initialize the flags so that, if sysctl fails for some bizarre
// reason, we get a predictable result.
info.kp_proc.p_flag = 0;
// Initialize mib, which tells sysctl the info we want, in this case
// we're looking for information about a specific process ID.
mib[0] = CTL_KERN;
mib[1] = KERN_PROC;
mib[2] = KERN_PROC_PID;
mib[3] = getpid();
// Call sysctl.
size = sizeof(info);
junk = sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, NULL, 0);
assert(junk == 0);
// We're being debugged if the P_TRACED flag is set.
return ( (info.kp_proc.p_flag & P_TRACED) != 0 );
}
推荐答案
澄清:您的C代码(以及下面的Swift版本)检查程序是否在调试器控件下运行
,,如果它是从
Xcode运行的话。可以调试Xcode之外的程序(通过直接调用lldb或
gdb),可以从Xcode运行程序而不调试它
(如果方案设置中的Debug Executable复选框关闭) 。
Clarification: Your C code (and the Swift version below) checks if the program is run under debugger control, not if it's being run from Xcode. One can debug a program outside of Xcode (by calling lldb or gdb directly) and one can run a program from Xcode without debugging it (if the "Debug Executable" checkbox in the scheme setting is off).
您可以简单地保留C函数并从Swift中调用它。
如何从中调用Objective-C代码中给出的配方Swift 也适用于纯C代码。
You could simply keep the C function and call it from Swift. The recipes given in How to call Objective-C code from Swift apply to pure C code as well.
但将代码转换为Swift实际上并不太复杂:
But it is actually not too complicated to translate that code to Swift:
func amIBeingDebugged() -> Bool {
var info = kinfo_proc()
var mib : [Int32] = [CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid()]
var size = strideofValue(info)
let junk = sysctl(&mib, UInt32(mib.count), &info, &size, nil, 0)
assert(junk == 0, "sysctl failed")
return (info.kp_proc.p_flag & P_TRACED) != 0
}
备注:
-
kinfo_proc()
创建完全初始化所有
字段的结构设置为零,因此无需设置info.kp_proc.p_flag = 0
。 - C
int
类型是Int32
是Swift。 -
sizeof(info)
来自C代码必须strideOfValue(info)$ c $在Swift中c>
包含结构填充。使用sizeofValue(info)
上述代码在64位设备的模拟器中始终返回false。这是最困难的部分。
kinfo_proc()
creates a fully initialized structure with all fields set to zero, therefore settinginfo.kp_proc.p_flag = 0
is not necessary.- The C
int
type isInt32
is Swift. sizeof(info)
from the C code has to bestrideOfValue(info)
in Swift to include the structure padding. WithsizeofValue(info)
the above code always returned false in the Simulator for 64-bit devices. This was the most difficult part to figure out.
Swift 3更新(Xcode 8):
strideofValue
且相关功能不再存在,
它们已由<$ c $替换c> MemoryLayout :
strideofValue
and the related functions do not exist anymore,
they have been replaced by MemoryLayout
:
func amIBeingDebugged() -> Bool {
var info = kinfo_proc()
var mib : [Int32] = [CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid()]
var size = MemoryLayout<kinfo_proc>.stride
let junk = sysctl(&mib, UInt32(mib.count), &info, &size, nil, 0)
assert(junk == 0, "sysctl failed")
return (info.kp_proc.p_flag & P_TRACED) != 0
}
这篇关于检测是否正在从Xcode运行Swift应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!