cmDebuggerThread.cxx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmDebuggerThread.h"
  4. #include <cstdint>
  5. #include <utility>
  6. #include <cm3p/cppdap/optional.h>
  7. #include <cm3p/cppdap/types.h>
  8. #include "cmDebuggerStackFrame.h"
  9. #include "cmDebuggerVariables.h"
  10. #include "cmDebuggerVariablesHelper.h"
  11. #include "cmDebuggerVariablesManager.h"
  12. namespace cmDebugger {
  13. cmDebuggerThread::cmDebuggerThread(int64_t id, std::string name)
  14. : Id(id)
  15. , Name(std::move(name))
  16. , VariablesManager(std::make_shared<cmDebuggerVariablesManager>())
  17. {
  18. }
  19. void cmDebuggerThread::PushStackFrame(cmMakefile* mf,
  20. std::string const& sourcePath,
  21. cmListFileFunction const& lff)
  22. {
  23. std::unique_lock<std::mutex> lock(Mutex);
  24. Frames.emplace_back(
  25. std::make_shared<cmDebuggerStackFrame>(mf, sourcePath, lff));
  26. FrameMap.insert({ Frames.back()->GetId(), Frames.back() });
  27. }
  28. void cmDebuggerThread::PopStackFrame()
  29. {
  30. std::unique_lock<std::mutex> lock(Mutex);
  31. FrameMap.erase(Frames.back()->GetId());
  32. FrameScopes.erase(Frames.back()->GetId());
  33. FrameVariables.erase(Frames.back()->GetId());
  34. Frames.pop_back();
  35. }
  36. std::shared_ptr<cmDebuggerStackFrame> cmDebuggerThread::GetTopStackFrame()
  37. {
  38. std::unique_lock<std::mutex> lock(Mutex);
  39. if (!Frames.empty()) {
  40. return Frames.back();
  41. }
  42. return {};
  43. }
  44. std::shared_ptr<cmDebuggerStackFrame> cmDebuggerThread::GetStackFrame(
  45. int64_t frameId)
  46. {
  47. std::unique_lock<std::mutex> lock(Mutex);
  48. auto it = FrameMap.find(frameId);
  49. if (it == FrameMap.end()) {
  50. return {};
  51. }
  52. return it->second;
  53. }
  54. dap::ScopesResponse cmDebuggerThread::GetScopesResponse(
  55. int64_t frameId, bool supportsVariableType)
  56. {
  57. std::unique_lock<std::mutex> lock(Mutex);
  58. auto it = FrameScopes.find(frameId);
  59. if (it != FrameScopes.end()) {
  60. dap::ScopesResponse response;
  61. response.scopes = it->second;
  62. return response;
  63. }
  64. auto it2 = FrameMap.find(frameId);
  65. if (it2 == FrameMap.end()) {
  66. return dap::ScopesResponse();
  67. }
  68. std::shared_ptr<cmDebuggerStackFrame> frame = it2->second;
  69. std::shared_ptr<cmDebuggerVariables> localVariables =
  70. cmDebuggerVariablesHelper::Create(VariablesManager, "Locals",
  71. supportsVariableType, frame);
  72. FrameVariables[frameId].emplace_back(localVariables);
  73. dap::Scope scope;
  74. scope.name = localVariables->GetName();
  75. scope.presentationHint = "locals";
  76. scope.variablesReference = localVariables->GetId();
  77. dap::Source source;
  78. source.name = frame->GetFileName();
  79. source.path = source.name;
  80. scope.source = source;
  81. FrameScopes[frameId].push_back(scope);
  82. dap::ScopesResponse response;
  83. response.scopes.push_back(scope);
  84. return response;
  85. }
  86. dap::VariablesResponse cmDebuggerThread::GetVariablesResponse(
  87. dap::VariablesRequest const& request)
  88. {
  89. std::unique_lock<std::mutex> lock(Mutex);
  90. dap::VariablesResponse response;
  91. response.variables = VariablesManager->HandleVariablesRequest(request);
  92. return response;
  93. }
  94. dap::StackTraceResponse GetStackTraceResponse(
  95. std::shared_ptr<cmDebuggerThread> const& thread)
  96. {
  97. dap::StackTraceResponse response;
  98. std::unique_lock<std::mutex> lock(thread->Mutex);
  99. for (int i = static_cast<int>(thread->Frames.size()) - 1; i >= 0; --i) {
  100. dap::Source source;
  101. source.name = thread->Frames[i]->GetFileName();
  102. source.path = source.name;
  103. #ifdef __GNUC__
  104. # pragma GCC diagnostic push
  105. # pragma GCC diagnostic ignored "-Warray-bounds"
  106. #endif
  107. dap::StackFrame stackFrame;
  108. #ifdef __GNUC__
  109. # pragma GCC diagnostic pop
  110. #endif
  111. stackFrame.line = thread->Frames[i]->GetLine();
  112. stackFrame.column = 1;
  113. stackFrame.name = thread->Frames[i]->GetFileName() + " Line " +
  114. std::to_string(stackFrame.line);
  115. stackFrame.id = thread->Frames[i]->GetId();
  116. stackFrame.source = source;
  117. response.stackFrames.push_back(stackFrame);
  118. }
  119. response.totalFrames = response.stackFrames.size();
  120. return response;
  121. }
  122. } // namespace cmDebugger