CreateProcess + CREATE_SUSPENDED 标志在 Linux 中是否等效?

标签 c linux debugging process

我想知道是否有启动进程挂起的方法?

类似于 Windows 中的 CreateProcess + CREATE_SUSPENDED:

CreateProcessA(
        NULL, 
        CmdLine, 
        NULL, 
        NULL, 
        FALSE, 
        CREATE_SUSPENDED,
        NULL, 
        "C:\\Windows\\System32\\", 
        &si, 
        &pi);

ptrace 似乎只支持 PTRACE_ATTACH,没有办法启动一个进程并直接挂起它,有什么想法吗?

编辑

我需要能够像这样捕捉过程,

int main()
{
   exit(0);
}

因此 shell 方法将不起作用,因为进程退出速度非常快。

最佳答案

一种可能的方法是将流程详细信息作为流程、fork 和 child 中的输入,然后再执行流程,向自己发送停止信号。

示例代码:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>

main()
{
    int pid;
    //Note: Replace argv_list with corresponding binary and arguments
    //Can also get these values as command line argument from main(argc, argv)
    char *argv_list[] = {"/usr/bin/ls","-lrt", "/", NULL};

    if((pid=fork())==0)
    {
        printf("Child Executing\n");
        //Send STOP signal to self to suspend
        kill(getpid(), SIGSTOP);
        //Sleep for stop signal handler to suspend current process
        sleep(1);
        //This line should start to execute only after CONT signal is sent.
        execv(argv_list[0],argv_list);

    }
    else
    {
        printf("Child PID:%d, Parent continuing\n", pid);
    }

    printf("\n***Main - Exiting\n");
    exit(0);
}

关于CreateProcess + CREATE_SUSPENDED 标志在 Linux 中是否等效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54509868/

相关文章:

linux - SYSTEMTAP中如何显示进程总量、主内存、虚拟内存使用量?

linux - 符号表示法中的 unix 权限转换器(包括粘性位)

python Eclipse - 如何在警告时中断/暂停?

delphi - 为什么 Delphi-XE2 64 位 Windows 程序不能在集成环境中运行?

ios - 无法将统一调试器附加到 iOS 设备

c - 将多维数组传递给 C 中的函数

c - C语言中的二进制计数器

c - 确保字符串与 c 中的特定格式匹配

c - 理解指向常量的指针指向整型常量的指针(const int * const * variable)

php - C++ 应用程序在几个小时后崩溃