<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

from bluebird import *

from os import getpid
from time import sleep
from threading import Thread


class TraceResults(object):

    def __init__(self, trace_value=None):
        self.trace_value = trace_value

    def __get__(self, obj, objtype):
        return self.trace_value

    def __set__(self, obj, value):
        if obj.tracing:
            raise RunningTraceError
        self.trace_value = value


class Bluebird(object):

    trace_results = TraceResults()

    def __init__(self, pid):
        self.pid = getpid()
        self.traced_pid = pid
        self.wstrs = {}
        self.rstrs = {}
        self.attached = False
        self.tracing = False
        # set attached pid COMM

    def start(self):
        # XXX handle already attached trace
        if not self.attached:
            attach(self.traced_pid)
            self.attached = True

    def stop(self):
        if self.attached:
            detach(self.traced_pid)
            self.attached = False

    def is_attached(self):
        status_fields = self._parse_status()
        for field in status_fields:
            if 'TracerPid' in field[0]:
                break
        if str(self.pid) != field[1]:
            return False
        return True
 
    def write(self, addr, data):
        if isinstance(data, int):
            writeint(self.traced_pid, addr, data)
        else:
            writestring(self.traced_pid, addr, data)

    def read(self, addr, nwords, readtype=str):
        if isinstance(readtype, int):
            return readint(self.traced_pid, addr, nwords)
        else:
            return readstring(self.traced_pid, addr, nwords)
            
    def get_current_call(self):
        return get_syscall(self.traced_pid)

    def get_ranged_syscalls(self, nsyscalls):
        # this allows the tracer get a consecutive count
        # syscalls without allowing any calls to slip by
        return get_syscalls(self.traced_pid, nsyscalls)

    def get_call(self, call, non_blocking=False, timeout=None):
        if non_blocking:
            if self.tracing:
                raise RunningTrace
            self.stop()
            sleep(1)
            trace_thread = TracingThread(self, find_syscall, None,
                                         self.traced_pid, call)
            trace_thread.start()
        else:    
            return find_syscall(self.traced_pid, call)

    def dump_exec(self):
        pass

    def exec_dir(self):
        #make_call(getcwd)
        pass

    def sbrk(self, amount):
        #make_call(sbrk, amount)
        pass

    def name(self):
        status = self._parse_status()
        self.name = status[0][1]

    def _parse_status(self):
        with open('/proc/{}/status'.format(self.traced_pid)) as f:
            status_raw = f.read()
        status = [field.split('\t') for field in status_raw.split('\n')]
        return status
        

class TracingThread(Thread):

    def __init__(self, trace_obj, trace_func, trace_cb, *args):
        super(TracingThread, self).__init__()
        self.trace_obj = trace_obj
        self.trace_func = trace_func
        self.trace_cb = trace_cb<commit_msg>Add in support for a timeout<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

from bluebird import *

from os import getpid
from time import sleep
from threading import Thread


class TraceResults(object):

    def __init__(self, trace_value=None):
        self.trace_value = trace_value

    def __get__(self, obj, objtype):
        return self.trace_value

    def __set__(self, obj, value):
        if obj.tracing:
            raise RunningTraceError
        self.trace_value = value


class Bluebird(object):

    trace_results = TraceResults()

    def __init__(self, pid):
        self.pid = getpid()
        self.traced_pid = pid
        self.wstrs = {}
        self.rstrs = {}
        self.attached = False
        self.tracing = False
        # set attached pid COMM

    def start(self):
        # XXX handle already attached trace
        if not self.attached:
            attach(self.traced_pid)
            self.attached = True

    def stop(self):
        if self.attached:
            detach(self.traced_pid)
            self.attached = False

    def is_attached(self):
        status_fields = self._parse_status()
        for field in status_fields:
            if 'TracerPid' in field[0]:
                break
        if str(self.pid) != field[1]:
            return False
        return True
 
    def write(self, addr, data):
        if isinstance(data, int):
            writeint(self.traced_pid, addr, data)
        else:
            writestring(self.traced_pid, addr, data)

    def read(self, addr, nwords, readtype=str):
        if isinstance(readtype, int):
            return readint(self.traced_pid, addr, nwords)
        else:
            return readstring(self.traced_pid, addr, nwords)
            
    def get_current_call(self):
        return get_syscall(self.traced_pid)

    def get_ranged_syscalls(self, nsyscalls):
        # this allows the tracer get a consecutive count
        # syscalls without allowing any calls to slip by
        return get_syscalls(self.traced_pid, nsyscalls)

    def get_call(self, call, non_blocking=False, timeout=0):
        if non_blocking:
            if self.tracing:
                raise RunningTrace
            self.stop()
            sleep(1)
            trace_thread = TracingThread(self, find_syscall, None,
                                         self.traced_pid, call, timeout, 1)
            trace_thread.start()
        else:    
            find_syscall(self.traced_pid, call, timeout, 0)

    def dump_exec(self):
        pass

    def exec_dir(self):
        #make_call(getcwd)
        pass

    def sbrk(self, amount):
        #make_call(sbrk, amount)
        pass

    def name(self):
        status = self._parse_status()
        self.name = status[0][1]

    def _parse_status(self):
        with open('/proc/{}/status'.format(self.traced_pid)) as f:
            status_raw = f.read()
        status = [field.split('\t') for field in status_raw.split('\n')]
        return status
        

class TracingThread(Thread):

    def __init__(self, trace_obj, trace_func, trace_cb, *args):
        super(TracingThread, self).__init__()
        self.trace_obj = trace_obj
        self.trace_func = trace_func
        self.trace_cb = trace_cb
{'fraction_non_alphanumeric': 0.09157224895235139, 'fraction_numerical': 0.0023281080242123233, 'mean_word_length': 1.99581589958159, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '592063', 'n_tokens_mistral': 1963, 'n_tokens_neox': 1886, 'n_words': 477}