@@ -974,68 +974,57 @@ Status MinidumpFileBuilder::ReadWriteMemoryInChunks(
974
974
const lldb_private::CoreFileMemoryRange &range, uint64_t &bytes_read) {
975
975
976
976
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, const void *buf, lldb::addr_t current_addr,
980
+ uint64_t bytes_to_read,
981
+ uint64_t bytes_read_for_chunk) -> lldb_private::IterationAction {
990
982
if (error.Fail () || bytes_read_for_chunk == 0 ) {
991
983
LLDB_LOGF (log,
992
984
" Failed to read memory region at: %" PRIx64
993
985
" . Bytes read: %zu, error: %s" ,
994
- addr , bytes_read_for_chunk, error.AsCString ());
986
+ current_addr , bytes_read_for_chunk, error.AsCString ());
995
987
996
988
// If we failed in a memory read, we would normally want to skip
997
989
// 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.
999
991
//
1000
992
// So if we do encounter an error while reading, we just return
1001
993
// immediately, any prior bytes read will still be included but
1002
994
// any bytes partially read before the error are ignored.
1003
- return Status () ;
995
+ return lldb_private::IterationAction::Stop ;
1004
996
}
1005
997
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 (buf, bytes_read_for_chunk);
1004
+ if (addDataError.Fail ())
1005
+ return lldb_private::IterationAction::Stop;
1024
1006
1025
1007
if (bytes_read_for_chunk != bytes_to_read) {
1026
1008
LLDB_LOGF (log,
1027
1009
" Memory region at: %" PRIx64 " partiall read %" PRIx64
1028
1010
" bytes out of %" PRIx64 " bytes." ,
1029
- addr , bytes_read_for_chunk,
1011
+ current_addr , bytes_read_for_chunk,
1030
1012
bytes_to_read - bytes_read_for_chunk);
1031
1013
1032
1014
// If we've read some bytes, we stop trying to read more and return
1033
1015
// this best effort attempt
1034
- break ;
1016
+ return lldb_private::IterationAction::Stop ;
1035
1017
}
1036
- }
1037
1018
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 = m_process_sp->ReadMemoryInChunks (
1026
+ addr, data_buffer.GetBytes (), data_buffer.GetByteSize (), size, callback);
1027
+ return addDataError;
1039
1028
}
1040
1029
1041
1030
static uint64_t
0 commit comments