@@ -65,7 +65,7 @@ DAP::DAP(std::string name, llvm::StringRef path, std::ofstream *log,
65
65
lldb::IOObjectSP input, lldb::IOObjectSP output, ReplMode repl_mode,
66
66
std::vector<std::string> pre_init_commands)
67
67
: name(std::move(name)), debug_adapter_path(path), log(log),
68
- input ( std::move(input)), output( std::move(output)),
68
+ transport ( this ->name, std::move(input), std::move(output)),
69
69
broadcaster(" lldb-dap" ), exception_breakpoints(),
70
70
pre_init_commands(std::move(pre_init_commands)),
71
71
focus_tid(LLDB_INVALID_THREAD_ID), stop_at_entry(false ), is_attach(false ),
@@ -237,65 +237,25 @@ void DAP::StopEventHandlers() {
237
237
}
238
238
}
239
239
240
- // Send the JSON in "json_str" to the "out" stream. Correctly send the
241
- // "Content-Length:" field followed by the length, followed by the raw
242
- // JSON bytes.
243
- void DAP::SendJSON (const std::string &json_str) {
244
- output.write_full (" Content-Length: " );
245
- output.write_full (llvm::utostr (json_str.size ()));
246
- output.write_full (" \r\n\r\n " );
247
- output.write_full (json_str);
248
- }
249
-
250
240
// Serialize the JSON value into a string and send the JSON packet to
251
241
// the "out" stream.
252
242
void DAP::SendJSON (const llvm::json::Value &json) {
253
- std::string json_str;
254
- llvm::raw_string_ostream strm (json_str);
255
- strm << json;
256
- static std::mutex mutex;
257
- std::lock_guard<std::mutex> locker (mutex);
258
- SendJSON (json_str);
259
-
260
- if (log) {
261
- auto now = std::chrono::duration<double >(
262
- std::chrono::system_clock::now ().time_since_epoch ());
263
- *log << llvm::formatv (" {0:f9} {1} <-- " , now.count (), name).str ()
264
- << std::endl
265
- << " Content-Length: " << json_str.size () << " \r\n\r\n "
266
- << llvm::formatv (" {0:2}" , json).str () << std::endl;
267
- }
268
- }
269
-
270
- // Read a JSON packet from the "in" stream.
271
- std::string DAP::ReadJSON () {
272
- std::string length_str;
273
- std::string json_str;
274
- int length;
275
-
276
- if (!input.read_expected (log, " Content-Length: " ))
277
- return json_str;
278
-
279
- if (!input.read_line (log, length_str))
280
- return json_str;
281
-
282
- if (!llvm::to_integer (length_str, length))
283
- return json_str;
284
-
285
- if (!input.read_expected (log, " \r\n " ))
286
- return json_str;
287
-
288
- if (!input.read_full (log, length, json_str))
289
- return json_str;
290
-
291
- if (log) {
292
- auto now = std::chrono::duration<double >(
293
- std::chrono::system_clock::now ().time_since_epoch ());
294
- *log << llvm::formatv (" {0:f9} {1} --> " , now.count (), name).str ()
295
- << std::endl
296
- << " Content-Length: " << length << " \r\n\r\n " ;
243
+ protocol::Message M;
244
+ llvm::json::Path::Root root;
245
+ if (!protocol::fromJSON (json, M, root)) {
246
+ if (log) {
247
+ std::string error;
248
+ llvm::raw_string_ostream OS (error);
249
+ root.printErrorContext (json, OS);
250
+ *log << " encoding failure: " << error << " \n " ;
251
+ }
252
+ return ;
297
253
}
298
- return json_str;
254
+ auto status = transport.Write (log, M);
255
+ if (status.Fail () && log)
256
+ *log << llvm::formatv (" failed to send {0}: {1}\n " , llvm::json::Value (M),
257
+ status.AsCString ())
258
+ .str ();
299
259
}
300
260
301
261
// "OutputEvent": {
@@ -722,40 +682,9 @@ void DAP::SetTarget(const lldb::SBTarget target) {
722
682
}
723
683
}
724
684
725
- PacketStatus DAP::GetNextObject (llvm::json::Object &object) {
726
- std::string json = ReadJSON ();
727
- if (json.empty ())
728
- return PacketStatus::EndOfFile;
729
-
730
- llvm::StringRef json_sref (json);
731
- llvm::Expected<llvm::json::Value> json_value = llvm::json::parse (json_sref);
732
- if (!json_value) {
733
- auto error = json_value.takeError ();
734
- if (log) {
735
- std::string error_str;
736
- llvm::raw_string_ostream strm (error_str);
737
- strm << error;
738
- *log << " error: failed to parse JSON: " << error_str << std::endl
739
- << json << std::endl;
740
- }
741
- return PacketStatus::JSONMalformed;
742
- }
743
-
744
- if (log) {
745
- *log << llvm::formatv (" {0:2}" , *json_value).str () << std::endl;
746
- }
747
-
748
- llvm::json::Object *object_ptr = json_value->getAsObject ();
749
- if (!object_ptr) {
750
- if (log)
751
- *log << " error: json packet isn't a object" << std::endl;
752
- return PacketStatus::JSONNotObject;
753
- }
754
- object = *object_ptr;
755
- return PacketStatus::Success;
756
- }
757
-
758
- bool DAP::HandleObject (const llvm::json::Object &object) {
685
+ bool DAP::HandleObject (const protocol::Message &M) {
686
+ llvm::json::Value v = toJSON (M);
687
+ llvm::json::Object object = *v.getAsObject ();
759
688
const auto packet_type = GetString (object, " type" );
760
689
if (packet_type == " request" ) {
761
690
const auto command = GetString (object, " command" );
@@ -858,25 +787,16 @@ lldb::SBError DAP::Disconnect(bool terminateDebuggee) {
858
787
}
859
788
860
789
llvm::Error DAP::Loop () {
861
- auto cleanup = llvm::make_scope_exit ([this ]() {
862
- if (output.descriptor )
863
- output.descriptor ->Close ();
864
- StopEventHandlers ();
865
- });
790
+ auto cleanup = llvm::make_scope_exit ([this ]() { StopEventHandlers (); });
866
791
while (!disconnecting) {
867
- llvm::json::Object object ;
868
- lldb_dap::PacketStatus status = GetNextObject (object );
792
+ protocol::Message next ;
793
+ auto status = transport. Read (log, next );
869
794
870
- if (status == lldb_dap::PacketStatus::EndOfFile) {
871
- break ;
872
- }
873
-
874
- if (status != lldb_dap::PacketStatus::Success) {
875
- return llvm::createStringError (llvm::inconvertibleErrorCode (),
876
- " failed to send packet" );
795
+ if (status.Fail ()) {
796
+ return status.takeError ();
877
797
}
878
798
879
- if (!HandleObject (object )) {
799
+ if (!HandleObject (next )) {
880
800
return llvm::createStringError (llvm::inconvertibleErrorCode (),
881
801
" unhandled packet" );
882
802
}
0 commit comments