V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Whyto
V2EX  ›  Xcode

Xcode 14 / 15 Could not attach to pid: attach failed

  •  
  •   Whyto · 308 天前 · 1400 次点击
    这是一个创建于 308 天前的主题,其中的信息可能已经有所发展或是发生改变。
    我参考了该文章 https://juejin.cn/post/7025992880464330766 ,已经关闭了 SIP ,在 iOS simulator 上可以正常使用 Xcode 来 attach 其他应用(如 Maps.app 等),但是在真机上 attach 其他应用时却总是报错(测试了许多不同的第三方应用),如图所示:

    ![1.png]( https://s2.loli.net/2023/06/24/JuYDjfzUpnkAcb7.png)

    > Could not attach to pid: “34569”
    > attach failed (Not allowed to attach to process. Look in the console messages (Console.app), near the debugserver entries, when the attach failed. The subsystem that denied the attach permission will likely have logged an informative message about why it was denied.)

    Console.app 中,搜索 debugserver 得到的记录是:

    ```
    15:22:13.862180+0800 debugserver [LaunchAttach] (38215) about to task_for_pid(34569)
    15:22:13.862271+0800 debugserver error: [LaunchAttach] MachTask::TaskPortForProcessID task_for_pid(34569) failed: ::task_for_pid ( target_tport = 0x0203, pid = 34569, &task ) => err = 0x00000005 ((os/kern) failure)
    15:22:13.862366+0800 debugserver 10 +0.011235 sec [9547/0103]: error: ::task_for_pid ( target_tport = 0x0203, pid = 34569, &task ) => err = 0x00000005 ((os/kern) failure) err = ::task_for_pid ( target_tport = 0x0203, pid = 34569, &task ) => err = 0x00000005 ((os/kern) failure) (0x00000005)
    15:22:13.873606+0800 debugserver error: MachTask::StartExceptionThread (): task invalid, exception thread start failed.
    15:22:13.873711+0800 debugserver error: [LaunchAttach] END (38215) MachProcess::AttachForDebug failed to start exception thread attaching to pid 34569: unable to start the exception thread
    15:22:13.873790+0800 debugserver error: Attach failed
    15:22:13.876379+0800 debugserver error: Attach failed: "Not allowed to attach to process. Look in the console messages (Console.app), near the debugserver entries, when the attach failed. The subsystem that denied the attach permission will likely have logged an informative message about why it was denied.".
    15:22:13.876475+0800 debugserver error: attach failed.
    15:22:13.876552+0800 debugserver debugserver about to shut down packet communications to lldb.
    15:22:13.876640+0800 debugserver 11 +0.014034 sec [9547/1307]: error: ::read ( 6, 0x16b7a6b18, 1024 ) => -1 err = Bad file descriptor (0x00000009)
    15:22:13.877361+0800 debugserver debugserver secure socket proxy finish-block shut down.
    15:22:13.877499+0800 debugserver debugserver expedited exit.
    ```

    我在网上参考了其他网页,已经设置了`sudo DevToolsSecurity -enable`,检查了`get-task-allow`在我的应用中已经设为`true`,甚至是抹掉了 iPhone ,但是依然没有解决问题。

    不知有没有 iOS 开发大佬遇到类似的问题,能够提供解决方案的。

    我测试的 Xcode 版本为 14.3.1 (14E300c)和 15.0 beta 2 (15A5161b),macOS 版本为 13.4.1 ( Intel 处理器),真机试了两台,iOS 版本分别为 16.5 和 17.0 beta 2 ,均出现上述问题。

    莫非这个 feature 是必须加入 Developer Program 才可以使用吗,或是 Apple 芯片专用?
    7 条回复    2023-06-24 23:37:51 +08:00
    ShikiSuen
        1
    ShikiSuen  
       308 天前
    你的专案的 build target 系统版本是多少?
    Whyto
        2
    Whyto  
    OP
       308 天前
    @ShikiSuen Xcode 14 里试过 iOS 13.0 和 16.4 ,都不行( 16.4 似乎兼容 16.5 ?) ; Xcode 15 试过 iOS17.0 ,同样不行
    Whyto
        3
    Whyto  
    OP
       308 天前
    好家伙发现没启用 Markdown 格式
    yzwduck
        4
    yzwduck  
       307 天前
    原文底下有评论说了正解: 需要 jailbreak.

    macOS 上禁用 SIP 来获取 task_for_pid 权限只会影响运行在 macOS 上的程序, e.g., simulator 下的 app; 如果需要真机调试的话, 需要在 iOS 上获取进程的 task_for_pid 权限. 在目标 app 没有 get-task-allow 的 entitlement 前提下, 只有 jailbreak 这条路 (虽然还有 Apple Security Research Device Program, 但他们一定知道这问题的答案).
    icyalala
        5
    icyalala  
       307 天前
    想要调试其他 App ,找台手机越狱才是正解。要不然就只能找砸壳的 ipa 自签名。
    Whyto
        6
    Whyto  
    OP
       307 天前
    @icyalala @yzwduck 这样说似乎逻辑就通了,谢谢大佬们解答
    ngn999
        7
    ngn999  
       307 天前
    就算是 jailbreak 了, 也不是所有的 app 都能 attach, 目标 app 可能加了防 debugger 的代码.
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2962 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 08:06 · PVG 16:06 · LAX 01:06 · JFK 04:06
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.