corefile name 변경하기 : OSX

Computing 2008. 9. 30. 12:14
일전에 이런 포스트를 한 적이 있다 :
core file name 변경하기

뭔가를 하다가 우연히 sysctl 맨페이지를 보고서는 sysctl -A 를 하면서 어떤 값들이 있나 구경하고 있던 중 문득 corefile 이라는 엔트리를 보게 되었다. 틀림없이 corefile 이름의 형식을 이야기하는 것 같았다.

shawn.ygdrasil:~$ sysctl kern.corefile
kern.corefile: /cores/core.%P

그래서 어떤 format string 을 쓸 수 있나 보려고 man page 를 뒤졌는데, core file 에 관련된 내용이 문서에 없었다. man 5 core 를 해 봐도 그냥 디폴트로 pid 가 뒤에 붙는다라고만 되어 있고 별다른 정보가 없었다.

가만있자... OSX 이 결국은 BSD 니까... 혹시 BSD 맨페이지에는 어떻게 나오나 해서 BSD 맨페이지를 살펴 보았다. 역시나 FreeBSD 의 sysctl (8) 맨페이지에는 corefile 에 대한 언급이 있었다.
그래, 그러면 그렇지... 그럼, FreeBSD 의 core (5) 맨페이지를 보면 포맷이 나올 수도 있겠구나. 아니나 다를까, 포맷 스트링에 대한 이야기가 나온다. 인용하자면 아래와 같다 :

     The name of the file is controlled via the sysctl(8) variable
     kern.corefile.  The contents of this variable describes a filename to
     store the core image to.  This filename can be absolute, or relative
     (which will resolve to the current working directory of the program gen-
     erating it).  Any sequence of %N in this filename template will be
     replaced by the process name, %P by the processes PID, and %U by the UID.
     The name defaults to %N.core, yielding the traditional FreeBSD behaviour.

그럼, 테스트를 해 보자.

포맷을, 위에 나오는 세 가지 타입을 다 넣어서 만들자.

shawn.ygdrasil:~$ sudo sysctl -w kern.corefile='/cores/core.%U.%N.%P'
Password:
kern.corefile: /cores/core.%P -> /cores/core.%U.%N.%P

이제, core file name 변경하기 포스트에 있는 프로그램을 만들어서 실행시키면 코어 파일이 덤프되었다는 메세지가 나온다.

shawn.ygdrasil:~/work/core_file$ ls /cores/
core.20198  core.79363  core.79409
shawn.ygdrasil:~/work/core_file$ ./a.out
hello
Bus error (core dumped)
shawn.ygdrasil:~/work/core_file$ ls /cores/
core.20198            core.501.a.out.20421  core.79363            core.79409

오케이! 잘 되네.

그런데, 문제는 이것이 애플의 문서에 나오지 않는다는 것인데... 왜일까? ㅡ,.ㅡ

아무튼, 정리하자면,

1. sysctl 을 사용해서 corefile 포맷을 변경할 수 있다.
2. 포맷 스트링 템플릿에는 아래의 세가지 변수(?) 가 들어갈 수 있다 :
   %U : uid
   %P : process pid
   %N : process name

P.S. on wordpress :
http://orchistro.wordpress.com/2008/09/30/changing-core-file-name-format-on-osx/
: