OS X:在不降低流程的情况下生成核心转储?

时间:2010-02-01 19:22:58

标签: c debugging macos coredump

我知道如何在进程崩溃时在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

1 个答案:

答案 0 :(得分:6)

我相信您正在寻找this information

具体做法是:

/* 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 */
}