<commit_before>#include <iostream>
#include <unistd.h>

#include "attached_probe.h"
#include "libbpf.h"
#include "perf_reader.h"

namespace ebpf {
namespace bpftrace {

AttachedProbe::AttachedProbe(Probe &probe, std::tuple<uint8_t *, uintptr_t> &func)
  : probe_(probe), func_(func)
{
  load_prog();
  switch (probe_.type)
  {
    case ProbeType::kprobe:
    case ProbeType::kretprobe:
      attach_kprobe();
      break;
    default:
      abort();
  }
}

AttachedProbe::~AttachedProbe()
{
  perf_reader_free(perf_reader_);
  close(progfd_);
  int err = 0;
  switch (probe_.type)
  {
    case ProbeType::kprobe:
    case ProbeType::kretprobe:
      err = bpf_detach_kprobe(eventname());
      break;
    default:
      abort();
  }
  if (err)
    std::cerr << "Error detaching probe: " << probe_.name << std::endl;
}

const char *AttachedProbe::eventname() const
{
  std::string event;
  switch (probe_.type)
  {
    case ProbeType::kprobe:
      event =  "p_" + probe_.attach_point;
      break;
    case ProbeType::kretprobe:
      event =  "r_" + probe_.attach_point;
      break;
    default:
      abort();
  }
  return event.c_str();
}

void AttachedProbe::load_prog()
{
  uint8_t *insns = std::get<0>(func_);
  int prog_len = std::get<1>(func_);
  const char *license = "GPL";
  unsigned kern_version = (4 << 16) | (10 << 8) | 13;
  char *log_buf = nullptr;
  unsigned log_buf_size = 0;
  progfd_ = bpf_prog_load(progtype(probe_.type),
      reinterpret_cast<struct bpf_insn*>(insns), prog_len,<commit_msg>Parse uname(2) output to get kernel version code<commit_after>#include <iostream>
#include <sys/utsname.h>
#include <unistd.h>

#include "attached_probe.h"
#include "libbpf.h"
#include "perf_reader.h"

namespace ebpf {
namespace bpftrace {

AttachedProbe::AttachedProbe(Probe &probe, std::tuple<uint8_t *, uintptr_t> &func)
  : probe_(probe), func_(func)
{
  load_prog();
  switch (probe_.type)
  {
    case ProbeType::kprobe:
    case ProbeType::kretprobe:
      attach_kprobe();
      break;
    default:
      abort();
  }
}

AttachedProbe::~AttachedProbe()
{
  perf_reader_free(perf_reader_);
  close(progfd_);
  int err = 0;
  switch (probe_.type)
  {
    case ProbeType::kprobe:
    case ProbeType::kretprobe:
      err = bpf_detach_kprobe(eventname());
      break;
    default:
      abort();
  }
  if (err)
    std::cerr << "Error detaching probe: " << probe_.name << std::endl;
}

const char *AttachedProbe::eventname() const
{
  std::string event;
  switch (probe_.type)
  {
    case ProbeType::kprobe:
      event =  "p_" + probe_.attach_point;
      break;
    case ProbeType::kretprobe:
      event =  "r_" + probe_.attach_point;
      break;
    default:
      abort();
  }
  return event.c_str();
}

static unsigned kernel_version()
{
  struct utsname utsname;
  uname(&utsname);
  unsigned x, y, z;
  sscanf(utsname.release, "%d.%d.%d", &x, &y, &z);
  return (x << 16) + (y << 8) + z;
}

void AttachedProbe::load_prog()
{
  uint8_t *insns = std::get<0>(func_);
  int prog_len = std::get<1>(func_);
  const char *license = "GPL";
  unsigned kern_version = kernel_version();
  char *log_buf = nullptr;
  unsigned log_buf_size = 0;

  progfd_ = bpf_prog_load(progtype(probe_.type),
      reinterpret_cast<struct bpf_insn*>(insns), prog_len,
{'fraction_non_alphanumeric': 0.14749082007343942, 'fraction_numerical': 0.0073439412484700125, 'mean_word_length': 3.6500711237553345, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 16, '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': '1742376', 'n_tokens_mistral': 1264, 'n_tokens_neox': 1235, 'n_words': 248}