OS X:在不关闭进程的情况下生成核心转储?

发布于 2024-08-20 09:11:57 字数 640 浏览 5 评论 0原文

我知道如何在进程崩溃时在 OS X 上生成核心转储,但我真正需要做的是附加到进程,生成核心转储,然后恢复该进程(而不杀死它)。

很久以前(也许一年半前)我有 C 代码可以做到这一点......它使用 OS X 内核库连接到一个进程,读取其所有线程状态和内存,并将其写入磁盘上的 Mach-O 文件。这非常有效(这正是我正在寻找的),但现在我似乎无法找到适合我一生的代码。我似乎记得代码与 OS X 系统内部原理书有些关系,但这只是一个模糊的回忆。

有谁知道我正在谈论的代码并可以指出我吗?如果没有,有人知道最好用一些示例代码来做到这一点的好方法吗?

编辑:这是答案。

信息:http://osxbook.com/book/bonus/chapter8/core/

可以为您完成此操作的程序: http://osxbook .com/book/bonus/chapter8/core/download/gcore-1.3.tar.gz

I know how to generate a core dump on OS X when a process crashes, but what I really need to do is attach to a process, generate a core dump, then resume that process (without killing it).

A long time ago (maybe a year and a half ago) I had C code that would do this... It used the OS X kernel libraries to connect to a process, read all of its thread states and memory, and write that into a Mach-O file on disk. This worked great (and it's exactly what I'm looking for), but now I can't seem to find that code for the life of me. I seem to recall that code was related somewhat to the OS X system internals book, but that's just a vague recollection.

Does anyone know the code I'm talking about and could point me at it? If not does anyone know a good way of doing this preferably with some example code?

Edit: Here is the answer.

Information: http://osxbook.com/book/bonus/chapter8/core/

Program that will do it for you: http://osxbook.com/book/bonus/chapter8/core/download/gcore-1.3.tar.gz

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

审判长 2024-08-27 09:11:57

我相信您正在寻找这个信息

具体来说:

/* UNIX Third Edition, circa early 1973 */
/* ken/sig.c */

core()
{
int s, *ip;
extern schar;

/* u is the user area */
u.u_error = 0;          /* reset error code to "no error" */
u.u_dirp = "core";      /* file name to search for */
ip = namei(&schar, 1);  /* do search; schar means it's a kernel string */

if (ip == NULL) {       /* failed to find */
    if (u.u_error)      /* because of some error */
        return(0);      /* so bail out */
    ip = maknode(0666); /* didn't exist; so create it */
}

if (!access(ip, IWRITE)) { /* check "write" permission; 0 means OK */
    itrunc(ip);            /* truncate the core file */

    /* first we write the user area */
    u.u_offset[0] = 0;     /* offset for I/O */
    u.u_offset[1] = 0;     /* offset for I/O */
    u.u_base = &u;         /* base address for I/O (user area itself) */
    u.u_count = USIZE*64;  /* bytes remaining for I/O; USIZE=8 */
    u.u_segflg = 1;        /* specify kernel address space */
    writei(ip);            /* do the write */

    /*
     * u_procp points to the process structure
     * p_size is the size of the process's swappable image (x 64 bytes) */
     */
    s = u.u_procp->p_size - USIZE; /* compute size left to write */
    
    /*
     * This sets up software prototype segmentation registers to implement
     * text(=0 here), data(=s here), and stack(=0 here) sizes specified.
     */
    estabur(0, s, 0);

    u.u_base = 0;          /* base address for I/O (start of space) */
    u.u_count = s*64;      /* s is in units of 64 bytes, so adjust */
    u.u_segflg = 0;        /* specify user address space */
    writei(ip);            /* do the write */
}
iput(ip);                  /* decrement inode reference count */
return(u.u_error==0);      /* done */
}

I believe you are looking for this information

Specifically:

/* UNIX Third Edition, circa early 1973 */
/* ken/sig.c */

core()
{
int s, *ip;
extern schar;

/* u is the user area */
u.u_error = 0;          /* reset error code to "no error" */
u.u_dirp = "core";      /* file name to search for */
ip = namei(&schar, 1);  /* do search; schar means it's a kernel string */

if (ip == NULL) {       /* failed to find */
    if (u.u_error)      /* because of some error */
        return(0);      /* so bail out */
    ip = maknode(0666); /* didn't exist; so create it */
}

if (!access(ip, IWRITE)) { /* check "write" permission; 0 means OK */
    itrunc(ip);            /* truncate the core file */

    /* first we write the user area */
    u.u_offset[0] = 0;     /* offset for I/O */
    u.u_offset[1] = 0;     /* offset for I/O */
    u.u_base = &u;         /* base address for I/O (user area itself) */
    u.u_count = USIZE*64;  /* bytes remaining for I/O; USIZE=8 */
    u.u_segflg = 1;        /* specify kernel address space */
    writei(ip);            /* do the write */

    /*
     * u_procp points to the process structure
     * p_size is the size of the process's swappable image (x 64 bytes) */
     */
    s = u.u_procp->p_size - USIZE; /* compute size left to write */
    
    /*
     * This sets up software prototype segmentation registers to implement
     * text(=0 here), data(=s here), and stack(=0 here) sizes specified.
     */
    estabur(0, s, 0);

    u.u_base = 0;          /* base address for I/O (start of space) */
    u.u_count = s*64;      /* s is in units of 64 bytes, so adjust */
    u.u_segflg = 0;        /* specify user address space */
    writei(ip);            /* do the write */
}
iput(ip);                  /* decrement inode reference count */
return(u.u_error==0);      /* done */
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文