[Lldb-commits] [lldb] [lldb] Introduce ScriptedFrameProvider for real threads (PR #161870)

Med Ismail Bennani via lldb-commits lldb-commits at lists.llvm.org
Tue Oct 7 05:07:16 PDT 2025


================
@@ -1439,13 +1444,133 @@ void Thread::CalculateExecutionContext(ExecutionContext &exe_ctx) {
 StackFrameListSP Thread::GetStackFrameList() {
   std::lock_guard<std::recursive_mutex> guard(m_frame_mutex);
 
-  if (!m_curr_frames_sp)
-    m_curr_frames_sp =
-        std::make_shared<StackFrameList>(*this, m_prev_frames_sp, true);
+  if (!m_curr_frames_sp) {
+    
+    StackFrameListSP real_frames_sp =
+          std::make_shared<StackFrameList>(*this, m_prev_frames_sp, true);
+    
+    if (m_frame_provider_sp) {
+      lldb::ScriptedFrameProviderMergeStrategy strategy =
+          m_frame_provider_sp->GetMergeStrategy();
+      
+      auto scripted_list_or_err = GetScriptedFrameList();
+      if (!scripted_list_or_err) {
+        LLDB_LOG_ERROR(GetLog(LLDBLog::Thread),
+                       scripted_list_or_err.takeError(),
+                       "Failed to get scripted frame list: {0}");
+        m_curr_frames_sp = real_frames_sp;
+        return m_curr_frames_sp;
+      }
+      
+      StackFrameListSP scripted_frames_sp = *scripted_list_or_err;
+      uint32_t num_real = real_frames_sp->GetNumFrames(true);
+      uint32_t num_scripted = scripted_frames_sp->GetNumFrames(false);
+
+      switch (strategy) {
+      case lldb::eScriptedFrameProviderMergeStrategyReplace: {
+        m_curr_frames_sp = *scripted_list_or_err;
+        return m_curr_frames_sp;
+      }
+
+      case lldb::eScriptedFrameProviderMergeStrategyReplaceByIndex: {
+        // Create normal frame list first
+        for (uint32_t i = 0; i < num_scripted; i++) {
+          StackFrameSP scripted_frame =
+              scripted_frames_sp->GetFrameAtIndex(i);
+          if (scripted_frame) {
+            uint32_t frame_idx = scripted_frame->GetFrameIndex();
+            m_curr_frames_sp->SetFrameAtIndex(frame_idx, scripted_frame);
+          }
+        }
+        return m_curr_frames_sp;
+      }
+
+      case lldb::eScriptedFrameProviderMergeStrategyPrepend: {
+        // Add scripted frames first (indices 0 to num_scripted-1)
+        for (uint32_t i = 0; i < num_scripted; i++) {
+          StackFrameSP scripted_frame = scripted_frames_sp->GetFrameAtIndex(i);
+          if (scripted_frame) {
+            // Create new frame with adjusted index
+            m_curr_frames_sp->SetFrameAtIndex(i, scripted_frame);
+          }
+        }
+
+        // Add real frames after scripted frames (shifted indices)
+        for (uint32_t i = 0; i < num_real; i++) {
+          StackFrameSP real_frame = real_frames_sp->GetFrameAtIndex(i);
----------------
medismailben wrote:

I'm still working on the CPython Frame Provider but the idea is to iterate over all the frames in the current thread and save the the frame indices for frames coming from the CPython module and since the script would be implemented from python, I was going to use some `inspect` API to unwind the stack and replace the CPython frames by the python interpreter ones.

https://github.com/llvm/llvm-project/pull/161870


More information about the lldb-commits mailing list