ptrace

å称
     ptrace — ããã»ã¹ã®ãã¬ã¼ã¹ã¨ãããã°

ã©ã¤ãã©ãª
     Standard C Library (libc, -lc)

æ¸å¼
     <sys/types.h> <sys/ptrace.h> int ptrace(int request, pid_t pid, caddr_t
     addr, int data)

解説
     ptrace() ã·ã¹ãã ã³ã¼ã«ã¯ãã¬ã¼ã¹ã¨ãããã°ã®æ©è½ãæä¾ãã¾ãã ããã«ãã£ã¦ 1 ã¤ã®ãã‐
     ã»ã¹ (ãã¬ã¼ã¹ããããã»ã¹) ãä»ã®ããã»ã¹ (ãã¬ã¼ã¹ãããããã»ã¹) ãå¶å¾¡ã§ãã¾ãã
     ãã¬ã¼ã¹ããããã»ã¹ã¯ãæåã«ãã¬ã¼ã¹ãããããã»ã¹ã«ã¢ã¿ãã ããªããã°ãªãã¾ããã
     ããããããã®ããã»ã¹ã®å®è¡ãå¶å¾¡ããããã« ptrace() ã·ã¹ãã ã³ã¼ã«ãçºè¡ãã¾ãã ãã‐
     ã»ã¹ã®ã¡ã¢ãªããã³ã¬ã¸ã¹ã¿ç¶æã¸ã®ã¢ã¯ã»ã¹ãåæ§ã§ãã ãã¬ã¼ã¹ããã»ãã·ã§ã³ã®æéä¸‐
     ããã¬ã¼ã¹ãããããã»ã¹ã¯ ãã®è¦ªããã»ã¹ ID ã (ãã®çµæã®åä½ã¨ãã¦) ãã¬ã¼ã¹ãããã‐
     ã»ã¹ã« “親ãå¤æ´” ããã¾ãã ãã¬ã¼ã¹ããããã»ã¹ãåæã« 1 ã¤ä»¥ä¸ã®ãã‐
     ã»ã¹ãã¢ã¿ãããããã¨ã¯ã 許å¯ããã¾ãã ãã¬ã¼ã¹ããããã»ã¹ããã®ä½æ¥‐
     ãå®äºããã¨ãã«ã¯ããã®ãã¬ã¼ã¹ããã ããã»ã¹ããã¿ããããªããã°ãªãã¾ããã ãã¬ã¼ã¹ãããã‐
     ã»ã¹ãæåã«ã¢ã¿ããããã¦ããå¨ã¦ã®ããã»ã¹ããã¿ãã ããã« exit ããå ´åã«ã¯ããããã®ãã‐
     ã»ã¹ã¯ kill ããã¾ãã

     ã»ã¨ãã©ã®å ´åããã¬ã¼ã¹ãããããã»ã¹ã¯æ£å¸¸ã«å®è¡ããã¾ãã ãã ãããã¬ã¼ã¹ããããã‐
     ã»ã¹ã¯ã·ã°ãã« (sigaction(2) ãåç§) ãåä¿¡ããã¨åæ¢ãã¾ãã ãã¬ã¼ã¹ããããã»ã¹ã¯ã
     wait(2) ã¾ã㯠SIGCHLD ã·ã°ãã«ã«ãã£ã¦ãããæ¤ç¥ããåæ¢ãããããã»ã¹ã®ç¶æã調æ»ãã¦ã
     ãããçµäºãããããã¾ãã¯é©åãªå½¢ã§å®è¡ãç¶ç¶ããã¾ãã ãã®ã·ã°ãã«ã¯ããã¬ã¼ã¹ããããã‐
     ã»ã¹ã®åä½ã®çµæã¨ãã¦çæããããã kill(2) ã·ã¹ãã ã³ã¼ã«ã®ä½¿ç¨ã«ãããé常ã®ãã‐
     ã»ã¹ã®ã·ã°ãã«ããããã¾ããã ãããã¯ãã¢ã¿ãããã·ã¹ãã ã³ã¼ã«ã ã¾ãã¯ãã¬ã¼ã¹ãããã‐
     ã»ã¹ã«ããã¹ãããã®çµæã ãã¬ã¼ã¹ããä»çµã¿ã«ãã£ã¦çæããããã®ããããã¾ããã
     ãã¬ã¼ã¹ãã¦ããããã»ã¹ã¯ããã®ã·ã°ãã«ã使ç¨ãã¦ããã»ã¹ã®åä½ã観測ãã ããã«
     (SIGTRAP ã®æ§ã«) ãã®ã·ã°ãã«ã横åãããããããé©åã§ããã°ãã®ã·ã°ãã«ã ãã®ãã‐
     ã»ã¹ã«è»¢éãããããé¸æã§ãã¾ãã ptrace()
     ã·ã¹ãã ã³ã¼ã«ã¯ãããããã¹ã¦ãå¶å¾¡ããã¡ã«ããºã ã§ãã

     request å¼æ°ã¯ãã©ã®æä½ãå®è¡ããããæå®ãã¾ãã
     æ®ãã®å¼æ°ã®æå³ã¯æä½ã«ãã£ã¦ç°ãªãã¾ãã å¾è¿°ãã 1 ã¤ã®ç¹æ®ãªã±ã¼ã¹ãé¤ãã¦ã
     ptrace() å¼ã³åºãã¯ãã¹ã¦ãã¬ã¼ã¹ããããã»ã¹ã«ãã£ã¦è¡ããã pid
     å¼æ°ã¯ãã¬ã¼ã¹ãããããã»ã¹ã®ããã»ã¹ ID ãæå®ãã¾ãã request
     å¼æ°ã¯æ¬¡ã®ãã®ã«ã§ãã¾ãã

     PT_TRACE_ME   ãã®è¦æ±ã¯ããã¬ã¼ã¹ãããããã»ã¹ã使ç¨ããå¯ä¸ã®è¦æ±ã§ãã
                   ãã®è¦æ±ã¯ãããã»ã¹ããã®è¦ªã«ãã£ã¦ãã¬ã¼ã¹ããããã¨ã宣è¨ãã¾ãã
                   ä»ã®å¼æ°ã¯ãã¹ã¦ç¡è¦ããã¾ã (親ããã»ã¹ãåãã‐
                   ã»ã¹ããã¬ã¼ã¹ããªãå ´åã¯ã ããªãæ··ä¹±ããçµæã«ãªãã¾ãã
                   ãã¬ã¼ã¹ãããããã»ã¹ãåæ¢ããã¨ããã®ããã»ã¹ã¯ã ptrace()
                   ã«ãã£ã¦ããå®è¡ãç¶ç¶ã§ãã¾ãã)ã ããã»ã¹ããã®è¦æ±ã使ç¨ãã
                   execve(2) ã¾ãã¯ããã«çµã¿è¾¼ã¾ãã¦ããã«ã¼ãã³ (ãã¨ãã°ã execv(3))
                   ãå¼ã³åºããå ´åã ãã®ãã‐
                   ã»ã¹ã¯æ°ããã¤ã¡ã¼ã¸ã®æåã®å½ä»¤ãå®è¡ããåã«åæ¢ãã¾ãã
                   ã¾ããå®è¡ãããå®è¡å¯è½ã¢ã¸ã¥ã¼ã«ã® setuid ã¾ã㯠setgid
                   ãããã¯ç¡è¦ããã¾ãã

     PT_READ_I, PT_READ_D
                   ãããã®è¦æ±ã¯ããã¬ã¼ã¹ãããããã»ã¹ã®ã¢ãã¬ã¹ç©ºéãã 1 ã¤ã® int
                   ãã¼ã¿ãèªã¿åãã¾ãã å¾æ¥ã ptrace()
                   ã¯å½ä»¤ã¨ãã¼ã¿ã«ã¤ãã¦åºå¥ãããã¢ãã¬ã¹ç©ºéã®ãã
                   ãã·ã³ã許容ãã¦ãã¾ããã ããã 2 ã¤ã®è¦æ±ãããçç±ã§ãã æ¦å¿µçã«ã¯ã
                   PT_READ_I ãå½ä»¤ç©ºéããèªã¿åãã PT_READ_D ããã¼ã¿ç©ºéããèª‐
                   ã¿åãã¾ãã ç¾å¨ã® FreeBSD ã·ã¹ãã ã§ã¯ããããã® 2
                   ã¤ã®è¦æ±ã¯å®å¨ã«åä¸ã§ãã addr å¼æ°ããèªåããè¡ããã
                   (ãã¬ã¼ã¹ãããããã»ã¹ã®ä»®æ³ã¢ãã¬ã¹ç©ºéåã®) ã¢ãã¬ã¹ãæå®ãã¾ãã
                   ãã®ã¢ãã¬ã¹ã¯ã©ã®ãããªå¢ç調æ´å¶ç´ãæºããå¿è¦ã¯ããã¾ããã èª‐
                   ã¿åãããå¤ã¯ ptrace() ããã®æ»ãå¤ã¨ãã¦è¿ããã¾ãã

     PT_WRITE_I, PT_WRITE_D
                   ãããã®è¦æ±ã¯ PT_READ_I ããã³ PT_READ_D ã¨åæ§ã§ãããèª‐
                   ã¿åãã®ã§ã¯ãªãæ¸ãè¾¼ãã¨ãããç°ãªãã¾ãã data
                   å¼æ°ã§æ¸ãè¾¼ã¾ããå¤ãæå®ãã¾ãã

     PT_IO         ãã®è¦æ±ã«ãã£ã¦ããã¬ã¼ã¹ãããããã»ã¹ã®ã¢ãã¬ã¹ç©ºéåã«ãã
                   ä»»æéã®ãã¼ã¿ã®èªåãã¨æ¸è¾¼ã¿ãæå¹ã¨ãªãã¾ãã addr
                   å¼æ°ã¯ã以ä¸ã®ããã«å®ç¾©ããã struct ptrace_io_desc
                   ã¸ã®ãã¤ã³ã¿ãæå®ãã¾ãã

                   struct ptrace_io_desc {
                           int     piod_op;        /* I/O æä½ */
                           void    *piod_offs;     /* åãªãã»ãã */
                           void    *piod_addr;     /* 親ãªãã»ãã */
                           size_t  piod_len;       /* è¦æ±ã®é·ã */
                   };

                   /*
                    * piod_op ã§æå®ãããæä½
                    */
                   #define PIOD_READ_D     1       /* ãã¼ã¿ç©ºéããã®èªåã */
                   #define PIOD_WRITE_D    2       /* ãã¼ã¿ç©ºéã¸ã®æ¸è¾¼ã¿ */
                   #define PIOD_READ_I     3       /* å½ä»¤ç©ºéããã®èªåã */
                   #define PIOD_WRITE_I    4       /* å½ä»¤ç©ºéã¸ã®æ¸è¾¼ã¿ */

                   data å¼æ°ã¯ç¡è¦ããã¾ãã å®éã«èªã¿æ¸ãããããã¤ãæ°ã¯ãä¸è¨ã®
                   piod_len ã«å¥ãããè¿ããã¾ãã

     PT_CONTINUE   ãã¬ã¼ã¹ãããããã»ã¹ã¯å®è¡ãç¶ç¶ãã¾ãã addr å¼æ°ã¯ãå®è¡ãåéãããå ´æ
                   (ããã°ã©ã ã«ã¦ã³ã¿ã®æ°ããå¤)ã ã¾ãã¯å®è¡ãåæ‐
                   ¢ãããã¨ããã§åéããããã¨ã示ã (caddr_t)1 ãæå®ãã¾ãã data
                   å¼æ°ã«ã¯ããã¬ã¼ã¹ãããããã»ã¹ãå®è¡ãåéããã¨ãã«åä¿¡ããã·ã°ãã«çªå·ã
                   ã¾ãã¯ã·ã°ãã«ãéä¿¡ããªãå ´åã«ã¯ 0 ãæå®ãã¾ãã

     PT_STEP       ãã¬ã¼ã¹ãããããã»ã¹ã¯ 1 å½ä»¤ãã¤ã¹ãããå®è¡ããã¾ãã addr å¼æ°ã«ã¯
                   (caddr_t)1 ã渡ãå¿è¦ãããã¾ãã data
                   ã«ã¯å®è¡ã®åéã®ããã«ããã¬ã¼ã¹ãããããã»ã¹ã¸éä¿¡ãããã¹ãã·ã°ãã«ã®æ°ã
                   ä¸ãã¾ãã ã¾ãã¯ãéãããã¹ãã·ã°ãã«ãç¡ãå ´åã«ã¯ 0 ãä¸ãã¾ãã

     PT_KILL       ãã¬ã¼ã¹ãããããã»ã¹ã¯ããããã SIGKILL ãéä¿¡ã·ã°ãã«ã¨ãã¦
                   PT_CONTINUE ã使ç¨ããããã®ããã«ãçµäºãã¾ãã

     PT_ATTACH     ãã®è¦æ±ã¯ãä»ã®ç¡é¢ä¿ãªããã»ã¹ã®å¶å¾¡ãåå¾ãã
                   ãã®ãã¬ã¼ã¹ãéå§ãã¾ãã ãã¬ã¼ã¹ãããããã»ã¹ããã®ååã¯å¿è¦ã¨ãã¾ããã
                   ãã®ã±ã¼ã¹ã§ã¯ã pid ã«ãã¬ã¼ã¹ãããããã»ã¹ã®ããã»ã¹ ID ãæå®ãã
                   ä»ã® 2 ã¤ã®å¼æ°ã¯ç¡è¦ããã¾ãã ãã®è¦æ±ã§ã¯ãã¿ã¼ã²ãããã‐
                   ã»ã¹ããã¬ã¼ã¹ãã ããã»ã¹ã¨åãå® UID ãæã¤ãã¨ã ããã setuid ã¾ãã¯
                   setgid ãããå®è¡å¯è½ã¢ã¸ã¥ã¼ã«ã§ãªããã¨ã è¦æ±ããã¾ã
                   (ãã¬ã¼ã¹ããããã»ã¹ã root ã¨ãã¦å®è¡ããã¦ããå ´åã
                   ãããã®å¶ç´ã¯é©ç¨ããã¾ãã)ã ãã¬ã¼ã¹ãããã‐
                   ã»ã¹ã¯ãæ°ãã«ãã¬ã¼ã¹ãããããã»ã¹ãåæ¢ããã
                   ããããæåãããã£ã¨ãã¬ã¼ã¹ãã¦ãããã®ããã«å¶å¾¡ã§ãã¾ãã

     PT_DETACH     ãã®è¦æ±ã¯ PT_CONTINUE ã¨é¡ä¼¼ãã¦ãã¾ãããå®è¡ãç¶ç¶ããå¥ã®å ´æã
                   æå®ã§ããªããã¨ãããã³è¦æ±ãæåããå¾ã ãã¬ã¼ã¹ããã¦ãããã‐
                   ã»ã¹ã¯ãã¯ããã¬ã¼ã¹ãããã é常ã©ããå®è¡ãç¶ç¶ãããã¨ãç°ãªãã¾ãã

     PT_GETREGS    ãã®è¦æ±ã¯ããã¬ã¼ã¹ãããããã»ã¹ã®ãã·ã³ã¬ã¸ã¹ã¿ãã addr ãæã
                   “struct reg” (<machine/reg.h> åã«å®ç¾©ããã¦ãã¾ã) åã«èª‐
                   ã¿åãã¾ãã

     PT_SETREGS    ãã®è¦æ±ã¯ PT_GETREGS ã®éã§ãã addr ãæã “struct reg”
                   (<machine/reg.h> åã«å®ç¾©ããã¦ãã¾ã) ãããã¬ã¼ã¹ããããã‐
                   ã»ã¹ã®ãã·ã³ã¬ã¸ã¹ã¿ããã¼ããã¾ãã

     PT_GETFPREGS  ãã®è¦æ±ã¯ãã¬ã¼ã¹ãããããã»ã¹ã®æµ®åå°æ°ç¹ã¬ã¸ã¹ã¿ã addr ãæã
                   “struct fpreg” (<machine/reg.h> ã«å®ç¾©ããã¦ãã¾ã) ã«èª‐
                   ã¿åãã¾ãã

     PT_SETFPREGS  ãã®è¦æ±ã¯ PT_GETFPREGS ã®å対ã§ãã addr ãæã “struct fpreg”
                   (<machine/reg.h> åã«å®ç¾©ããã¦ãã¾ã) ãããã¬ã¼ã¹ããããã‐
                   ã»ã¹ã®æµ®åå°æ°ç¹ã¬ã¸ã¹ã¿ããã¼ããã¾ãã

     PT_GETDBREGS  ãã®è¦æ±ã¯ãã¬ã¼ã¹ãããããã»ã¹ã®ãããã°ã¬ã¸ã¹ã¿ã addr ãæã “struct
                   dbreg” (<machine/reg.h> åã«å®ç¾©ããã¦ãã¾ã) ã«èªã¿åãã¾ãã

     PT_SETDBREGS  ãã®è¦æ±ã¯ PT_GETDBREGS ã®å対ã§ãã addr ã®æã “struct dbreg”
                   (<machine/reg.h> åã«å®ç¾©ããã¦ãã¾ã) ãããã¬ã¼ã¹ããããã‐
                   ã»ã¹ã®ãããã°ã¬ã¸ã¹ã¿ã«ãã¼ããã¾ãã

     PT_LWPINFO    ãã®è¦æ±ã¯ããã¬ã¼ã¹ãããããã»ã¹ãåæ¢ããåå ã¨ãªã£ã (軽éãã‐
                   ã»ã¹ã¨ãã¦ãç¥ããã) ã«ã¼ãã«ã¹ã¬ããã«ã¤ãã¦ã®æå ±ã
                   ç²å¾ããããã«ä½¿ç¨ããã¾ãã addr å¼æ°ã¯ä»¥ä¸ã®ããã«å®ç¾©ããã struct
                   ptrace_lwpinfo ã¸ã®ãã¤ã³ã¿ãæå®ãã¾ã:

                   struct ptrace_lwpinfo {
                           lwpid_t pl_lwpid;       /* LWP èå¥å */
                           int     pl_event;       /* åãåã£ãã¤ãã³ã */
                   };

                   data å¼æ°ã¯ãå¼ã³åºãå´ã«ç¥ããã¦ããæ§é ä½ã®å¤§ããã«è¨å®ããã¾ãã
                   ããã«ãããå¤ãããã°ã©ã ã«å½±é¿ãä¸ãããã¨ç¡ãã«ã
                   ãã®æ§é ä½ã大ãããããã¨ãã§ãã¾ãã

     ããã«ãã·ã³åºæã®è¦æ±ãåå¨ãããã¨ãããã¾ãã

æ»ãå¤
     ããã¤ãã®è¦æ±ã§ ptrace() ã¯ã¨ã©ã¼ä»¥å¤ã®å ´åã«ã -1 ãè¿ãã¾ãã
     ææ§ãããªããããã«ã¯ãå¼ã³åºãã®åã« errno ã 0 ã«è¨å®ããå¾ã§ãã§ãã¯ãã¾ãã

ã¨ã©ã¼
     ptrace() ã·ã¹ãã ã³ã¼ã«ã¯æ¬¡ã®å ´åã«å¦çã«å¤±æãã¾ãã

     [ESRCH]
                        ·   æå®ãããããã»ã¹ ID ãæã¤ããã»ã¹ãåå¨ãã¾ããã

     [EINVAL]
                        ·   ããã»ã¹ãèªåèªèº«ã«å¯¾ã㦠PT_ATTACH ã使ããã¨ãã¾ããã
                        ·   request å¼æ°ãæ£ããè¦æ±ã® 1 ã¤ã§ã¯ããã¾ããã§ããã
                        ·   PT_CONTINUE ã¸ã®ã·ã°ãã«çªå· (data) ã 0
                            ã§ãªãããã¾ãã¯æ£ããã·ã°ãã«çªå·ã§ã¯ããã¾ããã§ããã
                        ·   PT_GETREGS, PT_SETREGS, PT_GETFPREGS,
                            PT_SETFPREGS, PT_GETDBREGS ã¾ã㯠PT_SETDBREGS
                            ããæå¹ãªã¬ã¸ã¹ã¿ã»ãããè¨å®ããã«ä½¿ç¨ããã¾ãã
                            (ãããçã«ãªãã®ã¯ãé常ãã·ã¹ãã ããã»ã¹ã«ã¤ãã¦ã®ã¿ã§ã)ã

     [EBUSY]
                        ·   PT_ATTACH ãæ¢ã«ãã¬ã¼ã¹ä¸ã®ãã‐
                            ã»ã¹ã«ã¤ãã¦ä½¿ç¨ããã¾ããã
                        ·   è¦æ±ããã¦ããããã»ã¹ä»¥å¤ã®ããã»ã¹ã«ãã£ã¦ããã¬ã¼ã¹ããããã‐
                            ã»ã¹ã æä½ãããã¨ããè¦æ±ã試ã¿ããã¾ããã
                        ·   è¦æ± (PT_ATTACH 以å¤ã®) ãåæ¢ããã¦ããªããã‐
                            ã»ã¹ãæå®ãã¾ããã

     [EPERM]
                        ·   è¦æ± (PT_ATTACH 以å¤ã®)
                            ããã¾ã£ãããã¬ã¼ã¹ããã¦ããªãããã»ã¹ãæä½ãããã¨ãã¾ããã
                        ·   åè¿°ã® PT_ATTACH ã§èª¬æããæ¡ä»¶ãæºãããªããã‐
                            ã»ã¹ã«ã¤ã㦠PT_ATTACH ã使ããã¨ãã¾ããã

é¢é£é ç®
     execve(2), sigaction(2), wait(2), execv(3), i386_clr_watch(3),
     i386_set_watch(3)

æ´å²
     ptrace() é¢æ°ã¯ Version 7 AT&T UNIX ã§ç»å ´ãã¾ããã