Module: MilkTea::DAP::Server::ServerLaunch

Included in:
MilkTea::DAP::Server
Defined in:
lib/milk_tea/dap/server/launch.rb

Instance Method Summary collapse

Instance Method Details

#backend_start_arguments(command, arguments, resolved) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/milk_tea/dap/server/launch.rb', line 155

def backend_start_arguments(command, arguments, resolved)
  rewritten = arguments.dup

  rewritten.delete("backend")
  rewritten.delete("adapterPath")

  if resolved[:runnable_path]
    rewritten["program"] = resolved[:runnable_path]
  elsif rewritten["program"].to_s.empty?
    rewritten.delete("program")
  end

  rewritten["stopOnEntry"] = true if command == "launch"

  rewritten["cwd"] = File.expand_path(rewritten["cwd"]) if rewritten["cwd"].is_a?(String) && !rewritten["cwd"].empty?
  rewritten["coreFile"] = File.expand_path(rewritten["coreFile"]) if rewritten["coreFile"].is_a?(String) && !rewritten["coreFile"].empty?
  rewritten
end

#continue_runtime_if_pausedObject



234
235
236
237
238
239
240
241
242
243
# File 'lib/milk_tea/dap/server/launch.rb', line 234

def continue_runtime_if_paused
  pid, paused = @runtime_mutex.synchronize { [@runtime_pid, @runtime_paused] }
  return unless paused
  return if pid.nil?

  Process.kill("CONT", pid)
  @runtime_mutex.synchronize { @runtime_paused = false }
rescue StandardError
  nil
end

#default_backend_kindObject



108
109
110
# File 'lib/milk_tea/dap/server/launch.rb', line 108

def default_backend_kind
  @preferred_backend_kind
end

#maybe_start_or_stop_on_entryObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/milk_tea/dap/server/launch.rb', line 83

def maybe_start_or_stop_on_entry
  return if using_lldb_backend?

  return unless @session.launched?
  return unless @session.configuration_done?
  return if @session.runtime_started?

  if @session.stop_on_entry?
    return if @session.entry_stop_emitted?

    write_stopped("entry")
    @session.mark_entry_stop_emitted!
  else
    start_runtime_if_needed
  end
end

#pause_runtime_if_runningObject



223
224
225
226
227
228
229
230
231
232
# File 'lib/milk_tea/dap/server/launch.rb', line 223

def pause_runtime_if_running
  pid = @runtime_mutex.synchronize { @runtime_pid }
  return false if pid.nil?

  Process.kill("STOP", pid)
  @runtime_mutex.synchronize { @runtime_paused = true }
  true
rescue StandardError
  false
end

#request_start(message) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/milk_tea/dap/server/launch.rb', line 7

def request_start(message)
  if @session.launched?
    write_error_response(message, "Program already launched")
    return
  end

  args = message["arguments"] || {}
  if message["command"] == "launch" && args["program"].to_s.empty?
    write_error_response(message, "launch requires a non-empty 'program' argument")
    return
  end

  resolved = resolve_debug_program(message["command"], args["program"])
  unless resolved[:ok]
    write_error_response(message, resolved[:error])
    return
  end

  program_args = args["args"] || []
  unless program_args.is_a?(Array)
    write_error_response(message, "launch 'args' must be an array when provided")
    return
  end

  no_debug = message["command"] == "launch" && args["noDebug"] == true
  stop_on_entry = no_debug ? false : (args.key?("stopOnEntry") ? !!args["stopOnEntry"] : true)
  backend_kind = if no_debug
                   "process"
                 else
                   resolved_backend_kind(args["backend"])
                 end
  unless backend_kind
    write_error_response(message, "unsupported backend: #{args['backend']}")
    return
  end

  @session.request_start!(
    program_path: args["program"].to_s.empty? ? nil : args["program"],
    runnable_path: resolved[:runnable_path],
    program_args:,
    stop_on_entry:,
    backend_kind:
  )
  @session.launch!
  @backend_auto_continue_after_configuration = using_lldb_backend? && message["command"] == "launch" && !stop_on_entry
  @backend_stopped_thread_id = nil if using_lldb_backend?
  @last_forwarded_continued_signature = nil
  load_debug_map(resolved[:runnable_path])

  if using_lldb_backend?
    start_lldb_backend(args)
    reset_backend_configuration_ready
    init_response = ensure_backend_initialized({})
    return write_backend_response(message, init_response) unless init_response["success"]

    # Merge backend capabilities with adapter capabilities and notify the client.
    merged_caps = effective_adapter_capabilities
    write_event("capabilities", merged_caps)

    launch_arguments = backend_start_arguments(message["command"], args, resolved)
    @breakpoints_synced_to_backend = false
    @function_breakpoints_synced_to_backend = false
    @exception_breakpoints_synced_to_backend = false
    start_backend_start_request(message, launch_arguments)
    return
  end

  write_response(message, {})
  write_event("process", {
    name: File.basename(@session.program_path.to_s),
    startMethod: "launch",
    isLocalProcess: true
  })
  maybe_start_or_stop_on_entry
end

#resolve_debug_program(command, program) ⇒ Object



146
147
148
149
150
151
152
153
# File 'lib/milk_tea/dap/server/launch.rb', line 146

def resolve_debug_program(command, program)
  return resolve_launch_program(program) if command == "launch"

  path = program.to_s
  return { ok: true, runnable_path: nil } if path.empty?

  resolve_launch_program(path)
end

#resolve_launch_program(program) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/milk_tea/dap/server/launch.rb', line 120

def resolve_launch_program(program)
  path = program.to_s
  return { ok: false, error: "launch requires a non-empty 'program' argument" } if path.empty?

  expanded = File.expand_path(path)
  if expanded.end_with?(".mt")
    return { ok: false, error: "Milk Tea program not found: #{expanded}" } unless File.file?(expanded)

    tmp_dir = Dir.mktmpdir("milk-tea-dap")
    @tmp_dirs << tmp_dir
    output_path = File.join(tmp_dir, File.basename(expanded, ".mt"))

    begin
      Build.build(expanded, output_path:, debug: true, debug_guards: false)
    rescue BuildError => e
      return { ok: false, error: e.message }
    end

    return { ok: true, runnable_path: output_path }
  end

  return { ok: false, error: "Program not found: #{expanded}" } unless File.file?(expanded)

  { ok: true, runnable_path: expanded }
end

#resolved_backend_kind(requested_backend) ⇒ Object



112
113
114
115
116
117
118
# File 'lib/milk_tea/dap/server/launch.rb', line 112

def resolved_backend_kind(requested_backend)
  backend = requested_backend.to_s.strip
  backend = default_backend_kind if backend.empty?
  return backend if backend == "process" || backend == "lldb-dap"

  nil
end

#start_runtime_if_neededObject



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/milk_tea/dap/server/launch.rb', line 174

def start_runtime_if_needed
  return if @session.runtime_started?
  return unless @session.runnable_path

  @session.mark_runtime_started!
  runnable_path = @session.runnable_path
  command = [runnable_path, *@session.program_args]
  chdir = File.dirname(File.expand_path(@session.program_path || runnable_path))

  Thread.new do
    Open3.popen3(*command, chdir:) do |stdin, stdout, stderr, wait_thr|
      stdin.close
      @runtime_mutex.synchronize { @runtime_pid = wait_thr.pid }

      out_thread = Thread.new do
        stdout.each_line do |line|
          write_event("output", { category: "stdout", output: line })
        end
      end

      err_thread = Thread.new do
        stderr.each_line do |line|
          write_event("output", { category: "stderr", output: line })
        end
      end

      out_thread.join
      err_thread.join

      status = wait_thr.value
      exit_code = status.exited? ? status.exitstatus : 1
      @session.terminate!

      write_event("terminated")
      write_event("exited", { exitCode: exit_code })
    end
  rescue StandardError => e
    @session.terminate!
    write_event("output", { category: "stderr", output: "DAP runtime error: #{e.message}\n" })
    write_event("terminated")
    write_event("exited", { exitCode: 1 })
  ensure
    @runtime_mutex.synchronize do
      @runtime_pid = nil
      @runtime_paused = false
    end
  end
end

#terminate_runtime_if_runningObject



245
246
247
248
249
250
251
252
253
# File 'lib/milk_tea/dap/server/launch.rb', line 245

def terminate_runtime_if_running
  pid = @runtime_mutex.synchronize { @runtime_pid }
  return if pid.nil?

  Process.kill("TERM", pid)
  @runtime_mutex.synchronize { @runtime_paused = false }
rescue StandardError
  nil
end

#write_stopped(reason) ⇒ Object



100
101
102
103
104
105
106
# File 'lib/milk_tea/dap/server/launch.rb', line 100

def write_stopped(reason)
  write_event("stopped", {
    reason: reason,
    threadId: @session.thread_id,
    allThreadsStopped: true
  })
end