| 308 | |
| 309 | |
| 310 | bool LldbAdapter::Connect(const std::string& server, std::uint32_t port) |
| 311 | { |
| 312 | m_debugger.SetAsync(true); |
| 313 | |
| 314 | std::thread thread([&]() { EventListener(); }); |
| 315 | thread.detach(); |
| 316 | |
| 317 | SBError err; |
| 318 | |
| 319 | // *Attempt* to create a functional target triple for the binary. |
| 320 | // This allows attaching to fat binaries. If the triple is empty, it will still attach on thin binaries. |
| 321 | auto archName = lldbArchNameForBinaryNinjaArchName(m_defaultArchitecture); |
| 322 | std::string triple = ""; |
| 323 | if (!archName.empty()) |
| 324 | triple = archName + "-unknown-none"; |
| 325 | |
| 326 | m_target = m_debugger.CreateTarget(m_originalFileName.c_str(), triple.c_str(), "", true, err); |
| 327 | |
| 328 | if (!m_target.IsValid()) |
| 329 | { |
| 330 | // It is likely lldb did not like our target triple. |
| 331 | if (err.GetCString() && std::string(err.GetCString()).find("is not compatible with") != std::string::npos) |
| 332 | { |
| 333 | // Last-ditch effort. If it is a thin binary, we will be able to attach without passing a triple. |
| 334 | m_target = m_debugger.CreateTarget(m_originalFileName.c_str(), "", "", true, err); |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | if (!m_target.IsValid()) |
| 339 | { |
| 340 | DebuggerEvent event; |
| 341 | event.type = LaunchFailureEventType; |
| 342 | event.data.errorData.shortError = fmt::format("LLDB failed to connect to target."); |
| 343 | event.data.errorData.error = |
| 344 | fmt::format("LLDB failed to connect to target with \"{}\"", err.GetCString() ? err.GetCString() : ""); |
| 345 | PostDebuggerEvent(event); |
| 346 | return false; |
| 347 | } |
| 348 | |
| 349 | m_targetActive = true; |
| 350 | ApplyBreakpoints(); |
| 351 | |
| 352 | if (Settings::Instance()->Get<bool>("debugger.stopAtEntryPoint") && m_hasEntryFunction) |
| 353 | AddBreakpoint(ModuleNameAndOffset(m_originalFileName, m_entryPoint - m_start)); |
| 354 | |
| 355 | std::string url = fmt::format("connect://{}:{}", server, port); |
| 356 | SBListener listener; |
| 357 | const char* plugin = nullptr; |
| 358 | if (!m_processPlugin.empty() && m_processPlugin != "debugserver/lldb") |
| 359 | plugin = m_processPlugin.c_str(); |
| 360 | m_process = m_target.ConnectRemote(listener, url.c_str(), plugin, err); |
| 361 | if (!m_process.IsValid() || (m_process.GetState() == StateType::eStateInvalid) || err.Fail()) |
| 362 | { |
| 363 | DebuggerEvent event; |
| 364 | event.type = LaunchFailureEventType; |
| 365 | event.data.errorData.shortError = fmt::format("LLDB failed to connect to target."); |
| 366 | event.data.errorData.error = |
| 367 | fmt::format("LLDB Failed to connect to target with \"{}\"", err.GetCString() ? err.GetCString() : ""); |
nothing calls this directly
no test coverage detected