// File: /workspace/github_cpp_makefile_star2_O3/overfl0[P]iojack/iojack.cpp
// Function: main
int main(int argc, char *argv[])
{
    pthread_t pollStdinThread;
    setSignalHandlers();
    initSyscallHooks();

    int firstPid = getArgs(argc, argv);

    printf("Attaching to pid: %d\n", firstPid);
    if(ptrace(PTRACE_ATTACH, firstPid, NULL, NULL) == -1)
    {
        if(errno == EPERM)
        {
            pexit("Could not attach to process due to lack of permissions\n"
                  "If you're on ubuntu, you may try the following as root:\n"
                  "echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope\n");
        }
        else
            perrorexit("PTRACE_ATTACH");
    }

    int status;
    // Should be checked
    firstPid = wait(&status);
    if(WIFEXITED(status))
    {
        printf("Process %d exited\n", firstPid);
        return 0;
    }

    if(ptrace(PTRACE_SETOPTIONS, firstPid, NULL,
        PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK | PTRACE_O_TRACECLONE | PTRACE_O_TRACEEXEC
        /*| PTRACE_O_TRACESYSGOOD ? */) == -1)
        perrorexit("PTRACE_SETOPTIONS");

    int retval = pthread_create(&pollStdinThread, NULL, stdinPoll, (void*)&inputBuffer);
    if(retval)
        perrorexit("Reading input thread");

    processes[firstPid] = new processInfo(firstPid);
    processes[firstPid]->guessFds();

    // We are interested only in syscalls
    processes[firstPid]->stopAtSyscall();

    unsigned char applicationSequences[] = {0x1b, 0x5b, 0x3f, 0x31, 0x68};
    if(!globalSettings.sendANSI)
        printf("%s", applicationSequences);

    //=========================================================================
    while(!processes.empty())
    {
        // Wait for a syscall to be called
        int pidReceived = wait(&status);
        if(pidReceived == -1)
        {
            perror("wait");
            continue;
        }

        // pidReceived != -1, so get full info about this process
        processes_t::iterator it = processes.find(pidReceived);
        if(it == processes.end())
        {
            // On purpose. This may be a bug that should not get unnoticed!
            //pexit("Unexpected pid %d received by wait call\n", pidReceived);

            // Too bad that this may happen on clone() and fork() calls :(
            // Ugly workaround
            // FIXME TODO FIXME TODO This is WAY too ugly to be kept unchanged
            processes[pidReceived] = new processInfo(pidReceived);
            it = processes.find(pidReceived);
            it->second->sigstopNewChild = 1;
        }
        processInfo * pi = it->second;

        if(WIFEXITED(status) || WIFSIGNALED(status))
        {
            dprintf("Process %d exited\n", pidReceived);
            processes.erase(pidReceived);

            continue;
        }

        if(WIFSTOPPED(status) && WSTOPSIG(status) == SIGSTOP)
        {
            dprintf("[%d] ******WIFSTOPPED!******\n", pi->pid);
            if(pi->sigstopToDetach)
            {
                // That's our chance! :)
                printf("Pid %d stopped while we wanted to quit. Trying to detach now...\n", pi->pid);
                pi->detachProcess();
                processes.erase(pidReceived);
                continue;
            }

            if(pi->sigstopToRestartSyscall)
            {
                dprintf("Restarting tracing of pid %d\n", pi->pid);
                pi->sigstopToRestartSyscall = 0;

                pi->stopAtSyscall();
                continue;
            }

            if(pi->sigstopNewChild)
            {
                pi->sigstopNewChild = 0;
                dprintf("[%d] The new child has stopped.\n", pi->pid);

                if(ptrace(PTRACE_SETOPTIONS, pi->pid, NULL,
                   PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK | PTRACE_O_TRACECLONE | PTRACE_O_TRACEEXEC
                   /*| PTRACE_O_TRACESYSGOOD ? */) == -1)
                    perrorexit("PTRACE_SETOPTIONS");

                pi->stopAtSyscall();
                continue;
            }
        }

        // If it's not our signal, forward it to the program and continue the loop
        if(WIFSTOPPED(status) && WSTOPSIG(status) != SIGTRAP)
        {
            dprintf("Pid %d stopped with signal: %d\n", pi->pid, WSTOPSIG(status));

            pi->stopAtSyscall(WSTOPSIG(status));
            continue;
        }

        // Check if we have got a new child
        // FIXME: Find a macro that returns the event
        int event = status >> 16;
        if(event == PTRACE_EVENT_FORK || event == PTRACE_EVENT_VFORK || event == PTRACE_EVENT_CLONE)
        {
            unsigned long newPid;
            if(ptrace(PTRACE_GETEVENTMSG, pi->pid, 0, &newPid) == -1)
                perrorexit("PTRACE_GETEVENTMSG");
            printf("[%d] A new process forked/vforked/cloned: %lu\n", pi->pid, newPid);

            processInfo *newPi = new processInfo(newPid);
            newPi->sigstopNewChild = 1;
            // Copy file descriptors from the parent
            //FIXME: Race condition with pi->pid calling close() before we get here
            newPi->stdin = pi->stdin;
            newPi->stdout = pi->stdout;
            newPi->stderr = pi->stderr;
            processes[newPid] = newPi;

            pi->stopAtSyscall();

            continue;
        }
        if(event == PTRACE_EVENT_EXEC)
        {
            // TODO: We probably need to close some fds here (FD_CLOEXEC)
            dprintf("###################################################################\n");
            dprintf("Wykryto exec!\n");
            dprintf("###################################################################\n");
            pi->stopAtSyscall();

            continue;
        }

        //=Handle syscalls and registers=======================================
        struct user_regs_struct regs;
        if(ptrace((__ptrace_request)PTRACE_GETREGS, pi->pid, 0, &regs) == -1)
            perrorexit("PTRACE_GETREGS");

        int saveRegs = 0;
        processSyscall(pi, &regs, &saveRegs);
        if(saveRegs)
        {
            if(ptrace((__ptrace_request)PTRACE_SETREGS, pi->pid, 0, &regs) == -1)
                perrorexit("PTRACE_SETREGS");
        }
        //=====================================================================

        if(wantToExit && pi->inSyscall == 0 && pi->fakingSyscall != -1)
        {
            pi->detachProcess();
            processes.erase(pi->pid);
            continue;
        }

        // We are interested only in syscalls
        pi->stopAtSyscall();
    }

    wantToExit = 1;
    pthread_join(pollStdinThread, NULL);

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq %rsi, %rbx
movl %edi, %ebp
leaq 0x28(%rsp), %r14
movl $0x90, %edx
movq %r14, %rdi
xorl %esi, %esi
callq 0x20d0
leaq -0x43a(%rip), %rax      # 0x273c
movq %rax, -0x8(%r14)
movl $0x10000000, 0x80(%r14) # imm = 0x10000000
leaq 0x20(%rsp), %rsi
movl $0x2, %edi
xorl %edx, %edx
callq 0x2200
callq 0x5712
movl %ebp, %edi
movq %rbx, %rsi
callq 0x26ab
movl %eax, %ebx
leaq 0x3538(%rip), %rdi      # 0x60e6
movl %eax, %esi
xorl %eax, %eax
callq 0x2040
movl $0x10, %edi
movl %ebx, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x2140
cmpq $-0x1, %rax
je 0x31df
leaq 0x8(%rsp), %rbx
movq %rbx, %rdi
callq 0x21f0
movl %eax, 0x14(%rsp)
testb $0x7f, (%rbx)
je 0x31bb
movl $0x4200, %edi           # imm = 0x4200
movl %eax, %esi
xorl %edx, %edx
movl $0x1e, %ecx
xorl %eax, %eax
callq 0x2140
cmpq $-0x1, %rax
je 0x3204
leaq -0x386(%rip), %rdx      # 0x288d
leaq 0x865e(%rip), %rcx      # 0xb278
xorl %r15d, %r15d
leaq 0x18(%rsp), %rdi
xorl %esi, %esi
callq 0x2320
testl %eax, %eax
jne 0x3209
movl $0x180, %edi            # imm = 0x180
callq 0x2170
movq %rax, %r14
leaq 0x14(%rsp), %rbx
movl (%rbx), %eax
leaq 0xf8(%r14), %rcx
movl %r15d, 0xf8(%r14)
movq %r15, 0x100(%r14)
movq %rcx, 0x108(%r14)
movq %rcx, 0x110(%r14)
movq %r15, 0x118(%r14)
leaq 0x128(%r14), %rcx
movl %r15d, 0x128(%r14)
movq %r15, 0x130(%r14)
movq %rcx, 0x138(%r14)
movq %rcx, 0x140(%r14)
movq %r15, 0x148(%r14)
leaq 0x158(%r14), %rcx
movl %r15d, 0x158(%r14)
movq %r15, 0x160(%r14)
movq %rcx, 0x168(%r14)
movq %rcx, 0x170(%r14)
movq %r15, 0x178(%r14)
movl %eax, (%r14)
movabsq $-0x100000000, %rax     # imm = 0xFFFFFFFF00000000
movq %rax, 0x4(%r14)
leaq 0xc(%r14), %rdi
movl $0xe4, %edx
xorl %esi, %esi
callq 0x20d0
leaq 0x84d5(%rip), %r15      # 0xb1c0
movq %r15, %rdi
movq %rbx, %rsi
callq 0x3228
movq %r14, (%rax)
movq %r15, %rdi
movq %rbx, %rsi
callq 0x3228
movq (%rax), %rdi
callq 0x4894
movq %r15, %rdi
movq %rbx, %rsi
callq 0x3228
movq (%rax), %rdi
xorl %esi, %esi
callq 0x4874
movb $0x68, 0x10(%rsp)
movl $0x313f5b1b, 0xc(%rsp)  # imm = 0x313F5B1B
cmpb $0x0, 0x8483(%rip)      # 0xb1b8
jne 0x2d4a
leaq 0x4d0a(%rip), %rdi      # 0x7a48
leaq 0xc(%rsp), %rsi
xorl %eax, %eax
callq 0x2040
cmpq $0x0, 0x8496(%rip)      # 0xb1e8
je 0x31a3
xorl %ebp, %ebp
leaq 0x845f(%rip), %r15      # 0xb1c0
leaq 0x4(%rsp), %r12
leaq 0x845b(%rip), %rbx      # 0xb1c8
leaq 0x20(%rsp), %r13
leaq 0x8(%rsp), %rdi
callq 0x21f0
movl %eax, 0x4(%rsp)
cmpl $-0x1, %eax
je 0x30b0
movl %eax, %r14d
movq 0x843d(%rip), %rcx      # 0xb1d0
testq %rcx, %rcx
je 0x2dc1
movq %rbx, %rax
xorl %edx, %edx
cmpl %r14d, 0x20(%rcx)
setl %dl
cmovgeq %rcx, %rax
movq 0x10(%rcx,%rdx,8), %rcx
testq %rcx, %rcx
jne 0x2d9b
cmpq %rbx, %rax
je 0x2dc1
cmpl 0x20(%rax), %r14d
jge 0x2f3f
movl $0x180, %edi            # imm = 0x180
callq 0x2170
movq %rax, %r13
addq $0xf8, %rax
movl %ebp, 0xf8(%r13)
movq %rbp, 0x100(%r13)
movq %rax, 0x108(%r13)
movq %rax, 0x110(%r13)
movq %rbp, 0x118(%r13)
leaq 0x128(%r13), %rax
movl %ebp, 0x128(%r13)
movq %rbp, 0x130(%r13)
movq %rax, 0x138(%r13)
movq %rax, 0x140(%r13)
movq %rbp, 0x148(%r13)
leaq 0x158(%r13), %rax
movl %ebp, 0x158(%r13)
movq %rbp, 0x160(%r13)
movq %rax, 0x168(%r13)
movq %rax, 0x170(%r13)
movq %rbp, 0x178(%r13)
movl %r14d, (%r13)
movabsq $-0x100000000, %rax     # imm = 0xFFFFFFFF00000000
movq %rax, 0x4(%r13)
leaq 0xc(%r13), %rdi
movl $0xe4, %edx
xorl %esi, %esi
callq 0x20d0
movq %r15, %rdi
movq %r12, %rsi
callq 0x3228
movq %r13, (%rax)
movq 0x834e(%rip), %rdx      # 0xb1d0
movq %rbx, %rsi
testq %rdx, %rdx
je 0x2eba
movl 0x4(%rsp), %ecx
movq %rbx, %rax
xorl %esi, %esi
cmpl %ecx, 0x20(%rdx)
setl %sil
cmovgeq %rdx, %rax
movq 0x10(%rdx,%rsi,8), %rdx
testq %rdx, %rdx
jne 0x2e91
movq %rbx, %rsi
cmpq %rbx, %rax
je 0x2eba
cmpl 0x20(%rax), %ecx
cmovlq %rbx, %rax
movq %rax, %rsi
movq 0x28(%rsi), %r14
movl $0x1, 0x14(%r14)
leaq 0x20(%rsp), %r13
movl 0x8(%rsp), %eax
movl %eax, %ecx
andl $0x7f, %ecx
je 0x2f16
shll $0x18, %ecx
addl $0x1000000, %ecx        # imm = 0x1000000
cmpl $0x2000000, %ecx        # imm = 0x2000000
jge 0x2f16
movzwl %ax, %ecx
cmpl $0x137f, %ecx           # imm = 0x137F
jne 0x2f45
cmpl $0x0, 0xc(%r14)
je 0x314b
movl (%r14), %esi
leaq 0x323d(%rip), %rdi      # 0x6144
xorl %eax, %eax
callq 0x2040
movq %r14, %rdi
callq 0x47be
movq %r15, %rdi
movq %r12, %rsi
callq 0x3814
movq %r15, %rdi
movq %rax, %rsi
callq 0x3892
cmpq $0x0, 0x82b4(%rip)      # 0xb1e8
jne 0x2d72
jmp 0x31a3
movq 0x28(%rax), %r14
jmp 0x2ecb
cmpb $0x7f, %al
jne 0x2f55
movzbl %ah, %esi
cmpl $0x5, %esi
jne 0x315c
sarl $0x10, %eax
leal -0x1(%rax), %ecx
cmpl $0x2, %ecx
ja 0x30c1
movl (%r14), %esi
movl $0x4201, %edi           # imm = 0x4201
xorl %edx, %edx
movq %r13, %rcx
xorl %eax, %eax
callq 0x2140
cmpq $-0x1, %rax
je 0x31ff
movl (%r14), %esi
movq 0x20(%rsp), %rdx
leaq 0x3207(%rip), %rdi      # 0x6198
xorl %eax, %eax
callq 0x2040
movl $0x180, %edi            # imm = 0x180
callq 0x2170
movq %rax, %r13
movl 0x20(%rsp), %eax
leaq 0xf8(%r13), %rcx
movl %ebp, 0xf8(%r13)
movq %rbp, 0x100(%r13)
movq %rcx, 0x108(%r13)
movq %rcx, 0x110(%r13)
movq %rbp, 0x118(%r13)
leaq 0x128(%r13), %rcx
movl %ebp, 0x128(%r13)
movq %rbp, 0x130(%r13)
movq %rcx, 0x138(%r13)
movq %rcx, 0x140(%r13)
movq %rbp, 0x148(%r13)
leaq 0x158(%r13), %rcx
movl %ebp, 0x158(%r13)
movq %rbp, 0x160(%r13)
movq %rcx, 0x168(%r13)
movq %rcx, 0x170(%r13)
movq %rbp, 0x178(%r13)
movl %eax, (%r13)
movabsq $-0x100000000, %rax     # imm = 0xFFFFFFFF00000000
movq %rax, 0x4(%r13)
leaq 0xc(%r13), %rdi
movl $0xe4, %edx
xorl %esi, %esi
callq 0x20d0
movl $0x1, 0x14(%r13)
movl $0xf0, %eax
leaq (%r14,%rax), %rsi
leaq (%rax,%r13), %rdi
callq 0x334e
movl $0x120, %eax            # imm = 0x120
leaq (%r14,%rax), %rsi
leaq (%rax,%r13), %rdi
callq 0x334e
movl $0x150, %eax            # imm = 0x150
leaq (%r14,%rax), %rsi
leaq (%rax,%r13), %rdi
callq 0x334e
movl 0x20(%rsp), %eax
movl %eax, (%rsp)
movq %r15, %rdi
movq %rsp, %rsi
callq 0x328e
movq %r13, (%rax)
movq %r14, %rdi
xorl %esi, %esi
callq 0x4874
leaq 0x20(%rsp), %r13
jmp 0x2f2c
leaq 0x4455(%rip), %rdi      # 0x750c
callq 0x2130
jmp 0x2f2c
cmpl $0x4, %eax
je 0x3194
movl (%r14), %esi
movl $0xc, %edi
xorl %edx, %edx
movq %r13, %rcx
xorl %eax, %eax
callq 0x2140
cmpq $-0x1, %rax
je 0x3213
movl $0x0, (%rsp)
movq %r14, %rdi
movq %r13, %rsi
movq %rsp, %rdx
callq 0x2a0d
cmpl $0x0, (%rsp)
je 0x3121
movl (%r14), %esi
movl $0xd, %edi
xorl %edx, %edx
movq %r13, %rcx
xorl %eax, %eax
callq 0x2140
cmpq $-0x1, %rax
je 0x3218
cmpl $0x0, 0x8094(%rip)      # 0xb1bc
je 0x3194
cmpl $0x0, 0x4(%r14)
jne 0x3194
cmpl $-0x1, 0x8(%r14)
je 0x3194
movq %r14, %rdi
callq 0x47be
movq %r15, %rdi
movq %r14, %rsi
jmp 0x2f1c
cmpl $0x0, 0x10(%r14)
je 0x3161
movl $0x0, 0x10(%r14)
jmp 0x3194
movq %r14, %rdi
jmp 0x3199
cmpl $0x0, 0x14(%r14)
je 0x2f49
movl $0x0, 0x14(%r14)
movl (%r14), %esi
movl $0x4200, %edi           # imm = 0x4200
xorl %edx, %edx
movl $0x1e, %ecx
xorl %eax, %eax
callq 0x2140
cmpq $-0x1, %rax
je 0x321d
movq %r14, %rdi
xorl %esi, %esi
callq 0x4874
jmp 0x2f2c
movl $0x1, 0x800f(%rip)      # 0xb1bc
movq 0x18(%rsp), %rdi
xorl %esi, %esi
callq 0x2160
jmp 0x31cb
leaq 0x2f48(%rip), %rdi      # 0x610a
movl %eax, %esi
xorl %eax, %eax
callq 0x2040
xorl %eax, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x2030
cmpl $0x1, (%rax)
jne 0x320e
leaq 0x30c6(%rip), %rdi      # 0x62b6
callq 0x22b0
movl $0x1, %edi
callq 0x21e0
callq 0x23d7
callq 0x23ec
callq 0x2383
callq 0x2401
callq 0x23c2
callq 0x23ad
callq 0x2398