// File: /workspace/github_c_makefile_star2_O2/mvermeulen[P]wspy/topdown.c
// Function: ptrace_loop
void ptrace_loop(void){
  pid_t pid,child;
  int status;
  int i;
  int last_syscall = 0;
  int syscall_entry = 0;
  unsigned long data;
  char buffer[1024];
  char stat_name[128];
  struct stat statbuf;
  FILE *stat_file;
  struct user_regs_struct regs;
  struct rusage rusage;
  double elapsed;
  char *filename;

  while(1){
    pid = wait4(-1,&status,0,&rusage);
    clock_gettime(CLOCK_REALTIME,&finish_time);
    elapsed = finish_time.tv_sec + finish_time.tv_nsec / 1000000000.0 -
      start_time.tv_sec - start_time.tv_nsec / 1000000000.0;
    debug2("event: pid=%d status=%x\n",pid,status);
    if (pid == -1){
      if (errno == ECHILD){
	break; // no more children to wait
      } else {
	error("wait returns -1 with errno %d - %s\n",errno,strerror(errno));
      }
    }
    if (WIFEXITED(status)){
      fprintf(treefile,"%5.3f %d exited\n",elapsed,pid);
      fflush(treefile);
      debug2("   exited\n");
      if (pid == child_pid) break;
      continue;
    } else if (WIFSIGNALED(status)){
      // dump contents of proc/<pid>/comm
      snprintf(stat_name,sizeof(stat_name),"/proc/%d/comm",pid);
      if ((stat_file = fopen(stat_name,"r")) != NULL){
	if (fgets(buffer,sizeof(buffer),stat_file) != NULL){
	  fprintf(treefile,"%5.3f %d comm ",elapsed,pid);
	  fputs(buffer,treefile);
	}
	fclose(stat_file);
      }
      // dump the full command line
      if (tree_cmdline){
	snprintf(stat_name,sizeof(stat_name),"/proc/%d/cmdline",pid);
	fprintf(treefile,"%5.3f %d cmdline",elapsed,pid);
	if ((stat_file = fopen(stat_name,"rb")) != NULL){
	  char *arg = 0;
	  size_t size = 0;
	  while (getdelim(&arg,&size,0,stat_file) != -1){
	    fprintf(treefile," %s",arg);
	  }
	  fclose(stat_file);
	}
	fprintf(treefile,"\n");
      }
      
      fprintf(treefile,"%5.3f %d signal %u\n",elapsed,pid,WTERMSIG(status));
      fflush(treefile);
      debug2("   signaled\n");
      continue;
    } else if (WIFSTOPPED(status)){
      if (WSTOPSIG(status) == SIGTRAP){
	// we have an event!
	switch(status>>16){
	case PTRACE_EVENT_CLONE:
	case PTRACE_EVENT_FORK:
	case PTRACE_EVENT_VFORK:
	  ptrace(PTRACE_GETEVENTMSG,pid,NULL,&data);
	  fprintf(treefile,"%5.3f %d fork %lu\n",elapsed,pid,data);
	  fflush(treefile);
	  debug2("   clone/fork/vfork - pid=%d\n",data);
	  break;
	case PTRACE_EVENT_EXIT:
	  ptrace(PTRACE_GETEVENTMSG,pid,NULL,&data);
	  // dump contents of proc/<pid>/comm
	  snprintf(stat_name,sizeof(stat_name),"/proc/%d/comm",pid);
	  if ((stat_file = fopen(stat_name,"r")) != NULL){
	    if (fgets(buffer,sizeof(buffer),stat_file) != NULL){
	      fprintf(treefile,"%5.3f %d comm ",elapsed,pid);
	      fputs(buffer,treefile);
	    }
	    fclose(stat_file);
	  }
	  // dump the full command line
	  if (tree_cmdline){
	    snprintf(stat_name,sizeof(stat_name),"/proc/%d/cmdline",pid);
	    fprintf(treefile,"%5.3f %d cmdline",elapsed,pid);
	    if ((stat_file = fopen(stat_name,"rb")) != NULL){
	      char *arg = 0;
	      size_t size = 0;
	      while (getdelim(&arg,&size,0,stat_file) != -1){
		fprintf(treefile," %s",arg);
	      }
	    }
	    fprintf(treefile,"\n");
	    fclose(stat_file);
	  }

	  // dump contents of proc/<pid>/stat
	  snprintf(stat_name,sizeof(stat_name),"/proc/%d/stat",pid);
	  if ((stat_file = fopen(stat_name,"r")) != NULL){
	    if (fgets(buffer,sizeof(buffer),stat_file) != NULL){
	      fprintf(treefile,"%5.3f %d exit ",elapsed,pid);
	      fputs(buffer,treefile);
	      fflush(treefile);
	    }
	    fclose(stat_file);
	  }
	  debug2("   exit - exit status=%d\n",data);
	  break;
	default:
	  // normal SIGTRAP - not sure how we got here, but continue without it.
	  // we seem to get these after a process has exited...
	  fprintf(treefile,"%5.3f %d unknown %x\n",elapsed,pid,status);
	  fflush(treefile);
	  debug2("   unknown event: %d status=%x %d\n",pid,status);
	  ptrace(trace_syscall?PTRACE_SYSCALL:PTRACE_CONT,pid,NULL,NULL);
	  continue;
	}
      } else if (WSTOPSIG(status) == SIGSTOP){
	// newly created process after fork/vfork/clone, continue without passing along signal
	debug2("   new pid\n");
	ptrace(trace_syscall?PTRACE_SYSCALL:PTRACE_CONT,pid,NULL,NULL);
	continue;
      } else if (WSTOPSIG(status) == (SIGTRAP | 0x80)){
	// stopped because of a system call
	ptrace(PTRACE_GETREGS,pid,0,&regs);
	if (last_syscall != regs.orig_rax){
	  syscall_entry = 1;
	} else {
	  syscall_entry = (1-syscall_entry);
	}
	last_syscall = regs.orig_rax;
	if (tree_open && (syscall_entry == 0) && (last_syscall == SYS_openat)){
	  filename = ptrace_read_null_terminated_string(pid, regs.rsi);
	  fprintf(treefile,"%5.3f %d open %s\n",elapsed,pid,filename);
	}
	ptrace(PTRACE_SYSCALL,pid,NULL,NULL,NULL);
      } else {
	// pass other signals to the child
	fprintf(treefile,"%5.3f %d signal %d\n",elapsed,pid,WSTOPSIG(status));
	fflush(treefile);
	ptrace(trace_syscall?PTRACE_SYSCALL:PTRACE_CONT,pid,NULL,WSTOPSIG(status));
      }
    } else if (WIFCONTINUED(status)){
      fprintf(treefile,"%5.3f %d continued\n",elapsed,pid);
      fflush(treefile);      
      // nothing here
    }
    // let the child go to the next event
    ptrace(trace_syscall?PTRACE_SYSCALL:PTRACE_CONT,pid,NULL,NULL);
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x648, %rsp            # imm = 0x648
leaq 0xcd42(%rip), %rbp      # 0x11100
leaq 0x18(%rsp), %rbx
leaq 0x30(%rsp), %r14
leaq 0x73e3(%rip), %r15      # 0xb7b2
movl $0x0, 0x14(%rsp)
movq $0x0, 0x28(%rsp)
pushq $-0x1
popq %rdi
leaq 0x10(%rsp), %rsi
xorl %edx, %edx
leaq 0x1b0(%rsp), %rcx
callq 0x3240
movl %eax, %r12d
xorl %edi, %edi
leaq 0xdd4d(%rip), %rsi      # 0x12150
callq 0x3070
cvtsi2sdq 0xdd3f(%rip), %xmm0 # 0x12150
movsd %xmm0, 0x40(%rsp)
cvtsi2sdq 0xdd38(%rip), %xmm0 # 0x12158
movsd %xmm0, 0x8(%rsp)
cvtsi2sdq 0xdd31(%rip), %xmm0 # 0x12160
movsd %xmm0, 0x38(%rsp)
cvtsi2sdq 0xdd2a(%rip), %xmm0 # 0x12168
movsd %xmm0, 0x48(%rsp)
movl 0x10(%rsp), %edx
leaq 0x72c8(%rip), %rdi      # 0xb717
movl %r12d, %esi
xorl %eax, %eax
callq 0x94bd
movsd 0x8(%rsp), %xmm2
cmpl $-0x1, %r12d
jne 0x4499
callq 0x3040
movl (%rax), %r13d
cmpl $0xa, %r13d
je 0x4ac6
movl %r13d, %edi
callq 0x3300
leaq 0x72aa(%rip), %rdi      # 0xb730
movl %r13d, %esi
movq %rax, %rdx
xorl %eax, %eax
callq 0x901e
movsd 0x8(%rsp), %xmm2
movsd 0x5d47(%rip), %xmm0     # 0xa1e8
divsd %xmm0, %xmm2
addsd 0x40(%rsp), %xmm2
subsd 0x38(%rsp), %xmm2
movsd 0x48(%rsp), %xmm1
divsd %xmm0, %xmm1
subsd %xmm1, %xmm2
movl 0x10(%rsp), %ecx
movl %ecx, %eax
andl $0x7f, %eax
movapd %xmm2, %xmm0
je 0x4612
shll $0x18, %eax
addl $0x1000000, %eax        # imm = 0x1000000
cmpl $0x2000000, %eax        # imm = 0x2000000
jl 0x4650
movsd %xmm0, 0x8(%rsp)
movl $0x80, %esi
leaq 0x50(%rsp), %r13
movq %r13, %rdi
leaq 0x7271(%rip), %rdx      # 0xb770
movl %r12d, %ecx
xorl %eax, %eax
callq 0x30f0
movq %r13, %rdi
leaq 0x6e0b(%rip), %rsi      # 0xb31e
callq 0x3260
testq %rax, %rax
je 0x4573
movq %rax, %r13
leaq 0x240(%rsp), %r15
movq %r15, %rdi
movl $0x400, %esi            # imm = 0x400
movq %rax, %rdx
callq 0x3170
testq %rax, %rax
je 0x4564
movq (%rbp), %rdi
leaq 0x7236(%rip), %rsi      # 0xb77e
movsd 0x8(%rsp), %xmm0
movl %r12d, %edx
movb $0x1, %al
callq 0x31c0
movq (%rbp), %rsi
movq %r15, %rdi
callq 0x3100
movq %r13, %rdi
callq 0x30a0
leaq 0x723f(%rip), %r15      # 0xb7b2
leaq 0xcb7a(%rip), %rax      # 0x110f4
cmpl $0x0, (%rax)
je 0x4711
movl $0x80, %esi
leaq 0x50(%rsp), %r13
movq %r13, %rdi
leaq 0x71f6(%rip), %rdx      # 0xb78d
movl %r12d, %ecx
xorl %eax, %eax
callq 0x30f0
movq (%rbp), %rdi
leaq 0x71f2(%rip), %rsi      # 0xb79e
movsd 0x8(%rsp), %xmm0
movl %r12d, %edx
movb $0x1, %al
callq 0x31c0
movq %r13, %rdi
leaq 0x71e9(%rip), %rsi      # 0xb7af
callq 0x3260
testq %rax, %rax
je 0x4705
movq %rax, %r13
andq $0x0, 0x18(%rsp)
andq $0x0, 0x30(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
movq %r13, %rcx
callq 0x3330
cmpq $-0x1, %rax
je 0x46fd
movq (%rbp), %rdi
movq 0x18(%rsp), %rdx
movq %r15, %rsi
xorl %eax, %eax
callq 0x31c0
jmp 0x45e3
movq (%rbp), %rdi
leaq 0x7137(%rip), %rsi      # 0xb754
movl %r12d, %edx
movb $0x1, %al
callq 0x31c0
movq (%rbp), %rdi
callq 0x3200
leaq 0x712e(%rip), %rdi      # 0xb765
xorl %eax, %eax
callq 0x94bd
cmpl 0xcaf3(%rip), %r12d     # 0x11138
jne 0x43e0
jmp 0x4ac6
cmpb $0x7f, %cl
jne 0x479f
movl %ecx, %eax
shrl $0x8, %eax
cmpb $0x5, %al
je 0x467e
movzbl %al, %ecx
cmpl $0x13, %ecx
jne 0x47ce
leaq 0x7202(%rip), %rdi      # 0xb877
xorl %eax, %eax
callq 0x94bd
jmp 0x46d7
movsd %xmm0, 0x8(%rsp)
movl %ecx, %eax
sarl $0x10, %eax
leal -0x1(%rax), %edx
cmpl $0x3, %edx
jb 0x474f
cmpl $0x6, %eax
je 0x48d4
movq (%rbp), %rdi
leaq 0x7196(%rip), %rsi      # 0xb83f
movsd 0x8(%rsp), %xmm0
movl %r12d, %edx
movb $0x1, %al
callq 0x31c0
movq (%rbp), %rdi
callq 0x3200
movl 0x10(%rsp), %edx
leaq 0x7187(%rip), %rdi      # 0xb854
movl %r12d, %esi
xorl %eax, %eax
callq 0x94bd
leaq 0xca1e(%rip), %rax      # 0x110fc
cmpl $0x0, (%rax)
pushq $0x18
popq %rdi
pushq $0x7
popq %rax
cmovel %eax, %edi
movl %r12d, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x3230
jmp 0x43e0
movq %r13, %rdi
callq 0x30a0
movq (%rbp), %rsi
pushq $0xa
popq %rdi
callq 0x3150
movq (%rbp), %rdi
movl 0x10(%rsp), %ecx
andl $0x7f, %ecx
leaq 0x7093(%rip), %rsi      # 0xb7b6
movsd 0x8(%rsp), %xmm0
movl %r12d, %edx
movb $0x1, %al
callq 0x31c0
movq (%rbp), %rdi
callq 0x3200
leaq 0x7087(%rip), %rdi      # 0xb7ca
xorl %eax, %eax
callq 0x94bd
jmp 0x43e0
movl $0x4201, %edi           # imm = 0x4201
movl %r12d, %esi
xorl %edx, %edx
leaq 0x20(%rsp), %rcx
xorl %eax, %eax
callq 0x3230
movq (%rbp), %rdi
movq 0x20(%rsp), %rcx
leaq 0x7062(%rip), %rsi      # 0xb7d7
movsd 0x8(%rsp), %xmm0
movl %r12d, %edx
movb $0x1, %al
callq 0x31c0
movq (%rbp), %rdi
callq 0x3200
movq 0x20(%rsp), %rsi
leaq 0x7050(%rip), %rdi      # 0xb7ea
jmp 0x4aba
cmpl $0xffff, %ecx           # imm = 0xFFFF
jne 0x46d7
movq (%rbp), %rdi
leaq 0x70f3(%rip), %rsi      # 0xb8a9
movl %r12d, %edx
movb $0x1, %al
callq 0x31c0
movq (%rbp), %rdi
callq 0x3200
jmp 0x46d7
cmpl $0x85, %ecx
jne 0x488a
movsd %xmm0, 0x8(%rsp)
pushq $0xc
popq %rdi
movl %r12d, %esi
xorl %edx, %edx
leaq 0xd8(%rsp), %rcx
xorl %eax, %eax
callq 0x3230
movslq 0x28(%rsp), %rax
movq 0x150(%rsp), %rdx
pushq $0x1
popq %rcx
movl %ecx, %r13d
subl 0x14(%rsp), %r13d
cmpq %rax, %rdx
cmovnel %ecx, %r13d
leaq 0xc8db(%rip), %rax      # 0x110f8
cmpl $0x0, (%rax)
setne %al
movq %rdx, %rcx
movq %rdx, 0x28(%rsp)
xorl $0x101, %ecx            # imm = 0x101
orl %r13d, %ecx
sete %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x486c
movq 0x140(%rsp), %rsi
movl %r12d, %edi
callq 0x42f6
movq (%rbp), %rdi
leaq 0x702a(%rip), %rsi      # 0xb883
movsd 0x8(%rsp), %xmm0
movl %r12d, %edx
movq %rax, %rcx
movb $0x1, %al
callq 0x31c0
pushq $0x18
popq %rdi
movl %r12d, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %eax, %eax
callq 0x3230
movl %r13d, 0x14(%rsp)
jmp 0x46d7
movzbl %al, %ecx
movq (%rbp), %rdi
leaq 0x6ffd(%rip), %rsi      # 0xb895
movl %r12d, %edx
movb $0x1, %al
callq 0x31c0
movq (%rbp), %rdi
callq 0x3200
leaq 0xc84a(%rip), %rax      # 0x110fc
cmpl $0x0, (%rax)
pushq $0x18
popq %rdi
pushq $0x7
popq %rax
cmovel %eax, %edi
movzbl 0x11(%rsp), %ecx
movl %r12d, %esi
xorl %edx, %edx
xorl %eax, %eax
callq 0x3230
jmp 0x46d7
movl $0x4201, %edi           # imm = 0x4201
movl %r12d, %esi
xorl %edx, %edx
leaq 0x20(%rsp), %rcx
xorl %eax, %eax
callq 0x3230
movl $0x80, %esi
leaq 0x50(%rsp), %r13
movq %r13, %rdi
leaq 0x6e72(%rip), %rdx      # 0xb770
movl %r12d, %ecx
xorl %eax, %eax
callq 0x30f0
movq %r13, %rdi
leaq 0x6a0c(%rip), %rsi      # 0xb31e
callq 0x3260
testq %rax, %rax
je 0x4972
movq %rax, %r13
leaq 0x240(%rsp), %r15
movq %r15, %rdi
movl $0x400, %esi            # imm = 0x400
movq %rax, %rdx
callq 0x3170
testq %rax, %rax
je 0x4963
movq (%rbp), %rdi
leaq 0x6e37(%rip), %rsi      # 0xb77e
movsd 0x8(%rsp), %xmm0
movl %r12d, %edx
movb $0x1, %al
callq 0x31c0
movq (%rbp), %rsi
movq %r15, %rdi
callq 0x3100
movq %r13, %rdi
callq 0x30a0
leaq 0x6e40(%rip), %r15      # 0xb7b2
leaq 0xc77b(%rip), %rax      # 0x110f4
cmpl $0x0, (%rax)
je 0x4a1d
movl $0x80, %esi
leaq 0x50(%rsp), %r13
movq %r13, %rdi
leaq 0x6df7(%rip), %rdx      # 0xb78d
movl %r12d, %ecx
xorl %eax, %eax
callq 0x30f0
movq (%rbp), %rdi
leaq 0x6df3(%rip), %rsi      # 0xb79e
movsd 0x8(%rsp), %xmm0
movl %r12d, %edx
movb $0x1, %al
callq 0x31c0
movq %r13, %rdi
leaq 0x6dea(%rip), %rsi      # 0xb7af
callq 0x3260
movq %rax, %r13
testq %rax, %rax
je 0x4a09
andq $0x0, 0x18(%rsp)
andq $0x0, 0x30(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
movq %r13, %rcx
callq 0x3330
cmpq $-0x1, %rax
je 0x4a09
movq (%rbp), %rdi
movq 0x18(%rsp), %rdx
movq %r15, %rsi
xorl %eax, %eax
callq 0x31c0
jmp 0x49de
movq (%rbp), %rsi
pushq $0xa
popq %rdi
callq 0x3150
movq %r13, %rdi
callq 0x30a0
movl $0x80, %esi
leaq 0x50(%rsp), %r13
movq %r13, %rdi
leaq 0x6dd7(%rip), %rdx      # 0xb808
movl %r12d, %ecx
xorl %eax, %eax
callq 0x30f0
movq %r13, %rdi
leaq 0x68d9(%rip), %rsi      # 0xb31e
callq 0x3260
testq %rax, %rax
je 0x4aae
movq %rax, %r13
leaq 0x240(%rsp), %r15
movq %r15, %rdi
movl $0x400, %esi            # imm = 0x400
movq %rax, %rdx
callq 0x3170
testq %rax, %rax
je 0x4a9f
movq (%rbp), %rdi
leaq 0x6d9c(%rip), %rsi      # 0xb816
movsd 0x8(%rsp), %xmm0
movl %r12d, %edx
movb $0x1, %al
callq 0x31c0
movq (%rbp), %rsi
movq %r15, %rdi
callq 0x3100
movq (%rbp), %rdi
callq 0x3200
movq %r13, %rdi
callq 0x30a0
leaq 0x6d04(%rip), %r15      # 0xb7b2
movq 0x20(%rsp), %rsi
leaq 0x6d6b(%rip), %rdi      # 0xb825
xorl %eax, %eax
callq 0x94bd
jmp 0x46d7
addq $0x648, %rsp            # imm = 0x648
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq