2010-02-01 40 views
8

我知道如何在進程崩潰時在OS X上生成核心轉儲,但我真正需要做的是附加到進程,生成核心轉儲,然後恢復該進程(而不殺死它)。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

回答

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

上面的代碼是什麼,他的比較鏈接到gzip中的代碼,所以這是不正確的,但鏈接(特別是gcore gzip)是精確的只是我在找什麼。謝謝! – LCC

+0

這會教會我仔細複製和粘貼 – mbarnett