@@ -69,9 +69,10 @@ Status MinidumpFileBuilder::AddHeaderAndCalculateDirectories() {
6969 m_expected_directories += 9 ;
7070
7171 // Go through all of the threads and check for exceptions.
72- std::vector<lldb::ThreadSP> threads =
73- m_process_sp->CalculateCoreFileThreadList (m_save_core_options);
74- for (const ThreadSP &thread_sp : threads) {
72+ lldb_private::ThreadList thread_list = m_process_sp->GetThreadList ();
73+ const uint32_t num_threads = thread_list.GetSize ();
74+ for (uint32_t thread_idx = 0 ; thread_idx < num_threads; ++thread_idx) {
75+ ThreadSP thread_sp (thread_list.GetThreadAtIndex (thread_idx));
7576 StopInfoSP stop_info_sp = thread_sp->GetStopInfo ();
7677 if (stop_info_sp) {
7778 const StopReason &stop_reason = stop_info_sp->GetStopReason ();
@@ -587,13 +588,12 @@ Status MinidumpFileBuilder::FixThreadStacks() {
587588
588589Status MinidumpFileBuilder::AddThreadList () {
589590 constexpr size_t minidump_thread_size = sizeof (llvm::minidump::Thread);
590- std::vector<ThreadSP> thread_list =
591- m_process_sp->CalculateCoreFileThreadList (m_save_core_options);
591+ lldb_private::ThreadList thread_list = m_process_sp->GetThreadList ();
592592
593593 // size of the entire thread stream consists of:
594594 // number of threads and threads array
595595 size_t thread_stream_size = sizeof (llvm::support::ulittle32_t ) +
596- thread_list.size () * minidump_thread_size;
596+ thread_list.GetSize () * minidump_thread_size;
597597 // save for the ability to set up RVA
598598 size_t size_before = GetCurrentDataEndOffset ();
599599 Status error;
@@ -602,15 +602,17 @@ Status MinidumpFileBuilder::AddThreadList() {
602602 return error;
603603
604604 llvm::support::ulittle32_t thread_count =
605- static_cast <llvm::support::ulittle32_t >(thread_list.size ());
605+ static_cast <llvm::support::ulittle32_t >(thread_list.GetSize ());
606606 m_data.AppendData (&thread_count, sizeof (llvm::support::ulittle32_t ));
607607
608608 // Take the offset after the thread count.
609609 m_thread_list_start = GetCurrentDataEndOffset ();
610610 DataBufferHeap helper_data;
611611
612+ const uint32_t num_threads = thread_list.GetSize ();
612613 Log *log = GetLog (LLDBLog::Object);
613- for (const ThreadSP &thread_sp : thread_list) {
614+ for (uint32_t thread_idx = 0 ; thread_idx < num_threads; ++thread_idx) {
615+ ThreadSP thread_sp (thread_list.GetThreadAtIndex (thread_idx));
614616 RegisterContextSP reg_ctx_sp (thread_sp->GetRegisterContext ());
615617
616618 if (!reg_ctx_sp) {
@@ -648,7 +650,7 @@ Status MinidumpFileBuilder::AddThreadList() {
648650 m_tid_to_reg_ctx[thread_sp->GetID ()] = thread_context_memory_locator;
649651
650652 LLDB_LOGF (log, " AddThreadList for thread %d: thread_context %zu bytes" ,
651- thread_sp-> GetIndexID () , thread_context.size ());
653+ thread_idx , thread_context.size ());
652654 helper_data.AppendData (thread_context.data (), thread_context.size ());
653655
654656 llvm::minidump::Thread t;
@@ -672,10 +674,11 @@ Status MinidumpFileBuilder::AddThreadList() {
672674}
673675
674676Status MinidumpFileBuilder::AddExceptions () {
675- std::vector<ThreadSP> thread_list =
676- m_process_sp->CalculateCoreFileThreadList (m_save_core_options);
677+ lldb_private::ThreadList thread_list = m_process_sp->GetThreadList ();
677678 Status error;
678- for (const ThreadSP &thread_sp : thread_list) {
679+ const uint32_t num_threads = thread_list.GetSize ();
680+ for (uint32_t thread_idx = 0 ; thread_idx < num_threads; ++thread_idx) {
681+ ThreadSP thread_sp (thread_list.GetThreadAtIndex (thread_idx));
679682 StopInfoSP stop_info_sp = thread_sp->GetStopInfo ();
680683 bool add_exception = false ;
681684 if (stop_info_sp) {
@@ -816,7 +819,7 @@ Status MinidumpFileBuilder::AddLinuxFileStreams() {
816819 return error;
817820}
818821
819- Status MinidumpFileBuilder::AddMemoryList () {
822+ Status MinidumpFileBuilder::AddMemoryList (SaveCoreStyle core_style ) {
820823 Status error;
821824
822825 // We first save the thread stacks to ensure they fit in the first UINT32_MAX
@@ -825,26 +828,18 @@ Status MinidumpFileBuilder::AddMemoryList() {
825828 // in accessible with a 32 bit offset.
826829 Process::CoreFileMemoryRanges ranges_32;
827830 Process::CoreFileMemoryRanges ranges_64;
828- Process::CoreFileMemoryRanges all_core_memory_ranges;
829- error = m_process_sp->CalculateCoreFileSaveRanges (m_save_core_options,
830- all_core_memory_ranges);
831+ error = m_process_sp->CalculateCoreFileSaveRanges (
832+ SaveCoreStyle::eSaveCoreStackOnly, ranges_32);
831833 if (error.Fail ())
832834 return error;
833835
834- // Start by saving all of the stacks and ensuring they fit under the 32b
835- // limit.
836+ // Calculate totalsize including the current offset.
836837 uint64_t total_size = GetCurrentDataEndOffset ();
837- auto iterator = all_core_memory_ranges.begin ();
838- while (iterator != all_core_memory_ranges.end ()) {
839- if (m_saved_stack_ranges.count (iterator->range .start ()) > 0 ) {
840- // We don't save stacks twice.
841- ranges_32.push_back (*iterator);
842- total_size +=
843- iterator->range .size () + sizeof (llvm::minidump::MemoryDescriptor);
844- iterator = all_core_memory_ranges.erase (iterator);
845- } else {
846- iterator++;
847- }
838+ total_size += ranges_32.size () * sizeof (llvm::minidump::MemoryDescriptor);
839+ std::unordered_set<addr_t > stack_start_addresses;
840+ for (const auto &core_range : ranges_32) {
841+ stack_start_addresses.insert (core_range.range .start ());
842+ total_size += core_range.range .size ();
848843 }
849844
850845 if (total_size >= UINT32_MAX) {
@@ -854,20 +849,31 @@ Status MinidumpFileBuilder::AddMemoryList() {
854849 return error;
855850 }
856851
852+ Process::CoreFileMemoryRanges all_core_memory_ranges;
853+ if (core_style != SaveCoreStyle::eSaveCoreStackOnly) {
854+ error = m_process_sp->CalculateCoreFileSaveRanges (core_style,
855+ all_core_memory_ranges);
856+ if (error.Fail ())
857+ return error;
858+ }
859+
857860 // After saving the stacks, we start packing as much as we can into 32b.
858861 // We apply a generous padding here so that the Directory, MemoryList and
859862 // Memory64List sections all begin in 32b addressable space.
860863 // Then anything overflow extends into 64b addressable space.
861864 // All core memeroy ranges will either container nothing on stacks only
862865 // or all the memory ranges including stacks
863866 if (!all_core_memory_ranges.empty ())
864- total_size += 256 + (all_core_memory_ranges.size () *
865- sizeof (llvm::minidump::MemoryDescriptor_64));
867+ total_size +=
868+ 256 + (all_core_memory_ranges.size () - stack_start_addresses.size ()) *
869+ sizeof (llvm::minidump::MemoryDescriptor_64);
866870
867871 for (const auto &core_range : all_core_memory_ranges) {
868872 const addr_t range_size = core_range.range .size ();
869- // We don't need to check for stacks here because we already removed them
870- // from all_core_memory_ranges.
873+ if (stack_start_addresses.count (core_range.range .start ()) > 0 )
874+ // Don't double save stacks.
875+ continue ;
876+
871877 if (total_size + range_size < UINT32_MAX) {
872878 ranges_32.push_back (core_range);
873879 total_size += range_size;
0 commit comments