@@ -962,6 +962,68 @@ bool ReplModeRequestHandler::DoExecute(lldb::SBDebugger debugger,
962962 return true ;
963963}
964964
965+ // Sends a DAP event with an optional body.
966+ //
967+ // See
968+ // https://code.visualstudio.com/api/references/vscode-api#debug.onDidReceiveDebugSessionCustomEvent
969+ bool SendEventRequestHandler::DoExecute (lldb::SBDebugger debugger,
970+ char **command,
971+ lldb::SBCommandReturnObject &result) {
972+ // Command format like: `send-event <name> <body>?`
973+ if (!command || !command[0 ] || llvm::StringRef (command[0 ]).empty ()) {
974+ result.SetError (" Not enough arguments found, expected format "
975+ " `lldb-dap send-event <name> <body>?`." );
976+ return false ;
977+ }
978+
979+ llvm::StringRef name{command[0 ]};
980+ // Events that are stateful and should be handled by lldb-dap internally.
981+ const std::array internal_events{" breakpoint" , " capabilities" , " continued" ,
982+ " exited" , " initialize" , " loadedSource" ,
983+ " module" , " process" , " stopped" ,
984+ " terminated" , " thread" };
985+ if (std::find (internal_events.begin (), internal_events.end (), name) !=
986+ std::end (internal_events)) {
987+ std::string msg =
988+ llvm::formatv (" Invalid use of lldb-dap send-event, event \" {0}\" "
989+ " should be handled by lldb-dap internally." ,
990+ name)
991+ .str ();
992+ result.SetError (msg.c_str ());
993+ return false ;
994+ }
995+
996+ llvm::json::Object event (CreateEventObject (name));
997+
998+ if (command[1 ] && !llvm::StringRef (command[1 ]).empty ()) {
999+ // See if we have unused arguments.
1000+ if (command[2 ]) {
1001+ result.SetError (
1002+ " Additional arguments found, expected `lldb-dap send-event "
1003+ " <name> <body>?`." );
1004+ return false ;
1005+ }
1006+
1007+ llvm::StringRef raw_body{command[1 ]};
1008+
1009+ llvm::Expected<llvm::json::Value> body = llvm::json::parse (raw_body);
1010+
1011+ if (!body) {
1012+ llvm::Error err = body.takeError ();
1013+ std::string msg = " Failed to parse custom event body: " +
1014+ llvm::toString (std::move (err));
1015+ result.SetError (msg.c_str ());
1016+ return false ;
1017+ }
1018+
1019+ event.try_emplace (" body" , std::move (*body));
1020+ }
1021+
1022+ g_dap.SendJSON (llvm::json::Value (std::move (event)));
1023+ result.SetStatus (lldb::eReturnStatusSuccessFinishNoResult);
1024+ return true ;
1025+ }
1026+
9651027void DAP::SetFrameFormat (llvm::StringRef format) {
9661028 if (format.empty ())
9671029 return ;
0 commit comments