macOS 13.4.1使用lldb调试C程序,被信号5终止。

huangapple go评论88阅读模式
英文:

m2 macos 13.4.1 use lldb debug c program terminated by signal 5

问题

我的问题

  • 当我在macOS M2上的主函数中使用lldb调试C程序时,使用了fork()和execl()函数,lldb被信号5终止。
  • 但是在x86 ubuntu系统上使用相同的代码,gdb和lldb都正常工作。

我的代码

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
  pid_t childpid;
  int fd[2];
  if ((pipe(fd) == -1) || ((childpid = fork()) == -1)) {
    perror("Failed to setup pipeline");
    return 1;
  }
  if (childpid == 0) {
    if (dup2(fd[1], STDOUT_FILENO) == -1)
      perror("Failed to redirect stdout of ls");
    if ((close(fd[0]) == -1) || (close(fd[1])) == -1)
      perror("Failed to close extra pipe descriptors on ls");
    else {
      execl("/bin/ls", "ls", "-l", NULL);
      perror("Failed to exec ls");
    }
    return 1;
  }
  if (dup2(fd[0], STDIN_FILENO) == -1)
    perror("Failed to redirect stdin of sort");
  if ((close(fd[0]) == -1) || (close(fd[1]) == -1))
    perror("Failed to close extra pipe descriptors on sort");
  else {
    execl("/usr/bin/sort", "sort", "-n", "+4", NULL);
    perror("Failed to exec sort");
  }
  return 1;
}

运行详细信息

  • 直接运行程序显示:
➜   ./simpleredirect
total 104
drwxr-xr-x  3 tonghui  staff     96  7 23 15:24 simpleredirect.dSYM
-rw-r--r--@ 1 tonghui  staff    747  7 22 11:32 parentwritepipe.c
-rw-r--r--@ 1 tonghui  staff    989  7 23 16:28 simpleredirect.c
-rw-r--r--@ 1 tonghui  staff    999  7 22 13:17 synchronizefan_pipe.c
-rw-r--r--@ 1 tonghui  staff   1033  7 23 13:14 simpleredirect_1.c
-rwxr-xr-x  1 tonghui  staff  34059  7 23 16:28 simpleredirect
  • 使用lldb调试
➜   lldb simpleredirect
(lldb) target create "simpleredirect"
Current executable set to '/Users/specialfile/simpleredirect' (arm64).
(lldb) r
Process 49076 launched: '/Users/workspace/code-container/c/unix/specialfile/simpleredirect' (arm64)
Process 49076 exited with status = 5 (0x00000005) Terminated due to signal 5
(lldb) n
error: Command requires a process which is currently stopped.
  • 请帮助我,谢谢!

    lldb终止 Process 49190退出状态为5 (0x00000005) 由于信号5而终止

英文:

My problem

  • when i use lldb to debug a c program, i use function fork() execl() in a main function in macos m2, lldb terminated by signal 5
  • but when i use x86 ubuntu system, the same code, Both gdb and lldb work properly

My code

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
  pid_t childpid;
  int fd[2];
  if ((pipe(fd) == -1) || ((childpid = fork()) == -1)) {
    perror("Failed to setup pipeline");
    return 1;
  }
  if (childpid == 0) {
    if (dup2(fd[1], STDOUT_FILENO) == -1)
      perror("Failed to redirect stdout of ls");
    if ((close(fd[0]) == -1) || (close(fd[1])) == -1)
      perror("Failed to close extra pipe descriptors on ls");
    else {
      execl("/bin/ls", "ls", "-l", NULL);
      perror("Failed to exec ls");
    }
    return 1;
  }
  if (dup2(fd[0], STDIN_FILENO) == -1)
    perror("Failed to redirect stdin of sort");
  if ((close(fd[0]) == -1) || (close(fd[1]) == -1))
    perror("Failed to close extra pipe descriptors on sort");
  else {
    execl("/usr/bin/sort", "sort", "-n", "+4", NULL);
    perror("Failed to exec sort");
  }
  return 1;
}

Run detail info

  • run program directly shows:
➜   ./simpleredirect
total 104
drwxr-xr-x  3 tonghui  staff     96  7 23 15:24 simpleredirect.dSYM
-rw-r--r--@ 1 tonghui  staff    747  7 22 11:32 parentwritepipe.c
-rw-r--r--@ 1 tonghui  staff    989  7 23 16:28 simpleredirect.c
-rw-r--r--@ 1 tonghui  staff    999  7 22 13:17 synchronizefan_pipe.c
-rw-r--r--@ 1 tonghui  staff   1033  7 23 13:14 simpleredirect_1.c
-rwxr-xr-x  1 tonghui  staff  34059  7 23 16:28 simpleredirect
  • use lldb to debug
➜   lldb simpleredirect
(lldb) target create "simpleredirect"
Current executable set to '/Users/specialfile/simpleredirect' (arm64).
(lldb) r
Process 49076 launched: '/Users/workspace/code-container/c/unix/specialfile/simpleredirect' (arm64)
Process 49076 exited with status = 5 (0x00000005) Terminated due to signal 5
(lldb) n
error: Command requires a process which is currently stopped.
  • please help me,thanks!

lldb terminated Process 49190 exited with status = 5 (0x00000005) Terminated due to signal 5

答案1

得分: 0

达尔文系统默认不允许对二进制文件进行调试。它们要么必须是临时签名的代码,要么必须使用 get-task-allow 权限设置为 "true" 进行签名。系统二进制文件始终由苹果进行代码签名,通常不具备该权限,因此无法进行调试。如果您确实需要调试系统二进制文件,您必须关闭SIP(系统完整性保护)。

根据您编写的代码方式,在 fork 的子进程中使用 execl 执行 /usr/bin/ls(这是可以的),同时在第27行的父进程中执行 /usr/bin/sort

当您在调试器下尝试执行而不是 fork/exec 系统二进制文件时,您的程序将在执行阶段被终止,否则这将为调试未选择进行调试的系统二进制文件提供了一种间接的方式。由于系统不希望调试器有机会尝试使调试会话以某种方式工作,因此它会以“极端偏见”的方式终止它,这会导致您的程序被终止。

英文:

darwin systems don't allow binaries to be debugged by default. They either have to be ad hoc codesigned, or signed with the get-task-allow entitlement set to "true". System binaries are always codesigned by Apple and generally don't have that entitlement, and so can't be debugged. If you really need to debug a system binary you have to turn off SIP.

The way you've written your code, you execl /usr/bin/ls in the forked child (which would be fine) AND /usr/bin/sort on the parent side of the fork at line 27.

When you are running under the debugger and try to exec as opposed to fork/exec a system binary, your program will get killed in exec, otherwise that would provide a backhand way to debug a system binary that didn't opt into being debugged. Your program gets killed in a not very helpful way, because the system doesn't want the give the debugger any leeway to try to make the debug session work somehow, so it terminates it with "extreme prejudice".

huangapple
  • 本文由 发表于 2023年7月23日 17:09:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76747436.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定