[Lldb-commits] [compiler-rt] [openmp] [clang] [flang] [llvm] [lldb] [libcxx] [clang-tools-extra] [OpenMP] Add memory diff dump for kernel record-replay (PR #70667)

Giorgis Georgakoudis via lldb-commits lldb-commits at lists.llvm.org
Sat Nov 4 07:34:23 PDT 2023


================
@@ -151,6 +151,74 @@ struct RecordReplayTy {
     OS.close();
   }
 
+  void dumpDeviceMemoryDiff(StringRef Filename) {
+    ErrorOr<std::unique_ptr<WritableMemoryBuffer>> DeviceMemoryMB =
+        WritableMemoryBuffer::getNewUninitMemBuffer(MemorySize);
+    if (!DeviceMemoryMB)
+      report_fatal_error("Error creating MemoryBuffer for device memory");
+
+    auto Err = Device->dataRetrieve(DeviceMemoryMB.get()->getBufferStart(),
+                                    MemoryStart, MemorySize, nullptr);
+    if (Err)
+      report_fatal_error("Error retrieving data for target pointer");
+
+    // Get the pre-record memory filename
+    SmallString<128> InputFilename = {Filename.split('.').first, ".memory"};
+
+    // Read the pre-record memorydump
+    auto InputFileBuffer = MemoryBuffer::getFileOrSTDIN(InputFilename);
+    if (std::error_code EC = InputFileBuffer.getError())
+      report_fatal_error("Error reading pre-record device memory");
+
+    StringRef InputBufferContents = (*InputFileBuffer)->getBuffer();
+    if (InputBufferContents.size() != MemorySize)
+      report_fatal_error("Error: Pre-record device memory size mismatch");
+
+    std::error_code EC;
+    raw_fd_ostream OS(Filename, EC);
+    if (EC)
+      report_fatal_error("Error dumping memory to file " + Filename + " :" +
+                         EC.message());
+
+    // Get current memory contents
+    StringRef DeviceMemoryContents(DeviceMemoryMB.get()->getBuffer().data(),
+                                   DeviceMemoryMB.get()->getBuffer().size());
+
+    for (size_t I = 0; I < MemorySize; ++I) {
+      // If buffers are same, continue
+      if (InputBufferContents[I] == DeviceMemoryContents[I])
+        continue;
+
+      // If mismatch is found create a new diff line
+      // Current format: location, size, differences
+      OS << I << " "; // Marks the start offset
+
+      SmallVector<uint8_t, 128> Modified;
+      Modified.push_back(DeviceMemoryContents[I]);
+
+      size_t J; // Length of current diff line
+      // Loop until next match is found
+      for (J = 1; J < MemorySize - I; ++J) {
+        // If no more mismatch, break out of the loop
+        if (InputBufferContents[I + J] == DeviceMemoryContents[I + J])
+          break;
+
+        // If mismatch continues - push diff to Modified
+        Modified.push_back(DeviceMemoryContents[I + J]);
+      }
+
+      OS << J << " "; // Marks the length of the mismatching sequence
+      for (const auto &value : Modified)
+        OS << value << " ";
+      OS << "\n";
+
+      // Increment I by J to skip ahead to next
+      // matching sequence in the buffer
+      I += J;
----------------
ggeorgakoudis wrote:

Simpler, more readable as:
```
for (I+=1; I < MemorySize; ++I) {
        // If no more mismatch, break out of the loop
        if (InputBufferContents[I] == DeviceMemoryContents[I])
          break;
        // If mismatch continues - push diff to Modified
        Modified.push_back(DeviceMemoryContents[I]);
      }
...
OS << Modified.size() << " ";
```
Avoids the skip-ahead increment too

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


More information about the lldb-commits mailing list