@@ -497,6 +497,27 @@ DAP::SendFormattedOutput(OutputType o, const char *format, ...) {
497497 o, llvm::StringRef (buffer, std::min<int >(actual_length, sizeof (buffer))));
498498}
499499
500+ int32_t DAP::CreateSourceReference (lldb::addr_t address) {
501+ std::lock_guard<std::mutex> guard (m_source_references_mutex);
502+ auto iter = llvm::find (m_source_references, address);
503+ if (iter != m_source_references.end ())
504+ return std::distance (m_source_references.begin (), iter) + 1 ;
505+
506+ m_source_references.emplace_back (address);
507+ return static_cast <int32_t >(m_source_references.size ());
508+ }
509+
510+ std::optional<lldb::addr_t > DAP::GetSourceReferenceAddress (int32_t reference) {
511+ std::lock_guard<std::mutex> guard (m_source_references_mutex);
512+ if (reference <= LLDB_DAP_INVALID_SRC_REF)
513+ return std::nullopt ;
514+
515+ if (static_cast <size_t >(reference) > m_source_references.size ())
516+ return std::nullopt ;
517+
518+ return m_source_references[reference - 1 ];
519+ }
520+
500521ExceptionBreakpoint *DAP::GetExceptionBPFromStopReason (lldb::SBThread &thread) {
501522 const auto num = thread.GetStopReasonDataCount ();
502523 // Check to see if have hit an exception breakpoint and change the
@@ -602,6 +623,55 @@ ReplMode DAP::DetectReplMode(lldb::SBFrame frame, std::string &expression,
602623 llvm_unreachable (" enum cases exhausted." );
603624}
604625
626+ std::optional<protocol::Source> DAP::ResolveSource (lldb::SBAddress address) {
627+ if (DisplayAssemblySource (debugger, address))
628+ return ResolveAssemblySource (address);
629+
630+ lldb::SBLineEntry line_entry = GetLineEntryForAddress (target, address);
631+ if (!line_entry.IsValid ())
632+ return std::nullopt ;
633+
634+ return CreateSource (line_entry.GetFileSpec ());
635+ }
636+
637+ std::optional<protocol::Source>
638+ DAP::ResolveAssemblySource (lldb::SBAddress address) {
639+ lldb::SBSymbol symbol = address.GetSymbol ();
640+ lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
641+ std::string name;
642+ if (symbol.IsValid ()) {
643+ load_addr = symbol.GetStartAddress ().GetLoadAddress (target);
644+ name = symbol.GetName ();
645+ } else {
646+ load_addr = address.GetLoadAddress (target);
647+ name = GetLoadAddressString (load_addr);
648+ }
649+
650+ if (load_addr == LLDB_INVALID_ADDRESS)
651+ return std::nullopt ;
652+
653+ protocol::Source source;
654+ source.sourceReference = CreateSourceReference (load_addr);
655+ lldb::SBModule module = address.GetModule ();
656+ if (module .IsValid ()) {
657+ lldb::SBFileSpec file_spec = module .GetFileSpec ();
658+ if (file_spec.IsValid ()) {
659+ std::string path = GetSBFileSpecPath (file_spec);
660+ if (!path.empty ())
661+ source.path = path + ' `' + name;
662+ }
663+ }
664+
665+ source.name = std::move (name);
666+
667+ // Mark the source as deemphasized since users will only be able to view
668+ // assembly for these frames.
669+ source.presentationHint =
670+ protocol::Source::eSourcePresentationHintDeemphasize;
671+
672+ return source;
673+ }
674+
605675bool DAP::RunLLDBCommands (llvm::StringRef prefix,
606676 llvm::ArrayRef<std::string> commands) {
607677 bool required_command_failed = false ;
0 commit comments