<commit_before># frozen-string-literal: true
require 'open3'

class Gdbdump
  # GDB.new(pid: 999, debug: true).run do |gdb|
  #   puts gdb.cmd_exec('bt')
  # end
  class GDB
    COMMAND_READ_BUFFER_SIZE = 1024
    SUDO_CMD = 'sudo'

    def initialize(pid:, debug: false, gdbinit: nil, gdb: nil, ruby: nil)
      @pid = pid.to_s
      @debug = debug
      @gdb = gdb || 'gdb'
      @ruby = ruby || Procfs.new(@pid).exe
      @gdbinit = gdbinit || File.join(ROOT, 'vendor', 'ruby', ruby_minor_version, 'gdbinit')
      @exec_options = [SUDO_CMD, @gdb, '-silent', '-nw', '-x', @gdbinit, @ruby, @pid]
    end

    private def ruby_version
      `#{@ruby} -e 'puts RUBY_VERSION'`.chomp
    end

    private def ruby_minor_version
      ruby_version.split('.')[0,2].join('.')
    end

    def print_backtrace
      run do |gdb|
        out, err = gdb.cmd_exec('rb_ps')
        $stdout.puts out
        $stderr.puts err unless err.empty?
      end
      # run do |gdb|
      #   gdb.cmd_exec('call write(2, "== c backtrace ==\n", 18)')
      #   gdb.cmd_exec('call rb_print_backtrace()')
      #   gdb.cmd_exec('call write(2, "== ruby backtrace ==\n", 21)')
      #   gdb.cmd_exec('call rb_backtrace()')
      # end<commit_msg>Enhance error messages<commit_after># frozen-string-literal: true
require 'open3'

class Gdbdump
  # GDB.new(pid: 999, debug: true).run do |gdb|
  #   puts gdb.cmd_exec('bt')
  # end
  class GDB
    COMMAND_READ_BUFFER_SIZE = 1024
    SUDO_CMD = 'sudo'

    def initialize(pid:, debug: false, gdbinit: nil, gdb: nil, ruby: nil)
      @pid = pid.to_s
      @debug = debug
      @gdb = gdb || 'gdb'
      @ruby = (ruby || Procfs.new(@pid).exe).tap do |path|
        raise "ruby #{path} is not accessible" unless File.executable?(path)
      end
      @gdbinit = (gdbinit || File.join(ROOT, 'vendor', 'ruby', ruby_minor_version, 'gdbinit')).tap do |path|
        raise "gdbinit #{path} is not readable" unless File.readable?(path)
      end
      @exec_options = [SUDO_CMD, @gdb, '-silent', '-nw', '-x', @gdbinit, @ruby, @pid]
    end

    private def ruby_version
      `#{@ruby} -e 'puts RUBY_VERSION'`.chomp
    end

    private def ruby_minor_version
      ruby_version.split('.')[0,2].join('.')
    end

    def print_backtrace
      run do |gdb|
        out, err = gdb.cmd_exec('rb_ps')
        $stdout.puts out
        $stderr.puts err unless err.empty?
      end
      # run do |gdb|
      #   gdb.cmd_exec('call write(2, "== c backtrace ==\n", 18)')
      #   gdb.cmd_exec('call rb_print_backtrace()')
      #   gdb.cmd_exec('call write(2, "== ruby backtrace ==\n", 21)')
      #   gdb.cmd_exec('call rb_backtrace()')
      # end
{'fraction_non_alphanumeric': 0.15813253012048192, 'fraction_numerical': 0.012048192771084338, 'mean_word_length': 2.644718792866941, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7176464', 'n_tokens_mistral': 1064, 'n_tokens_neox': 999, 'n_words': 237}