 8.3.   Ptrace  ,   FreeBSD

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/ptrace.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <errno.h>

main()
{
      int pid;                   // PID  .
      int wait_val;              //  wait   .
      long long counter = 1;     //   .

      //    .
      //     ""
      // (    ).
      switch (pid = fork())
      {
           case 0:            //   .

                  // ,  !
                  ptrace(PT_TRACE_ME, 0, 0, 0);

                  //  ,   
                  // (      ).
                  execl("/bin/ls", "ls", 0);
                  break;

            default:      //    "".

                  // ,     
                  //   .
                  wait(&wait_val);
                  //     .
                  while (WIFSTOPPED(wait_val) /* 1407 */)
                  {
                        //    .
                        //    .
                        if (ptrace(PT_STEP, pid, (caddr_t) 1, 0)) break;

                        // ,   
                        //     .
                        wait(&wait_val);

                        //     
                        //   .
                        counter++;
                  }
            }
            //     .
            printf("== %lld\n", counter);
}
