module DEBUGGER__::DAP_TraceInspector::Custom_ThreadClient

def custom_dap_request_rdbgTraceInspector(req)

def custom_dap_request_rdbgTraceInspector(req)
  cmd = req.dig('arguments', 'command')
  case cmd
  when 'record'
    process_record_cmd(req)
  else
    raise "Unknown command #{cmd}"
  end
end

def process_record_cmd(req)

def process_record_cmd(req)
  cmd = req.dig('arguments', 'subCommand')
  case cmd
  when 'enable'
    size = req.dig('arguments', 'maxLogSize')
    @recorder = Custom_Recorder.new max_log_size: size
    @recorder.enable
    event! :protocol_result, :rdbgTraceInspector, req
  when 'disable'
    if @recorder&.enabled?
      @recorder.disable
    end
    @recorder = nil
    event! :protocol_result, :rdbgTraceInspector, req
  when 'collect'
    logs = []
    log_index = nil
    trace_cnt = 0
    unless @recorder.nil?
      log_index = @recorder.log_index
      @recorder.log.each{|frames|
        crt_frame = frames[0]
        log = {
          name: crt_frame.name,
          location: {
            path: crt_frame.location.path,
            line: crt_frame.location.lineno,
          },
          depth: crt_frame.frame_depth
        }
        if params = crt_frame.iseq_parameters_info
          log[:parameters] = params
        end
        if return_str = crt_frame.return_str
          log[:returnValue] = return_str
        end
        logs << log
      }
      trace_cnt = @recorder.dropped_trace_cnt
      @recorder.dropped_trace_cnt = 0
    end
    event! :protocol_result, :rdbgTraceInspector, req, logs: logs, stoppedIndex: log_index, dropped_trace_cnt: trace_cnt
  else
    raise "Unknown command #{cmd}"
  end
end