<commit_before>/**
 * @file
 *
 * @brief
 *
 * @date 16.08.2011
 * @author <NAME>
 */
#include <types.h>
#include <errno.h>
#include <stdio.h>
#include <framework/example/self.h>
#include <kernel/thread/api.h>
#include <kernel/thread/event.h>
#include <kernel/thread/sched.h>
#include <unistd.h>
#include <assert.h>

#include <kernel/irq.h>
#include <kernel/printk.h>

#include <asm/traps.h>
#include <asm/flags.h>

#include <sys/user.h>

/**
 * This macro is used to register this example at the system.
 */
EMBOX_EXAMPLE(run);

static struct event breakpointHappened;

static unsigned counter = 0;

static irq_nr_t intNo = -1;

static void* threadB(void* args) {
  while (1) {
    sched_sleep(&breakpointHappened);
    if (intNo == 3) printk("int03. counter = %d\n", counter);
    ++counter;
  }
  return NULL;
}

static void setTraceFlagX86(void);
// static void clearTraceFlagX86(void);

static struct user lastProcessorState;

//static irq_return_t int03Handler(irq_nr_t irq_nr, void *dev_id) {
void int0301HandlerHack(pt_regs_t* regs) {<commit_msg>started doing ptrace<commit_after>/**
 * @file
 *
 * @brief
 *
 * @date 16.08.2011
 * @author <NAME>
 */
#include <types.h>
#include <errno.h>
#include <stdio.h>
#include <framework/example/self.h>
#include <kernel/thread/api.h>
#include <kernel/thread/event.h>
#include <kernel/thread/sched.h>
#include <unistd.h>
#include <assert.h>

#include <kernel/irq.h>
#include <kernel/printk.h>

#include <asm/traps.h>
#include <asm/flags.h>

#include <sys/user.h>
#include <sys/ptrace.h>

static struct event breakpointHappened;

long int ptrace (enum __ptrace_request request, thread_id_t thread_id, void *addr, void *data) {
  struct thread* t = thread_lookup(thread_id);

  if (t == NULL) {
    errno = ESRCH; // todo: is this the right way?
    return -1;
  }

  switch (request) {

    case PTRACE_ATTACH: break;
    case PTRACE_DETACH: break;
    case PTRACE_TRACEME: break;

    case PTRACE_PEEKTEXT:
    case PTRACE_PEEKDATA: {
      errno = ENOERR; // todo: is this the right way?
      return *(long int*)(addr);
    }

    case PTRACE_PEEKUSER:
    case PTRACE_POKEUSER:
    {
      unsigned x = (unsigned)addr;
      long int* a;
      if (x >= sizeof(struct user)) {
        errno = EIO; // todo: is this the right way?
	return -1;
      }
      a = (long int*)((unsigned)thread_get_ptrace_state(t) + (unsigned)addr);

      if (request == PTRACE_PEEKUSER)
        return *a;
      else {
        *a = *(long int*)data;
        return 0;
      }
    }

    case PTRACE_POKETEXT:
    case PTRACE_POKEDATA: {
      *(long int*)addr = *(long int*)data;
      break;
    }

    case PTRACE_GETREGS: {
      *(struct user_regs_struct*)data = thread_get_ptrace_state(t) -> regs;
      break;
    }

    case PTRACE_SETREGS: {
      thread_get_ptrace_state(t) -> regs = *(struct user_regs_struct*)data;
      break;
    }

    case PTRACE_SETFPREGS:
    case PTRACE_GETFPREGS: { // todo: no support for fp regs at all!
      break;
    }

    case PTRACE_SETSIGINFO:
    case PTRACE_SETOPTIONS: { // todo: no support
      break;
    }

    case PTRACE_CONT: {
      sched_sleep(&breakpointHappened); // wait for breakpoint/trace event to happen
      break;
    }

    case PTRACE_SINGLESTEP: {
      thread_get_ptrace_state(t) -> regs . eflags |= X86_EFLAGS_TF;
      sched_sleep(&breakpointHappened); // wait for breakpoint/trace event to happen
      thread_get_ptrace_state(t) -> regs . eflags &= ~X86_EFLAGS_TF;
      break;
    }

    case PTRACE_KILL: {
      break; // todo: how to kill a process?
    }

    default : {
      errno = EINVAL; // todo: is this the right way?
      return -1;
    }
  }
  errno = ENOERR; // todo: is this the right way?
  return 0;
}

/**
 * This macro is used to register this example at the system.
 */
EMBOX_EXAMPLE(run);

static struct event breakpointHappened;

static unsigned counter = 0;

static irq_nr_t intNo = -1;

static void* threadB(void* args) {
  while (1) {
    sched_sleep(&breakpointHappened);
    if (intNo == 3) printk("int03. counter = %d\n", counter);
    ++counter;
  }
  return NULL;
}

static void setTraceFlagX86(void);
// static void clearTraceFlagX86(void);

static struct user lastProcessorState;

//static irq_return_t int03Handler(irq_nr_t irq_nr, void *dev_id) {
void int0301HandlerHack(pt_regs_t* regs) {
{'fraction_non_alphanumeric': 0.12927054478301014, 'fraction_numerical': 0.013157894736842105, 'mean_word_length': 3.1227402473834442, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3307034', 'n_tokens_mistral': 1631, 'n_tokens_neox': 1581, 'n_words': 394}