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