macOS下lldb直接附加进程失败,手动启动debugserver可正常连接
macOS下lldb直接附加进程失败,但手动启动debugserver连接正常的问题
问题现象
执行lldb -p <PID>或在lldb会话中运行process attach -p <PID>时,始终报错:
error: Connection shut down by remote side while waiting for reply to initial handshake packet
相关日志如下:
❯ /Applications/Xcode.app/Contents/Developer/usr/bin/lldb -O log (lldb) process attach -p 16986 < 1> send packet: + history[1] tid=0x0103 < 1> send packet: + < 19> send packet: $QStartNoAckMode#b0 error: attach failed: Connection shut down by remote side while waiting for reply to initial handshake packet
正常工作的场景
手动启动debugserver后,再将lldb连接到该服务,一切运行正常:
启动debugserver
❯ /Library/Developer/CommandLineTools/Library/PrivateFrameworks/LLDB.framework/Versions/A/Resources/debugserver localhost:1234 --attach=17154 debugserver-@(#)PROGRAM:LLDB PROJECT:lldb-1500.0.404.7 for arm64. Attaching to process 17154... Listening to port 1234 for a connection from localhost... Waiting for debugger instructions for process 17154.
连接lldb
❯ /Applications/Xcode.app/Contents/Developer/usr/bin/lldb (lldb) process connect connect://localhost:1234 Process 17154 stopped * thread #1, queue = 'com.apple.main-thread', stop reason = signal SIGSTOP frame #0: 0x000000018ec6c3e8 libsystem_kernel.dylib`__semwait_signal + 8 libsystem_kernel.dylib`: -> 0x18ec6c3e8 <+8>: b.lo 0x18ec6c408 ; <+40> 0x18ec6c3ec <+12>: pacibsp 0x18ec6c3f0 <+16>: stp x29, x30, [sp, #-0x10]! 0x18ec6c3f4 <+20>: mov x29, sp Target 0: (lldb_test_program) stopped. (lldb)
测试程序信息
测试程序无签名或保护机制,代码如下:
#include <stdio.h> #include <unistd.h> #include <signal.h> void signal_handler(int sig) { printf("Received signal %d, but continuing...\n", sig); } int main() { signal(SIGINT, signal_handler); printf("Test program started, PID: %d\n", getpid()); printf("This program will run indefinitely.\n"); printf("You can attach lldb to PID: %d\n", getpid()); printf("Press Ctrl+C to see signal handling, or kill the process to exit.\n"); int counter = 0; while(1) { printf("Counter: %d\n", counter++); sleep(2); if(counter % 10 == 0) { printf("Reminder: PID is %d\n", getpid()); } } return 0; }
编译命令:gcc -g -o lldb_test_program lldb_test_program.c
疑问
理论上lldb -p <pid>应该和手动启动debugserver再连接的效果一致,但实际存在差异。请问这种差异是什么原因导致的?是预期行为还是我的系统配置有误?如需更多上下文或系统信息,可补充提供。
内容的提问来源于stack exchange,提问作者John Joker
相关产品推荐
相关产品推荐

