// File: /workspace/github_cpp_makefile_star2_O1/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 0x20(%rsp), %r14
movl $0x98, %edx
movq %r14, %rdi
xorl %esi, %esi
callq 0x20d0
leaq -0x435(%rip), %rax      # 0x273c
movq %rax, (%r14)
movl $0x10000000, 0x88(%r14) # imm = 0x10000000
movl $0x2, %edi
movq %r14, %rsi
xorl %edx, %edx
callq 0x2200
callq 0x57f8
movl %ebp, %edi
movq %rbx, %rsi
callq 0x26ab
movl %eax, 0xc(%rsp)
leaq 0x353e(%rip), %rdi      # 0x60e6
movl %eax, %esi
xorl %eax, %eax
callq 0x2040
movl 0xc(%rsp), %esi
movl $0x10, %edi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x2140
cmpq $-0x1, %rax
je 0x320f
leaq 0x8(%rsp), %rbx
movq %rbx, %rdi
callq 0x21f0
movl %eax, 0xc(%rsp)
testb $0x7f, (%rbx)
je 0x31eb
movl $0x4200, %edi           # imm = 0x4200
movl %eax, %esi
xorl %edx, %edx
movl $0x1e, %ecx
xorl %eax, %eax
callq 0x2140
cmpq $-0x1, %rax
je 0x3234
leaq -0x385(%rip), %rdx      # 0x288a
leaq 0x8662(%rip), %rcx      # 0xb278
xorl %r15d, %r15d
leaq 0x18(%rsp), %rdi
xorl %esi, %esi
callq 0x2320
testl %eax, %eax
jne 0x3239
movl $0x180, %edi            # imm = 0x180
callq 0x2170
movq %rax, %r14
leaq 0xc(%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 0x84d9(%rip), %r15      # 0xb1c0
movq %r15, %rdi
movq %rbx, %rsi
callq 0x3258
movq %r14, (%rax)
movq %r15, %rdi
movq %rbx, %rsi
callq 0x3258
movq (%rax), %rdi
callq 0x4932
movq %r15, %rdi
movq %rbx, %rsi
callq 0x3258
movq (%rax), %rdi
xorl %esi, %esi
callq 0x4912
movb $0x68, 0x14(%rsp)
movl $0x313f5b1b, 0x10(%rsp) # imm = 0x313F5B1B
cmpb $0x0, 0x8487(%rip)      # 0xb1b8
jne 0x2d46
leaq 0x4d0e(%rip), %rdi      # 0x7a48
leaq 0x10(%rsp), %rsi
xorl %eax, %eax
callq 0x2040
cmpq $0x0, 0x849a(%rip)      # 0xb1e8
je 0x31d3
leaq 0x8(%rsp), %r13
leaq 0x8468(%rip), %rbp      # 0xb1c8
xorl %ebx, %ebx
leaq 0x8457(%rip), %r15      # 0xb1c0
leaq 0x20(%rsp), %r12
movq %r13, %rdi
callq 0x21f0
movl %eax, (%rsp)
cmpl $-0x1, %eax
je 0x2f4d
movl %eax, %r14d
movq 0x8444(%rip), %rcx      # 0xb1d0
movq %rbp, %rax
testq %rcx, %rcx
je 0x2dae
movq %rbp, %rax
xorl %edx, %edx
cmpl %r14d, 0x20(%rcx)
setl %dl
cmovgeq %rcx, %rax
movq 0x10(%rcx,%rdx,8), %rcx
testq %rcx, %rcx
jne 0x2d97
movq %rbp, %rcx
cmpq %rbp, %rax
je 0x2dc1
cmpl 0x20(%rax), %r14d
cmovlq %rbp, %rax
movq %rax, %rcx
cmpq %rbp, %rcx
jne 0x2ed5
movl $0x180, %edi            # imm = 0x180
callq 0x2170
movq %rax, %r13
addq $0xf8, %rax
movl %ebx, 0xf8(%r13)
movq %rbx, 0x100(%r13)
movq %rax, 0x108(%r13)
movq %rax, 0x110(%r13)
movq %rbx, 0x118(%r13)
leaq 0x128(%r13), %rax
movl %ebx, 0x128(%r13)
movq %rbx, 0x130(%r13)
movq %rax, 0x138(%r13)
movq %rax, 0x140(%r13)
movq %rbx, 0x148(%r13)
leaq 0x158(%r13), %rax
movl %ebx, 0x158(%r13)
movq %rbx, 0x160(%r13)
movq %rax, 0x168(%r13)
movq %rax, 0x170(%r13)
movq %rbx, 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 %rsp, %rsi
callq 0x3258
movq %r13, (%rax)
movq 0x8345(%rip), %rcx      # 0xb1d0
movq %rbp, %rax
testq %rcx, %rcx
je 0x2eb0
movl (%rsp), %edx
movq %rbp, %rax
xorl %esi, %esi
cmpl %edx, 0x20(%rcx)
setl %sil
cmovgeq %rcx, %rax
movq 0x10(%rcx,%rsi,8), %rcx
testq %rcx, %rcx
jne 0x2e99
movq %rbp, %rcx
cmpq %rbp, %rax
je 0x2ec5
movl (%rsp), %ecx
cmpl 0x20(%rax), %ecx
cmovlq %rbp, %rax
movq %rax, %rcx
movq 0x28(%rcx), %rax
movl $0x1, 0x14(%rax)
leaq 0x8(%rsp), %r13
movl 0x8(%rsp), %eax
movl %eax, %edx
andl $0x7f, %edx
je 0x2f24
shll $0x18, %edx
addl $0x1000000, %edx        # imm = 0x1000000
cmpl $0x2000000, %edx        # imm = 0x2000000
jge 0x2f24
movq 0x28(%rcx), %r14
movzwl %ax, %ecx
cmpl $0x137f, %ecx           # imm = 0x137F
jne 0x2fa3
cmpl $0x0, 0xc(%r14)
je 0x2f5b
movl (%r14), %esi
leaq 0x322f(%rip), %rdi      # 0x6144
xorl %eax, %eax
callq 0x2040
movq %r14, %rdi
callq 0x485c
movq %r15, %rdi
movq %rsp, %rsi
callq 0x3846
movq %r15, %rdi
movq %rax, %rsi
callq 0x38be
cmpq $0x0, 0x82a6(%rip)      # 0xb1e8
jne 0x2d6e
jmp 0x31d3
leaq 0x45b8(%rip), %rdi      # 0x750c
callq 0x2130
jmp 0x2f3a
cmpl $0x0, 0x10(%r14)
je 0x2f6f
movl $0x0, 0x10(%r14)
jmp 0x311a
cmpl $0x0, 0x14(%r14)
je 0x2fa3
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
jne 0x311a
jmp 0x324d
movzbl %al, %ecx
cmpl $0x7f, %ecx
jne 0x2fb7
movzbl %ah, %esi
cmpl $0x5, %esi
jne 0x3121
sarl $0x10, %eax
leal -0x1(%rax), %ecx
cmpl $0x2, %ecx
ja 0x3115
movl (%r14), %esi
movl $0x4201, %edi           # imm = 0x4201
xorl %edx, %edx
movq %r12, %rcx
xorl %eax, %eax
callq 0x2140
cmpq $-0x1, %rax
je 0x322f
movl (%r14), %esi
movq 0x20(%rsp), %rdx
leaq 0x31a5(%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 %ebx, 0xf8(%r13)
movq %rbx, 0x100(%r13)
movq %rcx, 0x108(%r13)
movq %rcx, 0x110(%r13)
movq %rbx, 0x118(%r13)
leaq 0x128(%r13), %rcx
movl %ebx, 0x128(%r13)
movq %rbx, 0x130(%r13)
movq %rcx, 0x138(%r13)
movq %rcx, 0x140(%r13)
movq %rbx, 0x148(%r13)
leaq 0x158(%r13), %rcx
movl %ebx, 0x158(%r13)
movq %rbx, 0x160(%r13)
movq %rcx, 0x168(%r13)
movq %rcx, 0x170(%r13)
movq %rbx, 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 0x337c
movl $0x120, %eax            # imm = 0x120
leaq (%r14,%rax), %rsi
leaq (%rax,%r13), %rdi
callq 0x337c
movl $0x150, %eax            # imm = 0x150
leaq (%r14,%rax), %rsi
leaq (%rax,%r13), %rdi
callq 0x337c
movl 0x20(%rsp), %eax
movl %eax, 0x4(%rsp)
movq %r15, %rdi
leaq 0x4(%rsp), %rsi
callq 0x32bc
movq %r13, (%rax)
movq %r14, %rdi
xorl %esi, %esi
callq 0x4912
leaq 0x8(%rsp), %r13
jmp 0x2f3a
cmpl $0x4, %eax
jne 0x312e
movq %r14, %rdi
xorl %esi, %esi
jmp 0x3124
movq %r14, %rdi
callq 0x4912
jmp 0x2f3a
movl (%r14), %esi
movl $0xc, %edi
xorl %edx, %edx
movq %r12, %r15
movq %r12, %rcx
xorl %eax, %eax
callq 0x2140
cmpq $-0x1, %rax
je 0x3243
movl $0x0, 0x4(%rsp)
movq %r14, %rdi
movq %r15, %rsi
leaq 0x4(%rsp), %rdx
callq 0x2a0a
cmpl $0x0, 0x4(%rsp)
je 0x318c
movl (%r14), %esi
movl $0xd, %edi
xorl %edx, %edx
movq %r15, %rcx
xorl %eax, %eax
callq 0x2140
cmpq $-0x1, %rax
je 0x3248
cmpl $0x0, 0x8029(%rip)      # 0xb1bc
je 0x319c
cmpl $0x0, 0x4(%r14)
je 0x31b2
movq %r14, %rdi
xorl %esi, %esi
callq 0x4912
leaq 0x8013(%rip), %r15      # 0xb1c0
jmp 0x2f3a
cmpl $-0x1, 0x8(%r14)
je 0x319c
movq %r14, %rdi
callq 0x485c
leaq 0x7ff8(%rip), %r15      # 0xb1c0
movq %r15, %rdi
movq %r14, %rsi
jmp 0x2f2a
movl $0x1, 0x7fdf(%rip)      # 0xb1bc
movq 0x18(%rsp), %rdi
xorl %esi, %esi
callq 0x2160
jmp 0x31fb
leaq 0x2f18(%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 0x323e
leaq 0x3096(%rip), %rdi      # 0x62b6
callq 0x22b0
movl $0x1, %edi
callq 0x21e0
callq 0x23d7
callq 0x23ec
callq 0x2383
callq 0x2401
callq 0x23c2
callq 0x23ad
callq 0x2398