@@ -974,68 +974,57 @@ Status MinidumpFileBuilder::ReadWriteMemoryInChunks(
974974 const lldb_private::CoreFileMemoryRange &range, uint64_t &bytes_read) {
975975
976976 Log *log = GetLog (LLDBLog::Object);
977- const lldb::addr_t addr = range.range .start ();
978- const lldb::addr_t size = range.range .size ();
979-
980- uint64_t bytes_remaining = size;
981- Status error;
982- while (bytes_remaining > 0 ) {
983- // Get the next read chunk size as the minimum of the remaining bytes and
984- // the write chunk max size.
985- const size_t bytes_to_read =
986- std::min (bytes_remaining, data_buffer.GetByteSize ());
987- const size_t bytes_read_for_chunk =
988- m_process_sp->ReadMemory (range.range .start () + bytes_read,
989- data_buffer.GetBytes (), bytes_to_read, error);
977+ Status addDataError;
978+ Process::ReadMemoryChunkCallback callback =
979+ [&](Status &error, DataBufferHeap &data, lldb::addr_t current_addr,
980+ uint64_t bytes_to_read,
981+ uint64_t bytes_read_for_chunk) -> lldb_private::IterationAction {
990982 if (error.Fail () || bytes_read_for_chunk == 0 ) {
991983 LLDB_LOGF (log,
992984 " Failed to read memory region at: %" PRIx64
993985 " . Bytes read: %zu, error: %s" ,
994- addr , bytes_read_for_chunk, error.AsCString ());
986+ current_addr , bytes_read_for_chunk, error.AsCString ());
995987
996988 // If we failed in a memory read, we would normally want to skip
997989 // this entire region, if we had already written to the minidump
998- // file, we can't easily rewind the state.
990+ // file, we can't easily rewind that state.
999991 //
1000992 // So if we do encounter an error while reading, we just return
1001993 // immediately, any prior bytes read will still be included but
1002994 // any bytes partially read before the error are ignored.
1003- return Status () ;
995+ return lldb_private::IterationAction::Stop ;
1004996 }
1005997
1006- // Write to the minidump file with the chunk potentially flushing to disk.
1007- // this is the only place we want to return a true error, so that we can
1008- // fail. If we get an error writing to disk we can't easily gaurauntee
1009- // that we won't corrupt the minidump.
1010- error = AddData (data_buffer.GetBytes (), bytes_read_for_chunk);
1011- if (error.Fail ())
1012- return error;
1013-
1014- // If the bytes read in this chunk would cause us to overflow, something
1015- // went wrong and we should fail out of creating the Minidump.
1016- if (bytes_read_for_chunk > bytes_remaining)
1017- return Status::FromErrorString (" Unexpected number of bytes read." );
1018- else
1019- bytes_remaining -= bytes_read_for_chunk;
1020-
1021- // Update the caller with the number of bytes read, but also written to the
1022- // underlying buffer.
1023- bytes_read += bytes_read_for_chunk;
998+ // Write to the minidump file with the chunk potentially flushing to
999+ // disk.
1000+ // This error will be captured by the outer scope and is considered fatal.
1001+ // If we get an error writing to disk we can't easily guarauntee that we
1002+ // won't corrupt the minidump.
1003+ addDataError = AddData (data_buffer.GetBytes (), bytes_read_for_chunk);
1004+ if (addDataError.Fail ())
1005+ return lldb_private::IterationAction::Stop;
10241006
10251007 if (bytes_read_for_chunk != bytes_to_read) {
10261008 LLDB_LOGF (log,
10271009 " Memory region at: %" PRIx64 " partiall read %" PRIx64
10281010 " bytes out of %" PRIx64 " bytes." ,
1029- addr , bytes_read_for_chunk,
1011+ current_addr , bytes_read_for_chunk,
10301012 bytes_to_read - bytes_read_for_chunk);
10311013
10321014 // If we've read some bytes, we stop trying to read more and return
10331015 // this best effort attempt
1034- break ;
1016+ return lldb_private::IterationAction::Stop ;
10351017 }
1036- }
10371018
1038- return error;
1019+ // No problems, keep going!
1020+ return lldb_private::IterationAction::Continue;
1021+ };
1022+
1023+ const lldb::addr_t addr = range.range .start ();
1024+ const lldb::addr_t size = range.range .size ();
1025+ bytes_read =
1026+ m_process_sp->ReadMemoryInChunks (addr, data_buffer, size, callback);
1027+ return addDataError;
10391028}
10401029
10411030static uint64_t
0 commit comments