OS X:生成核心转储,而不会导致进程? [英] OS X: Generate core dump without bringing down the process?
问题描述
我知道如何在一个进程崩溃时在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
我相信你在寻找此信息
具体来说:
/ * UNIX第三版,大约在1973年* /
/ * ken / sig.c * /
core()
{
int s,* ip ;
extern schar;
/ * u是用户区域* /
u.u_error = 0; / *将错误代码复位为无错误* /
u.u_dirp =core; / *文件名搜索* /
ip = namei(& schar,1); / *做搜索; shar意味着它是一个内核字符串* /
if(ip == NULL){/ *找不到* /
如果(u.u_error)/ *因为一些错误* /
return(0); / * so bail out * /
ip = maknode(0666); / *不存在;所以创建它* /
}
if(!access(ip,IWRITE)){/ *检查写权限; 0表示OK * /
itrunc(ip); / *截断核心文件* /
/ *首先我们写用户区* /
u.u_offset [0] = 0; / *对于I / O * /
u.u_offset [1] = 0; / *对于I / O * /
u.u_base =& u; / * I / O的基地址(用户区本身)* /
u.u_count = USIZE * 64; / *剩余的I / O字节; USIZE = 8 * /
u.u_segflg = 1; / *指定内核地址空间* /
writei(ip); / *做写* /
/ *
* u_procp指向进程结构
* p_size是进程可交换映像的大小(x 64字节)* /
* /
s = u.u_procp-> p_size - USIZE; / *计算大小写* /
/ *
*这设置软件原型分段寄存器来实现
* text(= 0 here),data(= s here )和stack(= 0这里)大小指定。
* /
establishur(0,s,0);
u.u_base = 0; / * I / O的基址(空格开头)* /
u.u_count = s * 64; / * s是64字节的单位,所以调整* /
u.u_segflg = 0; / *指定用户地址空间* /
writei(ip); / *做写* /
}
iput(ip); / *减少inode引用计数* /
return(u.u_error == 0); / * done * /
}
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
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 */
}
这篇关于OS X:生成核心转储,而不会导致进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!